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;
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;
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;
88 session->shadow_copy = parms->open_cfg->shadow_copy;
90 tfp_memcpy(session->ctrl_chan_name,
91 parms->open_cfg->ctrl_chan_name,
95 parms->open_cfg->device_type,
97 &parms->open_cfg->resources,
99 /* Logging handled by dev_bind */
103 session->ref_count++;
108 tfp_free(tfp->session->core_data);
109 tfp_free(tfp->session);
115 tf_session_attach_session(struct tf *tfp __rte_unused,
116 struct tf_session_attach_session_parms *parms __rte_unused)
118 int rc = -EOPNOTSUPP;
120 TF_CHECK_PARMS2(tfp, parms);
123 "Attach not yet supported, rc:%s\n",
129 tf_session_close_session(struct tf *tfp,
130 struct tf_session_close_session_parms *parms)
133 struct tf_session *tfs = NULL;
134 struct tf_dev_info *tfd = NULL;
136 TF_CHECK_PARMS2(tfp, parms);
138 rc = tf_session_get_session(tfp, &tfs);
141 "Session lookup failed, rc:%s\n",
146 if (tfs->session_id.id == TF_SESSION_ID_INVALID) {
149 "Invalid session id, unable to close, rc:%s\n",
154 /* Record the session we're closing so the caller knows the
157 *parms->session_id = tfs->session_id;
159 rc = tf_session_get_device(tfs, &tfd);
162 "Device lookup failed, rc:%s\n",
167 /* In case we're attached only the session client gets closed */
168 rc = tf_msg_session_close(tfp);
172 "FW Session close failed, rc:%s\n",
178 /* Final cleanup as we're last user of the session */
179 if (tfs->ref_count == 0) {
180 /* Unbind the device */
181 rc = dev_unbind(tfp, tfd);
185 "Device unbind failed, rc:%s\n",
189 tfp_free(tfp->session->core_data);
190 tfp_free(tfp->session);
198 tf_session_get_session(struct tf *tfp,
199 struct tf_session **tfs)
203 if (tfp->session == NULL || tfp->session->core_data == NULL) {
206 "Session not created, rc:%s\n",
211 *tfs = (struct tf_session *)(tfp->session->core_data);
217 tf_session_get_device(struct tf_session *tfs,
218 struct tf_dev_info **tfd)
226 tf_session_get_fw_session_id(struct tf *tfp,
227 uint8_t *fw_session_id)
230 struct tf_session *tfs = NULL;
232 if (tfp->session == NULL) {
235 "Session not created, rc:%s\n",
240 rc = tf_session_get_session(tfp, &tfs);
244 *fw_session_id = tfs->session_id.internal.fw_session_id;