^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * sun4i-ss-hash.c - hardware cryptographic accelerator for Allwinner A20 SoC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2013-2015 Corentin LABBE <clabbe.montjoie@gmail.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * This file add support for MD5 and SHA1.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * You could find the datasheet in Documentation/arm/sunxi.rst
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include "sun4i-ss.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <asm/unaligned.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/scatterlist.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) /* This is a totally arbitrary value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #define SS_TIMEOUT 100
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) int sun4i_hash_crainit(struct crypto_tfm *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) struct sun4i_tfm_ctx *op = crypto_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) struct ahash_alg *alg = __crypto_ahash_alg(tfm->__crt_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) struct sun4i_ss_alg_template *algt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) memset(op, 0, sizeof(struct sun4i_tfm_ctx));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) algt = container_of(alg, struct sun4i_ss_alg_template, alg.hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) op->ss = algt->ss;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) err = pm_runtime_get_sync(op->ss->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) sizeof(struct sun4i_req_ctx));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) void sun4i_hash_craexit(struct crypto_tfm *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct sun4i_tfm_ctx *op = crypto_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) pm_runtime_put(op->ss->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) /* sun4i_hash_init: initialize request context */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) int sun4i_hash_init(struct ahash_request *areq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct sun4i_req_ctx *op = ahash_request_ctx(areq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) struct ahash_alg *alg = __crypto_ahash_alg(tfm->base.__crt_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) struct sun4i_ss_alg_template *algt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) memset(op, 0, sizeof(struct sun4i_req_ctx));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) algt = container_of(alg, struct sun4i_ss_alg_template, alg.hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) op->mode = algt->mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) int sun4i_hash_export_md5(struct ahash_request *areq, void *out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) struct sun4i_req_ctx *op = ahash_request_ctx(areq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) struct md5_state *octx = out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) octx->byte_count = op->byte_count + op->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) memcpy(octx->block, op->buf, op->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) if (op->byte_count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) for (i = 0; i < 4; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) octx->hash[i] = op->hash[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) octx->hash[0] = SHA1_H0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) octx->hash[1] = SHA1_H1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) octx->hash[2] = SHA1_H2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) octx->hash[3] = SHA1_H3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) int sun4i_hash_import_md5(struct ahash_request *areq, const void *in)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) struct sun4i_req_ctx *op = ahash_request_ctx(areq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) const struct md5_state *ictx = in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) sun4i_hash_init(areq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) op->byte_count = ictx->byte_count & ~0x3F;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) op->len = ictx->byte_count & 0x3F;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) memcpy(op->buf, ictx->block, op->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) for (i = 0; i < 4; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) op->hash[i] = ictx->hash[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) int sun4i_hash_export_sha1(struct ahash_request *areq, void *out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) struct sun4i_req_ctx *op = ahash_request_ctx(areq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) struct sha1_state *octx = out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) octx->count = op->byte_count + op->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) memcpy(octx->buffer, op->buf, op->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) if (op->byte_count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) for (i = 0; i < 5; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) octx->state[i] = op->hash[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) octx->state[0] = SHA1_H0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) octx->state[1] = SHA1_H1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) octx->state[2] = SHA1_H2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) octx->state[3] = SHA1_H3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) octx->state[4] = SHA1_H4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) int sun4i_hash_import_sha1(struct ahash_request *areq, const void *in)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) struct sun4i_req_ctx *op = ahash_request_ctx(areq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) const struct sha1_state *ictx = in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) sun4i_hash_init(areq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) op->byte_count = ictx->count & ~0x3F;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) op->len = ictx->count & 0x3F;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) memcpy(op->buf, ictx->buffer, op->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) for (i = 0; i < 5; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) op->hash[i] = ictx->state[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) #define SS_HASH_UPDATE 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) #define SS_HASH_FINAL 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) * sun4i_hash_update: update hash engine
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) * Could be used for both SHA1 and MD5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) * Write data by step of 32bits and put then in the SS.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) * Since we cannot leave partial data and hash state in the engine,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) * we need to get the hash state at the end of this function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) * We can get the hash state every 64 bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) * So the first work is to get the number of bytes to write to SS modulo 64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) * The extra bytes will go to a temporary buffer op->buf storing op->len bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) * So at the begin of update()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) * if op->len + areq->nbytes < 64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) * => all data will be written to wait buffer (op->buf) and end=0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) * if not, write all data from op->buf to the device and position end to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) * complete to 64bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) * example 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) * update1 60o => op->len=60
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) * update2 60o => need one more word to have 64 bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) * end=4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) * so write all data from op->buf and one word of SGs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) * write remaining data in op->buf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) * final state op->len=56
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) static int sun4i_hash(struct ahash_request *areq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) * i is the total bytes read from SGs, to be compared to areq->nbytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) * i is important because we cannot rely on SG length since the sum of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) * SG->length could be greater than areq->nbytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) * end is the position when we need to stop writing to the device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) * to be compared to i
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) * in_i: advancement in the current SG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) unsigned int i = 0, end, fill, min_fill, nwait, nbw = 0, j = 0, todo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) unsigned int in_i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) u32 spaces, rx_cnt = SS_RX_DEFAULT, bf[32] = {0}, v, ivmode = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) struct sun4i_req_ctx *op = ahash_request_ctx(areq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) struct sun4i_tfm_ctx *tfmctx = crypto_ahash_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) struct sun4i_ss_ctx *ss = tfmctx->ss;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) struct scatterlist *in_sg = areq->src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) struct sg_mapping_iter mi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) int in_r, err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) size_t copied = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) u32 wb = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) dev_dbg(ss->dev, "%s %s bc=%llu len=%u mode=%x wl=%u h0=%0x",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) __func__, crypto_tfm_alg_name(areq->base.tfm),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) op->byte_count, areq->nbytes, op->mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) op->len, op->hash[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) if (unlikely(!areq->nbytes) && !(op->flags & SS_HASH_FINAL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) /* protect against overflow */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) if (unlikely(areq->nbytes > UINT_MAX - op->len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) dev_err(ss->dev, "Cannot process too large request\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) if (op->len + areq->nbytes < 64 && !(op->flags & SS_HASH_FINAL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) /* linearize data to op->buf */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) copied = sg_pcopy_to_buffer(areq->src, sg_nents(areq->src),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) op->buf + op->len, areq->nbytes, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) op->len += copied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) spin_lock_bh(&ss->slock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) * if some data have been processed before,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) * we need to restore the partial hash state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) if (op->byte_count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) ivmode = SS_IV_ARBITRARY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) for (i = 0; i < crypto_ahash_digestsize(tfm) / 4; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) writel(op->hash[i], ss->base + SS_IV0 + i * 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) /* Enable the device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) writel(op->mode | SS_ENABLED | ivmode, ss->base + SS_CTL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) if (!(op->flags & SS_HASH_UPDATE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) goto hash_final;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) /* start of handling data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) if (!(op->flags & SS_HASH_FINAL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) end = ((areq->nbytes + op->len) / 64) * 64 - op->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) if (end > areq->nbytes || areq->nbytes - end > 63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) dev_err(ss->dev, "ERROR: Bound error %u %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) end, areq->nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) goto release_ss;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) /* Since we have the flag final, we can go up to modulo 4 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) if (areq->nbytes < 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) end = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) end = ((areq->nbytes + op->len) / 4) * 4 - op->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) /* TODO if SGlen % 4 and !op->len then DMA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) i = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) while (in_sg && i == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) if (in_sg->length % 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) in_sg = sg_next(in_sg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) if (i == 1 && !op->len && areq->nbytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) dev_dbg(ss->dev, "We can DMA\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) sg_miter_start(&mi, areq->src, sg_nents(areq->src),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) SG_MITER_FROM_SG | SG_MITER_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) sg_miter_next(&mi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) in_i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) * we need to linearize in two case:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) * - the buffer is already used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) * - the SG does not have enough byte remaining ( < 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) if (op->len || (mi.length - in_i) < 4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) * if we have entered here we have two reason to stop
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) * - the buffer is full
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) * - reach the end
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) while (op->len < 64 && i < end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) /* how many bytes we can read from current SG */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) in_r = min(end - i, 64 - op->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) in_r = min_t(size_t, mi.length - in_i, in_r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) memcpy(op->buf + op->len, mi.addr + in_i, in_r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) op->len += in_r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) i += in_r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) in_i += in_r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) if (in_i == mi.length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) sg_miter_next(&mi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) in_i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) if (op->len > 3 && !(op->len % 4)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) /* write buf to the device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) writesl(ss->base + SS_RXFIFO, op->buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) op->len / 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) op->byte_count += op->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) op->len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) if (mi.length - in_i > 3 && i < end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) /* how many bytes we can read from current SG */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) in_r = min_t(size_t, mi.length - in_i, areq->nbytes - i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) in_r = min_t(size_t, ((mi.length - in_i) / 4) * 4, in_r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) /* how many bytes we can write in the device*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) todo = min3((u32)(end - i) / 4, rx_cnt, (u32)in_r / 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) writesl(ss->base + SS_RXFIFO, mi.addr + in_i, todo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) op->byte_count += todo * 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) i += todo * 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) in_i += todo * 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) rx_cnt -= todo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) if (!rx_cnt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) spaces = readl(ss->base + SS_FCSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) rx_cnt = SS_RXFIFO_SPACES(spaces);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) if (in_i == mi.length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) sg_miter_next(&mi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) in_i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) } while (i < end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) * Now we have written to the device all that we can,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) * store the remaining bytes in op->buf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) if ((areq->nbytes - i) < 64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) while (i < areq->nbytes && in_i < mi.length && op->len < 64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) /* how many bytes we can read from current SG */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) in_r = min(areq->nbytes - i, 64 - op->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) in_r = min_t(size_t, mi.length - in_i, in_r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) memcpy(op->buf + op->len, mi.addr + in_i, in_r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) op->len += in_r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) i += in_r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) in_i += in_r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) if (in_i == mi.length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) sg_miter_next(&mi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) in_i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) sg_miter_stop(&mi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) * End of data process
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) * Now if we have the flag final go to finalize part
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) * If not, store the partial hash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) if (op->flags & SS_HASH_FINAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) goto hash_final;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) writel(op->mode | SS_ENABLED | SS_DATA_END, ss->base + SS_CTL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) v = readl(ss->base + SS_CTL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) } while (i < SS_TIMEOUT && (v & SS_DATA_END));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) if (unlikely(i >= SS_TIMEOUT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) dev_err_ratelimited(ss->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) "ERROR: hash end timeout %d>%d ctl=%x len=%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) i, SS_TIMEOUT, v, areq->nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) goto release_ss;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) * The datasheet isn't very clear about when to retrieve the digest. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) * bit SS_DATA_END is cleared when the engine has processed the data and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) * when the digest is computed *but* it doesn't mean the digest is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) * available in the digest registers. Hence the delay to be sure we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) * read it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) ndelay(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) for (i = 0; i < crypto_ahash_digestsize(tfm) / 4; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) op->hash[i] = readl(ss->base + SS_MD0 + i * 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) goto release_ss;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) * hash_final: finalize hashing operation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) * If we have some remaining bytes, we write them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) * Then ask the SS for finalizing the hashing operation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) * I do not check RX FIFO size in this function since the size is 32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) * after each enabling and this function neither write more than 32 words.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) * If we come from the update part, we cannot have more than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) * 3 remaining bytes to write and SS is fast enough to not care about it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) hash_final:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) /* write the remaining words of the wait buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) if (op->len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) nwait = op->len / 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) if (nwait) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) writesl(ss->base + SS_RXFIFO, op->buf, nwait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) op->byte_count += 4 * nwait;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) nbw = op->len - 4 * nwait;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) if (nbw) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) wb = le32_to_cpup((__le32 *)(op->buf + nwait * 4));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) wb &= GENMASK((nbw * 8) - 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) op->byte_count += nbw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) /* write the remaining bytes of the nbw buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) wb |= ((1 << 7) << (nbw * 8));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) ((__le32 *)bf)[j++] = cpu_to_le32(wb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) * number of space to pad to obtain 64o minus 8(size) minus 4 (final 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) * I take the operations from other MD5/SHA1 implementations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) /* last block size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) fill = 64 - (op->byte_count % 64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) min_fill = 2 * sizeof(u32) + (nbw ? 0 : sizeof(u32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) /* if we can't fill all data, jump to the next 64 block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) if (fill < min_fill)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) fill += 64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) j += (fill - min_fill) / sizeof(u32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) /* write the length of data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) if (op->mode == SS_OP_SHA1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) __be64 *bits = (__be64 *)&bf[j];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) *bits = cpu_to_be64(op->byte_count << 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) j += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) __le64 *bits = (__le64 *)&bf[j];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) *bits = cpu_to_le64(op->byte_count << 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) j += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) writesl(ss->base + SS_RXFIFO, bf, j);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) /* Tell the SS to stop the hashing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) writel(op->mode | SS_ENABLED | SS_DATA_END, ss->base + SS_CTL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) * Wait for SS to finish the hash.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) * The timeout could happen only in case of bad overclocking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) * or driver bug.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) v = readl(ss->base + SS_CTL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) } while (i < SS_TIMEOUT && (v & SS_DATA_END));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) if (unlikely(i >= SS_TIMEOUT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) dev_err_ratelimited(ss->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) "ERROR: hash end timeout %d>%d ctl=%x len=%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) i, SS_TIMEOUT, v, areq->nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) goto release_ss;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) * The datasheet isn't very clear about when to retrieve the digest. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) * bit SS_DATA_END is cleared when the engine has processed the data and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) * when the digest is computed *but* it doesn't mean the digest is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) * available in the digest registers. Hence the delay to be sure we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) * read it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) ndelay(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) /* Get the hash from the device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) if (op->mode == SS_OP_SHA1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) for (i = 0; i < 5; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) v = readl(ss->base + SS_MD0 + i * 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) if (ss->variant->sha1_in_be)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) put_unaligned_le32(v, areq->result + i * 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) put_unaligned_be32(v, areq->result + i * 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) for (i = 0; i < 4; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) v = readl(ss->base + SS_MD0 + i * 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) put_unaligned_le32(v, areq->result + i * 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) release_ss:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) writel(0, ss->base + SS_CTL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) spin_unlock_bh(&ss->slock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) int sun4i_hash_final(struct ahash_request *areq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) struct sun4i_req_ctx *op = ahash_request_ctx(areq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) op->flags = SS_HASH_FINAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) return sun4i_hash(areq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) int sun4i_hash_update(struct ahash_request *areq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) struct sun4i_req_ctx *op = ahash_request_ctx(areq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) op->flags = SS_HASH_UPDATE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) return sun4i_hash(areq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) /* sun4i_hash_finup: finalize hashing operation after an update */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) int sun4i_hash_finup(struct ahash_request *areq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) struct sun4i_req_ctx *op = ahash_request_ctx(areq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) op->flags = SS_HASH_UPDATE | SS_HASH_FINAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) return sun4i_hash(areq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) /* combo of init/update/final functions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) int sun4i_hash_digest(struct ahash_request *areq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) struct sun4i_req_ctx *op = ahash_request_ctx(areq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) err = sun4i_hash_init(areq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) op->flags = SS_HASH_UPDATE | SS_HASH_FINAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) return sun4i_hash(areq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) }