Merge "Replace dm-crypt.c with v3.4.108"
diff --git a/drivers/md/dm-crypt.c b/drivers/md/dm-crypt.c
index 5040ce1..926989d 100644
--- a/drivers/md/dm-crypt.c
+++ b/drivers/md/dm-crypt.c
@@ -29,16 +29,6 @@
 
 #include <linux/device-mapper.h>
 
-#if defined(CONFIG_OCF_DM_CRYPT)
-#undef DM_DEBUG
-#ifdef DM_DEBUG
-#define dmprintk printk
-#else
-#define dmprintk(fmt,args...)
-#endif
-#include <linux/cryptodev.h>
-#endif /* CONFIG_OCF_DM_CRYPT */
-
 #define DM_MSG_PREFIX "crypt"
 
 /*
@@ -156,10 +146,6 @@
 	sector_t iv_offset;
 	unsigned int iv_size;
 
-#if defined(CONFIG_OCF_DM_CRYPT)
-	struct cryptoini	cr_dm;	/* OCF session */
-	uint64_t	ocf_cryptoid;	/* OCF sesssion ID */
-#endif
 	/*
 	 * Duplicated per cpu state. Access through
 	 * per_cpu_ptr() only.
@@ -283,7 +269,7 @@
 		return err;
 
 	for_each_possible_cpu(cpu) {
-		essiv_tfm = per_cpu_ptr(cc->cpu, cpu)->iv_private;
+		essiv_tfm = per_cpu_ptr(cc->cpu, cpu)->iv_private,
 
 		err = crypto_cipher_setkey(essiv_tfm, essiv->salt,
 				    crypto_hash_digestsize(essiv->hash_tfm));
@@ -319,7 +305,7 @@
 					     struct dm_target *ti,
 					     u8 *salt, unsigned saltsize)
 {
-	struct crypto_cipher *essiv_tfm=NULL;
+	struct crypto_cipher *essiv_tfm;
 	int err;
 
 	/* Setup the essiv_tfm with the given salt */
@@ -329,12 +315,8 @@
 		return essiv_tfm;
 	}
 
-#if  defined(CONFIG_OCF_DM_CRYPT)
-	if (crypto_cipher_blocksize(essiv_tfm) != cc->iv_size) {
-#else
 	if (crypto_cipher_blocksize(essiv_tfm) !=
 	    crypto_ablkcipher_ivsize(any_tfm(cc))) {
-#endif
 		ti->error = "Block size of ESSIV cipher does "
 			    "not match IV size of block cipher";
 		crypto_free_cipher(essiv_tfm);
@@ -437,7 +419,6 @@
 	return 0;
 }
 
-#if !defined(CONFIG_OCF_DM_CRYPT)
 static int crypt_iv_benbi_ctr(struct crypt_config *cc, struct dm_target *ti,
 			      const char *opts)
 {
@@ -478,7 +459,6 @@
 
 	return 0;
 }
-#endif
 
 static int crypt_iv_null_gen(struct crypt_config *cc, u8 *iv,
 			     struct dm_crypt_request *dmreq)
@@ -609,9 +589,9 @@
 	int r = 0;
 
 	if (bio_data_dir(dmreq->ctx->bio_in) == WRITE) {
-		src = kmap_atomic(sg_page(&dmreq->sg_in), KM_USER0);
+		src = kmap_atomic(sg_page(&dmreq->sg_in));
 		r = crypt_iv_lmk_one(cc, iv, dmreq, src + dmreq->sg_in.offset);
-		kunmap_atomic(src, KM_USER0);
+		kunmap_atomic(src);
 	} else
 		memset(iv, 0, cc->iv_size);
 
@@ -627,14 +607,14 @@
 	if (bio_data_dir(dmreq->ctx->bio_in) == WRITE)
 		return 0;
 
-	dst = kmap_atomic(sg_page(&dmreq->sg_out), KM_USER0);
+	dst = kmap_atomic(sg_page(&dmreq->sg_out));
 	r = crypt_iv_lmk_one(cc, iv, dmreq, dst + dmreq->sg_out.offset);
 
 	/* Tweak the first block of plaintext sector */
 	if (!r)
 		crypto_xor(dst + dmreq->sg_out.offset, iv, cc->iv_size);
 
-	kunmap_atomic(dst, KM_USER0);
+	kunmap_atomic(dst);
 	return r;
 }
 
