net/bnxt: support multiple device
[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         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 = 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 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         rc = tf_ident_unbind(tfp);
124         if (rc) {
125                 TFP_DRV_LOG(ERR,
126                             "Device unbind failed, Identifier\n");
127                 fail = true;
128         }
129
130         rc = tf_tbl_unbind(tfp);
131         if (rc) {
132                 TFP_DRV_LOG(ERR,
133                             "Device unbind failed, Table Type\n");
134                 fail = true;
135         }
136
137         rc = tf_tcam_unbind(tfp);
138         if (rc) {
139                 TFP_DRV_LOG(ERR,
140                             "Device unbind failed, TCAM\n");
141                 fail = true;
142         }
143
144         if (fail)
145                 return -1;
146
147         return rc;
148 }
149
150 int
151 dev_bind(struct tf *tfp __rte_unused,
152          enum tf_device_type type,
153          bool shadow_copy,
154          struct tf_session_resources *resources,
155          struct tf_dev_info *dev_handle)
156 {
157         switch (type) {
158         case TF_DEVICE_TYPE_WH:
159                 return dev_bind_p4(tfp,
160                                    shadow_copy,
161                                    resources,
162                                    dev_handle);
163         default:
164                 TFP_DRV_LOG(ERR,
165                             "No such device\n");
166                 return -ENODEV;
167         }
168 }
169
170 int
171 dev_unbind(struct tf *tfp,
172            struct tf_dev_info *dev_handle)
173 {
174         switch (dev_handle->type) {
175         case TF_DEVICE_TYPE_WH:
176                 return dev_unbind_p4(tfp);
177         default:
178                 TFP_DRV_LOG(ERR,
179                             "No such device\n");
180                 return -ENODEV;
181         }
182 }