1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2019-2021 Broadcom
7 #include <rte_malloc.h>
9 #include <rte_flow_driver.h>
10 #include <rte_tailq.h>
11 #include <rte_spinlock.h>
15 #include "bnxt_tf_common.h"
17 #include "tf_ext_flow_handle.h"
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"
27 #include "ulp_ha_mgr.h"
28 #include "bnxt_tf_pmd_shim.h"
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);
34 /* Mutex to synchronize bnxt_ulp_session_list operations. */
35 static pthread_mutex_t bnxt_ulp_global_mutex = PTHREAD_MUTEX_INITIALIZER;
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);
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);
49 * Allow the deletion of context only for the bnxt device that
50 * created the session.
53 ulp_ctx_deinit_allowed(struct bnxt_ulp_context *ulp_ctx)
55 if (!ulp_ctx || !ulp_ctx->cfg_data)
58 if (!ulp_ctx->cfg_data->ref_cnt) {
59 BNXT_TF_DBG(DEBUG, "ulp ctx shall initiate deinit\n");
67 bnxt_ulp_devid_get(struct bnxt *bp,
68 enum bnxt_ulp_device_id *ulp_dev_id)
70 if (BNXT_CHIP_P5(bp)) {
71 *ulp_dev_id = BNXT_ULP_DEVICE_ID_THOR;
75 if (BNXT_STINGRAY(bp))
76 *ulp_dev_id = BNXT_ULP_DEVICE_ID_STINGRAY;
78 /* Assuming Whitney */
79 *ulp_dev_id = BNXT_ULP_DEVICE_ID_WH_PLUS;
84 struct bnxt_ulp_app_capabilities_info *
85 bnxt_ulp_app_cap_list_get(uint32_t *num_entries)
89 *num_entries = BNXT_ULP_APP_CAP_TBL_MAX_SZ;
90 return ulp_app_cap_info_list;
93 static struct bnxt_ulp_resource_resv_info *
94 bnxt_ulp_app_resource_resv_list_get(uint32_t *num_entries)
96 if (num_entries == NULL)
98 *num_entries = BNXT_ULP_APP_RESOURCE_RESV_LIST_MAX_SZ;
99 return ulp_app_resource_resv_list;
102 struct bnxt_ulp_resource_resv_info *
103 bnxt_ulp_resource_resv_list_get(uint32_t *num_entries)
107 *num_entries = BNXT_ULP_RESOURCE_RESV_LIST_MAX_SZ;
108 return ulp_resource_resv_list;
111 struct bnxt_ulp_glb_resource_info *
112 bnxt_ulp_app_glb_resource_info_list_get(uint32_t *num_entries)
116 *num_entries = BNXT_ULP_APP_GLB_RESOURCE_TBL_MAX_SZ;
117 return ulp_app_glb_resource_tbl;
121 bnxt_ulp_named_resources_calc(struct bnxt_ulp_context *ulp_ctx,
122 struct bnxt_ulp_glb_resource_info *info,
124 struct tf_session_resources *res)
126 uint32_t dev_id = BNXT_ULP_DEVICE_ID_LAST, res_type, i;
131 if (ulp_ctx == NULL || info == NULL || res == NULL || num == 0) {
132 BNXT_TF_DBG(ERR, "Invalid parms to named resources calc.\n");
136 rc = bnxt_ulp_cntxt_app_id_get(ulp_ctx, &app_id);
138 BNXT_TF_DBG(ERR, "Unable to get the app id from ulp.\n");
142 rc = bnxt_ulp_cntxt_dev_id_get(ulp_ctx, &dev_id);
144 BNXT_TF_DBG(ERR, "Unable to get the dev id from ulp.\n");
148 for (i = 0; i < num; i++) {
149 if (dev_id != info[i].device_id || app_id != info[i].app_id)
151 dir = info[i].direction;
152 res_type = info[i].resource_type;
154 switch (info[i].resource_func) {
155 case BNXT_ULP_RESOURCE_FUNC_IDENTIFIER:
156 res->ident_cnt[dir].cnt[res_type]++;
158 case BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE:
159 res->tbl_cnt[dir].cnt[res_type]++;
161 case BNXT_ULP_RESOURCE_FUNC_TCAM_TABLE:
162 res->tcam_cnt[dir].cnt[res_type]++;
164 case BNXT_ULP_RESOURCE_FUNC_EM_TABLE:
165 res->em_cnt[dir].cnt[res_type]++;
168 BNXT_TF_DBG(ERR, "Unknown resource func (0x%x)\n,",
169 info[i].resource_func);
178 bnxt_ulp_unnamed_resources_calc(struct bnxt_ulp_context *ulp_ctx,
179 struct bnxt_ulp_resource_resv_info *info,
181 struct tf_session_resources *res)
183 uint32_t dev_id, res_type, i;
188 if (ulp_ctx == NULL || res == NULL || info == NULL || num == 0) {
189 BNXT_TF_DBG(ERR, "Invalid arguments to get resources.\n");
193 rc = bnxt_ulp_cntxt_app_id_get(ulp_ctx, &app_id);
195 BNXT_TF_DBG(ERR, "Unable to get the app id from ulp.\n");
199 rc = bnxt_ulp_cntxt_dev_id_get(ulp_ctx, &dev_id);
201 BNXT_TF_DBG(ERR, "Unable to get the dev id from ulp.\n");
205 for (i = 0; i < num; i++) {
206 if (app_id != info[i].app_id || dev_id != info[i].device_id)
208 dir = info[i].direction;
209 res_type = info[i].resource_type;
211 switch (info[i].resource_func) {
212 case BNXT_ULP_RESOURCE_FUNC_IDENTIFIER:
213 res->ident_cnt[dir].cnt[res_type] = info[i].count;
215 case BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE:
216 res->tbl_cnt[dir].cnt[res_type] = info[i].count;
218 case BNXT_ULP_RESOURCE_FUNC_TCAM_TABLE:
219 res->tcam_cnt[dir].cnt[res_type] = info[i].count;
221 case BNXT_ULP_RESOURCE_FUNC_EM_TABLE:
222 res->em_cnt[dir].cnt[res_type] = info[i].count;
232 bnxt_ulp_tf_resources_get(struct bnxt_ulp_context *ulp_ctx,
233 struct tf_session_resources *res)
235 struct bnxt_ulp_resource_resv_info *unnamed = NULL;
239 if (ulp_ctx == NULL || res == NULL) {
240 BNXT_TF_DBG(ERR, "Invalid arguments to get resources.\n");
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");
250 rc = bnxt_ulp_unnamed_resources_calc(ulp_ctx, unnamed, unum, res);
252 BNXT_TF_DBG(ERR, "Unable to calc resources for session.\n");
258 bnxt_ulp_tf_shared_session_resources_get(struct bnxt_ulp_context *ulp_ctx,
259 struct tf_session_resources *res)
261 struct bnxt_ulp_resource_resv_info *unnamed;
262 struct bnxt_ulp_glb_resource_info *named;
266 if (ulp_ctx == NULL || res == NULL) {
267 BNXT_TF_DBG(ERR, "Invalid arguments to get resources.\n");
271 /* Make sure the resources are zero before accumulating. */
272 memset(res, 0, sizeof(struct tf_session_resources));
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.
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");
284 rc = bnxt_ulp_unnamed_resources_calc(ulp_ctx, unnamed, unum, res);
286 BNXT_TF_DBG(ERR, "Unable to calc resources for shared session.\n");
290 /* Get the named list and add the totals */
291 named = bnxt_ulp_app_glb_resource_info_list_get(&nnum);
293 BNXT_TF_DBG(ERR, "Unable to get app global resource list\n");
296 rc = bnxt_ulp_named_resources_calc(ulp_ctx, named, nnum, res);
298 BNXT_TF_DBG(ERR, "Unable to calc named resources\n");
304 bnxt_ulp_cntxt_app_caps_init(struct bnxt *bp,
305 uint8_t app_id, uint32_t dev_id)
307 struct bnxt_ulp_app_capabilities_info *info;
311 struct bnxt_ulp_context *ulp_ctx = bp->ulp_ctx;
313 if (ULP_APP_DEV_UNSUPPORTED_ENABLED(ulp_ctx->cfg_data->ulp_flags)) {
314 BNXT_TF_DBG(ERR, "APP ID %d, Device ID: 0x%x not supported.\n",
319 info = bnxt_ulp_app_cap_list_get(&num);
321 BNXT_TF_DBG(ERR, "Failed to get app capabilities.\n");
325 for (i = 0; i < num; i++) {
326 if (info[i].app_id != app_id || info[i].device_id != dev_id)
329 if (info[i].flags & BNXT_ULP_APP_CAP_SHARED_EN)
330 ulp_ctx->cfg_data->ulp_flags |=
331 BNXT_ULP_SHARED_SESSION_ENABLED;
332 if (info[i].flags & BNXT_ULP_APP_CAP_HOT_UPGRADE_EN)
333 ulp_ctx->cfg_data->ulp_flags |=
334 BNXT_ULP_HIGH_AVAIL_ENABLED;
335 if (info[i].flags & BNXT_ULP_APP_CAP_UNICAST_ONLY)
336 ulp_ctx->cfg_data->ulp_flags |=
337 BNXT_ULP_APP_UNICAST_ONLY;
338 if (info[i].flags & BNXT_ULP_APP_CAP_SOCKET_DIRECT) {
339 /* Enable socket direction only if MR is enabled in fw*/
340 if (BNXT_MULTIROOT_EN(bp))
341 ulp_ctx->cfg_data->ulp_flags |=
342 BNXT_ULP_APP_SOCKET_DIRECT;
346 BNXT_TF_DBG(ERR, "APP ID %d, Device ID: 0x%x not supported.\n",
348 ulp_ctx->cfg_data->ulp_flags |= BNXT_ULP_APP_DEV_UNSUPPORTED;
356 ulp_ctx_shared_session_close(struct bnxt *bp,
357 struct bnxt_ulp_session_state *session)
362 if (!bnxt_ulp_cntxt_shared_session_enabled(bp->ulp_ctx))
365 tfp = bnxt_ulp_cntxt_shared_tfp_get(bp->ulp_ctx);
368 * Log it under debug since this is likely a case of the
369 * shared session not being created. For example, a failed
372 BNXT_TF_DBG(DEBUG, "Failed to get shared tfp on close.\n");
375 rc = tf_close_session(tfp);
377 BNXT_TF_DBG(ERR, "Failed to close the shared session rc=%d.\n",
379 (void)bnxt_ulp_cntxt_shared_tfp_set(bp->ulp_ctx, NULL);
381 session->g_shared_tfp.session = NULL;
385 ulp_ctx_shared_session_open(struct bnxt *bp,
386 struct bnxt_ulp_session_state *session)
388 struct rte_eth_dev *ethdev = bp->eth_dev;
389 struct tf_session_resources *resources;
390 struct tf_open_session_parms parms;
392 uint32_t ulp_dev_id = BNXT_ULP_DEVICE_ID_LAST;
396 /* only perform this if shared session is enabled. */
397 if (!bnxt_ulp_cntxt_shared_session_enabled(bp->ulp_ctx))
400 memset(&parms, 0, sizeof(parms));
402 rc = rte_eth_dev_get_name_by_port(ethdev->data->port_id,
403 parms.ctrl_chan_name);
405 BNXT_TF_DBG(ERR, "Invalid port %d, rc = %d\n",
406 ethdev->data->port_id, rc);
409 resources = &parms.resources;
412 * Need to account for size of ctrl_chan_name and 1 extra for Null
415 copy_nbytes = sizeof(parms.ctrl_chan_name) -
416 strlen(parms.ctrl_chan_name) - 1;
419 * Build the ctrl_chan_name with shared token.
420 * When HA is enabled, the WC TCAM needs extra management by the core,
421 * so add the wc_tcam string to the control channel.
423 if (bnxt_ulp_cntxt_ha_enabled(bp->ulp_ctx))
424 strncat(parms.ctrl_chan_name, "-tf_shared-wc_tcam",
427 strncat(parms.ctrl_chan_name, "-tf_shared", copy_nbytes);
429 rc = bnxt_ulp_tf_shared_session_resources_get(bp->ulp_ctx, resources);
433 rc = bnxt_ulp_cntxt_app_id_get(bp->ulp_ctx, &app_id);
435 BNXT_TF_DBG(ERR, "Unable to get the app id from ulp.\n");
439 rc = bnxt_ulp_cntxt_dev_id_get(bp->ulp_ctx, &ulp_dev_id);
441 BNXT_TF_DBG(ERR, "Unable to get device id from ulp.\n");
445 switch (ulp_dev_id) {
446 case BNXT_ULP_DEVICE_ID_WH_PLUS:
447 parms.device_type = TF_DEVICE_TYPE_WH;
449 case BNXT_ULP_DEVICE_ID_STINGRAY:
450 parms.device_type = TF_DEVICE_TYPE_SR;
452 case BNXT_ULP_DEVICE_ID_THOR:
453 parms.device_type = TF_DEVICE_TYPE_THOR;
456 BNXT_TF_DBG(ERR, "Unable to determine dev for opening session.\n");
460 parms.shadow_copy = true;
462 if (app_id == 0 || app_id == 3)
463 parms.wc_num_slices = TF_WC_TCAM_2_SLICE_PER_ROW;
465 parms.wc_num_slices = TF_WC_TCAM_1_SLICE_PER_ROW;
468 * Open the session here, but the collect the resources during the
469 * mapper initialization.
471 rc = tf_open_session(&bp->tfp_shared, &parms);
475 if (parms.shared_session_creator)
476 BNXT_TF_DBG(DEBUG, "Shared session creator.\n");
478 BNXT_TF_DBG(DEBUG, "Shared session attached.\n");
480 /* Save the shared session in global data */
481 if (!session->g_shared_tfp.session)
482 session->g_shared_tfp.session = bp->tfp_shared.session;
484 rc = bnxt_ulp_cntxt_shared_tfp_set(bp->ulp_ctx, &bp->tfp_shared);
486 BNXT_TF_DBG(ERR, "Failed to add shared tfp to ulp (%d)\n", rc);
492 ulp_ctx_shared_session_attach(struct bnxt *bp,
493 struct bnxt_ulp_session_state *session)
497 /* Simply return success if shared session not enabled */
498 if (bnxt_ulp_cntxt_shared_session_enabled(bp->ulp_ctx)) {
499 bp->tfp_shared.session = session->g_shared_tfp.session;
500 rc = ulp_ctx_shared_session_open(bp, session);
507 ulp_ctx_shared_session_detach(struct bnxt *bp)
509 if (bnxt_ulp_cntxt_shared_session_enabled(bp->ulp_ctx)) {
510 if (bp->tfp_shared.session) {
511 tf_close_session(&bp->tfp_shared);
512 bp->tfp_shared.session = NULL;
518 * Initialize an ULP session.
519 * An ULP session will contain all the resources needed to support rte flow
520 * offloads. A session is initialized as part of rte_eth_device start.
521 * A single vswitch instance can have multiple uplinks which means
522 * rte_eth_device start will be called for each of these devices.
523 * ULP session manager will make sure that a single ULP session is only
524 * initialized once. Apart from this, it also initializes MARK database,
525 * EEM table & flow database. ULP session manager also manages a list of
526 * all opened ULP sessions.
529 ulp_ctx_session_open(struct bnxt *bp,
530 struct bnxt_ulp_session_state *session)
532 struct rte_eth_dev *ethdev = bp->eth_dev;
534 struct tf_open_session_parms params;
535 struct tf_session_resources *resources;
536 uint32_t ulp_dev_id = BNXT_ULP_DEVICE_ID_LAST;
539 memset(¶ms, 0, sizeof(params));
541 rc = rte_eth_dev_get_name_by_port(ethdev->data->port_id,
542 params.ctrl_chan_name);
544 BNXT_TF_DBG(ERR, "Invalid port %d, rc = %d\n",
545 ethdev->data->port_id, rc);
549 params.shadow_copy = true;
551 rc = bnxt_ulp_cntxt_app_id_get(bp->ulp_ctx, &app_id);
553 BNXT_TF_DBG(ERR, "Unable to get the app id from ulp.\n");
557 rc = bnxt_ulp_cntxt_dev_id_get(bp->ulp_ctx, &ulp_dev_id);
559 BNXT_TF_DBG(ERR, "Unable to get device id from ulp.\n");
563 switch (ulp_dev_id) {
564 case BNXT_ULP_DEVICE_ID_WH_PLUS:
565 params.device_type = TF_DEVICE_TYPE_WH;
567 case BNXT_ULP_DEVICE_ID_STINGRAY:
568 params.device_type = TF_DEVICE_TYPE_SR;
570 case BNXT_ULP_DEVICE_ID_THOR:
571 params.device_type = TF_DEVICE_TYPE_THOR;
574 BNXT_TF_DBG(ERR, "Unable to determine device for opening session.\n");
578 resources = ¶ms.resources;
579 rc = bnxt_ulp_tf_resources_get(bp->ulp_ctx, resources);
584 if (app_id == 0 || app_id == 3)
585 params.wc_num_slices = TF_WC_TCAM_2_SLICE_PER_ROW;
587 params.wc_num_slices = TF_WC_TCAM_1_SLICE_PER_ROW;
589 rc = tf_open_session(&bp->tfp, ¶ms);
591 BNXT_TF_DBG(ERR, "Failed to open TF session - %s, rc = %d\n",
592 params.ctrl_chan_name, rc);
595 if (!session->session_opened) {
596 session->session_opened = 1;
597 session->g_tfp = rte_zmalloc("bnxt_ulp_session_tfp",
598 sizeof(struct tf), 0);
599 session->g_tfp->session = bp->tfp.session;
605 * Close the ULP session.
606 * It takes the ulp context pointer.
609 ulp_ctx_session_close(struct bnxt *bp,
610 struct bnxt_ulp_session_state *session)
612 /* close the session in the hardware */
613 if (session->session_opened)
614 tf_close_session(&bp->tfp);
615 session->session_opened = 0;
616 rte_free(session->g_tfp);
617 session->g_tfp = NULL;
621 bnxt_init_tbl_scope_parms(struct bnxt *bp,
622 struct tf_alloc_tbl_scope_parms *params)
624 struct bnxt_ulp_device_params *dparms;
628 rc = bnxt_ulp_cntxt_dev_id_get(bp->ulp_ctx, &dev_id);
630 /* TBD: For now, just use default. */
633 dparms = bnxt_ulp_device_params_get(dev_id);
636 * Set the flush timer for EEM entries. The value is in 100ms intervals,
639 params->hw_flow_cache_flush_timer = 100;
642 params->rx_max_key_sz_in_bits = BNXT_ULP_DFLT_RX_MAX_KEY;
643 params->rx_max_action_entry_sz_in_bits =
644 BNXT_ULP_DFLT_RX_MAX_ACTN_ENTRY;
645 params->rx_mem_size_in_mb = BNXT_ULP_DFLT_RX_MEM;
646 params->rx_num_flows_in_k = BNXT_ULP_RX_NUM_FLOWS;
648 params->tx_max_key_sz_in_bits = BNXT_ULP_DFLT_TX_MAX_KEY;
649 params->tx_max_action_entry_sz_in_bits =
650 BNXT_ULP_DFLT_TX_MAX_ACTN_ENTRY;
651 params->tx_mem_size_in_mb = BNXT_ULP_DFLT_TX_MEM;
652 params->tx_num_flows_in_k = BNXT_ULP_TX_NUM_FLOWS;
654 params->rx_max_key_sz_in_bits = BNXT_ULP_DFLT_RX_MAX_KEY;
655 params->rx_max_action_entry_sz_in_bits =
656 BNXT_ULP_DFLT_RX_MAX_ACTN_ENTRY;
657 params->rx_mem_size_in_mb = BNXT_ULP_DFLT_RX_MEM;
658 params->rx_num_flows_in_k =
659 dparms->ext_flow_db_num_entries / 1024;
661 params->tx_max_key_sz_in_bits = BNXT_ULP_DFLT_TX_MAX_KEY;
662 params->tx_max_action_entry_sz_in_bits =
663 BNXT_ULP_DFLT_TX_MAX_ACTN_ENTRY;
664 params->tx_mem_size_in_mb = BNXT_ULP_DFLT_TX_MEM;
665 params->tx_num_flows_in_k =
666 dparms->ext_flow_db_num_entries / 1024;
668 BNXT_TF_DBG(INFO, "Table Scope initialized with %uK flows.\n",
669 params->rx_num_flows_in_k);
672 /* Initialize Extended Exact Match host memory. */
674 ulp_eem_tbl_scope_init(struct bnxt *bp)
676 struct tf_alloc_tbl_scope_parms params = {0};
677 struct bnxt_ulp_device_params *dparms;
678 enum bnxt_ulp_flow_mem_type mtype;
682 /* Get the dev specific number of flows that needed to be supported. */
683 if (bnxt_ulp_cntxt_dev_id_get(bp->ulp_ctx, &dev_id)) {
684 BNXT_TF_DBG(ERR, "Invalid device id\n");
688 dparms = bnxt_ulp_device_params_get(dev_id);
690 BNXT_TF_DBG(ERR, "could not fetch the device params\n");
694 if (bnxt_ulp_cntxt_mem_type_get(bp->ulp_ctx, &mtype))
696 if (mtype != BNXT_ULP_FLOW_MEM_TYPE_EXT) {
697 BNXT_TF_DBG(INFO, "Table Scope alloc is not required\n");
701 bnxt_init_tbl_scope_parms(bp, ¶ms);
702 rc = tf_alloc_tbl_scope(&bp->tfp, ¶ms);
704 BNXT_TF_DBG(ERR, "Unable to allocate eem table scope rc = %d\n",
708 rc = bnxt_ulp_cntxt_tbl_scope_id_set(bp->ulp_ctx, params.tbl_scope_id);
710 BNXT_TF_DBG(ERR, "Unable to set table scope id\n");
717 /* Free Extended Exact Match host memory */
719 ulp_eem_tbl_scope_deinit(struct bnxt *bp, struct bnxt_ulp_context *ulp_ctx)
721 struct tf_free_tbl_scope_parms params = {0};
724 struct bnxt_ulp_device_params *dparms;
725 enum bnxt_ulp_flow_mem_type mtype;
728 if (!ulp_ctx || !ulp_ctx->cfg_data)
731 tfp = bnxt_ulp_cntxt_tfp_get(ulp_ctx, BNXT_ULP_SHARED_SESSION_NO);
733 BNXT_TF_DBG(ERR, "Failed to get the truflow pointer\n");
737 /* Get the dev specific number of flows that needed to be supported. */
738 if (bnxt_ulp_cntxt_dev_id_get(bp->ulp_ctx, &dev_id)) {
739 BNXT_TF_DBG(ERR, "Invalid device id\n");
743 dparms = bnxt_ulp_device_params_get(dev_id);
745 BNXT_TF_DBG(ERR, "could not fetch the device params\n");
749 if (bnxt_ulp_cntxt_mem_type_get(ulp_ctx, &mtype))
751 if (mtype != BNXT_ULP_FLOW_MEM_TYPE_EXT) {
752 BNXT_TF_DBG(INFO, "Table Scope free is not required\n");
756 rc = bnxt_ulp_cntxt_tbl_scope_id_get(ulp_ctx, ¶ms.tbl_scope_id);
758 BNXT_TF_DBG(ERR, "Failed to get the table scope id\n");
762 rc = tf_free_tbl_scope(tfp, ¶ms);
764 BNXT_TF_DBG(ERR, "Unable to free table scope\n");
770 /* The function to free and deinit the ulp context data. */
772 ulp_ctx_deinit(struct bnxt *bp,
773 struct bnxt_ulp_session_state *session)
775 /* close the tf session */
776 ulp_ctx_session_close(bp, session);
778 /* The shared session must be closed last. */
779 ulp_ctx_shared_session_close(bp, session);
781 /* Free the contents */
782 if (session->cfg_data) {
783 rte_free(session->cfg_data);
784 bp->ulp_ctx->cfg_data = NULL;
785 session->cfg_data = NULL;
790 /* The function to allocate and initialize the ulp context data. */
792 ulp_ctx_init(struct bnxt *bp,
793 struct bnxt_ulp_session_state *session)
795 struct bnxt_ulp_data *ulp_data;
797 enum bnxt_ulp_device_id devid;
799 /* Initialize the context entries list */
800 bnxt_ulp_cntxt_list_init();
802 /* Add the context to the context entries list */
803 rc = bnxt_ulp_cntxt_list_add(bp->ulp_ctx);
805 BNXT_TF_DBG(ERR, "Failed to add the context list entry\n");
809 /* Allocate memory to hold ulp context data. */
810 ulp_data = rte_zmalloc("bnxt_ulp_data",
811 sizeof(struct bnxt_ulp_data), 0);
813 BNXT_TF_DBG(ERR, "Failed to allocate memory for ulp data\n");
817 /* Increment the ulp context data reference count usage. */
818 bp->ulp_ctx->cfg_data = ulp_data;
819 session->cfg_data = ulp_data;
821 ulp_data->ulp_flags |= BNXT_ULP_VF_REP_ENABLED;
823 rc = bnxt_ulp_devid_get(bp, &devid);
825 BNXT_TF_DBG(ERR, "Unable to determine device for ULP init.\n");
829 rc = bnxt_ulp_cntxt_dev_id_set(bp->ulp_ctx, devid);
831 BNXT_TF_DBG(ERR, "Unable to set device for ULP init.\n");
835 rc = bnxt_ulp_cntxt_app_id_set(bp->ulp_ctx, bp->app_id);
837 BNXT_TF_DBG(ERR, "Unable to set app_id for ULP init.\n");
840 BNXT_TF_DBG(DEBUG, "Ulp initialized with app id %d\n", bp->app_id);
842 rc = bnxt_ulp_cntxt_app_caps_init(bp, bp->app_id, devid);
844 BNXT_TF_DBG(ERR, "Unable to set caps for app(%x)/dev(%x)\n",
850 * Shared session must be created before first regular session but after
851 * the ulp_ctx is valid.
853 rc = ulp_ctx_shared_session_open(bp, session);
855 BNXT_TF_DBG(ERR, "Unable to open shared session (%d)\n", rc);
859 /* Open the ulp session. */
860 rc = ulp_ctx_session_open(bp, session);
864 bnxt_ulp_cntxt_tfp_set(bp->ulp_ctx, &bp->tfp);
868 session->session_opened = 1;
869 (void)ulp_ctx_deinit(bp, session);
873 /* The function to initialize ulp dparms with devargs */
875 ulp_dparms_init(struct bnxt *bp, struct bnxt_ulp_context *ulp_ctx)
877 struct bnxt_ulp_device_params *dparms;
878 uint32_t dev_id = BNXT_ULP_DEVICE_ID_LAST;
880 if (!bp->max_num_kflows) {
881 /* Defaults to Internal */
882 bnxt_ulp_cntxt_mem_type_set(ulp_ctx,
883 BNXT_ULP_FLOW_MEM_TYPE_INT);
887 /* The max_num_kflows were set, so move to external */
888 if (bnxt_ulp_cntxt_mem_type_set(ulp_ctx, BNXT_ULP_FLOW_MEM_TYPE_EXT))
891 if (bnxt_ulp_cntxt_dev_id_get(ulp_ctx, &dev_id)) {
892 BNXT_TF_DBG(DEBUG, "Failed to get device id\n");
896 dparms = bnxt_ulp_device_params_get(dev_id);
898 BNXT_TF_DBG(DEBUG, "Failed to get device parms\n");
902 /* num_flows = max_num_kflows * 1024 */
903 dparms->ext_flow_db_num_entries = bp->max_num_kflows * 1024;
904 /* GFID = 2 * num_flows */
905 dparms->mark_db_gfid_entries = dparms->ext_flow_db_num_entries * 2;
906 BNXT_TF_DBG(DEBUG, "Set the number of flows = %" PRIu64 "\n",
907 dparms->ext_flow_db_num_entries);
912 /* The function to initialize bp flags with truflow features */
914 ulp_dparms_dev_port_intf_update(struct bnxt *bp,
915 struct bnxt_ulp_context *ulp_ctx)
917 enum bnxt_ulp_flow_mem_type mtype;
919 if (bnxt_ulp_cntxt_mem_type_get(ulp_ctx, &mtype))
921 /* Update the bp flag with gfid flag */
922 if (mtype == BNXT_ULP_FLOW_MEM_TYPE_EXT)
923 bp->flags |= BNXT_FLAG_GFID_ENABLE;
929 ulp_ctx_attach(struct bnxt *bp,
930 struct bnxt_ulp_session_state *session)
933 uint32_t flags, dev_id = BNXT_ULP_DEVICE_ID_LAST;
936 /* Increment the ulp context data reference count usage. */
937 bp->ulp_ctx->cfg_data = session->cfg_data;
938 bp->ulp_ctx->cfg_data->ref_cnt++;
940 /* update the session details in bnxt tfp */
941 bp->tfp.session = session->g_tfp->session;
943 /* Add the context to the context entries list */
944 rc = bnxt_ulp_cntxt_list_add(bp->ulp_ctx);
946 BNXT_TF_DBG(ERR, "Failed to add the context list entry\n");
951 * The supported flag will be set during the init. Use it now to
952 * know if we should go through the attach.
954 rc = bnxt_ulp_cntxt_app_id_get(bp->ulp_ctx, &app_id);
956 BNXT_TF_DBG(ERR, "Unable to get the app id from ulp.\n");
960 rc = bnxt_ulp_cntxt_dev_id_get(bp->ulp_ctx, &dev_id);
962 BNXT_TF_DBG(ERR, "Unable do get the dev_id.\n");
966 flags = bp->ulp_ctx->cfg_data->ulp_flags;
967 if (ULP_APP_DEV_UNSUPPORTED_ENABLED(flags)) {
968 BNXT_TF_DBG(ERR, "APP ID %d, Device ID: 0x%x not supported.\n",
973 /* Create a TF Client */
974 rc = ulp_ctx_session_open(bp, session);
976 PMD_DRV_LOG(ERR, "Failed to open ctxt session, rc:%d\n", rc);
977 bp->tfp.session = NULL;
981 bnxt_ulp_cntxt_tfp_set(bp->ulp_ctx, &bp->tfp);
986 ulp_ctx_detach(struct bnxt *bp)
988 if (bp->tfp.session) {
989 tf_close_session(&bp->tfp);
990 bp->tfp.session = NULL;
995 * Initialize the state of an ULP session.
996 * If the state of an ULP session is not initialized, set it's state to
997 * initialized. If the state is already initialized, do nothing.
1000 ulp_context_initialized(struct bnxt_ulp_session_state *session, bool *init)
1002 pthread_mutex_lock(&session->bnxt_ulp_mutex);
1004 if (!session->bnxt_ulp_init) {
1005 session->bnxt_ulp_init = true;
1011 pthread_mutex_unlock(&session->bnxt_ulp_mutex);
1015 * Check if an ULP session is already allocated for a specific PCI
1016 * domain & bus. If it is already allocated simply return the session
1017 * pointer, otherwise allocate a new session.
1019 static struct bnxt_ulp_session_state *
1020 ulp_get_session(struct bnxt *bp, struct rte_pci_addr *pci_addr)
1022 struct bnxt_ulp_session_state *session;
1024 /* if multi root capability is enabled, then ignore the pci bus id */
1025 STAILQ_FOREACH(session, &bnxt_ulp_session_list, next) {
1026 if (session->pci_info.domain == pci_addr->domain &&
1027 (BNXT_MULTIROOT_EN(bp) ||
1028 session->pci_info.bus == pci_addr->bus)) {
1036 * Allocate and Initialize an ULP session and set it's state to INITIALIZED.
1037 * If it's already initialized simply return the already existing session.
1039 static struct bnxt_ulp_session_state *
1040 ulp_session_init(struct bnxt *bp,
1043 struct rte_pci_device *pci_dev;
1044 struct rte_pci_addr *pci_addr;
1045 struct bnxt_ulp_session_state *session;
1051 pci_dev = RTE_DEV_TO_PCI(bp->eth_dev->device);
1052 pci_addr = &pci_dev->addr;
1054 pthread_mutex_lock(&bnxt_ulp_global_mutex);
1056 session = ulp_get_session(bp, pci_addr);
1058 /* Not Found the session Allocate a new one */
1059 session = rte_zmalloc("bnxt_ulp_session",
1060 sizeof(struct bnxt_ulp_session_state),
1064 "Allocation failed for bnxt_ulp_session\n");
1065 pthread_mutex_unlock(&bnxt_ulp_global_mutex);
1069 /* Add it to the queue */
1070 session->pci_info.domain = pci_addr->domain;
1071 session->pci_info.bus = pci_addr->bus;
1072 rc = pthread_mutex_init(&session->bnxt_ulp_mutex, NULL);
1074 BNXT_TF_DBG(ERR, "mutex create failed\n");
1075 pthread_mutex_unlock(&bnxt_ulp_global_mutex);
1078 STAILQ_INSERT_TAIL(&bnxt_ulp_session_list,
1082 ulp_context_initialized(session, init);
1083 pthread_mutex_unlock(&bnxt_ulp_global_mutex);
1088 * When a device is closed, remove it's associated session from the global
1092 ulp_session_deinit(struct bnxt_ulp_session_state *session)
1097 if (!session->cfg_data) {
1098 pthread_mutex_lock(&bnxt_ulp_global_mutex);
1099 STAILQ_REMOVE(&bnxt_ulp_session_list, session,
1100 bnxt_ulp_session_state, next);
1101 pthread_mutex_destroy(&session->bnxt_ulp_mutex);
1103 pthread_mutex_unlock(&bnxt_ulp_global_mutex);
1108 * Internal api to enable NAT feature.
1109 * Set set_flag to 1 to set the value or zero to reset the value.
1110 * returns 0 on success.
1113 bnxt_ulp_global_cfg_update(struct bnxt *bp,
1115 enum tf_global_config_type type,
1120 uint32_t global_cfg = 0;
1122 struct tf_global_cfg_parms parms = { 0 };
1124 /* Initialize the params */
1127 parms.offset = offset,
1128 parms.config = (uint8_t *)&global_cfg,
1129 parms.config_sz_in_bytes = sizeof(global_cfg);
1131 rc = tf_get_global_cfg(&bp->tfp, &parms);
1133 BNXT_TF_DBG(ERR, "Failed to get global cfg 0x%x rc:%d\n",
1139 global_cfg |= value;
1141 global_cfg &= ~value;
1143 /* SET the register RE_CFA_REG_ACT_TECT */
1144 rc = tf_set_global_cfg(&bp->tfp, &parms);
1146 BNXT_TF_DBG(ERR, "Failed to set global cfg 0x%x rc:%d\n",
1153 /* Internal function to delete all the flows belonging to the given port */
1155 bnxt_ulp_flush_port_flows(struct bnxt *bp)
1159 /* it is assumed that port is either TVF or PF */
1160 if (ulp_port_db_port_func_id_get(bp->ulp_ctx,
1161 bp->eth_dev->data->port_id,
1163 BNXT_TF_DBG(ERR, "Invalid argument\n");
1166 (void)ulp_flow_db_function_flow_flush(bp->ulp_ctx, func_id);
1169 /* Internal function to delete the VFR default flows */
1171 bnxt_ulp_destroy_vfr_default_rules(struct bnxt *bp, bool global)
1173 struct bnxt_ulp_vfr_rule_info *info;
1175 struct rte_eth_dev *vfr_eth_dev;
1176 struct bnxt_representor *vfr_bp;
1178 if (!BNXT_TRUFLOW_EN(bp) || BNXT_ETH_DEV_IS_REPRESENTOR(bp->eth_dev))
1181 if (!bp->ulp_ctx || !bp->ulp_ctx->cfg_data)
1184 /* Delete default rules for all ports */
1185 for (port_id = 0; port_id < RTE_MAX_ETHPORTS; port_id++) {
1186 info = &bp->ulp_ctx->cfg_data->vfr_rule_info[port_id];
1190 if (!global && info->parent_port_id !=
1191 bp->eth_dev->data->port_id)
1194 /* Destroy the flows */
1195 ulp_default_flow_destroy(bp->eth_dev, info->vfr_flow_id);
1196 /* Clean up the tx action pointer */
1197 vfr_eth_dev = &rte_eth_devices[port_id];
1199 vfr_bp = vfr_eth_dev->data->dev_private;
1200 vfr_bp->vfr_tx_cfa_action = 0;
1202 memset(info, 0, sizeof(struct bnxt_ulp_vfr_rule_info));
1207 * When a port is deinit'ed by dpdk. This function is called
1208 * and this function clears the ULP context and rest of the
1209 * infrastructure associated with it.
1212 bnxt_ulp_deinit(struct bnxt *bp,
1213 struct bnxt_ulp_session_state *session)
1217 if (!bp->ulp_ctx || !bp->ulp_ctx->cfg_data)
1220 ha_enabled = bnxt_ulp_cntxt_ha_enabled(bp->ulp_ctx);
1221 if (ha_enabled && session->session_opened) {
1222 int32_t rc = ulp_ha_mgr_close(bp->ulp_ctx);
1224 BNXT_TF_DBG(ERR, "Failed to close HA (%d)\n", rc);
1227 /* clean up default flows */
1228 bnxt_ulp_destroy_df_rules(bp, true);
1230 /* clean up default VFR flows */
1231 bnxt_ulp_destroy_vfr_default_rules(bp, true);
1233 /* clean up regular flows */
1234 ulp_flow_db_flush_flows(bp->ulp_ctx, BNXT_ULP_FDB_TYPE_REGULAR);
1236 /* cleanup the eem table scope */
1237 ulp_eem_tbl_scope_deinit(bp, bp->ulp_ctx);
1239 /* cleanup the flow database */
1240 ulp_flow_db_deinit(bp->ulp_ctx);
1242 /* Delete the Mark database */
1243 ulp_mark_db_deinit(bp->ulp_ctx);
1245 /* cleanup the ulp mapper */
1246 ulp_mapper_deinit(bp->ulp_ctx);
1248 /* Delete the Flow Counter Manager */
1249 ulp_fc_mgr_deinit(bp->ulp_ctx);
1251 /* Delete the Port database */
1252 ulp_port_db_deinit(bp->ulp_ctx);
1254 /* Disable NAT feature */
1255 (void)bnxt_ulp_global_cfg_update(bp, TF_DIR_RX, TF_TUNNEL_ENCAP,
1256 TF_TUNNEL_ENCAP_NAT,
1257 BNXT_ULP_NAT_OUTER_MOST_FLAGS, 0);
1259 (void)bnxt_ulp_global_cfg_update(bp, TF_DIR_TX, TF_TUNNEL_ENCAP,
1260 TF_TUNNEL_ENCAP_NAT,
1261 BNXT_ULP_NAT_OUTER_MOST_FLAGS, 0);
1263 /* free the flow db lock */
1264 pthread_mutex_destroy(&bp->ulp_ctx->cfg_data->flow_db_lock);
1267 ulp_ha_mgr_deinit(bp->ulp_ctx);
1269 /* Delete the ulp context and tf session and free the ulp context */
1270 ulp_ctx_deinit(bp, session);
1271 BNXT_TF_DBG(DEBUG, "ulp ctx has been deinitialized\n");
1275 * When a port is initialized by dpdk. This functions is called
1276 * and this function initializes the ULP context and rest of the
1277 * infrastructure associated with it.
1280 bnxt_ulp_init(struct bnxt *bp,
1281 struct bnxt_ulp_session_state *session)
1285 /* Allocate and Initialize the ulp context. */
1286 rc = ulp_ctx_init(bp, session);
1288 BNXT_TF_DBG(ERR, "Failed to create the ulp context\n");
1292 rc = pthread_mutex_init(&bp->ulp_ctx->cfg_data->flow_db_lock, NULL);
1294 BNXT_TF_DBG(ERR, "Unable to initialize flow db lock\n");
1298 /* Initialize ulp dparms with values devargs passed */
1299 rc = ulp_dparms_init(bp, bp->ulp_ctx);
1301 BNXT_TF_DBG(ERR, "Failed to initialize the dparms\n");
1305 /* create the port database */
1306 rc = ulp_port_db_init(bp->ulp_ctx, bp->port_cnt);
1308 BNXT_TF_DBG(ERR, "Failed to create the port database\n");
1312 /* Create the Mark database. */
1313 rc = ulp_mark_db_init(bp->ulp_ctx);
1315 BNXT_TF_DBG(ERR, "Failed to create the mark database\n");
1319 /* Create the flow database. */
1320 rc = ulp_flow_db_init(bp->ulp_ctx);
1322 BNXT_TF_DBG(ERR, "Failed to create the flow database\n");
1326 /* Create the eem table scope. */
1327 rc = ulp_eem_tbl_scope_init(bp);
1329 BNXT_TF_DBG(ERR, "Failed to create the eem scope table\n");
1333 rc = ulp_mapper_init(bp->ulp_ctx);
1335 BNXT_TF_DBG(ERR, "Failed to initialize ulp mapper\n");
1339 rc = ulp_fc_mgr_init(bp->ulp_ctx);
1341 BNXT_TF_DBG(ERR, "Failed to initialize ulp flow counter mgr\n");
1346 * Enable NAT feature. Set the global configuration register
1347 * Tunnel encap to enable NAT with the reuse of existing inner
1348 * L2 header smac and dmac
1350 rc = bnxt_ulp_global_cfg_update(bp, TF_DIR_RX, TF_TUNNEL_ENCAP,
1351 TF_TUNNEL_ENCAP_NAT,
1352 BNXT_ULP_NAT_OUTER_MOST_FLAGS, 1);
1354 BNXT_TF_DBG(ERR, "Failed to set rx global configuration\n");
1358 rc = bnxt_ulp_global_cfg_update(bp, TF_DIR_TX, TF_TUNNEL_ENCAP,
1359 TF_TUNNEL_ENCAP_NAT,
1360 BNXT_ULP_NAT_OUTER_MOST_FLAGS, 1);
1362 BNXT_TF_DBG(ERR, "Failed to set tx global configuration\n");
1366 if (bnxt_ulp_cntxt_ha_enabled(bp->ulp_ctx)) {
1367 rc = ulp_ha_mgr_init(bp->ulp_ctx);
1369 BNXT_TF_DBG(ERR, "Failed to initialize HA %d\n", rc);
1372 rc = ulp_ha_mgr_open(bp->ulp_ctx);
1374 BNXT_TF_DBG(ERR, "Failed to Process HA Open %d\n", rc);
1378 BNXT_TF_DBG(DEBUG, "ulp ctx has been initialized\n");
1382 bnxt_ulp_deinit(bp, session);
1387 * When a port is initialized by dpdk. This functions sets up
1388 * the port specific details.
1391 bnxt_ulp_port_init(struct bnxt *bp)
1393 struct bnxt_ulp_session_state *session;
1395 enum bnxt_ulp_device_id devid = BNXT_ULP_DEVICE_ID_LAST;
1399 if (!BNXT_TRUFLOW_EN(bp)) {
1401 "Skip ulp init for port: %d, TF is not enabled\n",
1402 bp->eth_dev->data->port_id);
1406 if (!BNXT_PF(bp) && !BNXT_VF_IS_TRUSTED(bp)) {
1408 "Skip ulp init for port: %d, not a TVF or PF\n",
1409 bp->eth_dev->data->port_id);
1414 BNXT_TF_DBG(DEBUG, "ulp ctx already allocated\n");
1418 bp->ulp_ctx = rte_zmalloc("bnxt_ulp_ctx",
1419 sizeof(struct bnxt_ulp_context), 0);
1421 BNXT_TF_DBG(ERR, "Failed to allocate ulp ctx\n");
1426 * Multiple uplink ports can be associated with a single vswitch.
1427 * Make sure only the port that is started first will initialize
1430 session = ulp_session_init(bp, &initialized);
1432 BNXT_TF_DBG(ERR, "Failed to initialize the tf session\n");
1439 * If ULP is already initialized for a specific domain then
1440 * simply assign the ulp context to this rte_eth_dev.
1442 rc = ulp_ctx_attach(bp, session);
1444 BNXT_TF_DBG(ERR, "Failed to attach the ulp context\n");
1449 * Attach to the shared session, must be called after the
1450 * ulp_ctx_attach in order to ensure that ulp data is available
1453 rc = ulp_ctx_shared_session_attach(bp, session);
1456 "Failed attach to shared session (%d)", rc);
1460 rc = bnxt_ulp_init(bp, session);
1462 BNXT_TF_DBG(ERR, "Failed to initialize the ulp init\n");
1467 /* Update bnxt driver flags */
1468 rc = ulp_dparms_dev_port_intf_update(bp, bp->ulp_ctx);
1470 BNXT_TF_DBG(ERR, "Failed to update driver flags\n");
1474 /* update the port database for the given interface */
1475 rc = ulp_port_db_dev_port_intf_update(bp->ulp_ctx, bp->eth_dev);
1477 BNXT_TF_DBG(ERR, "Failed to update port database\n");
1480 /* create the default rules */
1481 rc = bnxt_ulp_create_df_rules(bp);
1483 BNXT_TF_DBG(ERR, "Failed to create default flow\n");
1487 rc = bnxt_ulp_devid_get(bp, &devid);
1489 BNXT_TF_DBG(ERR, "Unable to determine device for ULP port init.\n");
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");
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");
1508 bnxt_ulp_port_deinit(bp);
1513 * When a port is de-initialized by dpdk. This functions clears up
1514 * the port specific details.
1517 bnxt_ulp_port_deinit(struct bnxt *bp)
1519 struct bnxt_ulp_session_state *session;
1520 struct rte_pci_device *pci_dev;
1521 struct rte_pci_addr *pci_addr;
1523 if (!BNXT_TRUFLOW_EN(bp)) {
1525 "Skip ULP deinit for port:%d, TF is not enabled\n",
1526 bp->eth_dev->data->port_id);
1530 if (!BNXT_PF(bp) && !BNXT_VF_IS_TRUSTED(bp)) {
1532 "Skip ULP deinit port:%d, not a TVF or PF\n",
1533 bp->eth_dev->data->port_id);
1538 BNXT_TF_DBG(DEBUG, "ulp ctx already de-allocated\n");
1542 BNXT_TF_DBG(DEBUG, "BNXT Port:%d ULP port deinit\n",
1543 bp->eth_dev->data->port_id);
1545 /* Free the ulp context in the context entry list */
1546 bnxt_ulp_cntxt_list_del(bp->ulp_ctx);
1548 /* Get the session details */
1549 pci_dev = RTE_DEV_TO_PCI(bp->eth_dev->device);
1550 pci_addr = &pci_dev->addr;
1551 pthread_mutex_lock(&bnxt_ulp_global_mutex);
1552 session = ulp_get_session(bp, pci_addr);
1553 pthread_mutex_unlock(&bnxt_ulp_global_mutex);
1555 /* session not found then just exit */
1557 /* Free the ulp context */
1558 rte_free(bp->ulp_ctx);
1563 /* Check the reference count to deinit or deattach*/
1564 if (bp->ulp_ctx->cfg_data && bp->ulp_ctx->cfg_data->ref_cnt) {
1565 bp->ulp_ctx->cfg_data->ref_cnt--;
1566 if (bp->ulp_ctx->cfg_data->ref_cnt) {
1567 /* free the port details */
1568 /* Free the default flow rule associated to this port */
1569 bnxt_ulp_destroy_df_rules(bp, false);
1570 bnxt_ulp_destroy_vfr_default_rules(bp, false);
1572 /* free flows associated with this port */
1573 bnxt_ulp_flush_port_flows(bp);
1575 /* close the session associated with this port */
1578 /* always detach/close shared after the session. */
1579 ulp_ctx_shared_session_detach(bp);
1581 /* Perform ulp ctx deinit */
1582 bnxt_ulp_deinit(bp, session);
1586 /* clean up the session */
1587 ulp_session_deinit(session);
1589 /* Free the ulp context */
1590 rte_free(bp->ulp_ctx);
1594 /* Below are the access functions to access internal data of ulp context. */
1595 /* Function to set the Mark DB into the context */
1597 bnxt_ulp_cntxt_ptr2_mark_db_set(struct bnxt_ulp_context *ulp_ctx,
1598 struct bnxt_ulp_mark_tbl *mark_tbl)
1600 if (!ulp_ctx || !ulp_ctx->cfg_data) {
1601 BNXT_TF_DBG(ERR, "Invalid ulp context data\n");
1605 ulp_ctx->cfg_data->mark_tbl = mark_tbl;
1610 /* Function to retrieve the Mark DB from the context. */
1611 struct bnxt_ulp_mark_tbl *
1612 bnxt_ulp_cntxt_ptr2_mark_db_get(struct bnxt_ulp_context *ulp_ctx)
1614 if (!ulp_ctx || !ulp_ctx->cfg_data)
1617 return ulp_ctx->cfg_data->mark_tbl;
1621 bnxt_ulp_cntxt_shared_session_enabled(struct bnxt_ulp_context *ulp_ctx)
1623 return ULP_SHARED_SESSION_IS_ENABLED(ulp_ctx->cfg_data->ulp_flags);
1627 bnxt_ulp_cntxt_app_id_set(struct bnxt_ulp_context *ulp_ctx, uint8_t app_id)
1631 ulp_ctx->cfg_data->app_id = app_id;
1636 bnxt_ulp_cntxt_app_id_get(struct bnxt_ulp_context *ulp_ctx, uint8_t *app_id)
1638 /* Default APP id is zero */
1639 if (!ulp_ctx || !app_id)
1641 *app_id = ulp_ctx->cfg_data->app_id;
1645 /* Function to set the device id of the hardware. */
1647 bnxt_ulp_cntxt_dev_id_set(struct bnxt_ulp_context *ulp_ctx,
1650 if (ulp_ctx && ulp_ctx->cfg_data) {
1651 ulp_ctx->cfg_data->dev_id = dev_id;
1658 /* Function to get the device id of the hardware. */
1660 bnxt_ulp_cntxt_dev_id_get(struct bnxt_ulp_context *ulp_ctx,
1663 if (ulp_ctx && ulp_ctx->cfg_data) {
1664 *dev_id = ulp_ctx->cfg_data->dev_id;
1667 *dev_id = BNXT_ULP_DEVICE_ID_LAST;
1668 BNXT_TF_DBG(ERR, "Failed to read dev_id from ulp ctxt\n");
1673 bnxt_ulp_cntxt_mem_type_set(struct bnxt_ulp_context *ulp_ctx,
1674 enum bnxt_ulp_flow_mem_type mem_type)
1676 if (ulp_ctx && ulp_ctx->cfg_data) {
1677 ulp_ctx->cfg_data->mem_type = mem_type;
1680 BNXT_TF_DBG(ERR, "Failed to write mem_type in ulp ctxt\n");
1685 bnxt_ulp_cntxt_mem_type_get(struct bnxt_ulp_context *ulp_ctx,
1686 enum bnxt_ulp_flow_mem_type *mem_type)
1688 if (ulp_ctx && ulp_ctx->cfg_data) {
1689 *mem_type = ulp_ctx->cfg_data->mem_type;
1692 *mem_type = BNXT_ULP_FLOW_MEM_TYPE_LAST;
1693 BNXT_TF_DBG(ERR, "Failed to read mem_type in ulp ctxt\n");
1697 /* Function to get the table scope id of the EEM table. */
1699 bnxt_ulp_cntxt_tbl_scope_id_get(struct bnxt_ulp_context *ulp_ctx,
1700 uint32_t *tbl_scope_id)
1702 if (ulp_ctx && ulp_ctx->cfg_data) {
1703 *tbl_scope_id = ulp_ctx->cfg_data->tbl_scope_id;
1710 /* Function to set the table scope id of the EEM table. */
1712 bnxt_ulp_cntxt_tbl_scope_id_set(struct bnxt_ulp_context *ulp_ctx,
1713 uint32_t tbl_scope_id)
1715 if (ulp_ctx && ulp_ctx->cfg_data) {
1716 ulp_ctx->cfg_data->tbl_scope_id = tbl_scope_id;
1723 /* Function to set the shared tfp session details from the ulp context. */
1725 bnxt_ulp_cntxt_shared_tfp_set(struct bnxt_ulp_context *ulp, struct tf *tfp)
1728 BNXT_TF_DBG(ERR, "Invalid arguments\n");
1733 if (ulp->cfg_data->num_shared_clients > 0)
1734 ulp->cfg_data->num_shared_clients--;
1736 ulp->cfg_data->num_shared_clients++;
1739 ulp->g_shared_tfp = tfp;
1743 /* Function to get the shared tfp session details from the ulp context. */
1745 bnxt_ulp_cntxt_shared_tfp_get(struct bnxt_ulp_context *ulp)
1748 BNXT_TF_DBG(ERR, "Invalid arguments\n");
1751 return ulp->g_shared_tfp;
1754 /* Function to get the number of shared clients attached */
1756 bnxt_ulp_cntxt_num_shared_clients_get(struct bnxt_ulp_context *ulp)
1758 if (ulp == NULL || ulp->cfg_data == NULL) {
1759 BNXT_TF_DBG(ERR, "Invalid arguments\n");
1762 return ulp->cfg_data->num_shared_clients;
1765 /* Function to set the tfp session details from the ulp context. */
1767 bnxt_ulp_cntxt_tfp_set(struct bnxt_ulp_context *ulp, struct tf *tfp)
1770 BNXT_TF_DBG(ERR, "Invalid arguments\n");
1778 /* Function to get the tfp session details from the ulp context. */
1780 bnxt_ulp_cntxt_tfp_get(struct bnxt_ulp_context *ulp,
1781 enum bnxt_ulp_shared_session shared)
1784 BNXT_TF_DBG(ERR, "Invalid arguments\n");
1788 return ulp->g_shared_tfp;
1794 * Get the device table entry based on the device id.
1796 * dev_id [in] The device id of the hardware
1798 * Returns the pointer to the device parameters.
1800 struct bnxt_ulp_device_params *
1801 bnxt_ulp_device_params_get(uint32_t dev_id)
1803 if (dev_id < BNXT_ULP_MAX_NUM_DEVICES)
1804 return &ulp_device_params[dev_id];
1808 /* Function to set the flow database to the ulp context. */
1810 bnxt_ulp_cntxt_ptr2_flow_db_set(struct bnxt_ulp_context *ulp_ctx,
1811 struct bnxt_ulp_flow_db *flow_db)
1813 if (!ulp_ctx || !ulp_ctx->cfg_data)
1816 ulp_ctx->cfg_data->flow_db = flow_db;
1820 /* Function to get the flow database from the ulp context. */
1821 struct bnxt_ulp_flow_db *
1822 bnxt_ulp_cntxt_ptr2_flow_db_get(struct bnxt_ulp_context *ulp_ctx)
1824 if (!ulp_ctx || !ulp_ctx->cfg_data)
1827 return ulp_ctx->cfg_data->flow_db;
1830 /* Function to get the tunnel cache table info from the ulp context. */
1831 struct bnxt_tun_cache_entry *
1832 bnxt_ulp_cntxt_ptr2_tun_tbl_get(struct bnxt_ulp_context *ulp_ctx)
1834 if (!ulp_ctx || !ulp_ctx->cfg_data)
1837 return ulp_ctx->cfg_data->tun_tbl;
1840 /* Function to get the ulp context from eth device. */
1841 struct bnxt_ulp_context *
1842 bnxt_ulp_eth_dev_ptr2_cntxt_get(struct rte_eth_dev *dev)
1844 struct bnxt *bp = (struct bnxt *)dev->data->dev_private;
1846 if (BNXT_ETH_DEV_IS_REPRESENTOR(dev)) {
1847 struct bnxt_representor *vfr = dev->data->dev_private;
1849 bp = vfr->parent_dev->data->dev_private;
1853 BNXT_TF_DBG(ERR, "Bnxt private data is not initialized\n");
1860 bnxt_ulp_cntxt_ptr2_mapper_data_set(struct bnxt_ulp_context *ulp_ctx,
1863 if (!ulp_ctx || !ulp_ctx->cfg_data) {
1864 BNXT_TF_DBG(ERR, "Invalid ulp context data\n");
1868 ulp_ctx->cfg_data->mapper_data = mapper_data;
1873 bnxt_ulp_cntxt_ptr2_mapper_data_get(struct bnxt_ulp_context *ulp_ctx)
1875 if (!ulp_ctx || !ulp_ctx->cfg_data) {
1876 BNXT_TF_DBG(ERR, "Invalid ulp context data\n");
1880 return ulp_ctx->cfg_data->mapper_data;
1883 /* Function to set the port database to the ulp context. */
1885 bnxt_ulp_cntxt_ptr2_port_db_set(struct bnxt_ulp_context *ulp_ctx,
1886 struct bnxt_ulp_port_db *port_db)
1888 if (!ulp_ctx || !ulp_ctx->cfg_data)
1891 ulp_ctx->cfg_data->port_db = port_db;
1895 /* Function to get the port database from the ulp context. */
1896 struct bnxt_ulp_port_db *
1897 bnxt_ulp_cntxt_ptr2_port_db_get(struct bnxt_ulp_context *ulp_ctx)
1899 if (!ulp_ctx || !ulp_ctx->cfg_data)
1902 return ulp_ctx->cfg_data->port_db;
1905 /* Function to set the flow counter info into the context */
1907 bnxt_ulp_cntxt_ptr2_fc_info_set(struct bnxt_ulp_context *ulp_ctx,
1908 struct bnxt_ulp_fc_info *ulp_fc_info)
1910 if (!ulp_ctx || !ulp_ctx->cfg_data) {
1911 BNXT_TF_DBG(ERR, "Invalid ulp context data\n");
1915 ulp_ctx->cfg_data->fc_info = ulp_fc_info;
1920 /* Function to retrieve the flow counter info from the context. */
1921 struct bnxt_ulp_fc_info *
1922 bnxt_ulp_cntxt_ptr2_fc_info_get(struct bnxt_ulp_context *ulp_ctx)
1924 if (!ulp_ctx || !ulp_ctx->cfg_data)
1927 return ulp_ctx->cfg_data->fc_info;
1930 /* Function to get the ulp flags from the ulp context. */
1932 bnxt_ulp_cntxt_ptr2_ulp_flags_get(struct bnxt_ulp_context *ulp_ctx,
1935 if (!ulp_ctx || !ulp_ctx->cfg_data)
1938 *flags = ulp_ctx->cfg_data->ulp_flags;
1942 /* Function to get the ulp vfr info from the ulp context. */
1943 struct bnxt_ulp_vfr_rule_info*
1944 bnxt_ulp_cntxt_ptr2_ulp_vfr_info_get(struct bnxt_ulp_context *ulp_ctx,
1947 if (!ulp_ctx || !ulp_ctx->cfg_data || port_id >= RTE_MAX_ETHPORTS)
1950 return &ulp_ctx->cfg_data->vfr_rule_info[port_id];
1953 /* Function to acquire the flow database lock from the ulp context. */
1955 bnxt_ulp_cntxt_acquire_fdb_lock(struct bnxt_ulp_context *ulp_ctx)
1957 if (!ulp_ctx || !ulp_ctx->cfg_data)
1960 if (pthread_mutex_lock(&ulp_ctx->cfg_data->flow_db_lock)) {
1961 BNXT_TF_DBG(ERR, "unable to acquire fdb lock\n");
1967 /* Function to release the flow database lock from the ulp context. */
1969 bnxt_ulp_cntxt_release_fdb_lock(struct bnxt_ulp_context *ulp_ctx)
1971 if (!ulp_ctx || !ulp_ctx->cfg_data)
1974 pthread_mutex_unlock(&ulp_ctx->cfg_data->flow_db_lock);
1977 /* Function to set the ha info into the context */
1979 bnxt_ulp_cntxt_ptr2_ha_info_set(struct bnxt_ulp_context *ulp_ctx,
1980 struct bnxt_ulp_ha_mgr_info *ulp_ha_info)
1982 if (ulp_ctx == NULL || ulp_ctx->cfg_data == NULL) {
1983 BNXT_TF_DBG(ERR, "Invalid ulp context data\n");
1986 ulp_ctx->cfg_data->ha_info = ulp_ha_info;
1990 /* Function to retrieve the ha info from the context. */
1991 struct bnxt_ulp_ha_mgr_info *
1992 bnxt_ulp_cntxt_ptr2_ha_info_get(struct bnxt_ulp_context *ulp_ctx)
1994 if (ulp_ctx == NULL || ulp_ctx->cfg_data == NULL)
1996 return ulp_ctx->cfg_data->ha_info;
2000 bnxt_ulp_cntxt_ha_enabled(struct bnxt_ulp_context *ulp_ctx)
2002 if (ulp_ctx == NULL || ulp_ctx->cfg_data == NULL)
2004 return !!ULP_HIGH_AVAIL_IS_ENABLED(ulp_ctx->cfg_data->ulp_flags);
2008 bnxt_ulp_cntxt_list_init(void)
2010 /* Create the cntxt spin lock */
2011 rte_spinlock_init(&bnxt_ulp_ctxt_lock);
2017 bnxt_ulp_cntxt_list_add(struct bnxt_ulp_context *ulp_ctx)
2019 struct ulp_context_list_entry *entry;
2021 entry = rte_zmalloc(NULL, sizeof(struct ulp_context_list_entry), 0);
2022 if (entry == NULL) {
2023 BNXT_TF_DBG(ERR, "unable to allocate memory\n");
2027 rte_spinlock_lock(&bnxt_ulp_ctxt_lock);
2028 entry->ulp_ctx = ulp_ctx;
2029 TAILQ_INSERT_TAIL(&ulp_cntx_list, entry, next);
2030 rte_spinlock_unlock(&bnxt_ulp_ctxt_lock);
2035 bnxt_ulp_cntxt_list_del(struct bnxt_ulp_context *ulp_ctx)
2037 struct ulp_context_list_entry *entry, *temp;
2039 rte_spinlock_lock(&bnxt_ulp_ctxt_lock);
2040 RTE_TAILQ_FOREACH_SAFE(entry, &ulp_cntx_list, next, temp) {
2041 if (entry->ulp_ctx == ulp_ctx) {
2042 TAILQ_REMOVE(&ulp_cntx_list, entry, next);
2047 rte_spinlock_unlock(&bnxt_ulp_ctxt_lock);
2050 struct bnxt_ulp_context *
2051 bnxt_ulp_cntxt_entry_acquire(void)
2053 struct ulp_context_list_entry *entry;
2055 /* take a lock and get the first ulp context available */
2056 if (rte_spinlock_trylock(&bnxt_ulp_ctxt_lock)) {
2057 TAILQ_FOREACH(entry, &ulp_cntx_list, next)
2059 return entry->ulp_ctx;
2065 bnxt_ulp_cntxt_entry_release(void)
2067 rte_spinlock_unlock(&bnxt_ulp_ctxt_lock);
2070 /* Function to get the app tunnel details from the ulp context. */
2071 struct bnxt_flow_app_tun_ent *
2072 bnxt_ulp_cntxt_ptr2_app_tun_list_get(struct bnxt_ulp_context *ulp)
2074 if (!ulp || !ulp->cfg_data)
2077 return ulp->cfg_data->app_tun;