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(void *ptr)
40 struct bnxt *bp = (struct bnxt *)ptr;
42 if (!bp || !bp->ulp_ctx || !bp->ulp_ctx->cfg_data)
45 if (!bp->ulp_ctx->cfg_data->ref_cnt) {
46 BNXT_TF_DBG(DEBUG, "ulp ctx shall initiate deinit\n");
54 * Initialize an ULP session.
55 * An ULP session will contain all the resources needed to support rte flow
56 * offloads. A session is initialized as part of rte_eth_device start.
57 * A single vswitch instance can have multiple uplinks which means
58 * rte_eth_device start will be called for each of these devices.
59 * ULP session manager will make sure that a single ULP session is only
60 * initialized once. Apart from this, it also initializes MARK database,
61 * EEM table & flow database. ULP session manager also manages a list of
62 * all opened ULP sessions.
65 ulp_ctx_session_open(struct bnxt *bp,
66 struct bnxt_ulp_session_state *session)
68 struct rte_eth_dev *ethdev = bp->eth_dev;
70 struct tf_open_session_parms params;
71 struct tf_session_resources *resources;
73 memset(¶ms, 0, sizeof(params));
75 rc = rte_eth_dev_get_name_by_port(ethdev->data->port_id,
76 params.ctrl_chan_name);
78 BNXT_TF_DBG(ERR, "Invalid port %d, rc = %d\n",
79 ethdev->data->port_id, rc);
83 params.shadow_copy = true;
84 params.device_type = TF_DEVICE_TYPE_WH;
85 resources = ¶ms.resources;
88 resources->ident_cnt[TF_DIR_RX].cnt[TF_IDENT_TYPE_L2_CTXT_HIGH] = 422;
89 resources->ident_cnt[TF_DIR_RX].cnt[TF_IDENT_TYPE_L2_CTXT_LOW] = 6;
90 resources->ident_cnt[TF_DIR_RX].cnt[TF_IDENT_TYPE_WC_PROF] = 8;
91 resources->ident_cnt[TF_DIR_RX].cnt[TF_IDENT_TYPE_PROF_FUNC] = 8;
92 resources->ident_cnt[TF_DIR_RX].cnt[TF_IDENT_TYPE_EM_PROF] = 8;
95 resources->tbl_cnt[TF_DIR_RX].cnt[TF_TBL_TYPE_FULL_ACT_RECORD] = 8192;
96 resources->tbl_cnt[TF_DIR_RX].cnt[TF_TBL_TYPE_ACT_STATS_64] = 8192;
97 resources->tbl_cnt[TF_DIR_RX].cnt[TF_TBL_TYPE_ACT_MODIFY_IPV4] = 1023;
100 resources->tbl_cnt[TF_DIR_RX].cnt[TF_TBL_TYPE_ACT_ENCAP_8B] = 16;
101 resources->tbl_cnt[TF_DIR_RX].cnt[TF_TBL_TYPE_ACT_ENCAP_16B] = 63;
104 resources->tcam_cnt[TF_DIR_RX].cnt[TF_TCAM_TBL_TYPE_L2_CTXT_TCAM_HIGH] =
106 resources->tcam_cnt[TF_DIR_RX].cnt[TF_TCAM_TBL_TYPE_L2_CTXT_TCAM_LOW] =
108 resources->tcam_cnt[TF_DIR_RX].cnt[TF_TCAM_TBL_TYPE_PROF_TCAM] = 8;
109 resources->tcam_cnt[TF_DIR_RX].cnt[TF_TCAM_TBL_TYPE_WC_TCAM] = 88;
112 resources->em_cnt[TF_DIR_RX].cnt[TF_EM_TBL_TYPE_EM_RECORD] = 13176;
115 resources->em_cnt[TF_DIR_RX].cnt[TF_EM_TBL_TYPE_TBL_SCOPE] = 1;
119 resources->ident_cnt[TF_DIR_TX].cnt[TF_IDENT_TYPE_L2_CTXT_HIGH] = 292;
120 resources->ident_cnt[TF_DIR_TX].cnt[TF_IDENT_TYPE_L2_CTXT_LOW] = 144;
121 resources->ident_cnt[TF_DIR_TX].cnt[TF_IDENT_TYPE_WC_PROF] = 8;
122 resources->ident_cnt[TF_DIR_TX].cnt[TF_IDENT_TYPE_PROF_FUNC] = 8;
123 resources->ident_cnt[TF_DIR_TX].cnt[TF_IDENT_TYPE_EM_PROF] = 8;
126 resources->tbl_cnt[TF_DIR_TX].cnt[TF_TBL_TYPE_FULL_ACT_RECORD] = 8192;
127 resources->tbl_cnt[TF_DIR_TX].cnt[TF_TBL_TYPE_ACT_STATS_64] = 8192;
128 resources->tbl_cnt[TF_DIR_TX].cnt[TF_TBL_TYPE_ACT_MODIFY_IPV4] = 1023;
131 resources->tbl_cnt[TF_DIR_TX].cnt[TF_TBL_TYPE_ACT_ENCAP_64B] = 511;
132 resources->tbl_cnt[TF_DIR_TX].cnt[TF_TBL_TYPE_ACT_ENCAP_16B] = 200;
135 resources->tcam_cnt[TF_DIR_TX].cnt[TF_TCAM_TBL_TYPE_L2_CTXT_TCAM_HIGH] =
137 resources->tcam_cnt[TF_DIR_TX].cnt[TF_TCAM_TBL_TYPE_L2_CTXT_TCAM_LOW] =
139 resources->tcam_cnt[TF_DIR_TX].cnt[TF_TCAM_TBL_TYPE_PROF_TCAM] = 8;
140 resources->tcam_cnt[TF_DIR_TX].cnt[TF_TCAM_TBL_TYPE_WC_TCAM] = 8;
143 resources->em_cnt[TF_DIR_TX].cnt[TF_EM_TBL_TYPE_EM_RECORD] = 15232;
146 resources->em_cnt[TF_DIR_TX].cnt[TF_EM_TBL_TYPE_TBL_SCOPE] = 1;
149 resources->tbl_cnt[TF_DIR_TX].cnt[TF_TBL_TYPE_ACT_SP_SMAC_IPV4] = 488;
151 rc = tf_open_session(&bp->tfp, ¶ms);
153 BNXT_TF_DBG(ERR, "Failed to open TF session - %s, rc = %d\n",
154 params.ctrl_chan_name, rc);
157 if (!session->session_opened) {
158 session->session_opened = 1;
159 session->g_tfp = &bp->tfp;
165 * Close the ULP session.
166 * It takes the ulp context pointer.
169 ulp_ctx_session_close(struct bnxt *bp,
170 struct bnxt_ulp_session_state *session)
172 /* close the session in the hardware */
173 if (session->session_opened)
174 tf_close_session(&bp->tfp);
175 session->session_opened = 0;
176 session->g_tfp = NULL;
180 bnxt_init_tbl_scope_parms(struct bnxt *bp,
181 struct tf_alloc_tbl_scope_parms *params)
183 struct bnxt_ulp_device_params *dparms;
187 rc = bnxt_ulp_cntxt_dev_id_get(bp->ulp_ctx, &dev_id);
189 /* TBD: For now, just use default. */
192 dparms = bnxt_ulp_device_params_get(dev_id);
195 * Set the flush timer for EEM entries. The value is in 100ms intervals,
198 params->hw_flow_cache_flush_timer = 100;
201 params->rx_max_key_sz_in_bits = BNXT_ULP_DFLT_RX_MAX_KEY;
202 params->rx_max_action_entry_sz_in_bits =
203 BNXT_ULP_DFLT_RX_MAX_ACTN_ENTRY;
204 params->rx_mem_size_in_mb = BNXT_ULP_DFLT_RX_MEM;
205 params->rx_num_flows_in_k = BNXT_ULP_RX_NUM_FLOWS;
206 params->rx_tbl_if_id = BNXT_ULP_RX_TBL_IF_ID;
208 params->tx_max_key_sz_in_bits = BNXT_ULP_DFLT_TX_MAX_KEY;
209 params->tx_max_action_entry_sz_in_bits =
210 BNXT_ULP_DFLT_TX_MAX_ACTN_ENTRY;
211 params->tx_mem_size_in_mb = BNXT_ULP_DFLT_TX_MEM;
212 params->tx_num_flows_in_k = BNXT_ULP_TX_NUM_FLOWS;
213 params->tx_tbl_if_id = BNXT_ULP_TX_TBL_IF_ID;
215 params->rx_max_key_sz_in_bits = BNXT_ULP_DFLT_RX_MAX_KEY;
216 params->rx_max_action_entry_sz_in_bits =
217 BNXT_ULP_DFLT_RX_MAX_ACTN_ENTRY;
218 params->rx_mem_size_in_mb = BNXT_ULP_DFLT_RX_MEM;
219 params->rx_num_flows_in_k = dparms->flow_db_num_entries / 1024;
220 params->rx_tbl_if_id = BNXT_ULP_RX_TBL_IF_ID;
222 params->tx_max_key_sz_in_bits = BNXT_ULP_DFLT_TX_MAX_KEY;
223 params->tx_max_action_entry_sz_in_bits =
224 BNXT_ULP_DFLT_TX_MAX_ACTN_ENTRY;
225 params->tx_mem_size_in_mb = BNXT_ULP_DFLT_TX_MEM;
226 params->tx_num_flows_in_k = dparms->flow_db_num_entries / 1024;
227 params->tx_tbl_if_id = BNXT_ULP_TX_TBL_IF_ID;
231 /* Initialize Extended Exact Match host memory. */
233 ulp_eem_tbl_scope_init(struct bnxt *bp)
235 struct tf_alloc_tbl_scope_parms params = {0};
237 struct bnxt_ulp_device_params *dparms;
240 /* Get the dev specific number of flows that needed to be supported. */
241 if (bnxt_ulp_cntxt_dev_id_get(bp->ulp_ctx, &dev_id)) {
242 BNXT_TF_DBG(ERR, "Invalid device id\n");
246 dparms = bnxt_ulp_device_params_get(dev_id);
248 BNXT_TF_DBG(ERR, "could not fetch the device params\n");
252 if (dparms->flow_mem_type != BNXT_ULP_FLOW_MEM_TYPE_EXT) {
253 BNXT_TF_DBG(INFO, "Table Scope alloc is not required\n");
257 bnxt_init_tbl_scope_parms(bp, ¶ms);
259 rc = tf_alloc_tbl_scope(&bp->tfp, ¶ms);
261 BNXT_TF_DBG(ERR, "Unable to allocate eem table scope rc = %d\n",
266 rc = bnxt_ulp_cntxt_tbl_scope_id_set(bp->ulp_ctx, params.tbl_scope_id);
268 BNXT_TF_DBG(ERR, "Unable to set table scope id\n");
275 /* Free Extended Exact Match host memory */
277 ulp_eem_tbl_scope_deinit(struct bnxt *bp, struct bnxt_ulp_context *ulp_ctx)
279 struct tf_free_tbl_scope_parms params = {0};
282 struct bnxt_ulp_device_params *dparms;
285 if (!ulp_ctx || !ulp_ctx->cfg_data)
288 tfp = bnxt_ulp_cntxt_tfp_get(ulp_ctx);
290 BNXT_TF_DBG(ERR, "Failed to get the truflow pointer\n");
294 /* Get the dev specific number of flows that needed to be supported. */
295 if (bnxt_ulp_cntxt_dev_id_get(bp->ulp_ctx, &dev_id)) {
296 BNXT_TF_DBG(ERR, "Invalid device id\n");
300 dparms = bnxt_ulp_device_params_get(dev_id);
302 BNXT_TF_DBG(ERR, "could not fetch the device params\n");
306 if (dparms->flow_mem_type != BNXT_ULP_FLOW_MEM_TYPE_EXT) {
307 BNXT_TF_DBG(INFO, "Table Scope free is not required\n");
311 rc = bnxt_ulp_cntxt_tbl_scope_id_get(ulp_ctx, ¶ms.tbl_scope_id);
313 BNXT_TF_DBG(ERR, "Failed to get the table scope id\n");
317 rc = tf_free_tbl_scope(tfp, ¶ms);
319 BNXT_TF_DBG(ERR, "Unable to free table scope\n");
325 /* The function to free and deinit the ulp context data. */
327 ulp_ctx_deinit(struct bnxt *bp,
328 struct bnxt_ulp_session_state *session)
330 /* close the tf session */
331 ulp_ctx_session_close(bp, session);
333 /* Free the contents */
334 if (session->cfg_data) {
335 rte_free(session->cfg_data);
336 bp->ulp_ctx->cfg_data = NULL;
337 session->cfg_data = NULL;
342 /* The function to allocate and initialize the ulp context data. */
344 ulp_ctx_init(struct bnxt *bp,
345 struct bnxt_ulp_session_state *session)
347 struct bnxt_ulp_data *ulp_data;
350 /* Allocate memory to hold ulp context data. */
351 ulp_data = rte_zmalloc("bnxt_ulp_data",
352 sizeof(struct bnxt_ulp_data), 0);
354 BNXT_TF_DBG(ERR, "Failed to allocate memory for ulp data\n");
358 /* Increment the ulp context data reference count usage. */
359 bp->ulp_ctx->cfg_data = ulp_data;
360 session->cfg_data = ulp_data;
362 ulp_data->ulp_flags |= BNXT_ULP_VF_REP_ENABLED;
364 /* Open the ulp session. */
365 rc = ulp_ctx_session_open(bp, session);
367 session->session_opened = 1;
368 (void)ulp_ctx_deinit(bp, session);
372 bnxt_ulp_cntxt_tfp_set(bp->ulp_ctx, &bp->tfp);
376 /* The function to initialize ulp dparms with devargs */
378 ulp_dparms_init(struct bnxt *bp,
379 struct bnxt_ulp_context *ulp_ctx)
381 struct bnxt_ulp_device_params *dparms;
384 if (!bp->max_num_kflows)
387 if (bnxt_ulp_cntxt_dev_id_get(ulp_ctx, &dev_id)) {
388 BNXT_TF_DBG(DEBUG, "Failed to get device id\n");
392 dparms = bnxt_ulp_device_params_get(dev_id);
394 BNXT_TF_DBG(DEBUG, "Failed to get device parms\n");
398 /* num_flows = max_num_kflows * 1024 */
399 dparms->flow_db_num_entries = bp->max_num_kflows * 1024;
400 /* GFID = 2 * num_flows */
401 dparms->mark_db_gfid_entries = dparms->flow_db_num_entries * 2;
402 BNXT_TF_DBG(DEBUG, "Set the number of flows = %"PRIu64"\n",
403 dparms->flow_db_num_entries);
408 /* The function to initialize bp flags with truflow features */
410 ulp_dparms_dev_port_intf_update(struct bnxt *bp,
411 struct bnxt_ulp_context *ulp_ctx)
413 struct bnxt_ulp_device_params *dparms;
416 if (bnxt_ulp_cntxt_dev_id_get(ulp_ctx, &dev_id)) {
417 BNXT_TF_DBG(DEBUG, "Failed to get device id\n");
421 dparms = bnxt_ulp_device_params_get(dev_id);
423 BNXT_TF_DBG(DEBUG, "Failed to get device parms\n");
427 /* Update the bp flag with gfid flag */
428 if (dparms->flow_mem_type == BNXT_ULP_FLOW_MEM_TYPE_EXT)
429 bp->flags |= BNXT_FLAG_GFID_ENABLE;
435 ulp_ctx_attach(struct bnxt *bp,
436 struct bnxt_ulp_session_state *session)
440 /* Increment the ulp context data reference count usage. */
441 bp->ulp_ctx->cfg_data = session->cfg_data;
442 bp->ulp_ctx->cfg_data->ref_cnt++;
444 /* update the session details in bnxt tfp */
445 bp->tfp.session = session->g_tfp->session;
447 /* Create a TF Client */
448 rc = ulp_ctx_session_open(bp, session);
450 PMD_DRV_LOG(ERR, "Failed to open ctxt session, rc:%d\n", rc);
451 bp->tfp.session = NULL;
455 bnxt_ulp_cntxt_tfp_set(bp->ulp_ctx, &bp->tfp);
460 ulp_ctx_detach(struct bnxt *bp)
462 if (bp->tfp.session) {
463 tf_close_session(&bp->tfp);
464 bp->tfp.session = NULL;
469 * Initialize the state of an ULP session.
470 * If the state of an ULP session is not initialized, set it's state to
471 * initialized. If the state is already initialized, do nothing.
474 ulp_context_initialized(struct bnxt_ulp_session_state *session, bool *init)
476 pthread_mutex_lock(&session->bnxt_ulp_mutex);
478 if (!session->bnxt_ulp_init) {
479 session->bnxt_ulp_init = true;
485 pthread_mutex_unlock(&session->bnxt_ulp_mutex);
489 * Check if an ULP session is already allocated for a specific PCI
490 * domain & bus. If it is already allocated simply return the session
491 * pointer, otherwise allocate a new session.
493 static struct bnxt_ulp_session_state *
494 ulp_get_session(struct rte_pci_addr *pci_addr)
496 struct bnxt_ulp_session_state *session;
498 STAILQ_FOREACH(session, &bnxt_ulp_session_list, next) {
499 if (session->pci_info.domain == pci_addr->domain &&
500 session->pci_info.bus == pci_addr->bus) {
508 * Allocate and Initialize an ULP session and set it's state to INITIALIZED.
509 * If it's already initialized simply return the already existing session.
511 static struct bnxt_ulp_session_state *
512 ulp_session_init(struct bnxt *bp,
515 struct rte_pci_device *pci_dev;
516 struct rte_pci_addr *pci_addr;
517 struct bnxt_ulp_session_state *session;
523 pci_dev = RTE_DEV_TO_PCI(bp->eth_dev->device);
524 pci_addr = &pci_dev->addr;
526 pthread_mutex_lock(&bnxt_ulp_global_mutex);
528 session = ulp_get_session(pci_addr);
530 /* Not Found the session Allocate a new one */
531 session = rte_zmalloc("bnxt_ulp_session",
532 sizeof(struct bnxt_ulp_session_state),
536 "Allocation failed for bnxt_ulp_session\n");
537 pthread_mutex_unlock(&bnxt_ulp_global_mutex);
541 /* Add it to the queue */
542 session->pci_info.domain = pci_addr->domain;
543 session->pci_info.bus = pci_addr->bus;
544 rc = pthread_mutex_init(&session->bnxt_ulp_mutex, NULL);
546 BNXT_TF_DBG(ERR, "mutex create failed\n");
547 pthread_mutex_unlock(&bnxt_ulp_global_mutex);
550 STAILQ_INSERT_TAIL(&bnxt_ulp_session_list,
554 ulp_context_initialized(session, init);
555 pthread_mutex_unlock(&bnxt_ulp_global_mutex);
560 * When a device is closed, remove it's associated session from the global
564 ulp_session_deinit(struct bnxt_ulp_session_state *session)
569 if (!session->cfg_data) {
570 pthread_mutex_lock(&bnxt_ulp_global_mutex);
571 STAILQ_REMOVE(&bnxt_ulp_session_list, session,
572 bnxt_ulp_session_state, next);
573 pthread_mutex_destroy(&session->bnxt_ulp_mutex);
575 pthread_mutex_unlock(&bnxt_ulp_global_mutex);
580 * Internal api to enable NAT feature.
581 * Set set_flag to 1 to set the value or zero to reset the value.
582 * returns 0 on success.
585 bnxt_ulp_global_cfg_update(struct bnxt *bp,
587 enum tf_global_config_type type,
592 uint32_t global_cfg = 0;
594 struct tf_global_cfg_parms parms;
596 /* Initialize the params */
599 parms.offset = offset,
600 parms.config = (uint8_t *)&global_cfg,
601 parms.config_sz_in_bytes = sizeof(global_cfg);
603 rc = tf_get_global_cfg(&bp->tfp, &parms);
605 BNXT_TF_DBG(ERR, "Failed to get global cfg 0x%x rc:%d\n",
613 global_cfg &= ~value;
615 /* SET the register RE_CFA_REG_ACT_TECT */
616 rc = tf_set_global_cfg(&bp->tfp, &parms);
618 BNXT_TF_DBG(ERR, "Failed to set global cfg 0x%x rc:%d\n",
625 /* Internal function to delete all the flows belonging to the given port */
627 bnxt_ulp_flush_port_flows(struct bnxt *bp)
631 func_id = bnxt_get_fw_func_id(bp->eth_dev->data->port_id,
632 BNXT_ULP_INTF_TYPE_INVALID);
633 ulp_flow_db_function_flow_flush(bp->ulp_ctx, func_id);
636 /* Internal function to delete the VFR default flows */
638 bnxt_ulp_destroy_vfr_default_rules(struct bnxt *bp, bool global)
640 struct bnxt_ulp_vfr_rule_info *info;
642 struct rte_eth_dev *vfr_eth_dev;
643 struct bnxt_vf_representor *vfr_bp;
645 if (!BNXT_TRUFLOW_EN(bp) || BNXT_ETH_DEV_IS_REPRESENTOR(bp->eth_dev))
648 if (!bp->ulp_ctx || !bp->ulp_ctx->cfg_data)
651 /* Delete default rules for all ports */
652 for (port_id = 0; port_id < RTE_MAX_ETHPORTS; port_id++) {
653 info = &bp->ulp_ctx->cfg_data->vfr_rule_info[port_id];
657 if (!global && info->parent_port_id !=
658 bp->eth_dev->data->port_id)
661 /* Destroy the flows */
662 ulp_default_flow_destroy(bp->eth_dev, info->rep2vf_flow_id);
663 ulp_default_flow_destroy(bp->eth_dev, info->vf2rep_flow_id);
664 /* Clean up the tx action pointer */
665 vfr_eth_dev = &rte_eth_devices[port_id];
667 vfr_bp = vfr_eth_dev->data->dev_private;
668 vfr_bp->vfr_tx_cfa_action = 0;
670 memset(info, 0, sizeof(struct bnxt_ulp_vfr_rule_info));
675 * When a port is deinit'ed by dpdk. This function is called
676 * and this function clears the ULP context and rest of the
677 * infrastructure associated with it.
680 bnxt_ulp_deinit(struct bnxt *bp,
681 struct bnxt_ulp_session_state *session)
683 if (!bp->ulp_ctx || !bp->ulp_ctx->cfg_data)
686 /* clean up default flows */
687 bnxt_ulp_destroy_df_rules(bp, true);
689 /* clean up default VFR flows */
690 bnxt_ulp_destroy_vfr_default_rules(bp, true);
692 /* clean up regular flows */
693 ulp_flow_db_flush_flows(bp->ulp_ctx, BNXT_ULP_REGULAR_FLOW_TABLE);
695 /* cleanup the eem table scope */
696 ulp_eem_tbl_scope_deinit(bp, bp->ulp_ctx);
698 /* cleanup the flow database */
699 ulp_flow_db_deinit(bp->ulp_ctx);
701 /* Delete the Mark database */
702 ulp_mark_db_deinit(bp->ulp_ctx);
704 /* cleanup the ulp mapper */
705 ulp_mapper_deinit(bp->ulp_ctx);
707 /* Delete the Flow Counter Manager */
708 ulp_fc_mgr_deinit(bp->ulp_ctx);
710 /* Delete the Port database */
711 ulp_port_db_deinit(bp->ulp_ctx);
713 /* Disable NAT feature */
714 (void)bnxt_ulp_global_cfg_update(bp, TF_DIR_RX, TF_TUNNEL_ENCAP,
716 (BNXT_ULP_NAT_INNER_L2_HEADER_SMAC |
717 BNXT_ULP_NAT_INNER_L2_HEADER_DMAC),
720 (void)bnxt_ulp_global_cfg_update(bp, TF_DIR_TX, TF_TUNNEL_ENCAP,
722 (BNXT_ULP_NAT_INNER_L2_HEADER_SMAC |
723 BNXT_ULP_NAT_INNER_L2_HEADER_DMAC),
726 /* Delete the ulp context and tf session and free the ulp context */
727 ulp_ctx_deinit(bp, session);
728 BNXT_TF_DBG(DEBUG, "ulp ctx has been deinitialized\n");
732 * When a port is initialized by dpdk. This functions is called
733 * and this function initializes the ULP context and rest of the
734 * infrastructure associated with it.
737 bnxt_ulp_init(struct bnxt *bp,
738 struct bnxt_ulp_session_state *session)
742 /* Allocate and Initialize the ulp context. */
743 rc = ulp_ctx_init(bp, session);
745 BNXT_TF_DBG(ERR, "Failed to create the ulp context\n");
749 /* Initialize ulp dparms with values devargs passed */
750 rc = ulp_dparms_init(bp, bp->ulp_ctx);
752 BNXT_TF_DBG(ERR, "Failed to initialize the dparms\n");
756 /* create the port database */
757 rc = ulp_port_db_init(bp->ulp_ctx, bp->port_cnt);
759 BNXT_TF_DBG(ERR, "Failed to create the port database\n");
763 /* Create the Mark database. */
764 rc = ulp_mark_db_init(bp->ulp_ctx);
766 BNXT_TF_DBG(ERR, "Failed to create the mark database\n");
770 /* Create the flow database. */
771 rc = ulp_flow_db_init(bp->ulp_ctx);
773 BNXT_TF_DBG(ERR, "Failed to create the flow database\n");
777 /* Create the eem table scope. */
778 rc = ulp_eem_tbl_scope_init(bp);
780 BNXT_TF_DBG(ERR, "Failed to create the eem scope table\n");
784 rc = ulp_mapper_init(bp->ulp_ctx);
786 BNXT_TF_DBG(ERR, "Failed to initialize ulp mapper\n");
790 rc = ulp_fc_mgr_init(bp->ulp_ctx);
792 BNXT_TF_DBG(ERR, "Failed to initialize ulp flow counter mgr\n");
797 * Enable NAT feature. Set the global configuration register
798 * Tunnel encap to enable NAT with the reuse of existing inner
799 * L2 header smac and dmac
801 rc = bnxt_ulp_global_cfg_update(bp, TF_DIR_RX, TF_TUNNEL_ENCAP,
803 (BNXT_ULP_NAT_INNER_L2_HEADER_SMAC |
804 BNXT_ULP_NAT_INNER_L2_HEADER_DMAC), 1);
806 BNXT_TF_DBG(ERR, "Failed to set rx global configuration\n");
810 rc = bnxt_ulp_global_cfg_update(bp, TF_DIR_TX, TF_TUNNEL_ENCAP,
812 (BNXT_ULP_NAT_INNER_L2_HEADER_SMAC |
813 BNXT_ULP_NAT_INNER_L2_HEADER_DMAC), 1);
815 BNXT_TF_DBG(ERR, "Failed to set tx global configuration\n");
818 BNXT_TF_DBG(DEBUG, "ulp ctx has been initialized\n");
822 bnxt_ulp_deinit(bp, session);
827 * When a port is initialized by dpdk. This functions sets up
828 * the port specific details.
831 bnxt_ulp_port_init(struct bnxt *bp)
833 struct bnxt_ulp_session_state *session;
837 if (!bp || !BNXT_TRUFLOW_EN(bp))
840 if (!BNXT_PF(bp) && !BNXT_VF_IS_TRUSTED(bp)) {
842 "Skip ulp init for port: %d, not a TVF or PF\n",
843 bp->eth_dev->data->port_id);
848 BNXT_TF_DBG(DEBUG, "ulp ctx already allocated\n");
852 bp->ulp_ctx = rte_zmalloc("bnxt_ulp_ctx",
853 sizeof(struct bnxt_ulp_context), 0);
855 BNXT_TF_DBG(ERR, "Failed to allocate ulp ctx\n");
860 * Multiple uplink ports can be associated with a single vswitch.
861 * Make sure only the port that is started first will initialize
864 session = ulp_session_init(bp, &initialized);
866 BNXT_TF_DBG(ERR, "Failed to initialize the tf session\n");
873 * If ULP is already initialized for a specific domain then
874 * simply assign the ulp context to this rte_eth_dev.
876 rc = ulp_ctx_attach(bp, session);
878 BNXT_TF_DBG(ERR, "Failed to attach the ulp context\n");
882 rc = bnxt_ulp_init(bp, session);
884 BNXT_TF_DBG(ERR, "Failed to initialize the ulp init\n");
889 /* Update bnxt driver flags */
890 rc = ulp_dparms_dev_port_intf_update(bp, bp->ulp_ctx);
892 BNXT_TF_DBG(ERR, "Failed to update driver flags\n");
896 /* update the port database for the given interface */
897 rc = ulp_port_db_dev_port_intf_update(bp->ulp_ctx, bp->eth_dev);
899 BNXT_TF_DBG(ERR, "Failed to update port database\n");
902 /* create the default rules */
903 bnxt_ulp_create_df_rules(bp);
904 BNXT_TF_DBG(DEBUG, "ULP Port:%d created and initialized\n",
905 bp->eth_dev->data->port_id);
909 bnxt_ulp_port_deinit(bp);
914 * When a port is de-initialized by dpdk. This functions clears up
915 * the port specific details.
918 bnxt_ulp_port_deinit(struct bnxt *bp)
920 struct bnxt_ulp_session_state *session;
921 struct rte_pci_device *pci_dev;
922 struct rte_pci_addr *pci_addr;
924 if (!BNXT_TRUFLOW_EN(bp))
927 if (!BNXT_PF(bp) && !BNXT_VF_IS_TRUSTED(bp)) {
929 "Skip ULP deinit port:%d, not a TVF or PF\n",
930 bp->eth_dev->data->port_id);
935 BNXT_TF_DBG(DEBUG, "ulp ctx already de-allocated\n");
939 BNXT_TF_DBG(DEBUG, "ULP Port:%d destroyed\n",
940 bp->eth_dev->data->port_id);
942 /* Get the session details */
943 pci_dev = RTE_DEV_TO_PCI(bp->eth_dev->device);
944 pci_addr = &pci_dev->addr;
945 pthread_mutex_lock(&bnxt_ulp_global_mutex);
946 session = ulp_get_session(pci_addr);
947 pthread_mutex_unlock(&bnxt_ulp_global_mutex);
949 /* session not found then just exit */
951 /* Free the ulp context */
952 rte_free(bp->ulp_ctx);
957 /* Check the reference count to deinit or deattach*/
958 if (bp->ulp_ctx->cfg_data && bp->ulp_ctx->cfg_data->ref_cnt) {
959 bp->ulp_ctx->cfg_data->ref_cnt--;
960 if (bp->ulp_ctx->cfg_data->ref_cnt) {
961 /* free the port details */
962 /* Free the default flow rule associated to this port */
963 bnxt_ulp_destroy_df_rules(bp, false);
964 bnxt_ulp_destroy_vfr_default_rules(bp, false);
966 /* free flows associated with this port */
967 bnxt_ulp_flush_port_flows(bp);
969 /* close the session associated with this port */
972 /* Perform ulp ctx deinit */
973 bnxt_ulp_deinit(bp, session);
977 /* clean up the session */
978 ulp_session_deinit(session);
980 /* Free the ulp context */
981 rte_free(bp->ulp_ctx);
985 /* Below are the access functions to access internal data of ulp context. */
986 /* Function to set the Mark DB into the context */
988 bnxt_ulp_cntxt_ptr2_mark_db_set(struct bnxt_ulp_context *ulp_ctx,
989 struct bnxt_ulp_mark_tbl *mark_tbl)
991 if (!ulp_ctx || !ulp_ctx->cfg_data) {
992 BNXT_TF_DBG(ERR, "Invalid ulp context data\n");
996 ulp_ctx->cfg_data->mark_tbl = mark_tbl;
1001 /* Function to retrieve the Mark DB from the context. */
1002 struct bnxt_ulp_mark_tbl *
1003 bnxt_ulp_cntxt_ptr2_mark_db_get(struct bnxt_ulp_context *ulp_ctx)
1005 if (!ulp_ctx || !ulp_ctx->cfg_data)
1008 return ulp_ctx->cfg_data->mark_tbl;
1011 /* Function to set the device id of the hardware. */
1013 bnxt_ulp_cntxt_dev_id_set(struct bnxt_ulp_context *ulp_ctx,
1016 if (ulp_ctx && ulp_ctx->cfg_data) {
1017 ulp_ctx->cfg_data->dev_id = dev_id;
1024 /* Function to get the device id of the hardware. */
1026 bnxt_ulp_cntxt_dev_id_get(struct bnxt_ulp_context *ulp_ctx,
1029 if (ulp_ctx && ulp_ctx->cfg_data) {
1030 *dev_id = ulp_ctx->cfg_data->dev_id;
1037 /* Function to get the table scope id of the EEM table. */
1039 bnxt_ulp_cntxt_tbl_scope_id_get(struct bnxt_ulp_context *ulp_ctx,
1040 uint32_t *tbl_scope_id)
1042 if (ulp_ctx && ulp_ctx->cfg_data) {
1043 *tbl_scope_id = ulp_ctx->cfg_data->tbl_scope_id;
1050 /* Function to set the table scope id of the EEM table. */
1052 bnxt_ulp_cntxt_tbl_scope_id_set(struct bnxt_ulp_context *ulp_ctx,
1053 uint32_t tbl_scope_id)
1055 if (ulp_ctx && ulp_ctx->cfg_data) {
1056 ulp_ctx->cfg_data->tbl_scope_id = tbl_scope_id;
1063 /* Function to set the tfp session details from the ulp context. */
1065 bnxt_ulp_cntxt_tfp_set(struct bnxt_ulp_context *ulp, struct tf *tfp)
1068 BNXT_TF_DBG(ERR, "Invalid arguments\n");
1076 /* Function to get the tfp session details from the ulp context. */
1078 bnxt_ulp_cntxt_tfp_get(struct bnxt_ulp_context *ulp)
1081 BNXT_TF_DBG(ERR, "Invalid arguments\n");
1088 * Get the device table entry based on the device id.
1090 * dev_id [in] The device id of the hardware
1092 * Returns the pointer to the device parameters.
1094 struct bnxt_ulp_device_params *
1095 bnxt_ulp_device_params_get(uint32_t dev_id)
1097 if (dev_id < BNXT_ULP_MAX_NUM_DEVICES)
1098 return &ulp_device_params[dev_id];
1102 /* Function to set the flow database to the ulp context. */
1104 bnxt_ulp_cntxt_ptr2_flow_db_set(struct bnxt_ulp_context *ulp_ctx,
1105 struct bnxt_ulp_flow_db *flow_db)
1107 if (!ulp_ctx || !ulp_ctx->cfg_data)
1110 ulp_ctx->cfg_data->flow_db = flow_db;
1114 /* Function to get the flow database from the ulp context. */
1115 struct bnxt_ulp_flow_db *
1116 bnxt_ulp_cntxt_ptr2_flow_db_get(struct bnxt_ulp_context *ulp_ctx)
1118 if (!ulp_ctx || !ulp_ctx->cfg_data)
1121 return ulp_ctx->cfg_data->flow_db;
1124 /* Function to get the ulp context from eth device. */
1125 struct bnxt_ulp_context *
1126 bnxt_ulp_eth_dev_ptr2_cntxt_get(struct rte_eth_dev *dev)
1128 struct bnxt *bp = (struct bnxt *)dev->data->dev_private;
1130 if (BNXT_ETH_DEV_IS_REPRESENTOR(dev)) {
1131 struct bnxt_vf_representor *vfr = dev->data->dev_private;
1133 bp = vfr->parent_dev->data->dev_private;
1137 BNXT_TF_DBG(ERR, "Bnxt private data is not initialized\n");
1144 bnxt_ulp_cntxt_ptr2_mapper_data_set(struct bnxt_ulp_context *ulp_ctx,
1147 if (!ulp_ctx || !ulp_ctx->cfg_data) {
1148 BNXT_TF_DBG(ERR, "Invalid ulp context data\n");
1152 ulp_ctx->cfg_data->mapper_data = mapper_data;
1157 bnxt_ulp_cntxt_ptr2_mapper_data_get(struct bnxt_ulp_context *ulp_ctx)
1159 if (!ulp_ctx || !ulp_ctx->cfg_data) {
1160 BNXT_TF_DBG(ERR, "Invalid ulp context data\n");
1164 return ulp_ctx->cfg_data->mapper_data;
1167 /* Function to set the port database to the ulp context. */
1169 bnxt_ulp_cntxt_ptr2_port_db_set(struct bnxt_ulp_context *ulp_ctx,
1170 struct bnxt_ulp_port_db *port_db)
1172 if (!ulp_ctx || !ulp_ctx->cfg_data)
1175 ulp_ctx->cfg_data->port_db = port_db;
1179 /* Function to get the port database from the ulp context. */
1180 struct bnxt_ulp_port_db *
1181 bnxt_ulp_cntxt_ptr2_port_db_get(struct bnxt_ulp_context *ulp_ctx)
1183 if (!ulp_ctx || !ulp_ctx->cfg_data)
1186 return ulp_ctx->cfg_data->port_db;
1189 /* Function to set the flow counter info into the context */
1191 bnxt_ulp_cntxt_ptr2_fc_info_set(struct bnxt_ulp_context *ulp_ctx,
1192 struct bnxt_ulp_fc_info *ulp_fc_info)
1194 if (!ulp_ctx || !ulp_ctx->cfg_data) {
1195 BNXT_TF_DBG(ERR, "Invalid ulp context data\n");
1199 ulp_ctx->cfg_data->fc_info = ulp_fc_info;
1204 /* Function to retrieve the flow counter info from the context. */
1205 struct bnxt_ulp_fc_info *
1206 bnxt_ulp_cntxt_ptr2_fc_info_get(struct bnxt_ulp_context *ulp_ctx)
1208 if (!ulp_ctx || !ulp_ctx->cfg_data)
1211 return ulp_ctx->cfg_data->fc_info;
1214 /* Function to get the ulp flags from the ulp context. */
1216 bnxt_ulp_cntxt_ptr2_ulp_flags_get(struct bnxt_ulp_context *ulp_ctx,
1219 if (!ulp_ctx || !ulp_ctx->cfg_data)
1222 *flags = ulp_ctx->cfg_data->ulp_flags;
1226 /* Function to get the ulp vfr info from the ulp context. */
1227 struct bnxt_ulp_vfr_rule_info*
1228 bnxt_ulp_cntxt_ptr2_ulp_vfr_info_get(struct bnxt_ulp_context *ulp_ctx,
1231 if (!ulp_ctx || !ulp_ctx->cfg_data || port_id >= RTE_MAX_ETHPORTS)
1234 return &ulp_ctx->cfg_data->vfr_rule_info[port_id];