blob: d81a1297cb9e5f75693a15ad3d50d7439ead3de1 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Jordan Crouse9fe757b2006-10-04 18:48:57 +10002 /* Copyright (C) 2004-2006, Advanced Micro Devices, Inc.
Chris Gorman24086e32017-07-06 14:44:56 -04003 */
Jordan Crouse9fe757b2006-10-04 18:48:57 +10004
5#include <linux/module.h>
6#include <linux/kernel.h>
Jordan Crouse9fe757b2006-10-04 18:48:57 +10007#include <linux/pci.h>
8#include <linux/pci_ids.h>
9#include <linux/crypto.h>
10#include <linux/spinlock.h>
11#include <crypto/algapi.h>
Sebastian Siewior89e12652007-10-17 23:18:57 +080012#include <crypto/aes.h>
Jordan Crouse9fe757b2006-10-04 18:48:57 +100013
Chihau Chau99700712010-04-19 21:02:41 +080014#include <linux/io.h>
15#include <linux/delay.h>
Jordan Crouse9fe757b2006-10-04 18:48:57 +100016
17#include "geode-aes.h"
18
Jordan Crouse9fe757b2006-10-04 18:48:57 +100019/* Static structures */
20
Chihau Chau99700712010-04-19 21:02:41 +080021static void __iomem *_iobase;
Jordan Crouse9fe757b2006-10-04 18:48:57 +100022static spinlock_t lock;
23
24/* Write a 128 bit field (either a writable key or IV) */
25static inline void
26_writefield(u32 offset, void *value)
27{
28 int i;
Chris Gorman24086e32017-07-06 14:44:56 -040029
Chihau Chau99700712010-04-19 21:02:41 +080030 for (i = 0; i < 4; i++)
Jordan Crouse9fe757b2006-10-04 18:48:57 +100031 iowrite32(((u32 *) value)[i], _iobase + offset + (i * 4));
32}
33
34/* Read a 128 bit field (either a writable key or IV) */
35static inline void
36_readfield(u32 offset, void *value)
37{
38 int i;
Chris Gorman24086e32017-07-06 14:44:56 -040039
Chihau Chau99700712010-04-19 21:02:41 +080040 for (i = 0; i < 4; i++)
Jordan Crouse9fe757b2006-10-04 18:48:57 +100041 ((u32 *) value)[i] = ioread32(_iobase + offset + (i * 4));
42}
43
44static int
45do_crypt(void *src, void *dst, int len, u32 flags)
46{
47 u32 status;
48 u32 counter = AES_OP_TIMEOUT;
49
50 iowrite32(virt_to_phys(src), _iobase + AES_SOURCEA_REG);
51 iowrite32(virt_to_phys(dst), _iobase + AES_DSTA_REG);
52 iowrite32(len, _iobase + AES_LENA_REG);
53
54 /* Start the operation */
55 iowrite32(AES_CTRL_START | flags, _iobase + AES_CTRLA_REG);
56
Sebastian Siewior1f4e4772007-10-21 16:18:12 +080057 do {
Jordan Crouse9fe757b2006-10-04 18:48:57 +100058 status = ioread32(_iobase + AES_INTR_REG);
Sebastian Siewior1f4e4772007-10-21 16:18:12 +080059 cpu_relax();
Chihau Chau99700712010-04-19 21:02:41 +080060 } while (!(status & AES_INTRA_PENDING) && --counter);
Jordan Crouse9fe757b2006-10-04 18:48:57 +100061
62 /* Clear the event */
63 iowrite32((status & 0xFF) | AES_INTRA_PENDING, _iobase + AES_INTR_REG);
64 return counter ? 0 : 1;
65}
66
Adrian Bunkab782702006-11-17 13:43:55 +110067static unsigned int
Jordan Crouse9fe757b2006-10-04 18:48:57 +100068geode_aes_crypt(struct geode_aes_op *op)
69{
Jordan Crouse9fe757b2006-10-04 18:48:57 +100070 u32 flags = 0;
Alexey Dobriyan5efee172007-03-06 01:42:13 -080071 unsigned long iflags;
Sebastian Siewior1f4e4772007-10-21 16:18:12 +080072 int ret;
Jordan Crouse9fe757b2006-10-04 18:48:57 +100073
Jordan Crouse761e7842007-05-24 21:23:24 +100074 if (op->len == 0)
Jordan Crouse9fe757b2006-10-04 18:48:57 +100075 return 0;
76
Jordan Crouse761e7842007-05-24 21:23:24 +100077 /* If the source and destination is the same, then
78 * we need to turn on the coherent flags, otherwise
79 * we don't need to worry
80 */
81
Sebastian Siewior2e216302007-11-10 19:37:49 +080082 flags |= (AES_CTRL_DCA | AES_CTRL_SCA);
Jordan Crouse9fe757b2006-10-04 18:48:57 +100083
84 if (op->dir == AES_DIR_ENCRYPT)
85 flags |= AES_CTRL_ENCRYPT;
86
87 /* Start the critical section */
88
89 spin_lock_irqsave(&lock, iflags);
90
91 if (op->mode == AES_MODE_CBC) {
92 flags |= AES_CTRL_CBC;
93 _writefield(AES_WRITEIV0_REG, op->iv);
94 }
95
Jordan Crouse761e7842007-05-24 21:23:24 +100096 if (!(op->flags & AES_FLAGS_HIDDENKEY)) {
Jordan Crouse9fe757b2006-10-04 18:48:57 +100097 flags |= AES_CTRL_WRKEY;
98 _writefield(AES_WRITEKEY0_REG, op->key);
99 }
100
Sebastian Siewior1f4e4772007-10-21 16:18:12 +0800101 ret = do_crypt(op->src, op->dst, op->len, flags);
102 BUG_ON(ret);
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000103
104 if (op->mode == AES_MODE_CBC)
105 _readfield(AES_WRITEIV0_REG, op->iv);
106
107 spin_unlock_irqrestore(&lock, iflags);
108
109 return op->len;
110}
111
112/* CRYPTO-API Functions */
113
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800114static int geode_setkey_cip(struct crypto_tfm *tfm, const u8 *key,
115 unsigned int len)
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000116{
117 struct geode_aes_op *op = crypto_tfm_ctx(tfm);
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800118 unsigned int ret;
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000119
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800120 op->keylen = len;
121
122 if (len == AES_KEYSIZE_128) {
123 memcpy(op->key, key, len);
124 return 0;
125 }
126
127 if (len != AES_KEYSIZE_192 && len != AES_KEYSIZE_256) {
128 /* not supported at all */
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000129 tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
130 return -EINVAL;
131 }
132
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800133 /*
134 * The requested key size is not supported by HW, do a fallback
135 */
Roel Kluinfaad98f2010-01-08 14:19:21 +1100136 op->fallback.cip->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK;
137 op->fallback.cip->base.crt_flags |= (tfm->crt_flags & CRYPTO_TFM_REQ_MASK);
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800138
139 ret = crypto_cipher_setkey(op->fallback.cip, key, len);
140 if (ret) {
141 tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
Roel Kluine054f162010-02-04 11:39:13 +1100142 tfm->crt_flags |= (op->fallback.cip->base.crt_flags & CRYPTO_TFM_RES_MASK);
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800143 }
144 return ret;
145}
146
147static int geode_setkey_blk(struct crypto_tfm *tfm, const u8 *key,
148 unsigned int len)
149{
150 struct geode_aes_op *op = crypto_tfm_ctx(tfm);
151 unsigned int ret;
152
153 op->keylen = len;
154
155 if (len == AES_KEYSIZE_128) {
156 memcpy(op->key, key, len);
157 return 0;
158 }
159
160 if (len != AES_KEYSIZE_192 && len != AES_KEYSIZE_256) {
161 /* not supported at all */
162 tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
163 return -EINVAL;
164 }
165
166 /*
167 * The requested key size is not supported by HW, do a fallback
168 */
169 op->fallback.blk->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK;
170 op->fallback.blk->base.crt_flags |= (tfm->crt_flags & CRYPTO_TFM_REQ_MASK);
171
172 ret = crypto_blkcipher_setkey(op->fallback.blk, key, len);
173 if (ret) {
174 tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
175 tfm->crt_flags |= (op->fallback.blk->base.crt_flags & CRYPTO_TFM_RES_MASK);
176 }
177 return ret;
178}
179
180static int fallback_blk_dec(struct blkcipher_desc *desc,
181 struct scatterlist *dst, struct scatterlist *src,
182 unsigned int nbytes)
183{
184 unsigned int ret;
185 struct crypto_blkcipher *tfm;
186 struct geode_aes_op *op = crypto_blkcipher_ctx(desc->tfm);
187
188 tfm = desc->tfm;
189 desc->tfm = op->fallback.blk;
190
Sebastian Siewiorfdc520a2007-12-10 15:48:17 +0800191 ret = crypto_blkcipher_decrypt_iv(desc, dst, src, nbytes);
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800192
193 desc->tfm = tfm;
194 return ret;
195}
196static int fallback_blk_enc(struct blkcipher_desc *desc,
197 struct scatterlist *dst, struct scatterlist *src,
198 unsigned int nbytes)
199{
200 unsigned int ret;
201 struct crypto_blkcipher *tfm;
202 struct geode_aes_op *op = crypto_blkcipher_ctx(desc->tfm);
203
204 tfm = desc->tfm;
205 desc->tfm = op->fallback.blk;
206
Sebastian Siewiorfdc520a2007-12-10 15:48:17 +0800207 ret = crypto_blkcipher_encrypt_iv(desc, dst, src, nbytes);
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800208
209 desc->tfm = tfm;
210 return ret;
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000211}
212
213static void
214geode_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
215{
216 struct geode_aes_op *op = crypto_tfm_ctx(tfm);
217
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800218 if (unlikely(op->keylen != AES_KEYSIZE_128)) {
219 crypto_cipher_encrypt_one(op->fallback.cip, out, in);
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000220 return;
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800221 }
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000222
223 op->src = (void *) in;
224 op->dst = (void *) out;
225 op->mode = AES_MODE_ECB;
226 op->flags = 0;
Marek Vasutb9d865e2014-05-14 11:36:38 +0200227 op->len = AES_BLOCK_SIZE;
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000228 op->dir = AES_DIR_ENCRYPT;
229
230 geode_aes_crypt(op);
231}
232
233
234static void
235geode_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
236{
237 struct geode_aes_op *op = crypto_tfm_ctx(tfm);
238
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800239 if (unlikely(op->keylen != AES_KEYSIZE_128)) {
240 crypto_cipher_decrypt_one(op->fallback.cip, out, in);
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000241 return;
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800242 }
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000243
244 op->src = (void *) in;
245 op->dst = (void *) out;
246 op->mode = AES_MODE_ECB;
247 op->flags = 0;
Marek Vasutb9d865e2014-05-14 11:36:38 +0200248 op->len = AES_BLOCK_SIZE;
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000249 op->dir = AES_DIR_DECRYPT;
250
251 geode_aes_crypt(op);
252}
253
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800254static int fallback_init_cip(struct crypto_tfm *tfm)
255{
Marek Vasutd207a382014-05-14 11:40:57 +0200256 const char *name = crypto_tfm_alg_name(tfm);
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800257 struct geode_aes_op *op = crypto_tfm_ctx(tfm);
258
259 op->fallback.cip = crypto_alloc_cipher(name, 0,
Eric Biggers1ad0f162018-11-14 12:19:39 -0800260 CRYPTO_ALG_NEED_FALLBACK);
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800261
262 if (IS_ERR(op->fallback.cip)) {
263 printk(KERN_ERR "Error allocating fallback algo %s\n", name);
Roel Kluinfaad98f2010-01-08 14:19:21 +1100264 return PTR_ERR(op->fallback.cip);
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800265 }
266
267 return 0;
268}
269
270static void fallback_exit_cip(struct crypto_tfm *tfm)
271{
272 struct geode_aes_op *op = crypto_tfm_ctx(tfm);
273
274 crypto_free_cipher(op->fallback.cip);
275 op->fallback.cip = NULL;
276}
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000277
278static struct crypto_alg geode_alg = {
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800279 .cra_name = "aes",
280 .cra_driver_name = "geode-aes",
281 .cra_priority = 300,
282 .cra_alignmask = 15,
283 .cra_flags = CRYPTO_ALG_TYPE_CIPHER |
284 CRYPTO_ALG_NEED_FALLBACK,
285 .cra_init = fallback_init_cip,
286 .cra_exit = fallback_exit_cip,
Marek Vasutb9d865e2014-05-14 11:36:38 +0200287 .cra_blocksize = AES_BLOCK_SIZE,
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000288 .cra_ctxsize = sizeof(struct geode_aes_op),
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800289 .cra_module = THIS_MODULE,
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800290 .cra_u = {
291 .cipher = {
292 .cia_min_keysize = AES_MIN_KEY_SIZE,
293 .cia_max_keysize = AES_MAX_KEY_SIZE,
294 .cia_setkey = geode_setkey_cip,
295 .cia_encrypt = geode_encrypt,
296 .cia_decrypt = geode_decrypt
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000297 }
298 }
299};
300
301static int
302geode_cbc_decrypt(struct blkcipher_desc *desc,
303 struct scatterlist *dst, struct scatterlist *src,
304 unsigned int nbytes)
305{
306 struct geode_aes_op *op = crypto_blkcipher_ctx(desc->tfm);
307 struct blkcipher_walk walk;
308 int err, ret;
309
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800310 if (unlikely(op->keylen != AES_KEYSIZE_128))
311 return fallback_blk_dec(desc, dst, src, nbytes);
312
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000313 blkcipher_walk_init(&walk, dst, src, nbytes);
314 err = blkcipher_walk_virt(desc, &walk);
Sebastian Siewiord2456c62007-11-30 16:36:57 +1100315 op->iv = walk.iv;
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000316
Chihau Chau99700712010-04-19 21:02:41 +0800317 while ((nbytes = walk.nbytes)) {
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000318 op->src = walk.src.virt.addr,
319 op->dst = walk.dst.virt.addr;
320 op->mode = AES_MODE_CBC;
Marek Vasutb9d865e2014-05-14 11:36:38 +0200321 op->len = nbytes - (nbytes % AES_BLOCK_SIZE);
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000322 op->dir = AES_DIR_DECRYPT;
323
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000324 ret = geode_aes_crypt(op);
325
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000326 nbytes -= ret;
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000327 err = blkcipher_walk_done(desc, &walk, nbytes);
328 }
329
330 return err;
331}
332
333static int
334geode_cbc_encrypt(struct blkcipher_desc *desc,
335 struct scatterlist *dst, struct scatterlist *src,
336 unsigned int nbytes)
337{
338 struct geode_aes_op *op = crypto_blkcipher_ctx(desc->tfm);
339 struct blkcipher_walk walk;
340 int err, ret;
341
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800342 if (unlikely(op->keylen != AES_KEYSIZE_128))
343 return fallback_blk_enc(desc, dst, src, nbytes);
344
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000345 blkcipher_walk_init(&walk, dst, src, nbytes);
346 err = blkcipher_walk_virt(desc, &walk);
Sebastian Siewiord2456c62007-11-30 16:36:57 +1100347 op->iv = walk.iv;
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000348
Chihau Chau99700712010-04-19 21:02:41 +0800349 while ((nbytes = walk.nbytes)) {
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000350 op->src = walk.src.virt.addr,
351 op->dst = walk.dst.virt.addr;
352 op->mode = AES_MODE_CBC;
Marek Vasutb9d865e2014-05-14 11:36:38 +0200353 op->len = nbytes - (nbytes % AES_BLOCK_SIZE);
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000354 op->dir = AES_DIR_ENCRYPT;
355
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000356 ret = geode_aes_crypt(op);
357 nbytes -= ret;
358 err = blkcipher_walk_done(desc, &walk, nbytes);
359 }
360
361 return err;
362}
363
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800364static int fallback_init_blk(struct crypto_tfm *tfm)
365{
Marek Vasutd207a382014-05-14 11:40:57 +0200366 const char *name = crypto_tfm_alg_name(tfm);
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800367 struct geode_aes_op *op = crypto_tfm_ctx(tfm);
368
369 op->fallback.blk = crypto_alloc_blkcipher(name, 0,
370 CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK);
371
372 if (IS_ERR(op->fallback.blk)) {
373 printk(KERN_ERR "Error allocating fallback algo %s\n", name);
374 return PTR_ERR(op->fallback.blk);
375 }
376
377 return 0;
378}
379
380static void fallback_exit_blk(struct crypto_tfm *tfm)
381{
382 struct geode_aes_op *op = crypto_tfm_ctx(tfm);
383
384 crypto_free_blkcipher(op->fallback.blk);
385 op->fallback.blk = NULL;
386}
387
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000388static struct crypto_alg geode_cbc_alg = {
389 .cra_name = "cbc(aes)",
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800390 .cra_driver_name = "cbc-aes-geode",
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000391 .cra_priority = 400,
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800392 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
Nikos Mavrogiannopoulosd912bb72011-11-01 13:39:56 +0100393 CRYPTO_ALG_KERN_DRIVER_ONLY |
394 CRYPTO_ALG_NEED_FALLBACK,
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800395 .cra_init = fallback_init_blk,
396 .cra_exit = fallback_exit_blk,
Marek Vasutb9d865e2014-05-14 11:36:38 +0200397 .cra_blocksize = AES_BLOCK_SIZE,
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000398 .cra_ctxsize = sizeof(struct geode_aes_op),
399 .cra_alignmask = 15,
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800400 .cra_type = &crypto_blkcipher_type,
401 .cra_module = THIS_MODULE,
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800402 .cra_u = {
403 .blkcipher = {
404 .min_keysize = AES_MIN_KEY_SIZE,
405 .max_keysize = AES_MAX_KEY_SIZE,
406 .setkey = geode_setkey_blk,
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000407 .encrypt = geode_cbc_encrypt,
408 .decrypt = geode_cbc_decrypt,
Marek Vasutbac79a22014-05-14 11:36:39 +0200409 .ivsize = AES_BLOCK_SIZE,
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000410 }
411 }
412};
413
414static int
415geode_ecb_decrypt(struct blkcipher_desc *desc,
416 struct scatterlist *dst, struct scatterlist *src,
417 unsigned int nbytes)
418{
419 struct geode_aes_op *op = crypto_blkcipher_ctx(desc->tfm);
420 struct blkcipher_walk walk;
421 int err, ret;
422
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800423 if (unlikely(op->keylen != AES_KEYSIZE_128))
424 return fallback_blk_dec(desc, dst, src, nbytes);
425
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000426 blkcipher_walk_init(&walk, dst, src, nbytes);
427 err = blkcipher_walk_virt(desc, &walk);
428
Chihau Chau99700712010-04-19 21:02:41 +0800429 while ((nbytes = walk.nbytes)) {
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000430 op->src = walk.src.virt.addr,
431 op->dst = walk.dst.virt.addr;
432 op->mode = AES_MODE_ECB;
Marek Vasutb9d865e2014-05-14 11:36:38 +0200433 op->len = nbytes - (nbytes % AES_BLOCK_SIZE);
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000434 op->dir = AES_DIR_DECRYPT;
435
436 ret = geode_aes_crypt(op);
437 nbytes -= ret;
438 err = blkcipher_walk_done(desc, &walk, nbytes);
439 }
440
441 return err;
442}
443
444static int
445geode_ecb_encrypt(struct blkcipher_desc *desc,
446 struct scatterlist *dst, struct scatterlist *src,
447 unsigned int nbytes)
448{
449 struct geode_aes_op *op = crypto_blkcipher_ctx(desc->tfm);
450 struct blkcipher_walk walk;
451 int err, ret;
452
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800453 if (unlikely(op->keylen != AES_KEYSIZE_128))
454 return fallback_blk_enc(desc, dst, src, nbytes);
455
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000456 blkcipher_walk_init(&walk, dst, src, nbytes);
457 err = blkcipher_walk_virt(desc, &walk);
458
Chihau Chau99700712010-04-19 21:02:41 +0800459 while ((nbytes = walk.nbytes)) {
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000460 op->src = walk.src.virt.addr,
461 op->dst = walk.dst.virt.addr;
462 op->mode = AES_MODE_ECB;
Marek Vasutb9d865e2014-05-14 11:36:38 +0200463 op->len = nbytes - (nbytes % AES_BLOCK_SIZE);
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000464 op->dir = AES_DIR_ENCRYPT;
465
466 ret = geode_aes_crypt(op);
467 nbytes -= ret;
468 ret = blkcipher_walk_done(desc, &walk, nbytes);
469 }
470
471 return err;
472}
473
474static struct crypto_alg geode_ecb_alg = {
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800475 .cra_name = "ecb(aes)",
476 .cra_driver_name = "ecb-aes-geode",
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000477 .cra_priority = 400,
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800478 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
Nikos Mavrogiannopoulosd912bb72011-11-01 13:39:56 +0100479 CRYPTO_ALG_KERN_DRIVER_ONLY |
480 CRYPTO_ALG_NEED_FALLBACK,
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800481 .cra_init = fallback_init_blk,
482 .cra_exit = fallback_exit_blk,
Marek Vasutb9d865e2014-05-14 11:36:38 +0200483 .cra_blocksize = AES_BLOCK_SIZE,
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000484 .cra_ctxsize = sizeof(struct geode_aes_op),
485 .cra_alignmask = 15,
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800486 .cra_type = &crypto_blkcipher_type,
487 .cra_module = THIS_MODULE,
Sebastian Siewiorcd7c3bf2007-11-10 19:29:33 +0800488 .cra_u = {
489 .blkcipher = {
490 .min_keysize = AES_MIN_KEY_SIZE,
491 .max_keysize = AES_MAX_KEY_SIZE,
492 .setkey = geode_setkey_blk,
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000493 .encrypt = geode_ecb_encrypt,
494 .decrypt = geode_ecb_decrypt,
495 }
496 }
497};
498
Greg Kroah-Hartman49cfe4d2012-12-21 13:14:09 -0800499static void geode_aes_remove(struct pci_dev *dev)
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000500{
501 crypto_unregister_alg(&geode_alg);
502 crypto_unregister_alg(&geode_ecb_alg);
503 crypto_unregister_alg(&geode_cbc_alg);
504
505 pci_iounmap(dev, _iobase);
506 _iobase = NULL;
507
508 pci_release_regions(dev);
509 pci_disable_device(dev);
510}
511
512
Greg Kroah-Hartman49cfe4d2012-12-21 13:14:09 -0800513static int geode_aes_probe(struct pci_dev *dev, const struct pci_device_id *id)
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000514{
515 int ret;
Chris Gorman24086e32017-07-06 14:44:56 -0400516
Chihau Chau99700712010-04-19 21:02:41 +0800517 ret = pci_enable_device(dev);
518 if (ret)
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000519 return ret;
520
Chihau Chau99700712010-04-19 21:02:41 +0800521 ret = pci_request_regions(dev, "geode-aes");
522 if (ret)
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000523 goto eenable;
524
525 _iobase = pci_iomap(dev, 0, 0);
526
527 if (_iobase == NULL) {
528 ret = -ENOMEM;
529 goto erequest;
530 }
531
532 spin_lock_init(&lock);
533
534 /* Clear any pending activity */
535 iowrite32(AES_INTR_PENDING | AES_INTR_MASK, _iobase + AES_INTR_REG);
536
Chihau Chau99700712010-04-19 21:02:41 +0800537 ret = crypto_register_alg(&geode_alg);
538 if (ret)
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000539 goto eiomap;
540
Chihau Chau99700712010-04-19 21:02:41 +0800541 ret = crypto_register_alg(&geode_ecb_alg);
542 if (ret)
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000543 goto ealg;
544
Chihau Chau99700712010-04-19 21:02:41 +0800545 ret = crypto_register_alg(&geode_cbc_alg);
546 if (ret)
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000547 goto eecb;
548
Marek Vasut701bcbc2014-05-14 11:36:41 +0200549 dev_notice(&dev->dev, "GEODE AES engine enabled.\n");
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000550 return 0;
551
552 eecb:
553 crypto_unregister_alg(&geode_ecb_alg);
554
555 ealg:
556 crypto_unregister_alg(&geode_alg);
557
558 eiomap:
559 pci_iounmap(dev, _iobase);
560
561 erequest:
562 pci_release_regions(dev);
563
564 eenable:
565 pci_disable_device(dev);
566
Marek Vasut701bcbc2014-05-14 11:36:41 +0200567 dev_err(&dev->dev, "GEODE AES initialization failed.\n");
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000568 return ret;
569}
570
571static struct pci_device_id geode_aes_tbl[] = {
Chris Gorman24086e32017-07-06 14:44:56 -0400572 { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_LX_AES), },
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000573 { 0, }
574};
575
576MODULE_DEVICE_TABLE(pci, geode_aes_tbl);
577
578static struct pci_driver geode_aes_driver = {
579 .name = "Geode LX AES",
580 .id_table = geode_aes_tbl,
581 .probe = geode_aes_probe,
Greg Kroah-Hartman49cfe4d2012-12-21 13:14:09 -0800582 .remove = geode_aes_remove,
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000583};
584
Sachin Kamat49d30d32012-08-27 15:45:31 +0530585module_pci_driver(geode_aes_driver);
Jordan Crouse9fe757b2006-10-04 18:48:57 +1000586
587MODULE_AUTHOR("Advanced Micro Devices, Inc.");
588MODULE_DESCRIPTION("Geode LX Hardware AES driver");
589MODULE_LICENSE("GPL");