net/bnxt: add access to NAT global register
[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_enum.h"
19 #include "ulp_template_struct.h"
20 #include "ulp_mark_mgr.h"
21 #include "ulp_fc_mgr.h"
22 #include "ulp_flow_db.h"
23 #include "ulp_mapper.h"
24 #include "ulp_port_db.h"
25
26 /* Linked list of all TF sessions. */
27 STAILQ_HEAD(, bnxt_ulp_session_state) bnxt_ulp_session_list =
28                         STAILQ_HEAD_INITIALIZER(bnxt_ulp_session_list);
29
30 /* Mutex to synchronize bnxt_ulp_session_list operations. */
31 static pthread_mutex_t bnxt_ulp_global_mutex = PTHREAD_MUTEX_INITIALIZER;
32
33 /*
34  * Allow the deletion of context only for the bnxt device that
35  * created the session
36  * TBD - The implementation of the function should change to
37  * using the reference count once tf_session_attach functionality
38  * is fixed.
39  */
40 bool
41 ulp_ctx_deinit_allowed(void *ptr)
42 {
43         struct bnxt *bp = (struct bnxt *)ptr;
44
45         if (!bp)
46                 return 0;
47
48         if (&bp->tfp == bp->ulp_ctx->g_tfp)
49                 return 1;
50
51         return 0;
52 }
53
54 /*
55  * Initialize an ULP session.
56  * An ULP session will contain all the resources needed to support rte flow
57  * offloads. A session is initialized as part of rte_eth_device start.
58  * A single vswitch instance can have multiple uplinks which means
59  * rte_eth_device start will be called for each of these devices.
60  * ULP session manager will make sure that a single ULP session is only
61  * initialized once. Apart from this, it also initializes MARK database,
62  * EEM table & flow database. ULP session manager also manages a list of
63  * all opened ULP sessions.
64  */
65 static int32_t
66 ulp_ctx_session_open(struct bnxt *bp,
67                      struct bnxt_ulp_session_state *session)
68 {
69         struct rte_eth_dev              *ethdev = bp->eth_dev;
70         int32_t                         rc = 0;
71         struct tf_open_session_parms    params;
72         struct tf_session_resources     *resources;
73
74         memset(&params, 0, sizeof(params));
75
76         rc = rte_eth_dev_get_name_by_port(ethdev->data->port_id,
77                                           params.ctrl_chan_name);
78         if (rc) {
79                 BNXT_TF_DBG(ERR, "Invalid port %d, rc = %d\n",
80                             ethdev->data->port_id, rc);
81                 return rc;
82         }
83
84         params.shadow_copy = false;
85         params.device_type = TF_DEVICE_TYPE_WH;
86         resources = &params.resources;
87         /** RX **/
88         /* Identifiers */
89         resources->ident_cnt[TF_DIR_RX].cnt[TF_IDENT_TYPE_L2_CTXT_HIGH] = 200;
90         resources->ident_cnt[TF_DIR_RX].cnt[TF_IDENT_TYPE_L2_CTXT_LOW] = 20;
91         resources->ident_cnt[TF_DIR_RX].cnt[TF_IDENT_TYPE_WC_PROF] = 8;
92         resources->ident_cnt[TF_DIR_RX].cnt[TF_IDENT_TYPE_PROF_FUNC] = 8;
93         resources->ident_cnt[TF_DIR_RX].cnt[TF_IDENT_TYPE_EM_PROF] = 8;
94
95         /* Table Types */
96         resources->tbl_cnt[TF_DIR_RX].cnt[TF_TBL_TYPE_FULL_ACT_RECORD] = 720;
97         resources->tbl_cnt[TF_DIR_RX].cnt[TF_TBL_TYPE_ACT_STATS_64] = 720;
98         resources->tbl_cnt[TF_DIR_RX].cnt[TF_TBL_TYPE_ACT_MODIFY_IPV4] = 8;
99
100         /* ENCAP */
101         resources->tbl_cnt[TF_DIR_RX].cnt[TF_TBL_TYPE_ACT_ENCAP_8B] = 16;
102         resources->tbl_cnt[TF_DIR_RX].cnt[TF_TBL_TYPE_ACT_ENCAP_16B] = 16;
103
104         /* TCAMs */
105         resources->tcam_cnt[TF_DIR_RX].cnt[TF_TCAM_TBL_TYPE_L2_CTXT_TCAM_HIGH] =
106                 200;
107         resources->tcam_cnt[TF_DIR_RX].cnt[TF_TCAM_TBL_TYPE_L2_CTXT_TCAM_LOW] =
108                 20;
109         resources->tcam_cnt[TF_DIR_RX].cnt[TF_TCAM_TBL_TYPE_PROF_TCAM] = 8;
110         resources->tcam_cnt[TF_DIR_RX].cnt[TF_TCAM_TBL_TYPE_WC_TCAM] = 416;
111
112         /* EM */
113         resources->em_cnt[TF_DIR_RX].cnt[TF_EM_TBL_TYPE_EM_RECORD] = 2048;
114
115         /* EEM */
116         resources->em_cnt[TF_DIR_RX].cnt[TF_EM_TBL_TYPE_TBL_SCOPE] = 1;
117
118         /** TX **/
119         /* Identifiers */
120         resources->ident_cnt[TF_DIR_TX].cnt[TF_IDENT_TYPE_L2_CTXT_HIGH] = 200;
121         resources->ident_cnt[TF_DIR_TX].cnt[TF_IDENT_TYPE_L2_CTXT_LOW] = 20;
122         resources->ident_cnt[TF_DIR_TX].cnt[TF_IDENT_TYPE_WC_PROF] = 8;
123         resources->ident_cnt[TF_DIR_TX].cnt[TF_IDENT_TYPE_PROF_FUNC] = 8;
124         resources->ident_cnt[TF_DIR_TX].cnt[TF_IDENT_TYPE_EM_PROF] = 8;
125
126         /* Table Types */
127         resources->tbl_cnt[TF_DIR_TX].cnt[TF_TBL_TYPE_FULL_ACT_RECORD] = 16;
128         resources->tbl_cnt[TF_DIR_TX].cnt[TF_TBL_TYPE_ACT_STATS_64] = 16;
129         resources->tbl_cnt[TF_DIR_TX].cnt[TF_TBL_TYPE_ACT_MODIFY_IPV4] = 8;
130
131         /* ENCAP */
132         resources->tbl_cnt[TF_DIR_TX].cnt[TF_TBL_TYPE_ACT_ENCAP_64B] = 16;
133         resources->tbl_cnt[TF_DIR_TX].cnt[TF_TBL_TYPE_ACT_ENCAP_16B] = 16;
134
135         /* TCAMs */
136         resources->tcam_cnt[TF_DIR_TX].cnt[TF_TCAM_TBL_TYPE_L2_CTXT_TCAM_HIGH] =
137                 200;
138         resources->tcam_cnt[TF_DIR_TX].cnt[TF_TCAM_TBL_TYPE_L2_CTXT_TCAM_LOW] =
139                 20;
140         resources->tcam_cnt[TF_DIR_TX].cnt[TF_TCAM_TBL_TYPE_PROF_TCAM] = 8;
141         resources->tcam_cnt[TF_DIR_TX].cnt[TF_TCAM_TBL_TYPE_WC_TCAM] = 8;
142
143         /* EM */
144         resources->em_cnt[TF_DIR_TX].cnt[TF_EM_TBL_TYPE_EM_RECORD] = 2048;
145
146         /* EEM */
147         resources->em_cnt[TF_DIR_TX].cnt[TF_EM_TBL_TYPE_TBL_SCOPE] = 1;
148
149         /* SP */
150         resources->tbl_cnt[TF_DIR_TX].cnt[TF_TBL_TYPE_ACT_SP_SMAC_IPV4] = 128;
151
152         rc = tf_open_session(&bp->tfp, &params);
153         if (rc) {
154                 BNXT_TF_DBG(ERR, "Failed to open TF session - %s, rc = %d\n",
155                             params.ctrl_chan_name, rc);
156                 return -EINVAL;
157         }
158         session->session_opened = 1;
159         session->g_tfp = &bp->tfp;
160         return rc;
161 }
162
163 /*
164  * Close the ULP session.
165  * It takes the ulp context pointer.
166  */
167 static void
168 ulp_ctx_session_close(struct bnxt *bp,
169                       struct bnxt_ulp_session_state *session)
170 {
171         /* close the session in the hardware */
172         if (session->session_opened)
173                 tf_close_session(&bp->tfp);
174         session->session_opened = 0;
175         session->g_tfp = NULL;
176         bp->ulp_ctx->g_tfp = NULL;
177 }
178
179 static void
180 bnxt_init_tbl_scope_parms(struct bnxt *bp,
181                           struct tf_alloc_tbl_scope_parms *params)
182 {
183         struct bnxt_ulp_device_params   *dparms;
184         uint32_t dev_id;
185         int rc;
186
187         rc = bnxt_ulp_cntxt_dev_id_get(bp->ulp_ctx, &dev_id);
188         if (rc)
189                 /* TBD: For now, just use default. */
190                 dparms = 0;
191         else
192                 dparms = bnxt_ulp_device_params_get(dev_id);
193
194         /*
195          * Set the flush timer for EEM entries. The value is in 100ms intervals,
196          * so 100 is 10s.
197          */
198         params->hw_flow_cache_flush_timer = 100;
199
200         if (!dparms) {
201                 params->rx_max_key_sz_in_bits = BNXT_ULP_DFLT_RX_MAX_KEY;
202                 params->rx_max_action_entry_sz_in_bits =
203                         BNXT_ULP_DFLT_RX_MAX_ACTN_ENTRY;
204                 params->rx_mem_size_in_mb = BNXT_ULP_DFLT_RX_MEM;
205                 params->rx_num_flows_in_k = BNXT_ULP_RX_NUM_FLOWS;
206                 params->rx_tbl_if_id = BNXT_ULP_RX_TBL_IF_ID;
207
208                 params->tx_max_key_sz_in_bits = BNXT_ULP_DFLT_TX_MAX_KEY;
209                 params->tx_max_action_entry_sz_in_bits =
210                         BNXT_ULP_DFLT_TX_MAX_ACTN_ENTRY;
211                 params->tx_mem_size_in_mb = BNXT_ULP_DFLT_TX_MEM;
212                 params->tx_num_flows_in_k = BNXT_ULP_TX_NUM_FLOWS;
213                 params->tx_tbl_if_id = BNXT_ULP_TX_TBL_IF_ID;
214         } else {
215                 params->rx_max_key_sz_in_bits = BNXT_ULP_DFLT_RX_MAX_KEY;
216                 params->rx_max_action_entry_sz_in_bits =
217                         BNXT_ULP_DFLT_RX_MAX_ACTN_ENTRY;
218                 params->rx_mem_size_in_mb = BNXT_ULP_DFLT_RX_MEM;
219                 params->rx_num_flows_in_k = dparms->flow_db_num_entries / 1024;
220                 params->rx_tbl_if_id = BNXT_ULP_RX_TBL_IF_ID;
221
222                 params->tx_max_key_sz_in_bits = BNXT_ULP_DFLT_TX_MAX_KEY;
223                 params->tx_max_action_entry_sz_in_bits =
224                         BNXT_ULP_DFLT_TX_MAX_ACTN_ENTRY;
225                 params->tx_mem_size_in_mb = BNXT_ULP_DFLT_TX_MEM;
226                 params->tx_num_flows_in_k = dparms->flow_db_num_entries / 1024;
227                 params->tx_tbl_if_id = BNXT_ULP_TX_TBL_IF_ID;
228         }
229 }
230
231 /* Initialize Extended Exact Match host memory. */
232 static int32_t
233 ulp_eem_tbl_scope_init(struct bnxt *bp)
234 {
235         struct tf_alloc_tbl_scope_parms params = {0};
236         uint32_t dev_id;
237         struct bnxt_ulp_device_params *dparms;
238         int rc;
239
240         /* Get the dev specific number of flows that needed to be supported. */
241         if (bnxt_ulp_cntxt_dev_id_get(bp->ulp_ctx, &dev_id)) {
242                 BNXT_TF_DBG(ERR, "Invalid device id\n");
243                 return -EINVAL;
244         }
245
246         dparms = bnxt_ulp_device_params_get(dev_id);
247         if (!dparms) {
248                 BNXT_TF_DBG(ERR, "could not fetch the device params\n");
249                 return -ENODEV;
250         }
251
252         if (dparms->flow_mem_type != BNXT_ULP_FLOW_MEM_TYPE_EXT) {
253                 BNXT_TF_DBG(INFO, "Table Scope alloc is not required\n");
254                 return 0;
255         }
256
257         bnxt_init_tbl_scope_parms(bp, &params);
258
259         rc = tf_alloc_tbl_scope(&bp->tfp, &params);
260         if (rc) {
261                 BNXT_TF_DBG(ERR, "Unable to allocate eem table scope rc = %d\n",
262                             rc);
263                 return rc;
264         }
265
266         rc = bnxt_ulp_cntxt_tbl_scope_id_set(bp->ulp_ctx, params.tbl_scope_id);
267         if (rc) {
268                 BNXT_TF_DBG(ERR, "Unable to set table scope id\n");
269                 return rc;
270         }
271
272         return 0;
273 }
274
275 /* Free Extended Exact Match host memory */
276 static int32_t
277 ulp_eem_tbl_scope_deinit(struct bnxt *bp, struct bnxt_ulp_context *ulp_ctx)
278 {
279         struct tf_free_tbl_scope_parms  params = {0};
280         struct tf                       *tfp;
281         int32_t                         rc = 0;
282         struct bnxt_ulp_device_params *dparms;
283         uint32_t dev_id;
284
285         if (!ulp_ctx || !ulp_ctx->cfg_data)
286                 return -EINVAL;
287
288         /* Free the resources for the last device */
289         if (!ulp_ctx_deinit_allowed(bp))
290                 return rc;
291
292         tfp = bnxt_ulp_cntxt_tfp_get(ulp_ctx);
293         if (!tfp) {
294                 BNXT_TF_DBG(ERR, "Failed to get the truflow pointer\n");
295                 return -EINVAL;
296         }
297
298         /* Get the dev specific number of flows that needed to be supported. */
299         if (bnxt_ulp_cntxt_dev_id_get(bp->ulp_ctx, &dev_id)) {
300                 BNXT_TF_DBG(ERR, "Invalid device id\n");
301                 return -EINVAL;
302         }
303
304         dparms = bnxt_ulp_device_params_get(dev_id);
305         if (!dparms) {
306                 BNXT_TF_DBG(ERR, "could not fetch the device params\n");
307                 return -ENODEV;
308         }
309
310         if (dparms->flow_mem_type != BNXT_ULP_FLOW_MEM_TYPE_EXT) {
311                 BNXT_TF_DBG(INFO, "Table Scope free is not required\n");
312                 return 0;
313         }
314
315         rc = bnxt_ulp_cntxt_tbl_scope_id_get(ulp_ctx, &params.tbl_scope_id);
316         if (rc) {
317                 BNXT_TF_DBG(ERR, "Failed to get the table scope id\n");
318                 return -EINVAL;
319         }
320
321         rc = tf_free_tbl_scope(tfp, &params);
322         if (rc) {
323                 BNXT_TF_DBG(ERR, "Unable to free table scope\n");
324                 return -EINVAL;
325         }
326         return rc;
327 }
328
329 /* The function to free and deinit the ulp context data. */
330 static int32_t
331 ulp_ctx_deinit(struct bnxt *bp,
332                struct bnxt_ulp_session_state *session)
333 {
334         if (!session || !bp) {
335                 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
336                 return -EINVAL;
337         }
338
339         /* close the tf session */
340         ulp_ctx_session_close(bp, session);
341
342         /* Free the contents */
343         if (session->cfg_data) {
344                 rte_free(session->cfg_data);
345                 bp->ulp_ctx->cfg_data = NULL;
346                 session->cfg_data = NULL;
347         }
348         return 0;
349 }
350
351 /* The function to allocate and initialize the ulp context data. */
352 static int32_t
353 ulp_ctx_init(struct bnxt *bp,
354              struct bnxt_ulp_session_state *session)
355 {
356         struct bnxt_ulp_data    *ulp_data;
357         int32_t                 rc = 0;
358
359         if (!session || !bp) {
360                 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
361                 return -EINVAL;
362         }
363
364         /* Allocate memory to hold ulp context data. */
365         ulp_data = rte_zmalloc("bnxt_ulp_data",
366                                sizeof(struct bnxt_ulp_data), 0);
367         if (!ulp_data) {
368                 BNXT_TF_DBG(ERR, "Failed to allocate memory for ulp data\n");
369                 return -ENOMEM;
370         }
371
372         /* Increment the ulp context data reference count usage. */
373         bp->ulp_ctx->cfg_data = ulp_data;
374         session->cfg_data = ulp_data;
375         ulp_data->ref_cnt++;
376         ulp_data->ulp_flags |= BNXT_ULP_VF_REP_ENABLED;
377
378         /* Open the ulp session. */
379         rc = ulp_ctx_session_open(bp, session);
380         if (rc) {
381                 (void)ulp_ctx_deinit(bp, session);
382                 return rc;
383         }
384         bnxt_ulp_cntxt_tfp_set(bp->ulp_ctx, session->g_tfp);
385         return rc;
386 }
387
388 /* The function to initialize ulp dparms with devargs */
389 static int32_t
390 ulp_dparms_init(struct bnxt *bp,
391                 struct bnxt_ulp_context *ulp_ctx)
392 {
393         struct bnxt_ulp_device_params *dparms;
394         uint32_t dev_id;
395
396         if (!bp->max_num_kflows)
397                 return -EINVAL;
398
399         if (bnxt_ulp_cntxt_dev_id_get(ulp_ctx, &dev_id)) {
400                 BNXT_TF_DBG(DEBUG, "Failed to get device id\n");
401                 return -EINVAL;
402         }
403
404         dparms = bnxt_ulp_device_params_get(dev_id);
405         if (!dparms) {
406                 BNXT_TF_DBG(DEBUG, "Failed to get device parms\n");
407                 return -EINVAL;
408         }
409
410         /* num_flows = max_num_kflows * 1024 */
411         dparms->flow_db_num_entries = bp->max_num_kflows * 1024;
412         /* GFID =  2 * num_flows */
413         dparms->mark_db_gfid_entries = dparms->flow_db_num_entries * 2;
414         BNXT_TF_DBG(DEBUG, "Set the number of flows = %"PRIu64"\n",
415                     dparms->flow_db_num_entries);
416
417         return 0;
418 }
419
420 /* The function to initialize bp flags with truflow features */
421 static int32_t
422 ulp_dparms_dev_port_intf_update(struct bnxt *bp,
423                                 struct bnxt_ulp_context *ulp_ctx)
424 {
425         struct bnxt_ulp_device_params *dparms;
426         uint32_t dev_id;
427
428         if (bnxt_ulp_cntxt_dev_id_get(ulp_ctx, &dev_id)) {
429                 BNXT_TF_DBG(DEBUG, "Failed to get device id\n");
430                 return -EINVAL;
431         }
432
433         dparms = bnxt_ulp_device_params_get(dev_id);
434         if (!dparms) {
435                 BNXT_TF_DBG(DEBUG, "Failed to get device parms\n");
436                 return -EINVAL;
437         }
438
439         /* Update the bp flag with gfid flag */
440         if (dparms->flow_mem_type == BNXT_ULP_FLOW_MEM_TYPE_EXT)
441                 bp->flags |= BNXT_FLAG_GFID_ENABLE;
442
443         return 0;
444 }
445
446 static int32_t
447 ulp_ctx_attach(struct bnxt_ulp_context *ulp_ctx,
448                struct bnxt_ulp_session_state *session)
449 {
450         if (!ulp_ctx || !session) {
451                 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
452                 return -EINVAL;
453         }
454
455         /* Increment the ulp context data reference count usage. */
456         ulp_ctx->cfg_data = session->cfg_data;
457         ulp_ctx->cfg_data->ref_cnt++;
458
459         /* TBD call TF_session_attach. */
460         ulp_ctx->g_tfp = session->g_tfp;
461         return 0;
462 }
463
464 static int32_t
465 ulp_ctx_detach(struct bnxt *bp,
466                struct bnxt_ulp_session_state *session)
467 {
468         struct bnxt_ulp_context *ulp_ctx;
469
470         if (!bp || !session) {
471                 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
472                 return -EINVAL;
473         }
474         ulp_ctx = bp->ulp_ctx;
475
476         if (!ulp_ctx->cfg_data)
477                 return 0;
478
479         /* TBD call TF_session_detach */
480
481         /* Increment the ulp context data reference count usage. */
482         if (ulp_ctx->cfg_data->ref_cnt >= 1) {
483                 ulp_ctx->cfg_data->ref_cnt--;
484                 if (ulp_ctx_deinit_allowed(bp))
485                         ulp_ctx_deinit(bp, session);
486                 ulp_ctx->cfg_data = NULL;
487                 ulp_ctx->g_tfp = NULL;
488                 return 0;
489         }
490         BNXT_TF_DBG(ERR, "context deatach on invalid data\n");
491         return 0;
492 }
493
494 /*
495  * Initialize the state of an ULP session.
496  * If the state of an ULP session is not initialized, set it's state to
497  * initialized. If the state is already initialized, do nothing.
498  */
499 static void
500 ulp_context_initialized(struct bnxt_ulp_session_state *session, bool *init)
501 {
502         pthread_mutex_lock(&session->bnxt_ulp_mutex);
503
504         if (!session->bnxt_ulp_init) {
505                 session->bnxt_ulp_init = true;
506                 *init = false;
507         } else {
508                 *init = true;
509         }
510
511         pthread_mutex_unlock(&session->bnxt_ulp_mutex);
512 }
513
514 /*
515  * Check if an ULP session is already allocated for a specific PCI
516  * domain & bus. If it is already allocated simply return the session
517  * pointer, otherwise allocate a new session.
518  */
519 static struct bnxt_ulp_session_state *
520 ulp_get_session(struct rte_pci_addr *pci_addr)
521 {
522         struct bnxt_ulp_session_state *session;
523
524         STAILQ_FOREACH(session, &bnxt_ulp_session_list, next) {
525                 if (session->pci_info.domain == pci_addr->domain &&
526                     session->pci_info.bus == pci_addr->bus) {
527                         return session;
528                 }
529         }
530         return NULL;
531 }
532
533 /*
534  * Allocate and Initialize an ULP session and set it's state to INITIALIZED.
535  * If it's already initialized simply return the already existing session.
536  */
537 static struct bnxt_ulp_session_state *
538 ulp_session_init(struct bnxt *bp,
539                  bool *init)
540 {
541         struct rte_pci_device           *pci_dev;
542         struct rte_pci_addr             *pci_addr;
543         struct bnxt_ulp_session_state   *session;
544
545         if (!bp)
546                 return NULL;
547
548         pci_dev = RTE_DEV_TO_PCI(bp->eth_dev->device);
549         pci_addr = &pci_dev->addr;
550
551         pthread_mutex_lock(&bnxt_ulp_global_mutex);
552
553         session = ulp_get_session(pci_addr);
554         if (!session) {
555                 /* Not Found the session  Allocate a new one */
556                 session = rte_zmalloc("bnxt_ulp_session",
557                                       sizeof(struct bnxt_ulp_session_state),
558                                       0);
559                 if (!session) {
560                         BNXT_TF_DBG(ERR,
561                                     "Allocation failed for bnxt_ulp_session\n");
562                         pthread_mutex_unlock(&bnxt_ulp_global_mutex);
563                         return NULL;
564
565                 } else {
566                         /* Add it to the queue */
567                         session->pci_info.domain = pci_addr->domain;
568                         session->pci_info.bus = pci_addr->bus;
569                         pthread_mutex_init(&session->bnxt_ulp_mutex, NULL);
570                         STAILQ_INSERT_TAIL(&bnxt_ulp_session_list,
571                                            session, next);
572                 }
573         }
574         ulp_context_initialized(session, init);
575         pthread_mutex_unlock(&bnxt_ulp_global_mutex);
576         return session;
577 }
578
579 /*
580  * When a device is closed, remove it's associated session from the global
581  * session list.
582  */
583 static void
584 ulp_session_deinit(struct bnxt_ulp_session_state *session)
585 {
586         if (!session)
587                 return;
588
589         if (!session->cfg_data) {
590                 pthread_mutex_lock(&bnxt_ulp_global_mutex);
591                 STAILQ_REMOVE(&bnxt_ulp_session_list, session,
592                               bnxt_ulp_session_state, next);
593                 pthread_mutex_destroy(&session->bnxt_ulp_mutex);
594                 rte_free(session);
595                 pthread_mutex_unlock(&bnxt_ulp_global_mutex);
596         }
597 }
598
599 /*
600  * Internal api to enable NAT feature.
601  * Set set_flag to 1 to set the value or zero to reset the value.
602  * returns 0 on success.
603  */
604 static int32_t
605 bnxt_ulp_global_cfg_update(struct bnxt *bp,
606                            enum tf_dir dir,
607                            enum tf_global_config_type type,
608                            uint32_t offset,
609                            uint32_t value,
610                            uint32_t set_flag)
611 {
612         uint32_t global_cfg = 0;
613         int rc;
614         struct tf_global_cfg_parms parms;
615
616         /* Initialize the params */
617         parms.dir = dir,
618         parms.type = type,
619         parms.offset = offset,
620         parms.config = (uint8_t *)&global_cfg,
621         parms.config_sz_in_bytes = sizeof(global_cfg);
622
623         rc = tf_get_global_cfg(&bp->tfp, &parms);
624         if (rc) {
625                 BNXT_TF_DBG(ERR, "Failed to get global cfg 0x%x rc:%d\n",
626                             type, rc);
627                 return rc;
628         }
629
630         if (set_flag)
631                 global_cfg |= value;
632         else
633                 global_cfg &= ~value;
634
635         /* SET the register RE_CFA_REG_ACT_TECT */
636         rc = tf_set_global_cfg(&bp->tfp, &parms);
637         if (rc) {
638                 BNXT_TF_DBG(ERR, "Failed to set global cfg 0x%x rc:%d\n",
639                             type, rc);
640                 return rc;
641         }
642         return rc;
643 }
644
645 /*
646  * When a port is initialized by dpdk. This functions is called
647  * and this function initializes the ULP context and rest of the
648  * infrastructure associated with it.
649  */
650 int32_t
651 bnxt_ulp_init(struct bnxt *bp)
652 {
653         struct bnxt_ulp_session_state *session;
654         bool init;
655         int rc;
656
657         if (bp->ulp_ctx) {
658                 BNXT_TF_DBG(DEBUG, "ulp ctx already allocated\n");
659                 return -EINVAL;
660         }
661
662         /*
663          * Multiple uplink ports can be associated with a single vswitch.
664          * Make sure only the port that is started first will initialize
665          * the TF session.
666          */
667         session = ulp_session_init(bp, &init);
668         if (!session) {
669                 BNXT_TF_DBG(ERR, "Failed to initialize the tf session\n");
670                 return -EINVAL;
671         }
672
673         bp->ulp_ctx = rte_zmalloc("bnxt_ulp_ctx",
674                                   sizeof(struct bnxt_ulp_context), 0);
675         if (!bp->ulp_ctx) {
676                 BNXT_TF_DBG(ERR, "Failed to allocate ulp ctx\n");
677                 ulp_session_deinit(session);
678                 return -ENOMEM;
679         }
680
681         /*
682          * If ULP is already initialized for a specific domain then simply
683          * assign the ulp context to this rte_eth_dev.
684          */
685         if (init) {
686                 rc = ulp_ctx_attach(bp->ulp_ctx, session);
687                 if (rc) {
688                         BNXT_TF_DBG(ERR,
689                                     "Failed to attach the ulp context\n");
690                         ulp_session_deinit(session);
691                         rte_free(bp->ulp_ctx);
692                         return rc;
693                 }
694
695                 /* Update bnxt driver flags */
696                 rc = ulp_dparms_dev_port_intf_update(bp, bp->ulp_ctx);
697                 if (rc) {
698                         BNXT_TF_DBG(ERR, "Failed to update driver flags\n");
699                         ulp_ctx_detach(bp, session);
700                         ulp_session_deinit(session);
701                         rte_free(bp->ulp_ctx);
702                         return rc;
703                 }
704
705                 /* update the port database */
706                 rc = ulp_port_db_dev_port_intf_update(bp->ulp_ctx, bp->eth_dev);
707                 if (rc) {
708                         BNXT_TF_DBG(ERR,
709                                     "Failed to update port database\n");
710                         ulp_ctx_detach(bp, session);
711                         ulp_session_deinit(session);
712                         rte_free(bp->ulp_ctx);
713                 }
714                 return rc;
715         }
716
717         /* Allocate and Initialize the ulp context. */
718         rc = ulp_ctx_init(bp, session);
719         if (rc) {
720                 BNXT_TF_DBG(ERR, "Failed to create the ulp context\n");
721                 goto jump_to_error;
722         }
723
724         /* Initialize ulp dparms with values devargs passed */
725         rc = ulp_dparms_init(bp, bp->ulp_ctx);
726
727         /* create the port database */
728         rc = ulp_port_db_init(bp->ulp_ctx, bp->port_cnt);
729         if (rc) {
730                 BNXT_TF_DBG(ERR, "Failed to create the port database\n");
731                 goto jump_to_error;
732         }
733
734         /* Update bnxt driver flags */
735         rc = ulp_dparms_dev_port_intf_update(bp, bp->ulp_ctx);
736         if (rc) {
737                 BNXT_TF_DBG(ERR, "Failed to update driver flags\n");
738                 goto jump_to_error;
739         }
740
741         /* update the port database */
742         rc = ulp_port_db_dev_port_intf_update(bp->ulp_ctx, bp->eth_dev);
743         if (rc) {
744                 BNXT_TF_DBG(ERR, "Failed to update port database\n");
745                 goto jump_to_error;
746         }
747
748         /* Create the Mark database. */
749         rc = ulp_mark_db_init(bp->ulp_ctx);
750         if (rc) {
751                 BNXT_TF_DBG(ERR, "Failed to create the mark database\n");
752                 goto jump_to_error;
753         }
754
755         /* Create the flow database. */
756         rc = ulp_flow_db_init(bp->ulp_ctx);
757         if (rc) {
758                 BNXT_TF_DBG(ERR, "Failed to create the flow database\n");
759                 goto jump_to_error;
760         }
761
762         /* Create the eem table scope. */
763         rc = ulp_eem_tbl_scope_init(bp);
764         if (rc) {
765                 BNXT_TF_DBG(ERR, "Failed to create the eem scope table\n");
766                 goto jump_to_error;
767         }
768
769         rc = ulp_mapper_init(bp->ulp_ctx);
770         if (rc) {
771                 BNXT_TF_DBG(ERR, "Failed to initialize ulp mapper\n");
772                 goto jump_to_error;
773         }
774
775         rc = ulp_fc_mgr_init(bp->ulp_ctx);
776         if (rc) {
777                 BNXT_TF_DBG(ERR, "Failed to initialize ulp flow counter mgr\n");
778                 goto jump_to_error;
779         }
780
781         /*
782          * Enable NAT feature. Set the global configuration register
783          * Tunnel encap to enable NAT with the reuse of existing inner
784          * L2 header smac and dmac
785          */
786         rc = bnxt_ulp_global_cfg_update(bp, TF_DIR_RX, TF_TUNNEL_ENCAP,
787                                         TF_TUNNEL_ENCAP_NAT,
788                                         (BNXT_ULP_NAT_INNER_L2_HEADER_SMAC |
789                                         BNXT_ULP_NAT_INNER_L2_HEADER_DMAC), 1);
790         if (rc) {
791                 BNXT_TF_DBG(ERR, "Failed to set rx global configuration\n");
792                 goto jump_to_error;
793         }
794
795         rc = bnxt_ulp_global_cfg_update(bp, TF_DIR_TX, TF_TUNNEL_ENCAP,
796                                         TF_TUNNEL_ENCAP_NAT,
797                                         (BNXT_ULP_NAT_INNER_L2_HEADER_SMAC |
798                                         BNXT_ULP_NAT_INNER_L2_HEADER_DMAC), 1);
799         if (rc) {
800                 BNXT_TF_DBG(ERR, "Failed to set tx global configuration\n");
801                 goto jump_to_error;
802         }
803
804         return rc;
805
806 jump_to_error:
807         bnxt_ulp_deinit(bp);
808         return -ENOMEM;
809 }
810
811 /* Below are the access functions to access internal data of ulp context. */
812
813 /*
814  * When a port is deinit'ed by dpdk. This function is called
815  * and this function clears the ULP context and rest of the
816  * infrastructure associated with it.
817  */
818 void
819 bnxt_ulp_deinit(struct bnxt *bp)
820 {
821         struct bnxt_ulp_session_state   *session;
822         struct rte_pci_device           *pci_dev;
823         struct rte_pci_addr             *pci_addr;
824
825         /* Get the session first */
826         pci_dev = RTE_DEV_TO_PCI(bp->eth_dev->device);
827         pci_addr = &pci_dev->addr;
828         pthread_mutex_lock(&bnxt_ulp_global_mutex);
829         session = ulp_get_session(pci_addr);
830         pthread_mutex_unlock(&bnxt_ulp_global_mutex);
831
832         /* session not found then just exit */
833         if (!session)
834                 return;
835
836         /* clean up regular flows */
837         ulp_flow_db_flush_flows(bp->ulp_ctx, BNXT_ULP_REGULAR_FLOW_TABLE);
838
839         /* cleanup the eem table scope */
840         ulp_eem_tbl_scope_deinit(bp, bp->ulp_ctx);
841
842         /* cleanup the flow database */
843         ulp_flow_db_deinit(bp->ulp_ctx);
844
845         /* Delete the Mark database */
846         ulp_mark_db_deinit(bp->ulp_ctx);
847
848         /* cleanup the ulp mapper */
849         ulp_mapper_deinit(bp->ulp_ctx);
850
851         /* Delete the Flow Counter Manager */
852         ulp_fc_mgr_deinit(bp->ulp_ctx);
853
854         /* Delete the Port database */
855         ulp_port_db_deinit(bp->ulp_ctx);
856
857         /* Disable NAT feature */
858         (void)bnxt_ulp_global_cfg_update(bp, TF_DIR_RX, TF_TUNNEL_ENCAP,
859                                          TF_TUNNEL_ENCAP_NAT,
860                                          (BNXT_ULP_NAT_INNER_L2_HEADER_SMAC |
861                                           BNXT_ULP_NAT_INNER_L2_HEADER_DMAC),
862                                          0);
863
864         (void)bnxt_ulp_global_cfg_update(bp, TF_DIR_TX, TF_TUNNEL_ENCAP,
865                                          TF_TUNNEL_ENCAP_NAT,
866                                          (BNXT_ULP_NAT_INNER_L2_HEADER_SMAC |
867                                           BNXT_ULP_NAT_INNER_L2_HEADER_DMAC),
868                                          0);
869
870         /* Delete the ulp context and tf session */
871         ulp_ctx_detach(bp, session);
872
873         /* Finally delete the bnxt session*/
874         ulp_session_deinit(session);
875
876         rte_free(bp->ulp_ctx);
877 }
878
879 /* Function to set the Mark DB into the context */
880 int32_t
881 bnxt_ulp_cntxt_ptr2_mark_db_set(struct bnxt_ulp_context *ulp_ctx,
882                                 struct bnxt_ulp_mark_tbl *mark_tbl)
883 {
884         if (!ulp_ctx || !ulp_ctx->cfg_data) {
885                 BNXT_TF_DBG(ERR, "Invalid ulp context data\n");
886                 return -EINVAL;
887         }
888
889         ulp_ctx->cfg_data->mark_tbl = mark_tbl;
890
891         return 0;
892 }
893
894 /* Function to retrieve the Mark DB from the context. */
895 struct bnxt_ulp_mark_tbl *
896 bnxt_ulp_cntxt_ptr2_mark_db_get(struct bnxt_ulp_context *ulp_ctx)
897 {
898         if (!ulp_ctx || !ulp_ctx->cfg_data)
899                 return NULL;
900
901         return ulp_ctx->cfg_data->mark_tbl;
902 }
903
904 /* Function to set the device id of the hardware. */
905 int32_t
906 bnxt_ulp_cntxt_dev_id_set(struct bnxt_ulp_context *ulp_ctx,
907                           uint32_t dev_id)
908 {
909         if (ulp_ctx && ulp_ctx->cfg_data) {
910                 ulp_ctx->cfg_data->dev_id = dev_id;
911                 return 0;
912         }
913
914         return -EINVAL;
915 }
916
917 /* Function to get the device id of the hardware. */
918 int32_t
919 bnxt_ulp_cntxt_dev_id_get(struct bnxt_ulp_context *ulp_ctx,
920                           uint32_t *dev_id)
921 {
922         if (ulp_ctx && ulp_ctx->cfg_data) {
923                 *dev_id = ulp_ctx->cfg_data->dev_id;
924                 return 0;
925         }
926
927         return -EINVAL;
928 }
929
930 /* Function to get the table scope id of the EEM table. */
931 int32_t
932 bnxt_ulp_cntxt_tbl_scope_id_get(struct bnxt_ulp_context *ulp_ctx,
933                                 uint32_t *tbl_scope_id)
934 {
935         if (ulp_ctx && ulp_ctx->cfg_data) {
936                 *tbl_scope_id = ulp_ctx->cfg_data->tbl_scope_id;
937                 return 0;
938         }
939
940         return -EINVAL;
941 }
942
943 /* Function to set the table scope id of the EEM table. */
944 int32_t
945 bnxt_ulp_cntxt_tbl_scope_id_set(struct bnxt_ulp_context *ulp_ctx,
946                                 uint32_t tbl_scope_id)
947 {
948         if (ulp_ctx && ulp_ctx->cfg_data) {
949                 ulp_ctx->cfg_data->tbl_scope_id = tbl_scope_id;
950                 return 0;
951         }
952
953         return -EINVAL;
954 }
955
956 /* Function to set the tfp session details from the ulp context. */
957 int32_t
958 bnxt_ulp_cntxt_tfp_set(struct bnxt_ulp_context *ulp, struct tf *tfp)
959 {
960         if (!ulp) {
961                 BNXT_TF_DBG(ERR, "Invalid arguments\n");
962                 return -EINVAL;
963         }
964
965         /* TBD The tfp should be removed once tf_attach is implemented. */
966         ulp->g_tfp = tfp;
967         return 0;
968 }
969
970 /* Function to get the tfp session details from the ulp context. */
971 struct tf *
972 bnxt_ulp_cntxt_tfp_get(struct bnxt_ulp_context *ulp)
973 {
974         if (!ulp) {
975                 BNXT_TF_DBG(ERR, "Invalid arguments\n");
976                 return NULL;
977         }
978         /* TBD The tfp should be removed once tf_attach is implemented. */
979         return ulp->g_tfp;
980 }
981
982 /*
983  * Get the device table entry based on the device id.
984  *
985  * dev_id [in] The device id of the hardware
986  *
987  * Returns the pointer to the device parameters.
988  */
989 struct bnxt_ulp_device_params *
990 bnxt_ulp_device_params_get(uint32_t dev_id)
991 {
992         if (dev_id < BNXT_ULP_MAX_NUM_DEVICES)
993                 return &ulp_device_params[dev_id];
994         return NULL;
995 }
996
997 /* Function to set the flow database to the ulp context. */
998 int32_t
999 bnxt_ulp_cntxt_ptr2_flow_db_set(struct bnxt_ulp_context *ulp_ctx,
1000                                 struct bnxt_ulp_flow_db *flow_db)
1001 {
1002         if (!ulp_ctx || !ulp_ctx->cfg_data)
1003                 return -EINVAL;
1004
1005         ulp_ctx->cfg_data->flow_db = flow_db;
1006         return 0;
1007 }
1008
1009 /* Function to get the flow database from the ulp context. */
1010 struct bnxt_ulp_flow_db *
1011 bnxt_ulp_cntxt_ptr2_flow_db_get(struct bnxt_ulp_context *ulp_ctx)
1012 {
1013         if (!ulp_ctx || !ulp_ctx->cfg_data)
1014                 return NULL;
1015
1016         return ulp_ctx->cfg_data->flow_db;
1017 }
1018
1019 /* Function to get the ulp context from eth device. */
1020 struct bnxt_ulp_context *
1021 bnxt_ulp_eth_dev_ptr2_cntxt_get(struct rte_eth_dev      *dev)
1022 {
1023         struct bnxt *bp = (struct bnxt *)dev->data->dev_private;
1024
1025         if (BNXT_ETH_DEV_IS_REPRESENTOR(dev)) {
1026                 struct bnxt_vf_representor *vfr = dev->data->dev_private;
1027
1028                 bp = vfr->parent_dev->data->dev_private;
1029         }
1030
1031         if (!bp) {
1032                 BNXT_TF_DBG(ERR, "Bnxt private data is not initialized\n");
1033                 return NULL;
1034         }
1035         return bp->ulp_ctx;
1036 }
1037
1038 int32_t
1039 bnxt_ulp_cntxt_ptr2_mapper_data_set(struct bnxt_ulp_context *ulp_ctx,
1040                                     void *mapper_data)
1041 {
1042         if (!ulp_ctx || !ulp_ctx->cfg_data) {
1043                 BNXT_TF_DBG(ERR, "Invalid ulp context data\n");
1044                 return -EINVAL;
1045         }
1046
1047         ulp_ctx->cfg_data->mapper_data = mapper_data;
1048         return 0;
1049 }
1050
1051 void *
1052 bnxt_ulp_cntxt_ptr2_mapper_data_get(struct bnxt_ulp_context *ulp_ctx)
1053 {
1054         if (!ulp_ctx || !ulp_ctx->cfg_data) {
1055                 BNXT_TF_DBG(ERR, "Invalid ulp context data\n");
1056                 return NULL;
1057         }
1058
1059         return ulp_ctx->cfg_data->mapper_data;
1060 }
1061
1062 /* Function to set the port database to the ulp context. */
1063 int32_t
1064 bnxt_ulp_cntxt_ptr2_port_db_set(struct bnxt_ulp_context *ulp_ctx,
1065                                 struct bnxt_ulp_port_db *port_db)
1066 {
1067         if (!ulp_ctx || !ulp_ctx->cfg_data)
1068                 return -EINVAL;
1069
1070         ulp_ctx->cfg_data->port_db = port_db;
1071         return 0;
1072 }
1073
1074 /* Function to get the port database from the ulp context. */
1075 struct bnxt_ulp_port_db *
1076 bnxt_ulp_cntxt_ptr2_port_db_get(struct bnxt_ulp_context *ulp_ctx)
1077 {
1078         if (!ulp_ctx || !ulp_ctx->cfg_data)
1079                 return NULL;
1080
1081         return ulp_ctx->cfg_data->port_db;
1082 }
1083
1084 /* Function to set the flow counter info into the context */
1085 int32_t
1086 bnxt_ulp_cntxt_ptr2_fc_info_set(struct bnxt_ulp_context *ulp_ctx,
1087                                 struct bnxt_ulp_fc_info *ulp_fc_info)
1088 {
1089         if (!ulp_ctx || !ulp_ctx->cfg_data) {
1090                 BNXT_TF_DBG(ERR, "Invalid ulp context data\n");
1091                 return -EINVAL;
1092         }
1093
1094         ulp_ctx->cfg_data->fc_info = ulp_fc_info;
1095
1096         return 0;
1097 }
1098
1099 /* Function to retrieve the flow counter info from the context. */
1100 struct bnxt_ulp_fc_info *
1101 bnxt_ulp_cntxt_ptr2_fc_info_get(struct bnxt_ulp_context *ulp_ctx)
1102 {
1103         if (!ulp_ctx || !ulp_ctx->cfg_data)
1104                 return NULL;
1105
1106         return ulp_ctx->cfg_data->fc_info;
1107 }
1108
1109 /* Function to get the ulp flags from the ulp context. */
1110 int32_t
1111 bnxt_ulp_cntxt_ptr2_ulp_flags_get(struct bnxt_ulp_context *ulp_ctx,
1112                                   uint32_t *flags)
1113 {
1114         if (!ulp_ctx || !ulp_ctx->cfg_data)
1115                 return -1;
1116
1117         *flags =  ulp_ctx->cfg_data->ulp_flags;
1118         return 0;
1119 }