Align with Linux FF-A driver

The user space driver for FF-A has been updated. Align the necessary
parts and replace the driver header in external.

Signed-off-by: Balint Dobszay <balint.dobszay@arm.com>
Change-Id: I8bcf601d54e93584f6a9d624bd74039dc12eda18
diff --git a/components/rpc/ffarpc/caller/linux/component.cmake b/components/rpc/ffarpc/caller/linux/component.cmake
index 3bab840..ec5387c 100644
--- a/components/rpc/ffarpc/caller/linux/component.cmake
+++ b/components/rpc/ffarpc/caller/linux/component.cmake
@@ -1,14 +1,19 @@
 #-------------------------------------------------------------------------------
-# Copyright (c) 2020, Arm Limited and Contributors. All rights reserved.
+# Copyright (c) 2020-2021, Arm Limited and Contributors. All rights reserved.
 #
 # SPDX-License-Identifier: BSD-3-Clause
 #
 #-------------------------------------------------------------------------------
-if (NOT DEFINED TGT)
+if(NOT DEFINED TGT)
 	message(FATAL_ERROR "mandatory parameter TGT is not defined.")
 endif()
 
+include(${TS_ROOT}/external/LinuxFFAUserShim/LinuxFFAUserShim.cmake)
+
 target_sources(${TGT} PRIVATE
 	"${CMAKE_CURRENT_LIST_DIR}/ffarpc_caller.c"
-	)
+)
 
+target_include_directories(${TGT} PRIVATE
+	"${LINUX_FFA_USER_SHIM_INCLUDE_DIR}"
+)
diff --git a/components/rpc/ffarpc/caller/linux/ffarpc_caller.c b/components/rpc/ffarpc/caller/linux/ffarpc_caller.c
index af86664..70d6ca3 100644
--- a/components/rpc/ffarpc/caller/linux/ffarpc_caller.c
+++ b/components/rpc/ffarpc/caller/linux/ffarpc_caller.c
@@ -1,11 +1,11 @@
 /*
- * Copyright (c) 2020, Arm Limited and Contributors. All rights reserved.
+ * Copyright (c) 2020-2021, Arm Limited and Contributors. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  */
 
 #include "ffarpc_caller.h"
-#include <external/ffa_tool/buf_common.h>
+#include <arm_ffa_user.h>
 #include <rpc/ffarpc/endpoint/ffarpc_call_args.h>
 #include <rpc/ffarpc/endpoint/ffarpc_call_ops.h>
 #include <protocols/rpc/common/packed-c/status.h>
@@ -65,7 +65,7 @@
 	ffarpc_caller_close(s);
 }
 
