net/bnxt: fix allocation of ULP context
[dpdk.git] / drivers / net / bnxt / tf_ulp / bnxt_ulp.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2019-2020 Broadcom
3  * All rights reserved.
4  */
5
6 #include <rte_log.h>
7 #include <rte_malloc.h>
8 #include <rte_flow.h>
9 #include <rte_flow_driver.h>
10 #include <rte_tailq.h>
11
12 #include "bnxt_ulp.h"
13 #include "bnxt_tf_common.h"
14 #include "bnxt.h"
15 #include "tf_core.h"
16 #include "tf_ext_flow_handle.h"
17
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"
24
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);
28
29 /* Mutex to synchronize bnxt_ulp_session_list operations. */
30 static pthread_mutex_t bnxt_ulp_global_mutex = PTHREAD_MUTEX_INITIALIZER;
31
32 /*
33  * Allow the deletion of context only for the bnxt device that
34  * created the session
35  * TBD - The implementation of the function should change to
36  * using the reference count once tf_session_attach functionality
37  * is fixed.
38  */
39 bool
40 ulp_ctx_deinit_allowed(void *ptr)
41 {
42         struct bnxt *bp = (struct bnxt *)ptr;
43
44         if (!bp)
45                 return 0;
46
47         if (&bp->tfp == bp->ulp_ctx->g_tfp)
48                 return 1;
49
50         return 0;
51 }
52
53 /*
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.
63  */
64 static int32_t
65 ulp_ctx_session_open(struct bnxt *bp,
66                      struct bnxt_ulp_session_state *session)
67 {
68         struct rte_eth_dev              *ethdev = bp->eth_dev;
69         int32_t                         rc = 0;
70         struct tf_open_session_parms    params;
71
72         memset(&params, 0, sizeof(params));
73
74         rc = rte_eth_dev_get_name_by_port(ethdev->data->port_id,
75                                           params.ctrl_chan_name);
76         if (rc) {
77                 BNXT_TF_DBG(ERR, "Invalid port %d, rc = %d\n",
78                             ethdev->data->port_id, rc);
79                 return rc;
80         }
81
82         rc = tf_open_session(&bp->tfp, &params);
83         if (rc) {
84                 BNXT_TF_DBG(ERR, "Failed to open TF session - %s, rc = %d\n",
85                             params.ctrl_chan_name, rc);
86                 return -EINVAL;
87         }
88         session->session_opened = 1;
89         session->g_tfp = &bp->tfp;
90         return rc;
91 }
92
93 /*
94  * Close the ULP session.
95  * It takes the ulp context pointer.
96  */
97 static void
98 ulp_ctx_session_close(struct bnxt *bp,
99                       struct bnxt_ulp_session_state *session)
100 {
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;
107 }
108
109 static void
110 bnxt_init_tbl_scope_parms(struct bnxt *bp,
111                           struct tf_alloc_tbl_scope_parms *params)
112 {
113         struct bnxt_ulp_device_params   *dparms;
114         uint32_t dev_id;
115         int rc;
116
117         rc = bnxt_ulp_cntxt_dev_id_get(bp->ulp_ctx, &dev_id);
118         if (rc)
119                 /* TBD: For now, just use default. */
120                 dparms = 0;
121         else
122                 dparms = bnxt_ulp_device_params_get(dev_id);
123
124         /*
125          * Set the flush timer for EEM entries. The value is in 100ms intervals,
126          * so 100 is 10s.
127          */
128         params->hw_flow_cache_flush_timer = 100;
129
130         if (!dparms) {
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;
137
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;
144         } else {
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;
151
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;
158         }
159 }
160
161 /* Initialize Extended Exact Match host memory. */
162 static int32_t
163 ulp_eem_tbl_scope_init(struct bnxt *bp)
164 {
165         struct tf_alloc_tbl_scope_parms params = {0};
166         int rc;
167
168         bnxt_init_tbl_scope_parms(bp, &params);
169
170         rc = tf_alloc_tbl_scope(&bp->tfp, &params);
171         if (rc) {
172                 BNXT_TF_DBG(ERR, "Unable to allocate eem table scope rc = %d\n",
173                             rc);
174                 return rc;
175         }
176
177         rc = bnxt_ulp_cntxt_tbl_scope_id_set(bp->ulp_ctx, params.tbl_scope_id);
178         if (rc) {
179                 BNXT_TF_DBG(ERR, "Unable to set table scope id\n");
180                 return rc;
181         }
182
183         return 0;
184 }
185
186 /* Free Extended Exact Match host memory */
187 static int32_t
188 ulp_eem_tbl_scope_deinit(struct bnxt *bp, struct bnxt_ulp_context *ulp_ctx)
189 {
190         struct tf_free_tbl_scope_parms  params = {0};
191         struct tf                       *tfp;
192         int32_t                         rc = 0;
193
194         if (!ulp_ctx || !ulp_ctx->cfg_data)
195                 return -EINVAL;
196
197         /* Free the resources for the last device */
198         if (!ulp_ctx_deinit_allowed(bp))
199                 return rc;
200
201         tfp = bnxt_ulp_cntxt_tfp_get(ulp_ctx);
202         if (!tfp) {
203                 BNXT_TF_DBG(ERR, "Failed to get the truflow pointer\n");
204                 return -EINVAL;
205         }
206
207         rc = bnxt_ulp_cntxt_tbl_scope_id_get(ulp_ctx, &params.tbl_scope_id);
208         if (rc) {
209                 BNXT_TF_DBG(ERR, "Failed to get the table scope id\n");
210                 return -EINVAL;
211         }
212
213         rc = tf_free_tbl_scope(tfp, &params);
214         if (rc) {
215                 BNXT_TF_DBG(ERR, "Unable to free table scope\n");
216                 return -EINVAL;
217         }
218         return rc;
219 }
220
221 /* The function to free and deinit the ulp context data. */
222 static int32_t
223 ulp_ctx_deinit(struct bnxt *bp,
224                struct bnxt_ulp_session_state *session)
225 {
226         if (!session || !bp) {
227                 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
228                 return -EINVAL;
229         }
230
231         /* close the tf session */
232         ulp_ctx_session_close(bp, session);
233
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;
239         }
240         return 0;
241 }
242
243 /* The function to allocate and initialize the ulp context data. */
244 static int32_t
245 ulp_ctx_init(struct bnxt *bp,
246              struct bnxt_ulp_session_state *session)
247 {
248         struct bnxt_ulp_data    *ulp_data;
249         int32_t                 rc = 0;
250
251         if (!session || !bp) {
252                 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
253                 return -EINVAL;
254         }
255
256         /* Allocate memory to hold ulp context data. */
257         ulp_data = rte_zmalloc("bnxt_ulp_data",
258                                sizeof(struct bnxt_ulp_data), 0);
259         if (!ulp_data) {
260                 BNXT_TF_DBG(ERR, "Failed to allocate memory for ulp data\n");
261                 return -ENOMEM;
262         }
263
264         /* Increment the ulp context data reference count usage. */
265         bp->ulp_ctx->cfg_data = ulp_data;
266         session->cfg_data = ulp_data;
267         ulp_data->ref_cnt++;
268
269         /* Open the ulp session. */
270         rc = ulp_ctx_session_open(bp, session);
271         if (rc) {
272                 (void)ulp_ctx_deinit(bp, session);
273                 return rc;
274         }
275         bnxt_ulp_cntxt_tfp_set(bp->ulp_ctx, session->g_tfp);
276         return rc;
277 }
278
279 static int32_t
280 ulp_ctx_attach(struct bnxt_ulp_context *ulp_ctx,
281                struct bnxt_ulp_session_state *session)
282 {
283         if (!ulp_ctx || !session) {
284                 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
285                 return -EINVAL;
286         }
287
288         /* Increment the ulp context data reference count usage. */
289         ulp_ctx->cfg_data = session->cfg_data;
290         ulp_ctx->cfg_data->ref_cnt++;
291
292         /* TBD call TF_session_attach. */
293         ulp_ctx->g_tfp = session->g_tfp;
294         return 0;
295 }
296
297 static int32_t
298 ulp_ctx_detach(struct bnxt *bp,
299                struct bnxt_ulp_session_state *session)
300 {
301         struct bnxt_ulp_context *ulp_ctx;
302
303         if (!bp || !session) {
304                 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
305                 return -EINVAL;
306         }
307         ulp_ctx = bp->ulp_ctx;
308
309         if (!ulp_ctx->cfg_data)
310                 return 0;
311
312         /* TBD call TF_session_detach */
313
314         /* Increment the ulp context data reference count usage. */
315         if (ulp_ctx->cfg_data->ref_cnt >= 1) {
316                 ulp_ctx->cfg_data->ref_cnt--;
317                 if (ulp_ctx_deinit_allowed(bp))
318                         ulp_ctx_deinit(bp, session);
319                 ulp_ctx->cfg_data = NULL;
320                 ulp_ctx->g_tfp = NULL;
321                 return 0;
322         }
323         BNXT_TF_DBG(ERR, "context deatach on invalid data\n");
324         return 0;
325 }
326
327 /*
328  * Initialize the state of an ULP session.
329  * If the state of an ULP session is not initialized, set it's state to
330  * initialized. If the state is already initialized, do nothing.
331  */
332 static void
333 ulp_context_initialized(struct bnxt_ulp_session_state *session, bool *init)
334 {
335         pthread_mutex_lock(&session->bnxt_ulp_mutex);
336
337         if (!session->bnxt_ulp_init) {
338                 session->bnxt_ulp_init = true;
339                 *init = false;
340         } else {
341                 *init = true;
342         }
343
344         pthread_mutex_unlock(&session->bnxt_ulp_mutex);
345 }
346
347 /*
348  * Check if an ULP session is already allocated for a specific PCI
349  * domain & bus. If it is already allocated simply return the session
350  * pointer, otherwise allocate a new session.
351  */
352 static struct bnxt_ulp_session_state *
353 ulp_get_session(struct rte_pci_addr *pci_addr)
354 {
355         struct bnxt_ulp_session_state *session;
356
357         STAILQ_FOREACH(session, &bnxt_ulp_session_list, next) {
358                 if (session->pci_info.domain == pci_addr->domain &&
359                     session->pci_info.bus == pci_addr->bus) {
360                         return session;
361                 }
362         }
363         return NULL;
364 }
365
366 /*
367  * Allocate and Initialize an ULP session and set it's state to INITIALIZED.
368  * If it's already initialized simply return the already existing session.
369  */
370 static struct bnxt_ulp_session_state *
371 ulp_session_init(struct bnxt *bp,
372                  bool *init)
373 {
374         struct rte_pci_device           *pci_dev;
375         struct rte_pci_addr             *pci_addr;
376         struct bnxt_ulp_session_state   *session;
377
378         if (!bp)
379                 return NULL;
380
381         pci_dev = RTE_DEV_TO_PCI(bp->eth_dev->device);
382         pci_addr = &pci_dev->addr;
383
384         pthread_mutex_lock(&bnxt_ulp_global_mutex);
385
386         session = ulp_get_session(pci_addr);
387         if (!session) {
388                 /* Not Found the session  Allocate a new one */
389                 session = rte_zmalloc("bnxt_ulp_session",
390                                       sizeof(struct bnxt_ulp_session_state),
391                                       0);
392                 if (!session) {
393                         BNXT_TF_DBG(ERR,
394                                     "Allocation failed for bnxt_ulp_session\n");
395                         pthread_mutex_unlock(&bnxt_ulp_global_mutex);
396                         return NULL;
397
398                 } else {
399                         /* Add it to the queue */
400                         session->pci_info.domain = pci_addr->domain;
401                         session->pci_info.bus = pci_addr->bus;
402                         pthread_mutex_init(&session->bnxt_ulp_mutex, NULL);
403                         STAILQ_INSERT_TAIL(&bnxt_ulp_session_list,
404                                            session, next);
405                 }
406         }
407         ulp_context_initialized(session, init);
408         pthread_mutex_unlock(&bnxt_ulp_global_mutex);
409         return session;
410 }
411
412 /*
413  * When a device is closed, remove it's associated session from the global
414  * session list.
415  */
416 static void
417 ulp_session_deinit(struct bnxt_ulp_session_state *session)
418 {
419         if (!session)
420                 return;
421
422         if (!session->cfg_data) {
423                 pthread_mutex_lock(&bnxt_ulp_global_mutex);
424                 STAILQ_REMOVE(&bnxt_ulp_session_list, session,
425                               bnxt_ulp_session_state, next);
426                 pthread_mutex_destroy(&session->bnxt_ulp_mutex);
427                 rte_free(session);
428                 pthread_mutex_unlock(&bnxt_ulp_global_mutex);
429         }
430 }
431
432 /*
433  * When a port is initialized by dpdk. This functions is called
434  * and this function initializes the ULP context and rest of the
435  * infrastructure associated with it.
436  */
437 int32_t
438 bnxt_ulp_init(struct bnxt *bp)
439 {
440         struct bnxt_ulp_session_state *session;
441         bool init;
442         int rc;
443
444         if (bp->ulp_ctx) {
445                 BNXT_TF_DBG(ERR, "ulp ctx already allocated\n");
446                 return -EINVAL;
447         }
448
449         /*
450          * Multiple uplink ports can be associated with a single vswitch.
451          * Make sure only the port that is started first will initialize
452          * the TF session.
453          */
454         session = ulp_session_init(bp, &init);
455         if (!session) {
456                 BNXT_TF_DBG(ERR, "Failed to initialize the tf session\n");
457                 return -EINVAL;
458         }
459
460         bp->ulp_ctx = rte_zmalloc("bnxt_ulp_ctx",
461                                   sizeof(struct bnxt_ulp_context), 0);
462         if (!bp->ulp_ctx) {
463                 BNXT_TF_DBG(ERR, "Failed to allocate ulp ctx\n");
464                 ulp_session_deinit(session);
465                 return -ENOMEM;
466         }
467
468         /*
469          * If ULP is already initialized for a specific domain then simply
470          * assign the ulp context to this rte_eth_dev.
471          */
472         if (init) {
473                 rc = ulp_ctx_attach(bp->ulp_ctx, session);
474                 if (rc) {
475                         BNXT_TF_DBG(ERR,
476                                     "Failed to attach the ulp context\n");
477                         ulp_session_deinit(session);
478                         rte_free(bp->ulp_ctx);
479                         return rc;
480                 }
481                 /* update the port database */
482                 rc = ulp_port_db_dev_port_intf_update(bp->ulp_ctx, bp);
483                 if (rc) {
484                         BNXT_TF_DBG(ERR,
485                                     "Failed to update port database\n");
486                         ulp_ctx_detach(bp, session);
487                         ulp_session_deinit(session);
488                         rte_free(bp->ulp_ctx);
489                 }
490                 return rc;
491         }
492
493         /* Allocate and Initialize the ulp context. */
494         rc = ulp_ctx_init(bp, session);
495         if (rc) {
496                 BNXT_TF_DBG(ERR, "Failed to create the ulp context\n");
497                 goto jump_to_error;
498         }
499
500         /* create the port database */
501         rc = ulp_port_db_init(bp->ulp_ctx);
502         if (rc) {
503                 BNXT_TF_DBG(ERR, "Failed to create the port database\n");
504                 goto jump_to_error;
505         }
506
507         /* update the port database */
508         rc = ulp_port_db_dev_port_intf_update(bp->ulp_ctx, bp);
509         if (rc) {
510                 BNXT_TF_DBG(ERR, "Failed to update port database\n");
511                 goto jump_to_error;
512         }
513
514         /* Create the Mark database. */
515         rc = ulp_mark_db_init(bp->ulp_ctx);
516         if (rc) {
517                 BNXT_TF_DBG(ERR, "Failed to create the mark database\n");
518                 goto jump_to_error;
519         }
520
521         /* Create the flow database. */
522         rc = ulp_flow_db_init(bp->ulp_ctx);
523         if (rc) {
524                 BNXT_TF_DBG(ERR, "Failed to create the flow database\n");
525                 goto jump_to_error;
526         }
527
528         /* Create the eem table scope. */
529         rc = ulp_eem_tbl_scope_init(bp);
530         if (rc) {
531                 BNXT_TF_DBG(ERR, "Failed to create the eem scope table\n");
532                 goto jump_to_error;
533         }
534
535         rc = ulp_mapper_init(bp->ulp_ctx);
536         if (rc) {
537                 BNXT_TF_DBG(ERR, "Failed to initialize ulp mapper\n");
538                 goto jump_to_error;
539         }
540
541         return rc;
542
543 jump_to_error:
544         bnxt_ulp_deinit(bp);
545         return -ENOMEM;
546 }
547
548 /* Below are the access functions to access internal data of ulp context. */
549
550 /*
551  * When a port is deinit'ed by dpdk. This function is called
552  * and this function clears the ULP context and rest of the
553  * infrastructure associated with it.
554  */
555 void
556 bnxt_ulp_deinit(struct bnxt *bp)
557 {
558         struct bnxt_ulp_session_state   *session;
559         struct rte_pci_device           *pci_dev;
560         struct rte_pci_addr             *pci_addr;
561
562         /* Get the session first */
563         pci_dev = RTE_DEV_TO_PCI(bp->eth_dev->device);
564         pci_addr = &pci_dev->addr;
565         pthread_mutex_lock(&bnxt_ulp_global_mutex);
566         session = ulp_get_session(pci_addr);
567         pthread_mutex_unlock(&bnxt_ulp_global_mutex);
568
569         /* session not found then just exit */
570         if (!session)
571                 return;
572
573         /* clean up regular flows */
574         ulp_flow_db_flush_flows(bp->ulp_ctx, BNXT_ULP_REGULAR_FLOW_TABLE);
575
576         /* cleanup the eem table scope */
577         ulp_eem_tbl_scope_deinit(bp, bp->ulp_ctx);
578
579         /* cleanup the flow database */
580         ulp_flow_db_deinit(bp->ulp_ctx);
581
582         /* Delete the Mark database */
583         ulp_mark_db_deinit(bp->ulp_ctx);
584
585         /* cleanup the ulp mapper */
586         ulp_mapper_deinit(bp->ulp_ctx);
587
588         /* Delete the Port database */
589         ulp_port_db_deinit(bp->ulp_ctx);
590
591         /* Delete the ulp context and tf session */
592         ulp_ctx_detach(bp, session);
593
594         /* Finally delete the bnxt session*/
595         ulp_session_deinit(session);
596
597         rte_free(bp->ulp_ctx);
598 }
599
600 /* Function to set the Mark DB into the context */
601 int32_t
602 bnxt_ulp_cntxt_ptr2_mark_db_set(struct bnxt_ulp_context *ulp_ctx,
603                                 struct bnxt_ulp_mark_tbl *mark_tbl)
604 {
605         if (!ulp_ctx || !ulp_ctx->cfg_data) {
606                 BNXT_TF_DBG(ERR, "Invalid ulp context data\n");
607                 return -EINVAL;
608         }
609
610         ulp_ctx->cfg_data->mark_tbl = mark_tbl;
611
612         return 0;
613 }
614
615 /* Function to retrieve the Mark DB from the context. */
616 struct bnxt_ulp_mark_tbl *
617 bnxt_ulp_cntxt_ptr2_mark_db_get(struct bnxt_ulp_context *ulp_ctx)
618 {
619         if (!ulp_ctx || !ulp_ctx->cfg_data)
620                 return NULL;
621
622         return ulp_ctx->cfg_data->mark_tbl;
623 }
624
625 /* Function to set the device id of the hardware. */
626 int32_t
627 bnxt_ulp_cntxt_dev_id_set(struct bnxt_ulp_context *ulp_ctx,
628                           uint32_t dev_id)
629 {
630         if (ulp_ctx && ulp_ctx->cfg_data) {
631                 ulp_ctx->cfg_data->dev_id = dev_id;
632                 return 0;
633         }
634
635         return -EINVAL;
636 }
637
638 /* Function to get the device id of the hardware. */
639 int32_t
640 bnxt_ulp_cntxt_dev_id_get(struct bnxt_ulp_context *ulp_ctx,
641                           uint32_t *dev_id)
642 {
643         if (ulp_ctx && ulp_ctx->cfg_data) {
644                 *dev_id = ulp_ctx->cfg_data->dev_id;
645                 return 0;
646         }
647
648         return -EINVAL;
649 }
650
651 /* Function to get the table scope id of the EEM table. */
652 int32_t
653 bnxt_ulp_cntxt_tbl_scope_id_get(struct bnxt_ulp_context *ulp_ctx,
654                                 uint32_t *tbl_scope_id)
655 {
656         if (ulp_ctx && ulp_ctx->cfg_data) {
657                 *tbl_scope_id = ulp_ctx->cfg_data->tbl_scope_id;
658                 return 0;
659         }
660
661         return -EINVAL;
662 }
663
664 /* Function to set the table scope id of the EEM table. */
665 int32_t
666 bnxt_ulp_cntxt_tbl_scope_id_set(struct bnxt_ulp_context *ulp_ctx,
667                                 uint32_t tbl_scope_id)
668 {
669         if (ulp_ctx && ulp_ctx->cfg_data) {
670                 ulp_ctx->cfg_data->tbl_scope_id = tbl_scope_id;
671                 return 0;
672         }
673
674         return -EINVAL;
675 }
676
677 /* Function to set the tfp session details from the ulp context. */
678 int32_t
679 bnxt_ulp_cntxt_tfp_set(struct bnxt_ulp_context *ulp, struct tf *tfp)
680 {
681         if (!ulp) {
682                 BNXT_TF_DBG(ERR, "Invalid arguments\n");
683                 return -EINVAL;
684         }
685
686         /* TBD The tfp should be removed once tf_attach is implemented. */
687         ulp->g_tfp = tfp;
688         return 0;
689 }
690
691 /* Function to get the tfp session details from the ulp context. */
692 struct tf *
693 bnxt_ulp_cntxt_tfp_get(struct bnxt_ulp_context *ulp)
694 {
695         if (!ulp) {
696                 BNXT_TF_DBG(ERR, "Invalid arguments\n");
697                 return NULL;
698         }
699         /* TBD The tfp should be removed once tf_attach is implemented. */
700         return ulp->g_tfp;
701 }
702
703 /*
704  * Get the device table entry based on the device id.
705  *
706  * dev_id [in] The device id of the hardware
707  *
708  * Returns the pointer to the device parameters.
709  */
710 struct bnxt_ulp_device_params *
711 bnxt_ulp_device_params_get(uint32_t dev_id)
712 {
713         if (dev_id < BNXT_ULP_MAX_NUM_DEVICES)
714                 return &ulp_device_params[dev_id];
715         return NULL;
716 }
717
718 /* Function to set the flow database to the ulp context. */
719 int32_t
720 bnxt_ulp_cntxt_ptr2_flow_db_set(struct bnxt_ulp_context *ulp_ctx,
721                                 struct bnxt_ulp_flow_db *flow_db)
722 {
723         if (!ulp_ctx || !ulp_ctx->cfg_data)
724                 return -EINVAL;
725
726         ulp_ctx->cfg_data->flow_db = flow_db;
727         return 0;
728 }
729
730 /* Function to get the flow database from the ulp context. */
731 struct bnxt_ulp_flow_db *
732 bnxt_ulp_cntxt_ptr2_flow_db_get(struct bnxt_ulp_context *ulp_ctx)
733 {
734         if (!ulp_ctx || !ulp_ctx->cfg_data)
735                 return NULL;
736
737         return ulp_ctx->cfg_data->flow_db;
738 }
739
740 /* Function to get the ulp context from eth device. */
741 struct bnxt_ulp_context *
742 bnxt_ulp_eth_dev_ptr2_cntxt_get(struct rte_eth_dev      *dev)
743 {
744         struct bnxt     *bp;
745
746         bp = (struct bnxt *)dev->data->dev_private;
747         if (!bp) {
748                 BNXT_TF_DBG(ERR, "Bnxt private data is not initialized\n");
749                 return NULL;
750         }
751         return bp->ulp_ctx;
752 }
753
754 int32_t
755 bnxt_ulp_cntxt_ptr2_mapper_data_set(struct bnxt_ulp_context *ulp_ctx,
756                                     void *mapper_data)
757 {
758         if (!ulp_ctx || !ulp_ctx->cfg_data) {
759                 BNXT_TF_DBG(ERR, "Invalid ulp context data\n");
760                 return -EINVAL;
761         }
762
763         ulp_ctx->cfg_data->mapper_data = mapper_data;
764         return 0;
765 }
766
767 void *
768 bnxt_ulp_cntxt_ptr2_mapper_data_get(struct bnxt_ulp_context *ulp_ctx)
769 {
770         if (!ulp_ctx || !ulp_ctx->cfg_data) {
771                 BNXT_TF_DBG(ERR, "Invalid ulp context data\n");
772                 return NULL;
773         }
774
775         return ulp_ctx->cfg_data->mapper_data;
776 }
777
778 /* Function to set the port database to the ulp context. */
779 int32_t
780 bnxt_ulp_cntxt_ptr2_port_db_set(struct bnxt_ulp_context *ulp_ctx,
781                                 struct bnxt_ulp_port_db *port_db)
782 {
783         if (!ulp_ctx || !ulp_ctx->cfg_data)
784                 return -EINVAL;
785
786         ulp_ctx->cfg_data->port_db = port_db;
787         return 0;
788 }
789
790 /* Function to get the port database from the ulp context. */
791 struct bnxt_ulp_port_db *
792 bnxt_ulp_cntxt_ptr2_port_db_get(struct bnxt_ulp_context *ulp_ctx)
793 {
794         if (!ulp_ctx || !ulp_ctx->cfg_data)
795                 return NULL;
796
797         return ulp_ctx->cfg_data->port_db;
798 }