net/bnxt: add templates for shared sessions
[dpdk.git] / drivers / net / bnxt / tf_ulp / bnxt_ulp.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2019-2021 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.h"
13 #include "bnxt_ulp.h"
14 #include "bnxt_tf_common.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 #include "ulp_tun.h"
26
27 /* Linked list of all TF sessions. */
28 STAILQ_HEAD(, bnxt_ulp_session_state) bnxt_ulp_session_list =
29                         STAILQ_HEAD_INITIALIZER(bnxt_ulp_session_list);
30
31 /* Mutex to synchronize bnxt_ulp_session_list operations. */
32 static pthread_mutex_t bnxt_ulp_global_mutex = PTHREAD_MUTEX_INITIALIZER;
33
34 /*
35  * Allow the deletion of context only for the bnxt device that
36  * created the session.
37  */
38 bool
39 ulp_ctx_deinit_allowed(struct bnxt_ulp_context *ulp_ctx)
40 {
41         if (!ulp_ctx || !ulp_ctx->cfg_data)
42                 return false;
43
44         if (!ulp_ctx->cfg_data->ref_cnt) {
45                 BNXT_TF_DBG(DEBUG, "ulp ctx shall initiate deinit\n");
46                 return true;
47         }
48
49         return false;
50 }
51
52 static int32_t
53 bnxt_ulp_devid_get(struct bnxt *bp,
54                    enum bnxt_ulp_device_id  *ulp_dev_id)
55 {
56         if (BNXT_CHIP_P5(bp)) {
57                 /* TBD: needs to accommodate even SR2 */
58                 *ulp_dev_id = BNXT_ULP_DEVICE_ID_THOR;
59                 return 0;
60         }
61
62         if (BNXT_STINGRAY(bp))
63                 *ulp_dev_id = BNXT_ULP_DEVICE_ID_STINGRAY;
64         else
65                 /* Assuming Whitney */
66                 *ulp_dev_id = BNXT_ULP_DEVICE_ID_WH_PLUS;
67
68         return 0;
69 }
70
71 struct bnxt_ulp_app_capabilities_info *
72 bnxt_ulp_app_cap_list_get(uint32_t *num_entries)
73 {
74         if (!num_entries)
75                 return NULL;
76         *num_entries = BNXT_ULP_APP_CAP_TBL_MAX_SZ;
77         return ulp_app_cap_info_list;
78 }
79
80 static struct bnxt_ulp_resource_resv_info *
81 bnxt_ulp_app_resource_resv_list_get(uint32_t *num_entries)
82 {
83         if (num_entries == NULL)
84                 return NULL;
85         *num_entries = BNXT_ULP_APP_RESOURCE_RESV_LIST_MAX_SZ;
86         return ulp_app_resource_resv_list;
87 }
88
89 struct bnxt_ulp_resource_resv_info *
90 bnxt_ulp_resource_resv_list_get(uint32_t *num_entries)
91 {
92         if (!num_entries)
93                 return NULL;
94         *num_entries = BNXT_ULP_RESOURCE_RESV_LIST_MAX_SZ;
95         return ulp_resource_resv_list;
96 }
97
98 struct bnxt_ulp_glb_resource_info *
99 bnxt_ulp_app_glb_resource_info_list_get(uint32_t *num_entries)
100 {
101         if (!num_entries)
102                 return NULL;
103         *num_entries = BNXT_ULP_APP_GLB_RESOURCE_TBL_MAX_SZ;
104         return ulp_app_glb_resource_tbl;
105 }
106
107 static int32_t
108 bnxt_ulp_named_resources_calc(struct bnxt_ulp_context *ulp_ctx,
109                               struct bnxt_ulp_glb_resource_info *info,
110                               uint32_t num,
111                               struct tf_session_resources *res)
112 {
113         uint32_t dev_id, res_type, i;
114         enum tf_dir dir;
115         uint8_t app_id;
116         int32_t rc = 0;
117
118         if (ulp_ctx == NULL || info == NULL || res == NULL || num == 0) {
119                 BNXT_TF_DBG(ERR, "Invalid parms to named resources calc.\n");
120                 return -EINVAL;
121         }
122
123         rc = bnxt_ulp_cntxt_app_id_get(ulp_ctx, &app_id);
124         if (rc) {
125                 BNXT_TF_DBG(ERR, "Unable to get the app id from ulp.\n");
126                 return -EINVAL;
127         }
128
129         rc = bnxt_ulp_cntxt_dev_id_get(ulp_ctx, &dev_id);
130         if (rc) {
131                 BNXT_TF_DBG(ERR, "Unable to get the dev id from ulp.\n");
132                 return -EINVAL;
133         }
134
135         for (i = 0; i < num; i++) {
136                 if (dev_id != info[i].device_id || app_id != info[i].app_id)
137                         continue;
138                 dir = info[i].direction;
139                 res_type = info[i].resource_type;
140
141                 switch (info[i].resource_func) {
142                 case BNXT_ULP_RESOURCE_FUNC_IDENTIFIER:
143                         res->ident_cnt[dir].cnt[res_type]++;
144                         break;
145                 case BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE:
146                         res->tbl_cnt[dir].cnt[res_type]++;
147                         break;
148                 case BNXT_ULP_RESOURCE_FUNC_TCAM_TABLE:
149                         res->tcam_cnt[dir].cnt[res_type]++;
150                         break;
151                 case BNXT_ULP_RESOURCE_FUNC_EM_TABLE:
152                         res->em_cnt[dir].cnt[res_type]++;
153                         break;
154                 default:
155                         BNXT_TF_DBG(ERR, "Unknown resource func (0x%x)\n,",
156                                     info[i].resource_func);
157                         continue;
158                 }
159         }
160
161         return 0;
162 }
163
164 static int32_t
165 bnxt_ulp_unnamed_resources_calc(struct bnxt_ulp_context *ulp_ctx,
166                                 struct bnxt_ulp_resource_resv_info *info,
167                                 uint32_t num,
168                                 struct tf_session_resources *res)
169 {
170         uint32_t dev_id, res_type, i;
171         enum tf_dir dir;
172         uint8_t app_id;
173         int32_t rc = 0;
174
175         if (ulp_ctx == NULL || res == NULL || info == NULL || num == 0) {
176                 BNXT_TF_DBG(ERR, "Invalid arguments to get resources.\n");
177                 return -EINVAL;
178         }
179
180         rc = bnxt_ulp_cntxt_app_id_get(ulp_ctx, &app_id);
181         if (rc) {
182                 BNXT_TF_DBG(ERR, "Unable to get the app id from ulp.\n");
183                 return -EINVAL;
184         }
185
186         rc = bnxt_ulp_cntxt_dev_id_get(ulp_ctx, &dev_id);
187         if (rc) {
188                 BNXT_TF_DBG(ERR, "Unable to get the dev id from ulp.\n");
189                 return -EINVAL;
190         }
191
192         for (i = 0; i < num; i++) {
193                 if (app_id != info[i].app_id || dev_id != info[i].device_id)
194                         continue;
195                 dir = info[i].direction;
196                 res_type = info[i].resource_type;
197
198                 switch (info[i].resource_func) {
199                 case BNXT_ULP_RESOURCE_FUNC_IDENTIFIER:
200                         res->ident_cnt[dir].cnt[res_type] = info[i].count;
201                         break;
202                 case BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE:
203                         res->tbl_cnt[dir].cnt[res_type] = info[i].count;
204                         break;
205                 case BNXT_ULP_RESOURCE_FUNC_TCAM_TABLE:
206                         res->tcam_cnt[dir].cnt[res_type] = info[i].count;
207                         break;
208                 case BNXT_ULP_RESOURCE_FUNC_EM_TABLE:
209                         res->em_cnt[dir].cnt[res_type] = info[i].count;
210                         break;
211                 default:
212                         break;
213                 }
214         }
215         return 0;
216 }
217
218 static int32_t
219 bnxt_ulp_tf_resources_get(struct bnxt_ulp_context *ulp_ctx,
220                           struct tf_session_resources *res)
221 {
222         struct bnxt_ulp_resource_resv_info *unnamed = NULL;
223         uint32_t unum;
224         int32_t rc = 0;
225
226         if (ulp_ctx == NULL || res == NULL) {
227                 BNXT_TF_DBG(ERR, "Invalid arguments to get resources.\n");
228                 return -EINVAL;
229         }
230
231         unnamed = bnxt_ulp_resource_resv_list_get(&unum);
232         if (unnamed == NULL) {
233                 BNXT_TF_DBG(ERR, "Unable to get resource resv list.\n");
234                 return -EINVAL;
235         }
236
237         rc = bnxt_ulp_unnamed_resources_calc(ulp_ctx, unnamed, unum, res);
238         if (rc)
239                 BNXT_TF_DBG(ERR, "Unable to calc resources for session.\n");
240
241         return rc;
242 }
243
244 static int32_t
245 bnxt_ulp_tf_shared_session_resources_get(struct bnxt_ulp_context *ulp_ctx,
246                                          struct tf_session_resources *res)
247 {
248         struct bnxt_ulp_resource_resv_info *unnamed;
249         struct bnxt_ulp_glb_resource_info *named;
250         uint32_t unum, nnum;
251         int32_t rc;
252
253         if (ulp_ctx == NULL || res == NULL) {
254                 BNXT_TF_DBG(ERR, "Invalid arguments to get resources.\n");
255                 return -EINVAL;
256         }
257
258         /* Make sure the resources are zero before accumulating. */
259         memset(res, 0, sizeof(struct tf_session_resources));
260
261         /*
262          * Shared resources are comprised of both named and unnamed resources.
263          * First get the unnamed counts, and then add the named to the result.
264          */
265         /* Get the baseline counts */
266         unnamed = bnxt_ulp_app_resource_resv_list_get(&unum);
267         if (unnamed == NULL) {
268                 BNXT_TF_DBG(ERR, "Unable to get shared resource resv list.\n");
269                 return -EINVAL;
270         }
271         rc = bnxt_ulp_unnamed_resources_calc(ulp_ctx, unnamed, unum, res);
272         if (rc) {
273                 BNXT_TF_DBG(ERR, "Unable to calc resources for shared session.\n");
274                 return -EINVAL;
275         }
276
277         /* Get the named list and add the totals */
278         named = bnxt_ulp_app_glb_resource_info_list_get(&nnum);
279         if (named == NULL) {
280                 BNXT_TF_DBG(ERR, "Unable to get app global resource list\n");
281                 return -EINVAL;
282         }
283         rc = bnxt_ulp_named_resources_calc(ulp_ctx, named, nnum, res);
284         if (rc)
285                 BNXT_TF_DBG(ERR, "Unable to calc named resources\n");
286
287         return rc;
288 }
289
290 int32_t
291 bnxt_ulp_cntxt_app_caps_init(struct bnxt_ulp_context *ulp_ctx,
292                              uint8_t app_id, uint32_t dev_id)
293 {
294         struct bnxt_ulp_app_capabilities_info *info;
295         uint32_t num = 0;
296         uint16_t i;
297         bool found = false;
298
299         if (ULP_APP_DEV_UNSUPPORTED_ENABLED(ulp_ctx->cfg_data->ulp_flags)) {
300                 BNXT_TF_DBG(ERR, "APP ID %d, Device ID: 0x%x not supported.\n",
301                             app_id, dev_id);
302                 return -EINVAL;
303         }
304
305         info = bnxt_ulp_app_cap_list_get(&num);
306         if (!info || !num) {
307                 BNXT_TF_DBG(ERR, "Failed to get app capabilities.\n");
308                 return -EINVAL;
309         }
310
311         for (i = 0; i < num; i++) {
312                 if (info[i].app_id != app_id || info[i].device_id != dev_id)
313                         continue;
314                 found = true;
315                 if (info[i].flags & BNXT_ULP_APP_CAP_SHARED_EN)
316                         ulp_ctx->cfg_data->ulp_flags |=
317                                 BNXT_ULP_SHARED_SESSION_ENABLED;
318         }
319         if (!found) {
320                 BNXT_TF_DBG(ERR, "APP ID %d, Device ID: 0x%x not supported.\n",
321                             app_id, dev_id);
322                 ulp_ctx->cfg_data->ulp_flags |= BNXT_ULP_APP_DEV_UNSUPPORTED;
323                 return -EINVAL;
324         }
325
326         return 0;
327 }
328
329 static void
330 ulp_ctx_shared_session_close(struct bnxt *bp,
331                              struct bnxt_ulp_session_state *session)
332 {
333         struct tf *tfp;
334         int32_t rc;
335
336         if (!bnxt_ulp_cntxt_shared_session_enabled(bp->ulp_ctx))
337                 return;
338
339         tfp = bnxt_ulp_cntxt_shared_tfp_get(bp->ulp_ctx);
340         if (!tfp) {
341                 /*
342                  * Log it under debug since this is likely a case of the
343                  * shared session not being created.  For example, a failed
344                  * initialization.
345                  */
346                 BNXT_TF_DBG(DEBUG, "Failed to get shared tfp on close.\n");
347                 return;
348         }
349         rc = tf_close_session(tfp);
350         if (rc)
351                 BNXT_TF_DBG(ERR, "Failed to close the shared session rc=%d.\n",
352                             rc);
353         (void)bnxt_ulp_cntxt_shared_tfp_set(bp->ulp_ctx, NULL);
354
355         session->g_shared_tfp.session = NULL;
356 }
357
358 static int32_t
359 ulp_ctx_shared_session_open(struct bnxt *bp,
360                             struct bnxt_ulp_session_state *session)
361 {
362         struct rte_eth_dev *ethdev = bp->eth_dev;
363         struct tf_session_resources *resources;
364         struct tf_open_session_parms parms;
365         size_t copy_num_bytes;
366         uint32_t ulp_dev_id;
367         int32_t rc = 0;
368
369         /* only perform this if shared session is enabled. */
370         if (!bnxt_ulp_cntxt_shared_session_enabled(bp->ulp_ctx))
371                 return 0;
372
373         memset(&parms, 0, sizeof(parms));
374
375         rc = rte_eth_dev_get_name_by_port(ethdev->data->port_id,
376                                           parms.ctrl_chan_name);
377         if (rc) {
378                 BNXT_TF_DBG(ERR, "Invalid port %d, rc = %d\n",
379                             ethdev->data->port_id, rc);
380                 return rc;
381         }
382         resources = &parms.resources;
383
384         /*
385          * Need to account for size of ctrl_chan_name and 1 extra for Null
386          * terminator
387          */
388         copy_num_bytes = sizeof(parms.ctrl_chan_name) -
389                 strlen(parms.ctrl_chan_name) - 1;
390
391         /* Build the ctrl_chan_name with shared token */
392         strncat(parms.ctrl_chan_name, "-tf_shared", copy_num_bytes);
393
394         rc = bnxt_ulp_tf_shared_session_resources_get(bp->ulp_ctx, resources);
395         if (rc)
396                 return rc;
397
398         rc = bnxt_ulp_cntxt_dev_id_get(bp->ulp_ctx, &ulp_dev_id);
399         if (rc) {
400                 BNXT_TF_DBG(ERR, "Unable to get device id from ulp.\n");
401                 return rc;
402         }
403
404         switch (ulp_dev_id) {
405         case BNXT_ULP_DEVICE_ID_WH_PLUS:
406                 parms.device_type = TF_DEVICE_TYPE_WH;
407                 break;
408         case BNXT_ULP_DEVICE_ID_STINGRAY:
409                 parms.device_type = TF_DEVICE_TYPE_SR;
410                 break;
411         case BNXT_ULP_DEVICE_ID_THOR:
412                 parms.device_type = TF_DEVICE_TYPE_THOR;
413                 break;
414         default:
415                 BNXT_TF_DBG(ERR, "Unable to determine dev for opening session.\n");
416                 return rc;
417         }
418
419         parms.shadow_copy = true;
420         parms.bp = bp;
421
422         /*
423          * Open the session here, but the collect the resources during the
424          * mapper initialization.
425          */
426         rc = tf_open_session(&bp->tfp_shared, &parms);
427         if (rc)
428                 return rc;
429
430         if (parms.shared_session_creator)
431                 BNXT_TF_DBG(DEBUG, "Shared session creator.\n");
432         else
433                 BNXT_TF_DBG(DEBUG, "Shared session attached.\n");
434
435         /* Save the shared session in global data */
436         if (!session->g_shared_tfp.session)
437                 session->g_shared_tfp.session = bp->tfp_shared.session;
438
439         rc = bnxt_ulp_cntxt_shared_tfp_set(bp->ulp_ctx, &bp->tfp_shared);
440         if (rc)
441                 BNXT_TF_DBG(ERR, "Failed to add shared tfp to ulp (%d)\n", rc);
442
443         return rc;
444 }
445
446 static int32_t
447 ulp_ctx_shared_session_attach(struct bnxt *bp,
448                               struct bnxt_ulp_session_state *session)
449 {
450         int32_t rc = 0;
451
452         /* Simply return success if shared session not enabled */
453         if (bnxt_ulp_cntxt_shared_session_enabled(bp->ulp_ctx)) {
454                 bp->tfp_shared.session = session->g_shared_tfp.session;
455                 rc = ulp_ctx_shared_session_open(bp, session);
456         }
457
458         return rc;
459 }
460
461 static void
462 ulp_ctx_shared_session_detach(struct bnxt *bp)
463 {
464         if (bnxt_ulp_cntxt_shared_session_enabled(bp->ulp_ctx)) {
465                 if (bp->tfp_shared.session) {
466                         tf_close_session(&bp->tfp_shared);
467                         bp->tfp_shared.session = NULL;
468                 }
469         }
470 }
471
472 /*
473  * Initialize an ULP session.
474  * An ULP session will contain all the resources needed to support rte flow
475  * offloads. A session is initialized as part of rte_eth_device start.
476  * A single vswitch instance can have multiple uplinks which means
477  * rte_eth_device start will be called for each of these devices.
478  * ULP session manager will make sure that a single ULP session is only
479  * initialized once. Apart from this, it also initializes MARK database,
480  * EEM table & flow database. ULP session manager also manages a list of
481  * all opened ULP sessions.
482  */
483 static int32_t
484 ulp_ctx_session_open(struct bnxt *bp,
485                      struct bnxt_ulp_session_state *session)
486 {
487         struct rte_eth_dev              *ethdev = bp->eth_dev;
488         int32_t                         rc = 0;
489         struct tf_open_session_parms    params;
490         struct tf_session_resources     *resources;
491         uint32_t                        ulp_dev_id;
492
493         memset(&params, 0, sizeof(params));
494
495         rc = rte_eth_dev_get_name_by_port(ethdev->data->port_id,
496                                           params.ctrl_chan_name);
497         if (rc) {
498                 BNXT_TF_DBG(ERR, "Invalid port %d, rc = %d\n",
499                             ethdev->data->port_id, rc);
500                 return rc;
501         }
502
503         params.shadow_copy = true;
504
505         rc = bnxt_ulp_cntxt_dev_id_get(bp->ulp_ctx, &ulp_dev_id);
506         if (rc) {
507                 BNXT_TF_DBG(ERR, "Unable to get device id from ulp.\n");
508                 return rc;
509         }
510
511         switch (ulp_dev_id) {
512         case BNXT_ULP_DEVICE_ID_WH_PLUS:
513                 params.device_type = TF_DEVICE_TYPE_WH;
514                 break;
515         case BNXT_ULP_DEVICE_ID_STINGRAY:
516                 params.device_type = TF_DEVICE_TYPE_SR;
517                 break;
518         case BNXT_ULP_DEVICE_ID_THOR:
519                 params.device_type = TF_DEVICE_TYPE_THOR;
520                 break;
521         default:
522                 BNXT_TF_DBG(ERR, "Unable to determine device for opening session.\n");
523                 return rc;
524         }
525
526         resources = &params.resources;
527         rc = bnxt_ulp_tf_resources_get(bp->ulp_ctx, resources);
528         if (rc)
529                 return rc;
530
531         params.bp = bp;
532         rc = tf_open_session(&bp->tfp, &params);
533         if (rc) {
534                 BNXT_TF_DBG(ERR, "Failed to open TF session - %s, rc = %d\n",
535                             params.ctrl_chan_name, rc);
536                 return -EINVAL;
537         }
538         if (!session->session_opened) {
539                 session->session_opened = 1;
540                 session->g_tfp = rte_zmalloc("bnxt_ulp_session_tfp",
541                                              sizeof(struct tf), 0);
542                 session->g_tfp->session = bp->tfp.session;
543         }
544         return rc;
545 }
546
547 /*
548  * Close the ULP session.
549  * It takes the ulp context pointer.
550  */
551 static void
552 ulp_ctx_session_close(struct bnxt *bp,
553                       struct bnxt_ulp_session_state *session)
554 {
555         /* close the session in the hardware */
556         if (session->session_opened)
557                 tf_close_session(&bp->tfp);
558         session->session_opened = 0;
559         rte_free(session->g_tfp);
560         session->g_tfp = NULL;
561 }
562
563 static void
564 bnxt_init_tbl_scope_parms(struct bnxt *bp,
565                           struct tf_alloc_tbl_scope_parms *params)
566 {
567         struct bnxt_ulp_device_params   *dparms;
568         uint32_t dev_id;
569         int rc;
570
571         rc = bnxt_ulp_cntxt_dev_id_get(bp->ulp_ctx, &dev_id);
572         if (rc)
573                 /* TBD: For now, just use default. */
574                 dparms = 0;
575         else
576                 dparms = bnxt_ulp_device_params_get(dev_id);
577
578         /*
579          * Set the flush timer for EEM entries. The value is in 100ms intervals,
580          * so 100 is 10s.
581          */
582         params->hw_flow_cache_flush_timer = 100;
583
584         if (!dparms) {
585                 params->rx_max_key_sz_in_bits = BNXT_ULP_DFLT_RX_MAX_KEY;
586                 params->rx_max_action_entry_sz_in_bits =
587                         BNXT_ULP_DFLT_RX_MAX_ACTN_ENTRY;
588                 params->rx_mem_size_in_mb = BNXT_ULP_DFLT_RX_MEM;
589                 params->rx_num_flows_in_k = BNXT_ULP_RX_NUM_FLOWS;
590
591                 params->tx_max_key_sz_in_bits = BNXT_ULP_DFLT_TX_MAX_KEY;
592                 params->tx_max_action_entry_sz_in_bits =
593                         BNXT_ULP_DFLT_TX_MAX_ACTN_ENTRY;
594                 params->tx_mem_size_in_mb = BNXT_ULP_DFLT_TX_MEM;
595                 params->tx_num_flows_in_k = BNXT_ULP_TX_NUM_FLOWS;
596         } else {
597                 params->rx_max_key_sz_in_bits = BNXT_ULP_DFLT_RX_MAX_KEY;
598                 params->rx_max_action_entry_sz_in_bits =
599                         BNXT_ULP_DFLT_RX_MAX_ACTN_ENTRY;
600                 params->rx_mem_size_in_mb = BNXT_ULP_DFLT_RX_MEM;
601                 params->rx_num_flows_in_k =
602                         dparms->ext_flow_db_num_entries / 1024;
603
604                 params->tx_max_key_sz_in_bits = BNXT_ULP_DFLT_TX_MAX_KEY;
605                 params->tx_max_action_entry_sz_in_bits =
606                         BNXT_ULP_DFLT_TX_MAX_ACTN_ENTRY;
607                 params->tx_mem_size_in_mb = BNXT_ULP_DFLT_TX_MEM;
608                 params->tx_num_flows_in_k =
609                         dparms->ext_flow_db_num_entries / 1024;
610         }
611         BNXT_TF_DBG(INFO, "Table Scope initialized with %uK flows.\n",
612                     params->rx_num_flows_in_k);
613 }
614
615 /* Initialize Extended Exact Match host memory. */
616 static int32_t
617 ulp_eem_tbl_scope_init(struct bnxt *bp)
618 {
619         struct tf_alloc_tbl_scope_parms params = {0};
620         struct bnxt_ulp_device_params *dparms;
621         enum bnxt_ulp_flow_mem_type mtype;
622         uint32_t dev_id;
623         int rc;
624
625         /* Get the dev specific number of flows that needed to be supported. */
626         if (bnxt_ulp_cntxt_dev_id_get(bp->ulp_ctx, &dev_id)) {
627                 BNXT_TF_DBG(ERR, "Invalid device id\n");
628                 return -EINVAL;
629         }
630
631         dparms = bnxt_ulp_device_params_get(dev_id);
632         if (!dparms) {
633                 BNXT_TF_DBG(ERR, "could not fetch the device params\n");
634                 return -ENODEV;
635         }
636
637         if (bnxt_ulp_cntxt_mem_type_get(bp->ulp_ctx, &mtype))
638                 return -EINVAL;
639         if (mtype != BNXT_ULP_FLOW_MEM_TYPE_EXT) {
640                 BNXT_TF_DBG(INFO, "Table Scope alloc is not required\n");
641                 return 0;
642         }
643
644         bnxt_init_tbl_scope_parms(bp, &params);
645         rc = tf_alloc_tbl_scope(&bp->tfp, &params);
646         if (rc) {
647                 BNXT_TF_DBG(ERR, "Unable to allocate eem table scope rc = %d\n",
648                             rc);
649                 return rc;
650         }
651         rc = bnxt_ulp_cntxt_tbl_scope_id_set(bp->ulp_ctx, params.tbl_scope_id);
652         if (rc) {
653                 BNXT_TF_DBG(ERR, "Unable to set table scope id\n");
654                 return rc;
655         }
656
657         return 0;
658 }
659
660 /* Free Extended Exact Match host memory */
661 static int32_t
662 ulp_eem_tbl_scope_deinit(struct bnxt *bp, struct bnxt_ulp_context *ulp_ctx)
663 {
664         struct tf_free_tbl_scope_parms  params = {0};
665         struct tf                       *tfp;
666         int32_t                         rc = 0;
667         struct bnxt_ulp_device_params *dparms;
668         enum bnxt_ulp_flow_mem_type mtype;
669         uint32_t dev_id;
670
671         if (!ulp_ctx || !ulp_ctx->cfg_data)
672                 return -EINVAL;
673
674         tfp = bnxt_ulp_cntxt_tfp_get(ulp_ctx, BNXT_ULP_SHARED_SESSION_NO);
675         if (!tfp) {
676                 BNXT_TF_DBG(ERR, "Failed to get the truflow pointer\n");
677                 return -EINVAL;
678         }
679
680         /* Get the dev specific number of flows that needed to be supported. */
681         if (bnxt_ulp_cntxt_dev_id_get(bp->ulp_ctx, &dev_id)) {
682                 BNXT_TF_DBG(ERR, "Invalid device id\n");
683                 return -EINVAL;
684         }
685
686         dparms = bnxt_ulp_device_params_get(dev_id);
687         if (!dparms) {
688                 BNXT_TF_DBG(ERR, "could not fetch the device params\n");
689                 return -ENODEV;
690         }
691
692         if (bnxt_ulp_cntxt_mem_type_get(ulp_ctx, &mtype))
693                 return -EINVAL;
694         if (mtype != BNXT_ULP_FLOW_MEM_TYPE_EXT) {
695                 BNXT_TF_DBG(INFO, "Table Scope free is not required\n");
696                 return 0;
697         }
698
699         rc = bnxt_ulp_cntxt_tbl_scope_id_get(ulp_ctx, &params.tbl_scope_id);
700         if (rc) {
701                 BNXT_TF_DBG(ERR, "Failed to get the table scope id\n");
702                 return -EINVAL;
703         }
704
705         rc = tf_free_tbl_scope(tfp, &params);
706         if (rc) {
707                 BNXT_TF_DBG(ERR, "Unable to free table scope\n");
708                 return -EINVAL;
709         }
710         return rc;
711 }
712
713 /* The function to free and deinit the ulp context data. */
714 static int32_t
715 ulp_ctx_deinit(struct bnxt *bp,
716                struct bnxt_ulp_session_state *session)
717 {
718         /* close the tf session */
719         ulp_ctx_session_close(bp, session);
720
721         /* The shared session must be closed last. */
722         ulp_ctx_shared_session_close(bp, session);
723
724         /* Free the contents */
725         if (session->cfg_data) {
726                 rte_free(session->cfg_data);
727                 bp->ulp_ctx->cfg_data = NULL;
728                 session->cfg_data = NULL;
729         }
730         return 0;
731 }
732
733 /* The function to allocate and initialize the ulp context data. */
734 static int32_t
735 ulp_ctx_init(struct bnxt *bp,
736              struct bnxt_ulp_session_state *session)
737 {
738         struct bnxt_ulp_data    *ulp_data;
739         int32_t                 rc = 0;
740         enum bnxt_ulp_device_id devid;
741
742         /* Allocate memory to hold ulp context data. */
743         ulp_data = rte_zmalloc("bnxt_ulp_data",
744                                sizeof(struct bnxt_ulp_data), 0);
745         if (!ulp_data) {
746                 BNXT_TF_DBG(ERR, "Failed to allocate memory for ulp data\n");
747                 return -ENOMEM;
748         }
749
750         /* Increment the ulp context data reference count usage. */
751         bp->ulp_ctx->cfg_data = ulp_data;
752         session->cfg_data = ulp_data;
753         ulp_data->ref_cnt++;
754         ulp_data->ulp_flags |= BNXT_ULP_VF_REP_ENABLED;
755
756         rc = bnxt_ulp_devid_get(bp, &devid);
757         if (rc) {
758                 BNXT_TF_DBG(ERR, "Unable to determine device for ULP init.\n");
759                 goto error_deinit;
760         }
761
762         rc = bnxt_ulp_cntxt_dev_id_set(bp->ulp_ctx, devid);
763         if (rc) {
764                 BNXT_TF_DBG(ERR, "Unable to set device for ULP init.\n");
765                 goto error_deinit;
766         }
767
768         rc = bnxt_ulp_cntxt_app_id_set(bp->ulp_ctx, bp->app_id);
769         if (rc) {
770                 BNXT_TF_DBG(ERR, "Unable to set app_id for ULP init.\n");
771                 goto error_deinit;
772         }
773
774         rc = bnxt_ulp_cntxt_app_caps_init(bp->ulp_ctx, bp->app_id, devid);
775         if (rc) {
776                 BNXT_TF_DBG(ERR, "Unable to set caps for app(%x)/dev(%x)\n",
777                             bp->app_id, devid);
778                 goto error_deinit;
779         }
780
781         /*
782          * Shared session must be created before first regular session but after
783          * the ulp_ctx is valid.
784          */
785         rc = ulp_ctx_shared_session_open(bp, session);
786         if (rc) {
787                 BNXT_TF_DBG(ERR, "Unable to open shared session (%d)\n", rc);
788                 goto error_deinit;
789         }
790
791         /* Open the ulp session. */
792         rc = ulp_ctx_session_open(bp, session);
793         if (rc)
794                 goto error_deinit;
795
796         ulp_tun_tbl_init(ulp_data->tun_tbl);
797
798         bnxt_ulp_cntxt_tfp_set(bp->ulp_ctx, &bp->tfp);
799         return rc;
800
801 error_deinit:
802         session->session_opened = 1;
803         (void)ulp_ctx_deinit(bp, session);
804         return rc;
805 }
806
807 /* The function to initialize ulp dparms with devargs */
808 static int32_t
809 ulp_dparms_init(struct bnxt *bp, struct bnxt_ulp_context *ulp_ctx)
810 {
811         struct bnxt_ulp_device_params *dparms;
812         uint32_t dev_id;
813
814         if (!bp->max_num_kflows) {
815                 /* Defaults to Internal */
816                 bnxt_ulp_cntxt_mem_type_set(ulp_ctx,
817                                             BNXT_ULP_FLOW_MEM_TYPE_INT);
818                 return 0;
819         }
820
821         /* The max_num_kflows were set, so move to external */
822         if (bnxt_ulp_cntxt_mem_type_set(ulp_ctx, BNXT_ULP_FLOW_MEM_TYPE_EXT))
823                 return -EINVAL;
824
825         if (bnxt_ulp_cntxt_dev_id_get(ulp_ctx, &dev_id)) {
826                 BNXT_TF_DBG(DEBUG, "Failed to get device id\n");
827                 return -EINVAL;
828         }
829
830         dparms = bnxt_ulp_device_params_get(dev_id);
831         if (!dparms) {
832                 BNXT_TF_DBG(DEBUG, "Failed to get device parms\n");
833                 return -EINVAL;
834         }
835
836         /* num_flows = max_num_kflows * 1024 */
837         dparms->ext_flow_db_num_entries = bp->max_num_kflows * 1024;
838         /* GFID =  2 * num_flows */
839         dparms->mark_db_gfid_entries = dparms->ext_flow_db_num_entries * 2;
840         BNXT_TF_DBG(DEBUG, "Set the number of flows = %"PRIu64"\n",
841                     dparms->ext_flow_db_num_entries);
842
843         return 0;
844 }
845
846 /* The function to initialize bp flags with truflow features */
847 static int32_t
848 ulp_dparms_dev_port_intf_update(struct bnxt *bp,
849                                 struct bnxt_ulp_context *ulp_ctx)
850 {
851         enum bnxt_ulp_flow_mem_type mtype;
852
853         if (bnxt_ulp_cntxt_mem_type_get(ulp_ctx, &mtype))
854                 return -EINVAL;
855         /* Update the bp flag with gfid flag */
856         if (mtype == BNXT_ULP_FLOW_MEM_TYPE_EXT)
857                 bp->flags |= BNXT_FLAG_GFID_ENABLE;
858
859         return 0;
860 }
861
862 static int32_t
863 ulp_ctx_attach(struct bnxt *bp,
864                struct bnxt_ulp_session_state *session)
865 {
866         int32_t rc = 0;
867         uint32_t flags, dev_id;
868         uint8_t app_id;
869
870         /* Increment the ulp context data reference count usage. */
871         bp->ulp_ctx->cfg_data = session->cfg_data;
872         bp->ulp_ctx->cfg_data->ref_cnt++;
873
874         /* update the session details in bnxt tfp */
875         bp->tfp.session = session->g_tfp->session;
876
877         /*
878          * The supported flag will be set during the init. Use it now to
879          * know if we should go through the attach.
880          */
881         rc = bnxt_ulp_cntxt_app_id_get(bp->ulp_ctx, &app_id);
882         if (rc) {
883                 BNXT_TF_DBG(ERR, "Unable to get the app id from ulp.\n");
884                 return -EINVAL;
885         }
886
887         rc = bnxt_ulp_cntxt_dev_id_get(bp->ulp_ctx, &dev_id);
888         if (rc) {
889                 BNXT_TF_DBG(ERR, "Unable do get the dev_id.\n");
890                 return -EINVAL;
891         }
892
893         flags = bp->ulp_ctx->cfg_data->ulp_flags;
894         if (ULP_APP_DEV_UNSUPPORTED_ENABLED(flags)) {
895                 BNXT_TF_DBG(ERR, "APP ID %d, Device ID: 0x%x not supported.\n",
896                             app_id, dev_id);
897                 return -EINVAL;
898         }
899
900         /* Create a TF Client */
901         rc = ulp_ctx_session_open(bp, session);
902         if (rc) {
903                 PMD_DRV_LOG(ERR, "Failed to open ctxt session, rc:%d\n", rc);
904                 bp->tfp.session = NULL;
905                 return rc;
906         }
907
908         bnxt_ulp_cntxt_tfp_set(bp->ulp_ctx, &bp->tfp);
909         return rc;
910 }
911
912 static void
913 ulp_ctx_detach(struct bnxt *bp)
914 {
915         if (bp->tfp.session) {
916                 tf_close_session(&bp->tfp);
917                 bp->tfp.session = NULL;
918         }
919 }
920
921 /*
922  * Initialize the state of an ULP session.
923  * If the state of an ULP session is not initialized, set it's state to
924  * initialized. If the state is already initialized, do nothing.
925  */
926 static void
927 ulp_context_initialized(struct bnxt_ulp_session_state *session, bool *init)
928 {
929         pthread_mutex_lock(&session->bnxt_ulp_mutex);
930
931         if (!session->bnxt_ulp_init) {
932                 session->bnxt_ulp_init = true;
933                 *init = false;
934         } else {
935                 *init = true;
936         }
937
938         pthread_mutex_unlock(&session->bnxt_ulp_mutex);
939 }
940
941 /*
942  * Check if an ULP session is already allocated for a specific PCI
943  * domain & bus. If it is already allocated simply return the session
944  * pointer, otherwise allocate a new session.
945  */
946 static struct bnxt_ulp_session_state *
947 ulp_get_session(struct rte_pci_addr *pci_addr)
948 {
949         struct bnxt_ulp_session_state *session;
950
951         STAILQ_FOREACH(session, &bnxt_ulp_session_list, next) {
952                 if (session->pci_info.domain == pci_addr->domain &&
953                     session->pci_info.bus == pci_addr->bus) {
954                         return session;
955                 }
956         }
957         return NULL;
958 }
959
960 /*
961  * Allocate and Initialize an ULP session and set it's state to INITIALIZED.
962  * If it's already initialized simply return the already existing session.
963  */
964 static struct bnxt_ulp_session_state *
965 ulp_session_init(struct bnxt *bp,
966                  bool *init)
967 {
968         struct rte_pci_device           *pci_dev;
969         struct rte_pci_addr             *pci_addr;
970         struct bnxt_ulp_session_state   *session;
971         int rc = 0;
972
973         if (!bp)
974                 return NULL;
975
976         pci_dev = RTE_DEV_TO_PCI(bp->eth_dev->device);
977         pci_addr = &pci_dev->addr;
978
979         pthread_mutex_lock(&bnxt_ulp_global_mutex);
980
981         session = ulp_get_session(pci_addr);
982         if (!session) {
983                 /* Not Found the session  Allocate a new one */
984                 session = rte_zmalloc("bnxt_ulp_session",
985                                       sizeof(struct bnxt_ulp_session_state),
986                                       0);
987                 if (!session) {
988                         BNXT_TF_DBG(ERR,
989                                     "Allocation failed for bnxt_ulp_session\n");
990                         pthread_mutex_unlock(&bnxt_ulp_global_mutex);
991                         return NULL;
992
993                 } else {
994                         /* Add it to the queue */
995                         session->pci_info.domain = pci_addr->domain;
996                         session->pci_info.bus = pci_addr->bus;
997                         rc = pthread_mutex_init(&session->bnxt_ulp_mutex, NULL);
998                         if (rc) {
999                                 BNXT_TF_DBG(ERR, "mutex create failed\n");
1000                                 pthread_mutex_unlock(&bnxt_ulp_global_mutex);
1001                                 return NULL;
1002                         }
1003                         STAILQ_INSERT_TAIL(&bnxt_ulp_session_list,
1004                                            session, next);
1005                 }
1006         }
1007         ulp_context_initialized(session, init);
1008         pthread_mutex_unlock(&bnxt_ulp_global_mutex);
1009         return session;
1010 }
1011
1012 /*
1013  * When a device is closed, remove it's associated session from the global
1014  * session list.
1015  */
1016 static void
1017 ulp_session_deinit(struct bnxt_ulp_session_state *session)
1018 {
1019         if (!session)
1020                 return;
1021
1022         if (!session->cfg_data) {
1023                 pthread_mutex_lock(&bnxt_ulp_global_mutex);
1024                 STAILQ_REMOVE(&bnxt_ulp_session_list, session,
1025                               bnxt_ulp_session_state, next);
1026                 pthread_mutex_destroy(&session->bnxt_ulp_mutex);
1027                 rte_free(session);
1028                 pthread_mutex_unlock(&bnxt_ulp_global_mutex);
1029         }
1030 }
1031
1032 /*
1033  * Internal api to enable NAT feature.
1034  * Set set_flag to 1 to set the value or zero to reset the value.
1035  * returns 0 on success.
1036  */
1037 static int32_t
1038 bnxt_ulp_global_cfg_update(struct bnxt *bp,
1039                            enum tf_dir dir,
1040                            enum tf_global_config_type type,
1041                            uint32_t offset,
1042                            uint32_t value,
1043                            uint32_t set_flag)
1044 {
1045         uint32_t global_cfg = 0;
1046         int rc;
1047         struct tf_global_cfg_parms parms = { 0 };
1048
1049         /* Initialize the params */
1050         parms.dir = dir,
1051         parms.type = type,
1052         parms.offset = offset,
1053         parms.config = (uint8_t *)&global_cfg,
1054         parms.config_sz_in_bytes = sizeof(global_cfg);
1055
1056         rc = tf_get_global_cfg(&bp->tfp, &parms);
1057         if (rc) {
1058                 BNXT_TF_DBG(ERR, "Failed to get global cfg 0x%x rc:%d\n",
1059                             type, rc);
1060                 return rc;
1061         }
1062
1063         if (set_flag)
1064                 global_cfg |= value;
1065         else
1066                 global_cfg &= ~value;
1067
1068         /* SET the register RE_CFA_REG_ACT_TECT */
1069         rc = tf_set_global_cfg(&bp->tfp, &parms);
1070         if (rc) {
1071                 BNXT_TF_DBG(ERR, "Failed to set global cfg 0x%x rc:%d\n",
1072                             type, rc);
1073                 return rc;
1074         }
1075         return rc;
1076 }
1077
1078 /* Internal function to delete all the flows belonging to the given port */
1079 static void
1080 bnxt_ulp_flush_port_flows(struct bnxt *bp)
1081 {
1082         uint16_t func_id;
1083
1084         /* it is assumed that port is either TVF or PF */
1085         if (ulp_port_db_port_func_id_get(bp->ulp_ctx,
1086                                          bp->eth_dev->data->port_id,
1087                                          &func_id)) {
1088                 BNXT_TF_DBG(ERR, "Invalid argument\n");
1089                 return;
1090         }
1091         (void)ulp_flow_db_function_flow_flush(bp->ulp_ctx, func_id);
1092 }
1093
1094 /* Internal function to delete the VFR default flows */
1095 static void
1096 bnxt_ulp_destroy_vfr_default_rules(struct bnxt *bp, bool global)
1097 {
1098         struct bnxt_ulp_vfr_rule_info *info;
1099         uint16_t port_id;
1100         struct rte_eth_dev *vfr_eth_dev;
1101         struct bnxt_representor *vfr_bp;
1102
1103         if (!BNXT_TRUFLOW_EN(bp) || BNXT_ETH_DEV_IS_REPRESENTOR(bp->eth_dev))
1104                 return;
1105
1106         if (!bp->ulp_ctx || !bp->ulp_ctx->cfg_data)
1107                 return;
1108
1109         /* Delete default rules for all ports */
1110         for (port_id = 0; port_id < RTE_MAX_ETHPORTS; port_id++) {
1111                 info = &bp->ulp_ctx->cfg_data->vfr_rule_info[port_id];
1112                 if (!info->valid)
1113                         continue;
1114
1115                 if (!global && info->parent_port_id !=
1116                     bp->eth_dev->data->port_id)
1117                         continue;
1118
1119                 /* Destroy the flows */
1120                 ulp_default_flow_destroy(bp->eth_dev, info->vfr_flow_id);
1121                 /* Clean up the tx action pointer */
1122                 vfr_eth_dev = &rte_eth_devices[port_id];
1123                 if (vfr_eth_dev) {
1124                         vfr_bp = vfr_eth_dev->data->dev_private;
1125                         vfr_bp->vfr_tx_cfa_action = 0;
1126                 }
1127                 memset(info, 0, sizeof(struct bnxt_ulp_vfr_rule_info));
1128         }
1129 }
1130
1131 /*
1132  * When a port is deinit'ed by dpdk. This function is called
1133  * and this function clears the ULP context and rest of the
1134  * infrastructure associated with it.
1135  */
1136 static void
1137 bnxt_ulp_deinit(struct bnxt *bp,
1138                 struct bnxt_ulp_session_state *session)
1139 {
1140         if (!bp->ulp_ctx || !bp->ulp_ctx->cfg_data)
1141                 return;
1142
1143         /* clean up default flows */
1144         bnxt_ulp_destroy_df_rules(bp, true);
1145
1146         /* clean up default VFR flows */
1147         bnxt_ulp_destroy_vfr_default_rules(bp, true);
1148
1149         /* clean up regular flows */
1150         ulp_flow_db_flush_flows(bp->ulp_ctx, BNXT_ULP_FDB_TYPE_REGULAR);
1151
1152         /* cleanup the eem table scope */
1153         ulp_eem_tbl_scope_deinit(bp, bp->ulp_ctx);
1154
1155         /* cleanup the flow database */
1156         ulp_flow_db_deinit(bp->ulp_ctx);
1157
1158         /* Delete the Mark database */
1159         ulp_mark_db_deinit(bp->ulp_ctx);
1160
1161         /* cleanup the ulp mapper */
1162         ulp_mapper_deinit(bp->ulp_ctx);
1163
1164         /* Delete the Flow Counter Manager */
1165         ulp_fc_mgr_deinit(bp->ulp_ctx);
1166
1167         /* Delete the Port database */
1168         ulp_port_db_deinit(bp->ulp_ctx);
1169
1170         /* Disable NAT feature */
1171         (void)bnxt_ulp_global_cfg_update(bp, TF_DIR_RX, TF_TUNNEL_ENCAP,
1172                                          TF_TUNNEL_ENCAP_NAT,
1173                                          BNXT_ULP_NAT_OUTER_MOST_FLAGS, 0);
1174
1175         (void)bnxt_ulp_global_cfg_update(bp, TF_DIR_TX, TF_TUNNEL_ENCAP,
1176                                          TF_TUNNEL_ENCAP_NAT,
1177                                          BNXT_ULP_NAT_OUTER_MOST_FLAGS, 0);
1178
1179         /* free the flow db lock */
1180         pthread_mutex_destroy(&bp->ulp_ctx->cfg_data->flow_db_lock);
1181
1182         /* Delete the ulp context and tf session and free the ulp context */
1183         ulp_ctx_deinit(bp, session);
1184         BNXT_TF_DBG(DEBUG, "ulp ctx has been deinitialized\n");
1185 }
1186
1187 /*
1188  * When a port is initialized by dpdk. This functions is called
1189  * and this function initializes the ULP context and rest of the
1190  * infrastructure associated with it.
1191  */
1192 static int32_t
1193 bnxt_ulp_init(struct bnxt *bp,
1194               struct bnxt_ulp_session_state *session)
1195 {
1196         int rc;
1197
1198         /* Allocate and Initialize the ulp context. */
1199         rc = ulp_ctx_init(bp, session);
1200         if (rc) {
1201                 BNXT_TF_DBG(ERR, "Failed to create the ulp context\n");
1202                 goto jump_to_error;
1203         }
1204
1205         rc = pthread_mutex_init(&bp->ulp_ctx->cfg_data->flow_db_lock, NULL);
1206         if (rc) {
1207                 BNXT_TF_DBG(ERR, "Unable to initialize flow db lock\n");
1208                 goto jump_to_error;
1209         }
1210
1211         /* Initialize ulp dparms with values devargs passed */
1212         rc = ulp_dparms_init(bp, bp->ulp_ctx);
1213         if (rc) {
1214                 BNXT_TF_DBG(ERR, "Failed to initialize the dparms\n");
1215                 goto jump_to_error;
1216         }
1217
1218         /* create the port database */
1219         rc = ulp_port_db_init(bp->ulp_ctx, bp->port_cnt);
1220         if (rc) {
1221                 BNXT_TF_DBG(ERR, "Failed to create the port database\n");
1222                 goto jump_to_error;
1223         }
1224
1225         /* Create the Mark database. */
1226         rc = ulp_mark_db_init(bp->ulp_ctx);
1227         if (rc) {
1228                 BNXT_TF_DBG(ERR, "Failed to create the mark database\n");
1229                 goto jump_to_error;
1230         }
1231
1232         /* Create the flow database. */
1233         rc = ulp_flow_db_init(bp->ulp_ctx);
1234         if (rc) {
1235                 BNXT_TF_DBG(ERR, "Failed to create the flow database\n");
1236                 goto jump_to_error;
1237         }
1238
1239         /* Create the eem table scope. */
1240         rc = ulp_eem_tbl_scope_init(bp);
1241         if (rc) {
1242                 BNXT_TF_DBG(ERR, "Failed to create the eem scope table\n");
1243                 goto jump_to_error;
1244         }
1245
1246         rc = ulp_mapper_init(bp->ulp_ctx);
1247         if (rc) {
1248                 BNXT_TF_DBG(ERR, "Failed to initialize ulp mapper\n");
1249                 goto jump_to_error;
1250         }
1251
1252         rc = ulp_fc_mgr_init(bp->ulp_ctx);
1253         if (rc) {
1254                 BNXT_TF_DBG(ERR, "Failed to initialize ulp flow counter mgr\n");
1255                 goto jump_to_error;
1256         }
1257
1258         /*
1259          * Enable NAT feature. Set the global configuration register
1260          * Tunnel encap to enable NAT with the reuse of existing inner
1261          * L2 header smac and dmac
1262          */
1263         rc = bnxt_ulp_global_cfg_update(bp, TF_DIR_RX, TF_TUNNEL_ENCAP,
1264                                         TF_TUNNEL_ENCAP_NAT,
1265                                         BNXT_ULP_NAT_OUTER_MOST_FLAGS, 1);
1266         if (rc) {
1267                 BNXT_TF_DBG(ERR, "Failed to set rx global configuration\n");
1268                 goto jump_to_error;
1269         }
1270
1271         rc = bnxt_ulp_global_cfg_update(bp, TF_DIR_TX, TF_TUNNEL_ENCAP,
1272                                         TF_TUNNEL_ENCAP_NAT,
1273                                         BNXT_ULP_NAT_OUTER_MOST_FLAGS, 1);
1274         if (rc) {
1275                 BNXT_TF_DBG(ERR, "Failed to set tx global configuration\n");
1276                 goto jump_to_error;
1277         }
1278         BNXT_TF_DBG(DEBUG, "ulp ctx has been initialized\n");
1279         return rc;
1280
1281 jump_to_error:
1282         bnxt_ulp_deinit(bp, session);
1283         return rc;
1284 }
1285
1286 /*
1287  * When a port is initialized by dpdk. This functions sets up
1288  * the port specific details.
1289  */
1290 int32_t
1291 bnxt_ulp_port_init(struct bnxt *bp)
1292 {
1293         struct bnxt_ulp_session_state *session;
1294         bool initialized;
1295         int32_t rc = 0;
1296
1297         if (!bp || !BNXT_TRUFLOW_EN(bp))
1298                 return rc;
1299
1300         if (!BNXT_PF(bp) && !BNXT_VF_IS_TRUSTED(bp)) {
1301                 BNXT_TF_DBG(ERR,
1302                             "Skip ulp init for port: %d, not a TVF or PF\n",
1303                         bp->eth_dev->data->port_id);
1304                 return rc;
1305         }
1306
1307         if (bp->ulp_ctx) {
1308                 BNXT_TF_DBG(DEBUG, "ulp ctx already allocated\n");
1309                 return rc;
1310         }
1311
1312         bp->ulp_ctx = rte_zmalloc("bnxt_ulp_ctx",
1313                                   sizeof(struct bnxt_ulp_context), 0);
1314         if (!bp->ulp_ctx) {
1315                 BNXT_TF_DBG(ERR, "Failed to allocate ulp ctx\n");
1316                 return -ENOMEM;
1317         }
1318
1319         /*
1320          * Multiple uplink ports can be associated with a single vswitch.
1321          * Make sure only the port that is started first will initialize
1322          * the TF session.
1323          */
1324         session = ulp_session_init(bp, &initialized);
1325         if (!session) {
1326                 BNXT_TF_DBG(ERR, "Failed to initialize the tf session\n");
1327                 rc = -EIO;
1328                 goto jump_to_error;
1329         }
1330
1331         if (initialized) {
1332                 /*
1333                  * If ULP is already initialized for a specific domain then
1334                  * simply assign the ulp context to this rte_eth_dev.
1335                  */
1336                 rc = ulp_ctx_attach(bp, session);
1337                 if (rc) {
1338                         BNXT_TF_DBG(ERR, "Failed to attach the ulp context\n");
1339                         goto jump_to_error;
1340                 }
1341
1342                 /*
1343                  * Attach to the shared session, must be called after the
1344                  * ulp_ctx_attach in order to ensure that ulp data is available
1345                  * for attaching.
1346                  */
1347                 rc = ulp_ctx_shared_session_attach(bp, session);
1348                 if (rc) {
1349                         BNXT_TF_DBG(ERR,
1350                                     "Failed attach to shared session (%d)", rc);
1351                         goto jump_to_error;
1352                 }
1353         } else {
1354                 rc = bnxt_ulp_init(bp, session);
1355                 if (rc) {
1356                         BNXT_TF_DBG(ERR, "Failed to initialize the ulp init\n");
1357                         goto jump_to_error;
1358                 }
1359         }
1360
1361         /* Update bnxt driver flags */
1362         rc = ulp_dparms_dev_port_intf_update(bp, bp->ulp_ctx);
1363         if (rc) {
1364                 BNXT_TF_DBG(ERR, "Failed to update driver flags\n");
1365                 goto jump_to_error;
1366         }
1367
1368         /* update the port database for the given interface */
1369         rc = ulp_port_db_dev_port_intf_update(bp->ulp_ctx, bp->eth_dev);
1370         if (rc) {
1371                 BNXT_TF_DBG(ERR, "Failed to update port database\n");
1372                 goto jump_to_error;
1373         }
1374         /* create the default rules */
1375         rc = bnxt_ulp_create_df_rules(bp);
1376         if (rc) {
1377                 BNXT_TF_DBG(ERR, "Failed to create default flow\n");
1378                 goto jump_to_error;
1379         }
1380
1381         if (BNXT_ACCUM_STATS_EN(bp))
1382                 bp->ulp_ctx->cfg_data->accum_stats = true;
1383
1384         BNXT_TF_DBG(DEBUG, "BNXT Port:%d ULP port init\n",
1385                     bp->eth_dev->data->port_id);
1386         return rc;
1387
1388 jump_to_error:
1389         bnxt_ulp_port_deinit(bp);
1390         return rc;
1391 }
1392
1393 /*
1394  * When a port is de-initialized by dpdk. This functions clears up
1395  * the port specific details.
1396  */
1397 void
1398 bnxt_ulp_port_deinit(struct bnxt *bp)
1399 {
1400         struct bnxt_ulp_session_state *session;
1401         struct rte_pci_device *pci_dev;
1402         struct rte_pci_addr *pci_addr;
1403
1404         if (!BNXT_TRUFLOW_EN(bp))
1405                 return;
1406
1407         if (!BNXT_PF(bp) && !BNXT_VF_IS_TRUSTED(bp)) {
1408                 BNXT_TF_DBG(ERR,
1409                             "Skip ULP deinit port:%d, not a TVF or PF\n",
1410                             bp->eth_dev->data->port_id);
1411                 return;
1412         }
1413
1414         if (!bp->ulp_ctx) {
1415                 BNXT_TF_DBG(DEBUG, "ulp ctx already de-allocated\n");
1416                 return;
1417         }
1418
1419         BNXT_TF_DBG(DEBUG, "BNXT Port:%d ULP port deinit\n",
1420                     bp->eth_dev->data->port_id);
1421
1422         /* Get the session details  */
1423         pci_dev = RTE_DEV_TO_PCI(bp->eth_dev->device);
1424         pci_addr = &pci_dev->addr;
1425         pthread_mutex_lock(&bnxt_ulp_global_mutex);
1426         session = ulp_get_session(pci_addr);
1427         pthread_mutex_unlock(&bnxt_ulp_global_mutex);
1428
1429         /* session not found then just exit */
1430         if (!session) {
1431                 /* Free the ulp context */
1432                 rte_free(bp->ulp_ctx);
1433                 bp->ulp_ctx = NULL;
1434                 return;
1435         }
1436
1437         /* Check the reference count to deinit or deattach*/
1438         if (bp->ulp_ctx->cfg_data && bp->ulp_ctx->cfg_data->ref_cnt) {
1439                 bp->ulp_ctx->cfg_data->ref_cnt--;
1440                 if (bp->ulp_ctx->cfg_data->ref_cnt) {
1441                         /* free the port details */
1442                         /* Free the default flow rule associated to this port */
1443                         bnxt_ulp_destroy_df_rules(bp, false);
1444                         bnxt_ulp_destroy_vfr_default_rules(bp, false);
1445
1446                         /* free flows associated with this port */
1447                         bnxt_ulp_flush_port_flows(bp);
1448
1449                         /* close the session associated with this port */
1450                         ulp_ctx_detach(bp);
1451
1452                         /* always detach/close shared after the session. */
1453                         ulp_ctx_shared_session_detach(bp);
1454                 } else {
1455                         /* Perform ulp ctx deinit */
1456                         bnxt_ulp_deinit(bp, session);
1457                 }
1458         }
1459
1460         /* clean up the session */
1461         ulp_session_deinit(session);
1462
1463         /* Free the ulp context */
1464         rte_free(bp->ulp_ctx);
1465         bp->ulp_ctx = NULL;
1466 }
1467
1468 /* Below are the access functions to access internal data of ulp context. */
1469 /* Function to set the Mark DB into the context */
1470 int32_t
1471 bnxt_ulp_cntxt_ptr2_mark_db_set(struct bnxt_ulp_context *ulp_ctx,
1472                                 struct bnxt_ulp_mark_tbl *mark_tbl)
1473 {
1474         if (!ulp_ctx || !ulp_ctx->cfg_data) {
1475                 BNXT_TF_DBG(ERR, "Invalid ulp context data\n");
1476                 return -EINVAL;
1477         }
1478
1479         ulp_ctx->cfg_data->mark_tbl = mark_tbl;
1480
1481         return 0;
1482 }
1483
1484 /* Function to retrieve the Mark DB from the context. */
1485 struct bnxt_ulp_mark_tbl *
1486 bnxt_ulp_cntxt_ptr2_mark_db_get(struct bnxt_ulp_context *ulp_ctx)
1487 {
1488         if (!ulp_ctx || !ulp_ctx->cfg_data)
1489                 return NULL;
1490
1491         return ulp_ctx->cfg_data->mark_tbl;
1492 }
1493
1494 bool
1495 bnxt_ulp_cntxt_shared_session_enabled(struct bnxt_ulp_context *ulp_ctx)
1496 {
1497         return ULP_SHARED_SESSION_IS_ENABLED(ulp_ctx->cfg_data->ulp_flags);
1498 }
1499
1500 int32_t
1501 bnxt_ulp_cntxt_app_id_set(struct bnxt_ulp_context *ulp_ctx, uint8_t app_id)
1502 {
1503         if (!ulp_ctx)
1504                 return -EINVAL;
1505         ulp_ctx->cfg_data->app_id = app_id;
1506         return 0;
1507 }
1508
1509 int32_t
1510 bnxt_ulp_cntxt_app_id_get(struct bnxt_ulp_context *ulp_ctx, uint8_t *app_id)
1511 {
1512         /* Default APP id is zero */
1513         if (!ulp_ctx || !app_id)
1514                 return -EINVAL;
1515         *app_id = ulp_ctx->cfg_data->app_id;
1516         return 0;
1517 }
1518
1519 /* Function to set the device id of the hardware. */
1520 int32_t
1521 bnxt_ulp_cntxt_dev_id_set(struct bnxt_ulp_context *ulp_ctx,
1522                           uint32_t dev_id)
1523 {
1524         if (ulp_ctx && ulp_ctx->cfg_data) {
1525                 ulp_ctx->cfg_data->dev_id = dev_id;
1526                 return 0;
1527         }
1528
1529         return -EINVAL;
1530 }
1531
1532 /* Function to get the device id of the hardware. */
1533 int32_t
1534 bnxt_ulp_cntxt_dev_id_get(struct bnxt_ulp_context *ulp_ctx,
1535                           uint32_t *dev_id)
1536 {
1537         if (ulp_ctx && ulp_ctx->cfg_data) {
1538                 *dev_id = ulp_ctx->cfg_data->dev_id;
1539                 return 0;
1540         }
1541
1542         BNXT_TF_DBG(ERR, "Failed to read dev_id from ulp ctxt\n");
1543         return -EINVAL;
1544 }
1545
1546 int32_t
1547 bnxt_ulp_cntxt_mem_type_set(struct bnxt_ulp_context *ulp_ctx,
1548                             enum bnxt_ulp_flow_mem_type mem_type)
1549 {
1550         if (ulp_ctx && ulp_ctx->cfg_data) {
1551                 ulp_ctx->cfg_data->mem_type = mem_type;
1552                 return 0;
1553         }
1554         BNXT_TF_DBG(ERR, "Failed to write mem_type in ulp ctxt\n");
1555         return -EINVAL;
1556 }
1557
1558 int32_t
1559 bnxt_ulp_cntxt_mem_type_get(struct bnxt_ulp_context *ulp_ctx,
1560                             enum bnxt_ulp_flow_mem_type *mem_type)
1561 {
1562         if (ulp_ctx && ulp_ctx->cfg_data) {
1563                 *mem_type = ulp_ctx->cfg_data->mem_type;
1564                 return 0;
1565         }
1566         BNXT_TF_DBG(ERR, "Failed to read mem_type in ulp ctxt\n");
1567         return -EINVAL;
1568 }
1569
1570 /* Function to get the table scope id of the EEM table. */
1571 int32_t
1572 bnxt_ulp_cntxt_tbl_scope_id_get(struct bnxt_ulp_context *ulp_ctx,
1573                                 uint32_t *tbl_scope_id)
1574 {
1575         if (ulp_ctx && ulp_ctx->cfg_data) {
1576                 *tbl_scope_id = ulp_ctx->cfg_data->tbl_scope_id;
1577                 return 0;
1578         }
1579
1580         return -EINVAL;
1581 }
1582
1583 /* Function to set the table scope id of the EEM table. */
1584 int32_t
1585 bnxt_ulp_cntxt_tbl_scope_id_set(struct bnxt_ulp_context *ulp_ctx,
1586                                 uint32_t tbl_scope_id)
1587 {
1588         if (ulp_ctx && ulp_ctx->cfg_data) {
1589                 ulp_ctx->cfg_data->tbl_scope_id = tbl_scope_id;
1590                 return 0;
1591         }
1592
1593         return -EINVAL;
1594 }
1595
1596 /* Function to set the shared tfp session details from the ulp context. */
1597 int32_t
1598 bnxt_ulp_cntxt_shared_tfp_set(struct bnxt_ulp_context *ulp, struct tf *tfp)
1599 {
1600         if (!ulp) {
1601                 BNXT_TF_DBG(ERR, "Invalid arguments\n");
1602                 return -EINVAL;
1603         }
1604
1605         ulp->g_shared_tfp = tfp;
1606         return 0;
1607 }
1608
1609 /* Function to get the shared tfp session details from the ulp context. */
1610 struct tf *
1611 bnxt_ulp_cntxt_shared_tfp_get(struct bnxt_ulp_context *ulp)
1612 {
1613         if (!ulp) {
1614                 BNXT_TF_DBG(ERR, "Invalid arguments\n");
1615                 return NULL;
1616         }
1617         return ulp->g_shared_tfp;
1618 }
1619
1620 /* Function to set the tfp session details from the ulp context. */
1621 int32_t
1622 bnxt_ulp_cntxt_tfp_set(struct bnxt_ulp_context *ulp, struct tf *tfp)
1623 {
1624         if (!ulp) {
1625                 BNXT_TF_DBG(ERR, "Invalid arguments\n");
1626                 return -EINVAL;
1627         }
1628
1629         ulp->g_tfp = tfp;
1630         return 0;
1631 }
1632
1633 /* Function to get the tfp session details from the ulp context. */
1634 struct tf *
1635 bnxt_ulp_cntxt_tfp_get(struct bnxt_ulp_context *ulp,
1636                        enum bnxt_ulp_shared_session shared)
1637 {
1638         if (!ulp) {
1639                 BNXT_TF_DBG(ERR, "Invalid arguments\n");
1640                 return NULL;
1641         }
1642         if (shared)
1643                 return ulp->g_shared_tfp;
1644         else
1645                 return ulp->g_tfp;
1646 }
1647
1648 /*
1649  * Get the device table entry based on the device id.
1650  *
1651  * dev_id [in] The device id of the hardware
1652  *
1653  * Returns the pointer to the device parameters.
1654  */
1655 struct bnxt_ulp_device_params *
1656 bnxt_ulp_device_params_get(uint32_t dev_id)
1657 {
1658         if (dev_id < BNXT_ULP_MAX_NUM_DEVICES)
1659                 return &ulp_device_params[dev_id];
1660         return NULL;
1661 }
1662
1663 /* Function to set the flow database to the ulp context. */
1664 int32_t
1665 bnxt_ulp_cntxt_ptr2_flow_db_set(struct bnxt_ulp_context *ulp_ctx,
1666                                 struct bnxt_ulp_flow_db *flow_db)
1667 {
1668         if (!ulp_ctx || !ulp_ctx->cfg_data)
1669                 return -EINVAL;
1670
1671         ulp_ctx->cfg_data->flow_db = flow_db;
1672         return 0;
1673 }
1674
1675 /* Function to get the flow database from the ulp context. */
1676 struct bnxt_ulp_flow_db *
1677 bnxt_ulp_cntxt_ptr2_flow_db_get(struct bnxt_ulp_context *ulp_ctx)
1678 {
1679         if (!ulp_ctx || !ulp_ctx->cfg_data)
1680                 return NULL;
1681
1682         return ulp_ctx->cfg_data->flow_db;
1683 }
1684
1685 /* Function to get the tunnel cache table info from the ulp context. */
1686 struct bnxt_tun_cache_entry *
1687 bnxt_ulp_cntxt_ptr2_tun_tbl_get(struct bnxt_ulp_context *ulp_ctx)
1688 {
1689         if (!ulp_ctx || !ulp_ctx->cfg_data)
1690                 return NULL;
1691
1692         return ulp_ctx->cfg_data->tun_tbl;
1693 }
1694
1695 /* Function to get the ulp context from eth device. */
1696 struct bnxt_ulp_context *
1697 bnxt_ulp_eth_dev_ptr2_cntxt_get(struct rte_eth_dev      *dev)
1698 {
1699         struct bnxt *bp = (struct bnxt *)dev->data->dev_private;
1700
1701         if (BNXT_ETH_DEV_IS_REPRESENTOR(dev)) {
1702                 struct bnxt_representor *vfr = dev->data->dev_private;
1703
1704                 bp = vfr->parent_dev->data->dev_private;
1705         }
1706
1707         if (!bp) {
1708                 BNXT_TF_DBG(ERR, "Bnxt private data is not initialized\n");
1709                 return NULL;
1710         }
1711         return bp->ulp_ctx;
1712 }
1713
1714 int32_t
1715 bnxt_ulp_cntxt_ptr2_mapper_data_set(struct bnxt_ulp_context *ulp_ctx,
1716                                     void *mapper_data)
1717 {
1718         if (!ulp_ctx || !ulp_ctx->cfg_data) {
1719                 BNXT_TF_DBG(ERR, "Invalid ulp context data\n");
1720                 return -EINVAL;
1721         }
1722
1723         ulp_ctx->cfg_data->mapper_data = mapper_data;
1724         return 0;
1725 }
1726
1727 void *
1728 bnxt_ulp_cntxt_ptr2_mapper_data_get(struct bnxt_ulp_context *ulp_ctx)
1729 {
1730         if (!ulp_ctx || !ulp_ctx->cfg_data) {
1731                 BNXT_TF_DBG(ERR, "Invalid ulp context data\n");
1732                 return NULL;
1733         }
1734
1735         return ulp_ctx->cfg_data->mapper_data;
1736 }
1737
1738 /* Function to set the port database to the ulp context. */
1739 int32_t
1740 bnxt_ulp_cntxt_ptr2_port_db_set(struct bnxt_ulp_context *ulp_ctx,
1741                                 struct bnxt_ulp_port_db *port_db)
1742 {
1743         if (!ulp_ctx || !ulp_ctx->cfg_data)
1744                 return -EINVAL;
1745
1746         ulp_ctx->cfg_data->port_db = port_db;
1747         return 0;
1748 }
1749
1750 /* Function to get the port database from the ulp context. */
1751 struct bnxt_ulp_port_db *
1752 bnxt_ulp_cntxt_ptr2_port_db_get(struct bnxt_ulp_context *ulp_ctx)
1753 {
1754         if (!ulp_ctx || !ulp_ctx->cfg_data)
1755                 return NULL;
1756
1757         return ulp_ctx->cfg_data->port_db;
1758 }
1759
1760 /* Function to set the flow counter info into the context */
1761 int32_t
1762 bnxt_ulp_cntxt_ptr2_fc_info_set(struct bnxt_ulp_context *ulp_ctx,
1763                                 struct bnxt_ulp_fc_info *ulp_fc_info)
1764 {
1765         if (!ulp_ctx || !ulp_ctx->cfg_data) {
1766                 BNXT_TF_DBG(ERR, "Invalid ulp context data\n");
1767                 return -EINVAL;
1768         }
1769
1770         ulp_ctx->cfg_data->fc_info = ulp_fc_info;
1771
1772         return 0;
1773 }
1774
1775 /* Function to retrieve the flow counter info from the context. */
1776 struct bnxt_ulp_fc_info *
1777 bnxt_ulp_cntxt_ptr2_fc_info_get(struct bnxt_ulp_context *ulp_ctx)
1778 {
1779         if (!ulp_ctx || !ulp_ctx->cfg_data)
1780                 return NULL;
1781
1782         return ulp_ctx->cfg_data->fc_info;
1783 }
1784
1785 /* Function to get the ulp flags from the ulp context. */
1786 int32_t
1787 bnxt_ulp_cntxt_ptr2_ulp_flags_get(struct bnxt_ulp_context *ulp_ctx,
1788                                   uint32_t *flags)
1789 {
1790         if (!ulp_ctx || !ulp_ctx->cfg_data)
1791                 return -1;
1792
1793         *flags =  ulp_ctx->cfg_data->ulp_flags;
1794         return 0;
1795 }
1796
1797 /* Function to get the ulp vfr info from the ulp context. */
1798 struct bnxt_ulp_vfr_rule_info*
1799 bnxt_ulp_cntxt_ptr2_ulp_vfr_info_get(struct bnxt_ulp_context *ulp_ctx,
1800                                      uint32_t port_id)
1801 {
1802         if (!ulp_ctx || !ulp_ctx->cfg_data || port_id >= RTE_MAX_ETHPORTS)
1803                 return NULL;
1804
1805         return &ulp_ctx->cfg_data->vfr_rule_info[port_id];
1806 }
1807
1808 /* Function to acquire the flow database lock from the ulp context. */
1809 int32_t
1810 bnxt_ulp_cntxt_acquire_fdb_lock(struct bnxt_ulp_context *ulp_ctx)
1811 {
1812         if (!ulp_ctx || !ulp_ctx->cfg_data)
1813                 return -1;
1814
1815         if (pthread_mutex_lock(&ulp_ctx->cfg_data->flow_db_lock)) {
1816                 BNXT_TF_DBG(ERR, "unable to acquire fdb lock\n");
1817                 return -1;
1818         }
1819         return 0;
1820 }
1821
1822 /* Function to release the flow database lock from the ulp context. */
1823 void
1824 bnxt_ulp_cntxt_release_fdb_lock(struct bnxt_ulp_context *ulp_ctx)
1825 {
1826         if (!ulp_ctx || !ulp_ctx->cfg_data)
1827                 return;
1828
1829         pthread_mutex_unlock(&ulp_ctx->cfg_data->flow_db_lock);
1830 }