xref: /openbmc/linux/drivers/crypto/allwinner/sun4i-ss/sun4i-ss-hash.c (revision 2612e3bbc0386368a850140a6c9b990cd496a5ec)
117513547SCorentin Labbe // SPDX-License-Identifier: GPL-2.0-or-later
217513547SCorentin Labbe /*
317513547SCorentin Labbe  * sun4i-ss-hash.c - hardware cryptographic accelerator for Allwinner A20 SoC
417513547SCorentin Labbe  *
517513547SCorentin Labbe  * Copyright (C) 2013-2015 Corentin LABBE <clabbe.montjoie@gmail.com>
617513547SCorentin Labbe  *
717513547SCorentin Labbe  * This file add support for MD5 and SHA1.
817513547SCorentin Labbe  *
9*39db3f15SJonathan Corbet  * You could find the datasheet in Documentation/arch/arm/sunxi.rst
1017513547SCorentin Labbe  */
1117513547SCorentin Labbe #include "sun4i-ss.h"
129b20cbf8SHerbert Xu #include <asm/unaligned.h>
1317513547SCorentin Labbe #include <linux/scatterlist.h>
1417513547SCorentin Labbe 
1517513547SCorentin Labbe /* This is a totally arbitrary value */
1617513547SCorentin Labbe #define SS_TIMEOUT 100
1717513547SCorentin Labbe 
sun4i_hash_crainit(struct crypto_tfm * tfm)1817513547SCorentin Labbe int sun4i_hash_crainit(struct crypto_tfm *tfm)
1917513547SCorentin Labbe {
2017513547SCorentin Labbe 	struct sun4i_tfm_ctx *op = crypto_tfm_ctx(tfm);
2117513547SCorentin Labbe 	struct ahash_alg *alg = __crypto_ahash_alg(tfm->__crt_alg);
2217513547SCorentin Labbe 	struct sun4i_ss_alg_template *algt;
2317513547SCorentin Labbe 	int err;
2417513547SCorentin Labbe 
2517513547SCorentin Labbe 	memset(op, 0, sizeof(struct sun4i_tfm_ctx));
2617513547SCorentin Labbe 
2717513547SCorentin Labbe 	algt = container_of(alg, struct sun4i_ss_alg_template, alg.hash);
2817513547SCorentin Labbe 	op->ss = algt->ss;
2917513547SCorentin Labbe 
30ac98fc5eSShixin Liu 	err = pm_runtime_resume_and_get(op->ss->dev);
3117513547SCorentin Labbe 	if (err < 0)
3217513547SCorentin Labbe 		return err;
3317513547SCorentin Labbe 
3417513547SCorentin Labbe 	crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
3517513547SCorentin Labbe 				 sizeof(struct sun4i_req_ctx));
3617513547SCorentin Labbe 	return 0;
3717513547SCorentin Labbe }
3817513547SCorentin Labbe 
sun4i_hash_craexit(struct crypto_tfm * tfm)3917513547SCorentin Labbe void sun4i_hash_craexit(struct crypto_tfm *tfm)
4017513547SCorentin Labbe {
4117513547SCorentin Labbe 	struct sun4i_tfm_ctx *op = crypto_tfm_ctx(tfm);
4217513547SCorentin Labbe 
4317513547SCorentin Labbe 	pm_runtime_put(op->ss->dev);
4417513547SCorentin Labbe }
4517513547SCorentin Labbe 
4617513547SCorentin Labbe /* sun4i_hash_init: initialize request context */
sun4i_hash_init(struct ahash_request * areq)4717513547SCorentin Labbe int sun4i_hash_init(struct ahash_request *areq)
4817513547SCorentin Labbe {
4917513547SCorentin Labbe 	struct sun4i_req_ctx *op = ahash_request_ctx(areq);
5017513547SCorentin Labbe 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
5117513547SCorentin Labbe 	struct ahash_alg *alg = __crypto_ahash_alg(tfm->base.__crt_alg);
5217513547SCorentin Labbe 	struct sun4i_ss_alg_template *algt;
5317513547SCorentin Labbe 
5417513547SCorentin Labbe 	memset(op, 0, sizeof(struct sun4i_req_ctx));
5517513547SCorentin Labbe 
5617513547SCorentin Labbe 	algt = container_of(alg, struct sun4i_ss_alg_template, alg.hash);
5717513547SCorentin Labbe 	op->mode = algt->mode;
5817513547SCorentin Labbe 
5917513547SCorentin Labbe 	return 0;
6017513547SCorentin Labbe }
6117513547SCorentin Labbe 
sun4i_hash_export_md5(struct ahash_request * areq,void * out)6217513547SCorentin Labbe int sun4i_hash_export_md5(struct ahash_request *areq, void *out)
6317513547SCorentin Labbe {
6417513547SCorentin Labbe 	struct sun4i_req_ctx *op = ahash_request_ctx(areq);
6517513547SCorentin Labbe 	struct md5_state *octx = out;
6617513547SCorentin Labbe 	int i;
6717513547SCorentin Labbe 
6817513547SCorentin Labbe 	octx->byte_count = op->byte_count + op->len;
6917513547SCorentin Labbe 
7017513547SCorentin Labbe 	memcpy(octx->block, op->buf, op->len);
7117513547SCorentin Labbe 
7217513547SCorentin Labbe 	if (op->byte_count) {
7317513547SCorentin Labbe 		for (i = 0; i < 4; i++)
7417513547SCorentin Labbe 			octx->hash[i] = op->hash[i];
7517513547SCorentin Labbe 	} else {
7617513547SCorentin Labbe 		octx->hash[0] = SHA1_H0;
7717513547SCorentin Labbe 		octx->hash[1] = SHA1_H1;
7817513547SCorentin Labbe 		octx->hash[2] = SHA1_H2;
7917513547SCorentin Labbe 		octx->hash[3] = SHA1_H3;
8017513547SCorentin Labbe 	}
8117513547SCorentin Labbe 
8217513547SCorentin Labbe 	return 0;
8317513547SCorentin Labbe }
8417513547SCorentin Labbe 
sun4i_hash_import_md5(struct ahash_request * areq,const void * in)8517513547SCorentin Labbe int sun4i_hash_import_md5(struct ahash_request *areq, const void *in)
8617513547SCorentin Labbe {
8717513547SCorentin Labbe 	struct sun4i_req_ctx *op = ahash_request_ctx(areq);
8817513547SCorentin Labbe 	const struct md5_state *ictx = in;
8917513547SCorentin Labbe 	int i;
9017513547SCorentin Labbe 
9117513547SCorentin Labbe 	sun4i_hash_init(areq);
9217513547SCorentin Labbe 
9317513547SCorentin Labbe 	op->byte_count = ictx->byte_count & ~0x3F;
9417513547SCorentin Labbe 	op->len = ictx->byte_count & 0x3F;
9517513547SCorentin Labbe 
9617513547SCorentin Labbe 	memcpy(op->buf, ictx->block, op->len);
9717513547SCorentin Labbe 
9817513547SCorentin Labbe 	for (i = 0; i < 4; i++)
9917513547SCorentin Labbe 		op->hash[i] = ictx->hash[i];
10017513547SCorentin Labbe 
10117513547SCorentin Labbe 	return 0;
10217513547SCorentin Labbe }
10317513547SCorentin Labbe 
sun4i_hash_export_sha1(struct ahash_request * areq,void * out)10417513547SCorentin Labbe int sun4i_hash_export_sha1(struct ahash_request *areq, void *out)
10517513547SCorentin Labbe {
10617513547SCorentin Labbe 	struct sun4i_req_ctx *op = ahash_request_ctx(areq);
10717513547SCorentin Labbe 	struct sha1_state *octx = out;
10817513547SCorentin Labbe 	int i;
10917513547SCorentin Labbe 
11017513547SCorentin Labbe 	octx->count = op->byte_count + op->len;
11117513547SCorentin Labbe 
11217513547SCorentin Labbe 	memcpy(octx->buffer, op->buf, op->len);
11317513547SCorentin Labbe 
11417513547SCorentin Labbe 	if (op->byte_count) {
11517513547SCorentin Labbe 		for (i = 0; i < 5; i++)
11617513547SCorentin Labbe 			octx->state[i] = op->hash[i];
11717513547SCorentin Labbe 	} else {
11817513547SCorentin Labbe 		octx->state[0] = SHA1_H0;
11917513547SCorentin Labbe 		octx->state[1] = SHA1_H1;
12017513547SCorentin Labbe 		octx->state[2] = SHA1_H2;
12117513547SCorentin Labbe 		octx->state[3] = SHA1_H3;
12217513547SCorentin Labbe 		octx->state[4] = SHA1_H4;
12317513547SCorentin Labbe 	}
12417513547SCorentin Labbe 
12517513547SCorentin Labbe 	return 0;
12617513547SCorentin Labbe }
12717513547SCorentin Labbe 
sun4i_hash_import_sha1(struct ahash_request * areq,const void * in)12817513547SCorentin Labbe int sun4i_hash_import_sha1(struct ahash_request *areq, const void *in)
12917513547SCorentin Labbe {
13017513547SCorentin Labbe 	struct sun4i_req_ctx *op = ahash_request_ctx(areq);
13117513547SCorentin Labbe 	const struct sha1_state *ictx = in;
13217513547SCorentin Labbe 	int i;
13317513547SCorentin Labbe 
13417513547SCorentin Labbe 	sun4i_hash_init(areq);
13517513547SCorentin Labbe 
13617513547SCorentin Labbe 	op->byte_count = ictx->count & ~0x3F;
13717513547SCorentin Labbe 	op->len = ictx->count & 0x3F;
13817513547SCorentin Labbe 
13917513547SCorentin Labbe 	memcpy(op->buf, ictx->buffer, op->len);
14017513547SCorentin Labbe 
14117513547SCorentin Labbe 	for (i = 0; i < 5; i++)
14217513547SCorentin Labbe 		op->hash[i] = ictx->state[i];
14317513547SCorentin Labbe 
14417513547SCorentin Labbe 	return 0;
14517513547SCorentin Labbe }
14617513547SCorentin Labbe 
14717513547SCorentin Labbe #define SS_HASH_UPDATE 1
14817513547SCorentin Labbe #define SS_HASH_FINAL 2
14917513547SCorentin Labbe 
15017513547SCorentin Labbe /*
15117513547SCorentin Labbe  * sun4i_hash_update: update hash engine
15217513547SCorentin Labbe  *
15317513547SCorentin Labbe  * Could be used for both SHA1 and MD5
15417513547SCorentin Labbe  * Write data by step of 32bits and put then in the SS.
15517513547SCorentin Labbe  *
15617513547SCorentin Labbe  * Since we cannot leave partial data and hash state in the engine,
15717513547SCorentin Labbe  * we need to get the hash state at the end of this function.
15817513547SCorentin Labbe  * We can get the hash state every 64 bytes
15917513547SCorentin Labbe  *
16017513547SCorentin Labbe  * So the first work is to get the number of bytes to write to SS modulo 64
16117513547SCorentin Labbe  * The extra bytes will go to a temporary buffer op->buf storing op->len bytes
16217513547SCorentin Labbe  *
16317513547SCorentin Labbe  * So at the begin of update()
16417513547SCorentin Labbe  * if op->len + areq->nbytes < 64
16517513547SCorentin Labbe  * => all data will be written to wait buffer (op->buf) and end=0
16617513547SCorentin Labbe  * if not, write all data from op->buf to the device and position end to
16717513547SCorentin Labbe  * complete to 64bytes
16817513547SCorentin Labbe  *
16917513547SCorentin Labbe  * example 1:
17017513547SCorentin Labbe  * update1 60o => op->len=60
17117513547SCorentin Labbe  * update2 60o => need one more word to have 64 bytes
17217513547SCorentin Labbe  * end=4
17317513547SCorentin Labbe  * so write all data from op->buf and one word of SGs
17417513547SCorentin Labbe  * write remaining data in op->buf
17517513547SCorentin Labbe  * final state op->len=56
17617513547SCorentin Labbe  */
sun4i_hash(struct ahash_request * areq)17717513547SCorentin Labbe static int sun4i_hash(struct ahash_request *areq)
17817513547SCorentin Labbe {
17917513547SCorentin Labbe 	/*
18017513547SCorentin Labbe 	 * i is the total bytes read from SGs, to be compared to areq->nbytes
18117513547SCorentin Labbe 	 * i is important because we cannot rely on SG length since the sum of
18217513547SCorentin Labbe 	 * SG->length could be greater than areq->nbytes
18317513547SCorentin Labbe 	 *
18417513547SCorentin Labbe 	 * end is the position when we need to stop writing to the device,
18517513547SCorentin Labbe 	 * to be compared to i
18617513547SCorentin Labbe 	 *
18717513547SCorentin Labbe 	 * in_i: advancement in the current SG
18817513547SCorentin Labbe 	 */
18917513547SCorentin Labbe 	unsigned int i = 0, end, fill, min_fill, nwait, nbw = 0, j = 0, todo;
19017513547SCorentin Labbe 	unsigned int in_i = 0;
191d1d787bcSCorentin Labbe 	u32 spaces, rx_cnt = SS_RX_DEFAULT, bf[32] = {0}, v, ivmode = 0;
19217513547SCorentin Labbe 	struct sun4i_req_ctx *op = ahash_request_ctx(areq);
19317513547SCorentin Labbe 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
194b1f578b8SCorentin Labbe 	struct ahash_alg *alg = __crypto_ahash_alg(tfm->base.__crt_alg);
19517513547SCorentin Labbe 	struct sun4i_tfm_ctx *tfmctx = crypto_ahash_ctx(tfm);
19617513547SCorentin Labbe 	struct sun4i_ss_ctx *ss = tfmctx->ss;
197b1f578b8SCorentin Labbe 	struct sun4i_ss_alg_template *algt;
19817513547SCorentin Labbe 	struct scatterlist *in_sg = areq->src;
19917513547SCorentin Labbe 	struct sg_mapping_iter mi;
20017513547SCorentin Labbe 	int in_r, err = 0;
20117513547SCorentin Labbe 	size_t copied = 0;
2029b20cbf8SHerbert Xu 	u32 wb = 0;
20317513547SCorentin Labbe 
20417513547SCorentin Labbe 	dev_dbg(ss->dev, "%s %s bc=%llu len=%u mode=%x wl=%u h0=%0x",
20517513547SCorentin Labbe 		__func__, crypto_tfm_alg_name(areq->base.tfm),
20617513547SCorentin Labbe 		op->byte_count, areq->nbytes, op->mode,
20717513547SCorentin Labbe 		op->len, op->hash[0]);
20817513547SCorentin Labbe 
20917513547SCorentin Labbe 	if (unlikely(!areq->nbytes) && !(op->flags & SS_HASH_FINAL))
21017513547SCorentin Labbe 		return 0;
21117513547SCorentin Labbe 
21217513547SCorentin Labbe 	/* protect against overflow */
21317513547SCorentin Labbe 	if (unlikely(areq->nbytes > UINT_MAX - op->len)) {
21417513547SCorentin Labbe 		dev_err(ss->dev, "Cannot process too large request\n");
21517513547SCorentin Labbe 		return -EINVAL;
21617513547SCorentin Labbe 	}
21717513547SCorentin Labbe 
21817513547SCorentin Labbe 	if (op->len + areq->nbytes < 64 && !(op->flags & SS_HASH_FINAL)) {
21917513547SCorentin Labbe 		/* linearize data to op->buf */
22017513547SCorentin Labbe 		copied = sg_pcopy_to_buffer(areq->src, sg_nents(areq->src),
22117513547SCorentin Labbe 					    op->buf + op->len, areq->nbytes, 0);
22217513547SCorentin Labbe 		op->len += copied;
22317513547SCorentin Labbe 		return 0;
22417513547SCorentin Labbe 	}
22517513547SCorentin Labbe 
22617513547SCorentin Labbe 	spin_lock_bh(&ss->slock);
22717513547SCorentin Labbe 
22817513547SCorentin Labbe 	/*
22917513547SCorentin Labbe 	 * if some data have been processed before,
23017513547SCorentin Labbe 	 * we need to restore the partial hash state
23117513547SCorentin Labbe 	 */
23217513547SCorentin Labbe 	if (op->byte_count) {
23317513547SCorentin Labbe 		ivmode = SS_IV_ARBITRARY;
234626abd31SCorentin Labbe 		for (i = 0; i < crypto_ahash_digestsize(tfm) / 4; i++)
23517513547SCorentin Labbe 			writel(op->hash[i], ss->base + SS_IV0 + i * 4);
23617513547SCorentin Labbe 	}
23717513547SCorentin Labbe 	/* Enable the device */
23817513547SCorentin Labbe 	writel(op->mode | SS_ENABLED | ivmode, ss->base + SS_CTL);
23917513547SCorentin Labbe 
24017513547SCorentin Labbe 	if (!(op->flags & SS_HASH_UPDATE))
24117513547SCorentin Labbe 		goto hash_final;
24217513547SCorentin Labbe 
24317513547SCorentin Labbe 	/* start of handling data */
24417513547SCorentin Labbe 	if (!(op->flags & SS_HASH_FINAL)) {
24517513547SCorentin Labbe 		end = ((areq->nbytes + op->len) / 64) * 64 - op->len;
24617513547SCorentin Labbe 
24717513547SCorentin Labbe 		if (end > areq->nbytes || areq->nbytes - end > 63) {
24817513547SCorentin Labbe 			dev_err(ss->dev, "ERROR: Bound error %u %u\n",
24917513547SCorentin Labbe 				end, areq->nbytes);
25017513547SCorentin Labbe 			err = -EINVAL;
25117513547SCorentin Labbe 			goto release_ss;
25217513547SCorentin Labbe 		}
25317513547SCorentin Labbe 	} else {
25417513547SCorentin Labbe 		/* Since we have the flag final, we can go up to modulo 4 */
25517513547SCorentin Labbe 		if (areq->nbytes < 4)
25617513547SCorentin Labbe 			end = 0;
25717513547SCorentin Labbe 		else
25817513547SCorentin Labbe 			end = ((areq->nbytes + op->len) / 4) * 4 - op->len;
25917513547SCorentin Labbe 	}
26017513547SCorentin Labbe 
26117513547SCorentin Labbe 	/* TODO if SGlen % 4 and !op->len then DMA */
26217513547SCorentin Labbe 	i = 1;
26317513547SCorentin Labbe 	while (in_sg && i == 1) {
26417513547SCorentin Labbe 		if (in_sg->length % 4)
26517513547SCorentin Labbe 			i = 0;
26617513547SCorentin Labbe 		in_sg = sg_next(in_sg);
26717513547SCorentin Labbe 	}
26817513547SCorentin Labbe 	if (i == 1 && !op->len && areq->nbytes)
26917513547SCorentin Labbe 		dev_dbg(ss->dev, "We can DMA\n");
27017513547SCorentin Labbe 
27117513547SCorentin Labbe 	i = 0;
27217513547SCorentin Labbe 	sg_miter_start(&mi, areq->src, sg_nents(areq->src),
27317513547SCorentin Labbe 		       SG_MITER_FROM_SG | SG_MITER_ATOMIC);
27417513547SCorentin Labbe 	sg_miter_next(&mi);
27517513547SCorentin Labbe 	in_i = 0;
27617513547SCorentin Labbe 
27717513547SCorentin Labbe 	do {
27817513547SCorentin Labbe 		/*
27917513547SCorentin Labbe 		 * we need to linearize in two case:
28017513547SCorentin Labbe 		 * - the buffer is already used
28117513547SCorentin Labbe 		 * - the SG does not have enough byte remaining ( < 4)
28217513547SCorentin Labbe 		 */
28317513547SCorentin Labbe 		if (op->len || (mi.length - in_i) < 4) {
28417513547SCorentin Labbe 			/*
28517513547SCorentin Labbe 			 * if we have entered here we have two reason to stop
28617513547SCorentin Labbe 			 * - the buffer is full
28717513547SCorentin Labbe 			 * - reach the end
28817513547SCorentin Labbe 			 */
28917513547SCorentin Labbe 			while (op->len < 64 && i < end) {
29017513547SCorentin Labbe 				/* how many bytes we can read from current SG */
291a7126603SCorentin Labbe 				in_r = min(end - i, 64 - op->len);
292a7126603SCorentin Labbe 				in_r = min_t(size_t, mi.length - in_i, in_r);
29317513547SCorentin Labbe 				memcpy(op->buf + op->len, mi.addr + in_i, in_r);
29417513547SCorentin Labbe 				op->len += in_r;
29517513547SCorentin Labbe 				i += in_r;
29617513547SCorentin Labbe 				in_i += in_r;
29717513547SCorentin Labbe 				if (in_i == mi.length) {
29817513547SCorentin Labbe 					sg_miter_next(&mi);
29917513547SCorentin Labbe 					in_i = 0;
30017513547SCorentin Labbe 				}
30117513547SCorentin Labbe 			}
30217513547SCorentin Labbe 			if (op->len > 3 && !(op->len % 4)) {
30317513547SCorentin Labbe 				/* write buf to the device */
30417513547SCorentin Labbe 				writesl(ss->base + SS_RXFIFO, op->buf,
30517513547SCorentin Labbe 					op->len / 4);
30617513547SCorentin Labbe 				op->byte_count += op->len;
30717513547SCorentin Labbe 				op->len = 0;
30817513547SCorentin Labbe 			}
30917513547SCorentin Labbe 		}
31017513547SCorentin Labbe 		if (mi.length - in_i > 3 && i < end) {
31117513547SCorentin Labbe 			/* how many bytes we can read from current SG */
312a7126603SCorentin Labbe 			in_r = min_t(size_t, mi.length - in_i, areq->nbytes - i);
313a7126603SCorentin Labbe 			in_r = min_t(size_t, ((mi.length - in_i) / 4) * 4, in_r);
31417513547SCorentin Labbe 			/* how many bytes we can write in the device*/
31517513547SCorentin Labbe 			todo = min3((u32)(end - i) / 4, rx_cnt, (u32)in_r / 4);
31617513547SCorentin Labbe 			writesl(ss->base + SS_RXFIFO, mi.addr + in_i, todo);
31717513547SCorentin Labbe 			op->byte_count += todo * 4;
31817513547SCorentin Labbe 			i += todo * 4;
31917513547SCorentin Labbe 			in_i += todo * 4;
32017513547SCorentin Labbe 			rx_cnt -= todo;
32117513547SCorentin Labbe 			if (!rx_cnt) {
32217513547SCorentin Labbe 				spaces = readl(ss->base + SS_FCSR);
32317513547SCorentin Labbe 				rx_cnt = SS_RXFIFO_SPACES(spaces);
32417513547SCorentin Labbe 			}
32517513547SCorentin Labbe 			if (in_i == mi.length) {
32617513547SCorentin Labbe 				sg_miter_next(&mi);
32717513547SCorentin Labbe 				in_i = 0;
32817513547SCorentin Labbe 			}
32917513547SCorentin Labbe 		}
33017513547SCorentin Labbe 	} while (i < end);
33117513547SCorentin Labbe 
33217513547SCorentin Labbe 	/*
33317513547SCorentin Labbe 	 * Now we have written to the device all that we can,
33417513547SCorentin Labbe 	 * store the remaining bytes in op->buf
33517513547SCorentin Labbe 	 */
33617513547SCorentin Labbe 	if ((areq->nbytes - i) < 64) {
33717513547SCorentin Labbe 		while (i < areq->nbytes && in_i < mi.length && op->len < 64) {
33817513547SCorentin Labbe 			/* how many bytes we can read from current SG */
339a7126603SCorentin Labbe 			in_r = min(areq->nbytes - i, 64 - op->len);
340a7126603SCorentin Labbe 			in_r = min_t(size_t, mi.length - in_i, in_r);
34117513547SCorentin Labbe 			memcpy(op->buf + op->len, mi.addr + in_i, in_r);
34217513547SCorentin Labbe 			op->len += in_r;
34317513547SCorentin Labbe 			i += in_r;
34417513547SCorentin Labbe 			in_i += in_r;
34517513547SCorentin Labbe 			if (in_i == mi.length) {
34617513547SCorentin Labbe 				sg_miter_next(&mi);
34717513547SCorentin Labbe 				in_i = 0;
34817513547SCorentin Labbe 			}
34917513547SCorentin Labbe 		}
35017513547SCorentin Labbe 	}
35117513547SCorentin Labbe 
35217513547SCorentin Labbe 	sg_miter_stop(&mi);
35317513547SCorentin Labbe 
35417513547SCorentin Labbe 	/*
35517513547SCorentin Labbe 	 * End of data process
35617513547SCorentin Labbe 	 * Now if we have the flag final go to finalize part
35717513547SCorentin Labbe 	 * If not, store the partial hash
35817513547SCorentin Labbe 	 */
35917513547SCorentin Labbe 	if (op->flags & SS_HASH_FINAL)
36017513547SCorentin Labbe 		goto hash_final;
36117513547SCorentin Labbe 
36217513547SCorentin Labbe 	writel(op->mode | SS_ENABLED | SS_DATA_END, ss->base + SS_CTL);
36317513547SCorentin Labbe 	i = 0;
36417513547SCorentin Labbe 	do {
36517513547SCorentin Labbe 		v = readl(ss->base + SS_CTL);
36617513547SCorentin Labbe 		i++;
36717513547SCorentin Labbe 	} while (i < SS_TIMEOUT && (v & SS_DATA_END));
36817513547SCorentin Labbe 	if (unlikely(i >= SS_TIMEOUT)) {
36917513547SCorentin Labbe 		dev_err_ratelimited(ss->dev,
37017513547SCorentin Labbe 				    "ERROR: hash end timeout %d>%d ctl=%x len=%u\n",
37117513547SCorentin Labbe 				    i, SS_TIMEOUT, v, areq->nbytes);
37217513547SCorentin Labbe 		err = -EIO;
37317513547SCorentin Labbe 		goto release_ss;
37417513547SCorentin Labbe 	}
37517513547SCorentin Labbe 
37617513547SCorentin Labbe 	/*
37717513547SCorentin Labbe 	 * The datasheet isn't very clear about when to retrieve the digest. The
37817513547SCorentin Labbe 	 * bit SS_DATA_END is cleared when the engine has processed the data and
37917513547SCorentin Labbe 	 * when the digest is computed *but* it doesn't mean the digest is
38017513547SCorentin Labbe 	 * available in the digest registers. Hence the delay to be sure we can
38117513547SCorentin Labbe 	 * read it.
38217513547SCorentin Labbe 	 */
38317513547SCorentin Labbe 	ndelay(1);
38417513547SCorentin Labbe 
38517513547SCorentin Labbe 	for (i = 0; i < crypto_ahash_digestsize(tfm) / 4; i++)
38617513547SCorentin Labbe 		op->hash[i] = readl(ss->base + SS_MD0 + i * 4);
38717513547SCorentin Labbe 
38817513547SCorentin Labbe 	goto release_ss;
38917513547SCorentin Labbe 
39017513547SCorentin Labbe /*
39117513547SCorentin Labbe  * hash_final: finalize hashing operation
39217513547SCorentin Labbe  *
39317513547SCorentin Labbe  * If we have some remaining bytes, we write them.
39417513547SCorentin Labbe  * Then ask the SS for finalizing the hashing operation
39517513547SCorentin Labbe  *
39617513547SCorentin Labbe  * I do not check RX FIFO size in this function since the size is 32
39717513547SCorentin Labbe  * after each enabling and this function neither write more than 32 words.
39817513547SCorentin Labbe  * If we come from the update part, we cannot have more than
39917513547SCorentin Labbe  * 3 remaining bytes to write and SS is fast enough to not care about it.
40017513547SCorentin Labbe  */
40117513547SCorentin Labbe 
40217513547SCorentin Labbe hash_final:
403b1f578b8SCorentin Labbe 	if (IS_ENABLED(CONFIG_CRYPTO_DEV_SUN4I_SS_DEBUG)) {
404b1f578b8SCorentin Labbe 		algt = container_of(alg, struct sun4i_ss_alg_template, alg.hash);
405b1f578b8SCorentin Labbe 		algt->stat_req++;
406b1f578b8SCorentin Labbe 	}
40717513547SCorentin Labbe 
40817513547SCorentin Labbe 	/* write the remaining words of the wait buffer */
40917513547SCorentin Labbe 	if (op->len) {
41017513547SCorentin Labbe 		nwait = op->len / 4;
41117513547SCorentin Labbe 		if (nwait) {
41217513547SCorentin Labbe 			writesl(ss->base + SS_RXFIFO, op->buf, nwait);
41317513547SCorentin Labbe 			op->byte_count += 4 * nwait;
41417513547SCorentin Labbe 		}
41517513547SCorentin Labbe 
41617513547SCorentin Labbe 		nbw = op->len - 4 * nwait;
41717513547SCorentin Labbe 		if (nbw) {
4189b20cbf8SHerbert Xu 			wb = le32_to_cpup((__le32 *)(op->buf + nwait * 4));
41917513547SCorentin Labbe 			wb &= GENMASK((nbw * 8) - 1, 0);
42017513547SCorentin Labbe 
42117513547SCorentin Labbe 			op->byte_count += nbw;
42217513547SCorentin Labbe 		}
42317513547SCorentin Labbe 	}
42417513547SCorentin Labbe 
42517513547SCorentin Labbe 	/* write the remaining bytes of the nbw buffer */
42617513547SCorentin Labbe 	wb |= ((1 << 7) << (nbw * 8));
4279b20cbf8SHerbert Xu 	((__le32 *)bf)[j++] = cpu_to_le32(wb);
42817513547SCorentin Labbe 
42917513547SCorentin Labbe 	/*
43017513547SCorentin Labbe 	 * number of space to pad to obtain 64o minus 8(size) minus 4 (final 1)
43117513547SCorentin Labbe 	 * I take the operations from other MD5/SHA1 implementations
43217513547SCorentin Labbe 	 */
43317513547SCorentin Labbe 
43417513547SCorentin Labbe 	/* last block size */
43517513547SCorentin Labbe 	fill = 64 - (op->byte_count % 64);
43617513547SCorentin Labbe 	min_fill = 2 * sizeof(u32) + (nbw ? 0 : sizeof(u32));
43717513547SCorentin Labbe 
43817513547SCorentin Labbe 	/* if we can't fill all data, jump to the next 64 block */
43917513547SCorentin Labbe 	if (fill < min_fill)
44017513547SCorentin Labbe 		fill += 64;
44117513547SCorentin Labbe 
44217513547SCorentin Labbe 	j += (fill - min_fill) / sizeof(u32);
44317513547SCorentin Labbe 
44417513547SCorentin Labbe 	/* write the length of data */
44517513547SCorentin Labbe 	if (op->mode == SS_OP_SHA1) {
446d1d787bcSCorentin Labbe 		__be64 *bits = (__be64 *)&bf[j];
447d1d787bcSCorentin Labbe 		*bits = cpu_to_be64(op->byte_count << 3);
448d1d787bcSCorentin Labbe 		j += 2;
44917513547SCorentin Labbe 	} else {
450d1d787bcSCorentin Labbe 		__le64 *bits = (__le64 *)&bf[j];
451d1d787bcSCorentin Labbe 		*bits = cpu_to_le64(op->byte_count << 3);
452d1d787bcSCorentin Labbe 		j += 2;
45317513547SCorentin Labbe 	}
45417513547SCorentin Labbe 	writesl(ss->base + SS_RXFIFO, bf, j);
45517513547SCorentin Labbe 
45617513547SCorentin Labbe 	/* Tell the SS to stop the hashing */
45717513547SCorentin Labbe 	writel(op->mode | SS_ENABLED | SS_DATA_END, ss->base + SS_CTL);
45817513547SCorentin Labbe 
45917513547SCorentin Labbe 	/*
46017513547SCorentin Labbe 	 * Wait for SS to finish the hash.
46117513547SCorentin Labbe 	 * The timeout could happen only in case of bad overclocking
46217513547SCorentin Labbe 	 * or driver bug.
46317513547SCorentin Labbe 	 */
46417513547SCorentin Labbe 	i = 0;
46517513547SCorentin Labbe 	do {
46617513547SCorentin Labbe 		v = readl(ss->base + SS_CTL);
46717513547SCorentin Labbe 		i++;
46817513547SCorentin Labbe 	} while (i < SS_TIMEOUT && (v & SS_DATA_END));
46917513547SCorentin Labbe 	if (unlikely(i >= SS_TIMEOUT)) {
47017513547SCorentin Labbe 		dev_err_ratelimited(ss->dev,
47117513547SCorentin Labbe 				    "ERROR: hash end timeout %d>%d ctl=%x len=%u\n",
47217513547SCorentin Labbe 				    i, SS_TIMEOUT, v, areq->nbytes);
47317513547SCorentin Labbe 		err = -EIO;
47417513547SCorentin Labbe 		goto release_ss;
47517513547SCorentin Labbe 	}
47617513547SCorentin Labbe 
47717513547SCorentin Labbe 	/*
47817513547SCorentin Labbe 	 * The datasheet isn't very clear about when to retrieve the digest. The
47917513547SCorentin Labbe 	 * bit SS_DATA_END is cleared when the engine has processed the data and
48017513547SCorentin Labbe 	 * when the digest is computed *but* it doesn't mean the digest is
48117513547SCorentin Labbe 	 * available in the digest registers. Hence the delay to be sure we can
48217513547SCorentin Labbe 	 * read it.
48317513547SCorentin Labbe 	 */
48417513547SCorentin Labbe 	ndelay(1);
48517513547SCorentin Labbe 
48617513547SCorentin Labbe 	/* Get the hash from the device */
48717513547SCorentin Labbe 	if (op->mode == SS_OP_SHA1) {
48817513547SCorentin Labbe 		for (i = 0; i < 5; i++) {
4899b20cbf8SHerbert Xu 			v = readl(ss->base + SS_MD0 + i * 4);
4901e02e6fbSCorentin Labbe 			if (ss->variant->sha1_in_be)
4919b20cbf8SHerbert Xu 				put_unaligned_le32(v, areq->result + i * 4);
4921e02e6fbSCorentin Labbe 			else
4939b20cbf8SHerbert Xu 				put_unaligned_be32(v, areq->result + i * 4);
49417513547SCorentin Labbe 		}
49517513547SCorentin Labbe 	} else {
49617513547SCorentin Labbe 		for (i = 0; i < 4; i++) {
4979b20cbf8SHerbert Xu 			v = readl(ss->base + SS_MD0 + i * 4);
4989b20cbf8SHerbert Xu 			put_unaligned_le32(v, areq->result + i * 4);
49917513547SCorentin Labbe 		}
50017513547SCorentin Labbe 	}
50117513547SCorentin Labbe 
50217513547SCorentin Labbe release_ss:
50317513547SCorentin Labbe 	writel(0, ss->base + SS_CTL);
50417513547SCorentin Labbe 	spin_unlock_bh(&ss->slock);
50517513547SCorentin Labbe 	return err;
50617513547SCorentin Labbe }
50717513547SCorentin Labbe 
sun4i_hash_final(struct ahash_request * areq)50817513547SCorentin Labbe int sun4i_hash_final(struct ahash_request *areq)
50917513547SCorentin Labbe {
51017513547SCorentin Labbe 	struct sun4i_req_ctx *op = ahash_request_ctx(areq);
51117513547SCorentin Labbe 
51217513547SCorentin Labbe 	op->flags = SS_HASH_FINAL;
51317513547SCorentin Labbe 	return sun4i_hash(areq);
51417513547SCorentin Labbe }
51517513547SCorentin Labbe 
sun4i_hash_update(struct ahash_request * areq)51617513547SCorentin Labbe int sun4i_hash_update(struct ahash_request *areq)
51717513547SCorentin Labbe {
51817513547SCorentin Labbe 	struct sun4i_req_ctx *op = ahash_request_ctx(areq);
51917513547SCorentin Labbe 
52017513547SCorentin Labbe 	op->flags = SS_HASH_UPDATE;
52117513547SCorentin Labbe 	return sun4i_hash(areq);
52217513547SCorentin Labbe }
52317513547SCorentin Labbe 
52417513547SCorentin Labbe /* sun4i_hash_finup: finalize hashing operation after an update */
sun4i_hash_finup(struct ahash_request * areq)52517513547SCorentin Labbe int sun4i_hash_finup(struct ahash_request *areq)
52617513547SCorentin Labbe {
52717513547SCorentin Labbe 	struct sun4i_req_ctx *op = ahash_request_ctx(areq);
52817513547SCorentin Labbe 
52917513547SCorentin Labbe 	op->flags = SS_HASH_UPDATE | SS_HASH_FINAL;
53017513547SCorentin Labbe 	return sun4i_hash(areq);
53117513547SCorentin Labbe }
53217513547SCorentin Labbe 
53317513547SCorentin Labbe /* combo of init/update/final functions */
sun4i_hash_digest(struct ahash_request * areq)53417513547SCorentin Labbe int sun4i_hash_digest(struct ahash_request *areq)
53517513547SCorentin Labbe {
53617513547SCorentin Labbe 	int err;
53717513547SCorentin Labbe 	struct sun4i_req_ctx *op = ahash_request_ctx(areq);
53817513547SCorentin Labbe 
53917513547SCorentin Labbe 	err = sun4i_hash_init(areq);
54017513547SCorentin Labbe 	if (err)
54117513547SCorentin Labbe 		return err;
54217513547SCorentin Labbe 
54317513547SCorentin Labbe 	op->flags = SS_HASH_UPDATE | SS_HASH_FINAL;
54417513547SCorentin Labbe 	return sun4i_hash(areq);
54517513547SCorentin Labbe }
546