@@ -654,13 +634,11 @@
 	.generator = crypt_iv_essiv_gen
 };
 
-#if !defined(CONFIG_OCF_DM_CRYPT)
 static struct crypt_iv_operations crypt_iv_benbi_ops = {
 	.ctr	   = crypt_iv_benbi_ctr,
 	.dtr	   = crypt_iv_benbi_dtr,
 	.generator = crypt_iv_benbi_gen
 };
-#endif
 
 static struct crypt_iv_operations crypt_iv_null_ops = {
 	.generator = crypt_iv_null_gen
@@ -709,275 +687,6 @@
 		crypto_ablkcipher_alignmask(any_tfm(cc)) + 1);
 }
 
-static void kcryptd_crypt_read_done(struct dm_crypt_io *io);
-static void crypt_alloc_req(struct crypt_config *cc,
-			    struct convert_context *ctx);
-static void crypt_inc_pending(struct dm_crypt_io *io);
-static void crypt_dec_pending(struct dm_crypt_io *io);
-
-#if defined(CONFIG_OCF_DM_CRYPT)
-struct ocf_wr_priv {
-	u32			dm_ocf_wr_completed;	/* Num of wr completions */
-	u32			dm_ocf_wr_pending;	/* Num of wr pendings */
-	wait_queue_head_t	dm_ocf_wr_queue;	/* waiting Q, for wr completion */
-};
-
-struct ocf_rd_priv {
-	u32			dm_ocf_rd_completed;	/* Num of rd completions */
-	u32			dm_ocf_rd_pending;	/* Num of rd pendings */
-	wait_queue_head_t	dm_ocf_rd_queue;	/* waiting Q, for rd completion */
-};
-
-static int dm_ocf_wr_cb(struct cryptop *crp)
-{
-	struct ocf_wr_priv *ocf_wr_priv;
-
-	if(crp == NULL) {
-		printk("dm_ocf_wr_cb: crp is NULL!! \n");
-		return 0;
-	}
-
-	ocf_wr_priv = (struct ocf_wr_priv*)crp->crp_opaque;
-
-	ocf_wr_priv->dm_ocf_wr_completed++;
-
-	/* if no more pending for read, wake up the read task. */
-	if(ocf_wr_priv->dm_ocf_wr_completed == ocf_wr_priv->dm_ocf_wr_pending)
-		wake_up(&ocf_wr_priv->dm_ocf_wr_queue);
-
-	crypto_freereq(crp);
-	return 0;
-}
-
-static int dm_ocf_rd_cb(struct cryptop *crp)
-{
-	struct ocf_rd_priv *ocf_rd_priv;
-
-	if(crp == NULL) {
-		printk("dm_ocf_rd_cb: crp is NULL!! \n");
-		return 0;
-	}
-
-	ocf_rd_priv = (struct ocf_rd_priv*)crp->crp_opaque;
-
-	ocf_rd_priv->dm_ocf_rd_completed++;
-
-	/* if no more pending for read, wake up the read task. */
-	if(ocf_rd_priv->dm_ocf_rd_completed == ocf_rd_priv->dm_ocf_rd_pending)
-		wake_up(&ocf_rd_priv->dm_ocf_rd_queue);
-
-	crypto_freereq(crp);
-	return 0;
-}
-
-static inline int dm_ocf_process(struct crypt_config *cc, struct scatterlist *out,
-		struct scatterlist *in, unsigned int len, u8 *iv, int iv_size, int write, void *priv)
-{
-	struct cryptop *crp;
-	struct cryptodesc *crda = NULL;
-	struct page *in_page = sg_page(in);
-	struct page *out_page = sg_page(out);
-	int r=0;
-
-	if(!iv) {
-		printk("dm_ocf_process: only CBC mode is supported\n");
-		return -EPERM;
-	}
-
-	crp = crypto_getreq(1);  /* only encryption/decryption */
-	if (!crp) {
-		printk("\ndm_ocf_process: crypto_getreq failed!!\n");
-		return -ENOMEM;
-	}
-
-	crda = crp->crp_desc;
-
-	crda->crd_flags  = (write)? CRD_F_ENCRYPT: 0;
-	crda->crd_alg    = cc->cr_dm.cri_alg;
-	crda->crd_skip   = 0;
-	crda->crd_len    = len;
-	crda->crd_inject = 0; /* NA */
-	crda->crd_klen   = cc->cr_dm.cri_klen;
-	crda->crd_key    = cc->cr_dm.cri_key;
-
-	if (iv) {
-		//crda->crd_flags |= (CRD_F_IV_PRESENT);
-		crda->crd_flags |= (CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT);
-		if( iv_size > EALG_MAX_BLOCK_LEN ) {
-			printk("dm_ocf_process: iv is too big!!\n");
-		}
-		memcpy(&crda->crd_iv, iv, iv_size);
-	}
-
-
-	//dmprintk("len: %d\n",len);
-	crp->crp_ilen = len; /* Total input length */
-	//crp->crp_flags = CRYPTO_F_CBIMM;
-	crp->crp_flags = CRYPTO_F_CBIMM | CRYPTO_F_BATCH;
-	crp->crp_buf = page_address(in_page) + in->offset;
-	crp->crp_out_buf = page_address(out_page) + out->offset;
-	crp->crp_opaque = priv;
-	if(write) {
-		crp->crp_callback = dm_ocf_wr_cb;
-	}
-	else {
-		crp->crp_callback = dm_ocf_rd_cb;
-	}
-	crp->crp_sid = cc->ocf_cryptoid;
-	r = crypto_dispatch(crp);
-	if (r == ENOMEM) {
-		dmprintk("crypto_dispatch returned ENOMEM \n");
-	}
-	return r;
-}
-
-static int ocf_crypt_convert_block(struct crypt_config *cc,
-				struct convert_context *ctx,
-				struct ablkcipher_request *req,
-				int write, void *priv)
-{
-	struct bio_vec *bv_in = bio_iovec_idx(ctx->bio_in, ctx->idx_in);
-	struct bio_vec *bv_out = bio_iovec_idx(ctx->bio_out, ctx->idx_out);
-	struct dm_crypt_request *dmreq;
-	u8 iv[cc->iv_size];
-	int r = 0;
-
-	dmreq = dmreq_of_req(cc, req);
-	dmreq->iv_sector = ctx->sector;
-	dmreq->ctx = ctx;
-	sg_init_table(&dmreq->sg_in, 1);
-	sg_set_page(&dmreq->sg_in, bv_in->bv_page, 1 << SECTOR_SHIFT,
-		    bv_in->bv_offset + ctx->offset_in);
-
-	sg_init_table(&dmreq->sg_out, 1);
-	sg_set_page(&dmreq->sg_out, bv_out->bv_page, 1 << SECTOR_SHIFT,
-		    bv_out->bv_offset + ctx->offset_out);
-
-	ctx->offset_in += 1 << SECTOR_SHIFT;
-	if (ctx->offset_in >= bv_in->bv_len) {
-		ctx->offset_in = 0;
-		ctx->idx_in++;
-	}
-
-	ctx->offset_out += 1 << SECTOR_SHIFT;
-	if (ctx->offset_out >= bv_out->bv_len) {
-		ctx->offset_out = 0;
-		ctx->idx_out++;
-	}
-
-	if (cc->iv_gen_ops) {
-		r = cc->iv_gen_ops->generator(cc, iv, dmreq);
-		if (r < 0)
-			return r;
-		r = dm_ocf_process(cc, &dmreq->sg_out, &dmreq->sg_in, dmreq->sg_in.length, iv, cc->iv_size, write, priv);
-	} else {
-		r = dm_ocf_process(cc, &dmreq->sg_in, &dmreq->sg_in, dmreq->sg_in.length, NULL, 0, write, priv);
-	}
-
-	return r;
-}
-
-/*
- * Encrypt / decrypt data from one bio to another one (can be the same one)
- */
-static int ocf_crypt_convert(struct crypt_config *cc,
-			 struct convert_context *ctx, struct dm_crypt_io *io)
-{
-	int r = 0;
-	long wr_timeout = 300000;
-	long rd_timeout = 300000;
-	long wr_tm, rd_tm;
-	int num = 0, num1 =0;
-	void *priv = NULL;
-	struct ocf_wr_priv *ocf_wr_priv = NULL;
-	struct ocf_rd_priv *ocf_rd_priv = NULL;
-	int write=0;
-	struct crypt_cpu *this_cc = this_crypt_config(cc);
-
-	atomic_set(&ctx->pending, 1);
-
-
-	if(bio_data_dir(ctx->bio_in) == WRITE) {
-		write = 1;
-		ocf_wr_priv = kmalloc(sizeof(struct ocf_wr_priv),GFP_KERNEL);
-		if(!ocf_wr_priv) {
-			printk("ocf_crypt_convert: out of memory \n");
-			return -ENOMEM;
-		}
-		ocf_wr_priv->dm_ocf_wr_pending = 0;
-		ocf_wr_priv->dm_ocf_wr_completed = 0;
-		init_waitqueue_head(&ocf_wr_priv->dm_ocf_wr_queue);
-		priv = ocf_wr_priv;
-	}
-	else {
-		ocf_rd_priv = kmalloc(sizeof(struct ocf_rd_priv),GFP_KERNEL);
-		if(!ocf_rd_priv) {
-			printk("ocf_crypt_convert: out of memory \n");
-			return -ENOMEM;
-		}
-		ocf_rd_priv->dm_ocf_rd_pending = 0;
-		ocf_rd_priv->dm_ocf_rd_completed = 0;
-		init_waitqueue_head(&ocf_rd_priv->dm_ocf_rd_queue);
-		priv = ocf_rd_priv;
-	}
-
-	while(ctx->idx_in < ctx->bio_in->bi_vcnt &&
-	      ctx->idx_out < ctx->bio_out->bi_vcnt) {
-
-		crypt_alloc_req(cc, ctx);
-
-		atomic_inc(&ctx->pending);
-
-		if(bio_data_dir(ctx->bio_in) == WRITE) {
-			num++;
-		}
-		/* if last read in the context - send the io, so the OCF read callback will release the IO. */
-		else {
-			num1++;
-		}
-
-		r = ocf_crypt_convert_block(cc, ctx, this_cc->req, write, priv);
-
-		switch (r) {
-			case 0:
-				atomic_dec(&ctx->pending);
-				ctx->sector++;
-				//cond_resched();
-				continue;
-			/* error */
-			default:
-				atomic_dec(&ctx->pending);
-				//return r;
-		}
-	}
-
-	if(bio_data_dir(ctx->bio_in) == WRITE) {
-		ocf_wr_priv->dm_ocf_wr_pending += num;
-		wr_tm = wait_event_timeout(ocf_wr_priv->dm_ocf_wr_queue,
-					(ocf_wr_priv->dm_ocf_wr_pending == ocf_wr_priv->dm_ocf_wr_completed)
-										, msecs_to_jiffies(wr_timeout) );
-		if (!wr_tm) {
-			printk("ocf_crypt_convert: wr work was not finished in %ld msecs, %d pending %d completed.\n",
-					wr_timeout, ocf_wr_priv->dm_ocf_wr_pending, ocf_wr_priv->dm_ocf_wr_completed);
-		}
-		kfree(ocf_wr_priv);
-	}
-	else {
-		ocf_rd_priv->dm_ocf_rd_pending += num1;
-		rd_tm = wait_event_timeout(ocf_rd_priv->dm_ocf_rd_queue,
-					(ocf_rd_priv->dm_ocf_rd_pending == ocf_rd_priv->dm_ocf_rd_completed)
-										, msecs_to_jiffies(rd_timeout) );
-		if (!rd_tm) {
-			printk("ocf_crypt_convert: rd work was not finished in %ld msecs, %d pending %d completed.\n",
-					rd_timeout, ocf_rd_priv->dm_ocf_rd_pending, ocf_rd_priv->dm_ocf_rd_completed);
-		}
-		kfree(ocf_rd_priv);
-	}
-
-	return r;
-}
-#endif /*CONFIG_OCF_DM_CRYPT*/
-
 static int crypt_convert_block(struct crypt_config *cc,
 			       struct convert_context *ctx,
 			       struct ablkcipher_request *req)
