Integrate routing extension into existing SPs

The RPC layer uses the SP messaging layer with the routing extension
enabled thus it makes SPs able to make or forward return channel calls.

Change-Id: Id359b0e570702404ef0151697436d454eb950e44
Signed-off-by: Imre Kis <imre.kis@arm.com>
diff --git a/components/rpc/ffarpc/caller/sp/ffarpc_caller.c b/components/rpc/ffarpc/caller/sp/ffarpc_caller.c
index 5f38194..9df7476 100644
--- a/components/rpc/ffarpc/caller/sp/ffarpc_caller.c
+++ b/components/rpc/ffarpc/caller/sp/ffarpc_caller.c
@@ -5,11 +5,12 @@
  */
 
 #include "ffarpc_caller.h"
-#include <components/rpc/ffarpc/endpoint/ffarpc_call_args.h>
+#include "ffarpc_sp_call_args.h"
 #include <components/rpc/ffarpc/endpoint/ffarpc_call_ops.h>
 #include <protocols/rpc/common/packed-c/status.h>
 #include <ffa_api.h>
 #include <sp_memory_management.h>
+#include <sp_messaging.h>
 #include <sp_rxtx.h>
 #include <trace.h>
 #include <stdbool.h>
@@ -59,9 +60,9 @@
 				int *opstatus, uint8_t **resp_buf, size_t *resp_len)
 {
 	struct ffarpc_caller *this_context = (struct ffarpc_caller *)context;
-	ffa_result res = FFA_OK;
-	struct ffa_direct_msg req = { };
-	struct ffa_direct_msg resp = { };
+	sp_result sp_res = SP_RESULT_OK;
+	struct sp_msg req = { 0 };
+	struct sp_msg resp = { 0 };
 	rpc_status_t status = TS_RPC_ERROR_INTERNAL;
 
 	if (handle != this_context || opstatus == NULL ||
@@ -79,31 +80,28 @@
 
 	req.destination_id = this_context->dest_partition_id;
 	req.source_id = own_id;
-	req.args[FFA_CALL_ARGS_IFACE_ID_OPCODE] =
+	req.args[SP_CALL_ARGS_IFACE_ID_OPCODE] =
 		FFA_CALL_ARGS_COMBINE_IFACE_ID_OPCODE(this_context->dest_partition_id, opcode);
 	//TODO: downcast problem?
-	req.args[FFA_CALL_ARGS_REQ_DATA_LEN] = (uint32_t)this_context->req_len;
-	req.args[FFA_CALL_ARGS_ENCODING] = this_context->rpc_caller.encoding;
+	req.args[SP_CALL_ARGS_REQ_DATA_LEN] = (uint32_t)this_context->req_len;
+	req.args[SP_CALL_ARGS_ENCODING] = this_context->rpc_caller.encoding;
 
 	/* Initialise the caller ID.  Depending on the call path, this may
 	 * be overridden by a higher privilege execution level, based on its
 	 * perspective of the caller identity.
 	 */
-	req.args[FFA_CALL_ARGS_CALLER_ID] = 0;
+	req.args[SP_CALL_ARGS_CALLER_ID] = 0;
 
-	res = ffa_msg_send_direct_req(req.source_id, req.destination_id,
-						req.args[0], req.args[1],
-						req.args[2], req.args[3],
-						req.args[4], &resp);
+	sp_res = sp_msg_send_direct_req(&req, &resp);
 
-	if (res != FFA_OK) {
-		EMSG("ffa_msg_send_direct_req(): error %"PRId32, res);
+	if (sp_res != SP_RESULT_OK) {
+		EMSG("sp_msg_send_direct_req(): error %"PRId32, sp_res);
 		goto out;
 	}
 
-	this_context->resp_len = (size_t)resp.args[FFA_CALL_ARGS_RESP_DATA_LEN];
-	status = resp.args[FFA_CALL_ARGS_RESP_RPC_STATUS];
-	*opstatus = resp.args[FFA_CALL_ARGS_RESP_OP_STATUS];
+	this_context->resp_len = (size_t)resp.args[SP_CALL_ARGS_RESP_DATA_LEN];
+	status = resp.args[SP_CALL_ARGS_RESP_RPC_STATUS];
+	*opstatus = resp.args[SP_CALL_ARGS_RESP_OP_STATUS];
 
 	if (this_context->resp_len > 0) {
 		this_context->resp_buf = shared_buffer;
@@ -209,11 +207,10 @@
 int ffarpc_caller_open(struct ffarpc_caller *s, uint16_t dest_partition_id, uint16_t dest_iface_id)
 {
 	//TODO: revise return type, error handling
-	ffa_result ffa_res;
-	struct ffa_direct_msg req = { };
-	struct ffa_direct_msg resp = { };
+	sp_result sp_res = SP_RESULT_OK;
+	struct sp_msg req = { 0 };
+	struct sp_msg resp = { 0 };
 
-	sp_result sp_res;
 	struct sp_memory_descriptor desc = { };
 	struct sp_memory_access_descriptor acc_desc = { };
 	struct sp_memory_region region = { };
@@ -240,19 +237,17 @@
 
 	req.source_id = own_id;
 	req.destination_id = dest_partition_id;
-	req.args[FFA_CALL_ARGS_IFACE_ID_OPCODE] =
+	req.args[SP_CALL_ARGS_IFACE_ID_OPCODE] =
 		FFA_CALL_ARGS_COMBINE_IFACE_ID_OPCODE(FFA_CALL_MGMT_IFACE_ID, FFA_CALL_OPCODE_SHARE_BUF);
-	req.args[FFA_CALL_ARGS_SHARE_MEM_HANDLE_LSW] = (uint32_t)(handle & UINT32_MAX);
-	req.args[FFA_CALL_ARGS_SHARE_MEM_HANDLE_MSW] = (uint32_t)(handle >> 32);
+	req.args[SP_CALL_ARGS_SHARE_MEM_HANDLE_LSW] = (uint32_t)(handle & UINT32_MAX);
+	req.args[SP_CALL_ARGS_SHARE_MEM_HANDLE_MSW] = (uint32_t)(handle >> 32);
 	//TODO: downcast
-	req.args[FFA_CALL_ARGS_SHARE_MEM_SIZE] = (uint32_t)(s->shared_mem_required_size);
+	req.args[SP_CALL_ARGS_SHARE_MEM_SIZE] = (uint32_t)(s->shared_mem_required_size);
 
-	ffa_res = ffa_msg_send_direct_req(req.source_id, req.destination_id,
-						req.args[0], req.args[1],
-						req.args[2], req.args[3],
-						req.args[4], &resp);
-	if (ffa_res != FFA_OK) {
-		EMSG("ffa_msg_send_direct_req(): error %"PRId32, ffa_res);
+	sp_res = sp_msg_send_direct_req(&req, &resp);
+
+	if (sp_res != SP_RESULT_OK) {
+		EMSG("sp_msg_send_direct_req(): error %"PRId32, sp_res);
 		return -1;
 	}
 
@@ -266,11 +261,10 @@
 int ffarpc_caller_close(struct ffarpc_caller *s)
 {
 	//TODO: revise return type, error handling
-	ffa_result ffa_res;
-	struct ffa_direct_msg req = { 0 };
-	struct ffa_direct_msg resp = { 0 };
+	sp_result sp_res = SP_RESULT_OK;
+	struct sp_msg req = { 0 };
+	struct sp_msg resp = { 0 };
 
-	sp_result sp_res;
 	uint32_t handle_lo, handle_hi;
 
 	handle_lo = (uint32_t)(s->shared_mem_handle & UINT32_MAX);
@@ -278,17 +272,15 @@
 
 	req.source_id = own_id;
 	req.destination_id = s->dest_partition_id;
-	req.args[FFA_CALL_ARGS_IFACE_ID_OPCODE] =
+	req.args[SP_CALL_ARGS_IFACE_ID_OPCODE] =
 		FFA_CALL_ARGS_COMBINE_IFACE_ID_OPCODE(FFA_CALL_MGMT_IFACE_ID, FFA_CALL_OPCODE_UNSHARE_BUF);
-	req.args[FFA_CALL_ARGS_SHARE_MEM_HANDLE_LSW] = handle_lo;
-	req.args[FFA_CALL_ARGS_SHARE_MEM_HANDLE_MSW] = handle_hi;
+	req.args[SP_CALL_ARGS_SHARE_MEM_HANDLE_LSW] = handle_lo;
+	req.args[SP_CALL_ARGS_SHARE_MEM_HANDLE_MSW] = handle_hi;
 
-	ffa_res = ffa_msg_send_direct_req(req.source_id, req.destination_id,
-						req.args[0], req.args[1],
-						req.args[2], req.args[3],
-						req.args[4], &resp);
-	if (ffa_res != FFA_OK) {
-		EMSG("ffa_msg_send_direct_req(): error %"PRId32, ffa_res);
+	sp_res = sp_msg_send_direct_req(&req, &resp);
+
+	if (sp_res != SP_RESULT_OK) {
+		EMSG("sp_msg_send_direct_req(): error %"PRId32, sp_res);
 		return -1;
 	}
 
diff --git a/components/rpc/ffarpc/caller/sp/ffarpc_sp_call_args.h b/components/rpc/ffarpc/caller/sp/ffarpc_sp_call_args.h
new file mode 100644
index 0000000..e85c9a9
--- /dev/null
+++ b/components/rpc/ffarpc/caller/sp/ffarpc_sp_call_args.h
@@ -0,0 +1,44 @@
+/* SPDX-License-Identifier: BSD-3-Clause */
+/*
+ * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
+ */
+
+#ifndef FFA_RPC_SP_CALL_ARGS_H_
+#define FFA_RPC_SP_CALL_ARGS_H_
+
+#include "components/rpc/ffarpc/endpoint/ffarpc_call_args.h"
+
+/*
+ * sp_msg args is shifted by one compared to ffa_direct_msg because the first
+ * parameter register (w3/w3) is used by the routing extension and it is not
+ * included in sp_msg args.
+ */
+#define FFA_TO_SP_CALL_OFFSET(offset) ((offset)-1)
+
+/* Common req & resp arg offests into sp_msg args structure */
+#define SP_CALL_ARGS_IFACE_ID_OPCODE                                           \
+	FFA_TO_SP_CALL_OFFSET(FFA_CALL_ARGS_IFACE_ID_OPCODE)
+
+/* Req arg offsets */
+#define SP_CALL_ARGS_REQ_DATA_LEN                                              \
+	FFA_TO_SP_CALL_OFFSET(FFA_CALL_ARGS_REQ_DATA_LEN)
+#define SP_CALL_ARGS_CALLER_ID FFA_TO_SP_CALL_OFFSET(FFA_CALL_ARGS_CALLER_ID)
+#define SP_CALL_ARGS_ENCODING FFA_TO_SP_CALL_OFFSET(FFA_CALL_ARGS_ENCODING)
+
+/* Resp arg offsets */
+#define SP_CALL_ARGS_RESP_DATA_LEN                                             \
+	FFA_TO_SP_CALL_OFFSET(FFA_CALL_ARGS_RESP_DATA_LEN)
+#define SP_CALL_ARGS_RESP_RPC_STATUS                                           \
+	FFA_TO_SP_CALL_OFFSET(FFA_CALL_ARGS_RESP_RPC_STATUS)
+#define SP_CALL_ARGS_RESP_OP_STATUS                                            \
+	FFA_TO_SP_CALL_OFFSET(FFA_CALL_ARGS_RESP_OP_STATUS)
+
+/* Share/unshare offsets */
+#define SP_CALL_ARGS_SHARE_MEM_HANDLE_LSW                                      \
+	FFA_TO_SP_CALL_OFFSET(FFA_CALL_ARGS_SHARE_MEM_HANDLE_LSW)
+#define SP_CALL_ARGS_SHARE_MEM_HANDLE_MSW                                      \
+	FFA_TO_SP_CALL_OFFSET(FFA_CALL_ARGS_SHARE_MEM_HANDLE_MSW)
+#define SP_CALL_ARGS_SHARE_MEM_SIZE                                            \
+	FFA_TO_SP_CALL_OFFSET(FFA_CALL_ARGS_SHARE_MEM_SIZE)
+
+#endif /* FFA_RPC_SP_CALL_ARGS_H_ */
diff --git a/components/rpc/ffarpc/endpoint/ffarpc_call_args.h b/components/rpc/ffarpc/endpoint/ffarpc_call_args.h
index fbe7320..1d2d9c9 100644
--- a/components/rpc/ffarpc/endpoint/ffarpc_call_args.h
+++ b/components/rpc/ffarpc/endpoint/ffarpc_call_args.h
@@ -26,22 +26,22 @@
     ((reg) & 0xffff)
 
 /* Common req & resp arg offests into msg_args structure */
-#define FFA_CALL_ARGS_IFACE_ID_OPCODE	    (0)
+#define FFA_CALL_ARGS_IFACE_ID_OPCODE	    (1)
 
 /* Req arg offsets */
-#define FFA_CALL_ARGS_REQ_DATA_LEN		    (1)
-#define FFA_CALL_ARGS_CALLER_ID		        (2)
-#define FFA_CALL_ARGS_ENCODING		        (3)
+#define FFA_CALL_ARGS_REQ_DATA_LEN		    (2)
+#define FFA_CALL_ARGS_CALLER_ID		        (3)
+#define FFA_CALL_ARGS_ENCODING		        (4)
 
 /* Resp arg offsets */
-#define FFA_CALL_ARGS_RESP_DATA_LEN		    (1)
-#define FFA_CALL_ARGS_RESP_RPC_STATUS	    (2)
-#define FFA_CALL_ARGS_RESP_OP_STATUS		(3)
+#define FFA_CALL_ARGS_RESP_DATA_LEN		    (2)
+#define FFA_CALL_ARGS_RESP_RPC_STATUS	    (3)
+#define FFA_CALL_ARGS_RESP_OP_STATUS		(4)
 
 /* Share/unshare offsets */
-#define FFA_CALL_ARGS_SHARE_MEM_HANDLE_LSW	(1)
-#define FFA_CALL_ARGS_SHARE_MEM_HANDLE_MSW	(2)
-#define FFA_CALL_ARGS_SHARE_MEM_SIZE		(3)
+#define FFA_CALL_ARGS_SHARE_MEM_HANDLE_LSW	(2)
+#define FFA_CALL_ARGS_SHARE_MEM_HANDLE_MSW	(3)
+#define FFA_CALL_ARGS_SHARE_MEM_SIZE		(4)
 
 #ifdef __cplusplus
 }
diff --git a/components/rpc/ffarpc/endpoint/ffarpc_call_ep.c b/components/rpc/ffarpc/endpoint/ffarpc_call_ep.c
index a502d82..9db53eb 100644
--- a/components/rpc/ffarpc/endpoint/ffarpc_call_ep.c
+++ b/components/rpc/ffarpc/endpoint/ffarpc_call_ep.c
@@ -4,7 +4,7 @@
  * SPDX-License-Identifier: BSD-3-Clause
  */
 
-#include "ffarpc_call_args.h"
+#include "components/rpc/ffarpc/caller/sp/ffarpc_sp_call_args.h"
 #include "ffarpc_call_ep.h"
 #include "ffarpc_call_ops.h"
 #include <ffa_api.h>
@@ -19,10 +19,10 @@
 static void set_resp_args(uint32_t *resp_args, uint32_t ifaceid_opcode, uint32_t data_len,
 			  rpc_status_t rpc_status, uint32_t opstatus)
 {
-	resp_args[FFA_CALL_ARGS_IFACE_ID_OPCODE] = ifaceid_opcode;
-	resp_args[FFA_CALL_ARGS_RESP_DATA_LEN] = data_len;
-	resp_args[FFA_CALL_ARGS_RESP_RPC_STATUS] = rpc_status;
-	resp_args[FFA_CALL_ARGS_RESP_OP_STATUS] = opstatus;
+	resp_args[SP_CALL_ARGS_IFACE_ID_OPCODE] = ifaceid_opcode;
+	resp_args[SP_CALL_ARGS_RESP_DATA_LEN] = data_len;
+	resp_args[SP_CALL_ARGS_RESP_RPC_STATUS] = rpc_status;
+	resp_args[SP_CALL_ARGS_RESP_OP_STATUS] = opstatus;
 }
 
 static void set_mgmt_resp_args(uint32_t *resp_args, uint32_t ifaceid_opcode,
@@ -91,8 +91,8 @@
 	desc.flags.transaction_type = sp_memory_transaction_type_share;
 	acc_desc.receiver_id = own_id;
 	acc_desc.data_access = sp_data_access_read_write;
-	handle = req_args[FFA_CALL_ARGS_SHARE_MEM_HANDLE_MSW];
-	handle = (handle << 32) | req_args[FFA_CALL_ARGS_SHARE_MEM_HANDLE_LSW];
+	handle = req_args[SP_CALL_ARGS_SHARE_MEM_HANDLE_MSW];
+	handle = (handle << 32) | req_args[SP_CALL_ARGS_SHARE_MEM_HANDLE_LSW];
 
 	sp_res = sp_memory_retrieve(&desc, &acc_desc, &region, in_region_count,
 				    &out_region_count, handle);
@@ -100,7 +100,7 @@
 	if (sp_res == SP_RESULT_OK) {
 		call_ep->shmem_buf[idx] = region.address;
 		call_ep->shmem_buf_handle[idx] = handle;
-		call_ep->shmem_buf_size[idx] = (size_t)req_args[FFA_CALL_ARGS_SHARE_MEM_SIZE];
+		call_ep->shmem_buf_size[idx] = (size_t)req_args[SP_CALL_ARGS_SHARE_MEM_SIZE];
 		call_ep->src_id[idx] = source_id;
 		rpc_status = TS_RPC_CALL_ACCEPTED;
 	} else {
@@ -108,7 +108,7 @@
 	}
 
 out:
-	set_mgmt_resp_args(resp_args, req_args[FFA_CALL_ARGS_IFACE_ID_OPCODE], rpc_status);
+	set_mgmt_resp_args(resp_args, req_args[SP_CALL_ARGS_IFACE_ID_OPCODE], rpc_status);
 }
 
 static void deinit_shmem_buf(struct ffa_call_ep *call_ep, uint16_t source_id,
@@ -144,7 +144,7 @@
 	}
 
 out:
-	set_mgmt_resp_args(resp_args, req_args[FFA_CALL_ARGS_IFACE_ID_OPCODE], rpc_status);
+	set_mgmt_resp_args(resp_args, req_args[SP_CALL_ARGS_IFACE_ID_OPCODE], rpc_status);
 }
 
 static void handle_service_msg(struct ffa_call_ep *call_ep, uint16_t source_id,
@@ -153,7 +153,7 @@
 	rpc_status_t rpc_status = TS_RPC_ERROR_INTERNAL;
 	struct call_req call_req;
 
-	uint32_t ifaceid_opcode = req_args[FFA_CALL_ARGS_IFACE_ID_OPCODE];
+	uint32_t ifaceid_opcode = req_args[SP_CALL_ARGS_IFACE_ID_OPCODE];
 	int idx = find_shm(call_ep, source_id);
 
 	if (idx < 0) {
@@ -164,10 +164,10 @@
 	call_req.caller_id = source_id;
 	call_req.interface_id = FFA_CALL_ARGS_EXTRACT_IFACE(ifaceid_opcode);
 	call_req.opcode = FFA_CALL_ARGS_EXTRACT_OPCODE(ifaceid_opcode);
-	call_req.encoding = req_args[FFA_CALL_ARGS_ENCODING];
+	call_req.encoding = req_args[SP_CALL_ARGS_ENCODING];
 
 	call_req.req_buf.data = call_ep->shmem_buf[idx];
-	call_req.req_buf.data_len = req_args[FFA_CALL_ARGS_REQ_DATA_LEN];
+	call_req.req_buf.data_len = req_args[SP_CALL_ARGS_REQ_DATA_LEN];
 	call_req.req_buf.size = call_ep->shmem_buf_size[idx];
 
 	call_req.resp_buf.data = call_ep->shmem_buf[idx];
@@ -187,7 +187,7 @@
 static void handle_mgmt_msg(struct ffa_call_ep *call_ep, uint16_t source_id,
 			    const uint32_t *req_args, uint32_t *resp_args)
 {
-	uint32_t ifaceid_opcode = req_args[FFA_CALL_ARGS_IFACE_ID_OPCODE];
+	uint32_t ifaceid_opcode = req_args[SP_CALL_ARGS_IFACE_ID_OPCODE];
 	uint32_t opcode = FFA_CALL_ARGS_EXTRACT_OPCODE(ifaceid_opcode);
 
 	switch (opcode) {
@@ -218,15 +218,15 @@
 }
 
 void ffa_call_ep_receive(struct ffa_call_ep *call_ep,
-			 const struct ffa_direct_msg *req_msg,
-			 struct ffa_direct_msg *resp_msg)
+			 const struct sp_msg *req_msg,
+			 struct sp_msg *resp_msg)
 {
 	const uint32_t *req_args = req_msg->args;
 	uint32_t *resp_args = resp_msg->args;
 	int idx;
 
 	uint16_t source_id = req_msg->source_id;
-	uint32_t ifaceid_opcode = req_args[FFA_CALL_ARGS_IFACE_ID_OPCODE];
+	uint32_t ifaceid_opcode = req_args[SP_CALL_ARGS_IFACE_ID_OPCODE];
 
 	if (FFA_CALL_ARGS_EXTRACT_IFACE(ifaceid_opcode) == FFA_CALL_MGMT_IFACE_ID) {
 		/* It's an RPC layer management request */
diff --git a/components/rpc/ffarpc/endpoint/ffarpc_call_ep.h b/components/rpc/ffarpc/endpoint/ffarpc_call_ep.h
index 02b97b5..df4417d 100644
--- a/components/rpc/ffarpc/endpoint/ffarpc_call_ep.h
+++ b/components/rpc/ffarpc/endpoint/ffarpc_call_ep.h
@@ -8,6 +8,7 @@
 #define FFA_CALL_EP_H
 
 #include <ffa_api.h>
+#include "sp_messaging.h"
 #include <components/rpc/common/endpoint/rpc_interface.h>
 #include <stddef.h>
 #include <stdint.h>
@@ -30,8 +31,8 @@
 
 void ffa_call_ep_init(struct ffa_call_ep *ffa_call_ep, struct rpc_interface *iface);
 void ffa_call_ep_receive(struct ffa_call_ep *call_ep,
-			 const struct ffa_direct_msg *req_msg,
-			 struct ffa_direct_msg *resp_msg);
+			 const struct sp_msg *req_msg,
+			 struct sp_msg *resp_msg);
 
 #ifdef __cplusplus
 }
diff --git a/deployments/crypto/opteesp/crypto_sp.c b/deployments/crypto/opteesp/crypto_sp.c
index b9c1fb2..f3679d1 100644
--- a/deployments/crypto/opteesp/crypto_sp.c
+++ b/deployments/crypto/opteesp/crypto_sp.c
@@ -14,6 +14,7 @@
 #include <config/loader/sp/sp_config_loader.h>
 #include <ffa_api.h>
 #include <sp_api.h>
+#include <sp_messaging.h>
 #include <sp_rxtx.h>
 #include <trace.h>
 
@@ -28,7 +29,8 @@
 	struct mbed_crypto_provider crypto_provider;
 	struct ffa_call_ep ffarpc_call_ep;
 	struct rpc_interface *crypto_iface;
-	struct ffa_direct_msg req_msg;
+	struct sp_msg req_msg = { 0 };
+	struct sp_msg resp_msg = { 0 };
 	struct storage_backend *storage_backend;
 
 	/* Boot phase */
@@ -53,20 +55,15 @@
 	ffa_call_ep_init(&ffarpc_call_ep, crypto_iface);
 
 	/* End of boot phase */
-	ffa_msg_wait(&req_msg);
+	sp_msg_wait(&req_msg);
 
 	while (1) {
-		if (req_msg.function_id == FFA_MSG_SEND_DIRECT_REQ_32) {
+		ffa_call_ep_receive(&ffarpc_call_ep, &req_msg, &resp_msg);
 
-			struct ffa_direct_msg resp_msg;
+		resp_msg.source_id = req_msg.destination_id;
+		resp_msg.destination_id = req_msg.source_id;
 
-			ffa_call_ep_receive(&ffarpc_call_ep, &req_msg, &resp_msg);
-
-			ffa_msg_send_direct_resp(req_msg.destination_id,
-					req_msg.source_id, resp_msg.args[0], resp_msg.args[1],
-					resp_msg.args[2], resp_msg.args[3], resp_msg.args[4],
-					&req_msg);
-		}
+		sp_msg_send_direct_resp(&resp_msg, &req_msg);
 	}
 
 fatal_error:
diff --git a/deployments/internal-trusted-storage/opteesp/sp.c b/deployments/internal-trusted-storage/opteesp/sp.c
index 626c2d4..a469905 100644
--- a/deployments/internal-trusted-storage/opteesp/sp.c
+++ b/deployments/internal-trusted-storage/opteesp/sp.c
@@ -11,6 +11,7 @@
 #include <components/service/secure_storage/factory/storage_factory.h>
 #include <components/service/secure_storage/frontend/secure_storage_provider/secure_storage_provider.h>
 #include <sp_api.h>
+#include <sp_messaging.h>
 #include <sp_rxtx.h>
 #include <trace.h>
 
@@ -24,8 +25,8 @@
 	sp_result sp_res;
 	struct rpc_interface *secure_storage_iface;
 	struct ffa_call_ep ffa_call_ep;
-	struct ffa_direct_msg req_msg;
-	struct ffa_direct_msg resp_msg;
+	struct sp_msg req_msg = { 0 };
+	struct sp_msg resp_msg = { 0 };
 	struct secure_storage_provider secure_storage_provider;
 	struct storage_backend *storage_backend;
 
@@ -47,17 +48,15 @@
 	ffa_call_ep_init(&ffa_call_ep, secure_storage_iface);
 
 	/* End of boot phase */
-	ffa_msg_wait(&req_msg);
+	sp_msg_wait(&req_msg);
 
 	while (1) {
-		if (req_msg.function_id == FFA_MSG_SEND_DIRECT_REQ_32) {
-			ffa_call_ep_receive(&ffa_call_ep, &req_msg, &resp_msg);
+		ffa_call_ep_receive(&ffa_call_ep, &req_msg, &resp_msg);
 
-			ffa_msg_send_direct_resp(req_msg.destination_id,
-					req_msg.source_id, resp_msg.args[0], resp_msg.args[1],
-					resp_msg.args[2], resp_msg.args[3], resp_msg.args[4],
-					&req_msg);
-		}
+		resp_msg.source_id = req_msg.destination_id;
+		resp_msg.destination_id = req_msg.source_id;
+
+		sp_msg_send_direct_resp(&resp_msg, &req_msg);
 	}
 }
 
diff --git a/deployments/protected-storage/opteesp/sp.c b/deployments/protected-storage/opteesp/sp.c
index 3bf3f1d..af75d89 100644
--- a/deployments/protected-storage/opteesp/sp.c
+++ b/deployments/protected-storage/opteesp/sp.c
@@ -11,6 +11,7 @@
 #include <components/service/secure_storage/factory/storage_factory.h>
 #include <components/service/secure_storage/frontend/secure_storage_provider/secure_storage_provider.h>
 #include <sp_api.h>
+#include <sp_messaging.h>
 #include <sp_rxtx.h>
 #include <trace.h>
 
@@ -24,8 +25,8 @@
 	sp_result sp_res;
 	struct rpc_interface *secure_storage_iface;
 	struct ffa_call_ep ffa_call_ep;
-	struct ffa_direct_msg req_msg;
-	struct ffa_direct_msg resp_msg;
+	struct sp_msg req_msg = { 0 };
+	struct sp_msg resp_msg = { 0 };
 	struct secure_storage_provider secure_storage_provider;
 	struct storage_backend *storage_backend;
 
@@ -47,17 +48,15 @@
 	ffa_call_ep_init(&ffa_call_ep, secure_storage_iface);
 
 	/* End of boot phase */
-	ffa_msg_wait(&req_msg);
+	sp_msg_wait(&req_msg);
 
 	while (1) {
-		if (req_msg.function_id == FFA_MSG_SEND_DIRECT_REQ_32) {
-			ffa_call_ep_receive(&ffa_call_ep, &req_msg, &resp_msg);
+		ffa_call_ep_receive(&ffa_call_ep, &req_msg, &resp_msg);
 
-			ffa_msg_send_direct_resp(req_msg.destination_id,
-					req_msg.source_id, resp_msg.args[0], resp_msg.args[1],
-					resp_msg.args[2], resp_msg.args[3], resp_msg.args[4],
-					&req_msg);
-		}
+		resp_msg.source_id = req_msg.destination_id;
+		resp_msg.destination_id = req_msg.source_id;
+
+		sp_msg_send_direct_resp(&resp_msg, &req_msg);
 	}
 }
 
diff --git a/deployments/sfs-demo/opteesp/sp.c b/deployments/sfs-demo/opteesp/sp.c
index 1d8fe87..1f049ae 100644
--- a/deployments/sfs-demo/opteesp/sp.c
+++ b/deployments/sfs-demo/opteesp/sp.c
@@ -11,6 +11,7 @@
 #include <service/secure_storage/backend/secure_storage_client/secure_storage_client.h>
 #include <psa/internal_trusted_storage.h>
 #include <sp_api.h>
+#include "sp_messaging.h"
 #include <sp_rxtx.h>
 #include <trace.h>
 #include <string.h>
@@ -131,7 +132,7 @@
 
 	ffa_result ffa_res;
 	sp_result sp_res;
-	struct ffa_direct_msg req_msg;
+	struct sp_msg req_msg = { 0 };
 	struct rpc_caller *caller;
 	struct ffarpc_caller ffa_caller;
 	struct secure_storage_client secure_storage_client;
@@ -181,7 +182,7 @@
 	}
 
 	/* End of boot phase */
-	ffa_msg_wait(&req_msg);
+	sp_msg_wait(&req_msg);
 
 err:
 	EMSG("Test SP error");