ccp-crypto-main.c 9.5 KB

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