@@ -1040,19 +749,15 @@
 			    struct convert_context *ctx)
 {
 	struct crypt_cpu *this_cc = this_crypt_config(cc);
-#if !defined(CONFIG_OCF_DM_CRYPT)
 	unsigned key_index = ctx->sector & (cc->tfms_count - 1);
-#endif
 
 	if (!this_cc->req)
 		this_cc->req = mempool_alloc(cc->req_pool, GFP_NOIO);
 
-#if !defined(CONFIG_OCF_DM_CRYPT)
 	ablkcipher_request_set_tfm(this_cc->req, this_cc->tfms[key_index]);
 	ablkcipher_request_set_callback(this_cc->req,
 	    CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
 	    kcryptd_async_done, dmreq_of_req(cc, this_cc->req));
-#endif
 }
 
 /*
@@ -1397,18 +1102,10 @@
 		sector += bio_sectors(clone);
 
 		crypt_inc_pending(io);
-#if defined(CONFIG_OCF_DM_CRYPT)
-		r = ocf_crypt_convert(cc, &io->ctx, io);
 
-		if(r < 0) {
-			printk("\n%s() ocf_crypt_convert failed\n",__FUNCTION__);
-			io->error = -EIO;
-		}
-#else
 		r = crypt_convert(cc, &io->ctx);
 		if (r < 0)
 			io->error = -EIO;
-#endif
 
 		crypt_finished = atomic_dec_and_test(&io->ctx.pending);
 
@@ -1480,17 +1177,9 @@
 	crypt_convert_init(cc, &io->ctx, io->base_bio, io->base_bio,
 			   io->sector);
 
-#if defined(CONFIG_OCF_DM_CRYPT)
-	r = ocf_crypt_convert(cc, &io->ctx, io);
-	if(r < 0) {
-		printk("\n%s() ocf_crypt_convert failed\n",__FUNCTION__);
-		io->error = -EIO;
-	}
-#else
 	r = crypt_convert(cc, &io->ctx);
 	if (r < 0)
 		io->error = -EIO;
-#endif
 
 	if (atomic_dec_and_test(&io->ctx.pending))
 		kcryptd_crypt_read_done(io);
@@ -1573,20 +1262,6 @@
 	return 0;
 }
 
-/*
- * Encode key into its hex representation
- */
-static void crypt_encode_key(char *hex, u8 *key, unsigned int size)
-{
-	unsigned int i;
-
-	for (i = 0; i < size; i++) {
-		sprintf(hex, "%02x", *key);
-		hex += 2;
-		key++;
-	}
-}
-
 static void crypt_free_tfms(struct crypt_config *cc, int cpu)
 {
 	struct crypt_cpu *cpu_cc = per_cpu_ptr(cc->cpu, cpu);
@@ -1652,11 +1327,7 @@
 
 	set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
 
-#if !defined(CONFIG_OCF_DM_CRYPT)
 	r = crypt_setkey_allcpus(cc);
-#else
-	r = 0;
-#endif
 
 out:
 	/* Hex key string not needed after here, so wipe it. */
@@ -1670,11 +1341,7 @@
 	clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
 	memset(&cc->key, 0, cc->key_size * sizeof(u8));
 
-#if !defined(CONFIG_OCF_DM_CRYPT)
 	return crypt_setkey_allcpus(cc);
-#else
-	return 0;
-#endif
 }
 
 static void crypt_dtr(struct dm_target *ti)
