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