blob: 0884d6243cbc9ee3673b8aab1429a118c51ca9f8 [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
Ryder Lee3d21c412017-03-09 10:11:15 +0800155static inline void mtk_sha_ring_shift(struct mtk_ring *ring,
156 struct mtk_desc **cmd_curr,
157 struct mtk_desc **res_curr,
158 int *count)
159{
160 *cmd_curr = ring->cmd_next++;
161 *res_curr = ring->res_next++;
162 (*count)++;
163
164 if (ring->cmd_next == ring->cmd_base + MTK_DESC_NUM) {
165 ring->cmd_next = ring->cmd_base;
166 ring->res_next = ring->res_base;
167 }
168}
169
Ryder Lee785e5c62016-12-19 10:20:44 +0800170static struct mtk_cryp *mtk_sha_find_dev(struct mtk_sha_ctx *tctx)
171{
172 struct mtk_cryp *cryp = NULL;
173 struct mtk_cryp *tmp;
174
175 spin_lock_bh(&mtk_sha.lock);
176 if (!tctx->cryp) {
177 list_for_each_entry(tmp, &mtk_sha.dev_list, sha_list) {
178 cryp = tmp;
179 break;
180 }
181 tctx->cryp = cryp;
182 } else {
183 cryp = tctx->cryp;
184 }
185
186 /*
187 * Assign record id to tfm in round-robin fashion, and this
188 * will help tfm to bind to corresponding descriptor rings.
189 */
190 tctx->id = cryp->rec;
191 cryp->rec = !cryp->rec;
192
193 spin_unlock_bh(&mtk_sha.lock);
194
195 return cryp;
196}
197
198static int mtk_sha_append_sg(struct mtk_sha_reqctx *ctx)
199{
200 size_t count;
201
202 while ((ctx->bufcnt < SHA_BUF_SIZE) && ctx->total) {
203 count = min(ctx->sg->length - ctx->offset, ctx->total);
204 count = min(count, SHA_BUF_SIZE - ctx->bufcnt);
205
206 if (count <= 0) {
207 /*
208 * Check if count <= 0 because the buffer is full or
209 * because the sg length is 0. In the latest case,
210 * check if there is another sg in the list, a 0 length
211 * sg doesn't necessarily mean the end of the sg list.
212 */
213 if ((ctx->sg->length == 0) && !sg_is_last(ctx->sg)) {
214 ctx->sg = sg_next(ctx->sg);
215 continue;
216 } else {
217 break;
218 }
219 }
220
221 scatterwalk_map_and_copy(ctx->buffer + ctx->bufcnt, ctx->sg,
222 ctx->offset, count, 0);
223
224 ctx->bufcnt += count;
225 ctx->offset += count;
226 ctx->total -= count;
227
228 if (ctx->offset == ctx->sg->length) {
229 ctx->sg = sg_next(ctx->sg);
230 if (ctx->sg)
231 ctx->offset = 0;
232 else
233 ctx->total = 0;
234 }
235 }
236
237 return 0;
238}
239
240/*
241 * The purpose of this padding is to ensure that the padded message is a
242 * multiple of 512 bits (SHA1/SHA224/SHA256) or 1024 bits (SHA384/SHA512).
243 * The bit "1" is appended at the end of the message followed by
244 * "padlen-1" zero bits. Then a 64 bits block (SHA1/SHA224/SHA256) or
245 * 128 bits block (SHA384/SHA512) equals to the message length in bits
246 * is appended.
247 *
248 * For SHA1/SHA224/SHA256, padlen is calculated as followed:
249 * - if message length < 56 bytes then padlen = 56 - message length
250 * - else padlen = 64 + 56 - message length
251 *
252 * For SHA384/SHA512, padlen is calculated as followed:
253 * - if message length < 112 bytes then padlen = 112 - message length
254 * - else padlen = 128 + 112 - message length
255 */
256static void mtk_sha_fill_padding(struct mtk_sha_reqctx *ctx, u32 len)
257{
258 u32 index, padlen;
259 u64 bits[2];
260 u64 size = ctx->digcnt;
261
262 size += ctx->bufcnt;
263 size += len;
264
265 bits[1] = cpu_to_be64(size << 3);
266 bits[0] = cpu_to_be64(size >> 61);
267
268 if (ctx->flags & (SHA_FLAGS_SHA384 | SHA_FLAGS_SHA512)) {
269 index = ctx->bufcnt & 0x7f;
270 padlen = (index < 112) ? (112 - index) : ((128 + 112) - index);
271 *(ctx->buffer + ctx->bufcnt) = 0x80;
272 memset(ctx->buffer + ctx->bufcnt + 1, 0, padlen - 1);
273 memcpy(ctx->buffer + ctx->bufcnt + padlen, bits, 16);
274 ctx->bufcnt += padlen + 16;
275 ctx->flags |= SHA_FLAGS_PAD;
276 } else {
277 index = ctx->bufcnt & 0x3f;
278 padlen = (index < 56) ? (56 - index) : ((64 + 56) - index);
279 *(ctx->buffer + ctx->bufcnt) = 0x80;
280 memset(ctx->buffer + ctx->bufcnt + 1, 0, padlen - 1);
281 memcpy(ctx->buffer + ctx->bufcnt + padlen, &bits[1], 8);
282 ctx->bufcnt += padlen + 8;
283 ctx->flags |= SHA_FLAGS_PAD;
284 }
285}
286
287/* Initialize basic transform information of SHA */
Ryder Leea87399622017-01-20 13:41:08 +0800288static void mtk_sha_info_init(struct mtk_sha_reqctx *ctx)
Ryder Lee785e5c62016-12-19 10:20:44 +0800289{
Ryder Leea87399622017-01-20 13:41:08 +0800290 struct mtk_sha_ct *ct = &ctx->info.ct;
291 struct mtk_sha_tfm *tfm = &ctx->info.tfm;
Ryder Lee785e5c62016-12-19 10:20:44 +0800292
Ryder Leea87399622017-01-20 13:41:08 +0800293 ctx->ct_hdr = SHA_CT_CTRL_HDR;
294 ctx->ct_size = SHA_CT_SIZE;
Ryder Lee785e5c62016-12-19 10:20:44 +0800295
Ryder Leea87399622017-01-20 13:41:08 +0800296 tfm->ctrl[0] = SHA_TFM_HASH | SHA_TFM_INNER_DIG |
297 SHA_TFM_SIZE(SIZE_IN_WORDS(ctx->ds));
Ryder Lee785e5c62016-12-19 10:20:44 +0800298
299 switch (ctx->flags & SHA_FLAGS_ALGO_MSK) {
300 case SHA_FLAGS_SHA1:
Ryder Leea87399622017-01-20 13:41:08 +0800301 tfm->ctrl[0] |= SHA_TFM_SHA1;
Ryder Lee785e5c62016-12-19 10:20:44 +0800302 break;
303 case SHA_FLAGS_SHA224:
Ryder Leea87399622017-01-20 13:41:08 +0800304 tfm->ctrl[0] |= SHA_TFM_SHA224;
Ryder Lee785e5c62016-12-19 10:20:44 +0800305 break;
306 case SHA_FLAGS_SHA256:
Ryder Leea87399622017-01-20 13:41:08 +0800307 tfm->ctrl[0] |= SHA_TFM_SHA256;
Ryder Lee785e5c62016-12-19 10:20:44 +0800308 break;
309 case SHA_FLAGS_SHA384:
Ryder Leea87399622017-01-20 13:41:08 +0800310 tfm->ctrl[0] |= SHA_TFM_SHA384;
Ryder Lee785e5c62016-12-19 10:20:44 +0800311 break;
312 case SHA_FLAGS_SHA512:
Ryder Leea87399622017-01-20 13:41:08 +0800313 tfm->ctrl[0] |= SHA_TFM_SHA512;
Ryder Lee785e5c62016-12-19 10:20:44 +0800314 break;
315
316 default:
317 /* Should not happen... */
318 return;
319 }
320
Ryder Leea87399622017-01-20 13:41:08 +0800321 tfm->ctrl[1] = SHA_TFM_HASH_STORE;
322 ct->ctrl[0] = tfm->ctrl[0] | SHA_TFM_CONTINUE | SHA_TFM_START;
323 ct->ctrl[1] = tfm->ctrl[1];
Ryder Lee785e5c62016-12-19 10:20:44 +0800324
Ryder Leea87399622017-01-20 13:41:08 +0800325 ct->cmd[0] = SHA_CMD0;
326 ct->cmd[1] = SHA_CMD1;
327 ct->cmd[2] = SHA_CMD2 | SHA_TFM_DIGEST(SIZE_IN_WORDS(ctx->ds));
Ryder Lee785e5c62016-12-19 10:20:44 +0800328}
329
330/*
331 * Update input data length field of transform information and
332 * map it to DMA region.
333 */
Ryder Lee059b1492017-01-20 13:41:13 +0800334static int mtk_sha_info_update(struct mtk_cryp *cryp,
335 struct mtk_sha_rec *sha,
Ryder Lee82445fe2017-03-09 10:11:14 +0800336 size_t len1, size_t len2)
Ryder Lee785e5c62016-12-19 10:20:44 +0800337{
338 struct mtk_sha_reqctx *ctx = ahash_request_ctx(sha->req);
Ryder Leea87399622017-01-20 13:41:08 +0800339 struct mtk_sha_info *info = &ctx->info;
Ryder Lee785e5c62016-12-19 10:20:44 +0800340 struct mtk_sha_ct *ct = &info->ct;
341
342 if (ctx->start)
343 ctx->start = false;
344 else
Ryder Leea87399622017-01-20 13:41:08 +0800345 ct->ctrl[0] &= ~SHA_TFM_START;
Ryder Lee785e5c62016-12-19 10:20:44 +0800346
Ryder Leea87399622017-01-20 13:41:08 +0800347 ctx->ct_hdr &= ~SHA_DATA_LEN_MSK;
Ryder Lee82445fe2017-03-09 10:11:14 +0800348 ctx->ct_hdr |= cpu_to_le32(len1 + len2);
Ryder Leea87399622017-01-20 13:41:08 +0800349 ct->cmd[0] &= ~SHA_DATA_LEN_MSK;
Ryder Lee82445fe2017-03-09 10:11:14 +0800350 ct->cmd[0] |= cpu_to_le32(len1 + len2);
Ryder Lee785e5c62016-12-19 10:20:44 +0800351
Ryder Lee82445fe2017-03-09 10:11:14 +0800352 ctx->digcnt += len1;
Ryder Lee785e5c62016-12-19 10:20:44 +0800353
Ryder Leea87399622017-01-20 13:41:08 +0800354 ctx->ct_dma = dma_map_single(cryp->dev, info, sizeof(*info),
Ryder Lee059b1492017-01-20 13:41:13 +0800355 DMA_BIDIRECTIONAL);
Ryder Leea87399622017-01-20 13:41:08 +0800356 if (unlikely(dma_mapping_error(cryp->dev, ctx->ct_dma))) {
Arnd Bergmann41e05322017-01-11 14:55:20 +0100357 dev_err(cryp->dev, "dma %zu bytes error\n", sizeof(*info));
Ryder Lee785e5c62016-12-19 10:20:44 +0800358 return -EINVAL;
359 }
Ryder Leea87399622017-01-20 13:41:08 +0800360 ctx->tfm_dma = ctx->ct_dma + sizeof(*ct);
Ryder Lee785e5c62016-12-19 10:20:44 +0800361
362 return 0;
363}
364
365/*
366 * Because of hardware limitation, we must pre-calculate the inner
367 * and outer digest that need to be processed firstly by engine, then
368 * apply the result digest to the input message. These complex hashing
369 * procedures limits HMAC performance, so we use fallback SW encoding.
370 */
371static int mtk_sha_finish_hmac(struct ahash_request *req)
372{
373 struct mtk_sha_ctx *tctx = crypto_tfm_ctx(req->base.tfm);
374 struct mtk_sha_hmac_ctx *bctx = tctx->base;
375 struct mtk_sha_reqctx *ctx = ahash_request_ctx(req);
376
377 SHASH_DESC_ON_STACK(shash, bctx->shash);
378
379 shash->tfm = bctx->shash;
380 shash->flags = 0; /* not CRYPTO_TFM_REQ_MAY_SLEEP */
381
382 return crypto_shash_init(shash) ?:
383 crypto_shash_update(shash, bctx->opad, ctx->bs) ?:
384 crypto_shash_finup(shash, req->result, ctx->ds, req->result);
385}
386
387/* Initialize request context */
388static int mtk_sha_init(struct ahash_request *req)
389{
390 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
391 struct mtk_sha_ctx *tctx = crypto_ahash_ctx(tfm);
392 struct mtk_sha_reqctx *ctx = ahash_request_ctx(req);
393
394 ctx->flags = 0;
395 ctx->ds = crypto_ahash_digestsize(tfm);
396
397 switch (ctx->ds) {
398 case SHA1_DIGEST_SIZE:
399 ctx->flags |= SHA_FLAGS_SHA1;
400 ctx->bs = SHA1_BLOCK_SIZE;
401 break;
402 case SHA224_DIGEST_SIZE:
403 ctx->flags |= SHA_FLAGS_SHA224;
404 ctx->bs = SHA224_BLOCK_SIZE;
405 break;
406 case SHA256_DIGEST_SIZE:
407 ctx->flags |= SHA_FLAGS_SHA256;
408 ctx->bs = SHA256_BLOCK_SIZE;
409 break;
410 case SHA384_DIGEST_SIZE:
411 ctx->flags |= SHA_FLAGS_SHA384;
412 ctx->bs = SHA384_BLOCK_SIZE;
413 break;
414 case SHA512_DIGEST_SIZE:
415 ctx->flags |= SHA_FLAGS_SHA512;
416 ctx->bs = SHA512_BLOCK_SIZE;
417 break;
418 default:
419 return -EINVAL;
420 }
421
422 ctx->bufcnt = 0;
423 ctx->digcnt = 0;
424 ctx->buffer = tctx->buf;
425 ctx->start = true;
426
427 if (tctx->flags & SHA_FLAGS_HMAC) {
428 struct mtk_sha_hmac_ctx *bctx = tctx->base;
429
430 memcpy(ctx->buffer, bctx->ipad, ctx->bs);
431 ctx->bufcnt = ctx->bs;
432 ctx->flags |= SHA_FLAGS_HMAC;
433 }
434
435 return 0;
436}
437
438static int mtk_sha_xmit(struct mtk_cryp *cryp, struct mtk_sha_rec *sha,
Ryder Lee82445fe2017-03-09 10:11:14 +0800439 dma_addr_t addr1, size_t len1,
440 dma_addr_t addr2, size_t len2)
Ryder Lee785e5c62016-12-19 10:20:44 +0800441{
Ryder Leea87399622017-01-20 13:41:08 +0800442 struct mtk_sha_reqctx *ctx = ahash_request_ctx(sha->req);
Ryder Lee785e5c62016-12-19 10:20:44 +0800443 struct mtk_ring *ring = cryp->ring[sha->id];
Ryder Lee3d21c412017-03-09 10:11:15 +0800444 struct mtk_desc *cmd, *res;
Ryder Lee82445fe2017-03-09 10:11:14 +0800445 int err, count = 0;
Ryder Lee785e5c62016-12-19 10:20:44 +0800446
Ryder Lee82445fe2017-03-09 10:11:14 +0800447 err = mtk_sha_info_update(cryp, sha, len1, len2);
Ryder Lee785e5c62016-12-19 10:20:44 +0800448 if (err)
449 return err;
450
451 /* Fill in the command/result descriptors */
Ryder Lee3d21c412017-03-09 10:11:15 +0800452 mtk_sha_ring_shift(ring, &cmd, &res, &count);
453
Ryder Lee82445fe2017-03-09 10:11:14 +0800454 res->hdr = MTK_DESC_FIRST | MTK_DESC_BUF_LEN(len1);
455 cmd->hdr = MTK_DESC_FIRST | MTK_DESC_BUF_LEN(len1) |
Ryder Leea87399622017-01-20 13:41:08 +0800456 MTK_DESC_CT_LEN(ctx->ct_size);
Ryder Lee82445fe2017-03-09 10:11:14 +0800457 cmd->buf = cpu_to_le32(addr1);
Ryder Leea87399622017-01-20 13:41:08 +0800458 cmd->ct = cpu_to_le32(ctx->ct_dma);
459 cmd->ct_hdr = ctx->ct_hdr;
460 cmd->tfm = cpu_to_le32(ctx->tfm_dma);
Ryder Lee785e5c62016-12-19 10:20:44 +0800461
Ryder Lee82445fe2017-03-09 10:11:14 +0800462 if (len2) {
Ryder Lee3d21c412017-03-09 10:11:15 +0800463 mtk_sha_ring_shift(ring, &cmd, &res, &count);
Ryder Lee785e5c62016-12-19 10:20:44 +0800464
Ryder Lee82445fe2017-03-09 10:11:14 +0800465 res->hdr = MTK_DESC_BUF_LEN(len2);
466 cmd->hdr = MTK_DESC_BUF_LEN(len2);
467 cmd->buf = cpu_to_le32(addr2);
Ryder Lee82445fe2017-03-09 10:11:14 +0800468 }
Ryder Lee785e5c62016-12-19 10:20:44 +0800469
Ryder Lee82445fe2017-03-09 10:11:14 +0800470 cmd->hdr |= MTK_DESC_LAST;
471 res->hdr |= MTK_DESC_LAST;
Ryder Lee785e5c62016-12-19 10:20:44 +0800472
473 /*
474 * Make sure that all changes to the DMA ring are done before we
475 * start engine.
476 */
477 wmb();
478 /* Start DMA transfer */
Ryder Lee82445fe2017-03-09 10:11:14 +0800479 mtk_sha_write(cryp, RDR_PREP_COUNT(sha->id), MTK_DESC_CNT(count));
480 mtk_sha_write(cryp, CDR_PREP_COUNT(sha->id), MTK_DESC_CNT(count));
Ryder Lee785e5c62016-12-19 10:20:44 +0800481
482 return -EINPROGRESS;
483}
484
485static int mtk_sha_dma_map(struct mtk_cryp *cryp,
486 struct mtk_sha_rec *sha,
487 struct mtk_sha_reqctx *ctx,
488 size_t count)
489{
490 ctx->dma_addr = dma_map_single(cryp->dev, ctx->buffer,
Ryder Lee059b1492017-01-20 13:41:13 +0800491 SHA_BUF_SIZE, DMA_TO_DEVICE);
Ryder Lee785e5c62016-12-19 10:20:44 +0800492 if (unlikely(dma_mapping_error(cryp->dev, ctx->dma_addr))) {
493 dev_err(cryp->dev, "dma map error\n");
494 return -EINVAL;
495 }
496
497 ctx->flags &= ~SHA_FLAGS_SG;
498
Ryder Lee82445fe2017-03-09 10:11:14 +0800499 return mtk_sha_xmit(cryp, sha, ctx->dma_addr, count, 0, 0);
Ryder Lee785e5c62016-12-19 10:20:44 +0800500}
501
502static int mtk_sha_update_slow(struct mtk_cryp *cryp,
503 struct mtk_sha_rec *sha)
504{
505 struct mtk_sha_reqctx *ctx = ahash_request_ctx(sha->req);
506 size_t count;
507 u32 final;
508
509 mtk_sha_append_sg(ctx);
510
511 final = (ctx->flags & SHA_FLAGS_FINUP) && !ctx->total;
512
Arnd Bergmann41e05322017-01-11 14:55:20 +0100513 dev_dbg(cryp->dev, "slow: bufcnt: %zu\n", ctx->bufcnt);
Ryder Lee785e5c62016-12-19 10:20:44 +0800514
515 if (final) {
516 sha->flags |= SHA_FLAGS_FINAL;
517 mtk_sha_fill_padding(ctx, 0);
518 }
519
520 if (final || (ctx->bufcnt == SHA_BUF_SIZE && ctx->total)) {
521 count = ctx->bufcnt;
522 ctx->bufcnt = 0;
523
524 return mtk_sha_dma_map(cryp, sha, ctx, count);
525 }
526 return 0;
527}
528
529static int mtk_sha_update_start(struct mtk_cryp *cryp,
530 struct mtk_sha_rec *sha)
531{
532 struct mtk_sha_reqctx *ctx = ahash_request_ctx(sha->req);
533 u32 len, final, tail;
534 struct scatterlist *sg;
535
536 if (!ctx->total)
537 return 0;
538
539 if (ctx->bufcnt || ctx->offset)
540 return mtk_sha_update_slow(cryp, sha);
541
542 sg = ctx->sg;
543
544 if (!IS_ALIGNED(sg->offset, sizeof(u32)))
545 return mtk_sha_update_slow(cryp, sha);
546
547 if (!sg_is_last(sg) && !IS_ALIGNED(sg->length, ctx->bs))
548 /* size is not ctx->bs aligned */
549 return mtk_sha_update_slow(cryp, sha);
550
551 len = min(ctx->total, sg->length);
552
553 if (sg_is_last(sg)) {
554 if (!(ctx->flags & SHA_FLAGS_FINUP)) {
555 /* not last sg must be ctx->bs aligned */
556 tail = len & (ctx->bs - 1);
557 len -= tail;
558 }
559 }
560
561 ctx->total -= len;
562 ctx->offset = len; /* offset where to start slow */
563
564 final = (ctx->flags & SHA_FLAGS_FINUP) && !ctx->total;
565
566 /* Add padding */
567 if (final) {
568 size_t count;
569
570 tail = len & (ctx->bs - 1);
571 len -= tail;
572 ctx->total += tail;
573 ctx->offset = len; /* offset where to start slow */
574
575 sg = ctx->sg;
576 mtk_sha_append_sg(ctx);
577 mtk_sha_fill_padding(ctx, len);
578
579 ctx->dma_addr = dma_map_single(cryp->dev, ctx->buffer,
Ryder Lee059b1492017-01-20 13:41:13 +0800580 SHA_BUF_SIZE, DMA_TO_DEVICE);
Ryder Lee785e5c62016-12-19 10:20:44 +0800581 if (unlikely(dma_mapping_error(cryp->dev, ctx->dma_addr))) {
582 dev_err(cryp->dev, "dma map bytes error\n");
583 return -EINVAL;
584 }
585
586 sha->flags |= SHA_FLAGS_FINAL;
587 count = ctx->bufcnt;
588 ctx->bufcnt = 0;
589
590 if (len == 0) {
591 ctx->flags &= ~SHA_FLAGS_SG;
Ryder Lee82445fe2017-03-09 10:11:14 +0800592 return mtk_sha_xmit(cryp, sha, ctx->dma_addr,
593 count, 0, 0);
Ryder Lee785e5c62016-12-19 10:20:44 +0800594
595 } else {
596 ctx->sg = sg;
597 if (!dma_map_sg(cryp->dev, ctx->sg, 1, DMA_TO_DEVICE)) {
598 dev_err(cryp->dev, "dma_map_sg error\n");
599 return -EINVAL;
600 }
601
602 ctx->flags |= SHA_FLAGS_SG;
Ryder Lee82445fe2017-03-09 10:11:14 +0800603 return mtk_sha_xmit(cryp, sha, sg_dma_address(ctx->sg),
604 len, ctx->dma_addr, count);
Ryder Lee785e5c62016-12-19 10:20:44 +0800605 }
606 }
607
608 if (!dma_map_sg(cryp->dev, ctx->sg, 1, DMA_TO_DEVICE)) {
609 dev_err(cryp->dev, "dma_map_sg error\n");
610 return -EINVAL;
611 }
612
613 ctx->flags |= SHA_FLAGS_SG;
614
Ryder Lee82445fe2017-03-09 10:11:14 +0800615 return mtk_sha_xmit(cryp, sha, sg_dma_address(ctx->sg),
616 len, 0, 0);
Ryder Lee785e5c62016-12-19 10:20:44 +0800617}
618
619static int mtk_sha_final_req(struct mtk_cryp *cryp,
620 struct mtk_sha_rec *sha)
621{
Ryder Lee059b1492017-01-20 13:41:13 +0800622 struct mtk_sha_reqctx *ctx = ahash_request_ctx(sha->req);
Ryder Lee785e5c62016-12-19 10:20:44 +0800623 size_t count;
624
625 mtk_sha_fill_padding(ctx, 0);
626
627 sha->flags |= SHA_FLAGS_FINAL;
628 count = ctx->bufcnt;
629 ctx->bufcnt = 0;
630
631 return mtk_sha_dma_map(cryp, sha, ctx, count);
632}
633
634/* Copy ready hash (+ finalize hmac) */
635static int mtk_sha_finish(struct ahash_request *req)
636{
637 struct mtk_sha_reqctx *ctx = ahash_request_ctx(req);
638 u32 *digest = ctx->info.tfm.digest;
639 u32 *result = (u32 *)req->result;
640 int i;
641
642 /* Get the hash from the digest buffer */
643 for (i = 0; i < SIZE_IN_WORDS(ctx->ds); i++)
644 result[i] = le32_to_cpu(digest[i]);
645
646 if (ctx->flags & SHA_FLAGS_HMAC)
647 return mtk_sha_finish_hmac(req);
648
649 return 0;
650}
651
652static void mtk_sha_finish_req(struct mtk_cryp *cryp,
Ryder Lee059b1492017-01-20 13:41:13 +0800653 struct mtk_sha_rec *sha,
654 int err)
Ryder Lee785e5c62016-12-19 10:20:44 +0800655{
656 if (likely(!err && (SHA_FLAGS_FINAL & sha->flags)))
657 err = mtk_sha_finish(sha->req);
658
659 sha->flags &= ~(SHA_FLAGS_BUSY | SHA_FLAGS_FINAL);
660
661 sha->req->base.complete(&sha->req->base, err);
662
663 /* Handle new request */
Ryder Leeb7a2be32017-03-09 10:11:13 +0800664 mtk_sha_handle_queue(cryp, sha->id - MTK_RING2, NULL);
Ryder Lee785e5c62016-12-19 10:20:44 +0800665}
666
667static int mtk_sha_handle_queue(struct mtk_cryp *cryp, u8 id,
668 struct ahash_request *req)
669{
670 struct mtk_sha_rec *sha = cryp->sha[id];
671 struct crypto_async_request *async_req, *backlog;
672 struct mtk_sha_reqctx *ctx;
673 unsigned long flags;
674 int err = 0, ret = 0;
675
676 spin_lock_irqsave(&sha->lock, flags);
677 if (req)
678 ret = ahash_enqueue_request(&sha->queue, req);
679
680 if (SHA_FLAGS_BUSY & sha->flags) {
681 spin_unlock_irqrestore(&sha->lock, flags);
682 return ret;
683 }
684
685 backlog = crypto_get_backlog(&sha->queue);
686 async_req = crypto_dequeue_request(&sha->queue);
687 if (async_req)
688 sha->flags |= SHA_FLAGS_BUSY;
689 spin_unlock_irqrestore(&sha->lock, flags);
690
691 if (!async_req)
692 return ret;
693
694 if (backlog)
695 backlog->complete(backlog, -EINPROGRESS);
696
697 req = ahash_request_cast(async_req);
698 ctx = ahash_request_ctx(req);
699
700 sha->req = req;
Ryder Lee785e5c62016-12-19 10:20:44 +0800701
Ryder Leea87399622017-01-20 13:41:08 +0800702 mtk_sha_info_init(ctx);
Ryder Lee785e5c62016-12-19 10:20:44 +0800703
704 if (ctx->op == SHA_OP_UPDATE) {
705 err = mtk_sha_update_start(cryp, sha);
706 if (err != -EINPROGRESS && (ctx->flags & SHA_FLAGS_FINUP))
707 /* No final() after finup() */
708 err = mtk_sha_final_req(cryp, sha);
709 } else if (ctx->op == SHA_OP_FINAL) {
710 err = mtk_sha_final_req(cryp, sha);
711 }
712
713 if (unlikely(err != -EINPROGRESS))
714 /* Task will not finish it, so do it here */
715 mtk_sha_finish_req(cryp, sha, err);
716
717 return ret;
718}
719
720static int mtk_sha_enqueue(struct ahash_request *req, u32 op)
721{
722 struct mtk_sha_reqctx *ctx = ahash_request_ctx(req);
723 struct mtk_sha_ctx *tctx = crypto_tfm_ctx(req->base.tfm);
724
725 ctx->op = op;
726
727 return mtk_sha_handle_queue(tctx->cryp, tctx->id, req);
728}
729
730static void mtk_sha_unmap(struct mtk_cryp *cryp, struct mtk_sha_rec *sha)
731{
732 struct mtk_sha_reqctx *ctx = ahash_request_ctx(sha->req);
733
Ryder Leea87399622017-01-20 13:41:08 +0800734 dma_unmap_single(cryp->dev, ctx->ct_dma, sizeof(ctx->info),
735 DMA_BIDIRECTIONAL);
Ryder Lee785e5c62016-12-19 10:20:44 +0800736
737 if (ctx->flags & SHA_FLAGS_SG) {
738 dma_unmap_sg(cryp->dev, ctx->sg, 1, DMA_TO_DEVICE);
739 if (ctx->sg->length == ctx->offset) {
740 ctx->sg = sg_next(ctx->sg);
741 if (ctx->sg)
742 ctx->offset = 0;
743 }
744 if (ctx->flags & SHA_FLAGS_PAD) {
745 dma_unmap_single(cryp->dev, ctx->dma_addr,
746 SHA_BUF_SIZE, DMA_TO_DEVICE);
747 }
748 } else
749 dma_unmap_single(cryp->dev, ctx->dma_addr,
750 SHA_BUF_SIZE, DMA_TO_DEVICE);
751}
752
753static void mtk_sha_complete(struct mtk_cryp *cryp,
754 struct mtk_sha_rec *sha)
755{
756 int err = 0;
757
758 err = mtk_sha_update_start(cryp, sha);
759 if (err != -EINPROGRESS)
760 mtk_sha_finish_req(cryp, sha, err);
761}
762
763static int mtk_sha_update(struct ahash_request *req)
764{
765 struct mtk_sha_reqctx *ctx = ahash_request_ctx(req);
766
767 ctx->total = req->nbytes;
768 ctx->sg = req->src;
769 ctx->offset = 0;
770
771 if ((ctx->bufcnt + ctx->total < SHA_BUF_SIZE) &&
772 !(ctx->flags & SHA_FLAGS_FINUP))
773 return mtk_sha_append_sg(ctx);
774
775 return mtk_sha_enqueue(req, SHA_OP_UPDATE);
776}
777
778static int mtk_sha_final(struct ahash_request *req)
779{
780 struct mtk_sha_reqctx *ctx = ahash_request_ctx(req);
781
782 ctx->flags |= SHA_FLAGS_FINUP;
783
784 if (ctx->flags & SHA_FLAGS_PAD)
785 return mtk_sha_finish(req);
786
787 return mtk_sha_enqueue(req, SHA_OP_FINAL);
788}
789
790static int mtk_sha_finup(struct ahash_request *req)
791{
792 struct mtk_sha_reqctx *ctx = ahash_request_ctx(req);
793 int err1, err2;
794
795 ctx->flags |= SHA_FLAGS_FINUP;
796
797 err1 = mtk_sha_update(req);
798 if (err1 == -EINPROGRESS || err1 == -EBUSY)
799 return err1;
800 /*
801 * final() has to be always called to cleanup resources
802 * even if update() failed
803 */
804 err2 = mtk_sha_final(req);
805
806 return err1 ?: err2;
807}
808
809static int mtk_sha_digest(struct ahash_request *req)
810{
811 return mtk_sha_init(req) ?: mtk_sha_finup(req);
812}
813
Ryder Lee059b1492017-01-20 13:41:13 +0800814static int mtk_sha_setkey(struct crypto_ahash *tfm, const u8 *key,
815 u32 keylen)
Ryder Lee785e5c62016-12-19 10:20:44 +0800816{
817 struct mtk_sha_ctx *tctx = crypto_ahash_ctx(tfm);
818 struct mtk_sha_hmac_ctx *bctx = tctx->base;
819 size_t bs = crypto_shash_blocksize(bctx->shash);
820 size_t ds = crypto_shash_digestsize(bctx->shash);
821 int err, i;
822
823 SHASH_DESC_ON_STACK(shash, bctx->shash);
824
825 shash->tfm = bctx->shash;
826 shash->flags = crypto_shash_get_flags(bctx->shash) &
Ryder Lee059b1492017-01-20 13:41:13 +0800827 CRYPTO_TFM_REQ_MAY_SLEEP;
Ryder Lee785e5c62016-12-19 10:20:44 +0800828
829 if (keylen > bs) {
830 err = crypto_shash_digest(shash, key, keylen, bctx->ipad);
831 if (err)
832 return err;
833 keylen = ds;
834 } else {
835 memcpy(bctx->ipad, key, keylen);
836 }
837
838 memset(bctx->ipad + keylen, 0, bs - keylen);
839 memcpy(bctx->opad, bctx->ipad, bs);
840
841 for (i = 0; i < bs; i++) {
842 bctx->ipad[i] ^= 0x36;
843 bctx->opad[i] ^= 0x5c;
844 }
845
Colin Ian Kingf2831482017-01-03 13:21:22 +0000846 return 0;
Ryder Lee785e5c62016-12-19 10:20:44 +0800847}
848
849static int mtk_sha_export(struct ahash_request *req, void *out)
850{
851 const struct mtk_sha_reqctx *ctx = ahash_request_ctx(req);
852
853 memcpy(out, ctx, sizeof(*ctx));
854 return 0;
855}
856
857static int mtk_sha_import(struct ahash_request *req, const void *in)
858{
859 struct mtk_sha_reqctx *ctx = ahash_request_ctx(req);
860
861 memcpy(ctx, in, sizeof(*ctx));
862 return 0;
863}
864
865static int mtk_sha_cra_init_alg(struct crypto_tfm *tfm,
866 const char *alg_base)
867{
868 struct mtk_sha_ctx *tctx = crypto_tfm_ctx(tfm);
869 struct mtk_cryp *cryp = NULL;
870
871 cryp = mtk_sha_find_dev(tctx);
872 if (!cryp)
873 return -ENODEV;
874
875 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
876 sizeof(struct mtk_sha_reqctx));
877
878 if (alg_base) {
879 struct mtk_sha_hmac_ctx *bctx = tctx->base;
880
881 tctx->flags |= SHA_FLAGS_HMAC;
882 bctx->shash = crypto_alloc_shash(alg_base, 0,
883 CRYPTO_ALG_NEED_FALLBACK);
884 if (IS_ERR(bctx->shash)) {
885 pr_err("base driver %s could not be loaded.\n",
886 alg_base);
887
888 return PTR_ERR(bctx->shash);
889 }
890 }
891 return 0;
892}
893
894static int mtk_sha_cra_init(struct crypto_tfm *tfm)
895{
896 return mtk_sha_cra_init_alg(tfm, NULL);
897}
898
899static int mtk_sha_cra_sha1_init(struct crypto_tfm *tfm)
900{
901 return mtk_sha_cra_init_alg(tfm, "sha1");
902}
903
904static int mtk_sha_cra_sha224_init(struct crypto_tfm *tfm)
905{
906 return mtk_sha_cra_init_alg(tfm, "sha224");
907}
908
909static int mtk_sha_cra_sha256_init(struct crypto_tfm *tfm)
910{
911 return mtk_sha_cra_init_alg(tfm, "sha256");
912}
913
914static int mtk_sha_cra_sha384_init(struct crypto_tfm *tfm)
915{
916 return mtk_sha_cra_init_alg(tfm, "sha384");
917}
918
919static int mtk_sha_cra_sha512_init(struct crypto_tfm *tfm)
920{
921 return mtk_sha_cra_init_alg(tfm, "sha512");
922}
923
924static void mtk_sha_cra_exit(struct crypto_tfm *tfm)
925{
926 struct mtk_sha_ctx *tctx = crypto_tfm_ctx(tfm);
927
928 if (tctx->flags & SHA_FLAGS_HMAC) {
929 struct mtk_sha_hmac_ctx *bctx = tctx->base;
930
931 crypto_free_shash(bctx->shash);
932 }
933}
934
935static struct ahash_alg algs_sha1_sha224_sha256[] = {
936{
937 .init = mtk_sha_init,
938 .update = mtk_sha_update,
939 .final = mtk_sha_final,
940 .finup = mtk_sha_finup,
941 .digest = mtk_sha_digest,
942 .export = mtk_sha_export,
943 .import = mtk_sha_import,
944 .halg.digestsize = SHA1_DIGEST_SIZE,
945 .halg.statesize = sizeof(struct mtk_sha_reqctx),
946 .halg.base = {
947 .cra_name = "sha1",
948 .cra_driver_name = "mtk-sha1",
949 .cra_priority = 400,
950 .cra_flags = CRYPTO_ALG_ASYNC,
951 .cra_blocksize = SHA1_BLOCK_SIZE,
952 .cra_ctxsize = sizeof(struct mtk_sha_ctx),
953 .cra_alignmask = SHA_ALIGN_MSK,
954 .cra_module = THIS_MODULE,
955 .cra_init = mtk_sha_cra_init,
956 .cra_exit = mtk_sha_cra_exit,
957 }
958},
959{
960 .init = mtk_sha_init,
961 .update = mtk_sha_update,
962 .final = mtk_sha_final,
963 .finup = mtk_sha_finup,
964 .digest = mtk_sha_digest,
965 .export = mtk_sha_export,
966 .import = mtk_sha_import,
967 .halg.digestsize = SHA224_DIGEST_SIZE,
968 .halg.statesize = sizeof(struct mtk_sha_reqctx),
969 .halg.base = {
970 .cra_name = "sha224",
971 .cra_driver_name = "mtk-sha224",
972 .cra_priority = 400,
973 .cra_flags = CRYPTO_ALG_ASYNC,
974 .cra_blocksize = SHA224_BLOCK_SIZE,
975 .cra_ctxsize = sizeof(struct mtk_sha_ctx),
976 .cra_alignmask = SHA_ALIGN_MSK,
977 .cra_module = THIS_MODULE,
978 .cra_init = mtk_sha_cra_init,
979 .cra_exit = mtk_sha_cra_exit,
980 }
981},
982{
983 .init = mtk_sha_init,
984 .update = mtk_sha_update,
985 .final = mtk_sha_final,
986 .finup = mtk_sha_finup,
987 .digest = mtk_sha_digest,
988 .export = mtk_sha_export,
989 .import = mtk_sha_import,
990 .halg.digestsize = SHA256_DIGEST_SIZE,
991 .halg.statesize = sizeof(struct mtk_sha_reqctx),
992 .halg.base = {
993 .cra_name = "sha256",
994 .cra_driver_name = "mtk-sha256",
995 .cra_priority = 400,
996 .cra_flags = CRYPTO_ALG_ASYNC,
997 .cra_blocksize = SHA256_BLOCK_SIZE,
998 .cra_ctxsize = sizeof(struct mtk_sha_ctx),
999 .cra_alignmask = SHA_ALIGN_MSK,
1000 .cra_module = THIS_MODULE,
1001 .cra_init = mtk_sha_cra_init,
1002 .cra_exit = mtk_sha_cra_exit,
1003 }
1004},
1005{
1006 .init = mtk_sha_init,
1007 .update = mtk_sha_update,
1008 .final = mtk_sha_final,
1009 .finup = mtk_sha_finup,
1010 .digest = mtk_sha_digest,
1011 .export = mtk_sha_export,
1012 .import = mtk_sha_import,
1013 .setkey = mtk_sha_setkey,
1014 .halg.digestsize = SHA1_DIGEST_SIZE,
1015 .halg.statesize = sizeof(struct mtk_sha_reqctx),
1016 .halg.base = {
1017 .cra_name = "hmac(sha1)",
1018 .cra_driver_name = "mtk-hmac-sha1",
1019 .cra_priority = 400,
1020 .cra_flags = CRYPTO_ALG_ASYNC |
1021 CRYPTO_ALG_NEED_FALLBACK,
1022 .cra_blocksize = SHA1_BLOCK_SIZE,
1023 .cra_ctxsize = sizeof(struct mtk_sha_ctx) +
1024 sizeof(struct mtk_sha_hmac_ctx),
1025 .cra_alignmask = SHA_ALIGN_MSK,
1026 .cra_module = THIS_MODULE,
1027 .cra_init = mtk_sha_cra_sha1_init,
1028 .cra_exit = mtk_sha_cra_exit,
1029 }
1030},
1031{
1032 .init = mtk_sha_init,
1033 .update = mtk_sha_update,
1034 .final = mtk_sha_final,
1035 .finup = mtk_sha_finup,
1036 .digest = mtk_sha_digest,
1037 .export = mtk_sha_export,
1038 .import = mtk_sha_import,
1039 .setkey = mtk_sha_setkey,
1040 .halg.digestsize = SHA224_DIGEST_SIZE,
1041 .halg.statesize = sizeof(struct mtk_sha_reqctx),
1042 .halg.base = {
1043 .cra_name = "hmac(sha224)",
1044 .cra_driver_name = "mtk-hmac-sha224",
1045 .cra_priority = 400,
1046 .cra_flags = CRYPTO_ALG_ASYNC |
1047 CRYPTO_ALG_NEED_FALLBACK,
1048 .cra_blocksize = SHA224_BLOCK_SIZE,
1049 .cra_ctxsize = sizeof(struct mtk_sha_ctx) +
1050 sizeof(struct mtk_sha_hmac_ctx),
1051 .cra_alignmask = SHA_ALIGN_MSK,
1052 .cra_module = THIS_MODULE,
1053 .cra_init = mtk_sha_cra_sha224_init,
1054 .cra_exit = mtk_sha_cra_exit,
1055 }
1056},
1057{
1058 .init = mtk_sha_init,
1059 .update = mtk_sha_update,
1060 .final = mtk_sha_final,
1061 .finup = mtk_sha_finup,
1062 .digest = mtk_sha_digest,
1063 .export = mtk_sha_export,
1064 .import = mtk_sha_import,
1065 .setkey = mtk_sha_setkey,
1066 .halg.digestsize = SHA256_DIGEST_SIZE,
1067 .halg.statesize = sizeof(struct mtk_sha_reqctx),
1068 .halg.base = {
1069 .cra_name = "hmac(sha256)",
1070 .cra_driver_name = "mtk-hmac-sha256",
1071 .cra_priority = 400,
1072 .cra_flags = CRYPTO_ALG_ASYNC |
1073 CRYPTO_ALG_NEED_FALLBACK,
1074 .cra_blocksize = SHA256_BLOCK_SIZE,
1075 .cra_ctxsize = sizeof(struct mtk_sha_ctx) +
1076 sizeof(struct mtk_sha_hmac_ctx),
1077 .cra_alignmask = SHA_ALIGN_MSK,
1078 .cra_module = THIS_MODULE,
1079 .cra_init = mtk_sha_cra_sha256_init,
1080 .cra_exit = mtk_sha_cra_exit,
1081 }
1082},
1083};
1084
1085static struct ahash_alg algs_sha384_sha512[] = {
1086{
1087 .init = mtk_sha_init,
1088 .update = mtk_sha_update,
1089 .final = mtk_sha_final,
1090 .finup = mtk_sha_finup,
1091 .digest = mtk_sha_digest,
1092 .export = mtk_sha_export,
1093 .import = mtk_sha_import,
1094 .halg.digestsize = SHA384_DIGEST_SIZE,
1095 .halg.statesize = sizeof(struct mtk_sha_reqctx),
1096 .halg.base = {
1097 .cra_name = "sha384",
1098 .cra_driver_name = "mtk-sha384",
1099 .cra_priority = 400,
1100 .cra_flags = CRYPTO_ALG_ASYNC,
1101 .cra_blocksize = SHA384_BLOCK_SIZE,
1102 .cra_ctxsize = sizeof(struct mtk_sha_ctx),
1103 .cra_alignmask = SHA_ALIGN_MSK,
1104 .cra_module = THIS_MODULE,
1105 .cra_init = mtk_sha_cra_init,
1106 .cra_exit = mtk_sha_cra_exit,
1107 }
1108},
1109{
1110 .init = mtk_sha_init,
1111 .update = mtk_sha_update,
1112 .final = mtk_sha_final,
1113 .finup = mtk_sha_finup,
1114 .digest = mtk_sha_digest,
1115 .export = mtk_sha_export,
1116 .import = mtk_sha_import,
1117 .halg.digestsize = SHA512_DIGEST_SIZE,
1118 .halg.statesize = sizeof(struct mtk_sha_reqctx),
1119 .halg.base = {
1120 .cra_name = "sha512",
1121 .cra_driver_name = "mtk-sha512",
1122 .cra_priority = 400,
1123 .cra_flags = CRYPTO_ALG_ASYNC,
1124 .cra_blocksize = SHA512_BLOCK_SIZE,
1125 .cra_ctxsize = sizeof(struct mtk_sha_ctx),
1126 .cra_alignmask = SHA_ALIGN_MSK,
1127 .cra_module = THIS_MODULE,
1128 .cra_init = mtk_sha_cra_init,
1129 .cra_exit = mtk_sha_cra_exit,
1130 }
1131},
1132{
1133 .init = mtk_sha_init,
1134 .update = mtk_sha_update,
1135 .final = mtk_sha_final,
1136 .finup = mtk_sha_finup,
1137 .digest = mtk_sha_digest,
1138 .export = mtk_sha_export,
1139 .import = mtk_sha_import,
1140 .setkey = mtk_sha_setkey,
1141 .halg.digestsize = SHA384_DIGEST_SIZE,
1142 .halg.statesize = sizeof(struct mtk_sha_reqctx),
1143 .halg.base = {
1144 .cra_name = "hmac(sha384)",
1145 .cra_driver_name = "mtk-hmac-sha384",
1146 .cra_priority = 400,
1147 .cra_flags = CRYPTO_ALG_ASYNC |
1148 CRYPTO_ALG_NEED_FALLBACK,
1149 .cra_blocksize = SHA384_BLOCK_SIZE,
1150 .cra_ctxsize = sizeof(struct mtk_sha_ctx) +
1151 sizeof(struct mtk_sha_hmac_ctx),
1152 .cra_alignmask = SHA_ALIGN_MSK,
1153 .cra_module = THIS_MODULE,
1154 .cra_init = mtk_sha_cra_sha384_init,
1155 .cra_exit = mtk_sha_cra_exit,
1156 }
1157},
1158{
1159 .init = mtk_sha_init,
1160 .update = mtk_sha_update,
1161 .final = mtk_sha_final,
1162 .finup = mtk_sha_finup,
1163 .digest = mtk_sha_digest,
1164 .export = mtk_sha_export,
1165 .import = mtk_sha_import,
1166 .setkey = mtk_sha_setkey,
1167 .halg.digestsize = SHA512_DIGEST_SIZE,
1168 .halg.statesize = sizeof(struct mtk_sha_reqctx),
1169 .halg.base = {
1170 .cra_name = "hmac(sha512)",
1171 .cra_driver_name = "mtk-hmac-sha512",
1172 .cra_priority = 400,
1173 .cra_flags = CRYPTO_ALG_ASYNC |
1174 CRYPTO_ALG_NEED_FALLBACK,
1175 .cra_blocksize = SHA512_BLOCK_SIZE,
1176 .cra_ctxsize = sizeof(struct mtk_sha_ctx) +
1177 sizeof(struct mtk_sha_hmac_ctx),
1178 .cra_alignmask = SHA_ALIGN_MSK,
1179 .cra_module = THIS_MODULE,
1180 .cra_init = mtk_sha_cra_sha512_init,
1181 .cra_exit = mtk_sha_cra_exit,
1182 }
1183},
1184};
1185
Ryder Lee132c57c2017-03-09 10:11:12 +08001186static void mtk_sha_done_task(unsigned long data)
Ryder Lee785e5c62016-12-19 10:20:44 +08001187{
Ryder Lee132c57c2017-03-09 10:11:12 +08001188 struct mtk_sha_rec *sha = (struct mtk_sha_rec *)data;
1189 struct mtk_cryp *cryp = sha->cryp;
Ryder Lee785e5c62016-12-19 10:20:44 +08001190
1191 mtk_sha_unmap(cryp, sha);
1192 mtk_sha_complete(cryp, sha);
1193}
1194
Ryder Lee132c57c2017-03-09 10:11:12 +08001195static irqreturn_t mtk_sha_irq(int irq, void *dev_id)
Ryder Lee785e5c62016-12-19 10:20:44 +08001196{
Ryder Lee132c57c2017-03-09 10:11:12 +08001197 struct mtk_sha_rec *sha = (struct mtk_sha_rec *)dev_id;
1198 struct mtk_cryp *cryp = sha->cryp;
1199 u32 val = mtk_sha_read(cryp, RDR_STAT(sha->id));
Ryder Lee785e5c62016-12-19 10:20:44 +08001200
Ryder Lee132c57c2017-03-09 10:11:12 +08001201 mtk_sha_write(cryp, RDR_STAT(sha->id), val);
Ryder Lee785e5c62016-12-19 10:20:44 +08001202
1203 if (likely((SHA_FLAGS_BUSY & sha->flags))) {
Ryder Lee132c57c2017-03-09 10:11:12 +08001204 mtk_sha_write(cryp, RDR_PROC_COUNT(sha->id), MTK_CNT_RST);
1205 mtk_sha_write(cryp, RDR_THRESH(sha->id),
Ryder Lee785e5c62016-12-19 10:20:44 +08001206 MTK_RDR_PROC_THRESH | MTK_RDR_PROC_MODE);
1207
1208 tasklet_schedule(&sha->task);
1209 } else {
Ryder Lee132c57c2017-03-09 10:11:12 +08001210 dev_warn(cryp->dev, "SHA interrupt when no active requests.\n");
Ryder Lee785e5c62016-12-19 10:20:44 +08001211 }
1212 return IRQ_HANDLED;
1213}
1214
1215/*
1216 * The purpose of two SHA records is used to get extra performance.
1217 * It is similar to mtk_aes_record_init().
1218 */
1219static int mtk_sha_record_init(struct mtk_cryp *cryp)
1220{
1221 struct mtk_sha_rec **sha = cryp->sha;
1222 int i, err = -ENOMEM;
1223
1224 for (i = 0; i < MTK_REC_NUM; i++) {
1225 sha[i] = kzalloc(sizeof(**sha), GFP_KERNEL);
1226 if (!sha[i])
1227 goto err_cleanup;
1228
Ryder Lee132c57c2017-03-09 10:11:12 +08001229 sha[i]->cryp = cryp;
Ryder Lee785e5c62016-12-19 10:20:44 +08001230
1231 spin_lock_init(&sha[i]->lock);
1232 crypto_init_queue(&sha[i]->queue, SHA_QUEUE_SIZE);
Ryder Lee132c57c2017-03-09 10:11:12 +08001233
1234 tasklet_init(&sha[i]->task, mtk_sha_done_task,
1235 (unsigned long)sha[i]);
Ryder Lee785e5c62016-12-19 10:20:44 +08001236 }
1237
Ryder Lee132c57c2017-03-09 10:11:12 +08001238 /* Link to ring2 and ring3 respectively */
Ryder Leeb7a2be32017-03-09 10:11:13 +08001239 sha[0]->id = MTK_RING2;
1240 sha[1]->id = MTK_RING3;
Ryder Lee785e5c62016-12-19 10:20:44 +08001241
1242 cryp->rec = 1;
1243
1244 return 0;
1245
1246err_cleanup:
1247 for (; i--; )
1248 kfree(sha[i]);
1249 return err;
1250}
1251
1252static void mtk_sha_record_free(struct mtk_cryp *cryp)
1253{
1254 int i;
1255
1256 for (i = 0; i < MTK_REC_NUM; i++) {
1257 tasklet_kill(&cryp->sha[i]->task);
1258 kfree(cryp->sha[i]);
1259 }
1260}
1261
1262static void mtk_sha_unregister_algs(void)
1263{
1264 int i;
1265
1266 for (i = 0; i < ARRAY_SIZE(algs_sha1_sha224_sha256); i++)
1267 crypto_unregister_ahash(&algs_sha1_sha224_sha256[i]);
1268
1269 for (i = 0; i < ARRAY_SIZE(algs_sha384_sha512); i++)
1270 crypto_unregister_ahash(&algs_sha384_sha512[i]);
1271}
1272
1273static int mtk_sha_register_algs(void)
1274{
1275 int err, i;
1276
1277 for (i = 0; i < ARRAY_SIZE(algs_sha1_sha224_sha256); i++) {
1278 err = crypto_register_ahash(&algs_sha1_sha224_sha256[i]);
1279 if (err)
1280 goto err_sha_224_256_algs;
1281 }
1282
1283 for (i = 0; i < ARRAY_SIZE(algs_sha384_sha512); i++) {
1284 err = crypto_register_ahash(&algs_sha384_sha512[i]);
1285 if (err)
1286 goto err_sha_384_512_algs;
1287 }
1288
1289 return 0;
1290
1291err_sha_384_512_algs:
1292 for (; i--; )
1293 crypto_unregister_ahash(&algs_sha384_sha512[i]);
1294 i = ARRAY_SIZE(algs_sha1_sha224_sha256);
1295err_sha_224_256_algs:
1296 for (; i--; )
1297 crypto_unregister_ahash(&algs_sha1_sha224_sha256[i]);
1298
1299 return err;
1300}
1301
1302int mtk_hash_alg_register(struct mtk_cryp *cryp)
1303{
1304 int err;
1305
1306 INIT_LIST_HEAD(&cryp->sha_list);
1307
1308 /* Initialize two hash records */
1309 err = mtk_sha_record_init(cryp);
1310 if (err)
1311 goto err_record;
1312
Ryder Leeb7a2be32017-03-09 10:11:13 +08001313 err = devm_request_irq(cryp->dev, cryp->irq[MTK_RING2], mtk_sha_irq,
Ryder Lee132c57c2017-03-09 10:11:12 +08001314 0, "mtk-sha", cryp->sha[0]);
Ryder Lee785e5c62016-12-19 10:20:44 +08001315 if (err) {
1316 dev_err(cryp->dev, "unable to request sha irq0.\n");
1317 goto err_res;
1318 }
1319
Ryder Leeb7a2be32017-03-09 10:11:13 +08001320 err = devm_request_irq(cryp->dev, cryp->irq[MTK_RING3], mtk_sha_irq,
Ryder Lee132c57c2017-03-09 10:11:12 +08001321 0, "mtk-sha", cryp->sha[1]);
Ryder Lee785e5c62016-12-19 10:20:44 +08001322 if (err) {
1323 dev_err(cryp->dev, "unable to request sha irq1.\n");
1324 goto err_res;
1325 }
1326
1327 /* Enable ring2 and ring3 interrupt for hash */
Ryder Leeb7a2be32017-03-09 10:11:13 +08001328 mtk_sha_write(cryp, AIC_ENABLE_SET(MTK_RING2), MTK_IRQ_RDR2);
1329 mtk_sha_write(cryp, AIC_ENABLE_SET(MTK_RING3), MTK_IRQ_RDR3);
Ryder Lee785e5c62016-12-19 10:20:44 +08001330
Ryder Lee785e5c62016-12-19 10:20:44 +08001331 spin_lock(&mtk_sha.lock);
1332 list_add_tail(&cryp->sha_list, &mtk_sha.dev_list);
1333 spin_unlock(&mtk_sha.lock);
1334
1335 err = mtk_sha_register_algs();
1336 if (err)
1337 goto err_algs;
1338
1339 return 0;
1340
1341err_algs:
1342 spin_lock(&mtk_sha.lock);
1343 list_del(&cryp->sha_list);
1344 spin_unlock(&mtk_sha.lock);
Ryder Lee785e5c62016-12-19 10:20:44 +08001345err_res:
1346 mtk_sha_record_free(cryp);
1347err_record:
1348
1349 dev_err(cryp->dev, "mtk-sha initialization failed.\n");
1350 return err;
1351}
1352
1353void mtk_hash_alg_release(struct mtk_cryp *cryp)
1354{
1355 spin_lock(&mtk_sha.lock);
1356 list_del(&cryp->sha_list);
1357 spin_unlock(&mtk_sha.lock);
1358
1359 mtk_sha_unregister_algs();
Ryder Lee785e5c62016-12-19 10:20:44 +08001360 mtk_sha_record_free(cryp);
1361}