3a3277329e98f8f68a0f324ee3fe86dbbdd5db60
[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         printf("Table Type - initialized\n");
78
79         return 0;
80 }
81
82 int
83 tf_tbl_unbind(struct tf *tfp)
84 {
85         int rc;
86         int i;
87         struct tf_rm_free_db_parms fparms = { 0 };
88
89         TF_CHECK_PARMS1(tfp);
90
91         /* Bail if nothing has been initialized */
92         if (!init) {
93                 TFP_DRV_LOG(INFO,
94                             "No Table DBs created\n");
95                 return 0;
96         }
97
98         for (i = 0; i < TF_DIR_MAX; i++) {
99                 fparms.dir = i;
100                 fparms.rm_db = tbl_db[i];
101                 rc = tf_rm_free_db(tfp, &fparms);
102                 if (rc)
103                         return rc;
104
105                 tbl_db[i] = NULL;
106         }
107
108         init = 0;
109
110         return 0;
111 }
112
113 int
114 tf_tbl_alloc(struct tf *tfp __rte_unused,
115              struct tf_tbl_alloc_parms *parms)
116 {
117         int rc;
118         uint32_t idx;
119         struct tf_rm_allocate_parms aparms = { 0 };
120
121         TF_CHECK_PARMS2(tfp, parms);
122
123         if (!init) {
124                 TFP_DRV_LOG(ERR,
125                             "%s: No Table DBs created\n",
126                             tf_dir_2_str(parms->dir));
127                 return -EINVAL;
128         }
129
130         /* Allocate requested element */
131         aparms.rm_db = tbl_db[parms->dir];
132         aparms.db_index = parms->type;
133         aparms.index = &idx;
134         rc = tf_rm_allocate(&aparms);
135         if (rc) {
136                 TFP_DRV_LOG(ERR,
137                             "%s: Failed allocate, type:%d\n",
138                             tf_dir_2_str(parms->dir),
139                             parms->type);
140                 return rc;
141         }
142
143         *parms->idx = idx;
144
145         return 0;
146 }
147
148 int
149 tf_tbl_free(struct tf *tfp __rte_unused,
150             struct tf_tbl_free_parms *parms)
151 {
152         int rc;
153         struct tf_rm_is_allocated_parms aparms = { 0 };
154         struct tf_rm_free_parms fparms = { 0 };
155         int allocated = 0;
156
157         TF_CHECK_PARMS2(tfp, parms);
158
159         if (!init) {
160                 TFP_DRV_LOG(ERR,
161                             "%s: No Table DBs created\n",
162                             tf_dir_2_str(parms->dir));
163                 return -EINVAL;
164         }
165
166         /* Check if element is in use */
167         aparms.rm_db = tbl_db[parms->dir];
168         aparms.db_index = parms->type;
169         aparms.index = parms->idx;
170         aparms.allocated = &allocated;
171         rc = tf_rm_is_allocated(&aparms);
172         if (rc)
173                 return rc;
174
175         if (allocated != TF_RM_ALLOCATED_ENTRY_IN_USE) {
176                 TFP_DRV_LOG(ERR,
177                             "%s: Entry already free, type:%d, index:%d\n",
178                             tf_dir_2_str(parms->dir),
179                             parms->type,
180                             parms->idx);
181                 return -EINVAL;
182         }
183
184         /* Free requested element */
185         fparms.rm_db = tbl_db[parms->dir];
186         fparms.db_index = parms->type;
187         fparms.index = parms->idx;
188         rc = tf_rm_free(&fparms);
189         if (rc) {
190                 TFP_DRV_LOG(ERR,
191                             "%s: Free failed, type:%d, index:%d\n",
192                             tf_dir_2_str(parms->dir),
193                             parms->type,
194                             parms->idx);
195                 return rc;
196         }
197
198         return 0;
199 }
200
201 int
202 tf_tbl_alloc_search(struct tf *tfp __rte_unused,
203                     struct tf_tbl_alloc_search_parms *parms __rte_unused)
204 {
205         return 0;
206 }
207
208 int
209 tf_tbl_set(struct tf *tfp,
210            struct tf_tbl_set_parms *parms)
211 {
212         int rc;
213         int allocated = 0;
214         uint16_t hcapi_type;
215         struct tf_rm_is_allocated_parms aparms = { 0 };
216         struct tf_rm_get_hcapi_parms hparms = { 0 };
217
218         TF_CHECK_PARMS3(tfp, parms, parms->data);
219
220         if (!init) {
221                 TFP_DRV_LOG(ERR,
222                             "%s: No Table DBs created\n",
223                             tf_dir_2_str(parms->dir));
224                 return -EINVAL;
225         }
226
227         /* Verify that the entry has been previously allocated */
228         aparms.rm_db = tbl_db[parms->dir];
229         aparms.db_index = parms->type;
230         aparms.index = parms->idx;
231         aparms.allocated = &allocated;
232         rc = tf_rm_is_allocated(&aparms);
233         if (rc)
234                 return rc;
235
236         if (allocated != TF_RM_ALLOCATED_ENTRY_IN_USE) {
237                 TFP_DRV_LOG(ERR,
238                    "%s, Invalid or not allocated index, type:%d, idx:%d\n",
239                    tf_dir_2_str(parms->dir),
240                    parms->type,
241                    parms->idx);
242                 return -EINVAL;
243         }
244
245         /* Set the entry */
246         hparms.rm_db = tbl_db[parms->dir];
247         hparms.db_index = parms->type;
248         hparms.hcapi_type = &hcapi_type;
249         rc = tf_rm_get_hcapi_type(&hparms);
250         if (rc) {
251                 TFP_DRV_LOG(ERR,
252                             "%s, Failed type lookup, type:%d, rc:%s\n",
253                             tf_dir_2_str(parms->dir),
254                             parms->type,
255                             strerror(-rc));
256                 return rc;
257         }
258
259         rc = tf_msg_set_tbl_entry(tfp,
260                                   parms->dir,
261                                   hcapi_type,
262                                   parms->data_sz_in_bytes,
263                                   parms->data,
264                                   parms->idx);
265         if (rc) {
266                 TFP_DRV_LOG(ERR,
267                             "%s, Set failed, type:%d, rc:%s\n",
268                             tf_dir_2_str(parms->dir),
269                             parms->type,
270                             strerror(-rc));
271         }
272
273         return 0;
274 }
275
276 int
277 tf_tbl_get(struct tf *tfp,
278            struct tf_tbl_get_parms *parms)
279 {
280         int rc;
281         uint16_t hcapi_type;
282         int allocated = 0;
283         struct tf_rm_is_allocated_parms aparms = { 0 };
284         struct tf_rm_get_hcapi_parms hparms = { 0 };
285
286         TF_CHECK_PARMS3(tfp, parms, parms->data);
287
288         if (!init) {
289                 TFP_DRV_LOG(ERR,
290                             "%s: No Table DBs created\n",
291                             tf_dir_2_str(parms->dir));
292                 return -EINVAL;
293         }
294
295         /* Verify that the entry has been previously allocated */
296         aparms.rm_db = tbl_db[parms->dir];
297         aparms.db_index = parms->type;
298         aparms.index = parms->idx;
299         aparms.allocated = &allocated;
300         rc = tf_rm_is_allocated(&aparms);
301         if (rc)
302                 return rc;
303
304         if (allocated != TF_RM_ALLOCATED_ENTRY_IN_USE) {
305                 TFP_DRV_LOG(ERR,
306                    "%s, Invalid or not allocated index, type:%d, idx:%d\n",
307                    tf_dir_2_str(parms->dir),
308                    parms->type,
309                    parms->idx);
310                 return -EINVAL;
311         }
312
313         /* Set the entry */
314         hparms.rm_db = tbl_db[parms->dir];
315         hparms.db_index = parms->type;
316         hparms.hcapi_type = &hcapi_type;
317         rc = tf_rm_get_hcapi_type(&hparms);
318         if (rc) {
319                 TFP_DRV_LOG(ERR,
320                             "%s, Failed type lookup, type:%d, rc:%s\n",
321                             tf_dir_2_str(parms->dir),
322                             parms->type,
323                             strerror(-rc));
324                 return rc;
325         }
326
327         /* Get the entry */
328         rc = tf_msg_get_tbl_entry(tfp,
329                                   parms->dir,
330                                   hcapi_type,
331                                   parms->data_sz_in_bytes,
332                                   parms->data,
333                                   parms->idx);
334         if (rc) {
335                 TFP_DRV_LOG(ERR,
336                             "%s, Get failed, type:%d, rc:%s\n",
337                             tf_dir_2_str(parms->dir),
338                             parms->type,
339                             strerror(-rc));
340         }
341
342         return 0;
343 }
344
345 int
346 tf_tbl_bulk_get(struct tf *tfp,
347                 struct tf_tbl_get_bulk_parms *parms)
348 {
349         int rc;
350         int i;
351         uint16_t hcapi_type;
352         uint32_t idx;
353         int allocated = 0;
354         struct tf_rm_is_allocated_parms aparms = { 0 };
355         struct tf_rm_get_hcapi_parms hparms = { 0 };
356
357         TF_CHECK_PARMS2(tfp, parms);
358
359         if (!init) {
360                 TFP_DRV_LOG(ERR,
361                             "%s: No Table DBs created\n",
362                             tf_dir_2_str(parms->dir));
363
364                 return -EINVAL;
365         }
366         /* Verify that the entries has been previously allocated */
367         aparms.rm_db = tbl_db[parms->dir];
368         aparms.db_index = parms->type;
369         aparms.allocated = &allocated;
370         idx = parms->starting_idx;
371         for (i = 0; i < parms->num_entries; i++) {
372                 aparms.index = idx;
373                 rc = tf_rm_is_allocated(&aparms);
374                 if (rc)
375                         return rc;
376
377                 if (allocated != TF_RM_ALLOCATED_ENTRY_IN_USE) {
378                         TFP_DRV_LOG(ERR,
379                                     "%s, Invalid or not allocated index, type:%d, idx:%d\n",
380                                     tf_dir_2_str(parms->dir),
381                                     parms->type,
382                                     idx);
383                         return -EINVAL;
384                 }
385                 idx++;
386         }
387
388         hparms.rm_db = tbl_db[parms->dir];
389         hparms.db_index = parms->type;
390         hparms.hcapi_type = &hcapi_type;
391         rc = tf_rm_get_hcapi_type(&hparms);
392         if (rc) {
393                 TFP_DRV_LOG(ERR,
394                             "%s, Failed type lookup, type:%d, rc:%s\n",
395                             tf_dir_2_str(parms->dir),
396                             parms->type,
397                             strerror(-rc));
398                 return rc;
399         }
400
401         /* Get the entries */
402         rc = tf_msg_bulk_get_tbl_entry(tfp,
403                                        parms->dir,
404                                        hcapi_type,
405                                        parms->starting_idx,
406                                        parms->num_entries,
407                                        parms->entry_sz_in_bytes,
408                                        parms->physical_mem_addr);
409         if (rc) {
410                 TFP_DRV_LOG(ERR,
411                             "%s, Bulk get failed, type:%d, rc:%s\n",
412                             tf_dir_2_str(parms->dir),
413                             parms->type,
414                             strerror(-rc));
415         }
416
417         return rc;
418 }