@@ -1698,14 +1365,8 @@
 			cpu_cc = per_cpu_ptr(cc->cpu, cpu);
 			if (cpu_cc->req)
 				mempool_free(cpu_cc->req, cc->req_pool);
-#if !defined(CONFIG_OCF_DM_CRYPT)
 			crypt_free_tfms(cc, cpu);
-#endif
 		}
-#if defined(CONFIG_OCF_DM_CRYPT)
-	if(cc->ocf_cryptoid)
-		crypto_freesession(cc->ocf_cryptoid);
-#endif
 
 	if (cc->bs)
 		bioset_free(cc->bs);
@@ -1739,11 +1400,8 @@
 	struct crypt_config *cc = ti->private;
 	char *tmp, *cipher, *chainmode, *ivmode, *ivopts, *keycount;
 	char *cipher_api = NULL;
-#if !defined(CONFIG_OCF_DM_CRYPT)
 	int cpu, ret = -EINVAL;
-#else
-	int ret = -EINVAL;
-#endif
+	char dummy;
 
 	/* Convert to crypto api definition? */
 	if (strchr(cipher_in, '(')) {
@@ -1765,7 +1423,7 @@
 
 	if (!keycount)
 		cc->tfms_count = 1;
-	else if (sscanf(keycount, "%u", &cc->tfms_count) != 1 ||
+	else if (sscanf(keycount, "%u%c", &cc->tfms_count, &dummy) != 1 ||
 		 !is_power_of_2(cc->tfms_count)) {
 		ti->error = "Bad cipher key count specification";
 		return -EINVAL;
@@ -1816,40 +1474,6 @@
 		goto bad_mem;
 	}
 
-	/* Initialize and set key */
-	ret = crypt_set_key(cc, key);
-	if (ret < 0) {
-		ti->error = "Error decoding and setting key";
-		goto bad;
-	}
-
-#if defined(CONFIG_OCF_DM_CRYPT)
-	/* prepare a new OCF session */
-	memset(&cc->cr_dm, 0, sizeof(struct cryptoini));
-
-	if((strcmp(cipher,"aes") == 0) && (strcmp(chainmode, "cbc") == 0))
-		cc->cr_dm.cri_alg  = CRYPTO_AES_CBC;
-	else if((strcmp(cipher,"des") == 0) && (strcmp(chainmode, "cbc") == 0))
-		cc->cr_dm.cri_alg  = CRYPTO_DES_CBC;
-	else if((strcmp(cipher,"des3_ede") == 0) && (strcmp(chainmode, "cbc") == 0))
-		cc->cr_dm.cri_alg  = CRYPTO_3DES_CBC;
-	else {
-		dmprintk("\ncrypt_ctr: using OCF: unknown cipher or bad chain mode\n");
-		ti->error = DM_MSG_PREFIX "using OCF: unknown cipher or bad chain mode";
-		goto bad;
-	}
-
-	//dmprintk("key size is %d\n",cc->key_size);
-	cc->cr_dm.cri_klen = cc->key_size*8;
-	cc->cr_dm.cri_key  = cc->key;
-	cc->cr_dm.cri_next = NULL;
-
-	if(crypto_newsession(&cc->ocf_cryptoid, &cc->cr_dm, CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE)){
-		dmprintk("crypt_ctr: crypto_newsession failed\n");
-		ti->error = DM_MSG_PREFIX "crypto_newsession failed";
-		goto bad;
-	}
-#else
 	/* Allocate cipher */
 	for_each_possible_cpu(cpu) {
 		ret = crypt_alloc_tfms(cc, cpu, cipher_api);
@@ -1859,6 +1483,13 @@
 		}
 	}
 
+	/* Initialize and set key */
+	ret = crypt_set_key(cc, key);
+	if (ret < 0) {
+		ti->error = "Error decoding and setting key";
+		goto bad;
+	}
+
 	/* Initialize IV */
 	cc->iv_size = crypto_ablkcipher_ivsize(any_tfm(cc));
 	if (cc->iv_size)
@@ -1869,7 +1500,6 @@
 		DMWARN("Selected cipher does not support IVs");
 		ivmode = NULL;
 	}
