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