net/bnxt: update multi device design
[dpdk.git] / drivers / net / bnxt / tf_core / tf_identifier.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2019-2020 Broadcom
3  * All rights reserved.
4  */
5
6 #include <rte_common.h>
7
8 #include "tf_identifier.h"
9 #include "tf_common.h"
10 #include "tf_rm_new.h"
11 #include "tf_util.h"
12 #include "tfp.h"
13
14 struct tf;
15
16 /**
17  * Identifier DBs.
18  */
19 static void *ident_db[TF_DIR_MAX];
20
21 /**
22  * Init flag, set on bind and cleared on unbind
23  */
24 static uint8_t init;
25
26 int
27 tf_ident_bind(struct tf *tfp,
28               struct tf_ident_cfg_parms *parms)
29 {
30         int rc;
31         int i;
32         struct tf_rm_create_db_parms db_cfg = { 0 };
33
34         TF_CHECK_PARMS2(tfp, parms);
35
36         if (init) {
37                 TFP_DRV_LOG(ERR,
38                             "Identifier already initialized\n");
39                 return -EINVAL;
40         }
41
42         db_cfg.num_elements = parms->num_elements;
43
44         for (i = 0; i < TF_DIR_MAX; i++) {
45                 db_cfg.dir = i;
46                 db_cfg.num_elements = parms->num_elements;
47                 db_cfg.cfg = parms->cfg;
48                 db_cfg.alloc_num = parms->resources->identifier_cnt[i];
49                 db_cfg.rm_db = ident_db[i];
50                 rc = tf_rm_create_db(tfp, &db_cfg);
51                 if (rc) {
52                         TFP_DRV_LOG(ERR,
53                                     "%s: Identifier DB creation failed\n",
54                                     tf_dir_2_str(i));
55                         return rc;
56                 }
57         }
58
59         init = 1;
60
61         return 0;
62 }
63
64 int
65 tf_ident_unbind(struct tf *tfp __rte_unused)
66 {
67         int rc;
68         int i;
69         struct tf_rm_free_db_parms fparms = { 0 };
70
71         TF_CHECK_PARMS1(tfp);
72
73         /* Bail if nothing has been initialized done silent as to
74          * allow for creation cleanup.
75          */
76         if (!init)
77                 return -EINVAL;
78
79         for (i = 0; i < TF_DIR_MAX; i++) {
80                 fparms.dir = i;
81                 fparms.rm_db = ident_db[i];
82                 rc = tf_rm_free_db(tfp, &fparms);
83                 if (rc)
84                         return rc;
85
86                 ident_db[i] = NULL;
87         }
88
89         init = 0;
90
91         return 0;
92 }
93
94 int
95 tf_ident_alloc(struct tf *tfp __rte_unused,
96                struct tf_ident_alloc_parms *parms)
97 {
98         int rc;
99         struct tf_rm_allocate_parms aparms = { 0 };
100
101         TF_CHECK_PARMS2(tfp, parms);
102
103         if (!init) {
104                 TFP_DRV_LOG(ERR,
105                             "%s: No Identifier DBs created\n",
106                             tf_dir_2_str(parms->dir));
107                 return -EINVAL;
108         }
109
110         /* Allocate requested element */
111         aparms.rm_db = ident_db[parms->dir];
112         aparms.db_index = parms->ident_type;
113         aparms.index = (uint32_t *)&parms->id;
114         rc = tf_rm_allocate(&aparms);
115         if (rc) {
116                 TFP_DRV_LOG(ERR,
117                             "%s: Failed allocate, type:%d\n",
118                             tf_dir_2_str(parms->dir),
119                             parms->ident_type);
120                 return rc;
121         }
122
123         return 0;
124 }
125
126 int
127 tf_ident_free(struct tf *tfp __rte_unused,
128               struct tf_ident_free_parms *parms)
129 {
130         int rc;
131         struct tf_rm_is_allocated_parms aparms = { 0 };
132         struct tf_rm_free_parms fparms = { 0 };
133         int allocated = 0;
134
135         TF_CHECK_PARMS2(tfp, parms);
136
137         if (!init) {
138                 TFP_DRV_LOG(ERR,
139                             "%s: No Identifier DBs created\n",
140                             tf_dir_2_str(parms->dir));
141                 return -EINVAL;
142         }
143
144         /* Check if element is in use */
145         aparms.rm_db = ident_db[parms->dir];
146         aparms.db_index = parms->ident_type;
147         aparms.index = parms->id;
148         aparms.allocated = &allocated;
149         rc = tf_rm_is_allocated(&aparms);
150         if (rc)
151                 return rc;
152
153         if (!allocated) {
154                 TFP_DRV_LOG(ERR,
155                             "%s: Entry already free, type:%d, index:%d\n",
156                             tf_dir_2_str(parms->dir),
157                             parms->ident_type,
158                             parms->id);
159                 return rc;
160         }
161
162         /* Free requested element */
163         fparms.rm_db = ident_db[parms->dir];
164         fparms.db_index = parms->ident_type;
165         fparms.index = parms->id;
166         rc = tf_rm_free(&fparms);
167         if (rc) {
168                 TFP_DRV_LOG(ERR,
169                             "%s: Free failed, type:%d, index:%d\n",
170                             tf_dir_2_str(parms->dir),
171                             parms->ident_type,
172                             parms->id);
173                 return rc;
174         }
175
176         return 0;
177 }