net/bnxt: add global config set and get functions
[dpdk.git] / drivers / net / bnxt / tf_core / tf_device.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2019-2020 Broadcom
3  * All rights reserved.
4  */
5
6 #include "tf_device.h"
7 #include "tf_device_p4.h"
8 #include "tfp.h"
9 #include "tf_em.h"
10
11 struct tf;
12
13 /* Forward declarations */
14 static int tf_dev_unbind_p4(struct tf *tfp);
15
16 /**
17  * Device specific bind function, WH+
18  *
19  * [in] tfp
20  *   Pointer to TF handle
21  *
22  * [in] shadow_copy
23  *   Flag controlling shadow copy DB creation
24  *
25  * [in] resources
26  *   Pointer to resource allocation information
27  *
28  * [out] dev_handle
29  *   Device handle
30  *
31  * Returns
32  *   - (0) if successful.
33  *   - (-EINVAL) on parameter or internal failure.
34  */
35 static int
36 tf_dev_bind_p4(struct tf *tfp,
37                bool shadow_copy,
38                struct tf_session_resources *resources,
39                struct tf_dev_info *dev_handle)
40 {
41         int rc;
42         int frc;
43         struct tf_ident_cfg_parms ident_cfg;
44         struct tf_tbl_cfg_parms tbl_cfg;
45         struct tf_tcam_cfg_parms tcam_cfg;
46         struct tf_em_cfg_parms em_cfg;
47         struct tf_if_tbl_cfg_parms if_tbl_cfg;
48         struct tf_global_cfg_cfg_parms global_cfg;
49
50         dev_handle->type = TF_DEVICE_TYPE_WH;
51         /* Initial function initialization */
52         dev_handle->ops = &tf_dev_ops_p4_init;
53
54         /* Initialize the modules */
55
56         ident_cfg.num_elements = TF_IDENT_TYPE_MAX;
57         ident_cfg.cfg = tf_ident_p4;
58         ident_cfg.shadow_copy = shadow_copy;
59         ident_cfg.resources = resources;
60         rc = tf_ident_bind(tfp, &ident_cfg);
61         if (rc) {
62                 TFP_DRV_LOG(ERR,
63                             "Identifier initialization failure\n");
64                 goto fail;
65         }
66
67         tbl_cfg.num_elements = TF_TBL_TYPE_MAX;
68         tbl_cfg.cfg = tf_tbl_p4;
69         tbl_cfg.shadow_copy = shadow_copy;
70         tbl_cfg.resources = resources;
71         rc = tf_tbl_bind(tfp, &tbl_cfg);
72         if (rc) {
73                 TFP_DRV_LOG(ERR,
74                             "Table initialization failure\n");
75                 goto fail;
76         }
77
78         tcam_cfg.num_elements = TF_TCAM_TBL_TYPE_MAX;
79         tcam_cfg.cfg = tf_tcam_p4;
80         tcam_cfg.shadow_copy = shadow_copy;
81         tcam_cfg.resources = resources;
82         rc = tf_tcam_bind(tfp, &tcam_cfg);
83         if (rc) {
84                 TFP_DRV_LOG(ERR,
85                             "TCAM initialization failure\n");
86                 goto fail;
87         }
88
89         /*
90          * EEM
91          */
92         em_cfg.num_elements = TF_EM_TBL_TYPE_MAX;
93         em_cfg.cfg = tf_em_ext_p4;
94         em_cfg.resources = resources;
95         em_cfg.mem_type = TF_EEM_MEM_TYPE_HOST;
96
97         rc = tf_em_ext_common_bind(tfp, &em_cfg);
98         if (rc) {
99                 TFP_DRV_LOG(ERR,
100                             "EEM initialization failure\n");
101                 goto fail;
102         }
103
104         /*
105          * EM
106          */
107         em_cfg.num_elements = TF_EM_TBL_TYPE_MAX;
108         em_cfg.cfg = tf_em_int_p4;
109         em_cfg.resources = resources;
110         em_cfg.mem_type = 0; /* Not used by EM */
111
112         rc = tf_em_int_bind(tfp, &em_cfg);
113         if (rc) {
114                 TFP_DRV_LOG(ERR,
115                             "EM initialization failure\n");
116                 goto fail;
117         }
118
119         /*
120          * IF_TBL
121          */
122         if_tbl_cfg.num_elements = TF_IF_TBL_TYPE_MAX;
123         if_tbl_cfg.cfg = tf_if_tbl_p4;
124         if_tbl_cfg.shadow_copy = shadow_copy;
125         rc = tf_if_tbl_bind(tfp, &if_tbl_cfg);
126         if (rc) {
127                 TFP_DRV_LOG(ERR,
128                             "IF Table initialization failure\n");
129                 goto fail;
130         }
131
132         /*
133          * GLOBAL_CFG
134          */
135         global_cfg.num_elements = TF_GLOBAL_CFG_TYPE_MAX;
136         global_cfg.cfg = tf_global_cfg_p4;
137         rc = tf_global_cfg_bind(tfp, &global_cfg);
138         if (rc) {
139                 TFP_DRV_LOG(ERR,
140                             "Global Cfg initialization failure\n");
141                 goto fail;
142         }
143
144         /* Final function initialization */
145         dev_handle->ops = &tf_dev_ops_p4;
146
147         return 0;
148
149  fail:
150         /* Cleanup of already created modules */
151         frc = tf_dev_unbind_p4(tfp);
152         if (frc)
153                 return frc;
154
155         return rc;
156 }
157
158 /**
159  * Device specific unbind function, WH+
160  *
161  * [in] tfp
162  *   Pointer to TF handle
163  *
164  * Returns
165  *   - (0) if successful.
166  *   - (-EINVAL) on failure.
167  */
168 static int
169 tf_dev_unbind_p4(struct tf *tfp)
170 {
171         int rc = 0;
172         bool fail = false;
173
174         /* Unbind all the support modules. As this is only done on
175          * close we only report errors as everything has to be cleaned
176          * up regardless.
177          *
178          * In case of residuals TCAMs are cleaned up first as to
179          * invalidate the pipeline in a clean manner.
180          */
181         rc = tf_tcam_unbind(tfp);
182         if (rc) {
183                 TFP_DRV_LOG(ERR,
184                             "Device unbind failed, TCAM\n");
185                 fail = true;
186         }
187
188         rc = tf_ident_unbind(tfp);
189         if (rc) {
190                 TFP_DRV_LOG(ERR,
191                             "Device unbind failed, Identifier\n");
192                 fail = true;
193         }
194
195         rc = tf_tbl_unbind(tfp);
196         if (rc) {
197                 TFP_DRV_LOG(ERR,
198                             "Device unbind failed, Table Type\n");
199                 fail = true;
200         }
201
202         rc = tf_em_ext_common_unbind(tfp);
203         if (rc) {
204                 TFP_DRV_LOG(ERR,
205                             "Device unbind failed, EEM\n");
206                 fail = true;
207         }
208
209         rc = tf_em_int_unbind(tfp);
210         if (rc) {
211                 TFP_DRV_LOG(ERR,
212                             "Device unbind failed, EM\n");
213                 fail = true;
214         }
215
216         rc = tf_if_tbl_unbind(tfp);
217         if (rc) {
218                 TFP_DRV_LOG(ERR,
219                             "Device unbind failed, IF Table Type\n");
220                 fail = true;
221         }
222
223         rc = tf_global_cfg_unbind(tfp);
224         if (rc) {
225                 TFP_DRV_LOG(ERR,
226                             "Device unbind failed, Global Cfg Type\n");
227                 fail = true;
228         }
229
230         if (fail)
231                 return -1;
232
233         return rc;
234 }
235
236 int
237 tf_dev_bind(struct tf *tfp __rte_unused,
238             enum tf_device_type type,
239             bool shadow_copy,
240             struct tf_session_resources *resources,
241             struct tf_dev_info *dev_handle)
242 {
243         switch (type) {
244         case TF_DEVICE_TYPE_WH:
245                 return tf_dev_bind_p4(tfp,
246                                       shadow_copy,
247                                       resources,
248                                       dev_handle);
249         default:
250                 TFP_DRV_LOG(ERR,
251                             "No such device\n");
252                 return -ENODEV;
253         }
254 }
255
256 int
257 tf_dev_unbind(struct tf *tfp,
258               struct tf_dev_info *dev_handle)
259 {
260         switch (dev_handle->type) {
261         case TF_DEVICE_TYPE_WH:
262                 return tf_dev_unbind_p4(tfp);
263         default:
264                 TFP_DRV_LOG(ERR,
265                             "No such device\n");
266                 return -ENODEV;
267         }
268 }