net/bnxt: add action SRAM translation
[dpdk.git] / drivers / net / bnxt / tf_core / tf_tbl.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2019-2021 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 #include "tf_session.h"
17 #include "tf_device.h"
18
19 struct tf;
20
21 /**
22  * Table DBs.
23  */
24 static void *tbl_db[TF_DIR_MAX];
25
26 /**
27  * Table Shadow DBs
28  */
29 static void *shadow_tbl_db[TF_DIR_MAX];
30
31 /**
32  * Init flag, set on bind and cleared on unbind
33  */
34 static uint8_t init;
35
36 /**
37  * Shadow init flag, set on bind and cleared on unbind
38  */
39 static uint8_t shadow_init;
40
41 int
42 tf_tbl_bind(struct tf *tfp,
43             struct tf_tbl_cfg_parms *parms)
44 {
45         int rc, d;
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.module = TF_MODULE_TYPE_TABLE;
58         db_cfg.num_elements = parms->num_elements;
59         db_cfg.cfg = parms->cfg;
60
61         for (d = 0; d < TF_DIR_MAX; d++) {
62                 db_cfg.dir = d;
63                 db_cfg.alloc_cnt = parms->resources->tbl_cnt[d].cnt;
64                 db_cfg.rm_db = &tbl_db[d];
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(d));
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         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         shadow_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.subtype = 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.subtype = 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         /* Free requested element */
185         fparms.rm_db = tbl_db[parms->dir];
186         fparms.subtype = 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,
203                     struct tf_tbl_alloc_search_parms *parms)
204 {
205         int rc = 0;
206         TF_CHECK_PARMS2(tfp, parms);
207
208         if (!shadow_init || !shadow_tbl_db[parms->dir]) {
209                 TFP_DRV_LOG(ERR, "%s: Shadow TBL not initialized.\n",
210                             tf_dir_2_str(parms->dir));
211                 return -EINVAL;
212         }
213
214         return rc;
215 }
216
217 int
218 tf_tbl_set(struct tf *tfp,
219            struct tf_tbl_set_parms *parms)
220 {
221         int rc;
222         int allocated = 0;
223         uint16_t hcapi_type;
224         struct tf_rm_is_allocated_parms aparms = { 0 };
225         struct tf_rm_get_hcapi_parms hparms = { 0 };
226
227         TF_CHECK_PARMS3(tfp, parms, parms->data);
228
229         if (!init) {
230                 TFP_DRV_LOG(ERR,
231                             "%s: No Table DBs created\n",
232                             tf_dir_2_str(parms->dir));
233                 return -EINVAL;
234         }
235
236         /* Verify that the entry has been previously allocated */
237         aparms.rm_db = tbl_db[parms->dir];
238         aparms.subtype = parms->type;
239         aparms.index = parms->idx;
240         aparms.allocated = &allocated;
241         rc = tf_rm_is_allocated(&aparms);
242         if (rc)
243                 return rc;
244
245         if (allocated != TF_RM_ALLOCATED_ENTRY_IN_USE) {
246                 TFP_DRV_LOG(ERR,
247                    "%s, Invalid or not allocated index, type:%d, idx:%d\n",
248                    tf_dir_2_str(parms->dir),
249                    parms->type,
250                    parms->idx);
251                 return -EINVAL;
252         }
253
254         /* Set the entry */
255         hparms.rm_db = tbl_db[parms->dir];
256         hparms.subtype = parms->type;
257         hparms.hcapi_type = &hcapi_type;
258         rc = tf_rm_get_hcapi_type(&hparms);
259         if (rc) {
260                 TFP_DRV_LOG(ERR,
261                             "%s, Failed type lookup, type:%d, rc:%s\n",
262                             tf_dir_2_str(parms->dir),
263                             parms->type,
264                             strerror(-rc));
265                 return rc;
266         }
267
268         rc = tf_msg_set_tbl_entry(tfp,
269                                   parms->dir,
270                                   hcapi_type,
271                                   parms->data_sz_in_bytes,
272                                   parms->data,
273                                   parms->idx);
274         if (rc) {
275                 TFP_DRV_LOG(ERR,
276                             "%s, Set failed, type:%d, rc:%s\n",
277                             tf_dir_2_str(parms->dir),
278                             parms->type,
279                             strerror(-rc));
280                 return rc;
281         }
282
283         return 0;
284 }
285
286 int
287 tf_tbl_get(struct tf *tfp,
288            struct tf_tbl_get_parms *parms)
289 {
290         int rc;
291         uint16_t hcapi_type;
292         int allocated = 0;
293         struct tf_rm_is_allocated_parms aparms = { 0 };
294         struct tf_rm_get_hcapi_parms hparms = { 0 };
295
296         TF_CHECK_PARMS3(tfp, parms, parms->data);
297
298         if (!init) {
299                 TFP_DRV_LOG(ERR,
300                             "%s: No Table DBs created\n",
301                             tf_dir_2_str(parms->dir));
302                 return -EINVAL;
303         }
304
305         /* Verify that the entry has been previously allocated */
306         aparms.rm_db = tbl_db[parms->dir];
307         aparms.subtype = parms->type;
308         aparms.index = parms->idx;
309         aparms.allocated = &allocated;
310         rc = tf_rm_is_allocated(&aparms);
311         if (rc)
312                 return rc;
313
314         if (allocated != TF_RM_ALLOCATED_ENTRY_IN_USE) {
315                 TFP_DRV_LOG(ERR,
316                    "%s, Invalid or not allocated index, type:%d, idx:%d\n",
317                    tf_dir_2_str(parms->dir),
318                    parms->type,
319                    parms->idx);
320                 return -EINVAL;
321         }
322
323         /* Set the entry */
324         hparms.rm_db = tbl_db[parms->dir];
325         hparms.subtype = parms->type;
326         hparms.hcapi_type = &hcapi_type;
327         rc = tf_rm_get_hcapi_type(&hparms);
328         if (rc) {
329                 TFP_DRV_LOG(ERR,
330                             "%s, Failed type lookup, type:%d, rc:%s\n",
331                             tf_dir_2_str(parms->dir),
332                             parms->type,
333                             strerror(-rc));
334                 return rc;
335         }
336
337         /* Get the entry */
338         rc = tf_msg_get_tbl_entry(tfp,
339                                   parms->dir,
340                                   hcapi_type,
341                                   parms->data_sz_in_bytes,
342                                   parms->data,
343                                   parms->idx);
344         if (rc) {
345                 TFP_DRV_LOG(ERR,
346                             "%s, Get failed, type:%d, rc:%s\n",
347                             tf_dir_2_str(parms->dir),
348                             parms->type,
349                             strerror(-rc));
350                 return rc;
351         }
352
353         return 0;
354 }
355
356 int
357 tf_tbl_bulk_get(struct tf *tfp,
358                 struct tf_tbl_get_bulk_parms *parms)
359 {
360         int rc;
361         uint16_t hcapi_type;
362         struct tf_rm_get_hcapi_parms hparms = { 0 };
363         struct tf_rm_check_indexes_in_range_parms cparms = { 0 };
364
365         TF_CHECK_PARMS2(tfp, parms);
366
367         if (!init) {
368                 TFP_DRV_LOG(ERR,
369                             "%s: No Table DBs created\n",
370                             tf_dir_2_str(parms->dir));
371
372                 return -EINVAL;
373         }
374
375         /* Verify that the entries are in the range of reserved resources. */
376         cparms.rm_db = tbl_db[parms->dir];
377         cparms.subtype = parms->type;
378         cparms.starting_index = parms->starting_idx;
379         cparms.num_entries = parms->num_entries;
380
381         rc = tf_rm_check_indexes_in_range(&cparms);
382         if (rc) {
383                 TFP_DRV_LOG(ERR,
384                             "%s, Invalid or %d index starting from %d"
385                             " not in range, type:%d",
386                             tf_dir_2_str(parms->dir),
387                             parms->starting_idx,
388                             parms->num_entries,
389                             parms->type);
390                 return rc;
391         }
392
393         hparms.rm_db = tbl_db[parms->dir];
394         hparms.subtype = parms->type;
395         hparms.hcapi_type = &hcapi_type;
396         rc = tf_rm_get_hcapi_type(&hparms);
397         if (rc) {
398                 TFP_DRV_LOG(ERR,
399                             "%s, Failed type lookup, type:%d, rc:%s\n",
400                             tf_dir_2_str(parms->dir),
401                             parms->type,
402                             strerror(-rc));
403                 return rc;
404         }
405
406         /* Get the entries */
407         rc = tf_msg_bulk_get_tbl_entry(tfp,
408                                        parms->dir,
409                                        hcapi_type,
410                                        parms->starting_idx,
411                                        parms->num_entries,
412                                        parms->entry_sz_in_bytes,
413                                        parms->physical_mem_addr);
414         if (rc) {
415                 TFP_DRV_LOG(ERR,
416                             "%s, Bulk get failed, type:%d, rc:%s\n",
417                             tf_dir_2_str(parms->dir),
418                             parms->type,
419                             strerror(-rc));
420         }
421
422         return rc;
423 }