net/bnxt: update RM with residual checker
[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
10 struct tf;
11
12 /* Forward declarations */
13 static int tf_dev_unbind_p4(struct tf *tfp);
14
15 /**
16  * Device specific bind function, WH+
17  *
18  * [in] tfp
19  *   Pointer to TF handle
20  *
21  * [in] shadow_copy
22  *   Flag controlling shadow copy DB creation
23  *
24  * [in] resources
25  *   Pointer to resource allocation information
26  *
27  * [out] dev_handle
28  *   Device handle
29  *
30  * Returns
31  *   - (0) if successful.
32  *   - (-EINVAL) on parameter or internal failure.
33  */
34 static int
35 tf_dev_bind_p4(struct tf *tfp,
36                bool shadow_copy,
37                struct tf_session_resources *resources,
38                struct tf_dev_info *dev_handle)
39 {
40         int rc;
41         int frc;
42         struct tf_ident_cfg_parms ident_cfg;
43         struct tf_tbl_cfg_parms tbl_cfg;
44         struct tf_tcam_cfg_parms tcam_cfg;
45
46         dev_handle->type = TF_DEVICE_TYPE_WH;
47         /* Initial function initialization */
48         dev_handle->ops = &tf_dev_ops_p4_init;
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         /* Final function initialization */
90         dev_handle->ops = &tf_dev_ops_p4;
91
92         return 0;
93
94  fail:
95         /* Cleanup of already created modules */
96         frc = tf_dev_unbind_p4(tfp);
97         if (frc)
98                 return frc;
99
100         return rc;
101 }
102
103 /**
104  * Device specific unbind function, WH+
105  *
106  * [in] tfp
107  *   Pointer to TF handle
108  *
109  * Returns
110  *   - (0) if successful.
111  *   - (-EINVAL) on failure.
112  */
113 static int
114 tf_dev_unbind_p4(struct tf *tfp)
115 {
116         int rc = 0;
117         bool fail = false;
118
119         /* Unbind all the support modules. As this is only done on
120          * close we only report errors as everything has to be cleaned
121          * up regardless.
122          *
123          * In case of residuals TCAMs are cleaned up first as to
124          * invalidate the pipeline in a clean manner.
125          */
126         rc = tf_tcam_unbind(tfp);
127         if (rc) {
128                 TFP_DRV_LOG(ERR,
129                             "Device unbind failed, TCAM\n");
130                 fail = true;
131         }
132
133         rc = tf_ident_unbind(tfp);
134         if (rc) {
135                 TFP_DRV_LOG(ERR,
136                             "Device unbind failed, Identifier\n");
137                 fail = true;
138         }
139
140         rc = tf_tbl_unbind(tfp);
141         if (rc) {
142                 TFP_DRV_LOG(ERR,
143                             "Device unbind failed, Table Type\n");
144                 fail = true;
145         }
146
147         if (fail)
148                 return -1;
149
150         return rc;
151 }
152
153 int
154 tf_dev_bind(struct tf *tfp __rte_unused,
155             enum tf_device_type type,
156             bool shadow_copy,
157             struct tf_session_resources *resources,
158             struct tf_dev_info *dev_handle)
159 {
160         switch (type) {
161         case TF_DEVICE_TYPE_WH:
162                 return tf_dev_bind_p4(tfp,
163                                       shadow_copy,
164                                       resources,
165                                       dev_handle);
166         default:
167                 TFP_DRV_LOG(ERR,
168                             "No such device\n");
169                 return -ENODEV;
170         }
171 }
172
173 int
174 tf_dev_unbind(struct tf *tfp,
175               struct tf_dev_info *dev_handle)
176 {
177         switch (dev_handle->type) {
178         case TF_DEVICE_TYPE_WH:
179                 return tf_dev_unbind_p4(tfp);
180         default:
181                 TFP_DRV_LOG(ERR,
182                             "No such device\n");
183                 return -ENODEV;
184         }
185 }