6e3cd24ac04c55b58428f2a8ca09d155d7b79a5e
[openwrt/staging/ynezz.git] /
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
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 Use devres to de-initialize the RNG and drop explicit de-initialization
10 code in caam_remove().
11
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)
23 ---
24 drivers/crypto/caam/ctrl.c | 130 ++++++++++++++++++++++++---------------------
25 1 file changed, 70 insertions(+), 60 deletions(-)
26
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
30 }
31
32 /*
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
39 + *
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
44 + */
45 +static int deinstantiate_rng(struct device *ctrldev, int state_handle_mask)
46 +{
47 + u32 *desc, status;
48 + int sh_idx, ret = 0;
49 +
50 + desc = kmalloc(CAAM_CMD_SZ * 3, GFP_KERNEL);
51 + if (!desc)
52 + return -ENOMEM;
53 +
54 + for (sh_idx = 0; sh_idx < RNG4_MAX_HANDLES; sh_idx++) {
55 + /*
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
59 + */
60 + if ((1 << sh_idx) & state_handle_mask) {
61 + /*
62 + * Create the descriptor for deinstantating this state
63 + * handle
64 + */
65 + build_deinstantiation_desc(desc, sh_idx);
66 +
67 + /* Try to run it through DECO0 */
68 + ret = run_descriptor_deco0(ctrldev, desc, &status);
69 +
70 + if (ret ||
71 + (status && status != JRSTA_SSRC_JUMP_HALT_CC)) {
72 + dev_err(ctrldev,
73 + "Failed to deinstantiate RNG4 SH%d\n",
74 + sh_idx);
75 + break;
76 + }
77 + dev_info(ctrldev, "Deinstantiated RNG4 SH%d\n", sh_idx);
78 + }
79 + }
80 +
81 + kfree(desc);
82 +
83 + return ret;
84 +}
85 +
86 +static void devm_deinstantiate_rng(void *data)
87 +{
88 + struct device *ctrldev = data;
89 + struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctrldev);
90 +
91 + /*
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.
94 + */
95 + if (ctrlpriv->rng4_sh_init)
96 + deinstantiate_rng(ctrldev, ctrlpriv->rng4_sh_init);
97 +}
98 +
99 +/*
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
104
105 kfree(desc);
106
107 - return ret;
108 -}
109 -
110 -/*
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
117 - *
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
122 - */
123 -static int deinstantiate_rng(struct device *ctrldev, int state_handle_mask)
124 -{
125 - u32 *desc, status;
126 - int sh_idx, ret = 0;
127 -
128 - desc = kmalloc(CAAM_CMD_SZ * 3, GFP_KERNEL);
129 - if (!desc)
130 - return -ENOMEM;
131 -
132 - for (sh_idx = 0; sh_idx < RNG4_MAX_HANDLES; sh_idx++) {
133 - /*
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
137 - */
138 - if ((1 << sh_idx) & state_handle_mask) {
139 - /*
140 - * Create the descriptor for deinstantating this state
141 - * handle
142 - */
143 - build_deinstantiation_desc(desc, sh_idx);
144 -
145 - /* Try to run it through DECO0 */
146 - ret = run_descriptor_deco0(ctrldev, desc, &status);
147 -
148 - if (ret ||
149 - (status && status != JRSTA_SSRC_JUMP_HALT_CC)) {
150 - dev_err(ctrldev,
151 - "Failed to deinstantiate RNG4 SH%d\n",
152 - sh_idx);
153 - break;
154 - }
155 - dev_info(ctrldev, "Deinstantiated RNG4 SH%d\n", sh_idx);
156 - }
157 - }
158 -
159 - kfree(desc);
160 + if (!ret)
161 + ret = devm_add_action_or_reset(ctrldev, devm_deinstantiate_rng,
162 + ctrldev);
163
164 return ret;
165 }
166 @@ -320,13 +337,6 @@ static int caam_remove(struct platform_d
167 caam_qi_shutdown(ctrldev);
168 #endif
169
170 - /*
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.
173 - */
174 - if (!ctrlpriv->mc_en && ctrlpriv->rng4_sh_init)
175 - deinstantiate_rng(ctrldev, ctrlpriv->rng4_sh_init);
176 -
177 return 0;
178 }
179