blob: 7d98635c2c5e26d9b6196a82e4b88c36d17342f5 [file] [log] [blame]
Tom Lendackyd3123592013-11-12 11:46:22 -06001/*
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
13#include <linux/module.h>
Tom Lendackyd81ed652014-01-24 16:17:56 -060014#include <linux/moduleparam.h>
Tom Lendackyd3123592013-11-12 11:46:22 -060015#include <linux/kernel.h>
16#include <linux/list.h>
17#include <linux/ccp.h>
18#include <linux/scatterlist.h>
19#include <crypto/internal/hash.h>
20
21#include "ccp-crypto.h"
22
23MODULE_AUTHOR("Tom Lendacky <thomas.lendacky@amd.com>");
24MODULE_LICENSE("GPL");
25MODULE_VERSION("1.0.0");
26MODULE_DESCRIPTION("AMD Cryptographic Coprocessor crypto API support");
27
Tom Lendackyd81ed652014-01-24 16:17:56 -060028static unsigned int aes_disable;
29module_param(aes_disable, uint, 0444);
30MODULE_PARM_DESC(aes_disable, "Disable use of AES - any non-zero value");
31
32static unsigned int sha_disable;
33module_param(sha_disable, uint, 0444);
34MODULE_PARM_DESC(sha_disable, "Disable use of SHA - any non-zero value");
35
Tom Lendackyd3123592013-11-12 11:46:22 -060036
37/* List heads for the supported algorithms */
38static LIST_HEAD(hash_algs);
39static LIST_HEAD(cipher_algs);
40
Tom Lendackybc385442014-01-24 16:18:08 -060041/* For any tfm, requests for that tfm must be returned on the order
42 * received. With multiple queues available, the CCP can process more
43 * than one cmd at a time. Therefore we must maintain a cmd list to insure
44 * the proper ordering of requests on a given tfm.
Tom Lendackyd3123592013-11-12 11:46:22 -060045 */
Tom Lendackybc385442014-01-24 16:18:08 -060046struct ccp_crypto_queue {
Tom Lendackyd3123592013-11-12 11:46:22 -060047 struct list_head cmds;
48 struct list_head *backlog;
49 unsigned int cmd_count;
50};
Tom Lendackybc385442014-01-24 16:18:08 -060051#define CCP_CRYPTO_MAX_QLEN 100
Tom Lendackyd3123592013-11-12 11:46:22 -060052
Tom Lendackybc385442014-01-24 16:18:08 -060053static struct ccp_crypto_queue req_queue;
54static spinlock_t req_queue_lock;
Tom Lendackyd3123592013-11-12 11:46:22 -060055
56struct ccp_crypto_cmd {
57 struct list_head entry;
58
59 struct ccp_cmd *cmd;
60
61 /* Save the crypto_tfm and crypto_async_request addresses
62 * separately to avoid any reference to a possibly invalid
63 * crypto_async_request structure after invoking the request
64 * callback
65 */
66 struct crypto_async_request *req;
67 struct crypto_tfm *tfm;
68
69 /* Used for held command processing to determine state */
70 int ret;
Tom Lendackyd3123592013-11-12 11:46:22 -060071};
72
73struct ccp_crypto_cpu {
74 struct work_struct work;
75 struct completion completion;
76 struct ccp_crypto_cmd *crypto_cmd;
77 int err;
78};
79
80
81static inline bool ccp_crypto_success(int err)
82{
83 if (err && (err != -EINPROGRESS) && (err != -EBUSY))
84 return false;
85
86 return true;
87}
88
Tom Lendackyd3123592013-11-12 11:46:22 -060089static struct ccp_crypto_cmd *ccp_crypto_cmd_complete(
90 struct ccp_crypto_cmd *crypto_cmd, struct ccp_crypto_cmd **backlog)
91{
Tom Lendackyd3123592013-11-12 11:46:22 -060092 struct ccp_crypto_cmd *held = NULL, *tmp;
Tom Lendackybc385442014-01-24 16:18:08 -060093 unsigned long flags;
Tom Lendackyd3123592013-11-12 11:46:22 -060094
95 *backlog = NULL;
96
Tom Lendackybc385442014-01-24 16:18:08 -060097 spin_lock_irqsave(&req_queue_lock, flags);
Tom Lendackyd3123592013-11-12 11:46:22 -060098
99 /* Held cmds will be after the current cmd in the queue so start
100 * searching for a cmd with a matching tfm for submission.
101 */
102 tmp = crypto_cmd;
Tom Lendackybc385442014-01-24 16:18:08 -0600103 list_for_each_entry_continue(tmp, &req_queue.cmds, entry) {
Tom Lendackyd3123592013-11-12 11:46:22 -0600104 if (crypto_cmd->tfm != tmp->tfm)
105 continue;
106 held = tmp;
107 break;
108 }
109
110 /* Process the backlog:
111 * Because cmds can be executed from any point in the cmd list
112 * special precautions have to be taken when handling the backlog.
113 */
Tom Lendackybc385442014-01-24 16:18:08 -0600114 if (req_queue.backlog != &req_queue.cmds) {
Tom Lendackyd3123592013-11-12 11:46:22 -0600115 /* Skip over this cmd if it is the next backlog cmd */
Tom Lendackybc385442014-01-24 16:18:08 -0600116 if (req_queue.backlog == &crypto_cmd->entry)
117 req_queue.backlog = crypto_cmd->entry.next;
Tom Lendackyd3123592013-11-12 11:46:22 -0600118
Tom Lendackybc385442014-01-24 16:18:08 -0600119 *backlog = container_of(req_queue.backlog,
Tom Lendackyd3123592013-11-12 11:46:22 -0600120 struct ccp_crypto_cmd, entry);
Tom Lendackybc385442014-01-24 16:18:08 -0600121 req_queue.backlog = req_queue.backlog->next;
Tom Lendackyd3123592013-11-12 11:46:22 -0600122
123 /* Skip over this cmd if it is now the next backlog cmd */
Tom Lendackybc385442014-01-24 16:18:08 -0600124 if (req_queue.backlog == &crypto_cmd->entry)
125 req_queue.backlog = crypto_cmd->entry.next;
Tom Lendackyd3123592013-11-12 11:46:22 -0600126 }
127
128 /* Remove the cmd entry from the list of cmds */
Tom Lendackybc385442014-01-24 16:18:08 -0600129 req_queue.cmd_count--;
Tom Lendackyd3123592013-11-12 11:46:22 -0600130 list_del(&crypto_cmd->entry);
131
Tom Lendackybc385442014-01-24 16:18:08 -0600132 spin_unlock_irqrestore(&req_queue_lock, flags);
133
Tom Lendackyd3123592013-11-12 11:46:22 -0600134 return held;
135}
136
Tom Lendackybc385442014-01-24 16:18:08 -0600137static void ccp_crypto_complete(void *data, int err)
Tom Lendackyd3123592013-11-12 11:46:22 -0600138{
Tom Lendackybc385442014-01-24 16:18:08 -0600139 struct ccp_crypto_cmd *crypto_cmd = data;
Tom Lendackyd3123592013-11-12 11:46:22 -0600140 struct ccp_crypto_cmd *held, *next, *backlog;
141 struct crypto_async_request *req = crypto_cmd->req;
142 struct ccp_ctx *ctx = crypto_tfm_ctx(req->tfm);
Tom Lendackybc385442014-01-24 16:18:08 -0600143 int ret;
Tom Lendackyd3123592013-11-12 11:46:22 -0600144
Tom Lendackybc385442014-01-24 16:18:08 -0600145 if (err == -EINPROGRESS) {
Tom Lendackyd3123592013-11-12 11:46:22 -0600146 /* Only propogate the -EINPROGRESS if necessary */
147 if (crypto_cmd->ret == -EBUSY) {
148 crypto_cmd->ret = -EINPROGRESS;
149 req->complete(req, -EINPROGRESS);
150 }
151
Tom Lendackybc385442014-01-24 16:18:08 -0600152 return;
Tom Lendackyd3123592013-11-12 11:46:22 -0600153 }
154
155 /* Operation has completed - update the queue before invoking
156 * the completion callbacks and retrieve the next cmd (cmd with
157 * a matching tfm) that can be submitted to the CCP.
158 */
159 held = ccp_crypto_cmd_complete(crypto_cmd, &backlog);
160 if (backlog) {
161 backlog->ret = -EINPROGRESS;
162 backlog->req->complete(backlog->req, -EINPROGRESS);
163 }
164
165 /* Transition the state from -EBUSY to -EINPROGRESS first */
166 if (crypto_cmd->ret == -EBUSY)
167 req->complete(req, -EINPROGRESS);
168
169 /* Completion callbacks */
Tom Lendackybc385442014-01-24 16:18:08 -0600170 ret = err;
Tom Lendackyd3123592013-11-12 11:46:22 -0600171 if (ctx->complete)
172 ret = ctx->complete(req, ret);
173 req->complete(req, ret);
174
175 /* Submit the next cmd */
176 while (held) {
Tom Lendacky06114512014-02-24 08:42:02 -0600177 /* Since we have already queued the cmd, we must indicate that
178 * we can backlog so as not to "lose" this request.
179 */
180 held->cmd->flags |= CCP_CMD_MAY_BACKLOG;
Tom Lendackyd3123592013-11-12 11:46:22 -0600181 ret = ccp_enqueue_cmd(held->cmd);
182 if (ccp_crypto_success(ret))
183 break;
184
185 /* Error occurred, report it and get the next entry */
Tom Lendacky950b10b2014-02-24 08:42:08 -0600186 ctx = crypto_tfm_ctx(held->req->tfm);
187 if (ctx->complete)
188 ret = ctx->complete(held->req, ret);
Tom Lendackyd3123592013-11-12 11:46:22 -0600189 held->req->complete(held->req, ret);
190
191 next = ccp_crypto_cmd_complete(held, &backlog);
192 if (backlog) {
193 backlog->ret = -EINPROGRESS;
194 backlog->req->complete(backlog->req, -EINPROGRESS);
195 }
196
197 kfree(held);
198 held = next;
199 }
200
201 kfree(crypto_cmd);
Tom Lendackyd3123592013-11-12 11:46:22 -0600202}
203
204static int ccp_crypto_enqueue_cmd(struct ccp_crypto_cmd *crypto_cmd)
205{
Tom Lendackyd3123592013-11-12 11:46:22 -0600206 struct ccp_crypto_cmd *active = NULL, *tmp;
Tom Lendackybc385442014-01-24 16:18:08 -0600207 unsigned long flags;
208 int ret;
Tom Lendackyd3123592013-11-12 11:46:22 -0600209
Tom Lendackybc385442014-01-24 16:18:08 -0600210 spin_lock_irqsave(&req_queue_lock, flags);
Tom Lendackyd3123592013-11-12 11:46:22 -0600211
212 /* Check if the cmd can/should be queued */
Tom Lendackybc385442014-01-24 16:18:08 -0600213 if (req_queue.cmd_count >= CCP_CRYPTO_MAX_QLEN) {
Tom Lendackyd3123592013-11-12 11:46:22 -0600214 ret = -EBUSY;
215 if (!(crypto_cmd->cmd->flags & CCP_CMD_MAY_BACKLOG))
Tom Lendackybc385442014-01-24 16:18:08 -0600216 goto e_lock;
Tom Lendackyd3123592013-11-12 11:46:22 -0600217 }
218
219 /* Look for an entry with the same tfm. If there is a cmd
Tom Lendackybc385442014-01-24 16:18:08 -0600220 * with the same tfm in the list then the current cmd cannot
221 * be submitted to the CCP yet.
Tom Lendackyd3123592013-11-12 11:46:22 -0600222 */
Tom Lendackybc385442014-01-24 16:18:08 -0600223 list_for_each_entry(tmp, &req_queue.cmds, entry) {
Tom Lendackyd3123592013-11-12 11:46:22 -0600224 if (crypto_cmd->tfm != tmp->tfm)
225 continue;
226 active = tmp;
227 break;
228 }
229
230 ret = -EINPROGRESS;
231 if (!active) {
232 ret = ccp_enqueue_cmd(crypto_cmd->cmd);
233 if (!ccp_crypto_success(ret))
Tom Lendackybc385442014-01-24 16:18:08 -0600234 goto e_lock;
Tom Lendackyd3123592013-11-12 11:46:22 -0600235 }
236
Tom Lendackybc385442014-01-24 16:18:08 -0600237 if (req_queue.cmd_count >= CCP_CRYPTO_MAX_QLEN) {
Tom Lendackyd3123592013-11-12 11:46:22 -0600238 ret = -EBUSY;
Tom Lendackybc385442014-01-24 16:18:08 -0600239 if (req_queue.backlog == &req_queue.cmds)
240 req_queue.backlog = &crypto_cmd->entry;
Tom Lendackyd3123592013-11-12 11:46:22 -0600241 }
242 crypto_cmd->ret = ret;
243
Tom Lendackybc385442014-01-24 16:18:08 -0600244 req_queue.cmd_count++;
245 list_add_tail(&crypto_cmd->entry, &req_queue.cmds);
Tom Lendackyd3123592013-11-12 11:46:22 -0600246
Tom Lendackybc385442014-01-24 16:18:08 -0600247e_lock:
248 spin_unlock_irqrestore(&req_queue_lock, flags);
Tom Lendackyd3123592013-11-12 11:46:22 -0600249
250 return ret;
251}
252
253/**
254 * ccp_crypto_enqueue_request - queue an crypto async request for processing
255 * by the CCP
256 *
257 * @req: crypto_async_request struct to be processed
258 * @cmd: ccp_cmd struct to be sent to the CCP
259 */
260int ccp_crypto_enqueue_request(struct crypto_async_request *req,
261 struct ccp_cmd *cmd)
262{
263 struct ccp_crypto_cmd *crypto_cmd;
264 gfp_t gfp;
265 int ret;
266
267 gfp = req->flags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL : GFP_ATOMIC;
268
269 crypto_cmd = kzalloc(sizeof(*crypto_cmd), gfp);
270 if (!crypto_cmd)
271 return -ENOMEM;
272
273 /* The tfm pointer must be saved and not referenced from the
274 * crypto_async_request (req) pointer because it is used after
275 * completion callback for the request and the req pointer
276 * might not be valid anymore.
277 */
278 crypto_cmd->cmd = cmd;
279 crypto_cmd->req = req;
280 crypto_cmd->tfm = req->tfm;
281
282 cmd->callback = ccp_crypto_complete;
283 cmd->data = crypto_cmd;
284
285 if (req->flags & CRYPTO_TFM_REQ_MAY_BACKLOG)
286 cmd->flags |= CCP_CMD_MAY_BACKLOG;
287 else
288 cmd->flags &= ~CCP_CMD_MAY_BACKLOG;
289
290 ret = ccp_crypto_enqueue_cmd(crypto_cmd);
291 if (!ccp_crypto_success(ret))
292 kfree(crypto_cmd);
293
294 return ret;
295}
296
297struct scatterlist *ccp_crypto_sg_table_add(struct sg_table *table,
298 struct scatterlist *sg_add)
299{
300 struct scatterlist *sg, *sg_last = NULL;
301
302 for (sg = table->sgl; sg; sg = sg_next(sg))
303 if (!sg_page(sg))
304 break;
305 BUG_ON(!sg);
306
307 for (; sg && sg_add; sg = sg_next(sg), sg_add = sg_next(sg_add)) {
308 sg_set_page(sg, sg_page(sg_add), sg_add->length,
309 sg_add->offset);
310 sg_last = sg;
311 }
312 BUG_ON(sg_add);
313
314 return sg_last;
315}
316
317static int ccp_register_algs(void)
318{
319 int ret;
320
Tom Lendackyd81ed652014-01-24 16:17:56 -0600321 if (!aes_disable) {
322 ret = ccp_register_aes_algs(&cipher_algs);
323 if (ret)
324 return ret;
Tom Lendackyd3123592013-11-12 11:46:22 -0600325
Tom Lendackyd81ed652014-01-24 16:17:56 -0600326 ret = ccp_register_aes_cmac_algs(&hash_algs);
327 if (ret)
328 return ret;
Tom Lendackyd3123592013-11-12 11:46:22 -0600329
Tom Lendackyd81ed652014-01-24 16:17:56 -0600330 ret = ccp_register_aes_xts_algs(&cipher_algs);
331 if (ret)
332 return ret;
333 }
Tom Lendackyd3123592013-11-12 11:46:22 -0600334
Tom Lendackyd81ed652014-01-24 16:17:56 -0600335 if (!sha_disable) {
336 ret = ccp_register_sha_algs(&hash_algs);
337 if (ret)
338 return ret;
339 }
Tom Lendackyd3123592013-11-12 11:46:22 -0600340
341 return 0;
342}
343
344static void ccp_unregister_algs(void)
345{
346 struct ccp_crypto_ahash_alg *ahash_alg, *ahash_tmp;
347 struct ccp_crypto_ablkcipher_alg *ablk_alg, *ablk_tmp;
348
349 list_for_each_entry_safe(ahash_alg, ahash_tmp, &hash_algs, entry) {
350 crypto_unregister_ahash(&ahash_alg->alg);
351 list_del(&ahash_alg->entry);
352 kfree(ahash_alg);
353 }
354
355 list_for_each_entry_safe(ablk_alg, ablk_tmp, &cipher_algs, entry) {
356 crypto_unregister_alg(&ablk_alg->alg);
357 list_del(&ablk_alg->entry);
358 kfree(ablk_alg);
359 }
360}
361
Tom Lendackyd3123592013-11-12 11:46:22 -0600362static int ccp_crypto_init(void)
363{
364 int ret;
365
Tom Lendackybc385442014-01-24 16:18:08 -0600366 spin_lock_init(&req_queue_lock);
367 INIT_LIST_HEAD(&req_queue.cmds);
368 req_queue.backlog = &req_queue.cmds;
369 req_queue.cmd_count = 0;
Tom Lendackyd3123592013-11-12 11:46:22 -0600370
371 ret = ccp_register_algs();
Tom Lendackybc385442014-01-24 16:18:08 -0600372 if (ret)
Tom Lendackyd3123592013-11-12 11:46:22 -0600373 ccp_unregister_algs();
Tom Lendackyd3123592013-11-12 11:46:22 -0600374
375 return ret;
376}
377
378static void ccp_crypto_exit(void)
379{
380 ccp_unregister_algs();
Tom Lendackyd3123592013-11-12 11:46:22 -0600381}
382
383module_init(ccp_crypto_init);
384module_exit(ccp_crypto_exit);