-size_t ffarpc_caller_discover(const struct ffarpc_caller *s, const uint8_t *uuid,
+size_t ffarpc_caller_discover(const struct ffarpc_caller *s, const struct uuid_canonical *uuid,
 						uint16_t *partition_ids, size_t discover_limit)
 {
 	size_t discover_count = 0;
@@ -77,15 +77,12 @@
 
 		if (fd >= 0) {
 			int ioctl_status;
-			struct endpoint_id discovered_partition;
+			struct ffa_ioctl_ep_desc discovered_partition;
 
-			discovered_partition.uuid_0 = uuid[3]  << 24 | uuid[2]  << 16 | uuid[1]  << 8 | uuid[0];
-			discovered_partition.uuid_1 = uuid[7]  << 24 | uuid[6]  << 16 | uuid[5]  << 8 | uuid[4];
-			discovered_partition.uuid_2 = uuid[11] << 24 | uuid[10] << 16 | uuid[9]  << 8 | uuid[8];
-			discovered_partition.uuid_3 = uuid[15] << 24 | uuid[14] << 16 | uuid[13] << 8 | uuid[12];
+			discovered_partition.uuid_ptr = (uintptr_t)&uuid->characters;
 			discovered_partition.id = 0;
 
-			ioctl_status = ioctl(fd, GET_PART_ID, &discovered_partition);
+			ioctl_status = ioctl(fd, FFA_IOC_GET_PART_ID, &discovered_partition);
 
 			if ((ioctl_status == 0) && (discover_count < discover_limit)) {
 				partition_ids[discover_count] = discovered_partition.id;
@@ -109,7 +106,7 @@
 
 		if (s->fd >= 0) {
 			/* Allocate resource for session */
-			ioctl_status = ioctl(s->fd, SHARE_INIT, &s->shared_mem_handle);
+			ioctl_status = ioctl(s->fd, FFA_IOC_SHM_INIT, &s->shared_mem_handle);
 
 			if (ioctl_status == 0) {
 				/* Session successfully opened */
@@ -135,7 +132,7 @@
     if (s->fd >= 0) {
 
         unshare_mem_with_partition(s);
-        ioctl_status = ioctl(s->fd, SHARE_DEINIT);
+        ioctl_status = ioctl(s->fd, FFA_IOC_SHM_DEINIT);
         close(s->fd);
         s->fd = -1;
         s->call_ep_id = 0;
@@ -195,14 +192,14 @@
 
         if (kernel_op_status == 0) {
             /* Make direct call to send the request */
-            struct msg_args direct_msg;
+            struct ffa_ioctl_msg_args direct_msg;
             memset(&direct_msg, 0, sizeof(direct_msg));
 
 		    direct_msg.dst_id = s->call_ep_id;
 		    direct_msg.args[FFA_CALL_ARGS_OPCODE] = (uint64_t)opcode;
 		    direct_msg.args[FFA_CALL_ARGS_REQ_DATA_LEN] = (uint64_t)s->req_len;
 
-            kernel_op_status = ioctl(s->fd, SEND_SYNC_MSG, &direct_msg);
+            kernel_op_status = ioctl(s->fd, FFA_IOC_MSG_SEND, &direct_msg);
 
             if (kernel_op_status == 0) {
                 /* Send completed normally - ffa return args in msg_args struct */
@@ -263,11 +260,11 @@
 static int kernel_write_req_buf(struct ffarpc_caller *s) {
 
     int ioctl_status;
-    struct buf_descr req_descr;
+    struct ffa_ioctl_buf_desc req_descr;
 
-    req_descr.buf = s->req_buf;
-    req_descr.length = s->req_len;
-    ioctl_status = ioctl(s->fd, SHARE_WRITE, &req_descr);
+    req_descr.buf_ptr = (uintptr_t)s->req_buf;
+    req_descr.buf_len = s->req_len;
+    ioctl_status = ioctl(s->fd, FFA_IOC_SHM_WRITE, &req_descr);
 
     return ioctl_status;
 }
@@ -276,11 +273,11 @@
 static int kernel_read_resp_buf(struct ffarpc_caller *s) {
 
     int ioctl_status;
-    struct buf_descr resp_descr;
+    struct ffa_ioctl_buf_desc resp_descr;
 
-    resp_descr.buf = s->resp_buf;
-    resp_descr.length = s->resp_len;
-    ioctl_status = ioctl(s->fd, SHARE_READ, &resp_descr);
+    resp_descr.buf_ptr = (uintptr_t)s->resp_buf;
+    resp_descr.buf_len = s->resp_len;
+    ioctl_status = ioctl(s->fd, FFA_IOC_SHM_READ, &resp_descr);
 
     return ioctl_status;
 }
@@ -288,7 +285,7 @@
 static int share_mem_with_partition(struct ffarpc_caller *s) {
 
     int ioctl_status;
-    struct msg_args direct_msg;
+    struct ffa_ioctl_msg_args direct_msg;
     memset(&direct_msg, 0, sizeof(direct_msg));
 
     direct_msg.dst_id = s->call_ep_id;
@@ -297,7 +294,7 @@
     direct_msg.args[FFA_CALL_ARGS_SHARE_MEM_HANDLE_MSW] = (uint32_t)(s->shared_mem_handle >> 32);
     direct_msg.args[FFA_CALL_ARGS_SHARE_MEM_SIZE] = (uint64_t)s->shared_mem_required_size;
 
-    ioctl_status = ioctl(s->fd, SEND_SYNC_MSG, &direct_msg);
+    ioctl_status = ioctl(s->fd, FFA_IOC_MSG_SEND, &direct_msg);
 
     return ioctl_status;
 }
@@ -305,7 +302,7 @@
 static int unshare_mem_with_partition(struct ffarpc_caller *s) {
 
     int ioctl_status;
-    struct msg_args direct_msg;
+    struct ffa_ioctl_msg_args direct_msg;
     memset(&direct_msg, 0, sizeof(direct_msg));
 
     direct_msg.dst_id = s->call_ep_id;
@@ -313,7 +310,7 @@
     direct_msg.args[FFA_CALL_ARGS_SHARE_MEM_HANDLE_LSW] = (uint32_t)s->shared_mem_handle;
     direct_msg.args[FFA_CALL_ARGS_SHARE_MEM_HANDLE_MSW] = (uint32_t)(s->shared_mem_handle >> 32);
 
-    ioctl_status = ioctl(s->fd, SEND_SYNC_MSG, &direct_msg);
+    ioctl_status = ioctl(s->fd, FFA_IOC_MSG_SEND, &direct_msg);
 
     return ioctl_status;
 }
diff --git a/components/rpc/ffarpc/caller/linux/ffarpc_caller.h b/components/rpc/ffarpc/caller/linux/ffarpc_caller.h
index 69410ab..7e846ba 100644
--- a/components/rpc/ffarpc/caller/linux/ffarpc_caller.h
+++ b/components/rpc/ffarpc/caller/linux/ffarpc_caller.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2020, Arm Limited and Contributors. All rights reserved.
+ * Copyright (c) 2020-2021, Arm Limited and Contributors. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  */
@@ -7,6 +7,7 @@
 #ifndef FFARPC_CALLER_H
 #define FFARPC_CALLER_H
 
+#include <common/uuid/uuid.h>
 #include <rpc_caller.h>
 #include <stdbool.h>
 
@@ -35,7 +36,7 @@
 
 struct rpc_caller *ffarpc_caller_init(struct ffarpc_caller *s, const char *device_path);
 void ffarpc_caller_deinit(struct ffarpc_caller *s);
-size_t ffarpc_caller_discover(const struct ffarpc_caller *s, const uint8_t *uuid,
+size_t ffarpc_caller_discover(const struct ffarpc_caller *s, const struct uuid_canonical *uuid,
 						uint16_t *partition_ids, size_t discover_limit);
 int ffarpc_caller_open(struct ffarpc_caller *s, uint16_t call_ep_id);
 int ffarpc_caller_close(struct ffarpc_caller *s);
diff --git a/components/service/locator/linux/ffa/linuxffa_location_strategy.c b/components/service/locator/linux/ffa/linuxffa_location_strategy.c
index 8c1cb64..e965076 100644
--- a/components/service/locator/linux/ffa/linuxffa_location_strategy.c
+++ b/components/service/locator/linux/ffa/linuxffa_location_strategy.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2020, Arm Limited and Contributors. All rights reserved.
+ * Copyright (c) 2020-2021, Arm Limited and Contributors. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  */
@@ -59,7 +59,7 @@
     for (suggeston_index = 0; suggeston_index < num_suggestons; ++suggeston_index) {
 
         uint16_t partition_id;
-        const char *dev_path = "/sys/kernel/debug/buf";
+        const char *dev_path = "/sys/kernel/debug/arm_ffa_user";
 
         if (discover_partition(sn, &uuids[suggeston_index], dev_path, &partition_id)) {
 
@@ -134,9 +134,8 @@
                             const char *dev_path, uint16_t *partition_id)
 {
     bool discovered = false;
-    struct uuid_octets uuid_bytes;
 
-    if (uuid_parse_to_octets_reversed(uuid->characters, uuid_bytes.octets, UUID_OCTETS_LEN) == UUID_CANONICAL_FORM_LEN) {
+    if (uuid_is_valid(uuid->characters) == UUID_CANONICAL_FORM_LEN) {
 
         struct ffarpc_caller ffarpc_caller;
         unsigned int required_instance = sn_get_service_instance(sn);
@@ -146,7 +145,7 @@
         uint16_t discovered_partitions[MAX_PARTITION_INSTANCES];
         size_t discovered_count;
 
-        discovered_count = ffarpc_caller_discover(&ffarpc_caller, uuid_bytes.octets,
+        discovered_count = ffarpc_caller_discover(&ffarpc_caller, uuid,
                                         discovered_partitions, MAX_PARTITION_INSTANCES);
 
         if ((discovered_count > 0) && (required_instance < discovered_count)) {
diff --git a/deployments/crypto/opteesp/crypto_sp.c b/deployments/crypto/opteesp/crypto_sp.c
index 136c4a3..17e9cf4 100644
--- a/deployments/crypto/opteesp/crypto_sp.c
+++ b/deployments/crypto/opteesp/crypto_sp.c
@@ -1,6 +1,6 @@
 // SPDX-License-Identifier: BSD-3-Clause
 /*
- * Copyright (c) 2020, Arm Limited and Contributors. All rights reserved.
+ * Copyright (c) 2020-2021, Arm Limited and Contributors. All rights reserved.
  */
 
 #include <rpc/ffarpc/caller/sp/ffarpc_caller.h>
@@ -16,7 +16,7 @@
 
 
 #define SP_STORAGE_UUID_BYTES \
-	{ 0x48, 0xef, 0x1e, 0xdc, 0x7a, 0xb1, 0xcf, 0x4c, \
+	{ 0xdc, 0x1e, 0xef, 0x48, 0xb1, 0x7a, 0x4c, 0xcf, \
 	  0xac, 0x8b, 0xdf, 0xcf, 0xf7, 0x71, 0x1b, 0x14, }
 
 uint16_t own_id = 0; /* !!Needs refactoring as parameter to ffarpc_caller_init */
diff --git a/external/LinuxFFAUserShim/LinuxFFAUserShim.cmake b/external/LinuxFFAUserShim/LinuxFFAUserShim.cmake
new file mode 100644
index 0000000..565017b
--- /dev/null
+++ b/external/LinuxFFAUserShim/LinuxFFAUserShim.cmake
@@ -0,0 +1,54 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2020-2021, Arm Limited and Contributors. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#-------------------------------------------------------------------------------
+
+# Find Linux FF-A user space shim repo location.
+# It contains a kernel module which exposes FF-A operations to user space using DebugFS.
+
+# If a CMake variable exists, use it as is.
+# If not, try to copy the value from the environment.
+# If neither is present, try to download.
+if(NOT DEFINED LINUX_FFA_USER_SHIM_DIR)
+	if(DEFINED ENV{LINUX_FFA_USER_SHIM_DIR})
+		set(LINUX_FFA_USER_SHIM_DIR $ENV{LINUX_FFA_USER_SHIM_DIR}
+				CACHE STRING "Linux FF-A user space shim dir")
+	else()
+		set(LINUX_FFA_USER_SHIM_URL "https://git.gitlab.arm.com/linux-arm/linux-trusted-services.git"
+				CACHE STRING "Linux FF-A user space shim repository URL")
+		set(LINUX_FFA_USER_SHIM_REFSPEC "main"
+				CACHE STRING "Linux FF-A user space shim git refspec")
+
+		find_program(GIT_COMMAND "git")
+		if (NOT GIT_COMMAND)
+			message(FATAL_ERROR "Please install git")
+		endif()
+
+		include(FetchContent)
+		FetchContent_Declare(linux_ffa_user_shim
+			GIT_REPOSITORY ${LINUX_FFA_USER_SHIM_URL}
+			GIT_TAG ${LINUX_FFA_USER_SHIM_REFSPEC}
+			GIT_SHALLOW TRUE
+		)
+
+		# FetchContent_GetProperties exports <name>_SOURCE_DIR and <name>_BINARY_DIR variables
+		FetchContent_GetProperties(linux_ffa_user_shim)
+		if(NOT linux_ffa_user_shim_POPULATED)
+			message(STATUS "Fetching Linux FF-A user space shim")
+			FetchContent_Populate(linux_ffa_user_shim)
+		endif()
+
+		set(LINUX_FFA_USER_SHIM_DIR ${linux_ffa_user_shim_SOURCE_DIR}
+				CACHE STRING "Linux FF-A user space shim dir")
+	endif()
+endif()
+
+find_path(LINUX_FFA_USER_SHIM_INCLUDE_DIR
+	NAMES arm_ffa_user.h
+	PATHS ${LINUX_FFA_USER_SHIM_DIR}
+	NO_DEFAULT_PATH
+	REQUIRED
+	DOC "Linux FF-A user space shim include directory"
+)
diff --git a/external/ffa_tool/buf_common.h b/external/ffa_tool/buf_common.h
deleted file mode 100644
index 326bd93..0000000
--- a/external/ffa_tool/buf_common.h
+++ /dev/null
@@ -1,46 +0,0 @@
-/*
- * Copyright (c) 2020, Arm Limited and Contributors. All rights reserved.
- *
- * SPDX-License-Identifier: BSD-3-Clause
- */
-
-#ifndef __BUF_COMMON_H
-#define __BUF_COMMON_H
-
-#ifdef __KERNEL__
-#include <linux/types.h>
-#include <uapi/asm-generic/ioctl.h>
-#else
-#include <stdint.h>
-#include <sys/ioctl.h>
-#endif
-
-struct buf_descr {
-	uint8_t *buf;
-	size_t length;
-};
-
-struct msg_args {
-	uint16_t dst_id;
-	uint32_t args[5];
-};
-
-struct endpoint_id {
-	uint32_t uuid_0;
-	uint32_t uuid_1;
-	uint32_t uuid_2;
-	uint32_t uuid_3;
-	uint16_t id;
-};
-
-#define IOCTL_TYPE	0xf0
-
-#define GET_PART_ID	_IOWR(IOCTL_TYPE, 0x00, struct endpoint_id)
-#define SEND_SYNC_MSG	_IOWR(IOCTL_TYPE, 0x01, struct msg_args)
-
-#define SHARE_INIT	_IOR(IOCTL_TYPE, 0x10, unsigned long)
-#define SHARE_DEINIT	_IO(IOCTL_TYPE, 0x11)
-#define SHARE_READ	_IOW(IOCTL_TYPE, 0x12, struct buf_descr)
-#define SHARE_WRITE	_IOW(IOCTL_TYPE, 0x13, struct buf_descr)
-
-#endif /* __BUF_COMMON_H */