blob: fe538e5287a55b8ba2b231629956e32826ed34a1 [file] [log] [blame]
Jordan Crouse9fe757b2006-10-04 18:48:57 +10001 /* Copyright (C) 2004-2006, Advanced Micro Devices, Inc.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 */
8
9#include <linux/module.h>
10#include <linux/kernel.h>
Jordan Crouse9fe757b2006-10-04 18:48:57 +100011#include <linux/pci.h>
12#include <linux/pci_ids.h>
13#include <linux/crypto.h>
14#include <linux/spinlock.h>
15#include <crypto/algapi.h>
Sebastian Siewior89e12652007-10-17 23:18:57 +080016#include <crypto/aes.h>
Jordan Crouse9fe757b2006-10-04 18:48:57 +100017
Chihau Chau99700712010-04-19 21:02:41 +080018#include <linux/io.h>
19#include <linux/delay.h>
Jordan Crouse9fe757b2006-10-04 18:48:57 +100020
21#include "geode-aes.h"
22
Jordan Crouse9fe757b2006-10-04 18:48:57 +100023/* Static structures */
24
Chihau Chau99700712010-04-19 21:02:41 +080025static void __iomem *_iobase;
Jordan Crouse9fe757b2006-10-04 18:48:57 +100026static spinlock_t lock;
27
28/* Write a 128 bit field (either a writable key or IV) */
29static inline void
30_writefield(u32 offset, void *value)
31{
32 int i;
Chihau Chau99700712010-04-19 21:02:41 +080033 for (i = 0; i < 4; i++)
Jordan Crouse9fe757b2006-10-04 18:48:57 +100034 iowrite32(((u32 *) value)[i], _iobase + offset + (i * 4));
35}
36
37/* Read a 128 bit field (either a writable key or IV) */
38static inline void
39_readfield(u32 offset, void *value)
40{
41 int i;
Chihau Chau99700712010-04-19 21:02:41 +080042 for (i = 0; i < 4; i++)
Jordan Crouse9fe757b2006-10-04 18:48:57 +100043 ((u32 *) value)[i] = ioread32(_iobase + offset + (i * 4));
44}
45
46static int
47do_crypt(void *src, void *dst, int len, u32 flags)
48{
49 u32 status;
50 u32 counter = AES_OP_TIMEOUT;
51
52 iowrite32(virt_to_phys(src), _iobase + AES_SOURCEA_REG);
53 iowrite32(virt_to_phys(dst), _iobase + AES_DSTA_REG);
54 iowrite32(len, _iobase + AES_LENA_REG);
55
56 /* Start the operation */
57 iowrite32(AES_CTRL_START | flags, _iobase + AES_CTRLA_REG);
58
Sebastian Siewior1f4e4772007-10-21 16:18:12 +080059 do {
Jordan Crouse9fe757b2006-10-04 18:48:57 +100060 status = ioread32(_iobase + AES_INTR_REG);
Sebastian Siewior1f4e4772007-10-21 16:18:12 +080061 cpu_relax();
Chihau Chau99700712010-04-19 21:02:41 +080062 } while (!(status & AES_INTRA_PENDING) && --counter);
Jordan Crouse9fe757b2006-10-04 18:48:57 +100063
64 /* Clear the event */
65 iowrite32((status & 0xFF) | AES_INTRA_PENDING, _iobase + AES_INTR_REG);
66 return counter ? 0 : 1;
67}
68
Adrian Bunkab782702006-11-17 13:43:55 +110069static unsigned int
Jordan Crouse9fe757b2006-10-04 18:48:57 +100070geode_aes_crypt(struct geode_aes_op *op)
71{
Jordan Crouse9fe757b2006-10-04 18:48:57 +100072 u32 flags = 0;
Alexey Dobriyan5efee172007-03-06 01:42:13 -080073 unsigned long iflags;
Sebastian Siewior1f4e4772007-10-21 16:18:12 +080074 int ret;
Jordan Crouse9fe757b2006-10-04 18:48:57 +100075
Jordan Crouse761e7842007-05-24 21:23:24 +100076 if (op->len == 0)
Jordan Crouse9fe757b2006-10-04 18:48:57 +100077 return 0;
78
Jordan Crouse761e7842007-05-24 21:23:24 +100079 /* If the source and destination is the same, then
80 * we need to turn on the coherent flags, otherwise
81 * we don't need to worry
82 */
83
Sebastian Siewior2e216302007-11-10 19:37:49 +080084 flags |= (AES_CTRL_DCA | AES_CTRL_SCA);
Jordan Crouse9fe757b2006-10-04 18:48:57 +100085
86 if (op->dir == AES_DIR_ENCRYPT)
87 flags |= AES_CTRL_ENCRYPT;
88
89 /* Start the critical section */
90
91 spin_lock_irqsave(&lock, iflags);
92
93 if (op->mode == AES_MODE_CBC) {
94 flags |= AES_CTRL_CBC;
95 _writefield(AES_WRITEIV0_REG, op->iv);
96 }
97
Jordan Crouse761e7842007-05-24 21:23:24 +100098 if (!(op->flags & AES_FLAGS_HIDDENKEY)) {
Jordan Crouse9fe757b2006-10-04 18:48:57 +100099 flags |= AES_CTRL_WRKEY;
100 _writefield(AES_WRITEKEY0_REG, op->key);
101 }
102
Sebastian Siewior1f4e4772007-10-21 16:18:12 +0800103 ret = do_crypt(op->src, op->dst, op->len, flags);
104 BUG_ON(ret);
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000105
106 if (op->mode == AES_MODE_CBC)
107 _readfield(AES_WRITEIV0_REG, op->iv);
108
109 spin_unlock_irqrestore(&lock, iflags);
110
111 return op->len;
112}
113
114/* CRYPTO-API Functions */
115
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800116static int geode_setkey_cip(struct crypto_tfm *tfm, const u8 *key,
117 unsigned int len)
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000118{
119 struct geode_aes_op *op = crypto_tfm_ctx(tfm);
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800120 unsigned int ret;
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000121
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800122 op->keylen = len;
123
124 if (len == AES_KEYSIZE_128) {
125 memcpy(op->key, key, len);
126 return 0;
127 }
128
129 if (len != AES_KEYSIZE_192 && len != AES_KEYSIZE_256) {
130 /* not supported at all */
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000131 tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
132 return -EINVAL;
133 }
134
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800135 /*
136 * The requested key size is not supported by HW, do a fallback
137 */
Roel Kluinfaad98f2010-01-08 14:19:21 +1100138 op->fallback.cip->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK;
139 op->fallback.cip->base.crt_flags |= (tfm->crt_flags & CRYPTO_TFM_REQ_MASK);
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800140
141 ret = crypto_cipher_setkey(op->fallback.cip, key, len);
142 if (ret) {
143 tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
Roel Kluine054f162010-02-04 11:39:13 +1100144 tfm->crt_flags |= (op->fallback.cip->base.crt_flags & CRYPTO_TFM_RES_MASK);
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800145 }
146 return ret;
147}
148
149static int geode_setkey_blk(struct crypto_tfm *tfm, const u8 *key,
150 unsigned int len)
151{
152 struct geode_aes_op *op = crypto_tfm_ctx(tfm);
153 unsigned int ret;
154
155 op->keylen = len;
156
157 if (len == AES_KEYSIZE_128) {
158 memcpy(op->key, key, len);
159 return 0;
160 }
161
162 if (len != AES_KEYSIZE_192 && len != AES_KEYSIZE_256) {
163 /* not supported at all */
164 tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
165 return -EINVAL;
166 }
167
168 /*
169 * The requested key size is not supported by HW, do a fallback
170 */
171 op->fallback.blk->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK;
172 op->fallback.blk->base.crt_flags |= (tfm->crt_flags & CRYPTO_TFM_REQ_MASK);
173
174 ret = crypto_blkcipher_setkey(op->fallback.blk, key, len);
175 if (ret) {
176 tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
177 tfm->crt_flags |= (op->fallback.blk->base.crt_flags & CRYPTO_TFM_RES_MASK);
178 }
179 return ret;
180}
181
182static int fallback_blk_dec(struct blkcipher_desc *desc,
183 struct scatterlist *dst, struct scatterlist *src,
184 unsigned int nbytes)
185{
186 unsigned int ret;
187 struct crypto_blkcipher *tfm;
188 struct geode_aes_op *op = crypto_blkcipher_ctx(desc->tfm);
189
190 tfm = desc->tfm;
191 desc->tfm = op->fallback.blk;
192
Sebastian Siewiorfdc520a2007-12-10 15:48:17 +0800193 ret = crypto_blkcipher_decrypt_iv(desc, dst, src, nbytes);
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800194
195 desc->tfm = tfm;
196 return ret;
197}
198static int fallback_blk_enc(struct blkcipher_desc *desc,
199 struct scatterlist *dst, struct scatterlist *src,
200 unsigned int nbytes)
201{
202 unsigned int ret;
203 struct crypto_blkcipher *tfm;
204 struct geode_aes_op *op = crypto_blkcipher_ctx(desc->tfm);
205
206 tfm = desc->tfm;
207 desc->tfm = op->fallback.blk;
208
Sebastian Siewiorfdc520a2007-12-10 15:48:17 +0800209 ret = crypto_blkcipher_encrypt_iv(desc, dst, src, nbytes);
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800210
211 desc->tfm = tfm;
212 return ret;
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000213}
214
215static void
216geode_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
217{
218 struct geode_aes_op *op = crypto_tfm_ctx(tfm);
219
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800220 if (unlikely(op->keylen != AES_KEYSIZE_128)) {
221 crypto_cipher_encrypt_one(op->fallback.cip, out, in);
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000222 return;
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800223 }
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000224
225 op->src = (void *) in;
226 op->dst = (void *) out;
227 op->mode = AES_MODE_ECB;
228 op->flags = 0;
Marek Vasutb9d865e2014-05-14 11:36:38 +0200229 op->len = AES_BLOCK_SIZE;
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000230 op->dir = AES_DIR_ENCRYPT;
231
232 geode_aes_crypt(op);
233}
234
235
236static void
237geode_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
238{
239 struct geode_aes_op *op = crypto_tfm_ctx(tfm);
240
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800241 if (unlikely(op->keylen != AES_KEYSIZE_128)) {
242 crypto_cipher_decrypt_one(op->fallback.cip, out, in);
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000243 return;
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800244 }
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000245
246 op->src = (void *) in;
247 op->dst = (void *) out;
248 op->mode = AES_MODE_ECB;
249 op->flags = 0;
Marek Vasutb9d865e2014-05-14 11:36:38 +0200250 op->len = AES_BLOCK_SIZE;
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000251 op->dir = AES_DIR_DECRYPT;
252
253 geode_aes_crypt(op);
254}
255
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800256static int fallback_init_cip(struct crypto_tfm *tfm)
257{
Marek Vasutd207a382014-05-14 11:40:57 +0200258 const char *name = crypto_tfm_alg_name(tfm);
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800259 struct geode_aes_op *op = crypto_tfm_ctx(tfm);
260
261 op->fallback.cip = crypto_alloc_cipher(name, 0,
262 CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK);
263
264 if (IS_ERR(op->fallback.cip)) {
265 printk(KERN_ERR "Error allocating fallback algo %s\n", name);
Roel Kluinfaad98f2010-01-08 14:19:21 +1100266 return PTR_ERR(op->fallback.cip);
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800267 }
268
269 return 0;
270}
271
272static void fallback_exit_cip(struct crypto_tfm *tfm)
273{
274 struct geode_aes_op *op = crypto_tfm_ctx(tfm);
275
276 crypto_free_cipher(op->fallback.cip);
277 op->fallback.cip = NULL;
278}
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000279
280static struct crypto_alg geode_alg = {
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800281 .cra_name = "aes",
282 .cra_driver_name = "geode-aes",
283 .cra_priority = 300,
284 .cra_alignmask = 15,
285 .cra_flags = CRYPTO_ALG_TYPE_CIPHER |
286 CRYPTO_ALG_NEED_FALLBACK,
287 .cra_init = fallback_init_cip,
288 .cra_exit = fallback_exit_cip,
Marek Vasutb9d865e2014-05-14 11:36:38 +0200289 .cra_blocksize = AES_BLOCK_SIZE,
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000290 .cra_ctxsize = sizeof(struct geode_aes_op),
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800291 .cra_module = THIS_MODULE,
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800292 .cra_u = {
293 .cipher = {
294 .cia_min_keysize = AES_MIN_KEY_SIZE,
295 .cia_max_keysize = AES_MAX_KEY_SIZE,
296 .cia_setkey = geode_setkey_cip,
297 .cia_encrypt = geode_encrypt,
298 .cia_decrypt = geode_decrypt
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000299 }
300 }
301};
302
303static int
304geode_cbc_decrypt(struct blkcipher_desc *desc,
305 struct scatterlist *dst, struct scatterlist *src,
306 unsigned int nbytes)
307{
308 struct geode_aes_op *op = crypto_blkcipher_ctx(desc->tfm);
309 struct blkcipher_walk walk;
310 int err, ret;
311
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800312 if (unlikely(op->keylen != AES_KEYSIZE_128))
313 return fallback_blk_dec(desc, dst, src, nbytes);
314
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000315 blkcipher_walk_init(&walk, dst, src, nbytes);
316 err = blkcipher_walk_virt(desc, &walk);
Sebastian Siewiord2456c62007-11-30 16:36:57 +1100317 op->iv = walk.iv;
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000318
Chihau Chau99700712010-04-19 21:02:41 +0800319 while ((nbytes = walk.nbytes)) {
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000320 op->src = walk.src.virt.addr,
321 op->dst = walk.dst.virt.addr;
322 op->mode = AES_MODE_CBC;
Marek Vasutb9d865e2014-05-14 11:36:38 +0200323 op->len = nbytes - (nbytes % AES_BLOCK_SIZE);
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000324 op->dir = AES_DIR_DECRYPT;
325
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000326 ret = geode_aes_crypt(op);
327
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000328 nbytes -= ret;
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000329 err = blkcipher_walk_done(desc, &walk, nbytes);
330 }
331
332 return err;
333}
334
335static int
336geode_cbc_encrypt(struct blkcipher_desc *desc,
337 struct scatterlist *dst, struct scatterlist *src,
338 unsigned int nbytes)
339{
340 struct geode_aes_op *op = crypto_blkcipher_ctx(desc->tfm);
341 struct blkcipher_walk walk;
342 int err, ret;
343
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800344 if (unlikely(op->keylen != AES_KEYSIZE_128))
345 return fallback_blk_enc(desc, dst, src, nbytes);
346
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000347 blkcipher_walk_init(&walk, dst, src, nbytes);
348 err = blkcipher_walk_virt(desc, &walk);
Sebastian Siewiord2456c62007-11-30 16:36:57 +1100349 op->iv = walk.iv;
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000350
Chihau Chau99700712010-04-19 21:02:41 +0800351 while ((nbytes = walk.nbytes)) {
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000352 op->src = walk.src.virt.addr,
353 op->dst = walk.dst.virt.addr;
354 op->mode = AES_MODE_CBC;
Marek Vasutb9d865e2014-05-14 11:36:38 +0200355 op->len = nbytes - (nbytes % AES_BLOCK_SIZE);
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000356 op->dir = AES_DIR_ENCRYPT;
357
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000358 ret = geode_aes_crypt(op);
359 nbytes -= ret;
360 err = blkcipher_walk_done(desc, &walk, nbytes);
361 }
362
363 return err;
364}
365
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800366static int fallback_init_blk(struct crypto_tfm *tfm)
367{
Marek Vasutd207a382014-05-14 11:40:57 +0200368 const char *name = crypto_tfm_alg_name(tfm);
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800369 struct geode_aes_op *op = crypto_tfm_ctx(tfm);
370
371 op->fallback.blk = crypto_alloc_blkcipher(name, 0,
372 CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK);
373
374 if (IS_ERR(op->fallback.blk)) {
375 printk(KERN_ERR "Error allocating fallback algo %s\n", name);
376 return PTR_ERR(op->fallback.blk);
377 }
378
379 return 0;
380}
381
382static void fallback_exit_blk(struct crypto_tfm *tfm)
383{
384 struct geode_aes_op *op = crypto_tfm_ctx(tfm);
385
386 crypto_free_blkcipher(op->fallback.blk);
387 op->fallback.blk = NULL;
388}
389
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000390static struct crypto_alg geode_cbc_alg = {
391 .cra_name = "cbc(aes)",
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800392 .cra_driver_name = "cbc-aes-geode",
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000393 .cra_priority = 400,
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800394 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
Nikos Mavrogiannopoulosd912bb72011-11-01 13:39:56 +0100395 CRYPTO_ALG_KERN_DRIVER_ONLY |
396 CRYPTO_ALG_NEED_FALLBACK,
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800397 .cra_init = fallback_init_blk,
398 .cra_exit = fallback_exit_blk,
Marek Vasutb9d865e2014-05-14 11:36:38 +0200399 .cra_blocksize = AES_BLOCK_SIZE,
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000400 .cra_ctxsize = sizeof(struct geode_aes_op),
401 .cra_alignmask = 15,
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800402 .cra_type = &crypto_blkcipher_type,
403 .cra_module = THIS_MODULE,
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800404 .cra_u = {
405 .blkcipher = {
406 .min_keysize = AES_MIN_KEY_SIZE,
407 .max_keysize = AES_MAX_KEY_SIZE,
408 .setkey = geode_setkey_blk,
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000409 .encrypt = geode_cbc_encrypt,
410 .decrypt = geode_cbc_decrypt,
Marek Vasutbac79a22014-05-14 11:36:39 +0200411 .ivsize = AES_BLOCK_SIZE,
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000412 }
413 }
414};
415
416static int
417geode_ecb_decrypt(struct blkcipher_desc *desc,
418 struct scatterlist *dst, struct scatterlist *src,
419 unsigned int nbytes)
420{
421 struct geode_aes_op *op = crypto_blkcipher_ctx(desc->tfm);
422 struct blkcipher_walk walk;
423 int err, ret;
424
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800425 if (unlikely(op->keylen != AES_KEYSIZE_128))
426 return fallback_blk_dec(desc, dst, src, nbytes);
427
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000428 blkcipher_walk_init(&walk, dst, src, nbytes);
429 err = blkcipher_walk_virt(desc, &walk);
430
Chihau Chau99700712010-04-19 21:02:41 +0800431 while ((nbytes = walk.nbytes)) {
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000432 op->src = walk.src.virt.addr,
433 op->dst = walk.dst.virt.addr;
434 op->mode = AES_MODE_ECB;
Marek Vasutb9d865e2014-05-14 11:36:38 +0200435 op->len = nbytes - (nbytes % AES_BLOCK_SIZE);
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000436 op->dir = AES_DIR_DECRYPT;
437
438 ret = geode_aes_crypt(op);
439 nbytes -= ret;
440 err = blkcipher_walk_done(desc, &walk, nbytes);
441 }
442
443 return err;
444}
445
446static int
447geode_ecb_encrypt(struct blkcipher_desc *desc,
448 struct scatterlist *dst, struct scatterlist *src,
449 unsigned int nbytes)
450{
451 struct geode_aes_op *op = crypto_blkcipher_ctx(desc->tfm);
452 struct blkcipher_walk walk;
453 int err, ret;
454
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800455 if (unlikely(op->keylen != AES_KEYSIZE_128))
456 return fallback_blk_enc(desc, dst, src, nbytes);
457
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000458 blkcipher_walk_init(&walk, dst, src, nbytes);
459 err = blkcipher_walk_virt(desc, &walk);
460
Chihau Chau99700712010-04-19 21:02:41 +0800461 while ((nbytes = walk.nbytes)) {
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000462 op->src = walk.src.virt.addr,
463 op->dst = walk.dst.virt.addr;
464 op->mode = AES_MODE_ECB;
Marek Vasutb9d865e2014-05-14 11:36:38 +0200465 op->len = nbytes - (nbytes % AES_BLOCK_SIZE);
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000466 op->dir = AES_DIR_ENCRYPT;
467
468 ret = geode_aes_crypt(op);
469 nbytes -= ret;
470 ret = blkcipher_walk_done(desc, &walk, nbytes);
471 }
472
473 return err;
474}
475
476static struct crypto_alg geode_ecb_alg = {
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800477 .cra_name = "ecb(aes)",
478 .cra_driver_name = "ecb-aes-geode",
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000479 .cra_priority = 400,
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800480 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
Nikos Mavrogiannopoulosd912bb72011-11-01 13:39:56 +0100481 CRYPTO_ALG_KERN_DRIVER_ONLY |
482 CRYPTO_ALG_NEED_FALLBACK,
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800483 .cra_init = fallback_init_blk,
484 .cra_exit = fallback_exit_blk,
Marek Vasutb9d865e2014-05-14 11:36:38 +0200485 .cra_blocksize = AES_BLOCK_SIZE,
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000486 .cra_ctxsize = sizeof(struct geode_aes_op),
487 .cra_alignmask = 15,
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800488 .cra_type = &crypto_blkcipher_type,
489 .cra_module = THIS_MODULE,
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800490 .cra_u = {
491 .blkcipher = {
492 .min_keysize = AES_MIN_KEY_SIZE,
493 .max_keysize = AES_MAX_KEY_SIZE,
494 .setkey = geode_setkey_blk,
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000495 .encrypt = geode_ecb_encrypt,
496 .decrypt = geode_ecb_decrypt,
497 }
498 }
499};
500
Greg Kroah-Hartman49cfe4d2012-12-21 13:14:09 -0800501static void geode_aes_remove(struct pci_dev *dev)
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000502{
503 crypto_unregister_alg(&geode_alg);
504 crypto_unregister_alg(&geode_ecb_alg);
505 crypto_unregister_alg(&geode_cbc_alg);
506
507 pci_iounmap(dev, _iobase);
508 _iobase = NULL;
509
510 pci_release_regions(dev);
511 pci_disable_device(dev);
512}
513
514
Greg Kroah-Hartman49cfe4d2012-12-21 13:14:09 -0800515static int geode_aes_probe(struct pci_dev *dev, const struct pci_device_id *id)
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000516{
517 int ret;
Chihau Chau99700712010-04-19 21:02:41 +0800518 ret = pci_enable_device(dev);
519 if (ret)
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000520 return ret;
521
Chihau Chau99700712010-04-19 21:02:41 +0800522 ret = pci_request_regions(dev, "geode-aes");
523 if (ret)
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000524 goto eenable;
525
526 _iobase = pci_iomap(dev, 0, 0);
527
528 if (_iobase == NULL) {
529 ret = -ENOMEM;
530 goto erequest;
531 }
532
533 spin_lock_init(&lock);
534
535 /* Clear any pending activity */
536 iowrite32(AES_INTR_PENDING | AES_INTR_MASK, _iobase + AES_INTR_REG);
537
Chihau Chau99700712010-04-19 21:02:41 +0800538 ret = crypto_register_alg(&geode_alg);
539 if (ret)
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000540 goto eiomap;
541
Chihau Chau99700712010-04-19 21:02:41 +0800542 ret = crypto_register_alg(&geode_ecb_alg);
543 if (ret)
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000544 goto ealg;
545
Chihau Chau99700712010-04-19 21:02:41 +0800546 ret = crypto_register_alg(&geode_cbc_alg);
547 if (ret)
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000548 goto eecb;
549
Marek Vasut701bcbc2014-05-14 11:36:41 +0200550 dev_notice(&dev->dev, "GEODE AES engine enabled.\n");
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000551 return 0;
552
553 eecb:
554 crypto_unregister_alg(&geode_ecb_alg);
555
556 ealg:
557 crypto_unregister_alg(&geode_alg);
558
559 eiomap:
560 pci_iounmap(dev, _iobase);
561
562 erequest:
563 pci_release_regions(dev);
564
565 eenable:
566 pci_disable_device(dev);
567
Marek Vasut701bcbc2014-05-14 11:36:41 +0200568 dev_err(&dev->dev, "GEODE AES initialization failed.\n");
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000569 return ret;
570}
571
572static struct pci_device_id geode_aes_tbl[] = {
Peter Huewe1fb1def2010-07-15 21:00:41 +0200573 { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_LX_AES), } ,
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000574 { 0, }
575};
576
577MODULE_DEVICE_TABLE(pci, geode_aes_tbl);
578
579static struct pci_driver geode_aes_driver = {
580 .name = "Geode LX AES",
581 .id_table = geode_aes_tbl,
582 .probe = geode_aes_probe,
Greg Kroah-Hartman49cfe4d2012-12-21 13:14:09 -0800583 .remove = geode_aes_remove,
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000584};
585
Sachin Kamat49d30d32012-08-27 15:45:31 +0530586module_pci_driver(geode_aes_driver);
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000587
588MODULE_AUTHOR("Advanced Micro Devices, Inc.");
589MODULE_DESCRIPTION("Geode LX Hardware AES driver");
590MODULE_LICENSE("GPL");