net/bnxt: update RM with residual checker
[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.type = TF_DEVICE_MODULE_TYPE_IDENTIFIER;
43         db_cfg.num_elements = parms->num_elements;
44         db_cfg.cfg = parms->cfg;
45
46         for (i = 0; i < TF_DIR_MAX; i++) {
47                 db_cfg.dir = i;
48                 db_cfg.alloc_cnt = parms->resources->ident_cnt[i].cnt;
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
56                         return rc;
57                 }
58         }
59
60         init = 1;
61
62         printf("Identifier - initialized\n");
63
64         return 0;
65 }
66
67 int
68 tf_ident_unbind(struct tf *tfp __rte_unused)
69 {
70         int rc;
71         int i;
72         struct tf_rm_free_db_parms fparms = { 0 };
73
74         TF_CHECK_PARMS1(tfp);
75
76         /* Bail if nothing has been initialized done silent as to
77          * allow for creation cleanup.
78          */
79         if (!init) {
80                 TFP_DRV_LOG(ERR,
81                             "No Identifier DBs created\n");
82                 return -EINVAL;
83         }
84
85         for (i = 0; i < TF_DIR_MAX; i++) {
86                 fparms.dir = i;
87                 fparms.rm_db = ident_db[i];
88                 rc = tf_rm_free_db(tfp, &fparms);
89                 if (rc) {
90                         TFP_DRV_LOG(ERR,
91                                     "rm free failed on unbind\n");
92                 }
93
94                 ident_db[i] = NULL;
95         }
96
97         init = 0;
98
99         return 0;
100 }
101
102 int
103 tf_ident_alloc(struct tf *tfp __rte_unused,
104                struct tf_ident_alloc_parms *parms)
105 {
106         int rc;
107         uint32_t id;
108         struct tf_rm_allocate_parms aparms = { 0 };
109
110         TF_CHECK_PARMS2(tfp, parms);
111
112         if (!init) {
113                 TFP_DRV_LOG(ERR,
114                             "%s: No Identifier DBs created\n",
115                             tf_dir_2_str(parms->dir));
116                 return -EINVAL;
117         }
118
119         /* Allocate requested element */
120         aparms.rm_db = ident_db[parms->dir];
121         aparms.db_index = parms->type;
122         aparms.index = &id;
123         rc = tf_rm_allocate(&aparms);
124         if (rc) {
125                 TFP_DRV_LOG(ERR,
126                             "%s: Failed allocate, type:%d\n",
127                             tf_dir_2_str(parms->dir),
128                             parms->type);
129                 return rc;
130         }
131
132         *parms->id = id;
133
134         return 0;
135 }
136
137 int
138 tf_ident_free(struct tf *tfp __rte_unused,
139               struct tf_ident_free_parms *parms)
140 {
141         int rc;
142         struct tf_rm_is_allocated_parms aparms = { 0 };
143         struct tf_rm_free_parms fparms = { 0 };
144         int allocated = 0;
145
146         TF_CHECK_PARMS2(tfp, parms);
147
148         if (!init) {
149                 TFP_DRV_LOG(ERR,
150                             "%s: No Identifier DBs created\n",
151                             tf_dir_2_str(parms->dir));
152                 return -EINVAL;
153         }
154
155         /* Check if element is in use */
156         aparms.rm_db = ident_db[parms->dir];
157         aparms.db_index = parms->type;
158         aparms.index = parms->id;
159         aparms.allocated = &allocated;
160         rc = tf_rm_is_allocated(&aparms);
161         if (rc)
162                 return rc;
163
164         if (!allocated) {
165                 TFP_DRV_LOG(ERR,
166                             "%s: Entry already free, type:%d, index:%d\n",
167                             tf_dir_2_str(parms->dir),
168                             parms->type,
169                             parms->id);
170                 return rc;
171         }
172
173         /* Free requested element */
174         fparms.rm_db = ident_db[parms->dir];
175         fparms.db_index = parms->type;
176         fparms.index = parms->id;
177         rc = tf_rm_free(&fparms);
178         if (rc) {
179                 TFP_DRV_LOG(ERR,
180                             "%s: Free failed, type:%d, index:%d\n",
181                             tf_dir_2_str(parms->dir),
182                             parms->type,
183                             parms->id);
184                 return rc;
185         }
186
187         return 0;
188 }