1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2019-2020 Broadcom
8 #include <rte_common.h>
10 #include "tf_session.h"
11 #include "tf_common.h"
16 tf_session_open_session(struct tf *tfp,
17 struct tf_session_open_session_parms *parms)
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;
25 TF_CHECK_PARMS2(tfp, parms);
27 /* Open FW session and get a new session_id */
28 rc = tf_msg_session_open(tfp,
29 parms->open_cfg->ctrl_chan_name,
35 "Session is already open, rc:%s\n",
39 "Open message send failed, rc:%s\n",
42 parms->open_cfg->session_id.id = TF_FW_SESSION_ID_INVALID;
46 /* Allocate session */
48 cparms.size = sizeof(struct tf_session_info);
50 rc = tfp_calloc(&cparms);
54 "Failed to allocate session info, rc:%s\n",
58 tfp->session = (struct tf_session_info *)cparms.mem_va;
60 /* Allocate core data for the session */
62 cparms.size = sizeof(struct tf_session);
64 rc = tfp_calloc(&cparms);
68 "Failed to allocate session data, rc:%s\n",
72 tfp->session->core_data = cparms.mem_va;
73 session_id = &parms->open_cfg->session_id;
75 /* Update Session Info, which is what is visible to the caller */
76 tfp->session->ver.major = 0;
77 tfp->session->ver.minor = 0;
78 tfp->session->ver.update = 0;
80 tfp->session->session_id.internal.domain = session_id->internal.domain;
81 tfp->session->session_id.internal.bus = session_id->internal.bus;
82 tfp->session->session_id.internal.device = session_id->internal.device;
83 tfp->session->session_id.internal.fw_session_id = fw_session_id;
85 /* Initialize Session and Device, which is private */
86 session = (struct tf_session *)tfp->session->core_data;
87 session->ver.major = 0;
88 session->ver.minor = 0;
89 session->ver.update = 0;
91 session->session_id.internal.domain = session_id->internal.domain;
92 session->session_id.internal.bus = session_id->internal.bus;
93 session->session_id.internal.device = session_id->internal.device;
94 session->session_id.internal.fw_session_id = fw_session_id;
95 /* Return the allocated fw session id */
96 session_id->internal.fw_session_id = fw_session_id;
98 session->shadow_copy = parms->open_cfg->shadow_copy;
100 tfp_memcpy(session->ctrl_chan_name,
101 parms->open_cfg->ctrl_chan_name,
102 TF_SESSION_NAME_MAX);
104 rc = tf_dev_bind(tfp,
105 parms->open_cfg->device_type,
106 session->shadow_copy,
107 &parms->open_cfg->resources,
109 /* Logging handled by dev_bind */
113 session->ref_count++;
118 tfp_free(tfp->session->core_data);
119 tfp_free(tfp->session);
125 tf_session_attach_session(struct tf *tfp __rte_unused,
126 struct tf_session_attach_session_parms *parms __rte_unused)
128 int rc = -EOPNOTSUPP;
130 TF_CHECK_PARMS2(tfp, parms);
133 "Attach not yet supported, rc:%s\n",
139 tf_session_close_session(struct tf *tfp,
140 struct tf_session_close_session_parms *parms)
143 struct tf_session *tfs = NULL;
144 struct tf_dev_info *tfd = NULL;
146 TF_CHECK_PARMS2(tfp, parms);
148 rc = tf_session_get_session(tfp, &tfs);
151 "Session lookup failed, rc:%s\n",
156 if (tfs->session_id.id == TF_SESSION_ID_INVALID) {
159 "Invalid session id, unable to close, rc:%s\n",
166 /* Record the session we're closing so the caller knows the
169 *parms->session_id = tfs->session_id;
171 rc = tf_session_get_device(tfs, &tfd);
174 "Device lookup failed, rc:%s\n",
179 if (tfs->ref_count > 0) {
180 /* In case we're attached only the session client gets
183 rc = tf_msg_session_close(tfp);
187 "FW Session close failed, rc:%s\n",
194 /* Final cleanup as we're last user of the session */
196 /* Unbind the device */
197 rc = tf_dev_unbind(tfp, tfd);
201 "Device unbind failed, rc:%s\n",
205 /* In case we're attached only the session client gets closed */
206 rc = tf_msg_session_close(tfp);
210 "FW Session close failed, rc:%s\n",
214 tfp_free(tfp->session->core_data);
215 tfp_free(tfp->session);
222 tf_session_get_session(struct tf *tfp,
223 struct tf_session **tfs)
227 if (tfp->session == NULL || tfp->session->core_data == NULL) {
230 "Session not created, rc:%s\n",
235 *tfs = (struct tf_session *)(tfp->session->core_data);
241 tf_session_get_device(struct tf_session *tfs,
242 struct tf_dev_info **tfd)
250 tf_session_get_fw_session_id(struct tf *tfp,
251 uint8_t *fw_session_id)
254 struct tf_session *tfs = NULL;
256 if (tfp->session == NULL) {
259 "Session not created, rc:%s\n",
264 rc = tf_session_get_session(tfp, &tfs);
268 *fw_session_id = tfs->session_id.internal.fw_session_id;