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 name = &parms->open_cfg->ctrl_chan_name[name_len -
192 strlen("tf_shared-wc_tcam")];
193 if (!strncmp(name, "tf_shared-wc_tcam", strlen("tf_shared-wc_tcam")))
194 session->shared_session = true;
196 if (session->shared_session && shared_session_creator) {
197 session->shared_session_creator = true;
198 parms->open_cfg->shared_session_creator = true;
201 rc = tf_dev_bind(tfp,
202 parms->open_cfg->device_type,
203 session->shadow_copy,
204 &parms->open_cfg->resources,
207 /* Logging handled by dev_bind */
211 if (session->dev.ops->tf_dev_get_mailbox == NULL) {
214 "No tf_dev_get_mailbox() defined for device\n");
218 session->dev_init = true;
223 rc = tf_msg_session_close(tfp,
225 dev.ops->tf_dev_get_mailbox());
229 "FW Session close failed, rc:%s\n",
233 tfp_free(tfp->session->core_data);
234 tfp_free(tfp->session);
240 * Creates a Session Client on an existing Session.
243 * Pointer to TF handle
246 * Pointer to session client create parameters
249 * - (0) if successful.
250 * - (-EINVAL) on failure.
251 * - (-ENOMEM) if max session clients has been reached.
254 tf_session_client_create(struct tf *tfp,
255 struct tf_session_client_create_parms *parms)
258 struct tf_session *session = NULL;
259 struct tf_session_client *client;
260 struct tfp_calloc_parms cparms;
261 union tf_session_client_id session_client_id;
263 TF_CHECK_PARMS2(tfp, parms);
265 /* Using internal version as session client may not exist yet */
266 rc = tf_session_get_session_internal(tfp, &session);
269 "Failed to lookup session, rc:%s\n",
274 client = tf_session_find_session_client_by_name(session,
275 parms->ctrl_chan_name);
278 "Client %s, already registered with this session\n",
279 parms->ctrl_chan_name);
283 rc = tf_msg_session_client_register
286 parms->ctrl_chan_name,
287 &session_client_id.internal.fw_session_client_id);
290 "Failed to create client on session, rc:%s\n",
295 /* Create the local session client, initialize and attach to
299 cparms.size = sizeof(struct tf_session_client);
300 cparms.alignment = 0;
301 rc = tfp_calloc(&cparms);
304 "Failed to allocate session client, rc:%s\n",
308 client = cparms.mem_va;
310 /* Register FID with the client */
311 rc = tfp_get_fid(tfp, &client->fw_fid);
315 /* Build the Session Client ID by adding the fw_session_id */
316 rc = tf_session_get_fw_session_id
318 &session_client_id.internal.fw_session_id);
321 "Session Firmware id lookup failed, rc:%s\n",
326 tfp_memcpy(client->ctrl_chan_name,
327 parms->ctrl_chan_name,
328 TF_SESSION_NAME_MAX);
330 client->session_client_id.id = session_client_id.id;
332 ll_insert(&session->client_ll, &client->ll_entry);
334 session->ref_count++;
336 /* Build the return value */
337 parms->session_client_id->id = session_client_id.id;
340 /* TBD - Add code to unregister newly create client from fw */
347 * Destroys a Session Client on an existing Session.
350 * Pointer to TF handle
353 * Pointer to the session client destroy parameters
356 * - (0) if successful.
357 * - (-EINVAL) on failure.
358 * - (-ENOTFOUND) error, client not owned by the session.
359 * - (-ENOTSUPP) error, unable to destroy client as its the last
360 * client. Please use the tf_session_close().
363 tf_session_client_destroy(struct tf *tfp,
364 struct tf_session_client_destroy_parms *parms)
367 struct tf_session *tfs;
368 struct tf_session_client *client;
370 TF_CHECK_PARMS2(tfp, parms);
372 rc = tf_session_get_session(tfp, &tfs);
375 "Failed to lookup session, rc:%s\n",
380 /* Check session owns this client and that we're not the last client */
381 client = tf_session_get_session_client(tfs,
382 parms->session_client_id);
383 if (client == NULL) {
385 "Client %d, not found within this session\n",
386 parms->session_client_id.id);
390 /* If last client the request is rejected and cleanup should
391 * be done by session close.
393 if (tfs->ref_count == 1)
396 rc = tf_msg_session_client_unregister
399 parms->session_client_id.internal.fw_session_client_id);
401 /* Log error, but continue. If FW fails we do not really have
402 * a way to fix this but the client would no longer be valid
403 * thus we remove from the session.
407 "Client destroy on FW Failed, rc:%s\n",
411 ll_delete(&tfs->client_ll, &client->ll_entry);
413 /* Decrement the session ref_count */
422 tf_session_open_session(struct tf *tfp,
423 struct tf_session_open_session_parms *parms)
426 struct tf_session_client_create_parms scparms;
428 TF_CHECK_PARMS3(tfp, parms, parms->open_cfg->bp);
430 tfp->bp = parms->open_cfg->bp;
431 /* Decide if we're creating a new session or session client */
432 if (tfp->session == NULL) {
433 rc = tf_session_create(tfp, parms);
436 "Failed to create session, ctrl_chan_name:%s, rc:%s\n",
437 parms->open_cfg->ctrl_chan_name,
443 "Session created, session_client_id:%d,"
444 "session_id:0x%08x, fw_session_id:%d\n",
445 parms->open_cfg->session_client_id.id,
446 parms->open_cfg->session_id.id,
447 parms->open_cfg->session_id.internal.fw_session_id);
449 scparms.ctrl_chan_name = parms->open_cfg->ctrl_chan_name;
450 scparms.session_client_id = &parms->open_cfg->session_client_id;
452 /* Create the new client and get it associated with
455 rc = tf_session_client_create(tfp, &scparms);
458 "Failed to create client on session 0x%x, rc:%s\n",
459 parms->open_cfg->session_id.id,
465 "Session Client:%d registered on session:0x%8x\n",
466 scparms.session_client_id->internal.fw_session_client_id,
467 tfp->session->session_id.id);
474 tf_session_attach_session(struct tf *tfp __rte_unused,
475 struct tf_session_attach_session_parms *parms __rte_unused)
477 int rc = -EOPNOTSUPP;
479 TF_CHECK_PARMS2(tfp, parms);
482 "Attach not yet supported, rc:%s\n",
488 tf_session_close_session(struct tf *tfp,
489 struct tf_session_close_session_parms *parms)
492 struct tf_session *tfs = NULL;
493 struct tf_session_client *client;
494 struct tf_dev_info *tfd = NULL;
495 struct tf_session_client_destroy_parms scdparms;
497 uint8_t fw_session_id = 1;
500 TF_CHECK_PARMS2(tfp, parms);
502 rc = tf_session_get_session(tfp, &tfs);
505 "Session lookup failed, rc:%s\n",
510 if (tfs->session_id.id == TF_SESSION_ID_INVALID) {
513 "Invalid session id, unable to close, rc:%s\n",
518 /* Get the client, we need it independently of the closure
519 * type (client or session closure).
521 * We find the client by way of the fid. Thus one cannot close
522 * a client on behalf of someone else.
524 rc = tfp_get_fid(tfp, &fid);
528 client = tf_session_find_session_client_by_fid(tfs,
533 "Client not part of the session, unable to close, rc:%s\n",
538 /* In case multiple clients we chose to close those first */
539 if (tfs->ref_count > 1) {
540 /* Linaro gcc can't static init this structure */
543 sizeof(struct tf_session_client_destroy_parms));
545 scdparms.session_client_id = client->session_client_id;
546 /* Destroy requested client so its no longer
547 * registered with this session.
549 rc = tf_session_client_destroy(tfp, &scdparms);
552 "Failed to unregister Client %d, rc:%s\n",
553 client->session_client_id.id,
559 "Closed session client, session_client_id:%d\n",
560 client->session_client_id.id);
563 "session_id:0x%08x, ref_count:%d\n",
570 /* Record the session we're closing so the caller knows the
573 *parms->session_id = tfs->session_id;
575 rc = tf_session_get_device(tfs, &tfd);
578 "Device lookup failed, rc:%s\n",
583 mailbox = tfd->ops->tf_dev_get_mailbox();
585 rc = tf_session_get_fw_session_id(tfp, &fw_session_id);
588 "Unable to lookup FW id, rc:%s\n",
593 /* Unbind the device */
594 rc = tf_dev_unbind(tfp, tfd);
598 "Device unbind failed, rc:%s\n",
602 rc = tf_msg_session_close(tfp, fw_session_id, mailbox);
606 "FW Session close failed, rc:%s\n",
610 /* Final cleanup as we're last user of the session thus we
611 * also delete the last client.
613 ll_delete(&tfs->client_ll, &client->ll_entry);
619 "Closed session, session_id:0x%08x, ref_count:%d\n",
623 tfs->dev_init = false;
625 tfp_free(tfp->session->core_data);
626 tfp_free(tfp->session);
633 tf_session_is_fid_supported(struct tf_session *tfs,
636 struct ll_entry *c_entry;
637 struct tf_session_client *client;
639 for (c_entry = tfs->client_ll.head;
641 c_entry = c_entry->next) {
642 client = (struct tf_session_client *)c_entry;
643 if (client->fw_fid == fid)
651 tf_session_get_session_internal(struct tf *tfp,
652 struct tf_session **tfs)
656 /* Skip using the check macro as we want to control the error msg */
657 if (tfp->session == NULL || tfp->session->core_data == NULL) {
660 "Session not created, rc:%s\n",
665 *tfs = (struct tf_session *)(tfp->session->core_data);
671 tf_session_get_session(struct tf *tfp,
672 struct tf_session **tfs)
676 bool supported = false;
678 rc = tf_session_get_session_internal(tfp,
680 /* Logging done by tf_session_get_session_internal */
684 /* As session sharing among functions aka 'individual clients'
685 * is supported we have to assure that the client is indeed
686 * registered before we get deep in the TruFlow api stack.
688 rc = tfp_get_fid(tfp, &fw_fid);
691 "Internal FID lookup\n, rc:%s\n",
696 supported = tf_session_is_fid_supported(*tfs, fw_fid);
700 "Ctrl channel not registered with session\n, rc:%s\n",
708 struct tf_session_client *
709 tf_session_get_session_client(struct tf_session *tfs,
710 union tf_session_client_id session_client_id)
712 struct ll_entry *c_entry;
713 struct tf_session_client *client;
715 /* Skip using the check macro as we just want to return */
719 for (c_entry = tfs->client_ll.head;
721 c_entry = c_entry->next) {
722 client = (struct tf_session_client *)c_entry;
723 if (client->session_client_id.id == session_client_id.id)
730 struct tf_session_client *
731 tf_session_find_session_client_by_name(struct tf_session *tfs,
732 const char *ctrl_chan_name)
734 struct ll_entry *c_entry;
735 struct tf_session_client *client;
737 /* Skip using the check macro as we just want to return */
738 if (tfs == NULL || ctrl_chan_name == NULL)
741 for (c_entry = tfs->client_ll.head;
743 c_entry = c_entry->next) {
744 client = (struct tf_session_client *)c_entry;
745 if (strncmp(client->ctrl_chan_name,
747 TF_SESSION_NAME_MAX) == 0)
754 struct tf_session_client *
755 tf_session_find_session_client_by_fid(struct tf_session *tfs,
758 struct ll_entry *c_entry;
759 struct tf_session_client *client;
761 /* Skip using the check macro as we just want to return */
765 for (c_entry = tfs->client_ll.head;
767 c_entry = c_entry->next) {
768 client = (struct tf_session_client *)c_entry;
769 if (client->fw_fid == fid)
777 tf_session_get_device(struct tf_session *tfs,
778 struct tf_dev_info **tfd)
786 tf_session_get_fw_session_id(struct tf *tfp,
787 uint8_t *fw_session_id)
790 struct tf_session *tfs = NULL;
792 /* Skip using the check macro as we want to control the error msg */
793 if (tfp->session == NULL) {
796 "Session not created, rc:%s\n",
801 if (fw_session_id == NULL) {
804 "Invalid Argument(s), rc:%s\n",
809 rc = tf_session_get_session_internal(tfp, &tfs);
813 *fw_session_id = tfs->session_id.internal.fw_session_id;
819 tf_session_get_session_id(struct tf *tfp,
820 union tf_session_id *session_id)
823 struct tf_session *tfs = NULL;
825 if (tfp->session == NULL) {
828 "Session not created, rc:%s\n",
833 if (session_id == NULL) {
836 "Invalid Argument(s), rc:%s\n",
841 /* Using internal version as session client may not exist yet */
842 rc = tf_session_get_session_internal(tfp, &tfs);
846 *session_id = tfs->session_id;
852 tf_session_get_em_ext_db(struct tf *tfp,
853 void **em_ext_db_handle)
855 struct tf_session *tfs = NULL;
858 *em_ext_db_handle = NULL;
863 rc = tf_session_get_session_internal(tfp, &tfs);
867 *em_ext_db_handle = tfs->em_ext_db_handle;
872 tf_session_set_em_ext_db(struct tf *tfp,
873 void *em_ext_db_handle)
875 struct tf_session *tfs = NULL;
881 rc = tf_session_get_session_internal(tfp, &tfs);
885 tfs->em_ext_db_handle = em_ext_db_handle;
890 tf_session_get_db(struct tf *tfp,
891 enum tf_module_type type,
894 struct tf_session *tfs = NULL;
902 rc = tf_session_get_session_internal(tfp, &tfs);
907 case TF_MODULE_TYPE_IDENTIFIER:
908 if (tfs->id_db_handle)
909 *db_handle = tfs->id_db_handle;
913 case TF_MODULE_TYPE_TABLE:
914 if (tfs->tbl_db_handle)
915 *db_handle = tfs->tbl_db_handle;
920 case TF_MODULE_TYPE_TCAM:
921 if (tfs->tcam_db_handle)
922 *db_handle = tfs->tcam_db_handle;
926 case TF_MODULE_TYPE_EM:
927 if (tfs->em_db_handle)
928 *db_handle = tfs->em_db_handle;
941 tf_session_set_db(struct tf *tfp,
942 enum tf_module_type type,
945 struct tf_session *tfs = NULL;
951 rc = tf_session_get_session_internal(tfp, &tfs);
956 case TF_MODULE_TYPE_IDENTIFIER:
957 tfs->id_db_handle = db_handle;
959 case TF_MODULE_TYPE_TABLE:
960 tfs->tbl_db_handle = db_handle;
962 case TF_MODULE_TYPE_TCAM:
963 tfs->tcam_db_handle = db_handle;
965 case TF_MODULE_TYPE_EM:
966 tfs->em_db_handle = db_handle;
976 #ifdef TF_TCAM_SHARED
979 tf_session_get_tcam_shared_db(struct tf *tfp,
980 void **tcam_shared_db_handle)
982 struct tf_session *tfs = NULL;
985 *tcam_shared_db_handle = NULL;
990 rc = tf_session_get_session_internal(tfp, &tfs);
994 *tcam_shared_db_handle = tfs->tcam_shared_db_handle;
999 tf_session_set_tcam_shared_db(struct tf *tfp,
1000 void *tcam_shared_db_handle)
1002 struct tf_session *tfs = NULL;
1008 rc = tf_session_get_session_internal(tfp, &tfs);
1012 tfs->tcam_shared_db_handle = tcam_shared_db_handle;
1015 #endif /* TF_TCAM_SHARED */