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 params->rx_max_key_sz_in_bits = BNXT_ULP_DFLT_RX_MAX_KEY;
126 params->rx_max_action_entry_sz_in_bits =
127 BNXT_ULP_DFLT_RX_MAX_ACTN_ENTRY;
128 params->rx_mem_size_in_mb = BNXT_ULP_DFLT_RX_MEM;
129 params->rx_num_flows_in_k = BNXT_ULP_RX_NUM_FLOWS;
130 params->rx_tbl_if_id = BNXT_ULP_RX_TBL_IF_ID;
132 params->tx_max_key_sz_in_bits = BNXT_ULP_DFLT_TX_MAX_KEY;
133 params->tx_max_action_entry_sz_in_bits =
134 BNXT_ULP_DFLT_TX_MAX_ACTN_ENTRY;
135 params->tx_mem_size_in_mb = BNXT_ULP_DFLT_TX_MEM;
136 params->tx_num_flows_in_k = BNXT_ULP_TX_NUM_FLOWS;
137 params->tx_tbl_if_id = BNXT_ULP_TX_TBL_IF_ID;
139 params->rx_max_key_sz_in_bits = BNXT_ULP_DFLT_RX_MAX_KEY;
140 params->rx_max_action_entry_sz_in_bits =
141 BNXT_ULP_DFLT_RX_MAX_ACTN_ENTRY;
142 params->rx_mem_size_in_mb = BNXT_ULP_DFLT_RX_MEM;
143 params->rx_num_flows_in_k = dparms->num_flows / (1024);
144 params->rx_tbl_if_id = BNXT_ULP_RX_TBL_IF_ID;
146 params->tx_max_key_sz_in_bits = BNXT_ULP_DFLT_TX_MAX_KEY;
147 params->tx_max_action_entry_sz_in_bits =
148 BNXT_ULP_DFLT_TX_MAX_ACTN_ENTRY;
149 params->tx_mem_size_in_mb = BNXT_ULP_DFLT_TX_MEM;
150 params->tx_num_flows_in_k = dparms->num_flows / (1024);
151 params->tx_tbl_if_id = BNXT_ULP_TX_TBL_IF_ID;
155 /* Initialize Extended Exact Match host memory. */
157 ulp_eem_tbl_scope_init(struct bnxt *bp)
159 struct tf_alloc_tbl_scope_parms params = {0};
162 bnxt_init_tbl_scope_parms(bp, ¶ms);
164 rc = tf_alloc_tbl_scope(&bp->tfp, ¶ms);
166 BNXT_TF_DBG(ERR, "Unable to allocate eem table scope rc = %d\n",
171 rc = bnxt_ulp_cntxt_tbl_scope_id_set(&bp->ulp_ctx, params.tbl_scope_id);
173 BNXT_TF_DBG(ERR, "Unable to set table scope id\n");
180 /* Free Extended Exact Match host memory */
182 ulp_eem_tbl_scope_deinit(struct bnxt *bp, struct bnxt_ulp_context *ulp_ctx)
184 struct tf_free_tbl_scope_parms params = {0};
188 if (!ulp_ctx || !ulp_ctx->cfg_data)
191 /* Free the resources for the last device */
192 if (!ulp_ctx_deinit_allowed(bp))
195 tfp = bnxt_ulp_cntxt_tfp_get(ulp_ctx);
197 BNXT_TF_DBG(ERR, "Failed to get the truflow pointer\n");
201 rc = bnxt_ulp_cntxt_tbl_scope_id_get(ulp_ctx, ¶ms.tbl_scope_id);
203 BNXT_TF_DBG(ERR, "Failed to get the table scope id\n");
207 rc = tf_free_tbl_scope(tfp, ¶ms);
209 BNXT_TF_DBG(ERR, "Unable to free table scope\n");
215 /* The function to free and deinit the ulp context data. */
217 ulp_ctx_deinit(struct bnxt *bp,
218 struct bnxt_ulp_session_state *session)
220 if (!session || !bp) {
221 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
225 /* close the tf session */
226 ulp_ctx_session_close(bp, session);
228 /* Free the contents */
229 if (session->cfg_data) {
230 rte_free(session->cfg_data);
231 bp->ulp_ctx.cfg_data = NULL;
232 session->cfg_data = NULL;
237 /* The function to allocate and initialize the ulp context data. */
239 ulp_ctx_init(struct bnxt *bp,
240 struct bnxt_ulp_session_state *session)
242 struct bnxt_ulp_data *ulp_data;
245 if (!session || !bp) {
246 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
250 /* Allocate memory to hold ulp context data. */
251 ulp_data = rte_zmalloc("bnxt_ulp_data",
252 sizeof(struct bnxt_ulp_data), 0);
254 BNXT_TF_DBG(ERR, "Failed to allocate memory for ulp data\n");
258 /* Increment the ulp context data reference count usage. */
259 bp->ulp_ctx.cfg_data = ulp_data;
260 session->cfg_data = ulp_data;
263 /* Open the ulp session. */
264 rc = ulp_ctx_session_open(bp, session);
266 (void)ulp_ctx_deinit(bp, session);
269 bnxt_ulp_cntxt_tfp_set(&bp->ulp_ctx, session->g_tfp);
274 ulp_ctx_attach(struct bnxt_ulp_context *ulp_ctx,
275 struct bnxt_ulp_session_state *session)
277 if (!ulp_ctx || !session) {
278 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
282 /* Increment the ulp context data reference count usage. */
283 ulp_ctx->cfg_data = session->cfg_data;
284 ulp_ctx->cfg_data->ref_cnt++;
286 /* TBD call TF_session_attach. */
287 ulp_ctx->g_tfp = session->g_tfp;
292 ulp_ctx_detach(struct bnxt *bp,
293 struct bnxt_ulp_session_state *session)
295 struct bnxt_ulp_context *ulp_ctx;
297 if (!bp || !session) {
298 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
301 ulp_ctx = &bp->ulp_ctx;
303 if (!ulp_ctx->cfg_data)
306 /* TBD call TF_session_detach */
308 /* Increment the ulp context data reference count usage. */
309 if (ulp_ctx->cfg_data->ref_cnt >= 1) {
310 ulp_ctx->cfg_data->ref_cnt--;
311 if (ulp_ctx_deinit_allowed(bp))
312 ulp_ctx_deinit(bp, session);
313 ulp_ctx->cfg_data = NULL;
314 ulp_ctx->g_tfp = NULL;
317 BNXT_TF_DBG(ERR, "context deatach on invalid data\n");
322 * Initialize the state of an ULP session.
323 * If the state of an ULP session is not initialized, set it's state to
324 * initialized. If the state is already initialized, do nothing.
327 ulp_context_initialized(struct bnxt_ulp_session_state *session, bool *init)
329 pthread_mutex_lock(&session->bnxt_ulp_mutex);
331 if (!session->bnxt_ulp_init) {
332 session->bnxt_ulp_init = true;
338 pthread_mutex_unlock(&session->bnxt_ulp_mutex);
342 * Check if an ULP session is already allocated for a specific PCI
343 * domain & bus. If it is already allocated simply return the session
344 * pointer, otherwise allocate a new session.
346 static struct bnxt_ulp_session_state *
347 ulp_get_session(struct rte_pci_addr *pci_addr)
349 struct bnxt_ulp_session_state *session;
351 STAILQ_FOREACH(session, &bnxt_ulp_session_list, next) {
352 if (session->pci_info.domain == pci_addr->domain &&
353 session->pci_info.bus == pci_addr->bus) {
361 * Allocate and Initialize an ULP session and set it's state to INITIALIZED.
362 * If it's already initialized simply return the already existing session.
364 static struct bnxt_ulp_session_state *
365 ulp_session_init(struct bnxt *bp,
368 struct rte_pci_device *pci_dev;
369 struct rte_pci_addr *pci_addr;
370 struct bnxt_ulp_session_state *session;
375 pci_dev = RTE_DEV_TO_PCI(bp->eth_dev->device);
376 pci_addr = &pci_dev->addr;
378 pthread_mutex_lock(&bnxt_ulp_global_mutex);
380 session = ulp_get_session(pci_addr);
382 /* Not Found the session Allocate a new one */
383 session = rte_zmalloc("bnxt_ulp_session",
384 sizeof(struct bnxt_ulp_session_state),
388 "Allocation failed for bnxt_ulp_session\n");
389 pthread_mutex_unlock(&bnxt_ulp_global_mutex);
393 /* Add it to the queue */
394 session->pci_info.domain = pci_addr->domain;
395 session->pci_info.bus = pci_addr->bus;
396 pthread_mutex_init(&session->bnxt_ulp_mutex, NULL);
397 STAILQ_INSERT_TAIL(&bnxt_ulp_session_list,
401 ulp_context_initialized(session, init);
402 pthread_mutex_unlock(&bnxt_ulp_global_mutex);
407 * When a device is closed, remove it's associated session from the global
411 ulp_session_deinit(struct bnxt_ulp_session_state *session)
416 if (!session->cfg_data) {
417 pthread_mutex_lock(&bnxt_ulp_global_mutex);
418 STAILQ_REMOVE(&bnxt_ulp_session_list, session,
419 bnxt_ulp_session_state, next);
420 pthread_mutex_destroy(&session->bnxt_ulp_mutex);
422 pthread_mutex_unlock(&bnxt_ulp_global_mutex);
427 * When a port is initialized by dpdk. This functions is called
428 * and this function initializes the ULP context and rest of the
429 * infrastructure associated with it.
432 bnxt_ulp_init(struct bnxt *bp)
434 struct bnxt_ulp_session_state *session;
439 * Multiple uplink ports can be associated with a single vswitch.
440 * Make sure only the port that is started first will initialize
443 session = ulp_session_init(bp, &init);
445 BNXT_TF_DBG(ERR, "Failed to initialize the tf session\n");
450 * If ULP is already initialized for a specific domain then simply
451 * assign the ulp context to this rte_eth_dev.
454 rc = ulp_ctx_attach(&bp->ulp_ctx, session);
457 "Failed to attach the ulp context\n");
460 /* update the port database */
461 rc = ulp_port_db_dev_port_intf_update(&bp->ulp_ctx, bp);
464 "Failed to update port database\n");
469 /* Allocate and Initialize the ulp context. */
470 rc = ulp_ctx_init(bp, session);
472 BNXT_TF_DBG(ERR, "Failed to create the ulp context\n");
476 /* create the port database */
477 rc = ulp_port_db_init(&bp->ulp_ctx);
479 BNXT_TF_DBG(ERR, "Failed to create the port database\n");
483 /* update the port database */
484 rc = ulp_port_db_dev_port_intf_update(&bp->ulp_ctx, bp);
486 BNXT_TF_DBG(ERR, "Failed to update port database\n");
490 /* Create the Mark database. */
491 rc = ulp_mark_db_init(&bp->ulp_ctx);
493 BNXT_TF_DBG(ERR, "Failed to create the mark database\n");
497 /* Create the flow database. */
498 rc = ulp_flow_db_init(&bp->ulp_ctx);
500 BNXT_TF_DBG(ERR, "Failed to create the flow database\n");
504 /* Create the eem table scope. */
505 rc = ulp_eem_tbl_scope_init(bp);
507 BNXT_TF_DBG(ERR, "Failed to create the eem scope table\n");
511 rc = ulp_mapper_init(&bp->ulp_ctx);
513 BNXT_TF_DBG(ERR, "Failed to initialize ulp mapper\n");
524 /* Below are the access functions to access internal data of ulp context. */
527 * When a port is deinit'ed by dpdk. This function is called
528 * and this function clears the ULP context and rest of the
529 * infrastructure associated with it.
532 bnxt_ulp_deinit(struct bnxt *bp)
534 struct bnxt_ulp_session_state *session;
535 struct rte_pci_device *pci_dev;
536 struct rte_pci_addr *pci_addr;
538 /* Get the session first */
539 pci_dev = RTE_DEV_TO_PCI(bp->eth_dev->device);
540 pci_addr = &pci_dev->addr;
541 pthread_mutex_lock(&bnxt_ulp_global_mutex);
542 session = ulp_get_session(pci_addr);
543 pthread_mutex_unlock(&bnxt_ulp_global_mutex);
545 /* session not found then just exit */
549 /* clean up regular flows */
550 ulp_flow_db_flush_flows(&bp->ulp_ctx, BNXT_ULP_REGULAR_FLOW_TABLE);
552 /* cleanup the eem table scope */
553 ulp_eem_tbl_scope_deinit(bp, &bp->ulp_ctx);
555 /* cleanup the flow database */
556 ulp_flow_db_deinit(&bp->ulp_ctx);
558 /* Delete the Mark database */
559 ulp_mark_db_deinit(&bp->ulp_ctx);
561 /* cleanup the ulp mapper */
562 ulp_mapper_deinit(&bp->ulp_ctx);
564 /* Delete the Port database */
565 ulp_port_db_deinit(&bp->ulp_ctx);
567 /* Delete the ulp context and tf session */
568 ulp_ctx_detach(bp, session);
570 /* Finally delete the bnxt session*/
571 ulp_session_deinit(session);
574 /* Function to set the Mark DB into the context */
576 bnxt_ulp_cntxt_ptr2_mark_db_set(struct bnxt_ulp_context *ulp_ctx,
577 struct bnxt_ulp_mark_tbl *mark_tbl)
579 if (!ulp_ctx || !ulp_ctx->cfg_data) {
580 BNXT_TF_DBG(ERR, "Invalid ulp context data\n");
584 ulp_ctx->cfg_data->mark_tbl = mark_tbl;
589 /* Function to retrieve the Mark DB from the context. */
590 struct bnxt_ulp_mark_tbl *
591 bnxt_ulp_cntxt_ptr2_mark_db_get(struct bnxt_ulp_context *ulp_ctx)
593 if (!ulp_ctx || !ulp_ctx->cfg_data)
596 return ulp_ctx->cfg_data->mark_tbl;
599 /* Function to set the device id of the hardware. */
601 bnxt_ulp_cntxt_dev_id_set(struct bnxt_ulp_context *ulp_ctx,
604 if (ulp_ctx && ulp_ctx->cfg_data) {
605 ulp_ctx->cfg_data->dev_id = dev_id;
612 /* Function to get the device id of the hardware. */
614 bnxt_ulp_cntxt_dev_id_get(struct bnxt_ulp_context *ulp_ctx,
617 if (ulp_ctx && ulp_ctx->cfg_data) {
618 *dev_id = ulp_ctx->cfg_data->dev_id;
625 /* Function to get the table scope id of the EEM table. */
627 bnxt_ulp_cntxt_tbl_scope_id_get(struct bnxt_ulp_context *ulp_ctx,
628 uint32_t *tbl_scope_id)
630 if (ulp_ctx && ulp_ctx->cfg_data) {
631 *tbl_scope_id = ulp_ctx->cfg_data->tbl_scope_id;
638 /* Function to set the table scope id of the EEM table. */
640 bnxt_ulp_cntxt_tbl_scope_id_set(struct bnxt_ulp_context *ulp_ctx,
641 uint32_t tbl_scope_id)
643 if (ulp_ctx && ulp_ctx->cfg_data) {
644 ulp_ctx->cfg_data->tbl_scope_id = tbl_scope_id;
651 /* Function to set the tfp session details from the ulp context. */
653 bnxt_ulp_cntxt_tfp_set(struct bnxt_ulp_context *ulp, struct tf *tfp)
656 BNXT_TF_DBG(ERR, "Invalid arguments\n");
660 /* TBD The tfp should be removed once tf_attach is implemented. */
665 /* Function to get the tfp session details from the ulp context. */
667 bnxt_ulp_cntxt_tfp_get(struct bnxt_ulp_context *ulp)
670 BNXT_TF_DBG(ERR, "Invalid arguments\n");
673 /* TBD The tfp should be removed once tf_attach is implemented. */
678 * Get the device table entry based on the device id.
680 * dev_id [in] The device id of the hardware
682 * Returns the pointer to the device parameters.
684 struct bnxt_ulp_device_params *
685 bnxt_ulp_device_params_get(uint32_t dev_id)
687 if (dev_id < BNXT_ULP_MAX_NUM_DEVICES)
688 return &ulp_device_params[dev_id];
692 /* Function to set the flow database to the ulp context. */
694 bnxt_ulp_cntxt_ptr2_flow_db_set(struct bnxt_ulp_context *ulp_ctx,
695 struct bnxt_ulp_flow_db *flow_db)
697 if (!ulp_ctx || !ulp_ctx->cfg_data)
700 ulp_ctx->cfg_data->flow_db = flow_db;
704 /* Function to get the flow database from the ulp context. */
705 struct bnxt_ulp_flow_db *
706 bnxt_ulp_cntxt_ptr2_flow_db_get(struct bnxt_ulp_context *ulp_ctx)
708 if (!ulp_ctx || !ulp_ctx->cfg_data)
711 return ulp_ctx->cfg_data->flow_db;
714 /* Function to get the ulp context from eth device. */
715 struct bnxt_ulp_context *
716 bnxt_ulp_eth_dev_ptr2_cntxt_get(struct rte_eth_dev *dev)
720 bp = (struct bnxt *)dev->data->dev_private;
722 BNXT_TF_DBG(ERR, "Bnxt private data is not initialized\n");
729 bnxt_ulp_cntxt_ptr2_mapper_data_set(struct bnxt_ulp_context *ulp_ctx,
732 if (!ulp_ctx || !ulp_ctx->cfg_data) {
733 BNXT_TF_DBG(ERR, "Invalid ulp context data\n");
737 ulp_ctx->cfg_data->mapper_data = mapper_data;
742 bnxt_ulp_cntxt_ptr2_mapper_data_get(struct bnxt_ulp_context *ulp_ctx)
744 if (!ulp_ctx || !ulp_ctx->cfg_data) {
745 BNXT_TF_DBG(ERR, "Invalid ulp context data\n");
749 return ulp_ctx->cfg_data->mapper_data;
752 /* Function to set the port database to the ulp context. */
754 bnxt_ulp_cntxt_ptr2_port_db_set(struct bnxt_ulp_context *ulp_ctx,
755 struct bnxt_ulp_port_db *port_db)
757 if (!ulp_ctx || !ulp_ctx->cfg_data)
760 ulp_ctx->cfg_data->port_db = port_db;
764 /* Function to get the port database from the ulp context. */
765 struct bnxt_ulp_port_db *
766 bnxt_ulp_cntxt_ptr2_port_db_get(struct bnxt_ulp_context *ulp_ctx)
768 if (!ulp_ctx || !ulp_ctx->cfg_data)
771 return ulp_ctx->cfg_data->port_db;