-#endif/*CONFIG_OCF_DM_CRYPT*/
 
 	/* Choose ivmode, see comments at iv code. */
 	if (ivmode == NULL)
@@ -1880,10 +1510,8 @@
 		cc->iv_gen_ops = &crypt_iv_plain64_ops;
 	else if (strcmp(ivmode, "essiv") == 0)
 		cc->iv_gen_ops = &crypt_iv_essiv_ops;
-#if !defined(CONFIG_OCF_DM_CRYPT)
 	else if (strcmp(ivmode, "benbi") == 0)
 		cc->iv_gen_ops = &crypt_iv_benbi_ops;
-#endif
 	else if (strcmp(ivmode, "null") == 0)
 		cc->iv_gen_ops = &crypt_iv_null_ops;
 	else if (strcmp(ivmode, "lmk") == 0) {
@@ -1900,17 +1528,6 @@
 		goto bad;
 	}
 
-#if defined(CONFIG_OCF_DM_CRYPT)
-	switch (cc->cr_dm.cri_alg) {
-		case CRYPTO_AES_CBC:
-			cc->iv_size = 16;
-			break;
-		default:
-			cc->iv_size = 8;
-			break;
-	}
-#endif
-
 	/* Allocate IV */
 	if (cc->iv_gen_ops && cc->iv_gen_ops->ctr) {
 		ret = cc->iv_gen_ops->ctr(cc, ti, ivopts);
@@ -1920,7 +1537,6 @@
 		}
 	}
 
