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>
13 #include "bnxt_tf_common.h"
16 #include "tf_ext_flow_handle.h"
18 #include "ulp_template_db.h"
19 #include "ulp_template_struct.h"
20 #include "ulp_mark_mgr.h"
21 #include "ulp_flow_db.h"
22 #include "ulp_mapper.h"
23 #include "ulp_port_db.h"
25 /* Linked list of all TF sessions. */
26 STAILQ_HEAD(, bnxt_ulp_session_state) bnxt_ulp_session_list =
27 STAILQ_HEAD_INITIALIZER(bnxt_ulp_session_list);
29 /* Mutex to synchronize bnxt_ulp_session_list operations. */
30 static pthread_mutex_t bnxt_ulp_global_mutex = PTHREAD_MUTEX_INITIALIZER;
33 * Allow the deletion of context only for the bnxt device that
35 * TBD - The implementation of the function should change to
36 * using the reference count once tf_session_attach functionality
40 ulp_ctx_deinit_allowed(void *ptr)
42 struct bnxt *bp = (struct bnxt *)ptr;
47 if (&bp->tfp == bp->ulp_ctx->g_tfp)
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;
72 memset(¶ms, 0, sizeof(params));
74 rc = rte_eth_dev_get_name_by_port(ethdev->data->port_id,
75 params.ctrl_chan_name);
77 BNXT_TF_DBG(ERR, "Invalid port %d, rc = %d\n",
78 ethdev->data->port_id, rc);
82 rc = tf_open_session(&bp->tfp, ¶ms);
84 BNXT_TF_DBG(ERR, "Failed to open TF session - %s, rc = %d\n",
85 params.ctrl_chan_name, rc);
88 session->session_opened = 1;
89 session->g_tfp = &bp->tfp;
94 * Close the ULP session.
95 * It takes the ulp context pointer.
98 ulp_ctx_session_close(struct bnxt *bp,
99 struct bnxt_ulp_session_state *session)
101 /* close the session in the hardware */
102 if (session->session_opened)
103 tf_close_session(&bp->tfp);
104 session->session_opened = 0;
105 session->g_tfp = NULL;
106 bp->ulp_ctx->g_tfp = NULL;
110 bnxt_init_tbl_scope_parms(struct bnxt *bp,
111 struct tf_alloc_tbl_scope_parms *params)
113 struct bnxt_ulp_device_params *dparms;
117 rc = bnxt_ulp_cntxt_dev_id_get(bp->ulp_ctx, &dev_id);
119 /* TBD: For now, just use default. */
122 dparms = bnxt_ulp_device_params_get(dev_id);
125 * Set the flush timer for EEM entries. The value is in 100ms intervals,
128 params->hw_flow_cache_flush_timer = 100;
131 params->rx_max_key_sz_in_bits = BNXT_ULP_DFLT_RX_MAX_KEY;
132 params->rx_max_action_entry_sz_in_bits =
133 BNXT_ULP_DFLT_RX_MAX_ACTN_ENTRY;
134 params->rx_mem_size_in_mb = BNXT_ULP_DFLT_RX_MEM;
135 params->rx_num_flows_in_k = BNXT_ULP_RX_NUM_FLOWS;
136 params->rx_tbl_if_id = BNXT_ULP_RX_TBL_IF_ID;
138 params->tx_max_key_sz_in_bits = BNXT_ULP_DFLT_TX_MAX_KEY;
139 params->tx_max_action_entry_sz_in_bits =
140 BNXT_ULP_DFLT_TX_MAX_ACTN_ENTRY;
141 params->tx_mem_size_in_mb = BNXT_ULP_DFLT_TX_MEM;
142 params->tx_num_flows_in_k = BNXT_ULP_TX_NUM_FLOWS;
143 params->tx_tbl_if_id = BNXT_ULP_TX_TBL_IF_ID;
145 params->rx_max_key_sz_in_bits = BNXT_ULP_DFLT_RX_MAX_KEY;
146 params->rx_max_action_entry_sz_in_bits =
147 BNXT_ULP_DFLT_RX_MAX_ACTN_ENTRY;
148 params->rx_mem_size_in_mb = BNXT_ULP_DFLT_RX_MEM;
149 params->rx_num_flows_in_k = dparms->num_flows / (1024);
150 params->rx_tbl_if_id = BNXT_ULP_RX_TBL_IF_ID;
152 params->tx_max_key_sz_in_bits = BNXT_ULP_DFLT_TX_MAX_KEY;
153 params->tx_max_action_entry_sz_in_bits =
154 BNXT_ULP_DFLT_TX_MAX_ACTN_ENTRY;
155 params->tx_mem_size_in_mb = BNXT_ULP_DFLT_TX_MEM;
156 params->tx_num_flows_in_k = dparms->num_flows / (1024);
157 params->tx_tbl_if_id = BNXT_ULP_TX_TBL_IF_ID;
161 /* Initialize Extended Exact Match host memory. */
163 ulp_eem_tbl_scope_init(struct bnxt *bp)
165 struct tf_alloc_tbl_scope_parms params = {0};
168 bnxt_init_tbl_scope_parms(bp, ¶ms);
170 rc = tf_alloc_tbl_scope(&bp->tfp, ¶ms);
172 BNXT_TF_DBG(ERR, "Unable to allocate eem table scope rc = %d\n",
177 rc = bnxt_ulp_cntxt_tbl_scope_id_set(bp->ulp_ctx, params.tbl_scope_id);
179 BNXT_TF_DBG(ERR, "Unable to set table scope id\n");
186 /* Free Extended Exact Match host memory */
188 ulp_eem_tbl_scope_deinit(struct bnxt *bp, struct bnxt_ulp_context *ulp_ctx)
190 struct tf_free_tbl_scope_parms params = {0};
194 if (!ulp_ctx || !ulp_ctx->cfg_data)
197 /* Free the resources for the last device */
198 if (!ulp_ctx_deinit_allowed(bp))
201 tfp = bnxt_ulp_cntxt_tfp_get(ulp_ctx);
203 BNXT_TF_DBG(ERR, "Failed to get the truflow pointer\n");
207 rc = bnxt_ulp_cntxt_tbl_scope_id_get(ulp_ctx, ¶ms.tbl_scope_id);
209 BNXT_TF_DBG(ERR, "Failed to get the table scope id\n");
213 rc = tf_free_tbl_scope(tfp, ¶ms);
215 BNXT_TF_DBG(ERR, "Unable to free table scope\n");
221 /* The function to free and deinit the ulp context data. */
223 ulp_ctx_deinit(struct bnxt *bp,
224 struct bnxt_ulp_session_state *session)
226 if (!session || !bp) {
227 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
231 /* close the tf session */
232 ulp_ctx_session_close(bp, session);
234 /* Free the contents */
235 if (session->cfg_data) {
236 rte_free(session->cfg_data);
237 bp->ulp_ctx->cfg_data = NULL;
238 session->cfg_data = NULL;
243 /* The function to allocate and initialize the ulp context data. */
245 ulp_ctx_init(struct bnxt *bp,
246 struct bnxt_ulp_session_state *session)
248 struct bnxt_ulp_data *ulp_data;
251 if (!session || !bp) {
252 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
256 /* Allocate memory to hold ulp context data. */
257 ulp_data = rte_zmalloc("bnxt_ulp_data",
258 sizeof(struct bnxt_ulp_data), 0);
260 BNXT_TF_DBG(ERR, "Failed to allocate memory for ulp data\n");
264 /* Increment the ulp context data reference count usage. */
265 bp->ulp_ctx->cfg_data = ulp_data;
266 session->cfg_data = ulp_data;
269 /* Open the ulp session. */
270 rc = ulp_ctx_session_open(bp, session);
272 (void)ulp_ctx_deinit(bp, session);
275 bnxt_ulp_cntxt_tfp_set(bp->ulp_ctx, session->g_tfp);
279 /* The function to initialize ulp dparms with devargs */
281 ulp_dparms_init(struct bnxt *bp,
282 struct bnxt_ulp_context *ulp_ctx)
284 struct bnxt_ulp_device_params *dparms;
287 if (!bp->max_num_kflows)
290 if (bnxt_ulp_cntxt_dev_id_get(ulp_ctx, &dev_id)) {
291 BNXT_TF_DBG(DEBUG, "Failed to get device id\n");
295 dparms = bnxt_ulp_device_params_get(dev_id);
297 BNXT_TF_DBG(DEBUG, "Failed to get device parms\n");
301 /* num_flows = max_num_kflows * 1024 */
302 dparms->num_flows = bp->max_num_kflows * 1024;
303 /* GFID = 2 * num_flows */
304 dparms->gfid_entries = dparms->num_flows * 2;
305 BNXT_TF_DBG(DEBUG, "Set the number of flows = %"PRIu64"\n",
312 ulp_ctx_attach(struct bnxt_ulp_context *ulp_ctx,
313 struct bnxt_ulp_session_state *session)
315 if (!ulp_ctx || !session) {
316 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
320 /* Increment the ulp context data reference count usage. */
321 ulp_ctx->cfg_data = session->cfg_data;
322 ulp_ctx->cfg_data->ref_cnt++;
324 /* TBD call TF_session_attach. */
325 ulp_ctx->g_tfp = session->g_tfp;
330 ulp_ctx_detach(struct bnxt *bp,
331 struct bnxt_ulp_session_state *session)
333 struct bnxt_ulp_context *ulp_ctx;
335 if (!bp || !session) {
336 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
339 ulp_ctx = bp->ulp_ctx;
341 if (!ulp_ctx->cfg_data)
344 /* TBD call TF_session_detach */
346 /* Increment the ulp context data reference count usage. */
347 if (ulp_ctx->cfg_data->ref_cnt >= 1) {
348 ulp_ctx->cfg_data->ref_cnt--;
349 if (ulp_ctx_deinit_allowed(bp))
350 ulp_ctx_deinit(bp, session);
351 ulp_ctx->cfg_data = NULL;
352 ulp_ctx->g_tfp = NULL;
355 BNXT_TF_DBG(ERR, "context deatach on invalid data\n");
360 * Initialize the state of an ULP session.
361 * If the state of an ULP session is not initialized, set it's state to
362 * initialized. If the state is already initialized, do nothing.
365 ulp_context_initialized(struct bnxt_ulp_session_state *session, bool *init)
367 pthread_mutex_lock(&session->bnxt_ulp_mutex);
369 if (!session->bnxt_ulp_init) {
370 session->bnxt_ulp_init = true;
376 pthread_mutex_unlock(&session->bnxt_ulp_mutex);
380 * Check if an ULP session is already allocated for a specific PCI
381 * domain & bus. If it is already allocated simply return the session
382 * pointer, otherwise allocate a new session.
384 static struct bnxt_ulp_session_state *
385 ulp_get_session(struct rte_pci_addr *pci_addr)
387 struct bnxt_ulp_session_state *session;
389 STAILQ_FOREACH(session, &bnxt_ulp_session_list, next) {
390 if (session->pci_info.domain == pci_addr->domain &&
391 session->pci_info.bus == pci_addr->bus) {
399 * Allocate and Initialize an ULP session and set it's state to INITIALIZED.
400 * If it's already initialized simply return the already existing session.
402 static struct bnxt_ulp_session_state *
403 ulp_session_init(struct bnxt *bp,
406 struct rte_pci_device *pci_dev;
407 struct rte_pci_addr *pci_addr;
408 struct bnxt_ulp_session_state *session;
413 pci_dev = RTE_DEV_TO_PCI(bp->eth_dev->device);
414 pci_addr = &pci_dev->addr;
416 pthread_mutex_lock(&bnxt_ulp_global_mutex);
418 session = ulp_get_session(pci_addr);
420 /* Not Found the session Allocate a new one */
421 session = rte_zmalloc("bnxt_ulp_session",
422 sizeof(struct bnxt_ulp_session_state),
426 "Allocation failed for bnxt_ulp_session\n");
427 pthread_mutex_unlock(&bnxt_ulp_global_mutex);
431 /* Add it to the queue */
432 session->pci_info.domain = pci_addr->domain;
433 session->pci_info.bus = pci_addr->bus;
434 pthread_mutex_init(&session->bnxt_ulp_mutex, NULL);
435 STAILQ_INSERT_TAIL(&bnxt_ulp_session_list,
439 ulp_context_initialized(session, init);
440 pthread_mutex_unlock(&bnxt_ulp_global_mutex);
445 * When a device is closed, remove it's associated session from the global
449 ulp_session_deinit(struct bnxt_ulp_session_state *session)
454 if (!session->cfg_data) {
455 pthread_mutex_lock(&bnxt_ulp_global_mutex);
456 STAILQ_REMOVE(&bnxt_ulp_session_list, session,
457 bnxt_ulp_session_state, next);
458 pthread_mutex_destroy(&session->bnxt_ulp_mutex);
460 pthread_mutex_unlock(&bnxt_ulp_global_mutex);
465 * When a port is initialized by dpdk. This functions is called
466 * and this function initializes the ULP context and rest of the
467 * infrastructure associated with it.
470 bnxt_ulp_init(struct bnxt *bp)
472 struct bnxt_ulp_session_state *session;
477 BNXT_TF_DBG(ERR, "ulp ctx already allocated\n");
482 * Multiple uplink ports can be associated with a single vswitch.
483 * Make sure only the port that is started first will initialize
486 session = ulp_session_init(bp, &init);
488 BNXT_TF_DBG(ERR, "Failed to initialize the tf session\n");
492 bp->ulp_ctx = rte_zmalloc("bnxt_ulp_ctx",
493 sizeof(struct bnxt_ulp_context), 0);
495 BNXT_TF_DBG(ERR, "Failed to allocate ulp ctx\n");
496 ulp_session_deinit(session);
501 * If ULP is already initialized for a specific domain then simply
502 * assign the ulp context to this rte_eth_dev.
505 rc = ulp_ctx_attach(bp->ulp_ctx, session);
508 "Failed to attach the ulp context\n");
509 ulp_session_deinit(session);
510 rte_free(bp->ulp_ctx);
513 /* update the port database */
514 rc = ulp_port_db_dev_port_intf_update(bp->ulp_ctx, bp);
517 "Failed to update port database\n");
518 ulp_ctx_detach(bp, session);
519 ulp_session_deinit(session);
520 rte_free(bp->ulp_ctx);
525 /* Allocate and Initialize the ulp context. */
526 rc = ulp_ctx_init(bp, session);
528 BNXT_TF_DBG(ERR, "Failed to create the ulp context\n");
532 /* Initialize ulp dparms with values devargs passed */
533 rc = ulp_dparms_init(bp, bp->ulp_ctx);
535 /* create the port database */
536 rc = ulp_port_db_init(bp->ulp_ctx);
538 BNXT_TF_DBG(ERR, "Failed to create the port database\n");
542 /* update the port database */
543 rc = ulp_port_db_dev_port_intf_update(bp->ulp_ctx, bp);
545 BNXT_TF_DBG(ERR, "Failed to update port database\n");
549 /* Create the Mark database. */
550 rc = ulp_mark_db_init(bp->ulp_ctx);
552 BNXT_TF_DBG(ERR, "Failed to create the mark database\n");
556 /* Create the flow database. */
557 rc = ulp_flow_db_init(bp->ulp_ctx);
559 BNXT_TF_DBG(ERR, "Failed to create the flow database\n");
563 /* Create the eem table scope. */
564 rc = ulp_eem_tbl_scope_init(bp);
566 BNXT_TF_DBG(ERR, "Failed to create the eem scope table\n");
570 rc = ulp_mapper_init(bp->ulp_ctx);
572 BNXT_TF_DBG(ERR, "Failed to initialize ulp mapper\n");
583 /* Below are the access functions to access internal data of ulp context. */
586 * When a port is deinit'ed by dpdk. This function is called
587 * and this function clears the ULP context and rest of the
588 * infrastructure associated with it.
591 bnxt_ulp_deinit(struct bnxt *bp)
593 struct bnxt_ulp_session_state *session;
594 struct rte_pci_device *pci_dev;
595 struct rte_pci_addr *pci_addr;
597 /* Get the session first */
598 pci_dev = RTE_DEV_TO_PCI(bp->eth_dev->device);
599 pci_addr = &pci_dev->addr;
600 pthread_mutex_lock(&bnxt_ulp_global_mutex);
601 session = ulp_get_session(pci_addr);
602 pthread_mutex_unlock(&bnxt_ulp_global_mutex);
604 /* session not found then just exit */
608 /* clean up regular flows */
609 ulp_flow_db_flush_flows(bp->ulp_ctx, BNXT_ULP_REGULAR_FLOW_TABLE);
611 /* cleanup the eem table scope */
612 ulp_eem_tbl_scope_deinit(bp, bp->ulp_ctx);
614 /* cleanup the flow database */
615 ulp_flow_db_deinit(bp->ulp_ctx);
617 /* Delete the Mark database */
618 ulp_mark_db_deinit(bp->ulp_ctx);
620 /* cleanup the ulp mapper */
621 ulp_mapper_deinit(bp->ulp_ctx);
623 /* Delete the Port database */
624 ulp_port_db_deinit(bp->ulp_ctx);
626 /* Delete the ulp context and tf session */
627 ulp_ctx_detach(bp, session);
629 /* Finally delete the bnxt session*/
630 ulp_session_deinit(session);
632 rte_free(bp->ulp_ctx);
635 /* Function to set the Mark DB into the context */
637 bnxt_ulp_cntxt_ptr2_mark_db_set(struct bnxt_ulp_context *ulp_ctx,
638 struct bnxt_ulp_mark_tbl *mark_tbl)
640 if (!ulp_ctx || !ulp_ctx->cfg_data) {
641 BNXT_TF_DBG(ERR, "Invalid ulp context data\n");
645 ulp_ctx->cfg_data->mark_tbl = mark_tbl;
650 /* Function to retrieve the Mark DB from the context. */
651 struct bnxt_ulp_mark_tbl *
652 bnxt_ulp_cntxt_ptr2_mark_db_get(struct bnxt_ulp_context *ulp_ctx)
654 if (!ulp_ctx || !ulp_ctx->cfg_data)
657 return ulp_ctx->cfg_data->mark_tbl;
660 /* Function to set the device id of the hardware. */
662 bnxt_ulp_cntxt_dev_id_set(struct bnxt_ulp_context *ulp_ctx,
665 if (ulp_ctx && ulp_ctx->cfg_data) {
666 ulp_ctx->cfg_data->dev_id = dev_id;
673 /* Function to get the device id of the hardware. */
675 bnxt_ulp_cntxt_dev_id_get(struct bnxt_ulp_context *ulp_ctx,
678 if (ulp_ctx && ulp_ctx->cfg_data) {
679 *dev_id = ulp_ctx->cfg_data->dev_id;
686 /* Function to get the table scope id of the EEM table. */
688 bnxt_ulp_cntxt_tbl_scope_id_get(struct bnxt_ulp_context *ulp_ctx,
689 uint32_t *tbl_scope_id)
691 if (ulp_ctx && ulp_ctx->cfg_data) {
692 *tbl_scope_id = ulp_ctx->cfg_data->tbl_scope_id;
699 /* Function to set the table scope id of the EEM table. */
701 bnxt_ulp_cntxt_tbl_scope_id_set(struct bnxt_ulp_context *ulp_ctx,
702 uint32_t tbl_scope_id)
704 if (ulp_ctx && ulp_ctx->cfg_data) {
705 ulp_ctx->cfg_data->tbl_scope_id = tbl_scope_id;
712 /* Function to set the tfp session details from the ulp context. */
714 bnxt_ulp_cntxt_tfp_set(struct bnxt_ulp_context *ulp, struct tf *tfp)
717 BNXT_TF_DBG(ERR, "Invalid arguments\n");
721 /* TBD The tfp should be removed once tf_attach is implemented. */
726 /* Function to get the tfp session details from the ulp context. */
728 bnxt_ulp_cntxt_tfp_get(struct bnxt_ulp_context *ulp)
731 BNXT_TF_DBG(ERR, "Invalid arguments\n");
734 /* TBD The tfp should be removed once tf_attach is implemented. */
739 * Get the device table entry based on the device id.
741 * dev_id [in] The device id of the hardware
743 * Returns the pointer to the device parameters.
745 struct bnxt_ulp_device_params *
746 bnxt_ulp_device_params_get(uint32_t dev_id)
748 if (dev_id < BNXT_ULP_MAX_NUM_DEVICES)
749 return &ulp_device_params[dev_id];
753 /* Function to set the flow database to the ulp context. */
755 bnxt_ulp_cntxt_ptr2_flow_db_set(struct bnxt_ulp_context *ulp_ctx,
756 struct bnxt_ulp_flow_db *flow_db)
758 if (!ulp_ctx || !ulp_ctx->cfg_data)
761 ulp_ctx->cfg_data->flow_db = flow_db;
765 /* Function to get the flow database from the ulp context. */
766 struct bnxt_ulp_flow_db *
767 bnxt_ulp_cntxt_ptr2_flow_db_get(struct bnxt_ulp_context *ulp_ctx)
769 if (!ulp_ctx || !ulp_ctx->cfg_data)
772 return ulp_ctx->cfg_data->flow_db;
775 /* Function to get the ulp context from eth device. */
776 struct bnxt_ulp_context *
777 bnxt_ulp_eth_dev_ptr2_cntxt_get(struct rte_eth_dev *dev)
781 bp = (struct bnxt *)dev->data->dev_private;
783 BNXT_TF_DBG(ERR, "Bnxt private data is not initialized\n");
790 bnxt_ulp_cntxt_ptr2_mapper_data_set(struct bnxt_ulp_context *ulp_ctx,
793 if (!ulp_ctx || !ulp_ctx->cfg_data) {
794 BNXT_TF_DBG(ERR, "Invalid ulp context data\n");
798 ulp_ctx->cfg_data->mapper_data = mapper_data;
803 bnxt_ulp_cntxt_ptr2_mapper_data_get(struct bnxt_ulp_context *ulp_ctx)
805 if (!ulp_ctx || !ulp_ctx->cfg_data) {
806 BNXT_TF_DBG(ERR, "Invalid ulp context data\n");
810 return ulp_ctx->cfg_data->mapper_data;
813 /* Function to set the port database to the ulp context. */
815 bnxt_ulp_cntxt_ptr2_port_db_set(struct bnxt_ulp_context *ulp_ctx,
816 struct bnxt_ulp_port_db *port_db)
818 if (!ulp_ctx || !ulp_ctx->cfg_data)
821 ulp_ctx->cfg_data->port_db = port_db;
825 /* Function to get the port database from the ulp context. */
826 struct bnxt_ulp_port_db *
827 bnxt_ulp_cntxt_ptr2_port_db_get(struct bnxt_ulp_context *ulp_ctx)
829 if (!ulp_ctx || !ulp_ctx->cfg_data)
832 return ulp_ctx->cfg_data->port_db;