These changes are the raw update to linux-4.4.6-rt14. Kernel sources
[kvmfornfv.git] / kernel / drivers / crypto / sahara.c
index 6be377f..f68c24a 100644 (file)
@@ -173,7 +173,6 @@ struct sahara_aes_reqctx {
  * @sg_in_idx: number of hw links
  * @in_sg: scatterlist for input data
  * @in_sg_chain: scatterlists for chained input data
- * @in_sg_chained: specifies if chained scatterlists are used or not
  * @total: total number of bytes for transfer
  * @last: is this the last block
  * @first: is this the first block
@@ -191,7 +190,6 @@ struct sahara_sha_reqctx {
        unsigned int            sg_in_idx;
        struct scatterlist      *in_sg;
        struct scatterlist      in_sg_chain[2];
-       bool                    in_sg_chained;
        size_t                  total;
        unsigned int            last;
        unsigned int            first;
@@ -274,31 +272,7 @@ static u32 sahara_aes_data_link_hdr(struct sahara_dev *dev)
                        SAHARA_HDR_CHA_SKHA | SAHARA_HDR_PARITY_BIT;
 }
 
-static int sahara_sg_length(struct scatterlist *sg,
-                           unsigned int total)
-{
-       int sg_nb;
-       unsigned int len;
-       struct scatterlist *sg_list;
-
-       sg_nb = 0;
-       sg_list = sg;
-
-       while (total) {
-               len = min(sg_list->length, total);
-
-               sg_nb++;
-               total -= len;
-
-               sg_list = sg_next(sg_list);
-               if (!sg_list)
-                       total = 0;
-       }
-
-       return sg_nb;
-}
-
-static char *sahara_err_src[16] = {
+static const char *sahara_err_src[16] = {
        "No error",
        "Header error",
        "Descriptor length error",
@@ -317,14 +291,14 @@ static char *sahara_err_src[16] = {
        "DMA error"
 };
 
-static char *sahara_err_dmasize[4] = {
+static const char *sahara_err_dmasize[4] = {
        "Byte transfer",
        "Half-word transfer",
        "Word transfer",
        "Reserved"
 };
 
-static char *sahara_err_dmasrc[8] = {
+static const char *sahara_err_dmasrc[8] = {
        "No error",
        "AHB bus error",
        "Internal IP bus error",
@@ -335,7 +309,7 @@ static char *sahara_err_dmasrc[8] = {
        "DMA HW error"
 };
 
-static char *sahara_cha_errsrc[12] = {
+static const char *sahara_cha_errsrc[12] = {
        "Input buffer non-empty",
        "Illegal address",
        "Illegal mode",
@@ -350,7 +324,7 @@ static char *sahara_cha_errsrc[12] = {
        "Reserved"
 };
 
-static char *sahara_cha_err[4] = { "No error", "SKHA", "MDHA", "RNG" };
+static const char *sahara_cha_err[4] = { "No error", "SKHA", "MDHA", "RNG" };
 
 static void sahara_decode_error(struct sahara_dev *dev, unsigned int error)
 {
@@ -380,7 +354,7 @@ static void sahara_decode_error(struct sahara_dev *dev, unsigned int error)
        dev_err(dev->device, "\n");
 }
 
-static char *sahara_state[4] = { "Idle", "Busy", "Error", "HW Fault" };
+static const char *sahara_state[4] = { "Idle", "Busy", "Error", "HW Fault" };
 
 static void sahara_decode_status(struct sahara_dev *dev, unsigned int status)
 {
@@ -502,8 +476,8 @@ static int sahara_hw_descriptor_create(struct sahara_dev *dev)
                idx++;
        }
 
-       dev->nb_in_sg = sahara_sg_length(dev->in_sg, dev->total);
-       dev->nb_out_sg = sahara_sg_length(dev->out_sg, dev->total);
+       dev->nb_in_sg = sg_nents_for_len(dev->in_sg, dev->total);
+       dev->nb_out_sg = sg_nents_for_len(dev->out_sg, dev->total);
        if ((dev->nb_in_sg + dev->nb_out_sg) > SAHARA_MAX_HW_LINK) {
                dev_err(dev->device, "not enough hw links (%d)\n",
                        dev->nb_in_sg + dev->nb_out_sg);
@@ -818,45 +792,26 @@ static int sahara_sha_hw_links_create(struct sahara_dev *dev,
 
        dev->in_sg = rctx->in_sg;
 
-       dev->nb_in_sg = sahara_sg_length(dev->in_sg, rctx->total);
+       dev->nb_in_sg = sg_nents_for_len(dev->in_sg, rctx->total);
        if ((dev->nb_in_sg) > SAHARA_MAX_HW_LINK) {
                dev_err(dev->device, "not enough hw links (%d)\n",
                        dev->nb_in_sg + dev->nb_out_sg);
                return -EINVAL;
        }
 
-       if (rctx->in_sg_chained) {
-               i = start;
-               sg = dev->in_sg;
-               while (sg) {
-                       ret = dma_map_sg(dev->device, sg, 1,
-                                        DMA_TO_DEVICE);
-                       if (!ret)
-                               return -EFAULT;
-
-                       dev->hw_link[i]->len = sg->length;
-                       dev->hw_link[i]->p = sg->dma_address;
+       sg = dev->in_sg;
+       ret = dma_map_sg(dev->device, dev->in_sg, dev->nb_in_sg, DMA_TO_DEVICE);
+       if (!ret)
+               return -EFAULT;
+
+       for (i = start; i < dev->nb_in_sg + start; i++) {
+               dev->hw_link[i]->len = sg->length;
+               dev->hw_link[i]->p = sg->dma_address;
+               if (i == (dev->nb_in_sg + start - 1)) {
+                       dev->hw_link[i]->next = 0;
+               } else {
                        dev->hw_link[i]->next = dev->hw_phys_link[i + 1];
                        sg = sg_next(sg);
-                       i += 1;
-               }
-               dev->hw_link[i-1]->next = 0;
-       } else {
-               sg = dev->in_sg;
-               ret = dma_map_sg(dev->device, dev->in_sg, dev->nb_in_sg,
-                                DMA_TO_DEVICE);
-               if (!ret)
-                       return -EFAULT;
-
-               for (i = start; i < dev->nb_in_sg + start; i++) {
-                       dev->hw_link[i]->len = sg->length;
-                       dev->hw_link[i]->p = sg->dma_address;
-                       if (i == (dev->nb_in_sg + start - 1)) {
-                               dev->hw_link[i]->next = 0;
-                       } else {
-                               dev->hw_link[i]->next = dev->hw_phys_link[i + 1];
-                               sg = sg_next(sg);
-                       }
                }
        }
 
@@ -999,12 +954,11 @@ static int sahara_sha_prepare_request(struct ahash_request *req)
                sg_init_table(rctx->in_sg_chain, 2);
                sg_set_buf(rctx->in_sg_chain, rctx->rembuf, rctx->buf_cnt);
 
-               scatterwalk_sg_chain(rctx->in_sg_chain, 2, req->src);
+               sg_chain(rctx->in_sg_chain, 2, req->src);
 
                rctx->total = req->nbytes + rctx->buf_cnt;
                rctx->in_sg = rctx->in_sg_chain;
 
-               rctx->in_sg_chained = true;
                req->src = rctx->in_sg_chain;
        /* only data from previous operation */
        } else if (rctx->buf_cnt) {
@@ -1015,13 +969,11 @@ static int sahara_sha_prepare_request(struct ahash_request *req)
                /* buf was copied into rembuf above */
                sg_init_one(rctx->in_sg, rctx->rembuf, rctx->buf_cnt);
                rctx->total = rctx->buf_cnt;
-               rctx->in_sg_chained = false;
        /* no data from previous operation */
        } else {
                rctx->in_sg = req->src;
                rctx->total = req->nbytes;
                req->src = rctx->in_sg;
-               rctx->in_sg_chained = false;
        }
 
        /* on next call, we only have the remaining data in the buffer */
@@ -1030,23 +982,6 @@ static int sahara_sha_prepare_request(struct ahash_request *req)
        return -EINPROGRESS;
 }
 
-static void sahara_sha_unmap_sg(struct sahara_dev *dev,
-                               struct sahara_sha_reqctx *rctx)
-{
-       struct scatterlist *sg;
-
-       if (rctx->in_sg_chained) {
-               sg = dev->in_sg;
-               while (sg) {
-                       dma_unmap_sg(dev->device, sg, 1, DMA_TO_DEVICE);
-                       sg = sg_next(sg);
-               }
-       } else {
-               dma_unmap_sg(dev->device, dev->in_sg, dev->nb_in_sg,
-                       DMA_TO_DEVICE);
-       }
-}
-
 static int sahara_sha_process(struct ahash_request *req)
 {
        struct sahara_dev *dev = dev_ptr;
@@ -1086,7 +1021,8 @@ static int sahara_sha_process(struct ahash_request *req)
        }
 
        if (rctx->sg_in_idx)
-               sahara_sha_unmap_sg(dev, rctx);
+               dma_unmap_sg(dev->device, dev->in_sg, dev->nb_in_sg,
+                            DMA_TO_DEVICE);
 
        memcpy(rctx->context, dev->context_base, rctx->context_size);
 
@@ -1516,7 +1452,7 @@ static int sahara_probe(struct platform_device *pdev)
        }
 
        /* Allocate HW descriptors */
-       dev->hw_desc[0] = dma_alloc_coherent(&pdev->dev,
+       dev->hw_desc[0] = dmam_alloc_coherent(&pdev->dev,
                        SAHARA_MAX_HW_DESC * sizeof(struct sahara_hw_desc),
                        &dev->hw_phys_desc[0], GFP_KERNEL);
        if (!dev->hw_desc[0]) {
@@ -1528,34 +1464,31 @@ static int sahara_probe(struct platform_device *pdev)
                                sizeof(struct sahara_hw_desc);
 
        /* Allocate space for iv and key */
-       dev->key_base = dma_alloc_coherent(&pdev->dev, 2 * AES_KEYSIZE_128,
+       dev->key_base = dmam_alloc_coherent(&pdev->dev, 2 * AES_KEYSIZE_128,
                                &dev->key_phys_base, GFP_KERNEL);
        if (!dev->key_base) {
                dev_err(&pdev->dev, "Could not allocate memory for key\n");
-               err = -ENOMEM;
-               goto err_key;
+               return -ENOMEM;
        }
        dev->iv_base = dev->key_base + AES_KEYSIZE_128;
        dev->iv_phys_base = dev->key_phys_base + AES_KEYSIZE_128;
 
        /* Allocate space for context: largest digest + message length field */
-       dev->context_base = dma_alloc_coherent(&pdev->dev,
+       dev->context_base = dmam_alloc_coherent(&pdev->dev,
                                        SHA256_DIGEST_SIZE + 4,
                                        &dev->context_phys_base, GFP_KERNEL);
        if (!dev->context_base) {
                dev_err(&pdev->dev, "Could not allocate memory for MDHA context\n");
-               err = -ENOMEM;
-               goto err_key;
+               return -ENOMEM;
        }
 
        /* Allocate space for HW links */
-       dev->hw_link[0] = dma_alloc_coherent(&pdev->dev,
+       dev->hw_link[0] = dmam_alloc_coherent(&pdev->dev,
                        SAHARA_MAX_HW_LINK * sizeof(struct sahara_hw_link),
                        &dev->hw_phys_link[0], GFP_KERNEL);
        if (!dev->hw_link[0]) {
                dev_err(&pdev->dev, "Could not allocate hw links\n");
-               err = -ENOMEM;
-               goto err_link;
+               return -ENOMEM;
        }
        for (i = 1; i < SAHARA_MAX_HW_LINK; i++) {
                dev->hw_phys_link[i] = dev->hw_phys_link[i - 1] +
@@ -1572,14 +1505,17 @@ static int sahara_probe(struct platform_device *pdev)
 
        dev->kthread = kthread_run(sahara_queue_manage, dev, "sahara_crypto");
        if (IS_ERR(dev->kthread)) {
-               err = PTR_ERR(dev->kthread);
-               goto err_link;
+               return PTR_ERR(dev->kthread);
        }
 
        init_completion(&dev->dma_completion);
 
-       clk_prepare_enable(dev->clk_ipg);
-       clk_prepare_enable(dev->clk_ahb);
+       err = clk_prepare_enable(dev->clk_ipg);
+       if (err)
+               return err;
+       err = clk_prepare_enable(dev->clk_ahb);
+       if (err)
+               goto clk_ipg_disable;
 
        version = sahara_read(dev, SAHARA_REG_VERSION);
        if (of_device_is_compatible(pdev->dev.of_node, "fsl,imx27-sahara")) {
@@ -1616,24 +1552,11 @@ static int sahara_probe(struct platform_device *pdev)
        return 0;
 
 err_algs:
-       dma_free_coherent(&pdev->dev,
-                         SAHARA_MAX_HW_LINK * sizeof(struct sahara_hw_link),
-                         dev->hw_link[0], dev->hw_phys_link[0]);
-       clk_disable_unprepare(dev->clk_ipg);
-       clk_disable_unprepare(dev->clk_ahb);
        kthread_stop(dev->kthread);
        dev_ptr = NULL;
-err_link:
-       dma_free_coherent(&pdev->dev,
-                         2 * AES_KEYSIZE_128,
-                         dev->key_base, dev->key_phys_base);
-       dma_free_coherent(&pdev->dev,
-                         SHA256_DIGEST_SIZE,
-                         dev->context_base, dev->context_phys_base);
-err_key:
-       dma_free_coherent(&pdev->dev,
-                         SAHARA_MAX_HW_DESC * sizeof(struct sahara_hw_desc),
-                         dev->hw_desc[0], dev->hw_phys_desc[0]);
+       clk_disable_unprepare(dev->clk_ahb);
+clk_ipg_disable:
+       clk_disable_unprepare(dev->clk_ipg);
 
        return err;
 }
@@ -1642,16 +1565,6 @@ static int sahara_remove(struct platform_device *pdev)
 {
        struct sahara_dev *dev = platform_get_drvdata(pdev);
 
-       dma_free_coherent(&pdev->dev,
-                         SAHARA_MAX_HW_LINK * sizeof(struct sahara_hw_link),
-                         dev->hw_link[0], dev->hw_phys_link[0]);
-       dma_free_coherent(&pdev->dev,
-                         2 * AES_KEYSIZE_128,
-                         dev->key_base, dev->key_phys_base);
-       dma_free_coherent(&pdev->dev,
-                         SAHARA_MAX_HW_DESC * sizeof(struct sahara_hw_desc),
-                         dev->hw_desc[0], dev->hw_phys_desc[0]);
-
        kthread_stop(dev->kthread);
 
        sahara_unregister_algs(dev);