a4d48c71ad79ce1dffb5bd53631067aa1bcf389e
[dpdk.git] / drivers / net / bnxt / tf_ulp / bnxt_ulp.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2019-2020 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
12 #include "bnxt.h"
13 #include "bnxt_ulp.h"
14 #include "bnxt_tf_common.h"
15 #include "tf_core.h"
16 #include "tf_ext_flow_handle.h"
17
18 #include "ulp_template_db_enum.h"
19 #include "ulp_template_struct.h"
20 #include "ulp_mark_mgr.h"
21 #include "ulp_fc_mgr.h"
22 #include "ulp_flow_db.h"
23 #include "ulp_mapper.h"
24 #include "ulp_port_db.h"
25
26 /* Linked list of all TF sessions. */
27 STAILQ_HEAD(, bnxt_ulp_session_state) bnxt_ulp_session_list =
28                         STAILQ_HEAD_INITIALIZER(bnxt_ulp_session_list);
29
30 /* Mutex to synchronize bnxt_ulp_session_list operations. */
31 static pthread_mutex_t bnxt_ulp_global_mutex = PTHREAD_MUTEX_INITIALIZER;
32
33 /*
34  * Allow the deletion of context only for the bnxt device that
35  * created the session.
36  */
37 bool
38 ulp_ctx_deinit_allowed(struct bnxt_ulp_context *ulp_ctx)
39 {
40         if (!ulp_ctx || !ulp_ctx->cfg_data)
41                 return false;
42
43         if (!ulp_ctx->cfg_data->ref_cnt) {
44                 BNXT_TF_DBG(DEBUG, "ulp ctx shall initiate deinit\n");
45                 return true;
46         }
47
48         return false;
49 }
50
51 /*
52  * Initialize an ULP session.
53  * An ULP session will contain all the resources needed to support rte flow
54  * offloads. A session is initialized as part of rte_eth_device start.
55  * A single vswitch instance can have multiple uplinks which means
56  * rte_eth_device start will be called for each of these devices.
57  * ULP session manager will make sure that a single ULP session is only
58  * initialized once. Apart from this, it also initializes MARK database,
59  * EEM table & flow database. ULP session manager also manages a list of
60  * all opened ULP sessions.
61  */
62 static int32_t
63 ulp_ctx_session_open(struct bnxt *bp,
64                      struct bnxt_ulp_session_state *session)
65 {
66         struct rte_eth_dev              *ethdev = bp->eth_dev;
67         int32_t                         rc = 0;
68         struct tf_open_session_parms    params;
69         struct tf_session_resources     *resources;
70
71         memset(&params, 0, sizeof(params));
72
73         rc = rte_eth_dev_get_name_by_port(ethdev->data->port_id,
74                                           params.ctrl_chan_name);
75         if (rc) {
76                 BNXT_TF_DBG(ERR, "Invalid port %d, rc = %d\n",
77                             ethdev->data->port_id, rc);
78                 return rc;
79         }
80
81         params.shadow_copy = true;
82         params.device_type = TF_DEVICE_TYPE_WH;
83         resources = &params.resources;
84         /** RX **/
85         /* Identifiers */
86         resources->ident_cnt[TF_DIR_RX].cnt[TF_IDENT_TYPE_L2_CTXT_HIGH] = 422;
87         resources->ident_cnt[TF_DIR_RX].cnt[TF_IDENT_TYPE_L2_CTXT_LOW] = 6;
88         resources->ident_cnt[TF_DIR_RX].cnt[TF_IDENT_TYPE_WC_PROF] = 192;
89         resources->ident_cnt[TF_DIR_RX].cnt[TF_IDENT_TYPE_PROF_FUNC] = 64;
90         resources->ident_cnt[TF_DIR_RX].cnt[TF_IDENT_TYPE_EM_PROF] = 192;
91
92         /* Table Types */
93         resources->tbl_cnt[TF_DIR_RX].cnt[TF_TBL_TYPE_FULL_ACT_RECORD] = 8192;
94         resources->tbl_cnt[TF_DIR_RX].cnt[TF_TBL_TYPE_ACT_STATS_64] = 16384;
95         resources->tbl_cnt[TF_DIR_RX].cnt[TF_TBL_TYPE_ACT_MODIFY_IPV4] = 1023;
96
97         /* ENCAP */
98         resources->tbl_cnt[TF_DIR_RX].cnt[TF_TBL_TYPE_ACT_ENCAP_8B] = 511;
99         resources->tbl_cnt[TF_DIR_RX].cnt[TF_TBL_TYPE_ACT_ENCAP_16B] = 63;
100
101         /* TCAMs */
102         resources->tcam_cnt[TF_DIR_RX].cnt[TF_TCAM_TBL_TYPE_L2_CTXT_TCAM_HIGH] =
103                 422;
104         resources->tcam_cnt[TF_DIR_RX].cnt[TF_TCAM_TBL_TYPE_L2_CTXT_TCAM_LOW] =
105                 6;
106         resources->tcam_cnt[TF_DIR_RX].cnt[TF_TCAM_TBL_TYPE_PROF_TCAM] = 960;
107         resources->tcam_cnt[TF_DIR_RX].cnt[TF_TCAM_TBL_TYPE_WC_TCAM] = 88;
108
109         /* EM */
110         resources->em_cnt[TF_DIR_RX].cnt[TF_EM_TBL_TYPE_EM_RECORD] = 13168;
111
112         /* EEM */
113         resources->em_cnt[TF_DIR_RX].cnt[TF_EM_TBL_TYPE_TBL_SCOPE] = 1;
114
115         /* SP */
116         resources->tbl_cnt[TF_DIR_RX].cnt[TF_TBL_TYPE_ACT_SP_SMAC] = 255;
117
118         /** TX **/
119         /* Identifiers */
120         resources->ident_cnt[TF_DIR_TX].cnt[TF_IDENT_TYPE_L2_CTXT_HIGH] = 292;
121         resources->ident_cnt[TF_DIR_TX].cnt[TF_IDENT_TYPE_L2_CTXT_LOW] = 148;
122         resources->ident_cnt[TF_DIR_TX].cnt[TF_IDENT_TYPE_WC_PROF] = 192;
123         resources->ident_cnt[TF_DIR_TX].cnt[TF_IDENT_TYPE_PROF_FUNC] = 64;
124         resources->ident_cnt[TF_DIR_TX].cnt[TF_IDENT_TYPE_EM_PROF] = 192;
125
126         /* Table Types */
127         resources->tbl_cnt[TF_DIR_TX].cnt[TF_TBL_TYPE_FULL_ACT_RECORD] = 8192;
128         resources->tbl_cnt[TF_DIR_TX].cnt[TF_TBL_TYPE_ACT_STATS_64] = 16384;
129         resources->tbl_cnt[TF_DIR_TX].cnt[TF_TBL_TYPE_ACT_MODIFY_IPV4] = 1023;
130
131         /* ENCAP */
132         resources->tbl_cnt[TF_DIR_TX].cnt[TF_TBL_TYPE_ACT_ENCAP_64B] = 511;
133         resources->tbl_cnt[TF_DIR_TX].cnt[TF_TBL_TYPE_ACT_ENCAP_16B] = 223;
134         resources->tbl_cnt[TF_DIR_TX].cnt[TF_TBL_TYPE_ACT_ENCAP_8B] = 255;
135
136         /* TCAMs */
137         resources->tcam_cnt[TF_DIR_TX].cnt[TF_TCAM_TBL_TYPE_L2_CTXT_TCAM_HIGH] =
138                 292;
139         resources->tcam_cnt[TF_DIR_TX].cnt[TF_TCAM_TBL_TYPE_L2_CTXT_TCAM_LOW] =
140                 144;
141         resources->tcam_cnt[TF_DIR_TX].cnt[TF_TCAM_TBL_TYPE_PROF_TCAM] = 960;
142         resources->tcam_cnt[TF_DIR_TX].cnt[TF_TCAM_TBL_TYPE_WC_TCAM] = 928;
143
144         /* EM */
145         resources->em_cnt[TF_DIR_TX].cnt[TF_EM_TBL_TYPE_EM_RECORD] = 15232;
146
147         /* EEM */
148         resources->em_cnt[TF_DIR_TX].cnt[TF_EM_TBL_TYPE_TBL_SCOPE] = 1;
149
150         /* SP */
151         resources->tbl_cnt[TF_DIR_TX].cnt[TF_TBL_TYPE_ACT_SP_SMAC_IPV4] = 488;
152         resources->tbl_cnt[TF_DIR_TX].cnt[TF_TBL_TYPE_ACT_SP_SMAC_IPV6] = 511;
153
154         rc = tf_open_session(&bp->tfp, &params);
155         if (rc) {
156                 BNXT_TF_DBG(ERR, "Failed to open TF session - %s, rc = %d\n",
157                             params.ctrl_chan_name, rc);
158                 return -EINVAL;
159         }
160         if (!session->session_opened) {
161                 session->session_opened = 1;
162                 session->g_tfp = rte_zmalloc("bnxt_ulp_session_tfp",
163                                              sizeof(struct tf), 0);
164                 session->g_tfp->session = bp->tfp.session;
165         }
166         return rc;
167 }
168
169 /*
170  * Close the ULP session.
171  * It takes the ulp context pointer.
172  */
173 static void
174 ulp_ctx_session_close(struct bnxt *bp,
175                       struct bnxt_ulp_session_state *session)
176 {
177         /* close the session in the hardware */
178         if (session->session_opened)
179                 tf_close_session(&bp->tfp);
180         session->session_opened = 0;
181         rte_free(session->g_tfp);
182         session->g_tfp = NULL;
183 }
184
185 static void
186 bnxt_init_tbl_scope_parms(struct bnxt *bp,
187                           struct tf_alloc_tbl_scope_parms *params)
188 {
189         struct bnxt_ulp_device_params   *dparms;
190         uint32_t dev_id;
191         int rc;
192
193         rc = bnxt_ulp_cntxt_dev_id_get(bp->ulp_ctx, &dev_id);
194         if (rc)
195                 /* TBD: For now, just use default. */
196                 dparms = 0;
197         else
198                 dparms = bnxt_ulp_device_params_get(dev_id);
199
200         /*
201          * Set the flush timer for EEM entries. The value is in 100ms intervals,
202          * so 100 is 10s.
203          */
204         params->hw_flow_cache_flush_timer = 100;
205
206         if (!dparms) {
207                 params->rx_max_key_sz_in_bits = BNXT_ULP_DFLT_RX_MAX_KEY;
208                 params->rx_max_action_entry_sz_in_bits =
209                         BNXT_ULP_DFLT_RX_MAX_ACTN_ENTRY;
210                 params->rx_mem_size_in_mb = BNXT_ULP_DFLT_RX_MEM;
211                 params->rx_num_flows_in_k = BNXT_ULP_RX_NUM_FLOWS;
212                 params->rx_tbl_if_id = BNXT_ULP_RX_TBL_IF_ID;
213
214                 params->tx_max_key_sz_in_bits = BNXT_ULP_DFLT_TX_MAX_KEY;
215                 params->tx_max_action_entry_sz_in_bits =
216                         BNXT_ULP_DFLT_TX_MAX_ACTN_ENTRY;
217                 params->tx_mem_size_in_mb = BNXT_ULP_DFLT_TX_MEM;
218                 params->tx_num_flows_in_k = BNXT_ULP_TX_NUM_FLOWS;
219                 params->tx_tbl_if_id = BNXT_ULP_TX_TBL_IF_ID;
220         } else {
221                 params->rx_max_key_sz_in_bits = BNXT_ULP_DFLT_RX_MAX_KEY;
222                 params->rx_max_action_entry_sz_in_bits =
223                         BNXT_ULP_DFLT_RX_MAX_ACTN_ENTRY;
224                 params->rx_mem_size_in_mb = BNXT_ULP_DFLT_RX_MEM;
225                 params->rx_num_flows_in_k = dparms->flow_db_num_entries / 1024;
226                 params->rx_tbl_if_id = BNXT_ULP_RX_TBL_IF_ID;
227
228                 params->tx_max_key_sz_in_bits = BNXT_ULP_DFLT_TX_MAX_KEY;
229                 params->tx_max_action_entry_sz_in_bits =
230                         BNXT_ULP_DFLT_TX_MAX_ACTN_ENTRY;
231                 params->tx_mem_size_in_mb = BNXT_ULP_DFLT_TX_MEM;
232                 params->tx_num_flows_in_k = dparms->flow_db_num_entries / 1024;
233                 params->tx_tbl_if_id = BNXT_ULP_TX_TBL_IF_ID;
234         }
235 }
236
237 /* Initialize Extended Exact Match host memory. */
238 static int32_t
239 ulp_eem_tbl_scope_init(struct bnxt *bp)
240 {
241         struct tf_alloc_tbl_scope_parms params = {0};
242         uint32_t dev_id;
243         struct bnxt_ulp_device_params *dparms;
244         int rc;
245
246         /* Get the dev specific number of flows that needed to be supported. */
247         if (bnxt_ulp_cntxt_dev_id_get(bp->ulp_ctx, &dev_id)) {
248                 BNXT_TF_DBG(ERR, "Invalid device id\n");
249                 return -EINVAL;
250         }
251
252         dparms = bnxt_ulp_device_params_get(dev_id);
253         if (!dparms) {
254                 BNXT_TF_DBG(ERR, "could not fetch the device params\n");
255                 return -ENODEV;
256         }
257
258         if (dparms->flow_mem_type != BNXT_ULP_FLOW_MEM_TYPE_EXT) {
259                 BNXT_TF_DBG(INFO, "Table Scope alloc is not required\n");
260                 return 0;
261         }
262
263         bnxt_init_tbl_scope_parms(bp, &params);
264
265         rc = tf_alloc_tbl_scope(&bp->tfp, &params);
266         if (rc) {
267                 BNXT_TF_DBG(ERR, "Unable to allocate eem table scope rc = %d\n",
268                             rc);
269                 return rc;
270         }
271
272         rc = bnxt_ulp_cntxt_tbl_scope_id_set(bp->ulp_ctx, params.tbl_scope_id);
273         if (rc) {
274                 BNXT_TF_DBG(ERR, "Unable to set table scope id\n");
275                 return rc;
276         }
277
278         return 0;
279 }
280
281 /* Free Extended Exact Match host memory */
282 static int32_t
283 ulp_eem_tbl_scope_deinit(struct bnxt *bp, struct bnxt_ulp_context *ulp_ctx)
284 {
285         struct tf_free_tbl_scope_parms  params = {0};
286         struct tf                       *tfp;
287         int32_t                         rc = 0;
288         struct bnxt_ulp_device_params *dparms;
289         uint32_t dev_id;
290
291         if (!ulp_ctx || !ulp_ctx->cfg_data)
292                 return -EINVAL;
293
294         tfp = bnxt_ulp_cntxt_tfp_get(ulp_ctx);
295         if (!tfp) {
296                 BNXT_TF_DBG(ERR, "Failed to get the truflow pointer\n");
297                 return -EINVAL;
298         }
299
300         /* Get the dev specific number of flows that needed to be supported. */
301         if (bnxt_ulp_cntxt_dev_id_get(bp->ulp_ctx, &dev_id)) {
302                 BNXT_TF_DBG(ERR, "Invalid device id\n");
303                 return -EINVAL;
304         }
305
306         dparms = bnxt_ulp_device_params_get(dev_id);
307         if (!dparms) {
308                 BNXT_TF_DBG(ERR, "could not fetch the device params\n");
309                 return -ENODEV;
310         }
311
312         if (dparms->flow_mem_type != BNXT_ULP_FLOW_MEM_TYPE_EXT) {
313                 BNXT_TF_DBG(INFO, "Table Scope free is not required\n");
314                 return 0;
315         }
316
317         rc = bnxt_ulp_cntxt_tbl_scope_id_get(ulp_ctx, &params.tbl_scope_id);
318         if (rc) {
319                 BNXT_TF_DBG(ERR, "Failed to get the table scope id\n");
320                 return -EINVAL;
321         }
322
323         rc = tf_free_tbl_scope(tfp, &params);
324         if (rc) {
325                 BNXT_TF_DBG(ERR, "Unable to free table scope\n");
326                 return -EINVAL;
327         }
328         return rc;
329 }
330
331 /* The function to free and deinit the ulp context data. */
332 static int32_t
333 ulp_ctx_deinit(struct bnxt *bp,
334                struct bnxt_ulp_session_state *session)
335 {
336         /* close the tf session */
337         ulp_ctx_session_close(bp, session);
338
339         /* Free the contents */
340         if (session->cfg_data) {
341                 rte_free(session->cfg_data);
342                 bp->ulp_ctx->cfg_data = NULL;
343                 session->cfg_data = NULL;
344         }
345         return 0;
346 }
347
348 /* The function to allocate and initialize the ulp context data. */
349 static int32_t
350 ulp_ctx_init(struct bnxt *bp,
351              struct bnxt_ulp_session_state *session)
352 {
353         struct bnxt_ulp_data    *ulp_data;
354         int32_t                 rc = 0;
355
356         /* Allocate memory to hold ulp context data. */
357         ulp_data = rte_zmalloc("bnxt_ulp_data",
358                                sizeof(struct bnxt_ulp_data), 0);
359         if (!ulp_data) {
360                 BNXT_TF_DBG(ERR, "Failed to allocate memory for ulp data\n");
361                 return -ENOMEM;
362         }
363
364         /* Increment the ulp context data reference count usage. */
365         bp->ulp_ctx->cfg_data = ulp_data;
366         session->cfg_data = ulp_data;
367         ulp_data->ref_cnt++;
368         ulp_data->ulp_flags |= BNXT_ULP_VF_REP_ENABLED;
369
370         /* Open the ulp session. */
371         rc = ulp_ctx_session_open(bp, session);
372         if (rc) {
373                 session->session_opened = 1;
374                 (void)ulp_ctx_deinit(bp, session);
375                 return rc;
376         }
377
378         bnxt_ulp_cntxt_tfp_set(bp->ulp_ctx, &bp->tfp);
379         return rc;
380 }
381
382 /* The function to initialize ulp dparms with devargs */
383 static int32_t
384 ulp_dparms_init(struct bnxt *bp,
385                 struct bnxt_ulp_context *ulp_ctx)
386 {
387         struct bnxt_ulp_device_params *dparms;
388         uint32_t dev_id;
389
390         if (!bp->max_num_kflows)
391                 return 0;
392
393         if (bnxt_ulp_cntxt_dev_id_get(ulp_ctx, &dev_id)) {
394                 BNXT_TF_DBG(DEBUG, "Failed to get device id\n");
395                 return -EINVAL;
396         }
397
398         dparms = bnxt_ulp_device_params_get(dev_id);
399         if (!dparms) {
400                 BNXT_TF_DBG(DEBUG, "Failed to get device parms\n");
401                 return -EINVAL;
402         }
403
404         /* num_flows = max_num_kflows * 1024 */
405         dparms->flow_db_num_entries = bp->max_num_kflows * 1024;
406         /* GFID =  2 * num_flows */
407         dparms->mark_db_gfid_entries = dparms->flow_db_num_entries * 2;
408         BNXT_TF_DBG(DEBUG, "Set the number of flows = %"PRIu64"\n",
409                     dparms->flow_db_num_entries);
410
411         return 0;
412 }
413
414 /* The function to initialize bp flags with truflow features */
415 static int32_t
416 ulp_dparms_dev_port_intf_update(struct bnxt *bp,
417                                 struct bnxt_ulp_context *ulp_ctx)
418 {
419         struct bnxt_ulp_device_params *dparms;
420         uint32_t dev_id;
421
422         if (bnxt_ulp_cntxt_dev_id_get(ulp_ctx, &dev_id)) {
423                 BNXT_TF_DBG(DEBUG, "Failed to get device id\n");
424                 return -EINVAL;
425         }
426
427         dparms = bnxt_ulp_device_params_get(dev_id);
428         if (!dparms) {
429                 BNXT_TF_DBG(DEBUG, "Failed to get device parms\n");
430                 return -EINVAL;
431         }
432
433         /* Update the bp flag with gfid flag */
434         if (dparms->flow_mem_type == BNXT_ULP_FLOW_MEM_TYPE_EXT)
435                 bp->flags |= BNXT_FLAG_GFID_ENABLE;
436
437         return 0;
438 }
439
440 static int32_t
441 ulp_ctx_attach(struct bnxt *bp,
442                struct bnxt_ulp_session_state *session)
443 {
444         int32_t rc = 0;
445
446         /* Increment the ulp context data reference count usage. */
447         bp->ulp_ctx->cfg_data = session->cfg_data;
448         bp->ulp_ctx->cfg_data->ref_cnt++;
449
450         /* update the session details in bnxt tfp */
451         bp->tfp.session = session->g_tfp->session;
452
453         /* Create a TF Client */
454         rc = ulp_ctx_session_open(bp, session);
455         if (rc) {
456                 PMD_DRV_LOG(ERR, "Failed to open ctxt session, rc:%d\n", rc);
457                 bp->tfp.session = NULL;
458                 return rc;
459         }
460
461         bnxt_ulp_cntxt_tfp_set(bp->ulp_ctx, &bp->tfp);
462         return rc;
463 }
464
465 static void
466 ulp_ctx_detach(struct bnxt *bp)
467 {
468         if (bp->tfp.session) {
469                 tf_close_session(&bp->tfp);
470                 bp->tfp.session = NULL;
471         }
472 }
473
474 /*
475  * Initialize the state of an ULP session.
476  * If the state of an ULP session is not initialized, set it's state to
477  * initialized. If the state is already initialized, do nothing.
478  */
479 static void
480 ulp_context_initialized(struct bnxt_ulp_session_state *session, bool *init)
481 {
482         pthread_mutex_lock(&session->bnxt_ulp_mutex);
483
484         if (!session->bnxt_ulp_init) {
485                 session->bnxt_ulp_init = true;
486                 *init = false;
487         } else {
488                 *init = true;
489         }
490
491         pthread_mutex_unlock(&session->bnxt_ulp_mutex);
492 }
493
494 /*
495  * Check if an ULP session is already allocated for a specific PCI
496  * domain & bus. If it is already allocated simply return the session
497  * pointer, otherwise allocate a new session.
498  */
499 static struct bnxt_ulp_session_state *
500 ulp_get_session(struct rte_pci_addr *pci_addr)
501 {
502         struct bnxt_ulp_session_state *session;
503
504         STAILQ_FOREACH(session, &bnxt_ulp_session_list, next) {
505                 if (session->pci_info.domain == pci_addr->domain &&
506                     session->pci_info.bus == pci_addr->bus) {
507                         return session;
508                 }
509         }
510         return NULL;
511 }
512
513 /*
514  * Allocate and Initialize an ULP session and set it's state to INITIALIZED.
515  * If it's already initialized simply return the already existing session.
516  */
517 static struct bnxt_ulp_session_state *
518 ulp_session_init(struct bnxt *bp,
519                  bool *init)
520 {
521         struct rte_pci_device           *pci_dev;
522         struct rte_pci_addr             *pci_addr;
523         struct bnxt_ulp_session_state   *session;
524         int rc = 0;
525
526         if (!bp)
527                 return NULL;
528
529         pci_dev = RTE_DEV_TO_PCI(bp->eth_dev->device);
530         pci_addr = &pci_dev->addr;
531
532         pthread_mutex_lock(&bnxt_ulp_global_mutex);
533
534         session = ulp_get_session(pci_addr);
535         if (!session) {
536                 /* Not Found the session  Allocate a new one */
537                 session = rte_zmalloc("bnxt_ulp_session",
538                                       sizeof(struct bnxt_ulp_session_state),
539                                       0);
540                 if (!session) {
541                         BNXT_TF_DBG(ERR,
542                                     "Allocation failed for bnxt_ulp_session\n");
543                         pthread_mutex_unlock(&bnxt_ulp_global_mutex);
544                         return NULL;
545
546                 } else {
547                         /* Add it to the queue */
548                         session->pci_info.domain = pci_addr->domain;
549                         session->pci_info.bus = pci_addr->bus;
550                         rc = pthread_mutex_init(&session->bnxt_ulp_mutex, NULL);
551                         if (rc) {
552                                 BNXT_TF_DBG(ERR, "mutex create failed\n");
553                                 pthread_mutex_unlock(&bnxt_ulp_global_mutex);
554                                 return NULL;
555                         }
556                         STAILQ_INSERT_TAIL(&bnxt_ulp_session_list,
557                                            session, next);
558                 }
559         }
560         ulp_context_initialized(session, init);
561         pthread_mutex_unlock(&bnxt_ulp_global_mutex);
562         return session;
563 }
564
565 /*
566  * When a device is closed, remove it's associated session from the global
567  * session list.
568  */
569 static void
570 ulp_session_deinit(struct bnxt_ulp_session_state *session)
571 {
572         if (!session)
573                 return;
574
575         if (!session->cfg_data) {
576                 pthread_mutex_lock(&bnxt_ulp_global_mutex);
577                 STAILQ_REMOVE(&bnxt_ulp_session_list, session,
578                               bnxt_ulp_session_state, next);
579                 pthread_mutex_destroy(&session->bnxt_ulp_mutex);
580                 rte_free(session);
581                 pthread_mutex_unlock(&bnxt_ulp_global_mutex);
582         }
583 }
584
585 /*
586  * Internal api to enable NAT feature.
587  * Set set_flag to 1 to set the value or zero to reset the value.
588  * returns 0 on success.
589  */
590 static int32_t
591 bnxt_ulp_global_cfg_update(struct bnxt *bp,
592                            enum tf_dir dir,
593                            enum tf_global_config_type type,
594                            uint32_t offset,
595                            uint32_t value,
596                            uint32_t set_flag)
597 {
598         uint32_t global_cfg = 0;
599         int rc;
600         struct tf_global_cfg_parms parms = { 0 };
601
602         /* Initialize the params */
603         parms.dir = dir,
604         parms.type = type,
605         parms.offset = offset,
606         parms.config = (uint8_t *)&global_cfg,
607         parms.config_sz_in_bytes = sizeof(global_cfg);
608
609         rc = tf_get_global_cfg(&bp->tfp, &parms);
610         if (rc) {
611                 BNXT_TF_DBG(ERR, "Failed to get global cfg 0x%x rc:%d\n",
612                             type, rc);
613                 return rc;
614         }
615
616         if (set_flag)
617                 global_cfg |= value;
618         else
619                 global_cfg &= ~value;
620
621         /* SET the register RE_CFA_REG_ACT_TECT */
622         rc = tf_set_global_cfg(&bp->tfp, &parms);
623         if (rc) {
624                 BNXT_TF_DBG(ERR, "Failed to set global cfg 0x%x rc:%d\n",
625                             type, rc);
626                 return rc;
627         }
628         return rc;
629 }
630
631 /* Internal function to delete all the flows belonging to the given port */
632 static void
633 bnxt_ulp_flush_port_flows(struct bnxt *bp)
634 {
635         uint16_t func_id;
636
637         /* it is assumed that port is either TVF or PF */
638         if (ulp_port_db_port_func_id_get(bp->ulp_ctx,
639                                          bp->eth_dev->data->port_id,
640                                          &func_id)) {
641                 BNXT_TF_DBG(ERR, "Invalid argument\n");
642                 return;
643         }
644         (void)ulp_flow_db_function_flow_flush(bp->ulp_ctx, func_id);
645 }
646
647 /* Internal function to delete the VFR default flows */
648 static void
649 bnxt_ulp_destroy_vfr_default_rules(struct bnxt *bp, bool global)
650 {
651         struct bnxt_ulp_vfr_rule_info *info;
652         uint16_t port_id;
653         struct rte_eth_dev *vfr_eth_dev;
654         struct bnxt_representor *vfr_bp;
655
656         if (!BNXT_TRUFLOW_EN(bp) || BNXT_ETH_DEV_IS_REPRESENTOR(bp->eth_dev))
657                 return;
658
659         if (!bp->ulp_ctx || !bp->ulp_ctx->cfg_data)
660                 return;
661
662         /* Delete default rules for all ports */
663         for (port_id = 0; port_id < RTE_MAX_ETHPORTS; port_id++) {
664                 info = &bp->ulp_ctx->cfg_data->vfr_rule_info[port_id];
665                 if (!info->valid)
666                         continue;
667
668                 if (!global && info->parent_port_id !=
669                     bp->eth_dev->data->port_id)
670                         continue;
671
672                 /* Destroy the flows */
673                 ulp_default_flow_destroy(bp->eth_dev, info->rep2vf_flow_id);
674                 ulp_default_flow_destroy(bp->eth_dev, info->vf2rep_flow_id);
675                 /* Clean up the tx action pointer */
676                 vfr_eth_dev = &rte_eth_devices[port_id];
677                 if (vfr_eth_dev) {
678                         vfr_bp = vfr_eth_dev->data->dev_private;
679                         vfr_bp->vfr_tx_cfa_action = 0;
680                 }
681                 memset(info, 0, sizeof(struct bnxt_ulp_vfr_rule_info));
682         }
683 }
684
685 /*
686  * When a port is deinit'ed by dpdk. This function is called
687  * and this function clears the ULP context and rest of the
688  * infrastructure associated with it.
689  */
690 static void
691 bnxt_ulp_deinit(struct bnxt *bp,
692                 struct bnxt_ulp_session_state *session)
693 {
694         if (!bp->ulp_ctx || !bp->ulp_ctx->cfg_data)
695                 return;
696
697         /* clean up default flows */
698         bnxt_ulp_destroy_df_rules(bp, true);
699
700         /* clean up default VFR flows */
701         bnxt_ulp_destroy_vfr_default_rules(bp, true);
702
703         /* clean up regular flows */
704         ulp_flow_db_flush_flows(bp->ulp_ctx, BNXT_ULP_REGULAR_FLOW_TABLE);
705
706         /* cleanup the eem table scope */
707         ulp_eem_tbl_scope_deinit(bp, bp->ulp_ctx);
708
709         /* cleanup the flow database */
710         ulp_flow_db_deinit(bp->ulp_ctx);
711
712         /* Delete the Mark database */
713         ulp_mark_db_deinit(bp->ulp_ctx);
714
715         /* cleanup the ulp mapper */
716         ulp_mapper_deinit(bp->ulp_ctx);
717
718         /* Delete the Flow Counter Manager */
719         ulp_fc_mgr_deinit(bp->ulp_ctx);
720
721         /* Delete the Port database */
722         ulp_port_db_deinit(bp->ulp_ctx);
723
724         /* Disable NAT feature */
725         (void)bnxt_ulp_global_cfg_update(bp, TF_DIR_RX, TF_TUNNEL_ENCAP,
726                                          TF_TUNNEL_ENCAP_NAT,
727                                          BNXT_ULP_NAT_OUTER_MOST_FLAGS, 0);
728
729         (void)bnxt_ulp_global_cfg_update(bp, TF_DIR_TX, TF_TUNNEL_ENCAP,
730                                          TF_TUNNEL_ENCAP_NAT,
731                                          BNXT_ULP_NAT_OUTER_MOST_FLAGS, 0);
732
733         /* free the flow db lock */
734         pthread_mutex_destroy(&bp->ulp_ctx->cfg_data->flow_db_lock);
735
736         /* Delete the ulp context and tf session and free the ulp context */
737         ulp_ctx_deinit(bp, session);
738         BNXT_TF_DBG(DEBUG, "ulp ctx has been deinitialized\n");
739 }
740
741 /*
742  * When a port is initialized by dpdk. This functions is called
743  * and this function initializes the ULP context and rest of the
744  * infrastructure associated with it.
745  */
746 static int32_t
747 bnxt_ulp_init(struct bnxt *bp,
748               struct bnxt_ulp_session_state *session)
749 {
750         int rc;
751
752         /* Allocate and Initialize the ulp context. */
753         rc = ulp_ctx_init(bp, session);
754         if (rc) {
755                 BNXT_TF_DBG(ERR, "Failed to create the ulp context\n");
756                 goto jump_to_error;
757         }
758
759         rc = pthread_mutex_init(&bp->ulp_ctx->cfg_data->flow_db_lock, NULL);
760         if (rc) {
761                 BNXT_TF_DBG(ERR, "Unable to initialize flow db lock\n");
762                 goto jump_to_error;
763         }
764
765         /* Initialize ulp dparms with values devargs passed */
766         rc = ulp_dparms_init(bp, bp->ulp_ctx);
767         if (rc) {
768                 BNXT_TF_DBG(ERR, "Failed to initialize the dparms\n");
769                 goto jump_to_error;
770         }
771
772         /* create the port database */
773         rc = ulp_port_db_init(bp->ulp_ctx, bp->port_cnt);
774         if (rc) {
775                 BNXT_TF_DBG(ERR, "Failed to create the port database\n");
776                 goto jump_to_error;
777         }
778
779         /* Create the Mark database. */
780         rc = ulp_mark_db_init(bp->ulp_ctx);
781         if (rc) {
782                 BNXT_TF_DBG(ERR, "Failed to create the mark database\n");
783                 goto jump_to_error;
784         }
785
786         /* Create the flow database. */
787         rc = ulp_flow_db_init(bp->ulp_ctx);
788         if (rc) {
789                 BNXT_TF_DBG(ERR, "Failed to create the flow database\n");
790                 goto jump_to_error;
791         }
792
793         /* Create the eem table scope. */
794         rc = ulp_eem_tbl_scope_init(bp);
795         if (rc) {
796                 BNXT_TF_DBG(ERR, "Failed to create the eem scope table\n");
797                 goto jump_to_error;
798         }
799
800         rc = ulp_mapper_init(bp->ulp_ctx);
801         if (rc) {
802                 BNXT_TF_DBG(ERR, "Failed to initialize ulp mapper\n");
803                 goto jump_to_error;
804         }
805
806         rc = ulp_fc_mgr_init(bp->ulp_ctx);
807         if (rc) {
808                 BNXT_TF_DBG(ERR, "Failed to initialize ulp flow counter mgr\n");
809                 goto jump_to_error;
810         }
811
812         /*
813          * Enable NAT feature. Set the global configuration register
814          * Tunnel encap to enable NAT with the reuse of existing inner
815          * L2 header smac and dmac
816          */
817         rc = bnxt_ulp_global_cfg_update(bp, TF_DIR_RX, TF_TUNNEL_ENCAP,
818                                         TF_TUNNEL_ENCAP_NAT,
819                                         BNXT_ULP_NAT_OUTER_MOST_FLAGS, 1);
820         if (rc) {
821                 BNXT_TF_DBG(ERR, "Failed to set rx global configuration\n");
822                 goto jump_to_error;
823         }
824
825         rc = bnxt_ulp_global_cfg_update(bp, TF_DIR_TX, TF_TUNNEL_ENCAP,
826                                         TF_TUNNEL_ENCAP_NAT,
827                                         BNXT_ULP_NAT_OUTER_MOST_FLAGS, 1);
828         if (rc) {
829                 BNXT_TF_DBG(ERR, "Failed to set tx global configuration\n");
830                 goto jump_to_error;
831         }
832         BNXT_TF_DBG(DEBUG, "ulp ctx has been initialized\n");
833         return rc;
834
835 jump_to_error:
836         bnxt_ulp_deinit(bp, session);
837         return rc;
838 }
839
840 /*
841  * When a port is initialized by dpdk. This functions sets up
842  * the port specific details.
843  */
844 int32_t
845 bnxt_ulp_port_init(struct bnxt *bp)
846 {
847         struct bnxt_ulp_session_state *session;
848         bool initialized;
849         int32_t rc = 0;
850
851         if (!bp || !BNXT_TRUFLOW_EN(bp))
852                 return rc;
853
854         if (!BNXT_PF(bp) && !BNXT_VF_IS_TRUSTED(bp)) {
855                 BNXT_TF_DBG(ERR,
856                             "Skip ulp init for port: %d, not a TVF or PF\n",
857                         bp->eth_dev->data->port_id);
858                 return rc;
859         }
860
861         if (bp->ulp_ctx) {
862                 BNXT_TF_DBG(DEBUG, "ulp ctx already allocated\n");
863                 return rc;
864         }
865
866         bp->ulp_ctx = rte_zmalloc("bnxt_ulp_ctx",
867                                   sizeof(struct bnxt_ulp_context), 0);
868         if (!bp->ulp_ctx) {
869                 BNXT_TF_DBG(ERR, "Failed to allocate ulp ctx\n");
870                 return -ENOMEM;
871         }
872
873         /*
874          * Multiple uplink ports can be associated with a single vswitch.
875          * Make sure only the port that is started first will initialize
876          * the TF session.
877          */
878         session = ulp_session_init(bp, &initialized);
879         if (!session) {
880                 BNXT_TF_DBG(ERR, "Failed to initialize the tf session\n");
881                 rc = -EIO;
882                 goto jump_to_error;
883         }
884
885         if (initialized) {
886                 /*
887                  * If ULP is already initialized for a specific domain then
888                  * simply assign the ulp context to this rte_eth_dev.
889                  */
890                 rc = ulp_ctx_attach(bp, session);
891                 if (rc) {
892                         BNXT_TF_DBG(ERR, "Failed to attach the ulp context\n");
893                         goto jump_to_error;
894                 }
895         } else {
896                 rc = bnxt_ulp_init(bp, session);
897                 if (rc) {
898                         BNXT_TF_DBG(ERR, "Failed to initialize the ulp init\n");
899                         goto jump_to_error;
900                 }
901         }
902
903         /* Update bnxt driver flags */
904         rc = ulp_dparms_dev_port_intf_update(bp, bp->ulp_ctx);
905         if (rc) {
906                 BNXT_TF_DBG(ERR, "Failed to update driver flags\n");
907                 goto jump_to_error;
908         }
909
910         /* update the port database for the given interface */
911         rc = ulp_port_db_dev_port_intf_update(bp->ulp_ctx, bp->eth_dev);
912         if (rc) {
913                 BNXT_TF_DBG(ERR, "Failed to update port database\n");
914                 goto jump_to_error;
915         }
916         /* create the default rules */
917         bnxt_ulp_create_df_rules(bp);
918         BNXT_TF_DBG(DEBUG, "BNXT Port:%d ULP port init\n",
919                     bp->eth_dev->data->port_id);
920         return rc;
921
922 jump_to_error:
923         bnxt_ulp_port_deinit(bp);
924         return rc;
925 }
926
927 /*
928  * When a port is de-initialized by dpdk. This functions clears up
929  * the port specific details.
930  */
931 void
932 bnxt_ulp_port_deinit(struct bnxt *bp)
933 {
934         struct bnxt_ulp_session_state *session;
935         struct rte_pci_device *pci_dev;
936         struct rte_pci_addr *pci_addr;
937
938         if (!BNXT_TRUFLOW_EN(bp))
939                 return;
940
941         if (!BNXT_PF(bp) && !BNXT_VF_IS_TRUSTED(bp)) {
942                 BNXT_TF_DBG(ERR,
943                             "Skip ULP deinit port:%d, not a TVF or PF\n",
944                             bp->eth_dev->data->port_id);
945                 return;
946         }
947
948         if (!bp->ulp_ctx) {
949                 BNXT_TF_DBG(DEBUG, "ulp ctx already de-allocated\n");
950                 return;
951         }
952
953         BNXT_TF_DBG(DEBUG, "BNXT Port:%d ULP port deinit\n",
954                     bp->eth_dev->data->port_id);
955
956         /* Get the session details  */
957         pci_dev = RTE_DEV_TO_PCI(bp->eth_dev->device);
958         pci_addr = &pci_dev->addr;
959         pthread_mutex_lock(&bnxt_ulp_global_mutex);
960         session = ulp_get_session(pci_addr);
961         pthread_mutex_unlock(&bnxt_ulp_global_mutex);
962
963         /* session not found then just exit */
964         if (!session) {
965                 /* Free the ulp context */
966                 rte_free(bp->ulp_ctx);
967                 bp->ulp_ctx = NULL;
968                 return;
969         }
970
971         /* Check the reference count to deinit or deattach*/
972         if (bp->ulp_ctx->cfg_data && bp->ulp_ctx->cfg_data->ref_cnt) {
973                 bp->ulp_ctx->cfg_data->ref_cnt--;
974                 if (bp->ulp_ctx->cfg_data->ref_cnt) {
975                         /* free the port details */
976                         /* Free the default flow rule associated to this port */
977                         bnxt_ulp_destroy_df_rules(bp, false);
978                         bnxt_ulp_destroy_vfr_default_rules(bp, false);
979
980                         /* free flows associated with this port */
981                         bnxt_ulp_flush_port_flows(bp);
982
983                         /* close the session associated with this port */
984                         ulp_ctx_detach(bp);
985                 } else {
986                         /* Perform ulp ctx deinit */
987                         bnxt_ulp_deinit(bp, session);
988                 }
989         }
990
991         /* clean up the session */
992         ulp_session_deinit(session);
993
994         /* Free the ulp context */
995         rte_free(bp->ulp_ctx);
996         bp->ulp_ctx = NULL;
997 }
998
999 /* Below are the access functions to access internal data of ulp context. */
1000 /* Function to set the Mark DB into the context */
1001 int32_t
1002 bnxt_ulp_cntxt_ptr2_mark_db_set(struct bnxt_ulp_context *ulp_ctx,
1003                                 struct bnxt_ulp_mark_tbl *mark_tbl)
1004 {
1005         if (!ulp_ctx || !ulp_ctx->cfg_data) {
1006                 BNXT_TF_DBG(ERR, "Invalid ulp context data\n");
1007                 return -EINVAL;
1008         }
1009
1010         ulp_ctx->cfg_data->mark_tbl = mark_tbl;
1011
1012         return 0;
1013 }
1014
1015 /* Function to retrieve the Mark DB from the context. */
1016 struct bnxt_ulp_mark_tbl *
1017 bnxt_ulp_cntxt_ptr2_mark_db_get(struct bnxt_ulp_context *ulp_ctx)
1018 {
1019         if (!ulp_ctx || !ulp_ctx->cfg_data)
1020                 return NULL;
1021
1022         return ulp_ctx->cfg_data->mark_tbl;
1023 }
1024
1025 /* Function to set the device id of the hardware. */
1026 int32_t
1027 bnxt_ulp_cntxt_dev_id_set(struct bnxt_ulp_context *ulp_ctx,
1028                           uint32_t dev_id)
1029 {
1030         if (ulp_ctx && ulp_ctx->cfg_data) {
1031                 ulp_ctx->cfg_data->dev_id = dev_id;
1032                 return 0;
1033         }
1034
1035         return -EINVAL;
1036 }
1037
1038 /* Function to get the device id of the hardware. */
1039 int32_t
1040 bnxt_ulp_cntxt_dev_id_get(struct bnxt_ulp_context *ulp_ctx,
1041                           uint32_t *dev_id)
1042 {
1043         if (ulp_ctx && ulp_ctx->cfg_data) {
1044                 *dev_id = ulp_ctx->cfg_data->dev_id;
1045                 return 0;
1046         }
1047
1048         return -EINVAL;
1049 }
1050
1051 /* Function to get the table scope id of the EEM table. */
1052 int32_t
1053 bnxt_ulp_cntxt_tbl_scope_id_get(struct bnxt_ulp_context *ulp_ctx,
1054                                 uint32_t *tbl_scope_id)
1055 {
1056         if (ulp_ctx && ulp_ctx->cfg_data) {
1057                 *tbl_scope_id = ulp_ctx->cfg_data->tbl_scope_id;
1058                 return 0;
1059         }
1060
1061         return -EINVAL;
1062 }
1063
1064 /* Function to set the table scope id of the EEM table. */
1065 int32_t
1066 bnxt_ulp_cntxt_tbl_scope_id_set(struct bnxt_ulp_context *ulp_ctx,
1067                                 uint32_t tbl_scope_id)
1068 {
1069         if (ulp_ctx && ulp_ctx->cfg_data) {
1070                 ulp_ctx->cfg_data->tbl_scope_id = tbl_scope_id;
1071                 return 0;
1072         }
1073
1074         return -EINVAL;
1075 }
1076
1077 /* Function to set the tfp session details from the ulp context. */
1078 int32_t
1079 bnxt_ulp_cntxt_tfp_set(struct bnxt_ulp_context *ulp, struct tf *tfp)
1080 {
1081         if (!ulp) {
1082                 BNXT_TF_DBG(ERR, "Invalid arguments\n");
1083                 return -EINVAL;
1084         }
1085
1086         ulp->g_tfp = tfp;
1087         return 0;
1088 }
1089
1090 /* Function to get the tfp session details from the ulp context. */
1091 struct tf *
1092 bnxt_ulp_cntxt_tfp_get(struct bnxt_ulp_context *ulp)
1093 {
1094         if (!ulp) {
1095                 BNXT_TF_DBG(ERR, "Invalid arguments\n");
1096                 return NULL;
1097         }
1098         return ulp->g_tfp;
1099 }
1100
1101 /*
1102  * Get the device table entry based on the device id.
1103  *
1104  * dev_id [in] The device id of the hardware
1105  *
1106  * Returns the pointer to the device parameters.
1107  */
1108 struct bnxt_ulp_device_params *
1109 bnxt_ulp_device_params_get(uint32_t dev_id)
1110 {
1111         if (dev_id < BNXT_ULP_MAX_NUM_DEVICES)
1112                 return &ulp_device_params[dev_id];
1113         return NULL;
1114 }
1115
1116 /* Function to set the flow database to the ulp context. */
1117 int32_t
1118 bnxt_ulp_cntxt_ptr2_flow_db_set(struct bnxt_ulp_context *ulp_ctx,
1119                                 struct bnxt_ulp_flow_db *flow_db)
1120 {
1121         if (!ulp_ctx || !ulp_ctx->cfg_data)
1122                 return -EINVAL;
1123
1124         ulp_ctx->cfg_data->flow_db = flow_db;
1125         return 0;
1126 }
1127
1128 /* Function to get the flow database from the ulp context. */
1129 struct bnxt_ulp_flow_db *
1130 bnxt_ulp_cntxt_ptr2_flow_db_get(struct bnxt_ulp_context *ulp_ctx)
1131 {
1132         if (!ulp_ctx || !ulp_ctx->cfg_data)
1133                 return NULL;
1134
1135         return ulp_ctx->cfg_data->flow_db;
1136 }
1137
1138 /* Function to get the ulp context from eth device. */
1139 struct bnxt_ulp_context *
1140 bnxt_ulp_eth_dev_ptr2_cntxt_get(struct rte_eth_dev      *dev)
1141 {
1142         struct bnxt *bp = (struct bnxt *)dev->data->dev_private;
1143
1144         if (BNXT_ETH_DEV_IS_REPRESENTOR(dev)) {
1145                 struct bnxt_representor *vfr = dev->data->dev_private;
1146
1147                 bp = vfr->parent_dev->data->dev_private;
1148         }
1149
1150         if (!bp) {
1151                 BNXT_TF_DBG(ERR, "Bnxt private data is not initialized\n");
1152                 return NULL;
1153         }
1154         return bp->ulp_ctx;
1155 }
1156
1157 int32_t
1158 bnxt_ulp_cntxt_ptr2_mapper_data_set(struct bnxt_ulp_context *ulp_ctx,
1159                                     void *mapper_data)
1160 {
1161         if (!ulp_ctx || !ulp_ctx->cfg_data) {
1162                 BNXT_TF_DBG(ERR, "Invalid ulp context data\n");
1163                 return -EINVAL;
1164         }
1165
1166         ulp_ctx->cfg_data->mapper_data = mapper_data;
1167         return 0;
1168 }
1169
1170 void *
1171 bnxt_ulp_cntxt_ptr2_mapper_data_get(struct bnxt_ulp_context *ulp_ctx)
1172 {
1173         if (!ulp_ctx || !ulp_ctx->cfg_data) {
1174                 BNXT_TF_DBG(ERR, "Invalid ulp context data\n");
1175                 return NULL;
1176         }
1177
1178         return ulp_ctx->cfg_data->mapper_data;
1179 }
1180
1181 /* Function to set the port database to the ulp context. */
1182 int32_t
1183 bnxt_ulp_cntxt_ptr2_port_db_set(struct bnxt_ulp_context *ulp_ctx,
1184                                 struct bnxt_ulp_port_db *port_db)
1185 {
1186         if (!ulp_ctx || !ulp_ctx->cfg_data)
1187                 return -EINVAL;
1188
1189         ulp_ctx->cfg_data->port_db = port_db;
1190         return 0;
1191 }
1192
1193 /* Function to get the port database from the ulp context. */
1194 struct bnxt_ulp_port_db *
1195 bnxt_ulp_cntxt_ptr2_port_db_get(struct bnxt_ulp_context *ulp_ctx)
1196 {
1197         if (!ulp_ctx || !ulp_ctx->cfg_data)
1198                 return NULL;
1199
1200         return ulp_ctx->cfg_data->port_db;
1201 }
1202
1203 /* Function to set the flow counter info into the context */
1204 int32_t
1205 bnxt_ulp_cntxt_ptr2_fc_info_set(struct bnxt_ulp_context *ulp_ctx,
1206                                 struct bnxt_ulp_fc_info *ulp_fc_info)
1207 {
1208         if (!ulp_ctx || !ulp_ctx->cfg_data) {
1209                 BNXT_TF_DBG(ERR, "Invalid ulp context data\n");
1210                 return -EINVAL;
1211         }
1212
1213         ulp_ctx->cfg_data->fc_info = ulp_fc_info;
1214
1215         return 0;
1216 }
1217
1218 /* Function to retrieve the flow counter info from the context. */
1219 struct bnxt_ulp_fc_info *
1220 bnxt_ulp_cntxt_ptr2_fc_info_get(struct bnxt_ulp_context *ulp_ctx)
1221 {
1222         if (!ulp_ctx || !ulp_ctx->cfg_data)
1223                 return NULL;
1224
1225         return ulp_ctx->cfg_data->fc_info;
1226 }
1227
1228 /* Function to get the ulp flags from the ulp context. */
1229 int32_t
1230 bnxt_ulp_cntxt_ptr2_ulp_flags_get(struct bnxt_ulp_context *ulp_ctx,
1231                                   uint32_t *flags)
1232 {
1233         if (!ulp_ctx || !ulp_ctx->cfg_data)
1234                 return -1;
1235
1236         *flags =  ulp_ctx->cfg_data->ulp_flags;
1237         return 0;
1238 }
1239
1240 /* Function to get the ulp vfr info from the ulp context. */
1241 struct bnxt_ulp_vfr_rule_info*
1242 bnxt_ulp_cntxt_ptr2_ulp_vfr_info_get(struct bnxt_ulp_context *ulp_ctx,
1243                                      uint32_t port_id)
1244 {
1245         if (!ulp_ctx || !ulp_ctx->cfg_data || port_id >= RTE_MAX_ETHPORTS)
1246                 return NULL;
1247
1248         return &ulp_ctx->cfg_data->vfr_rule_info[port_id];
1249 }
1250
1251 /* Function to acquire the flow database lock from the ulp context. */
1252 int32_t
1253 bnxt_ulp_cntxt_acquire_fdb_lock(struct bnxt_ulp_context *ulp_ctx)
1254 {
1255         if (!ulp_ctx || !ulp_ctx->cfg_data)
1256                 return -1;
1257
1258         if (pthread_mutex_lock(&ulp_ctx->cfg_data->flow_db_lock)) {
1259                 BNXT_TF_DBG(ERR, "unable to acquire fdb lock\n");
1260                 return -1;
1261         }
1262         return 0;
1263 }
1264
1265 /* Function to release the flow database lock from the ulp context. */
1266 void
1267 bnxt_ulp_cntxt_release_fdb_lock(struct bnxt_ulp_context *ulp_ctx)
1268 {
1269         if (!ulp_ctx || !ulp_ctx->cfg_data)
1270                 return;
1271
1272         pthread_mutex_unlock(&ulp_ctx->cfg_data->flow_db_lock);
1273 }