blob: a617c5cb379722d5d2b88bf4d2142e86688f4f1d [file] [log] [blame]
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +10001/*
2 * Support for Marvell's crypto engine which can be found on some Orion5X
3 * boards.
4 *
5 * Author: Sebastian Andrzej Siewior < sebastian at breakpoint dot cc >
6 * License: GPLv2
7 *
8 */
9#include <crypto/aes.h>
10#include <crypto/algapi.h>
11#include <linux/crypto.h>
12#include <linux/interrupt.h>
13#include <linux/io.h>
14#include <linux/kthread.h>
15#include <linux/platform_device.h>
16#include <linux/scatterlist.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Paul Gortmaker4bb33cc2011-05-27 14:41:48 -040018#include <linux/module.h>
Uri Simchoni750052d2010-04-08 19:34:55 +030019#include <crypto/internal/hash.h>
20#include <crypto/sha.h>
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +100021
22#include "mv_cesa.h"
Uri Simchoni750052d2010-04-08 19:34:55 +030023
24#define MV_CESA "MV-CESA:"
25#define MAX_HW_HASH_SIZE 0xFFFF
26
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +100027/*
28 * STM:
29 * /---------------------------------------\
30 * | | request complete
31 * \./ |
32 * IDLE -> new request -> BUSY -> done -> DEQUEUE
33 * /°\ |
34 * | | more scatter entries
35 * \________________/
36 */
37enum engine_status {
38 ENGINE_IDLE,
39 ENGINE_BUSY,
40 ENGINE_W_DEQUEUE,
41};
42
43/**
44 * struct req_progress - used for every crypt request
45 * @src_sg_it: sg iterator for src
46 * @dst_sg_it: sg iterator for dst
47 * @sg_src_left: bytes left in src to process (scatter list)
48 * @src_start: offset to add to src start position (scatter list)
Uri Simchoni750052d2010-04-08 19:34:55 +030049 * @crypt_len: length of current hw crypt/hash process
Uri Simchoni3b61a902010-04-08 19:27:33 +030050 * @hw_nbytes: total bytes to process in hw for this request
Uri Simchonif0d03de2010-04-08 19:31:48 +030051 * @copy_back: whether to copy data back (crypt) or not (hash)
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +100052 * @sg_dst_left: bytes left dst to process in this scatter list
53 * @dst_start: offset to add to dst start position (scatter list)
Uri Simchoni7a5f6912010-04-08 19:29:16 +030054 * @hw_processed_bytes: number of bytes processed by hw (request).
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +100055 *
56 * sg helper are used to iterate over the scatterlist. Since the size of the
57 * SRAM may be less than the scatter size, this struct struct is used to keep
58 * track of progress within current scatterlist.
59 */
60struct req_progress {
61 struct sg_mapping_iter src_sg_it;
62 struct sg_mapping_iter dst_sg_it;
Uri Simchonia58094a2010-04-08 19:30:19 +030063 void (*complete) (void);
64 void (*process) (int is_first);
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +100065
66 /* src mostly */
67 int sg_src_left;
68 int src_start;
69 int crypt_len;
Uri Simchoni3b61a902010-04-08 19:27:33 +030070 int hw_nbytes;
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +100071 /* dst mostly */
Uri Simchonif0d03de2010-04-08 19:31:48 +030072 int copy_back;
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +100073 int sg_dst_left;
74 int dst_start;
Uri Simchoni7a5f6912010-04-08 19:29:16 +030075 int hw_processed_bytes;
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +100076};
77
78struct crypto_priv {
79 void __iomem *reg;
80 void __iomem *sram;
81 int irq;
82 struct task_struct *queue_th;
83
84 /* the lock protects queue and eng_st */
85 spinlock_t lock;
86 struct crypto_queue queue;
87 enum engine_status eng_st;
Uri Simchoni3b61a902010-04-08 19:27:33 +030088 struct crypto_async_request *cur_req;
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +100089 struct req_progress p;
90 int max_req_size;
91 int sram_size;
Uri Simchoni750052d2010-04-08 19:34:55 +030092 int has_sha1;
93 int has_hmac_sha1;
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +100094};
95
96static struct crypto_priv *cpg;
97
98struct mv_ctx {
99 u8 aes_enc_key[AES_KEY_LEN];
100 u32 aes_dec_key[8];
101 int key_len;
102 u32 need_calc_aes_dkey;
103};
104
105enum crypto_op {
106 COP_AES_ECB,
107 COP_AES_CBC,
108};
109
110struct mv_req_ctx {
111 enum crypto_op op;
112 int decrypt;
113};
114
Uri Simchoni750052d2010-04-08 19:34:55 +0300115enum hash_op {
116 COP_SHA1,
117 COP_HMAC_SHA1
118};
119
120struct mv_tfm_hash_ctx {
121 struct crypto_shash *fallback;
122 struct crypto_shash *base_hash;
123 u32 ivs[2 * SHA1_DIGEST_SIZE / 4];
124 int count_add;
125 enum hash_op op;
126};
127
128struct mv_req_hash_ctx {
129 u64 count;
130 u32 state[SHA1_DIGEST_SIZE / 4];
131 u8 buffer[SHA1_BLOCK_SIZE];
132 int first_hash; /* marks that we don't have previous state */
133 int last_chunk; /* marks that this is the 'final' request */
134 int extra_bytes; /* unprocessed bytes in buffer */
135 enum hash_op op;
136 int count_add;
Uri Simchoni750052d2010-04-08 19:34:55 +0300137};
138
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +1000139static void compute_aes_dec_key(struct mv_ctx *ctx)
140{
141 struct crypto_aes_ctx gen_aes_key;
142 int key_pos;
143
144 if (!ctx->need_calc_aes_dkey)
145 return;
146
147 crypto_aes_expand_key(&gen_aes_key, ctx->aes_enc_key, ctx->key_len);
148
149 key_pos = ctx->key_len + 24;
150 memcpy(ctx->aes_dec_key, &gen_aes_key.key_enc[key_pos], 4 * 4);
151 switch (ctx->key_len) {
152 case AES_KEYSIZE_256:
153 key_pos -= 2;
154 /* fall */
155 case AES_KEYSIZE_192:
156 key_pos -= 2;
157 memcpy(&ctx->aes_dec_key[4], &gen_aes_key.key_enc[key_pos],
158 4 * 4);
159 break;
160 }
161 ctx->need_calc_aes_dkey = 0;
162}
163
164static int mv_setkey_aes(struct crypto_ablkcipher *cipher, const u8 *key,
165 unsigned int len)
166{
167 struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
168 struct mv_ctx *ctx = crypto_tfm_ctx(tfm);
169
170 switch (len) {
171 case AES_KEYSIZE_128:
172 case AES_KEYSIZE_192:
173 case AES_KEYSIZE_256:
174 break;
175 default:
176 crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
177 return -EINVAL;
178 }
179 ctx->key_len = len;
180 ctx->need_calc_aes_dkey = 1;
181
182 memcpy(ctx->aes_enc_key, key, AES_KEY_LEN);
183 return 0;
184}
185
Uri Simchoni15d4dd32010-04-08 19:27:02 +0300186static void copy_src_to_buf(struct req_progress *p, char *dbuf, int len)
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +1000187{
188 int ret;
Uri Simchoni15d4dd32010-04-08 19:27:02 +0300189 void *sbuf;
Phil Sutter6677a772011-05-05 15:29:02 +0200190 int copy_len;
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +1000191
Phil Sutter6677a772011-05-05 15:29:02 +0200192 while (len) {
Uri Simchoni15d4dd32010-04-08 19:27:02 +0300193 if (!p->sg_src_left) {
194 ret = sg_miter_next(&p->src_sg_it);
195 BUG_ON(!ret);
196 p->sg_src_left = p->src_sg_it.length;
197 p->src_start = 0;
198 }
199
200 sbuf = p->src_sg_it.addr + p->src_start;
201
Phil Sutter6677a772011-05-05 15:29:02 +0200202 copy_len = min(p->sg_src_left, len);
203 memcpy(dbuf, sbuf, copy_len);
204
205 p->src_start += copy_len;
206 p->sg_src_left -= copy_len;
207
208 len -= copy_len;
209 dbuf += copy_len;
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +1000210 }
Uri Simchoni15d4dd32010-04-08 19:27:02 +0300211}
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +1000212
Uri Simchoni3b61a902010-04-08 19:27:33 +0300213static void setup_data_in(void)
Uri Simchoni15d4dd32010-04-08 19:27:02 +0300214{
215 struct req_progress *p = &cpg->p;
Uri Simchoni0c5c6c42010-04-08 19:33:26 +0300216 int data_in_sram =
Uri Simchoni7a5f6912010-04-08 19:29:16 +0300217 min(p->hw_nbytes - p->hw_processed_bytes, cpg->max_req_size);
Uri Simchoni0c5c6c42010-04-08 19:33:26 +0300218 copy_src_to_buf(p, cpg->sram + SRAM_DATA_IN_START + p->crypt_len,
219 data_in_sram - p->crypt_len);
220 p->crypt_len = data_in_sram;
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +1000221}
222
223static void mv_process_current_q(int first_block)
224{
Uri Simchoni3b61a902010-04-08 19:27:33 +0300225 struct ablkcipher_request *req = ablkcipher_request_cast(cpg->cur_req);
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +1000226 struct mv_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
227 struct mv_req_ctx *req_ctx = ablkcipher_request_ctx(req);
228 struct sec_accel_config op;
229
230 switch (req_ctx->op) {
231 case COP_AES_ECB:
232 op.config = CFG_OP_CRYPT_ONLY | CFG_ENCM_AES | CFG_ENC_MODE_ECB;
233 break;
234 case COP_AES_CBC:
Uri Simchoni6bc6fcd2010-04-08 19:25:56 +0300235 default:
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +1000236 op.config = CFG_OP_CRYPT_ONLY | CFG_ENCM_AES | CFG_ENC_MODE_CBC;
237 op.enc_iv = ENC_IV_POINT(SRAM_DATA_IV) |
238 ENC_IV_BUF_POINT(SRAM_DATA_IV_BUF);
239 if (first_block)
240 memcpy(cpg->sram + SRAM_DATA_IV, req->info, 16);
241 break;
242 }
243 if (req_ctx->decrypt) {
244 op.config |= CFG_DIR_DEC;
245 memcpy(cpg->sram + SRAM_DATA_KEY_P, ctx->aes_dec_key,
246 AES_KEY_LEN);
247 } else {
248 op.config |= CFG_DIR_ENC;
249 memcpy(cpg->sram + SRAM_DATA_KEY_P, ctx->aes_enc_key,
250 AES_KEY_LEN);
251 }
252
253 switch (ctx->key_len) {
254 case AES_KEYSIZE_128:
255 op.config |= CFG_AES_LEN_128;
256 break;
257 case AES_KEYSIZE_192:
258 op.config |= CFG_AES_LEN_192;
259 break;
260 case AES_KEYSIZE_256:
261 op.config |= CFG_AES_LEN_256;
262 break;
263 }
264 op.enc_p = ENC_P_SRC(SRAM_DATA_IN_START) |
265 ENC_P_DST(SRAM_DATA_OUT_START);
266 op.enc_key_p = SRAM_DATA_KEY_P;
267
Uri Simchoni3b61a902010-04-08 19:27:33 +0300268 setup_data_in();
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +1000269 op.enc_len = cpg->p.crypt_len;
270 memcpy(cpg->sram + SRAM_CONFIG, &op,
271 sizeof(struct sec_accel_config));
272
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +1000273 /* GO */
274 writel(SEC_CMD_EN_SEC_ACCL0, cpg->reg + SEC_ACCEL_CMD);
275
276 /*
277 * XXX: add timer if the interrupt does not occur for some mystery
278 * reason
279 */
280}
281
282static void mv_crypto_algo_completion(void)
283{
Uri Simchoni3b61a902010-04-08 19:27:33 +0300284 struct ablkcipher_request *req = ablkcipher_request_cast(cpg->cur_req);
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +1000285 struct mv_req_ctx *req_ctx = ablkcipher_request_ctx(req);
286
Uri Simchonia58094a2010-04-08 19:30:19 +0300287 sg_miter_stop(&cpg->p.src_sg_it);
288 sg_miter_stop(&cpg->p.dst_sg_it);
289
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +1000290 if (req_ctx->op != COP_AES_CBC)
291 return ;
292
293 memcpy(req->info, cpg->sram + SRAM_DATA_IV_BUF, 16);
294}
295
Uri Simchoni750052d2010-04-08 19:34:55 +0300296static void mv_process_hash_current(int first_block)
297{
298 struct ahash_request *req = ahash_request_cast(cpg->cur_req);
Phil Suttercc8d3502011-05-05 15:29:03 +0200299 const struct mv_tfm_hash_ctx *tfm_ctx = crypto_tfm_ctx(req->base.tfm);
Uri Simchoni750052d2010-04-08 19:34:55 +0300300 struct mv_req_hash_ctx *req_ctx = ahash_request_ctx(req);
301 struct req_progress *p = &cpg->p;
302 struct sec_accel_config op = { 0 };
303 int is_last;
304
305 switch (req_ctx->op) {
306 case COP_SHA1:
307 default:
308 op.config = CFG_OP_MAC_ONLY | CFG_MACM_SHA1;
309 break;
310 case COP_HMAC_SHA1:
311 op.config = CFG_OP_MAC_ONLY | CFG_MACM_HMAC_SHA1;
Phil Suttercc8d3502011-05-05 15:29:03 +0200312 memcpy(cpg->sram + SRAM_HMAC_IV_IN,
313 tfm_ctx->ivs, sizeof(tfm_ctx->ivs));
Uri Simchoni750052d2010-04-08 19:34:55 +0300314 break;
315 }
316
317 op.mac_src_p =
318 MAC_SRC_DATA_P(SRAM_DATA_IN_START) | MAC_SRC_TOTAL_LEN((u32)
319 req_ctx->
320 count);
321
322 setup_data_in();
323
324 op.mac_digest =
325 MAC_DIGEST_P(SRAM_DIGEST_BUF) | MAC_FRAG_LEN(p->crypt_len);
326 op.mac_iv =
327 MAC_INNER_IV_P(SRAM_HMAC_IV_IN) |
328 MAC_OUTER_IV_P(SRAM_HMAC_IV_OUT);
329
330 is_last = req_ctx->last_chunk
331 && (p->hw_processed_bytes + p->crypt_len >= p->hw_nbytes)
332 && (req_ctx->count <= MAX_HW_HASH_SIZE);
333 if (req_ctx->first_hash) {
334 if (is_last)
335 op.config |= CFG_NOT_FRAG;
336 else
337 op.config |= CFG_FIRST_FRAG;
338
339 req_ctx->first_hash = 0;
340 } else {
341 if (is_last)
342 op.config |= CFG_LAST_FRAG;
343 else
344 op.config |= CFG_MID_FRAG;
Phil Sutter86523482011-05-05 15:29:04 +0200345
346 writel(req_ctx->state[0], cpg->reg + DIGEST_INITIAL_VAL_A);
347 writel(req_ctx->state[1], cpg->reg + DIGEST_INITIAL_VAL_B);
348 writel(req_ctx->state[2], cpg->reg + DIGEST_INITIAL_VAL_C);
349 writel(req_ctx->state[3], cpg->reg + DIGEST_INITIAL_VAL_D);
350 writel(req_ctx->state[4], cpg->reg + DIGEST_INITIAL_VAL_E);
Uri Simchoni750052d2010-04-08 19:34:55 +0300351 }
352
353 memcpy(cpg->sram + SRAM_CONFIG, &op, sizeof(struct sec_accel_config));
354
Uri Simchoni750052d2010-04-08 19:34:55 +0300355 /* GO */
356 writel(SEC_CMD_EN_SEC_ACCL0, cpg->reg + SEC_ACCEL_CMD);
357
358 /*
359 * XXX: add timer if the interrupt does not occur for some mystery
360 * reason
361 */
362}
363
364static inline int mv_hash_import_sha1_ctx(const struct mv_req_hash_ctx *ctx,
365 struct shash_desc *desc)
366{
367 int i;
368 struct sha1_state shash_state;
369
370 shash_state.count = ctx->count + ctx->count_add;
371 for (i = 0; i < 5; i++)
372 shash_state.state[i] = ctx->state[i];
373 memcpy(shash_state.buffer, ctx->buffer, sizeof(shash_state.buffer));
374 return crypto_shash_import(desc, &shash_state);
375}
376
377static int mv_hash_final_fallback(struct ahash_request *req)
378{
379 const struct mv_tfm_hash_ctx *tfm_ctx = crypto_tfm_ctx(req->base.tfm);
380 struct mv_req_hash_ctx *req_ctx = ahash_request_ctx(req);
381 struct {
382 struct shash_desc shash;
383 char ctx[crypto_shash_descsize(tfm_ctx->fallback)];
384 } desc;
385 int rc;
386
387 desc.shash.tfm = tfm_ctx->fallback;
388 desc.shash.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
389 if (unlikely(req_ctx->first_hash)) {
390 crypto_shash_init(&desc.shash);
391 crypto_shash_update(&desc.shash, req_ctx->buffer,
392 req_ctx->extra_bytes);
393 } else {
394 /* only SHA1 for now....
395 */
396 rc = mv_hash_import_sha1_ctx(req_ctx, &desc.shash);
397 if (rc)
398 goto out;
399 }
400 rc = crypto_shash_final(&desc.shash, req->result);
401out:
402 return rc;
403}
404
405static void mv_hash_algo_completion(void)
406{
407 struct ahash_request *req = ahash_request_cast(cpg->cur_req);
408 struct mv_req_hash_ctx *ctx = ahash_request_ctx(req);
409
410 if (ctx->extra_bytes)
411 copy_src_to_buf(&cpg->p, ctx->buffer, ctx->extra_bytes);
412 sg_miter_stop(&cpg->p.src_sg_it);
413
Uri Simchoni750052d2010-04-08 19:34:55 +0300414 if (likely(ctx->last_chunk)) {
415 if (likely(ctx->count <= MAX_HW_HASH_SIZE)) {
416 memcpy(req->result, cpg->sram + SRAM_DIGEST_BUF,
417 crypto_ahash_digestsize(crypto_ahash_reqtfm
418 (req)));
419 } else
420 mv_hash_final_fallback(req);
Phil Sutter7a1c6bc2011-05-05 15:29:01 +0200421 } else {
422 ctx->state[0] = readl(cpg->reg + DIGEST_INITIAL_VAL_A);
423 ctx->state[1] = readl(cpg->reg + DIGEST_INITIAL_VAL_B);
424 ctx->state[2] = readl(cpg->reg + DIGEST_INITIAL_VAL_C);
425 ctx->state[3] = readl(cpg->reg + DIGEST_INITIAL_VAL_D);
426 ctx->state[4] = readl(cpg->reg + DIGEST_INITIAL_VAL_E);
Uri Simchoni750052d2010-04-08 19:34:55 +0300427 }
428}
429
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +1000430static void dequeue_complete_req(void)
431{
Uri Simchoni3b61a902010-04-08 19:27:33 +0300432 struct crypto_async_request *req = cpg->cur_req;
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +1000433 void *buf;
434 int ret;
Uri Simchoni7a5f6912010-04-08 19:29:16 +0300435 cpg->p.hw_processed_bytes += cpg->p.crypt_len;
Uri Simchonif0d03de2010-04-08 19:31:48 +0300436 if (cpg->p.copy_back) {
437 int need_copy_len = cpg->p.crypt_len;
438 int sram_offset = 0;
439 do {
440 int dst_copy;
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +1000441
Uri Simchonif0d03de2010-04-08 19:31:48 +0300442 if (!cpg->p.sg_dst_left) {
443 ret = sg_miter_next(&cpg->p.dst_sg_it);
444 BUG_ON(!ret);
445 cpg->p.sg_dst_left = cpg->p.dst_sg_it.length;
446 cpg->p.dst_start = 0;
447 }
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +1000448
Uri Simchonif0d03de2010-04-08 19:31:48 +0300449 buf = cpg->p.dst_sg_it.addr;
450 buf += cpg->p.dst_start;
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +1000451
Uri Simchonif0d03de2010-04-08 19:31:48 +0300452 dst_copy = min(need_copy_len, cpg->p.sg_dst_left);
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +1000453
Uri Simchonif0d03de2010-04-08 19:31:48 +0300454 memcpy(buf,
455 cpg->sram + SRAM_DATA_OUT_START + sram_offset,
456 dst_copy);
457 sram_offset += dst_copy;
458 cpg->p.sg_dst_left -= dst_copy;
459 need_copy_len -= dst_copy;
460 cpg->p.dst_start += dst_copy;
461 } while (need_copy_len > 0);
462 }
463
Uri Simchoni0c5c6c42010-04-08 19:33:26 +0300464 cpg->p.crypt_len = 0;
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +1000465
466 BUG_ON(cpg->eng_st != ENGINE_W_DEQUEUE);
Uri Simchoni7a5f6912010-04-08 19:29:16 +0300467 if (cpg->p.hw_processed_bytes < cpg->p.hw_nbytes) {
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +1000468 /* process next scatter list entry */
469 cpg->eng_st = ENGINE_BUSY;
Uri Simchonia58094a2010-04-08 19:30:19 +0300470 cpg->p.process(0);
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +1000471 } else {
Uri Simchonia58094a2010-04-08 19:30:19 +0300472 cpg->p.complete();
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +1000473 cpg->eng_st = ENGINE_IDLE;
Uri Simchoni0328ac22010-04-08 19:25:37 +0300474 local_bh_disable();
Uri Simchoni3b61a902010-04-08 19:27:33 +0300475 req->complete(req, 0);
Uri Simchoni0328ac22010-04-08 19:25:37 +0300476 local_bh_enable();
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +1000477 }
478}
479
480static int count_sgs(struct scatterlist *sl, unsigned int total_bytes)
481{
482 int i = 0;
Uri Simchoni15d4dd32010-04-08 19:27:02 +0300483 size_t cur_len;
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +1000484
Phil Sutter6ef84502011-05-05 15:29:06 +0200485 while (sl) {
Uri Simchoni15d4dd32010-04-08 19:27:02 +0300486 cur_len = sl[i].length;
487 ++i;
488 if (total_bytes > cur_len)
489 total_bytes -= cur_len;
490 else
491 break;
492 }
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +1000493
494 return i;
495}
496
Uri Simchoni750052d2010-04-08 19:34:55 +0300497static void mv_start_new_crypt_req(struct ablkcipher_request *req)
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +1000498{
Uri Simchoni3b61a902010-04-08 19:27:33 +0300499 struct req_progress *p = &cpg->p;
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +1000500 int num_sgs;
501
Uri Simchoni3b61a902010-04-08 19:27:33 +0300502 cpg->cur_req = &req->base;
503 memset(p, 0, sizeof(struct req_progress));
504 p->hw_nbytes = req->nbytes;
Uri Simchonia58094a2010-04-08 19:30:19 +0300505 p->complete = mv_crypto_algo_completion;
506 p->process = mv_process_current_q;
Uri Simchonif0d03de2010-04-08 19:31:48 +0300507 p->copy_back = 1;
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +1000508
509 num_sgs = count_sgs(req->src, req->nbytes);
Uri Simchoni3b61a902010-04-08 19:27:33 +0300510 sg_miter_start(&p->src_sg_it, req->src, num_sgs, SG_MITER_FROM_SG);
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +1000511
512 num_sgs = count_sgs(req->dst, req->nbytes);
Uri Simchoni3b61a902010-04-08 19:27:33 +0300513 sg_miter_start(&p->dst_sg_it, req->dst, num_sgs, SG_MITER_TO_SG);
514
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +1000515 mv_process_current_q(1);
516}
517
Uri Simchoni750052d2010-04-08 19:34:55 +0300518static void mv_start_new_hash_req(struct ahash_request *req)
519{
520 struct req_progress *p = &cpg->p;
521 struct mv_req_hash_ctx *ctx = ahash_request_ctx(req);
Uri Simchoni750052d2010-04-08 19:34:55 +0300522 int num_sgs, hw_bytes, old_extra_bytes, rc;
523 cpg->cur_req = &req->base;
524 memset(p, 0, sizeof(struct req_progress));
525 hw_bytes = req->nbytes + ctx->extra_bytes;
526 old_extra_bytes = ctx->extra_bytes;
527
Uri Simchoni750052d2010-04-08 19:34:55 +0300528 ctx->extra_bytes = hw_bytes % SHA1_BLOCK_SIZE;
529 if (ctx->extra_bytes != 0
530 && (!ctx->last_chunk || ctx->count > MAX_HW_HASH_SIZE))
531 hw_bytes -= ctx->extra_bytes;
532 else
533 ctx->extra_bytes = 0;
534
535 num_sgs = count_sgs(req->src, req->nbytes);
536 sg_miter_start(&p->src_sg_it, req->src, num_sgs, SG_MITER_FROM_SG);
537
538 if (hw_bytes) {
539 p->hw_nbytes = hw_bytes;
540 p->complete = mv_hash_algo_completion;
541 p->process = mv_process_hash_current;
542
Phil Sutter77599952011-05-05 15:29:05 +0200543 if (unlikely(old_extra_bytes)) {
544 memcpy(cpg->sram + SRAM_DATA_IN_START, ctx->buffer,
545 old_extra_bytes);
546 p->crypt_len = old_extra_bytes;
547 }
548
Uri Simchoni750052d2010-04-08 19:34:55 +0300549 mv_process_hash_current(1);
550 } else {
551 copy_src_to_buf(p, ctx->buffer + old_extra_bytes,
552 ctx->extra_bytes - old_extra_bytes);
553 sg_miter_stop(&p->src_sg_it);
554 if (ctx->last_chunk)
555 rc = mv_hash_final_fallback(req);
556 else
557 rc = 0;
558 cpg->eng_st = ENGINE_IDLE;
559 local_bh_disable();
560 req->base.complete(&req->base, rc);
561 local_bh_enable();
562 }
563}
564
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +1000565static int queue_manag(void *data)
566{
567 cpg->eng_st = ENGINE_IDLE;
568 do {
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +1000569 struct crypto_async_request *async_req = NULL;
570 struct crypto_async_request *backlog;
571
572 __set_current_state(TASK_INTERRUPTIBLE);
573
574 if (cpg->eng_st == ENGINE_W_DEQUEUE)
575 dequeue_complete_req();
576
577 spin_lock_irq(&cpg->lock);
578 if (cpg->eng_st == ENGINE_IDLE) {
579 backlog = crypto_get_backlog(&cpg->queue);
580 async_req = crypto_dequeue_request(&cpg->queue);
581 if (async_req) {
582 BUG_ON(cpg->eng_st != ENGINE_IDLE);
583 cpg->eng_st = ENGINE_BUSY;
584 }
585 }
586 spin_unlock_irq(&cpg->lock);
587
588 if (backlog) {
589 backlog->complete(backlog, -EINPROGRESS);
590 backlog = NULL;
591 }
592
593 if (async_req) {
Uri Simchoni750052d2010-04-08 19:34:55 +0300594 if (async_req->tfm->__crt_alg->cra_type !=
595 &crypto_ahash_type) {
596 struct ablkcipher_request *req =
Phil Sutter042e9e72011-05-05 15:28:57 +0200597 ablkcipher_request_cast(async_req);
Uri Simchoni750052d2010-04-08 19:34:55 +0300598 mv_start_new_crypt_req(req);
599 } else {
600 struct ahash_request *req =
601 ahash_request_cast(async_req);
602 mv_start_new_hash_req(req);
603 }
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +1000604 async_req = NULL;
605 }
606
607 schedule();
608
609 } while (!kthread_should_stop());
610 return 0;
611}
612
Uri Simchoni3b61a902010-04-08 19:27:33 +0300613static int mv_handle_req(struct crypto_async_request *req)
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +1000614{
615 unsigned long flags;
616 int ret;
617
618 spin_lock_irqsave(&cpg->lock, flags);
Uri Simchoni3b61a902010-04-08 19:27:33 +0300619 ret = crypto_enqueue_request(&cpg->queue, req);
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +1000620 spin_unlock_irqrestore(&cpg->lock, flags);
621 wake_up_process(cpg->queue_th);
622 return ret;
623}
624
625static int mv_enc_aes_ecb(struct ablkcipher_request *req)
626{
627 struct mv_req_ctx *req_ctx = ablkcipher_request_ctx(req);
628
629 req_ctx->op = COP_AES_ECB;
630 req_ctx->decrypt = 0;
631
Uri Simchoni3b61a902010-04-08 19:27:33 +0300632 return mv_handle_req(&req->base);
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +1000633}
634
635static int mv_dec_aes_ecb(struct ablkcipher_request *req)
636{
637 struct mv_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
638 struct mv_req_ctx *req_ctx = ablkcipher_request_ctx(req);
639
640 req_ctx->op = COP_AES_ECB;
641 req_ctx->decrypt = 1;
642
643 compute_aes_dec_key(ctx);
Uri Simchoni3b61a902010-04-08 19:27:33 +0300644 return mv_handle_req(&req->base);
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +1000645}
646
647static int mv_enc_aes_cbc(struct ablkcipher_request *req)
648{
649 struct mv_req_ctx *req_ctx = ablkcipher_request_ctx(req);
650
651 req_ctx->op = COP_AES_CBC;
652 req_ctx->decrypt = 0;
653
Uri Simchoni3b61a902010-04-08 19:27:33 +0300654 return mv_handle_req(&req->base);
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +1000655}
656
657static int mv_dec_aes_cbc(struct ablkcipher_request *req)
658{
659 struct mv_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
660 struct mv_req_ctx *req_ctx = ablkcipher_request_ctx(req);
661
662 req_ctx->op = COP_AES_CBC;
663 req_ctx->decrypt = 1;
664
665 compute_aes_dec_key(ctx);
Uri Simchoni3b61a902010-04-08 19:27:33 +0300666 return mv_handle_req(&req->base);
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +1000667}
668
669static int mv_cra_init(struct crypto_tfm *tfm)
670{
671 tfm->crt_ablkcipher.reqsize = sizeof(struct mv_req_ctx);
672 return 0;
673}
674
Uri Simchoni750052d2010-04-08 19:34:55 +0300675static void mv_init_hash_req_ctx(struct mv_req_hash_ctx *ctx, int op,
676 int is_last, unsigned int req_len,
677 int count_add)
678{
679 memset(ctx, 0, sizeof(*ctx));
680 ctx->op = op;
681 ctx->count = req_len;
682 ctx->first_hash = 1;
683 ctx->last_chunk = is_last;
684 ctx->count_add = count_add;
685}
686
687static void mv_update_hash_req_ctx(struct mv_req_hash_ctx *ctx, int is_last,
688 unsigned req_len)
689{
690 ctx->last_chunk = is_last;
691 ctx->count += req_len;
692}
693
694static int mv_hash_init(struct ahash_request *req)
695{
696 const struct mv_tfm_hash_ctx *tfm_ctx = crypto_tfm_ctx(req->base.tfm);
697 mv_init_hash_req_ctx(ahash_request_ctx(req), tfm_ctx->op, 0, 0,
698 tfm_ctx->count_add);
699 return 0;
700}
701
702static int mv_hash_update(struct ahash_request *req)
703{
704 if (!req->nbytes)
705 return 0;
706
707 mv_update_hash_req_ctx(ahash_request_ctx(req), 0, req->nbytes);
708 return mv_handle_req(&req->base);
709}
710
711static int mv_hash_final(struct ahash_request *req)
712{
713 struct mv_req_hash_ctx *ctx = ahash_request_ctx(req);
Phil Sutter6ef84502011-05-05 15:29:06 +0200714
Phil Sutterf8f54e12012-02-27 12:17:04 +0100715 ahash_request_set_crypt(req, NULL, req->result, 0);
Uri Simchoni750052d2010-04-08 19:34:55 +0300716 mv_update_hash_req_ctx(ctx, 1, 0);
717 return mv_handle_req(&req->base);
718}
719
720static int mv_hash_finup(struct ahash_request *req)
721{
Uri Simchoni750052d2010-04-08 19:34:55 +0300722 mv_update_hash_req_ctx(ahash_request_ctx(req), 1, req->nbytes);
723 return mv_handle_req(&req->base);
724}
725
726static int mv_hash_digest(struct ahash_request *req)
727{
728 const struct mv_tfm_hash_ctx *tfm_ctx = crypto_tfm_ctx(req->base.tfm);
729 mv_init_hash_req_ctx(ahash_request_ctx(req), tfm_ctx->op, 1,
730 req->nbytes, tfm_ctx->count_add);
731 return mv_handle_req(&req->base);
732}
733
734static void mv_hash_init_ivs(struct mv_tfm_hash_ctx *ctx, const void *istate,
735 const void *ostate)
736{
737 const struct sha1_state *isha1_state = istate, *osha1_state = ostate;
738 int i;
739 for (i = 0; i < 5; i++) {
740 ctx->ivs[i] = cpu_to_be32(isha1_state->state[i]);
741 ctx->ivs[i + 5] = cpu_to_be32(osha1_state->state[i]);
742 }
743}
744
745static int mv_hash_setkey(struct crypto_ahash *tfm, const u8 * key,
746 unsigned int keylen)
747{
748 int rc;
749 struct mv_tfm_hash_ctx *ctx = crypto_tfm_ctx(&tfm->base);
750 int bs, ds, ss;
751
752 if (!ctx->base_hash)
753 return 0;
754
755 rc = crypto_shash_setkey(ctx->fallback, key, keylen);
756 if (rc)
757 return rc;
758
759 /* Can't see a way to extract the ipad/opad from the fallback tfm
760 so I'm basically copying code from the hmac module */
761 bs = crypto_shash_blocksize(ctx->base_hash);
762 ds = crypto_shash_digestsize(ctx->base_hash);
763 ss = crypto_shash_statesize(ctx->base_hash);
764
765 {
766 struct {
767 struct shash_desc shash;
768 char ctx[crypto_shash_descsize(ctx->base_hash)];
769 } desc;
770 unsigned int i;
771 char ipad[ss];
772 char opad[ss];
773
774 desc.shash.tfm = ctx->base_hash;
775 desc.shash.flags = crypto_shash_get_flags(ctx->base_hash) &
776 CRYPTO_TFM_REQ_MAY_SLEEP;
777
778 if (keylen > bs) {
779 int err;
780
781 err =
782 crypto_shash_digest(&desc.shash, key, keylen, ipad);
783 if (err)
784 return err;
785
786 keylen = ds;
787 } else
788 memcpy(ipad, key, keylen);
789
790 memset(ipad + keylen, 0, bs - keylen);
791 memcpy(opad, ipad, bs);
792
793 for (i = 0; i < bs; i++) {
794 ipad[i] ^= 0x36;
795 opad[i] ^= 0x5c;
796 }
797
798 rc = crypto_shash_init(&desc.shash) ? :
799 crypto_shash_update(&desc.shash, ipad, bs) ? :
800 crypto_shash_export(&desc.shash, ipad) ? :
801 crypto_shash_init(&desc.shash) ? :
802 crypto_shash_update(&desc.shash, opad, bs) ? :
803 crypto_shash_export(&desc.shash, opad);
804
805 if (rc == 0)
806 mv_hash_init_ivs(ctx, ipad, opad);
807
808 return rc;
809 }
810}
811
812static int mv_cra_hash_init(struct crypto_tfm *tfm, const char *base_hash_name,
813 enum hash_op op, int count_add)
814{
815 const char *fallback_driver_name = tfm->__crt_alg->cra_name;
816 struct mv_tfm_hash_ctx *ctx = crypto_tfm_ctx(tfm);
817 struct crypto_shash *fallback_tfm = NULL;
818 struct crypto_shash *base_hash = NULL;
819 int err = -ENOMEM;
820
821 ctx->op = op;
822 ctx->count_add = count_add;
823
824 /* Allocate a fallback and abort if it failed. */
825 fallback_tfm = crypto_alloc_shash(fallback_driver_name, 0,
826 CRYPTO_ALG_NEED_FALLBACK);
827 if (IS_ERR(fallback_tfm)) {
828 printk(KERN_WARNING MV_CESA
829 "Fallback driver '%s' could not be loaded!\n",
830 fallback_driver_name);
831 err = PTR_ERR(fallback_tfm);
832 goto out;
833 }
834 ctx->fallback = fallback_tfm;
835
836 if (base_hash_name) {
837 /* Allocate a hash to compute the ipad/opad of hmac. */
838 base_hash = crypto_alloc_shash(base_hash_name, 0,
839 CRYPTO_ALG_NEED_FALLBACK);
840 if (IS_ERR(base_hash)) {
841 printk(KERN_WARNING MV_CESA
842 "Base driver '%s' could not be loaded!\n",
843 base_hash_name);
Roel Kluin41f29772011-01-04 15:37:16 +1100844 err = PTR_ERR(base_hash);
Uri Simchoni750052d2010-04-08 19:34:55 +0300845 goto err_bad_base;
846 }
847 }
848 ctx->base_hash = base_hash;
849
850 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
851 sizeof(struct mv_req_hash_ctx) +
852 crypto_shash_descsize(ctx->fallback));
853 return 0;
854err_bad_base:
855 crypto_free_shash(fallback_tfm);
856out:
857 return err;
858}
859
860static void mv_cra_hash_exit(struct crypto_tfm *tfm)
861{
862 struct mv_tfm_hash_ctx *ctx = crypto_tfm_ctx(tfm);
863
864 crypto_free_shash(ctx->fallback);
865 if (ctx->base_hash)
866 crypto_free_shash(ctx->base_hash);
867}
868
869static int mv_cra_hash_sha1_init(struct crypto_tfm *tfm)
870{
871 return mv_cra_hash_init(tfm, NULL, COP_SHA1, 0);
872}
873
874static int mv_cra_hash_hmac_sha1_init(struct crypto_tfm *tfm)
875{
876 return mv_cra_hash_init(tfm, "sha1", COP_HMAC_SHA1, SHA1_BLOCK_SIZE);
877}
878
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +1000879irqreturn_t crypto_int(int irq, void *priv)
880{
881 u32 val;
882
883 val = readl(cpg->reg + SEC_ACCEL_INT_STATUS);
884 if (!(val & SEC_INT_ACCEL0_DONE))
885 return IRQ_NONE;
886
887 val &= ~SEC_INT_ACCEL0_DONE;
888 writel(val, cpg->reg + FPGA_INT_STATUS);
889 writel(val, cpg->reg + SEC_ACCEL_INT_STATUS);
890 BUG_ON(cpg->eng_st != ENGINE_BUSY);
891 cpg->eng_st = ENGINE_W_DEQUEUE;
892 wake_up_process(cpg->queue_th);
893 return IRQ_HANDLED;
894}
895
896struct crypto_alg mv_aes_alg_ecb = {
897 .cra_name = "ecb(aes)",
898 .cra_driver_name = "mv-ecb-aes",
899 .cra_priority = 300,
900 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
901 .cra_blocksize = 16,
902 .cra_ctxsize = sizeof(struct mv_ctx),
903 .cra_alignmask = 0,
904 .cra_type = &crypto_ablkcipher_type,
905 .cra_module = THIS_MODULE,
906 .cra_init = mv_cra_init,
907 .cra_u = {
908 .ablkcipher = {
909 .min_keysize = AES_MIN_KEY_SIZE,
910 .max_keysize = AES_MAX_KEY_SIZE,
911 .setkey = mv_setkey_aes,
912 .encrypt = mv_enc_aes_ecb,
913 .decrypt = mv_dec_aes_ecb,
914 },
915 },
916};
917
918struct crypto_alg mv_aes_alg_cbc = {
919 .cra_name = "cbc(aes)",
920 .cra_driver_name = "mv-cbc-aes",
921 .cra_priority = 300,
922 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
923 .cra_blocksize = AES_BLOCK_SIZE,
924 .cra_ctxsize = sizeof(struct mv_ctx),
925 .cra_alignmask = 0,
926 .cra_type = &crypto_ablkcipher_type,
927 .cra_module = THIS_MODULE,
928 .cra_init = mv_cra_init,
929 .cra_u = {
930 .ablkcipher = {
931 .ivsize = AES_BLOCK_SIZE,
932 .min_keysize = AES_MIN_KEY_SIZE,
933 .max_keysize = AES_MAX_KEY_SIZE,
934 .setkey = mv_setkey_aes,
935 .encrypt = mv_enc_aes_cbc,
936 .decrypt = mv_dec_aes_cbc,
937 },
938 },
939};
940
Uri Simchoni750052d2010-04-08 19:34:55 +0300941struct ahash_alg mv_sha1_alg = {
942 .init = mv_hash_init,
943 .update = mv_hash_update,
944 .final = mv_hash_final,
945 .finup = mv_hash_finup,
946 .digest = mv_hash_digest,
947 .halg = {
948 .digestsize = SHA1_DIGEST_SIZE,
949 .base = {
950 .cra_name = "sha1",
951 .cra_driver_name = "mv-sha1",
952 .cra_priority = 300,
953 .cra_flags =
954 CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK,
955 .cra_blocksize = SHA1_BLOCK_SIZE,
956 .cra_ctxsize = sizeof(struct mv_tfm_hash_ctx),
957 .cra_init = mv_cra_hash_sha1_init,
958 .cra_exit = mv_cra_hash_exit,
959 .cra_module = THIS_MODULE,
960 }
961 }
962};
963
964struct ahash_alg mv_hmac_sha1_alg = {
965 .init = mv_hash_init,
966 .update = mv_hash_update,
967 .final = mv_hash_final,
968 .finup = mv_hash_finup,
969 .digest = mv_hash_digest,
970 .setkey = mv_hash_setkey,
971 .halg = {
972 .digestsize = SHA1_DIGEST_SIZE,
973 .base = {
974 .cra_name = "hmac(sha1)",
975 .cra_driver_name = "mv-hmac-sha1",
976 .cra_priority = 300,
977 .cra_flags =
978 CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK,
979 .cra_blocksize = SHA1_BLOCK_SIZE,
980 .cra_ctxsize = sizeof(struct mv_tfm_hash_ctx),
981 .cra_init = mv_cra_hash_hmac_sha1_init,
982 .cra_exit = mv_cra_hash_exit,
983 .cra_module = THIS_MODULE,
984 }
985 }
986};
987
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +1000988static int mv_probe(struct platform_device *pdev)
989{
990 struct crypto_priv *cp;
991 struct resource *res;
992 int irq;
993 int ret;
994
995 if (cpg) {
Uri Simchoni750052d2010-04-08 19:34:55 +0300996 printk(KERN_ERR MV_CESA "Second crypto dev?\n");
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +1000997 return -EEXIST;
998 }
999
1000 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
1001 if (!res)
1002 return -ENXIO;
1003
1004 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
1005 if (!cp)
1006 return -ENOMEM;
1007
1008 spin_lock_init(&cp->lock);
1009 crypto_init_queue(&cp->queue, 50);
Tobias Klauser5bdd5de2010-05-14 14:58:05 +10001010 cp->reg = ioremap(res->start, resource_size(res));
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +10001011 if (!cp->reg) {
1012 ret = -ENOMEM;
1013 goto err;
1014 }
1015
1016 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "sram");
1017 if (!res) {
1018 ret = -ENXIO;
1019 goto err_unmap_reg;
1020 }
Tobias Klauser5bdd5de2010-05-14 14:58:05 +10001021 cp->sram_size = resource_size(res);
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +10001022 cp->max_req_size = cp->sram_size - SRAM_CFG_SPACE;
1023 cp->sram = ioremap(res->start, cp->sram_size);
1024 if (!cp->sram) {
1025 ret = -ENOMEM;
1026 goto err_unmap_reg;
1027 }
1028
1029 irq = platform_get_irq(pdev, 0);
1030 if (irq < 0 || irq == NO_IRQ) {
1031 ret = irq;
1032 goto err_unmap_sram;
1033 }
1034 cp->irq = irq;
1035
1036 platform_set_drvdata(pdev, cp);
1037 cpg = cp;
1038
1039 cp->queue_th = kthread_run(queue_manag, cp, "mv_crypto");
1040 if (IS_ERR(cp->queue_th)) {
1041 ret = PTR_ERR(cp->queue_th);
Dan Carpenter7cc28352010-05-26 10:45:22 +10001042 goto err_unmap_sram;
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +10001043 }
1044
1045 ret = request_irq(irq, crypto_int, IRQF_DISABLED, dev_name(&pdev->dev),
1046 cp);
1047 if (ret)
Dan Carpenter7cc28352010-05-26 10:45:22 +10001048 goto err_thread;
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +10001049
1050 writel(SEC_INT_ACCEL0_DONE, cpg->reg + SEC_ACCEL_INT_MASK);
1051 writel(SEC_CFG_STOP_DIG_ERR, cpg->reg + SEC_ACCEL_CFG);
Phil Sutter99db3ea2011-05-05 15:28:58 +02001052 writel(SRAM_CONFIG, cpg->reg + SEC_ACCEL_DESC_P0);
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +10001053
1054 ret = crypto_register_alg(&mv_aes_alg_ecb);
Phil Sutter2a025f52011-05-05 15:29:00 +02001055 if (ret) {
1056 printk(KERN_WARNING MV_CESA
1057 "Could not register aes-ecb driver\n");
Dan Carpenter7cc28352010-05-26 10:45:22 +10001058 goto err_irq;
Phil Sutter2a025f52011-05-05 15:29:00 +02001059 }
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +10001060
1061 ret = crypto_register_alg(&mv_aes_alg_cbc);
Phil Sutter2a025f52011-05-05 15:29:00 +02001062 if (ret) {
1063 printk(KERN_WARNING MV_CESA
1064 "Could not register aes-cbc driver\n");
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +10001065 goto err_unreg_ecb;
Phil Sutter2a025f52011-05-05 15:29:00 +02001066 }
Uri Simchoni750052d2010-04-08 19:34:55 +03001067
1068 ret = crypto_register_ahash(&mv_sha1_alg);
1069 if (ret == 0)
1070 cpg->has_sha1 = 1;
1071 else
1072 printk(KERN_WARNING MV_CESA "Could not register sha1 driver\n");
1073
1074 ret = crypto_register_ahash(&mv_hmac_sha1_alg);
1075 if (ret == 0) {
1076 cpg->has_hmac_sha1 = 1;
1077 } else {
1078 printk(KERN_WARNING MV_CESA
1079 "Could not register hmac-sha1 driver\n");
1080 }
1081
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +10001082 return 0;
1083err_unreg_ecb:
1084 crypto_unregister_alg(&mv_aes_alg_ecb);
Dan Carpenter7cc28352010-05-26 10:45:22 +10001085err_irq:
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +10001086 free_irq(irq, cp);
Dan Carpenter7cc28352010-05-26 10:45:22 +10001087err_thread:
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +10001088 kthread_stop(cp->queue_th);
1089err_unmap_sram:
1090 iounmap(cp->sram);
1091err_unmap_reg:
1092 iounmap(cp->reg);
1093err:
1094 kfree(cp);
1095 cpg = NULL;
1096 platform_set_drvdata(pdev, NULL);
1097 return ret;
1098}
1099
1100static int mv_remove(struct platform_device *pdev)
1101{
1102 struct crypto_priv *cp = platform_get_drvdata(pdev);
1103
1104 crypto_unregister_alg(&mv_aes_alg_ecb);
1105 crypto_unregister_alg(&mv_aes_alg_cbc);
Uri Simchoni750052d2010-04-08 19:34:55 +03001106 if (cp->has_sha1)
1107 crypto_unregister_ahash(&mv_sha1_alg);
1108 if (cp->has_hmac_sha1)
1109 crypto_unregister_ahash(&mv_hmac_sha1_alg);
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +10001110 kthread_stop(cp->queue_th);
1111 free_irq(cp->irq, cp);
1112 memset(cp->sram, 0, cp->sram_size);
1113 iounmap(cp->sram);
1114 iounmap(cp->reg);
1115 kfree(cp);
1116 cpg = NULL;
1117 return 0;
1118}
1119
1120static struct platform_driver marvell_crypto = {
1121 .probe = mv_probe,
1122 .remove = mv_remove,
1123 .driver = {
1124 .owner = THIS_MODULE,
1125 .name = "mv_crypto",
1126 },
1127};
1128MODULE_ALIAS("platform:mv_crypto");
1129
Axel Lin741e8c22011-11-26 21:26:19 +08001130module_platform_driver(marvell_crypto);
Sebastian Andrzej Siewior85a7f0a2009-08-10 12:50:03 +10001131
1132MODULE_AUTHOR("Sebastian Andrzej Siewior <sebastian@breakpoint.cc>");
1133MODULE_DESCRIPTION("Support for Marvell's cryptographic engine");
1134MODULE_LICENSE("GPL");