Skip to content

Commit 0605fa0

Browse files
montjoieherbertx
authored andcommitted
crypto: sun8i-ce - split into prepare/run/unprepare
This patch split the do_one_request into three. Prepare will handle all DMA mapping and initialisation of the task structure. Unprepare will clean all DMA mapping. And the do_one_request will be limited to just executing the task. Signed-off-by: Corentin Labbe <clabbe@baylibre.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent a216f8d commit 0605fa0

2 files changed

Lines changed: 79 additions & 20 deletions

File tree

‎drivers/crypto/allwinner/sun8i-ce/sun8i-ce-cipher.c‎

Lines changed: 71 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,9 @@ static int sun8i_ce_cipher_fallback(struct skcipher_request *areq)
7575
return err;
7676
}
7777

78-
static int sun8i_ce_cipher(struct skcipher_request *areq)
78+
static int sun8i_ce_cipher_prepare(struct crypto_engine *engine, void *async_req)
7979
{
80+
struct skcipher_request *areq = container_of(async_req, struct skcipher_request, base);
8081
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
8182
struct sun8i_cipher_tfm_ctx *op = crypto_skcipher_ctx(tfm);
8283
struct sun8i_ce_dev *ce = op->ce;
@@ -87,7 +88,6 @@ static int sun8i_ce_cipher(struct skcipher_request *areq)
8788
struct ce_task *cet;
8889
struct scatterlist *sg;
8990
unsigned int todo, len, offset, ivsize;
90-
dma_addr_t addr_iv = 0, addr_key = 0;
9191
u32 common, sym;
9292
int flow, i;
9393
int nr_sgs = 0;
@@ -140,13 +140,13 @@ static int sun8i_ce_cipher(struct skcipher_request *areq)
140140
cet->t_sym_ctl = cpu_to_le32(sym);
141141
cet->t_asym_ctl = 0;
142142

143-
addr_key = dma_map_single(ce->dev, op->key, op->keylen, DMA_TO_DEVICE);
144-
cet->t_key = cpu_to_le32(addr_key);
145-
if (dma_mapping_error(ce->dev, addr_key)) {
143+
rctx->addr_key = dma_map_single(ce->dev, op->key, op->keylen, DMA_TO_DEVICE);
144+
if (dma_mapping_error(ce->dev, rctx->addr_key)) {
146145
dev_err(ce->dev, "Cannot DMA MAP KEY\n");
147146
err = -EFAULT;
148147
goto theend;
149148
}
149+
cet->t_key = cpu_to_le32(rctx->addr_key);
150150

151151
ivsize = crypto_skcipher_ivsize(tfm);
152152
if (areq->iv && crypto_skcipher_ivsize(tfm) > 0) {
@@ -167,14 +167,14 @@ static int sun8i_ce_cipher(struct skcipher_request *areq)
167167
offset, ivsize, 0);
168168
}
169169
memcpy(rctx->bounce_iv, areq->iv, ivsize);
170-
addr_iv = dma_map_single(ce->dev, rctx->bounce_iv, rctx->ivlen,
171-
DMA_TO_DEVICE);
172-
cet->t_iv = cpu_to_le32(addr_iv);
173-
if (dma_mapping_error(ce->dev, addr_iv)) {
170+
rctx->addr_iv = dma_map_single(ce->dev, rctx->bounce_iv, rctx->ivlen,
171+
DMA_TO_DEVICE);
172+
if (dma_mapping_error(ce->dev, rctx->addr_iv)) {
174173
dev_err(ce->dev, "Cannot DMA MAP IV\n");
175174
err = -ENOMEM;
176175
goto theend_iv;
177176
}
177+
cet->t_iv = cpu_to_le32(rctx->addr_iv);
178178
}
179179

180180
if (areq->src == areq->dst) {
@@ -234,7 +234,9 @@ static int sun8i_ce_cipher(struct skcipher_request *areq)
234234
}
235235

236236
chan->timeout = areq->cryptlen;
237-
err = sun8i_ce_run_task(ce, flow, crypto_tfm_alg_name(areq->base.tfm));
237+
rctx->nr_sgs = nr_sgs;
238+
rctx->nr_sgd = nr_sgd;
239+
return 0;
238240

239241
theend_sgs:
240242
if (areq->src == areq->dst) {
@@ -247,9 +249,8 @@ static int sun8i_ce_cipher(struct skcipher_request *areq)
247249

248250
theend_iv:
249251
if (areq->iv && ivsize > 0) {
250-
if (addr_iv)
251-
dma_unmap_single(ce->dev, addr_iv, rctx->ivlen,
252-
DMA_TO_DEVICE);
252+
if (rctx->addr_iv)
253+
dma_unmap_single(ce->dev, rctx->addr_iv, rctx->ivlen, DMA_TO_DEVICE);
253254
offset = areq->cryptlen - ivsize;
254255
if (rctx->op_dir & CE_DECRYPTION) {
255256
memcpy(areq->iv, rctx->backup_iv, ivsize);
@@ -262,19 +263,69 @@ static int sun8i_ce_cipher(struct skcipher_request *areq)
262263
}
263264

264265
theend_key:
265-
dma_unmap_single(ce->dev, addr_key, op->keylen, DMA_TO_DEVICE);
266+
dma_unmap_single(ce->dev, rctx->addr_key, op->keylen, DMA_TO_DEVICE);
266267

267268
theend:
268269
return err;
269270
}
270271

271-
static int sun8i_ce_handle_cipher_request(struct crypto_engine *engine, void *areq)
272+
static int sun8i_ce_cipher_run(struct crypto_engine *engine, void *areq)
272273
{
273-
int err;
274274
struct skcipher_request *breq = container_of(areq, struct skcipher_request, base);
275+
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(breq);
276+
struct sun8i_cipher_tfm_ctx *op = crypto_skcipher_ctx(tfm);
277+
struct sun8i_ce_dev *ce = op->ce;
278+
struct sun8i_cipher_req_ctx *rctx = skcipher_request_ctx(breq);
279+
int flow, err;
275280

276-
err = sun8i_ce_cipher(breq);
281+
flow = rctx->flow;
282+
err = sun8i_ce_run_task(ce, flow, crypto_tfm_alg_name(breq->base.tfm));
277283
crypto_finalize_skcipher_request(engine, breq, err);
284+
return 0;
285+
}
286+
287+
static int sun8i_ce_cipher_unprepare(struct crypto_engine *engine, void *async_req)
288+
{
289+
struct skcipher_request *areq = container_of(async_req, struct skcipher_request, base);
290+
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
291+
struct sun8i_cipher_tfm_ctx *op = crypto_skcipher_ctx(tfm);
292+
struct sun8i_ce_dev *ce = op->ce;
293+
struct sun8i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
294+
struct sun8i_ce_flow *chan;
295+
struct ce_task *cet;
296+
unsigned int ivsize, offset;
297+
int nr_sgs = rctx->nr_sgs;
298+
int nr_sgd = rctx->nr_sgd;
299+
int flow;
300+
301+
flow = rctx->flow;
302+
chan = &ce->chanlist[flow];
303+
cet = chan->tl;
304+
ivsize = crypto_skcipher_ivsize(tfm);
305+
306+
if (areq->src == areq->dst) {
307+
dma_unmap_sg(ce->dev, areq->src, nr_sgs, DMA_BIDIRECTIONAL);
308+
} else {
309+
if (nr_sgs > 0)
310+
dma_unmap_sg(ce->dev, areq->src, nr_sgs, DMA_TO_DEVICE);
311+
dma_unmap_sg(ce->dev, areq->dst, nr_sgd, DMA_FROM_DEVICE);
312+
}
313+
314+
if (areq->iv && ivsize > 0) {
315+
if (cet->t_iv)
316+
dma_unmap_single(ce->dev, rctx->addr_iv, rctx->ivlen, DMA_TO_DEVICE);
317+
offset = areq->cryptlen - ivsize;
318+
if (rctx->op_dir & CE_DECRYPTION) {
319+
memcpy(areq->iv, rctx->backup_iv, ivsize);
320+
kfree_sensitive(rctx->backup_iv);
321+
} else {
322+
scatterwalk_map_and_copy(areq->iv, areq->dst, offset,
323+
ivsize, 0);
324+
}
325+
kfree(rctx->bounce_iv);
326+
}
327+
328+
dma_unmap_single(ce->dev, rctx->addr_key, op->keylen, DMA_TO_DEVICE);
278329

279330
return 0;
280331
}
@@ -346,9 +397,9 @@ int sun8i_ce_cipher_init(struct crypto_tfm *tfm)
346397
crypto_tfm_alg_driver_name(&sktfm->base),
347398
crypto_tfm_alg_driver_name(crypto_skcipher_tfm(op->fallback_tfm)));
348399

349-
op->enginectx.op.do_one_request = sun8i_ce_handle_cipher_request;
350-
op->enginectx.op.prepare_request = NULL;
351-
op->enginectx.op.unprepare_request = NULL;
400+
op->enginectx.op.do_one_request = sun8i_ce_cipher_run;
401+
op->enginectx.op.prepare_request = sun8i_ce_cipher_prepare;
402+
op->enginectx.op.unprepare_request = sun8i_ce_cipher_unprepare;
352403

353404
err = pm_runtime_get_sync(op->ce->dev);
354405
if (err < 0)

‎drivers/crypto/allwinner/sun8i-ce/sun8i-ce.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,10 @@ struct sun8i_ce_dev {
182182
* @backup_iv: buffer which contain the next IV to store
183183
* @bounce_iv: buffer which contain the IV
184184
* @ivlen: size of bounce_iv
185+
* @nr_sgs: The number of source SG (as given by dma_map_sg())
186+
* @nr_sgd: The number of destination SG (as given by dma_map_sg())
187+
* @addr_iv: The IV addr returned by dma_map_single, need to unmap later
188+
* @addr_key: The key addr returned by dma_map_single, need to unmap later
185189
* @fallback_req: request struct for invoking the fallback skcipher TFM
186190
*/
187191
struct sun8i_cipher_req_ctx {
@@ -190,6 +194,10 @@ struct sun8i_cipher_req_ctx {
190194
void *backup_iv;
191195
void *bounce_iv;
192196
unsigned int ivlen;
197+
int nr_sgs;
198+
int nr_sgd;
199+
dma_addr_t addr_iv;
200+
dma_addr_t addr_key;
193201
struct skcipher_request fallback_req; // keep at the end
194202
};
195203

0 commit comments

Comments
 (0)