-#if !defined(CONFIG_OCF_DM_CRYPT)
 	/* Initialize IV (set keys for ESSIV etc) */
 	if (cc->iv_gen_ops && cc->iv_gen_ops->init) {
 		ret = cc->iv_gen_ops->init(cc);
@@ -1929,7 +1545,6 @@
 			goto bad;
 		}
 	}
-#endif
 
 	ret = 0;
 bad:
@@ -1951,8 +1566,10 @@
 	unsigned int key_size, opt_params;
 	unsigned long long tmpll;
 	int ret;
+	size_t iv_size_padding;
 	struct dm_arg_set as;
 	const char *opt_string;
+	char dummy;
 
 	static struct dm_arg _args[] = {
 		{0, 1, "Invalid number of feature args"},
@@ -1985,15 +1602,24 @@
 	}
 
 	cc->dmreq_start = sizeof(struct ablkcipher_request);
-#if !defined(CONFIG_OCF_DM_CRYPT)
 	cc->dmreq_start += crypto_ablkcipher_reqsize(any_tfm(cc));
-	cc->dmreq_start = ALIGN(cc->dmreq_start, crypto_tfm_ctx_alignment());
-	cc->dmreq_start += crypto_ablkcipher_alignmask(any_tfm(cc)) &
-			   ~(crypto_tfm_ctx_alignment() - 1);
-#endif
+	cc->dmreq_start = ALIGN(cc->dmreq_start, __alignof__(struct dm_crypt_request));
+
+	if (crypto_ablkcipher_alignmask(any_tfm(cc)) < CRYPTO_MINALIGN) {
+		/* Allocate the padding exactly */
+		iv_size_padding = -(cc->dmreq_start + sizeof(struct dm_crypt_request))
+				& crypto_ablkcipher_alignmask(any_tfm(cc));
+	} else {
+		/*
+		 * If the cipher requires greater alignment than kmalloc
+		 * alignment, we don't know the exact position of the
+		 * initialization vector. We must assume worst case.
+		 */
+		iv_size_padding = crypto_ablkcipher_alignmask(any_tfm(cc));
+	}
 
 	cc->req_pool = mempool_create_kmalloc_pool(MIN_IOS, cc->dmreq_start +
-			sizeof(struct dm_crypt_request) + cc->iv_size);
+			sizeof(struct dm_crypt_request) + iv_size_padding + cc->iv_size);
 	if (!cc->req_pool) {
 		ti->error = "Cannot allocate crypt request mempool";
 		goto bad;
@@ -2012,7 +1638,7 @@
 	}
 
 	ret = -EINVAL;
