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 uint32_t bnxt_ulp_ctxt_lock_created;
39 rte_spinlock_t bnxt_ulp_ctxt_lock;
40 TAILQ_HEAD(cntx_list_entry_list, ulp_context_list_entry);
41 static struct cntx_list_entry_list ulp_cntx_list =
42 TAILQ_HEAD_INITIALIZER(ulp_cntx_list);
44 /* Static function declarations */
45 static int32_t bnxt_ulp_cntxt_list_init(void);
46 static int32_t bnxt_ulp_cntxt_list_add(struct bnxt_ulp_context *ulp_ctx);
47 static void bnxt_ulp_cntxt_list_del(struct bnxt_ulp_context *ulp_ctx);
50 * Allow the deletion of context only for the bnxt device that
51 * created the session.
54 ulp_ctx_deinit_allowed(struct bnxt_ulp_context *ulp_ctx)
56 if (!ulp_ctx || !ulp_ctx->cfg_data)
59 if (!ulp_ctx->cfg_data->ref_cnt) {
60 BNXT_TF_DBG(DEBUG, "ulp ctx shall initiate deinit\n");
68 bnxt_ulp_devid_get(struct bnxt *bp,
69 enum bnxt_ulp_device_id *ulp_dev_id)
71 if (BNXT_CHIP_P5(bp)) {
72 *ulp_dev_id = BNXT_ULP_DEVICE_ID_THOR;
76 if (BNXT_STINGRAY(bp))
77 *ulp_dev_id = BNXT_ULP_DEVICE_ID_STINGRAY;
79 /* Assuming Whitney */
80 *ulp_dev_id = BNXT_ULP_DEVICE_ID_WH_PLUS;
85 struct bnxt_ulp_app_capabilities_info *
86 bnxt_ulp_app_cap_list_get(uint32_t *num_entries)
90 *num_entries = BNXT_ULP_APP_CAP_TBL_MAX_SZ;
91 return ulp_app_cap_info_list;
94 static struct bnxt_ulp_resource_resv_info *
95 bnxt_ulp_app_resource_resv_list_get(uint32_t *num_entries)
97 if (num_entries == NULL)
99 *num_entries = BNXT_ULP_APP_RESOURCE_RESV_LIST_MAX_SZ;
100 return ulp_app_resource_resv_list;
103 struct bnxt_ulp_resource_resv_info *
104 bnxt_ulp_resource_resv_list_get(uint32_t *num_entries)
108 *num_entries = BNXT_ULP_RESOURCE_RESV_LIST_MAX_SZ;
109 return ulp_resource_resv_list;
112 struct bnxt_ulp_glb_resource_info *
113 bnxt_ulp_app_glb_resource_info_list_get(uint32_t *num_entries)
117 *num_entries = BNXT_ULP_APP_GLB_RESOURCE_TBL_MAX_SZ;
118 return ulp_app_glb_resource_tbl;
122 bnxt_ulp_named_resources_calc(struct bnxt_ulp_context *ulp_ctx,
123 struct bnxt_ulp_glb_resource_info *info,
125 struct tf_session_resources *res)
127 uint32_t dev_id = BNXT_ULP_DEVICE_ID_LAST, res_type, i;
132 if (ulp_ctx == NULL || info == NULL || res == NULL || num == 0) {
133 BNXT_TF_DBG(ERR, "Invalid parms to named resources calc.\n");
137 rc = bnxt_ulp_cntxt_app_id_get(ulp_ctx, &app_id);
139 BNXT_TF_DBG(ERR, "Unable to get the app id from ulp.\n");
143 rc = bnxt_ulp_cntxt_dev_id_get(ulp_ctx, &dev_id);
145 BNXT_TF_DBG(ERR, "Unable to get the dev id from ulp.\n");
149 for (i = 0; i < num; i++) {
150 if (dev_id != info[i].device_id || app_id != info[i].app_id)
152 dir = info[i].direction;
153 res_type = info[i].resource_type;
155 switch (info[i].resource_func) {
156 case BNXT_ULP_RESOURCE_FUNC_IDENTIFIER:
157 res->ident_cnt[dir].cnt[res_type]++;
159 case BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE:
160 res->tbl_cnt[dir].cnt[res_type]++;
162 case BNXT_ULP_RESOURCE_FUNC_TCAM_TABLE:
163 res->tcam_cnt[dir].cnt[res_type]++;
165 case BNXT_ULP_RESOURCE_FUNC_EM_TABLE:
166 res->em_cnt[dir].cnt[res_type]++;
169 BNXT_TF_DBG(ERR, "Unknown resource func (0x%x)\n,",
170 info[i].resource_func);
179 bnxt_ulp_unnamed_resources_calc(struct bnxt_ulp_context *ulp_ctx,
180 struct bnxt_ulp_resource_resv_info *info,
182 struct tf_session_resources *res)
184 uint32_t dev_id, res_type, i;
189 if (ulp_ctx == NULL || res == NULL || info == NULL || num == 0) {
190 BNXT_TF_DBG(ERR, "Invalid arguments to get resources.\n");
194 rc = bnxt_ulp_cntxt_app_id_get(ulp_ctx, &app_id);
196 BNXT_TF_DBG(ERR, "Unable to get the app id from ulp.\n");
200 rc = bnxt_ulp_cntxt_dev_id_get(ulp_ctx, &dev_id);
202 BNXT_TF_DBG(ERR, "Unable to get the dev id from ulp.\n");
206 for (i = 0; i < num; i++) {
207 if (app_id != info[i].app_id || dev_id != info[i].device_id)
209 dir = info[i].direction;
210 res_type = info[i].resource_type;
212 switch (info[i].resource_func) {
213 case BNXT_ULP_RESOURCE_FUNC_IDENTIFIER:
214 res->ident_cnt[dir].cnt[res_type] = info[i].count;
216 case BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE:
217 res->tbl_cnt[dir].cnt[res_type] = info[i].count;
219 case BNXT_ULP_RESOURCE_FUNC_TCAM_TABLE:
220 res->tcam_cnt[dir].cnt[res_type] = info[i].count;
222 case BNXT_ULP_RESOURCE_FUNC_EM_TABLE:
223 res->em_cnt[dir].cnt[res_type] = info[i].count;
233 bnxt_ulp_tf_resources_get(struct bnxt_ulp_context *ulp_ctx,
234 struct tf_session_resources *res)
236 struct bnxt_ulp_resource_resv_info *unnamed = NULL;
240 if (ulp_ctx == NULL || res == NULL) {
241 BNXT_TF_DBG(ERR, "Invalid arguments to get resources.\n");
245 unnamed = bnxt_ulp_resource_resv_list_get(&unum);
246 if (unnamed == NULL) {
247 BNXT_TF_DBG(ERR, "Unable to get resource resv list.\n");
251 rc = bnxt_ulp_unnamed_resources_calc(ulp_ctx, unnamed, unum, res);
253 BNXT_TF_DBG(ERR, "Unable to calc resources for session.\n");
259 bnxt_ulp_tf_shared_session_resources_get(struct bnxt_ulp_context *ulp_ctx,
260 struct tf_session_resources *res)
262 struct bnxt_ulp_resource_resv_info *unnamed;
263 struct bnxt_ulp_glb_resource_info *named;
267 if (ulp_ctx == NULL || res == NULL) {
268 BNXT_TF_DBG(ERR, "Invalid arguments to get resources.\n");
272 /* Make sure the resources are zero before accumulating. */
273 memset(res, 0, sizeof(struct tf_session_resources));
276 * Shared resources are comprised of both named and unnamed resources.
277 * First get the unnamed counts, and then add the named to the result.
279 /* Get the baseline counts */
280 unnamed = bnxt_ulp_app_resource_resv_list_get(&unum);
281 if (unnamed == NULL) {
282 BNXT_TF_DBG(ERR, "Unable to get shared resource resv list.\n");
285 rc = bnxt_ulp_unnamed_resources_calc(ulp_ctx, unnamed, unum, res);
287 BNXT_TF_DBG(ERR, "Unable to calc resources for shared session.\n");
291 /* Get the named list and add the totals */
292 named = bnxt_ulp_app_glb_resource_info_list_get(&nnum);
294 BNXT_TF_DBG(ERR, "Unable to get app global resource list\n");
297 rc = bnxt_ulp_named_resources_calc(ulp_ctx, named, nnum, res);
299 BNXT_TF_DBG(ERR, "Unable to calc named resources\n");
305 bnxt_ulp_cntxt_app_caps_init(struct bnxt *bp,
306 uint8_t app_id, uint32_t dev_id)
308 struct bnxt_ulp_app_capabilities_info *info;
312 struct bnxt_ulp_context *ulp_ctx = bp->ulp_ctx;
314 if (ULP_APP_DEV_UNSUPPORTED_ENABLED(ulp_ctx->cfg_data->ulp_flags)) {
315 BNXT_TF_DBG(ERR, "APP ID %d, Device ID: 0x%x not supported.\n",
320 info = bnxt_ulp_app_cap_list_get(&num);
322 BNXT_TF_DBG(ERR, "Failed to get app capabilities.\n");
326 for (i = 0; i < num; i++) {
327 if (info[i].app_id != app_id || info[i].device_id != dev_id)
330 if (info[i].flags & BNXT_ULP_APP_CAP_SHARED_EN)
331 ulp_ctx->cfg_data->ulp_flags |=
332 BNXT_ULP_SHARED_SESSION_ENABLED;
333 if (info[i].flags & BNXT_ULP_APP_CAP_HOT_UPGRADE_EN)
334 ulp_ctx->cfg_data->ulp_flags |=
335 BNXT_ULP_HIGH_AVAIL_ENABLED;
336 if (info[i].flags & BNXT_ULP_APP_CAP_UNICAST_ONLY)
337 ulp_ctx->cfg_data->ulp_flags |=
338 BNXT_ULP_APP_UNICAST_ONLY;
339 if (info[i].flags & BNXT_ULP_APP_CAP_SOCKET_DIRECT) {
340 /* Enable socket direction only if MR is enabled in fw*/
341 if (BNXT_MULTIROOT_EN(bp)) {
342 ulp_ctx->cfg_data->ulp_flags |=
343 BNXT_ULP_APP_SOCKET_DIRECT;
345 "Socket Direct feature is enabled");
350 BNXT_TF_DBG(ERR, "APP ID %d, Device ID: 0x%x not supported.\n",
352 ulp_ctx->cfg_data->ulp_flags |= BNXT_ULP_APP_DEV_UNSUPPORTED;
360 ulp_ctx_shared_session_close(struct bnxt *bp,
361 struct bnxt_ulp_session_state *session)
366 if (!bnxt_ulp_cntxt_shared_session_enabled(bp->ulp_ctx))
369 tfp = bnxt_ulp_cntxt_shared_tfp_get(bp->ulp_ctx);
372 * Log it under debug since this is likely a case of the
373 * shared session not being created. For example, a failed
376 BNXT_TF_DBG(DEBUG, "Failed to get shared tfp on close.\n");
379 rc = tf_close_session(tfp);
381 BNXT_TF_DBG(ERR, "Failed to close the shared session rc=%d.\n",
383 (void)bnxt_ulp_cntxt_shared_tfp_set(bp->ulp_ctx, NULL);
385 session->g_shared_tfp.session = NULL;
389 ulp_ctx_shared_session_open(struct bnxt *bp,
390 struct bnxt_ulp_session_state *session)
392 struct rte_eth_dev *ethdev = bp->eth_dev;
393 struct tf_session_resources *resources;
394 struct tf_open_session_parms parms;
396 uint32_t ulp_dev_id = BNXT_ULP_DEVICE_ID_LAST;
400 /* only perform this if shared session is enabled. */
401 if (!bnxt_ulp_cntxt_shared_session_enabled(bp->ulp_ctx))
404 memset(&parms, 0, sizeof(parms));
406 rc = rte_eth_dev_get_name_by_port(ethdev->data->port_id,
407 parms.ctrl_chan_name);
409 BNXT_TF_DBG(ERR, "Invalid port %d, rc = %d\n",
410 ethdev->data->port_id, rc);
413 resources = &parms.resources;
416 * Need to account for size of ctrl_chan_name and 1 extra for Null
419 copy_nbytes = sizeof(parms.ctrl_chan_name) -
420 strlen(parms.ctrl_chan_name) - 1;
423 * Build the ctrl_chan_name with shared token.
424 * When HA is enabled, the WC TCAM needs extra management by the core,
425 * so add the wc_tcam string to the control channel.
427 if (bnxt_ulp_cntxt_ha_enabled(bp->ulp_ctx))
428 strncat(parms.ctrl_chan_name, "-tf_shared-wc_tcam",
431 strncat(parms.ctrl_chan_name, "-tf_shared", copy_nbytes);
433 rc = bnxt_ulp_tf_shared_session_resources_get(bp->ulp_ctx, resources);
437 rc = bnxt_ulp_cntxt_app_id_get(bp->ulp_ctx, &app_id);
439 BNXT_TF_DBG(ERR, "Unable to get the app id from ulp.\n");
443 rc = bnxt_ulp_cntxt_dev_id_get(bp->ulp_ctx, &ulp_dev_id);
445 BNXT_TF_DBG(ERR, "Unable to get device id from ulp.\n");
449 switch (ulp_dev_id) {
450 case BNXT_ULP_DEVICE_ID_WH_PLUS:
451 parms.device_type = TF_DEVICE_TYPE_WH;
453 case BNXT_ULP_DEVICE_ID_STINGRAY:
454 parms.device_type = TF_DEVICE_TYPE_SR;
456 case BNXT_ULP_DEVICE_ID_THOR:
457 parms.device_type = TF_DEVICE_TYPE_THOR;
460 BNXT_TF_DBG(ERR, "Unable to determine dev for opening session.\n");
464 parms.shadow_copy = true;
467 parms.wc_num_slices = TF_WC_TCAM_2_SLICE_PER_ROW;
469 parms.wc_num_slices = TF_WC_TCAM_1_SLICE_PER_ROW;
472 * Open the session here, but the collect the resources during the
473 * mapper initialization.
475 rc = tf_open_session(&bp->tfp_shared, &parms);
479 if (parms.shared_session_creator)
480 BNXT_TF_DBG(DEBUG, "Shared session creator.\n");
482 BNXT_TF_DBG(DEBUG, "Shared session attached.\n");
484 /* Save the shared session in global data */
485 if (!session->g_shared_tfp.session)
486 session->g_shared_tfp.session = bp->tfp_shared.session;
488 rc = bnxt_ulp_cntxt_shared_tfp_set(bp->ulp_ctx, &bp->tfp_shared);
490 BNXT_TF_DBG(ERR, "Failed to add shared tfp to ulp (%d)\n", rc);
496 ulp_ctx_shared_session_attach(struct bnxt *bp,
497 struct bnxt_ulp_session_state *session)
501 /* Simply return success if shared session not enabled */
502 if (bnxt_ulp_cntxt_shared_session_enabled(bp->ulp_ctx)) {
503 bp->tfp_shared.session = session->g_shared_tfp.session;
504 rc = ulp_ctx_shared_session_open(bp, session);
511 ulp_ctx_shared_session_detach(struct bnxt *bp)
513 if (bnxt_ulp_cntxt_shared_session_enabled(bp->ulp_ctx)) {
514 if (bp->tfp_shared.session) {
515 tf_close_session(&bp->tfp_shared);
516 bp->tfp_shared.session = NULL;
522 * Initialize an ULP session.
523 * An ULP session will contain all the resources needed to support rte flow
524 * offloads. A session is initialized as part of rte_eth_device start.
525 * A single vswitch instance can have multiple uplinks which means
526 * rte_eth_device start will be called for each of these devices.
527 * ULP session manager will make sure that a single ULP session is only
528 * initialized once. Apart from this, it also initializes MARK database,
529 * EEM table & flow database. ULP session manager also manages a list of
530 * all opened ULP sessions.
533 ulp_ctx_session_open(struct bnxt *bp,
534 struct bnxt_ulp_session_state *session)
536 struct rte_eth_dev *ethdev = bp->eth_dev;
538 struct tf_open_session_parms params;
539 struct tf_session_resources *resources;
540 uint32_t ulp_dev_id = BNXT_ULP_DEVICE_ID_LAST;
543 memset(¶ms, 0, sizeof(params));
545 rc = rte_eth_dev_get_name_by_port(ethdev->data->port_id,
546 params.ctrl_chan_name);
548 BNXT_TF_DBG(ERR, "Invalid port %d, rc = %d\n",
549 ethdev->data->port_id, rc);
553 params.shadow_copy = true;
555 rc = bnxt_ulp_cntxt_app_id_get(bp->ulp_ctx, &app_id);
557 BNXT_TF_DBG(ERR, "Unable to get the app id from ulp.\n");
561 rc = bnxt_ulp_cntxt_dev_id_get(bp->ulp_ctx, &ulp_dev_id);
563 BNXT_TF_DBG(ERR, "Unable to get device id from ulp.\n");
567 switch (ulp_dev_id) {
568 case BNXT_ULP_DEVICE_ID_WH_PLUS:
569 params.device_type = TF_DEVICE_TYPE_WH;
571 case BNXT_ULP_DEVICE_ID_STINGRAY:
572 params.device_type = TF_DEVICE_TYPE_SR;
574 case BNXT_ULP_DEVICE_ID_THOR:
575 params.device_type = TF_DEVICE_TYPE_THOR;
578 BNXT_TF_DBG(ERR, "Unable to determine device for opening session.\n");
582 resources = ¶ms.resources;
583 rc = bnxt_ulp_tf_resources_get(bp->ulp_ctx, resources);
589 params.wc_num_slices = TF_WC_TCAM_2_SLICE_PER_ROW;
591 params.wc_num_slices = TF_WC_TCAM_1_SLICE_PER_ROW;
593 rc = tf_open_session(&bp->tfp, ¶ms);
595 BNXT_TF_DBG(ERR, "Failed to open TF session - %s, rc = %d\n",
596 params.ctrl_chan_name, rc);
599 if (!session->session_opened) {
600 session->session_opened = 1;
601 session->g_tfp = rte_zmalloc("bnxt_ulp_session_tfp",
602 sizeof(struct tf), 0);
603 session->g_tfp->session = bp->tfp.session;
609 * Close the ULP session.
610 * It takes the ulp context pointer.
613 ulp_ctx_session_close(struct bnxt *bp,
614 struct bnxt_ulp_session_state *session)
616 /* close the session in the hardware */
617 if (session->session_opened)
618 tf_close_session(&bp->tfp);
619 session->session_opened = 0;
620 rte_free(session->g_tfp);
621 session->g_tfp = NULL;
625 bnxt_init_tbl_scope_parms(struct bnxt *bp,
626 struct tf_alloc_tbl_scope_parms *params)
628 struct bnxt_ulp_device_params *dparms;
632 rc = bnxt_ulp_cntxt_dev_id_get(bp->ulp_ctx, &dev_id);
634 /* TBD: For now, just use default. */
637 dparms = bnxt_ulp_device_params_get(dev_id);
640 * Set the flush timer for EEM entries. The value is in 100ms intervals,
643 params->hw_flow_cache_flush_timer = 100;
646 params->rx_max_key_sz_in_bits = BNXT_ULP_DFLT_RX_MAX_KEY;
647 params->rx_max_action_entry_sz_in_bits =
648 BNXT_ULP_DFLT_RX_MAX_ACTN_ENTRY;
649 params->rx_mem_size_in_mb = BNXT_ULP_DFLT_RX_MEM;
650 params->rx_num_flows_in_k = BNXT_ULP_RX_NUM_FLOWS;
652 params->tx_max_key_sz_in_bits = BNXT_ULP_DFLT_TX_MAX_KEY;
653 params->tx_max_action_entry_sz_in_bits =
654 BNXT_ULP_DFLT_TX_MAX_ACTN_ENTRY;
655 params->tx_mem_size_in_mb = BNXT_ULP_DFLT_TX_MEM;
656 params->tx_num_flows_in_k = BNXT_ULP_TX_NUM_FLOWS;
658 params->rx_max_key_sz_in_bits = BNXT_ULP_DFLT_RX_MAX_KEY;
659 params->rx_max_action_entry_sz_in_bits =
660 BNXT_ULP_DFLT_RX_MAX_ACTN_ENTRY;
661 params->rx_mem_size_in_mb = BNXT_ULP_DFLT_RX_MEM;
662 params->rx_num_flows_in_k =
663 dparms->ext_flow_db_num_entries / 1024;
665 params->tx_max_key_sz_in_bits = BNXT_ULP_DFLT_TX_MAX_KEY;
666 params->tx_max_action_entry_sz_in_bits =
667 BNXT_ULP_DFLT_TX_MAX_ACTN_ENTRY;
668 params->tx_mem_size_in_mb = BNXT_ULP_DFLT_TX_MEM;
669 params->tx_num_flows_in_k =
670 dparms->ext_flow_db_num_entries / 1024;
672 BNXT_TF_DBG(INFO, "Table Scope initialized with %uK flows.\n",
673 params->rx_num_flows_in_k);
676 /* Initialize Extended Exact Match host memory. */
678 ulp_eem_tbl_scope_init(struct bnxt *bp)
680 struct tf_alloc_tbl_scope_parms params = {0};
681 struct bnxt_ulp_device_params *dparms;
682 enum bnxt_ulp_flow_mem_type mtype;
686 /* Get the dev specific number of flows that needed to be supported. */
687 if (bnxt_ulp_cntxt_dev_id_get(bp->ulp_ctx, &dev_id)) {
688 BNXT_TF_DBG(ERR, "Invalid device id\n");
692 dparms = bnxt_ulp_device_params_get(dev_id);
694 BNXT_TF_DBG(ERR, "could not fetch the device params\n");
698 if (bnxt_ulp_cntxt_mem_type_get(bp->ulp_ctx, &mtype))
700 if (mtype != BNXT_ULP_FLOW_MEM_TYPE_EXT) {
701 BNXT_TF_DBG(INFO, "Table Scope alloc is not required\n");
705 bnxt_init_tbl_scope_parms(bp, ¶ms);
706 rc = tf_alloc_tbl_scope(&bp->tfp, ¶ms);
708 BNXT_TF_DBG(ERR, "Unable to allocate eem table scope rc = %d\n",
712 rc = bnxt_ulp_cntxt_tbl_scope_id_set(bp->ulp_ctx, params.tbl_scope_id);
714 BNXT_TF_DBG(ERR, "Unable to set table scope id\n");
721 /* Free Extended Exact Match host memory */
723 ulp_eem_tbl_scope_deinit(struct bnxt *bp, struct bnxt_ulp_context *ulp_ctx)
725 struct tf_free_tbl_scope_parms params = {0};
728 struct bnxt_ulp_device_params *dparms;
729 enum bnxt_ulp_flow_mem_type mtype;
732 if (!ulp_ctx || !ulp_ctx->cfg_data)
735 tfp = bnxt_ulp_cntxt_tfp_get(ulp_ctx, BNXT_ULP_SHARED_SESSION_NO);
737 BNXT_TF_DBG(ERR, "Failed to get the truflow pointer\n");
741 /* Get the dev specific number of flows that needed to be supported. */
742 if (bnxt_ulp_cntxt_dev_id_get(bp->ulp_ctx, &dev_id)) {
743 BNXT_TF_DBG(ERR, "Invalid device id\n");
747 dparms = bnxt_ulp_device_params_get(dev_id);
749 BNXT_TF_DBG(ERR, "could not fetch the device params\n");
753 if (bnxt_ulp_cntxt_mem_type_get(ulp_ctx, &mtype))
755 if (mtype != BNXT_ULP_FLOW_MEM_TYPE_EXT) {
756 BNXT_TF_DBG(INFO, "Table Scope free is not required\n");
760 rc = bnxt_ulp_cntxt_tbl_scope_id_get(ulp_ctx, ¶ms.tbl_scope_id);
762 BNXT_TF_DBG(ERR, "Failed to get the table scope id\n");
766 rc = tf_free_tbl_scope(tfp, ¶ms);
768 BNXT_TF_DBG(ERR, "Unable to free table scope\n");
774 /* The function to free and deinit the ulp context data. */
776 ulp_ctx_deinit(struct bnxt *bp,
777 struct bnxt_ulp_session_state *session)
779 /* close the tf session */
780 ulp_ctx_session_close(bp, session);
782 /* The shared session must be closed last. */
783 ulp_ctx_shared_session_close(bp, session);
785 /* Free the contents */
786 if (session->cfg_data) {
787 rte_free(session->cfg_data);
788 bp->ulp_ctx->cfg_data = NULL;
789 session->cfg_data = NULL;
794 /* The function to allocate and initialize the ulp context data. */
796 ulp_ctx_init(struct bnxt *bp,
797 struct bnxt_ulp_session_state *session)
799 struct bnxt_ulp_data *ulp_data;
801 enum bnxt_ulp_device_id devid;
803 /* Initialize the context entries list */
804 bnxt_ulp_cntxt_list_init();
806 /* Add the context to the context entries list */
807 rc = bnxt_ulp_cntxt_list_add(bp->ulp_ctx);
809 BNXT_TF_DBG(ERR, "Failed to add the context list entry\n");
813 /* Allocate memory to hold ulp context data. */
814 ulp_data = rte_zmalloc("bnxt_ulp_data",
815 sizeof(struct bnxt_ulp_data), 0);
817 BNXT_TF_DBG(ERR, "Failed to allocate memory for ulp data\n");
821 /* Increment the ulp context data reference count usage. */
822 bp->ulp_ctx->cfg_data = ulp_data;
823 session->cfg_data = ulp_data;
825 ulp_data->ulp_flags |= BNXT_ULP_VF_REP_ENABLED;
827 rc = bnxt_ulp_devid_get(bp, &devid);
829 BNXT_TF_DBG(ERR, "Unable to determine device for ULP init.\n");
833 rc = bnxt_ulp_cntxt_dev_id_set(bp->ulp_ctx, devid);
835 BNXT_TF_DBG(ERR, "Unable to set device for ULP init.\n");
839 rc = bnxt_ulp_cntxt_app_id_set(bp->ulp_ctx, bp->app_id);
841 BNXT_TF_DBG(ERR, "Unable to set app_id for ULP init.\n");
844 BNXT_TF_DBG(DEBUG, "Ulp initialized with app id %d\n", bp->app_id);
846 rc = bnxt_ulp_cntxt_app_caps_init(bp, bp->app_id, devid);
848 BNXT_TF_DBG(ERR, "Unable to set caps for app(%x)/dev(%x)\n",
854 * Shared session must be created before first regular session but after
855 * the ulp_ctx is valid.
857 rc = ulp_ctx_shared_session_open(bp, session);
859 BNXT_TF_DBG(ERR, "Unable to open shared session (%d)\n", rc);
863 /* Open the ulp session. */
864 rc = ulp_ctx_session_open(bp, session);
868 bnxt_ulp_cntxt_tfp_set(bp->ulp_ctx, &bp->tfp);
872 session->session_opened = 1;
873 (void)ulp_ctx_deinit(bp, session);
877 /* The function to initialize ulp dparms with devargs */
879 ulp_dparms_init(struct bnxt *bp, struct bnxt_ulp_context *ulp_ctx)
881 struct bnxt_ulp_device_params *dparms;
882 uint32_t dev_id = BNXT_ULP_DEVICE_ID_LAST;
884 if (!bp->max_num_kflows) {
885 /* Defaults to Internal */
886 bnxt_ulp_cntxt_mem_type_set(ulp_ctx,
887 BNXT_ULP_FLOW_MEM_TYPE_INT);
891 /* The max_num_kflows were set, so move to external */
892 if (bnxt_ulp_cntxt_mem_type_set(ulp_ctx, BNXT_ULP_FLOW_MEM_TYPE_EXT))
895 if (bnxt_ulp_cntxt_dev_id_get(ulp_ctx, &dev_id)) {
896 BNXT_TF_DBG(DEBUG, "Failed to get device id\n");
900 dparms = bnxt_ulp_device_params_get(dev_id);
902 BNXT_TF_DBG(DEBUG, "Failed to get device parms\n");
906 /* num_flows = max_num_kflows * 1024 */
907 dparms->ext_flow_db_num_entries = bp->max_num_kflows * 1024;
908 /* GFID = 2 * num_flows */
909 dparms->mark_db_gfid_entries = dparms->ext_flow_db_num_entries * 2;
910 BNXT_TF_DBG(DEBUG, "Set the number of flows = %" PRIu64 "\n",
911 dparms->ext_flow_db_num_entries);
916 /* The function to initialize bp flags with truflow features */
918 ulp_dparms_dev_port_intf_update(struct bnxt *bp,
919 struct bnxt_ulp_context *ulp_ctx)
921 enum bnxt_ulp_flow_mem_type mtype;
923 if (bnxt_ulp_cntxt_mem_type_get(ulp_ctx, &mtype))
925 /* Update the bp flag with gfid flag */
926 if (mtype == BNXT_ULP_FLOW_MEM_TYPE_EXT)
927 bp->flags |= BNXT_FLAG_GFID_ENABLE;
933 ulp_ctx_attach(struct bnxt *bp,
934 struct bnxt_ulp_session_state *session)
937 uint32_t flags, dev_id = BNXT_ULP_DEVICE_ID_LAST;
940 /* Increment the ulp context data reference count usage. */
941 bp->ulp_ctx->cfg_data = session->cfg_data;
942 bp->ulp_ctx->cfg_data->ref_cnt++;
944 /* update the session details in bnxt tfp */
945 bp->tfp.session = session->g_tfp->session;
947 /* Add the context to the context entries list */
948 rc = bnxt_ulp_cntxt_list_add(bp->ulp_ctx);
950 BNXT_TF_DBG(ERR, "Failed to add the context list entry\n");
955 * The supported flag will be set during the init. Use it now to
956 * know if we should go through the attach.
958 rc = bnxt_ulp_cntxt_app_id_get(bp->ulp_ctx, &app_id);
960 BNXT_TF_DBG(ERR, "Unable to get the app id from ulp.\n");
964 rc = bnxt_ulp_cntxt_dev_id_get(bp->ulp_ctx, &dev_id);
966 BNXT_TF_DBG(ERR, "Unable do get the dev_id.\n");
970 flags = bp->ulp_ctx->cfg_data->ulp_flags;
971 if (ULP_APP_DEV_UNSUPPORTED_ENABLED(flags)) {
972 BNXT_TF_DBG(ERR, "APP ID %d, Device ID: 0x%x not supported.\n",
977 /* Create a TF Client */
978 rc = ulp_ctx_session_open(bp, session);
980 PMD_DRV_LOG(ERR, "Failed to open ctxt session, rc:%d\n", rc);
981 bp->tfp.session = NULL;
985 bnxt_ulp_cntxt_tfp_set(bp->ulp_ctx, &bp->tfp);
990 ulp_ctx_detach(struct bnxt *bp)
992 if (bp->tfp.session) {
993 tf_close_session(&bp->tfp);
994 bp->tfp.session = NULL;
999 * Initialize the state of an ULP session.
1000 * If the state of an ULP session is not initialized, set it's state to
1001 * initialized. If the state is already initialized, do nothing.
1004 ulp_context_initialized(struct bnxt_ulp_session_state *session, bool *init)
1006 pthread_mutex_lock(&session->bnxt_ulp_mutex);
1008 if (!session->bnxt_ulp_init) {
1009 session->bnxt_ulp_init = true;
1015 pthread_mutex_unlock(&session->bnxt_ulp_mutex);
1019 * Check if an ULP session is already allocated for a specific PCI
1020 * domain & bus. If it is already allocated simply return the session
1021 * pointer, otherwise allocate a new session.
1023 static struct bnxt_ulp_session_state *
1024 ulp_get_session(struct bnxt *bp, struct rte_pci_addr *pci_addr)
1026 struct bnxt_ulp_session_state *session;
1028 /* if multi root capability is enabled, then ignore the pci bus id */
1029 STAILQ_FOREACH(session, &bnxt_ulp_session_list, next) {
1030 if (session->pci_info.domain == pci_addr->domain &&
1031 (BNXT_MULTIROOT_EN(bp) ||
1032 session->pci_info.bus == pci_addr->bus)) {
1040 * Allocate and Initialize an ULP session and set it's state to INITIALIZED.
1041 * If it's already initialized simply return the already existing session.
1043 static struct bnxt_ulp_session_state *
1044 ulp_session_init(struct bnxt *bp,
1047 struct rte_pci_device *pci_dev;
1048 struct rte_pci_addr *pci_addr;
1049 struct bnxt_ulp_session_state *session;
1055 pci_dev = RTE_DEV_TO_PCI(bp->eth_dev->device);
1056 pci_addr = &pci_dev->addr;
1058 pthread_mutex_lock(&bnxt_ulp_global_mutex);
1060 session = ulp_get_session(bp, pci_addr);
1062 /* Not Found the session Allocate a new one */
1063 session = rte_zmalloc("bnxt_ulp_session",
1064 sizeof(struct bnxt_ulp_session_state),
1068 "Allocation failed for bnxt_ulp_session\n");
1069 pthread_mutex_unlock(&bnxt_ulp_global_mutex);
1073 /* Add it to the queue */
1074 session->pci_info.domain = pci_addr->domain;
1075 session->pci_info.bus = pci_addr->bus;
1076 rc = pthread_mutex_init(&session->bnxt_ulp_mutex, NULL);
1078 BNXT_TF_DBG(ERR, "mutex create failed\n");
1079 pthread_mutex_unlock(&bnxt_ulp_global_mutex);
1082 STAILQ_INSERT_TAIL(&bnxt_ulp_session_list,
1086 ulp_context_initialized(session, init);
1087 pthread_mutex_unlock(&bnxt_ulp_global_mutex);
1092 * When a device is closed, remove it's associated session from the global
1096 ulp_session_deinit(struct bnxt_ulp_session_state *session)
1101 if (!session->cfg_data) {
1102 pthread_mutex_lock(&bnxt_ulp_global_mutex);
1103 STAILQ_REMOVE(&bnxt_ulp_session_list, session,
1104 bnxt_ulp_session_state, next);
1105 pthread_mutex_destroy(&session->bnxt_ulp_mutex);
1107 pthread_mutex_unlock(&bnxt_ulp_global_mutex);
1112 * Internal api to enable NAT feature.
1113 * Set set_flag to 1 to set the value or zero to reset the value.
1114 * returns 0 on success.
1117 bnxt_ulp_global_cfg_update(struct bnxt *bp,
1119 enum tf_global_config_type type,
1124 uint32_t global_cfg = 0;
1126 struct tf_global_cfg_parms parms = { 0 };
1128 /* Initialize the params */
1131 parms.offset = offset,
1132 parms.config = (uint8_t *)&global_cfg,
1133 parms.config_sz_in_bytes = sizeof(global_cfg);
1135 rc = tf_get_global_cfg(&bp->tfp, &parms);
1137 BNXT_TF_DBG(ERR, "Failed to get global cfg 0x%x rc:%d\n",
1143 global_cfg |= value;
1145 global_cfg &= ~value;
1147 /* SET the register RE_CFA_REG_ACT_TECT */
1148 rc = tf_set_global_cfg(&bp->tfp, &parms);
1150 BNXT_TF_DBG(ERR, "Failed to set global cfg 0x%x rc:%d\n",
1157 /* Internal function to delete all the flows belonging to the given port */
1159 bnxt_ulp_flush_port_flows(struct bnxt *bp)
1163 /* it is assumed that port is either TVF or PF */
1164 if (ulp_port_db_port_func_id_get(bp->ulp_ctx,
1165 bp->eth_dev->data->port_id,
1167 BNXT_TF_DBG(ERR, "Invalid argument\n");
1170 (void)ulp_flow_db_function_flow_flush(bp->ulp_ctx, func_id);
1173 /* Internal function to delete the VFR default flows */
1175 bnxt_ulp_destroy_vfr_default_rules(struct bnxt *bp, bool global)
1177 struct bnxt_ulp_vfr_rule_info *info;
1179 struct rte_eth_dev *vfr_eth_dev;
1180 struct bnxt_representor *vfr_bp;
1182 if (!BNXT_TRUFLOW_EN(bp) || BNXT_ETH_DEV_IS_REPRESENTOR(bp->eth_dev))
1185 if (!bp->ulp_ctx || !bp->ulp_ctx->cfg_data)
1188 /* Delete default rules for all ports */
1189 for (port_id = 0; port_id < RTE_MAX_ETHPORTS; port_id++) {
1190 info = &bp->ulp_ctx->cfg_data->vfr_rule_info[port_id];
1194 if (!global && info->parent_port_id !=
1195 bp->eth_dev->data->port_id)
1198 /* Destroy the flows */
1199 ulp_default_flow_destroy(bp->eth_dev, info->vfr_flow_id);
1200 /* Clean up the tx action pointer */
1201 vfr_eth_dev = &rte_eth_devices[port_id];
1203 vfr_bp = vfr_eth_dev->data->dev_private;
1204 vfr_bp->vfr_tx_cfa_action = 0;
1206 memset(info, 0, sizeof(struct bnxt_ulp_vfr_rule_info));
1211 * When a port is deinit'ed by dpdk. This function is called
1212 * and this function clears the ULP context and rest of the
1213 * infrastructure associated with it.
1216 bnxt_ulp_deinit(struct bnxt *bp,
1217 struct bnxt_ulp_session_state *session)
1221 if (!bp->ulp_ctx || !bp->ulp_ctx->cfg_data)
1224 ha_enabled = bnxt_ulp_cntxt_ha_enabled(bp->ulp_ctx);
1225 if (ha_enabled && session->session_opened) {
1226 int32_t rc = ulp_ha_mgr_close(bp->ulp_ctx);
1228 BNXT_TF_DBG(ERR, "Failed to close HA (%d)\n", rc);
1231 /* clean up default flows */
1232 bnxt_ulp_destroy_df_rules(bp, true);
1234 /* clean up default VFR flows */
1235 bnxt_ulp_destroy_vfr_default_rules(bp, true);
1237 /* clean up regular flows */
1238 ulp_flow_db_flush_flows(bp->ulp_ctx, BNXT_ULP_FDB_TYPE_REGULAR);
1240 /* cleanup the eem table scope */
1241 ulp_eem_tbl_scope_deinit(bp, bp->ulp_ctx);
1243 /* cleanup the flow database */
1244 ulp_flow_db_deinit(bp->ulp_ctx);
1246 /* Delete the Mark database */
1247 ulp_mark_db_deinit(bp->ulp_ctx);
1249 /* cleanup the ulp mapper */
1250 ulp_mapper_deinit(bp->ulp_ctx);
1252 /* Delete the Flow Counter Manager */
1253 ulp_fc_mgr_deinit(bp->ulp_ctx);
1255 /* Delete the Port database */
1256 ulp_port_db_deinit(bp->ulp_ctx);
1258 /* Disable NAT feature */
1259 (void)bnxt_ulp_global_cfg_update(bp, TF_DIR_RX, TF_TUNNEL_ENCAP,
1260 TF_TUNNEL_ENCAP_NAT,
1261 BNXT_ULP_NAT_OUTER_MOST_FLAGS, 0);
1263 (void)bnxt_ulp_global_cfg_update(bp, TF_DIR_TX, TF_TUNNEL_ENCAP,
1264 TF_TUNNEL_ENCAP_NAT,
1265 BNXT_ULP_NAT_OUTER_MOST_FLAGS, 0);
1267 /* free the flow db lock */
1268 pthread_mutex_destroy(&bp->ulp_ctx->cfg_data->flow_db_lock);
1271 ulp_ha_mgr_deinit(bp->ulp_ctx);
1273 /* Delete the ulp context and tf session and free the ulp context */
1274 ulp_ctx_deinit(bp, session);
1275 BNXT_TF_DBG(DEBUG, "ulp ctx has been deinitialized\n");
1279 * When a port is initialized by dpdk. This functions is called
1280 * and this function initializes the ULP context and rest of the
1281 * infrastructure associated with it.
1284 bnxt_ulp_init(struct bnxt *bp,
1285 struct bnxt_ulp_session_state *session)
1289 /* Allocate and Initialize the ulp context. */
1290 rc = ulp_ctx_init(bp, session);
1292 BNXT_TF_DBG(ERR, "Failed to create the ulp context\n");
1296 rc = pthread_mutex_init(&bp->ulp_ctx->cfg_data->flow_db_lock, NULL);
1298 BNXT_TF_DBG(ERR, "Unable to initialize flow db lock\n");
1302 /* Initialize ulp dparms with values devargs passed */
1303 rc = ulp_dparms_init(bp, bp->ulp_ctx);
1305 BNXT_TF_DBG(ERR, "Failed to initialize the dparms\n");
1309 /* create the port database */
1310 rc = ulp_port_db_init(bp->ulp_ctx, bp->port_cnt);
1312 BNXT_TF_DBG(ERR, "Failed to create the port database\n");
1316 /* Create the Mark database. */
1317 rc = ulp_mark_db_init(bp->ulp_ctx);
1319 BNXT_TF_DBG(ERR, "Failed to create the mark database\n");
1323 /* Create the flow database. */
1324 rc = ulp_flow_db_init(bp->ulp_ctx);
1326 BNXT_TF_DBG(ERR, "Failed to create the flow database\n");
1330 /* Create the eem table scope. */
1331 rc = ulp_eem_tbl_scope_init(bp);
1333 BNXT_TF_DBG(ERR, "Failed to create the eem scope table\n");
1337 rc = ulp_mapper_init(bp->ulp_ctx);
1339 BNXT_TF_DBG(ERR, "Failed to initialize ulp mapper\n");
1343 rc = ulp_fc_mgr_init(bp->ulp_ctx);
1345 BNXT_TF_DBG(ERR, "Failed to initialize ulp flow counter mgr\n");
1350 * Enable NAT feature. Set the global configuration register
1351 * Tunnel encap to enable NAT with the reuse of existing inner
1352 * L2 header smac and dmac
1354 rc = bnxt_ulp_global_cfg_update(bp, TF_DIR_RX, TF_TUNNEL_ENCAP,
1355 TF_TUNNEL_ENCAP_NAT,
1356 BNXT_ULP_NAT_OUTER_MOST_FLAGS, 1);
1358 BNXT_TF_DBG(ERR, "Failed to set rx global configuration\n");
1362 rc = bnxt_ulp_global_cfg_update(bp, TF_DIR_TX, TF_TUNNEL_ENCAP,
1363 TF_TUNNEL_ENCAP_NAT,
1364 BNXT_ULP_NAT_OUTER_MOST_FLAGS, 1);
1366 BNXT_TF_DBG(ERR, "Failed to set tx global configuration\n");
1370 if (bnxt_ulp_cntxt_ha_enabled(bp->ulp_ctx)) {
1371 rc = ulp_ha_mgr_init(bp->ulp_ctx);
1373 BNXT_TF_DBG(ERR, "Failed to initialize HA %d\n", rc);
1376 rc = ulp_ha_mgr_open(bp->ulp_ctx);
1378 BNXT_TF_DBG(ERR, "Failed to Process HA Open %d\n", rc);
1382 BNXT_TF_DBG(DEBUG, "ulp ctx has been initialized\n");
1386 bnxt_ulp_deinit(bp, session);
1391 * When a port is initialized by dpdk. This functions sets up
1392 * the port specific details.
1395 bnxt_ulp_port_init(struct bnxt *bp)
1397 struct bnxt_ulp_session_state *session;
1399 enum bnxt_ulp_device_id devid = BNXT_ULP_DEVICE_ID_LAST;
1403 if (!BNXT_TRUFLOW_EN(bp)) {
1405 "Skip ulp init for port: %d, TF is not enabled\n",
1406 bp->eth_dev->data->port_id);
1410 if (!BNXT_PF(bp) && !BNXT_VF_IS_TRUSTED(bp)) {
1412 "Skip ulp init for port: %d, not a TVF or PF\n",
1413 bp->eth_dev->data->port_id);
1418 BNXT_TF_DBG(DEBUG, "ulp ctx already allocated\n");
1422 bp->ulp_ctx = rte_zmalloc("bnxt_ulp_ctx",
1423 sizeof(struct bnxt_ulp_context), 0);
1425 BNXT_TF_DBG(ERR, "Failed to allocate ulp ctx\n");
1430 * Multiple uplink ports can be associated with a single vswitch.
1431 * Make sure only the port that is started first will initialize
1434 session = ulp_session_init(bp, &initialized);
1436 BNXT_TF_DBG(ERR, "Failed to initialize the tf session\n");
1443 * If ULP is already initialized for a specific domain then
1444 * simply assign the ulp context to this rte_eth_dev.
1446 rc = ulp_ctx_attach(bp, session);
1448 BNXT_TF_DBG(ERR, "Failed to attach the ulp context\n");
1453 * Attach to the shared session, must be called after the
1454 * ulp_ctx_attach in order to ensure that ulp data is available
1457 rc = ulp_ctx_shared_session_attach(bp, session);
1460 "Failed attach to shared session (%d)", rc);
1464 rc = bnxt_ulp_init(bp, session);
1466 BNXT_TF_DBG(ERR, "Failed to initialize the ulp init\n");
1471 /* Update bnxt driver flags */
1472 rc = ulp_dparms_dev_port_intf_update(bp, bp->ulp_ctx);
1474 BNXT_TF_DBG(ERR, "Failed to update driver flags\n");
1478 /* update the port database for the given interface */
1479 rc = ulp_port_db_dev_port_intf_update(bp->ulp_ctx, bp->eth_dev);
1481 BNXT_TF_DBG(ERR, "Failed to update port database\n");
1484 /* create the default rules */
1485 rc = bnxt_ulp_create_df_rules(bp);
1487 BNXT_TF_DBG(ERR, "Failed to create default flow\n");
1491 rc = bnxt_ulp_devid_get(bp, &devid);
1493 BNXT_TF_DBG(ERR, "Unable to determine device for ULP port init.\n");
1497 /* set the unicast mode */
1498 if (bnxt_ulp_cntxt_ptr2_ulp_flags_get(bp->ulp_ctx, &ulp_flags)) {
1499 BNXT_TF_DBG(ERR, "Error in getting ULP context flags\n");
1502 if (ulp_flags & BNXT_ULP_APP_UNICAST_ONLY) {
1503 if (bnxt_pmd_set_unicast_rxmask(bp->eth_dev)) {
1504 BNXT_TF_DBG(ERR, "Error in setting unicast rxmode\n");
1512 bnxt_ulp_port_deinit(bp);
1517 * When a port is de-initialized by dpdk. This functions clears up
1518 * the port specific details.
1521 bnxt_ulp_port_deinit(struct bnxt *bp)
1523 struct bnxt_ulp_session_state *session;
1524 struct rte_pci_device *pci_dev;
1525 struct rte_pci_addr *pci_addr;
1527 if (!BNXT_TRUFLOW_EN(bp)) {
1529 "Skip ULP deinit for port:%d, TF is not enabled\n",
1530 bp->eth_dev->data->port_id);
1534 if (!BNXT_PF(bp) && !BNXT_VF_IS_TRUSTED(bp)) {
1536 "Skip ULP deinit port:%d, not a TVF or PF\n",
1537 bp->eth_dev->data->port_id);
1542 BNXT_TF_DBG(DEBUG, "ulp ctx already de-allocated\n");
1546 BNXT_TF_DBG(DEBUG, "BNXT Port:%d ULP port deinit\n",
1547 bp->eth_dev->data->port_id);
1549 /* Free the ulp context in the context entry list */
1550 bnxt_ulp_cntxt_list_del(bp->ulp_ctx);
1552 /* Get the session details */
1553 pci_dev = RTE_DEV_TO_PCI(bp->eth_dev->device);
1554 pci_addr = &pci_dev->addr;
1555 pthread_mutex_lock(&bnxt_ulp_global_mutex);
1556 session = ulp_get_session(bp, pci_addr);
1557 pthread_mutex_unlock(&bnxt_ulp_global_mutex);
1559 /* session not found then just exit */
1561 /* Free the ulp context */
1562 rte_free(bp->ulp_ctx);
1567 /* Check the reference count to deinit or deattach*/
1568 if (bp->ulp_ctx->cfg_data && bp->ulp_ctx->cfg_data->ref_cnt) {
1569 bp->ulp_ctx->cfg_data->ref_cnt--;
1570 if (bp->ulp_ctx->cfg_data->ref_cnt) {
1571 /* free the port details */
1572 /* Free the default flow rule associated to this port */
1573 bnxt_ulp_destroy_df_rules(bp, false);
1574 bnxt_ulp_destroy_vfr_default_rules(bp, false);
1576 /* free flows associated with this port */
1577 bnxt_ulp_flush_port_flows(bp);
1579 /* close the session associated with this port */
1582 /* always detach/close shared after the session. */
1583 ulp_ctx_shared_session_detach(bp);
1585 /* Perform ulp ctx deinit */
1586 bnxt_ulp_deinit(bp, session);
1590 /* clean up the session */
1591 ulp_session_deinit(session);
1593 /* Free the ulp context */
1594 rte_free(bp->ulp_ctx);
1598 /* Below are the access functions to access internal data of ulp context. */
1599 /* Function to set the Mark DB into the context */
1601 bnxt_ulp_cntxt_ptr2_mark_db_set(struct bnxt_ulp_context *ulp_ctx,
1602 struct bnxt_ulp_mark_tbl *mark_tbl)
1604 if (!ulp_ctx || !ulp_ctx->cfg_data) {
1605 BNXT_TF_DBG(ERR, "Invalid ulp context data\n");
1609 ulp_ctx->cfg_data->mark_tbl = mark_tbl;
1614 /* Function to retrieve the Mark DB from the context. */
1615 struct bnxt_ulp_mark_tbl *
1616 bnxt_ulp_cntxt_ptr2_mark_db_get(struct bnxt_ulp_context *ulp_ctx)
1618 if (!ulp_ctx || !ulp_ctx->cfg_data)
1621 return ulp_ctx->cfg_data->mark_tbl;
1625 bnxt_ulp_cntxt_shared_session_enabled(struct bnxt_ulp_context *ulp_ctx)
1627 return ULP_SHARED_SESSION_IS_ENABLED(ulp_ctx->cfg_data->ulp_flags);
1631 bnxt_ulp_cntxt_app_id_set(struct bnxt_ulp_context *ulp_ctx, uint8_t app_id)
1635 ulp_ctx->cfg_data->app_id = app_id;
1640 bnxt_ulp_cntxt_app_id_get(struct bnxt_ulp_context *ulp_ctx, uint8_t *app_id)
1642 /* Default APP id is zero */
1643 if (!ulp_ctx || !app_id)
1645 *app_id = ulp_ctx->cfg_data->app_id;
1649 /* Function to set the device id of the hardware. */
1651 bnxt_ulp_cntxt_dev_id_set(struct bnxt_ulp_context *ulp_ctx,
1654 if (ulp_ctx && ulp_ctx->cfg_data) {
1655 ulp_ctx->cfg_data->dev_id = dev_id;
1662 /* Function to get the device id of the hardware. */
1664 bnxt_ulp_cntxt_dev_id_get(struct bnxt_ulp_context *ulp_ctx,
1667 if (ulp_ctx && ulp_ctx->cfg_data) {
1668 *dev_id = ulp_ctx->cfg_data->dev_id;
1671 *dev_id = BNXT_ULP_DEVICE_ID_LAST;
1672 BNXT_TF_DBG(ERR, "Failed to read dev_id from ulp ctxt\n");
1677 bnxt_ulp_cntxt_mem_type_set(struct bnxt_ulp_context *ulp_ctx,
1678 enum bnxt_ulp_flow_mem_type mem_type)
1680 if (ulp_ctx && ulp_ctx->cfg_data) {
1681 ulp_ctx->cfg_data->mem_type = mem_type;
1684 BNXT_TF_DBG(ERR, "Failed to write mem_type in ulp ctxt\n");
1689 bnxt_ulp_cntxt_mem_type_get(struct bnxt_ulp_context *ulp_ctx,
1690 enum bnxt_ulp_flow_mem_type *mem_type)
1692 if (ulp_ctx && ulp_ctx->cfg_data) {
1693 *mem_type = ulp_ctx->cfg_data->mem_type;
1696 *mem_type = BNXT_ULP_FLOW_MEM_TYPE_LAST;
1697 BNXT_TF_DBG(ERR, "Failed to read mem_type in ulp ctxt\n");
1701 /* Function to get the table scope id of the EEM table. */
1703 bnxt_ulp_cntxt_tbl_scope_id_get(struct bnxt_ulp_context *ulp_ctx,
1704 uint32_t *tbl_scope_id)
1706 if (ulp_ctx && ulp_ctx->cfg_data) {
1707 *tbl_scope_id = ulp_ctx->cfg_data->tbl_scope_id;
1714 /* Function to set the table scope id of the EEM table. */
1716 bnxt_ulp_cntxt_tbl_scope_id_set(struct bnxt_ulp_context *ulp_ctx,
1717 uint32_t tbl_scope_id)
1719 if (ulp_ctx && ulp_ctx->cfg_data) {
1720 ulp_ctx->cfg_data->tbl_scope_id = tbl_scope_id;
1727 /* Function to set the shared tfp session details from the ulp context. */
1729 bnxt_ulp_cntxt_shared_tfp_set(struct bnxt_ulp_context *ulp, struct tf *tfp)
1732 BNXT_TF_DBG(ERR, "Invalid arguments\n");
1737 if (ulp->cfg_data->num_shared_clients > 0)
1738 ulp->cfg_data->num_shared_clients--;
1740 ulp->cfg_data->num_shared_clients++;
1743 ulp->g_shared_tfp = tfp;
1747 /* Function to get the shared tfp session details from the ulp context. */
1749 bnxt_ulp_cntxt_shared_tfp_get(struct bnxt_ulp_context *ulp)
1752 BNXT_TF_DBG(ERR, "Invalid arguments\n");
1755 return ulp->g_shared_tfp;
1758 /* Function to get the number of shared clients attached */
1760 bnxt_ulp_cntxt_num_shared_clients_get(struct bnxt_ulp_context *ulp)
1762 if (ulp == NULL || ulp->cfg_data == NULL) {
1763 BNXT_TF_DBG(ERR, "Invalid arguments\n");
1766 return ulp->cfg_data->num_shared_clients;
1769 /* Function to set the tfp session details from the ulp context. */
1771 bnxt_ulp_cntxt_tfp_set(struct bnxt_ulp_context *ulp, struct tf *tfp)
1774 BNXT_TF_DBG(ERR, "Invalid arguments\n");
1782 /* Function to get the tfp session details from the ulp context. */
1784 bnxt_ulp_cntxt_tfp_get(struct bnxt_ulp_context *ulp,
1785 enum bnxt_ulp_shared_session shared)
1788 BNXT_TF_DBG(ERR, "Invalid arguments\n");
1792 return ulp->g_shared_tfp;
1798 * Get the device table entry based on the device id.
1800 * dev_id [in] The device id of the hardware
1802 * Returns the pointer to the device parameters.
1804 struct bnxt_ulp_device_params *
1805 bnxt_ulp_device_params_get(uint32_t dev_id)
1807 if (dev_id < BNXT_ULP_MAX_NUM_DEVICES)
1808 return &ulp_device_params[dev_id];
1812 /* Function to set the flow database to the ulp context. */
1814 bnxt_ulp_cntxt_ptr2_flow_db_set(struct bnxt_ulp_context *ulp_ctx,
1815 struct bnxt_ulp_flow_db *flow_db)
1817 if (!ulp_ctx || !ulp_ctx->cfg_data)
1820 ulp_ctx->cfg_data->flow_db = flow_db;
1824 /* Function to get the flow database from the ulp context. */
1825 struct bnxt_ulp_flow_db *
1826 bnxt_ulp_cntxt_ptr2_flow_db_get(struct bnxt_ulp_context *ulp_ctx)
1828 if (!ulp_ctx || !ulp_ctx->cfg_data)
1831 return ulp_ctx->cfg_data->flow_db;
1834 /* Function to get the tunnel cache table info from the ulp context. */
1835 struct bnxt_tun_cache_entry *
1836 bnxt_ulp_cntxt_ptr2_tun_tbl_get(struct bnxt_ulp_context *ulp_ctx)
1838 if (!ulp_ctx || !ulp_ctx->cfg_data)
1841 return ulp_ctx->cfg_data->tun_tbl;
1844 /* Function to get the ulp context from eth device. */
1845 struct bnxt_ulp_context *
1846 bnxt_ulp_eth_dev_ptr2_cntxt_get(struct rte_eth_dev *dev)
1848 struct bnxt *bp = (struct bnxt *)dev->data->dev_private;
1850 if (BNXT_ETH_DEV_IS_REPRESENTOR(dev)) {
1851 struct bnxt_representor *vfr = dev->data->dev_private;
1853 bp = vfr->parent_dev->data->dev_private;
1857 BNXT_TF_DBG(ERR, "Bnxt private data is not initialized\n");
1864 bnxt_ulp_cntxt_ptr2_mapper_data_set(struct bnxt_ulp_context *ulp_ctx,
1867 if (!ulp_ctx || !ulp_ctx->cfg_data) {
1868 BNXT_TF_DBG(ERR, "Invalid ulp context data\n");
1872 ulp_ctx->cfg_data->mapper_data = mapper_data;
1877 bnxt_ulp_cntxt_ptr2_mapper_data_get(struct bnxt_ulp_context *ulp_ctx)
1879 if (!ulp_ctx || !ulp_ctx->cfg_data) {
1880 BNXT_TF_DBG(ERR, "Invalid ulp context data\n");
1884 return ulp_ctx->cfg_data->mapper_data;
1887 /* Function to set the port database to the ulp context. */
1889 bnxt_ulp_cntxt_ptr2_port_db_set(struct bnxt_ulp_context *ulp_ctx,
1890 struct bnxt_ulp_port_db *port_db)
1892 if (!ulp_ctx || !ulp_ctx->cfg_data)
1895 ulp_ctx->cfg_data->port_db = port_db;
1899 /* Function to get the port database from the ulp context. */
1900 struct bnxt_ulp_port_db *
1901 bnxt_ulp_cntxt_ptr2_port_db_get(struct bnxt_ulp_context *ulp_ctx)
1903 if (!ulp_ctx || !ulp_ctx->cfg_data)
1906 return ulp_ctx->cfg_data->port_db;
1909 /* Function to set the flow counter info into the context */
1911 bnxt_ulp_cntxt_ptr2_fc_info_set(struct bnxt_ulp_context *ulp_ctx,
1912 struct bnxt_ulp_fc_info *ulp_fc_info)
1914 if (!ulp_ctx || !ulp_ctx->cfg_data) {
1915 BNXT_TF_DBG(ERR, "Invalid ulp context data\n");
1919 ulp_ctx->cfg_data->fc_info = ulp_fc_info;
1924 /* Function to retrieve the flow counter info from the context. */
1925 struct bnxt_ulp_fc_info *
1926 bnxt_ulp_cntxt_ptr2_fc_info_get(struct bnxt_ulp_context *ulp_ctx)
1928 if (!ulp_ctx || !ulp_ctx->cfg_data)
1931 return ulp_ctx->cfg_data->fc_info;
1934 /* Function to get the ulp flags from the ulp context. */
1936 bnxt_ulp_cntxt_ptr2_ulp_flags_get(struct bnxt_ulp_context *ulp_ctx,
1939 if (!ulp_ctx || !ulp_ctx->cfg_data)
1942 *flags = ulp_ctx->cfg_data->ulp_flags;
1946 /* Function to get the ulp vfr info from the ulp context. */
1947 struct bnxt_ulp_vfr_rule_info*
1948 bnxt_ulp_cntxt_ptr2_ulp_vfr_info_get(struct bnxt_ulp_context *ulp_ctx,
1951 if (!ulp_ctx || !ulp_ctx->cfg_data || port_id >= RTE_MAX_ETHPORTS)
1954 return &ulp_ctx->cfg_data->vfr_rule_info[port_id];
1957 /* Function to acquire the flow database lock from the ulp context. */
1959 bnxt_ulp_cntxt_acquire_fdb_lock(struct bnxt_ulp_context *ulp_ctx)
1961 if (!ulp_ctx || !ulp_ctx->cfg_data)
1964 if (pthread_mutex_lock(&ulp_ctx->cfg_data->flow_db_lock)) {
1965 BNXT_TF_DBG(ERR, "unable to acquire fdb lock\n");
1971 /* Function to release the flow database lock from the ulp context. */
1973 bnxt_ulp_cntxt_release_fdb_lock(struct bnxt_ulp_context *ulp_ctx)
1975 if (!ulp_ctx || !ulp_ctx->cfg_data)
1978 pthread_mutex_unlock(&ulp_ctx->cfg_data->flow_db_lock);
1981 /* Function to set the ha info into the context */
1983 bnxt_ulp_cntxt_ptr2_ha_info_set(struct bnxt_ulp_context *ulp_ctx,
1984 struct bnxt_ulp_ha_mgr_info *ulp_ha_info)
1986 if (ulp_ctx == NULL || ulp_ctx->cfg_data == NULL) {
1987 BNXT_TF_DBG(ERR, "Invalid ulp context data\n");
1990 ulp_ctx->cfg_data->ha_info = ulp_ha_info;
1994 /* Function to retrieve the ha info from the context. */
1995 struct bnxt_ulp_ha_mgr_info *
1996 bnxt_ulp_cntxt_ptr2_ha_info_get(struct bnxt_ulp_context *ulp_ctx)
1998 if (ulp_ctx == NULL || ulp_ctx->cfg_data == NULL)
2000 return ulp_ctx->cfg_data->ha_info;
2004 bnxt_ulp_cntxt_ha_enabled(struct bnxt_ulp_context *ulp_ctx)
2006 if (ulp_ctx == NULL || ulp_ctx->cfg_data == NULL)
2008 return !!ULP_HIGH_AVAIL_IS_ENABLED(ulp_ctx->cfg_data->ulp_flags);
2012 bnxt_ulp_cntxt_list_init(void)
2014 /* Create the cntxt spin lock only once*/
2015 if (!bnxt_ulp_ctxt_lock_created)
2016 rte_spinlock_init(&bnxt_ulp_ctxt_lock);
2017 bnxt_ulp_ctxt_lock_created = 1;
2022 bnxt_ulp_cntxt_list_add(struct bnxt_ulp_context *ulp_ctx)
2024 struct ulp_context_list_entry *entry;
2026 entry = rte_zmalloc(NULL, sizeof(struct ulp_context_list_entry), 0);
2027 if (entry == NULL) {
2028 BNXT_TF_DBG(ERR, "unable to allocate memory\n");
2032 rte_spinlock_lock(&bnxt_ulp_ctxt_lock);
2033 entry->ulp_ctx = ulp_ctx;
2034 TAILQ_INSERT_TAIL(&ulp_cntx_list, entry, next);
2035 rte_spinlock_unlock(&bnxt_ulp_ctxt_lock);
2040 bnxt_ulp_cntxt_list_del(struct bnxt_ulp_context *ulp_ctx)
2042 struct ulp_context_list_entry *entry, *temp;
2044 rte_spinlock_lock(&bnxt_ulp_ctxt_lock);
2045 RTE_TAILQ_FOREACH_SAFE(entry, &ulp_cntx_list, next, temp) {
2046 if (entry->ulp_ctx == ulp_ctx) {
2047 TAILQ_REMOVE(&ulp_cntx_list, entry, next);
2052 rte_spinlock_unlock(&bnxt_ulp_ctxt_lock);
2055 struct bnxt_ulp_context *
2056 bnxt_ulp_cntxt_entry_acquire(void *arg)
2058 struct ulp_context_list_entry *entry;
2060 /* take a lock and get the first ulp context available */
2061 if (rte_spinlock_trylock(&bnxt_ulp_ctxt_lock)) {
2062 TAILQ_FOREACH(entry, &ulp_cntx_list, next)
2063 if (entry->ulp_ctx->cfg_data == arg)
2064 return entry->ulp_ctx;
2070 bnxt_ulp_cntxt_entry_release(void)
2072 rte_spinlock_unlock(&bnxt_ulp_ctxt_lock);
2075 /* Function to get the app tunnel details from the ulp context. */
2076 struct bnxt_flow_app_tun_ent *
2077 bnxt_ulp_cntxt_ptr2_app_tun_list_get(struct bnxt_ulp_context *ulp)
2079 if (!ulp || !ulp->cfg_data)
2082 return ulp->cfg_data->app_tun;