In case of GIVCIPHER, IV is generated by the device.
Fix the DMA mapping direction.

Cc: <sta...@vger.kernel.org> # 3.19+
Fixes: 7222d1a34103 ("crypto: caam - add support for givencrypt cbc(aes) and 
rfc3686(ctr(aes))")
Signed-off-by: Horia Geantă <horia.gea...@nxp.com>
---
 drivers/crypto/caam/caamalg.c | 29 +++++++++++++++++------------
 1 file changed, 17 insertions(+), 12 deletions(-)

diff --git a/drivers/crypto/caam/caamalg.c b/drivers/crypto/caam/caamalg.c
index 584a6c183548..a9a4df7012e2 100644
--- a/drivers/crypto/caam/caamalg.c
+++ b/drivers/crypto/caam/caamalg.c
@@ -767,6 +767,7 @@ struct aead_edesc {
  * @src_nents: number of segments in input s/w scatterlist
  * @dst_nents: number of segments in output s/w scatterlist
  * @iv_dma: dma address of iv for checking continuity and link table
+ * @iv_dir: DMA mapping direction for IV
  * @sec4_sg_bytes: length of dma mapped sec4_sg space
  * @sec4_sg_dma: bus physical mapped address of h/w link table
  * @sec4_sg: pointer to h/w link table
@@ -776,6 +777,7 @@ struct ablkcipher_edesc {
        int src_nents;
        int dst_nents;
        dma_addr_t iv_dma;
+       enum dma_data_direction iv_dir;
        int sec4_sg_bytes;
        dma_addr_t sec4_sg_dma;
        struct sec4_sg_entry *sec4_sg;
@@ -785,7 +787,8 @@ struct ablkcipher_edesc {
 static void caam_unmap(struct device *dev, struct scatterlist *src,
                       struct scatterlist *dst, int src_nents,
                       int dst_nents,
-                      dma_addr_t iv_dma, int ivsize, dma_addr_t sec4_sg_dma,
+                      dma_addr_t iv_dma, int ivsize,
+                      enum dma_data_direction iv_dir, dma_addr_t sec4_sg_dma,
                       int sec4_sg_bytes)
 {
        if (dst != src) {
@@ -797,7 +800,7 @@ static void caam_unmap(struct device *dev, struct 
scatterlist *src,
        }
 
        if (iv_dma)
-               dma_unmap_single(dev, iv_dma, ivsize, DMA_TO_DEVICE);
+               dma_unmap_single(dev, iv_dma, ivsize, iv_dir);
        if (sec4_sg_bytes)
                dma_unmap_single(dev, sec4_sg_dma, sec4_sg_bytes,
                                 DMA_TO_DEVICE);
@@ -808,7 +811,7 @@ static void aead_unmap(struct device *dev,
                       struct aead_request *req)
 {
        caam_unmap(dev, req->src, req->dst,
-                  edesc->src_nents, edesc->dst_nents, 0, 0,
+                  edesc->src_nents, edesc->dst_nents, 0, 0, DMA_NONE,
                   edesc->sec4_sg_dma, edesc->sec4_sg_bytes);
 }
 
@@ -821,7 +824,7 @@ static void ablkcipher_unmap(struct device *dev,
 
        caam_unmap(dev, req->src, req->dst,
                   edesc->src_nents, edesc->dst_nents,
-                  edesc->iv_dma, ivsize,
+                  edesc->iv_dma, ivsize, edesc->iv_dir,
                   edesc->sec4_sg_dma, edesc->sec4_sg_bytes);
 }
 
@@ -1285,7 +1288,7 @@ static struct aead_edesc *aead_edesc_alloc(struct 
aead_request *req,
                        GFP_DMA | flags);
        if (!edesc) {
                caam_unmap(jrdev, req->src, req->dst, src_nents, dst_nents, 0,
-                          0, 0, 0);
+                          0, DMA_NONE, 0, 0);
                return ERR_PTR(-ENOMEM);
        }
 
@@ -1548,7 +1551,7 @@ static struct ablkcipher_edesc 
*ablkcipher_edesc_alloc(struct ablkcipher_request
        if (dma_mapping_error(jrdev, iv_dma)) {
                dev_err(jrdev, "unable to map IV\n");
                caam_unmap(jrdev, req->src, req->dst, src_nents, dst_nents, 0,
-                          0, 0, 0);
+                          0, DMA_NONE, 0, 0);
                return ERR_PTR(-ENOMEM);
        }
 
@@ -1570,7 +1573,7 @@ static struct ablkcipher_edesc 
*ablkcipher_edesc_alloc(struct ablkcipher_request
        if (!edesc) {
                dev_err(jrdev, "could not allocate extended descriptor\n");
                caam_unmap(jrdev, req->src, req->dst, src_nents, dst_nents,
-                          iv_dma, ivsize, 0, 0);
+                          iv_dma, ivsize, DMA_TO_DEVICE, 0, 0);
                return ERR_PTR(-ENOMEM);
        }
 
@@ -1579,6 +1582,7 @@ static struct ablkcipher_edesc 
*ablkcipher_edesc_alloc(struct ablkcipher_request
        edesc->sec4_sg_bytes = sec4_sg_bytes;
        edesc->sec4_sg = (void *)edesc + sizeof(struct ablkcipher_edesc) +
                         desc_bytes;
+       edesc->iv_dir = DMA_TO_DEVICE;
 
        if (!in_contig) {
                dma_to_sec4_sg_one(edesc->sec4_sg, iv_dma, ivsize, 0);
@@ -1596,7 +1600,7 @@ static struct ablkcipher_edesc 
*ablkcipher_edesc_alloc(struct ablkcipher_request
        if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) {
                dev_err(jrdev, "unable to map S/G table\n");
                caam_unmap(jrdev, req->src, req->dst, src_nents, dst_nents,
-                          iv_dma, ivsize, 0, 0);
+                          iv_dma, ivsize, DMA_TO_DEVICE, 0, 0);
                kfree(edesc);
                return ERR_PTR(-ENOMEM);
        }
@@ -1754,11 +1758,11 @@ static struct ablkcipher_edesc 
*ablkcipher_giv_edesc_alloc(
         * Check if iv can be contiguous with source and destination.
         * If so, include it. If not, create scatterlist.
         */
-       iv_dma = dma_map_single(jrdev, greq->giv, ivsize, DMA_TO_DEVICE);
+       iv_dma = dma_map_single(jrdev, greq->giv, ivsize, DMA_FROM_DEVICE);
        if (dma_mapping_error(jrdev, iv_dma)) {
                dev_err(jrdev, "unable to map IV\n");
                caam_unmap(jrdev, req->src, req->dst, src_nents, dst_nents, 0,
-                          0, 0, 0);
+                          0, DMA_NONE, 0, 0);
                return ERR_PTR(-ENOMEM);
        }
 
@@ -1779,7 +1783,7 @@ static struct ablkcipher_edesc 
*ablkcipher_giv_edesc_alloc(
        if (!edesc) {
                dev_err(jrdev, "could not allocate extended descriptor\n");
                caam_unmap(jrdev, req->src, req->dst, src_nents, dst_nents,
-                          iv_dma, ivsize, 0, 0);
+                          iv_dma, ivsize, DMA_FROM_DEVICE, 0, 0);
                return ERR_PTR(-ENOMEM);
        }
 
@@ -1788,6 +1792,7 @@ static struct ablkcipher_edesc 
*ablkcipher_giv_edesc_alloc(
        edesc->sec4_sg_bytes = sec4_sg_bytes;
        edesc->sec4_sg = (void *)edesc + sizeof(struct ablkcipher_edesc) +
                         desc_bytes;
+       edesc->iv_dir = DMA_FROM_DEVICE;
 
        if (mapped_src_nents > 1)
                sg_to_sec4_sg_last(req->src, mapped_src_nents, edesc->sec4_sg,
@@ -1805,7 +1810,7 @@ static struct ablkcipher_edesc 
*ablkcipher_giv_edesc_alloc(
        if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) {
                dev_err(jrdev, "unable to map S/G table\n");
                caam_unmap(jrdev, req->src, req->dst, src_nents, dst_nents,
-                          iv_dma, ivsize, 0, 0);
+                          iv_dma, ivsize, DMA_FROM_DEVICE, 0, 0);
                kfree(edesc);
                return ERR_PTR(-ENOMEM);
        }
-- 
2.16.2

Reply via email to