Skip to content

Commit e57acaf

Browse files
ndreysherbertx
authored andcommitted
crypto: caam - use devres to de-initialize the RNG
Use devres to de-initialize the RNG and drop explicit de-initialization code in caam_remove(). Signed-off-by: Andrey Smirnov <[email protected]> Cc: Chris Healy <[email protected]> Cc: Lucas Stach <[email protected]> Cc: Horia Geantă <[email protected]> Cc: Herbert Xu <[email protected]> Cc: Iuliana Prodan <[email protected]> Cc: [email protected] Cc: [email protected] Reviewed-by: Horia Geantă <[email protected]> Signed-off-by: Herbert Xu <[email protected]>
1 parent eceb5da commit e57acaf

File tree

1 file changed

+70
-60
lines changed

1 file changed

+70
-60
lines changed

drivers/crypto/caam/ctrl.c

Lines changed: 70 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,73 @@ static inline int run_descriptor_deco0(struct device *ctrldev, u32 *desc,
175175
return 0;
176176
}
177177

178+
/*
179+
* deinstantiate_rng - builds and executes a descriptor on DECO0,
180+
* which deinitializes the RNG block.
181+
* @ctrldev - pointer to device
182+
* @state_handle_mask - bitmask containing the instantiation status
183+
* for the RNG4 state handles which exist in
184+
* the RNG4 block: 1 if it's been instantiated
185+
*
186+
* Return: - 0 if no error occurred
187+
* - -ENOMEM if there isn't enough memory to allocate the descriptor
188+
* - -ENODEV if DECO0 couldn't be acquired
189+
* - -EAGAIN if an error occurred when executing the descriptor
190+
*/
191+
static int deinstantiate_rng(struct device *ctrldev, int state_handle_mask)
192+
{
193+
u32 *desc, status;
194+
int sh_idx, ret = 0;
195+
196+
desc = kmalloc(CAAM_CMD_SZ * 3, GFP_KERNEL);
197+
if (!desc)
198+
return -ENOMEM;
199+
200+
for (sh_idx = 0; sh_idx < RNG4_MAX_HANDLES; sh_idx++) {
201+
/*
202+
* If the corresponding bit is set, then it means the state
203+
* handle was initialized by us, and thus it needs to be
204+
* deinitialized as well
205+
*/
206+
if ((1 << sh_idx) & state_handle_mask) {
207+
/*
208+
* Create the descriptor for deinstantating this state
209+
* handle
210+
*/
211+
build_deinstantiation_desc(desc, sh_idx);
212+
213+
/* Try to run it through DECO0 */
214+
ret = run_descriptor_deco0(ctrldev, desc, &status);
215+
216+
if (ret ||
217+
(status && status != JRSTA_SSRC_JUMP_HALT_CC)) {
218+
dev_err(ctrldev,
219+
"Failed to deinstantiate RNG4 SH%d\n",
220+
sh_idx);
221+
break;
222+
}
223+
dev_info(ctrldev, "Deinstantiated RNG4 SH%d\n", sh_idx);
224+
}
225+
}
226+
227+
kfree(desc);
228+
229+
return ret;
230+
}
231+
232+
static void devm_deinstantiate_rng(void *data)
233+
{
234+
struct device *ctrldev = data;
235+
struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctrldev);
236+
237+
/*
238+
* De-initialize RNG state handles initialized by this driver.
239+
* In case of SoCs with Management Complex, RNG is managed by MC f/w.
240+
*/
241+
if (ctrlpriv->rng4_sh_init)
242+
deinstantiate_rng(ctrldev, ctrlpriv->rng4_sh_init);
243+
}
244+
178245
/*
179246
* instantiate_rng - builds and executes a descriptor on DECO0,
180247
* which initializes the RNG block.
@@ -247,59 +314,9 @@ static int instantiate_rng(struct device *ctrldev, int state_handle_mask,
247314

248315
kfree(desc);
249316

250-
return ret;
251-
}
252-
253-
/*
254-
* deinstantiate_rng - builds and executes a descriptor on DECO0,
255-
* which deinitializes the RNG block.
256-
* @ctrldev - pointer to device
257-
* @state_handle_mask - bitmask containing the instantiation status
258-
* for the RNG4 state handles which exist in
259-
* the RNG4 block: 1 if it's been instantiated
260-
*
261-
* Return: - 0 if no error occurred
262-
* - -ENOMEM if there isn't enough memory to allocate the descriptor
263-
* - -ENODEV if DECO0 couldn't be acquired
264-
* - -EAGAIN if an error occurred when executing the descriptor
265-
*/
266-
static int deinstantiate_rng(struct device *ctrldev, int state_handle_mask)
267-
{
268-
u32 *desc, status;
269-
int sh_idx, ret = 0;
270-
271-
desc = kmalloc(CAAM_CMD_SZ * 3, GFP_KERNEL);
272-
if (!desc)
273-
return -ENOMEM;
274-
275-
for (sh_idx = 0; sh_idx < RNG4_MAX_HANDLES; sh_idx++) {
276-
/*
277-
* If the corresponding bit is set, then it means the state
278-
* handle was initialized by us, and thus it needs to be
279-
* deinitialized as well
280-
*/
281-
if ((1 << sh_idx) & state_handle_mask) {
282-
/*
283-
* Create the descriptor for deinstantating this state
284-
* handle
285-
*/
286-
build_deinstantiation_desc(desc, sh_idx);
287-
288-
/* Try to run it through DECO0 */
289-
ret = run_descriptor_deco0(ctrldev, desc, &status);
290-
291-
if (ret ||
292-
(status && status != JRSTA_SSRC_JUMP_HALT_CC)) {
293-
dev_err(ctrldev,
294-
"Failed to deinstantiate RNG4 SH%d\n",
295-
sh_idx);
296-
break;
297-
}
298-
dev_info(ctrldev, "Deinstantiated RNG4 SH%d\n", sh_idx);
299-
}
300-
}
301-
302-
kfree(desc);
317+
if (!ret)
318+
ret = devm_add_action_or_reset(ctrldev, devm_deinstantiate_rng,
319+
ctrldev);
303320

304321
return ret;
305322
}
@@ -320,13 +337,6 @@ static int caam_remove(struct platform_device *pdev)
320337
caam_qi_shutdown(ctrldev);
321338
#endif
322339

323-
/*
324-
* De-initialize RNG state handles initialized by this driver.
325-
* In case of SoCs with Management Complex, RNG is managed by MC f/w.
326-
*/
327-
if (!ctrlpriv->mc_en && ctrlpriv->rng4_sh_init)
328-
deinstantiate_rng(ctrldev, ctrlpriv->rng4_sh_init);
329-
330340
return 0;
331341
}
332342

0 commit comments

Comments
 (0)