net/bnxt: update multi device design
[dpdk.git] / drivers / net / bnxt / tf_core / tf_session.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2019-2020 Broadcom
3  * All rights reserved.
4  */
5
6 #include <string.h>
7
8 #include <rte_common.h>
9
10 #include "tf_session.h"
11 #include "tf_common.h"
12 #include "tf_msg.h"
13 #include "tfp.h"
14
15 int
16 tf_session_open_session(struct tf *tfp,
17                         struct tf_session_open_session_parms *parms)
18 {
19         int rc;
20         struct tf_session *session = NULL;
21         struct tfp_calloc_parms cparms;
22         uint8_t fw_session_id;
23         union tf_session_id *session_id;
24
25         TF_CHECK_PARMS2(tfp, parms);
26
27         /* Open FW session and get a new session_id */
28         rc = tf_msg_session_open(tfp,
29                                  parms->open_cfg->ctrl_chan_name,
30                                  &fw_session_id);
31         if (rc) {
32                 /* Log error */
33                 if (rc == -EEXIST)
34                         TFP_DRV_LOG(ERR,
35                                     "Session is already open, rc:%s\n",
36                                     strerror(-rc));
37                 else
38                         TFP_DRV_LOG(ERR,
39                                     "Open message send failed, rc:%s\n",
40                                     strerror(-rc));
41
42                 parms->open_cfg->session_id.id = TF_FW_SESSION_ID_INVALID;
43                 return rc;
44         }
45
46         /* Allocate session */
47         cparms.nitems = 1;
48         cparms.size = sizeof(struct tf_session_info);
49         cparms.alignment = 0;
50         rc = tfp_calloc(&cparms);
51         if (rc) {
52                 /* Log error */
53                 TFP_DRV_LOG(ERR,
54                             "Failed to allocate session info, rc:%s\n",
55                             strerror(-rc));
56                 goto cleanup;
57         }
58         tfp->session = (struct tf_session_info *)cparms.mem_va;
59
60         /* Allocate core data for the session */
61         cparms.nitems = 1;
62         cparms.size = sizeof(struct tf_session);
63         cparms.alignment = 0;
64         rc = tfp_calloc(&cparms);
65         if (rc) {
66                 /* Log error */
67                 TFP_DRV_LOG(ERR,
68                             "Failed to allocate session data, rc:%s\n",
69                             strerror(-rc));
70                 goto cleanup;
71         }
72         tfp->session->core_data = cparms.mem_va;
73
74         /* Initialize Session and Device */
75         session = (struct tf_session *)tfp->session->core_data;
76         session->ver.major = 0;
77         session->ver.minor = 0;
78         session->ver.update = 0;
79
80         session_id = &parms->open_cfg->session_id;
81         session->session_id.internal.domain = session_id->internal.domain;
82         session->session_id.internal.bus = session_id->internal.bus;
83         session->session_id.internal.device = session_id->internal.device;
84         session->session_id.internal.fw_session_id = fw_session_id;
85         /* Return the allocated fw session id */
86         session_id->internal.fw_session_id = fw_session_id;
87
88         session->shadow_copy = parms->open_cfg->shadow_copy;
89
90         tfp_memcpy(session->ctrl_chan_name,
91                    parms->open_cfg->ctrl_chan_name,
92                    TF_SESSION_NAME_MAX);
93
94         rc = dev_bind(tfp,
95                       parms->open_cfg->device_type,
96                       session->shadow_copy,
97                       &parms->open_cfg->resources,
98                       session->dev);
99         /* Logging handled by dev_bind */
100         if (rc)
101                 return rc;
102
103         /* Query for Session Config
104          */
105         rc = tf_msg_session_qcfg(tfp);
106         if (rc) {
107                 TFP_DRV_LOG(ERR,
108                             "Query config message send failed, rc:%s\n",
109                             strerror(-rc));
110                 goto cleanup_close;
111         }
112
113         session->ref_count++;
114
115         return 0;
116
117  cleanup:
118         tfp_free(tfp->session->core_data);
119         tfp_free(tfp->session);
120         tfp->session = NULL;
121         return rc;
122
123  cleanup_close:
124         tf_close_session(tfp);
125         return -EINVAL;
126 }
127
128 int
129 tf_session_attach_session(struct tf *tfp __rte_unused,
130                           struct tf_session_attach_session_parms *parms __rte_unused)
131 {
132         int rc = -EOPNOTSUPP;
133
134         TF_CHECK_PARMS2(tfp, parms);
135
136         TFP_DRV_LOG(ERR,
137                     "Attach not yet supported, rc:%s\n",
138                     strerror(-rc));
139         return rc;
140 }
141
142 int
143 tf_session_close_session(struct tf *tfp,
144                          struct tf_session_close_session_parms *parms)
145 {
146         int rc;
147         struct tf_session *tfs = NULL;
148         struct tf_dev_info *tfd = NULL;
149
150         TF_CHECK_PARMS2(tfp, parms);
151
152         rc = tf_session_get_session(tfp, &tfs);
153         if (rc) {
154                 TFP_DRV_LOG(ERR,
155                             "Session lookup failed, rc:%s\n",
156                             strerror(-rc));
157                 return rc;
158         }
159
160         if (tfs->session_id.id == TF_SESSION_ID_INVALID) {
161                 rc = -EINVAL;
162                 TFP_DRV_LOG(ERR,
163                             "Invalid session id, unable to close, rc:%s\n",
164                             strerror(-rc));
165                 return rc;
166         }
167
168         /* Record the session we're closing so the caller knows the
169          * details.
170          */
171         *parms->session_id = tfs->session_id;
172
173         rc = tf_session_get_device(tfs, &tfd);
174         if (rc) {
175                 TFP_DRV_LOG(ERR,
176                             "Device lookup failed, rc:%s\n",
177                             strerror(-rc));
178                 return rc;
179         }
180
181         /* In case we're attached only the session client gets closed */
182         rc = tf_msg_session_close(tfp);
183         if (rc) {
184                 /* Log error */
185                 TFP_DRV_LOG(ERR,
186                             "FW Session close failed, rc:%s\n",
187                             strerror(-rc));
188         }
189
190         tfs->ref_count--;
191
192         /* Final cleanup as we're last user of the session */
193         if (tfs->ref_count == 0) {
194                 /* Unbind the device */
195                 rc = dev_unbind(tfp, tfd);
196                 if (rc) {
197                         /* Log error */
198                         TFP_DRV_LOG(ERR,
199                                     "Device unbind failed, rc:%s\n",
200                                     strerror(-rc));
201                 }
202
203                 tfp_free(tfp->session->core_data);
204                 tfp_free(tfp->session);
205                 tfp->session = NULL;
206         }
207
208         return 0;
209 }
210
211 int
212 tf_session_get_session(struct tf *tfp,
213                        struct tf_session **tfs)
214 {
215         int rc;
216
217         if (tfp->session == NULL || tfp->session->core_data == NULL) {
218                 rc = -EINVAL;
219                 TFP_DRV_LOG(ERR,
220                             "Session not created, rc:%s\n",
221                             strerror(-rc));
222                 return rc;
223         }
224
225         *tfs = (struct tf_session *)(tfp->session->core_data);
226
227         return 0;
228 }
229
230 int
231 tf_session_get_device(struct tf_session *tfs,
232                       struct tf_dev_info **tfd)
233 {
234         int rc;
235
236         if (tfs->dev == NULL) {
237                 rc = -EINVAL;
238                 TFP_DRV_LOG(ERR,
239                             "Device not created, rc:%s\n",
240                             strerror(-rc));
241                 return rc;
242         }
243
244         *tfd = tfs->dev;
245
246         return 0;
247 }
248
249 int
250 tf_session_get_fw_session_id(struct tf *tfp,
251                              uint8_t *fw_session_id)
252 {
253         int rc;
254         struct tf_session *tfs = NULL;
255
256         if (tfp->session == NULL) {
257                 rc = -EINVAL;
258                 TFP_DRV_LOG(ERR,
259                             "Session not created, rc:%s\n",
260                             strerror(-rc));
261                 return rc;
262         }
263
264         rc = tf_session_get_session(tfp, &tfs);
265         if (rc)
266                 return rc;
267
268         *fw_session_id = tfs->session_id.internal.fw_session_id;
269
270         return 0;
271 }