1 From 4719469bf1e6d5bac8bb0426be4dd6a124471b69 Mon Sep 17 00:00:00 2001
2 From: Andrey Smirnov <andrew.smirnov@gmail.com>
3 Date: Tue, 22 Oct 2019 08:30:10 -0700
4 Subject: [PATCH] crypto: caam - use devres to de-initialize the RNG
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
9 Use devres to de-initialize the RNG and drop explicit de-initialization
10 code in caam_remove().
12 Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com>
13 Cc: Chris Healy <cphealy@gmail.com>
14 Cc: Lucas Stach <l.stach@pengutronix.de>
15 Cc: Horia Geantă <horia.geanta@nxp.com>
16 Cc: Herbert Xu <herbert@gondor.apana.org.au>
17 Cc: Iuliana Prodan <iuliana.prodan@nxp.com>
18 Cc: linux-crypto@vger.kernel.org
19 Cc: linux-kernel@vger.kernel.org
20 Reviewed-by: Horia Geantă <horia.geanta@nxp.com>
21 Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
22 (cherry picked from commit e57acaf0dfe0c8f63411d43cf7c689e43f6810c0)
24 drivers/crypto/caam/ctrl.c | 130 ++++++++++++++++++++++++---------------------
25 1 file changed, 70 insertions(+), 60 deletions(-)
27 --- a/drivers/crypto/caam/ctrl.c
28 +++ b/drivers/crypto/caam/ctrl.c
29 @@ -176,6 +176,73 @@ static inline int run_descriptor_deco0(s
33 + * deinstantiate_rng - builds and executes a descriptor on DECO0,
34 + * which deinitializes the RNG block.
35 + * @ctrldev - pointer to device
36 + * @state_handle_mask - bitmask containing the instantiation status
37 + * for the RNG4 state handles which exist in
38 + * the RNG4 block: 1 if it's been instantiated
40 + * Return: - 0 if no error occurred
41 + * - -ENOMEM if there isn't enough memory to allocate the descriptor
42 + * - -ENODEV if DECO0 couldn't be acquired
43 + * - -EAGAIN if an error occurred when executing the descriptor
45 +static int deinstantiate_rng(struct device *ctrldev, int state_handle_mask)
48 + int sh_idx, ret = 0;
50 + desc = kmalloc(CAAM_CMD_SZ * 3, GFP_KERNEL);
54 + for (sh_idx = 0; sh_idx < RNG4_MAX_HANDLES; sh_idx++) {
56 + * If the corresponding bit is set, then it means the state
57 + * handle was initialized by us, and thus it needs to be
58 + * deinitialized as well
60 + if ((1 << sh_idx) & state_handle_mask) {
62 + * Create the descriptor for deinstantating this state
65 + build_deinstantiation_desc(desc, sh_idx);
67 + /* Try to run it through DECO0 */
68 + ret = run_descriptor_deco0(ctrldev, desc, &status);
71 + (status && status != JRSTA_SSRC_JUMP_HALT_CC)) {
73 + "Failed to deinstantiate RNG4 SH%d\n",
77 + dev_info(ctrldev, "Deinstantiated RNG4 SH%d\n", sh_idx);
86 +static void devm_deinstantiate_rng(void *data)
88 + struct device *ctrldev = data;
89 + struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctrldev);
92 + * De-initialize RNG state handles initialized by this driver.
93 + * In case of SoCs with Management Complex, RNG is managed by MC f/w.
95 + if (ctrlpriv->rng4_sh_init)
96 + deinstantiate_rng(ctrldev, ctrlpriv->rng4_sh_init);
100 * instantiate_rng - builds and executes a descriptor on DECO0,
101 * which initializes the RNG block.
102 * @ctrldev - pointer to device
103 @@ -247,59 +314,9 @@ static int instantiate_rng(struct device
111 - * deinstantiate_rng - builds and executes a descriptor on DECO0,
112 - * which deinitializes the RNG block.
113 - * @ctrldev - pointer to device
114 - * @state_handle_mask - bitmask containing the instantiation status
115 - * for the RNG4 state handles which exist in
116 - * the RNG4 block: 1 if it's been instantiated
118 - * Return: - 0 if no error occurred
119 - * - -ENOMEM if there isn't enough memory to allocate the descriptor
120 - * - -ENODEV if DECO0 couldn't be acquired
121 - * - -EAGAIN if an error occurred when executing the descriptor
123 -static int deinstantiate_rng(struct device *ctrldev, int state_handle_mask)
126 - int sh_idx, ret = 0;
128 - desc = kmalloc(CAAM_CMD_SZ * 3, GFP_KERNEL);
132 - for (sh_idx = 0; sh_idx < RNG4_MAX_HANDLES; sh_idx++) {
134 - * If the corresponding bit is set, then it means the state
135 - * handle was initialized by us, and thus it needs to be
136 - * deinitialized as well
138 - if ((1 << sh_idx) & state_handle_mask) {
140 - * Create the descriptor for deinstantating this state
143 - build_deinstantiation_desc(desc, sh_idx);
145 - /* Try to run it through DECO0 */
146 - ret = run_descriptor_deco0(ctrldev, desc, &status);
149 - (status && status != JRSTA_SSRC_JUMP_HALT_CC)) {
151 - "Failed to deinstantiate RNG4 SH%d\n",
155 - dev_info(ctrldev, "Deinstantiated RNG4 SH%d\n", sh_idx);
161 + ret = devm_add_action_or_reset(ctrldev, devm_deinstantiate_rng,
166 @@ -320,13 +337,6 @@ static int caam_remove(struct platform_d
167 caam_qi_shutdown(ctrldev);
171 - * De-initialize RNG state handles initialized by this driver.
172 - * In case of SoCs with Management Complex, RNG is managed by MC f/w.
174 - if (!ctrlpriv->mc_en && ctrlpriv->rng4_sh_init)
175 - deinstantiate_rng(ctrldev, ctrlpriv->rng4_sh_init);