SPM: Merge 'internal_msg' members into handle

'internal_msg' is a member of handle, defining a dedicated type for
it causes troubles when:

- Defining interfaces for processing connections. Taking the handle
  as parameter makes message abstraction unnecessary, and taking
  message as paramter makes extra message-to-handle mapping happen.

This patch is basically a search and replace, which eases the review.
After the merging, several unnecessary mapping functions are removed
(set/get rhandle e.g.). Followed up patches focus on detailed refine.

Change-Id: I2ec1375fde470d4a3983848afb46575d3cb0b392
Signed-off-by: Ken Liu <Ken.Liu@arm.com>
diff --git a/secure_fw/spm/ffm/backend_ipc.c b/secure_fw/spm/ffm/backend_ipc.c
index 48c7899..443c5da 100644
--- a/secure_fw/spm/ffm/backend_ipc.c
+++ b/secure_fw/spm/ffm/backend_ipc.c
@@ -46,13 +46,13 @@
  * current thread and trigger scheduler.
  */
 static psa_status_t ipc_messaging(struct service_t *service,
-                                  struct tfm_msg_body_t *msg)
+                                  struct conn_handle_t *hdl)
 {
     struct partition_t *p_owner = NULL;
     psa_signal_t signal = 0;
     struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
 
-    if (!msg || !service || !service->p_ldinf || !service->partition) {
+    if (!hdl || !service || !service->p_ldinf || !service->partition) {
         return PSA_ERROR_PROGRAMMER_ERROR;
     }
 
@@ -61,7 +61,7 @@
 
     CRITICAL_SECTION_ENTER(cs_assert);
 
-    UNI_LIST_INSERT_AFTER(p_owner, msg, p_messages);
+    UNI_LIST_INSERT_AFTER(p_owner, hdl, p_handles);
 
     /* Messages put. Update signals */
     p_owner->signals_asserted |= signal;
@@ -78,19 +78,19 @@
      * thread.
      */
 
-    if (!is_tfm_rpc_msg(msg)) {
-        thrd_wait_on(&msg->ack_evnt, CURRENT_THREAD);
+    if (!is_tfm_rpc_msg(hdl)) {
+        thrd_wait_on(&hdl->ack_evnt, CURRENT_THREAD);
     }
 
     return PSA_SUCCESS;
 }
 
-static int32_t ipc_replying(struct tfm_msg_body_t *p_msg, int32_t status)
+static int32_t ipc_replying(struct conn_handle_t *hdl, int32_t status)
 {
-    if (is_tfm_rpc_msg(p_msg)) {
-        tfm_rpc_client_call_reply(p_msg, status);
+    if (is_tfm_rpc_msg(hdl)) {
+        tfm_rpc_client_call_reply(hdl, status);
     } else {
-        thrd_wake_up(&p_msg->ack_evnt, status);
+        thrd_wake_up(&hdl->ack_evnt, status);
     }
 
     /*
@@ -110,7 +110,7 @@
     p_pt->signals_allowed |= PSA_DOORBELL | service_setting;
 
     THRD_SYNC_INIT(&p_pt->waitobj);
-    UNI_LISI_INIT_NODE(p_pt, p_messages);
+    UNI_LISI_INIT_NODE(p_pt, p_handles);
 
     THRD_INIT(&p_pt->thrd, &p_pt->ctx_ctrl,
               TO_THREAD_PRIORITY(PARTITION_PRIORITY(p_pldi->flags)));
diff --git a/secure_fw/spm/ffm/backend_sfn.c b/secure_fw/spm/ffm/backend_sfn.c
index b84c307..ab427b2 100644
--- a/secure_fw/spm/ffm/backend_sfn.c
+++ b/secure_fw/spm/ffm/backend_sfn.c
@@ -36,18 +36,18 @@
  * current component state and activate the next component.
  */
 static psa_status_t sfn_messaging(struct service_t *service,
-                                  struct tfm_msg_body_t *msg)
+                                  struct conn_handle_t *hdl)
 {
     struct partition_t *p_target;
     psa_status_t status;
 
-    if (!msg || !service || !service->p_ldinf || !service->partition) {
+    if (!hdl || !service || !service->p_ldinf || !service->partition) {
         return PSA_ERROR_PROGRAMMER_ERROR;
     }
 
-    msg->sfn_magic = TFM_MSG_MAGIC_SFN;
+    hdl->sfn_magic = TFM_MSG_MAGIC_SFN;
     p_target = service->partition;
-    p_target->p_messages = msg;
+    p_target->p_handles = hdl;
 
     SET_CURRENT_COMPONENT(p_target);
 
@@ -62,14 +62,14 @@
         p_target->state = SFN_PARTITION_STATE_INITED;
     }
 
-    status = ((service_fn_t)service->p_ldinf->sfn)(&msg->msg);
+    status = ((service_fn_t)service->p_ldinf->sfn)(&hdl->msg);
 
     return status;
 }
 
-static int32_t sfn_replying(struct tfm_msg_body_t *p_msg, int32_t status)
+static int32_t sfn_replying(struct conn_handle_t *hdl, int32_t status)
 {
-    SET_CURRENT_COMPONENT(p_msg->p_client);
+    SET_CURRENT_COMPONENT(hdl->p_client);
 
     /*
      * Returning a value here is necessary, because 'psa_reply' is absent
@@ -89,7 +89,7 @@
 {
     const struct partition_load_info_t *p_pldi = p_pt->p_ldinf;
 
-    p_pt->p_messages = NULL;
+    p_pt->p_handles = NULL;
     p_pt->state = SFN_PARTITION_STATE_NOT_INITED;
 
     THRD_SYNC_INIT(&p_pt->waitobj);
diff --git a/secure_fw/spm/ffm/psa_api.c b/secure_fw/spm/ffm/psa_api.c
index 1588263..80af53f 100644
--- a/secure_fw/spm/ffm/psa_api.c
+++ b/secure_fw/spm/ffm/psa_api.c
@@ -146,9 +146,8 @@
 {
     psa_invec invecs[PSA_MAX_IOVEC];
     psa_outvec outvecs[PSA_MAX_IOVEC];
-    struct tfm_conn_handle_t *conn_handle;
+    struct conn_handle_t *conn_handle;
     struct service_t *service;
-    struct tfm_msg_body_t *msg;
     int i, j;
     int32_t client_id;
     uint32_t sid, version, index;
@@ -238,7 +237,7 @@
             return PSA_ERROR_PROGRAMMER_ERROR;
         }
 
-        service = conn_handle->internal_msg.service;
+        service = conn_handle->service;
 
         if (!service) {
             /* FixMe: Need to implement a mechanism to resolve this failure. */
@@ -313,12 +312,10 @@
         }
     }
 
-    msg = &(conn_handle->internal_msg);
-
-    tfm_spm_fill_msg(msg, service, handle, type, client_id,
+    spm_fill_message(conn_handle, service, handle, type, client_id,
                      invecs, in_num, outvecs, out_num, outptr);
 
-    return backend_instance.messaging(service, msg);
+    return backend_instance.messaging(service, conn_handle);
 }
 
 /* Following PSA APIs are only needed by connection-based services */
@@ -327,8 +324,7 @@
 psa_status_t tfm_spm_client_psa_connect(uint32_t sid, uint32_t version)
 {
     struct service_t *service;
-    struct tfm_msg_body_t *msg;
-    struct tfm_conn_handle_t *connect_handle;
+    struct conn_handle_t *connect_handle;
     int32_t client_id;
     psa_handle_t handle;
     bool ns_caller = tfm_spm_is_ns_caller();
@@ -377,21 +373,18 @@
         return PSA_ERROR_CONNECTION_BUSY;
     }
 
-    msg = &(connect_handle->internal_msg);
-
     handle = tfm_spm_to_user_handle(connect_handle);
     /* No input or output needed for connect message */
-    tfm_spm_fill_msg(msg, service, handle, PSA_IPC_CONNECT,
+    spm_fill_message(connect_handle, service, handle, PSA_IPC_CONNECT,
                      client_id, NULL, 0, NULL, 0, NULL);
 
-    return backend_instance.messaging(service, msg);
+    return backend_instance.messaging(service, connect_handle);
 }
 
 psa_status_t tfm_spm_client_psa_close(psa_handle_t handle)
 {
     struct service_t *service;
-    struct tfm_msg_body_t *msg;
-    struct tfm_conn_handle_t *conn_handle;
+    struct conn_handle_t *conn_handle;
     int32_t client_id;
     bool ns_caller = tfm_spm_is_ns_caller();
 
@@ -416,14 +409,12 @@
         return PSA_ERROR_PROGRAMMER_ERROR;
     }
 
-    service = conn_handle->internal_msg.service;
+    service = conn_handle->service;
     if (!service) {
         /* FixMe: Need to implement one mechanism to resolve this failure. */
         return PSA_ERROR_PROGRAMMER_ERROR;
     }
 
-    msg = &(conn_handle->internal_msg);
-
     /*
      * It is a PROGRAMMER ERROR if the connection is currently handling a
      * request.
@@ -433,10 +424,10 @@
     }
 
     /* No input or output needed for close message */
-    tfm_spm_fill_msg(msg, service, handle, PSA_IPC_DISCONNECT, client_id,
-                     NULL, 0, NULL, 0, NULL);
+    spm_fill_message(conn_handle, service, handle, PSA_IPC_DISCONNECT,
+                     client_id, NULL, 0, NULL, 0, NULL);
 
-    return backend_instance.messaging(service, msg);
+    return backend_instance.messaging(service, conn_handle);
 }
 
 #endif /* CONFIG_TFM_CONNECTION_BASED_SERVICE_API */
@@ -481,7 +472,7 @@
 
 psa_status_t tfm_spm_partition_psa_get(psa_signal_t signal, psa_msg_t *msg)
 {
-    struct tfm_msg_body_t *tmp_msg = NULL;
+    struct conn_handle_t *tmp_msg = NULL;
     struct partition_t *partition = NULL;
     uint32_t privileged;
 
@@ -526,14 +517,12 @@
      * Get message by signal from partition. It is a fatal error if getting
      * failed, which means the input signal is not correspond to an RoT service.
      */
-    tmp_msg = spm_get_msg_with_signal(partition, signal);
+    tmp_msg = spm_get_handle_by_signal(partition, signal);
     if (!tmp_msg) {
         return PSA_ERROR_DOES_NOT_EXIST;
     }
 
-    (TO_CONTAINER(tmp_msg,
-                  struct tfm_conn_handle_t,
-                  internal_msg))->status = TFM_HANDLE_STATUS_ACTIVE;
+    tmp_msg->status = TFM_HANDLE_STATUS_ACTIVE;
 
     spm_memcpy(msg, &tmp_msg->msg, sizeof(psa_msg_t));
 
@@ -544,11 +533,11 @@
                                   void *buffer, size_t num_bytes)
 {
     size_t bytes;
-    struct tfm_msg_body_t *msg = NULL;
+    struct conn_handle_t *msg = NULL;
     uint32_t priv_mode;
 
     /* It is a fatal error if message handle is invalid */
-    msg = tfm_spm_get_msg_from_handle(msg_handle);
+    msg = spm_get_handle_by_user_handle(msg_handle);
     if (!msg) {
         tfm_core_panic();
     }
@@ -612,10 +601,10 @@
 size_t tfm_spm_partition_psa_skip(psa_handle_t msg_handle, uint32_t invec_idx,
                                   size_t num_bytes)
 {
-    struct tfm_msg_body_t *msg = NULL;
+    struct conn_handle_t *msg = NULL;
 
     /* It is a fatal error if message handle is invalid */
-    msg = tfm_spm_get_msg_from_handle(msg_handle);
+    msg = spm_get_handle_by_user_handle(msg_handle);
     if (!msg) {
         tfm_core_panic();
     }
@@ -672,11 +661,11 @@
 void tfm_spm_partition_psa_write(psa_handle_t msg_handle, uint32_t outvec_idx,
                                  const void *buffer, size_t num_bytes)
 {
-    struct tfm_msg_body_t *msg = NULL;
+    struct conn_handle_t *msg = NULL;
     uint32_t priv_mode;
 
     /* It is a fatal error if message handle is invalid */
-    msg = tfm_spm_get_msg_from_handle(msg_handle);
+    msg = spm_get_handle_by_user_handle(msg_handle);
     if (!msg) {
         tfm_core_panic();
     }
@@ -739,15 +728,14 @@
 int32_t tfm_spm_partition_psa_reply(psa_handle_t msg_handle,
                                     psa_status_t status)
 {
-    struct service_t *service = NULL;
-    struct tfm_msg_body_t *msg = NULL;
+    struct service_t *service;
+    struct conn_handle_t *hdl;
     int32_t ret = PSA_SUCCESS;
-    struct tfm_conn_handle_t *conn_handle;
     struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
 
     /* It is a fatal error if message handle is invalid */
-    msg = tfm_spm_get_msg_from_handle(msg_handle);
-    if (!msg) {
+    hdl = spm_get_handle_by_user_handle(msg_handle);
+    if (!hdl) {
         tfm_core_panic();
     }
 
@@ -756,17 +744,12 @@
      * body structure. Only two parameters are passed in this function: handle
      * and status, so it is useful and simply to do like this.
      */
-    service = msg->service;
+    service = hdl->service;
     if (!service) {
         tfm_core_panic();
     }
 
-    /*
-     * Three type of message are passed in this function: CONNECTION, REQUEST,
-     * DISCONNECTION. It needs to process differently for each type.
-     */
-    conn_handle = tfm_spm_to_handle_instance(msg_handle);
-    switch (msg->msg.type) {
+    switch (hdl->msg.type) {
     case PSA_IPC_CONNECT:
         /*
          * Reply to PSA_IPC_CONNECT message. Connect handle is returned if the
@@ -777,7 +760,7 @@
             ret = msg_handle;
         } else if (status == PSA_ERROR_CONNECTION_REFUSED) {
             /* Refuse the client connection, indicating a permanent error. */
-            tfm_spm_free_conn_handle(service, conn_handle);
+            tfm_spm_free_conn_handle(service, hdl);
             ret = PSA_ERROR_CONNECTION_REFUSED;
         } else if (status == PSA_ERROR_CONNECTION_BUSY) {
             /* Fail the client connection, indicating a transient error. */
@@ -788,7 +771,7 @@
         break;
     case PSA_IPC_DISCONNECT:
         /* Service handle is not used anymore */
-        tfm_spm_free_conn_handle(service, conn_handle);
+        tfm_spm_free_conn_handle(service, hdl);
 
         /*
          * If the message type is PSA_IPC_DISCONNECT, then the status code is
@@ -796,7 +779,7 @@
          */
         break;
     default:
-        if (msg->msg.type >= PSA_IPC_CALL) {
+        if (hdl->msg.type >= PSA_IPC_CALL) {
 
 #if PSA_FRAMEWORK_HAS_MM_IOVEC
 
@@ -807,14 +790,14 @@
             int i;
 
             for (i = 0; i < PSA_MAX_IOVEC * 2; i++) {
-                if (IOVEC_IS_MAPPED(msg, i) && (!IOVEC_IS_UNMAPPED(msg, i))) {
-                    SET_IOVEC_UNMAPPED(msg, i);
+                if (IOVEC_IS_MAPPED(hdl, i) && (!IOVEC_IS_UNMAPPED(hdl, i))) {
+                    SET_IOVEC_UNMAPPED(hdl, i);
                     /*
                      * Any output vectors that are still mapped will report that
                      * zero bytes have been written.
                      */
                     if (i >= OUTVEC_IDX_BASE) {
-                        msg->outvec[i - OUTVEC_IDX_BASE].len = 0;
+                        hdl->outvec[i - OUTVEC_IDX_BASE].len = 0;
                     }
                 }
             }
@@ -828,9 +811,9 @@
              * psa_outvec structure for the parameter before returning from
              * psa_call().
              */
-            update_caller_outvec_len(msg);
+            update_caller_outvec_len(hdl);
             if (SERVICE_IS_STATELESS(service->p_ldinf->flags)) {
-                tfm_spm_free_conn_handle(service, conn_handle);
+                tfm_spm_free_conn_handle(service, hdl);
             }
         } else {
             tfm_core_panic();
@@ -842,13 +825,13 @@
          * If the source of the programmer error is a Secure Partition, the SPM
          * must panic the Secure Partition in response to a PROGRAMMER ERROR.
          */
-        if (TFM_CLIENT_ID_IS_NS(msg->msg.client_id)) {
-            conn_handle->status = TFM_HANDLE_STATUS_CONNECT_ERROR;
+        if (TFM_CLIENT_ID_IS_NS(hdl->msg.client_id)) {
+            hdl->status = TFM_HANDLE_STATUS_CONNECT_ERROR;
         } else {
             tfm_core_panic();
         }
     } else {
-        conn_handle->status = TFM_HANDLE_STATUS_IDLE;
+        hdl->status = TFM_HANDLE_STATUS_IDLE;
     }
 
     /*
@@ -857,7 +840,7 @@
      * involved.
      */
     CRITICAL_SECTION_ENTER(cs_assert);
-    ret = backend_instance.replying(msg, ret);
+    ret = backend_instance.replying(hdl, ret);
     CRITICAL_SECTION_LEAVE(cs_assert);
 
     return ret;
@@ -996,25 +979,21 @@
 
 void tfm_spm_partition_psa_set_rhandle(psa_handle_t msg_handle, void *rhandle)
 {
-    struct tfm_msg_body_t *msg = NULL;
-    struct tfm_conn_handle_t *conn_handle;
+    struct conn_handle_t *hdl;
 
     /* It is a fatal error if message handle is invalid */
-    msg = tfm_spm_get_msg_from_handle(msg_handle);
-    if (!msg) {
+    hdl = spm_get_handle_by_user_handle(msg_handle);
+    if (!hdl) {
         tfm_core_panic();
     }
 
     /* It is a PROGRAMMER ERROR if a stateless service sets rhandle. */
-    if (SERVICE_IS_STATELESS(msg->service->p_ldinf->flags)) {
+    if (SERVICE_IS_STATELESS(hdl->service->p_ldinf->flags)) {
         tfm_core_panic();
     }
 
-    msg->msg.rhandle = rhandle;
-    conn_handle = tfm_spm_to_handle_instance(msg_handle);
-
-    /* Store reverse handle for following client calls. */
-    tfm_spm_set_rhandle(msg->service, conn_handle, rhandle);
+    hdl->msg.rhandle = rhandle;
+    hdl->rhandle = rhandle;
 }
 
 #endif /* CONFIG_TFM_CONNECTION_BASED_SERVICE_API */
@@ -1024,24 +1003,24 @@
 const void *tfm_spm_partition_psa_map_invec(psa_handle_t msg_handle,
                                             uint32_t invec_idx)
 {
-    struct tfm_msg_body_t *msg = NULL;
+    struct conn_handle_t *hdl;
     uint32_t privileged;
     struct partition_t *partition = NULL;
 
     /* It is a fatal error if message handle is invalid */
-    msg = tfm_spm_get_msg_from_handle(msg_handle);
-    if (!msg) {
+    hdl = spm_get_handle_by_user_handle(msg_handle);
+    if (!hdl) {
         tfm_core_panic();
     }
 
-    partition = msg->service->partition;
+    partition = hdl->service->partition;
     privileged = GET_PARTITION_PRIVILEGED_MODE(partition->p_ldinf);
 
     /*
      * It is a fatal error if MM-IOVEC has not been enabled for the RoT
      * Service that received the message.
      */
-    if (!SERVICE_ENABLED_MM_IOVEC(msg->service->p_ldinf->flags)) {
+    if (!SERVICE_ENABLED_MM_IOVEC(hdl->service->p_ldinf->flags)) {
         tfm_core_panic();
     }
 
@@ -1049,7 +1028,7 @@
      * It is a fatal error if message handle does not refer to a request
      * message.
      */
-    if (msg->msg.type < PSA_IPC_CALL) {
+    if (hdl->msg.type < PSA_IPC_CALL) {
         tfm_core_panic();
     }
 
@@ -1062,7 +1041,7 @@
     }
 
     /* It is a fatal error if the input vector has length zero. */
-    if (msg->msg.in_size[invec_idx] == 0) {
+    if (hdl->msg.in_size[invec_idx] == 0) {
         tfm_core_panic();
     }
 
@@ -1070,7 +1049,7 @@
      * It is a fatal error if the input vector has already been mapped using
      * psa_map_invec().
      */
-    if (IOVEC_IS_MAPPED(msg, (invec_idx + INVEC_IDX_BASE))) {
+    if (IOVEC_IS_MAPPED(hdl, (invec_idx + INVEC_IDX_BASE))) {
         tfm_core_panic();
     }
 
@@ -1078,7 +1057,7 @@
      * It is a fatal error if the input vector has already been accessed
      * using psa_read() or psa_skip().
      */
-    if (IOVEC_IS_ACCESSED(msg, (invec_idx + INVEC_IDX_BASE))) {
+    if (IOVEC_IS_ACCESSED(hdl, (invec_idx + INVEC_IDX_BASE))) {
         tfm_core_panic();
     }
 
@@ -1086,24 +1065,24 @@
      * It is a fatal error if the memory reference for the wrap input vector is
      * invalid or not readable.
      */
-    if (tfm_memory_check(msg->invec[invec_idx].base, msg->invec[invec_idx].len,
+    if (tfm_memory_check(hdl->invec[invec_idx].base, hdl->invec[invec_idx].len,
         false, TFM_MEMORY_ACCESS_RO, privileged) != SPM_SUCCESS) {
         tfm_core_panic();
     }
 
-    SET_IOVEC_MAPPED(msg, (invec_idx + INVEC_IDX_BASE));
+    SET_IOVEC_MAPPED(hdl, (invec_idx + INVEC_IDX_BASE));
 
-    return msg->invec[invec_idx].base;
+    return hdl->invec[invec_idx].base;
 }
 
 void tfm_spm_partition_psa_unmap_invec(psa_handle_t msg_handle,
                                        uint32_t invec_idx)
 {
-    struct tfm_msg_body_t *msg = NULL;
+    struct conn_handle_t *hdl;
 
     /* It is a fatal error if message handle is invalid */
-    msg = tfm_spm_get_msg_from_handle(msg_handle);
-    if (!msg) {
+    hdl = spm_get_handle_by_user_handle(msg_handle);
+    if (!hdl) {
         tfm_core_panic();
     }
 
@@ -1111,7 +1090,7 @@
      * It is a fatal error if MM-IOVEC has not been enabled for the RoT
      * Service that received the message.
      */
-    if (!SERVICE_ENABLED_MM_IOVEC(msg->service->p_ldinf->flags)) {
+    if (!SERVICE_ENABLED_MM_IOVEC(hdl->service->p_ldinf->flags)) {
         tfm_core_panic();
     }
 
@@ -1119,7 +1098,7 @@
      * It is a fatal error if message handle does not refer to a request
      * message.
      */
-    if (msg->msg.type < PSA_IPC_CALL) {
+    if (hdl->msg.type < PSA_IPC_CALL) {
         tfm_core_panic();
     }
 
@@ -1135,7 +1114,7 @@
      * It is a fatal error if The input vector has not been mapped by a call to
      * psa_map_invec().
      */
-    if (!IOVEC_IS_MAPPED(msg, (invec_idx + INVEC_IDX_BASE))) {
+    if (!IOVEC_IS_MAPPED(hdl, (invec_idx + INVEC_IDX_BASE))) {
         tfm_core_panic();
     }
 
@@ -1143,34 +1122,34 @@
      * It is a fatal error if the input vector has already been unmapped by a
      * call to psa_unmap_invec().
      */
-    if (IOVEC_IS_UNMAPPED(msg, (invec_idx + INVEC_IDX_BASE))) {
+    if (IOVEC_IS_UNMAPPED(hdl, (invec_idx + INVEC_IDX_BASE))) {
         tfm_core_panic();
     }
 
-    SET_IOVEC_UNMAPPED(msg, (invec_idx + INVEC_IDX_BASE));
+    SET_IOVEC_UNMAPPED(hdl, (invec_idx + INVEC_IDX_BASE));
 }
 
 void *tfm_spm_partition_psa_map_outvec(psa_handle_t msg_handle,
                                        uint32_t outvec_idx)
 {
-    struct tfm_msg_body_t *msg = NULL;
+    struct conn_handle_t *hdl;
     uint32_t privileged;
     struct partition_t *partition = NULL;
 
     /* It is a fatal error if message handle is invalid */
-    msg = tfm_spm_get_msg_from_handle(msg_handle);
-    if (!msg) {
+    hdl = spm_get_handle_by_user_handle(msg_handle);
+    if (!hdl) {
         tfm_core_panic();
     }
 
-    partition = msg->service->partition;
+    partition = hdl->service->partition;
     privileged = GET_PARTITION_PRIVILEGED_MODE(partition->p_ldinf);
 
     /*
      * It is a fatal error if MM-IOVEC has not been enabled for the RoT
      * Service that received the message.
      */
-    if (!SERVICE_ENABLED_MM_IOVEC(msg->service->p_ldinf->flags)) {
+    if (!SERVICE_ENABLED_MM_IOVEC(hdl->service->p_ldinf->flags)) {
         tfm_core_panic();
     }
 
@@ -1178,7 +1157,7 @@
      * It is a fatal error if message handle does not refer to a request
      * message.
      */
-    if (msg->msg.type < PSA_IPC_CALL) {
+    if (hdl->msg.type < PSA_IPC_CALL) {
         tfm_core_panic();
     }
 
@@ -1191,7 +1170,7 @@
     }
 
     /* It is a fatal error if the output vector has length zero. */
-    if (msg->msg.out_size[outvec_idx] == 0) {
+    if (hdl->msg.out_size[outvec_idx] == 0) {
         tfm_core_panic();
     }
 
@@ -1199,7 +1178,7 @@
      * It is a fatal error if the output vector has already been mapped using
      * psa_map_outvec().
      */
-    if (IOVEC_IS_MAPPED(msg, (outvec_idx + OUTVEC_IDX_BASE))) {
+    if (IOVEC_IS_MAPPED(hdl, (outvec_idx + OUTVEC_IDX_BASE))) {
         tfm_core_panic();
     }
 
@@ -1207,31 +1186,31 @@
      * It is a fatal error if the output vector has already been accessed
      * using psa_write().
      */
-    if (IOVEC_IS_ACCESSED(msg, (outvec_idx + OUTVEC_IDX_BASE))) {
+    if (IOVEC_IS_ACCESSED(hdl, (outvec_idx + OUTVEC_IDX_BASE))) {
         tfm_core_panic();
     }
 
     /*
      * It is a fatal error if the output vector is invalid or not read-write.
      */
-    if (tfm_memory_check(msg->outvec[outvec_idx].base,
-                         msg->outvec[outvec_idx].len, false,
+    if (tfm_memory_check(hdl->outvec[outvec_idx].base,
+                         hdl->outvec[outvec_idx].len, false,
                          TFM_MEMORY_ACCESS_RW, privileged) != SPM_SUCCESS) {
         tfm_core_panic();
     }
-    SET_IOVEC_MAPPED(msg, (outvec_idx + OUTVEC_IDX_BASE));
+    SET_IOVEC_MAPPED(hdl, (outvec_idx + OUTVEC_IDX_BASE));
 
-    return msg->outvec[outvec_idx].base;
+    return hdl->outvec[outvec_idx].base;
 }
 
 void tfm_spm_partition_psa_unmap_outvec(psa_handle_t msg_handle,
                                         uint32_t outvec_idx, size_t len)
 {
-    struct tfm_msg_body_t *msg = NULL;
+    struct conn_handle_t *hdl;
 
     /* It is a fatal error if message handle is invalid */
-    msg = tfm_spm_get_msg_from_handle(msg_handle);
-    if (!msg) {
+    hdl = spm_get_handle_by_user_handle(msg_handle);
+    if (!hdl) {
         tfm_core_panic();
     }
 
@@ -1239,7 +1218,7 @@
      * It is a fatal error if MM-IOVEC has not been enabled for the RoT
      * Service that received the message.
      */
-    if (!SERVICE_ENABLED_MM_IOVEC(msg->service->p_ldinf->flags)) {
+    if (!SERVICE_ENABLED_MM_IOVEC(hdl->service->p_ldinf->flags)) {
         tfm_core_panic();
     }
 
@@ -1247,7 +1226,7 @@
      * It is a fatal error if message handle does not refer to a request
      * message.
      */
-    if (msg->msg.type < PSA_IPC_CALL) {
+    if (hdl->msg.type < PSA_IPC_CALL) {
         tfm_core_panic();
     }
 
@@ -1262,7 +1241,7 @@
     /*
      * It is a fatal error if len is greater than the output vector size.
      */
-    if (len > msg->msg.out_size[outvec_idx]) {
+    if (len > hdl->msg.out_size[outvec_idx]) {
         tfm_core_panic();
     }
 
@@ -1270,7 +1249,7 @@
      * It is a fatal error if The output vector has not been mapped by a call to
      * psa_map_outvec().
      */
-    if (!IOVEC_IS_MAPPED(msg, (outvec_idx + OUTVEC_IDX_BASE))) {
+    if (!IOVEC_IS_MAPPED(hdl, (outvec_idx + OUTVEC_IDX_BASE))) {
         tfm_core_panic();
     }
 
@@ -1278,14 +1257,14 @@
      * It is a fatal error if the output vector has already been unmapped by a
      * call to psa_unmap_outvec().
      */
-    if (IOVEC_IS_UNMAPPED(msg, (outvec_idx + OUTVEC_IDX_BASE))) {
+    if (IOVEC_IS_UNMAPPED(hdl, (outvec_idx + OUTVEC_IDX_BASE))) {
         tfm_core_panic();
     }
 
-    SET_IOVEC_UNMAPPED(msg, (outvec_idx + OUTVEC_IDX_BASE));
+    SET_IOVEC_UNMAPPED(hdl, (outvec_idx + OUTVEC_IDX_BASE));
 
     /* Update the write number */
-    msg->outvec[outvec_idx].len = len;
+    hdl->outvec[outvec_idx].len = len;
 }
 
 #endif /* PSA_FRAMEWORK_HAS_MM_IOVEC */