blob: 466e30bd529c7a22244a9c83c8574dd26c515d42 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Javier Martin5de88752013-03-01 12:37:53 +01002/*
3 * Cryptographic API.
4 *
5 * Support for SAHARA cryptographic accelerator.
6 *
Steffen Trumtrar5a2bb932014-12-01 13:26:34 +01007 * Copyright (c) 2014 Steffen Trumtrar <s.trumtrar@pengutronix.de>
Javier Martin5de88752013-03-01 12:37:53 +01008 * Copyright (c) 2013 Vista Silicon S.L.
9 * Author: Javier Martin <javier.martin@vista-silicon.com>
10 *
Javier Martin5de88752013-03-01 12:37:53 +010011 * Based on omap-aes.c and tegra-aes.c
12 */
13
Javier Martin5de88752013-03-01 12:37:53 +010014#include <crypto/aes.h>
Steffen Trumtrar5a2bb932014-12-01 13:26:34 +010015#include <crypto/internal/hash.h>
Herbert Xu678adec2016-06-29 18:04:05 +080016#include <crypto/internal/skcipher.h>
Steffen Trumtrar5a2bb932014-12-01 13:26:34 +010017#include <crypto/scatterwalk.h>
18#include <crypto/sha.h>
Javier Martin5de88752013-03-01 12:37:53 +010019
20#include <linux/clk.h>
21#include <linux/crypto.h>
22#include <linux/interrupt.h>
23#include <linux/io.h>
24#include <linux/irq.h>
25#include <linux/kernel.h>
Steffen Trumtrarc0c3c892014-12-01 13:26:33 +010026#include <linux/kthread.h>
Javier Martin5de88752013-03-01 12:37:53 +010027#include <linux/module.h>
Steffen Trumtrarc0c3c892014-12-01 13:26:33 +010028#include <linux/mutex.h>
Javier Martin5de88752013-03-01 12:37:53 +010029#include <linux/of.h>
Steffen Trumtrar5ed903b2014-12-01 13:26:32 +010030#include <linux/of_device.h>
Javier Martin5de88752013-03-01 12:37:53 +010031#include <linux/platform_device.h>
32
Steffen Trumtrar5a2bb932014-12-01 13:26:34 +010033#define SHA_BUFFER_LEN PAGE_SIZE
34#define SAHARA_MAX_SHA_BLOCK_SIZE SHA256_BLOCK_SIZE
35
Javier Martin5de88752013-03-01 12:37:53 +010036#define SAHARA_NAME "sahara"
37#define SAHARA_VERSION_3 3
Steffen Trumtrar5ed903b2014-12-01 13:26:32 +010038#define SAHARA_VERSION_4 4
Javier Martin5de88752013-03-01 12:37:53 +010039#define SAHARA_TIMEOUT_MS 1000
40#define SAHARA_MAX_HW_DESC 2
41#define SAHARA_MAX_HW_LINK 20
42
43#define FLAGS_MODE_MASK 0x000f
44#define FLAGS_ENCRYPT BIT(0)
45#define FLAGS_CBC BIT(1)
46#define FLAGS_NEW_KEY BIT(3)
Javier Martin5de88752013-03-01 12:37:53 +010047
48#define SAHARA_HDR_BASE 0x00800000
49#define SAHARA_HDR_SKHA_ALG_AES 0
50#define SAHARA_HDR_SKHA_OP_ENC (1 << 2)
51#define SAHARA_HDR_SKHA_MODE_ECB (0 << 3)
52#define SAHARA_HDR_SKHA_MODE_CBC (1 << 3)
53#define SAHARA_HDR_FORM_DATA (5 << 16)
54#define SAHARA_HDR_FORM_KEY (8 << 16)
55#define SAHARA_HDR_LLO (1 << 24)
56#define SAHARA_HDR_CHA_SKHA (1 << 28)
57#define SAHARA_HDR_CHA_MDHA (2 << 28)
58#define SAHARA_HDR_PARITY_BIT (1 << 31)
59
Steffen Trumtrar5a2bb932014-12-01 13:26:34 +010060#define SAHARA_HDR_MDHA_SET_MODE_MD_KEY 0x20880000
61#define SAHARA_HDR_MDHA_SET_MODE_HASH 0x208D0000
62#define SAHARA_HDR_MDHA_HASH 0xA0850000
63#define SAHARA_HDR_MDHA_STORE_DIGEST 0x20820000
64#define SAHARA_HDR_MDHA_ALG_SHA1 0
65#define SAHARA_HDR_MDHA_ALG_MD5 1
66#define SAHARA_HDR_MDHA_ALG_SHA256 2
67#define SAHARA_HDR_MDHA_ALG_SHA224 3
68#define SAHARA_HDR_MDHA_PDATA (1 << 2)
69#define SAHARA_HDR_MDHA_HMAC (1 << 3)
70#define SAHARA_HDR_MDHA_INIT (1 << 5)
71#define SAHARA_HDR_MDHA_IPAD (1 << 6)
72#define SAHARA_HDR_MDHA_OPAD (1 << 7)
73#define SAHARA_HDR_MDHA_SWAP (1 << 8)
74#define SAHARA_HDR_MDHA_MAC_FULL (1 << 9)
75#define SAHARA_HDR_MDHA_SSL (1 << 10)
76
Javier Martin5de88752013-03-01 12:37:53 +010077/* SAHARA can only process one request at a time */
78#define SAHARA_QUEUE_LENGTH 1
79
80#define SAHARA_REG_VERSION 0x00
81#define SAHARA_REG_DAR 0x04
82#define SAHARA_REG_CONTROL 0x08
83#define SAHARA_CONTROL_SET_THROTTLE(x) (((x) & 0xff) << 24)
84#define SAHARA_CONTROL_SET_MAXBURST(x) (((x) & 0xff) << 16)
85#define SAHARA_CONTROL_RNG_AUTORSD (1 << 7)
86#define SAHARA_CONTROL_ENABLE_INT (1 << 4)
87#define SAHARA_REG_CMD 0x0C
88#define SAHARA_CMD_RESET (1 << 0)
89#define SAHARA_CMD_CLEAR_INT (1 << 8)
90#define SAHARA_CMD_CLEAR_ERR (1 << 9)
91#define SAHARA_CMD_SINGLE_STEP (1 << 10)
92#define SAHARA_CMD_MODE_BATCH (1 << 16)
93#define SAHARA_CMD_MODE_DEBUG (1 << 18)
94#define SAHARA_REG_STATUS 0x10
95#define SAHARA_STATUS_GET_STATE(x) ((x) & 0x7)
96#define SAHARA_STATE_IDLE 0
97#define SAHARA_STATE_BUSY 1
98#define SAHARA_STATE_ERR 2
99#define SAHARA_STATE_FAULT 3
100#define SAHARA_STATE_COMPLETE 4
101#define SAHARA_STATE_COMP_FLAG (1 << 2)
102#define SAHARA_STATUS_DAR_FULL (1 << 3)
103#define SAHARA_STATUS_ERROR (1 << 4)
104#define SAHARA_STATUS_SECURE (1 << 5)
105#define SAHARA_STATUS_FAIL (1 << 6)
106#define SAHARA_STATUS_INIT (1 << 7)
107#define SAHARA_STATUS_RNG_RESEED (1 << 8)
108#define SAHARA_STATUS_ACTIVE_RNG (1 << 9)
109#define SAHARA_STATUS_ACTIVE_MDHA (1 << 10)
110#define SAHARA_STATUS_ACTIVE_SKHA (1 << 11)
111#define SAHARA_STATUS_MODE_BATCH (1 << 16)
112#define SAHARA_STATUS_MODE_DEDICATED (1 << 17)
113#define SAHARA_STATUS_MODE_DEBUG (1 << 18)
114#define SAHARA_STATUS_GET_ISTATE(x) (((x) >> 24) & 0xff)
115#define SAHARA_REG_ERRSTATUS 0x14
116#define SAHARA_ERRSTATUS_GET_SOURCE(x) ((x) & 0xf)
117#define SAHARA_ERRSOURCE_CHA 14
118#define SAHARA_ERRSOURCE_DMA 15
119#define SAHARA_ERRSTATUS_DMA_DIR (1 << 8)
120#define SAHARA_ERRSTATUS_GET_DMASZ(x)(((x) >> 9) & 0x3)
121#define SAHARA_ERRSTATUS_GET_DMASRC(x) (((x) >> 13) & 0x7)
122#define SAHARA_ERRSTATUS_GET_CHASRC(x) (((x) >> 16) & 0xfff)
123#define SAHARA_ERRSTATUS_GET_CHAERR(x) (((x) >> 28) & 0x3)
124#define SAHARA_REG_FADDR 0x18
125#define SAHARA_REG_CDAR 0x1C
126#define SAHARA_REG_IDAR 0x20
127
128struct sahara_hw_desc {
Arnd Bergmann75d3f812015-12-08 16:23:51 +0100129 u32 hdr;
130 u32 len1;
131 u32 p1;
132 u32 len2;
133 u32 p2;
134 u32 next;
Javier Martin5de88752013-03-01 12:37:53 +0100135};
136
137struct sahara_hw_link {
Arnd Bergmann75d3f812015-12-08 16:23:51 +0100138 u32 len;
139 u32 p;
140 u32 next;
Javier Martin5de88752013-03-01 12:37:53 +0100141};
142
143struct sahara_ctx {
Javier Martin5de88752013-03-01 12:37:53 +0100144 unsigned long flags;
Steffen Trumtrar5a2bb932014-12-01 13:26:34 +0100145
146 /* AES-specific context */
Javier Martin5de88752013-03-01 12:37:53 +0100147 int keylen;
148 u8 key[AES_KEYSIZE_128];
Kees Cookba701522018-09-18 19:10:53 -0700149 struct crypto_sync_skcipher *fallback;
Javier Martin5de88752013-03-01 12:37:53 +0100150};
151
152struct sahara_aes_reqctx {
153 unsigned long mode;
154};
155
Steffen Trumtrar5a2bb932014-12-01 13:26:34 +0100156/*
157 * struct sahara_sha_reqctx - private data per request
158 * @buf: holds data for requests smaller than block_size
159 * @rembuf: used to prepare one block_size-aligned request
160 * @context: hw-specific context for request. Digest is extracted from this
161 * @mode: specifies what type of hw-descriptor needs to be built
162 * @digest_size: length of digest for this request
163 * @context_size: length of hw-context for this request.
164 * Always digest_size + 4
165 * @buf_cnt: number of bytes saved in buf
166 * @sg_in_idx: number of hw links
167 * @in_sg: scatterlist for input data
168 * @in_sg_chain: scatterlists for chained input data
Steffen Trumtrar5a2bb932014-12-01 13:26:34 +0100169 * @total: total number of bytes for transfer
170 * @last: is this the last block
171 * @first: is this the first block
172 * @active: inside a transfer
173 */
174struct sahara_sha_reqctx {
175 u8 buf[SAHARA_MAX_SHA_BLOCK_SIZE];
176 u8 rembuf[SAHARA_MAX_SHA_BLOCK_SIZE];
177 u8 context[SHA256_DIGEST_SIZE + 4];
Steffen Trumtrar5a2bb932014-12-01 13:26:34 +0100178 unsigned int mode;
179 unsigned int digest_size;
180 unsigned int context_size;
181 unsigned int buf_cnt;
182 unsigned int sg_in_idx;
183 struct scatterlist *in_sg;
184 struct scatterlist in_sg_chain[2];
Steffen Trumtrar5a2bb932014-12-01 13:26:34 +0100185 size_t total;
186 unsigned int last;
187 unsigned int first;
188 unsigned int active;
189};
190
Javier Martin5de88752013-03-01 12:37:53 +0100191struct sahara_dev {
192 struct device *device;
Steffen Trumtrar5ed903b2014-12-01 13:26:32 +0100193 unsigned int version;
Javier Martin5de88752013-03-01 12:37:53 +0100194 void __iomem *regs_base;
195 struct clk *clk_ipg;
196 struct clk *clk_ahb;
Steffen Trumtrarc0c3c892014-12-01 13:26:33 +0100197 struct mutex queue_mutex;
198 struct task_struct *kthread;
199 struct completion dma_completion;
Javier Martin5de88752013-03-01 12:37:53 +0100200
201 struct sahara_ctx *ctx;
Javier Martin5de88752013-03-01 12:37:53 +0100202 struct crypto_queue queue;
203 unsigned long flags;
204
Javier Martin5de88752013-03-01 12:37:53 +0100205 struct sahara_hw_desc *hw_desc[SAHARA_MAX_HW_DESC];
206 dma_addr_t hw_phys_desc[SAHARA_MAX_HW_DESC];
207
208 u8 *key_base;
209 dma_addr_t key_phys_base;
210
211 u8 *iv_base;
212 dma_addr_t iv_phys_base;
213
Steffen Trumtrar5a2bb932014-12-01 13:26:34 +0100214 u8 *context_base;
215 dma_addr_t context_phys_base;
216
Javier Martin5de88752013-03-01 12:37:53 +0100217 struct sahara_hw_link *hw_link[SAHARA_MAX_HW_LINK];
218 dma_addr_t hw_phys_link[SAHARA_MAX_HW_LINK];
219
Javier Martin5de88752013-03-01 12:37:53 +0100220 size_t total;
221 struct scatterlist *in_sg;
LABBE Corentinf8e28a02015-11-19 13:38:17 +0100222 int nb_in_sg;
Javier Martin5de88752013-03-01 12:37:53 +0100223 struct scatterlist *out_sg;
LABBE Corentinf8e28a02015-11-19 13:38:17 +0100224 int nb_out_sg;
Javier Martin5de88752013-03-01 12:37:53 +0100225
226 u32 error;
Javier Martin5de88752013-03-01 12:37:53 +0100227};
228
229static struct sahara_dev *dev_ptr;
230
231static inline void sahara_write(struct sahara_dev *dev, u32 data, u32 reg)
232{
233 writel(data, dev->regs_base + reg);
234}
235
236static inline unsigned int sahara_read(struct sahara_dev *dev, u32 reg)
237{
238 return readl(dev->regs_base + reg);
239}
240
241static u32 sahara_aes_key_hdr(struct sahara_dev *dev)
242{
243 u32 hdr = SAHARA_HDR_BASE | SAHARA_HDR_SKHA_ALG_AES |
244 SAHARA_HDR_FORM_KEY | SAHARA_HDR_LLO |
245 SAHARA_HDR_CHA_SKHA | SAHARA_HDR_PARITY_BIT;
246
247 if (dev->flags & FLAGS_CBC) {
248 hdr |= SAHARA_HDR_SKHA_MODE_CBC;
249 hdr ^= SAHARA_HDR_PARITY_BIT;
250 }
251
252 if (dev->flags & FLAGS_ENCRYPT) {
253 hdr |= SAHARA_HDR_SKHA_OP_ENC;
254 hdr ^= SAHARA_HDR_PARITY_BIT;
255 }
256
257 return hdr;
258}
259
260static u32 sahara_aes_data_link_hdr(struct sahara_dev *dev)
261{
262 return SAHARA_HDR_BASE | SAHARA_HDR_FORM_DATA |
263 SAHARA_HDR_CHA_SKHA | SAHARA_HDR_PARITY_BIT;
264}
265
LABBE Corentincac367b2015-10-14 21:14:19 +0200266static const char *sahara_err_src[16] = {
Javier Martin5de88752013-03-01 12:37:53 +0100267 "No error",
268 "Header error",
269 "Descriptor length error",
270 "Descriptor length or pointer error",
271 "Link length error",
272 "Link pointer error",
273 "Input buffer error",
274 "Output buffer error",
275 "Output buffer starvation",
276 "Internal state fault",
277 "General descriptor problem",
278 "Reserved",
279 "Descriptor address error",
280 "Link address error",
281 "CHA error",
282 "DMA error"
283};
284
LABBE Corentincac367b2015-10-14 21:14:19 +0200285static const char *sahara_err_dmasize[4] = {
Javier Martin5de88752013-03-01 12:37:53 +0100286 "Byte transfer",
287 "Half-word transfer",
288 "Word transfer",
289 "Reserved"
290};
291
LABBE Corentincac367b2015-10-14 21:14:19 +0200292static const char *sahara_err_dmasrc[8] = {
Javier Martin5de88752013-03-01 12:37:53 +0100293 "No error",
294 "AHB bus error",
295 "Internal IP bus error",
296 "Parity error",
297 "DMA crosses 256 byte boundary",
298 "DMA is busy",
299 "Reserved",
300 "DMA HW error"
301};
302
LABBE Corentincac367b2015-10-14 21:14:19 +0200303static const char *sahara_cha_errsrc[12] = {
Javier Martin5de88752013-03-01 12:37:53 +0100304 "Input buffer non-empty",
305 "Illegal address",
306 "Illegal mode",
307 "Illegal data size",
308 "Illegal key size",
309 "Write during processing",
310 "CTX read during processing",
311 "HW error",
312 "Input buffer disabled/underflow",
313 "Output buffer disabled/overflow",
314 "DES key parity error",
315 "Reserved"
316};
317
LABBE Corentincac367b2015-10-14 21:14:19 +0200318static const char *sahara_cha_err[4] = { "No error", "SKHA", "MDHA", "RNG" };
Javier Martin5de88752013-03-01 12:37:53 +0100319
320static void sahara_decode_error(struct sahara_dev *dev, unsigned int error)
321{
322 u8 source = SAHARA_ERRSTATUS_GET_SOURCE(error);
323 u16 chasrc = ffs(SAHARA_ERRSTATUS_GET_CHASRC(error));
324
325 dev_err(dev->device, "%s: Error Register = 0x%08x\n", __func__, error);
326
327 dev_err(dev->device, " - %s.\n", sahara_err_src[source]);
328
329 if (source == SAHARA_ERRSOURCE_DMA) {
330 if (error & SAHARA_ERRSTATUS_DMA_DIR)
331 dev_err(dev->device, " * DMA read.\n");
332 else
333 dev_err(dev->device, " * DMA write.\n");
334
335 dev_err(dev->device, " * %s.\n",
336 sahara_err_dmasize[SAHARA_ERRSTATUS_GET_DMASZ(error)]);
337 dev_err(dev->device, " * %s.\n",
338 sahara_err_dmasrc[SAHARA_ERRSTATUS_GET_DMASRC(error)]);
339 } else if (source == SAHARA_ERRSOURCE_CHA) {
340 dev_err(dev->device, " * %s.\n",
341 sahara_cha_errsrc[chasrc]);
342 dev_err(dev->device, " * %s.\n",
343 sahara_cha_err[SAHARA_ERRSTATUS_GET_CHAERR(error)]);
344 }
345 dev_err(dev->device, "\n");
346}
347
LABBE Corentincac367b2015-10-14 21:14:19 +0200348static const char *sahara_state[4] = { "Idle", "Busy", "Error", "HW Fault" };
Javier Martin5de88752013-03-01 12:37:53 +0100349
350static void sahara_decode_status(struct sahara_dev *dev, unsigned int status)
351{
352 u8 state;
353
Joe Perches222f6b82019-04-09 09:33:13 -0700354 if (!__is_defined(DEBUG))
Javier Martin5de88752013-03-01 12:37:53 +0100355 return;
356
357 state = SAHARA_STATUS_GET_STATE(status);
358
359 dev_dbg(dev->device, "%s: Status Register = 0x%08x\n",
360 __func__, status);
361
362 dev_dbg(dev->device, " - State = %d:\n", state);
363 if (state & SAHARA_STATE_COMP_FLAG)
364 dev_dbg(dev->device, " * Descriptor completed. IRQ pending.\n");
365
366 dev_dbg(dev->device, " * %s.\n",
367 sahara_state[state & ~SAHARA_STATE_COMP_FLAG]);
368
369 if (status & SAHARA_STATUS_DAR_FULL)
370 dev_dbg(dev->device, " - DAR Full.\n");
371 if (status & SAHARA_STATUS_ERROR)
372 dev_dbg(dev->device, " - Error.\n");
373 if (status & SAHARA_STATUS_SECURE)
374 dev_dbg(dev->device, " - Secure.\n");
375 if (status & SAHARA_STATUS_FAIL)
376 dev_dbg(dev->device, " - Fail.\n");
377 if (status & SAHARA_STATUS_RNG_RESEED)
378 dev_dbg(dev->device, " - RNG Reseed Request.\n");
379 if (status & SAHARA_STATUS_ACTIVE_RNG)
380 dev_dbg(dev->device, " - RNG Active.\n");
381 if (status & SAHARA_STATUS_ACTIVE_MDHA)
382 dev_dbg(dev->device, " - MDHA Active.\n");
383 if (status & SAHARA_STATUS_ACTIVE_SKHA)
384 dev_dbg(dev->device, " - SKHA Active.\n");
385
386 if (status & SAHARA_STATUS_MODE_BATCH)
387 dev_dbg(dev->device, " - Batch Mode.\n");
388 else if (status & SAHARA_STATUS_MODE_DEDICATED)
Colin Ian King9ae811f2016-10-25 12:07:27 +0100389 dev_dbg(dev->device, " - Dedicated Mode.\n");
Javier Martin5de88752013-03-01 12:37:53 +0100390 else if (status & SAHARA_STATUS_MODE_DEBUG)
391 dev_dbg(dev->device, " - Debug Mode.\n");
392
393 dev_dbg(dev->device, " - Internal state = 0x%02x\n",
394 SAHARA_STATUS_GET_ISTATE(status));
395
396 dev_dbg(dev->device, "Current DAR: 0x%08x\n",
397 sahara_read(dev, SAHARA_REG_CDAR));
398 dev_dbg(dev->device, "Initial DAR: 0x%08x\n\n",
399 sahara_read(dev, SAHARA_REG_IDAR));
400}
401
402static void sahara_dump_descriptors(struct sahara_dev *dev)
403{
404 int i;
405
Joe Perches222f6b82019-04-09 09:33:13 -0700406 if (!__is_defined(DEBUG))
Javier Martin5de88752013-03-01 12:37:53 +0100407 return;
408
409 for (i = 0; i < SAHARA_MAX_HW_DESC; i++) {
Arnd Bergmannd4b98f22015-12-08 16:24:22 +0100410 dev_dbg(dev->device, "Descriptor (%d) (%pad):\n",
411 i, &dev->hw_phys_desc[i]);
Javier Martin5de88752013-03-01 12:37:53 +0100412 dev_dbg(dev->device, "\thdr = 0x%08x\n", dev->hw_desc[i]->hdr);
413 dev_dbg(dev->device, "\tlen1 = %u\n", dev->hw_desc[i]->len1);
414 dev_dbg(dev->device, "\tp1 = 0x%08x\n", dev->hw_desc[i]->p1);
415 dev_dbg(dev->device, "\tlen2 = %u\n", dev->hw_desc[i]->len2);
416 dev_dbg(dev->device, "\tp2 = 0x%08x\n", dev->hw_desc[i]->p2);
417 dev_dbg(dev->device, "\tnext = 0x%08x\n",
418 dev->hw_desc[i]->next);
419 }
420 dev_dbg(dev->device, "\n");
421}
422
423static void sahara_dump_links(struct sahara_dev *dev)
424{
425 int i;
426
Joe Perches222f6b82019-04-09 09:33:13 -0700427 if (!__is_defined(DEBUG))
Javier Martin5de88752013-03-01 12:37:53 +0100428 return;
429
430 for (i = 0; i < SAHARA_MAX_HW_LINK; i++) {
Arnd Bergmannd4b98f22015-12-08 16:24:22 +0100431 dev_dbg(dev->device, "Link (%d) (%pad):\n",
432 i, &dev->hw_phys_link[i]);
Javier Martin5de88752013-03-01 12:37:53 +0100433 dev_dbg(dev->device, "\tlen = %u\n", dev->hw_link[i]->len);
434 dev_dbg(dev->device, "\tp = 0x%08x\n", dev->hw_link[i]->p);
435 dev_dbg(dev->device, "\tnext = 0x%08x\n",
436 dev->hw_link[i]->next);
437 }
438 dev_dbg(dev->device, "\n");
439}
440
Javier Martin5de88752013-03-01 12:37:53 +0100441static int sahara_hw_descriptor_create(struct sahara_dev *dev)
442{
443 struct sahara_ctx *ctx = dev->ctx;
444 struct scatterlist *sg;
445 int ret;
446 int i, j;
Steffen Trumtrar17110452015-04-07 17:13:42 +0200447 int idx = 0;
Javier Martin5de88752013-03-01 12:37:53 +0100448
449 /* Copy new key if necessary */
450 if (ctx->flags & FLAGS_NEW_KEY) {
451 memcpy(dev->key_base, ctx->key, ctx->keylen);
452 ctx->flags &= ~FLAGS_NEW_KEY;
453
454 if (dev->flags & FLAGS_CBC) {
Steffen Trumtrar17110452015-04-07 17:13:42 +0200455 dev->hw_desc[idx]->len1 = AES_BLOCK_SIZE;
456 dev->hw_desc[idx]->p1 = dev->iv_phys_base;
Javier Martin5de88752013-03-01 12:37:53 +0100457 } else {
Steffen Trumtrar17110452015-04-07 17:13:42 +0200458 dev->hw_desc[idx]->len1 = 0;
459 dev->hw_desc[idx]->p1 = 0;
Javier Martin5de88752013-03-01 12:37:53 +0100460 }
Steffen Trumtrar17110452015-04-07 17:13:42 +0200461 dev->hw_desc[idx]->len2 = ctx->keylen;
462 dev->hw_desc[idx]->p2 = dev->key_phys_base;
463 dev->hw_desc[idx]->next = dev->hw_phys_desc[1];
464
465 dev->hw_desc[idx]->hdr = sahara_aes_key_hdr(dev);
466
467 idx++;
Javier Martin5de88752013-03-01 12:37:53 +0100468 }
Javier Martin5de88752013-03-01 12:37:53 +0100469
LABBE Corentind23afa12015-09-18 14:57:11 +0200470 dev->nb_in_sg = sg_nents_for_len(dev->in_sg, dev->total);
LABBE Corentin6c2b74d2015-11-04 21:13:35 +0100471 if (dev->nb_in_sg < 0) {
472 dev_err(dev->device, "Invalid numbers of src SG.\n");
473 return dev->nb_in_sg;
474 }
LABBE Corentind23afa12015-09-18 14:57:11 +0200475 dev->nb_out_sg = sg_nents_for_len(dev->out_sg, dev->total);
LABBE Corentin6c2b74d2015-11-04 21:13:35 +0100476 if (dev->nb_out_sg < 0) {
477 dev_err(dev->device, "Invalid numbers of dst SG.\n");
478 return dev->nb_out_sg;
479 }
Javier Martin5de88752013-03-01 12:37:53 +0100480 if ((dev->nb_in_sg + dev->nb_out_sg) > SAHARA_MAX_HW_LINK) {
481 dev_err(dev->device, "not enough hw links (%d)\n",
482 dev->nb_in_sg + dev->nb_out_sg);
483 return -EINVAL;
484 }
485
486 ret = dma_map_sg(dev->device, dev->in_sg, dev->nb_in_sg,
487 DMA_TO_DEVICE);
488 if (ret != dev->nb_in_sg) {
489 dev_err(dev->device, "couldn't map in sg\n");
490 goto unmap_in;
491 }
492 ret = dma_map_sg(dev->device, dev->out_sg, dev->nb_out_sg,
493 DMA_FROM_DEVICE);
494 if (ret != dev->nb_out_sg) {
495 dev_err(dev->device, "couldn't map out sg\n");
496 goto unmap_out;
497 }
498
499 /* Create input links */
Steffen Trumtrar17110452015-04-07 17:13:42 +0200500 dev->hw_desc[idx]->p1 = dev->hw_phys_link[0];
Javier Martin5de88752013-03-01 12:37:53 +0100501 sg = dev->in_sg;
502 for (i = 0; i < dev->nb_in_sg; i++) {
503 dev->hw_link[i]->len = sg->length;
504 dev->hw_link[i]->p = sg->dma_address;
505 if (i == (dev->nb_in_sg - 1)) {
506 dev->hw_link[i]->next = 0;
507 } else {
508 dev->hw_link[i]->next = dev->hw_phys_link[i + 1];
509 sg = sg_next(sg);
510 }
511 }
512
513 /* Create output links */
Steffen Trumtrar17110452015-04-07 17:13:42 +0200514 dev->hw_desc[idx]->p2 = dev->hw_phys_link[i];
Javier Martin5de88752013-03-01 12:37:53 +0100515 sg = dev->out_sg;
516 for (j = i; j < dev->nb_out_sg + i; j++) {
517 dev->hw_link[j]->len = sg->length;
518 dev->hw_link[j]->p = sg->dma_address;
519 if (j == (dev->nb_out_sg + i - 1)) {
520 dev->hw_link[j]->next = 0;
521 } else {
522 dev->hw_link[j]->next = dev->hw_phys_link[j + 1];
523 sg = sg_next(sg);
524 }
525 }
526
527 /* Fill remaining fields of hw_desc[1] */
Steffen Trumtrar17110452015-04-07 17:13:42 +0200528 dev->hw_desc[idx]->hdr = sahara_aes_data_link_hdr(dev);
529 dev->hw_desc[idx]->len1 = dev->total;
530 dev->hw_desc[idx]->len2 = dev->total;
531 dev->hw_desc[idx]->next = 0;
Javier Martin5de88752013-03-01 12:37:53 +0100532
533 sahara_dump_descriptors(dev);
534 sahara_dump_links(dev);
535
Javier Martin5de88752013-03-01 12:37:53 +0100536 sahara_write(dev, dev->hw_phys_desc[0], SAHARA_REG_DAR);
537
538 return 0;
539
540unmap_out:
541 dma_unmap_sg(dev->device, dev->out_sg, dev->nb_out_sg,
Mogens Lauridsen1e320412017-08-03 15:34:12 +0200542 DMA_FROM_DEVICE);
Javier Martin5de88752013-03-01 12:37:53 +0100543unmap_in:
544 dma_unmap_sg(dev->device, dev->in_sg, dev->nb_in_sg,
Mogens Lauridsen1e320412017-08-03 15:34:12 +0200545 DMA_TO_DEVICE);
Javier Martin5de88752013-03-01 12:37:53 +0100546
547 return -EINVAL;
548}
549
Ard Biesheuvel44c10a832019-11-09 18:09:43 +0100550static int sahara_aes_process(struct skcipher_request *req)
Javier Martin5de88752013-03-01 12:37:53 +0100551{
Steffen Trumtrarc0c3c892014-12-01 13:26:33 +0100552 struct sahara_dev *dev = dev_ptr;
Javier Martin5de88752013-03-01 12:37:53 +0100553 struct sahara_ctx *ctx;
554 struct sahara_aes_reqctx *rctx;
Javier Martin5de88752013-03-01 12:37:53 +0100555 int ret;
Nicholas Mc Guire58ed7982015-02-07 06:17:13 -0500556 unsigned long timeout;
Javier Martin5de88752013-03-01 12:37:53 +0100557
Javier Martin5de88752013-03-01 12:37:53 +0100558 /* Request is ready to be dispatched by the device */
559 dev_dbg(dev->device,
560 "dispatch request (nbytes=%d, src=%p, dst=%p)\n",
Ard Biesheuvel44c10a832019-11-09 18:09:43 +0100561 req->cryptlen, req->src, req->dst);
Javier Martin5de88752013-03-01 12:37:53 +0100562
563 /* assign new request to device */
Ard Biesheuvel44c10a832019-11-09 18:09:43 +0100564 dev->total = req->cryptlen;
Javier Martin5de88752013-03-01 12:37:53 +0100565 dev->in_sg = req->src;
566 dev->out_sg = req->dst;
567
Ard Biesheuvel44c10a832019-11-09 18:09:43 +0100568 rctx = skcipher_request_ctx(req);
569 ctx = crypto_skcipher_ctx(crypto_skcipher_reqtfm(req));
Javier Martin5de88752013-03-01 12:37:53 +0100570 rctx->mode &= FLAGS_MODE_MASK;
571 dev->flags = (dev->flags & ~FLAGS_MODE_MASK) | rctx->mode;
572
Ard Biesheuvel44c10a832019-11-09 18:09:43 +0100573 if ((dev->flags & FLAGS_CBC) && req->iv)
574 memcpy(dev->iv_base, req->iv, AES_KEYSIZE_128);
Javier Martin5de88752013-03-01 12:37:53 +0100575
576 /* assign new context to device */
Javier Martin5de88752013-03-01 12:37:53 +0100577 dev->ctx = ctx;
578
Steffen Trumtrarc0c3c892014-12-01 13:26:33 +0100579 reinit_completion(&dev->dma_completion);
580
Javier Martin5de88752013-03-01 12:37:53 +0100581 ret = sahara_hw_descriptor_create(dev);
Nicholas Mc Guire6cf02fc2015-02-07 06:27:45 -0500582 if (ret)
583 return -EINVAL;
Steffen Trumtrarc0c3c892014-12-01 13:26:33 +0100584
Nicholas Mc Guire58ed7982015-02-07 06:17:13 -0500585 timeout = wait_for_completion_timeout(&dev->dma_completion,
Steffen Trumtrarc0c3c892014-12-01 13:26:33 +0100586 msecs_to_jiffies(SAHARA_TIMEOUT_MS));
Nicholas Mc Guire58ed7982015-02-07 06:17:13 -0500587 if (!timeout) {
Steffen Trumtrarc0c3c892014-12-01 13:26:33 +0100588 dev_err(dev->device, "AES timeout\n");
589 return -ETIMEDOUT;
Javier Martin5de88752013-03-01 12:37:53 +0100590 }
Steffen Trumtrarc0c3c892014-12-01 13:26:33 +0100591
592 dma_unmap_sg(dev->device, dev->out_sg, dev->nb_out_sg,
Steffen Trumtrarc0c3c892014-12-01 13:26:33 +0100593 DMA_FROM_DEVICE);
Mogens Lauridsen1e320412017-08-03 15:34:12 +0200594 dma_unmap_sg(dev->device, dev->in_sg, dev->nb_in_sg,
595 DMA_TO_DEVICE);
Steffen Trumtrarc0c3c892014-12-01 13:26:33 +0100596
597 return 0;
Javier Martin5de88752013-03-01 12:37:53 +0100598}
599
Ard Biesheuvel44c10a832019-11-09 18:09:43 +0100600static int sahara_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,
Javier Martin5de88752013-03-01 12:37:53 +0100601 unsigned int keylen)
602{
Ard Biesheuvel44c10a832019-11-09 18:09:43 +0100603 struct sahara_ctx *ctx = crypto_skcipher_ctx(tfm);
Javier Martin5de88752013-03-01 12:37:53 +0100604
605 ctx->keylen = keylen;
606
607 /* SAHARA only supports 128bit keys */
608 if (keylen == AES_KEYSIZE_128) {
609 memcpy(ctx->key, key, keylen);
610 ctx->flags |= FLAGS_NEW_KEY;
611 return 0;
612 }
613
Herbert Xu678adec2016-06-29 18:04:05 +0800614 if (keylen != AES_KEYSIZE_192 && keylen != AES_KEYSIZE_256)
Javier Martin5de88752013-03-01 12:37:53 +0100615 return -EINVAL;
616
617 /*
618 * The requested key size is not supported by HW, do a fallback.
619 */
Kees Cookba701522018-09-18 19:10:53 -0700620 crypto_sync_skcipher_clear_flags(ctx->fallback, CRYPTO_TFM_REQ_MASK);
621 crypto_sync_skcipher_set_flags(ctx->fallback, tfm->base.crt_flags &
Herbert Xu678adec2016-06-29 18:04:05 +0800622 CRYPTO_TFM_REQ_MASK);
Eric Biggersaf5034e2019-12-30 21:19:38 -0600623 return crypto_sync_skcipher_setkey(ctx->fallback, key, keylen);
Javier Martin5de88752013-03-01 12:37:53 +0100624}
625
Ard Biesheuvel44c10a832019-11-09 18:09:43 +0100626static int sahara_aes_crypt(struct skcipher_request *req, unsigned long mode)
Javier Martin5de88752013-03-01 12:37:53 +0100627{
Ard Biesheuvel44c10a832019-11-09 18:09:43 +0100628 struct sahara_aes_reqctx *rctx = skcipher_request_ctx(req);
Javier Martin5de88752013-03-01 12:37:53 +0100629 struct sahara_dev *dev = dev_ptr;
630 int err = 0;
Javier Martin5de88752013-03-01 12:37:53 +0100631
632 dev_dbg(dev->device, "nbytes: %d, enc: %d, cbc: %d\n",
Ard Biesheuvel44c10a832019-11-09 18:09:43 +0100633 req->cryptlen, !!(mode & FLAGS_ENCRYPT), !!(mode & FLAGS_CBC));
Javier Martin5de88752013-03-01 12:37:53 +0100634
Ard Biesheuvel44c10a832019-11-09 18:09:43 +0100635 if (!IS_ALIGNED(req->cryptlen, AES_BLOCK_SIZE)) {
Javier Martin5de88752013-03-01 12:37:53 +0100636 dev_err(dev->device,
637 "request size is not exact amount of AES blocks\n");
638 return -EINVAL;
639 }
640
Javier Martin5de88752013-03-01 12:37:53 +0100641 rctx->mode = mode;
Javier Martin5de88752013-03-01 12:37:53 +0100642
Steffen Trumtrarc0c3c892014-12-01 13:26:33 +0100643 mutex_lock(&dev->queue_mutex);
Ard Biesheuvel44c10a832019-11-09 18:09:43 +0100644 err = crypto_enqueue_request(&dev->queue, &req->base);
Steffen Trumtrarc0c3c892014-12-01 13:26:33 +0100645 mutex_unlock(&dev->queue_mutex);
646
647 wake_up_process(dev->kthread);
Javier Martin5de88752013-03-01 12:37:53 +0100648
649 return err;
650}
651
Ard Biesheuvel44c10a832019-11-09 18:09:43 +0100652static int sahara_aes_ecb_encrypt(struct skcipher_request *req)
Javier Martin5de88752013-03-01 12:37:53 +0100653{
Ard Biesheuvel44c10a832019-11-09 18:09:43 +0100654 struct sahara_ctx *ctx = crypto_skcipher_ctx(
655 crypto_skcipher_reqtfm(req));
Javier Martin5de88752013-03-01 12:37:53 +0100656 int err;
657
658 if (unlikely(ctx->keylen != AES_KEYSIZE_128)) {
Kees Cookba701522018-09-18 19:10:53 -0700659 SYNC_SKCIPHER_REQUEST_ON_STACK(subreq, ctx->fallback);
Herbert Xu678adec2016-06-29 18:04:05 +0800660
Kees Cookba701522018-09-18 19:10:53 -0700661 skcipher_request_set_sync_tfm(subreq, ctx->fallback);
Herbert Xu678adec2016-06-29 18:04:05 +0800662 skcipher_request_set_callback(subreq, req->base.flags,
663 NULL, NULL);
664 skcipher_request_set_crypt(subreq, req->src, req->dst,
Ard Biesheuvel44c10a832019-11-09 18:09:43 +0100665 req->cryptlen, req->iv);
Herbert Xu678adec2016-06-29 18:04:05 +0800666 err = crypto_skcipher_encrypt(subreq);
667 skcipher_request_zero(subreq);
Javier Martin5de88752013-03-01 12:37:53 +0100668 return err;
669 }
670
671 return sahara_aes_crypt(req, FLAGS_ENCRYPT);
672}
673
Ard Biesheuvel44c10a832019-11-09 18:09:43 +0100674static int sahara_aes_ecb_decrypt(struct skcipher_request *req)
Javier Martin5de88752013-03-01 12:37:53 +0100675{
Ard Biesheuvel44c10a832019-11-09 18:09:43 +0100676 struct sahara_ctx *ctx = crypto_skcipher_ctx(
677 crypto_skcipher_reqtfm(req));
Javier Martin5de88752013-03-01 12:37:53 +0100678 int err;
679
680 if (unlikely(ctx->keylen != AES_KEYSIZE_128)) {
Kees Cookba701522018-09-18 19:10:53 -0700681 SYNC_SKCIPHER_REQUEST_ON_STACK(subreq, ctx->fallback);
Herbert Xu678adec2016-06-29 18:04:05 +0800682
Kees Cookba701522018-09-18 19:10:53 -0700683 skcipher_request_set_sync_tfm(subreq, ctx->fallback);
Herbert Xu678adec2016-06-29 18:04:05 +0800684 skcipher_request_set_callback(subreq, req->base.flags,
685 NULL, NULL);
686 skcipher_request_set_crypt(subreq, req->src, req->dst,
Ard Biesheuvel44c10a832019-11-09 18:09:43 +0100687 req->cryptlen, req->iv);
Herbert Xu678adec2016-06-29 18:04:05 +0800688 err = crypto_skcipher_decrypt(subreq);
689 skcipher_request_zero(subreq);
Javier Martin5de88752013-03-01 12:37:53 +0100690 return err;
691 }
692
693 return sahara_aes_crypt(req, 0);
694}
695
Ard Biesheuvel44c10a832019-11-09 18:09:43 +0100696static int sahara_aes_cbc_encrypt(struct skcipher_request *req)
Javier Martin5de88752013-03-01 12:37:53 +0100697{
Ard Biesheuvel44c10a832019-11-09 18:09:43 +0100698 struct sahara_ctx *ctx = crypto_skcipher_ctx(
699 crypto_skcipher_reqtfm(req));
Javier Martin5de88752013-03-01 12:37:53 +0100700 int err;
701
702 if (unlikely(ctx->keylen != AES_KEYSIZE_128)) {
Kees Cookba701522018-09-18 19:10:53 -0700703 SYNC_SKCIPHER_REQUEST_ON_STACK(subreq, ctx->fallback);
Herbert Xu678adec2016-06-29 18:04:05 +0800704
Kees Cookba701522018-09-18 19:10:53 -0700705 skcipher_request_set_sync_tfm(subreq, ctx->fallback);
Herbert Xu678adec2016-06-29 18:04:05 +0800706 skcipher_request_set_callback(subreq, req->base.flags,
707 NULL, NULL);
708 skcipher_request_set_crypt(subreq, req->src, req->dst,
Ard Biesheuvel44c10a832019-11-09 18:09:43 +0100709 req->cryptlen, req->iv);
Herbert Xu678adec2016-06-29 18:04:05 +0800710 err = crypto_skcipher_encrypt(subreq);
711 skcipher_request_zero(subreq);
Javier Martin5de88752013-03-01 12:37:53 +0100712 return err;
713 }
714
715 return sahara_aes_crypt(req, FLAGS_ENCRYPT | FLAGS_CBC);
716}
717
Ard Biesheuvel44c10a832019-11-09 18:09:43 +0100718static int sahara_aes_cbc_decrypt(struct skcipher_request *req)
Javier Martin5de88752013-03-01 12:37:53 +0100719{
Ard Biesheuvel44c10a832019-11-09 18:09:43 +0100720 struct sahara_ctx *ctx = crypto_skcipher_ctx(
721 crypto_skcipher_reqtfm(req));
Javier Martin5de88752013-03-01 12:37:53 +0100722 int err;
723
724 if (unlikely(ctx->keylen != AES_KEYSIZE_128)) {
Kees Cookba701522018-09-18 19:10:53 -0700725 SYNC_SKCIPHER_REQUEST_ON_STACK(subreq, ctx->fallback);
Herbert Xu678adec2016-06-29 18:04:05 +0800726
Kees Cookba701522018-09-18 19:10:53 -0700727 skcipher_request_set_sync_tfm(subreq, ctx->fallback);
Herbert Xu678adec2016-06-29 18:04:05 +0800728 skcipher_request_set_callback(subreq, req->base.flags,
729 NULL, NULL);
730 skcipher_request_set_crypt(subreq, req->src, req->dst,
Ard Biesheuvel44c10a832019-11-09 18:09:43 +0100731 req->cryptlen, req->iv);
Herbert Xu678adec2016-06-29 18:04:05 +0800732 err = crypto_skcipher_decrypt(subreq);
733 skcipher_request_zero(subreq);
Javier Martin5de88752013-03-01 12:37:53 +0100734 return err;
735 }
736
737 return sahara_aes_crypt(req, FLAGS_CBC);
738}
739
Ard Biesheuvel44c10a832019-11-09 18:09:43 +0100740static int sahara_aes_init_tfm(struct crypto_skcipher *tfm)
Javier Martin5de88752013-03-01 12:37:53 +0100741{
Ard Biesheuvel44c10a832019-11-09 18:09:43 +0100742 const char *name = crypto_tfm_alg_name(&tfm->base);
743 struct sahara_ctx *ctx = crypto_skcipher_ctx(tfm);
Javier Martin5de88752013-03-01 12:37:53 +0100744
Kees Cookba701522018-09-18 19:10:53 -0700745 ctx->fallback = crypto_alloc_sync_skcipher(name, 0,
Herbert Xu678adec2016-06-29 18:04:05 +0800746 CRYPTO_ALG_NEED_FALLBACK);
Javier Martin5de88752013-03-01 12:37:53 +0100747 if (IS_ERR(ctx->fallback)) {
748 pr_err("Error allocating fallback algo %s\n", name);
749 return PTR_ERR(ctx->fallback);
750 }
751
Ard Biesheuvel44c10a832019-11-09 18:09:43 +0100752 crypto_skcipher_set_reqsize(tfm, sizeof(struct sahara_aes_reqctx));
Javier Martin5de88752013-03-01 12:37:53 +0100753
754 return 0;
755}
756
Ard Biesheuvel44c10a832019-11-09 18:09:43 +0100757static void sahara_aes_exit_tfm(struct crypto_skcipher *tfm)
Javier Martin5de88752013-03-01 12:37:53 +0100758{
Ard Biesheuvel44c10a832019-11-09 18:09:43 +0100759 struct sahara_ctx *ctx = crypto_skcipher_ctx(tfm);
Javier Martin5de88752013-03-01 12:37:53 +0100760
Kees Cookba701522018-09-18 19:10:53 -0700761 crypto_free_sync_skcipher(ctx->fallback);
Javier Martin5de88752013-03-01 12:37:53 +0100762}
763
Steffen Trumtrar5a2bb932014-12-01 13:26:34 +0100764static u32 sahara_sha_init_hdr(struct sahara_dev *dev,
765 struct sahara_sha_reqctx *rctx)
766{
767 u32 hdr = 0;
768
769 hdr = rctx->mode;
770
771 if (rctx->first) {
772 hdr |= SAHARA_HDR_MDHA_SET_MODE_HASH;
773 hdr |= SAHARA_HDR_MDHA_INIT;
774 } else {
775 hdr |= SAHARA_HDR_MDHA_SET_MODE_MD_KEY;
776 }
777
778 if (rctx->last)
779 hdr |= SAHARA_HDR_MDHA_PDATA;
780
781 if (hweight_long(hdr) % 2 == 0)
782 hdr |= SAHARA_HDR_PARITY_BIT;
783
784 return hdr;
785}
786
787static int sahara_sha_hw_links_create(struct sahara_dev *dev,
788 struct sahara_sha_reqctx *rctx,
789 int start)
790{
791 struct scatterlist *sg;
792 unsigned int i;
793 int ret;
794
795 dev->in_sg = rctx->in_sg;
796
LABBE Corentind23afa12015-09-18 14:57:11 +0200797 dev->nb_in_sg = sg_nents_for_len(dev->in_sg, rctx->total);
LABBE Corentin6c2b74d2015-11-04 21:13:35 +0100798 if (dev->nb_in_sg < 0) {
799 dev_err(dev->device, "Invalid numbers of src SG.\n");
800 return dev->nb_in_sg;
801 }
Steffen Trumtrar5a2bb932014-12-01 13:26:34 +0100802 if ((dev->nb_in_sg) > SAHARA_MAX_HW_LINK) {
803 dev_err(dev->device, "not enough hw links (%d)\n",
804 dev->nb_in_sg + dev->nb_out_sg);
805 return -EINVAL;
806 }
807
LABBE Corentin640eec52015-09-23 13:55:28 +0200808 sg = dev->in_sg;
809 ret = dma_map_sg(dev->device, dev->in_sg, dev->nb_in_sg, DMA_TO_DEVICE);
810 if (!ret)
811 return -EFAULT;
Steffen Trumtrar5a2bb932014-12-01 13:26:34 +0100812
LABBE Corentin640eec52015-09-23 13:55:28 +0200813 for (i = start; i < dev->nb_in_sg + start; i++) {
814 dev->hw_link[i]->len = sg->length;
815 dev->hw_link[i]->p = sg->dma_address;
816 if (i == (dev->nb_in_sg + start - 1)) {
817 dev->hw_link[i]->next = 0;
818 } else {
Steffen Trumtrar5a2bb932014-12-01 13:26:34 +0100819 dev->hw_link[i]->next = dev->hw_phys_link[i + 1];
820 sg = sg_next(sg);
Steffen Trumtrar5a2bb932014-12-01 13:26:34 +0100821 }
822 }
823
824 return i;
825}
826
827static int sahara_sha_hw_data_descriptor_create(struct sahara_dev *dev,
828 struct sahara_sha_reqctx *rctx,
829 struct ahash_request *req,
830 int index)
831{
832 unsigned result_len;
833 int i = index;
834
835 if (rctx->first)
836 /* Create initial descriptor: #8*/
837 dev->hw_desc[index]->hdr = sahara_sha_init_hdr(dev, rctx);
838 else
839 /* Create hash descriptor: #10. Must follow #6. */
840 dev->hw_desc[index]->hdr = SAHARA_HDR_MDHA_HASH;
841
842 dev->hw_desc[index]->len1 = rctx->total;
843 if (dev->hw_desc[index]->len1 == 0) {
844 /* if len1 is 0, p1 must be 0, too */
845 dev->hw_desc[index]->p1 = 0;
846 rctx->sg_in_idx = 0;
847 } else {
848 /* Create input links */
849 dev->hw_desc[index]->p1 = dev->hw_phys_link[index];
850 i = sahara_sha_hw_links_create(dev, rctx, index);
851
852 rctx->sg_in_idx = index;
853 if (i < 0)
854 return i;
855 }
856
857 dev->hw_desc[index]->p2 = dev->hw_phys_link[i];
858
859 /* Save the context for the next operation */
860 result_len = rctx->context_size;
861 dev->hw_link[i]->p = dev->context_phys_base;
862
863 dev->hw_link[i]->len = result_len;
864 dev->hw_desc[index]->len2 = result_len;
865
866 dev->hw_link[i]->next = 0;
867
868 return 0;
869}
870
871/*
872 * Load descriptor aka #6
873 *
874 * To load a previously saved context back to the MDHA unit
875 *
876 * p1: Saved Context
877 * p2: NULL
878 *
879 */
880static int sahara_sha_hw_context_descriptor_create(struct sahara_dev *dev,
881 struct sahara_sha_reqctx *rctx,
882 struct ahash_request *req,
883 int index)
884{
885 dev->hw_desc[index]->hdr = sahara_sha_init_hdr(dev, rctx);
886
887 dev->hw_desc[index]->len1 = rctx->context_size;
888 dev->hw_desc[index]->p1 = dev->hw_phys_link[index];
889 dev->hw_desc[index]->len2 = 0;
890 dev->hw_desc[index]->p2 = 0;
891
892 dev->hw_link[index]->len = rctx->context_size;
893 dev->hw_link[index]->p = dev->context_phys_base;
894 dev->hw_link[index]->next = 0;
895
896 return 0;
897}
898
899static int sahara_walk_and_recalc(struct scatterlist *sg, unsigned int nbytes)
900{
901 if (!sg || !sg->length)
902 return nbytes;
903
904 while (nbytes && sg) {
905 if (nbytes <= sg->length) {
906 sg->length = nbytes;
907 sg_mark_end(sg);
908 break;
909 }
910 nbytes -= sg->length;
Cristian Stoica5be4d4c2015-01-20 10:06:16 +0200911 sg = sg_next(sg);
Steffen Trumtrar5a2bb932014-12-01 13:26:34 +0100912 }
913
914 return nbytes;
915}
916
917static int sahara_sha_prepare_request(struct ahash_request *req)
918{
919 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
920 struct sahara_sha_reqctx *rctx = ahash_request_ctx(req);
921 unsigned int hash_later;
922 unsigned int block_size;
923 unsigned int len;
924
925 block_size = crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
926
927 /* append bytes from previous operation */
928 len = rctx->buf_cnt + req->nbytes;
929
930 /* only the last transfer can be padded in hardware */
931 if (!rctx->last && (len < block_size)) {
932 /* to few data, save for next operation */
933 scatterwalk_map_and_copy(rctx->buf + rctx->buf_cnt, req->src,
934 0, req->nbytes, 0);
935 rctx->buf_cnt += req->nbytes;
936
937 return 0;
938 }
939
940 /* add data from previous operation first */
941 if (rctx->buf_cnt)
942 memcpy(rctx->rembuf, rctx->buf, rctx->buf_cnt);
943
944 /* data must always be a multiple of block_size */
945 hash_later = rctx->last ? 0 : len & (block_size - 1);
946 if (hash_later) {
947 unsigned int offset = req->nbytes - hash_later;
948 /* Save remaining bytes for later use */
949 scatterwalk_map_and_copy(rctx->buf, req->src, offset,
950 hash_later, 0);
951 }
952
953 /* nbytes should now be multiple of blocksize */
954 req->nbytes = req->nbytes - hash_later;
955
956 sahara_walk_and_recalc(req->src, req->nbytes);
957
958 /* have data from previous operation and current */
959 if (rctx->buf_cnt && req->nbytes) {
960 sg_init_table(rctx->in_sg_chain, 2);
961 sg_set_buf(rctx->in_sg_chain, rctx->rembuf, rctx->buf_cnt);
962
Dan Williamsc56f6d12015-08-07 18:15:13 +0200963 sg_chain(rctx->in_sg_chain, 2, req->src);
Steffen Trumtrar5a2bb932014-12-01 13:26:34 +0100964
965 rctx->total = req->nbytes + rctx->buf_cnt;
966 rctx->in_sg = rctx->in_sg_chain;
967
Steffen Trumtrar5a2bb932014-12-01 13:26:34 +0100968 req->src = rctx->in_sg_chain;
969 /* only data from previous operation */
970 } else if (rctx->buf_cnt) {
971 if (req->src)
972 rctx->in_sg = req->src;
973 else
974 rctx->in_sg = rctx->in_sg_chain;
975 /* buf was copied into rembuf above */
976 sg_init_one(rctx->in_sg, rctx->rembuf, rctx->buf_cnt);
977 rctx->total = rctx->buf_cnt;
Steffen Trumtrar5a2bb932014-12-01 13:26:34 +0100978 /* no data from previous operation */
979 } else {
980 rctx->in_sg = req->src;
981 rctx->total = req->nbytes;
982 req->src = rctx->in_sg;
Steffen Trumtrar5a2bb932014-12-01 13:26:34 +0100983 }
984
985 /* on next call, we only have the remaining data in the buffer */
986 rctx->buf_cnt = hash_later;
987
988 return -EINPROGRESS;
989}
990
Steffen Trumtrar5a2bb932014-12-01 13:26:34 +0100991static int sahara_sha_process(struct ahash_request *req)
992{
993 struct sahara_dev *dev = dev_ptr;
994 struct sahara_sha_reqctx *rctx = ahash_request_ctx(req);
Nicholas Mc Guiredf586cb2015-02-07 06:16:46 -0500995 int ret;
Nicholas Mc Guire58ed7982015-02-07 06:17:13 -0500996 unsigned long timeout;
Steffen Trumtrar5a2bb932014-12-01 13:26:34 +0100997
998 ret = sahara_sha_prepare_request(req);
999 if (!ret)
1000 return ret;
1001
1002 if (rctx->first) {
1003 sahara_sha_hw_data_descriptor_create(dev, rctx, req, 0);
1004 dev->hw_desc[0]->next = 0;
1005 rctx->first = 0;
1006 } else {
1007 memcpy(dev->context_base, rctx->context, rctx->context_size);
1008
1009 sahara_sha_hw_context_descriptor_create(dev, rctx, req, 0);
1010 dev->hw_desc[0]->next = dev->hw_phys_desc[1];
1011 sahara_sha_hw_data_descriptor_create(dev, rctx, req, 1);
1012 dev->hw_desc[1]->next = 0;
1013 }
1014
1015 sahara_dump_descriptors(dev);
1016 sahara_dump_links(dev);
1017
1018 reinit_completion(&dev->dma_completion);
1019
1020 sahara_write(dev, dev->hw_phys_desc[0], SAHARA_REG_DAR);
1021
Nicholas Mc Guire58ed7982015-02-07 06:17:13 -05001022 timeout = wait_for_completion_timeout(&dev->dma_completion,
Steffen Trumtrar5a2bb932014-12-01 13:26:34 +01001023 msecs_to_jiffies(SAHARA_TIMEOUT_MS));
Nicholas Mc Guire58ed7982015-02-07 06:17:13 -05001024 if (!timeout) {
Steffen Trumtrar5a2bb932014-12-01 13:26:34 +01001025 dev_err(dev->device, "SHA timeout\n");
1026 return -ETIMEDOUT;
1027 }
1028
1029 if (rctx->sg_in_idx)
LABBE Corentin640eec52015-09-23 13:55:28 +02001030 dma_unmap_sg(dev->device, dev->in_sg, dev->nb_in_sg,
1031 DMA_TO_DEVICE);
Steffen Trumtrar5a2bb932014-12-01 13:26:34 +01001032
1033 memcpy(rctx->context, dev->context_base, rctx->context_size);
1034
1035 if (req->result)
1036 memcpy(req->result, rctx->context, rctx->digest_size);
1037
1038 return 0;
1039}
1040
Steffen Trumtrarc0c3c892014-12-01 13:26:33 +01001041static int sahara_queue_manage(void *data)
1042{
1043 struct sahara_dev *dev = (struct sahara_dev *)data;
1044 struct crypto_async_request *async_req;
Steffen Trumtrarddacc622015-04-07 17:13:41 +02001045 struct crypto_async_request *backlog;
Steffen Trumtrarc0c3c892014-12-01 13:26:33 +01001046 int ret = 0;
1047
1048 do {
1049 __set_current_state(TASK_INTERRUPTIBLE);
1050
1051 mutex_lock(&dev->queue_mutex);
Steffen Trumtrarddacc622015-04-07 17:13:41 +02001052 backlog = crypto_get_backlog(&dev->queue);
Steffen Trumtrarc0c3c892014-12-01 13:26:33 +01001053 async_req = crypto_dequeue_request(&dev->queue);
1054 mutex_unlock(&dev->queue_mutex);
1055
Steffen Trumtrarddacc622015-04-07 17:13:41 +02001056 if (backlog)
1057 backlog->complete(backlog, -EINPROGRESS);
1058
Steffen Trumtrarc0c3c892014-12-01 13:26:33 +01001059 if (async_req) {
Steffen Trumtrar5a2bb932014-12-01 13:26:34 +01001060 if (crypto_tfm_alg_type(async_req->tfm) ==
1061 CRYPTO_ALG_TYPE_AHASH) {
1062 struct ahash_request *req =
1063 ahash_request_cast(async_req);
Steffen Trumtrarc0c3c892014-12-01 13:26:33 +01001064
Steffen Trumtrar5a2bb932014-12-01 13:26:34 +01001065 ret = sahara_sha_process(req);
1066 } else {
Ard Biesheuvel44c10a832019-11-09 18:09:43 +01001067 struct skcipher_request *req =
1068 skcipher_request_cast(async_req);
Steffen Trumtrar5a2bb932014-12-01 13:26:34 +01001069
1070 ret = sahara_aes_process(req);
1071 }
Steffen Trumtrarc0c3c892014-12-01 13:26:33 +01001072
1073 async_req->complete(async_req, ret);
1074
1075 continue;
1076 }
1077
1078 schedule();
1079 } while (!kthread_should_stop());
1080
1081 return 0;
1082}
1083
Steffen Trumtrar5a2bb932014-12-01 13:26:34 +01001084static int sahara_sha_enqueue(struct ahash_request *req, int last)
1085{
1086 struct sahara_sha_reqctx *rctx = ahash_request_ctx(req);
1087 struct sahara_dev *dev = dev_ptr;
1088 int ret;
1089
1090 if (!req->nbytes && !last)
1091 return 0;
1092
Steffen Trumtrar5a2bb932014-12-01 13:26:34 +01001093 rctx->last = last;
1094
1095 if (!rctx->active) {
1096 rctx->active = 1;
1097 rctx->first = 1;
1098 }
1099
1100 mutex_lock(&dev->queue_mutex);
1101 ret = crypto_enqueue_request(&dev->queue, &req->base);
1102 mutex_unlock(&dev->queue_mutex);
1103
1104 wake_up_process(dev->kthread);
Steffen Trumtrar5a2bb932014-12-01 13:26:34 +01001105
1106 return ret;
1107}
1108
1109static int sahara_sha_init(struct ahash_request *req)
1110{
1111 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1112 struct sahara_sha_reqctx *rctx = ahash_request_ctx(req);
1113
1114 memset(rctx, 0, sizeof(*rctx));
1115
1116 switch (crypto_ahash_digestsize(tfm)) {
1117 case SHA1_DIGEST_SIZE:
1118 rctx->mode |= SAHARA_HDR_MDHA_ALG_SHA1;
1119 rctx->digest_size = SHA1_DIGEST_SIZE;
1120 break;
1121 case SHA256_DIGEST_SIZE:
1122 rctx->mode |= SAHARA_HDR_MDHA_ALG_SHA256;
1123 rctx->digest_size = SHA256_DIGEST_SIZE;
1124 break;
1125 default:
1126 return -EINVAL;
1127 }
1128
1129 rctx->context_size = rctx->digest_size + 4;
1130 rctx->active = 0;
1131
Steffen Trumtrar5a2bb932014-12-01 13:26:34 +01001132 return 0;
1133}
1134
1135static int sahara_sha_update(struct ahash_request *req)
1136{
1137 return sahara_sha_enqueue(req, 0);
1138}
1139
1140static int sahara_sha_final(struct ahash_request *req)
1141{
1142 req->nbytes = 0;
1143 return sahara_sha_enqueue(req, 1);
1144}
1145
1146static int sahara_sha_finup(struct ahash_request *req)
1147{
1148 return sahara_sha_enqueue(req, 1);
1149}
1150
1151static int sahara_sha_digest(struct ahash_request *req)
1152{
1153 sahara_sha_init(req);
1154
1155 return sahara_sha_finup(req);
1156}
1157
1158static int sahara_sha_export(struct ahash_request *req, void *out)
1159{
Steffen Trumtrar5a2bb932014-12-01 13:26:34 +01001160 struct sahara_sha_reqctx *rctx = ahash_request_ctx(req);
1161
Fabio Estevambceab442016-02-03 10:46:51 -02001162 memcpy(out, rctx, sizeof(struct sahara_sha_reqctx));
Steffen Trumtrar5a2bb932014-12-01 13:26:34 +01001163
1164 return 0;
1165}
1166
1167static int sahara_sha_import(struct ahash_request *req, const void *in)
1168{
Steffen Trumtrar5a2bb932014-12-01 13:26:34 +01001169 struct sahara_sha_reqctx *rctx = ahash_request_ctx(req);
1170
Fabio Estevambceab442016-02-03 10:46:51 -02001171 memcpy(rctx, in, sizeof(struct sahara_sha_reqctx));
Steffen Trumtrar5a2bb932014-12-01 13:26:34 +01001172
1173 return 0;
1174}
1175
1176static int sahara_sha_cra_init(struct crypto_tfm *tfm)
1177{
Steffen Trumtrar5a2bb932014-12-01 13:26:34 +01001178 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
1179 sizeof(struct sahara_sha_reqctx) +
1180 SHA_BUFFER_LEN + SHA256_BLOCK_SIZE);
1181
1182 return 0;
1183}
1184
Ard Biesheuvel44c10a832019-11-09 18:09:43 +01001185static struct skcipher_alg aes_algs[] = {
Javier Martin5de88752013-03-01 12:37:53 +01001186{
Ard Biesheuvel44c10a832019-11-09 18:09:43 +01001187 .base.cra_name = "ecb(aes)",
1188 .base.cra_driver_name = "sahara-ecb-aes",
1189 .base.cra_priority = 300,
1190 .base.cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK,
1191 .base.cra_blocksize = AES_BLOCK_SIZE,
1192 .base.cra_ctxsize = sizeof(struct sahara_ctx),
1193 .base.cra_alignmask = 0x0,
1194 .base.cra_module = THIS_MODULE,
1195
1196 .init = sahara_aes_init_tfm,
1197 .exit = sahara_aes_exit_tfm,
1198 .min_keysize = AES_MIN_KEY_SIZE ,
1199 .max_keysize = AES_MAX_KEY_SIZE,
1200 .setkey = sahara_aes_setkey,
1201 .encrypt = sahara_aes_ecb_encrypt,
1202 .decrypt = sahara_aes_ecb_decrypt,
Javier Martin5de88752013-03-01 12:37:53 +01001203}, {
Ard Biesheuvel44c10a832019-11-09 18:09:43 +01001204 .base.cra_name = "cbc(aes)",
1205 .base.cra_driver_name = "sahara-cbc-aes",
1206 .base.cra_priority = 300,
1207 .base.cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK,
1208 .base.cra_blocksize = AES_BLOCK_SIZE,
1209 .base.cra_ctxsize = sizeof(struct sahara_ctx),
1210 .base.cra_alignmask = 0x0,
1211 .base.cra_module = THIS_MODULE,
1212
1213 .init = sahara_aes_init_tfm,
1214 .exit = sahara_aes_exit_tfm,
1215 .min_keysize = AES_MIN_KEY_SIZE ,
1216 .max_keysize = AES_MAX_KEY_SIZE,
1217 .ivsize = AES_BLOCK_SIZE,
1218 .setkey = sahara_aes_setkey,
1219 .encrypt = sahara_aes_cbc_encrypt,
1220 .decrypt = sahara_aes_cbc_decrypt,
Javier Martin5de88752013-03-01 12:37:53 +01001221}
1222};
1223
Steffen Trumtrar5a2bb932014-12-01 13:26:34 +01001224static struct ahash_alg sha_v3_algs[] = {
1225{
1226 .init = sahara_sha_init,
1227 .update = sahara_sha_update,
1228 .final = sahara_sha_final,
1229 .finup = sahara_sha_finup,
1230 .digest = sahara_sha_digest,
1231 .export = sahara_sha_export,
1232 .import = sahara_sha_import,
1233 .halg.digestsize = SHA1_DIGEST_SIZE,
Fabio Estevamd42cf2f12016-02-03 10:46:52 -02001234 .halg.statesize = sizeof(struct sahara_sha_reqctx),
Steffen Trumtrar5a2bb932014-12-01 13:26:34 +01001235 .halg.base = {
1236 .cra_name = "sha1",
1237 .cra_driver_name = "sahara-sha1",
1238 .cra_priority = 300,
Eric Biggers6a38f622018-06-30 15:16:12 -07001239 .cra_flags = CRYPTO_ALG_ASYNC |
Steffen Trumtrar5a2bb932014-12-01 13:26:34 +01001240 CRYPTO_ALG_NEED_FALLBACK,
1241 .cra_blocksize = SHA1_BLOCK_SIZE,
1242 .cra_ctxsize = sizeof(struct sahara_ctx),
1243 .cra_alignmask = 0,
1244 .cra_module = THIS_MODULE,
1245 .cra_init = sahara_sha_cra_init,
Steffen Trumtrar5a2bb932014-12-01 13:26:34 +01001246 }
1247},
1248};
1249
1250static struct ahash_alg sha_v4_algs[] = {
1251{
1252 .init = sahara_sha_init,
1253 .update = sahara_sha_update,
1254 .final = sahara_sha_final,
1255 .finup = sahara_sha_finup,
1256 .digest = sahara_sha_digest,
1257 .export = sahara_sha_export,
1258 .import = sahara_sha_import,
1259 .halg.digestsize = SHA256_DIGEST_SIZE,
Fabio Estevamd42cf2f12016-02-03 10:46:52 -02001260 .halg.statesize = sizeof(struct sahara_sha_reqctx),
Steffen Trumtrar5a2bb932014-12-01 13:26:34 +01001261 .halg.base = {
1262 .cra_name = "sha256",
1263 .cra_driver_name = "sahara-sha256",
1264 .cra_priority = 300,
Eric Biggers6a38f622018-06-30 15:16:12 -07001265 .cra_flags = CRYPTO_ALG_ASYNC |
Steffen Trumtrar5a2bb932014-12-01 13:26:34 +01001266 CRYPTO_ALG_NEED_FALLBACK,
1267 .cra_blocksize = SHA256_BLOCK_SIZE,
1268 .cra_ctxsize = sizeof(struct sahara_ctx),
1269 .cra_alignmask = 0,
1270 .cra_module = THIS_MODULE,
1271 .cra_init = sahara_sha_cra_init,
Steffen Trumtrar5a2bb932014-12-01 13:26:34 +01001272 }
1273},
1274};
1275
Javier Martin5de88752013-03-01 12:37:53 +01001276static irqreturn_t sahara_irq_handler(int irq, void *data)
1277{
1278 struct sahara_dev *dev = (struct sahara_dev *)data;
1279 unsigned int stat = sahara_read(dev, SAHARA_REG_STATUS);
1280 unsigned int err = sahara_read(dev, SAHARA_REG_ERRSTATUS);
1281
Javier Martin5de88752013-03-01 12:37:53 +01001282 sahara_write(dev, SAHARA_CMD_CLEAR_INT | SAHARA_CMD_CLEAR_ERR,
1283 SAHARA_REG_CMD);
1284
1285 sahara_decode_status(dev, stat);
1286
1287 if (SAHARA_STATUS_GET_STATE(stat) == SAHARA_STATE_BUSY) {
1288 return IRQ_NONE;
1289 } else if (SAHARA_STATUS_GET_STATE(stat) == SAHARA_STATE_COMPLETE) {
1290 dev->error = 0;
1291 } else {
1292 sahara_decode_error(dev, err);
1293 dev->error = -EINVAL;
1294 }
1295
Steffen Trumtrarc0c3c892014-12-01 13:26:33 +01001296 complete(&dev->dma_completion);
Javier Martin5de88752013-03-01 12:37:53 +01001297
1298 return IRQ_HANDLED;
1299}
1300
1301
1302static int sahara_register_algs(struct sahara_dev *dev)
1303{
Steffen Trumtrar5a2bb932014-12-01 13:26:34 +01001304 int err;
1305 unsigned int i, j, k, l;
Javier Martin5de88752013-03-01 12:37:53 +01001306
1307 for (i = 0; i < ARRAY_SIZE(aes_algs); i++) {
Ard Biesheuvel44c10a832019-11-09 18:09:43 +01001308 err = crypto_register_skcipher(&aes_algs[i]);
Javier Martin5de88752013-03-01 12:37:53 +01001309 if (err)
1310 goto err_aes_algs;
1311 }
1312
Steffen Trumtrar5a2bb932014-12-01 13:26:34 +01001313 for (k = 0; k < ARRAY_SIZE(sha_v3_algs); k++) {
1314 err = crypto_register_ahash(&sha_v3_algs[k]);
1315 if (err)
1316 goto err_sha_v3_algs;
1317 }
1318
1319 if (dev->version > SAHARA_VERSION_3)
1320 for (l = 0; l < ARRAY_SIZE(sha_v4_algs); l++) {
1321 err = crypto_register_ahash(&sha_v4_algs[l]);
1322 if (err)
1323 goto err_sha_v4_algs;
1324 }
1325
Javier Martin5de88752013-03-01 12:37:53 +01001326 return 0;
1327
Steffen Trumtrar5a2bb932014-12-01 13:26:34 +01001328err_sha_v4_algs:
1329 for (j = 0; j < l; j++)
1330 crypto_unregister_ahash(&sha_v4_algs[j]);
1331
1332err_sha_v3_algs:
1333 for (j = 0; j < k; j++)
Michael Müller0e7d4d92018-07-15 00:27:06 +02001334 crypto_unregister_ahash(&sha_v3_algs[j]);
Steffen Trumtrar5a2bb932014-12-01 13:26:34 +01001335
Javier Martin5de88752013-03-01 12:37:53 +01001336err_aes_algs:
1337 for (j = 0; j < i; j++)
Ard Biesheuvel44c10a832019-11-09 18:09:43 +01001338 crypto_unregister_skcipher(&aes_algs[j]);
Javier Martin5de88752013-03-01 12:37:53 +01001339
1340 return err;
1341}
1342
1343static void sahara_unregister_algs(struct sahara_dev *dev)
1344{
Steffen Trumtrar5a2bb932014-12-01 13:26:34 +01001345 unsigned int i;
Javier Martin5de88752013-03-01 12:37:53 +01001346
1347 for (i = 0; i < ARRAY_SIZE(aes_algs); i++)
Ard Biesheuvel44c10a832019-11-09 18:09:43 +01001348 crypto_unregister_skcipher(&aes_algs[i]);
Steffen Trumtrar5a2bb932014-12-01 13:26:34 +01001349
Michael Müller0e7d4d92018-07-15 00:27:06 +02001350 for (i = 0; i < ARRAY_SIZE(sha_v3_algs); i++)
Steffen Trumtrar5a2bb932014-12-01 13:26:34 +01001351 crypto_unregister_ahash(&sha_v3_algs[i]);
1352
1353 if (dev->version > SAHARA_VERSION_3)
1354 for (i = 0; i < ARRAY_SIZE(sha_v4_algs); i++)
1355 crypto_unregister_ahash(&sha_v4_algs[i]);
Javier Martin5de88752013-03-01 12:37:53 +01001356}
1357
Arvind Yadav249cb062017-08-13 14:34:00 +05301358static const struct platform_device_id sahara_platform_ids[] = {
Javier Martin5de88752013-03-01 12:37:53 +01001359 { .name = "sahara-imx27" },
1360 { /* sentinel */ }
1361};
1362MODULE_DEVICE_TABLE(platform, sahara_platform_ids);
1363
Arvind Yadav30aabe32017-06-27 17:11:23 +05301364static const struct of_device_id sahara_dt_ids[] = {
Steffen Trumtrar5ed903b2014-12-01 13:26:32 +01001365 { .compatible = "fsl,imx53-sahara" },
Javier Martin5de88752013-03-01 12:37:53 +01001366 { .compatible = "fsl,imx27-sahara" },
1367 { /* sentinel */ }
1368};
Arnd Bergmann68be0b1a2013-06-03 23:57:37 +02001369MODULE_DEVICE_TABLE(of, sahara_dt_ids);
Javier Martin5de88752013-03-01 12:37:53 +01001370
1371static int sahara_probe(struct platform_device *pdev)
1372{
1373 struct sahara_dev *dev;
Javier Martin5de88752013-03-01 12:37:53 +01001374 u32 version;
1375 int irq;
1376 int err;
1377 int i;
1378
Markus Elfringa8bc22f2018-02-14 14:14:05 +01001379 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
Markus Elfring0d576d92018-02-14 14:10:03 +01001380 if (!dev)
Javier Martin5de88752013-03-01 12:37:53 +01001381 return -ENOMEM;
Javier Martin5de88752013-03-01 12:37:53 +01001382
1383 dev->device = &pdev->dev;
1384 platform_set_drvdata(pdev, dev);
1385
1386 /* Get the base address */
Fabio Estevamb0d76522019-06-06 13:13:49 -03001387 dev->regs_base = devm_platform_ioremap_resource(pdev, 0);
Jingoo Han9e952752014-02-12 13:23:37 +09001388 if (IS_ERR(dev->regs_base))
1389 return PTR_ERR(dev->regs_base);
Javier Martin5de88752013-03-01 12:37:53 +01001390
1391 /* Get the IRQ */
1392 irq = platform_get_irq(pdev, 0);
Stephen Boyd514838e2019-07-30 11:15:05 -07001393 if (irq < 0)
Javier Martin5de88752013-03-01 12:37:53 +01001394 return irq;
Javier Martin5de88752013-03-01 12:37:53 +01001395
Alexander Shiyan3d6f1d12014-03-10 20:13:32 +08001396 err = devm_request_irq(&pdev->dev, irq, sahara_irq_handler,
1397 0, dev_name(&pdev->dev), dev);
1398 if (err) {
Javier Martin5de88752013-03-01 12:37:53 +01001399 dev_err(&pdev->dev, "failed to request irq\n");
Alexander Shiyan3d6f1d12014-03-10 20:13:32 +08001400 return err;
Javier Martin5de88752013-03-01 12:37:53 +01001401 }
1402
1403 /* clocks */
1404 dev->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
1405 if (IS_ERR(dev->clk_ipg)) {
1406 dev_err(&pdev->dev, "Could not get ipg clock\n");
1407 return PTR_ERR(dev->clk_ipg);
1408 }
1409
1410 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
1411 if (IS_ERR(dev->clk_ahb)) {
1412 dev_err(&pdev->dev, "Could not get ahb clock\n");
1413 return PTR_ERR(dev->clk_ahb);
1414 }
1415
1416 /* Allocate HW descriptors */
Vaishali Thakkar66c9a042015-08-18 11:36:05 +05301417 dev->hw_desc[0] = dmam_alloc_coherent(&pdev->dev,
Javier Martin5de88752013-03-01 12:37:53 +01001418 SAHARA_MAX_HW_DESC * sizeof(struct sahara_hw_desc),
1419 &dev->hw_phys_desc[0], GFP_KERNEL);
1420 if (!dev->hw_desc[0]) {
1421 dev_err(&pdev->dev, "Could not allocate hw descriptors\n");
1422 return -ENOMEM;
1423 }
1424 dev->hw_desc[1] = dev->hw_desc[0] + 1;
1425 dev->hw_phys_desc[1] = dev->hw_phys_desc[0] +
1426 sizeof(struct sahara_hw_desc);
1427
1428 /* Allocate space for iv and key */
Vaishali Thakkar66c9a042015-08-18 11:36:05 +05301429 dev->key_base = dmam_alloc_coherent(&pdev->dev, 2 * AES_KEYSIZE_128,
Javier Martin5de88752013-03-01 12:37:53 +01001430 &dev->key_phys_base, GFP_KERNEL);
1431 if (!dev->key_base) {
1432 dev_err(&pdev->dev, "Could not allocate memory for key\n");
Vaishali Thakkar66c9a042015-08-18 11:36:05 +05301433 return -ENOMEM;
Javier Martin5de88752013-03-01 12:37:53 +01001434 }
1435 dev->iv_base = dev->key_base + AES_KEYSIZE_128;
1436 dev->iv_phys_base = dev->key_phys_base + AES_KEYSIZE_128;
1437
Steffen Trumtrar5a2bb932014-12-01 13:26:34 +01001438 /* Allocate space for context: largest digest + message length field */
Vaishali Thakkar66c9a042015-08-18 11:36:05 +05301439 dev->context_base = dmam_alloc_coherent(&pdev->dev,
Steffen Trumtrar5a2bb932014-12-01 13:26:34 +01001440 SHA256_DIGEST_SIZE + 4,
1441 &dev->context_phys_base, GFP_KERNEL);
1442 if (!dev->context_base) {
1443 dev_err(&pdev->dev, "Could not allocate memory for MDHA context\n");
Vaishali Thakkar66c9a042015-08-18 11:36:05 +05301444 return -ENOMEM;
Steffen Trumtrar5a2bb932014-12-01 13:26:34 +01001445 }
1446
Javier Martin5de88752013-03-01 12:37:53 +01001447 /* Allocate space for HW links */
Vaishali Thakkar66c9a042015-08-18 11:36:05 +05301448 dev->hw_link[0] = dmam_alloc_coherent(&pdev->dev,
Javier Martin5de88752013-03-01 12:37:53 +01001449 SAHARA_MAX_HW_LINK * sizeof(struct sahara_hw_link),
1450 &dev->hw_phys_link[0], GFP_KERNEL);
Dan Carpenter393e6612013-08-20 11:51:41 +03001451 if (!dev->hw_link[0]) {
Javier Martin5de88752013-03-01 12:37:53 +01001452 dev_err(&pdev->dev, "Could not allocate hw links\n");
Vaishali Thakkar66c9a042015-08-18 11:36:05 +05301453 return -ENOMEM;
Javier Martin5de88752013-03-01 12:37:53 +01001454 }
1455 for (i = 1; i < SAHARA_MAX_HW_LINK; i++) {
1456 dev->hw_phys_link[i] = dev->hw_phys_link[i - 1] +
1457 sizeof(struct sahara_hw_link);
1458 dev->hw_link[i] = dev->hw_link[i - 1] + 1;
1459 }
1460
1461 crypto_init_queue(&dev->queue, SAHARA_QUEUE_LENGTH);
1462
Steffen Trumtrarc0c3c892014-12-01 13:26:33 +01001463 mutex_init(&dev->queue_mutex);
Steffen Trumtrar20ec9d82014-12-01 13:26:31 +01001464
Javier Martin5de88752013-03-01 12:37:53 +01001465 dev_ptr = dev;
1466
Steffen Trumtrarc0c3c892014-12-01 13:26:33 +01001467 dev->kthread = kthread_run(sahara_queue_manage, dev, "sahara_crypto");
1468 if (IS_ERR(dev->kthread)) {
Vaishali Thakkar66c9a042015-08-18 11:36:05 +05301469 return PTR_ERR(dev->kthread);
Steffen Trumtrarc0c3c892014-12-01 13:26:33 +01001470 }
Javier Martin5de88752013-03-01 12:37:53 +01001471
Steffen Trumtrarc0c3c892014-12-01 13:26:33 +01001472 init_completion(&dev->dma_completion);
Javier Martin5de88752013-03-01 12:37:53 +01001473
Fabio Estevam7eac7142015-06-20 15:30:22 -03001474 err = clk_prepare_enable(dev->clk_ipg);
1475 if (err)
Vaishali Thakkar66c9a042015-08-18 11:36:05 +05301476 return err;
Fabio Estevam7eac7142015-06-20 15:30:22 -03001477 err = clk_prepare_enable(dev->clk_ahb);
1478 if (err)
1479 goto clk_ipg_disable;
Javier Martin5de88752013-03-01 12:37:53 +01001480
1481 version = sahara_read(dev, SAHARA_REG_VERSION);
Steffen Trumtrar5ed903b2014-12-01 13:26:32 +01001482 if (of_device_is_compatible(pdev->dev.of_node, "fsl,imx27-sahara")) {
1483 if (version != SAHARA_VERSION_3)
1484 err = -ENODEV;
1485 } else if (of_device_is_compatible(pdev->dev.of_node,
1486 "fsl,imx53-sahara")) {
1487 if (((version >> 8) & 0xff) != SAHARA_VERSION_4)
1488 err = -ENODEV;
1489 version = (version >> 8) & 0xff;
1490 }
1491 if (err == -ENODEV) {
Javier Martin5de88752013-03-01 12:37:53 +01001492 dev_err(&pdev->dev, "SAHARA version %d not supported\n",
Steffen Trumtrar5ed903b2014-12-01 13:26:32 +01001493 version);
Javier Martin5de88752013-03-01 12:37:53 +01001494 goto err_algs;
1495 }
1496
Steffen Trumtrar5ed903b2014-12-01 13:26:32 +01001497 dev->version = version;
1498
Javier Martin5de88752013-03-01 12:37:53 +01001499 sahara_write(dev, SAHARA_CMD_RESET | SAHARA_CMD_MODE_BATCH,
1500 SAHARA_REG_CMD);
1501 sahara_write(dev, SAHARA_CONTROL_SET_THROTTLE(0) |
1502 SAHARA_CONTROL_SET_MAXBURST(8) |
1503 SAHARA_CONTROL_RNG_AUTORSD |
1504 SAHARA_CONTROL_ENABLE_INT,
1505 SAHARA_REG_CONTROL);
1506
1507 err = sahara_register_algs(dev);
1508 if (err)
1509 goto err_algs;
1510
1511 dev_info(&pdev->dev, "SAHARA version %d initialized\n", version);
1512
1513 return 0;
1514
1515err_algs:
Steffen Trumtrarc0c3c892014-12-01 13:26:33 +01001516 kthread_stop(dev->kthread);
Javier Martin5de88752013-03-01 12:37:53 +01001517 dev_ptr = NULL;
Fabio Estevam7eac7142015-06-20 15:30:22 -03001518 clk_disable_unprepare(dev->clk_ahb);
1519clk_ipg_disable:
1520 clk_disable_unprepare(dev->clk_ipg);
Javier Martin5de88752013-03-01 12:37:53 +01001521
1522 return err;
1523}
1524
1525static int sahara_remove(struct platform_device *pdev)
1526{
1527 struct sahara_dev *dev = platform_get_drvdata(pdev);
1528
Steffen Trumtrarc0c3c892014-12-01 13:26:33 +01001529 kthread_stop(dev->kthread);
Javier Martin5de88752013-03-01 12:37:53 +01001530
1531 sahara_unregister_algs(dev);
1532
1533 clk_disable_unprepare(dev->clk_ipg);
1534 clk_disable_unprepare(dev->clk_ahb);
1535
1536 dev_ptr = NULL;
1537
1538 return 0;
1539}
1540
1541static struct platform_driver sahara_driver = {
1542 .probe = sahara_probe,
1543 .remove = sahara_remove,
1544 .driver = {
1545 .name = SAHARA_NAME,
Sachin Kamat1b0b2602013-09-30 08:49:41 +05301546 .of_match_table = sahara_dt_ids,
Javier Martin5de88752013-03-01 12:37:53 +01001547 },
1548 .id_table = sahara_platform_ids,
1549};
1550
1551module_platform_driver(sahara_driver);
1552
1553MODULE_LICENSE("GPL");
1554MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
Steffen Trumtrar5a2bb932014-12-01 13:26:34 +01001555MODULE_AUTHOR("Steffen Trumtrar <s.trumtrar@pengutronix.de>");
Javier Martin5de88752013-03-01 12:37:53 +01001556MODULE_DESCRIPTION("SAHARA2 HW crypto accelerator");