treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / firmware / broadcom / tee_bnxt_fw.c
blobed10da5313e8652b3ff72bc16283a9315eb9370e
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright 2019 Broadcom.
4 */
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/sizes.h>
9 #include <linux/slab.h>
10 #include <linux/tee_drv.h>
11 #include <linux/uuid.h>
13 #include <linux/firmware/broadcom/tee_bnxt_fw.h>
15 #define MAX_SHM_MEM_SZ SZ_4M
17 #define MAX_TEE_PARAM_ARRY_MEMB 4
19 enum ta_cmd {
21 * TA_CMD_BNXT_FASTBOOT - boot bnxt device by copying f/w into sram
23 * param[0] unused
24 * param[1] unused
25 * param[2] unused
26 * param[3] unused
28 * Result:
29 * TEE_SUCCESS - Invoke command success
30 * TEE_ERROR_ITEM_NOT_FOUND - Corrupt f/w image found on memory
32 TA_CMD_BNXT_FASTBOOT = 0,
35 * TA_CMD_BNXT_COPY_COREDUMP - copy the core dump into shm
37 * param[0] (inout memref) - Coredump buffer memory reference
38 * param[1] (in value) - value.a: offset, data to be copied from
39 * value.b: size of data to be copied
40 * param[2] unused
41 * param[3] unused
43 * Result:
44 * TEE_SUCCESS - Invoke command success
45 * TEE_ERROR_BAD_PARAMETERS - Incorrect input param
46 * TEE_ERROR_ITEM_NOT_FOUND - Corrupt core dump
48 TA_CMD_BNXT_COPY_COREDUMP = 3,
51 /**
52 * struct tee_bnxt_fw_private - OP-TEE bnxt private data
53 * @dev: OP-TEE based bnxt device.
54 * @ctx: OP-TEE context handler.
55 * @session_id: TA session identifier.
57 struct tee_bnxt_fw_private {
58 struct device *dev;
59 struct tee_context *ctx;
60 u32 session_id;
61 struct tee_shm *fw_shm_pool;
64 static struct tee_bnxt_fw_private pvt_data;
66 static void prepare_args(int cmd,
67 struct tee_ioctl_invoke_arg *arg,
68 struct tee_param *param)
70 memset(arg, 0, sizeof(*arg));
71 memset(param, 0, MAX_TEE_PARAM_ARRY_MEMB * sizeof(*param));
73 arg->func = cmd;
74 arg->session = pvt_data.session_id;
75 arg->num_params = MAX_TEE_PARAM_ARRY_MEMB;
77 /* Fill invoke cmd params */
78 switch (cmd) {
79 case TA_CMD_BNXT_COPY_COREDUMP:
80 param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT;
81 param[0].u.memref.shm = pvt_data.fw_shm_pool;
82 param[0].u.memref.size = MAX_SHM_MEM_SZ;
83 param[0].u.memref.shm_offs = 0;
84 param[1].attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT;
85 break;
86 case TA_CMD_BNXT_FASTBOOT:
87 default:
88 /* Nothing to do */
89 break;
93 /**
94 * tee_bnxt_fw_load() - Load the bnxt firmware
95 * Uses an OP-TEE call to start a secure
96 * boot process.
97 * Returns 0 on success, negative errno otherwise.
99 int tee_bnxt_fw_load(void)
101 int ret = 0;
102 struct tee_ioctl_invoke_arg arg;
103 struct tee_param param[MAX_TEE_PARAM_ARRY_MEMB];
105 if (!pvt_data.ctx)
106 return -ENODEV;
108 prepare_args(TA_CMD_BNXT_FASTBOOT, &arg, param);
110 ret = tee_client_invoke_func(pvt_data.ctx, &arg, param);
111 if (ret < 0 || arg.ret != 0) {
112 dev_err(pvt_data.dev,
113 "TA_CMD_BNXT_FASTBOOT invoke failed TEE err: %x, ret:%x\n",
114 arg.ret, ret);
115 return -EINVAL;
118 return 0;
120 EXPORT_SYMBOL(tee_bnxt_fw_load);
123 * tee_bnxt_copy_coredump() - Copy coredump from the allocated memory
124 * Uses an OP-TEE call to copy coredump
125 * @buf: destination buffer where core dump is copied into
126 * @offset: offset from the base address of core dump area
127 * @size: size of the dump
129 * Returns 0 on success, negative errno otherwise.
131 int tee_bnxt_copy_coredump(void *buf, u32 offset, u32 size)
133 struct tee_ioctl_invoke_arg arg;
134 struct tee_param param[MAX_TEE_PARAM_ARRY_MEMB];
135 void *core_data;
136 u32 rbytes = size;
137 u32 nbytes = 0;
138 int ret = 0;
140 if (!pvt_data.ctx)
141 return -ENODEV;
143 prepare_args(TA_CMD_BNXT_COPY_COREDUMP, &arg, param);
145 while (rbytes) {
146 nbytes = rbytes;
148 nbytes = min_t(u32, rbytes, param[0].u.memref.size);
150 /* Fill additional invoke cmd params */
151 param[1].u.value.a = offset;
152 param[1].u.value.b = nbytes;
154 ret = tee_client_invoke_func(pvt_data.ctx, &arg, param);
155 if (ret < 0 || arg.ret != 0) {
156 dev_err(pvt_data.dev,
157 "TA_CMD_BNXT_COPY_COREDUMP invoke failed TEE err: %x, ret:%x\n",
158 arg.ret, ret);
159 return -EINVAL;
162 core_data = tee_shm_get_va(pvt_data.fw_shm_pool, 0);
163 if (IS_ERR(core_data)) {
164 dev_err(pvt_data.dev, "tee_shm_get_va failed\n");
165 return PTR_ERR(core_data);
168 memcpy(buf, core_data, nbytes);
170 rbytes -= nbytes;
171 buf += nbytes;
172 offset += nbytes;
175 return 0;
177 EXPORT_SYMBOL(tee_bnxt_copy_coredump);
179 static int optee_ctx_match(struct tee_ioctl_version_data *ver, const void *data)
181 return (ver->impl_id == TEE_IMPL_ID_OPTEE);
184 static int tee_bnxt_fw_probe(struct device *dev)
186 struct tee_client_device *bnxt_device = to_tee_client_device(dev);
187 int ret, err = -ENODEV;
188 struct tee_ioctl_open_session_arg sess_arg;
189 struct tee_shm *fw_shm_pool;
191 memset(&sess_arg, 0, sizeof(sess_arg));
193 /* Open context with TEE driver */
194 pvt_data.ctx = tee_client_open_context(NULL, optee_ctx_match, NULL,
195 NULL);
196 if (IS_ERR(pvt_data.ctx))
197 return -ENODEV;
199 /* Open session with Bnxt load Trusted App */
200 memcpy(sess_arg.uuid, bnxt_device->id.uuid.b, TEE_IOCTL_UUID_LEN);
201 sess_arg.clnt_login = TEE_IOCTL_LOGIN_PUBLIC;
202 sess_arg.num_params = 0;
204 ret = tee_client_open_session(pvt_data.ctx, &sess_arg, NULL);
205 if (ret < 0 || sess_arg.ret != 0) {
206 dev_err(dev, "tee_client_open_session failed, err: %x\n",
207 sess_arg.ret);
208 err = -EINVAL;
209 goto out_ctx;
211 pvt_data.session_id = sess_arg.session;
213 pvt_data.dev = dev;
215 fw_shm_pool = tee_shm_alloc(pvt_data.ctx, MAX_SHM_MEM_SZ,
216 TEE_SHM_MAPPED | TEE_SHM_DMA_BUF);
217 if (IS_ERR(fw_shm_pool)) {
218 dev_err(pvt_data.dev, "tee_shm_alloc failed\n");
219 err = PTR_ERR(fw_shm_pool);
220 goto out_sess;
223 pvt_data.fw_shm_pool = fw_shm_pool;
225 return 0;
227 out_sess:
228 tee_client_close_session(pvt_data.ctx, pvt_data.session_id);
229 out_ctx:
230 tee_client_close_context(pvt_data.ctx);
232 return err;
235 static int tee_bnxt_fw_remove(struct device *dev)
237 tee_shm_free(pvt_data.fw_shm_pool);
238 tee_client_close_session(pvt_data.ctx, pvt_data.session_id);
239 tee_client_close_context(pvt_data.ctx);
240 pvt_data.ctx = NULL;
242 return 0;
245 static const struct tee_client_device_id tee_bnxt_fw_id_table[] = {
246 {UUID_INIT(0x6272636D, 0x2019, 0x0716,
247 0x42, 0x43, 0x4D, 0x5F, 0x53, 0x43, 0x48, 0x49)},
251 MODULE_DEVICE_TABLE(tee, tee_bnxt_fw_id_table);
253 static struct tee_client_driver tee_bnxt_fw_driver = {
254 .id_table = tee_bnxt_fw_id_table,
255 .driver = {
256 .name = KBUILD_MODNAME,
257 .bus = &tee_bus_type,
258 .probe = tee_bnxt_fw_probe,
259 .remove = tee_bnxt_fw_remove,
263 static int __init tee_bnxt_fw_mod_init(void)
265 return driver_register(&tee_bnxt_fw_driver.driver);
268 static void __exit tee_bnxt_fw_mod_exit(void)
270 driver_unregister(&tee_bnxt_fw_driver.driver);
273 module_init(tee_bnxt_fw_mod_init);
274 module_exit(tee_bnxt_fw_mod_exit);
276 MODULE_AUTHOR("Vikas Gupta <vikas.gupta@broadcom.com>");
277 MODULE_DESCRIPTION("Broadcom bnxt firmware manager");
278 MODULE_LICENSE("GPL v2");