crypto: sahara - use BIT() macro
[linux-block.git] / drivers / crypto / geode-aes.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
9fe757b0 2 /* Copyright (C) 2004-2006, Advanced Micro Devices, Inc.
24086e3d 3 */
9fe757b0
JC
4
5#include <linux/module.h>
6#include <linux/kernel.h>
9fe757b0
JC
7#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>
89e12654 12#include <crypto/aes.h>
0eb76ba2 13#include <crypto/internal/cipher.h>
4549f7e5 14#include <crypto/internal/skcipher.h>
9fe757b0 15
99700716
CC
16#include <linux/io.h>
17#include <linux/delay.h>
9fe757b0
JC
18
19#include "geode-aes.h"
20
9fe757b0
JC
21/* Static structures */
22
99700716 23static void __iomem *_iobase;
25ee76a2 24static DEFINE_SPINLOCK(lock);
9fe757b0
JC
25
26/* Write a 128 bit field (either a writable key or IV) */
27static inline void
4549f7e5 28_writefield(u32 offset, const void *value)
9fe757b0
JC
29{
30 int i;
24086e3d 31
99700716 32 for (i = 0; i < 4; i++)
4549f7e5 33 iowrite32(((const u32 *) value)[i], _iobase + offset + (i * 4));
9fe757b0
JC
34}
35
36/* Read a 128 bit field (either a writable key or IV) */
37static inline void
38_readfield(u32 offset, void *value)
39{
40 int i;
24086e3d 41
99700716 42 for (i = 0; i < 4; i++)
9fe757b0
JC
43 ((u32 *) value)[i] = ioread32(_iobase + offset + (i * 4));
44}
45
46static int
4549f7e5 47do_crypt(const void *src, void *dst, u32 len, u32 flags)
9fe757b0
JC
48{
49 u32 status;
50 u32 counter = AES_OP_TIMEOUT;
51
4549f7e5 52 iowrite32(virt_to_phys((void *)src), _iobase + AES_SOURCEA_REG);
9fe757b0
JC
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
1f4e4773 59 do {
9fe757b0 60 status = ioread32(_iobase + AES_INTR_REG);
1f4e4773 61 cpu_relax();
99700716 62 } while (!(status & AES_INTRA_PENDING) && --counter);
9fe757b0
JC
63
64 /* Clear the event */
65 iowrite32((status & 0xFF) | AES_INTRA_PENDING, _iobase + AES_INTR_REG);
66 return counter ? 0 : 1;
67}
68
4549f7e5
EB
69static void
70geode_aes_crypt(const struct geode_aes_tfm_ctx *tctx, const void *src,
71 void *dst, u32 len, u8 *iv, int mode, int dir)
9fe757b0 72{
9fe757b0 73 u32 flags = 0;
5efee174 74 unsigned long iflags;
1f4e4773 75 int ret;
9fe757b0 76
761e7846
JC
77 /* 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
2e21630d 82 flags |= (AES_CTRL_DCA | AES_CTRL_SCA);
9fe757b0 83
4549f7e5 84 if (dir == AES_DIR_ENCRYPT)
9fe757b0
JC
85 flags |= AES_CTRL_ENCRYPT;
86
87 /* Start the critical section */
88
89 spin_lock_irqsave(&lock, iflags);
90
4549f7e5 91 if (mode == AES_MODE_CBC) {
9fe757b0 92 flags |= AES_CTRL_CBC;
4549f7e5 93 _writefield(AES_WRITEIV0_REG, iv);
9fe757b0
JC
94 }
95
4549f7e5
EB
96 flags |= AES_CTRL_WRKEY;
97 _writefield(AES_WRITEKEY0_REG, tctx->key);
9fe757b0 98
4549f7e5 99 ret = do_crypt(src, dst, len, flags);
1f4e4773 100 BUG_ON(ret);
9fe757b0 101
4549f7e5
EB
102 if (mode == AES_MODE_CBC)
103 _readfield(AES_WRITEIV0_REG, iv);
9fe757b0
JC
104
105 spin_unlock_irqrestore(&lock, iflags);
9fe757b0
JC
106}
107
108/* CRYPTO-API Functions */
109
cd7c3bfe
SS
110static int geode_setkey_cip(struct crypto_tfm *tfm, const u8 *key,
111 unsigned int len)
9fe757b0 112{
4549f7e5 113 struct geode_aes_tfm_ctx *tctx = crypto_tfm_ctx(tfm);
cd7c3bfe 114
4549f7e5 115 tctx->keylen = len;
cd7c3bfe
SS
116
117 if (len == AES_KEYSIZE_128) {
4549f7e5 118 memcpy(tctx->key, key, len);
cd7c3bfe
SS
119 return 0;
120 }
9fe757b0 121
674f368a 122 if (len != AES_KEYSIZE_192 && len != AES_KEYSIZE_256)
cd7c3bfe 123 /* not supported at all */
9fe757b0 124 return -EINVAL;
9fe757b0 125
cd7c3bfe
SS
126 /*
127 * The requested key size is not supported by HW, do a fallback
128 */
4549f7e5
EB
129 tctx->fallback.cip->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK;
130 tctx->fallback.cip->base.crt_flags |=
131 (tfm->crt_flags & CRYPTO_TFM_REQ_MASK);
cd7c3bfe 132
af5034e8 133 return crypto_cipher_setkey(tctx->fallback.cip, key, len);
cd7c3bfe
SS
134}
135
4549f7e5
EB
136static int geode_setkey_skcipher(struct crypto_skcipher *tfm, const u8 *key,
137 unsigned int len)
cd7c3bfe 138{
4549f7e5 139 struct geode_aes_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
cd7c3bfe 140
4549f7e5 141 tctx->keylen = len;
cd7c3bfe
SS
142
143 if (len == AES_KEYSIZE_128) {
4549f7e5 144 memcpy(tctx->key, key, len);
cd7c3bfe
SS
145 return 0;
146 }
147
674f368a 148 if (len != AES_KEYSIZE_192 && len != AES_KEYSIZE_256)
cd7c3bfe 149 /* not supported at all */
cd7c3bfe 150 return -EINVAL;
cd7c3bfe
SS
151
152 /*
153 * The requested key size is not supported by HW, do a fallback
154 */
4549f7e5
EB
155 crypto_skcipher_clear_flags(tctx->fallback.skcipher,
156 CRYPTO_TFM_REQ_MASK);
157 crypto_skcipher_set_flags(tctx->fallback.skcipher,
158 crypto_skcipher_get_flags(tfm) &
159 CRYPTO_TFM_REQ_MASK);
af5034e8 160 return crypto_skcipher_setkey(tctx->fallback.skcipher, key, len);
cd7c3bfe
SS
161}
162
9fe757b0
JC
163static void
164geode_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
165{
4549f7e5 166 const struct geode_aes_tfm_ctx *tctx = crypto_tfm_ctx(tfm);
9fe757b0 167
4549f7e5
EB
168 if (unlikely(tctx->keylen != AES_KEYSIZE_128)) {
169 crypto_cipher_encrypt_one(tctx->fallback.cip, out, in);
9fe757b0 170 return;
cd7c3bfe 171 }
9fe757b0 172
4549f7e5
EB
173 geode_aes_crypt(tctx, in, out, AES_BLOCK_SIZE, NULL,
174 AES_MODE_ECB, AES_DIR_ENCRYPT);
9fe757b0
JC
175}
176
177
178static void
179geode_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
180{
4549f7e5 181 const struct geode_aes_tfm_ctx *tctx = crypto_tfm_ctx(tfm);
9fe757b0 182
4549f7e5
EB
183 if (unlikely(tctx->keylen != AES_KEYSIZE_128)) {
184 crypto_cipher_decrypt_one(tctx->fallback.cip, out, in);
9fe757b0 185 return;
cd7c3bfe 186 }
9fe757b0 187
4549f7e5
EB
188 geode_aes_crypt(tctx, in, out, AES_BLOCK_SIZE, NULL,
189 AES_MODE_ECB, AES_DIR_DECRYPT);
9fe757b0
JC
190}
191
cd7c3bfe
SS
192static int fallback_init_cip(struct crypto_tfm *tfm)
193{
d207a38c 194 const char *name = crypto_tfm_alg_name(tfm);
4549f7e5 195 struct geode_aes_tfm_ctx *tctx = crypto_tfm_ctx(tfm);
cd7c3bfe 196
4549f7e5
EB
197 tctx->fallback.cip = crypto_alloc_cipher(name, 0,
198 CRYPTO_ALG_NEED_FALLBACK);
cd7c3bfe 199
4549f7e5 200 if (IS_ERR(tctx->fallback.cip)) {
cd7c3bfe 201 printk(KERN_ERR "Error allocating fallback algo %s\n", name);
4549f7e5 202 return PTR_ERR(tctx->fallback.cip);
cd7c3bfe
SS
203 }
204
205 return 0;
206}
207
208static void fallback_exit_cip(struct crypto_tfm *tfm)
209{
4549f7e5 210 struct geode_aes_tfm_ctx *tctx = crypto_tfm_ctx(tfm);
cd7c3bfe 211
4549f7e5 212 crypto_free_cipher(tctx->fallback.cip);
cd7c3bfe 213}
9fe757b0
JC
214
215static struct crypto_alg geode_alg = {
cd7c3bfe
SS
216 .cra_name = "aes",
217 .cra_driver_name = "geode-aes",
218 .cra_priority = 300,
219 .cra_alignmask = 15,
220 .cra_flags = CRYPTO_ALG_TYPE_CIPHER |
221 CRYPTO_ALG_NEED_FALLBACK,
222 .cra_init = fallback_init_cip,
223 .cra_exit = fallback_exit_cip,
b9d865e3 224 .cra_blocksize = AES_BLOCK_SIZE,
4549f7e5 225 .cra_ctxsize = sizeof(struct geode_aes_tfm_ctx),
cd7c3bfe 226 .cra_module = THIS_MODULE,
cd7c3bfe
SS
227 .cra_u = {
228 .cipher = {
229 .cia_min_keysize = AES_MIN_KEY_SIZE,
230 .cia_max_keysize = AES_MAX_KEY_SIZE,
231 .cia_setkey = geode_setkey_cip,
232 .cia_encrypt = geode_encrypt,
233 .cia_decrypt = geode_decrypt
9fe757b0
JC
234 }
235 }
236};
237
4549f7e5 238static int geode_init_skcipher(struct crypto_skcipher *tfm)
9fe757b0 239{
4549f7e5
EB
240 const char *name = crypto_tfm_alg_name(&tfm->base);
241 struct geode_aes_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
cd7c3bfe 242
4549f7e5
EB
243 tctx->fallback.skcipher =
244 crypto_alloc_skcipher(name, 0, CRYPTO_ALG_NEED_FALLBACK |
245 CRYPTO_ALG_ASYNC);
246 if (IS_ERR(tctx->fallback.skcipher)) {
247 printk(KERN_ERR "Error allocating fallback algo %s\n", name);
248 return PTR_ERR(tctx->fallback.skcipher);
9fe757b0
JC
249 }
250
4549f7e5
EB
251 crypto_skcipher_set_reqsize(tfm, sizeof(struct skcipher_request) +
252 crypto_skcipher_reqsize(tctx->fallback.skcipher));
253 return 0;
9fe757b0
JC
254}
255
4549f7e5 256static void geode_exit_skcipher(struct crypto_skcipher *tfm)
9fe757b0 257{
4549f7e5 258 struct geode_aes_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
9fe757b0 259
4549f7e5
EB
260 crypto_free_skcipher(tctx->fallback.skcipher);
261}
cd7c3bfe 262
4549f7e5
EB
263static int geode_skcipher_crypt(struct skcipher_request *req, int mode, int dir)
264{
265 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
266 const struct geode_aes_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
267 struct skcipher_walk walk;
268 unsigned int nbytes;
269 int err;
270
271 if (unlikely(tctx->keylen != AES_KEYSIZE_128)) {
272 struct skcipher_request *subreq = skcipher_request_ctx(req);
273
274 *subreq = *req;
275 skcipher_request_set_tfm(subreq, tctx->fallback.skcipher);
276 if (dir == AES_DIR_DECRYPT)
277 return crypto_skcipher_decrypt(subreq);
278 else
279 return crypto_skcipher_encrypt(subreq);
280 }
9fe757b0 281
4549f7e5 282 err = skcipher_walk_virt(&walk, req, false);
9fe757b0 283
4549f7e5
EB
284 while ((nbytes = walk.nbytes) != 0) {
285 geode_aes_crypt(tctx, walk.src.virt.addr, walk.dst.virt.addr,
286 round_down(nbytes, AES_BLOCK_SIZE),
287 walk.iv, mode, dir);
288 err = skcipher_walk_done(&walk, nbytes % AES_BLOCK_SIZE);
9fe757b0
JC
289 }
290
291 return err;
292}
293
4549f7e5 294static int geode_cbc_encrypt(struct skcipher_request *req)
cd7c3bfe 295{
4549f7e5 296 return geode_skcipher_crypt(req, AES_MODE_CBC, AES_DIR_ENCRYPT);
cd7c3bfe
SS
297}
298
4549f7e5 299static int geode_cbc_decrypt(struct skcipher_request *req)
cd7c3bfe 300{
4549f7e5 301 return geode_skcipher_crypt(req, AES_MODE_CBC, AES_DIR_DECRYPT);
cd7c3bfe
SS
302}
303
4549f7e5 304static int geode_ecb_encrypt(struct skcipher_request *req)
9fe757b0 305{
4549f7e5 306 return geode_skcipher_crypt(req, AES_MODE_ECB, AES_DIR_ENCRYPT);
9fe757b0
JC
307}
308
4549f7e5 309static int geode_ecb_decrypt(struct skcipher_request *req)
9fe757b0 310{
4549f7e5 311 return geode_skcipher_crypt(req, AES_MODE_ECB, AES_DIR_DECRYPT);
9fe757b0
JC
312}
313
4549f7e5
EB
314static struct skcipher_alg geode_skcipher_algs[] = {
315 {
316 .base.cra_name = "cbc(aes)",
317 .base.cra_driver_name = "cbc-aes-geode",
318 .base.cra_priority = 400,
319 .base.cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY |
320 CRYPTO_ALG_NEED_FALLBACK,
321 .base.cra_blocksize = AES_BLOCK_SIZE,
322 .base.cra_ctxsize = sizeof(struct geode_aes_tfm_ctx),
323 .base.cra_alignmask = 15,
324 .base.cra_module = THIS_MODULE,
325 .init = geode_init_skcipher,
326 .exit = geode_exit_skcipher,
327 .setkey = geode_setkey_skcipher,
328 .encrypt = geode_cbc_encrypt,
329 .decrypt = geode_cbc_decrypt,
330 .min_keysize = AES_MIN_KEY_SIZE,
331 .max_keysize = AES_MAX_KEY_SIZE,
332 .ivsize = AES_BLOCK_SIZE,
333 }, {
334 .base.cra_name = "ecb(aes)",
335 .base.cra_driver_name = "ecb-aes-geode",
336 .base.cra_priority = 400,
337 .base.cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY |
338 CRYPTO_ALG_NEED_FALLBACK,
339 .base.cra_blocksize = AES_BLOCK_SIZE,
340 .base.cra_ctxsize = sizeof(struct geode_aes_tfm_ctx),
341 .base.cra_alignmask = 15,
342 .base.cra_module = THIS_MODULE,
343 .init = geode_init_skcipher,
344 .exit = geode_exit_skcipher,
345 .setkey = geode_setkey_skcipher,
346 .encrypt = geode_ecb_encrypt,
347 .decrypt = geode_ecb_decrypt,
348 .min_keysize = AES_MIN_KEY_SIZE,
349 .max_keysize = AES_MAX_KEY_SIZE,
350 },
9fe757b0
JC
351};
352
49cfe4db 353static void geode_aes_remove(struct pci_dev *dev)
9fe757b0
JC
354{
355 crypto_unregister_alg(&geode_alg);
4549f7e5
EB
356 crypto_unregister_skciphers(geode_skcipher_algs,
357 ARRAY_SIZE(geode_skcipher_algs));
9fe757b0
JC
358
359 pci_iounmap(dev, _iobase);
360 _iobase = NULL;
361
362 pci_release_regions(dev);
363 pci_disable_device(dev);
364}
365
366
49cfe4db 367static int geode_aes_probe(struct pci_dev *dev, const struct pci_device_id *id)
9fe757b0
JC
368{
369 int ret;
24086e3d 370
99700716
CC
371 ret = pci_enable_device(dev);
372 if (ret)
9fe757b0
JC
373 return ret;
374
99700716
CC
375 ret = pci_request_regions(dev, "geode-aes");
376 if (ret)
9fe757b0
JC
377 goto eenable;
378
379 _iobase = pci_iomap(dev, 0, 0);
380
381 if (_iobase == NULL) {
382 ret = -ENOMEM;
383 goto erequest;
384 }
385
9fe757b0
JC
386 /* Clear any pending activity */
387 iowrite32(AES_INTR_PENDING | AES_INTR_MASK, _iobase + AES_INTR_REG);
388
99700716
CC
389 ret = crypto_register_alg(&geode_alg);
390 if (ret)
9fe757b0
JC
391 goto eiomap;
392
4549f7e5
EB
393 ret = crypto_register_skciphers(geode_skcipher_algs,
394 ARRAY_SIZE(geode_skcipher_algs));
99700716 395 if (ret)
9fe757b0
JC
396 goto ealg;
397
701bcbc1 398 dev_notice(&dev->dev, "GEODE AES engine enabled.\n");
9fe757b0
JC
399 return 0;
400
9fe757b0
JC
401 ealg:
402 crypto_unregister_alg(&geode_alg);
403
404 eiomap:
405 pci_iounmap(dev, _iobase);
406
407 erequest:
408 pci_release_regions(dev);
409
410 eenable:
411 pci_disable_device(dev);
412
701bcbc1 413 dev_err(&dev->dev, "GEODE AES initialization failed.\n");
9fe757b0
JC
414 return ret;
415}
416
417static struct pci_device_id geode_aes_tbl[] = {
24086e3d 418 { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_LX_AES), },
9fe757b0
JC
419 { 0, }
420};
421
422MODULE_DEVICE_TABLE(pci, geode_aes_tbl);
423
424static struct pci_driver geode_aes_driver = {
425 .name = "Geode LX AES",
426 .id_table = geode_aes_tbl,
427 .probe = geode_aes_probe,
49cfe4db 428 .remove = geode_aes_remove,
9fe757b0
JC
429};
430
49d30d3d 431module_pci_driver(geode_aes_driver);
9fe757b0
JC
432
433MODULE_AUTHOR("Advanced Micro Devices, Inc.");
434MODULE_DESCRIPTION("Geode LX Hardware AES driver");
435MODULE_LICENSE("GPL");
0eb76ba2 436MODULE_IMPORT_NS(CRYPTO_INTERNAL);