* Allocate an enhanced descriptor, which contains the hardware descriptor
  * and space for hardware scatter table containing sg_num entries.
  */
-static struct ahash_edesc *ahash_edesc_alloc(struct caam_hash_ctx *ctx,
+static struct ahash_edesc *ahash_edesc_alloc(struct ahash_request *req,
                                             int sg_num, u32 *sh_desc,
-                                            dma_addr_t sh_desc_dma,
-                                            gfp_t flags)
+                                            dma_addr_t sh_desc_dma)
 {
+       struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
+       struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
+       gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
+                      GFP_KERNEL : GFP_ATOMIC;
        struct ahash_edesc *edesc;
        unsigned int sg_size = sg_num * sizeof(struct sec4_sg_entry);
 
        struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
        struct caam_hash_state *state = ahash_request_ctx(req);
        struct device *jrdev = ctx->jrdev;
-       gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
-                      GFP_KERNEL : GFP_ATOMIC;
        u8 *buf = state->buf;
        int *buflen = &state->buflen;
        int *next_buflen = &state->next_buflen;
                 * allocate space for base edesc and hw desc commands,
                 * link tables
                 */
-               edesc = ahash_edesc_alloc(ctx, pad_nents, ctx->sh_desc_update,
-                                         ctx->sh_desc_update_dma, flags);
+               edesc = ahash_edesc_alloc(req, pad_nents, ctx->sh_desc_update,
+                                         ctx->sh_desc_update_dma);
                if (!edesc) {
                        dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
                        return -ENOMEM;
        struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
        struct caam_hash_state *state = ahash_request_ctx(req);
        struct device *jrdev = ctx->jrdev;
-       gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
-                      GFP_KERNEL : GFP_ATOMIC;
        int buflen = state->buflen;
        u32 *desc;
        int sec4_sg_bytes;
                        sizeof(struct sec4_sg_entry);
 
        /* allocate space for base edesc and hw desc commands, link tables */
-       edesc = ahash_edesc_alloc(ctx, 4, ctx->sh_desc_fin,
-                                 ctx->sh_desc_fin_dma, flags);
+       edesc = ahash_edesc_alloc(req, 4, ctx->sh_desc_fin,
+                                 ctx->sh_desc_fin_dma);
        if (!edesc)
                return -ENOMEM;
 
        struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
        struct caam_hash_state *state = ahash_request_ctx(req);
        struct device *jrdev = ctx->jrdev;
-       gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
-                      GFP_KERNEL : GFP_ATOMIC;
        int buflen = state->buflen;
        u32 *desc;
        int sec4_sg_src_index;
        sec4_sg_src_index = 1 + (buflen ? 1 : 0);
 
        /* allocate space for base edesc and hw desc commands, link tables */
-       edesc = ahash_edesc_alloc(ctx, sec4_sg_src_index + mapped_nents,
-                                 ctx->sh_desc_fin, ctx->sh_desc_fin_dma,
-                                 flags);
+       edesc = ahash_edesc_alloc(req, sec4_sg_src_index + mapped_nents,
+                                 ctx->sh_desc_fin, ctx->sh_desc_fin_dma);
        if (!edesc) {
                dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
                return -ENOMEM;
        struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
        struct caam_hash_state *state = ahash_request_ctx(req);
        struct device *jrdev = ctx->jrdev;
-       gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
-                      GFP_KERNEL : GFP_ATOMIC;
        u32 *desc;
        int digestsize = crypto_ahash_digestsize(ahash);
        int src_nents, mapped_nents;
        }
 
        /* allocate space for base edesc and hw desc commands, link tables */
-       edesc = ahash_edesc_alloc(ctx, mapped_nents > 1 ? mapped_nents : 0,
-                                 ctx->sh_desc_digest, ctx->sh_desc_digest_dma,
-                                 flags);
+       edesc = ahash_edesc_alloc(req, mapped_nents > 1 ? mapped_nents : 0,
+                                 ctx->sh_desc_digest, ctx->sh_desc_digest_dma);
        if (!edesc) {
                dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
                return -ENOMEM;
        struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
        struct caam_hash_state *state = ahash_request_ctx(req);
        struct device *jrdev = ctx->jrdev;
-       gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
-                      GFP_KERNEL : GFP_ATOMIC;
        u8 *buf = state->buf;
        int buflen = state->buflen;
        u32 *desc;
        int ret;
 
        /* allocate space for base edesc and hw desc commands, link tables */
-       edesc = ahash_edesc_alloc(ctx, 0, ctx->sh_desc_digest,
-                                 ctx->sh_desc_digest_dma, flags);
+       edesc = ahash_edesc_alloc(req, 0, ctx->sh_desc_digest,
+                                 ctx->sh_desc_digest_dma);
        if (!edesc)
                return -ENOMEM;
 
        struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
        struct caam_hash_state *state = ahash_request_ctx(req);
        struct device *jrdev = ctx->jrdev;
-       gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
-                      GFP_KERNEL : GFP_ATOMIC;
        u8 *buf = state->buf;
        int *buflen = &state->buflen;
        int *next_buflen = &state->next_buflen;
                 * allocate space for base edesc and hw desc commands,
                 * link tables
                 */
-               edesc = ahash_edesc_alloc(ctx, pad_nents,
+               edesc = ahash_edesc_alloc(req, pad_nents,
                                          ctx->sh_desc_update_first,
-                                         ctx->sh_desc_update_first_dma,
-                                         flags);
+                                         ctx->sh_desc_update_first_dma);
                if (!edesc) {
                        dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
                        return -ENOMEM;
        struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
        struct caam_hash_state *state = ahash_request_ctx(req);
        struct device *jrdev = ctx->jrdev;
-       gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
-                      GFP_KERNEL : GFP_ATOMIC;
        int buflen = state->buflen;
        u32 *desc;
        int sec4_sg_bytes, sec4_sg_src_index, src_nents, mapped_nents;
                         sizeof(struct sec4_sg_entry);
 
        /* allocate space for base edesc and hw desc commands, link tables */
-       edesc = ahash_edesc_alloc(ctx, sec4_sg_src_index + mapped_nents,
-                                 ctx->sh_desc_digest, ctx->sh_desc_digest_dma,
-                                 flags);
+       edesc = ahash_edesc_alloc(req, sec4_sg_src_index + mapped_nents,
+                                 ctx->sh_desc_digest, ctx->sh_desc_digest_dma);
        if (!edesc) {
                dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
                return -ENOMEM;
        struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
        struct caam_hash_state *state = ahash_request_ctx(req);
        struct device *jrdev = ctx->jrdev;
-       gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
-                      GFP_KERNEL : GFP_ATOMIC;
        u8 *buf = state->buf;
        int *buflen = &state->buflen;
        int *next_buflen = &state->next_buflen;
                 * allocate space for base edesc and hw desc commands,
                 * link tables
                 */
-               edesc = ahash_edesc_alloc(ctx, mapped_nents > 1 ?
+               edesc = ahash_edesc_alloc(req, mapped_nents > 1 ?
                                          mapped_nents : 0,
                                          ctx->sh_desc_update_first,
-                                         ctx->sh_desc_update_first_dma,
-                                         flags);
+                                         ctx->sh_desc_update_first_dma);
                if (!edesc) {
                        dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
                        return -ENOMEM;