net/bnxt: support tunnel offload
[dpdk.git] / drivers / net / bnxt / tf_ulp / bnxt_ulp.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2019-2021 Broadcom
3  * All rights reserved.
4  */
5
6 #include <rte_log.h>
7 #include <rte_malloc.h>
8 #include <rte_flow.h>
9 #include <rte_flow_driver.h>
10 #include <rte_tailq.h>
11 #include <rte_spinlock.h>
12
13 #include "bnxt.h"
14 #include "bnxt_ulp.h"
15 #include "bnxt_tf_common.h"
16 #include "tf_core.h"
17 #include "tf_ext_flow_handle.h"
18
19 #include "ulp_template_db_enum.h"
20 #include "ulp_template_struct.h"
21 #include "ulp_mark_mgr.h"
22 #include "ulp_fc_mgr.h"
23 #include "ulp_flow_db.h"
24 #include "ulp_mapper.h"
25 #include "ulp_port_db.h"
26 #include "ulp_tun.h"
27 #include "ulp_ha_mgr.h"
28 #include "bnxt_tf_pmd_shim.h"
29
30 /* Linked list of all TF sessions. */
31 STAILQ_HEAD(, bnxt_ulp_session_state) bnxt_ulp_session_list =
32                         STAILQ_HEAD_INITIALIZER(bnxt_ulp_session_list);
33
34 /* Mutex to synchronize bnxt_ulp_session_list operations. */
35 static pthread_mutex_t bnxt_ulp_global_mutex = PTHREAD_MUTEX_INITIALIZER;
36
37 /* Spin lock to protect context global list */
38 rte_spinlock_t bnxt_ulp_ctxt_lock;
39 TAILQ_HEAD(cntx_list_entry_list, ulp_context_list_entry);
40 static struct cntx_list_entry_list ulp_cntx_list =
41         TAILQ_HEAD_INITIALIZER(ulp_cntx_list);
42
43 /* Static function declarations */
44 static int32_t bnxt_ulp_cntxt_list_init(void);
45 static int32_t bnxt_ulp_cntxt_list_add(struct bnxt_ulp_context *ulp_ctx);
46 static void bnxt_ulp_cntxt_list_del(struct bnxt_ulp_context *ulp_ctx);
47
48 /*
49  * Allow the deletion of context only for the bnxt device that
50  * created the session.
51  */
52 bool
53 ulp_ctx_deinit_allowed(struct bnxt_ulp_context *ulp_ctx)
54 {
55         if (!ulp_ctx || !ulp_ctx->cfg_data)
56                 return false;
57
58         if (!ulp_ctx->cfg_data->ref_cnt) {
59                 BNXT_TF_DBG(DEBUG, "ulp ctx shall initiate deinit\n");
60                 return true;
61         }
62
63         return false;
64 }
65
66 static int32_t
67 bnxt_ulp_devid_get(struct bnxt *bp,
68                    enum bnxt_ulp_device_id  *ulp_dev_id)
69 {
70         if (BNXT_CHIP_P5(bp)) {
71                 *ulp_dev_id = BNXT_ULP_DEVICE_ID_THOR;
72                 return 0;
73         }
74
75         if (BNXT_STINGRAY(bp))
76                 *ulp_dev_id = BNXT_ULP_DEVICE_ID_STINGRAY;
77         else
78                 /* Assuming Whitney */
79                 *ulp_dev_id = BNXT_ULP_DEVICE_ID_WH_PLUS;
80
81         return 0;
82 }
83
84 struct bnxt_ulp_app_capabilities_info *
85 bnxt_ulp_app_cap_list_get(uint32_t *num_entries)
86 {
87         if (!num_entries)
88                 return NULL;
89         *num_entries = BNXT_ULP_APP_CAP_TBL_MAX_SZ;
90         return ulp_app_cap_info_list;
91 }
92
93 static struct bnxt_ulp_resource_resv_info *
94 bnxt_ulp_app_resource_resv_list_get(uint32_t *num_entries)
95 {
96         if (num_entries == NULL)
97                 return NULL;
98         *num_entries = BNXT_ULP_APP_RESOURCE_RESV_LIST_MAX_SZ;
99         return ulp_app_resource_resv_list;
100 }
101
102 struct bnxt_ulp_resource_resv_info *
103 bnxt_ulp_resource_resv_list_get(uint32_t *num_entries)
104 {
105         if (!num_entries)
106                 return NULL;
107         *num_entries = BNXT_ULP_RESOURCE_RESV_LIST_MAX_SZ;
108         return ulp_resource_resv_list;
109 }
110
111 struct bnxt_ulp_glb_resource_info *
112 bnxt_ulp_app_glb_resource_info_list_get(uint32_t *num_entries)
113 {
114         if (!num_entries)
115                 return NULL;
116         *num_entries = BNXT_ULP_APP_GLB_RESOURCE_TBL_MAX_SZ;
117         return ulp_app_glb_resource_tbl;
118 }
119
120 static int32_t
121 bnxt_ulp_named_resources_calc(struct bnxt_ulp_context *ulp_ctx,
122                               struct bnxt_ulp_glb_resource_info *info,
123                               uint32_t num,
124                               struct tf_session_resources *res)
125 {
126         uint32_t dev_id = BNXT_ULP_DEVICE_ID_LAST, res_type, i;
127         enum tf_dir dir;
128         uint8_t app_id;
129         int32_t rc = 0;
130
131         if (ulp_ctx == NULL || info == NULL || res == NULL || num == 0) {
132                 BNXT_TF_DBG(ERR, "Invalid parms to named resources calc.\n");
133                 return -EINVAL;
134         }
135
136         rc = bnxt_ulp_cntxt_app_id_get(ulp_ctx, &app_id);
137         if (rc) {
138                 BNXT_TF_DBG(ERR, "Unable to get the app id from ulp.\n");
139                 return -EINVAL;
140         }
141
142         rc = bnxt_ulp_cntxt_dev_id_get(ulp_ctx, &dev_id);
143         if (rc) {
144                 BNXT_TF_DBG(ERR, "Unable to get the dev id from ulp.\n");
145                 return -EINVAL;
146         }
147
148         for (i = 0; i < num; i++) {
149                 if (dev_id != info[i].device_id || app_id != info[i].app_id)
150                         continue;
151                 dir = info[i].direction;
152                 res_type = info[i].resource_type;
153
154                 switch (info[i].resource_func) {
155                 case BNXT_ULP_RESOURCE_FUNC_IDENTIFIER:
156                         res->ident_cnt[dir].cnt[res_type]++;
157                         break;
158                 case BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE:
159                         res->tbl_cnt[dir].cnt[res_type]++;
160                         break;
161                 case BNXT_ULP_RESOURCE_FUNC_TCAM_TABLE:
162                         res->tcam_cnt[dir].cnt[res_type]++;
163                         break;
164                 case BNXT_ULP_RESOURCE_FUNC_EM_TABLE:
165                         res->em_cnt[dir].cnt[res_type]++;
166                         break;
167                 default:
168                         BNXT_TF_DBG(ERR, "Unknown resource func (0x%x)\n,",
169                                     info[i].resource_func);
170                         continue;
171                 }
172         }
173
174         return 0;
175 }
176
177 static int32_t
178 bnxt_ulp_unnamed_resources_calc(struct bnxt_ulp_context *ulp_ctx,
179                                 struct bnxt_ulp_resource_resv_info *info,
180                                 uint32_t num,
181                                 struct tf_session_resources *res)
182 {
183         uint32_t dev_id, res_type, i;
184         enum tf_dir dir;
185         uint8_t app_id;
186         int32_t rc = 0;
187
188         if (ulp_ctx == NULL || res == NULL || info == NULL || num == 0) {
189                 BNXT_TF_DBG(ERR, "Invalid arguments to get resources.\n");
190                 return -EINVAL;
191         }
192
193         rc = bnxt_ulp_cntxt_app_id_get(ulp_ctx, &app_id);
194         if (rc) {
195                 BNXT_TF_DBG(ERR, "Unable to get the app id from ulp.\n");
196                 return -EINVAL;
197         }
198
199         rc = bnxt_ulp_cntxt_dev_id_get(ulp_ctx, &dev_id);
200         if (rc) {
201                 BNXT_TF_DBG(ERR, "Unable to get the dev id from ulp.\n");
202                 return -EINVAL;
203         }
204
205         for (i = 0; i < num; i++) {
206                 if (app_id != info[i].app_id || dev_id != info[i].device_id)
207                         continue;
208                 dir = info[i].direction;
209                 res_type = info[i].resource_type;
210
211                 switch (info[i].resource_func) {
212                 case BNXT_ULP_RESOURCE_FUNC_IDENTIFIER:
213                         res->ident_cnt[dir].cnt[res_type] = info[i].count;
214                         break;
215                 case BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE:
216                         res->tbl_cnt[dir].cnt[res_type] = info[i].count;
217                         break;
218                 case BNXT_ULP_RESOURCE_FUNC_TCAM_TABLE:
219                         res->tcam_cnt[dir].cnt[res_type] = info[i].count;
220                         break;
221                 case BNXT_ULP_RESOURCE_FUNC_EM_TABLE:
222                         res->em_cnt[dir].cnt[res_type] = info[i].count;
223                         break;
224                 default:
225                         break;
226                 }
227         }
228         return 0;
229 }
230
231 static int32_t
232 bnxt_ulp_tf_resources_get(struct bnxt_ulp_context *ulp_ctx,
233                           struct tf_session_resources *res)
234 {
235         struct bnxt_ulp_resource_resv_info *unnamed = NULL;
236         uint32_t unum;
237         int32_t rc = 0;
238
239         if (ulp_ctx == NULL || res == NULL) {
240                 BNXT_TF_DBG(ERR, "Invalid arguments to get resources.\n");
241                 return -EINVAL;
242         }
243
244         unnamed = bnxt_ulp_resource_resv_list_get(&unum);
245         if (unnamed == NULL) {
246                 BNXT_TF_DBG(ERR, "Unable to get resource resv list.\n");
247                 return -EINVAL;
248         }
249
250         rc = bnxt_ulp_unnamed_resources_calc(ulp_ctx, unnamed, unum, res);
251         if (rc)
252                 BNXT_TF_DBG(ERR, "Unable to calc resources for session.\n");
253
254         return rc;
255 }
256
257 static int32_t
258 bnxt_ulp_tf_shared_session_resources_get(struct bnxt_ulp_context *ulp_ctx,
259                                          struct tf_session_resources *res)
260 {
261         struct bnxt_ulp_resource_resv_info *unnamed;
262         struct bnxt_ulp_glb_resource_info *named;
263         uint32_t unum, nnum;
264         int32_t rc;
265
266         if (ulp_ctx == NULL || res == NULL) {
267                 BNXT_TF_DBG(ERR, "Invalid arguments to get resources.\n");
268                 return -EINVAL;
269         }
270
271         /* Make sure the resources are zero before accumulating. */
272         memset(res, 0, sizeof(struct tf_session_resources));
273
274         /*
275          * Shared resources are comprised of both named and unnamed resources.
276          * First get the unnamed counts, and then add the named to the result.
277          */
278         /* Get the baseline counts */
279         unnamed = bnxt_ulp_app_resource_resv_list_get(&unum);
280         if (unnamed == NULL) {
281                 BNXT_TF_DBG(ERR, "Unable to get shared resource resv list.\n");
282                 return -EINVAL;
283         }
284         rc = bnxt_ulp_unnamed_resources_calc(ulp_ctx, unnamed, unum, res);
285         if (rc) {
286                 BNXT_TF_DBG(ERR, "Unable to calc resources for shared session.\n");
287                 return -EINVAL;
288         }
289
290         /* Get the named list and add the totals */
291         named = bnxt_ulp_app_glb_resource_info_list_get(&nnum);
292         if (named == NULL) {
293                 BNXT_TF_DBG(ERR, "Unable to get app global resource list\n");
294                 return -EINVAL;
295         }
296         rc = bnxt_ulp_named_resources_calc(ulp_ctx, named, nnum, res);
297         if (rc)
298                 BNXT_TF_DBG(ERR, "Unable to calc named resources\n");
299
300         return rc;
301 }
302
303 int32_t
304 bnxt_ulp_cntxt_app_caps_init(struct bnxt_ulp_context *ulp_ctx,
305                              uint8_t app_id, uint32_t dev_id)
306 {
307         struct bnxt_ulp_app_capabilities_info *info;
308         uint32_t num = 0;
309         uint16_t i;
310         bool found = false;
311
312         if (ULP_APP_DEV_UNSUPPORTED_ENABLED(ulp_ctx->cfg_data->ulp_flags)) {
313                 BNXT_TF_DBG(ERR, "APP ID %d, Device ID: 0x%x not supported.\n",
314                             app_id, dev_id);
315                 return -EINVAL;
316         }
317
318         info = bnxt_ulp_app_cap_list_get(&num);
319         if (!info || !num) {
320                 BNXT_TF_DBG(ERR, "Failed to get app capabilities.\n");
321                 return -EINVAL;
322         }
323
324         for (i = 0; i < num; i++) {
325                 if (info[i].app_id != app_id || info[i].device_id != dev_id)
326                         continue;
327                 found = true;
328                 if (info[i].flags & BNXT_ULP_APP_CAP_SHARED_EN)
329                         ulp_ctx->cfg_data->ulp_flags |=
330                                 BNXT_ULP_SHARED_SESSION_ENABLED;
331                 if (info[i].flags & BNXT_ULP_APP_CAP_HOT_UPGRADE_EN)
332                         ulp_ctx->cfg_data->ulp_flags |=
333                                 BNXT_ULP_HIGH_AVAIL_ENABLED;
334                 if (info[i].flags & BNXT_ULP_APP_CAP_UNICAST_ONLY)
335                         ulp_ctx->cfg_data->ulp_flags |=
336                                 BNXT_ULP_APP_UNICAST_ONLY;
337         }
338         if (!found) {
339                 BNXT_TF_DBG(ERR, "APP ID %d, Device ID: 0x%x not supported.\n",
340                             app_id, dev_id);
341                 ulp_ctx->cfg_data->ulp_flags |= BNXT_ULP_APP_DEV_UNSUPPORTED;
342                 return -EINVAL;
343         }
344
345         return 0;
346 }
347
348 static void
349 ulp_ctx_shared_session_close(struct bnxt *bp,
350                              struct bnxt_ulp_session_state *session)
351 {
352         struct tf *tfp;
353         int32_t rc;
354
355         if (!bnxt_ulp_cntxt_shared_session_enabled(bp->ulp_ctx))
356                 return;
357
358         tfp = bnxt_ulp_cntxt_shared_tfp_get(bp->ulp_ctx);
359         if (!tfp) {
360                 /*
361                  * Log it under debug since this is likely a case of the
362                  * shared session not being created.  For example, a failed
363                  * initialization.
364                  */
365                 BNXT_TF_DBG(DEBUG, "Failed to get shared tfp on close.\n");
366                 return;
367         }
368         rc = tf_close_session(tfp);
369         if (rc)
370                 BNXT_TF_DBG(ERR, "Failed to close the shared session rc=%d.\n",
371                             rc);
372         (void)bnxt_ulp_cntxt_shared_tfp_set(bp->ulp_ctx, NULL);
373
374         session->g_shared_tfp.session = NULL;
375 }
376
377 static int32_t
378 ulp_ctx_shared_session_open(struct bnxt *bp,
379                             struct bnxt_ulp_session_state *session)
380 {
381         struct rte_eth_dev *ethdev = bp->eth_dev;
382         struct tf_session_resources *resources;
383         struct tf_open_session_parms parms;
384         size_t copy_nbytes;
385         uint32_t ulp_dev_id = BNXT_ULP_DEVICE_ID_LAST;
386         int32_t rc = 0;
387         uint8_t app_id;
388
389         /* only perform this if shared session is enabled. */
390         if (!bnxt_ulp_cntxt_shared_session_enabled(bp->ulp_ctx))
391                 return 0;
392
393         memset(&parms, 0, sizeof(parms));
394
395         rc = rte_eth_dev_get_name_by_port(ethdev->data->port_id,
396                                           parms.ctrl_chan_name);
397         if (rc) {
398                 BNXT_TF_DBG(ERR, "Invalid port %d, rc = %d\n",
399                             ethdev->data->port_id, rc);
400                 return rc;
401         }
402         resources = &parms.resources;
403
404         /*
405          * Need to account for size of ctrl_chan_name and 1 extra for Null
406          * terminator
407          */
408         copy_nbytes = sizeof(parms.ctrl_chan_name) -
409                 strlen(parms.ctrl_chan_name) - 1;
410
411         /*
412          * Build the ctrl_chan_name with shared token.
413          * When HA is enabled, the WC TCAM needs extra management by the core,
414          * so add the wc_tcam string to the control channel.
415          */
416         if (bnxt_ulp_cntxt_ha_enabled(bp->ulp_ctx))
417                 strncat(parms.ctrl_chan_name, "-tf_shared-wc_tcam",
418                         copy_nbytes);
419         else
420                 strncat(parms.ctrl_chan_name, "-tf_shared", copy_nbytes);
421
422         rc = bnxt_ulp_tf_shared_session_resources_get(bp->ulp_ctx, resources);
423         if (rc)
424                 return rc;
425
426         rc = bnxt_ulp_cntxt_app_id_get(bp->ulp_ctx, &app_id);
427         if (rc) {
428                 BNXT_TF_DBG(ERR, "Unable to get the app id from ulp.\n");
429                 return -EINVAL;
430         }
431
432         rc = bnxt_ulp_cntxt_dev_id_get(bp->ulp_ctx, &ulp_dev_id);
433         if (rc) {
434                 BNXT_TF_DBG(ERR, "Unable to get device id from ulp.\n");
435                 return rc;
436         }
437
438         switch (ulp_dev_id) {
439         case BNXT_ULP_DEVICE_ID_WH_PLUS:
440                 parms.device_type = TF_DEVICE_TYPE_WH;
441                 break;
442         case BNXT_ULP_DEVICE_ID_STINGRAY:
443                 parms.device_type = TF_DEVICE_TYPE_SR;
444                 break;
445         case BNXT_ULP_DEVICE_ID_THOR:
446                 parms.device_type = TF_DEVICE_TYPE_THOR;
447                 break;
448         default:
449                 BNXT_TF_DBG(ERR, "Unable to determine dev for opening session.\n");
450                 return rc;
451         }
452
453         parms.shadow_copy = true;
454         parms.bp = bp;
455         if (app_id == 0 || app_id == 3)
456                 parms.wc_num_slices = TF_WC_TCAM_2_SLICE_PER_ROW;
457         else
458                 parms.wc_num_slices = TF_WC_TCAM_1_SLICE_PER_ROW;
459
460         /*
461          * Open the session here, but the collect the resources during the
462          * mapper initialization.
463          */
464         rc = tf_open_session(&bp->tfp_shared, &parms);
465         if (rc)
466                 return rc;
467
468         if (parms.shared_session_creator)
469                 BNXT_TF_DBG(DEBUG, "Shared session creator.\n");
470         else
471                 BNXT_TF_DBG(DEBUG, "Shared session attached.\n");
472
473         /* Save the shared session in global data */
474         if (!session->g_shared_tfp.session)
475                 session->g_shared_tfp.session = bp->tfp_shared.session;
476
477         rc = bnxt_ulp_cntxt_shared_tfp_set(bp->ulp_ctx, &bp->tfp_shared);
478         if (rc)
479                 BNXT_TF_DBG(ERR, "Failed to add shared tfp to ulp (%d)\n", rc);
480
481         return rc;
482 }
483
484 static int32_t
485 ulp_ctx_shared_session_attach(struct bnxt *bp,
486                               struct bnxt_ulp_session_state *session)
487 {
488         int32_t rc = 0;
489
490         /* Simply return success if shared session not enabled */
491         if (bnxt_ulp_cntxt_shared_session_enabled(bp->ulp_ctx)) {
492                 bp->tfp_shared.session = session->g_shared_tfp.session;
493                 rc = ulp_ctx_shared_session_open(bp, session);
494         }
495
496         return rc;
497 }
498
499 static void
500 ulp_ctx_shared_session_detach(struct bnxt *bp)
501 {
502         if (bnxt_ulp_cntxt_shared_session_enabled(bp->ulp_ctx)) {
503                 if (bp->tfp_shared.session) {
504                         tf_close_session(&bp->tfp_shared);
505                         bp->tfp_shared.session = NULL;
506                 }
507         }
508 }
509
510 /*
511  * Initialize an ULP session.
512  * An ULP session will contain all the resources needed to support rte flow
513  * offloads. A session is initialized as part of rte_eth_device start.
514  * A single vswitch instance can have multiple uplinks which means
515  * rte_eth_device start will be called for each of these devices.
516  * ULP session manager will make sure that a single ULP session is only
517  * initialized once. Apart from this, it also initializes MARK database,
518  * EEM table & flow database. ULP session manager also manages a list of
519  * all opened ULP sessions.
520  */
521 static int32_t
522 ulp_ctx_session_open(struct bnxt *bp,
523                      struct bnxt_ulp_session_state *session)
524 {
525         struct rte_eth_dev              *ethdev = bp->eth_dev;
526         int32_t                         rc = 0;
527         struct tf_open_session_parms    params;
528         struct tf_session_resources     *resources;
529         uint32_t ulp_dev_id = BNXT_ULP_DEVICE_ID_LAST;
530         uint8_t app_id;
531
532         memset(&params, 0, sizeof(params));
533
534         rc = rte_eth_dev_get_name_by_port(ethdev->data->port_id,
535                                           params.ctrl_chan_name);
536         if (rc) {
537                 BNXT_TF_DBG(ERR, "Invalid port %d, rc = %d\n",
538                             ethdev->data->port_id, rc);
539                 return rc;
540         }
541
542         params.shadow_copy = true;
543
544         rc = bnxt_ulp_cntxt_app_id_get(bp->ulp_ctx, &app_id);
545         if (rc) {
546                 BNXT_TF_DBG(ERR, "Unable to get the app id from ulp.\n");
547                 return -EINVAL;
548         }
549
550         rc = bnxt_ulp_cntxt_dev_id_get(bp->ulp_ctx, &ulp_dev_id);
551         if (rc) {
552                 BNXT_TF_DBG(ERR, "Unable to get device id from ulp.\n");
553                 return rc;
554         }
555
556         switch (ulp_dev_id) {
557         case BNXT_ULP_DEVICE_ID_WH_PLUS:
558                 params.device_type = TF_DEVICE_TYPE_WH;
559                 break;
560         case BNXT_ULP_DEVICE_ID_STINGRAY:
561                 params.device_type = TF_DEVICE_TYPE_SR;
562                 break;
563         case BNXT_ULP_DEVICE_ID_THOR:
564                 params.device_type = TF_DEVICE_TYPE_THOR;
565                 break;
566         default:
567                 BNXT_TF_DBG(ERR, "Unable to determine device for opening session.\n");
568                 return rc;
569         }
570
571         resources = &params.resources;
572         rc = bnxt_ulp_tf_resources_get(bp->ulp_ctx, resources);
573         if (rc)
574                 return rc;
575
576         params.bp = bp;
577         if (app_id == 0 || app_id == 3)
578                 params.wc_num_slices = TF_WC_TCAM_2_SLICE_PER_ROW;
579         else
580                 params.wc_num_slices = TF_WC_TCAM_1_SLICE_PER_ROW;
581
582         rc = tf_open_session(&bp->tfp, &params);
583         if (rc) {
584                 BNXT_TF_DBG(ERR, "Failed to open TF session - %s, rc = %d\n",
585                             params.ctrl_chan_name, rc);
586                 return -EINVAL;
587         }
588         if (!session->session_opened) {
589                 session->session_opened = 1;
590                 session->g_tfp = rte_zmalloc("bnxt_ulp_session_tfp",
591                                              sizeof(struct tf), 0);
592                 session->g_tfp->session = bp->tfp.session;
593         }
594         return rc;
595 }
596
597 /*
598  * Close the ULP session.
599  * It takes the ulp context pointer.
600  */
601 static void
602 ulp_ctx_session_close(struct bnxt *bp,
603                       struct bnxt_ulp_session_state *session)
604 {
605         /* close the session in the hardware */
606         if (session->session_opened)
607                 tf_close_session(&bp->tfp);
608         session->session_opened = 0;
609         rte_free(session->g_tfp);
610         session->g_tfp = NULL;
611 }
612
613 static void
614 bnxt_init_tbl_scope_parms(struct bnxt *bp,
615                           struct tf_alloc_tbl_scope_parms *params)
616 {
617         struct bnxt_ulp_device_params   *dparms;
618         uint32_t dev_id;
619         int rc;
620
621         rc = bnxt_ulp_cntxt_dev_id_get(bp->ulp_ctx, &dev_id);
622         if (rc)
623                 /* TBD: For now, just use default. */
624                 dparms = 0;
625         else
626                 dparms = bnxt_ulp_device_params_get(dev_id);
627
628         /*
629          * Set the flush timer for EEM entries. The value is in 100ms intervals,
630          * so 100 is 10s.
631          */
632         params->hw_flow_cache_flush_timer = 100;
633
634         if (!dparms) {
635                 params->rx_max_key_sz_in_bits = BNXT_ULP_DFLT_RX_MAX_KEY;
636                 params->rx_max_action_entry_sz_in_bits =
637                         BNXT_ULP_DFLT_RX_MAX_ACTN_ENTRY;
638                 params->rx_mem_size_in_mb = BNXT_ULP_DFLT_RX_MEM;
639                 params->rx_num_flows_in_k = BNXT_ULP_RX_NUM_FLOWS;
640
641                 params->tx_max_key_sz_in_bits = BNXT_ULP_DFLT_TX_MAX_KEY;
642                 params->tx_max_action_entry_sz_in_bits =
643                         BNXT_ULP_DFLT_TX_MAX_ACTN_ENTRY;
644                 params->tx_mem_size_in_mb = BNXT_ULP_DFLT_TX_MEM;
645                 params->tx_num_flows_in_k = BNXT_ULP_TX_NUM_FLOWS;
646         } else {
647                 params->rx_max_key_sz_in_bits = BNXT_ULP_DFLT_RX_MAX_KEY;
648                 params->rx_max_action_entry_sz_in_bits =
649                         BNXT_ULP_DFLT_RX_MAX_ACTN_ENTRY;
650                 params->rx_mem_size_in_mb = BNXT_ULP_DFLT_RX_MEM;
651                 params->rx_num_flows_in_k =
652                         dparms->ext_flow_db_num_entries / 1024;
653
654                 params->tx_max_key_sz_in_bits = BNXT_ULP_DFLT_TX_MAX_KEY;
655                 params->tx_max_action_entry_sz_in_bits =
656                         BNXT_ULP_DFLT_TX_MAX_ACTN_ENTRY;
657                 params->tx_mem_size_in_mb = BNXT_ULP_DFLT_TX_MEM;
658                 params->tx_num_flows_in_k =
659                         dparms->ext_flow_db_num_entries / 1024;
660         }
661         BNXT_TF_DBG(INFO, "Table Scope initialized with %uK flows.\n",
662                     params->rx_num_flows_in_k);
663 }
664
665 /* Initialize Extended Exact Match host memory. */
666 static int32_t
667 ulp_eem_tbl_scope_init(struct bnxt *bp)
668 {
669         struct tf_alloc_tbl_scope_parms params = {0};
670         struct bnxt_ulp_device_params *dparms;
671         enum bnxt_ulp_flow_mem_type mtype;
672         uint32_t dev_id;
673         int rc;
674
675         /* Get the dev specific number of flows that needed to be supported. */
676         if (bnxt_ulp_cntxt_dev_id_get(bp->ulp_ctx, &dev_id)) {
677                 BNXT_TF_DBG(ERR, "Invalid device id\n");
678                 return -EINVAL;
679         }
680
681         dparms = bnxt_ulp_device_params_get(dev_id);
682         if (!dparms) {
683                 BNXT_TF_DBG(ERR, "could not fetch the device params\n");
684                 return -ENODEV;
685         }
686
687         if (bnxt_ulp_cntxt_mem_type_get(bp->ulp_ctx, &mtype))
688                 return -EINVAL;
689         if (mtype != BNXT_ULP_FLOW_MEM_TYPE_EXT) {
690                 BNXT_TF_DBG(INFO, "Table Scope alloc is not required\n");
691                 return 0;
692         }
693
694         bnxt_init_tbl_scope_parms(bp, &params);
695         rc = tf_alloc_tbl_scope(&bp->tfp, &params);
696         if (rc) {
697                 BNXT_TF_DBG(ERR, "Unable to allocate eem table scope rc = %d\n",
698                             rc);
699                 return rc;
700         }
701 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG
702         BNXT_TF_DBG(DEBUG, "TableScope=0x%0x %d\n",
703                     params.tbl_scope_id,
704                     params.tbl_scope_id);
705 #endif
706         rc = bnxt_ulp_cntxt_tbl_scope_id_set(bp->ulp_ctx, params.tbl_scope_id);
707         if (rc) {
708                 BNXT_TF_DBG(ERR, "Unable to set table scope id\n");
709                 return rc;
710         }
711
712         return 0;
713 }
714
715 /* Free Extended Exact Match host memory */
716 static int32_t
717 ulp_eem_tbl_scope_deinit(struct bnxt *bp, struct bnxt_ulp_context *ulp_ctx)
718 {
719         struct tf_free_tbl_scope_parms  params = {0};
720         struct tf                       *tfp;
721         int32_t                         rc = 0;
722         struct bnxt_ulp_device_params *dparms;
723         enum bnxt_ulp_flow_mem_type mtype;
724         uint32_t dev_id;
725
726         if (!ulp_ctx || !ulp_ctx->cfg_data)
727                 return -EINVAL;
728
729         tfp = bnxt_ulp_cntxt_tfp_get(ulp_ctx, BNXT_ULP_SHARED_SESSION_NO);
730         if (!tfp) {
731                 BNXT_TF_DBG(ERR, "Failed to get the truflow pointer\n");
732                 return -EINVAL;
733         }
734
735         /* Get the dev specific number of flows that needed to be supported. */
736         if (bnxt_ulp_cntxt_dev_id_get(bp->ulp_ctx, &dev_id)) {
737                 BNXT_TF_DBG(ERR, "Invalid device id\n");
738                 return -EINVAL;
739         }
740
741         dparms = bnxt_ulp_device_params_get(dev_id);
742         if (!dparms) {
743                 BNXT_TF_DBG(ERR, "could not fetch the device params\n");
744                 return -ENODEV;
745         }
746
747         if (bnxt_ulp_cntxt_mem_type_get(ulp_ctx, &mtype))
748                 return -EINVAL;
749         if (mtype != BNXT_ULP_FLOW_MEM_TYPE_EXT) {
750                 BNXT_TF_DBG(INFO, "Table Scope free is not required\n");
751                 return 0;
752         }
753
754         rc = bnxt_ulp_cntxt_tbl_scope_id_get(ulp_ctx, &params.tbl_scope_id);
755         if (rc) {
756                 BNXT_TF_DBG(ERR, "Failed to get the table scope id\n");
757                 return -EINVAL;
758         }
759
760         rc = tf_free_tbl_scope(tfp, &params);
761         if (rc) {
762                 BNXT_TF_DBG(ERR, "Unable to free table scope\n");
763                 return -EINVAL;
764         }
765         return rc;
766 }
767
768 /* The function to free and deinit the ulp context data. */
769 static int32_t
770 ulp_ctx_deinit(struct bnxt *bp,
771                struct bnxt_ulp_session_state *session)
772 {
773         /* close the tf session */
774         ulp_ctx_session_close(bp, session);
775
776         /* The shared session must be closed last. */
777         ulp_ctx_shared_session_close(bp, session);
778
779         /* Free the contents */
780         if (session->cfg_data) {
781                 rte_free(session->cfg_data);
782                 bp->ulp_ctx->cfg_data = NULL;
783                 session->cfg_data = NULL;
784         }
785         return 0;
786 }
787
788 /* The function to allocate and initialize the ulp context data. */
789 static int32_t
790 ulp_ctx_init(struct bnxt *bp,
791              struct bnxt_ulp_session_state *session)
792 {
793         struct bnxt_ulp_data    *ulp_data;
794         int32_t                 rc = 0;
795         enum bnxt_ulp_device_id devid;
796
797         /* Initialize the context entries list */
798         bnxt_ulp_cntxt_list_init();
799
800         /* Add the context to the context entries list */
801         rc = bnxt_ulp_cntxt_list_add(bp->ulp_ctx);
802         if (rc) {
803                 BNXT_TF_DBG(ERR, "Failed to add the context list entry\n");
804                 return -ENOMEM;
805         }
806
807         /* Allocate memory to hold ulp context data. */
808         ulp_data = rte_zmalloc("bnxt_ulp_data",
809                                sizeof(struct bnxt_ulp_data), 0);
810         if (!ulp_data) {
811                 BNXT_TF_DBG(ERR, "Failed to allocate memory for ulp data\n");
812                 return -ENOMEM;
813         }
814
815         /* Increment the ulp context data reference count usage. */
816         bp->ulp_ctx->cfg_data = ulp_data;
817         session->cfg_data = ulp_data;
818         ulp_data->ref_cnt++;
819         ulp_data->ulp_flags |= BNXT_ULP_VF_REP_ENABLED;
820
821         rc = bnxt_ulp_devid_get(bp, &devid);
822         if (rc) {
823                 BNXT_TF_DBG(ERR, "Unable to determine device for ULP init.\n");
824                 goto error_deinit;
825         }
826
827         rc = bnxt_ulp_cntxt_dev_id_set(bp->ulp_ctx, devid);
828         if (rc) {
829                 BNXT_TF_DBG(ERR, "Unable to set device for ULP init.\n");
830                 goto error_deinit;
831         }
832
833         /* TODO: For now we are overriding to APP:1 on this branch*/
834         bp->app_id = 1;
835         rc = bnxt_ulp_cntxt_app_id_set(bp->ulp_ctx, bp->app_id);
836         if (rc) {
837                 BNXT_TF_DBG(ERR, "Unable to set app_id for ULP init.\n");
838                 goto error_deinit;
839         }
840
841         rc = bnxt_ulp_cntxt_app_caps_init(bp->ulp_ctx, bp->app_id, devid);
842         if (rc) {
843                 BNXT_TF_DBG(ERR, "Unable to set caps for app(%x)/dev(%x)\n",
844                             bp->app_id, devid);
845                 goto error_deinit;
846         }
847
848         /*
849          * Shared session must be created before first regular session but after
850          * the ulp_ctx is valid.
851          */
852         rc = ulp_ctx_shared_session_open(bp, session);
853         if (rc) {
854                 BNXT_TF_DBG(ERR, "Unable to open shared session (%d)\n", rc);
855                 goto error_deinit;
856         }
857
858         /* Open the ulp session. */
859         rc = ulp_ctx_session_open(bp, session);
860         if (rc)
861                 goto error_deinit;
862
863         bnxt_ulp_cntxt_tfp_set(bp->ulp_ctx, &bp->tfp);
864         return rc;
865
866 error_deinit:
867         session->session_opened = 1;
868         (void)ulp_ctx_deinit(bp, session);
869         return rc;
870 }
871
872 /* The function to initialize ulp dparms with devargs */
873 static int32_t
874 ulp_dparms_init(struct bnxt *bp, struct bnxt_ulp_context *ulp_ctx)
875 {
876         struct bnxt_ulp_device_params *dparms;
877         uint32_t dev_id = BNXT_ULP_DEVICE_ID_LAST;
878
879         if (!bp->max_num_kflows) {
880                 /* Defaults to Internal */
881                 bnxt_ulp_cntxt_mem_type_set(ulp_ctx,
882                                             BNXT_ULP_FLOW_MEM_TYPE_INT);
883                 return 0;
884         }
885
886         /* The max_num_kflows were set, so move to external */
887         if (bnxt_ulp_cntxt_mem_type_set(ulp_ctx, BNXT_ULP_FLOW_MEM_TYPE_EXT))
888                 return -EINVAL;
889
890         if (bnxt_ulp_cntxt_dev_id_get(ulp_ctx, &dev_id)) {
891                 BNXT_TF_DBG(DEBUG, "Failed to get device id\n");
892                 return -EINVAL;
893         }
894
895         dparms = bnxt_ulp_device_params_get(dev_id);
896         if (!dparms) {
897                 BNXT_TF_DBG(DEBUG, "Failed to get device parms\n");
898                 return -EINVAL;
899         }
900
901         /* num_flows = max_num_kflows * 1024 */
902         dparms->ext_flow_db_num_entries = bp->max_num_kflows * 1024;
903         /* GFID =  2 * num_flows */
904         dparms->mark_db_gfid_entries = dparms->ext_flow_db_num_entries * 2;
905         BNXT_TF_DBG(DEBUG, "Set the number of flows = %" PRIu64 "\n",
906                     dparms->ext_flow_db_num_entries);
907
908         return 0;
909 }
910
911 /* The function to initialize bp flags with truflow features */
912 static int32_t
913 ulp_dparms_dev_port_intf_update(struct bnxt *bp,
914                                 struct bnxt_ulp_context *ulp_ctx)
915 {
916         enum bnxt_ulp_flow_mem_type mtype;
917
918         if (bnxt_ulp_cntxt_mem_type_get(ulp_ctx, &mtype))
919                 return -EINVAL;
920         /* Update the bp flag with gfid flag */
921         if (mtype == BNXT_ULP_FLOW_MEM_TYPE_EXT)
922                 bp->flags |= BNXT_FLAG_GFID_ENABLE;
923
924         return 0;
925 }
926
927 static int32_t
928 ulp_ctx_attach(struct bnxt *bp,
929                struct bnxt_ulp_session_state *session)
930 {
931         int32_t rc = 0;
932         uint32_t flags, dev_id = BNXT_ULP_DEVICE_ID_LAST;
933         uint8_t app_id;
934
935         /* Increment the ulp context data reference count usage. */
936         bp->ulp_ctx->cfg_data = session->cfg_data;
937         bp->ulp_ctx->cfg_data->ref_cnt++;
938
939         /* update the session details in bnxt tfp */
940         bp->tfp.session = session->g_tfp->session;
941
942         /* Add the context to the context entries list */
943         rc = bnxt_ulp_cntxt_list_add(bp->ulp_ctx);
944         if (rc) {
945                 BNXT_TF_DBG(ERR, "Failed to add the context list entry\n");
946                 return -EINVAL;
947         }
948
949         /*
950          * The supported flag will be set during the init. Use it now to
951          * know if we should go through the attach.
952          */
953         rc = bnxt_ulp_cntxt_app_id_get(bp->ulp_ctx, &app_id);
954         if (rc) {
955                 BNXT_TF_DBG(ERR, "Unable to get the app id from ulp.\n");
956                 return -EINVAL;
957         }
958
959         rc = bnxt_ulp_cntxt_dev_id_get(bp->ulp_ctx, &dev_id);
960         if (rc) {
961                 BNXT_TF_DBG(ERR, "Unable do get the dev_id.\n");
962                 return -EINVAL;
963         }
964
965         flags = bp->ulp_ctx->cfg_data->ulp_flags;
966         if (ULP_APP_DEV_UNSUPPORTED_ENABLED(flags)) {
967                 BNXT_TF_DBG(ERR, "APP ID %d, Device ID: 0x%x not supported.\n",
968                             app_id, dev_id);
969                 return -EINVAL;
970         }
971
972         /* Create a TF Client */
973         rc = ulp_ctx_session_open(bp, session);
974         if (rc) {
975                 PMD_DRV_LOG(ERR, "Failed to open ctxt session, rc:%d\n", rc);
976                 bp->tfp.session = NULL;
977                 return rc;
978         }
979
980         bnxt_ulp_cntxt_tfp_set(bp->ulp_ctx, &bp->tfp);
981         return rc;
982 }
983
984 static void
985 ulp_ctx_detach(struct bnxt *bp)
986 {
987         if (bp->tfp.session) {
988                 tf_close_session(&bp->tfp);
989                 bp->tfp.session = NULL;
990         }
991 }
992
993 /*
994  * Initialize the state of an ULP session.
995  * If the state of an ULP session is not initialized, set it's state to
996  * initialized. If the state is already initialized, do nothing.
997  */
998 static void
999 ulp_context_initialized(struct bnxt_ulp_session_state *session, bool *init)
1000 {
1001         pthread_mutex_lock(&session->bnxt_ulp_mutex);
1002
1003         if (!session->bnxt_ulp_init) {
1004                 session->bnxt_ulp_init = true;
1005                 *init = false;
1006         } else {
1007                 *init = true;
1008         }
1009
1010         pthread_mutex_unlock(&session->bnxt_ulp_mutex);
1011 }
1012
1013 /*
1014  * Check if an ULP session is already allocated for a specific PCI
1015  * domain & bus. If it is already allocated simply return the session
1016  * pointer, otherwise allocate a new session.
1017  */
1018 static struct bnxt_ulp_session_state *
1019 ulp_get_session(struct rte_pci_addr *pci_addr)
1020 {
1021         struct bnxt_ulp_session_state *session;
1022
1023         STAILQ_FOREACH(session, &bnxt_ulp_session_list, next) {
1024                 if (session->pci_info.domain == pci_addr->domain &&
1025                     session->pci_info.bus == pci_addr->bus) {
1026                         return session;
1027                 }
1028         }
1029         return NULL;
1030 }
1031
1032 /*
1033  * Allocate and Initialize an ULP session and set it's state to INITIALIZED.
1034  * If it's already initialized simply return the already existing session.
1035  */
1036 static struct bnxt_ulp_session_state *
1037 ulp_session_init(struct bnxt *bp,
1038                  bool *init)
1039 {
1040         struct rte_pci_device           *pci_dev;
1041         struct rte_pci_addr             *pci_addr;
1042         struct bnxt_ulp_session_state   *session;
1043         int rc = 0;
1044
1045         if (!bp)
1046                 return NULL;
1047
1048         pci_dev = RTE_DEV_TO_PCI(bp->eth_dev->device);
1049         pci_addr = &pci_dev->addr;
1050
1051         pthread_mutex_lock(&bnxt_ulp_global_mutex);
1052
1053         session = ulp_get_session(pci_addr);
1054         if (!session) {
1055                 /* Not Found the session  Allocate a new one */
1056                 session = rte_zmalloc("bnxt_ulp_session",
1057                                       sizeof(struct bnxt_ulp_session_state),
1058                                       0);
1059                 if (!session) {
1060                         BNXT_TF_DBG(ERR,
1061                                     "Allocation failed for bnxt_ulp_session\n");
1062                         pthread_mutex_unlock(&bnxt_ulp_global_mutex);
1063                         return NULL;
1064
1065                 } else {
1066                         /* Add it to the queue */
1067                         session->pci_info.domain = pci_addr->domain;
1068                         session->pci_info.bus = pci_addr->bus;
1069                         rc = pthread_mutex_init(&session->bnxt_ulp_mutex, NULL);
1070                         if (rc) {
1071                                 BNXT_TF_DBG(ERR, "mutex create failed\n");
1072                                 pthread_mutex_unlock(&bnxt_ulp_global_mutex);
1073                                 return NULL;
1074                         }
1075                         STAILQ_INSERT_TAIL(&bnxt_ulp_session_list,
1076                                            session, next);
1077                 }
1078         }
1079         ulp_context_initialized(session, init);
1080         pthread_mutex_unlock(&bnxt_ulp_global_mutex);
1081         return session;
1082 }
1083
1084 /*
1085  * When a device is closed, remove it's associated session from the global
1086  * session list.
1087  */
1088 static void
1089 ulp_session_deinit(struct bnxt_ulp_session_state *session)
1090 {
1091         if (!session)
1092                 return;
1093
1094         if (!session->cfg_data) {
1095                 pthread_mutex_lock(&bnxt_ulp_global_mutex);
1096                 STAILQ_REMOVE(&bnxt_ulp_session_list, session,
1097                               bnxt_ulp_session_state, next);
1098                 pthread_mutex_destroy(&session->bnxt_ulp_mutex);
1099                 rte_free(session);
1100                 pthread_mutex_unlock(&bnxt_ulp_global_mutex);
1101         }
1102 }
1103
1104 /*
1105  * Internal api to enable NAT feature.
1106  * Set set_flag to 1 to set the value or zero to reset the value.
1107  * returns 0 on success.
1108  */
1109 static int32_t
1110 bnxt_ulp_global_cfg_update(struct bnxt *bp,
1111                            enum tf_dir dir,
1112                            enum tf_global_config_type type,
1113                            uint32_t offset,
1114                            uint32_t value,
1115                            uint32_t set_flag)
1116 {
1117         uint32_t global_cfg = 0;
1118         int rc;
1119         struct tf_global_cfg_parms parms = { 0 };
1120
1121         /* Initialize the params */
1122         parms.dir = dir,
1123         parms.type = type,
1124         parms.offset = offset,
1125         parms.config = (uint8_t *)&global_cfg,
1126         parms.config_sz_in_bytes = sizeof(global_cfg);
1127
1128         rc = tf_get_global_cfg(&bp->tfp, &parms);
1129         if (rc) {
1130                 BNXT_TF_DBG(ERR, "Failed to get global cfg 0x%x rc:%d\n",
1131                             type, rc);
1132                 return rc;
1133         }
1134
1135         if (set_flag)
1136                 global_cfg |= value;
1137         else
1138                 global_cfg &= ~value;
1139
1140         /* SET the register RE_CFA_REG_ACT_TECT */
1141         rc = tf_set_global_cfg(&bp->tfp, &parms);
1142         if (rc) {
1143                 BNXT_TF_DBG(ERR, "Failed to set global cfg 0x%x rc:%d\n",
1144                             type, rc);
1145                 return rc;
1146         }
1147         return rc;
1148 }
1149
1150 /* Internal function to delete all the flows belonging to the given port */
1151 static void
1152 bnxt_ulp_flush_port_flows(struct bnxt *bp)
1153 {
1154         uint16_t func_id;
1155
1156         /* it is assumed that port is either TVF or PF */
1157         if (ulp_port_db_port_func_id_get(bp->ulp_ctx,
1158                                          bp->eth_dev->data->port_id,
1159                                          &func_id)) {
1160                 BNXT_TF_DBG(ERR, "Invalid argument\n");
1161                 return;
1162         }
1163         (void)ulp_flow_db_function_flow_flush(bp->ulp_ctx, func_id);
1164 }
1165
1166 /* Internal function to delete the VFR default flows */
1167 static void
1168 bnxt_ulp_destroy_vfr_default_rules(struct bnxt *bp, bool global)
1169 {
1170         struct bnxt_ulp_vfr_rule_info *info;
1171         uint16_t port_id;
1172         struct rte_eth_dev *vfr_eth_dev;
1173         struct bnxt_representor *vfr_bp;
1174
1175         if (!BNXT_TRUFLOW_EN(bp) || BNXT_ETH_DEV_IS_REPRESENTOR(bp->eth_dev))
1176                 return;
1177
1178         if (!bp->ulp_ctx || !bp->ulp_ctx->cfg_data)
1179                 return;
1180
1181         /* Delete default rules for all ports */
1182         for (port_id = 0; port_id < RTE_MAX_ETHPORTS; port_id++) {
1183                 info = &bp->ulp_ctx->cfg_data->vfr_rule_info[port_id];
1184                 if (!info->valid)
1185                         continue;
1186
1187                 if (!global && info->parent_port_id !=
1188                     bp->eth_dev->data->port_id)
1189                         continue;
1190
1191                 /* Destroy the flows */
1192                 ulp_default_flow_destroy(bp->eth_dev, info->vfr_flow_id);
1193                 /* Clean up the tx action pointer */
1194                 vfr_eth_dev = &rte_eth_devices[port_id];
1195                 if (vfr_eth_dev) {
1196                         vfr_bp = vfr_eth_dev->data->dev_private;
1197                         vfr_bp->vfr_tx_cfa_action = 0;
1198                 }
1199                 memset(info, 0, sizeof(struct bnxt_ulp_vfr_rule_info));
1200         }
1201 }
1202
1203 /*
1204  * When a port is deinit'ed by dpdk. This function is called
1205  * and this function clears the ULP context and rest of the
1206  * infrastructure associated with it.
1207  */
1208 static void
1209 bnxt_ulp_deinit(struct bnxt *bp,
1210                 struct bnxt_ulp_session_state *session)
1211 {
1212         bool ha_enabled;
1213
1214         if (!bp->ulp_ctx || !bp->ulp_ctx->cfg_data)
1215                 return;
1216
1217         ha_enabled = bnxt_ulp_cntxt_ha_enabled(bp->ulp_ctx);
1218         if (ha_enabled && session->session_opened) {
1219                 int32_t rc = ulp_ha_mgr_close(bp->ulp_ctx);
1220                 if (rc)
1221                         BNXT_TF_DBG(ERR, "Failed to close HA (%d)\n", rc);
1222         }
1223
1224         /* clean up default flows */
1225         bnxt_ulp_destroy_df_rules(bp, true);
1226
1227         /* clean up default VFR flows */
1228         bnxt_ulp_destroy_vfr_default_rules(bp, true);
1229
1230         /* clean up regular flows */
1231         ulp_flow_db_flush_flows(bp->ulp_ctx, BNXT_ULP_FDB_TYPE_REGULAR);
1232
1233         /* cleanup the eem table scope */
1234         ulp_eem_tbl_scope_deinit(bp, bp->ulp_ctx);
1235
1236         /* cleanup the flow database */
1237         ulp_flow_db_deinit(bp->ulp_ctx);
1238
1239         /* Delete the Mark database */
1240         ulp_mark_db_deinit(bp->ulp_ctx);
1241
1242         /* cleanup the ulp mapper */
1243         ulp_mapper_deinit(bp->ulp_ctx);
1244
1245         /* Delete the Flow Counter Manager */
1246         ulp_fc_mgr_deinit(bp->ulp_ctx);
1247
1248         /* Delete the Port database */
1249         ulp_port_db_deinit(bp->ulp_ctx);
1250
1251         /* Disable NAT feature */
1252         (void)bnxt_ulp_global_cfg_update(bp, TF_DIR_RX, TF_TUNNEL_ENCAP,
1253                                          TF_TUNNEL_ENCAP_NAT,
1254                                          BNXT_ULP_NAT_OUTER_MOST_FLAGS, 0);
1255
1256         (void)bnxt_ulp_global_cfg_update(bp, TF_DIR_TX, TF_TUNNEL_ENCAP,
1257                                          TF_TUNNEL_ENCAP_NAT,
1258                                          BNXT_ULP_NAT_OUTER_MOST_FLAGS, 0);
1259
1260         /* free the flow db lock */
1261         pthread_mutex_destroy(&bp->ulp_ctx->cfg_data->flow_db_lock);
1262
1263         if (ha_enabled)
1264                 ulp_ha_mgr_deinit(bp->ulp_ctx);
1265
1266         /* Delete the ulp context and tf session and free the ulp context */
1267         ulp_ctx_deinit(bp, session);
1268         BNXT_TF_DBG(DEBUG, "ulp ctx has been deinitialized\n");
1269 }
1270
1271 /*
1272  * When a port is initialized by dpdk. This functions is called
1273  * and this function initializes the ULP context and rest of the
1274  * infrastructure associated with it.
1275  */
1276 static int32_t
1277 bnxt_ulp_init(struct bnxt *bp,
1278               struct bnxt_ulp_session_state *session)
1279 {
1280         int rc;
1281
1282         /* Allocate and Initialize the ulp context. */
1283         rc = ulp_ctx_init(bp, session);
1284         if (rc) {
1285                 BNXT_TF_DBG(ERR, "Failed to create the ulp context\n");
1286                 goto jump_to_error;
1287         }
1288
1289         rc = pthread_mutex_init(&bp->ulp_ctx->cfg_data->flow_db_lock, NULL);
1290         if (rc) {
1291                 BNXT_TF_DBG(ERR, "Unable to initialize flow db lock\n");
1292                 goto jump_to_error;
1293         }
1294
1295         /* Initialize ulp dparms with values devargs passed */
1296         rc = ulp_dparms_init(bp, bp->ulp_ctx);
1297         if (rc) {
1298                 BNXT_TF_DBG(ERR, "Failed to initialize the dparms\n");
1299                 goto jump_to_error;
1300         }
1301
1302         /* create the port database */
1303         rc = ulp_port_db_init(bp->ulp_ctx, bp->port_cnt);
1304         if (rc) {
1305                 BNXT_TF_DBG(ERR, "Failed to create the port database\n");
1306                 goto jump_to_error;
1307         }
1308
1309         /* Create the Mark database. */
1310         rc = ulp_mark_db_init(bp->ulp_ctx);
1311         if (rc) {
1312                 BNXT_TF_DBG(ERR, "Failed to create the mark database\n");
1313                 goto jump_to_error;
1314         }
1315
1316         /* Create the flow database. */
1317         rc = ulp_flow_db_init(bp->ulp_ctx);
1318         if (rc) {
1319                 BNXT_TF_DBG(ERR, "Failed to create the flow database\n");
1320                 goto jump_to_error;
1321         }
1322
1323         /* Create the eem table scope. */
1324         rc = ulp_eem_tbl_scope_init(bp);
1325         if (rc) {
1326                 BNXT_TF_DBG(ERR, "Failed to create the eem scope table\n");
1327                 goto jump_to_error;
1328         }
1329
1330         rc = ulp_mapper_init(bp->ulp_ctx);
1331         if (rc) {
1332                 BNXT_TF_DBG(ERR, "Failed to initialize ulp mapper\n");
1333                 goto jump_to_error;
1334         }
1335
1336         rc = ulp_fc_mgr_init(bp->ulp_ctx);
1337         if (rc) {
1338                 BNXT_TF_DBG(ERR, "Failed to initialize ulp flow counter mgr\n");
1339                 goto jump_to_error;
1340         }
1341
1342         /*
1343          * Enable NAT feature. Set the global configuration register
1344          * Tunnel encap to enable NAT with the reuse of existing inner
1345          * L2 header smac and dmac
1346          */
1347         rc = bnxt_ulp_global_cfg_update(bp, TF_DIR_RX, TF_TUNNEL_ENCAP,
1348                                         TF_TUNNEL_ENCAP_NAT,
1349                                         BNXT_ULP_NAT_OUTER_MOST_FLAGS, 1);
1350         if (rc) {
1351                 BNXT_TF_DBG(ERR, "Failed to set rx global configuration\n");
1352                 goto jump_to_error;
1353         }
1354
1355         rc = bnxt_ulp_global_cfg_update(bp, TF_DIR_TX, TF_TUNNEL_ENCAP,
1356                                         TF_TUNNEL_ENCAP_NAT,
1357                                         BNXT_ULP_NAT_OUTER_MOST_FLAGS, 1);
1358         if (rc) {
1359                 BNXT_TF_DBG(ERR, "Failed to set tx global configuration\n");
1360                 goto jump_to_error;
1361         }
1362
1363         if (bnxt_ulp_cntxt_ha_enabled(bp->ulp_ctx)) {
1364                 rc = ulp_ha_mgr_init(bp->ulp_ctx);
1365                 if (rc) {
1366                         BNXT_TF_DBG(ERR, "Failed to initialize HA %d\n", rc);
1367                         goto jump_to_error;
1368                 }
1369                 rc = ulp_ha_mgr_open(bp->ulp_ctx);
1370                 if (rc) {
1371                         BNXT_TF_DBG(ERR, "Failed to Process HA Open %d\n", rc);
1372                         goto jump_to_error;
1373                 }
1374         }
1375         BNXT_TF_DBG(DEBUG, "ulp ctx has been initialized\n");
1376         return rc;
1377
1378 jump_to_error:
1379         bnxt_ulp_deinit(bp, session);
1380         return rc;
1381 }
1382
1383 /*
1384  * When a port is initialized by dpdk. This functions sets up
1385  * the port specific details.
1386  */
1387 int32_t
1388 bnxt_ulp_port_init(struct bnxt *bp)
1389 {
1390         struct bnxt_ulp_session_state *session;
1391         bool initialized;
1392         enum bnxt_ulp_device_id devid = BNXT_ULP_DEVICE_ID_LAST;
1393         uint32_t ulp_flags;
1394         int32_t rc = 0;
1395
1396         if (!bp || !BNXT_TRUFLOW_EN(bp))
1397                 return rc;
1398
1399         if (!BNXT_PF(bp) && !BNXT_VF_IS_TRUSTED(bp)) {
1400                 BNXT_TF_DBG(ERR,
1401                             "Skip ulp init for port: %d, not a TVF or PF\n",
1402                         bp->eth_dev->data->port_id);
1403                 return rc;
1404         }
1405
1406         if (bp->ulp_ctx) {
1407                 BNXT_TF_DBG(DEBUG, "ulp ctx already allocated\n");
1408                 return rc;
1409         }
1410
1411         bp->ulp_ctx = rte_zmalloc("bnxt_ulp_ctx",
1412                                   sizeof(struct bnxt_ulp_context), 0);
1413         if (!bp->ulp_ctx) {
1414                 BNXT_TF_DBG(ERR, "Failed to allocate ulp ctx\n");
1415                 return -ENOMEM;
1416         }
1417
1418         /*
1419          * Multiple uplink ports can be associated with a single vswitch.
1420          * Make sure only the port that is started first will initialize
1421          * the TF session.
1422          */
1423         session = ulp_session_init(bp, &initialized);
1424         if (!session) {
1425                 BNXT_TF_DBG(ERR, "Failed to initialize the tf session\n");
1426                 rc = -EIO;
1427                 goto jump_to_error;
1428         }
1429
1430         if (initialized) {
1431                 /*
1432                  * If ULP is already initialized for a specific domain then
1433                  * simply assign the ulp context to this rte_eth_dev.
1434                  */
1435                 rc = ulp_ctx_attach(bp, session);
1436                 if (rc) {
1437                         BNXT_TF_DBG(ERR, "Failed to attach the ulp context\n");
1438                         goto jump_to_error;
1439                 }
1440
1441                 /*
1442                  * Attach to the shared session, must be called after the
1443                  * ulp_ctx_attach in order to ensure that ulp data is available
1444                  * for attaching.
1445                  */
1446                 rc = ulp_ctx_shared_session_attach(bp, session);
1447                 if (rc) {
1448                         BNXT_TF_DBG(ERR,
1449                                     "Failed attach to shared session (%d)", rc);
1450                         goto jump_to_error;
1451                 }
1452         } else {
1453                 rc = bnxt_ulp_init(bp, session);
1454                 if (rc) {
1455                         BNXT_TF_DBG(ERR, "Failed to initialize the ulp init\n");
1456                         goto jump_to_error;
1457                 }
1458         }
1459
1460         /* Update bnxt driver flags */
1461         rc = ulp_dparms_dev_port_intf_update(bp, bp->ulp_ctx);
1462         if (rc) {
1463                 BNXT_TF_DBG(ERR, "Failed to update driver flags\n");
1464                 goto jump_to_error;
1465         }
1466
1467         /* update the port database for the given interface */
1468         rc = ulp_port_db_dev_port_intf_update(bp->ulp_ctx, bp->eth_dev);
1469         if (rc) {
1470                 BNXT_TF_DBG(ERR, "Failed to update port database\n");
1471                 goto jump_to_error;
1472         }
1473         /* create the default rules */
1474         rc = bnxt_ulp_create_df_rules(bp);
1475         if (rc) {
1476                 BNXT_TF_DBG(ERR, "Failed to create default flow\n");
1477                 goto jump_to_error;
1478         }
1479
1480         rc = bnxt_ulp_devid_get(bp, &devid);
1481         if (rc) {
1482                 BNXT_TF_DBG(ERR, "Unable to determine device for ULP port init.\n");
1483                 goto jump_to_error;
1484         }
1485
1486         if (devid != BNXT_ULP_DEVICE_ID_THOR && BNXT_ACCUM_STATS_EN(bp))
1487                 bp->ulp_ctx->cfg_data->accum_stats = true;
1488
1489         BNXT_TF_DBG(DEBUG, "BNXT Port:%d ULP port init, accum_stats:%d\n",
1490                     bp->eth_dev->data->port_id,
1491                     bp->ulp_ctx->cfg_data->accum_stats);
1492
1493         /* set the unicast mode */
1494         if (bnxt_ulp_cntxt_ptr2_ulp_flags_get(bp->ulp_ctx, &ulp_flags)) {
1495                 BNXT_TF_DBG(ERR, "Error in getting ULP context flags\n");
1496                 goto jump_to_error;
1497         }
1498         if (ulp_flags & BNXT_ULP_APP_UNICAST_ONLY) {
1499                 if (bnxt_pmd_set_unicast_rxmask(bp->eth_dev)) {
1500                         BNXT_TF_DBG(ERR, "Error in setting unicast rxmode\n");
1501                         goto jump_to_error;
1502                 }
1503         }
1504
1505         return rc;
1506
1507 jump_to_error:
1508         bnxt_ulp_port_deinit(bp);
1509         return rc;
1510 }
1511
1512 /*
1513  * When a port is de-initialized by dpdk. This functions clears up
1514  * the port specific details.
1515  */
1516 void
1517 bnxt_ulp_port_deinit(struct bnxt *bp)
1518 {
1519         struct bnxt_ulp_session_state *session;
1520         struct rte_pci_device *pci_dev;
1521         struct rte_pci_addr *pci_addr;
1522
1523         if (!BNXT_TRUFLOW_EN(bp))
1524                 return;
1525
1526         if (!BNXT_PF(bp) && !BNXT_VF_IS_TRUSTED(bp)) {
1527                 BNXT_TF_DBG(ERR,
1528                             "Skip ULP deinit port:%d, not a TVF or PF\n",
1529                             bp->eth_dev->data->port_id);
1530                 return;
1531         }
1532
1533         if (!bp->ulp_ctx) {
1534                 BNXT_TF_DBG(DEBUG, "ulp ctx already de-allocated\n");
1535                 return;
1536         }
1537
1538         BNXT_TF_DBG(DEBUG, "BNXT Port:%d ULP port deinit\n",
1539                     bp->eth_dev->data->port_id);
1540
1541         /* Free the ulp context in the context entry list */
1542         bnxt_ulp_cntxt_list_del(bp->ulp_ctx);
1543
1544         /* Get the session details  */
1545         pci_dev = RTE_DEV_TO_PCI(bp->eth_dev->device);
1546         pci_addr = &pci_dev->addr;
1547         pthread_mutex_lock(&bnxt_ulp_global_mutex);
1548         session = ulp_get_session(pci_addr);
1549         pthread_mutex_unlock(&bnxt_ulp_global_mutex);
1550
1551         /* session not found then just exit */
1552         if (!session) {
1553                 /* Free the ulp context */
1554                 rte_free(bp->ulp_ctx);
1555                 bp->ulp_ctx = NULL;
1556                 return;
1557         }
1558
1559         /* Check the reference count to deinit or deattach*/
1560         if (bp->ulp_ctx->cfg_data && bp->ulp_ctx->cfg_data->ref_cnt) {
1561                 bp->ulp_ctx->cfg_data->ref_cnt--;
1562                 if (bp->ulp_ctx->cfg_data->ref_cnt) {
1563                         /* free the port details */
1564                         /* Free the default flow rule associated to this port */
1565                         bnxt_ulp_destroy_df_rules(bp, false);
1566                         bnxt_ulp_destroy_vfr_default_rules(bp, false);
1567
1568                         /* free flows associated with this port */
1569                         bnxt_ulp_flush_port_flows(bp);
1570
1571                         /* close the session associated with this port */
1572                         ulp_ctx_detach(bp);
1573
1574                         /* always detach/close shared after the session. */
1575                         ulp_ctx_shared_session_detach(bp);
1576                 } else {
1577                         /* Perform ulp ctx deinit */
1578                         bnxt_ulp_deinit(bp, session);
1579                 }
1580         }
1581
1582         /* clean up the session */
1583         ulp_session_deinit(session);
1584
1585         /* Free the ulp context */
1586         rte_free(bp->ulp_ctx);
1587         bp->ulp_ctx = NULL;
1588 }
1589
1590 /* Below are the access functions to access internal data of ulp context. */
1591 /* Function to set the Mark DB into the context */
1592 int32_t
1593 bnxt_ulp_cntxt_ptr2_mark_db_set(struct bnxt_ulp_context *ulp_ctx,
1594                                 struct bnxt_ulp_mark_tbl *mark_tbl)
1595 {
1596         if (!ulp_ctx || !ulp_ctx->cfg_data) {
1597                 BNXT_TF_DBG(ERR, "Invalid ulp context data\n");
1598                 return -EINVAL;
1599         }
1600
1601         ulp_ctx->cfg_data->mark_tbl = mark_tbl;
1602
1603         return 0;
1604 }
1605
1606 /* Function to retrieve the Mark DB from the context. */
1607 struct bnxt_ulp_mark_tbl *
1608 bnxt_ulp_cntxt_ptr2_mark_db_get(struct bnxt_ulp_context *ulp_ctx)
1609 {
1610         if (!ulp_ctx || !ulp_ctx->cfg_data)
1611                 return NULL;
1612
1613         return ulp_ctx->cfg_data->mark_tbl;
1614 }
1615
1616 bool
1617 bnxt_ulp_cntxt_shared_session_enabled(struct bnxt_ulp_context *ulp_ctx)
1618 {
1619         return ULP_SHARED_SESSION_IS_ENABLED(ulp_ctx->cfg_data->ulp_flags);
1620 }
1621
1622 int32_t
1623 bnxt_ulp_cntxt_app_id_set(struct bnxt_ulp_context *ulp_ctx, uint8_t app_id)
1624 {
1625         if (!ulp_ctx)
1626                 return -EINVAL;
1627         ulp_ctx->cfg_data->app_id = app_id;
1628         return 0;
1629 }
1630
1631 int32_t
1632 bnxt_ulp_cntxt_app_id_get(struct bnxt_ulp_context *ulp_ctx, uint8_t *app_id)
1633 {
1634         /* Default APP id is zero */
1635         if (!ulp_ctx || !app_id)
1636                 return -EINVAL;
1637         *app_id = ulp_ctx->cfg_data->app_id;
1638         return 0;
1639 }
1640
1641 /* Function to set the device id of the hardware. */
1642 int32_t
1643 bnxt_ulp_cntxt_dev_id_set(struct bnxt_ulp_context *ulp_ctx,
1644                           uint32_t dev_id)
1645 {
1646         if (ulp_ctx && ulp_ctx->cfg_data) {
1647                 ulp_ctx->cfg_data->dev_id = dev_id;
1648                 return 0;
1649         }
1650
1651         return -EINVAL;
1652 }
1653
1654 /* Function to get the device id of the hardware. */
1655 int32_t
1656 bnxt_ulp_cntxt_dev_id_get(struct bnxt_ulp_context *ulp_ctx,
1657                           uint32_t *dev_id)
1658 {
1659         if (ulp_ctx && ulp_ctx->cfg_data) {
1660                 *dev_id = ulp_ctx->cfg_data->dev_id;
1661                 return 0;
1662         }
1663         *dev_id = BNXT_ULP_DEVICE_ID_LAST;
1664         BNXT_TF_DBG(ERR, "Failed to read dev_id from ulp ctxt\n");
1665         return -EINVAL;
1666 }
1667
1668 int32_t
1669 bnxt_ulp_cntxt_mem_type_set(struct bnxt_ulp_context *ulp_ctx,
1670                             enum bnxt_ulp_flow_mem_type mem_type)
1671 {
1672         if (ulp_ctx && ulp_ctx->cfg_data) {
1673                 ulp_ctx->cfg_data->mem_type = mem_type;
1674                 return 0;
1675         }
1676         BNXT_TF_DBG(ERR, "Failed to write mem_type in ulp ctxt\n");
1677         return -EINVAL;
1678 }
1679
1680 int32_t
1681 bnxt_ulp_cntxt_mem_type_get(struct bnxt_ulp_context *ulp_ctx,
1682                             enum bnxt_ulp_flow_mem_type *mem_type)
1683 {
1684         if (ulp_ctx && ulp_ctx->cfg_data) {
1685                 *mem_type = ulp_ctx->cfg_data->mem_type;
1686                 return 0;
1687         }
1688         *mem_type = BNXT_ULP_FLOW_MEM_TYPE_LAST;
1689         BNXT_TF_DBG(ERR, "Failed to read mem_type in ulp ctxt\n");
1690         return -EINVAL;
1691 }
1692
1693 /* Function to get the table scope id of the EEM table. */
1694 int32_t
1695 bnxt_ulp_cntxt_tbl_scope_id_get(struct bnxt_ulp_context *ulp_ctx,
1696                                 uint32_t *tbl_scope_id)
1697 {
1698         if (ulp_ctx && ulp_ctx->cfg_data) {
1699                 *tbl_scope_id = ulp_ctx->cfg_data->tbl_scope_id;
1700                 return 0;
1701         }
1702
1703         return -EINVAL;
1704 }
1705
1706 /* Function to set the table scope id of the EEM table. */
1707 int32_t
1708 bnxt_ulp_cntxt_tbl_scope_id_set(struct bnxt_ulp_context *ulp_ctx,
1709                                 uint32_t tbl_scope_id)
1710 {
1711         if (ulp_ctx && ulp_ctx->cfg_data) {
1712                 ulp_ctx->cfg_data->tbl_scope_id = tbl_scope_id;
1713                 return 0;
1714         }
1715
1716         return -EINVAL;
1717 }
1718
1719 /* Function to set the shared tfp session details from the ulp context. */
1720 int32_t
1721 bnxt_ulp_cntxt_shared_tfp_set(struct bnxt_ulp_context *ulp, struct tf *tfp)
1722 {
1723         if (!ulp) {
1724                 BNXT_TF_DBG(ERR, "Invalid arguments\n");
1725                 return -EINVAL;
1726         }
1727
1728         if (tfp == NULL) {
1729                 if (ulp->cfg_data->num_shared_clients > 0)
1730                         ulp->cfg_data->num_shared_clients--;
1731         } else {
1732                 ulp->cfg_data->num_shared_clients++;
1733         }
1734
1735         ulp->g_shared_tfp = tfp;
1736         return 0;
1737 }
1738
1739 /* Function to get the shared tfp session details from the ulp context. */
1740 struct tf *
1741 bnxt_ulp_cntxt_shared_tfp_get(struct bnxt_ulp_context *ulp)
1742 {
1743         if (!ulp) {
1744                 BNXT_TF_DBG(ERR, "Invalid arguments\n");
1745                 return NULL;
1746         }
1747         return ulp->g_shared_tfp;
1748 }
1749
1750 /* Function to get the number of shared clients attached */
1751 uint8_t
1752 bnxt_ulp_cntxt_num_shared_clients_get(struct bnxt_ulp_context *ulp)
1753 {
1754         if (ulp == NULL || ulp->cfg_data == NULL) {
1755                 BNXT_TF_DBG(ERR, "Invalid arguments\n");
1756                 return 0;
1757         }
1758         return ulp->cfg_data->num_shared_clients;
1759 }
1760
1761 /* Function to set the tfp session details from the ulp context. */
1762 int32_t
1763 bnxt_ulp_cntxt_tfp_set(struct bnxt_ulp_context *ulp, struct tf *tfp)
1764 {
1765         if (!ulp) {
1766                 BNXT_TF_DBG(ERR, "Invalid arguments\n");
1767                 return -EINVAL;
1768         }
1769
1770         ulp->g_tfp = tfp;
1771         return 0;
1772 }
1773
1774 /* Function to get the tfp session details from the ulp context. */
1775 struct tf *
1776 bnxt_ulp_cntxt_tfp_get(struct bnxt_ulp_context *ulp,
1777                        enum bnxt_ulp_shared_session shared)
1778 {
1779         if (!ulp) {
1780                 BNXT_TF_DBG(ERR, "Invalid arguments\n");
1781                 return NULL;
1782         }
1783         if (shared)
1784                 return ulp->g_shared_tfp;
1785         else
1786                 return ulp->g_tfp;
1787 }
1788
1789 /*
1790  * Get the device table entry based on the device id.
1791  *
1792  * dev_id [in] The device id of the hardware
1793  *
1794  * Returns the pointer to the device parameters.
1795  */
1796 struct bnxt_ulp_device_params *
1797 bnxt_ulp_device_params_get(uint32_t dev_id)
1798 {
1799         if (dev_id < BNXT_ULP_MAX_NUM_DEVICES)
1800                 return &ulp_device_params[dev_id];
1801         return NULL;
1802 }
1803
1804 /* Function to set the flow database to the ulp context. */
1805 int32_t
1806 bnxt_ulp_cntxt_ptr2_flow_db_set(struct bnxt_ulp_context *ulp_ctx,
1807                                 struct bnxt_ulp_flow_db *flow_db)
1808 {
1809         if (!ulp_ctx || !ulp_ctx->cfg_data)
1810                 return -EINVAL;
1811
1812         ulp_ctx->cfg_data->flow_db = flow_db;
1813         return 0;
1814 }
1815
1816 /* Function to get the flow database from the ulp context. */
1817 struct bnxt_ulp_flow_db *
1818 bnxt_ulp_cntxt_ptr2_flow_db_get(struct bnxt_ulp_context *ulp_ctx)
1819 {
1820         if (!ulp_ctx || !ulp_ctx->cfg_data)
1821                 return NULL;
1822
1823         return ulp_ctx->cfg_data->flow_db;
1824 }
1825
1826 /* Function to get the tunnel cache table info from the ulp context. */
1827 struct bnxt_tun_cache_entry *
1828 bnxt_ulp_cntxt_ptr2_tun_tbl_get(struct bnxt_ulp_context *ulp_ctx)
1829 {
1830         if (!ulp_ctx || !ulp_ctx->cfg_data)
1831                 return NULL;
1832
1833         return ulp_ctx->cfg_data->tun_tbl;
1834 }
1835
1836 /* Function to get the ulp context from eth device. */
1837 struct bnxt_ulp_context *
1838 bnxt_ulp_eth_dev_ptr2_cntxt_get(struct rte_eth_dev      *dev)
1839 {
1840         struct bnxt *bp = (struct bnxt *)dev->data->dev_private;
1841
1842         if (BNXT_ETH_DEV_IS_REPRESENTOR(dev)) {
1843                 struct bnxt_representor *vfr = dev->data->dev_private;
1844
1845                 bp = vfr->parent_dev->data->dev_private;
1846         }
1847
1848         if (!bp) {
1849                 BNXT_TF_DBG(ERR, "Bnxt private data is not initialized\n");
1850                 return NULL;
1851         }
1852         return bp->ulp_ctx;
1853 }
1854
1855 int32_t
1856 bnxt_ulp_cntxt_ptr2_mapper_data_set(struct bnxt_ulp_context *ulp_ctx,
1857                                     void *mapper_data)
1858 {
1859         if (!ulp_ctx || !ulp_ctx->cfg_data) {
1860                 BNXT_TF_DBG(ERR, "Invalid ulp context data\n");
1861                 return -EINVAL;
1862         }
1863
1864         ulp_ctx->cfg_data->mapper_data = mapper_data;
1865         return 0;
1866 }
1867
1868 void *
1869 bnxt_ulp_cntxt_ptr2_mapper_data_get(struct bnxt_ulp_context *ulp_ctx)
1870 {
1871         if (!ulp_ctx || !ulp_ctx->cfg_data) {
1872                 BNXT_TF_DBG(ERR, "Invalid ulp context data\n");
1873                 return NULL;
1874         }
1875
1876         return ulp_ctx->cfg_data->mapper_data;
1877 }
1878
1879 /* Function to set the port database to the ulp context. */
1880 int32_t
1881 bnxt_ulp_cntxt_ptr2_port_db_set(struct bnxt_ulp_context *ulp_ctx,
1882                                 struct bnxt_ulp_port_db *port_db)
1883 {
1884         if (!ulp_ctx || !ulp_ctx->cfg_data)
1885                 return -EINVAL;
1886
1887         ulp_ctx->cfg_data->port_db = port_db;
1888         return 0;
1889 }
1890
1891 /* Function to get the port database from the ulp context. */
1892 struct bnxt_ulp_port_db *
1893 bnxt_ulp_cntxt_ptr2_port_db_get(struct bnxt_ulp_context *ulp_ctx)
1894 {
1895         if (!ulp_ctx || !ulp_ctx->cfg_data)
1896                 return NULL;
1897
1898         return ulp_ctx->cfg_data->port_db;
1899 }
1900
1901 /* Function to set the flow counter info into the context */
1902 int32_t
1903 bnxt_ulp_cntxt_ptr2_fc_info_set(struct bnxt_ulp_context *ulp_ctx,
1904                                 struct bnxt_ulp_fc_info *ulp_fc_info)
1905 {
1906         if (!ulp_ctx || !ulp_ctx->cfg_data) {
1907                 BNXT_TF_DBG(ERR, "Invalid ulp context data\n");
1908                 return -EINVAL;
1909         }
1910
1911         ulp_ctx->cfg_data->fc_info = ulp_fc_info;
1912
1913         return 0;
1914 }
1915
1916 /* Function to retrieve the flow counter info from the context. */
1917 struct bnxt_ulp_fc_info *
1918 bnxt_ulp_cntxt_ptr2_fc_info_get(struct bnxt_ulp_context *ulp_ctx)
1919 {
1920         if (!ulp_ctx || !ulp_ctx->cfg_data)
1921                 return NULL;
1922
1923         return ulp_ctx->cfg_data->fc_info;
1924 }
1925
1926 /* Function to get the ulp flags from the ulp context. */
1927 int32_t
1928 bnxt_ulp_cntxt_ptr2_ulp_flags_get(struct bnxt_ulp_context *ulp_ctx,
1929                                   uint32_t *flags)
1930 {
1931         if (!ulp_ctx || !ulp_ctx->cfg_data)
1932                 return -1;
1933
1934         *flags =  ulp_ctx->cfg_data->ulp_flags;
1935         return 0;
1936 }
1937
1938 /* Function to get the ulp vfr info from the ulp context. */
1939 struct bnxt_ulp_vfr_rule_info*
1940 bnxt_ulp_cntxt_ptr2_ulp_vfr_info_get(struct bnxt_ulp_context *ulp_ctx,
1941                                      uint32_t port_id)
1942 {
1943         if (!ulp_ctx || !ulp_ctx->cfg_data || port_id >= RTE_MAX_ETHPORTS)
1944                 return NULL;
1945
1946         return &ulp_ctx->cfg_data->vfr_rule_info[port_id];
1947 }
1948
1949 /* Function to acquire the flow database lock from the ulp context. */
1950 int32_t
1951 bnxt_ulp_cntxt_acquire_fdb_lock(struct bnxt_ulp_context *ulp_ctx)
1952 {
1953         if (!ulp_ctx || !ulp_ctx->cfg_data)
1954                 return -1;
1955
1956         if (pthread_mutex_lock(&ulp_ctx->cfg_data->flow_db_lock)) {
1957                 BNXT_TF_DBG(ERR, "unable to acquire fdb lock\n");
1958                 return -1;
1959         }
1960         return 0;
1961 }
1962
1963 /* Function to release the flow database lock from the ulp context. */
1964 void
1965 bnxt_ulp_cntxt_release_fdb_lock(struct bnxt_ulp_context *ulp_ctx)
1966 {
1967         if (!ulp_ctx || !ulp_ctx->cfg_data)
1968                 return;
1969
1970         pthread_mutex_unlock(&ulp_ctx->cfg_data->flow_db_lock);
1971 }
1972
1973 /* Function to set the ha info into the context */
1974 int32_t
1975 bnxt_ulp_cntxt_ptr2_ha_info_set(struct bnxt_ulp_context *ulp_ctx,
1976                                 struct bnxt_ulp_ha_mgr_info *ulp_ha_info)
1977 {
1978         if (ulp_ctx == NULL || ulp_ctx->cfg_data == NULL) {
1979                 BNXT_TF_DBG(ERR, "Invalid ulp context data\n");
1980                 return -EINVAL;
1981         }
1982         ulp_ctx->cfg_data->ha_info = ulp_ha_info;
1983         return 0;
1984 }
1985
1986 /* Function to retrieve the ha info from the context. */
1987 struct bnxt_ulp_ha_mgr_info *
1988 bnxt_ulp_cntxt_ptr2_ha_info_get(struct bnxt_ulp_context *ulp_ctx)
1989 {
1990         if (ulp_ctx == NULL || ulp_ctx->cfg_data == NULL)
1991                 return NULL;
1992         return ulp_ctx->cfg_data->ha_info;
1993 }
1994
1995 bool
1996 bnxt_ulp_cntxt_ha_enabled(struct bnxt_ulp_context *ulp_ctx)
1997 {
1998         if (ulp_ctx == NULL || ulp_ctx->cfg_data == NULL)
1999                 return false;
2000         return !!ULP_HIGH_AVAIL_IS_ENABLED(ulp_ctx->cfg_data->ulp_flags);
2001 }
2002
2003 static int32_t
2004 bnxt_ulp_cntxt_list_init(void)
2005 {
2006         /* Create the cntxt spin lock */
2007         rte_spinlock_init(&bnxt_ulp_ctxt_lock);
2008
2009         return 0;
2010 }
2011
2012 static int32_t
2013 bnxt_ulp_cntxt_list_add(struct bnxt_ulp_context *ulp_ctx)
2014 {
2015         struct ulp_context_list_entry   *entry;
2016
2017         entry = rte_zmalloc(NULL, sizeof(struct ulp_context_list_entry), 0);
2018         if (entry == NULL) {
2019                 BNXT_TF_DBG(ERR, "unable to allocate memory\n");
2020                 return -ENOMEM;
2021         }
2022
2023         rte_spinlock_lock(&bnxt_ulp_ctxt_lock);
2024         entry->ulp_ctx = ulp_ctx;
2025         TAILQ_INSERT_TAIL(&ulp_cntx_list, entry, next);
2026         rte_spinlock_unlock(&bnxt_ulp_ctxt_lock);
2027         return 0;
2028 }
2029
2030 static void
2031 bnxt_ulp_cntxt_list_del(struct bnxt_ulp_context *ulp_ctx)
2032 {
2033         struct ulp_context_list_entry   *entry, *temp;
2034
2035         rte_spinlock_lock(&bnxt_ulp_ctxt_lock);
2036         TAILQ_FOREACH_SAFE(entry, &ulp_cntx_list, next, temp) {
2037                 if (entry->ulp_ctx == ulp_ctx) {
2038                         TAILQ_REMOVE(&ulp_cntx_list, entry, next);
2039                         rte_free(entry);
2040                         break;
2041                 }
2042         }
2043         rte_spinlock_unlock(&bnxt_ulp_ctxt_lock);
2044 }
2045
2046 struct bnxt_ulp_context *
2047 bnxt_ulp_cntxt_entry_acquire(void)
2048 {
2049         struct ulp_context_list_entry   *entry;
2050
2051         /* take a lock and get the first ulp context available */
2052         if (rte_spinlock_trylock(&bnxt_ulp_ctxt_lock)) {
2053                 TAILQ_FOREACH(entry, &ulp_cntx_list, next)
2054                         if (entry->ulp_ctx)
2055                                 return entry->ulp_ctx;
2056         }
2057         return NULL;
2058 }
2059
2060 void
2061 bnxt_ulp_cntxt_entry_release(void)
2062 {
2063         rte_spinlock_unlock(&bnxt_ulp_ctxt_lock);
2064 }
2065
2066 /* Function to get the app tunnel details from the ulp context. */
2067 struct bnxt_flow_app_tun_ent *
2068 bnxt_ulp_cntxt_ptr2_app_tun_list_get(struct bnxt_ulp_context *ulp)
2069 {
2070         if (!ulp || !ulp->cfg_data)
2071                 return NULL;
2072
2073         return ulp->cfg_data->app_tun;
2074 }