net/bnxt: align CFA resources with RM
[dpdk.git] / drivers / net / bnxt / tf_core / tf_tbl.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2019-2020 Broadcom
3  * All rights reserved.
4  */
5
6 /* Truflow Table APIs and supporting code */
7
8 #include <rte_common.h>
9
10 #include "tf_tbl.h"
11 #include "tf_common.h"
12 #include "tf_rm.h"
13 #include "tf_util.h"
14 #include "tf_msg.h"
15 #include "tfp.h"
16
17
18 struct tf;
19
20 /**
21  * Table DBs.
22  */
23 static void *tbl_db[TF_DIR_MAX];
24
25 /**
26  * Table Shadow DBs
27  */
28 /* static void *shadow_tbl_db[TF_DIR_MAX]; */
29
30 /**
31  * Init flag, set on bind and cleared on unbind
32  */
33 static uint8_t init;
34
35 /**
36  * Shadow init flag, set on bind and cleared on unbind
37  */
38 /* static uint8_t shadow_init; */
39
40 int
41 tf_tbl_bind(struct tf *tfp,
42             struct tf_tbl_cfg_parms *parms)
43 {
44         int rc;
45         int i;
46         struct tf_rm_create_db_parms db_cfg = { 0 };
47
48         TF_CHECK_PARMS2(tfp, parms);
49
50         if (init) {
51                 TFP_DRV_LOG(ERR,
52                             "Table DB already initialized\n");
53                 return -EINVAL;
54         }
55
56         db_cfg.num_elements = parms->num_elements;
57         db_cfg.type = TF_DEVICE_MODULE_TYPE_TABLE;
58         db_cfg.num_elements = parms->num_elements;
59         db_cfg.cfg = parms->cfg;
60
61         for (i = 0; i < TF_DIR_MAX; i++) {
62                 db_cfg.dir = i;
63                 db_cfg.alloc_cnt = parms->resources->tbl_cnt[i].cnt;
64                 db_cfg.rm_db = &tbl_db[i];
65                 rc = tf_rm_create_db(tfp, &db_cfg);
66                 if (rc) {
67                         TFP_DRV_LOG(ERR,
68                                     "%s: Table DB creation failed\n",
69                                     tf_dir_2_str(i));
70
71                         return rc;
72                 }
73         }
74
75         init = 1;
76
77         TFP_DRV_LOG(INFO,
78                     "Table Type - initialized\n");
79
80         return 0;
81 }
82
83 int
84 tf_tbl_unbind(struct tf *tfp)
85 {
86         int rc;
87         int i;
88         struct tf_rm_free_db_parms fparms = { 0 };
89
90         TF_CHECK_PARMS1(tfp);
91
92         /* Bail if nothing has been initialized */
93         if (!init) {
94                 TFP_DRV_LOG(INFO,
95                             "No Table DBs created\n");
96                 return 0;
97         }
98
99         for (i = 0; i < TF_DIR_MAX; i++) {
100                 fparms.dir = i;
101                 fparms.rm_db = tbl_db[i];
102                 rc = tf_rm_free_db(tfp, &fparms);
103                 if (rc)
104                         return rc;
105
106                 tbl_db[i] = NULL;
107         }
108
109         init = 0;
110
111         return 0;
112 }
113
114 int
115 tf_tbl_alloc(struct tf *tfp __rte_unused,
116              struct tf_tbl_alloc_parms *parms)
117 {
118         int rc;
119         uint32_t idx;
120         struct tf_rm_allocate_parms aparms = { 0 };
121
122         TF_CHECK_PARMS2(tfp, parms);
123
124         if (!init) {
125                 TFP_DRV_LOG(ERR,
126                             "%s: No Table DBs created\n",
127                             tf_dir_2_str(parms->dir));
128                 return -EINVAL;
129         }
130
131         /* Allocate requested element */
132         aparms.rm_db = tbl_db[parms->dir];
133         aparms.db_index = parms->type;
134         aparms.index = &idx;
135         rc = tf_rm_allocate(&aparms);
136         if (rc) {
137                 TFP_DRV_LOG(ERR,
138                             "%s: Failed allocate, type:%d\n",
139                             tf_dir_2_str(parms->dir),
140                             parms->type);
141                 return rc;
142         }
143
144         *parms->idx = idx;
145
146         return 0;
147 }
148
149 int
150 tf_tbl_free(struct tf *tfp __rte_unused,
151             struct tf_tbl_free_parms *parms)
152 {
153         int rc;
154         struct tf_rm_is_allocated_parms aparms = { 0 };
155         struct tf_rm_free_parms fparms = { 0 };
156         int allocated = 0;
157
158         TF_CHECK_PARMS2(tfp, parms);
159
160         if (!init) {
161                 TFP_DRV_LOG(ERR,
162                             "%s: No Table DBs created\n",
163                             tf_dir_2_str(parms->dir));
164                 return -EINVAL;
165         }
166
167         /* Check if element is in use */
168         aparms.rm_db = tbl_db[parms->dir];
169         aparms.db_index = parms->type;
170         aparms.index = parms->idx;
171         aparms.allocated = &allocated;
172         rc = tf_rm_is_allocated(&aparms);
173         if (rc)
174                 return rc;
175
176         if (allocated != TF_RM_ALLOCATED_ENTRY_IN_USE) {
177                 TFP_DRV_LOG(ERR,
178                             "%s: Entry already free, type:%d, index:%d\n",
179                             tf_dir_2_str(parms->dir),
180                             parms->type,
181                             parms->idx);
182                 return -EINVAL;
183         }
184
185         /* Free requested element */
186         fparms.rm_db = tbl_db[parms->dir];
187         fparms.db_index = parms->type;
188         fparms.index = parms->idx;
189         rc = tf_rm_free(&fparms);
190         if (rc) {
191                 TFP_DRV_LOG(ERR,
192                             "%s: Free failed, type:%d, index:%d\n",
193                             tf_dir_2_str(parms->dir),
194                             parms->type,
195                             parms->idx);
196                 return rc;
197         }
198
199         return 0;
200 }
201
202 int
203 tf_tbl_alloc_search(struct tf *tfp __rte_unused,
204                     struct tf_tbl_alloc_search_parms *parms __rte_unused)
205 {
206         return 0;
207 }
208
209 int
210 tf_tbl_set(struct tf *tfp,
211            struct tf_tbl_set_parms *parms)
212 {
213         int rc;
214         int allocated = 0;
215         uint16_t hcapi_type;
216         struct tf_rm_is_allocated_parms aparms = { 0 };
217         struct tf_rm_get_hcapi_parms hparms = { 0 };
218
219         TF_CHECK_PARMS3(tfp, parms, parms->data);
220
221         if (!init) {
222                 TFP_DRV_LOG(ERR,
223                             "%s: No Table DBs created\n",
224                             tf_dir_2_str(parms->dir));
225                 return -EINVAL;
226         }
227
228         /* Verify that the entry has been previously allocated */
229         aparms.rm_db = tbl_db[parms->dir];
230         aparms.db_index = parms->type;
231         aparms.index = parms->idx;
232         aparms.allocated = &allocated;
233         rc = tf_rm_is_allocated(&aparms);
234         if (rc)
235                 return rc;
236
237         if (allocated != TF_RM_ALLOCATED_ENTRY_IN_USE) {
238                 TFP_DRV_LOG(ERR,
239                    "%s, Invalid or not allocated index, type:%d, idx:%d\n",
240                    tf_dir_2_str(parms->dir),
241                    parms->type,
242                    parms->idx);
243                 return -EINVAL;
244         }
245
246         /* Set the entry */
247         hparms.rm_db = tbl_db[parms->dir];
248         hparms.db_index = parms->type;
249         hparms.hcapi_type = &hcapi_type;
250         rc = tf_rm_get_hcapi_type(&hparms);
251         if (rc) {
252                 TFP_DRV_LOG(ERR,
253                             "%s, Failed type lookup, type:%d, rc:%s\n",
254                             tf_dir_2_str(parms->dir),
255                             parms->type,
256                             strerror(-rc));
257                 return rc;
258         }
259
260         rc = tf_msg_set_tbl_entry(tfp,
261                                   parms->dir,
262                                   hcapi_type,
263                                   parms->data_sz_in_bytes,
264                                   parms->data,
265                                   parms->idx);
266         if (rc) {
267                 TFP_DRV_LOG(ERR,
268                             "%s, Set failed, type:%d, rc:%s\n",
269                             tf_dir_2_str(parms->dir),
270                             parms->type,
271                             strerror(-rc));
272         }
273
274         return 0;
275 }
276
277 int
278 tf_tbl_get(struct tf *tfp,
279            struct tf_tbl_get_parms *parms)
280 {
281         int rc;
282         uint16_t hcapi_type;
283         int allocated = 0;
284         struct tf_rm_is_allocated_parms aparms = { 0 };
285         struct tf_rm_get_hcapi_parms hparms = { 0 };
286
287         TF_CHECK_PARMS3(tfp, parms, parms->data);
288
289         if (!init) {
290                 TFP_DRV_LOG(ERR,
291                             "%s: No Table DBs created\n",
292                             tf_dir_2_str(parms->dir));
293                 return -EINVAL;
294         }
295
296         /* Verify that the entry has been previously allocated */
297         aparms.rm_db = tbl_db[parms->dir];
298         aparms.db_index = parms->type;
299         aparms.index = parms->idx;
300         aparms.allocated = &allocated;
301         rc = tf_rm_is_allocated(&aparms);
302         if (rc)
303                 return rc;
304
305         if (allocated != TF_RM_ALLOCATED_ENTRY_IN_USE) {
306                 TFP_DRV_LOG(ERR,
307                    "%s, Invalid or not allocated index, type:%d, idx:%d\n",
308                    tf_dir_2_str(parms->dir),
309                    parms->type,
310                    parms->idx);
311                 return -EINVAL;
312         }
313
314         /* Set the entry */
315         hparms.rm_db = tbl_db[parms->dir];
316         hparms.db_index = parms->type;
317         hparms.hcapi_type = &hcapi_type;
318         rc = tf_rm_get_hcapi_type(&hparms);
319         if (rc) {
320                 TFP_DRV_LOG(ERR,
321                             "%s, Failed type lookup, type:%d, rc:%s\n",
322                             tf_dir_2_str(parms->dir),
323                             parms->type,
324                             strerror(-rc));
325                 return rc;
326         }
327
328         /* Get the entry */
329         rc = tf_msg_get_tbl_entry(tfp,
330                                   parms->dir,
331                                   hcapi_type,
332                                   parms->data_sz_in_bytes,
333                                   parms->data,
334                                   parms->idx);
335         if (rc) {
336                 TFP_DRV_LOG(ERR,
337                             "%s, Get failed, type:%d, rc:%s\n",
338                             tf_dir_2_str(parms->dir),
339                             parms->type,
340                             strerror(-rc));
341         }
342
343         return 0;
344 }
345
346 int
347 tf_tbl_bulk_get(struct tf *tfp,
348                 struct tf_tbl_get_bulk_parms *parms)
349 {
350         int rc;
351         int i;
352         uint16_t hcapi_type;
353         uint32_t idx;
354         int allocated = 0;
355         struct tf_rm_is_allocated_parms aparms = { 0 };
356         struct tf_rm_get_hcapi_parms hparms = { 0 };
357
358         TF_CHECK_PARMS2(tfp, parms);
359
360         if (!init) {
361                 TFP_DRV_LOG(ERR,
362                             "%s: No Table DBs created\n",
363                             tf_dir_2_str(parms->dir));
364
365                 return -EINVAL;
366         }
367         /* Verify that the entries has been previously allocated */
368         aparms.rm_db = tbl_db[parms->dir];
369         aparms.db_index = parms->type;
370         aparms.allocated = &allocated;
371         idx = parms->starting_idx;
372         for (i = 0; i < parms->num_entries; i++) {
373                 aparms.index = idx;
374                 rc = tf_rm_is_allocated(&aparms);
375                 if (rc)
376                         return rc;
377
378                 if (allocated != TF_RM_ALLOCATED_ENTRY_IN_USE) {
379                         TFP_DRV_LOG(ERR,
380                                     "%s, Invalid or not allocated index, type:%d, idx:%d\n",
381                                     tf_dir_2_str(parms->dir),
382                                     parms->type,
383                                     idx);
384                         return -EINVAL;
385                 }
386                 idx++;
387         }
388
389         hparms.rm_db = tbl_db[parms->dir];
390         hparms.db_index = parms->type;
391         hparms.hcapi_type = &hcapi_type;
392         rc = tf_rm_get_hcapi_type(&hparms);
393         if (rc) {
394                 TFP_DRV_LOG(ERR,
395                             "%s, Failed type lookup, type:%d, rc:%s\n",
396                             tf_dir_2_str(parms->dir),
397                             parms->type,
398                             strerror(-rc));
399                 return rc;
400         }
401
402         /* Get the entries */
403         rc = tf_msg_bulk_get_tbl_entry(tfp,
404                                        parms->dir,
405                                        hcapi_type,
406                                        parms->starting_idx,
407                                        parms->num_entries,
408                                        parms->entry_sz_in_bytes,
409                                        parms->physical_mem_addr);
410         if (rc) {
411                 TFP_DRV_LOG(ERR,
412                             "%s, Bulk get failed, type:%d, rc:%s\n",
413                             tf_dir_2_str(parms->dir),
414                             parms->type,
415                             strerror(-rc));
416         }
417
418         return rc;
419 }