ccp-crypto-main.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /*
  2. * AMD Cryptographic Coprocessor (CCP) crypto API support
  3. *
  4. * Copyright (C) 2013 Advanced Micro Devices, Inc.
  5. *
  6. * Author: Tom Lendacky <thomas.lendacky@amd.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/moduleparam.h>
  14. #include <linux/kernel.h>
  15. #include <linux/list.h>
  16. #include <linux/ccp.h>
  17. #include <linux/scatterlist.h>
  18. #include <crypto/internal/hash.h>
  19. #include "ccp-crypto.h"
  20. MODULE_AUTHOR("Tom Lendacky <thomas.lendacky@amd.com>");
  21. MODULE_LICENSE("GPL");
  22. MODULE_VERSION("1.0.0");
  23. MODULE_DESCRIPTION("AMD Cryptographic Coprocessor crypto API support");
  24. static unsigned int aes_disable;
  25. module_param(aes_disable, uint, 0444);
  26. MODULE_PARM_DESC(aes_disable, "Disable use of AES - any non-zero value");
  27. static unsigned int sha_disable;
  28. module_param(sha_disable, uint, 0444);
  29. MODULE_PARM_DESC(sha_disable, "Disable use of SHA - any non-zero value");
  30. static unsigned int des3_disable;
  31. module_param(des3_disable, uint, 0444);
  32. MODULE_PARM_DESC(des3_disable, "Disable use of 3DES - any non-zero value");
  33. /* List heads for the supported algorithms */
  34. static LIST_HEAD(hash_algs);
  35. static LIST_HEAD(cipher_algs);
  36. static LIST_HEAD(aead_algs);
  37. /* For any tfm, requests for that tfm must be returned on the order
  38. * received. With multiple queues available, the CCP can process more
  39. * than one cmd at a time. Therefore we must maintain a cmd list to insure
  40. * the proper ordering of requests on a given tfm.
  41. */
  42. struct ccp_crypto_queue {
  43. struct list_head cmds;
  44. struct list_head *backlog;
  45. unsigned int cmd_count;
  46. };
  47. #define CCP_CRYPTO_MAX_QLEN 100
  48. static struct ccp_crypto_queue req_queue;
  49. static spinlock_t req_queue_lock;
  50. struct ccp_crypto_cmd {
  51. struct list_head entry;
  52. struct ccp_cmd *cmd;
  53. /* Save the crypto_tfm and crypto_async_request addresses
  54. * separately to avoid any reference to a possibly invalid
  55. * crypto_async_request structure after invoking the request
  56. * callback
  57. */
  58. struct crypto_async_request *req;
  59. struct crypto_tfm *tfm;
  60. /* Used for held command processing to determine state */
  61. int ret;
  62. };
  63. struct ccp_crypto_cpu {
  64. struct work_struct work;
  65. struct completion completion;
  66. struct ccp_crypto_cmd *crypto_cmd;
  67. int err;
  68. };
  69. static inline bool ccp_crypto_success(int err)
  70. {
  71. if (err && (err != -EINPROGRESS) && (err != -EBUSY))
  72. return false;
  73. return true;
  74. }
  75. static struct ccp_crypto_cmd *ccp_crypto_cmd_complete(
  76. struct ccp_crypto_cmd *crypto_cmd, struct ccp_crypto_cmd **backlog)
  77. {
  78. struct ccp_crypto_cmd *held = NULL, *tmp;
  79. unsigned long flags;
  80. *backlog = NULL;
  81. spin_lock_irqsave(&req_queue_lock, flags);
  82. /* Held cmds will be after the current cmd in the queue so start
  83. * searching for a cmd with a matching tfm for submission.
  84. */
  85. tmp = crypto_cmd;
  86. list_for_each_entry_continue(tmp, &req_queue.cmds, entry) {
  87. if (crypto_cmd->tfm != tmp->tfm)
  88. continue;
  89. held = tmp;
  90. break;
  91. }
  92. /* Process the backlog:
  93. * Because cmds can be executed from any point in the cmd list
  94. * special precautions have to be taken when handling the backlog.
  95. */
  96. if (req_queue.backlog != &req_queue.cmds) {
  97. /* Skip over this cmd if it is the next backlog cmd */
  98. if (req_queue.backlog == &crypto_cmd->entry)
  99. req_queue.backlog = crypto_cmd->entry.next;
  100. *backlog = container_of(req_queue.backlog,
  101. struct ccp_crypto_cmd, entry);
  102. req_queue.backlog = req_queue.backlog->next;
  103. /* Skip over this cmd if it is now the next backlog cmd */
  104. if (req_queue.backlog == &crypto_cmd->entry)
  105. req_queue.backlog = crypto_cmd->entry.next;
  106. }
  107. /* Remove the cmd entry from the list of cmds */
  108. req_queue.cmd_count--;
  109. list_del(&crypto_cmd->entry);
  110. spin_unlock_irqrestore(&req_queue_lock, flags);
  111. return held;
  112. }
  113. static void ccp_crypto_complete(void *data, int err)
  114. {
  115. struct ccp_crypto_cmd *crypto_cmd = data;
  116. struct ccp_crypto_cmd *held, *next, *backlog;
  117. struct crypto_async_request *req = crypto_cmd->req;
  118. struct ccp_ctx *ctx = crypto_tfm_ctx(req->tfm);
  119. int ret;
  120. if (err == -EINPROGRESS) {
  121. /* Only propagate the -EINPROGRESS if necessary */
  122. if (crypto_cmd->ret == -EBUSY) {
  123. crypto_cmd->ret = -EINPROGRESS;
  124. req->complete(req, -EINPROGRESS);
  125. }
  126. return;
  127. }
  128. /* Operation has completed - update the queue before invoking
  129. * the completion callbacks and retrieve the next cmd (cmd with
  130. * a matching tfm) that can be submitted to the CCP.
  131. */
  132. held = ccp_crypto_cmd_complete(crypto_cmd, &backlog);
  133. if (backlog) {
  134. backlog->ret = -EINPROGRESS;
  135. backlog->req->complete(backlog->req, -EINPROGRESS);
  136. }
  137. /* Transition the state from -EBUSY to -EINPROGRESS first */
  138. if (crypto_cmd->ret == -EBUSY)
  139. req->complete(req, -EINPROGRESS);
  140. /* Completion callbacks */
  141. ret = err;
  142. if (ctx->complete)
  143. ret = ctx->complete(req, ret);
  144. req->complete(req, ret);
  145. /* Submit the next cmd */
  146. while (held) {
  147. /* Since we have already queued the cmd, we must indicate that
  148. * we can backlog so as not to "lose" this request.
  149. */
  150. held->cmd->flags |= CCP_CMD_MAY_BACKLOG;
  151. ret = ccp_enqueue_cmd(held->cmd);
  152. if (ccp_crypto_success(ret))
  153. break;
  154. /* Error occurred, report it and get the next entry */
  155. ctx = crypto_tfm_ctx(held->req->tfm);
  156. if (ctx->complete)
  157. ret = ctx->complete(held->req, ret);
  158. held->req->complete(held->req, ret);
  159. next = ccp_crypto_cmd_complete(held, &backlog);
  160. if (backlog) {
  161. backlog->ret = -EINPROGRESS;
  162. backlog->req->complete(backlog->req, -EINPROGRESS);
  163. }
  164. kfree(held);
  165. held = next;
  166. }
  167. kfree(crypto_cmd);
  168. }
  169. static int ccp_crypto_enqueue_cmd(struct ccp_crypto_cmd *crypto_cmd)
  170. {
  171. struct ccp_crypto_cmd *active = NULL, *tmp;
  172. unsigned long flags;
  173. bool free_cmd = true;
  174. int ret;
  175. spin_lock_irqsave(&req_queue_lock, flags);
  176. /* Check if the cmd can/should be queued */
  177. if (req_queue.cmd_count >= CCP_CRYPTO_MAX_QLEN) {
  178. ret = -EBUSY;
  179. if (!(crypto_cmd->cmd->flags & CCP_CMD_MAY_BACKLOG))
  180. goto e_lock;
  181. }
  182. /* Look for an entry with the same tfm. If there is a cmd
  183. * with the same tfm in the list then the current cmd cannot
  184. * be submitted to the CCP yet.
  185. */
  186. list_for_each_entry(tmp, &req_queue.cmds, entry) {
  187. if (crypto_cmd->tfm != tmp->tfm)
  188. continue;
  189. active = tmp;
  190. break;
  191. }
  192. ret = -EINPROGRESS;
  193. if (!active) {
  194. ret = ccp_enqueue_cmd(crypto_cmd->cmd);
  195. if (!ccp_crypto_success(ret))
  196. goto e_lock; /* Error, don't queue it */
  197. if ((ret == -EBUSY) &&
  198. !(crypto_cmd->cmd->flags & CCP_CMD_MAY_BACKLOG))
  199. goto e_lock; /* Not backlogging, don't queue it */
  200. }
  201. if (req_queue.cmd_count >= CCP_CRYPTO_MAX_QLEN) {
  202. ret = -EBUSY;
  203. if (req_queue.backlog == &req_queue.cmds)
  204. req_queue.backlog = &crypto_cmd->entry;
  205. }
  206. crypto_cmd->ret = ret;
  207. req_queue.cmd_count++;
  208. list_add_tail(&crypto_cmd->entry, &req_queue.cmds);
  209. free_cmd = false;
  210. e_lock:
  211. spin_unlock_irqrestore(&req_queue_lock, flags);
  212. if (free_cmd)
  213. kfree(crypto_cmd);
  214. return ret;
  215. }
  216. /**
  217. * ccp_crypto_enqueue_request - queue an crypto async request for processing
  218. * by the CCP
  219. *
  220. * @req: crypto_async_request struct to be processed
  221. * @cmd: ccp_cmd struct to be sent to the CCP
  222. */
  223. int ccp_crypto_enqueue_request(struct crypto_async_request *req,
  224. struct ccp_cmd *cmd)
  225. {
  226. struct ccp_crypto_cmd *crypto_cmd;
  227. gfp_t gfp;
  228. gfp = req->flags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL : GFP_ATOMIC;
  229. crypto_cmd = kzalloc(sizeof(*crypto_cmd), gfp);
  230. if (!crypto_cmd)
  231. return -ENOMEM;
  232. /* The tfm pointer must be saved and not referenced from the
  233. * crypto_async_request (req) pointer because it is used after
  234. * completion callback for the request and the req pointer
  235. * might not be valid anymore.
  236. */
  237. crypto_cmd->cmd = cmd;
  238. crypto_cmd->req = req;
  239. crypto_cmd->tfm = req->tfm;
  240. cmd->callback = ccp_crypto_complete;
  241. cmd->data = crypto_cmd;
  242. if (req->flags & CRYPTO_TFM_REQ_MAY_BACKLOG)
  243. cmd->flags |= CCP_CMD_MAY_BACKLOG;
  244. else
  245. cmd->flags &= ~CCP_CMD_MAY_BACKLOG;
  246. return ccp_crypto_enqueue_cmd(crypto_cmd);
  247. }
  248. struct scatterlist *ccp_crypto_sg_table_add(struct sg_table *table,
  249. struct scatterlist *sg_add)
  250. {
  251. struct scatterlist *sg, *sg_last = NULL;
  252. for (sg = table->sgl; sg; sg = sg_next(sg))
  253. if (!sg_page(sg))
  254. break;
  255. if (WARN_ON(!sg))
  256. return NULL;
  257. for (; sg && sg_add; sg = sg_next(sg), sg_add = sg_next(sg_add)) {
  258. sg_set_page(sg, sg_page(sg_add), sg_add->length,
  259. sg_add->offset);
  260. sg_last = sg;
  261. }
  262. if (WARN_ON(sg_add))
  263. return NULL;
  264. return sg_last;
  265. }
  266. static int ccp_register_algs(void)
  267. {
  268. int ret;
  269. if (!aes_disable) {
  270. ret = ccp_register_aes_algs(&cipher_algs);
  271. if (ret)
  272. return ret;
  273. ret = ccp_register_aes_cmac_algs(&hash_algs);
  274. if (ret)
  275. return ret;
  276. ret = ccp_register_aes_xts_algs(&cipher_algs);
  277. if (ret)
  278. return ret;
  279. ret = ccp_register_aes_aeads(&aead_algs);
  280. if (ret)
  281. return ret;
  282. }
  283. if (!des3_disable) {
  284. ret = ccp_register_des3_algs(&cipher_algs);
  285. if (ret)
  286. return ret;
  287. }
  288. if (!sha_disable) {
  289. ret = ccp_register_sha_algs(&hash_algs);
  290. if (ret)
  291. return ret;
  292. }
  293. return 0;
  294. }
  295. static void ccp_unregister_algs(void)
  296. {
  297. struct ccp_crypto_ahash_alg *ahash_alg, *ahash_tmp;
  298. struct ccp_crypto_ablkcipher_alg *ablk_alg, *ablk_tmp;
  299. struct ccp_crypto_aead *aead_alg, *aead_tmp;
  300. list_for_each_entry_safe(ahash_alg, ahash_tmp, &hash_algs, entry) {
  301. crypto_unregister_ahash(&ahash_alg->alg);
  302. list_del(&ahash_alg->entry);
  303. kfree(ahash_alg);
  304. }
  305. list_for_each_entry_safe(ablk_alg, ablk_tmp, &cipher_algs, entry) {
  306. crypto_unregister_alg(&ablk_alg->alg);
  307. list_del(&ablk_alg->entry);
  308. kfree(ablk_alg);
  309. }
  310. list_for_each_entry_safe(aead_alg, aead_tmp, &aead_algs, entry) {
  311. crypto_unregister_aead(&aead_alg->alg);
  312. list_del(&aead_alg->entry);
  313. kfree(aead_alg);
  314. }
  315. }
  316. static int ccp_crypto_init(void)
  317. {
  318. int ret;
  319. ret = ccp_present();
  320. if (ret)
  321. return ret;
  322. spin_lock_init(&req_queue_lock);
  323. INIT_LIST_HEAD(&req_queue.cmds);
  324. req_queue.backlog = &req_queue.cmds;
  325. req_queue.cmd_count = 0;
  326. ret = ccp_register_algs();
  327. if (ret)
  328. ccp_unregister_algs();
  329. return ret;
  330. }
  331. static void ccp_crypto_exit(void)
  332. {
  333. ccp_unregister_algs();
  334. }
  335. module_init(ccp_crypto_init);
  336. module_exit(ccp_crypto_exit);