18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * sun4i-ss-hash.c - hardware cryptographic accelerator for Allwinner A20 SoC 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2013-2015 Corentin LABBE <clabbe.montjoie@gmail.com> 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * This file add support for MD5 and SHA1. 88c2ecf20Sopenharmony_ci * 98c2ecf20Sopenharmony_ci * You could find the datasheet in Documentation/arm/sunxi.rst 108c2ecf20Sopenharmony_ci */ 118c2ecf20Sopenharmony_ci#include "sun4i-ss.h" 128c2ecf20Sopenharmony_ci#include <asm/unaligned.h> 138c2ecf20Sopenharmony_ci#include <linux/scatterlist.h> 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ci/* This is a totally arbitrary value */ 168c2ecf20Sopenharmony_ci#define SS_TIMEOUT 100 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ciint sun4i_hash_crainit(struct crypto_tfm *tfm) 198c2ecf20Sopenharmony_ci{ 208c2ecf20Sopenharmony_ci struct sun4i_tfm_ctx *op = crypto_tfm_ctx(tfm); 218c2ecf20Sopenharmony_ci struct ahash_alg *alg = __crypto_ahash_alg(tfm->__crt_alg); 228c2ecf20Sopenharmony_ci struct sun4i_ss_alg_template *algt; 238c2ecf20Sopenharmony_ci int err; 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_ci memset(op, 0, sizeof(struct sun4i_tfm_ctx)); 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_ci algt = container_of(alg, struct sun4i_ss_alg_template, alg.hash); 288c2ecf20Sopenharmony_ci op->ss = algt->ss; 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ci err = pm_runtime_get_sync(op->ss->dev); 318c2ecf20Sopenharmony_ci if (err < 0) 328c2ecf20Sopenharmony_ci return err; 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_ci crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm), 358c2ecf20Sopenharmony_ci sizeof(struct sun4i_req_ctx)); 368c2ecf20Sopenharmony_ci return 0; 378c2ecf20Sopenharmony_ci} 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_civoid sun4i_hash_craexit(struct crypto_tfm *tfm) 408c2ecf20Sopenharmony_ci{ 418c2ecf20Sopenharmony_ci struct sun4i_tfm_ctx *op = crypto_tfm_ctx(tfm); 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_ci pm_runtime_put(op->ss->dev); 448c2ecf20Sopenharmony_ci} 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ci/* sun4i_hash_init: initialize request context */ 478c2ecf20Sopenharmony_ciint sun4i_hash_init(struct ahash_request *areq) 488c2ecf20Sopenharmony_ci{ 498c2ecf20Sopenharmony_ci struct sun4i_req_ctx *op = ahash_request_ctx(areq); 508c2ecf20Sopenharmony_ci struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq); 518c2ecf20Sopenharmony_ci struct ahash_alg *alg = __crypto_ahash_alg(tfm->base.__crt_alg); 528c2ecf20Sopenharmony_ci struct sun4i_ss_alg_template *algt; 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci memset(op, 0, sizeof(struct sun4i_req_ctx)); 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_ci algt = container_of(alg, struct sun4i_ss_alg_template, alg.hash); 578c2ecf20Sopenharmony_ci op->mode = algt->mode; 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_ci return 0; 608c2ecf20Sopenharmony_ci} 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ciint sun4i_hash_export_md5(struct ahash_request *areq, void *out) 638c2ecf20Sopenharmony_ci{ 648c2ecf20Sopenharmony_ci struct sun4i_req_ctx *op = ahash_request_ctx(areq); 658c2ecf20Sopenharmony_ci struct md5_state *octx = out; 668c2ecf20Sopenharmony_ci int i; 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_ci octx->byte_count = op->byte_count + op->len; 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci memcpy(octx->block, op->buf, op->len); 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ci if (op->byte_count) { 738c2ecf20Sopenharmony_ci for (i = 0; i < 4; i++) 748c2ecf20Sopenharmony_ci octx->hash[i] = op->hash[i]; 758c2ecf20Sopenharmony_ci } else { 768c2ecf20Sopenharmony_ci octx->hash[0] = SHA1_H0; 778c2ecf20Sopenharmony_ci octx->hash[1] = SHA1_H1; 788c2ecf20Sopenharmony_ci octx->hash[2] = SHA1_H2; 798c2ecf20Sopenharmony_ci octx->hash[3] = SHA1_H3; 808c2ecf20Sopenharmony_ci } 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_ci return 0; 838c2ecf20Sopenharmony_ci} 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_ciint sun4i_hash_import_md5(struct ahash_request *areq, const void *in) 868c2ecf20Sopenharmony_ci{ 878c2ecf20Sopenharmony_ci struct sun4i_req_ctx *op = ahash_request_ctx(areq); 888c2ecf20Sopenharmony_ci const struct md5_state *ictx = in; 898c2ecf20Sopenharmony_ci int i; 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci sun4i_hash_init(areq); 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_ci op->byte_count = ictx->byte_count & ~0x3F; 948c2ecf20Sopenharmony_ci op->len = ictx->byte_count & 0x3F; 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_ci memcpy(op->buf, ictx->block, op->len); 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ci for (i = 0; i < 4; i++) 998c2ecf20Sopenharmony_ci op->hash[i] = ictx->hash[i]; 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ci return 0; 1028c2ecf20Sopenharmony_ci} 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_ciint sun4i_hash_export_sha1(struct ahash_request *areq, void *out) 1058c2ecf20Sopenharmony_ci{ 1068c2ecf20Sopenharmony_ci struct sun4i_req_ctx *op = ahash_request_ctx(areq); 1078c2ecf20Sopenharmony_ci struct sha1_state *octx = out; 1088c2ecf20Sopenharmony_ci int i; 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_ci octx->count = op->byte_count + op->len; 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci memcpy(octx->buffer, op->buf, op->len); 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci if (op->byte_count) { 1158c2ecf20Sopenharmony_ci for (i = 0; i < 5; i++) 1168c2ecf20Sopenharmony_ci octx->state[i] = op->hash[i]; 1178c2ecf20Sopenharmony_ci } else { 1188c2ecf20Sopenharmony_ci octx->state[0] = SHA1_H0; 1198c2ecf20Sopenharmony_ci octx->state[1] = SHA1_H1; 1208c2ecf20Sopenharmony_ci octx->state[2] = SHA1_H2; 1218c2ecf20Sopenharmony_ci octx->state[3] = SHA1_H3; 1228c2ecf20Sopenharmony_ci octx->state[4] = SHA1_H4; 1238c2ecf20Sopenharmony_ci } 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_ci return 0; 1268c2ecf20Sopenharmony_ci} 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_ciint sun4i_hash_import_sha1(struct ahash_request *areq, const void *in) 1298c2ecf20Sopenharmony_ci{ 1308c2ecf20Sopenharmony_ci struct sun4i_req_ctx *op = ahash_request_ctx(areq); 1318c2ecf20Sopenharmony_ci const struct sha1_state *ictx = in; 1328c2ecf20Sopenharmony_ci int i; 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_ci sun4i_hash_init(areq); 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_ci op->byte_count = ictx->count & ~0x3F; 1378c2ecf20Sopenharmony_ci op->len = ictx->count & 0x3F; 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci memcpy(op->buf, ictx->buffer, op->len); 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_ci for (i = 0; i < 5; i++) 1428c2ecf20Sopenharmony_ci op->hash[i] = ictx->state[i]; 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_ci return 0; 1458c2ecf20Sopenharmony_ci} 1468c2ecf20Sopenharmony_ci 1478c2ecf20Sopenharmony_ci#define SS_HASH_UPDATE 1 1488c2ecf20Sopenharmony_ci#define SS_HASH_FINAL 2 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_ci/* 1518c2ecf20Sopenharmony_ci * sun4i_hash_update: update hash engine 1528c2ecf20Sopenharmony_ci * 1538c2ecf20Sopenharmony_ci * Could be used for both SHA1 and MD5 1548c2ecf20Sopenharmony_ci * Write data by step of 32bits and put then in the SS. 1558c2ecf20Sopenharmony_ci * 1568c2ecf20Sopenharmony_ci * Since we cannot leave partial data and hash state in the engine, 1578c2ecf20Sopenharmony_ci * we need to get the hash state at the end of this function. 1588c2ecf20Sopenharmony_ci * We can get the hash state every 64 bytes 1598c2ecf20Sopenharmony_ci * 1608c2ecf20Sopenharmony_ci * So the first work is to get the number of bytes to write to SS modulo 64 1618c2ecf20Sopenharmony_ci * The extra bytes will go to a temporary buffer op->buf storing op->len bytes 1628c2ecf20Sopenharmony_ci * 1638c2ecf20Sopenharmony_ci * So at the begin of update() 1648c2ecf20Sopenharmony_ci * if op->len + areq->nbytes < 64 1658c2ecf20Sopenharmony_ci * => all data will be written to wait buffer (op->buf) and end=0 1668c2ecf20Sopenharmony_ci * if not, write all data from op->buf to the device and position end to 1678c2ecf20Sopenharmony_ci * complete to 64bytes 1688c2ecf20Sopenharmony_ci * 1698c2ecf20Sopenharmony_ci * example 1: 1708c2ecf20Sopenharmony_ci * update1 60o => op->len=60 1718c2ecf20Sopenharmony_ci * update2 60o => need one more word to have 64 bytes 1728c2ecf20Sopenharmony_ci * end=4 1738c2ecf20Sopenharmony_ci * so write all data from op->buf and one word of SGs 1748c2ecf20Sopenharmony_ci * write remaining data in op->buf 1758c2ecf20Sopenharmony_ci * final state op->len=56 1768c2ecf20Sopenharmony_ci */ 1778c2ecf20Sopenharmony_cistatic int sun4i_hash(struct ahash_request *areq) 1788c2ecf20Sopenharmony_ci{ 1798c2ecf20Sopenharmony_ci /* 1808c2ecf20Sopenharmony_ci * i is the total bytes read from SGs, to be compared to areq->nbytes 1818c2ecf20Sopenharmony_ci * i is important because we cannot rely on SG length since the sum of 1828c2ecf20Sopenharmony_ci * SG->length could be greater than areq->nbytes 1838c2ecf20Sopenharmony_ci * 1848c2ecf20Sopenharmony_ci * end is the position when we need to stop writing to the device, 1858c2ecf20Sopenharmony_ci * to be compared to i 1868c2ecf20Sopenharmony_ci * 1878c2ecf20Sopenharmony_ci * in_i: advancement in the current SG 1888c2ecf20Sopenharmony_ci */ 1898c2ecf20Sopenharmony_ci unsigned int i = 0, end, fill, min_fill, nwait, nbw = 0, j = 0, todo; 1908c2ecf20Sopenharmony_ci unsigned int in_i = 0; 1918c2ecf20Sopenharmony_ci u32 spaces, rx_cnt = SS_RX_DEFAULT, bf[32] = {0}, v, ivmode = 0; 1928c2ecf20Sopenharmony_ci struct sun4i_req_ctx *op = ahash_request_ctx(areq); 1938c2ecf20Sopenharmony_ci struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq); 1948c2ecf20Sopenharmony_ci struct sun4i_tfm_ctx *tfmctx = crypto_ahash_ctx(tfm); 1958c2ecf20Sopenharmony_ci struct sun4i_ss_ctx *ss = tfmctx->ss; 1968c2ecf20Sopenharmony_ci struct scatterlist *in_sg = areq->src; 1978c2ecf20Sopenharmony_ci struct sg_mapping_iter mi; 1988c2ecf20Sopenharmony_ci int in_r, err = 0; 1998c2ecf20Sopenharmony_ci size_t copied = 0; 2008c2ecf20Sopenharmony_ci u32 wb = 0; 2018c2ecf20Sopenharmony_ci 2028c2ecf20Sopenharmony_ci dev_dbg(ss->dev, "%s %s bc=%llu len=%u mode=%x wl=%u h0=%0x", 2038c2ecf20Sopenharmony_ci __func__, crypto_tfm_alg_name(areq->base.tfm), 2048c2ecf20Sopenharmony_ci op->byte_count, areq->nbytes, op->mode, 2058c2ecf20Sopenharmony_ci op->len, op->hash[0]); 2068c2ecf20Sopenharmony_ci 2078c2ecf20Sopenharmony_ci if (unlikely(!areq->nbytes) && !(op->flags & SS_HASH_FINAL)) 2088c2ecf20Sopenharmony_ci return 0; 2098c2ecf20Sopenharmony_ci 2108c2ecf20Sopenharmony_ci /* protect against overflow */ 2118c2ecf20Sopenharmony_ci if (unlikely(areq->nbytes > UINT_MAX - op->len)) { 2128c2ecf20Sopenharmony_ci dev_err(ss->dev, "Cannot process too large request\n"); 2138c2ecf20Sopenharmony_ci return -EINVAL; 2148c2ecf20Sopenharmony_ci } 2158c2ecf20Sopenharmony_ci 2168c2ecf20Sopenharmony_ci if (op->len + areq->nbytes < 64 && !(op->flags & SS_HASH_FINAL)) { 2178c2ecf20Sopenharmony_ci /* linearize data to op->buf */ 2188c2ecf20Sopenharmony_ci copied = sg_pcopy_to_buffer(areq->src, sg_nents(areq->src), 2198c2ecf20Sopenharmony_ci op->buf + op->len, areq->nbytes, 0); 2208c2ecf20Sopenharmony_ci op->len += copied; 2218c2ecf20Sopenharmony_ci return 0; 2228c2ecf20Sopenharmony_ci } 2238c2ecf20Sopenharmony_ci 2248c2ecf20Sopenharmony_ci spin_lock_bh(&ss->slock); 2258c2ecf20Sopenharmony_ci 2268c2ecf20Sopenharmony_ci /* 2278c2ecf20Sopenharmony_ci * if some data have been processed before, 2288c2ecf20Sopenharmony_ci * we need to restore the partial hash state 2298c2ecf20Sopenharmony_ci */ 2308c2ecf20Sopenharmony_ci if (op->byte_count) { 2318c2ecf20Sopenharmony_ci ivmode = SS_IV_ARBITRARY; 2328c2ecf20Sopenharmony_ci for (i = 0; i < crypto_ahash_digestsize(tfm) / 4; i++) 2338c2ecf20Sopenharmony_ci writel(op->hash[i], ss->base + SS_IV0 + i * 4); 2348c2ecf20Sopenharmony_ci } 2358c2ecf20Sopenharmony_ci /* Enable the device */ 2368c2ecf20Sopenharmony_ci writel(op->mode | SS_ENABLED | ivmode, ss->base + SS_CTL); 2378c2ecf20Sopenharmony_ci 2388c2ecf20Sopenharmony_ci if (!(op->flags & SS_HASH_UPDATE)) 2398c2ecf20Sopenharmony_ci goto hash_final; 2408c2ecf20Sopenharmony_ci 2418c2ecf20Sopenharmony_ci /* start of handling data */ 2428c2ecf20Sopenharmony_ci if (!(op->flags & SS_HASH_FINAL)) { 2438c2ecf20Sopenharmony_ci end = ((areq->nbytes + op->len) / 64) * 64 - op->len; 2448c2ecf20Sopenharmony_ci 2458c2ecf20Sopenharmony_ci if (end > areq->nbytes || areq->nbytes - end > 63) { 2468c2ecf20Sopenharmony_ci dev_err(ss->dev, "ERROR: Bound error %u %u\n", 2478c2ecf20Sopenharmony_ci end, areq->nbytes); 2488c2ecf20Sopenharmony_ci err = -EINVAL; 2498c2ecf20Sopenharmony_ci goto release_ss; 2508c2ecf20Sopenharmony_ci } 2518c2ecf20Sopenharmony_ci } else { 2528c2ecf20Sopenharmony_ci /* Since we have the flag final, we can go up to modulo 4 */ 2538c2ecf20Sopenharmony_ci if (areq->nbytes < 4) 2548c2ecf20Sopenharmony_ci end = 0; 2558c2ecf20Sopenharmony_ci else 2568c2ecf20Sopenharmony_ci end = ((areq->nbytes + op->len) / 4) * 4 - op->len; 2578c2ecf20Sopenharmony_ci } 2588c2ecf20Sopenharmony_ci 2598c2ecf20Sopenharmony_ci /* TODO if SGlen % 4 and !op->len then DMA */ 2608c2ecf20Sopenharmony_ci i = 1; 2618c2ecf20Sopenharmony_ci while (in_sg && i == 1) { 2628c2ecf20Sopenharmony_ci if (in_sg->length % 4) 2638c2ecf20Sopenharmony_ci i = 0; 2648c2ecf20Sopenharmony_ci in_sg = sg_next(in_sg); 2658c2ecf20Sopenharmony_ci } 2668c2ecf20Sopenharmony_ci if (i == 1 && !op->len && areq->nbytes) 2678c2ecf20Sopenharmony_ci dev_dbg(ss->dev, "We can DMA\n"); 2688c2ecf20Sopenharmony_ci 2698c2ecf20Sopenharmony_ci i = 0; 2708c2ecf20Sopenharmony_ci sg_miter_start(&mi, areq->src, sg_nents(areq->src), 2718c2ecf20Sopenharmony_ci SG_MITER_FROM_SG | SG_MITER_ATOMIC); 2728c2ecf20Sopenharmony_ci sg_miter_next(&mi); 2738c2ecf20Sopenharmony_ci in_i = 0; 2748c2ecf20Sopenharmony_ci 2758c2ecf20Sopenharmony_ci do { 2768c2ecf20Sopenharmony_ci /* 2778c2ecf20Sopenharmony_ci * we need to linearize in two case: 2788c2ecf20Sopenharmony_ci * - the buffer is already used 2798c2ecf20Sopenharmony_ci * - the SG does not have enough byte remaining ( < 4) 2808c2ecf20Sopenharmony_ci */ 2818c2ecf20Sopenharmony_ci if (op->len || (mi.length - in_i) < 4) { 2828c2ecf20Sopenharmony_ci /* 2838c2ecf20Sopenharmony_ci * if we have entered here we have two reason to stop 2848c2ecf20Sopenharmony_ci * - the buffer is full 2858c2ecf20Sopenharmony_ci * - reach the end 2868c2ecf20Sopenharmony_ci */ 2878c2ecf20Sopenharmony_ci while (op->len < 64 && i < end) { 2888c2ecf20Sopenharmony_ci /* how many bytes we can read from current SG */ 2898c2ecf20Sopenharmony_ci in_r = min(end - i, 64 - op->len); 2908c2ecf20Sopenharmony_ci in_r = min_t(size_t, mi.length - in_i, in_r); 2918c2ecf20Sopenharmony_ci memcpy(op->buf + op->len, mi.addr + in_i, in_r); 2928c2ecf20Sopenharmony_ci op->len += in_r; 2938c2ecf20Sopenharmony_ci i += in_r; 2948c2ecf20Sopenharmony_ci in_i += in_r; 2958c2ecf20Sopenharmony_ci if (in_i == mi.length) { 2968c2ecf20Sopenharmony_ci sg_miter_next(&mi); 2978c2ecf20Sopenharmony_ci in_i = 0; 2988c2ecf20Sopenharmony_ci } 2998c2ecf20Sopenharmony_ci } 3008c2ecf20Sopenharmony_ci if (op->len > 3 && !(op->len % 4)) { 3018c2ecf20Sopenharmony_ci /* write buf to the device */ 3028c2ecf20Sopenharmony_ci writesl(ss->base + SS_RXFIFO, op->buf, 3038c2ecf20Sopenharmony_ci op->len / 4); 3048c2ecf20Sopenharmony_ci op->byte_count += op->len; 3058c2ecf20Sopenharmony_ci op->len = 0; 3068c2ecf20Sopenharmony_ci } 3078c2ecf20Sopenharmony_ci } 3088c2ecf20Sopenharmony_ci if (mi.length - in_i > 3 && i < end) { 3098c2ecf20Sopenharmony_ci /* how many bytes we can read from current SG */ 3108c2ecf20Sopenharmony_ci in_r = min_t(size_t, mi.length - in_i, areq->nbytes - i); 3118c2ecf20Sopenharmony_ci in_r = min_t(size_t, ((mi.length - in_i) / 4) * 4, in_r); 3128c2ecf20Sopenharmony_ci /* how many bytes we can write in the device*/ 3138c2ecf20Sopenharmony_ci todo = min3((u32)(end - i) / 4, rx_cnt, (u32)in_r / 4); 3148c2ecf20Sopenharmony_ci writesl(ss->base + SS_RXFIFO, mi.addr + in_i, todo); 3158c2ecf20Sopenharmony_ci op->byte_count += todo * 4; 3168c2ecf20Sopenharmony_ci i += todo * 4; 3178c2ecf20Sopenharmony_ci in_i += todo * 4; 3188c2ecf20Sopenharmony_ci rx_cnt -= todo; 3198c2ecf20Sopenharmony_ci if (!rx_cnt) { 3208c2ecf20Sopenharmony_ci spaces = readl(ss->base + SS_FCSR); 3218c2ecf20Sopenharmony_ci rx_cnt = SS_RXFIFO_SPACES(spaces); 3228c2ecf20Sopenharmony_ci } 3238c2ecf20Sopenharmony_ci if (in_i == mi.length) { 3248c2ecf20Sopenharmony_ci sg_miter_next(&mi); 3258c2ecf20Sopenharmony_ci in_i = 0; 3268c2ecf20Sopenharmony_ci } 3278c2ecf20Sopenharmony_ci } 3288c2ecf20Sopenharmony_ci } while (i < end); 3298c2ecf20Sopenharmony_ci 3308c2ecf20Sopenharmony_ci /* 3318c2ecf20Sopenharmony_ci * Now we have written to the device all that we can, 3328c2ecf20Sopenharmony_ci * store the remaining bytes in op->buf 3338c2ecf20Sopenharmony_ci */ 3348c2ecf20Sopenharmony_ci if ((areq->nbytes - i) < 64) { 3358c2ecf20Sopenharmony_ci while (i < areq->nbytes && in_i < mi.length && op->len < 64) { 3368c2ecf20Sopenharmony_ci /* how many bytes we can read from current SG */ 3378c2ecf20Sopenharmony_ci in_r = min(areq->nbytes - i, 64 - op->len); 3388c2ecf20Sopenharmony_ci in_r = min_t(size_t, mi.length - in_i, in_r); 3398c2ecf20Sopenharmony_ci memcpy(op->buf + op->len, mi.addr + in_i, in_r); 3408c2ecf20Sopenharmony_ci op->len += in_r; 3418c2ecf20Sopenharmony_ci i += in_r; 3428c2ecf20Sopenharmony_ci in_i += in_r; 3438c2ecf20Sopenharmony_ci if (in_i == mi.length) { 3448c2ecf20Sopenharmony_ci sg_miter_next(&mi); 3458c2ecf20Sopenharmony_ci in_i = 0; 3468c2ecf20Sopenharmony_ci } 3478c2ecf20Sopenharmony_ci } 3488c2ecf20Sopenharmony_ci } 3498c2ecf20Sopenharmony_ci 3508c2ecf20Sopenharmony_ci sg_miter_stop(&mi); 3518c2ecf20Sopenharmony_ci 3528c2ecf20Sopenharmony_ci /* 3538c2ecf20Sopenharmony_ci * End of data process 3548c2ecf20Sopenharmony_ci * Now if we have the flag final go to finalize part 3558c2ecf20Sopenharmony_ci * If not, store the partial hash 3568c2ecf20Sopenharmony_ci */ 3578c2ecf20Sopenharmony_ci if (op->flags & SS_HASH_FINAL) 3588c2ecf20Sopenharmony_ci goto hash_final; 3598c2ecf20Sopenharmony_ci 3608c2ecf20Sopenharmony_ci writel(op->mode | SS_ENABLED | SS_DATA_END, ss->base + SS_CTL); 3618c2ecf20Sopenharmony_ci i = 0; 3628c2ecf20Sopenharmony_ci do { 3638c2ecf20Sopenharmony_ci v = readl(ss->base + SS_CTL); 3648c2ecf20Sopenharmony_ci i++; 3658c2ecf20Sopenharmony_ci } while (i < SS_TIMEOUT && (v & SS_DATA_END)); 3668c2ecf20Sopenharmony_ci if (unlikely(i >= SS_TIMEOUT)) { 3678c2ecf20Sopenharmony_ci dev_err_ratelimited(ss->dev, 3688c2ecf20Sopenharmony_ci "ERROR: hash end timeout %d>%d ctl=%x len=%u\n", 3698c2ecf20Sopenharmony_ci i, SS_TIMEOUT, v, areq->nbytes); 3708c2ecf20Sopenharmony_ci err = -EIO; 3718c2ecf20Sopenharmony_ci goto release_ss; 3728c2ecf20Sopenharmony_ci } 3738c2ecf20Sopenharmony_ci 3748c2ecf20Sopenharmony_ci /* 3758c2ecf20Sopenharmony_ci * The datasheet isn't very clear about when to retrieve the digest. The 3768c2ecf20Sopenharmony_ci * bit SS_DATA_END is cleared when the engine has processed the data and 3778c2ecf20Sopenharmony_ci * when the digest is computed *but* it doesn't mean the digest is 3788c2ecf20Sopenharmony_ci * available in the digest registers. Hence the delay to be sure we can 3798c2ecf20Sopenharmony_ci * read it. 3808c2ecf20Sopenharmony_ci */ 3818c2ecf20Sopenharmony_ci ndelay(1); 3828c2ecf20Sopenharmony_ci 3838c2ecf20Sopenharmony_ci for (i = 0; i < crypto_ahash_digestsize(tfm) / 4; i++) 3848c2ecf20Sopenharmony_ci op->hash[i] = readl(ss->base + SS_MD0 + i * 4); 3858c2ecf20Sopenharmony_ci 3868c2ecf20Sopenharmony_ci goto release_ss; 3878c2ecf20Sopenharmony_ci 3888c2ecf20Sopenharmony_ci/* 3898c2ecf20Sopenharmony_ci * hash_final: finalize hashing operation 3908c2ecf20Sopenharmony_ci * 3918c2ecf20Sopenharmony_ci * If we have some remaining bytes, we write them. 3928c2ecf20Sopenharmony_ci * Then ask the SS for finalizing the hashing operation 3938c2ecf20Sopenharmony_ci * 3948c2ecf20Sopenharmony_ci * I do not check RX FIFO size in this function since the size is 32 3958c2ecf20Sopenharmony_ci * after each enabling and this function neither write more than 32 words. 3968c2ecf20Sopenharmony_ci * If we come from the update part, we cannot have more than 3978c2ecf20Sopenharmony_ci * 3 remaining bytes to write and SS is fast enough to not care about it. 3988c2ecf20Sopenharmony_ci */ 3998c2ecf20Sopenharmony_ci 4008c2ecf20Sopenharmony_cihash_final: 4018c2ecf20Sopenharmony_ci 4028c2ecf20Sopenharmony_ci /* write the remaining words of the wait buffer */ 4038c2ecf20Sopenharmony_ci if (op->len) { 4048c2ecf20Sopenharmony_ci nwait = op->len / 4; 4058c2ecf20Sopenharmony_ci if (nwait) { 4068c2ecf20Sopenharmony_ci writesl(ss->base + SS_RXFIFO, op->buf, nwait); 4078c2ecf20Sopenharmony_ci op->byte_count += 4 * nwait; 4088c2ecf20Sopenharmony_ci } 4098c2ecf20Sopenharmony_ci 4108c2ecf20Sopenharmony_ci nbw = op->len - 4 * nwait; 4118c2ecf20Sopenharmony_ci if (nbw) { 4128c2ecf20Sopenharmony_ci wb = le32_to_cpup((__le32 *)(op->buf + nwait * 4)); 4138c2ecf20Sopenharmony_ci wb &= GENMASK((nbw * 8) - 1, 0); 4148c2ecf20Sopenharmony_ci 4158c2ecf20Sopenharmony_ci op->byte_count += nbw; 4168c2ecf20Sopenharmony_ci } 4178c2ecf20Sopenharmony_ci } 4188c2ecf20Sopenharmony_ci 4198c2ecf20Sopenharmony_ci /* write the remaining bytes of the nbw buffer */ 4208c2ecf20Sopenharmony_ci wb |= ((1 << 7) << (nbw * 8)); 4218c2ecf20Sopenharmony_ci ((__le32 *)bf)[j++] = cpu_to_le32(wb); 4228c2ecf20Sopenharmony_ci 4238c2ecf20Sopenharmony_ci /* 4248c2ecf20Sopenharmony_ci * number of space to pad to obtain 64o minus 8(size) minus 4 (final 1) 4258c2ecf20Sopenharmony_ci * I take the operations from other MD5/SHA1 implementations 4268c2ecf20Sopenharmony_ci */ 4278c2ecf20Sopenharmony_ci 4288c2ecf20Sopenharmony_ci /* last block size */ 4298c2ecf20Sopenharmony_ci fill = 64 - (op->byte_count % 64); 4308c2ecf20Sopenharmony_ci min_fill = 2 * sizeof(u32) + (nbw ? 0 : sizeof(u32)); 4318c2ecf20Sopenharmony_ci 4328c2ecf20Sopenharmony_ci /* if we can't fill all data, jump to the next 64 block */ 4338c2ecf20Sopenharmony_ci if (fill < min_fill) 4348c2ecf20Sopenharmony_ci fill += 64; 4358c2ecf20Sopenharmony_ci 4368c2ecf20Sopenharmony_ci j += (fill - min_fill) / sizeof(u32); 4378c2ecf20Sopenharmony_ci 4388c2ecf20Sopenharmony_ci /* write the length of data */ 4398c2ecf20Sopenharmony_ci if (op->mode == SS_OP_SHA1) { 4408c2ecf20Sopenharmony_ci __be64 *bits = (__be64 *)&bf[j]; 4418c2ecf20Sopenharmony_ci *bits = cpu_to_be64(op->byte_count << 3); 4428c2ecf20Sopenharmony_ci j += 2; 4438c2ecf20Sopenharmony_ci } else { 4448c2ecf20Sopenharmony_ci __le64 *bits = (__le64 *)&bf[j]; 4458c2ecf20Sopenharmony_ci *bits = cpu_to_le64(op->byte_count << 3); 4468c2ecf20Sopenharmony_ci j += 2; 4478c2ecf20Sopenharmony_ci } 4488c2ecf20Sopenharmony_ci writesl(ss->base + SS_RXFIFO, bf, j); 4498c2ecf20Sopenharmony_ci 4508c2ecf20Sopenharmony_ci /* Tell the SS to stop the hashing */ 4518c2ecf20Sopenharmony_ci writel(op->mode | SS_ENABLED | SS_DATA_END, ss->base + SS_CTL); 4528c2ecf20Sopenharmony_ci 4538c2ecf20Sopenharmony_ci /* 4548c2ecf20Sopenharmony_ci * Wait for SS to finish the hash. 4558c2ecf20Sopenharmony_ci * The timeout could happen only in case of bad overclocking 4568c2ecf20Sopenharmony_ci * or driver bug. 4578c2ecf20Sopenharmony_ci */ 4588c2ecf20Sopenharmony_ci i = 0; 4598c2ecf20Sopenharmony_ci do { 4608c2ecf20Sopenharmony_ci v = readl(ss->base + SS_CTL); 4618c2ecf20Sopenharmony_ci i++; 4628c2ecf20Sopenharmony_ci } while (i < SS_TIMEOUT && (v & SS_DATA_END)); 4638c2ecf20Sopenharmony_ci if (unlikely(i >= SS_TIMEOUT)) { 4648c2ecf20Sopenharmony_ci dev_err_ratelimited(ss->dev, 4658c2ecf20Sopenharmony_ci "ERROR: hash end timeout %d>%d ctl=%x len=%u\n", 4668c2ecf20Sopenharmony_ci i, SS_TIMEOUT, v, areq->nbytes); 4678c2ecf20Sopenharmony_ci err = -EIO; 4688c2ecf20Sopenharmony_ci goto release_ss; 4698c2ecf20Sopenharmony_ci } 4708c2ecf20Sopenharmony_ci 4718c2ecf20Sopenharmony_ci /* 4728c2ecf20Sopenharmony_ci * The datasheet isn't very clear about when to retrieve the digest. The 4738c2ecf20Sopenharmony_ci * bit SS_DATA_END is cleared when the engine has processed the data and 4748c2ecf20Sopenharmony_ci * when the digest is computed *but* it doesn't mean the digest is 4758c2ecf20Sopenharmony_ci * available in the digest registers. Hence the delay to be sure we can 4768c2ecf20Sopenharmony_ci * read it. 4778c2ecf20Sopenharmony_ci */ 4788c2ecf20Sopenharmony_ci ndelay(1); 4798c2ecf20Sopenharmony_ci 4808c2ecf20Sopenharmony_ci /* Get the hash from the device */ 4818c2ecf20Sopenharmony_ci if (op->mode == SS_OP_SHA1) { 4828c2ecf20Sopenharmony_ci for (i = 0; i < 5; i++) { 4838c2ecf20Sopenharmony_ci v = readl(ss->base + SS_MD0 + i * 4); 4848c2ecf20Sopenharmony_ci if (ss->variant->sha1_in_be) 4858c2ecf20Sopenharmony_ci put_unaligned_le32(v, areq->result + i * 4); 4868c2ecf20Sopenharmony_ci else 4878c2ecf20Sopenharmony_ci put_unaligned_be32(v, areq->result + i * 4); 4888c2ecf20Sopenharmony_ci } 4898c2ecf20Sopenharmony_ci } else { 4908c2ecf20Sopenharmony_ci for (i = 0; i < 4; i++) { 4918c2ecf20Sopenharmony_ci v = readl(ss->base + SS_MD0 + i * 4); 4928c2ecf20Sopenharmony_ci put_unaligned_le32(v, areq->result + i * 4); 4938c2ecf20Sopenharmony_ci } 4948c2ecf20Sopenharmony_ci } 4958c2ecf20Sopenharmony_ci 4968c2ecf20Sopenharmony_cirelease_ss: 4978c2ecf20Sopenharmony_ci writel(0, ss->base + SS_CTL); 4988c2ecf20Sopenharmony_ci spin_unlock_bh(&ss->slock); 4998c2ecf20Sopenharmony_ci return err; 5008c2ecf20Sopenharmony_ci} 5018c2ecf20Sopenharmony_ci 5028c2ecf20Sopenharmony_ciint sun4i_hash_final(struct ahash_request *areq) 5038c2ecf20Sopenharmony_ci{ 5048c2ecf20Sopenharmony_ci struct sun4i_req_ctx *op = ahash_request_ctx(areq); 5058c2ecf20Sopenharmony_ci 5068c2ecf20Sopenharmony_ci op->flags = SS_HASH_FINAL; 5078c2ecf20Sopenharmony_ci return sun4i_hash(areq); 5088c2ecf20Sopenharmony_ci} 5098c2ecf20Sopenharmony_ci 5108c2ecf20Sopenharmony_ciint sun4i_hash_update(struct ahash_request *areq) 5118c2ecf20Sopenharmony_ci{ 5128c2ecf20Sopenharmony_ci struct sun4i_req_ctx *op = ahash_request_ctx(areq); 5138c2ecf20Sopenharmony_ci 5148c2ecf20Sopenharmony_ci op->flags = SS_HASH_UPDATE; 5158c2ecf20Sopenharmony_ci return sun4i_hash(areq); 5168c2ecf20Sopenharmony_ci} 5178c2ecf20Sopenharmony_ci 5188c2ecf20Sopenharmony_ci/* sun4i_hash_finup: finalize hashing operation after an update */ 5198c2ecf20Sopenharmony_ciint sun4i_hash_finup(struct ahash_request *areq) 5208c2ecf20Sopenharmony_ci{ 5218c2ecf20Sopenharmony_ci struct sun4i_req_ctx *op = ahash_request_ctx(areq); 5228c2ecf20Sopenharmony_ci 5238c2ecf20Sopenharmony_ci op->flags = SS_HASH_UPDATE | SS_HASH_FINAL; 5248c2ecf20Sopenharmony_ci return sun4i_hash(areq); 5258c2ecf20Sopenharmony_ci} 5268c2ecf20Sopenharmony_ci 5278c2ecf20Sopenharmony_ci/* combo of init/update/final functions */ 5288c2ecf20Sopenharmony_ciint sun4i_hash_digest(struct ahash_request *areq) 5298c2ecf20Sopenharmony_ci{ 5308c2ecf20Sopenharmony_ci int err; 5318c2ecf20Sopenharmony_ci struct sun4i_req_ctx *op = ahash_request_ctx(areq); 5328c2ecf20Sopenharmony_ci 5338c2ecf20Sopenharmony_ci err = sun4i_hash_init(areq); 5348c2ecf20Sopenharmony_ci if (err) 5358c2ecf20Sopenharmony_ci return err; 5368c2ecf20Sopenharmony_ci 5378c2ecf20Sopenharmony_ci op->flags = SS_HASH_UPDATE | SS_HASH_FINAL; 5388c2ecf20Sopenharmony_ci return sun4i_hash(areq); 5398c2ecf20Sopenharmony_ci} 540