Merge git://git.kernel.org/pub/scm/linux/kernel/git/herbert/crypto-2.6
[sfrench/cifs-2.6.git] / drivers / crypto / caam / caamalg.c
index b3044219772cd7ac57e0bf2559eb7dea8caeca08..156aad167cd6fcd44e66ea9456e253f8e3e36866 100644 (file)
 #else
 #define debug(format, arg...)
 #endif
+
+#ifdef DEBUG
+#include <linux/highmem.h>
+
+static void dbg_dump_sg(const char *level, const char *prefix_str,
+                       int prefix_type, int rowsize, int groupsize,
+                       struct scatterlist *sg, size_t tlen, bool ascii,
+                       bool may_sleep)
+{
+       struct scatterlist *it;
+       void *it_page;
+       size_t len;
+       void *buf;
+
+       for (it = sg; it != NULL && tlen > 0 ; it = sg_next(sg)) {
+               /*
+                * make sure the scatterlist's page
+                * has a valid virtual memory mapping
+                */
+               it_page = kmap_atomic(sg_page(it));
+               if (unlikely(!it_page)) {
+                       printk(KERN_ERR "dbg_dump_sg: kmap failed\n");
+                       return;
+               }
+
+               buf = it_page + it->offset;
+               len = min(tlen, it->length);
+               print_hex_dump(level, prefix_str, prefix_type, rowsize,
+                              groupsize, buf, len, ascii);
+               tlen -= len;
+
+               kunmap_atomic(it_page);
+       }
+}
+#endif
+
 static struct list_head alg_list;
 
 struct caam_alg_entry {
@@ -227,8 +263,9 @@ static void append_key_aead(u32 *desc, struct caam_ctx *ctx,
        if (is_rfc3686) {
                nonce = (u32 *)((void *)ctx->key + ctx->split_key_pad_len +
                               enckeylen);
-               append_load_imm_u32(desc, *nonce, LDST_CLASS_IND_CCB |
-                                   LDST_SRCDST_BYTE_OUTFIFO | LDST_IMM);
+               append_load_as_imm(desc, nonce, CTR_RFC3686_NONCE_SIZE,
+                                  LDST_CLASS_IND_CCB |
+                                  LDST_SRCDST_BYTE_OUTFIFO | LDST_IMM);
                append_move(desc,
                            MOVE_SRC_OUTFIFO |
                            MOVE_DEST_CLASS1CTX |
@@ -500,11 +537,10 @@ static int aead_set_sh_desc(struct crypto_aead *aead)
 
        /* Load Counter into CONTEXT1 reg */
        if (is_rfc3686)
-               append_load_imm_u32(desc, be32_to_cpu(1), LDST_IMM |
-                                   LDST_CLASS_1_CCB |
-                                   LDST_SRCDST_BYTE_CONTEXT |
-                                   ((ctx1_iv_off + CTR_RFC3686_IV_SIZE) <<
-                                    LDST_OFFSET_SHIFT));
+               append_load_imm_be32(desc, 1, LDST_IMM | LDST_CLASS_1_CCB |
+                                    LDST_SRCDST_BYTE_CONTEXT |
+                                    ((ctx1_iv_off + CTR_RFC3686_IV_SIZE) <<
+                                     LDST_OFFSET_SHIFT));
 
        /* Class 1 operation */
        append_operation(desc, ctx->class1_alg_type |
@@ -578,11 +614,10 @@ skip_enc:
 
        /* Load Counter into CONTEXT1 reg */
        if (is_rfc3686)
-               append_load_imm_u32(desc, be32_to_cpu(1), LDST_IMM |
-                                   LDST_CLASS_1_CCB |
-                                   LDST_SRCDST_BYTE_CONTEXT |
-                                   ((ctx1_iv_off + CTR_RFC3686_IV_SIZE) <<
-                                    LDST_OFFSET_SHIFT));
+               append_load_imm_be32(desc, 1, LDST_IMM | LDST_CLASS_1_CCB |
+                                    LDST_SRCDST_BYTE_CONTEXT |
+                                    ((ctx1_iv_off + CTR_RFC3686_IV_SIZE) <<
+                                     LDST_OFFSET_SHIFT));
 
        /* Choose operation */
        if (ctr_mode)
@@ -683,11 +718,10 @@ copy_iv:
 
        /* Load Counter into CONTEXT1 reg */
        if (is_rfc3686)
-               append_load_imm_u32(desc, be32_to_cpu(1), LDST_IMM |
-                                   LDST_CLASS_1_CCB |
-                                   LDST_SRCDST_BYTE_CONTEXT |
-                                   ((ctx1_iv_off + CTR_RFC3686_IV_SIZE) <<
-                                    LDST_OFFSET_SHIFT));
+               append_load_imm_be32(desc, 1, LDST_IMM | LDST_CLASS_1_CCB |
+                                    LDST_SRCDST_BYTE_CONTEXT |
+                                    ((ctx1_iv_off + CTR_RFC3686_IV_SIZE) <<
+                                     LDST_OFFSET_SHIFT));
 
        /* Class 1 operation */
        append_operation(desc, ctx->class1_alg_type |
@@ -1478,7 +1512,7 @@ static int ablkcipher_setkey(struct crypto_ablkcipher *ablkcipher,
        int ret = 0;
        u32 *key_jump_cmd;
        u32 *desc;
-       u32 *nonce;
+       u8 *nonce;
        u32 geniv;
        u32 ctx1_iv_off = 0;
        const bool ctr_mode = ((ctx->class1_alg_type & OP_ALG_AAI_MASK) ==
@@ -1531,9 +1565,10 @@ static int ablkcipher_setkey(struct crypto_ablkcipher *ablkcipher,
 
        /* Load nonce into CONTEXT1 reg */
        if (is_rfc3686) {
-               nonce = (u32 *)(key + keylen);
-               append_load_imm_u32(desc, *nonce, LDST_CLASS_IND_CCB |
-                                   LDST_SRCDST_BYTE_OUTFIFO | LDST_IMM);
+               nonce = (u8 *)key + keylen;
+               append_load_as_imm(desc, nonce, CTR_RFC3686_NONCE_SIZE,
+                                  LDST_CLASS_IND_CCB |
+                                  LDST_SRCDST_BYTE_OUTFIFO | LDST_IMM);
                append_move(desc, MOVE_WAITCOMP |
                            MOVE_SRC_OUTFIFO |
                            MOVE_DEST_CLASS1CTX |
@@ -1549,11 +1584,10 @@ static int ablkcipher_setkey(struct crypto_ablkcipher *ablkcipher,
 
        /* Load counter into CONTEXT1 reg */
        if (is_rfc3686)
-               append_load_imm_u32(desc, be32_to_cpu(1), LDST_IMM |
-                                   LDST_CLASS_1_CCB |
-                                   LDST_SRCDST_BYTE_CONTEXT |
-                                   ((ctx1_iv_off + CTR_RFC3686_IV_SIZE) <<
-                                    LDST_OFFSET_SHIFT));
+               append_load_imm_be32(desc, 1, LDST_IMM | LDST_CLASS_1_CCB |
+                                    LDST_SRCDST_BYTE_CONTEXT |
+                                    ((ctx1_iv_off + CTR_RFC3686_IV_SIZE) <<
+                                     LDST_OFFSET_SHIFT));
 
        /* Load operation */
        append_operation(desc, ctx->class1_alg_type |
@@ -1590,9 +1624,10 @@ static int ablkcipher_setkey(struct crypto_ablkcipher *ablkcipher,
 
        /* Load nonce into CONTEXT1 reg */
        if (is_rfc3686) {
-               nonce = (u32 *)(key + keylen);
-               append_load_imm_u32(desc, *nonce, LDST_CLASS_IND_CCB |
-                                   LDST_SRCDST_BYTE_OUTFIFO | LDST_IMM);
+               nonce = (u8 *)key + keylen;
+               append_load_as_imm(desc, nonce, CTR_RFC3686_NONCE_SIZE,
+                                  LDST_CLASS_IND_CCB |
+                                  LDST_SRCDST_BYTE_OUTFIFO | LDST_IMM);
                append_move(desc, MOVE_WAITCOMP |
                            MOVE_SRC_OUTFIFO |
                            MOVE_DEST_CLASS1CTX |
@@ -1608,11 +1643,10 @@ static int ablkcipher_setkey(struct crypto_ablkcipher *ablkcipher,
 
        /* Load counter into CONTEXT1 reg */
        if (is_rfc3686)
-               append_load_imm_u32(desc, be32_to_cpu(1), LDST_IMM |
-                                   LDST_CLASS_1_CCB |
-                                   LDST_SRCDST_BYTE_CONTEXT |
-                                   ((ctx1_iv_off + CTR_RFC3686_IV_SIZE) <<
-                                    LDST_OFFSET_SHIFT));
+               append_load_imm_be32(desc, 1, LDST_IMM | LDST_CLASS_1_CCB |
+                                    LDST_SRCDST_BYTE_CONTEXT |
+                                    ((ctx1_iv_off + CTR_RFC3686_IV_SIZE) <<
+                                     LDST_OFFSET_SHIFT));
 
        /* Choose operation */
        if (ctr_mode)
@@ -1653,9 +1687,10 @@ static int ablkcipher_setkey(struct crypto_ablkcipher *ablkcipher,
 
        /* Load Nonce into CONTEXT1 reg */
        if (is_rfc3686) {
-               nonce = (u32 *)(key + keylen);
-               append_load_imm_u32(desc, *nonce, LDST_CLASS_IND_CCB |
-                                   LDST_SRCDST_BYTE_OUTFIFO | LDST_IMM);
+               nonce = (u8 *)key + keylen;
+               append_load_as_imm(desc, nonce, CTR_RFC3686_NONCE_SIZE,
+                                  LDST_CLASS_IND_CCB |
+                                  LDST_SRCDST_BYTE_OUTFIFO | LDST_IMM);
                append_move(desc, MOVE_WAITCOMP |
                            MOVE_SRC_OUTFIFO |
                            MOVE_DEST_CLASS1CTX |
@@ -1685,11 +1720,10 @@ static int ablkcipher_setkey(struct crypto_ablkcipher *ablkcipher,
 
        /* Load Counter into CONTEXT1 reg */
        if (is_rfc3686)
-               append_load_imm_u32(desc, (u32)1, LDST_IMM |
-                                   LDST_CLASS_1_CCB |
-                                   LDST_SRCDST_BYTE_CONTEXT |
-                                   ((ctx1_iv_off + CTR_RFC3686_IV_SIZE) <<
-                                    LDST_OFFSET_SHIFT));
+               append_load_imm_be32(desc, 1, LDST_IMM | LDST_CLASS_1_CCB |
+                                    LDST_SRCDST_BYTE_CONTEXT |
+                                    ((ctx1_iv_off + CTR_RFC3686_IV_SIZE) <<
+                                     LDST_OFFSET_SHIFT));
 
        if (ctx1_iv_off)
                append_jump(desc, JUMP_JSL | JUMP_TEST_ALL | JUMP_COND_NCP |
@@ -1995,9 +2029,9 @@ static void ablkcipher_encrypt_done(struct device *jrdev, u32 *desc, u32 err,
        print_hex_dump(KERN_ERR, "dstiv  @"__stringify(__LINE__)": ",
                       DUMP_PREFIX_ADDRESS, 16, 4, req->info,
                       edesc->src_nents > 1 ? 100 : ivsize, 1);
-       print_hex_dump(KERN_ERR, "dst    @"__stringify(__LINE__)": ",
-                      DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src),
-                      edesc->dst_nents > 1 ? 100 : req->nbytes, 1);
+       dbg_dump_sg(KERN_ERR, "dst    @"__stringify(__LINE__)": ",
+                   DUMP_PREFIX_ADDRESS, 16, 4, req->dst,
+                   edesc->dst_nents > 1 ? 100 : req->nbytes, 1, true);
 #endif
 
        ablkcipher_unmap(jrdev, edesc, req);
@@ -2027,9 +2061,9 @@ static void ablkcipher_decrypt_done(struct device *jrdev, u32 *desc, u32 err,
        print_hex_dump(KERN_ERR, "dstiv  @"__stringify(__LINE__)": ",
                       DUMP_PREFIX_ADDRESS, 16, 4, req->info,
                       ivsize, 1);
-       print_hex_dump(KERN_ERR, "dst    @"__stringify(__LINE__)": ",
-                      DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src),
-                      edesc->dst_nents > 1 ? 100 : req->nbytes, 1);
+       dbg_dump_sg(KERN_ERR, "dst    @"__stringify(__LINE__)": ",
+                   DUMP_PREFIX_ADDRESS, 16, 4, req->dst,
+                   edesc->dst_nents > 1 ? 100 : req->nbytes, 1, true);
 #endif
 
        ablkcipher_unmap(jrdev, edesc, req);
@@ -2184,12 +2218,15 @@ static void init_ablkcipher_job(u32 *sh_desc, dma_addr_t ptr,
        int len, sec4_sg_index = 0;
 
 #ifdef DEBUG
+       bool may_sleep = ((req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
+                                             CRYPTO_TFM_REQ_MAY_SLEEP)) != 0);
        print_hex_dump(KERN_ERR, "presciv@"__stringify(__LINE__)": ",
                       DUMP_PREFIX_ADDRESS, 16, 4, req->info,
                       ivsize, 1);
-       print_hex_dump(KERN_ERR, "src    @"__stringify(__LINE__)": ",
-                      DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src),
-                      edesc->src_nents ? 100 : req->nbytes, 1);
+       printk(KERN_ERR "asked=%d, nbytes%d\n", (int)edesc->src_nents ? 100 : req->nbytes, req->nbytes);
+       dbg_dump_sg(KERN_ERR, "src    @"__stringify(__LINE__)": ",
+                   DUMP_PREFIX_ADDRESS, 16, 4, req->src,
+                   edesc->src_nents ? 100 : req->nbytes, 1, may_sleep);
 #endif
 
        len = desc_len(sh_desc);
@@ -2241,12 +2278,14 @@ static void init_ablkcipher_giv_job(u32 *sh_desc, dma_addr_t ptr,
        int len, sec4_sg_index = 0;
 
 #ifdef DEBUG
+       bool may_sleep = ((req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
+                                             CRYPTO_TFM_REQ_MAY_SLEEP)) != 0);
        print_hex_dump(KERN_ERR, "presciv@" __stringify(__LINE__) ": ",
                       DUMP_PREFIX_ADDRESS, 16, 4, req->info,
                       ivsize, 1);
-       print_hex_dump(KERN_ERR, "src    @" __stringify(__LINE__) ": ",
-                      DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src),
-                      edesc->src_nents ? 100 : req->nbytes, 1);
+       dbg_dump_sg(KERN_ERR, "src    @" __stringify(__LINE__) ": ",
+                   DUMP_PREFIX_ADDRESS, 16, 4, req->src,
+                   edesc->src_nents ? 100 : req->nbytes, 1, may_sleep);
 #endif
 
        len = desc_len(sh_desc);
@@ -2516,18 +2555,20 @@ static int aead_decrypt(struct aead_request *req)
        u32 *desc;
        int ret = 0;
 
+#ifdef DEBUG
+       bool may_sleep = ((req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
+                                             CRYPTO_TFM_REQ_MAY_SLEEP)) != 0);
+       dbg_dump_sg(KERN_ERR, "dec src@"__stringify(__LINE__)": ",
+                   DUMP_PREFIX_ADDRESS, 16, 4, req->src,
+                   req->assoclen + req->cryptlen, 1, may_sleep);
+#endif
+
        /* allocate extended descriptor */
        edesc = aead_edesc_alloc(req, AUTHENC_DESC_JOB_IO_LEN,
                                 &all_contig, false);
        if (IS_ERR(edesc))
                return PTR_ERR(edesc);
 
-#ifdef DEBUG
-       print_hex_dump(KERN_ERR, "dec src@"__stringify(__LINE__)": ",
-                      DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src),
-                      req->assoclen + req->cryptlen, 1);
-#endif
-
        /* Create and submit job descriptor*/
        init_authenc_job(req, edesc, all_contig, false);
 #ifdef DEBUG