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"
23 /* Linked list of all TF sessions. */
24 STAILQ_HEAD(, bnxt_ulp_session_state) bnxt_ulp_session_list =
25 STAILQ_HEAD_INITIALIZER(bnxt_ulp_session_list);
27 /* Mutex to synchronize bnxt_ulp_session_list operations. */
28 static pthread_mutex_t bnxt_ulp_global_mutex = PTHREAD_MUTEX_INITIALIZER;
31 * Allow the deletion of context only for the bnxt device that
33 * TBD - The implementation of the function should change to
34 * using the reference count once tf_session_attach functionality
38 ulp_ctx_deinit_allowed(void *ptr)
40 struct bnxt *bp = (struct bnxt *)ptr;
45 if (&bp->tfp == bp->ulp_ctx.g_tfp)
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;
70 memset(¶ms, 0, sizeof(params));
72 rc = rte_eth_dev_get_name_by_port(ethdev->data->port_id,
73 params.ctrl_chan_name);
75 BNXT_TF_DBG(ERR, "Invalid port %d, rc = %d\n",
76 ethdev->data->port_id, rc);
80 rc = tf_open_session(&bp->tfp, ¶ms);
82 BNXT_TF_DBG(ERR, "Failed to open TF session - %s, rc = %d\n",
83 params.ctrl_chan_name, rc);
86 session->session_opened = 1;
87 session->g_tfp = &bp->tfp;
92 * Close the ULP session.
93 * It takes the ulp context pointer.
96 ulp_ctx_session_close(struct bnxt *bp,
97 struct bnxt_ulp_session_state *session)
99 /* close the session in the hardware */
100 if (session->session_opened)
101 tf_close_session(&bp->tfp);
102 session->session_opened = 0;
103 session->g_tfp = NULL;
104 bp->ulp_ctx.g_tfp = NULL;
108 bnxt_init_tbl_scope_parms(struct bnxt *bp,
109 struct tf_alloc_tbl_scope_parms *params)
111 struct bnxt_ulp_device_params *dparms;
115 rc = bnxt_ulp_cntxt_dev_id_get(&bp->ulp_ctx, &dev_id);
117 /* TBD: For now, just use default. */
120 dparms = bnxt_ulp_device_params_get(dev_id);
123 params->rx_max_key_sz_in_bits = BNXT_ULP_DFLT_RX_MAX_KEY;
124 params->rx_max_action_entry_sz_in_bits =
125 BNXT_ULP_DFLT_RX_MAX_ACTN_ENTRY;
126 params->rx_mem_size_in_mb = BNXT_ULP_DFLT_RX_MEM;
127 params->rx_num_flows_in_k = BNXT_ULP_RX_NUM_FLOWS;
128 params->rx_tbl_if_id = BNXT_ULP_RX_TBL_IF_ID;
130 params->tx_max_key_sz_in_bits = BNXT_ULP_DFLT_TX_MAX_KEY;
131 params->tx_max_action_entry_sz_in_bits =
132 BNXT_ULP_DFLT_TX_MAX_ACTN_ENTRY;
133 params->tx_mem_size_in_mb = BNXT_ULP_DFLT_TX_MEM;
134 params->tx_num_flows_in_k = BNXT_ULP_TX_NUM_FLOWS;
135 params->tx_tbl_if_id = BNXT_ULP_TX_TBL_IF_ID;
137 params->rx_max_key_sz_in_bits = BNXT_ULP_DFLT_RX_MAX_KEY;
138 params->rx_max_action_entry_sz_in_bits =
139 BNXT_ULP_DFLT_RX_MAX_ACTN_ENTRY;
140 params->rx_mem_size_in_mb = BNXT_ULP_DFLT_RX_MEM;
141 params->rx_num_flows_in_k = dparms->num_flows / (1024);
142 params->rx_tbl_if_id = BNXT_ULP_RX_TBL_IF_ID;
144 params->tx_max_key_sz_in_bits = BNXT_ULP_DFLT_TX_MAX_KEY;
145 params->tx_max_action_entry_sz_in_bits =
146 BNXT_ULP_DFLT_TX_MAX_ACTN_ENTRY;
147 params->tx_mem_size_in_mb = BNXT_ULP_DFLT_TX_MEM;
148 params->tx_num_flows_in_k = dparms->num_flows / (1024);
149 params->tx_tbl_if_id = BNXT_ULP_TX_TBL_IF_ID;
153 /* Initialize Extended Exact Match host memory. */
155 ulp_eem_tbl_scope_init(struct bnxt *bp)
157 struct tf_alloc_tbl_scope_parms params = {0};
160 bnxt_init_tbl_scope_parms(bp, ¶ms);
162 rc = tf_alloc_tbl_scope(&bp->tfp, ¶ms);
164 BNXT_TF_DBG(ERR, "Unable to allocate eem table scope rc = %d\n",
169 rc = bnxt_ulp_cntxt_tbl_scope_id_set(&bp->ulp_ctx, params.tbl_scope_id);
171 BNXT_TF_DBG(ERR, "Unable to set table scope id\n");
178 /* Free Extended Exact Match host memory */
180 ulp_eem_tbl_scope_deinit(struct bnxt *bp, struct bnxt_ulp_context *ulp_ctx)
182 struct tf_free_tbl_scope_parms params = {0};
186 if (!ulp_ctx || !ulp_ctx->cfg_data)
189 /* Free the resources for the last device */
190 if (!ulp_ctx_deinit_allowed(bp))
193 tfp = bnxt_ulp_cntxt_tfp_get(ulp_ctx);
195 BNXT_TF_DBG(ERR, "Failed to get the truflow pointer\n");
199 rc = bnxt_ulp_cntxt_tbl_scope_id_get(ulp_ctx, ¶ms.tbl_scope_id);
201 BNXT_TF_DBG(ERR, "Failed to get the table scope id\n");
205 rc = tf_free_tbl_scope(tfp, ¶ms);
207 BNXT_TF_DBG(ERR, "Unable to free table scope\n");
213 /* The function to free and deinit the ulp context data. */
215 ulp_ctx_deinit(struct bnxt *bp,
216 struct bnxt_ulp_session_state *session)
218 if (!session || !bp) {
219 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
223 /* close the tf session */
224 ulp_ctx_session_close(bp, session);
226 /* Free the contents */
227 if (session->cfg_data) {
228 rte_free(session->cfg_data);
229 bp->ulp_ctx.cfg_data = NULL;
230 session->cfg_data = NULL;
235 /* The function to allocate and initialize the ulp context data. */
237 ulp_ctx_init(struct bnxt *bp,
238 struct bnxt_ulp_session_state *session)
240 struct bnxt_ulp_data *ulp_data;
243 if (!session || !bp) {
244 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
248 /* Allocate memory to hold ulp context data. */
249 ulp_data = rte_zmalloc("bnxt_ulp_data",
250 sizeof(struct bnxt_ulp_data), 0);
252 BNXT_TF_DBG(ERR, "Failed to allocate memory for ulp data\n");
256 /* Increment the ulp context data reference count usage. */
257 bp->ulp_ctx.cfg_data = ulp_data;
258 session->cfg_data = ulp_data;
261 /* Open the ulp session. */
262 rc = ulp_ctx_session_open(bp, session);
264 (void)ulp_ctx_deinit(bp, session);
267 bnxt_ulp_cntxt_tfp_set(&bp->ulp_ctx, session->g_tfp);
272 ulp_ctx_attach(struct bnxt_ulp_context *ulp_ctx,
273 struct bnxt_ulp_session_state *session)
275 if (!ulp_ctx || !session) {
276 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
280 /* Increment the ulp context data reference count usage. */
281 ulp_ctx->cfg_data = session->cfg_data;
282 ulp_ctx->cfg_data->ref_cnt++;
284 /* TBD call TF_session_attach. */
285 ulp_ctx->g_tfp = session->g_tfp;
290 ulp_ctx_detach(struct bnxt *bp,
291 struct bnxt_ulp_session_state *session)
293 struct bnxt_ulp_context *ulp_ctx;
295 if (!bp || !session) {
296 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
299 ulp_ctx = &bp->ulp_ctx;
301 if (!ulp_ctx->cfg_data)
304 /* TBD call TF_session_detach */
306 /* Increment the ulp context data reference count usage. */
307 if (ulp_ctx->cfg_data->ref_cnt >= 1) {
308 ulp_ctx->cfg_data->ref_cnt--;
309 if (ulp_ctx_deinit_allowed(bp))
310 ulp_ctx_deinit(bp, session);
311 ulp_ctx->cfg_data = NULL;
312 ulp_ctx->g_tfp = NULL;
315 BNXT_TF_DBG(ERR, "context deatach on invalid data\n");
320 * Initialize the state of an ULP session.
321 * If the state of an ULP session is not initialized, set it's state to
322 * initialized. If the state is already initialized, do nothing.
325 ulp_context_initialized(struct bnxt_ulp_session_state *session, bool *init)
327 pthread_mutex_lock(&session->bnxt_ulp_mutex);
329 if (!session->bnxt_ulp_init) {
330 session->bnxt_ulp_init = true;
336 pthread_mutex_unlock(&session->bnxt_ulp_mutex);
340 * Check if an ULP session is already allocated for a specific PCI
341 * domain & bus. If it is already allocated simply return the session
342 * pointer, otherwise allocate a new session.
344 static struct bnxt_ulp_session_state *
345 ulp_get_session(struct rte_pci_addr *pci_addr)
347 struct bnxt_ulp_session_state *session;
349 STAILQ_FOREACH(session, &bnxt_ulp_session_list, next) {
350 if (session->pci_info.domain == pci_addr->domain &&
351 session->pci_info.bus == pci_addr->bus) {
359 * Allocate and Initialize an ULP session and set it's state to INITIALIZED.
360 * If it's already initialized simply return the already existing session.
362 static struct bnxt_ulp_session_state *
363 ulp_session_init(struct bnxt *bp,
366 struct rte_pci_device *pci_dev;
367 struct rte_pci_addr *pci_addr;
368 struct bnxt_ulp_session_state *session;
373 pci_dev = RTE_DEV_TO_PCI(bp->eth_dev->device);
374 pci_addr = &pci_dev->addr;
376 pthread_mutex_lock(&bnxt_ulp_global_mutex);
378 session = ulp_get_session(pci_addr);
380 /* Not Found the session Allocate a new one */
381 session = rte_zmalloc("bnxt_ulp_session",
382 sizeof(struct bnxt_ulp_session_state),
386 "Allocation failed for bnxt_ulp_session\n");
387 pthread_mutex_unlock(&bnxt_ulp_global_mutex);
391 /* Add it to the queue */
392 session->pci_info.domain = pci_addr->domain;
393 session->pci_info.bus = pci_addr->bus;
394 pthread_mutex_init(&session->bnxt_ulp_mutex, NULL);
395 STAILQ_INSERT_TAIL(&bnxt_ulp_session_list,
399 ulp_context_initialized(session, init);
400 pthread_mutex_unlock(&bnxt_ulp_global_mutex);
405 * When a device is closed, remove it's associated session from the global
409 ulp_session_deinit(struct bnxt_ulp_session_state *session)
414 if (!session->cfg_data) {
415 pthread_mutex_lock(&bnxt_ulp_global_mutex);
416 STAILQ_REMOVE(&bnxt_ulp_session_list, session,
417 bnxt_ulp_session_state, next);
418 pthread_mutex_destroy(&session->bnxt_ulp_mutex);
420 pthread_mutex_unlock(&bnxt_ulp_global_mutex);
425 * When a port is initialized by dpdk. This functions is called
426 * and this function initializes the ULP context and rest of the
427 * infrastructure associated with it.
430 bnxt_ulp_init(struct bnxt *bp)
432 struct bnxt_ulp_session_state *session;
437 * Multiple uplink ports can be associated with a single vswitch.
438 * Make sure only the port that is started first will initialize
441 session = ulp_session_init(bp, &init);
443 BNXT_TF_DBG(ERR, "Failed to initialize the tf session\n");
448 * If ULP is already initialized for a specific domain then simply
449 * assign the ulp context to this rte_eth_dev.
452 rc = ulp_ctx_attach(&bp->ulp_ctx, session);
455 "Failed to attach the ulp context\n");
460 /* Allocate and Initialize the ulp context. */
461 rc = ulp_ctx_init(bp, session);
463 BNXT_TF_DBG(ERR, "Failed to create the ulp context\n");
467 /* Create the Mark database. */
468 rc = ulp_mark_db_init(&bp->ulp_ctx);
470 BNXT_TF_DBG(ERR, "Failed to create the mark database\n");
474 /* Create the flow database. */
475 rc = ulp_flow_db_init(&bp->ulp_ctx);
477 BNXT_TF_DBG(ERR, "Failed to create the flow database\n");
481 /* Create the eem table scope. */
482 rc = ulp_eem_tbl_scope_init(bp);
484 BNXT_TF_DBG(ERR, "Failed to create the eem scope table\n");
495 /* Below are the access functions to access internal data of ulp context. */
498 * When a port is deinit'ed by dpdk. This function is called
499 * and this function clears the ULP context and rest of the
500 * infrastructure associated with it.
503 bnxt_ulp_deinit(struct bnxt *bp)
505 struct bnxt_ulp_session_state *session;
506 struct rte_pci_device *pci_dev;
507 struct rte_pci_addr *pci_addr;
509 /* Get the session first */
510 pci_dev = RTE_DEV_TO_PCI(bp->eth_dev->device);
511 pci_addr = &pci_dev->addr;
512 pthread_mutex_lock(&bnxt_ulp_global_mutex);
513 session = ulp_get_session(pci_addr);
514 pthread_mutex_unlock(&bnxt_ulp_global_mutex);
516 /* session not found then just exit */
520 /* cleanup the eem table scope */
521 ulp_eem_tbl_scope_deinit(bp, &bp->ulp_ctx);
523 /* cleanup the flow database */
524 ulp_flow_db_deinit(&bp->ulp_ctx);
526 /* Delete the Mark database */
527 ulp_mark_db_deinit(&bp->ulp_ctx);
529 /* Delete the ulp context and tf session */
530 ulp_ctx_detach(bp, session);
532 /* Finally delete the bnxt session*/
533 ulp_session_deinit(session);
536 /* Function to set the Mark DB into the context */
538 bnxt_ulp_cntxt_ptr2_mark_db_set(struct bnxt_ulp_context *ulp_ctx,
539 struct bnxt_ulp_mark_tbl *mark_tbl)
541 if (!ulp_ctx || !ulp_ctx->cfg_data) {
542 BNXT_TF_DBG(ERR, "Invalid ulp context data\n");
546 ulp_ctx->cfg_data->mark_tbl = mark_tbl;
551 /* Function to retrieve the Mark DB from the context. */
552 struct bnxt_ulp_mark_tbl *
553 bnxt_ulp_cntxt_ptr2_mark_db_get(struct bnxt_ulp_context *ulp_ctx)
555 if (!ulp_ctx || !ulp_ctx->cfg_data)
558 return ulp_ctx->cfg_data->mark_tbl;
561 /* Function to set the device id of the hardware. */
563 bnxt_ulp_cntxt_dev_id_set(struct bnxt_ulp_context *ulp_ctx,
566 if (ulp_ctx && ulp_ctx->cfg_data) {
567 ulp_ctx->cfg_data->dev_id = dev_id;
574 /* Function to get the device id of the hardware. */
576 bnxt_ulp_cntxt_dev_id_get(struct bnxt_ulp_context *ulp_ctx,
579 if (ulp_ctx && ulp_ctx->cfg_data) {
580 *dev_id = ulp_ctx->cfg_data->dev_id;
587 /* Function to get the table scope id of the EEM table. */
589 bnxt_ulp_cntxt_tbl_scope_id_get(struct bnxt_ulp_context *ulp_ctx,
590 uint32_t *tbl_scope_id)
592 if (ulp_ctx && ulp_ctx->cfg_data) {
593 *tbl_scope_id = ulp_ctx->cfg_data->tbl_scope_id;
600 /* Function to set the table scope id of the EEM table. */
602 bnxt_ulp_cntxt_tbl_scope_id_set(struct bnxt_ulp_context *ulp_ctx,
603 uint32_t tbl_scope_id)
605 if (ulp_ctx && ulp_ctx->cfg_data) {
606 ulp_ctx->cfg_data->tbl_scope_id = tbl_scope_id;
613 /* Function to set the tfp session details from the ulp context. */
615 bnxt_ulp_cntxt_tfp_set(struct bnxt_ulp_context *ulp, struct tf *tfp)
618 BNXT_TF_DBG(ERR, "Invalid arguments\n");
622 /* TBD The tfp should be removed once tf_attach is implemented. */
627 /* Function to get the tfp session details from the ulp context. */
629 bnxt_ulp_cntxt_tfp_get(struct bnxt_ulp_context *ulp)
632 BNXT_TF_DBG(ERR, "Invalid arguments\n");
635 /* TBD The tfp should be removed once tf_attach is implemented. */
640 * Get the device table entry based on the device id.
642 * dev_id [in] The device id of the hardware
644 * Returns the pointer to the device parameters.
646 struct bnxt_ulp_device_params *
647 bnxt_ulp_device_params_get(uint32_t dev_id)
649 if (dev_id < BNXT_ULP_MAX_NUM_DEVICES)
650 return &ulp_device_params[dev_id];
654 /* Function to set the flow database to the ulp context. */
656 bnxt_ulp_cntxt_ptr2_flow_db_set(struct bnxt_ulp_context *ulp_ctx,
657 struct bnxt_ulp_flow_db *flow_db)
659 if (!ulp_ctx || !ulp_ctx->cfg_data) {
660 BNXT_TF_DBG(ERR, "Invalid ulp context data\n");
664 ulp_ctx->cfg_data->flow_db = flow_db;
668 /* Function to get the flow database from the ulp context. */
669 struct bnxt_ulp_flow_db *
670 bnxt_ulp_cntxt_ptr2_flow_db_get(struct bnxt_ulp_context *ulp_ctx)
672 if (!ulp_ctx || !ulp_ctx->cfg_data) {
673 BNXT_TF_DBG(ERR, "Invalid ulp context data\n");
677 return ulp_ctx->cfg_data->flow_db;
680 /* Function to get the ulp context from eth device. */
681 struct bnxt_ulp_context *
682 bnxt_ulp_eth_dev_ptr2_cntxt_get(struct rte_eth_dev *dev)
686 bp = (struct bnxt *)dev->data->dev_private;
688 BNXT_TF_DBG(ERR, "Bnxt private data is not initialized\n");