+/**
+ * Creates a Session Client on an existing Session.
+ *
+ * [in] tfp
+ * Pointer to TF handle
+ *
+ * [in] parms
+ * Pointer to session client create parameters
+ *
+ * Returns
+ * - (0) if successful.
+ * - (-EINVAL) on failure.
+ * - (-ENOMEM) if max session clients has been reached.
+ */
+static int
+tf_session_client_create(struct tf *tfp,
+ struct tf_session_client_create_parms *parms)
+{
+ int rc;
+ struct tf_session *session = NULL;
+ struct tf_session_client *client;
+ struct tfp_calloc_parms cparms;
+ union tf_session_client_id session_client_id;
+
+ TF_CHECK_PARMS2(tfp, parms);
+
+ /* Using internal version as session client may not exist yet */
+ rc = tf_session_get_session_internal(tfp, &session);
+ if (rc) {
+ TFP_DRV_LOG(ERR,
+ "Failed to lookup session, rc:%s\n",
+ strerror(-rc));
+ return rc;
+ }
+
+ client = tf_session_find_session_client_by_name(session,
+ parms->ctrl_chan_name);
+ if (client) {
+ TFP_DRV_LOG(ERR,
+ "Client %s, already registered with this session\n",
+ parms->ctrl_chan_name);
+ return -EOPNOTSUPP;
+ }
+
+ rc = tf_msg_session_client_register
+ (tfp,
+ parms->ctrl_chan_name,
+ &session_client_id.internal.fw_session_client_id);
+ if (rc) {
+ TFP_DRV_LOG(ERR,
+ "Failed to create client on session, rc:%s\n",
+ strerror(-rc));
+ return rc;
+ }
+
+ /* Create the local session client, initialize and attach to
+ * the session
+ */
+ cparms.nitems = 1;
+ cparms.size = sizeof(struct tf_session_client);
+ cparms.alignment = 0;
+ rc = tfp_calloc(&cparms);
+ if (rc) {
+ TFP_DRV_LOG(ERR,
+ "Failed to allocate session client, rc:%s\n",
+ strerror(-rc));
+ goto cleanup;
+ }
+ client = cparms.mem_va;
+
+ /* Register FID with the client */
+ rc = tfp_get_fid(tfp, &client->fw_fid);
+ if (rc)
+ return rc;
+
+ /* Build the Session Client ID by adding the fw_session_id */
+ rc = tf_session_get_fw_session_id
+ (tfp,
+ &session_client_id.internal.fw_session_id);
+ if (rc) {
+ TFP_DRV_LOG(ERR,
+ "Session Firmware id lookup failed, rc:%s\n",
+ strerror(-rc));
+ return rc;
+ }
+
+ tfp_memcpy(client->ctrl_chan_name,
+ parms->ctrl_chan_name,
+ TF_SESSION_NAME_MAX);
+
+ client->session_client_id.id = session_client_id.id;
+
+ ll_insert(&session->client_ll, &client->ll_entry);
+
+ session->ref_count++;
+
+ /* Build the return value */
+ parms->session_client_id->id = session_client_id.id;
+
+ cleanup:
+ /* TBD - Add code to unregister newly create client from fw */
+
+ return rc;
+}
+
+
+/**
+ * Destroys a Session Client on an existing Session.
+ *
+ * [in] tfp
+ * Pointer to TF handle
+ *
+ * [in] parms
+ * Pointer to the session client destroy parameters
+ *
+ * Returns
+ * - (0) if successful.
+ * - (-EINVAL) on failure.
+ * - (-ENOTFOUND) error, client not owned by the session.
+ * - (-ENOTSUPP) error, unable to destroy client as its the last
+ * client. Please use the tf_session_close().
+ */
+static int
+tf_session_client_destroy(struct tf *tfp,
+ struct tf_session_client_destroy_parms *parms)
+{
+ int rc;
+ struct tf_session *tfs;
+ struct tf_session_client *client;
+
+ TF_CHECK_PARMS2(tfp, parms);
+
+ rc = tf_session_get_session(tfp, &tfs);
+ if (rc) {
+ TFP_DRV_LOG(ERR,
+ "Failed to lookup session, rc:%s\n",
+ strerror(-rc));
+ return rc;
+ }
+
+ /* Check session owns this client and that we're not the last client */
+ client = tf_session_get_session_client(tfs,
+ parms->session_client_id);
+ if (client == NULL) {
+ TFP_DRV_LOG(ERR,
+ "Client %d, not found within this session\n",
+ parms->session_client_id.id);
+ return -EINVAL;
+ }
+
+ /* If last client the request is rejected and cleanup should
+ * be done by session close.
+ */
+ if (tfs->ref_count == 1)
+ return -EOPNOTSUPP;
+
+ rc = tf_msg_session_client_unregister
+ (tfp,
+ parms->session_client_id.internal.fw_session_client_id);
+
+ /* Log error, but continue. If FW fails we do not really have
+ * a way to fix this but the client would no longer be valid
+ * thus we remove from the session.
+ */
+ if (rc) {
+ TFP_DRV_LOG(ERR,
+ "Client destroy on FW Failed, rc:%s\n",
+ strerror(-rc));
+ }
+
+ ll_delete(&tfs->client_ll, &client->ll_entry);
+
+ /* Decrement the session ref_count */
+ tfs->ref_count--;
+
+ tfp_free(client);
+
+ return rc;
+}
+
+int
+tf_session_open_session(struct tf *tfp,
+ struct tf_session_open_session_parms *parms)
+{
+ int rc;
+ struct tf_session_client_create_parms scparms;
+
+ TF_CHECK_PARMS2(tfp, parms);
+
+ /* Decide if we're creating a new session or session client */
+ if (tfp->session == NULL) {
+ rc = tf_session_create(tfp, parms);
+ if (rc) {
+ TFP_DRV_LOG(ERR,
+ "Failed to create session, ctrl_chan_name:%s, rc:%s\n",
+ parms->open_cfg->ctrl_chan_name,
+ strerror(-rc));
+ return rc;
+ }
+
+ TFP_DRV_LOG(INFO,
+ "Session created, session_client_id:%d, session_id:%d\n",
+ parms->open_cfg->session_client_id.id,
+ parms->open_cfg->session_id.id);
+ } else {
+ scparms.ctrl_chan_name = parms->open_cfg->ctrl_chan_name;
+ scparms.session_client_id = &parms->open_cfg->session_client_id;
+
+ /* Create the new client and get it associated with
+ * the session.
+ */
+ rc = tf_session_client_create(tfp, &scparms);
+ if (rc) {
+ TFP_DRV_LOG(ERR,
+ "Failed to create client on session %d, rc:%s\n",
+ parms->open_cfg->session_id.id,
+ strerror(-rc));
+ return rc;
+ }
+
+ TFP_DRV_LOG(INFO,
+ "Session Client:%d created on session:%d\n",
+ parms->open_cfg->session_client_id.id,
+ parms->open_cfg->session_id.id);
+ }
+
+ return 0;
+}
+