1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2019-2021 Broadcom
8 #include <rte_common.h>
10 #include "tf_session.h"
11 #include "tf_common.h"
16 struct tf_session_client_create_parms {
18 * [in] Pointer to the control channel name string
23 * [out] Firmware Session Client ID
25 union tf_session_client_id *session_client_id;
28 struct tf_session_client_destroy_parms {
30 * FW Session Client Identifier
32 union tf_session_client_id session_client_id;
36 * Creates a Session and the associated client.
39 * Pointer to TF handle
42 * Pointer to session client create parameters
45 * - (0) if successful.
46 * - (-EINVAL) on failure.
47 * - (-ENOMEM) if max session clients has been reached.
50 tf_session_create(struct tf *tfp,
51 struct tf_session_open_session_parms *parms)
54 struct tf_session *session = NULL;
55 struct tf_session_client *client;
56 struct tfp_calloc_parms cparms;
57 uint8_t fw_session_id;
58 uint8_t fw_session_client_id;
59 union tf_session_id *session_id;
60 struct tf_dev_info dev;
61 bool shared_session_creator;
65 TF_CHECK_PARMS2(tfp, parms);
67 tf_dev_bind_ops(parms->open_cfg->device_type,
70 /* Open FW session and get a new session_id */
71 rc = tf_msg_session_open(parms->open_cfg->bp,
72 parms->open_cfg->ctrl_chan_name,
74 &fw_session_client_id,
76 &shared_session_creator);
81 "Session is already open, rc:%s\n",
85 "Open message send failed, rc:%s\n",
88 parms->open_cfg->session_id.id = TF_FW_SESSION_ID_INVALID;
92 /* Allocate session */
94 cparms.size = sizeof(struct tf_session_info);
96 rc = tfp_calloc(&cparms);
100 "Failed to allocate session info, rc:%s\n",
104 tfp->session = (struct tf_session_info *)cparms.mem_va;
106 /* Allocate core data for the session */
108 cparms.size = sizeof(struct tf_session);
109 cparms.alignment = 0;
110 rc = tfp_calloc(&cparms);
114 "Failed to allocate session data, rc:%s\n",
118 tfp->session->core_data = cparms.mem_va;
119 session_id = &parms->open_cfg->session_id;
121 /* Update Session Info, which is what is visible to the caller */
122 tfp->session->ver.major = 0;
123 tfp->session->ver.minor = 0;
124 tfp->session->ver.update = 0;
126 tfp->session->session_id.internal.domain = session_id->internal.domain;
127 tfp->session->session_id.internal.bus = session_id->internal.bus;
128 tfp->session->session_id.internal.device = session_id->internal.device;
129 tfp->session->session_id.internal.fw_session_id = fw_session_id;
131 /* Initialize Session and Device, which is private */
132 session = (struct tf_session *)tfp->session->core_data;
133 session->ver.major = 0;
134 session->ver.minor = 0;
135 session->ver.update = 0;
137 session->session_id.internal.domain = session_id->internal.domain;
138 session->session_id.internal.bus = session_id->internal.bus;
139 session->session_id.internal.device = session_id->internal.device;
140 session->session_id.internal.fw_session_id = fw_session_id;
141 /* Return the allocated session id */
142 session_id->id = session->session_id.id;
144 session->shadow_copy = parms->open_cfg->shadow_copy;
146 /* Init session client list */
147 ll_init(&session->client_ll);
149 /* Create the local session client, initialize and attach to
153 cparms.size = sizeof(struct tf_session_client);
154 cparms.alignment = 0;
155 rc = tfp_calloc(&cparms);
159 "Failed to allocate session client, rc:%s\n",
163 client = cparms.mem_va;
165 /* Register FID with the client */
166 rc = tfp_get_fid(tfp, &client->fw_fid);
170 client->session_client_id.internal.fw_session_id = fw_session_id;
171 client->session_client_id.internal.fw_session_client_id =
172 fw_session_client_id;
174 tfp_memcpy(client->ctrl_chan_name,
175 parms->open_cfg->ctrl_chan_name,
176 TF_SESSION_NAME_MAX);
178 ll_insert(&session->client_ll, &client->ll_entry);
179 session->ref_count++;
181 /* Init session em_ext_db */
182 session->em_ext_db_handle = NULL;
184 /* Populate the request */
185 name_len = strnlen(parms->open_cfg->ctrl_chan_name,
186 TF_SESSION_NAME_MAX);
187 name = &parms->open_cfg->ctrl_chan_name[name_len - strlen("tf_shared")];
188 if (!strncmp(name, "tf_shared", strlen("tf_shared")))
189 session->shared_session = true;
191 if (session->shared_session && shared_session_creator) {
192 session->shared_session_creator = true;
193 parms->open_cfg->shared_session_creator = true;
196 rc = tf_dev_bind(tfp,
197 parms->open_cfg->device_type,
198 session->shadow_copy,
199 &parms->open_cfg->resources,
202 /* Logging handled by dev_bind */
206 if (session->dev.ops->tf_dev_get_mailbox == NULL) {
209 "No tf_dev_get_mailbox() defined for device\n");
213 session->dev_init = true;
218 tfp_free(tfp->session->core_data);
219 tfp_free(tfp->session);
225 * Creates a Session Client on an existing Session.
228 * Pointer to TF handle
231 * Pointer to session client create parameters
234 * - (0) if successful.
235 * - (-EINVAL) on failure.
236 * - (-ENOMEM) if max session clients has been reached.
239 tf_session_client_create(struct tf *tfp,
240 struct tf_session_client_create_parms *parms)
243 struct tf_session *session = NULL;
244 struct tf_session_client *client;
245 struct tfp_calloc_parms cparms;
246 union tf_session_client_id session_client_id;
248 TF_CHECK_PARMS2(tfp, parms);
250 /* Using internal version as session client may not exist yet */
251 rc = tf_session_get_session_internal(tfp, &session);
254 "Failed to lookup session, rc:%s\n",
259 client = tf_session_find_session_client_by_name(session,
260 parms->ctrl_chan_name);
263 "Client %s, already registered with this session\n",
264 parms->ctrl_chan_name);
268 rc = tf_msg_session_client_register
271 parms->ctrl_chan_name,
272 &session_client_id.internal.fw_session_client_id);
275 "Failed to create client on session, rc:%s\n",
280 /* Create the local session client, initialize and attach to
284 cparms.size = sizeof(struct tf_session_client);
285 cparms.alignment = 0;
286 rc = tfp_calloc(&cparms);
289 "Failed to allocate session client, rc:%s\n",
293 client = cparms.mem_va;
295 /* Register FID with the client */
296 rc = tfp_get_fid(tfp, &client->fw_fid);
300 /* Build the Session Client ID by adding the fw_session_id */
301 rc = tf_session_get_fw_session_id
303 &session_client_id.internal.fw_session_id);
306 "Session Firmware id lookup failed, rc:%s\n",
311 tfp_memcpy(client->ctrl_chan_name,
312 parms->ctrl_chan_name,
313 TF_SESSION_NAME_MAX);
315 client->session_client_id.id = session_client_id.id;
317 ll_insert(&session->client_ll, &client->ll_entry);
319 session->ref_count++;
321 /* Build the return value */
322 parms->session_client_id->id = session_client_id.id;
325 /* TBD - Add code to unregister newly create client from fw */
332 * Destroys a Session Client on an existing Session.
335 * Pointer to TF handle
338 * Pointer to the session client destroy parameters
341 * - (0) if successful.
342 * - (-EINVAL) on failure.
343 * - (-ENOTFOUND) error, client not owned by the session.
344 * - (-ENOTSUPP) error, unable to destroy client as its the last
345 * client. Please use the tf_session_close().
348 tf_session_client_destroy(struct tf *tfp,
349 struct tf_session_client_destroy_parms *parms)
352 struct tf_session *tfs;
353 struct tf_session_client *client;
355 TF_CHECK_PARMS2(tfp, parms);
357 rc = tf_session_get_session(tfp, &tfs);
360 "Failed to lookup session, rc:%s\n",
365 /* Check session owns this client and that we're not the last client */
366 client = tf_session_get_session_client(tfs,
367 parms->session_client_id);
368 if (client == NULL) {
370 "Client %d, not found within this session\n",
371 parms->session_client_id.id);
375 /* If last client the request is rejected and cleanup should
376 * be done by session close.
378 if (tfs->ref_count == 1)
381 rc = tf_msg_session_client_unregister
384 parms->session_client_id.internal.fw_session_client_id);
386 /* Log error, but continue. If FW fails we do not really have
387 * a way to fix this but the client would no longer be valid
388 * thus we remove from the session.
392 "Client destroy on FW Failed, rc:%s\n",
396 ll_delete(&tfs->client_ll, &client->ll_entry);
398 /* Decrement the session ref_count */
407 tf_session_open_session(struct tf *tfp,
408 struct tf_session_open_session_parms *parms)
411 struct tf_session_client_create_parms scparms;
413 TF_CHECK_PARMS3(tfp, parms, parms->open_cfg->bp);
415 tfp->bp = parms->open_cfg->bp;
416 /* Decide if we're creating a new session or session client */
417 if (tfp->session == NULL) {
418 rc = tf_session_create(tfp, parms);
421 "Failed to create session, ctrl_chan_name:%s, rc:%s\n",
422 parms->open_cfg->ctrl_chan_name,
428 "Session created, session_client_id:%d,"
429 "session_id:0x%08x, fw_session_id:%d\n",
430 parms->open_cfg->session_client_id.id,
431 parms->open_cfg->session_id.id,
432 parms->open_cfg->session_id.internal.fw_session_id);
434 scparms.ctrl_chan_name = parms->open_cfg->ctrl_chan_name;
435 scparms.session_client_id = &parms->open_cfg->session_client_id;
437 /* Create the new client and get it associated with
440 rc = tf_session_client_create(tfp, &scparms);
443 "Failed to create client on session 0x%x, rc:%s\n",
444 parms->open_cfg->session_id.id,
450 "Session Client:%d registered on session:0x%8x\n",
451 scparms.session_client_id->internal.fw_session_client_id,
452 tfp->session->session_id.id);
459 tf_session_attach_session(struct tf *tfp __rte_unused,
460 struct tf_session_attach_session_parms *parms __rte_unused)
462 int rc = -EOPNOTSUPP;
464 TF_CHECK_PARMS2(tfp, parms);
467 "Attach not yet supported, rc:%s\n",
473 tf_session_close_session(struct tf *tfp,
474 struct tf_session_close_session_parms *parms)
477 struct tf_session *tfs = NULL;
478 struct tf_session_client *client;
479 struct tf_dev_info *tfd = NULL;
480 struct tf_session_client_destroy_parms scdparms;
483 TF_CHECK_PARMS2(tfp, parms);
485 rc = tf_session_get_session(tfp, &tfs);
488 "Session lookup failed, rc:%s\n",
493 if (tfs->session_id.id == TF_SESSION_ID_INVALID) {
496 "Invalid session id, unable to close, rc:%s\n",
501 /* Get the client, we need it independently of the closure
502 * type (client or session closure).
504 * We find the client by way of the fid. Thus one cannot close
505 * a client on behalf of someone else.
507 rc = tfp_get_fid(tfp, &fid);
511 client = tf_session_find_session_client_by_fid(tfs,
516 "Client not part of the session, unable to close, rc:%s\n",
521 /* In case multiple clients we chose to close those first */
522 if (tfs->ref_count > 1) {
523 /* Linaro gcc can't static init this structure */
526 sizeof(struct tf_session_client_destroy_parms));
528 scdparms.session_client_id = client->session_client_id;
529 /* Destroy requested client so its no longer
530 * registered with this session.
532 rc = tf_session_client_destroy(tfp, &scdparms);
535 "Failed to unregister Client %d, rc:%s\n",
536 client->session_client_id.id,
542 "Closed session client, session_client_id:%d\n",
543 client->session_client_id.id);
546 "session_id:0x%08x, ref_count:%d\n",
553 /* Record the session we're closing so the caller knows the
556 *parms->session_id = tfs->session_id;
558 rc = tf_session_get_device(tfs, &tfd);
561 "Device lookup failed, rc:%s\n",
566 /* Unbind the device */
567 rc = tf_dev_unbind(tfp, tfd);
571 "Device unbind failed, rc:%s\n",
575 rc = tf_msg_session_close(tfp, tfs);
579 "FW Session close failed, rc:%s\n",
583 /* Final cleanup as we're last user of the session thus we
584 * also delete the last client.
586 ll_delete(&tfs->client_ll, &client->ll_entry);
592 "Closed session, session_id:0x%08x, ref_count:%d\n",
596 tfs->dev_init = false;
598 tfp_free(tfp->session->core_data);
599 tfp_free(tfp->session);
606 tf_session_is_fid_supported(struct tf_session *tfs,
609 struct ll_entry *c_entry;
610 struct tf_session_client *client;
612 for (c_entry = tfs->client_ll.head;
614 c_entry = c_entry->next) {
615 client = (struct tf_session_client *)c_entry;
616 if (client->fw_fid == fid)
624 tf_session_get_session_internal(struct tf *tfp,
625 struct tf_session **tfs)
629 /* Skip using the check macro as we want to control the error msg */
630 if (tfp->session == NULL || tfp->session->core_data == NULL) {
633 "Session not created, rc:%s\n",
638 *tfs = (struct tf_session *)(tfp->session->core_data);
644 tf_session_get_session(struct tf *tfp,
645 struct tf_session **tfs)
649 bool supported = false;
651 rc = tf_session_get_session_internal(tfp,
653 /* Logging done by tf_session_get_session_internal */
657 /* As session sharing among functions aka 'individual clients'
658 * is supported we have to assure that the client is indeed
659 * registered before we get deep in the TruFlow api stack.
661 rc = tfp_get_fid(tfp, &fw_fid);
664 "Internal FID lookup\n, rc:%s\n",
669 supported = tf_session_is_fid_supported(*tfs, fw_fid);
673 "Ctrl channel not registered with session\n, rc:%s\n",
681 struct tf_session_client *
682 tf_session_get_session_client(struct tf_session *tfs,
683 union tf_session_client_id session_client_id)
685 struct ll_entry *c_entry;
686 struct tf_session_client *client;
688 /* Skip using the check macro as we just want to return */
692 for (c_entry = tfs->client_ll.head;
694 c_entry = c_entry->next) {
695 client = (struct tf_session_client *)c_entry;
696 if (client->session_client_id.id == session_client_id.id)
703 struct tf_session_client *
704 tf_session_find_session_client_by_name(struct tf_session *tfs,
705 const char *ctrl_chan_name)
707 struct ll_entry *c_entry;
708 struct tf_session_client *client;
710 /* Skip using the check macro as we just want to return */
711 if (tfs == NULL || ctrl_chan_name == NULL)
714 for (c_entry = tfs->client_ll.head;
716 c_entry = c_entry->next) {
717 client = (struct tf_session_client *)c_entry;
718 if (strncmp(client->ctrl_chan_name,
720 TF_SESSION_NAME_MAX) == 0)
727 struct tf_session_client *
728 tf_session_find_session_client_by_fid(struct tf_session *tfs,
731 struct ll_entry *c_entry;
732 struct tf_session_client *client;
734 /* Skip using the check macro as we just want to return */
738 for (c_entry = tfs->client_ll.head;
740 c_entry = c_entry->next) {
741 client = (struct tf_session_client *)c_entry;
742 if (client->fw_fid == fid)
750 tf_session_get_device(struct tf_session *tfs,
751 struct tf_dev_info **tfd)
759 tf_session_get_fw_session_id(struct tf *tfp,
760 uint8_t *fw_session_id)
763 struct tf_session *tfs = NULL;
765 /* Skip using the check macro as we want to control the error msg */
766 if (tfp->session == NULL) {
769 "Session not created, rc:%s\n",
774 if (fw_session_id == NULL) {
777 "Invalid Argument(s), rc:%s\n",
782 rc = tf_session_get_session_internal(tfp, &tfs);
786 *fw_session_id = tfs->session_id.internal.fw_session_id;
792 tf_session_get_session_id(struct tf *tfp,
793 union tf_session_id *session_id)
796 struct tf_session *tfs = NULL;
798 if (tfp->session == NULL) {
801 "Session not created, rc:%s\n",
806 if (session_id == NULL) {
809 "Invalid Argument(s), rc:%s\n",
814 /* Using internal version as session client may not exist yet */
815 rc = tf_session_get_session_internal(tfp, &tfs);
819 *session_id = tfs->session_id;
825 tf_session_get_em_ext_db(struct tf *tfp,
826 void **em_ext_db_handle)
828 struct tf_session *tfs = NULL;
831 *em_ext_db_handle = NULL;
836 rc = tf_session_get_session_internal(tfp, &tfs);
840 *em_ext_db_handle = tfs->em_ext_db_handle;
845 tf_session_set_em_ext_db(struct tf *tfp,
846 void *em_ext_db_handle)
848 struct tf_session *tfs = NULL;
854 rc = tf_session_get_session_internal(tfp, &tfs);
858 tfs->em_ext_db_handle = em_ext_db_handle;
863 tf_session_get_db(struct tf *tfp,
864 enum tf_module_type type,
867 struct tf_session *tfs = NULL;
875 rc = tf_session_get_session_internal(tfp, &tfs);
880 case TF_MODULE_TYPE_IDENTIFIER:
881 if (tfs->id_db_handle)
882 *db_handle = tfs->id_db_handle;
886 case TF_MODULE_TYPE_TABLE:
887 if (tfs->tbl_db_handle)
888 *db_handle = tfs->tbl_db_handle;
893 case TF_MODULE_TYPE_TCAM:
894 if (tfs->tcam_db_handle)
895 *db_handle = tfs->tcam_db_handle;
899 case TF_MODULE_TYPE_EM:
900 if (tfs->em_db_handle)
901 *db_handle = tfs->em_db_handle;
914 tf_session_set_db(struct tf *tfp,
915 enum tf_module_type type,
918 struct tf_session *tfs = NULL;
924 rc = tf_session_get_session_internal(tfp, &tfs);
929 case TF_MODULE_TYPE_IDENTIFIER:
930 tfs->id_db_handle = db_handle;
932 case TF_MODULE_TYPE_TABLE:
933 tfs->tbl_db_handle = db_handle;
935 case TF_MODULE_TYPE_TCAM:
936 tfs->tcam_db_handle = db_handle;
938 case TF_MODULE_TYPE_EM:
939 tfs->em_db_handle = db_handle;