geode-aes.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /* 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. #include <linux/module.h>
  9. #include <linux/kernel.h>
  10. #include <linux/pci.h>
  11. #include <linux/pci_ids.h>
  12. #include <linux/crypto.h>
  13. #include <linux/spinlock.h>
  14. #include <crypto/algapi.h>
  15. #include <crypto/aes.h>
  16. #include <crypto/internal/skcipher.h>
  17. #include <linux/io.h>
  18. #include <linux/delay.h>
  19. #include "geode-aes.h"
  20. /* Static structures */
  21. static void __iomem *_iobase;
  22. static spinlock_t lock;
  23. /* Write a 128 bit field (either a writable key or IV) */
  24. static inline void
  25. _writefield(u32 offset, const void *value)
  26. {
  27. int i;
  28. for (i = 0; i < 4; i++)
  29. iowrite32(((const u32 *) value)[i], _iobase + offset + (i * 4));
  30. }
  31. /* Read a 128 bit field (either a writable key or IV) */
  32. static inline void
  33. _readfield(u32 offset, void *value)
  34. {
  35. int i;
  36. for (i = 0; i < 4; i++)
  37. ((u32 *) value)[i] = ioread32(_iobase + offset + (i * 4));
  38. }
  39. static int
  40. do_crypt(const void *src, void *dst, u32 len, u32 flags)
  41. {
  42. u32 status;
  43. u32 counter = AES_OP_TIMEOUT;
  44. iowrite32(virt_to_phys((void *)src), _iobase + AES_SOURCEA_REG);
  45. iowrite32(virt_to_phys(dst), _iobase + AES_DSTA_REG);
  46. iowrite32(len, _iobase + AES_LENA_REG);
  47. /* Start the operation */
  48. iowrite32(AES_CTRL_START | flags, _iobase + AES_CTRLA_REG);
  49. do {
  50. status = ioread32(_iobase + AES_INTR_REG);
  51. cpu_relax();
  52. } while (!(status & AES_INTRA_PENDING) && --counter);
  53. /* Clear the event */
  54. iowrite32((status & 0xFF) | AES_INTRA_PENDING, _iobase + AES_INTR_REG);
  55. return counter ? 0 : 1;
  56. }
  57. static void
  58. geode_aes_crypt(const struct geode_aes_tfm_ctx *tctx, const void *src,
  59. void *dst, u32 len, u8 *iv, int mode, int dir)
  60. {
  61. u32 flags = 0;
  62. unsigned long iflags;
  63. int ret;
  64. /* If the source and destination is the same, then
  65. * we need to turn on the coherent flags, otherwise
  66. * we don't need to worry
  67. */
  68. flags |= (AES_CTRL_DCA | AES_CTRL_SCA);
  69. if (dir == AES_DIR_ENCRYPT)
  70. flags |= AES_CTRL_ENCRYPT;
  71. /* Start the critical section */
  72. spin_lock_irqsave(&lock, iflags);
  73. if (mode == AES_MODE_CBC) {
  74. flags |= AES_CTRL_CBC;
  75. _writefield(AES_WRITEIV0_REG, iv);
  76. }
  77. flags |= AES_CTRL_WRKEY;
  78. _writefield(AES_WRITEKEY0_REG, tctx->key);
  79. ret = do_crypt(src, dst, len, flags);
  80. BUG_ON(ret);
  81. if (mode == AES_MODE_CBC)
  82. _readfield(AES_WRITEIV0_REG, iv);
  83. spin_unlock_irqrestore(&lock, iflags);
  84. }
  85. /* CRYPTO-API Functions */
  86. static int geode_setkey_cip(struct crypto_tfm *tfm, const u8 *key,
  87. unsigned int len)
  88. {
  89. struct geode_aes_tfm_ctx *tctx = crypto_tfm_ctx(tfm);
  90. unsigned int ret;
  91. tctx->keylen = len;
  92. if (len == AES_KEYSIZE_128) {
  93. memcpy(tctx->key, key, len);
  94. return 0;
  95. }
  96. if (len != AES_KEYSIZE_192 && len != AES_KEYSIZE_256) {
  97. /* not supported at all */
  98. tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  99. return -EINVAL;
  100. }
  101. /*
  102. * The requested key size is not supported by HW, do a fallback
  103. */
  104. tctx->fallback.cip->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK;
  105. tctx->fallback.cip->base.crt_flags |=
  106. (tfm->crt_flags & CRYPTO_TFM_REQ_MASK);
  107. ret = crypto_cipher_setkey(tctx->fallback.cip, key, len);
  108. if (ret) {
  109. tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
  110. tfm->crt_flags |= (tctx->fallback.cip->base.crt_flags &
  111. CRYPTO_TFM_RES_MASK);
  112. }
  113. return ret;
  114. }
  115. static int geode_setkey_skcipher(struct crypto_skcipher *tfm, const u8 *key,
  116. unsigned int len)
  117. {
  118. struct geode_aes_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
  119. unsigned int ret;
  120. tctx->keylen = len;
  121. if (len == AES_KEYSIZE_128) {
  122. memcpy(tctx->key, key, len);
  123. return 0;
  124. }
  125. if (len != AES_KEYSIZE_192 && len != AES_KEYSIZE_256) {
  126. /* not supported at all */
  127. crypto_skcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  128. return -EINVAL;
  129. }
  130. /*
  131. * The requested key size is not supported by HW, do a fallback
  132. */
  133. crypto_skcipher_clear_flags(tctx->fallback.skcipher,
  134. CRYPTO_TFM_REQ_MASK);
  135. crypto_skcipher_set_flags(tctx->fallback.skcipher,
  136. crypto_skcipher_get_flags(tfm) &
  137. CRYPTO_TFM_REQ_MASK);
  138. ret = crypto_skcipher_setkey(tctx->fallback.skcipher, key, len);
  139. crypto_skcipher_set_flags(tfm,
  140. crypto_skcipher_get_flags(tctx->fallback.skcipher) &
  141. CRYPTO_TFM_RES_MASK);
  142. return ret;
  143. }
  144. static void
  145. geode_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
  146. {
  147. const struct geode_aes_tfm_ctx *tctx = crypto_tfm_ctx(tfm);
  148. if (unlikely(tctx->keylen != AES_KEYSIZE_128)) {
  149. crypto_cipher_encrypt_one(tctx->fallback.cip, out, in);
  150. return;
  151. }
  152. geode_aes_crypt(tctx, in, out, AES_BLOCK_SIZE, NULL,
  153. AES_MODE_ECB, AES_DIR_ENCRYPT);
  154. }
  155. static void
  156. geode_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
  157. {
  158. const struct geode_aes_tfm_ctx *tctx = crypto_tfm_ctx(tfm);
  159. if (unlikely(tctx->keylen != AES_KEYSIZE_128)) {
  160. crypto_cipher_decrypt_one(tctx->fallback.cip, out, in);
  161. return;
  162. }
  163. geode_aes_crypt(tctx, in, out, AES_BLOCK_SIZE, NULL,
  164. AES_MODE_ECB, AES_DIR_DECRYPT);
  165. }
  166. static int fallback_init_cip(struct crypto_tfm *tfm)
  167. {
  168. const char *name = crypto_tfm_alg_name(tfm);
  169. struct geode_aes_tfm_ctx *tctx = crypto_tfm_ctx(tfm);
  170. tctx->fallback.cip = crypto_alloc_cipher(name, 0,
  171. CRYPTO_ALG_NEED_FALLBACK);
  172. if (IS_ERR(tctx->fallback.cip)) {
  173. printk(KERN_ERR "Error allocating fallback algo %s\n", name);
  174. return PTR_ERR(tctx->fallback.cip);
  175. }
  176. return 0;
  177. }
  178. static void fallback_exit_cip(struct crypto_tfm *tfm)
  179. {
  180. struct geode_aes_tfm_ctx *tctx = crypto_tfm_ctx(tfm);
  181. crypto_free_cipher(tctx->fallback.cip);
  182. }
  183. static struct crypto_alg geode_alg = {
  184. .cra_name = "aes",
  185. .cra_driver_name = "geode-aes",
  186. .cra_priority = 300,
  187. .cra_alignmask = 15,
  188. .cra_flags = CRYPTO_ALG_TYPE_CIPHER |
  189. CRYPTO_ALG_NEED_FALLBACK,
  190. .cra_init = fallback_init_cip,
  191. .cra_exit = fallback_exit_cip,
  192. .cra_blocksize = AES_BLOCK_SIZE,
  193. .cra_ctxsize = sizeof(struct geode_aes_tfm_ctx),
  194. .cra_module = THIS_MODULE,
  195. .cra_u = {
  196. .cipher = {
  197. .cia_min_keysize = AES_MIN_KEY_SIZE,
  198. .cia_max_keysize = AES_MAX_KEY_SIZE,
  199. .cia_setkey = geode_setkey_cip,
  200. .cia_encrypt = geode_encrypt,
  201. .cia_decrypt = geode_decrypt
  202. }
  203. }
  204. };
  205. static int geode_init_skcipher(struct crypto_skcipher *tfm)
  206. {
  207. const char *name = crypto_tfm_alg_name(&tfm->base);
  208. struct geode_aes_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
  209. tctx->fallback.skcipher =
  210. crypto_alloc_skcipher(name, 0, CRYPTO_ALG_NEED_FALLBACK |
  211. CRYPTO_ALG_ASYNC);
  212. if (IS_ERR(tctx->fallback.skcipher)) {
  213. printk(KERN_ERR "Error allocating fallback algo %s\n", name);
  214. return PTR_ERR(tctx->fallback.skcipher);
  215. }
  216. crypto_skcipher_set_reqsize(tfm, sizeof(struct skcipher_request) +
  217. crypto_skcipher_reqsize(tctx->fallback.skcipher));
  218. return 0;
  219. }
  220. static void geode_exit_skcipher(struct crypto_skcipher *tfm)
  221. {
  222. struct geode_aes_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
  223. crypto_free_skcipher(tctx->fallback.skcipher);
  224. }
  225. static int geode_skcipher_crypt(struct skcipher_request *req, int mode, int dir)
  226. {
  227. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  228. const struct geode_aes_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
  229. struct skcipher_walk walk;
  230. unsigned int nbytes;
  231. int err;
  232. if (unlikely(tctx->keylen != AES_KEYSIZE_128)) {
  233. struct skcipher_request *subreq = skcipher_request_ctx(req);
  234. *subreq = *req;
  235. skcipher_request_set_tfm(subreq, tctx->fallback.skcipher);
  236. if (dir == AES_DIR_DECRYPT)
  237. return crypto_skcipher_decrypt(subreq);
  238. else
  239. return crypto_skcipher_encrypt(subreq);
  240. }
  241. err = skcipher_walk_virt(&walk, req, false);
  242. while ((nbytes = walk.nbytes) != 0) {
  243. geode_aes_crypt(tctx, walk.src.virt.addr, walk.dst.virt.addr,
  244. round_down(nbytes, AES_BLOCK_SIZE),
  245. walk.iv, mode, dir);
  246. err = skcipher_walk_done(&walk, nbytes % AES_BLOCK_SIZE);
  247. }
  248. return err;
  249. }
  250. static int geode_cbc_encrypt(struct skcipher_request *req)
  251. {
  252. return geode_skcipher_crypt(req, AES_MODE_CBC, AES_DIR_ENCRYPT);
  253. }
  254. static int geode_cbc_decrypt(struct skcipher_request *req)
  255. {
  256. return geode_skcipher_crypt(req, AES_MODE_CBC, AES_DIR_DECRYPT);
  257. }
  258. static int geode_ecb_encrypt(struct skcipher_request *req)
  259. {
  260. return geode_skcipher_crypt(req, AES_MODE_ECB, AES_DIR_ENCRYPT);
  261. }
  262. static int geode_ecb_decrypt(struct skcipher_request *req)
  263. {
  264. return geode_skcipher_crypt(req, AES_MODE_ECB, AES_DIR_DECRYPT);
  265. }
  266. static struct skcipher_alg geode_skcipher_algs[] = {
  267. {
  268. .base.cra_name = "cbc(aes)",
  269. .base.cra_driver_name = "cbc-aes-geode",
  270. .base.cra_priority = 400,
  271. .base.cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY |
  272. CRYPTO_ALG_NEED_FALLBACK,
  273. .base.cra_blocksize = AES_BLOCK_SIZE,
  274. .base.cra_ctxsize = sizeof(struct geode_aes_tfm_ctx),
  275. .base.cra_alignmask = 15,
  276. .base.cra_module = THIS_MODULE,
  277. .init = geode_init_skcipher,
  278. .exit = geode_exit_skcipher,
  279. .setkey = geode_setkey_skcipher,
  280. .encrypt = geode_cbc_encrypt,
  281. .decrypt = geode_cbc_decrypt,
  282. .min_keysize = AES_MIN_KEY_SIZE,
  283. .max_keysize = AES_MAX_KEY_SIZE,
  284. .ivsize = AES_BLOCK_SIZE,
  285. }, {
  286. .base.cra_name = "ecb(aes)",
  287. .base.cra_driver_name = "ecb-aes-geode",
  288. .base.cra_priority = 400,
  289. .base.cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY |
  290. CRYPTO_ALG_NEED_FALLBACK,
  291. .base.cra_blocksize = AES_BLOCK_SIZE,
  292. .base.cra_ctxsize = sizeof(struct geode_aes_tfm_ctx),
  293. .base.cra_alignmask = 15,
  294. .base.cra_module = THIS_MODULE,
  295. .init = geode_init_skcipher,
  296. .exit = geode_exit_skcipher,
  297. .setkey = geode_setkey_skcipher,
  298. .encrypt = geode_ecb_encrypt,
  299. .decrypt = geode_ecb_decrypt,
  300. .min_keysize = AES_MIN_KEY_SIZE,
  301. .max_keysize = AES_MAX_KEY_SIZE,
  302. },
  303. };
  304. static void geode_aes_remove(struct pci_dev *dev)
  305. {
  306. crypto_unregister_alg(&geode_alg);
  307. crypto_unregister_skciphers(geode_skcipher_algs,
  308. ARRAY_SIZE(geode_skcipher_algs));
  309. pci_iounmap(dev, _iobase);
  310. _iobase = NULL;
  311. pci_release_regions(dev);
  312. pci_disable_device(dev);
  313. }
  314. static int geode_aes_probe(struct pci_dev *dev, const struct pci_device_id *id)
  315. {
  316. int ret;
  317. ret = pci_enable_device(dev);
  318. if (ret)
  319. return ret;
  320. ret = pci_request_regions(dev, "geode-aes");
  321. if (ret)
  322. goto eenable;
  323. _iobase = pci_iomap(dev, 0, 0);
  324. if (_iobase == NULL) {
  325. ret = -ENOMEM;
  326. goto erequest;
  327. }
  328. spin_lock_init(&lock);
  329. /* Clear any pending activity */
  330. iowrite32(AES_INTR_PENDING | AES_INTR_MASK, _iobase + AES_INTR_REG);
  331. ret = crypto_register_alg(&geode_alg);
  332. if (ret)
  333. goto eiomap;
  334. ret = crypto_register_skciphers(geode_skcipher_algs,
  335. ARRAY_SIZE(geode_skcipher_algs));
  336. if (ret)
  337. goto ealg;
  338. dev_notice(&dev->dev, "GEODE AES engine enabled.\n");
  339. return 0;
  340. ealg:
  341. crypto_unregister_alg(&geode_alg);
  342. eiomap:
  343. pci_iounmap(dev, _iobase);
  344. erequest:
  345. pci_release_regions(dev);
  346. eenable:
  347. pci_disable_device(dev);
  348. dev_err(&dev->dev, "GEODE AES initialization failed.\n");
  349. return ret;
  350. }
  351. static struct pci_device_id geode_aes_tbl[] = {
  352. { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_LX_AES), },
  353. { 0, }
  354. };
  355. MODULE_DEVICE_TABLE(pci, geode_aes_tbl);
  356. static struct pci_driver geode_aes_driver = {
  357. .name = "Geode LX AES",
  358. .id_table = geode_aes_tbl,
  359. .probe = geode_aes_probe,
  360. .remove = geode_aes_remove,
  361. };
  362. module_pci_driver(geode_aes_driver);
  363. MODULE_AUTHOR("Advanced Micro Devices, Inc.");
  364. MODULE_DESCRIPTION("Geode LX Hardware AES driver");
  365. MODULE_LICENSE("GPL");