blob: ef6fb201f70db60e0c43600a7554a18182bad54f [file] [log] [blame]
Ryder Lee785e5c62016-12-19 10:20:44 +08001/*
2 * Cryptographic API.
3 *
4 * Driver for EIP97 SHA1/SHA2(HMAC) acceleration.
5 *
6 * Copyright (c) 2016 Ryder Lee <ryder.lee@mediatek.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
12 * Some ideas are from atmel-sha.c and omap-sham.c drivers.
13 */
14
15#include <crypto/sha.h>
16#include "mtk-platform.h"
17
18#define SHA_ALIGN_MSK (sizeof(u32) - 1)
19#define SHA_QUEUE_SIZE 512
Ryder Lee785e5c62016-12-19 10:20:44 +080020#define SHA_BUF_SIZE ((u32)PAGE_SIZE)
21
22#define SHA_OP_UPDATE 1
23#define SHA_OP_FINAL 2
24
25#define SHA_DATA_LEN_MSK cpu_to_le32(GENMASK(16, 0))
26
27/* SHA command token */
28#define SHA_CT_SIZE 5
29#define SHA_CT_CTRL_HDR cpu_to_le32(0x02220000)
Ryder Leea87399622017-01-20 13:41:08 +080030#define SHA_CMD0 cpu_to_le32(0x03020000)
31#define SHA_CMD1 cpu_to_le32(0x21060000)
32#define SHA_CMD2 cpu_to_le32(0xe0e63802)
Ryder Lee785e5c62016-12-19 10:20:44 +080033
34/* SHA transform information */
35#define SHA_TFM_HASH cpu_to_le32(0x2 << 0)
36#define SHA_TFM_INNER_DIG cpu_to_le32(0x1 << 21)
37#define SHA_TFM_SIZE(x) cpu_to_le32((x) << 8)
38#define SHA_TFM_START cpu_to_le32(0x1 << 4)
39#define SHA_TFM_CONTINUE cpu_to_le32(0x1 << 5)
40#define SHA_TFM_HASH_STORE cpu_to_le32(0x1 << 19)
41#define SHA_TFM_SHA1 cpu_to_le32(0x2 << 23)
42#define SHA_TFM_SHA256 cpu_to_le32(0x3 << 23)
43#define SHA_TFM_SHA224 cpu_to_le32(0x4 << 23)
44#define SHA_TFM_SHA512 cpu_to_le32(0x5 << 23)
45#define SHA_TFM_SHA384 cpu_to_le32(0x6 << 23)
46#define SHA_TFM_DIGEST(x) cpu_to_le32(((x) & GENMASK(3, 0)) << 24)
47
48/* SHA flags */
49#define SHA_FLAGS_BUSY BIT(0)
50#define SHA_FLAGS_FINAL BIT(1)
51#define SHA_FLAGS_FINUP BIT(2)
52#define SHA_FLAGS_SG BIT(3)
53#define SHA_FLAGS_ALGO_MSK GENMASK(8, 4)
54#define SHA_FLAGS_SHA1 BIT(4)
55#define SHA_FLAGS_SHA224 BIT(5)
56#define SHA_FLAGS_SHA256 BIT(6)
57#define SHA_FLAGS_SHA384 BIT(7)
58#define SHA_FLAGS_SHA512 BIT(8)
59#define SHA_FLAGS_HMAC BIT(9)
60#define SHA_FLAGS_PAD BIT(10)
61
62/**
63 * mtk_sha_ct is a set of hardware instructions(command token)
64 * that are used to control engine's processing flow of SHA,
65 * and it contains the first two words of transform state.
66 */
67struct mtk_sha_ct {
Ryder Leea87399622017-01-20 13:41:08 +080068 __le32 ctrl[2];
69 __le32 cmd[3];
Ryder Lee785e5c62016-12-19 10:20:44 +080070};
71
72/**
73 * mtk_sha_tfm is used to define SHA transform state
74 * and store result digest that produced by engine.
75 */
76struct mtk_sha_tfm {
Ryder Leea87399622017-01-20 13:41:08 +080077 __le32 ctrl[2];
Ryder Lee785e5c62016-12-19 10:20:44 +080078 __le32 digest[SIZE_IN_WORDS(SHA512_DIGEST_SIZE)];
79};
80
81/**
82 * mtk_sha_info consists of command token and transform state
83 * of SHA, its role is similar to mtk_aes_info.
84 */
85struct mtk_sha_info {
86 struct mtk_sha_ct ct;
87 struct mtk_sha_tfm tfm;
88};
89
90struct mtk_sha_reqctx {
91 struct mtk_sha_info info;
92 unsigned long flags;
93 unsigned long op;
94
95 u64 digcnt;
96 bool start;
97 size_t bufcnt;
98 dma_addr_t dma_addr;
99
Ryder Leea87399622017-01-20 13:41:08 +0800100 __le32 ct_hdr;
101 u32 ct_size;
102 dma_addr_t ct_dma;
103 dma_addr_t tfm_dma;
104
Ryder Lee785e5c62016-12-19 10:20:44 +0800105 /* Walk state */
106 struct scatterlist *sg;
107 u32 offset; /* Offset in current sg */
108 u32 total; /* Total request */
109 size_t ds;
110 size_t bs;
111
112 u8 *buffer;
113};
114
115struct mtk_sha_hmac_ctx {
116 struct crypto_shash *shash;
117 u8 ipad[SHA512_BLOCK_SIZE] __aligned(sizeof(u32));
118 u8 opad[SHA512_BLOCK_SIZE] __aligned(sizeof(u32));
119};
120
121struct mtk_sha_ctx {
122 struct mtk_cryp *cryp;
123 unsigned long flags;
124 u8 id;
125 u8 buf[SHA_BUF_SIZE] __aligned(sizeof(u32));
126
127 struct mtk_sha_hmac_ctx base[0];
128};
129
130struct mtk_sha_drv {
131 struct list_head dev_list;
132 /* Device list lock */
133 spinlock_t lock;
134};
135
136static struct mtk_sha_drv mtk_sha = {
137 .dev_list = LIST_HEAD_INIT(mtk_sha.dev_list),
138 .lock = __SPIN_LOCK_UNLOCKED(mtk_sha.lock),
139};
140
141static int mtk_sha_handle_queue(struct mtk_cryp *cryp, u8 id,
142 struct ahash_request *req);
143
144static inline u32 mtk_sha_read(struct mtk_cryp *cryp, u32 offset)
145{
146 return readl_relaxed(cryp->base + offset);
147}
148
149static inline void mtk_sha_write(struct mtk_cryp *cryp,
150 u32 offset, u32 value)
151{
152 writel_relaxed(value, cryp->base + offset);
153}
154
155static struct mtk_cryp *mtk_sha_find_dev(struct mtk_sha_ctx *tctx)
156{
157 struct mtk_cryp *cryp = NULL;
158 struct mtk_cryp *tmp;
159
160 spin_lock_bh(&mtk_sha.lock);
161 if (!tctx->cryp) {
162 list_for_each_entry(tmp, &mtk_sha.dev_list, sha_list) {
163 cryp = tmp;
164 break;
165 }
166 tctx->cryp = cryp;
167 } else {
168 cryp = tctx->cryp;
169 }
170
171 /*
172 * Assign record id to tfm in round-robin fashion, and this
173 * will help tfm to bind to corresponding descriptor rings.
174 */
175 tctx->id = cryp->rec;
176 cryp->rec = !cryp->rec;
177
178 spin_unlock_bh(&mtk_sha.lock);
179
180 return cryp;
181}
182
183static int mtk_sha_append_sg(struct mtk_sha_reqctx *ctx)
184{
185 size_t count;
186
187 while ((ctx->bufcnt < SHA_BUF_SIZE) && ctx->total) {
188 count = min(ctx->sg->length - ctx->offset, ctx->total);
189 count = min(count, SHA_BUF_SIZE - ctx->bufcnt);
190
191 if (count <= 0) {
192 /*
193 * Check if count <= 0 because the buffer is full or
194 * because the sg length is 0. In the latest case,
195 * check if there is another sg in the list, a 0 length
196 * sg doesn't necessarily mean the end of the sg list.
197 */
198 if ((ctx->sg->length == 0) && !sg_is_last(ctx->sg)) {
199 ctx->sg = sg_next(ctx->sg);
200 continue;
201 } else {
202 break;
203 }
204 }
205
206 scatterwalk_map_and_copy(ctx->buffer + ctx->bufcnt, ctx->sg,
207 ctx->offset, count, 0);
208
209 ctx->bufcnt += count;
210 ctx->offset += count;
211 ctx->total -= count;
212
213 if (ctx->offset == ctx->sg->length) {
214 ctx->sg = sg_next(ctx->sg);
215 if (ctx->sg)
216 ctx->offset = 0;
217 else
218 ctx->total = 0;
219 }
220 }
221
222 return 0;
223}
224
225/*
226 * The purpose of this padding is to ensure that the padded message is a
227 * multiple of 512 bits (SHA1/SHA224/SHA256) or 1024 bits (SHA384/SHA512).
228 * The bit "1" is appended at the end of the message followed by
229 * "padlen-1" zero bits. Then a 64 bits block (SHA1/SHA224/SHA256) or
230 * 128 bits block (SHA384/SHA512) equals to the message length in bits
231 * is appended.
232 *
233 * For SHA1/SHA224/SHA256, padlen is calculated as followed:
234 * - if message length < 56 bytes then padlen = 56 - message length
235 * - else padlen = 64 + 56 - message length
236 *
237 * For SHA384/SHA512, padlen is calculated as followed:
238 * - if message length < 112 bytes then padlen = 112 - message length
239 * - else padlen = 128 + 112 - message length
240 */
241static void mtk_sha_fill_padding(struct mtk_sha_reqctx *ctx, u32 len)
242{
243 u32 index, padlen;
244 u64 bits[2];
245 u64 size = ctx->digcnt;
246
247 size += ctx->bufcnt;
248 size += len;
249
250 bits[1] = cpu_to_be64(size << 3);
251 bits[0] = cpu_to_be64(size >> 61);
252
253 if (ctx->flags & (SHA_FLAGS_SHA384 | SHA_FLAGS_SHA512)) {
254 index = ctx->bufcnt & 0x7f;
255 padlen = (index < 112) ? (112 - index) : ((128 + 112) - index);
256 *(ctx->buffer + ctx->bufcnt) = 0x80;
257 memset(ctx->buffer + ctx->bufcnt + 1, 0, padlen - 1);
258 memcpy(ctx->buffer + ctx->bufcnt + padlen, bits, 16);
259 ctx->bufcnt += padlen + 16;
260 ctx->flags |= SHA_FLAGS_PAD;
261 } else {
262 index = ctx->bufcnt & 0x3f;
263 padlen = (index < 56) ? (56 - index) : ((64 + 56) - index);
264 *(ctx->buffer + ctx->bufcnt) = 0x80;
265 memset(ctx->buffer + ctx->bufcnt + 1, 0, padlen - 1);
266 memcpy(ctx->buffer + ctx->bufcnt + padlen, &bits[1], 8);
267 ctx->bufcnt += padlen + 8;
268 ctx->flags |= SHA_FLAGS_PAD;
269 }
270}
271
272/* Initialize basic transform information of SHA */
Ryder Leea87399622017-01-20 13:41:08 +0800273static void mtk_sha_info_init(struct mtk_sha_reqctx *ctx)
Ryder Lee785e5c62016-12-19 10:20:44 +0800274{
Ryder Leea87399622017-01-20 13:41:08 +0800275 struct mtk_sha_ct *ct = &ctx->info.ct;
276 struct mtk_sha_tfm *tfm = &ctx->info.tfm;
Ryder Lee785e5c62016-12-19 10:20:44 +0800277
Ryder Leea87399622017-01-20 13:41:08 +0800278 ctx->ct_hdr = SHA_CT_CTRL_HDR;
279 ctx->ct_size = SHA_CT_SIZE;
Ryder Lee785e5c62016-12-19 10:20:44 +0800280
Ryder Leea87399622017-01-20 13:41:08 +0800281 tfm->ctrl[0] = SHA_TFM_HASH | SHA_TFM_INNER_DIG |
282 SHA_TFM_SIZE(SIZE_IN_WORDS(ctx->ds));
Ryder Lee785e5c62016-12-19 10:20:44 +0800283
284 switch (ctx->flags & SHA_FLAGS_ALGO_MSK) {
285 case SHA_FLAGS_SHA1:
Ryder Leea87399622017-01-20 13:41:08 +0800286 tfm->ctrl[0] |= SHA_TFM_SHA1;
Ryder Lee785e5c62016-12-19 10:20:44 +0800287 break;
288 case SHA_FLAGS_SHA224:
Ryder Leea87399622017-01-20 13:41:08 +0800289 tfm->ctrl[0] |= SHA_TFM_SHA224;
Ryder Lee785e5c62016-12-19 10:20:44 +0800290 break;
291 case SHA_FLAGS_SHA256:
Ryder Leea87399622017-01-20 13:41:08 +0800292 tfm->ctrl[0] |= SHA_TFM_SHA256;
Ryder Lee785e5c62016-12-19 10:20:44 +0800293 break;
294 case SHA_FLAGS_SHA384:
Ryder Leea87399622017-01-20 13:41:08 +0800295 tfm->ctrl[0] |= SHA_TFM_SHA384;
Ryder Lee785e5c62016-12-19 10:20:44 +0800296 break;
297 case SHA_FLAGS_SHA512:
Ryder Leea87399622017-01-20 13:41:08 +0800298 tfm->ctrl[0] |= SHA_TFM_SHA512;
Ryder Lee785e5c62016-12-19 10:20:44 +0800299 break;
300
301 default:
302 /* Should not happen... */
303 return;
304 }
305
Ryder Leea87399622017-01-20 13:41:08 +0800306 tfm->ctrl[1] = SHA_TFM_HASH_STORE;
307 ct->ctrl[0] = tfm->ctrl[0] | SHA_TFM_CONTINUE | SHA_TFM_START;
308 ct->ctrl[1] = tfm->ctrl[1];
Ryder Lee785e5c62016-12-19 10:20:44 +0800309
Ryder Leea87399622017-01-20 13:41:08 +0800310 ct->cmd[0] = SHA_CMD0;
311 ct->cmd[1] = SHA_CMD1;
312 ct->cmd[2] = SHA_CMD2 | SHA_TFM_DIGEST(SIZE_IN_WORDS(ctx->ds));
Ryder Lee785e5c62016-12-19 10:20:44 +0800313}
314
315/*
316 * Update input data length field of transform information and
317 * map it to DMA region.
318 */
Ryder Lee059b1492017-01-20 13:41:13 +0800319static int mtk_sha_info_update(struct mtk_cryp *cryp,
320 struct mtk_sha_rec *sha,
Ryder Lee82445fe2017-03-09 10:11:14 +0800321 size_t len1, size_t len2)
Ryder Lee785e5c62016-12-19 10:20:44 +0800322{
323 struct mtk_sha_reqctx *ctx = ahash_request_ctx(sha->req);
Ryder Leea87399622017-01-20 13:41:08 +0800324 struct mtk_sha_info *info = &ctx->info;
Ryder Lee785e5c62016-12-19 10:20:44 +0800325 struct mtk_sha_ct *ct = &info->ct;
326
327 if (ctx->start)
328 ctx->start = false;
329 else
Ryder Leea87399622017-01-20 13:41:08 +0800330 ct->ctrl[0] &= ~SHA_TFM_START;
Ryder Lee785e5c62016-12-19 10:20:44 +0800331
Ryder Leea87399622017-01-20 13:41:08 +0800332 ctx->ct_hdr &= ~SHA_DATA_LEN_MSK;
Ryder Lee82445fe2017-03-09 10:11:14 +0800333 ctx->ct_hdr |= cpu_to_le32(len1 + len2);
Ryder Leea87399622017-01-20 13:41:08 +0800334 ct->cmd[0] &= ~SHA_DATA_LEN_MSK;
Ryder Lee82445fe2017-03-09 10:11:14 +0800335 ct->cmd[0] |= cpu_to_le32(len1 + len2);
Ryder Lee785e5c62016-12-19 10:20:44 +0800336
Ryder Lee82445fe2017-03-09 10:11:14 +0800337 ctx->digcnt += len1;
Ryder Lee785e5c62016-12-19 10:20:44 +0800338
Ryder Leea87399622017-01-20 13:41:08 +0800339 ctx->ct_dma = dma_map_single(cryp->dev, info, sizeof(*info),
Ryder Lee059b1492017-01-20 13:41:13 +0800340 DMA_BIDIRECTIONAL);
Ryder Leea87399622017-01-20 13:41:08 +0800341 if (unlikely(dma_mapping_error(cryp->dev, ctx->ct_dma))) {
Arnd Bergmann41e05322017-01-11 14:55:20 +0100342 dev_err(cryp->dev, "dma %zu bytes error\n", sizeof(*info));
Ryder Lee785e5c62016-12-19 10:20:44 +0800343 return -EINVAL;
344 }
Ryder Leea87399622017-01-20 13:41:08 +0800345 ctx->tfm_dma = ctx->ct_dma + sizeof(*ct);
Ryder Lee785e5c62016-12-19 10:20:44 +0800346
347 return 0;
348}
349
350/*
351 * Because of hardware limitation, we must pre-calculate the inner
352 * and outer digest that need to be processed firstly by engine, then
353 * apply the result digest to the input message. These complex hashing
354 * procedures limits HMAC performance, so we use fallback SW encoding.
355 */
356static int mtk_sha_finish_hmac(struct ahash_request *req)
357{
358 struct mtk_sha_ctx *tctx = crypto_tfm_ctx(req->base.tfm);
359 struct mtk_sha_hmac_ctx *bctx = tctx->base;
360 struct mtk_sha_reqctx *ctx = ahash_request_ctx(req);
361
362 SHASH_DESC_ON_STACK(shash, bctx->shash);
363
364 shash->tfm = bctx->shash;
365 shash->flags = 0; /* not CRYPTO_TFM_REQ_MAY_SLEEP */
366
367 return crypto_shash_init(shash) ?:
368 crypto_shash_update(shash, bctx->opad, ctx->bs) ?:
369 crypto_shash_finup(shash, req->result, ctx->ds, req->result);
370}
371
372/* Initialize request context */
373static int mtk_sha_init(struct ahash_request *req)
374{
375 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
376 struct mtk_sha_ctx *tctx = crypto_ahash_ctx(tfm);
377 struct mtk_sha_reqctx *ctx = ahash_request_ctx(req);
378
379 ctx->flags = 0;
380 ctx->ds = crypto_ahash_digestsize(tfm);
381
382 switch (ctx->ds) {
383 case SHA1_DIGEST_SIZE:
384 ctx->flags |= SHA_FLAGS_SHA1;
385 ctx->bs = SHA1_BLOCK_SIZE;
386 break;
387 case SHA224_DIGEST_SIZE:
388 ctx->flags |= SHA_FLAGS_SHA224;
389 ctx->bs = SHA224_BLOCK_SIZE;
390 break;
391 case SHA256_DIGEST_SIZE:
392 ctx->flags |= SHA_FLAGS_SHA256;
393 ctx->bs = SHA256_BLOCK_SIZE;
394 break;
395 case SHA384_DIGEST_SIZE:
396 ctx->flags |= SHA_FLAGS_SHA384;
397 ctx->bs = SHA384_BLOCK_SIZE;
398 break;
399 case SHA512_DIGEST_SIZE:
400 ctx->flags |= SHA_FLAGS_SHA512;
401 ctx->bs = SHA512_BLOCK_SIZE;
402 break;
403 default:
404 return -EINVAL;
405 }
406
407 ctx->bufcnt = 0;
408 ctx->digcnt = 0;
409 ctx->buffer = tctx->buf;
410 ctx->start = true;
411
412 if (tctx->flags & SHA_FLAGS_HMAC) {
413 struct mtk_sha_hmac_ctx *bctx = tctx->base;
414
415 memcpy(ctx->buffer, bctx->ipad, ctx->bs);
416 ctx->bufcnt = ctx->bs;
417 ctx->flags |= SHA_FLAGS_HMAC;
418 }
419
420 return 0;
421}
422
423static int mtk_sha_xmit(struct mtk_cryp *cryp, struct mtk_sha_rec *sha,
Ryder Lee82445fe2017-03-09 10:11:14 +0800424 dma_addr_t addr1, size_t len1,
425 dma_addr_t addr2, size_t len2)
Ryder Lee785e5c62016-12-19 10:20:44 +0800426{
Ryder Leea87399622017-01-20 13:41:08 +0800427 struct mtk_sha_reqctx *ctx = ahash_request_ctx(sha->req);
Ryder Lee785e5c62016-12-19 10:20:44 +0800428 struct mtk_ring *ring = cryp->ring[sha->id];
Ryder Lee44328612017-01-20 13:41:09 +0800429 struct mtk_desc *cmd = ring->cmd_base + ring->cmd_pos;
430 struct mtk_desc *res = ring->res_base + ring->res_pos;
Ryder Lee82445fe2017-03-09 10:11:14 +0800431 int err, count = 0;
Ryder Lee785e5c62016-12-19 10:20:44 +0800432
Ryder Lee82445fe2017-03-09 10:11:14 +0800433 err = mtk_sha_info_update(cryp, sha, len1, len2);
Ryder Lee785e5c62016-12-19 10:20:44 +0800434 if (err)
435 return err;
436
437 /* Fill in the command/result descriptors */
Ryder Lee82445fe2017-03-09 10:11:14 +0800438 res->hdr = MTK_DESC_FIRST | MTK_DESC_BUF_LEN(len1);
439 cmd->hdr = MTK_DESC_FIRST | MTK_DESC_BUF_LEN(len1) |
Ryder Leea87399622017-01-20 13:41:08 +0800440 MTK_DESC_CT_LEN(ctx->ct_size);
Ryder Lee82445fe2017-03-09 10:11:14 +0800441 cmd->buf = cpu_to_le32(addr1);
Ryder Leea87399622017-01-20 13:41:08 +0800442 cmd->ct = cpu_to_le32(ctx->ct_dma);
443 cmd->ct_hdr = ctx->ct_hdr;
444 cmd->tfm = cpu_to_le32(ctx->tfm_dma);
Ryder Lee785e5c62016-12-19 10:20:44 +0800445
Ryder Lee44328612017-01-20 13:41:09 +0800446 if (++ring->cmd_pos == MTK_DESC_NUM)
447 ring->cmd_pos = 0;
Ryder Lee785e5c62016-12-19 10:20:44 +0800448
Ryder Lee44328612017-01-20 13:41:09 +0800449 ring->res_pos = ring->cmd_pos;
Ryder Lee82445fe2017-03-09 10:11:14 +0800450 count++;
Ryder Lee785e5c62016-12-19 10:20:44 +0800451
Ryder Lee82445fe2017-03-09 10:11:14 +0800452 if (len2) {
453 cmd = ring->cmd_base + ring->cmd_pos;
454 res = ring->res_base + ring->res_pos;
Ryder Lee785e5c62016-12-19 10:20:44 +0800455
Ryder Lee82445fe2017-03-09 10:11:14 +0800456 res->hdr = MTK_DESC_BUF_LEN(len2);
457 cmd->hdr = MTK_DESC_BUF_LEN(len2);
458 cmd->buf = cpu_to_le32(addr2);
Ryder Lee785e5c62016-12-19 10:20:44 +0800459
Ryder Lee82445fe2017-03-09 10:11:14 +0800460 if (++ring->cmd_pos == MTK_DESC_NUM)
461 ring->cmd_pos = 0;
Ryder Lee785e5c62016-12-19 10:20:44 +0800462
Ryder Lee82445fe2017-03-09 10:11:14 +0800463 ring->res_pos = ring->cmd_pos;
464 count++;
465 }
Ryder Lee785e5c62016-12-19 10:20:44 +0800466
Ryder Lee82445fe2017-03-09 10:11:14 +0800467 cmd->hdr |= MTK_DESC_LAST;
468 res->hdr |= MTK_DESC_LAST;
Ryder Lee785e5c62016-12-19 10:20:44 +0800469
470 /*
471 * Make sure that all changes to the DMA ring are done before we
472 * start engine.
473 */
474 wmb();
475 /* Start DMA transfer */
Ryder Lee82445fe2017-03-09 10:11:14 +0800476 mtk_sha_write(cryp, RDR_PREP_COUNT(sha->id), MTK_DESC_CNT(count));
477 mtk_sha_write(cryp, CDR_PREP_COUNT(sha->id), MTK_DESC_CNT(count));
Ryder Lee785e5c62016-12-19 10:20:44 +0800478
479 return -EINPROGRESS;
480}
481
482static int mtk_sha_dma_map(struct mtk_cryp *cryp,
483 struct mtk_sha_rec *sha,
484 struct mtk_sha_reqctx *ctx,
485 size_t count)
486{
487 ctx->dma_addr = dma_map_single(cryp->dev, ctx->buffer,
Ryder Lee059b1492017-01-20 13:41:13 +0800488 SHA_BUF_SIZE, DMA_TO_DEVICE);
Ryder Lee785e5c62016-12-19 10:20:44 +0800489 if (unlikely(dma_mapping_error(cryp->dev, ctx->dma_addr))) {
490 dev_err(cryp->dev, "dma map error\n");
491 return -EINVAL;
492 }
493
494 ctx->flags &= ~SHA_FLAGS_SG;
495
Ryder Lee82445fe2017-03-09 10:11:14 +0800496 return mtk_sha_xmit(cryp, sha, ctx->dma_addr, count, 0, 0);
Ryder Lee785e5c62016-12-19 10:20:44 +0800497}
498
499static int mtk_sha_update_slow(struct mtk_cryp *cryp,
500 struct mtk_sha_rec *sha)
501{
502 struct mtk_sha_reqctx *ctx = ahash_request_ctx(sha->req);
503 size_t count;
504 u32 final;
505
506 mtk_sha_append_sg(ctx);
507
508 final = (ctx->flags & SHA_FLAGS_FINUP) && !ctx->total;
509
Arnd Bergmann41e05322017-01-11 14:55:20 +0100510 dev_dbg(cryp->dev, "slow: bufcnt: %zu\n", ctx->bufcnt);
Ryder Lee785e5c62016-12-19 10:20:44 +0800511
512 if (final) {
513 sha->flags |= SHA_FLAGS_FINAL;
514 mtk_sha_fill_padding(ctx, 0);
515 }
516
517 if (final || (ctx->bufcnt == SHA_BUF_SIZE && ctx->total)) {
518 count = ctx->bufcnt;
519 ctx->bufcnt = 0;
520
521 return mtk_sha_dma_map(cryp, sha, ctx, count);
522 }
523 return 0;
524}
525
526static int mtk_sha_update_start(struct mtk_cryp *cryp,
527 struct mtk_sha_rec *sha)
528{
529 struct mtk_sha_reqctx *ctx = ahash_request_ctx(sha->req);
530 u32 len, final, tail;
531 struct scatterlist *sg;
532
533 if (!ctx->total)
534 return 0;
535
536 if (ctx->bufcnt || ctx->offset)
537 return mtk_sha_update_slow(cryp, sha);
538
539 sg = ctx->sg;
540
541 if (!IS_ALIGNED(sg->offset, sizeof(u32)))
542 return mtk_sha_update_slow(cryp, sha);
543
544 if (!sg_is_last(sg) && !IS_ALIGNED(sg->length, ctx->bs))
545 /* size is not ctx->bs aligned */
546 return mtk_sha_update_slow(cryp, sha);
547
548 len = min(ctx->total, sg->length);
549
550 if (sg_is_last(sg)) {
551 if (!(ctx->flags & SHA_FLAGS_FINUP)) {
552 /* not last sg must be ctx->bs aligned */
553 tail = len & (ctx->bs - 1);
554 len -= tail;
555 }
556 }
557
558 ctx->total -= len;
559 ctx->offset = len; /* offset where to start slow */
560
561 final = (ctx->flags & SHA_FLAGS_FINUP) && !ctx->total;
562
563 /* Add padding */
564 if (final) {
565 size_t count;
566
567 tail = len & (ctx->bs - 1);
568 len -= tail;
569 ctx->total += tail;
570 ctx->offset = len; /* offset where to start slow */
571
572 sg = ctx->sg;
573 mtk_sha_append_sg(ctx);
574 mtk_sha_fill_padding(ctx, len);
575
576 ctx->dma_addr = dma_map_single(cryp->dev, ctx->buffer,
Ryder Lee059b1492017-01-20 13:41:13 +0800577 SHA_BUF_SIZE, DMA_TO_DEVICE);
Ryder Lee785e5c62016-12-19 10:20:44 +0800578 if (unlikely(dma_mapping_error(cryp->dev, ctx->dma_addr))) {
579 dev_err(cryp->dev, "dma map bytes error\n");
580 return -EINVAL;
581 }
582
583 sha->flags |= SHA_FLAGS_FINAL;
584 count = ctx->bufcnt;
585 ctx->bufcnt = 0;
586
587 if (len == 0) {
588 ctx->flags &= ~SHA_FLAGS_SG;
Ryder Lee82445fe2017-03-09 10:11:14 +0800589 return mtk_sha_xmit(cryp, sha, ctx->dma_addr,
590 count, 0, 0);
Ryder Lee785e5c62016-12-19 10:20:44 +0800591
592 } else {
593 ctx->sg = sg;
594 if (!dma_map_sg(cryp->dev, ctx->sg, 1, DMA_TO_DEVICE)) {
595 dev_err(cryp->dev, "dma_map_sg error\n");
596 return -EINVAL;
597 }
598
599 ctx->flags |= SHA_FLAGS_SG;
Ryder Lee82445fe2017-03-09 10:11:14 +0800600 return mtk_sha_xmit(cryp, sha, sg_dma_address(ctx->sg),
601 len, ctx->dma_addr, count);
Ryder Lee785e5c62016-12-19 10:20:44 +0800602 }
603 }
604
605 if (!dma_map_sg(cryp->dev, ctx->sg, 1, DMA_TO_DEVICE)) {
606 dev_err(cryp->dev, "dma_map_sg error\n");
607 return -EINVAL;
608 }
609
610 ctx->flags |= SHA_FLAGS_SG;
611
Ryder Lee82445fe2017-03-09 10:11:14 +0800612 return mtk_sha_xmit(cryp, sha, sg_dma_address(ctx->sg),
613 len, 0, 0);
Ryder Lee785e5c62016-12-19 10:20:44 +0800614}
615
616static int mtk_sha_final_req(struct mtk_cryp *cryp,
617 struct mtk_sha_rec *sha)
618{
Ryder Lee059b1492017-01-20 13:41:13 +0800619 struct mtk_sha_reqctx *ctx = ahash_request_ctx(sha->req);
Ryder Lee785e5c62016-12-19 10:20:44 +0800620 size_t count;
621
622 mtk_sha_fill_padding(ctx, 0);
623
624 sha->flags |= SHA_FLAGS_FINAL;
625 count = ctx->bufcnt;
626 ctx->bufcnt = 0;
627
628 return mtk_sha_dma_map(cryp, sha, ctx, count);
629}
630
631/* Copy ready hash (+ finalize hmac) */
632static int mtk_sha_finish(struct ahash_request *req)
633{
634 struct mtk_sha_reqctx *ctx = ahash_request_ctx(req);
635 u32 *digest = ctx->info.tfm.digest;
636 u32 *result = (u32 *)req->result;
637 int i;
638
639 /* Get the hash from the digest buffer */
640 for (i = 0; i < SIZE_IN_WORDS(ctx->ds); i++)
641 result[i] = le32_to_cpu(digest[i]);
642
643 if (ctx->flags & SHA_FLAGS_HMAC)
644 return mtk_sha_finish_hmac(req);
645
646 return 0;
647}
648
649static void mtk_sha_finish_req(struct mtk_cryp *cryp,
Ryder Lee059b1492017-01-20 13:41:13 +0800650 struct mtk_sha_rec *sha,
651 int err)
Ryder Lee785e5c62016-12-19 10:20:44 +0800652{
653 if (likely(!err && (SHA_FLAGS_FINAL & sha->flags)))
654 err = mtk_sha_finish(sha->req);
655
656 sha->flags &= ~(SHA_FLAGS_BUSY | SHA_FLAGS_FINAL);
657
658 sha->req->base.complete(&sha->req->base, err);
659
660 /* Handle new request */
Ryder Leeb7a2be32017-03-09 10:11:13 +0800661 mtk_sha_handle_queue(cryp, sha->id - MTK_RING2, NULL);
Ryder Lee785e5c62016-12-19 10:20:44 +0800662}
663
664static int mtk_sha_handle_queue(struct mtk_cryp *cryp, u8 id,
665 struct ahash_request *req)
666{
667 struct mtk_sha_rec *sha = cryp->sha[id];
668 struct crypto_async_request *async_req, *backlog;
669 struct mtk_sha_reqctx *ctx;
670 unsigned long flags;
671 int err = 0, ret = 0;
672
673 spin_lock_irqsave(&sha->lock, flags);
674 if (req)
675 ret = ahash_enqueue_request(&sha->queue, req);
676
677 if (SHA_FLAGS_BUSY & sha->flags) {
678 spin_unlock_irqrestore(&sha->lock, flags);
679 return ret;
680 }
681
682 backlog = crypto_get_backlog(&sha->queue);
683 async_req = crypto_dequeue_request(&sha->queue);
684 if (async_req)
685 sha->flags |= SHA_FLAGS_BUSY;
686 spin_unlock_irqrestore(&sha->lock, flags);
687
688 if (!async_req)
689 return ret;
690
691 if (backlog)
692 backlog->complete(backlog, -EINPROGRESS);
693
694 req = ahash_request_cast(async_req);
695 ctx = ahash_request_ctx(req);
696
697 sha->req = req;
Ryder Lee785e5c62016-12-19 10:20:44 +0800698
Ryder Leea87399622017-01-20 13:41:08 +0800699 mtk_sha_info_init(ctx);
Ryder Lee785e5c62016-12-19 10:20:44 +0800700
701 if (ctx->op == SHA_OP_UPDATE) {
702 err = mtk_sha_update_start(cryp, sha);
703 if (err != -EINPROGRESS && (ctx->flags & SHA_FLAGS_FINUP))
704 /* No final() after finup() */
705 err = mtk_sha_final_req(cryp, sha);
706 } else if (ctx->op == SHA_OP_FINAL) {
707 err = mtk_sha_final_req(cryp, sha);
708 }
709
710 if (unlikely(err != -EINPROGRESS))
711 /* Task will not finish it, so do it here */
712 mtk_sha_finish_req(cryp, sha, err);
713
714 return ret;
715}
716
717static int mtk_sha_enqueue(struct ahash_request *req, u32 op)
718{
719 struct mtk_sha_reqctx *ctx = ahash_request_ctx(req);
720 struct mtk_sha_ctx *tctx = crypto_tfm_ctx(req->base.tfm);
721
722 ctx->op = op;
723
724 return mtk_sha_handle_queue(tctx->cryp, tctx->id, req);
725}
726
727static void mtk_sha_unmap(struct mtk_cryp *cryp, struct mtk_sha_rec *sha)
728{
729 struct mtk_sha_reqctx *ctx = ahash_request_ctx(sha->req);
730
Ryder Leea87399622017-01-20 13:41:08 +0800731 dma_unmap_single(cryp->dev, ctx->ct_dma, sizeof(ctx->info),
732 DMA_BIDIRECTIONAL);
Ryder Lee785e5c62016-12-19 10:20:44 +0800733
734 if (ctx->flags & SHA_FLAGS_SG) {
735 dma_unmap_sg(cryp->dev, ctx->sg, 1, DMA_TO_DEVICE);
736 if (ctx->sg->length == ctx->offset) {
737 ctx->sg = sg_next(ctx->sg);
738 if (ctx->sg)
739 ctx->offset = 0;
740 }
741 if (ctx->flags & SHA_FLAGS_PAD) {
742 dma_unmap_single(cryp->dev, ctx->dma_addr,
743 SHA_BUF_SIZE, DMA_TO_DEVICE);
744 }
745 } else
746 dma_unmap_single(cryp->dev, ctx->dma_addr,
747 SHA_BUF_SIZE, DMA_TO_DEVICE);
748}
749
750static void mtk_sha_complete(struct mtk_cryp *cryp,
751 struct mtk_sha_rec *sha)
752{
753 int err = 0;
754
755 err = mtk_sha_update_start(cryp, sha);
756 if (err != -EINPROGRESS)
757 mtk_sha_finish_req(cryp, sha, err);
758}
759
760static int mtk_sha_update(struct ahash_request *req)
761{
762 struct mtk_sha_reqctx *ctx = ahash_request_ctx(req);
763
764 ctx->total = req->nbytes;
765 ctx->sg = req->src;
766 ctx->offset = 0;
767
768 if ((ctx->bufcnt + ctx->total < SHA_BUF_SIZE) &&
769 !(ctx->flags & SHA_FLAGS_FINUP))
770 return mtk_sha_append_sg(ctx);
771
772 return mtk_sha_enqueue(req, SHA_OP_UPDATE);
773}
774
775static int mtk_sha_final(struct ahash_request *req)
776{
777 struct mtk_sha_reqctx *ctx = ahash_request_ctx(req);
778
779 ctx->flags |= SHA_FLAGS_FINUP;
780
781 if (ctx->flags & SHA_FLAGS_PAD)
782 return mtk_sha_finish(req);
783
784 return mtk_sha_enqueue(req, SHA_OP_FINAL);
785}
786
787static int mtk_sha_finup(struct ahash_request *req)
788{
789 struct mtk_sha_reqctx *ctx = ahash_request_ctx(req);
790 int err1, err2;
791
792 ctx->flags |= SHA_FLAGS_FINUP;
793
794 err1 = mtk_sha_update(req);
795 if (err1 == -EINPROGRESS || err1 == -EBUSY)
796 return err1;
797 /*
798 * final() has to be always called to cleanup resources
799 * even if update() failed
800 */
801 err2 = mtk_sha_final(req);
802
803 return err1 ?: err2;
804}
805
806static int mtk_sha_digest(struct ahash_request *req)
807{
808 return mtk_sha_init(req) ?: mtk_sha_finup(req);
809}
810
Ryder Lee059b1492017-01-20 13:41:13 +0800811static int mtk_sha_setkey(struct crypto_ahash *tfm, const u8 *key,
812 u32 keylen)
Ryder Lee785e5c62016-12-19 10:20:44 +0800813{
814 struct mtk_sha_ctx *tctx = crypto_ahash_ctx(tfm);
815 struct mtk_sha_hmac_ctx *bctx = tctx->base;
816 size_t bs = crypto_shash_blocksize(bctx->shash);
817 size_t ds = crypto_shash_digestsize(bctx->shash);
818 int err, i;
819
820 SHASH_DESC_ON_STACK(shash, bctx->shash);
821
822 shash->tfm = bctx->shash;
823 shash->flags = crypto_shash_get_flags(bctx->shash) &
Ryder Lee059b1492017-01-20 13:41:13 +0800824 CRYPTO_TFM_REQ_MAY_SLEEP;
Ryder Lee785e5c62016-12-19 10:20:44 +0800825
826 if (keylen > bs) {
827 err = crypto_shash_digest(shash, key, keylen, bctx->ipad);
828 if (err)
829 return err;
830 keylen = ds;
831 } else {
832 memcpy(bctx->ipad, key, keylen);
833 }
834
835 memset(bctx->ipad + keylen, 0, bs - keylen);
836 memcpy(bctx->opad, bctx->ipad, bs);
837
838 for (i = 0; i < bs; i++) {
839 bctx->ipad[i] ^= 0x36;
840 bctx->opad[i] ^= 0x5c;
841 }
842
Colin Ian Kingf2831482017-01-03 13:21:22 +0000843 return 0;
Ryder Lee785e5c62016-12-19 10:20:44 +0800844}
845
846static int mtk_sha_export(struct ahash_request *req, void *out)
847{
848 const struct mtk_sha_reqctx *ctx = ahash_request_ctx(req);
849
850 memcpy(out, ctx, sizeof(*ctx));
851 return 0;
852}
853
854static int mtk_sha_import(struct ahash_request *req, const void *in)
855{
856 struct mtk_sha_reqctx *ctx = ahash_request_ctx(req);
857
858 memcpy(ctx, in, sizeof(*ctx));
859 return 0;
860}
861
862static int mtk_sha_cra_init_alg(struct crypto_tfm *tfm,
863 const char *alg_base)
864{
865 struct mtk_sha_ctx *tctx = crypto_tfm_ctx(tfm);
866 struct mtk_cryp *cryp = NULL;
867
868 cryp = mtk_sha_find_dev(tctx);
869 if (!cryp)
870 return -ENODEV;
871
872 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
873 sizeof(struct mtk_sha_reqctx));
874
875 if (alg_base) {
876 struct mtk_sha_hmac_ctx *bctx = tctx->base;
877
878 tctx->flags |= SHA_FLAGS_HMAC;
879 bctx->shash = crypto_alloc_shash(alg_base, 0,
880 CRYPTO_ALG_NEED_FALLBACK);
881 if (IS_ERR(bctx->shash)) {
882 pr_err("base driver %s could not be loaded.\n",
883 alg_base);
884
885 return PTR_ERR(bctx->shash);
886 }
887 }
888 return 0;
889}
890
891static int mtk_sha_cra_init(struct crypto_tfm *tfm)
892{
893 return mtk_sha_cra_init_alg(tfm, NULL);
894}
895
896static int mtk_sha_cra_sha1_init(struct crypto_tfm *tfm)
897{
898 return mtk_sha_cra_init_alg(tfm, "sha1");
899}
900
901static int mtk_sha_cra_sha224_init(struct crypto_tfm *tfm)
902{
903 return mtk_sha_cra_init_alg(tfm, "sha224");
904}
905
906static int mtk_sha_cra_sha256_init(struct crypto_tfm *tfm)
907{
908 return mtk_sha_cra_init_alg(tfm, "sha256");
909}
910
911static int mtk_sha_cra_sha384_init(struct crypto_tfm *tfm)
912{
913 return mtk_sha_cra_init_alg(tfm, "sha384");
914}
915
916static int mtk_sha_cra_sha512_init(struct crypto_tfm *tfm)
917{
918 return mtk_sha_cra_init_alg(tfm, "sha512");
919}
920
921static void mtk_sha_cra_exit(struct crypto_tfm *tfm)
922{
923 struct mtk_sha_ctx *tctx = crypto_tfm_ctx(tfm);
924
925 if (tctx->flags & SHA_FLAGS_HMAC) {
926 struct mtk_sha_hmac_ctx *bctx = tctx->base;
927
928 crypto_free_shash(bctx->shash);
929 }
930}
931
932static struct ahash_alg algs_sha1_sha224_sha256[] = {
933{
934 .init = mtk_sha_init,
935 .update = mtk_sha_update,
936 .final = mtk_sha_final,
937 .finup = mtk_sha_finup,
938 .digest = mtk_sha_digest,
939 .export = mtk_sha_export,
940 .import = mtk_sha_import,
941 .halg.digestsize = SHA1_DIGEST_SIZE,
942 .halg.statesize = sizeof(struct mtk_sha_reqctx),
943 .halg.base = {
944 .cra_name = "sha1",
945 .cra_driver_name = "mtk-sha1",
946 .cra_priority = 400,
947 .cra_flags = CRYPTO_ALG_ASYNC,
948 .cra_blocksize = SHA1_BLOCK_SIZE,
949 .cra_ctxsize = sizeof(struct mtk_sha_ctx),
950 .cra_alignmask = SHA_ALIGN_MSK,
951 .cra_module = THIS_MODULE,
952 .cra_init = mtk_sha_cra_init,
953 .cra_exit = mtk_sha_cra_exit,
954 }
955},
956{
957 .init = mtk_sha_init,
958 .update = mtk_sha_update,
959 .final = mtk_sha_final,
960 .finup = mtk_sha_finup,
961 .digest = mtk_sha_digest,
962 .export = mtk_sha_export,
963 .import = mtk_sha_import,
964 .halg.digestsize = SHA224_DIGEST_SIZE,
965 .halg.statesize = sizeof(struct mtk_sha_reqctx),
966 .halg.base = {
967 .cra_name = "sha224",
968 .cra_driver_name = "mtk-sha224",
969 .cra_priority = 400,
970 .cra_flags = CRYPTO_ALG_ASYNC,
971 .cra_blocksize = SHA224_BLOCK_SIZE,
972 .cra_ctxsize = sizeof(struct mtk_sha_ctx),
973 .cra_alignmask = SHA_ALIGN_MSK,
974 .cra_module = THIS_MODULE,
975 .cra_init = mtk_sha_cra_init,
976 .cra_exit = mtk_sha_cra_exit,
977 }
978},
979{
980 .init = mtk_sha_init,
981 .update = mtk_sha_update,
982 .final = mtk_sha_final,
983 .finup = mtk_sha_finup,
984 .digest = mtk_sha_digest,
985 .export = mtk_sha_export,
986 .import = mtk_sha_import,
987 .halg.digestsize = SHA256_DIGEST_SIZE,
988 .halg.statesize = sizeof(struct mtk_sha_reqctx),
989 .halg.base = {
990 .cra_name = "sha256",
991 .cra_driver_name = "mtk-sha256",
992 .cra_priority = 400,
993 .cra_flags = CRYPTO_ALG_ASYNC,
994 .cra_blocksize = SHA256_BLOCK_SIZE,
995 .cra_ctxsize = sizeof(struct mtk_sha_ctx),
996 .cra_alignmask = SHA_ALIGN_MSK,
997 .cra_module = THIS_MODULE,
998 .cra_init = mtk_sha_cra_init,
999 .cra_exit = mtk_sha_cra_exit,
1000 }
1001},
1002{
1003 .init = mtk_sha_init,
1004 .update = mtk_sha_update,
1005 .final = mtk_sha_final,
1006 .finup = mtk_sha_finup,
1007 .digest = mtk_sha_digest,
1008 .export = mtk_sha_export,
1009 .import = mtk_sha_import,
1010 .setkey = mtk_sha_setkey,
1011 .halg.digestsize = SHA1_DIGEST_SIZE,
1012 .halg.statesize = sizeof(struct mtk_sha_reqctx),
1013 .halg.base = {
1014 .cra_name = "hmac(sha1)",
1015 .cra_driver_name = "mtk-hmac-sha1",
1016 .cra_priority = 400,
1017 .cra_flags = CRYPTO_ALG_ASYNC |
1018 CRYPTO_ALG_NEED_FALLBACK,
1019 .cra_blocksize = SHA1_BLOCK_SIZE,
1020 .cra_ctxsize = sizeof(struct mtk_sha_ctx) +
1021 sizeof(struct mtk_sha_hmac_ctx),
1022 .cra_alignmask = SHA_ALIGN_MSK,
1023 .cra_module = THIS_MODULE,
1024 .cra_init = mtk_sha_cra_sha1_init,
1025 .cra_exit = mtk_sha_cra_exit,
1026 }
1027},
1028{
1029 .init = mtk_sha_init,
1030 .update = mtk_sha_update,
1031 .final = mtk_sha_final,
1032 .finup = mtk_sha_finup,
1033 .digest = mtk_sha_digest,
1034 .export = mtk_sha_export,
1035 .import = mtk_sha_import,
1036 .setkey = mtk_sha_setkey,
1037 .halg.digestsize = SHA224_DIGEST_SIZE,
1038 .halg.statesize = sizeof(struct mtk_sha_reqctx),
1039 .halg.base = {
1040 .cra_name = "hmac(sha224)",
1041 .cra_driver_name = "mtk-hmac-sha224",
1042 .cra_priority = 400,
1043 .cra_flags = CRYPTO_ALG_ASYNC |
1044 CRYPTO_ALG_NEED_FALLBACK,
1045 .cra_blocksize = SHA224_BLOCK_SIZE,
1046 .cra_ctxsize = sizeof(struct mtk_sha_ctx) +
1047 sizeof(struct mtk_sha_hmac_ctx),
1048 .cra_alignmask = SHA_ALIGN_MSK,
1049 .cra_module = THIS_MODULE,
1050 .cra_init = mtk_sha_cra_sha224_init,
1051 .cra_exit = mtk_sha_cra_exit,
1052 }
1053},
1054{
1055 .init = mtk_sha_init,
1056 .update = mtk_sha_update,
1057 .final = mtk_sha_final,
1058 .finup = mtk_sha_finup,
1059 .digest = mtk_sha_digest,
1060 .export = mtk_sha_export,
1061 .import = mtk_sha_import,
1062 .setkey = mtk_sha_setkey,
1063 .halg.digestsize = SHA256_DIGEST_SIZE,
1064 .halg.statesize = sizeof(struct mtk_sha_reqctx),
1065 .halg.base = {
1066 .cra_name = "hmac(sha256)",
1067 .cra_driver_name = "mtk-hmac-sha256",
1068 .cra_priority = 400,
1069 .cra_flags = CRYPTO_ALG_ASYNC |
1070 CRYPTO_ALG_NEED_FALLBACK,
1071 .cra_blocksize = SHA256_BLOCK_SIZE,
1072 .cra_ctxsize = sizeof(struct mtk_sha_ctx) +
1073 sizeof(struct mtk_sha_hmac_ctx),
1074 .cra_alignmask = SHA_ALIGN_MSK,
1075 .cra_module = THIS_MODULE,
1076 .cra_init = mtk_sha_cra_sha256_init,
1077 .cra_exit = mtk_sha_cra_exit,
1078 }
1079},
1080};
1081
1082static struct ahash_alg algs_sha384_sha512[] = {
1083{
1084 .init = mtk_sha_init,
1085 .update = mtk_sha_update,
1086 .final = mtk_sha_final,
1087 .finup = mtk_sha_finup,
1088 .digest = mtk_sha_digest,
1089 .export = mtk_sha_export,
1090 .import = mtk_sha_import,
1091 .halg.digestsize = SHA384_DIGEST_SIZE,
1092 .halg.statesize = sizeof(struct mtk_sha_reqctx),
1093 .halg.base = {
1094 .cra_name = "sha384",
1095 .cra_driver_name = "mtk-sha384",
1096 .cra_priority = 400,
1097 .cra_flags = CRYPTO_ALG_ASYNC,
1098 .cra_blocksize = SHA384_BLOCK_SIZE,
1099 .cra_ctxsize = sizeof(struct mtk_sha_ctx),
1100 .cra_alignmask = SHA_ALIGN_MSK,
1101 .cra_module = THIS_MODULE,
1102 .cra_init = mtk_sha_cra_init,
1103 .cra_exit = mtk_sha_cra_exit,
1104 }
1105},
1106{
1107 .init = mtk_sha_init,
1108 .update = mtk_sha_update,
1109 .final = mtk_sha_final,
1110 .finup = mtk_sha_finup,
1111 .digest = mtk_sha_digest,
1112 .export = mtk_sha_export,
1113 .import = mtk_sha_import,
1114 .halg.digestsize = SHA512_DIGEST_SIZE,
1115 .halg.statesize = sizeof(struct mtk_sha_reqctx),
1116 .halg.base = {
1117 .cra_name = "sha512",
1118 .cra_driver_name = "mtk-sha512",
1119 .cra_priority = 400,
1120 .cra_flags = CRYPTO_ALG_ASYNC,
1121 .cra_blocksize = SHA512_BLOCK_SIZE,
1122 .cra_ctxsize = sizeof(struct mtk_sha_ctx),
1123 .cra_alignmask = SHA_ALIGN_MSK,
1124 .cra_module = THIS_MODULE,
1125 .cra_init = mtk_sha_cra_init,
1126 .cra_exit = mtk_sha_cra_exit,
1127 }
1128},
1129{
1130 .init = mtk_sha_init,
1131 .update = mtk_sha_update,
1132 .final = mtk_sha_final,
1133 .finup = mtk_sha_finup,
1134 .digest = mtk_sha_digest,
1135 .export = mtk_sha_export,
1136 .import = mtk_sha_import,
1137 .setkey = mtk_sha_setkey,
1138 .halg.digestsize = SHA384_DIGEST_SIZE,
1139 .halg.statesize = sizeof(struct mtk_sha_reqctx),
1140 .halg.base = {
1141 .cra_name = "hmac(sha384)",
1142 .cra_driver_name = "mtk-hmac-sha384",
1143 .cra_priority = 400,
1144 .cra_flags = CRYPTO_ALG_ASYNC |
1145 CRYPTO_ALG_NEED_FALLBACK,
1146 .cra_blocksize = SHA384_BLOCK_SIZE,
1147 .cra_ctxsize = sizeof(struct mtk_sha_ctx) +
1148 sizeof(struct mtk_sha_hmac_ctx),
1149 .cra_alignmask = SHA_ALIGN_MSK,
1150 .cra_module = THIS_MODULE,
1151 .cra_init = mtk_sha_cra_sha384_init,
1152 .cra_exit = mtk_sha_cra_exit,
1153 }
1154},
1155{
1156 .init = mtk_sha_init,
1157 .update = mtk_sha_update,
1158 .final = mtk_sha_final,
1159 .finup = mtk_sha_finup,
1160 .digest = mtk_sha_digest,
1161 .export = mtk_sha_export,
1162 .import = mtk_sha_import,
1163 .setkey = mtk_sha_setkey,
1164 .halg.digestsize = SHA512_DIGEST_SIZE,
1165 .halg.statesize = sizeof(struct mtk_sha_reqctx),
1166 .halg.base = {
1167 .cra_name = "hmac(sha512)",
1168 .cra_driver_name = "mtk-hmac-sha512",
1169 .cra_priority = 400,
1170 .cra_flags = CRYPTO_ALG_ASYNC |
1171 CRYPTO_ALG_NEED_FALLBACK,
1172 .cra_blocksize = SHA512_BLOCK_SIZE,
1173 .cra_ctxsize = sizeof(struct mtk_sha_ctx) +
1174 sizeof(struct mtk_sha_hmac_ctx),
1175 .cra_alignmask = SHA_ALIGN_MSK,
1176 .cra_module = THIS_MODULE,
1177 .cra_init = mtk_sha_cra_sha512_init,
1178 .cra_exit = mtk_sha_cra_exit,
1179 }
1180},
1181};
1182
Ryder Lee132c57c2017-03-09 10:11:12 +08001183static void mtk_sha_done_task(unsigned long data)
Ryder Lee785e5c62016-12-19 10:20:44 +08001184{
Ryder Lee132c57c2017-03-09 10:11:12 +08001185 struct mtk_sha_rec *sha = (struct mtk_sha_rec *)data;
1186 struct mtk_cryp *cryp = sha->cryp;
Ryder Lee785e5c62016-12-19 10:20:44 +08001187
1188 mtk_sha_unmap(cryp, sha);
1189 mtk_sha_complete(cryp, sha);
1190}
1191
Ryder Lee132c57c2017-03-09 10:11:12 +08001192static irqreturn_t mtk_sha_irq(int irq, void *dev_id)
Ryder Lee785e5c62016-12-19 10:20:44 +08001193{
Ryder Lee132c57c2017-03-09 10:11:12 +08001194 struct mtk_sha_rec *sha = (struct mtk_sha_rec *)dev_id;
1195 struct mtk_cryp *cryp = sha->cryp;
1196 u32 val = mtk_sha_read(cryp, RDR_STAT(sha->id));
Ryder Lee785e5c62016-12-19 10:20:44 +08001197
Ryder Lee132c57c2017-03-09 10:11:12 +08001198 mtk_sha_write(cryp, RDR_STAT(sha->id), val);
Ryder Lee785e5c62016-12-19 10:20:44 +08001199
1200 if (likely((SHA_FLAGS_BUSY & sha->flags))) {
Ryder Lee132c57c2017-03-09 10:11:12 +08001201 mtk_sha_write(cryp, RDR_PROC_COUNT(sha->id), MTK_CNT_RST);
1202 mtk_sha_write(cryp, RDR_THRESH(sha->id),
Ryder Lee785e5c62016-12-19 10:20:44 +08001203 MTK_RDR_PROC_THRESH | MTK_RDR_PROC_MODE);
1204
1205 tasklet_schedule(&sha->task);
1206 } else {
Ryder Lee132c57c2017-03-09 10:11:12 +08001207 dev_warn(cryp->dev, "SHA interrupt when no active requests.\n");
Ryder Lee785e5c62016-12-19 10:20:44 +08001208 }
1209 return IRQ_HANDLED;
1210}
1211
1212/*
1213 * The purpose of two SHA records is used to get extra performance.
1214 * It is similar to mtk_aes_record_init().
1215 */
1216static int mtk_sha_record_init(struct mtk_cryp *cryp)
1217{
1218 struct mtk_sha_rec **sha = cryp->sha;
1219 int i, err = -ENOMEM;
1220
1221 for (i = 0; i < MTK_REC_NUM; i++) {
1222 sha[i] = kzalloc(sizeof(**sha), GFP_KERNEL);
1223 if (!sha[i])
1224 goto err_cleanup;
1225
Ryder Lee132c57c2017-03-09 10:11:12 +08001226 sha[i]->cryp = cryp;
Ryder Lee785e5c62016-12-19 10:20:44 +08001227
1228 spin_lock_init(&sha[i]->lock);
1229 crypto_init_queue(&sha[i]->queue, SHA_QUEUE_SIZE);
Ryder Lee132c57c2017-03-09 10:11:12 +08001230
1231 tasklet_init(&sha[i]->task, mtk_sha_done_task,
1232 (unsigned long)sha[i]);
Ryder Lee785e5c62016-12-19 10:20:44 +08001233 }
1234
Ryder Lee132c57c2017-03-09 10:11:12 +08001235 /* Link to ring2 and ring3 respectively */
Ryder Leeb7a2be32017-03-09 10:11:13 +08001236 sha[0]->id = MTK_RING2;
1237 sha[1]->id = MTK_RING3;
Ryder Lee785e5c62016-12-19 10:20:44 +08001238
1239 cryp->rec = 1;
1240
1241 return 0;
1242
1243err_cleanup:
1244 for (; i--; )
1245 kfree(sha[i]);
1246 return err;
1247}
1248
1249static void mtk_sha_record_free(struct mtk_cryp *cryp)
1250{
1251 int i;
1252
1253 for (i = 0; i < MTK_REC_NUM; i++) {
1254 tasklet_kill(&cryp->sha[i]->task);
1255 kfree(cryp->sha[i]);
1256 }
1257}
1258
1259static void mtk_sha_unregister_algs(void)
1260{
1261 int i;
1262
1263 for (i = 0; i < ARRAY_SIZE(algs_sha1_sha224_sha256); i++)
1264 crypto_unregister_ahash(&algs_sha1_sha224_sha256[i]);
1265
1266 for (i = 0; i < ARRAY_SIZE(algs_sha384_sha512); i++)
1267 crypto_unregister_ahash(&algs_sha384_sha512[i]);
1268}
1269
1270static int mtk_sha_register_algs(void)
1271{
1272 int err, i;
1273
1274 for (i = 0; i < ARRAY_SIZE(algs_sha1_sha224_sha256); i++) {
1275 err = crypto_register_ahash(&algs_sha1_sha224_sha256[i]);
1276 if (err)
1277 goto err_sha_224_256_algs;
1278 }
1279
1280 for (i = 0; i < ARRAY_SIZE(algs_sha384_sha512); i++) {
1281 err = crypto_register_ahash(&algs_sha384_sha512[i]);
1282 if (err)
1283 goto err_sha_384_512_algs;
1284 }
1285
1286 return 0;
1287
1288err_sha_384_512_algs:
1289 for (; i--; )
1290 crypto_unregister_ahash(&algs_sha384_sha512[i]);
1291 i = ARRAY_SIZE(algs_sha1_sha224_sha256);
1292err_sha_224_256_algs:
1293 for (; i--; )
1294 crypto_unregister_ahash(&algs_sha1_sha224_sha256[i]);
1295
1296 return err;
1297}
1298
1299int mtk_hash_alg_register(struct mtk_cryp *cryp)
1300{
1301 int err;
1302
1303 INIT_LIST_HEAD(&cryp->sha_list);
1304
1305 /* Initialize two hash records */
1306 err = mtk_sha_record_init(cryp);
1307 if (err)
1308 goto err_record;
1309
Ryder Leeb7a2be32017-03-09 10:11:13 +08001310 err = devm_request_irq(cryp->dev, cryp->irq[MTK_RING2], mtk_sha_irq,
Ryder Lee132c57c2017-03-09 10:11:12 +08001311 0, "mtk-sha", cryp->sha[0]);
Ryder Lee785e5c62016-12-19 10:20:44 +08001312 if (err) {
1313 dev_err(cryp->dev, "unable to request sha irq0.\n");
1314 goto err_res;
1315 }
1316
Ryder Leeb7a2be32017-03-09 10:11:13 +08001317 err = devm_request_irq(cryp->dev, cryp->irq[MTK_RING3], mtk_sha_irq,
Ryder Lee132c57c2017-03-09 10:11:12 +08001318 0, "mtk-sha", cryp->sha[1]);
Ryder Lee785e5c62016-12-19 10:20:44 +08001319 if (err) {
1320 dev_err(cryp->dev, "unable to request sha irq1.\n");
1321 goto err_res;
1322 }
1323
1324 /* Enable ring2 and ring3 interrupt for hash */
Ryder Leeb7a2be32017-03-09 10:11:13 +08001325 mtk_sha_write(cryp, AIC_ENABLE_SET(MTK_RING2), MTK_IRQ_RDR2);
1326 mtk_sha_write(cryp, AIC_ENABLE_SET(MTK_RING3), MTK_IRQ_RDR3);
Ryder Lee785e5c62016-12-19 10:20:44 +08001327
Ryder Lee785e5c62016-12-19 10:20:44 +08001328 spin_lock(&mtk_sha.lock);
1329 list_add_tail(&cryp->sha_list, &mtk_sha.dev_list);
1330 spin_unlock(&mtk_sha.lock);
1331
1332 err = mtk_sha_register_algs();
1333 if (err)
1334 goto err_algs;
1335
1336 return 0;
1337
1338err_algs:
1339 spin_lock(&mtk_sha.lock);
1340 list_del(&cryp->sha_list);
1341 spin_unlock(&mtk_sha.lock);
Ryder Lee785e5c62016-12-19 10:20:44 +08001342err_res:
1343 mtk_sha_record_free(cryp);
1344err_record:
1345
1346 dev_err(cryp->dev, "mtk-sha initialization failed.\n");
1347 return err;
1348}
1349
1350void mtk_hash_alg_release(struct mtk_cryp *cryp)
1351{
1352 spin_lock(&mtk_sha.lock);
1353 list_del(&cryp->sha_list);
1354 spin_unlock(&mtk_sha.lock);
1355
1356 mtk_sha_unregister_algs();
Ryder Lee785e5c62016-12-19 10:20:44 +08001357 mtk_sha_record_free(cryp);
1358}