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"
24 /* Linked list of all TF sessions. */
25 STAILQ_HEAD(, bnxt_ulp_session_state) bnxt_ulp_session_list =
26 STAILQ_HEAD_INITIALIZER(bnxt_ulp_session_list);
28 /* Mutex to synchronize bnxt_ulp_session_list operations. */
29 static pthread_mutex_t bnxt_ulp_global_mutex = PTHREAD_MUTEX_INITIALIZER;
32 * Allow the deletion of context only for the bnxt device that
34 * TBD - The implementation of the function should change to
35 * using the reference count once tf_session_attach functionality
39 ulp_ctx_deinit_allowed(void *ptr)
41 struct bnxt *bp = (struct bnxt *)ptr;
46 if (&bp->tfp == bp->ulp_ctx.g_tfp)
53 * Initialize an ULP session.
54 * An ULP session will contain all the resources needed to support rte flow
55 * offloads. A session is initialized as part of rte_eth_device start.
56 * A single vswitch instance can have multiple uplinks which means
57 * rte_eth_device start will be called for each of these devices.
58 * ULP session manager will make sure that a single ULP session is only
59 * initialized once. Apart from this, it also initializes MARK database,
60 * EEM table & flow database. ULP session manager also manages a list of
61 * all opened ULP sessions.
64 ulp_ctx_session_open(struct bnxt *bp,
65 struct bnxt_ulp_session_state *session)
67 struct rte_eth_dev *ethdev = bp->eth_dev;
69 struct tf_open_session_parms params;
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 rc = tf_open_session(&bp->tfp, ¶ms);
83 BNXT_TF_DBG(ERR, "Failed to open TF session - %s, rc = %d\n",
84 params.ctrl_chan_name, rc);
87 session->session_opened = 1;
88 session->g_tfp = &bp->tfp;
93 * Close the ULP session.
94 * It takes the ulp context pointer.
97 ulp_ctx_session_close(struct bnxt *bp,
98 struct bnxt_ulp_session_state *session)
100 /* close the session in the hardware */
101 if (session->session_opened)
102 tf_close_session(&bp->tfp);
103 session->session_opened = 0;
104 session->g_tfp = NULL;
105 bp->ulp_ctx.g_tfp = NULL;
109 bnxt_init_tbl_scope_parms(struct bnxt *bp,
110 struct tf_alloc_tbl_scope_parms *params)
112 struct bnxt_ulp_device_params *dparms;
116 rc = bnxt_ulp_cntxt_dev_id_get(&bp->ulp_ctx, &dev_id);
118 /* TBD: For now, just use default. */
121 dparms = bnxt_ulp_device_params_get(dev_id);
124 params->rx_max_key_sz_in_bits = BNXT_ULP_DFLT_RX_MAX_KEY;
125 params->rx_max_action_entry_sz_in_bits =
126 BNXT_ULP_DFLT_RX_MAX_ACTN_ENTRY;
127 params->rx_mem_size_in_mb = BNXT_ULP_DFLT_RX_MEM;
128 params->rx_num_flows_in_k = BNXT_ULP_RX_NUM_FLOWS;
129 params->rx_tbl_if_id = BNXT_ULP_RX_TBL_IF_ID;
131 params->tx_max_key_sz_in_bits = BNXT_ULP_DFLT_TX_MAX_KEY;
132 params->tx_max_action_entry_sz_in_bits =
133 BNXT_ULP_DFLT_TX_MAX_ACTN_ENTRY;
134 params->tx_mem_size_in_mb = BNXT_ULP_DFLT_TX_MEM;
135 params->tx_num_flows_in_k = BNXT_ULP_TX_NUM_FLOWS;
136 params->tx_tbl_if_id = BNXT_ULP_TX_TBL_IF_ID;
138 params->rx_max_key_sz_in_bits = BNXT_ULP_DFLT_RX_MAX_KEY;
139 params->rx_max_action_entry_sz_in_bits =
140 BNXT_ULP_DFLT_RX_MAX_ACTN_ENTRY;
141 params->rx_mem_size_in_mb = BNXT_ULP_DFLT_RX_MEM;
142 params->rx_num_flows_in_k = dparms->num_flows / (1024);
143 params->rx_tbl_if_id = BNXT_ULP_RX_TBL_IF_ID;
145 params->tx_max_key_sz_in_bits = BNXT_ULP_DFLT_TX_MAX_KEY;
146 params->tx_max_action_entry_sz_in_bits =
147 BNXT_ULP_DFLT_TX_MAX_ACTN_ENTRY;
148 params->tx_mem_size_in_mb = BNXT_ULP_DFLT_TX_MEM;
149 params->tx_num_flows_in_k = dparms->num_flows / (1024);
150 params->tx_tbl_if_id = BNXT_ULP_TX_TBL_IF_ID;
154 /* Initialize Extended Exact Match host memory. */
156 ulp_eem_tbl_scope_init(struct bnxt *bp)
158 struct tf_alloc_tbl_scope_parms params = {0};
161 bnxt_init_tbl_scope_parms(bp, ¶ms);
163 rc = tf_alloc_tbl_scope(&bp->tfp, ¶ms);
165 BNXT_TF_DBG(ERR, "Unable to allocate eem table scope rc = %d\n",
170 rc = bnxt_ulp_cntxt_tbl_scope_id_set(&bp->ulp_ctx, params.tbl_scope_id);
172 BNXT_TF_DBG(ERR, "Unable to set table scope id\n");
179 /* Free Extended Exact Match host memory */
181 ulp_eem_tbl_scope_deinit(struct bnxt *bp, struct bnxt_ulp_context *ulp_ctx)
183 struct tf_free_tbl_scope_parms params = {0};
187 if (!ulp_ctx || !ulp_ctx->cfg_data)
190 /* Free the resources for the last device */
191 if (!ulp_ctx_deinit_allowed(bp))
194 tfp = bnxt_ulp_cntxt_tfp_get(ulp_ctx);
196 BNXT_TF_DBG(ERR, "Failed to get the truflow pointer\n");
200 rc = bnxt_ulp_cntxt_tbl_scope_id_get(ulp_ctx, ¶ms.tbl_scope_id);
202 BNXT_TF_DBG(ERR, "Failed to get the table scope id\n");
206 rc = tf_free_tbl_scope(tfp, ¶ms);
208 BNXT_TF_DBG(ERR, "Unable to free table scope\n");
214 /* The function to free and deinit the ulp context data. */
216 ulp_ctx_deinit(struct bnxt *bp,
217 struct bnxt_ulp_session_state *session)
219 if (!session || !bp) {
220 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
224 /* close the tf session */
225 ulp_ctx_session_close(bp, session);
227 /* Free the contents */
228 if (session->cfg_data) {
229 rte_free(session->cfg_data);
230 bp->ulp_ctx.cfg_data = NULL;
231 session->cfg_data = NULL;
236 /* The function to allocate and initialize the ulp context data. */
238 ulp_ctx_init(struct bnxt *bp,
239 struct bnxt_ulp_session_state *session)
241 struct bnxt_ulp_data *ulp_data;
244 if (!session || !bp) {
245 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
249 /* Allocate memory to hold ulp context data. */
250 ulp_data = rte_zmalloc("bnxt_ulp_data",
251 sizeof(struct bnxt_ulp_data), 0);
253 BNXT_TF_DBG(ERR, "Failed to allocate memory for ulp data\n");
257 /* Increment the ulp context data reference count usage. */
258 bp->ulp_ctx.cfg_data = ulp_data;
259 session->cfg_data = ulp_data;
262 /* Open the ulp session. */
263 rc = ulp_ctx_session_open(bp, session);
265 (void)ulp_ctx_deinit(bp, session);
268 bnxt_ulp_cntxt_tfp_set(&bp->ulp_ctx, session->g_tfp);
273 ulp_ctx_attach(struct bnxt_ulp_context *ulp_ctx,
274 struct bnxt_ulp_session_state *session)
276 if (!ulp_ctx || !session) {
277 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
281 /* Increment the ulp context data reference count usage. */
282 ulp_ctx->cfg_data = session->cfg_data;
283 ulp_ctx->cfg_data->ref_cnt++;
285 /* TBD call TF_session_attach. */
286 ulp_ctx->g_tfp = session->g_tfp;
291 ulp_ctx_detach(struct bnxt *bp,
292 struct bnxt_ulp_session_state *session)
294 struct bnxt_ulp_context *ulp_ctx;
296 if (!bp || !session) {
297 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
300 ulp_ctx = &bp->ulp_ctx;
302 if (!ulp_ctx->cfg_data)
305 /* TBD call TF_session_detach */
307 /* Increment the ulp context data reference count usage. */
308 if (ulp_ctx->cfg_data->ref_cnt >= 1) {
309 ulp_ctx->cfg_data->ref_cnt--;
310 if (ulp_ctx_deinit_allowed(bp))
311 ulp_ctx_deinit(bp, session);
312 ulp_ctx->cfg_data = NULL;
313 ulp_ctx->g_tfp = NULL;
316 BNXT_TF_DBG(ERR, "context deatach on invalid data\n");
321 * Initialize the state of an ULP session.
322 * If the state of an ULP session is not initialized, set it's state to
323 * initialized. If the state is already initialized, do nothing.
326 ulp_context_initialized(struct bnxt_ulp_session_state *session, bool *init)
328 pthread_mutex_lock(&session->bnxt_ulp_mutex);
330 if (!session->bnxt_ulp_init) {
331 session->bnxt_ulp_init = true;
337 pthread_mutex_unlock(&session->bnxt_ulp_mutex);
341 * Check if an ULP session is already allocated for a specific PCI
342 * domain & bus. If it is already allocated simply return the session
343 * pointer, otherwise allocate a new session.
345 static struct bnxt_ulp_session_state *
346 ulp_get_session(struct rte_pci_addr *pci_addr)
348 struct bnxt_ulp_session_state *session;
350 STAILQ_FOREACH(session, &bnxt_ulp_session_list, next) {
351 if (session->pci_info.domain == pci_addr->domain &&
352 session->pci_info.bus == pci_addr->bus) {
360 * Allocate and Initialize an ULP session and set it's state to INITIALIZED.
361 * If it's already initialized simply return the already existing session.
363 static struct bnxt_ulp_session_state *
364 ulp_session_init(struct bnxt *bp,
367 struct rte_pci_device *pci_dev;
368 struct rte_pci_addr *pci_addr;
369 struct bnxt_ulp_session_state *session;
374 pci_dev = RTE_DEV_TO_PCI(bp->eth_dev->device);
375 pci_addr = &pci_dev->addr;
377 pthread_mutex_lock(&bnxt_ulp_global_mutex);
379 session = ulp_get_session(pci_addr);
381 /* Not Found the session Allocate a new one */
382 session = rte_zmalloc("bnxt_ulp_session",
383 sizeof(struct bnxt_ulp_session_state),
387 "Allocation failed for bnxt_ulp_session\n");
388 pthread_mutex_unlock(&bnxt_ulp_global_mutex);
392 /* Add it to the queue */
393 session->pci_info.domain = pci_addr->domain;
394 session->pci_info.bus = pci_addr->bus;
395 pthread_mutex_init(&session->bnxt_ulp_mutex, NULL);
396 STAILQ_INSERT_TAIL(&bnxt_ulp_session_list,
400 ulp_context_initialized(session, init);
401 pthread_mutex_unlock(&bnxt_ulp_global_mutex);
406 * When a device is closed, remove it's associated session from the global
410 ulp_session_deinit(struct bnxt_ulp_session_state *session)
415 if (!session->cfg_data) {
416 pthread_mutex_lock(&bnxt_ulp_global_mutex);
417 STAILQ_REMOVE(&bnxt_ulp_session_list, session,
418 bnxt_ulp_session_state, next);
419 pthread_mutex_destroy(&session->bnxt_ulp_mutex);
421 pthread_mutex_unlock(&bnxt_ulp_global_mutex);
426 * When a port is initialized by dpdk. This functions is called
427 * and this function initializes the ULP context and rest of the
428 * infrastructure associated with it.
431 bnxt_ulp_init(struct bnxt *bp)
433 struct bnxt_ulp_session_state *session;
438 * Multiple uplink ports can be associated with a single vswitch.
439 * Make sure only the port that is started first will initialize
442 session = ulp_session_init(bp, &init);
444 BNXT_TF_DBG(ERR, "Failed to initialize the tf session\n");
449 * If ULP is already initialized for a specific domain then simply
450 * assign the ulp context to this rte_eth_dev.
453 rc = ulp_ctx_attach(&bp->ulp_ctx, session);
456 "Failed to attach the ulp context\n");
461 /* Allocate and Initialize the ulp context. */
462 rc = ulp_ctx_init(bp, session);
464 BNXT_TF_DBG(ERR, "Failed to create the ulp context\n");
468 /* Create the Mark database. */
469 rc = ulp_mark_db_init(&bp->ulp_ctx);
471 BNXT_TF_DBG(ERR, "Failed to create the mark database\n");
475 /* Create the flow database. */
476 rc = ulp_flow_db_init(&bp->ulp_ctx);
478 BNXT_TF_DBG(ERR, "Failed to create the flow database\n");
482 /* Create the eem table scope. */
483 rc = ulp_eem_tbl_scope_init(bp);
485 BNXT_TF_DBG(ERR, "Failed to create the eem scope table\n");
489 rc = ulp_mapper_init(&bp->ulp_ctx);
491 BNXT_TF_DBG(ERR, "Failed to initialize ulp mapper\n");
502 /* Below are the access functions to access internal data of ulp context. */
505 * When a port is deinit'ed by dpdk. This function is called
506 * and this function clears the ULP context and rest of the
507 * infrastructure associated with it.
510 bnxt_ulp_deinit(struct bnxt *bp)
512 struct bnxt_ulp_session_state *session;
513 struct rte_pci_device *pci_dev;
514 struct rte_pci_addr *pci_addr;
516 /* Get the session first */
517 pci_dev = RTE_DEV_TO_PCI(bp->eth_dev->device);
518 pci_addr = &pci_dev->addr;
519 pthread_mutex_lock(&bnxt_ulp_global_mutex);
520 session = ulp_get_session(pci_addr);
521 pthread_mutex_unlock(&bnxt_ulp_global_mutex);
523 /* session not found then just exit */
527 /* clean up regular flows */
528 ulp_flow_db_flush_flows(&bp->ulp_ctx, BNXT_ULP_REGULAR_FLOW_TABLE);
530 /* cleanup the eem table scope */
531 ulp_eem_tbl_scope_deinit(bp, &bp->ulp_ctx);
533 /* cleanup the flow database */
534 ulp_flow_db_deinit(&bp->ulp_ctx);
536 /* Delete the Mark database */
537 ulp_mark_db_deinit(&bp->ulp_ctx);
539 /* cleanup the ulp mapper */
540 ulp_mapper_deinit(&bp->ulp_ctx);
542 /* Delete the ulp context and tf session */
543 ulp_ctx_detach(bp, session);
545 /* Finally delete the bnxt session*/
546 ulp_session_deinit(session);
549 /* Function to set the Mark DB into the context */
551 bnxt_ulp_cntxt_ptr2_mark_db_set(struct bnxt_ulp_context *ulp_ctx,
552 struct bnxt_ulp_mark_tbl *mark_tbl)
554 if (!ulp_ctx || !ulp_ctx->cfg_data) {
555 BNXT_TF_DBG(ERR, "Invalid ulp context data\n");
559 ulp_ctx->cfg_data->mark_tbl = mark_tbl;
564 /* Function to retrieve the Mark DB from the context. */
565 struct bnxt_ulp_mark_tbl *
566 bnxt_ulp_cntxt_ptr2_mark_db_get(struct bnxt_ulp_context *ulp_ctx)
568 if (!ulp_ctx || !ulp_ctx->cfg_data)
571 return ulp_ctx->cfg_data->mark_tbl;
574 /* Function to set the device id of the hardware. */
576 bnxt_ulp_cntxt_dev_id_set(struct bnxt_ulp_context *ulp_ctx,
579 if (ulp_ctx && ulp_ctx->cfg_data) {
580 ulp_ctx->cfg_data->dev_id = dev_id;
587 /* Function to get the device id of the hardware. */
589 bnxt_ulp_cntxt_dev_id_get(struct bnxt_ulp_context *ulp_ctx,
592 if (ulp_ctx && ulp_ctx->cfg_data) {
593 *dev_id = ulp_ctx->cfg_data->dev_id;
600 /* Function to get the table scope id of the EEM table. */
602 bnxt_ulp_cntxt_tbl_scope_id_get(struct bnxt_ulp_context *ulp_ctx,
603 uint32_t *tbl_scope_id)
605 if (ulp_ctx && ulp_ctx->cfg_data) {
606 *tbl_scope_id = ulp_ctx->cfg_data->tbl_scope_id;
613 /* Function to set the table scope id of the EEM table. */
615 bnxt_ulp_cntxt_tbl_scope_id_set(struct bnxt_ulp_context *ulp_ctx,
616 uint32_t tbl_scope_id)
618 if (ulp_ctx && ulp_ctx->cfg_data) {
619 ulp_ctx->cfg_data->tbl_scope_id = tbl_scope_id;
626 /* Function to set the tfp session details from the ulp context. */
628 bnxt_ulp_cntxt_tfp_set(struct bnxt_ulp_context *ulp, struct tf *tfp)
631 BNXT_TF_DBG(ERR, "Invalid arguments\n");
635 /* TBD The tfp should be removed once tf_attach is implemented. */
640 /* Function to get the tfp session details from the ulp context. */
642 bnxt_ulp_cntxt_tfp_get(struct bnxt_ulp_context *ulp)
645 BNXT_TF_DBG(ERR, "Invalid arguments\n");
648 /* TBD The tfp should be removed once tf_attach is implemented. */
653 * Get the device table entry based on the device id.
655 * dev_id [in] The device id of the hardware
657 * Returns the pointer to the device parameters.
659 struct bnxt_ulp_device_params *
660 bnxt_ulp_device_params_get(uint32_t dev_id)
662 if (dev_id < BNXT_ULP_MAX_NUM_DEVICES)
663 return &ulp_device_params[dev_id];
667 /* Function to set the flow database to the ulp context. */
669 bnxt_ulp_cntxt_ptr2_flow_db_set(struct bnxt_ulp_context *ulp_ctx,
670 struct bnxt_ulp_flow_db *flow_db)
672 if (!ulp_ctx || !ulp_ctx->cfg_data)
675 ulp_ctx->cfg_data->flow_db = flow_db;
679 /* Function to get the flow database from the ulp context. */
680 struct bnxt_ulp_flow_db *
681 bnxt_ulp_cntxt_ptr2_flow_db_get(struct bnxt_ulp_context *ulp_ctx)
683 if (!ulp_ctx || !ulp_ctx->cfg_data)
686 return ulp_ctx->cfg_data->flow_db;
689 /* Function to get the ulp context from eth device. */
690 struct bnxt_ulp_context *
691 bnxt_ulp_eth_dev_ptr2_cntxt_get(struct rte_eth_dev *dev)
695 bp = (struct bnxt *)dev->data->dev_private;
697 BNXT_TF_DBG(ERR, "Bnxt private data is not initialized\n");
704 bnxt_ulp_cntxt_ptr2_mapper_data_set(struct bnxt_ulp_context *ulp_ctx,
707 if (!ulp_ctx || !ulp_ctx->cfg_data) {
708 BNXT_TF_DBG(ERR, "Invalid ulp context data\n");
712 ulp_ctx->cfg_data->mapper_data = mapper_data;
717 bnxt_ulp_cntxt_ptr2_mapper_data_get(struct bnxt_ulp_context *ulp_ctx)
719 if (!ulp_ctx || !ulp_ctx->cfg_data) {
720 BNXT_TF_DBG(ERR, "Invalid ulp context data\n");
724 return ulp_ctx->cfg_data->mapper_data;