net/bnxt: update table get to use new design
[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
48         dev_handle->type = TF_DEVICE_TYPE_WH;
49         /* Initial function initialization */
50         dev_handle->ops = &tf_dev_ops_p4_init;
51
52         /* Initialize the modules */
53
54         ident_cfg.num_elements = TF_IDENT_TYPE_MAX;
55         ident_cfg.cfg = tf_ident_p4;
56         ident_cfg.shadow_copy = shadow_copy;
57         ident_cfg.resources = resources;
58         rc = tf_ident_bind(tfp, &ident_cfg);
59         if (rc) {
60                 TFP_DRV_LOG(ERR,
61                             "Identifier initialization failure\n");
62                 goto fail;
63         }
64
65         tbl_cfg.num_elements = TF_TBL_TYPE_MAX;
66         tbl_cfg.cfg = tf_tbl_p4;
67         tbl_cfg.shadow_copy = shadow_copy;
68         tbl_cfg.resources = resources;
69         rc = tf_tbl_bind(tfp, &tbl_cfg);
70         if (rc) {
71                 TFP_DRV_LOG(ERR,
72                             "Table initialization failure\n");
73                 goto fail;
74         }
75
76         tcam_cfg.num_elements = TF_TCAM_TBL_TYPE_MAX;
77         tcam_cfg.cfg = tf_tcam_p4;
78         tcam_cfg.shadow_copy = shadow_copy;
79         tcam_cfg.resources = resources;
80         rc = tf_tcam_bind(tfp, &tcam_cfg);
81         if (rc) {
82                 TFP_DRV_LOG(ERR,
83                             "TCAM initialization failure\n");
84                 goto fail;
85         }
86
87         /*
88          * EEM
89          */
90         em_cfg.num_elements = TF_EM_TBL_TYPE_MAX;
91         em_cfg.cfg = tf_em_ext_p4;
92         em_cfg.resources = resources;
93         em_cfg.mem_type = TF_EEM_MEM_TYPE_HOST;
94
95         rc = tf_em_ext_common_bind(tfp, &em_cfg);
96         if (rc) {
97                 TFP_DRV_LOG(ERR,
98                             "EEM initialization failure\n");
99                 goto fail;
100         }
101
102         /*
103          * EM
104          */
105         em_cfg.num_elements = TF_EM_TBL_TYPE_MAX;
106         em_cfg.cfg = tf_em_int_p4;
107         em_cfg.resources = resources;
108         em_cfg.mem_type = 0; /* Not used by EM */
109
110         rc = tf_em_int_bind(tfp, &em_cfg);
111         if (rc) {
112                 TFP_DRV_LOG(ERR,
113                             "EM initialization failure\n");
114                 goto fail;
115         }
116
117         /* Final function initialization */
118         dev_handle->ops = &tf_dev_ops_p4;
119
120         return 0;
121
122  fail:
123         /* Cleanup of already created modules */
124         frc = tf_dev_unbind_p4(tfp);
125         if (frc)
126                 return frc;
127
128         return rc;
129 }
130
131 /**
132  * Device specific unbind function, WH+
133  *
134  * [in] tfp
135  *   Pointer to TF handle
136  *
137  * Returns
138  *   - (0) if successful.
139  *   - (-EINVAL) on failure.
140  */
141 static int
142 tf_dev_unbind_p4(struct tf *tfp)
143 {
144         int rc = 0;
145         bool fail = false;
146
147         /* Unbind all the support modules. As this is only done on
148          * close we only report errors as everything has to be cleaned
149          * up regardless.
150          *
151          * In case of residuals TCAMs are cleaned up first as to
152          * invalidate the pipeline in a clean manner.
153          */
154         rc = tf_tcam_unbind(tfp);
155         if (rc) {
156                 TFP_DRV_LOG(ERR,
157                             "Device unbind failed, TCAM\n");
158                 fail = true;
159         }
160
161         rc = tf_ident_unbind(tfp);
162         if (rc) {
163                 TFP_DRV_LOG(ERR,
164                             "Device unbind failed, Identifier\n");
165                 fail = true;
166         }
167
168         rc = tf_tbl_unbind(tfp);
169         if (rc) {
170                 TFP_DRV_LOG(ERR,
171                             "Device unbind failed, Table Type\n");
172                 fail = true;
173         }
174
175         rc = tf_em_ext_common_unbind(tfp);
176         if (rc) {
177                 TFP_DRV_LOG(ERR,
178                             "Device unbind failed, EEM\n");
179                 fail = true;
180         }
181
182         rc = tf_em_int_unbind(tfp);
183         if (rc) {
184                 TFP_DRV_LOG(ERR,
185                             "Device unbind failed, EM\n");
186                 fail = true;
187         }
188
189         if (fail)
190                 return -1;
191
192         return rc;
193 }
194
195 int
196 tf_dev_bind(struct tf *tfp __rte_unused,
197             enum tf_device_type type,
198             bool shadow_copy,
199             struct tf_session_resources *resources,
200             struct tf_dev_info *dev_handle)
201 {
202         switch (type) {
203         case TF_DEVICE_TYPE_WH:
204                 return tf_dev_bind_p4(tfp,
205                                       shadow_copy,
206                                       resources,
207                                       dev_handle);
208         default:
209                 TFP_DRV_LOG(ERR,
210                             "No such device\n");
211                 return -ENODEV;
212         }
213 }
214
215 int
216 tf_dev_unbind(struct tf *tfp,
217               struct tf_dev_info *dev_handle)
218 {
219         switch (dev_handle->type) {
220         case TF_DEVICE_TYPE_WH:
221                 return tf_dev_unbind_p4(tfp);
222         default:
223                 TFP_DRV_LOG(ERR,
224                             "No such device\n");
225                 return -ENODEV;
226         }
227 }