Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Copyright (C) 2018-2019 Linaro Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/hw_random.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/tee_drv.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/uuid.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #define DRIVER_NAME "optee-rng"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #define TEE_ERROR_HEALTH_TEST_FAIL	0x00000001
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  * TA_CMD_GET_ENTROPY - Get Entropy from RNG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * param[0] (inout memref) - Entropy buffer memory reference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  * param[1] unused
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  * param[2] unused
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  * param[3] unused
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  * Result:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  * TEE_SUCCESS - Invoke command success
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  * TEE_ERROR_BAD_PARAMETERS - Incorrect input param
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  * TEE_ERROR_NOT_SUPPORTED - Requested entropy size greater than size of pool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  * TEE_ERROR_HEALTH_TEST_FAIL - Continuous health testing failed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #define TA_CMD_GET_ENTROPY		0x0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  * TA_CMD_GET_RNG_INFO - Get RNG information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38)  * param[0] (out value) - value.a: RNG data-rate in bytes per second
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39)  *                        value.b: Quality/Entropy per 1024 bit of data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40)  * param[1] unused
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41)  * param[2] unused
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)  * param[3] unused
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44)  * Result:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45)  * TEE_SUCCESS - Invoke command success
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46)  * TEE_ERROR_BAD_PARAMETERS - Incorrect input param
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) #define TA_CMD_GET_RNG_INFO		0x1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) #define MAX_ENTROPY_REQ_SZ		(4 * 1024)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53)  * struct optee_rng_private - OP-TEE Random Number Generator private data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54)  * @dev:		OP-TEE based RNG device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55)  * @ctx:		OP-TEE context handler.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56)  * @session_id:		RNG TA session identifier.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57)  * @data_rate:		RNG data rate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58)  * @entropy_shm_pool:	Memory pool shared with RNG device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59)  * @optee_rng:		OP-TEE RNG driver structure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) struct optee_rng_private {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	struct tee_context *ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	u32 session_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	u32 data_rate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	struct tee_shm *entropy_shm_pool;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	struct hwrng optee_rng;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) #define to_optee_rng_private(r) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		container_of(r, struct optee_rng_private, optee_rng)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) static size_t get_optee_rng_data(struct optee_rng_private *pvt_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 				 void *buf, size_t req_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	u8 *rng_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	size_t rng_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	struct tee_ioctl_invoke_arg inv_arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	struct tee_param param[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	memset(&inv_arg, 0, sizeof(inv_arg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	memset(&param, 0, sizeof(param));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	/* Invoke TA_CMD_GET_ENTROPY function of Trusted App */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	inv_arg.func = TA_CMD_GET_ENTROPY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	inv_arg.session = pvt_data->session_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	inv_arg.num_params = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	/* Fill invoke cmd params */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	param[0].u.memref.shm = pvt_data->entropy_shm_pool;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	param[0].u.memref.size = req_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	param[0].u.memref.shm_offs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	ret = tee_client_invoke_func(pvt_data->ctx, &inv_arg, param);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	if ((ret < 0) || (inv_arg.ret != 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		dev_err(pvt_data->dev, "TA_CMD_GET_ENTROPY invoke err: %x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 			inv_arg.ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	rng_data = tee_shm_get_va(pvt_data->entropy_shm_pool, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	if (IS_ERR(rng_data)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		dev_err(pvt_data->dev, "tee_shm_get_va failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	rng_size = param[0].u.memref.size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	memcpy(buf, rng_data, rng_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	return rng_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) static int optee_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	struct optee_rng_private *pvt_data = to_optee_rng_private(rng);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	size_t read = 0, rng_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	int timeout = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	u8 *data = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	if (max > MAX_ENTROPY_REQ_SZ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		max = MAX_ENTROPY_REQ_SZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	while (read < max) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		rng_size = get_optee_rng_data(pvt_data, data, (max - read));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		data += rng_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		read += rng_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		if (wait && pvt_data->data_rate) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 			if ((timeout-- == 0) || (read == max))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 				return read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 			msleep((1000 * (max - read)) / pvt_data->data_rate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 			return read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	return read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) static int optee_rng_init(struct hwrng *rng)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	struct optee_rng_private *pvt_data = to_optee_rng_private(rng);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	struct tee_shm *entropy_shm_pool = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	entropy_shm_pool = tee_shm_alloc(pvt_data->ctx, MAX_ENTROPY_REQ_SZ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 					 TEE_SHM_MAPPED | TEE_SHM_DMA_BUF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	if (IS_ERR(entropy_shm_pool)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		dev_err(pvt_data->dev, "tee_shm_alloc failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		return PTR_ERR(entropy_shm_pool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	pvt_data->entropy_shm_pool = entropy_shm_pool;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) static void optee_rng_cleanup(struct hwrng *rng)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	struct optee_rng_private *pvt_data = to_optee_rng_private(rng);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	tee_shm_free(pvt_data->entropy_shm_pool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) static struct optee_rng_private pvt_data = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	.optee_rng = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		.name		= DRIVER_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		.init		= optee_rng_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		.cleanup	= optee_rng_cleanup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		.read		= optee_rng_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) static int get_optee_rng_info(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	struct tee_ioctl_invoke_arg inv_arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	struct tee_param param[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	memset(&inv_arg, 0, sizeof(inv_arg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	memset(&param, 0, sizeof(param));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	/* Invoke TA_CMD_GET_RNG_INFO function of Trusted App */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	inv_arg.func = TA_CMD_GET_RNG_INFO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	inv_arg.session = pvt_data.session_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	inv_arg.num_params = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	/* Fill invoke cmd params */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	ret = tee_client_invoke_func(pvt_data.ctx, &inv_arg, param);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	if ((ret < 0) || (inv_arg.ret != 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		dev_err(dev, "TA_CMD_GET_RNG_INFO invoke err: %x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 			inv_arg.ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	pvt_data.data_rate = param[0].u.value.a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	pvt_data.optee_rng.quality = param[0].u.value.b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) static int optee_ctx_match(struct tee_ioctl_version_data *ver, const void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	if (ver->impl_id == TEE_IMPL_ID_OPTEE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) static int optee_rng_probe(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	struct tee_client_device *rng_device = to_tee_client_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	int ret = 0, err = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	struct tee_ioctl_open_session_arg sess_arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	memset(&sess_arg, 0, sizeof(sess_arg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	/* Open context with TEE driver */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	pvt_data.ctx = tee_client_open_context(NULL, optee_ctx_match, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 					       NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	if (IS_ERR(pvt_data.ctx))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	/* Open session with hwrng Trusted App */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	export_uuid(sess_arg.uuid, &rng_device->id.uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	sess_arg.clnt_login = TEE_IOCTL_LOGIN_PUBLIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	sess_arg.num_params = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	ret = tee_client_open_session(pvt_data.ctx, &sess_arg, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	if ((ret < 0) || (sess_arg.ret != 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 		dev_err(dev, "tee_client_open_session failed, err: %x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 			sess_arg.ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		goto out_ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	pvt_data.session_id = sess_arg.session;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	err = get_optee_rng_info(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		goto out_sess;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	err = hwrng_register(&pvt_data.optee_rng);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		dev_err(dev, "hwrng registration failed (%d)\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 		goto out_sess;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	pvt_data.dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) out_sess:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	tee_client_close_session(pvt_data.ctx, pvt_data.session_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) out_ctx:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	tee_client_close_context(pvt_data.ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) static int optee_rng_remove(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	hwrng_unregister(&pvt_data.optee_rng);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	tee_client_close_session(pvt_data.ctx, pvt_data.session_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	tee_client_close_context(pvt_data.ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) static const struct tee_client_device_id optee_rng_id_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	{UUID_INIT(0xab7a617c, 0xb8e7, 0x4d8f,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 		   0x83, 0x01, 0xd0, 0x9b, 0x61, 0x03, 0x6b, 0x64)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	{}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) MODULE_DEVICE_TABLE(tee, optee_rng_id_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) static struct tee_client_driver optee_rng_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	.id_table	= optee_rng_id_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	.driver		= {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		.name		= DRIVER_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		.bus		= &tee_bus_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		.probe		= optee_rng_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		.remove		= optee_rng_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) static int __init optee_rng_mod_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	return driver_register(&optee_rng_driver.driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) static void __exit optee_rng_mod_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	driver_unregister(&optee_rng_driver.driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) module_init(optee_rng_mod_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) module_exit(optee_rng_mod_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) MODULE_LICENSE("GPL v2");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) MODULE_AUTHOR("Sumit Garg <sumit.garg@linaro.org>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) MODULE_DESCRIPTION("OP-TEE based random number generator driver");