1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2019-2020 Broadcom
7 #include <rte_malloc.h>
9 #include <rte_flow_driver.h>
10 #include <rte_tailq.h>
14 #include "bnxt_tf_common.h"
16 #include "tf_ext_flow_handle.h"
18 #include "ulp_template_db_enum.h"
19 #include "ulp_template_struct.h"
20 #include "ulp_mark_mgr.h"
21 #include "ulp_fc_mgr.h"
22 #include "ulp_flow_db.h"
23 #include "ulp_mapper.h"
24 #include "ulp_port_db.h"
26 /* Linked list of all TF sessions. */
27 STAILQ_HEAD(, bnxt_ulp_session_state) bnxt_ulp_session_list =
28 STAILQ_HEAD_INITIALIZER(bnxt_ulp_session_list);
30 /* Mutex to synchronize bnxt_ulp_session_list operations. */
31 static pthread_mutex_t bnxt_ulp_global_mutex = PTHREAD_MUTEX_INITIALIZER;
34 * Allow the deletion of context only for the bnxt device that
35 * created the session.
38 ulp_ctx_deinit_allowed(struct bnxt_ulp_context *ulp_ctx)
40 if (!ulp_ctx || !ulp_ctx->cfg_data)
43 if (!ulp_ctx->cfg_data->ref_cnt) {
44 BNXT_TF_DBG(DEBUG, "ulp ctx shall initiate deinit\n");
52 * Initialize an ULP session.
53 * An ULP session will contain all the resources needed to support rte flow
54 * offloads. A session is initialized as part of rte_eth_device start.
55 * A single vswitch instance can have multiple uplinks which means
56 * rte_eth_device start will be called for each of these devices.
57 * ULP session manager will make sure that a single ULP session is only
58 * initialized once. Apart from this, it also initializes MARK database,
59 * EEM table & flow database. ULP session manager also manages a list of
60 * all opened ULP sessions.
63 ulp_ctx_session_open(struct bnxt *bp,
64 struct bnxt_ulp_session_state *session)
66 struct rte_eth_dev *ethdev = bp->eth_dev;
68 struct tf_open_session_parms params;
69 struct tf_session_resources *resources;
71 memset(¶ms, 0, sizeof(params));
73 rc = rte_eth_dev_get_name_by_port(ethdev->data->port_id,
74 params.ctrl_chan_name);
76 BNXT_TF_DBG(ERR, "Invalid port %d, rc = %d\n",
77 ethdev->data->port_id, rc);
81 params.shadow_copy = true;
82 params.device_type = TF_DEVICE_TYPE_WH;
83 resources = ¶ms.resources;
86 resources->ident_cnt[TF_DIR_RX].cnt[TF_IDENT_TYPE_L2_CTXT_HIGH] = 422;
87 resources->ident_cnt[TF_DIR_RX].cnt[TF_IDENT_TYPE_L2_CTXT_LOW] = 6;
88 resources->ident_cnt[TF_DIR_RX].cnt[TF_IDENT_TYPE_WC_PROF] = 192;
89 resources->ident_cnt[TF_DIR_RX].cnt[TF_IDENT_TYPE_PROF_FUNC] = 64;
90 resources->ident_cnt[TF_DIR_RX].cnt[TF_IDENT_TYPE_EM_PROF] = 192;
93 resources->tbl_cnt[TF_DIR_RX].cnt[TF_TBL_TYPE_FULL_ACT_RECORD] = 8192;
94 resources->tbl_cnt[TF_DIR_RX].cnt[TF_TBL_TYPE_ACT_STATS_64] = 16384;
95 resources->tbl_cnt[TF_DIR_RX].cnt[TF_TBL_TYPE_ACT_MODIFY_IPV4] = 1023;
98 resources->tbl_cnt[TF_DIR_RX].cnt[TF_TBL_TYPE_ACT_ENCAP_8B] = 511;
99 resources->tbl_cnt[TF_DIR_RX].cnt[TF_TBL_TYPE_ACT_ENCAP_16B] = 63;
102 resources->tcam_cnt[TF_DIR_RX].cnt[TF_TCAM_TBL_TYPE_L2_CTXT_TCAM_HIGH] =
104 resources->tcam_cnt[TF_DIR_RX].cnt[TF_TCAM_TBL_TYPE_L2_CTXT_TCAM_LOW] =
106 resources->tcam_cnt[TF_DIR_RX].cnt[TF_TCAM_TBL_TYPE_PROF_TCAM] = 960;
107 resources->tcam_cnt[TF_DIR_RX].cnt[TF_TCAM_TBL_TYPE_WC_TCAM] = 88;
110 resources->em_cnt[TF_DIR_RX].cnt[TF_EM_TBL_TYPE_EM_RECORD] = 13168;
113 resources->em_cnt[TF_DIR_RX].cnt[TF_EM_TBL_TYPE_TBL_SCOPE] = 1;
116 resources->tbl_cnt[TF_DIR_RX].cnt[TF_TBL_TYPE_ACT_SP_SMAC] = 255;
120 resources->ident_cnt[TF_DIR_TX].cnt[TF_IDENT_TYPE_L2_CTXT_HIGH] = 292;
121 resources->ident_cnt[TF_DIR_TX].cnt[TF_IDENT_TYPE_L2_CTXT_LOW] = 148;
122 resources->ident_cnt[TF_DIR_TX].cnt[TF_IDENT_TYPE_WC_PROF] = 192;
123 resources->ident_cnt[TF_DIR_TX].cnt[TF_IDENT_TYPE_PROF_FUNC] = 64;
124 resources->ident_cnt[TF_DIR_TX].cnt[TF_IDENT_TYPE_EM_PROF] = 192;
127 resources->tbl_cnt[TF_DIR_TX].cnt[TF_TBL_TYPE_FULL_ACT_RECORD] = 8192;
128 resources->tbl_cnt[TF_DIR_TX].cnt[TF_TBL_TYPE_ACT_STATS_64] = 16384;
129 resources->tbl_cnt[TF_DIR_TX].cnt[TF_TBL_TYPE_ACT_MODIFY_IPV4] = 1023;
132 resources->tbl_cnt[TF_DIR_TX].cnt[TF_TBL_TYPE_ACT_ENCAP_64B] = 511;
133 resources->tbl_cnt[TF_DIR_TX].cnt[TF_TBL_TYPE_ACT_ENCAP_16B] = 223;
134 resources->tbl_cnt[TF_DIR_TX].cnt[TF_TBL_TYPE_ACT_ENCAP_8B] = 255;
137 resources->tcam_cnt[TF_DIR_TX].cnt[TF_TCAM_TBL_TYPE_L2_CTXT_TCAM_HIGH] =
139 resources->tcam_cnt[TF_DIR_TX].cnt[TF_TCAM_TBL_TYPE_L2_CTXT_TCAM_LOW] =
141 resources->tcam_cnt[TF_DIR_TX].cnt[TF_TCAM_TBL_TYPE_PROF_TCAM] = 960;
142 resources->tcam_cnt[TF_DIR_TX].cnt[TF_TCAM_TBL_TYPE_WC_TCAM] = 928;
145 resources->em_cnt[TF_DIR_TX].cnt[TF_EM_TBL_TYPE_EM_RECORD] = 15232;
148 resources->em_cnt[TF_DIR_TX].cnt[TF_EM_TBL_TYPE_TBL_SCOPE] = 1;
151 resources->tbl_cnt[TF_DIR_TX].cnt[TF_TBL_TYPE_ACT_SP_SMAC_IPV4] = 488;
152 resources->tbl_cnt[TF_DIR_TX].cnt[TF_TBL_TYPE_ACT_SP_SMAC_IPV6] = 511;
154 rc = tf_open_session(&bp->tfp, ¶ms);
156 BNXT_TF_DBG(ERR, "Failed to open TF session - %s, rc = %d\n",
157 params.ctrl_chan_name, rc);
160 if (!session->session_opened) {
161 session->session_opened = 1;
162 session->g_tfp = rte_zmalloc("bnxt_ulp_session_tfp",
163 sizeof(struct tf), 0);
164 session->g_tfp->session = bp->tfp.session;
170 * Close the ULP session.
171 * It takes the ulp context pointer.
174 ulp_ctx_session_close(struct bnxt *bp,
175 struct bnxt_ulp_session_state *session)
177 /* close the session in the hardware */
178 if (session->session_opened)
179 tf_close_session(&bp->tfp);
180 session->session_opened = 0;
181 rte_free(session->g_tfp);
182 session->g_tfp = NULL;
186 bnxt_init_tbl_scope_parms(struct bnxt *bp,
187 struct tf_alloc_tbl_scope_parms *params)
189 struct bnxt_ulp_device_params *dparms;
193 rc = bnxt_ulp_cntxt_dev_id_get(bp->ulp_ctx, &dev_id);
195 /* TBD: For now, just use default. */
198 dparms = bnxt_ulp_device_params_get(dev_id);
201 * Set the flush timer for EEM entries. The value is in 100ms intervals,
204 params->hw_flow_cache_flush_timer = 100;
207 params->rx_max_key_sz_in_bits = BNXT_ULP_DFLT_RX_MAX_KEY;
208 params->rx_max_action_entry_sz_in_bits =
209 BNXT_ULP_DFLT_RX_MAX_ACTN_ENTRY;
210 params->rx_mem_size_in_mb = BNXT_ULP_DFLT_RX_MEM;
211 params->rx_num_flows_in_k = BNXT_ULP_RX_NUM_FLOWS;
212 params->rx_tbl_if_id = BNXT_ULP_RX_TBL_IF_ID;
214 params->tx_max_key_sz_in_bits = BNXT_ULP_DFLT_TX_MAX_KEY;
215 params->tx_max_action_entry_sz_in_bits =
216 BNXT_ULP_DFLT_TX_MAX_ACTN_ENTRY;
217 params->tx_mem_size_in_mb = BNXT_ULP_DFLT_TX_MEM;
218 params->tx_num_flows_in_k = BNXT_ULP_TX_NUM_FLOWS;
219 params->tx_tbl_if_id = BNXT_ULP_TX_TBL_IF_ID;
221 params->rx_max_key_sz_in_bits = BNXT_ULP_DFLT_RX_MAX_KEY;
222 params->rx_max_action_entry_sz_in_bits =
223 BNXT_ULP_DFLT_RX_MAX_ACTN_ENTRY;
224 params->rx_mem_size_in_mb = BNXT_ULP_DFLT_RX_MEM;
225 params->rx_num_flows_in_k = dparms->flow_db_num_entries / 1024;
226 params->rx_tbl_if_id = BNXT_ULP_RX_TBL_IF_ID;
228 params->tx_max_key_sz_in_bits = BNXT_ULP_DFLT_TX_MAX_KEY;
229 params->tx_max_action_entry_sz_in_bits =
230 BNXT_ULP_DFLT_TX_MAX_ACTN_ENTRY;
231 params->tx_mem_size_in_mb = BNXT_ULP_DFLT_TX_MEM;
232 params->tx_num_flows_in_k = dparms->flow_db_num_entries / 1024;
233 params->tx_tbl_if_id = BNXT_ULP_TX_TBL_IF_ID;
237 /* Initialize Extended Exact Match host memory. */
239 ulp_eem_tbl_scope_init(struct bnxt *bp)
241 struct tf_alloc_tbl_scope_parms params = {0};
243 struct bnxt_ulp_device_params *dparms;
246 /* Get the dev specific number of flows that needed to be supported. */
247 if (bnxt_ulp_cntxt_dev_id_get(bp->ulp_ctx, &dev_id)) {
248 BNXT_TF_DBG(ERR, "Invalid device id\n");
252 dparms = bnxt_ulp_device_params_get(dev_id);
254 BNXT_TF_DBG(ERR, "could not fetch the device params\n");
258 if (dparms->flow_mem_type != BNXT_ULP_FLOW_MEM_TYPE_EXT) {
259 BNXT_TF_DBG(INFO, "Table Scope alloc is not required\n");
263 bnxt_init_tbl_scope_parms(bp, ¶ms);
265 rc = tf_alloc_tbl_scope(&bp->tfp, ¶ms);
267 BNXT_TF_DBG(ERR, "Unable to allocate eem table scope rc = %d\n",
272 rc = bnxt_ulp_cntxt_tbl_scope_id_set(bp->ulp_ctx, params.tbl_scope_id);
274 BNXT_TF_DBG(ERR, "Unable to set table scope id\n");
281 /* Free Extended Exact Match host memory */
283 ulp_eem_tbl_scope_deinit(struct bnxt *bp, struct bnxt_ulp_context *ulp_ctx)
285 struct tf_free_tbl_scope_parms params = {0};
288 struct bnxt_ulp_device_params *dparms;
291 if (!ulp_ctx || !ulp_ctx->cfg_data)
294 tfp = bnxt_ulp_cntxt_tfp_get(ulp_ctx);
296 BNXT_TF_DBG(ERR, "Failed to get the truflow pointer\n");
300 /* Get the dev specific number of flows that needed to be supported. */
301 if (bnxt_ulp_cntxt_dev_id_get(bp->ulp_ctx, &dev_id)) {
302 BNXT_TF_DBG(ERR, "Invalid device id\n");
306 dparms = bnxt_ulp_device_params_get(dev_id);
308 BNXT_TF_DBG(ERR, "could not fetch the device params\n");
312 if (dparms->flow_mem_type != BNXT_ULP_FLOW_MEM_TYPE_EXT) {
313 BNXT_TF_DBG(INFO, "Table Scope free is not required\n");
317 rc = bnxt_ulp_cntxt_tbl_scope_id_get(ulp_ctx, ¶ms.tbl_scope_id);
319 BNXT_TF_DBG(ERR, "Failed to get the table scope id\n");
323 rc = tf_free_tbl_scope(tfp, ¶ms);
325 BNXT_TF_DBG(ERR, "Unable to free table scope\n");
331 /* The function to free and deinit the ulp context data. */
333 ulp_ctx_deinit(struct bnxt *bp,
334 struct bnxt_ulp_session_state *session)
336 /* close the tf session */
337 ulp_ctx_session_close(bp, session);
339 /* Free the contents */
340 if (session->cfg_data) {
341 rte_free(session->cfg_data);
342 bp->ulp_ctx->cfg_data = NULL;
343 session->cfg_data = NULL;
348 /* The function to allocate and initialize the ulp context data. */
350 ulp_ctx_init(struct bnxt *bp,
351 struct bnxt_ulp_session_state *session)
353 struct bnxt_ulp_data *ulp_data;
356 /* Allocate memory to hold ulp context data. */
357 ulp_data = rte_zmalloc("bnxt_ulp_data",
358 sizeof(struct bnxt_ulp_data), 0);
360 BNXT_TF_DBG(ERR, "Failed to allocate memory for ulp data\n");
364 /* Increment the ulp context data reference count usage. */
365 bp->ulp_ctx->cfg_data = ulp_data;
366 session->cfg_data = ulp_data;
368 ulp_data->ulp_flags |= BNXT_ULP_VF_REP_ENABLED;
370 /* Open the ulp session. */
371 rc = ulp_ctx_session_open(bp, session);
373 session->session_opened = 1;
374 (void)ulp_ctx_deinit(bp, session);
378 bnxt_ulp_cntxt_tfp_set(bp->ulp_ctx, &bp->tfp);
382 /* The function to initialize ulp dparms with devargs */
384 ulp_dparms_init(struct bnxt *bp,
385 struct bnxt_ulp_context *ulp_ctx)
387 struct bnxt_ulp_device_params *dparms;
390 if (!bp->max_num_kflows)
393 if (bnxt_ulp_cntxt_dev_id_get(ulp_ctx, &dev_id)) {
394 BNXT_TF_DBG(DEBUG, "Failed to get device id\n");
398 dparms = bnxt_ulp_device_params_get(dev_id);
400 BNXT_TF_DBG(DEBUG, "Failed to get device parms\n");
404 /* num_flows = max_num_kflows * 1024 */
405 dparms->flow_db_num_entries = bp->max_num_kflows * 1024;
406 /* GFID = 2 * num_flows */
407 dparms->mark_db_gfid_entries = dparms->flow_db_num_entries * 2;
408 BNXT_TF_DBG(DEBUG, "Set the number of flows = %"PRIu64"\n",
409 dparms->flow_db_num_entries);
414 /* The function to initialize bp flags with truflow features */
416 ulp_dparms_dev_port_intf_update(struct bnxt *bp,
417 struct bnxt_ulp_context *ulp_ctx)
419 struct bnxt_ulp_device_params *dparms;
422 if (bnxt_ulp_cntxt_dev_id_get(ulp_ctx, &dev_id)) {
423 BNXT_TF_DBG(DEBUG, "Failed to get device id\n");
427 dparms = bnxt_ulp_device_params_get(dev_id);
429 BNXT_TF_DBG(DEBUG, "Failed to get device parms\n");
433 /* Update the bp flag with gfid flag */
434 if (dparms->flow_mem_type == BNXT_ULP_FLOW_MEM_TYPE_EXT)
435 bp->flags |= BNXT_FLAG_GFID_ENABLE;
441 ulp_ctx_attach(struct bnxt *bp,
442 struct bnxt_ulp_session_state *session)
446 /* Increment the ulp context data reference count usage. */
447 bp->ulp_ctx->cfg_data = session->cfg_data;
448 bp->ulp_ctx->cfg_data->ref_cnt++;
450 /* update the session details in bnxt tfp */
451 bp->tfp.session = session->g_tfp->session;
453 /* Create a TF Client */
454 rc = ulp_ctx_session_open(bp, session);
456 PMD_DRV_LOG(ERR, "Failed to open ctxt session, rc:%d\n", rc);
457 bp->tfp.session = NULL;
461 bnxt_ulp_cntxt_tfp_set(bp->ulp_ctx, &bp->tfp);
466 ulp_ctx_detach(struct bnxt *bp)
468 if (bp->tfp.session) {
469 tf_close_session(&bp->tfp);
470 bp->tfp.session = NULL;
475 * Initialize the state of an ULP session.
476 * If the state of an ULP session is not initialized, set it's state to
477 * initialized. If the state is already initialized, do nothing.
480 ulp_context_initialized(struct bnxt_ulp_session_state *session, bool *init)
482 pthread_mutex_lock(&session->bnxt_ulp_mutex);
484 if (!session->bnxt_ulp_init) {
485 session->bnxt_ulp_init = true;
491 pthread_mutex_unlock(&session->bnxt_ulp_mutex);
495 * Check if an ULP session is already allocated for a specific PCI
496 * domain & bus. If it is already allocated simply return the session
497 * pointer, otherwise allocate a new session.
499 static struct bnxt_ulp_session_state *
500 ulp_get_session(struct rte_pci_addr *pci_addr)
502 struct bnxt_ulp_session_state *session;
504 STAILQ_FOREACH(session, &bnxt_ulp_session_list, next) {
505 if (session->pci_info.domain == pci_addr->domain &&
506 session->pci_info.bus == pci_addr->bus) {
514 * Allocate and Initialize an ULP session and set it's state to INITIALIZED.
515 * If it's already initialized simply return the already existing session.
517 static struct bnxt_ulp_session_state *
518 ulp_session_init(struct bnxt *bp,
521 struct rte_pci_device *pci_dev;
522 struct rte_pci_addr *pci_addr;
523 struct bnxt_ulp_session_state *session;
529 pci_dev = RTE_DEV_TO_PCI(bp->eth_dev->device);
530 pci_addr = &pci_dev->addr;
532 pthread_mutex_lock(&bnxt_ulp_global_mutex);
534 session = ulp_get_session(pci_addr);
536 /* Not Found the session Allocate a new one */
537 session = rte_zmalloc("bnxt_ulp_session",
538 sizeof(struct bnxt_ulp_session_state),
542 "Allocation failed for bnxt_ulp_session\n");
543 pthread_mutex_unlock(&bnxt_ulp_global_mutex);
547 /* Add it to the queue */
548 session->pci_info.domain = pci_addr->domain;
549 session->pci_info.bus = pci_addr->bus;
550 rc = pthread_mutex_init(&session->bnxt_ulp_mutex, NULL);
552 BNXT_TF_DBG(ERR, "mutex create failed\n");
553 pthread_mutex_unlock(&bnxt_ulp_global_mutex);
556 STAILQ_INSERT_TAIL(&bnxt_ulp_session_list,
560 ulp_context_initialized(session, init);
561 pthread_mutex_unlock(&bnxt_ulp_global_mutex);
566 * When a device is closed, remove it's associated session from the global
570 ulp_session_deinit(struct bnxt_ulp_session_state *session)
575 if (!session->cfg_data) {
576 pthread_mutex_lock(&bnxt_ulp_global_mutex);
577 STAILQ_REMOVE(&bnxt_ulp_session_list, session,
578 bnxt_ulp_session_state, next);
579 pthread_mutex_destroy(&session->bnxt_ulp_mutex);
581 pthread_mutex_unlock(&bnxt_ulp_global_mutex);
586 * Internal api to enable NAT feature.
587 * Set set_flag to 1 to set the value or zero to reset the value.
588 * returns 0 on success.
591 bnxt_ulp_global_cfg_update(struct bnxt *bp,
593 enum tf_global_config_type type,
598 uint32_t global_cfg = 0;
600 struct tf_global_cfg_parms parms = { 0 };
602 /* Initialize the params */
605 parms.offset = offset,
606 parms.config = (uint8_t *)&global_cfg,
607 parms.config_sz_in_bytes = sizeof(global_cfg);
609 rc = tf_get_global_cfg(&bp->tfp, &parms);
611 BNXT_TF_DBG(ERR, "Failed to get global cfg 0x%x rc:%d\n",
619 global_cfg &= ~value;
621 /* SET the register RE_CFA_REG_ACT_TECT */
622 rc = tf_set_global_cfg(&bp->tfp, &parms);
624 BNXT_TF_DBG(ERR, "Failed to set global cfg 0x%x rc:%d\n",
631 /* Internal function to delete all the flows belonging to the given port */
633 bnxt_ulp_flush_port_flows(struct bnxt *bp)
637 /* it is assumed that port is either TVF or PF */
638 if (ulp_port_db_port_func_id_get(bp->ulp_ctx,
639 bp->eth_dev->data->port_id,
641 BNXT_TF_DBG(ERR, "Invalid argument\n");
644 (void)ulp_flow_db_function_flow_flush(bp->ulp_ctx, func_id);
647 /* Internal function to delete the VFR default flows */
649 bnxt_ulp_destroy_vfr_default_rules(struct bnxt *bp, bool global)
651 struct bnxt_ulp_vfr_rule_info *info;
653 struct rte_eth_dev *vfr_eth_dev;
654 struct bnxt_representor *vfr_bp;
656 if (!BNXT_TRUFLOW_EN(bp) || BNXT_ETH_DEV_IS_REPRESENTOR(bp->eth_dev))
659 if (!bp->ulp_ctx || !bp->ulp_ctx->cfg_data)
662 /* Delete default rules for all ports */
663 for (port_id = 0; port_id < RTE_MAX_ETHPORTS; port_id++) {
664 info = &bp->ulp_ctx->cfg_data->vfr_rule_info[port_id];
668 if (!global && info->parent_port_id !=
669 bp->eth_dev->data->port_id)
672 /* Destroy the flows */
673 ulp_default_flow_destroy(bp->eth_dev, info->rep2vf_flow_id);
674 ulp_default_flow_destroy(bp->eth_dev, info->vf2rep_flow_id);
675 /* Clean up the tx action pointer */
676 vfr_eth_dev = &rte_eth_devices[port_id];
678 vfr_bp = vfr_eth_dev->data->dev_private;
679 vfr_bp->vfr_tx_cfa_action = 0;
681 memset(info, 0, sizeof(struct bnxt_ulp_vfr_rule_info));
686 * When a port is deinit'ed by dpdk. This function is called
687 * and this function clears the ULP context and rest of the
688 * infrastructure associated with it.
691 bnxt_ulp_deinit(struct bnxt *bp,
692 struct bnxt_ulp_session_state *session)
694 if (!bp->ulp_ctx || !bp->ulp_ctx->cfg_data)
697 /* clean up default flows */
698 bnxt_ulp_destroy_df_rules(bp, true);
700 /* clean up default VFR flows */
701 bnxt_ulp_destroy_vfr_default_rules(bp, true);
703 /* clean up regular flows */
704 ulp_flow_db_flush_flows(bp->ulp_ctx, BNXT_ULP_REGULAR_FLOW_TABLE);
706 /* cleanup the eem table scope */
707 ulp_eem_tbl_scope_deinit(bp, bp->ulp_ctx);
709 /* cleanup the flow database */
710 ulp_flow_db_deinit(bp->ulp_ctx);
712 /* Delete the Mark database */
713 ulp_mark_db_deinit(bp->ulp_ctx);
715 /* cleanup the ulp mapper */
716 ulp_mapper_deinit(bp->ulp_ctx);
718 /* Delete the Flow Counter Manager */
719 ulp_fc_mgr_deinit(bp->ulp_ctx);
721 /* Delete the Port database */
722 ulp_port_db_deinit(bp->ulp_ctx);
724 /* Disable NAT feature */
725 (void)bnxt_ulp_global_cfg_update(bp, TF_DIR_RX, TF_TUNNEL_ENCAP,
727 BNXT_ULP_NAT_OUTER_MOST_FLAGS, 0);
729 (void)bnxt_ulp_global_cfg_update(bp, TF_DIR_TX, TF_TUNNEL_ENCAP,
731 BNXT_ULP_NAT_OUTER_MOST_FLAGS, 0);
733 /* free the flow db lock */
734 pthread_mutex_destroy(&bp->ulp_ctx->cfg_data->flow_db_lock);
736 /* Delete the ulp context and tf session and free the ulp context */
737 ulp_ctx_deinit(bp, session);
738 BNXT_TF_DBG(DEBUG, "ulp ctx has been deinitialized\n");
742 * When a port is initialized by dpdk. This functions is called
743 * and this function initializes the ULP context and rest of the
744 * infrastructure associated with it.
747 bnxt_ulp_init(struct bnxt *bp,
748 struct bnxt_ulp_session_state *session)
752 /* Allocate and Initialize the ulp context. */
753 rc = ulp_ctx_init(bp, session);
755 BNXT_TF_DBG(ERR, "Failed to create the ulp context\n");
759 rc = pthread_mutex_init(&bp->ulp_ctx->cfg_data->flow_db_lock, NULL);
761 BNXT_TF_DBG(ERR, "Unable to initialize flow db lock\n");
765 /* Initialize ulp dparms with values devargs passed */
766 rc = ulp_dparms_init(bp, bp->ulp_ctx);
768 BNXT_TF_DBG(ERR, "Failed to initialize the dparms\n");
772 /* create the port database */
773 rc = ulp_port_db_init(bp->ulp_ctx, bp->port_cnt);
775 BNXT_TF_DBG(ERR, "Failed to create the port database\n");
779 /* Create the Mark database. */
780 rc = ulp_mark_db_init(bp->ulp_ctx);
782 BNXT_TF_DBG(ERR, "Failed to create the mark database\n");
786 /* Create the flow database. */
787 rc = ulp_flow_db_init(bp->ulp_ctx);
789 BNXT_TF_DBG(ERR, "Failed to create the flow database\n");
793 /* Create the eem table scope. */
794 rc = ulp_eem_tbl_scope_init(bp);
796 BNXT_TF_DBG(ERR, "Failed to create the eem scope table\n");
800 rc = ulp_mapper_init(bp->ulp_ctx);
802 BNXT_TF_DBG(ERR, "Failed to initialize ulp mapper\n");
806 rc = ulp_fc_mgr_init(bp->ulp_ctx);
808 BNXT_TF_DBG(ERR, "Failed to initialize ulp flow counter mgr\n");
813 * Enable NAT feature. Set the global configuration register
814 * Tunnel encap to enable NAT with the reuse of existing inner
815 * L2 header smac and dmac
817 rc = bnxt_ulp_global_cfg_update(bp, TF_DIR_RX, TF_TUNNEL_ENCAP,
819 BNXT_ULP_NAT_OUTER_MOST_FLAGS, 1);
821 BNXT_TF_DBG(ERR, "Failed to set rx global configuration\n");
825 rc = bnxt_ulp_global_cfg_update(bp, TF_DIR_TX, TF_TUNNEL_ENCAP,
827 BNXT_ULP_NAT_OUTER_MOST_FLAGS, 1);
829 BNXT_TF_DBG(ERR, "Failed to set tx global configuration\n");
832 BNXT_TF_DBG(DEBUG, "ulp ctx has been initialized\n");
836 bnxt_ulp_deinit(bp, session);
841 * When a port is initialized by dpdk. This functions sets up
842 * the port specific details.
845 bnxt_ulp_port_init(struct bnxt *bp)
847 struct bnxt_ulp_session_state *session;
851 if (!bp || !BNXT_TRUFLOW_EN(bp))
854 if (!BNXT_PF(bp) && !BNXT_VF_IS_TRUSTED(bp)) {
856 "Skip ulp init for port: %d, not a TVF or PF\n",
857 bp->eth_dev->data->port_id);
862 BNXT_TF_DBG(DEBUG, "ulp ctx already allocated\n");
866 bp->ulp_ctx = rte_zmalloc("bnxt_ulp_ctx",
867 sizeof(struct bnxt_ulp_context), 0);
869 BNXT_TF_DBG(ERR, "Failed to allocate ulp ctx\n");
874 * Multiple uplink ports can be associated with a single vswitch.
875 * Make sure only the port that is started first will initialize
878 session = ulp_session_init(bp, &initialized);
880 BNXT_TF_DBG(ERR, "Failed to initialize the tf session\n");
887 * If ULP is already initialized for a specific domain then
888 * simply assign the ulp context to this rte_eth_dev.
890 rc = ulp_ctx_attach(bp, session);
892 BNXT_TF_DBG(ERR, "Failed to attach the ulp context\n");
896 rc = bnxt_ulp_init(bp, session);
898 BNXT_TF_DBG(ERR, "Failed to initialize the ulp init\n");
903 /* Update bnxt driver flags */
904 rc = ulp_dparms_dev_port_intf_update(bp, bp->ulp_ctx);
906 BNXT_TF_DBG(ERR, "Failed to update driver flags\n");
910 /* update the port database for the given interface */
911 rc = ulp_port_db_dev_port_intf_update(bp->ulp_ctx, bp->eth_dev);
913 BNXT_TF_DBG(ERR, "Failed to update port database\n");
916 /* create the default rules */
917 bnxt_ulp_create_df_rules(bp);
918 BNXT_TF_DBG(DEBUG, "BNXT Port:%d ULP port init\n",
919 bp->eth_dev->data->port_id);
923 bnxt_ulp_port_deinit(bp);
928 * When a port is de-initialized by dpdk. This functions clears up
929 * the port specific details.
932 bnxt_ulp_port_deinit(struct bnxt *bp)
934 struct bnxt_ulp_session_state *session;
935 struct rte_pci_device *pci_dev;
936 struct rte_pci_addr *pci_addr;
938 if (!BNXT_TRUFLOW_EN(bp))
941 if (!BNXT_PF(bp) && !BNXT_VF_IS_TRUSTED(bp)) {
943 "Skip ULP deinit port:%d, not a TVF or PF\n",
944 bp->eth_dev->data->port_id);
949 BNXT_TF_DBG(DEBUG, "ulp ctx already de-allocated\n");
953 BNXT_TF_DBG(DEBUG, "BNXT Port:%d ULP port deinit\n",
954 bp->eth_dev->data->port_id);
956 /* Get the session details */
957 pci_dev = RTE_DEV_TO_PCI(bp->eth_dev->device);
958 pci_addr = &pci_dev->addr;
959 pthread_mutex_lock(&bnxt_ulp_global_mutex);
960 session = ulp_get_session(pci_addr);
961 pthread_mutex_unlock(&bnxt_ulp_global_mutex);
963 /* session not found then just exit */
965 /* Free the ulp context */
966 rte_free(bp->ulp_ctx);
971 /* Check the reference count to deinit or deattach*/
972 if (bp->ulp_ctx->cfg_data && bp->ulp_ctx->cfg_data->ref_cnt) {
973 bp->ulp_ctx->cfg_data->ref_cnt--;
974 if (bp->ulp_ctx->cfg_data->ref_cnt) {
975 /* free the port details */
976 /* Free the default flow rule associated to this port */
977 bnxt_ulp_destroy_df_rules(bp, false);
978 bnxt_ulp_destroy_vfr_default_rules(bp, false);
980 /* free flows associated with this port */
981 bnxt_ulp_flush_port_flows(bp);
983 /* close the session associated with this port */
986 /* Perform ulp ctx deinit */
987 bnxt_ulp_deinit(bp, session);
991 /* clean up the session */
992 ulp_session_deinit(session);
994 /* Free the ulp context */
995 rte_free(bp->ulp_ctx);
999 /* Below are the access functions to access internal data of ulp context. */
1000 /* Function to set the Mark DB into the context */
1002 bnxt_ulp_cntxt_ptr2_mark_db_set(struct bnxt_ulp_context *ulp_ctx,
1003 struct bnxt_ulp_mark_tbl *mark_tbl)
1005 if (!ulp_ctx || !ulp_ctx->cfg_data) {
1006 BNXT_TF_DBG(ERR, "Invalid ulp context data\n");
1010 ulp_ctx->cfg_data->mark_tbl = mark_tbl;
1015 /* Function to retrieve the Mark DB from the context. */
1016 struct bnxt_ulp_mark_tbl *
1017 bnxt_ulp_cntxt_ptr2_mark_db_get(struct bnxt_ulp_context *ulp_ctx)
1019 if (!ulp_ctx || !ulp_ctx->cfg_data)
1022 return ulp_ctx->cfg_data->mark_tbl;
1025 /* Function to set the device id of the hardware. */
1027 bnxt_ulp_cntxt_dev_id_set(struct bnxt_ulp_context *ulp_ctx,
1030 if (ulp_ctx && ulp_ctx->cfg_data) {
1031 ulp_ctx->cfg_data->dev_id = dev_id;
1038 /* Function to get the device id of the hardware. */
1040 bnxt_ulp_cntxt_dev_id_get(struct bnxt_ulp_context *ulp_ctx,
1043 if (ulp_ctx && ulp_ctx->cfg_data) {
1044 *dev_id = ulp_ctx->cfg_data->dev_id;
1051 /* Function to get the table scope id of the EEM table. */
1053 bnxt_ulp_cntxt_tbl_scope_id_get(struct bnxt_ulp_context *ulp_ctx,
1054 uint32_t *tbl_scope_id)
1056 if (ulp_ctx && ulp_ctx->cfg_data) {
1057 *tbl_scope_id = ulp_ctx->cfg_data->tbl_scope_id;
1064 /* Function to set the table scope id of the EEM table. */
1066 bnxt_ulp_cntxt_tbl_scope_id_set(struct bnxt_ulp_context *ulp_ctx,
1067 uint32_t tbl_scope_id)
1069 if (ulp_ctx && ulp_ctx->cfg_data) {
1070 ulp_ctx->cfg_data->tbl_scope_id = tbl_scope_id;
1077 /* Function to set the tfp session details from the ulp context. */
1079 bnxt_ulp_cntxt_tfp_set(struct bnxt_ulp_context *ulp, struct tf *tfp)
1082 BNXT_TF_DBG(ERR, "Invalid arguments\n");
1090 /* Function to get the tfp session details from the ulp context. */
1092 bnxt_ulp_cntxt_tfp_get(struct bnxt_ulp_context *ulp)
1095 BNXT_TF_DBG(ERR, "Invalid arguments\n");
1102 * Get the device table entry based on the device id.
1104 * dev_id [in] The device id of the hardware
1106 * Returns the pointer to the device parameters.
1108 struct bnxt_ulp_device_params *
1109 bnxt_ulp_device_params_get(uint32_t dev_id)
1111 if (dev_id < BNXT_ULP_MAX_NUM_DEVICES)
1112 return &ulp_device_params[dev_id];
1116 /* Function to set the flow database to the ulp context. */
1118 bnxt_ulp_cntxt_ptr2_flow_db_set(struct bnxt_ulp_context *ulp_ctx,
1119 struct bnxt_ulp_flow_db *flow_db)
1121 if (!ulp_ctx || !ulp_ctx->cfg_data)
1124 ulp_ctx->cfg_data->flow_db = flow_db;
1128 /* Function to get the flow database from the ulp context. */
1129 struct bnxt_ulp_flow_db *
1130 bnxt_ulp_cntxt_ptr2_flow_db_get(struct bnxt_ulp_context *ulp_ctx)
1132 if (!ulp_ctx || !ulp_ctx->cfg_data)
1135 return ulp_ctx->cfg_data->flow_db;
1138 /* Function to get the ulp context from eth device. */
1139 struct bnxt_ulp_context *
1140 bnxt_ulp_eth_dev_ptr2_cntxt_get(struct rte_eth_dev *dev)
1142 struct bnxt *bp = (struct bnxt *)dev->data->dev_private;
1144 if (BNXT_ETH_DEV_IS_REPRESENTOR(dev)) {
1145 struct bnxt_representor *vfr = dev->data->dev_private;
1147 bp = vfr->parent_dev->data->dev_private;
1151 BNXT_TF_DBG(ERR, "Bnxt private data is not initialized\n");
1158 bnxt_ulp_cntxt_ptr2_mapper_data_set(struct bnxt_ulp_context *ulp_ctx,
1161 if (!ulp_ctx || !ulp_ctx->cfg_data) {
1162 BNXT_TF_DBG(ERR, "Invalid ulp context data\n");
1166 ulp_ctx->cfg_data->mapper_data = mapper_data;
1171 bnxt_ulp_cntxt_ptr2_mapper_data_get(struct bnxt_ulp_context *ulp_ctx)
1173 if (!ulp_ctx || !ulp_ctx->cfg_data) {
1174 BNXT_TF_DBG(ERR, "Invalid ulp context data\n");
1178 return ulp_ctx->cfg_data->mapper_data;
1181 /* Function to set the port database to the ulp context. */
1183 bnxt_ulp_cntxt_ptr2_port_db_set(struct bnxt_ulp_context *ulp_ctx,
1184 struct bnxt_ulp_port_db *port_db)
1186 if (!ulp_ctx || !ulp_ctx->cfg_data)
1189 ulp_ctx->cfg_data->port_db = port_db;
1193 /* Function to get the port database from the ulp context. */
1194 struct bnxt_ulp_port_db *
1195 bnxt_ulp_cntxt_ptr2_port_db_get(struct bnxt_ulp_context *ulp_ctx)
1197 if (!ulp_ctx || !ulp_ctx->cfg_data)
1200 return ulp_ctx->cfg_data->port_db;
1203 /* Function to set the flow counter info into the context */
1205 bnxt_ulp_cntxt_ptr2_fc_info_set(struct bnxt_ulp_context *ulp_ctx,
1206 struct bnxt_ulp_fc_info *ulp_fc_info)
1208 if (!ulp_ctx || !ulp_ctx->cfg_data) {
1209 BNXT_TF_DBG(ERR, "Invalid ulp context data\n");
1213 ulp_ctx->cfg_data->fc_info = ulp_fc_info;
1218 /* Function to retrieve the flow counter info from the context. */
1219 struct bnxt_ulp_fc_info *
1220 bnxt_ulp_cntxt_ptr2_fc_info_get(struct bnxt_ulp_context *ulp_ctx)
1222 if (!ulp_ctx || !ulp_ctx->cfg_data)
1225 return ulp_ctx->cfg_data->fc_info;
1228 /* Function to get the ulp flags from the ulp context. */
1230 bnxt_ulp_cntxt_ptr2_ulp_flags_get(struct bnxt_ulp_context *ulp_ctx,
1233 if (!ulp_ctx || !ulp_ctx->cfg_data)
1236 *flags = ulp_ctx->cfg_data->ulp_flags;
1240 /* Function to get the ulp vfr info from the ulp context. */
1241 struct bnxt_ulp_vfr_rule_info*
1242 bnxt_ulp_cntxt_ptr2_ulp_vfr_info_get(struct bnxt_ulp_context *ulp_ctx,
1245 if (!ulp_ctx || !ulp_ctx->cfg_data || port_id >= RTE_MAX_ETHPORTS)
1248 return &ulp_ctx->cfg_data->vfr_rule_info[port_id];
1251 /* Function to acquire the flow database lock from the ulp context. */
1253 bnxt_ulp_cntxt_acquire_fdb_lock(struct bnxt_ulp_context *ulp_ctx)
1255 if (!ulp_ctx || !ulp_ctx->cfg_data)
1258 if (pthread_mutex_lock(&ulp_ctx->cfg_data->flow_db_lock)) {
1259 BNXT_TF_DBG(ERR, "unable to acquire fdb lock\n");
1265 /* Function to release the flow database lock from the ulp context. */
1267 bnxt_ulp_cntxt_release_fdb_lock(struct bnxt_ulp_context *ulp_ctx)
1269 if (!ulp_ctx || !ulp_ctx->cfg_data)
1272 pthread_mutex_unlock(&ulp_ctx->cfg_data->flow_db_lock);