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