1 From e640f4bcfa0088ff696bc5da6063a1ea8d782189 Mon Sep 17 00:00:00 2001
2 From: Iuliana Prodan <iuliana.prodan@nxp.com>
3 Date: Thu, 26 Sep 2019 15:26:29 +0300
4 Subject: [PATCH] crypto: caam - use mapped_{src,dst}_nents for descriptor
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
9 The mapped_{src,dst}_nents _returned_ from the dma_map_sg
10 call (which could be less than src/dst_nents) have to be
11 used to generate the job descriptors.
13 Signed-off-by: Iuliana Prodan <iuliana.prodan@nxp.com>
14 Reviewed-by: Horia Geantă <horia.geanta@nxp.com>
15 Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
16 (cherry picked from commit eff9771d51529acf7f6f58a60b2923b98da28f0e)
18 drivers/crypto/caam/caampkc.c | 72 +++++++++++++++++++++++--------------------
19 drivers/crypto/caam/caampkc.h | 8 +++--
20 2 files changed, 45 insertions(+), 35 deletions(-)
22 --- a/drivers/crypto/caam/caampkc.c
23 +++ b/drivers/crypto/caam/caampkc.c
24 @@ -252,9 +252,9 @@ static struct rsa_edesc *rsa_edesc_alloc
25 gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
26 GFP_KERNEL : GFP_ATOMIC;
27 int sg_flags = (flags == GFP_ATOMIC) ? SG_MITER_ATOMIC : 0;
29 int sec4_sg_index, sec4_sg_len = 0, sec4_sg_bytes;
30 int src_nents, dst_nents;
31 + int mapped_src_nents, mapped_dst_nents;
32 unsigned int diff_size = 0;
35 @@ -285,13 +285,27 @@ static struct rsa_edesc *rsa_edesc_alloc
36 req_ctx->fixup_src_len);
37 dst_nents = sg_nents_for_len(req->dst, req->dst_len);
39 - if (!diff_size && src_nents == 1)
40 + mapped_src_nents = dma_map_sg(dev, req_ctx->fixup_src, src_nents,
42 + if (unlikely(!mapped_src_nents)) {
43 + dev_err(dev, "unable to map source\n");
44 + return ERR_PTR(-ENOMEM);
46 + mapped_dst_nents = dma_map_sg(dev, req->dst, dst_nents,
48 + if (unlikely(!mapped_dst_nents)) {
49 + dev_err(dev, "unable to map destination\n");
53 + if (!diff_size && mapped_src_nents == 1)
54 sec4_sg_len = 0; /* no need for an input hw s/g table */
56 - sec4_sg_len = src_nents + !!diff_size;
57 + sec4_sg_len = mapped_src_nents + !!diff_size;
58 sec4_sg_index = sec4_sg_len;
60 - sec4_sg_len += pad_sg_nents(dst_nents);
62 + if (mapped_dst_nents > 1)
63 + sec4_sg_len += pad_sg_nents(mapped_dst_nents);
65 sec4_sg_len = pad_sg_nents(sec4_sg_len);
67 @@ -301,19 +315,7 @@ static struct rsa_edesc *rsa_edesc_alloc
68 edesc = kzalloc(sizeof(*edesc) + desclen + sec4_sg_bytes,
71 - return ERR_PTR(-ENOMEM);
73 - sgc = dma_map_sg(dev, req_ctx->fixup_src, src_nents, DMA_TO_DEVICE);
74 - if (unlikely(!sgc)) {
75 - dev_err(dev, "unable to map source\n");
79 - sgc = dma_map_sg(dev, req->dst, dst_nents, DMA_FROM_DEVICE);
80 - if (unlikely(!sgc)) {
81 - dev_err(dev, "unable to map destination\n");
85 edesc->sec4_sg = (void *)edesc + sizeof(*edesc) + desclen;
87 @@ -324,7 +326,7 @@ static struct rsa_edesc *rsa_edesc_alloc
88 sg_to_sec4_sg_last(req_ctx->fixup_src, req_ctx->fixup_src_len,
89 edesc->sec4_sg + !!diff_size, 0);
92 + if (mapped_dst_nents > 1)
93 sg_to_sec4_sg_last(req->dst, req->dst_len,
94 edesc->sec4_sg + sec4_sg_index, 0);
96 @@ -335,6 +337,9 @@ static struct rsa_edesc *rsa_edesc_alloc
100 + edesc->mapped_src_nents = mapped_src_nents;
101 + edesc->mapped_dst_nents = mapped_dst_nents;
103 edesc->sec4_sg_dma = dma_map_single(dev, edesc->sec4_sg,
104 sec4_sg_bytes, DMA_TO_DEVICE);
105 if (dma_mapping_error(dev, edesc->sec4_sg_dma)) {
106 @@ -351,11 +356,11 @@ static struct rsa_edesc *rsa_edesc_alloc
110 - dma_unmap_sg(dev, req->dst, dst_nents, DMA_FROM_DEVICE);
113 - dma_unmap_sg(dev, req_ctx->fixup_src, src_nents, DMA_TO_DEVICE);
114 + dma_unmap_sg(dev, req->dst, dst_nents, DMA_FROM_DEVICE);
117 + dma_unmap_sg(dev, req_ctx->fixup_src, src_nents, DMA_TO_DEVICE);
118 return ERR_PTR(-ENOMEM);
121 @@ -383,15 +388,15 @@ static int set_rsa_pub_pdb(struct akciph
125 - if (edesc->src_nents > 1) {
126 + if (edesc->mapped_src_nents > 1) {
127 pdb->sgf |= RSA_PDB_SGF_F;
128 pdb->f_dma = edesc->sec4_sg_dma;
129 - sec4_sg_index += edesc->src_nents;
130 + sec4_sg_index += edesc->mapped_src_nents;
132 pdb->f_dma = sg_dma_address(req_ctx->fixup_src);
135 - if (edesc->dst_nents > 1) {
136 + if (edesc->mapped_dst_nents > 1) {
137 pdb->sgf |= RSA_PDB_SGF_G;
138 pdb->g_dma = edesc->sec4_sg_dma +
139 sec4_sg_index * sizeof(struct sec4_sg_entry);
140 @@ -428,17 +433,18 @@ static int set_rsa_priv_f1_pdb(struct ak
144 - if (edesc->src_nents > 1) {
145 + if (edesc->mapped_src_nents > 1) {
146 pdb->sgf |= RSA_PRIV_PDB_SGF_G;
147 pdb->g_dma = edesc->sec4_sg_dma;
148 - sec4_sg_index += edesc->src_nents;
149 + sec4_sg_index += edesc->mapped_src_nents;
152 struct caam_rsa_req_ctx *req_ctx = akcipher_request_ctx(req);
154 pdb->g_dma = sg_dma_address(req_ctx->fixup_src);
157 - if (edesc->dst_nents > 1) {
158 + if (edesc->mapped_dst_nents > 1) {
159 pdb->sgf |= RSA_PRIV_PDB_SGF_F;
160 pdb->f_dma = edesc->sec4_sg_dma +
161 sec4_sg_index * sizeof(struct sec4_sg_entry);
162 @@ -493,17 +499,17 @@ static int set_rsa_priv_f2_pdb(struct ak
166 - if (edesc->src_nents > 1) {
167 + if (edesc->mapped_src_nents > 1) {
168 pdb->sgf |= RSA_PRIV_PDB_SGF_G;
169 pdb->g_dma = edesc->sec4_sg_dma;
170 - sec4_sg_index += edesc->src_nents;
171 + sec4_sg_index += edesc->mapped_src_nents;
173 struct caam_rsa_req_ctx *req_ctx = akcipher_request_ctx(req);
175 pdb->g_dma = sg_dma_address(req_ctx->fixup_src);
178 - if (edesc->dst_nents > 1) {
179 + if (edesc->mapped_dst_nents > 1) {
180 pdb->sgf |= RSA_PRIV_PDB_SGF_F;
181 pdb->f_dma = edesc->sec4_sg_dma +
182 sec4_sg_index * sizeof(struct sec4_sg_entry);
183 @@ -582,17 +588,17 @@ static int set_rsa_priv_f3_pdb(struct ak
187 - if (edesc->src_nents > 1) {
188 + if (edesc->mapped_src_nents > 1) {
189 pdb->sgf |= RSA_PRIV_PDB_SGF_G;
190 pdb->g_dma = edesc->sec4_sg_dma;
191 - sec4_sg_index += edesc->src_nents;
192 + sec4_sg_index += edesc->mapped_src_nents;
194 struct caam_rsa_req_ctx *req_ctx = akcipher_request_ctx(req);
196 pdb->g_dma = sg_dma_address(req_ctx->fixup_src);
199 - if (edesc->dst_nents > 1) {
200 + if (edesc->mapped_dst_nents > 1) {
201 pdb->sgf |= RSA_PRIV_PDB_SGF_F;
202 pdb->f_dma = edesc->sec4_sg_dma +
203 sec4_sg_index * sizeof(struct sec4_sg_entry);
204 --- a/drivers/crypto/caam/caampkc.h
205 +++ b/drivers/crypto/caam/caampkc.h
206 @@ -112,8 +112,10 @@ struct caam_rsa_req_ctx {
209 * rsa_edesc - s/w-extended rsa descriptor
210 - * @src_nents : number of segments in input scatterlist
211 - * @dst_nents : number of segments in output scatterlist
212 + * @src_nents : number of segments in input s/w scatterlist
213 + * @dst_nents : number of segments in output s/w scatterlist
214 + * @mapped_src_nents: number of segments in input h/w link table
215 + * @mapped_dst_nents: number of segments in output h/w link table
216 * @sec4_sg_bytes : length of h/w link table
217 * @sec4_sg_dma : dma address of h/w link table
218 * @sec4_sg : pointer to h/w link table
219 @@ -123,6 +125,8 @@ struct caam_rsa_req_ctx {
223 + int mapped_src_nents;
224 + int mapped_dst_nents;
226 dma_addr_t sec4_sg_dma;
227 struct sec4_sg_entry *sec4_sg;