-	if (sscanf(argv[2], "%llu", &tmpll) != 1) {
+	if (sscanf(argv[2], "%llu%c", &tmpll, &dummy) != 1) {
 		ti->error = "Invalid iv_offset sector";
 		goto bad;
 	}
@@ -2023,7 +1649,7 @@
 		goto bad;
 	}
 
-	if (sscanf(argv[4], "%llu", &tmpll) != 1) {
+	if (sscanf(argv[4], "%llu%c", &tmpll, &dummy) != 1) {
 		ti->error = "Invalid device sector";
 		goto bad;
 	}
@@ -2113,11 +1739,11 @@
 	return DM_MAPIO_SUBMITTED;
 }
 
-static int crypt_status(struct dm_target *ti, status_type_t type,
-			char *result, unsigned int maxlen)
+static void crypt_status(struct dm_target *ti, status_type_t type,
+			 char *result, unsigned int maxlen)
 {
 	struct crypt_config *cc = ti->private;
-	unsigned int sz = 0;
+	unsigned i, sz = 0;
 
 	switch (type) {
 	case STATUSTYPE_INFO:
@@ -2127,17 +1753,11 @@
 	case STATUSTYPE_TABLE:
 		DMEMIT("%s ", cc->cipher_string);
 
-		if (cc->key_size > 0) {
-			if ((maxlen - sz) < ((cc->key_size << 1) + 1))
-				return -ENOMEM;
-
-			crypt_encode_key(result + sz, cc->key, cc->key_size);
-			sz += cc->key_size << 1;
-		} else {
-			if (sz >= maxlen)
-				return -ENOMEM;
-			result[sz++] = '-';
-		}
+		if (cc->key_size > 0)
+			for (i = 0; i < cc->key_size; i++)
+				DMEMIT("%02x", cc->key[i]);
+		else
+			DMEMIT("-");
 
 		DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset,
 				cc->dev->name, (unsigned long long)cc->start);
@@ -2147,7 +1767,6 @@
 
 		break;
 	}
-	return 0;
 }
 
 static void crypt_postsuspend(struct dm_target *ti)
@@ -2269,10 +1888,6 @@
 		kmem_cache_destroy(_crypt_io_pool);
 	}
 
-#ifdef CONFIG_OCF_DM_CRYPT
-	printk("dm_crypt using the OCF for crypto acceleration.\n");
-#endif
-
 	return r;
 }