4c46cadc6d8001b2613b8c39de265036c75e5bef
[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 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 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         /* Initialize the modules */
47
48         ident_cfg.num_elements = TF_IDENT_TYPE_MAX;
49         ident_cfg.cfg = tf_ident_p4;
50         ident_cfg.shadow_copy = shadow_copy;
51         ident_cfg.resources = resources;
52         rc = tf_ident_bind(tfp, &ident_cfg);
53         if (rc) {
54                 TFP_DRV_LOG(ERR,
55                             "Identifier initialization failure\n");
56                 goto fail;
57         }
58
59         tbl_cfg.num_elements = TF_TBL_TYPE_MAX;
60         tbl_cfg.cfg = tf_tbl_p4;
61         tbl_cfg.shadow_copy = shadow_copy;
62         tbl_cfg.resources = resources;
63         rc = tf_tbl_bind(tfp, &tbl_cfg);
64         if (rc) {
65                 TFP_DRV_LOG(ERR,
66                             "Table initialization failure\n");
67                 goto fail;
68         }
69
70         tcam_cfg.num_elements = TF_TCAM_TBL_TYPE_MAX;
71         tcam_cfg.cfg = tf_tcam_p4;
72         tcam_cfg.shadow_copy = shadow_copy;
73         tcam_cfg.resources = resources;
74         rc = tf_tcam_bind(tfp, &tcam_cfg);
75         if (rc) {
76                 TFP_DRV_LOG(ERR,
77                             "TCAM initialization failure\n");
78                 goto fail;
79         }
80
81         dev_handle->type = TF_DEVICE_TYPE_WH;
82         dev_handle->ops = &tf_dev_ops_p4;
83
84         return 0;
85
86  fail:
87         /* Cleanup of already created modules */
88         frc = dev_unbind_p4(tfp);
89         if (frc)
90                 return frc;
91
92         return rc;
93 }
94
95 /**
96  * Device specific unbind function, WH+
97  *
98  * [in] tfp
99  *   Pointer to TF handle
100  *
101  * Returns
102  *   - (0) if successful.
103  *   - (-EINVAL) on failure.
104  */
105 static int
106 dev_unbind_p4(struct tf *tfp)
107 {
108         int rc = 0;
109         bool fail = false;
110
111         /* Unbind all the support modules. As this is only done on
112          * close we only report errors as everything has to be cleaned
113          * up regardless.
114          */
115         rc = tf_ident_unbind(tfp);
116         if (rc) {
117                 TFP_DRV_LOG(ERR,
118                             "Device unbind failed, Identifier\n");
119                 fail = true;
120         }
121
122         rc = tf_tbl_unbind(tfp);
123         if (rc) {
124                 TFP_DRV_LOG(ERR,
125                             "Device unbind failed, Table Type\n");
126                 fail = true;
127         }
128
129         rc = tf_tcam_unbind(tfp);
130         if (rc) {
131                 TFP_DRV_LOG(ERR,
132                             "Device unbind failed, TCAM\n");
133                 fail = true;
134         }
135
136         if (fail)
137                 return -1;
138
139         return rc;
140 }
141
142 int
143 dev_bind(struct tf *tfp __rte_unused,
144          enum tf_device_type type,
145          bool shadow_copy,
146          struct tf_session_resources *resources,
147          struct tf_dev_info *dev_handle)
148 {
149         switch (type) {
150         case TF_DEVICE_TYPE_WH:
151                 return dev_bind_p4(tfp,
152                                    shadow_copy,
153                                    resources,
154                                    dev_handle);
155         default:
156                 TFP_DRV_LOG(ERR,
157                             "No such device\n");
158                 return -ENODEV;
159         }
160 }
161
162 int
163 dev_unbind(struct tf *tfp,
164            struct tf_dev_info *dev_handle)
165 {
166         switch (dev_handle->type) {
167         case TF_DEVICE_TYPE_WH:
168                 return dev_unbind_p4(tfp);
169         default:
170                 TFP_DRV_LOG(ERR,
171                             "No such device\n");
172                 return -ENODEV;
173         }
174 }