1 /*
2  * Copyright (c) 2022, Arm Limited. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 #include <assert.h>
8 
9 #include "rss_comms_protocol_pointer_access.h"
10 
11 #define TYPE_OFFSET	(16U)
12 #define TYPE_MASK	(0xFFFFUL << TYPE_OFFSET)
13 #define IN_LEN_OFFSET	(8U)
14 #define IN_LEN_MASK	(0xFFUL << IN_LEN_OFFSET)
15 #define OUT_LEN_OFFSET	(0U)
16 #define OUT_LEN_MASK	(0xFFUL << OUT_LEN_OFFSET)
17 
18 #define PARAM_PACK(type, in_len, out_len)			  \
19 	(((((uint32_t)type) << TYPE_OFFSET) & TYPE_MASK)	| \
20 	 ((((uint32_t)in_len) << IN_LEN_OFFSET) & IN_LEN_MASK)	| \
21 	 ((((uint32_t)out_len) << OUT_LEN_OFFSET) & OUT_LEN_MASK))
22 
rss_protocol_pointer_access_serialize_msg(psa_handle_t handle,int16_t type,const psa_invec * in_vec,uint8_t in_len,const psa_outvec * out_vec,uint8_t out_len,struct rss_pointer_access_msg_t * msg,size_t * msg_len)23 psa_status_t rss_protocol_pointer_access_serialize_msg(psa_handle_t handle,
24 						       int16_t type,
25 						       const psa_invec *in_vec,
26 						       uint8_t in_len,
27 						       const psa_outvec *out_vec,
28 						       uint8_t out_len,
29 						       struct rss_pointer_access_msg_t *msg,
30 						       size_t *msg_len)
31 {
32 	unsigned int i;
33 
34 	assert(msg != NULL);
35 	assert(msg_len != NULL);
36 	assert(in_vec != NULL);
37 
38 	msg->ctrl_param = PARAM_PACK(type, in_len, out_len);
39 	msg->handle = handle;
40 
41 	/* Fill msg iovec lengths */
42 	for (i = 0U; i < in_len; ++i) {
43 		msg->io_sizes[i] = in_vec[i].len;
44 		msg->host_ptrs[i] = (uint64_t)in_vec[i].base;
45 	}
46 	for (i = 0U; i < out_len; ++i) {
47 		msg->io_sizes[in_len + i] = out_vec[i].len;
48 		msg->host_ptrs[in_len + i] = (uint64_t)out_vec[i].base;
49 	}
50 
51 	*msg_len = sizeof(*msg);
52 
53 	return PSA_SUCCESS;
54 }
55 
rss_protocol_pointer_access_deserialize_reply(psa_outvec * out_vec,uint8_t out_len,psa_status_t * return_val,const struct rss_pointer_access_reply_t * reply,size_t reply_size)56 psa_status_t rss_protocol_pointer_access_deserialize_reply(psa_outvec *out_vec,
57 							   uint8_t out_len,
58 							   psa_status_t *return_val,
59 							   const struct rss_pointer_access_reply_t *reply,
60 							   size_t reply_size)
61 {
62 	unsigned int i;
63 
64 	assert(reply != NULL);
65 	assert(return_val != NULL);
66 
67 	for (i = 0U; i < out_len; ++i) {
68 		out_vec[i].len = reply->out_sizes[i];
69 	}
70 
71 	*return_val = reply->return_val;
72 
73 	return PSA_SUCCESS;
74 }
75