xref: /openbmc/linux/drivers/crypto/hisilicon/sec2/sec_crypto.c (revision b694e3c604e999343258c49e574abd7be012e726)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019 HiSilicon Limited. */
3 
4 #include <crypto/aes.h>
5 #include <crypto/aead.h>
6 #include <crypto/algapi.h>
7 #include <crypto/authenc.h>
8 #include <crypto/des.h>
9 #include <crypto/hash.h>
10 #include <crypto/internal/aead.h>
11 #include <crypto/internal/des.h>
12 #include <crypto/sha1.h>
13 #include <crypto/sha2.h>
14 #include <crypto/skcipher.h>
15 #include <crypto/xts.h>
16 #include <linux/crypto.h>
17 #include <linux/dma-mapping.h>
18 #include <linux/idr.h>
19 
20 #include "sec.h"
21 #include "sec_crypto.h"
22 
23 #define SEC_PRIORITY		4001
24 #define SEC_XTS_MIN_KEY_SIZE	(2 * AES_MIN_KEY_SIZE)
25 #define SEC_XTS_MID_KEY_SIZE	(3 * AES_MIN_KEY_SIZE)
26 #define SEC_XTS_MAX_KEY_SIZE	(2 * AES_MAX_KEY_SIZE)
27 #define SEC_DES3_2KEY_SIZE	(2 * DES_KEY_SIZE)
28 #define SEC_DES3_3KEY_SIZE	(3 * DES_KEY_SIZE)
29 
30 /* SEC sqe(bd) bit operational relative MACRO */
31 #define SEC_DE_OFFSET		1
32 #define SEC_CIPHER_OFFSET	4
33 #define SEC_SCENE_OFFSET	3
34 #define SEC_DST_SGL_OFFSET	2
35 #define SEC_SRC_SGL_OFFSET	7
36 #define SEC_CKEY_OFFSET		9
37 #define SEC_CMODE_OFFSET	12
38 #define SEC_AKEY_OFFSET         5
39 #define SEC_AEAD_ALG_OFFSET     11
40 #define SEC_AUTH_OFFSET		6
41 
42 #define SEC_DE_OFFSET_V3		9
43 #define SEC_SCENE_OFFSET_V3	5
44 #define SEC_CKEY_OFFSET_V3	13
45 #define SEC_CTR_CNT_OFFSET	25
46 #define SEC_CTR_CNT_ROLLOVER	2
47 #define SEC_SRC_SGL_OFFSET_V3	11
48 #define SEC_DST_SGL_OFFSET_V3	14
49 #define SEC_CALG_OFFSET_V3	4
50 #define SEC_AKEY_OFFSET_V3	9
51 #define SEC_MAC_OFFSET_V3	4
52 #define SEC_AUTH_ALG_OFFSET_V3	15
53 #define SEC_CIPHER_AUTH_V3	0xbf
54 #define SEC_AUTH_CIPHER_V3	0x40
55 #define SEC_FLAG_OFFSET		7
56 #define SEC_FLAG_MASK		0x0780
57 #define SEC_TYPE_MASK		0x0F
58 #define SEC_DONE_MASK		0x0001
59 #define SEC_ICV_MASK		0x000E
60 
61 #define SEC_TOTAL_IV_SZ(depth)	(SEC_IV_SIZE * (depth))
62 #define SEC_SGL_SGE_NR		128
63 #define SEC_CIPHER_AUTH		0xfe
64 #define SEC_AUTH_CIPHER		0x1
65 #define SEC_MAX_MAC_LEN		64
66 #define SEC_MAX_AAD_LEN		65535
67 #define SEC_MAX_CCM_AAD_LEN	65279
68 #define SEC_TOTAL_MAC_SZ(depth) (SEC_MAX_MAC_LEN * (depth))
69 
70 #define SEC_PBUF_SZ			512
71 #define SEC_PBUF_IV_OFFSET		SEC_PBUF_SZ
72 #define SEC_PBUF_MAC_OFFSET		(SEC_PBUF_SZ + SEC_IV_SIZE)
73 #define SEC_PBUF_PKG		(SEC_PBUF_SZ + SEC_IV_SIZE +	\
74 			SEC_MAX_MAC_LEN * 2)
75 #define SEC_PBUF_NUM		(PAGE_SIZE / SEC_PBUF_PKG)
76 #define SEC_PBUF_PAGE_NUM(depth)	((depth) / SEC_PBUF_NUM)
77 #define SEC_PBUF_LEFT_SZ(depth)		(SEC_PBUF_PKG * ((depth) -	\
78 				SEC_PBUF_PAGE_NUM(depth) * SEC_PBUF_NUM))
79 #define SEC_TOTAL_PBUF_SZ(depth)	(PAGE_SIZE * SEC_PBUF_PAGE_NUM(depth) +	\
80 				SEC_PBUF_LEFT_SZ(depth))
81 
82 #define SEC_SQE_CFLAG		2
83 #define SEC_SQE_AEAD_FLAG	3
84 #define SEC_SQE_DONE		0x1
85 #define SEC_ICV_ERR		0x2
86 #define MAC_LEN_MASK		0x1U
87 #define MAX_INPUT_DATA_LEN	0xFFFE00
88 #define BITS_MASK		0xFF
89 #define WORD_MASK		0x3
90 #define BYTE_BITS		0x8
91 #define BYTES_TO_WORDS(bcount)	((bcount) >> 2)
92 #define SEC_XTS_NAME_SZ		0x3
93 #define IV_CM_CAL_NUM		2
94 #define IV_CL_MASK		0x7
95 #define IV_CL_MIN		2
96 #define IV_CL_MID		4
97 #define IV_CL_MAX		8
98 #define IV_FLAGS_OFFSET	0x6
99 #define IV_CM_OFFSET		0x3
100 #define IV_LAST_BYTE1		1
101 #define IV_LAST_BYTE2		2
102 #define IV_LAST_BYTE_MASK	0xFF
103 #define IV_CTR_INIT		0x1
104 #define IV_BYTE_OFFSET		0x8
105 
106 struct sec_skcipher {
107 	u64 alg_msk;
108 	struct skcipher_alg alg;
109 };
110 
111 struct sec_aead {
112 	u64 alg_msk;
113 	struct aead_alg alg;
114 };
115 
116 /* Get an en/de-cipher queue cyclically to balance load over queues of TFM */
sec_alloc_queue_id(struct sec_ctx * ctx,struct sec_req * req)117 static inline int sec_alloc_queue_id(struct sec_ctx *ctx, struct sec_req *req)
118 {
119 	if (req->c_req.encrypt)
120 		return (u32)atomic_inc_return(&ctx->enc_qcyclic) %
121 				 ctx->hlf_q_num;
122 
123 	return (u32)atomic_inc_return(&ctx->dec_qcyclic) % ctx->hlf_q_num +
124 				 ctx->hlf_q_num;
125 }
126 
sec_free_queue_id(struct sec_ctx * ctx,struct sec_req * req)127 static inline void sec_free_queue_id(struct sec_ctx *ctx, struct sec_req *req)
128 {
129 	if (req->c_req.encrypt)
130 		atomic_dec(&ctx->enc_qcyclic);
131 	else
132 		atomic_dec(&ctx->dec_qcyclic);
133 }
134 
sec_alloc_req_id(struct sec_req * req,struct sec_qp_ctx * qp_ctx)135 static int sec_alloc_req_id(struct sec_req *req, struct sec_qp_ctx *qp_ctx)
136 {
137 	int req_id;
138 
139 	spin_lock_bh(&qp_ctx->req_lock);
140 	req_id = idr_alloc_cyclic(&qp_ctx->req_idr, NULL, 0, qp_ctx->qp->sq_depth, GFP_ATOMIC);
141 	spin_unlock_bh(&qp_ctx->req_lock);
142 	if (unlikely(req_id < 0)) {
143 		dev_err(req->ctx->dev, "alloc req id fail!\n");
144 		return req_id;
145 	}
146 
147 	req->qp_ctx = qp_ctx;
148 	qp_ctx->req_list[req_id] = req;
149 
150 	return req_id;
151 }
152 
sec_free_req_id(struct sec_req * req)153 static void sec_free_req_id(struct sec_req *req)
154 {
155 	struct sec_qp_ctx *qp_ctx = req->qp_ctx;
156 	int req_id = req->req_id;
157 
158 	if (unlikely(req_id < 0 || req_id >= qp_ctx->qp->sq_depth)) {
159 		dev_err(req->ctx->dev, "free request id invalid!\n");
160 		return;
161 	}
162 
163 	qp_ctx->req_list[req_id] = NULL;
164 	req->qp_ctx = NULL;
165 
166 	spin_lock_bh(&qp_ctx->req_lock);
167 	idr_remove(&qp_ctx->req_idr, req_id);
168 	spin_unlock_bh(&qp_ctx->req_lock);
169 }
170 
pre_parse_finished_bd(struct bd_status * status,void * resp)171 static u8 pre_parse_finished_bd(struct bd_status *status, void *resp)
172 {
173 	struct sec_sqe *bd = resp;
174 
175 	status->done = le16_to_cpu(bd->type2.done_flag) & SEC_DONE_MASK;
176 	status->icv = (le16_to_cpu(bd->type2.done_flag) & SEC_ICV_MASK) >> 1;
177 	status->flag = (le16_to_cpu(bd->type2.done_flag) &
178 					SEC_FLAG_MASK) >> SEC_FLAG_OFFSET;
179 	status->tag = le16_to_cpu(bd->type2.tag);
180 	status->err_type = bd->type2.error_type;
181 
182 	return bd->type_cipher_auth & SEC_TYPE_MASK;
183 }
184 
pre_parse_finished_bd3(struct bd_status * status,void * resp)185 static u8 pre_parse_finished_bd3(struct bd_status *status, void *resp)
186 {
187 	struct sec_sqe3 *bd3 = resp;
188 
189 	status->done = le16_to_cpu(bd3->done_flag) & SEC_DONE_MASK;
190 	status->icv = (le16_to_cpu(bd3->done_flag) & SEC_ICV_MASK) >> 1;
191 	status->flag = (le16_to_cpu(bd3->done_flag) &
192 					SEC_FLAG_MASK) >> SEC_FLAG_OFFSET;
193 	status->tag = le64_to_cpu(bd3->tag);
194 	status->err_type = bd3->error_type;
195 
196 	return le32_to_cpu(bd3->bd_param) & SEC_TYPE_MASK;
197 }
198 
sec_cb_status_check(struct sec_req * req,struct bd_status * status)199 static int sec_cb_status_check(struct sec_req *req,
200 			       struct bd_status *status)
201 {
202 	struct sec_ctx *ctx = req->ctx;
203 
204 	if (unlikely(req->err_type || status->done != SEC_SQE_DONE)) {
205 		dev_err_ratelimited(ctx->dev, "err_type[%d], done[%u]\n",
206 				    req->err_type, status->done);
207 		return -EIO;
208 	}
209 
210 	if (unlikely(ctx->alg_type == SEC_SKCIPHER)) {
211 		if (unlikely(status->flag != SEC_SQE_CFLAG)) {
212 			dev_err_ratelimited(ctx->dev, "flag[%u]\n",
213 					    status->flag);
214 			return -EIO;
215 		}
216 	} else if (unlikely(ctx->alg_type == SEC_AEAD)) {
217 		if (unlikely(status->flag != SEC_SQE_AEAD_FLAG ||
218 			     status->icv == SEC_ICV_ERR)) {
219 			dev_err_ratelimited(ctx->dev,
220 					    "flag[%u], icv[%u]\n",
221 					    status->flag, status->icv);
222 			return -EBADMSG;
223 		}
224 	}
225 
226 	return 0;
227 }
228 
sec_req_cb(struct hisi_qp * qp,void * resp)229 static void sec_req_cb(struct hisi_qp *qp, void *resp)
230 {
231 	struct sec_qp_ctx *qp_ctx = qp->qp_ctx;
232 	struct sec_dfx *dfx = &qp_ctx->ctx->sec->debug.dfx;
233 	u8 type_supported = qp_ctx->ctx->type_supported;
234 	struct bd_status status;
235 	struct sec_ctx *ctx;
236 	struct sec_req *req;
237 	int err;
238 	u8 type;
239 
240 	if (type_supported == SEC_BD_TYPE2) {
241 		type = pre_parse_finished_bd(&status, resp);
242 		req = qp_ctx->req_list[status.tag];
243 	} else {
244 		type = pre_parse_finished_bd3(&status, resp);
245 		req = (void *)(uintptr_t)status.tag;
246 	}
247 
248 	if (unlikely(type != type_supported)) {
249 		atomic64_inc(&dfx->err_bd_cnt);
250 		pr_err("err bd type [%u]\n", type);
251 		return;
252 	}
253 
254 	if (unlikely(!req)) {
255 		atomic64_inc(&dfx->invalid_req_cnt);
256 		atomic_inc(&qp->qp_status.used);
257 		return;
258 	}
259 
260 	req->err_type = status.err_type;
261 	ctx = req->ctx;
262 	err = sec_cb_status_check(req, &status);
263 	if (err)
264 		atomic64_inc(&dfx->done_flag_cnt);
265 
266 	atomic64_inc(&dfx->recv_cnt);
267 
268 	ctx->req_op->buf_unmap(ctx, req);
269 
270 	ctx->req_op->callback(ctx, req, err);
271 }
272 
sec_bd_send(struct sec_ctx * ctx,struct sec_req * req)273 static int sec_bd_send(struct sec_ctx *ctx, struct sec_req *req)
274 {
275 	struct sec_qp_ctx *qp_ctx = req->qp_ctx;
276 	int ret;
277 
278 	if (ctx->fake_req_limit <=
279 	    atomic_read(&qp_ctx->qp->qp_status.used) &&
280 	    !(req->flag & CRYPTO_TFM_REQ_MAY_BACKLOG))
281 		return -EBUSY;
282 
283 	spin_lock_bh(&qp_ctx->req_lock);
284 	ret = hisi_qp_send(qp_ctx->qp, &req->sec_sqe);
285 	if (ctx->fake_req_limit <=
286 	    atomic_read(&qp_ctx->qp->qp_status.used) && !ret) {
287 		list_add_tail(&req->backlog_head, &qp_ctx->backlog);
288 		atomic64_inc(&ctx->sec->debug.dfx.send_cnt);
289 		atomic64_inc(&ctx->sec->debug.dfx.send_busy_cnt);
290 		spin_unlock_bh(&qp_ctx->req_lock);
291 		return -EBUSY;
292 	}
293 	spin_unlock_bh(&qp_ctx->req_lock);
294 
295 	if (unlikely(ret == -EBUSY))
296 		return -ENOBUFS;
297 
298 	if (likely(!ret)) {
299 		ret = -EINPROGRESS;
300 		atomic64_inc(&ctx->sec->debug.dfx.send_cnt);
301 	}
302 
303 	return ret;
304 }
305 
306 /* Get DMA memory resources */
sec_alloc_civ_resource(struct device * dev,struct sec_alg_res * res)307 static int sec_alloc_civ_resource(struct device *dev, struct sec_alg_res *res)
308 {
309 	u16 q_depth = res->depth;
310 	int i;
311 
312 	res->c_ivin = dma_alloc_coherent(dev, SEC_TOTAL_IV_SZ(q_depth),
313 					 &res->c_ivin_dma, GFP_KERNEL);
314 	if (!res->c_ivin)
315 		return -ENOMEM;
316 
317 	for (i = 1; i < q_depth; i++) {
318 		res[i].c_ivin_dma = res->c_ivin_dma + i * SEC_IV_SIZE;
319 		res[i].c_ivin = res->c_ivin + i * SEC_IV_SIZE;
320 	}
321 
322 	return 0;
323 }
324 
sec_free_civ_resource(struct device * dev,struct sec_alg_res * res)325 static void sec_free_civ_resource(struct device *dev, struct sec_alg_res *res)
326 {
327 	if (res->c_ivin)
328 		dma_free_coherent(dev, SEC_TOTAL_IV_SZ(res->depth),
329 				  res->c_ivin, res->c_ivin_dma);
330 }
331 
sec_alloc_aiv_resource(struct device * dev,struct sec_alg_res * res)332 static int sec_alloc_aiv_resource(struct device *dev, struct sec_alg_res *res)
333 {
334 	u16 q_depth = res->depth;
335 	int i;
336 
337 	res->a_ivin = dma_alloc_coherent(dev, SEC_TOTAL_IV_SZ(q_depth),
338 					 &res->a_ivin_dma, GFP_KERNEL);
339 	if (!res->a_ivin)
340 		return -ENOMEM;
341 
342 	for (i = 1; i < q_depth; i++) {
343 		res[i].a_ivin_dma = res->a_ivin_dma + i * SEC_IV_SIZE;
344 		res[i].a_ivin = res->a_ivin + i * SEC_IV_SIZE;
345 	}
346 
347 	return 0;
348 }
349 
sec_free_aiv_resource(struct device * dev,struct sec_alg_res * res)350 static void sec_free_aiv_resource(struct device *dev, struct sec_alg_res *res)
351 {
352 	if (res->a_ivin)
353 		dma_free_coherent(dev, SEC_TOTAL_IV_SZ(res->depth),
354 				  res->a_ivin, res->a_ivin_dma);
355 }
356 
sec_alloc_mac_resource(struct device * dev,struct sec_alg_res * res)357 static int sec_alloc_mac_resource(struct device *dev, struct sec_alg_res *res)
358 {
359 	u16 q_depth = res->depth;
360 	int i;
361 
362 	res->out_mac = dma_alloc_coherent(dev, SEC_TOTAL_MAC_SZ(q_depth) << 1,
363 					  &res->out_mac_dma, GFP_KERNEL);
364 	if (!res->out_mac)
365 		return -ENOMEM;
366 
367 	for (i = 1; i < q_depth; i++) {
368 		res[i].out_mac_dma = res->out_mac_dma +
369 				     i * (SEC_MAX_MAC_LEN << 1);
370 		res[i].out_mac = res->out_mac + i * (SEC_MAX_MAC_LEN << 1);
371 	}
372 
373 	return 0;
374 }
375 
sec_free_mac_resource(struct device * dev,struct sec_alg_res * res)376 static void sec_free_mac_resource(struct device *dev, struct sec_alg_res *res)
377 {
378 	if (res->out_mac)
379 		dma_free_coherent(dev, SEC_TOTAL_MAC_SZ(res->depth) << 1,
380 				  res->out_mac, res->out_mac_dma);
381 }
382 
sec_free_pbuf_resource(struct device * dev,struct sec_alg_res * res)383 static void sec_free_pbuf_resource(struct device *dev, struct sec_alg_res *res)
384 {
385 	if (res->pbuf)
386 		dma_free_coherent(dev, SEC_TOTAL_PBUF_SZ(res->depth),
387 				  res->pbuf, res->pbuf_dma);
388 }
389 
390 /*
391  * To improve performance, pbuffer is used for
392  * small packets (< 512Bytes) as IOMMU translation using.
393  */
sec_alloc_pbuf_resource(struct device * dev,struct sec_alg_res * res)394 static int sec_alloc_pbuf_resource(struct device *dev, struct sec_alg_res *res)
395 {
396 	u16 q_depth = res->depth;
397 	int size = SEC_PBUF_PAGE_NUM(q_depth);
398 	int pbuf_page_offset;
399 	int i, j, k;
400 
401 	res->pbuf = dma_alloc_coherent(dev, SEC_TOTAL_PBUF_SZ(q_depth),
402 				&res->pbuf_dma, GFP_KERNEL);
403 	if (!res->pbuf)
404 		return -ENOMEM;
405 
406 	/*
407 	 * SEC_PBUF_PKG contains data pbuf, iv and
408 	 * out_mac : <SEC_PBUF|SEC_IV|SEC_MAC>
409 	 * Every PAGE contains six SEC_PBUF_PKG
410 	 * The sec_qp_ctx contains QM_Q_DEPTH numbers of SEC_PBUF_PKG
411 	 * So we need SEC_PBUF_PAGE_NUM numbers of PAGE
412 	 * for the SEC_TOTAL_PBUF_SZ
413 	 */
414 	for (i = 0; i <= size; i++) {
415 		pbuf_page_offset = PAGE_SIZE * i;
416 		for (j = 0; j < SEC_PBUF_NUM; j++) {
417 			k = i * SEC_PBUF_NUM + j;
418 			if (k == q_depth)
419 				break;
420 			res[k].pbuf = res->pbuf +
421 				j * SEC_PBUF_PKG + pbuf_page_offset;
422 			res[k].pbuf_dma = res->pbuf_dma +
423 				j * SEC_PBUF_PKG + pbuf_page_offset;
424 		}
425 	}
426 
427 	return 0;
428 }
429 
sec_alg_resource_alloc(struct sec_ctx * ctx,struct sec_qp_ctx * qp_ctx)430 static int sec_alg_resource_alloc(struct sec_ctx *ctx,
431 				  struct sec_qp_ctx *qp_ctx)
432 {
433 	struct sec_alg_res *res = qp_ctx->res;
434 	struct device *dev = ctx->dev;
435 	int ret;
436 
437 	ret = sec_alloc_civ_resource(dev, res);
438 	if (ret)
439 		return ret;
440 
441 	if (ctx->alg_type == SEC_AEAD) {
442 		ret = sec_alloc_aiv_resource(dev, res);
443 		if (ret)
444 			goto alloc_aiv_fail;
445 
446 		ret = sec_alloc_mac_resource(dev, res);
447 		if (ret)
448 			goto alloc_mac_fail;
449 	}
450 	if (ctx->pbuf_supported) {
451 		ret = sec_alloc_pbuf_resource(dev, res);
452 		if (ret) {
453 			dev_err(dev, "fail to alloc pbuf dma resource!\n");
454 			goto alloc_pbuf_fail;
455 		}
456 	}
457 
458 	return 0;
459 
460 alloc_pbuf_fail:
461 	if (ctx->alg_type == SEC_AEAD)
462 		sec_free_mac_resource(dev, qp_ctx->res);
463 alloc_mac_fail:
464 	if (ctx->alg_type == SEC_AEAD)
465 		sec_free_aiv_resource(dev, res);
466 alloc_aiv_fail:
467 	sec_free_civ_resource(dev, res);
468 	return ret;
469 }
470 
sec_alg_resource_free(struct sec_ctx * ctx,struct sec_qp_ctx * qp_ctx)471 static void sec_alg_resource_free(struct sec_ctx *ctx,
472 				  struct sec_qp_ctx *qp_ctx)
473 {
474 	struct device *dev = ctx->dev;
475 
476 	sec_free_civ_resource(dev, qp_ctx->res);
477 
478 	if (ctx->pbuf_supported)
479 		sec_free_pbuf_resource(dev, qp_ctx->res);
480 	if (ctx->alg_type == SEC_AEAD) {
481 		sec_free_mac_resource(dev, qp_ctx->res);
482 		sec_free_aiv_resource(dev, qp_ctx->res);
483 	}
484 }
485 
sec_alloc_qp_ctx_resource(struct hisi_qm * qm,struct sec_ctx * ctx,struct sec_qp_ctx * qp_ctx)486 static int sec_alloc_qp_ctx_resource(struct hisi_qm *qm, struct sec_ctx *ctx,
487 				     struct sec_qp_ctx *qp_ctx)
488 {
489 	u16 q_depth = qp_ctx->qp->sq_depth;
490 	struct device *dev = ctx->dev;
491 	int ret = -ENOMEM;
492 
493 	qp_ctx->req_list = kcalloc(q_depth, sizeof(struct sec_req *), GFP_KERNEL);
494 	if (!qp_ctx->req_list)
495 		return ret;
496 
497 	qp_ctx->res = kcalloc(q_depth, sizeof(struct sec_alg_res), GFP_KERNEL);
498 	if (!qp_ctx->res)
499 		goto err_free_req_list;
500 	qp_ctx->res->depth = q_depth;
501 
502 	qp_ctx->c_in_pool = hisi_acc_create_sgl_pool(dev, q_depth, SEC_SGL_SGE_NR);
503 	if (IS_ERR(qp_ctx->c_in_pool)) {
504 		dev_err(dev, "fail to create sgl pool for input!\n");
505 		goto err_free_res;
506 	}
507 
508 	qp_ctx->c_out_pool = hisi_acc_create_sgl_pool(dev, q_depth, SEC_SGL_SGE_NR);
509 	if (IS_ERR(qp_ctx->c_out_pool)) {
510 		dev_err(dev, "fail to create sgl pool for output!\n");
511 		goto err_free_c_in_pool;
512 	}
513 
514 	ret = sec_alg_resource_alloc(ctx, qp_ctx);
515 	if (ret)
516 		goto err_free_c_out_pool;
517 
518 	return 0;
519 
520 err_free_c_out_pool:
521 	hisi_acc_free_sgl_pool(dev, qp_ctx->c_out_pool);
522 err_free_c_in_pool:
523 	hisi_acc_free_sgl_pool(dev, qp_ctx->c_in_pool);
524 err_free_res:
525 	kfree(qp_ctx->res);
526 err_free_req_list:
527 	kfree(qp_ctx->req_list);
528 	return ret;
529 }
530 
sec_free_qp_ctx_resource(struct sec_ctx * ctx,struct sec_qp_ctx * qp_ctx)531 static void sec_free_qp_ctx_resource(struct sec_ctx *ctx, struct sec_qp_ctx *qp_ctx)
532 {
533 	struct device *dev = ctx->dev;
534 
535 	sec_alg_resource_free(ctx, qp_ctx);
536 	hisi_acc_free_sgl_pool(dev, qp_ctx->c_out_pool);
537 	hisi_acc_free_sgl_pool(dev, qp_ctx->c_in_pool);
538 	kfree(qp_ctx->res);
539 	kfree(qp_ctx->req_list);
540 }
541 
sec_create_qp_ctx(struct hisi_qm * qm,struct sec_ctx * ctx,int qp_ctx_id,int alg_type)542 static int sec_create_qp_ctx(struct hisi_qm *qm, struct sec_ctx *ctx,
543 			     int qp_ctx_id, int alg_type)
544 {
545 	struct sec_qp_ctx *qp_ctx;
546 	struct hisi_qp *qp;
547 	int ret;
548 
549 	qp_ctx = &ctx->qp_ctx[qp_ctx_id];
550 	qp = ctx->qps[qp_ctx_id];
551 	qp->req_type = 0;
552 	qp->qp_ctx = qp_ctx;
553 	qp_ctx->qp = qp;
554 	qp_ctx->ctx = ctx;
555 
556 	qp->req_cb = sec_req_cb;
557 
558 	spin_lock_init(&qp_ctx->req_lock);
559 	idr_init(&qp_ctx->req_idr);
560 	INIT_LIST_HEAD(&qp_ctx->backlog);
561 
562 	ret = sec_alloc_qp_ctx_resource(qm, ctx, qp_ctx);
563 	if (ret)
564 		goto err_destroy_idr;
565 
566 	ret = hisi_qm_start_qp(qp, 0);
567 	if (ret < 0)
568 		goto err_resource_free;
569 
570 	return 0;
571 
572 err_resource_free:
573 	sec_free_qp_ctx_resource(ctx, qp_ctx);
574 err_destroy_idr:
575 	idr_destroy(&qp_ctx->req_idr);
576 	return ret;
577 }
578 
sec_release_qp_ctx(struct sec_ctx * ctx,struct sec_qp_ctx * qp_ctx)579 static void sec_release_qp_ctx(struct sec_ctx *ctx,
580 			       struct sec_qp_ctx *qp_ctx)
581 {
582 	hisi_qm_stop_qp(qp_ctx->qp);
583 	sec_free_qp_ctx_resource(ctx, qp_ctx);
584 	idr_destroy(&qp_ctx->req_idr);
585 }
586 
sec_ctx_base_init(struct sec_ctx * ctx)587 static int sec_ctx_base_init(struct sec_ctx *ctx)
588 {
589 	struct sec_dev *sec;
590 	int i, ret;
591 
592 	ctx->qps = sec_create_qps();
593 	if (!ctx->qps) {
594 		pr_err("Can not create sec qps!\n");
595 		return -ENODEV;
596 	}
597 
598 	sec = container_of(ctx->qps[0]->qm, struct sec_dev, qm);
599 	ctx->sec = sec;
600 	ctx->dev = &sec->qm.pdev->dev;
601 	ctx->hlf_q_num = sec->ctx_q_num >> 1;
602 
603 	ctx->pbuf_supported = ctx->sec->iommu_used;
604 
605 	/* Half of queue depth is taken as fake requests limit in the queue. */
606 	ctx->fake_req_limit = ctx->qps[0]->sq_depth >> 1;
607 	ctx->qp_ctx = kcalloc(sec->ctx_q_num, sizeof(struct sec_qp_ctx),
608 			      GFP_KERNEL);
609 	if (!ctx->qp_ctx) {
610 		ret = -ENOMEM;
611 		goto err_destroy_qps;
612 	}
613 
614 	for (i = 0; i < sec->ctx_q_num; i++) {
615 		ret = sec_create_qp_ctx(&sec->qm, ctx, i, 0);
616 		if (ret)
617 			goto err_sec_release_qp_ctx;
618 	}
619 
620 	return 0;
621 
622 err_sec_release_qp_ctx:
623 	for (i = i - 1; i >= 0; i--)
624 		sec_release_qp_ctx(ctx, &ctx->qp_ctx[i]);
625 	kfree(ctx->qp_ctx);
626 err_destroy_qps:
627 	sec_destroy_qps(ctx->qps, sec->ctx_q_num);
628 	return ret;
629 }
630 
sec_ctx_base_uninit(struct sec_ctx * ctx)631 static void sec_ctx_base_uninit(struct sec_ctx *ctx)
632 {
633 	int i;
634 
635 	for (i = 0; i < ctx->sec->ctx_q_num; i++)
636 		sec_release_qp_ctx(ctx, &ctx->qp_ctx[i]);
637 
638 	sec_destroy_qps(ctx->qps, ctx->sec->ctx_q_num);
639 	kfree(ctx->qp_ctx);
640 }
641 
sec_cipher_init(struct sec_ctx * ctx)642 static int sec_cipher_init(struct sec_ctx *ctx)
643 {
644 	struct sec_cipher_ctx *c_ctx = &ctx->c_ctx;
645 
646 	c_ctx->c_key = dma_alloc_coherent(ctx->dev, SEC_MAX_KEY_SIZE,
647 					  &c_ctx->c_key_dma, GFP_KERNEL);
648 	if (!c_ctx->c_key)
649 		return -ENOMEM;
650 
651 	return 0;
652 }
653 
sec_cipher_uninit(struct sec_ctx * ctx)654 static void sec_cipher_uninit(struct sec_ctx *ctx)
655 {
656 	struct sec_cipher_ctx *c_ctx = &ctx->c_ctx;
657 
658 	memzero_explicit(c_ctx->c_key, SEC_MAX_KEY_SIZE);
659 	dma_free_coherent(ctx->dev, SEC_MAX_KEY_SIZE,
660 			  c_ctx->c_key, c_ctx->c_key_dma);
661 }
662 
sec_auth_init(struct sec_ctx * ctx)663 static int sec_auth_init(struct sec_ctx *ctx)
664 {
665 	struct sec_auth_ctx *a_ctx = &ctx->a_ctx;
666 
667 	a_ctx->a_key = dma_alloc_coherent(ctx->dev, SEC_MAX_AKEY_SIZE,
668 					  &a_ctx->a_key_dma, GFP_KERNEL);
669 	if (!a_ctx->a_key)
670 		return -ENOMEM;
671 
672 	return 0;
673 }
674 
sec_auth_uninit(struct sec_ctx * ctx)675 static void sec_auth_uninit(struct sec_ctx *ctx)
676 {
677 	struct sec_auth_ctx *a_ctx = &ctx->a_ctx;
678 
679 	memzero_explicit(a_ctx->a_key, SEC_MAX_AKEY_SIZE);
680 	dma_free_coherent(ctx->dev, SEC_MAX_AKEY_SIZE,
681 			  a_ctx->a_key, a_ctx->a_key_dma);
682 }
683 
sec_skcipher_fbtfm_init(struct crypto_skcipher * tfm)684 static int sec_skcipher_fbtfm_init(struct crypto_skcipher *tfm)
685 {
686 	const char *alg = crypto_tfm_alg_name(&tfm->base);
687 	struct sec_ctx *ctx = crypto_skcipher_ctx(tfm);
688 	struct sec_cipher_ctx *c_ctx = &ctx->c_ctx;
689 
690 	c_ctx->fallback = false;
691 
692 	c_ctx->fbtfm = crypto_alloc_sync_skcipher(alg, 0,
693 						  CRYPTO_ALG_NEED_FALLBACK);
694 	if (IS_ERR(c_ctx->fbtfm)) {
695 		pr_err("failed to alloc fallback tfm for %s!\n", alg);
696 		return PTR_ERR(c_ctx->fbtfm);
697 	}
698 
699 	return 0;
700 }
701 
sec_skcipher_init(struct crypto_skcipher * tfm)702 static int sec_skcipher_init(struct crypto_skcipher *tfm)
703 {
704 	struct sec_ctx *ctx = crypto_skcipher_ctx(tfm);
705 	int ret;
706 
707 	ctx->alg_type = SEC_SKCIPHER;
708 	crypto_skcipher_set_reqsize(tfm, sizeof(struct sec_req));
709 	ctx->c_ctx.ivsize = crypto_skcipher_ivsize(tfm);
710 	if (ctx->c_ctx.ivsize > SEC_IV_SIZE) {
711 		pr_err("get error skcipher iv size!\n");
712 		return -EINVAL;
713 	}
714 
715 	ret = sec_ctx_base_init(ctx);
716 	if (ret)
717 		return ret;
718 
719 	ret = sec_cipher_init(ctx);
720 	if (ret)
721 		goto err_cipher_init;
722 
723 	ret = sec_skcipher_fbtfm_init(tfm);
724 	if (ret)
725 		goto err_fbtfm_init;
726 
727 	return 0;
728 
729 err_fbtfm_init:
730 	sec_cipher_uninit(ctx);
731 err_cipher_init:
732 	sec_ctx_base_uninit(ctx);
733 	return ret;
734 }
735 
sec_skcipher_uninit(struct crypto_skcipher * tfm)736 static void sec_skcipher_uninit(struct crypto_skcipher *tfm)
737 {
738 	struct sec_ctx *ctx = crypto_skcipher_ctx(tfm);
739 
740 	if (ctx->c_ctx.fbtfm)
741 		crypto_free_sync_skcipher(ctx->c_ctx.fbtfm);
742 
743 	sec_cipher_uninit(ctx);
744 	sec_ctx_base_uninit(ctx);
745 }
746 
sec_skcipher_3des_setkey(struct crypto_skcipher * tfm,const u8 * key,const u32 keylen,const enum sec_cmode c_mode)747 static int sec_skcipher_3des_setkey(struct crypto_skcipher *tfm, const u8 *key,
748 				    const u32 keylen,
749 				    const enum sec_cmode c_mode)
750 {
751 	struct sec_ctx *ctx = crypto_skcipher_ctx(tfm);
752 	struct sec_cipher_ctx *c_ctx = &ctx->c_ctx;
753 	int ret;
754 
755 	ret = verify_skcipher_des3_key(tfm, key);
756 	if (ret)
757 		return ret;
758 
759 	switch (keylen) {
760 	case SEC_DES3_2KEY_SIZE:
761 		c_ctx->c_key_len = SEC_CKEY_3DES_2KEY;
762 		break;
763 	case SEC_DES3_3KEY_SIZE:
764 		c_ctx->c_key_len = SEC_CKEY_3DES_3KEY;
765 		break;
766 	default:
767 		return -EINVAL;
768 	}
769 
770 	return 0;
771 }
772 
sec_skcipher_aes_sm4_setkey(struct sec_cipher_ctx * c_ctx,const u32 keylen,const enum sec_cmode c_mode)773 static int sec_skcipher_aes_sm4_setkey(struct sec_cipher_ctx *c_ctx,
774 				       const u32 keylen,
775 				       const enum sec_cmode c_mode)
776 {
777 	if (c_mode == SEC_CMODE_XTS) {
778 		switch (keylen) {
779 		case SEC_XTS_MIN_KEY_SIZE:
780 			c_ctx->c_key_len = SEC_CKEY_128BIT;
781 			break;
782 		case SEC_XTS_MID_KEY_SIZE:
783 			c_ctx->fallback = true;
784 			break;
785 		case SEC_XTS_MAX_KEY_SIZE:
786 			c_ctx->c_key_len = SEC_CKEY_256BIT;
787 			break;
788 		default:
789 			pr_err("hisi_sec2: xts mode key error!\n");
790 			return -EINVAL;
791 		}
792 	} else {
793 		if (c_ctx->c_alg == SEC_CALG_SM4 &&
794 		    keylen != AES_KEYSIZE_128) {
795 			pr_err("hisi_sec2: sm4 key error!\n");
796 			return -EINVAL;
797 		} else {
798 			switch (keylen) {
799 			case AES_KEYSIZE_128:
800 				c_ctx->c_key_len = SEC_CKEY_128BIT;
801 				break;
802 			case AES_KEYSIZE_192:
803 				c_ctx->c_key_len = SEC_CKEY_192BIT;
804 				break;
805 			case AES_KEYSIZE_256:
806 				c_ctx->c_key_len = SEC_CKEY_256BIT;
807 				break;
808 			default:
809 				pr_err("hisi_sec2: aes key error!\n");
810 				return -EINVAL;
811 			}
812 		}
813 	}
814 
815 	return 0;
816 }
817 
sec_skcipher_setkey(struct crypto_skcipher * tfm,const u8 * key,const u32 keylen,const enum sec_calg c_alg,const enum sec_cmode c_mode)818 static int sec_skcipher_setkey(struct crypto_skcipher *tfm, const u8 *key,
819 			       const u32 keylen, const enum sec_calg c_alg,
820 			       const enum sec_cmode c_mode)
821 {
822 	struct sec_ctx *ctx = crypto_skcipher_ctx(tfm);
823 	struct sec_cipher_ctx *c_ctx = &ctx->c_ctx;
824 	struct device *dev = ctx->dev;
825 	int ret;
826 
827 	if (c_mode == SEC_CMODE_XTS) {
828 		ret = xts_verify_key(tfm, key, keylen);
829 		if (ret) {
830 			dev_err(dev, "xts mode key err!\n");
831 			return ret;
832 		}
833 	}
834 
835 	c_ctx->c_alg  = c_alg;
836 	c_ctx->c_mode = c_mode;
837 
838 	switch (c_alg) {
839 	case SEC_CALG_3DES:
840 		ret = sec_skcipher_3des_setkey(tfm, key, keylen, c_mode);
841 		break;
842 	case SEC_CALG_AES:
843 	case SEC_CALG_SM4:
844 		ret = sec_skcipher_aes_sm4_setkey(c_ctx, keylen, c_mode);
845 		break;
846 	default:
847 		dev_err(dev, "sec c_alg err!\n");
848 		return -EINVAL;
849 	}
850 
851 	if (ret) {
852 		dev_err(dev, "set sec key err!\n");
853 		return ret;
854 	}
855 
856 	memcpy(c_ctx->c_key, key, keylen);
857 	if (c_ctx->fbtfm) {
858 		ret = crypto_sync_skcipher_setkey(c_ctx->fbtfm, key, keylen);
859 		if (ret) {
860 			dev_err(dev, "failed to set fallback skcipher key!\n");
861 			return ret;
862 		}
863 	}
864 	return 0;
865 }
866 
867 #define GEN_SEC_SETKEY_FUNC(name, c_alg, c_mode)			\
868 static int sec_setkey_##name(struct crypto_skcipher *tfm, const u8 *key,\
869 	u32 keylen)							\
870 {									\
871 	return sec_skcipher_setkey(tfm, key, keylen, c_alg, c_mode);	\
872 }
873 
GEN_SEC_SETKEY_FUNC(aes_ecb,SEC_CALG_AES,SEC_CMODE_ECB)874 GEN_SEC_SETKEY_FUNC(aes_ecb, SEC_CALG_AES, SEC_CMODE_ECB)
875 GEN_SEC_SETKEY_FUNC(aes_cbc, SEC_CALG_AES, SEC_CMODE_CBC)
876 GEN_SEC_SETKEY_FUNC(aes_xts, SEC_CALG_AES, SEC_CMODE_XTS)
877 GEN_SEC_SETKEY_FUNC(aes_ofb, SEC_CALG_AES, SEC_CMODE_OFB)
878 GEN_SEC_SETKEY_FUNC(aes_cfb, SEC_CALG_AES, SEC_CMODE_CFB)
879 GEN_SEC_SETKEY_FUNC(aes_ctr, SEC_CALG_AES, SEC_CMODE_CTR)
880 GEN_SEC_SETKEY_FUNC(3des_ecb, SEC_CALG_3DES, SEC_CMODE_ECB)
881 GEN_SEC_SETKEY_FUNC(3des_cbc, SEC_CALG_3DES, SEC_CMODE_CBC)
882 GEN_SEC_SETKEY_FUNC(sm4_xts, SEC_CALG_SM4, SEC_CMODE_XTS)
883 GEN_SEC_SETKEY_FUNC(sm4_cbc, SEC_CALG_SM4, SEC_CMODE_CBC)
884 GEN_SEC_SETKEY_FUNC(sm4_ofb, SEC_CALG_SM4, SEC_CMODE_OFB)
885 GEN_SEC_SETKEY_FUNC(sm4_cfb, SEC_CALG_SM4, SEC_CMODE_CFB)
886 GEN_SEC_SETKEY_FUNC(sm4_ctr, SEC_CALG_SM4, SEC_CMODE_CTR)
887 
888 static int sec_cipher_pbuf_map(struct sec_ctx *ctx, struct sec_req *req,
889 			struct scatterlist *src)
890 {
891 	struct sec_aead_req *a_req = &req->aead_req;
892 	struct aead_request *aead_req = a_req->aead_req;
893 	struct sec_cipher_req *c_req = &req->c_req;
894 	struct sec_qp_ctx *qp_ctx = req->qp_ctx;
895 	struct device *dev = ctx->dev;
896 	int copy_size, pbuf_length;
897 	int req_id = req->req_id;
898 	struct crypto_aead *tfm;
899 	size_t authsize;
900 	u8 *mac_offset;
901 
902 	if (ctx->alg_type == SEC_AEAD)
903 		copy_size = aead_req->cryptlen + aead_req->assoclen;
904 	else
905 		copy_size = c_req->c_len;
906 
907 	pbuf_length = sg_copy_to_buffer(src, sg_nents(src),
908 			qp_ctx->res[req_id].pbuf, copy_size);
909 	if (unlikely(pbuf_length != copy_size)) {
910 		dev_err(dev, "copy src data to pbuf error!\n");
911 		return -EINVAL;
912 	}
913 	if (!c_req->encrypt && ctx->alg_type == SEC_AEAD) {
914 		tfm = crypto_aead_reqtfm(aead_req);
915 		authsize = crypto_aead_authsize(tfm);
916 		mac_offset = qp_ctx->res[req_id].pbuf + copy_size - authsize;
917 		memcpy(a_req->out_mac, mac_offset, authsize);
918 	}
919 
920 	req->in_dma = qp_ctx->res[req_id].pbuf_dma;
921 	c_req->c_out_dma = req->in_dma;
922 
923 	return 0;
924 }
925 
sec_cipher_pbuf_unmap(struct sec_ctx * ctx,struct sec_req * req,struct scatterlist * dst)926 static void sec_cipher_pbuf_unmap(struct sec_ctx *ctx, struct sec_req *req,
927 			struct scatterlist *dst)
928 {
929 	struct aead_request *aead_req = req->aead_req.aead_req;
930 	struct sec_cipher_req *c_req = &req->c_req;
931 	struct sec_qp_ctx *qp_ctx = req->qp_ctx;
932 	int copy_size, pbuf_length;
933 	int req_id = req->req_id;
934 
935 	if (ctx->alg_type == SEC_AEAD)
936 		copy_size = c_req->c_len + aead_req->assoclen;
937 	else
938 		copy_size = c_req->c_len;
939 
940 	pbuf_length = sg_copy_from_buffer(dst, sg_nents(dst),
941 			qp_ctx->res[req_id].pbuf, copy_size);
942 	if (unlikely(pbuf_length != copy_size))
943 		dev_err(ctx->dev, "copy pbuf data to dst error!\n");
944 }
945 
sec_aead_mac_init(struct sec_aead_req * req)946 static int sec_aead_mac_init(struct sec_aead_req *req)
947 {
948 	struct aead_request *aead_req = req->aead_req;
949 	struct crypto_aead *tfm = crypto_aead_reqtfm(aead_req);
950 	size_t authsize = crypto_aead_authsize(tfm);
951 	struct scatterlist *sgl = aead_req->src;
952 	u8 *mac_out = req->out_mac;
953 	size_t copy_size;
954 	off_t skip_size;
955 
956 	/* Copy input mac */
957 	skip_size = aead_req->assoclen + aead_req->cryptlen - authsize;
958 	copy_size = sg_pcopy_to_buffer(sgl, sg_nents(sgl), mac_out, authsize, skip_size);
959 	if (unlikely(copy_size != authsize))
960 		return -EINVAL;
961 
962 	return 0;
963 }
964 
sec_cipher_map(struct sec_ctx * ctx,struct sec_req * req,struct scatterlist * src,struct scatterlist * dst)965 static int sec_cipher_map(struct sec_ctx *ctx, struct sec_req *req,
966 			  struct scatterlist *src, struct scatterlist *dst)
967 {
968 	struct sec_cipher_req *c_req = &req->c_req;
969 	struct sec_aead_req *a_req = &req->aead_req;
970 	struct sec_qp_ctx *qp_ctx = req->qp_ctx;
971 	struct sec_alg_res *res = &qp_ctx->res[req->req_id];
972 	struct device *dev = ctx->dev;
973 	int ret;
974 
975 	if (req->use_pbuf) {
976 		c_req->c_ivin = res->pbuf + SEC_PBUF_IV_OFFSET;
977 		c_req->c_ivin_dma = res->pbuf_dma + SEC_PBUF_IV_OFFSET;
978 		if (ctx->alg_type == SEC_AEAD) {
979 			a_req->a_ivin = res->a_ivin;
980 			a_req->a_ivin_dma = res->a_ivin_dma;
981 			a_req->out_mac = res->pbuf + SEC_PBUF_MAC_OFFSET;
982 			a_req->out_mac_dma = res->pbuf_dma +
983 					SEC_PBUF_MAC_OFFSET;
984 		}
985 		ret = sec_cipher_pbuf_map(ctx, req, src);
986 
987 		return ret;
988 	}
989 	c_req->c_ivin = res->c_ivin;
990 	c_req->c_ivin_dma = res->c_ivin_dma;
991 	if (ctx->alg_type == SEC_AEAD) {
992 		a_req->a_ivin = res->a_ivin;
993 		a_req->a_ivin_dma = res->a_ivin_dma;
994 		a_req->out_mac = res->out_mac;
995 		a_req->out_mac_dma = res->out_mac_dma;
996 	}
997 
998 	req->in = hisi_acc_sg_buf_map_to_hw_sgl(dev, src,
999 						qp_ctx->c_in_pool,
1000 						req->req_id,
1001 						&req->in_dma);
1002 	if (IS_ERR(req->in)) {
1003 		dev_err(dev, "fail to dma map input sgl buffers!\n");
1004 		return PTR_ERR(req->in);
1005 	}
1006 
1007 	if (!c_req->encrypt && ctx->alg_type == SEC_AEAD) {
1008 		ret = sec_aead_mac_init(a_req);
1009 		if (unlikely(ret)) {
1010 			dev_err(dev, "fail to init mac data for ICV!\n");
1011 			return ret;
1012 		}
1013 	}
1014 
1015 	if (dst == src) {
1016 		c_req->c_out = req->in;
1017 		c_req->c_out_dma = req->in_dma;
1018 	} else {
1019 		c_req->c_out = hisi_acc_sg_buf_map_to_hw_sgl(dev, dst,
1020 							     qp_ctx->c_out_pool,
1021 							     req->req_id,
1022 							     &c_req->c_out_dma);
1023 
1024 		if (IS_ERR(c_req->c_out)) {
1025 			dev_err(dev, "fail to dma map output sgl buffers!\n");
1026 			hisi_acc_sg_buf_unmap(dev, src, req->in);
1027 			return PTR_ERR(c_req->c_out);
1028 		}
1029 	}
1030 
1031 	return 0;
1032 }
1033 
sec_cipher_unmap(struct sec_ctx * ctx,struct sec_req * req,struct scatterlist * src,struct scatterlist * dst)1034 static void sec_cipher_unmap(struct sec_ctx *ctx, struct sec_req *req,
1035 			     struct scatterlist *src, struct scatterlist *dst)
1036 {
1037 	struct sec_cipher_req *c_req = &req->c_req;
1038 	struct device *dev = ctx->dev;
1039 
1040 	if (req->use_pbuf) {
1041 		sec_cipher_pbuf_unmap(ctx, req, dst);
1042 	} else {
1043 		if (dst != src)
1044 			hisi_acc_sg_buf_unmap(dev, src, req->in);
1045 
1046 		hisi_acc_sg_buf_unmap(dev, dst, c_req->c_out);
1047 	}
1048 }
1049 
sec_skcipher_sgl_map(struct sec_ctx * ctx,struct sec_req * req)1050 static int sec_skcipher_sgl_map(struct sec_ctx *ctx, struct sec_req *req)
1051 {
1052 	struct skcipher_request *sq = req->c_req.sk_req;
1053 
1054 	return sec_cipher_map(ctx, req, sq->src, sq->dst);
1055 }
1056 
sec_skcipher_sgl_unmap(struct sec_ctx * ctx,struct sec_req * req)1057 static void sec_skcipher_sgl_unmap(struct sec_ctx *ctx, struct sec_req *req)
1058 {
1059 	struct skcipher_request *sq = req->c_req.sk_req;
1060 
1061 	sec_cipher_unmap(ctx, req, sq->src, sq->dst);
1062 }
1063 
sec_aead_aes_set_key(struct sec_cipher_ctx * c_ctx,struct crypto_authenc_keys * keys)1064 static int sec_aead_aes_set_key(struct sec_cipher_ctx *c_ctx,
1065 				struct crypto_authenc_keys *keys)
1066 {
1067 	switch (keys->enckeylen) {
1068 	case AES_KEYSIZE_128:
1069 		c_ctx->c_key_len = SEC_CKEY_128BIT;
1070 		break;
1071 	case AES_KEYSIZE_192:
1072 		c_ctx->c_key_len = SEC_CKEY_192BIT;
1073 		break;
1074 	case AES_KEYSIZE_256:
1075 		c_ctx->c_key_len = SEC_CKEY_256BIT;
1076 		break;
1077 	default:
1078 		pr_err("hisi_sec2: aead aes key error!\n");
1079 		return -EINVAL;
1080 	}
1081 	memcpy(c_ctx->c_key, keys->enckey, keys->enckeylen);
1082 
1083 	return 0;
1084 }
1085 
sec_aead_auth_set_key(struct sec_auth_ctx * ctx,struct crypto_authenc_keys * keys)1086 static int sec_aead_auth_set_key(struct sec_auth_ctx *ctx,
1087 				 struct crypto_authenc_keys *keys)
1088 {
1089 	struct crypto_shash *hash_tfm = ctx->hash_tfm;
1090 	int blocksize, digestsize, ret;
1091 
1092 	blocksize = crypto_shash_blocksize(hash_tfm);
1093 	digestsize = crypto_shash_digestsize(hash_tfm);
1094 	if (keys->authkeylen > blocksize) {
1095 		ret = crypto_shash_tfm_digest(hash_tfm, keys->authkey,
1096 					      keys->authkeylen, ctx->a_key);
1097 		if (ret) {
1098 			pr_err("hisi_sec2: aead auth digest error!\n");
1099 			return -EINVAL;
1100 		}
1101 		ctx->a_key_len = digestsize;
1102 	} else {
1103 		if (keys->authkeylen)
1104 			memcpy(ctx->a_key, keys->authkey, keys->authkeylen);
1105 		ctx->a_key_len = keys->authkeylen;
1106 	}
1107 
1108 	return 0;
1109 }
1110 
sec_aead_setauthsize(struct crypto_aead * aead,unsigned int authsize)1111 static int sec_aead_setauthsize(struct crypto_aead *aead, unsigned int authsize)
1112 {
1113 	struct crypto_tfm *tfm = crypto_aead_tfm(aead);
1114 	struct sec_ctx *ctx = crypto_tfm_ctx(tfm);
1115 	struct sec_auth_ctx *a_ctx = &ctx->a_ctx;
1116 
1117 	return crypto_aead_setauthsize(a_ctx->fallback_aead_tfm, authsize);
1118 }
1119 
sec_aead_fallback_setkey(struct sec_auth_ctx * a_ctx,struct crypto_aead * tfm,const u8 * key,unsigned int keylen)1120 static int sec_aead_fallback_setkey(struct sec_auth_ctx *a_ctx,
1121 				    struct crypto_aead *tfm, const u8 *key,
1122 				    unsigned int keylen)
1123 {
1124 	crypto_aead_clear_flags(a_ctx->fallback_aead_tfm, CRYPTO_TFM_REQ_MASK);
1125 	crypto_aead_set_flags(a_ctx->fallback_aead_tfm,
1126 			      crypto_aead_get_flags(tfm) & CRYPTO_TFM_REQ_MASK);
1127 	return crypto_aead_setkey(a_ctx->fallback_aead_tfm, key, keylen);
1128 }
1129 
sec_aead_setkey(struct crypto_aead * tfm,const u8 * key,const u32 keylen,const enum sec_hash_alg a_alg,const enum sec_calg c_alg,const enum sec_cmode c_mode)1130 static int sec_aead_setkey(struct crypto_aead *tfm, const u8 *key,
1131 			   const u32 keylen, const enum sec_hash_alg a_alg,
1132 			   const enum sec_calg c_alg,
1133 			   const enum sec_cmode c_mode)
1134 {
1135 	struct sec_ctx *ctx = crypto_aead_ctx(tfm);
1136 	struct sec_cipher_ctx *c_ctx = &ctx->c_ctx;
1137 	struct sec_auth_ctx *a_ctx = &ctx->a_ctx;
1138 	struct device *dev = ctx->dev;
1139 	struct crypto_authenc_keys keys;
1140 	int ret;
1141 
1142 	ctx->a_ctx.a_alg = a_alg;
1143 	ctx->c_ctx.c_alg = c_alg;
1144 	c_ctx->c_mode = c_mode;
1145 
1146 	if (c_mode == SEC_CMODE_CCM || c_mode == SEC_CMODE_GCM) {
1147 		ret = sec_skcipher_aes_sm4_setkey(c_ctx, keylen, c_mode);
1148 		if (ret) {
1149 			dev_err(dev, "set sec aes ccm cipher key err!\n");
1150 			return ret;
1151 		}
1152 		memcpy(c_ctx->c_key, key, keylen);
1153 
1154 		return sec_aead_fallback_setkey(a_ctx, tfm, key, keylen);
1155 	}
1156 
1157 	ret = crypto_authenc_extractkeys(&keys, key, keylen);
1158 	if (ret) {
1159 		dev_err(dev, "sec extract aead keys err!\n");
1160 		goto bad_key;
1161 	}
1162 
1163 	ret = sec_aead_aes_set_key(c_ctx, &keys);
1164 	if (ret) {
1165 		dev_err(dev, "set sec cipher key err!\n");
1166 		goto bad_key;
1167 	}
1168 
1169 	ret = sec_aead_auth_set_key(&ctx->a_ctx, &keys);
1170 	if (ret) {
1171 		dev_err(dev, "set sec auth key err!\n");
1172 		goto bad_key;
1173 	}
1174 
1175 	ret = sec_aead_fallback_setkey(a_ctx, tfm, key, keylen);
1176 	if (ret) {
1177 		dev_err(dev, "set sec fallback key err!\n");
1178 		goto bad_key;
1179 	}
1180 
1181 	return 0;
1182 
1183 bad_key:
1184 	memzero_explicit(&keys, sizeof(struct crypto_authenc_keys));
1185 	return ret;
1186 }
1187 
1188 
1189 #define GEN_SEC_AEAD_SETKEY_FUNC(name, aalg, calg, cmode)				\
1190 static int sec_setkey_##name(struct crypto_aead *tfm, const u8 *key, u32 keylen)	\
1191 {											\
1192 	return sec_aead_setkey(tfm, key, keylen, aalg, calg, cmode);			\
1193 }
1194 
GEN_SEC_AEAD_SETKEY_FUNC(aes_cbc_sha1,SEC_A_HMAC_SHA1,SEC_CALG_AES,SEC_CMODE_CBC)1195 GEN_SEC_AEAD_SETKEY_FUNC(aes_cbc_sha1, SEC_A_HMAC_SHA1, SEC_CALG_AES, SEC_CMODE_CBC)
1196 GEN_SEC_AEAD_SETKEY_FUNC(aes_cbc_sha256, SEC_A_HMAC_SHA256, SEC_CALG_AES, SEC_CMODE_CBC)
1197 GEN_SEC_AEAD_SETKEY_FUNC(aes_cbc_sha512, SEC_A_HMAC_SHA512, SEC_CALG_AES, SEC_CMODE_CBC)
1198 GEN_SEC_AEAD_SETKEY_FUNC(aes_ccm, 0, SEC_CALG_AES, SEC_CMODE_CCM)
1199 GEN_SEC_AEAD_SETKEY_FUNC(aes_gcm, 0, SEC_CALG_AES, SEC_CMODE_GCM)
1200 GEN_SEC_AEAD_SETKEY_FUNC(sm4_ccm, 0, SEC_CALG_SM4, SEC_CMODE_CCM)
1201 GEN_SEC_AEAD_SETKEY_FUNC(sm4_gcm, 0, SEC_CALG_SM4, SEC_CMODE_GCM)
1202 
1203 static int sec_aead_sgl_map(struct sec_ctx *ctx, struct sec_req *req)
1204 {
1205 	struct aead_request *aq = req->aead_req.aead_req;
1206 
1207 	return sec_cipher_map(ctx, req, aq->src, aq->dst);
1208 }
1209 
sec_aead_sgl_unmap(struct sec_ctx * ctx,struct sec_req * req)1210 static void sec_aead_sgl_unmap(struct sec_ctx *ctx, struct sec_req *req)
1211 {
1212 	struct aead_request *aq = req->aead_req.aead_req;
1213 
1214 	sec_cipher_unmap(ctx, req, aq->src, aq->dst);
1215 }
1216 
sec_request_transfer(struct sec_ctx * ctx,struct sec_req * req)1217 static int sec_request_transfer(struct sec_ctx *ctx, struct sec_req *req)
1218 {
1219 	int ret;
1220 
1221 	ret = ctx->req_op->buf_map(ctx, req);
1222 	if (unlikely(ret))
1223 		return ret;
1224 
1225 	ctx->req_op->do_transfer(ctx, req);
1226 
1227 	ret = ctx->req_op->bd_fill(ctx, req);
1228 	if (unlikely(ret))
1229 		goto unmap_req_buf;
1230 
1231 	return ret;
1232 
1233 unmap_req_buf:
1234 	ctx->req_op->buf_unmap(ctx, req);
1235 	return ret;
1236 }
1237 
sec_request_untransfer(struct sec_ctx * ctx,struct sec_req * req)1238 static void sec_request_untransfer(struct sec_ctx *ctx, struct sec_req *req)
1239 {
1240 	ctx->req_op->buf_unmap(ctx, req);
1241 }
1242 
sec_skcipher_copy_iv(struct sec_ctx * ctx,struct sec_req * req)1243 static void sec_skcipher_copy_iv(struct sec_ctx *ctx, struct sec_req *req)
1244 {
1245 	struct skcipher_request *sk_req = req->c_req.sk_req;
1246 	struct sec_cipher_req *c_req = &req->c_req;
1247 
1248 	memcpy(c_req->c_ivin, sk_req->iv, ctx->c_ctx.ivsize);
1249 }
1250 
sec_skcipher_bd_fill(struct sec_ctx * ctx,struct sec_req * req)1251 static int sec_skcipher_bd_fill(struct sec_ctx *ctx, struct sec_req *req)
1252 {
1253 	struct sec_cipher_ctx *c_ctx = &ctx->c_ctx;
1254 	struct sec_cipher_req *c_req = &req->c_req;
1255 	struct sec_sqe *sec_sqe = &req->sec_sqe;
1256 	u8 scene, sa_type, da_type;
1257 	u8 bd_type, cipher;
1258 	u8 de = 0;
1259 
1260 	memset(sec_sqe, 0, sizeof(struct sec_sqe));
1261 
1262 	sec_sqe->type2.c_key_addr = cpu_to_le64(c_ctx->c_key_dma);
1263 	sec_sqe->type2.c_ivin_addr = cpu_to_le64(c_req->c_ivin_dma);
1264 	sec_sqe->type2.data_src_addr = cpu_to_le64(req->in_dma);
1265 	sec_sqe->type2.data_dst_addr = cpu_to_le64(c_req->c_out_dma);
1266 
1267 	sec_sqe->type2.icvw_kmode |= cpu_to_le16(((u16)c_ctx->c_mode) <<
1268 						SEC_CMODE_OFFSET);
1269 	sec_sqe->type2.c_alg = c_ctx->c_alg;
1270 	sec_sqe->type2.icvw_kmode |= cpu_to_le16(((u16)c_ctx->c_key_len) <<
1271 						SEC_CKEY_OFFSET);
1272 
1273 	bd_type = SEC_BD_TYPE2;
1274 	if (c_req->encrypt)
1275 		cipher = SEC_CIPHER_ENC << SEC_CIPHER_OFFSET;
1276 	else
1277 		cipher = SEC_CIPHER_DEC << SEC_CIPHER_OFFSET;
1278 	sec_sqe->type_cipher_auth = bd_type | cipher;
1279 
1280 	/* Set destination and source address type */
1281 	if (req->use_pbuf) {
1282 		sa_type = SEC_PBUF << SEC_SRC_SGL_OFFSET;
1283 		da_type = SEC_PBUF << SEC_DST_SGL_OFFSET;
1284 	} else {
1285 		sa_type = SEC_SGL << SEC_SRC_SGL_OFFSET;
1286 		da_type = SEC_SGL << SEC_DST_SGL_OFFSET;
1287 	}
1288 
1289 	sec_sqe->sdm_addr_type |= da_type;
1290 	scene = SEC_COMM_SCENE << SEC_SCENE_OFFSET;
1291 	if (req->in_dma != c_req->c_out_dma)
1292 		de = 0x1 << SEC_DE_OFFSET;
1293 
1294 	sec_sqe->sds_sa_type = (de | scene | sa_type);
1295 
1296 	sec_sqe->type2.clen_ivhlen |= cpu_to_le32(c_req->c_len);
1297 	sec_sqe->type2.tag = cpu_to_le16((u16)req->req_id);
1298 
1299 	return 0;
1300 }
1301 
sec_skcipher_bd_fill_v3(struct sec_ctx * ctx,struct sec_req * req)1302 static int sec_skcipher_bd_fill_v3(struct sec_ctx *ctx, struct sec_req *req)
1303 {
1304 	struct sec_sqe3 *sec_sqe3 = &req->sec_sqe3;
1305 	struct sec_cipher_ctx *c_ctx = &ctx->c_ctx;
1306 	struct sec_cipher_req *c_req = &req->c_req;
1307 	u32 bd_param = 0;
1308 	u16 cipher;
1309 
1310 	memset(sec_sqe3, 0, sizeof(struct sec_sqe3));
1311 
1312 	sec_sqe3->c_key_addr = cpu_to_le64(c_ctx->c_key_dma);
1313 	sec_sqe3->no_scene.c_ivin_addr = cpu_to_le64(c_req->c_ivin_dma);
1314 	sec_sqe3->data_src_addr = cpu_to_le64(req->in_dma);
1315 	sec_sqe3->data_dst_addr = cpu_to_le64(c_req->c_out_dma);
1316 
1317 	sec_sqe3->c_mode_alg = ((u8)c_ctx->c_alg << SEC_CALG_OFFSET_V3) |
1318 						c_ctx->c_mode;
1319 	sec_sqe3->c_icv_key |= cpu_to_le16(((u16)c_ctx->c_key_len) <<
1320 						SEC_CKEY_OFFSET_V3);
1321 
1322 	if (c_req->encrypt)
1323 		cipher = SEC_CIPHER_ENC;
1324 	else
1325 		cipher = SEC_CIPHER_DEC;
1326 	sec_sqe3->c_icv_key |= cpu_to_le16(cipher);
1327 
1328 	/* Set the CTR counter mode is 128bit rollover */
1329 	sec_sqe3->auth_mac_key = cpu_to_le32((u32)SEC_CTR_CNT_ROLLOVER <<
1330 					SEC_CTR_CNT_OFFSET);
1331 
1332 	if (req->use_pbuf) {
1333 		bd_param |= SEC_PBUF << SEC_SRC_SGL_OFFSET_V3;
1334 		bd_param |= SEC_PBUF << SEC_DST_SGL_OFFSET_V3;
1335 	} else {
1336 		bd_param |= SEC_SGL << SEC_SRC_SGL_OFFSET_V3;
1337 		bd_param |= SEC_SGL << SEC_DST_SGL_OFFSET_V3;
1338 	}
1339 
1340 	bd_param |= SEC_COMM_SCENE << SEC_SCENE_OFFSET_V3;
1341 	if (req->in_dma != c_req->c_out_dma)
1342 		bd_param |= 0x1 << SEC_DE_OFFSET_V3;
1343 
1344 	bd_param |= SEC_BD_TYPE3;
1345 	sec_sqe3->bd_param = cpu_to_le32(bd_param);
1346 
1347 	sec_sqe3->c_len_ivin |= cpu_to_le32(c_req->c_len);
1348 	sec_sqe3->tag = cpu_to_le64(req);
1349 
1350 	return 0;
1351 }
1352 
1353 /* increment counter (128-bit int) */
ctr_iv_inc(__u8 * counter,__u8 bits,__u32 nums)1354 static void ctr_iv_inc(__u8 *counter, __u8 bits, __u32 nums)
1355 {
1356 	do {
1357 		--bits;
1358 		nums += counter[bits];
1359 		counter[bits] = nums & BITS_MASK;
1360 		nums >>= BYTE_BITS;
1361 	} while (bits && nums);
1362 }
1363 
sec_update_iv(struct sec_req * req,enum sec_alg_type alg_type)1364 static void sec_update_iv(struct sec_req *req, enum sec_alg_type alg_type)
1365 {
1366 	struct aead_request *aead_req = req->aead_req.aead_req;
1367 	struct skcipher_request *sk_req = req->c_req.sk_req;
1368 	u32 iv_size = req->ctx->c_ctx.ivsize;
1369 	struct scatterlist *sgl;
1370 	unsigned int cryptlen;
1371 	size_t sz;
1372 	u8 *iv;
1373 
1374 	if (req->c_req.encrypt)
1375 		sgl = alg_type == SEC_SKCIPHER ? sk_req->dst : aead_req->dst;
1376 	else
1377 		sgl = alg_type == SEC_SKCIPHER ? sk_req->src : aead_req->src;
1378 
1379 	if (alg_type == SEC_SKCIPHER) {
1380 		iv = sk_req->iv;
1381 		cryptlen = sk_req->cryptlen;
1382 	} else {
1383 		iv = aead_req->iv;
1384 		cryptlen = aead_req->cryptlen;
1385 	}
1386 
1387 	if (req->ctx->c_ctx.c_mode == SEC_CMODE_CBC) {
1388 		sz = sg_pcopy_to_buffer(sgl, sg_nents(sgl), iv, iv_size,
1389 					cryptlen - iv_size);
1390 		if (unlikely(sz != iv_size))
1391 			dev_err(req->ctx->dev, "copy output iv error!\n");
1392 	} else {
1393 		sz = cryptlen / iv_size;
1394 		if (cryptlen % iv_size)
1395 			sz += 1;
1396 		ctr_iv_inc(iv, iv_size, sz);
1397 	}
1398 }
1399 
sec_back_req_clear(struct sec_ctx * ctx,struct sec_qp_ctx * qp_ctx)1400 static struct sec_req *sec_back_req_clear(struct sec_ctx *ctx,
1401 				struct sec_qp_ctx *qp_ctx)
1402 {
1403 	struct sec_req *backlog_req = NULL;
1404 
1405 	spin_lock_bh(&qp_ctx->req_lock);
1406 	if (ctx->fake_req_limit >=
1407 	    atomic_read(&qp_ctx->qp->qp_status.used) &&
1408 	    !list_empty(&qp_ctx->backlog)) {
1409 		backlog_req = list_first_entry(&qp_ctx->backlog,
1410 				typeof(*backlog_req), backlog_head);
1411 		list_del(&backlog_req->backlog_head);
1412 	}
1413 	spin_unlock_bh(&qp_ctx->req_lock);
1414 
1415 	return backlog_req;
1416 }
1417 
sec_skcipher_callback(struct sec_ctx * ctx,struct sec_req * req,int err)1418 static void sec_skcipher_callback(struct sec_ctx *ctx, struct sec_req *req,
1419 				  int err)
1420 {
1421 	struct skcipher_request *sk_req = req->c_req.sk_req;
1422 	struct sec_qp_ctx *qp_ctx = req->qp_ctx;
1423 	struct skcipher_request *backlog_sk_req;
1424 	struct sec_req *backlog_req;
1425 
1426 	sec_free_req_id(req);
1427 
1428 	/* IV output at encrypto of CBC/CTR mode */
1429 	if (!err && (ctx->c_ctx.c_mode == SEC_CMODE_CBC ||
1430 	    ctx->c_ctx.c_mode == SEC_CMODE_CTR) && req->c_req.encrypt)
1431 		sec_update_iv(req, SEC_SKCIPHER);
1432 
1433 	while (1) {
1434 		backlog_req = sec_back_req_clear(ctx, qp_ctx);
1435 		if (!backlog_req)
1436 			break;
1437 
1438 		backlog_sk_req = backlog_req->c_req.sk_req;
1439 		skcipher_request_complete(backlog_sk_req, -EINPROGRESS);
1440 		atomic64_inc(&ctx->sec->debug.dfx.recv_busy_cnt);
1441 	}
1442 
1443 	skcipher_request_complete(sk_req, err);
1444 }
1445 
set_aead_auth_iv(struct sec_ctx * ctx,struct sec_req * req)1446 static void set_aead_auth_iv(struct sec_ctx *ctx, struct sec_req *req)
1447 {
1448 	struct aead_request *aead_req = req->aead_req.aead_req;
1449 	struct crypto_aead *tfm = crypto_aead_reqtfm(aead_req);
1450 	size_t authsize = crypto_aead_authsize(tfm);
1451 	struct sec_aead_req *a_req = &req->aead_req;
1452 	struct sec_cipher_req *c_req = &req->c_req;
1453 	u32 data_size = aead_req->cryptlen;
1454 	u8 flage = 0;
1455 	u8 cm, cl;
1456 
1457 	/* the specification has been checked in aead_iv_demension_check() */
1458 	cl = c_req->c_ivin[0] + 1;
1459 	c_req->c_ivin[ctx->c_ctx.ivsize - cl] = 0x00;
1460 	memset(&c_req->c_ivin[ctx->c_ctx.ivsize - cl], 0, cl);
1461 	c_req->c_ivin[ctx->c_ctx.ivsize - IV_LAST_BYTE1] = IV_CTR_INIT;
1462 
1463 	/* the last 3bit is L' */
1464 	flage |= c_req->c_ivin[0] & IV_CL_MASK;
1465 
1466 	/* the M' is bit3~bit5, the Flags is bit6 */
1467 	cm = (authsize - IV_CM_CAL_NUM) / IV_CM_CAL_NUM;
1468 	flage |= cm << IV_CM_OFFSET;
1469 	if (aead_req->assoclen)
1470 		flage |= 0x01 << IV_FLAGS_OFFSET;
1471 
1472 	memcpy(a_req->a_ivin, c_req->c_ivin, ctx->c_ctx.ivsize);
1473 	a_req->a_ivin[0] = flage;
1474 
1475 	/*
1476 	 * the last 32bit is counter's initial number,
1477 	 * but the nonce uses the first 16bit
1478 	 * the tail 16bit fill with the cipher length
1479 	 */
1480 	if (!c_req->encrypt)
1481 		data_size = aead_req->cryptlen - authsize;
1482 
1483 	a_req->a_ivin[ctx->c_ctx.ivsize - IV_LAST_BYTE1] =
1484 			data_size & IV_LAST_BYTE_MASK;
1485 	data_size >>= IV_BYTE_OFFSET;
1486 	a_req->a_ivin[ctx->c_ctx.ivsize - IV_LAST_BYTE2] =
1487 			data_size & IV_LAST_BYTE_MASK;
1488 }
1489 
sec_aead_set_iv(struct sec_ctx * ctx,struct sec_req * req)1490 static void sec_aead_set_iv(struct sec_ctx *ctx, struct sec_req *req)
1491 {
1492 	struct aead_request *aead_req = req->aead_req.aead_req;
1493 	struct sec_aead_req *a_req = &req->aead_req;
1494 	struct sec_cipher_req *c_req = &req->c_req;
1495 
1496 	memcpy(c_req->c_ivin, aead_req->iv, ctx->c_ctx.ivsize);
1497 
1498 	if (ctx->c_ctx.c_mode == SEC_CMODE_CCM) {
1499 		/*
1500 		 * CCM 16Byte Cipher_IV: {1B_Flage,13B_IV,2B_counter},
1501 		 * the  counter must set to 0x01
1502 		 * CCM 16Byte Auth_IV: {1B_AFlage,13B_IV,2B_Ptext_length}
1503 		 */
1504 		set_aead_auth_iv(ctx, req);
1505 	} else if (ctx->c_ctx.c_mode == SEC_CMODE_GCM) {
1506 		/* GCM 12Byte Cipher_IV == Auth_IV */
1507 		memcpy(a_req->a_ivin, c_req->c_ivin, SEC_AIV_SIZE);
1508 	}
1509 }
1510 
sec_auth_bd_fill_xcm(struct sec_auth_ctx * ctx,int dir,struct sec_req * req,struct sec_sqe * sec_sqe)1511 static void sec_auth_bd_fill_xcm(struct sec_auth_ctx *ctx, int dir,
1512 				 struct sec_req *req, struct sec_sqe *sec_sqe)
1513 {
1514 	struct sec_aead_req *a_req = &req->aead_req;
1515 	struct aead_request *aq = a_req->aead_req;
1516 	struct crypto_aead *tfm = crypto_aead_reqtfm(aq);
1517 	size_t authsize = crypto_aead_authsize(tfm);
1518 
1519 	/* C_ICV_Len is MAC size, 0x4 ~ 0x10 */
1520 	sec_sqe->type2.icvw_kmode |= cpu_to_le16((u16)authsize);
1521 
1522 	/* mode set to CCM/GCM, don't set {A_Alg, AKey_Len, MAC_Len} */
1523 	sec_sqe->type2.a_key_addr = sec_sqe->type2.c_key_addr;
1524 	sec_sqe->type2.a_ivin_addr = cpu_to_le64(a_req->a_ivin_dma);
1525 	sec_sqe->type_cipher_auth |= SEC_NO_AUTH << SEC_AUTH_OFFSET;
1526 
1527 	if (dir)
1528 		sec_sqe->sds_sa_type &= SEC_CIPHER_AUTH;
1529 	else
1530 		sec_sqe->sds_sa_type |= SEC_AUTH_CIPHER;
1531 
1532 	sec_sqe->type2.alen_ivllen = cpu_to_le32(aq->assoclen);
1533 	sec_sqe->type2.auth_src_offset = cpu_to_le16(0x0);
1534 	sec_sqe->type2.cipher_src_offset = cpu_to_le16((u16)aq->assoclen);
1535 
1536 	sec_sqe->type2.mac_addr = cpu_to_le64(a_req->out_mac_dma);
1537 }
1538 
sec_auth_bd_fill_xcm_v3(struct sec_auth_ctx * ctx,int dir,struct sec_req * req,struct sec_sqe3 * sqe3)1539 static void sec_auth_bd_fill_xcm_v3(struct sec_auth_ctx *ctx, int dir,
1540 				    struct sec_req *req, struct sec_sqe3 *sqe3)
1541 {
1542 	struct sec_aead_req *a_req = &req->aead_req;
1543 	struct aead_request *aq = a_req->aead_req;
1544 	struct crypto_aead *tfm = crypto_aead_reqtfm(aq);
1545 	size_t authsize = crypto_aead_authsize(tfm);
1546 
1547 	/* C_ICV_Len is MAC size, 0x4 ~ 0x10 */
1548 	sqe3->c_icv_key |= cpu_to_le16((u16)authsize << SEC_MAC_OFFSET_V3);
1549 
1550 	/* mode set to CCM/GCM, don't set {A_Alg, AKey_Len, MAC_Len} */
1551 	sqe3->a_key_addr = sqe3->c_key_addr;
1552 	sqe3->auth_ivin.a_ivin_addr = cpu_to_le64(a_req->a_ivin_dma);
1553 	sqe3->auth_mac_key |= SEC_NO_AUTH;
1554 
1555 	if (dir)
1556 		sqe3->huk_iv_seq &= SEC_CIPHER_AUTH_V3;
1557 	else
1558 		sqe3->huk_iv_seq |= SEC_AUTH_CIPHER_V3;
1559 
1560 	sqe3->a_len_key = cpu_to_le32(aq->assoclen);
1561 	sqe3->auth_src_offset = cpu_to_le16(0x0);
1562 	sqe3->cipher_src_offset = cpu_to_le16((u16)aq->assoclen);
1563 	sqe3->mac_addr = cpu_to_le64(a_req->out_mac_dma);
1564 }
1565 
sec_auth_bd_fill_ex(struct sec_auth_ctx * ctx,int dir,struct sec_req * req,struct sec_sqe * sec_sqe)1566 static void sec_auth_bd_fill_ex(struct sec_auth_ctx *ctx, int dir,
1567 			       struct sec_req *req, struct sec_sqe *sec_sqe)
1568 {
1569 	struct sec_aead_req *a_req = &req->aead_req;
1570 	struct sec_cipher_req *c_req = &req->c_req;
1571 	struct aead_request *aq = a_req->aead_req;
1572 	struct crypto_aead *tfm = crypto_aead_reqtfm(aq);
1573 	size_t authsize = crypto_aead_authsize(tfm);
1574 
1575 	sec_sqe->type2.a_key_addr = cpu_to_le64(ctx->a_key_dma);
1576 
1577 	sec_sqe->type2.mac_key_alg = cpu_to_le32(BYTES_TO_WORDS(authsize));
1578 
1579 	sec_sqe->type2.mac_key_alg |=
1580 			cpu_to_le32((u32)BYTES_TO_WORDS(ctx->a_key_len) << SEC_AKEY_OFFSET);
1581 
1582 	sec_sqe->type2.mac_key_alg |=
1583 			cpu_to_le32((u32)(ctx->a_alg) << SEC_AEAD_ALG_OFFSET);
1584 
1585 	if (dir) {
1586 		sec_sqe->type_cipher_auth |= SEC_AUTH_TYPE1 << SEC_AUTH_OFFSET;
1587 		sec_sqe->sds_sa_type &= SEC_CIPHER_AUTH;
1588 	} else {
1589 		sec_sqe->type_cipher_auth |= SEC_AUTH_TYPE2 << SEC_AUTH_OFFSET;
1590 		sec_sqe->sds_sa_type |= SEC_AUTH_CIPHER;
1591 	}
1592 	sec_sqe->type2.alen_ivllen = cpu_to_le32(c_req->c_len + aq->assoclen);
1593 
1594 	sec_sqe->type2.cipher_src_offset = cpu_to_le16((u16)aq->assoclen);
1595 
1596 	sec_sqe->type2.mac_addr = cpu_to_le64(a_req->out_mac_dma);
1597 }
1598 
sec_aead_bd_fill(struct sec_ctx * ctx,struct sec_req * req)1599 static int sec_aead_bd_fill(struct sec_ctx *ctx, struct sec_req *req)
1600 {
1601 	struct sec_auth_ctx *auth_ctx = &ctx->a_ctx;
1602 	struct sec_sqe *sec_sqe = &req->sec_sqe;
1603 	int ret;
1604 
1605 	ret = sec_skcipher_bd_fill(ctx, req);
1606 	if (unlikely(ret)) {
1607 		dev_err(ctx->dev, "skcipher bd fill is error!\n");
1608 		return ret;
1609 	}
1610 
1611 	if (ctx->c_ctx.c_mode == SEC_CMODE_CCM ||
1612 	    ctx->c_ctx.c_mode == SEC_CMODE_GCM)
1613 		sec_auth_bd_fill_xcm(auth_ctx, req->c_req.encrypt, req, sec_sqe);
1614 	else
1615 		sec_auth_bd_fill_ex(auth_ctx, req->c_req.encrypt, req, sec_sqe);
1616 
1617 	return 0;
1618 }
1619 
sec_auth_bd_fill_ex_v3(struct sec_auth_ctx * ctx,int dir,struct sec_req * req,struct sec_sqe3 * sqe3)1620 static void sec_auth_bd_fill_ex_v3(struct sec_auth_ctx *ctx, int dir,
1621 				   struct sec_req *req, struct sec_sqe3 *sqe3)
1622 {
1623 	struct sec_aead_req *a_req = &req->aead_req;
1624 	struct sec_cipher_req *c_req = &req->c_req;
1625 	struct aead_request *aq = a_req->aead_req;
1626 	struct crypto_aead *tfm = crypto_aead_reqtfm(aq);
1627 	size_t authsize = crypto_aead_authsize(tfm);
1628 
1629 	sqe3->a_key_addr = cpu_to_le64(ctx->a_key_dma);
1630 
1631 	sqe3->auth_mac_key |=
1632 			cpu_to_le32(BYTES_TO_WORDS(authsize) << SEC_MAC_OFFSET_V3);
1633 
1634 	sqe3->auth_mac_key |=
1635 			cpu_to_le32((u32)BYTES_TO_WORDS(ctx->a_key_len) << SEC_AKEY_OFFSET_V3);
1636 
1637 	sqe3->auth_mac_key |=
1638 			cpu_to_le32((u32)(ctx->a_alg) << SEC_AUTH_ALG_OFFSET_V3);
1639 
1640 	if (dir) {
1641 		sqe3->auth_mac_key |= cpu_to_le32((u32)SEC_AUTH_TYPE1);
1642 		sqe3->huk_iv_seq &= SEC_CIPHER_AUTH_V3;
1643 	} else {
1644 		sqe3->auth_mac_key |= cpu_to_le32((u32)SEC_AUTH_TYPE2);
1645 		sqe3->huk_iv_seq |= SEC_AUTH_CIPHER_V3;
1646 	}
1647 	sqe3->a_len_key = cpu_to_le32(c_req->c_len + aq->assoclen);
1648 
1649 	sqe3->cipher_src_offset = cpu_to_le16((u16)aq->assoclen);
1650 
1651 	sqe3->mac_addr = cpu_to_le64(a_req->out_mac_dma);
1652 }
1653 
sec_aead_bd_fill_v3(struct sec_ctx * ctx,struct sec_req * req)1654 static int sec_aead_bd_fill_v3(struct sec_ctx *ctx, struct sec_req *req)
1655 {
1656 	struct sec_auth_ctx *auth_ctx = &ctx->a_ctx;
1657 	struct sec_sqe3 *sec_sqe3 = &req->sec_sqe3;
1658 	int ret;
1659 
1660 	ret = sec_skcipher_bd_fill_v3(ctx, req);
1661 	if (unlikely(ret)) {
1662 		dev_err(ctx->dev, "skcipher bd3 fill is error!\n");
1663 		return ret;
1664 	}
1665 
1666 	if (ctx->c_ctx.c_mode == SEC_CMODE_CCM ||
1667 	    ctx->c_ctx.c_mode == SEC_CMODE_GCM)
1668 		sec_auth_bd_fill_xcm_v3(auth_ctx, req->c_req.encrypt,
1669 					req, sec_sqe3);
1670 	else
1671 		sec_auth_bd_fill_ex_v3(auth_ctx, req->c_req.encrypt,
1672 				       req, sec_sqe3);
1673 
1674 	return 0;
1675 }
1676 
sec_aead_callback(struct sec_ctx * c,struct sec_req * req,int err)1677 static void sec_aead_callback(struct sec_ctx *c, struct sec_req *req, int err)
1678 {
1679 	struct aead_request *a_req = req->aead_req.aead_req;
1680 	struct crypto_aead *tfm = crypto_aead_reqtfm(a_req);
1681 	size_t authsize = crypto_aead_authsize(tfm);
1682 	struct sec_aead_req *aead_req = &req->aead_req;
1683 	struct sec_cipher_req *c_req = &req->c_req;
1684 	struct sec_qp_ctx *qp_ctx = req->qp_ctx;
1685 	struct aead_request *backlog_aead_req;
1686 	struct sec_req *backlog_req;
1687 	size_t sz;
1688 
1689 	if (!err && c->c_ctx.c_mode == SEC_CMODE_CBC && c_req->encrypt)
1690 		sec_update_iv(req, SEC_AEAD);
1691 
1692 	/* Copy output mac */
1693 	if (!err && c_req->encrypt) {
1694 		struct scatterlist *sgl = a_req->dst;
1695 
1696 		sz = sg_pcopy_from_buffer(sgl, sg_nents(sgl), aead_req->out_mac,
1697 					  authsize, a_req->cryptlen + a_req->assoclen);
1698 		if (unlikely(sz != authsize)) {
1699 			dev_err(c->dev, "copy out mac err!\n");
1700 			err = -EINVAL;
1701 		}
1702 	}
1703 
1704 	sec_free_req_id(req);
1705 
1706 	while (1) {
1707 		backlog_req = sec_back_req_clear(c, qp_ctx);
1708 		if (!backlog_req)
1709 			break;
1710 
1711 		backlog_aead_req = backlog_req->aead_req.aead_req;
1712 		aead_request_complete(backlog_aead_req, -EINPROGRESS);
1713 		atomic64_inc(&c->sec->debug.dfx.recv_busy_cnt);
1714 	}
1715 
1716 	aead_request_complete(a_req, err);
1717 }
1718 
sec_request_uninit(struct sec_ctx * ctx,struct sec_req * req)1719 static void sec_request_uninit(struct sec_ctx *ctx, struct sec_req *req)
1720 {
1721 	sec_free_req_id(req);
1722 	sec_free_queue_id(ctx, req);
1723 }
1724 
sec_request_init(struct sec_ctx * ctx,struct sec_req * req)1725 static int sec_request_init(struct sec_ctx *ctx, struct sec_req *req)
1726 {
1727 	struct sec_qp_ctx *qp_ctx;
1728 	int queue_id;
1729 
1730 	/* To load balance */
1731 	queue_id = sec_alloc_queue_id(ctx, req);
1732 	qp_ctx = &ctx->qp_ctx[queue_id];
1733 
1734 	req->req_id = sec_alloc_req_id(req, qp_ctx);
1735 	if (unlikely(req->req_id < 0)) {
1736 		sec_free_queue_id(ctx, req);
1737 		return req->req_id;
1738 	}
1739 
1740 	return 0;
1741 }
1742 
sec_process(struct sec_ctx * ctx,struct sec_req * req)1743 static int sec_process(struct sec_ctx *ctx, struct sec_req *req)
1744 {
1745 	struct sec_cipher_req *c_req = &req->c_req;
1746 	int ret;
1747 
1748 	ret = sec_request_init(ctx, req);
1749 	if (unlikely(ret))
1750 		return ret;
1751 
1752 	ret = sec_request_transfer(ctx, req);
1753 	if (unlikely(ret))
1754 		goto err_uninit_req;
1755 
1756 	/* Output IV as decrypto */
1757 	if (!req->c_req.encrypt && (ctx->c_ctx.c_mode == SEC_CMODE_CBC ||
1758 	    ctx->c_ctx.c_mode == SEC_CMODE_CTR))
1759 		sec_update_iv(req, ctx->alg_type);
1760 
1761 	ret = ctx->req_op->bd_send(ctx, req);
1762 	if (unlikely((ret != -EBUSY && ret != -EINPROGRESS) ||
1763 		(ret == -EBUSY && !(req->flag & CRYPTO_TFM_REQ_MAY_BACKLOG)))) {
1764 		dev_err_ratelimited(ctx->dev, "send sec request failed!\n");
1765 		goto err_send_req;
1766 	}
1767 
1768 	return ret;
1769 
1770 err_send_req:
1771 	/* As failing, restore the IV from user */
1772 	if (ctx->c_ctx.c_mode == SEC_CMODE_CBC && !req->c_req.encrypt) {
1773 		if (ctx->alg_type == SEC_SKCIPHER)
1774 			memcpy(req->c_req.sk_req->iv, c_req->c_ivin,
1775 			       ctx->c_ctx.ivsize);
1776 		else
1777 			memcpy(req->aead_req.aead_req->iv, c_req->c_ivin,
1778 			       ctx->c_ctx.ivsize);
1779 	}
1780 
1781 	sec_request_untransfer(ctx, req);
1782 err_uninit_req:
1783 	sec_request_uninit(ctx, req);
1784 	return ret;
1785 }
1786 
1787 static const struct sec_req_op sec_skcipher_req_ops = {
1788 	.buf_map	= sec_skcipher_sgl_map,
1789 	.buf_unmap	= sec_skcipher_sgl_unmap,
1790 	.do_transfer	= sec_skcipher_copy_iv,
1791 	.bd_fill	= sec_skcipher_bd_fill,
1792 	.bd_send	= sec_bd_send,
1793 	.callback	= sec_skcipher_callback,
1794 	.process	= sec_process,
1795 };
1796 
1797 static const struct sec_req_op sec_aead_req_ops = {
1798 	.buf_map	= sec_aead_sgl_map,
1799 	.buf_unmap	= sec_aead_sgl_unmap,
1800 	.do_transfer	= sec_aead_set_iv,
1801 	.bd_fill	= sec_aead_bd_fill,
1802 	.bd_send	= sec_bd_send,
1803 	.callback	= sec_aead_callback,
1804 	.process	= sec_process,
1805 };
1806 
1807 static const struct sec_req_op sec_skcipher_req_ops_v3 = {
1808 	.buf_map	= sec_skcipher_sgl_map,
1809 	.buf_unmap	= sec_skcipher_sgl_unmap,
1810 	.do_transfer	= sec_skcipher_copy_iv,
1811 	.bd_fill	= sec_skcipher_bd_fill_v3,
1812 	.bd_send	= sec_bd_send,
1813 	.callback	= sec_skcipher_callback,
1814 	.process	= sec_process,
1815 };
1816 
1817 static const struct sec_req_op sec_aead_req_ops_v3 = {
1818 	.buf_map	= sec_aead_sgl_map,
1819 	.buf_unmap	= sec_aead_sgl_unmap,
1820 	.do_transfer	= sec_aead_set_iv,
1821 	.bd_fill	= sec_aead_bd_fill_v3,
1822 	.bd_send	= sec_bd_send,
1823 	.callback	= sec_aead_callback,
1824 	.process	= sec_process,
1825 };
1826 
sec_skcipher_ctx_init(struct crypto_skcipher * tfm)1827 static int sec_skcipher_ctx_init(struct crypto_skcipher *tfm)
1828 {
1829 	struct sec_ctx *ctx = crypto_skcipher_ctx(tfm);
1830 	int ret;
1831 
1832 	ret = sec_skcipher_init(tfm);
1833 	if (ret)
1834 		return ret;
1835 
1836 	if (ctx->sec->qm.ver < QM_HW_V3) {
1837 		ctx->type_supported = SEC_BD_TYPE2;
1838 		ctx->req_op = &sec_skcipher_req_ops;
1839 	} else {
1840 		ctx->type_supported = SEC_BD_TYPE3;
1841 		ctx->req_op = &sec_skcipher_req_ops_v3;
1842 	}
1843 
1844 	return ret;
1845 }
1846 
sec_skcipher_ctx_exit(struct crypto_skcipher * tfm)1847 static void sec_skcipher_ctx_exit(struct crypto_skcipher *tfm)
1848 {
1849 	sec_skcipher_uninit(tfm);
1850 }
1851 
sec_aead_init(struct crypto_aead * tfm)1852 static int sec_aead_init(struct crypto_aead *tfm)
1853 {
1854 	struct sec_ctx *ctx = crypto_aead_ctx(tfm);
1855 	int ret;
1856 
1857 	crypto_aead_set_reqsize(tfm, sizeof(struct sec_req));
1858 	ctx->alg_type = SEC_AEAD;
1859 	ctx->c_ctx.ivsize = crypto_aead_ivsize(tfm);
1860 	if (ctx->c_ctx.ivsize < SEC_AIV_SIZE ||
1861 	    ctx->c_ctx.ivsize > SEC_IV_SIZE) {
1862 		pr_err("get error aead iv size!\n");
1863 		return -EINVAL;
1864 	}
1865 
1866 	ret = sec_ctx_base_init(ctx);
1867 	if (ret)
1868 		return ret;
1869 	if (ctx->sec->qm.ver < QM_HW_V3) {
1870 		ctx->type_supported = SEC_BD_TYPE2;
1871 		ctx->req_op = &sec_aead_req_ops;
1872 	} else {
1873 		ctx->type_supported = SEC_BD_TYPE3;
1874 		ctx->req_op = &sec_aead_req_ops_v3;
1875 	}
1876 
1877 	ret = sec_auth_init(ctx);
1878 	if (ret)
1879 		goto err_auth_init;
1880 
1881 	ret = sec_cipher_init(ctx);
1882 	if (ret)
1883 		goto err_cipher_init;
1884 
1885 	return ret;
1886 
1887 err_cipher_init:
1888 	sec_auth_uninit(ctx);
1889 err_auth_init:
1890 	sec_ctx_base_uninit(ctx);
1891 	return ret;
1892 }
1893 
sec_aead_exit(struct crypto_aead * tfm)1894 static void sec_aead_exit(struct crypto_aead *tfm)
1895 {
1896 	struct sec_ctx *ctx = crypto_aead_ctx(tfm);
1897 
1898 	sec_cipher_uninit(ctx);
1899 	sec_auth_uninit(ctx);
1900 	sec_ctx_base_uninit(ctx);
1901 }
1902 
sec_aead_ctx_init(struct crypto_aead * tfm,const char * hash_name)1903 static int sec_aead_ctx_init(struct crypto_aead *tfm, const char *hash_name)
1904 {
1905 	struct aead_alg *alg = crypto_aead_alg(tfm);
1906 	struct sec_ctx *ctx = crypto_aead_ctx(tfm);
1907 	struct sec_auth_ctx *a_ctx = &ctx->a_ctx;
1908 	const char *aead_name = alg->base.cra_name;
1909 	int ret;
1910 
1911 	ret = sec_aead_init(tfm);
1912 	if (ret) {
1913 		pr_err("hisi_sec2: aead init error!\n");
1914 		return ret;
1915 	}
1916 
1917 	a_ctx->hash_tfm = crypto_alloc_shash(hash_name, 0, 0);
1918 	if (IS_ERR(a_ctx->hash_tfm)) {
1919 		dev_err(ctx->dev, "aead alloc shash error!\n");
1920 		sec_aead_exit(tfm);
1921 		return PTR_ERR(a_ctx->hash_tfm);
1922 	}
1923 
1924 	a_ctx->fallback_aead_tfm = crypto_alloc_aead(aead_name, 0,
1925 						     CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC);
1926 	if (IS_ERR(a_ctx->fallback_aead_tfm)) {
1927 		dev_err(ctx->dev, "aead driver alloc fallback tfm error!\n");
1928 		crypto_free_shash(ctx->a_ctx.hash_tfm);
1929 		sec_aead_exit(tfm);
1930 		return PTR_ERR(a_ctx->fallback_aead_tfm);
1931 	}
1932 
1933 	return 0;
1934 }
1935 
sec_aead_ctx_exit(struct crypto_aead * tfm)1936 static void sec_aead_ctx_exit(struct crypto_aead *tfm)
1937 {
1938 	struct sec_ctx *ctx = crypto_aead_ctx(tfm);
1939 
1940 	crypto_free_aead(ctx->a_ctx.fallback_aead_tfm);
1941 	crypto_free_shash(ctx->a_ctx.hash_tfm);
1942 	sec_aead_exit(tfm);
1943 }
1944 
sec_aead_xcm_ctx_init(struct crypto_aead * tfm)1945 static int sec_aead_xcm_ctx_init(struct crypto_aead *tfm)
1946 {
1947 	struct aead_alg *alg = crypto_aead_alg(tfm);
1948 	struct sec_ctx *ctx = crypto_aead_ctx(tfm);
1949 	struct sec_auth_ctx *a_ctx = &ctx->a_ctx;
1950 	const char *aead_name = alg->base.cra_name;
1951 	int ret;
1952 
1953 	ret = sec_aead_init(tfm);
1954 	if (ret) {
1955 		dev_err(ctx->dev, "hisi_sec2: aead xcm init error!\n");
1956 		return ret;
1957 	}
1958 
1959 	a_ctx->fallback_aead_tfm = crypto_alloc_aead(aead_name, 0,
1960 						     CRYPTO_ALG_NEED_FALLBACK |
1961 						     CRYPTO_ALG_ASYNC);
1962 	if (IS_ERR(a_ctx->fallback_aead_tfm)) {
1963 		dev_err(ctx->dev, "aead driver alloc fallback tfm error!\n");
1964 		sec_aead_exit(tfm);
1965 		return PTR_ERR(a_ctx->fallback_aead_tfm);
1966 	}
1967 
1968 	return 0;
1969 }
1970 
sec_aead_xcm_ctx_exit(struct crypto_aead * tfm)1971 static void sec_aead_xcm_ctx_exit(struct crypto_aead *tfm)
1972 {
1973 	struct sec_ctx *ctx = crypto_aead_ctx(tfm);
1974 
1975 	crypto_free_aead(ctx->a_ctx.fallback_aead_tfm);
1976 	sec_aead_exit(tfm);
1977 }
1978 
sec_aead_sha1_ctx_init(struct crypto_aead * tfm)1979 static int sec_aead_sha1_ctx_init(struct crypto_aead *tfm)
1980 {
1981 	return sec_aead_ctx_init(tfm, "sha1");
1982 }
1983 
sec_aead_sha256_ctx_init(struct crypto_aead * tfm)1984 static int sec_aead_sha256_ctx_init(struct crypto_aead *tfm)
1985 {
1986 	return sec_aead_ctx_init(tfm, "sha256");
1987 }
1988 
sec_aead_sha512_ctx_init(struct crypto_aead * tfm)1989 static int sec_aead_sha512_ctx_init(struct crypto_aead *tfm)
1990 {
1991 	return sec_aead_ctx_init(tfm, "sha512");
1992 }
1993 
sec_skcipher_cryptlen_check(struct sec_ctx * ctx,struct sec_req * sreq)1994 static int sec_skcipher_cryptlen_check(struct sec_ctx *ctx, struct sec_req *sreq)
1995 {
1996 	u32 cryptlen = sreq->c_req.sk_req->cryptlen;
1997 	struct device *dev = ctx->dev;
1998 	u8 c_mode = ctx->c_ctx.c_mode;
1999 	int ret = 0;
2000 
2001 	switch (c_mode) {
2002 	case SEC_CMODE_XTS:
2003 		if (unlikely(cryptlen < AES_BLOCK_SIZE)) {
2004 			dev_err(dev, "skcipher XTS mode input length error!\n");
2005 			ret = -EINVAL;
2006 		}
2007 		break;
2008 	case SEC_CMODE_ECB:
2009 	case SEC_CMODE_CBC:
2010 		if (unlikely(cryptlen & (AES_BLOCK_SIZE - 1))) {
2011 			dev_err(dev, "skcipher AES input length error!\n");
2012 			ret = -EINVAL;
2013 		}
2014 		break;
2015 	case SEC_CMODE_CFB:
2016 	case SEC_CMODE_OFB:
2017 	case SEC_CMODE_CTR:
2018 		break;
2019 	default:
2020 		ret = -EINVAL;
2021 	}
2022 
2023 	return ret;
2024 }
2025 
sec_skcipher_param_check(struct sec_ctx * ctx,struct sec_req * sreq,bool * need_fallback)2026 static int sec_skcipher_param_check(struct sec_ctx *ctx,
2027 				    struct sec_req *sreq, bool *need_fallback)
2028 {
2029 	struct skcipher_request *sk_req = sreq->c_req.sk_req;
2030 	struct device *dev = ctx->dev;
2031 	u8 c_alg = ctx->c_ctx.c_alg;
2032 
2033 	if (unlikely(!sk_req->src || !sk_req->dst)) {
2034 		dev_err(dev, "skcipher input param error!\n");
2035 		return -EINVAL;
2036 	}
2037 
2038 	if (sk_req->cryptlen > MAX_INPUT_DATA_LEN)
2039 		*need_fallback = true;
2040 
2041 	sreq->c_req.c_len = sk_req->cryptlen;
2042 
2043 	if (ctx->pbuf_supported && sk_req->cryptlen <= SEC_PBUF_SZ)
2044 		sreq->use_pbuf = true;
2045 	else
2046 		sreq->use_pbuf = false;
2047 
2048 	if (c_alg == SEC_CALG_3DES) {
2049 		if (unlikely(sk_req->cryptlen & (DES3_EDE_BLOCK_SIZE - 1))) {
2050 			dev_err(dev, "skcipher 3des input length error!\n");
2051 			return -EINVAL;
2052 		}
2053 		return 0;
2054 	} else if (c_alg == SEC_CALG_AES || c_alg == SEC_CALG_SM4) {
2055 		return sec_skcipher_cryptlen_check(ctx, sreq);
2056 	}
2057 
2058 	dev_err(dev, "skcipher algorithm error!\n");
2059 
2060 	return -EINVAL;
2061 }
2062 
sec_skcipher_soft_crypto(struct sec_ctx * ctx,struct skcipher_request * sreq,bool encrypt)2063 static int sec_skcipher_soft_crypto(struct sec_ctx *ctx,
2064 				    struct skcipher_request *sreq, bool encrypt)
2065 {
2066 	struct sec_cipher_ctx *c_ctx = &ctx->c_ctx;
2067 	SYNC_SKCIPHER_REQUEST_ON_STACK(subreq, c_ctx->fbtfm);
2068 	struct device *dev = ctx->dev;
2069 	int ret;
2070 
2071 	if (!c_ctx->fbtfm) {
2072 		dev_err_ratelimited(dev, "the soft tfm isn't supported in the current system.\n");
2073 		return -EINVAL;
2074 	}
2075 
2076 	skcipher_request_set_sync_tfm(subreq, c_ctx->fbtfm);
2077 
2078 	/* software need sync mode to do crypto */
2079 	skcipher_request_set_callback(subreq, sreq->base.flags,
2080 				      NULL, NULL);
2081 	skcipher_request_set_crypt(subreq, sreq->src, sreq->dst,
2082 				   sreq->cryptlen, sreq->iv);
2083 	if (encrypt)
2084 		ret = crypto_skcipher_encrypt(subreq);
2085 	else
2086 		ret = crypto_skcipher_decrypt(subreq);
2087 
2088 	skcipher_request_zero(subreq);
2089 
2090 	return ret;
2091 }
2092 
sec_skcipher_crypto(struct skcipher_request * sk_req,bool encrypt)2093 static int sec_skcipher_crypto(struct skcipher_request *sk_req, bool encrypt)
2094 {
2095 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(sk_req);
2096 	struct sec_req *req = skcipher_request_ctx(sk_req);
2097 	struct sec_ctx *ctx = crypto_skcipher_ctx(tfm);
2098 	bool need_fallback = false;
2099 	int ret;
2100 
2101 	if (!sk_req->cryptlen) {
2102 		if (ctx->c_ctx.c_mode == SEC_CMODE_XTS)
2103 			return -EINVAL;
2104 		return 0;
2105 	}
2106 
2107 	req->flag = sk_req->base.flags;
2108 	req->c_req.sk_req = sk_req;
2109 	req->c_req.encrypt = encrypt;
2110 	req->ctx = ctx;
2111 
2112 	ret = sec_skcipher_param_check(ctx, req, &need_fallback);
2113 	if (unlikely(ret))
2114 		return -EINVAL;
2115 
2116 	if (unlikely(ctx->c_ctx.fallback || need_fallback))
2117 		return sec_skcipher_soft_crypto(ctx, sk_req, encrypt);
2118 
2119 	return ctx->req_op->process(ctx, req);
2120 }
2121 
sec_skcipher_encrypt(struct skcipher_request * sk_req)2122 static int sec_skcipher_encrypt(struct skcipher_request *sk_req)
2123 {
2124 	return sec_skcipher_crypto(sk_req, true);
2125 }
2126 
sec_skcipher_decrypt(struct skcipher_request * sk_req)2127 static int sec_skcipher_decrypt(struct skcipher_request *sk_req)
2128 {
2129 	return sec_skcipher_crypto(sk_req, false);
2130 }
2131 
2132 #define SEC_SKCIPHER_GEN_ALG(sec_cra_name, sec_set_key, sec_min_key_size, \
2133 	sec_max_key_size, ctx_init, ctx_exit, blk_size, iv_size)\
2134 {\
2135 	.base = {\
2136 		.cra_name = sec_cra_name,\
2137 		.cra_driver_name = "hisi_sec_"sec_cra_name,\
2138 		.cra_priority = SEC_PRIORITY,\
2139 		.cra_flags = CRYPTO_ALG_ASYNC |\
2140 		 CRYPTO_ALG_NEED_FALLBACK,\
2141 		.cra_blocksize = blk_size,\
2142 		.cra_ctxsize = sizeof(struct sec_ctx),\
2143 		.cra_module = THIS_MODULE,\
2144 	},\
2145 	.init = ctx_init,\
2146 	.exit = ctx_exit,\
2147 	.setkey = sec_set_key,\
2148 	.decrypt = sec_skcipher_decrypt,\
2149 	.encrypt = sec_skcipher_encrypt,\
2150 	.min_keysize = sec_min_key_size,\
2151 	.max_keysize = sec_max_key_size,\
2152 	.ivsize = iv_size,\
2153 }
2154 
2155 #define SEC_SKCIPHER_ALG(name, key_func, min_key_size, \
2156 	max_key_size, blk_size, iv_size) \
2157 	SEC_SKCIPHER_GEN_ALG(name, key_func, min_key_size, max_key_size, \
2158 	sec_skcipher_ctx_init, sec_skcipher_ctx_exit, blk_size, iv_size)
2159 
2160 static struct sec_skcipher sec_skciphers[] = {
2161 	{
2162 		.alg_msk = BIT(0),
2163 		.alg = SEC_SKCIPHER_ALG("ecb(aes)", sec_setkey_aes_ecb, AES_MIN_KEY_SIZE,
2164 					AES_MAX_KEY_SIZE, AES_BLOCK_SIZE, 0),
2165 	},
2166 	{
2167 		.alg_msk = BIT(1),
2168 		.alg = SEC_SKCIPHER_ALG("cbc(aes)", sec_setkey_aes_cbc, AES_MIN_KEY_SIZE,
2169 					AES_MAX_KEY_SIZE, AES_BLOCK_SIZE, AES_BLOCK_SIZE),
2170 	},
2171 	{
2172 		.alg_msk = BIT(2),
2173 		.alg = SEC_SKCIPHER_ALG("ctr(aes)", sec_setkey_aes_ctr,	AES_MIN_KEY_SIZE,
2174 					AES_MAX_KEY_SIZE, SEC_MIN_BLOCK_SZ, AES_BLOCK_SIZE),
2175 	},
2176 	{
2177 		.alg_msk = BIT(3),
2178 		.alg = SEC_SKCIPHER_ALG("xts(aes)", sec_setkey_aes_xts,	SEC_XTS_MIN_KEY_SIZE,
2179 					SEC_XTS_MAX_KEY_SIZE, AES_BLOCK_SIZE, AES_BLOCK_SIZE),
2180 	},
2181 	{
2182 		.alg_msk = BIT(4),
2183 		.alg = SEC_SKCIPHER_ALG("ofb(aes)", sec_setkey_aes_ofb,	AES_MIN_KEY_SIZE,
2184 					AES_MAX_KEY_SIZE, SEC_MIN_BLOCK_SZ, AES_BLOCK_SIZE),
2185 	},
2186 	{
2187 		.alg_msk = BIT(5),
2188 		.alg = SEC_SKCIPHER_ALG("cfb(aes)", sec_setkey_aes_cfb,	AES_MIN_KEY_SIZE,
2189 					AES_MAX_KEY_SIZE, SEC_MIN_BLOCK_SZ, AES_BLOCK_SIZE),
2190 	},
2191 	{
2192 		.alg_msk = BIT(12),
2193 		.alg = SEC_SKCIPHER_ALG("cbc(sm4)", sec_setkey_sm4_cbc,	AES_MIN_KEY_SIZE,
2194 					AES_MIN_KEY_SIZE, AES_BLOCK_SIZE, AES_BLOCK_SIZE),
2195 	},
2196 	{
2197 		.alg_msk = BIT(13),
2198 		.alg = SEC_SKCIPHER_ALG("ctr(sm4)", sec_setkey_sm4_ctr, AES_MIN_KEY_SIZE,
2199 					AES_MIN_KEY_SIZE, SEC_MIN_BLOCK_SZ, AES_BLOCK_SIZE),
2200 	},
2201 	{
2202 		.alg_msk = BIT(14),
2203 		.alg = SEC_SKCIPHER_ALG("xts(sm4)", sec_setkey_sm4_xts,	SEC_XTS_MIN_KEY_SIZE,
2204 					SEC_XTS_MIN_KEY_SIZE, AES_BLOCK_SIZE, AES_BLOCK_SIZE),
2205 	},
2206 	{
2207 		.alg_msk = BIT(15),
2208 		.alg = SEC_SKCIPHER_ALG("ofb(sm4)", sec_setkey_sm4_ofb,	AES_MIN_KEY_SIZE,
2209 					AES_MIN_KEY_SIZE, SEC_MIN_BLOCK_SZ, AES_BLOCK_SIZE),
2210 	},
2211 	{
2212 		.alg_msk = BIT(16),
2213 		.alg = SEC_SKCIPHER_ALG("cfb(sm4)", sec_setkey_sm4_cfb,	AES_MIN_KEY_SIZE,
2214 					AES_MIN_KEY_SIZE, SEC_MIN_BLOCK_SZ, AES_BLOCK_SIZE),
2215 	},
2216 	{
2217 		.alg_msk = BIT(23),
2218 		.alg = SEC_SKCIPHER_ALG("ecb(des3_ede)", sec_setkey_3des_ecb, SEC_DES3_3KEY_SIZE,
2219 					SEC_DES3_3KEY_SIZE, DES3_EDE_BLOCK_SIZE, 0),
2220 	},
2221 	{
2222 		.alg_msk = BIT(24),
2223 		.alg = SEC_SKCIPHER_ALG("cbc(des3_ede)", sec_setkey_3des_cbc, SEC_DES3_3KEY_SIZE,
2224 					SEC_DES3_3KEY_SIZE, DES3_EDE_BLOCK_SIZE,
2225 					DES3_EDE_BLOCK_SIZE),
2226 	},
2227 };
2228 
aead_iv_demension_check(struct aead_request * aead_req)2229 static int aead_iv_demension_check(struct aead_request *aead_req)
2230 {
2231 	u8 cl;
2232 
2233 	cl = aead_req->iv[0] + 1;
2234 	if (cl < IV_CL_MIN || cl > IV_CL_MAX)
2235 		return -EINVAL;
2236 
2237 	if (cl < IV_CL_MID && aead_req->cryptlen >> (BYTE_BITS * cl))
2238 		return -EOVERFLOW;
2239 
2240 	return 0;
2241 }
2242 
sec_aead_spec_check(struct sec_ctx * ctx,struct sec_req * sreq)2243 static int sec_aead_spec_check(struct sec_ctx *ctx, struct sec_req *sreq)
2244 {
2245 	struct aead_request *req = sreq->aead_req.aead_req;
2246 	struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2247 	size_t sz = crypto_aead_authsize(tfm);
2248 	u8 c_mode = ctx->c_ctx.c_mode;
2249 	int ret;
2250 
2251 	if (unlikely(ctx->sec->qm.ver == QM_HW_V2 && !sreq->c_req.c_len))
2252 		return -EINVAL;
2253 
2254 	if (unlikely(req->cryptlen + req->assoclen > MAX_INPUT_DATA_LEN ||
2255 		     req->assoclen > SEC_MAX_AAD_LEN))
2256 		return -EINVAL;
2257 
2258 	if (c_mode == SEC_CMODE_CCM) {
2259 		if (unlikely(req->assoclen > SEC_MAX_CCM_AAD_LEN))
2260 			return -EINVAL;
2261 
2262 		ret = aead_iv_demension_check(req);
2263 		if (unlikely(ret))
2264 			return -EINVAL;
2265 	} else if (c_mode == SEC_CMODE_CBC) {
2266 		if (unlikely(sz & WORD_MASK))
2267 			return -EINVAL;
2268 		if (unlikely(ctx->a_ctx.a_key_len & WORD_MASK))
2269 			return -EINVAL;
2270 	}
2271 
2272 	return 0;
2273 }
2274 
sec_aead_param_check(struct sec_ctx * ctx,struct sec_req * sreq,bool * need_fallback)2275 static int sec_aead_param_check(struct sec_ctx *ctx, struct sec_req *sreq, bool *need_fallback)
2276 {
2277 	struct aead_request *req = sreq->aead_req.aead_req;
2278 	struct device *dev = ctx->dev;
2279 	u8 c_alg = ctx->c_ctx.c_alg;
2280 
2281 	if (unlikely(!req->src || !req->dst)) {
2282 		dev_err(dev, "aead input param error!\n");
2283 		return -EINVAL;
2284 	}
2285 
2286 	if (unlikely(ctx->c_ctx.c_mode == SEC_CMODE_CBC &&
2287 		     sreq->c_req.c_len & (AES_BLOCK_SIZE - 1))) {
2288 		dev_err(dev, "aead cbc mode input data length error!\n");
2289 		return -EINVAL;
2290 	}
2291 
2292 	/* Support AES or SM4 */
2293 	if (unlikely(c_alg != SEC_CALG_AES && c_alg != SEC_CALG_SM4)) {
2294 		dev_err(dev, "aead crypto alg error!\n");
2295 		return -EINVAL;
2296 	}
2297 
2298 	if (unlikely(sec_aead_spec_check(ctx, sreq))) {
2299 		*need_fallback = true;
2300 		return -EINVAL;
2301 	}
2302 
2303 	if (ctx->pbuf_supported && (req->cryptlen + req->assoclen) <=
2304 		SEC_PBUF_SZ)
2305 		sreq->use_pbuf = true;
2306 	else
2307 		sreq->use_pbuf = false;
2308 
2309 	return 0;
2310 }
2311 
sec_aead_soft_crypto(struct sec_ctx * ctx,struct aead_request * aead_req,bool encrypt)2312 static int sec_aead_soft_crypto(struct sec_ctx *ctx,
2313 				struct aead_request *aead_req,
2314 				bool encrypt)
2315 {
2316 	struct sec_auth_ctx *a_ctx = &ctx->a_ctx;
2317 	struct aead_request *subreq;
2318 	int ret;
2319 
2320 	subreq = aead_request_alloc(a_ctx->fallback_aead_tfm, GFP_KERNEL);
2321 	if (!subreq)
2322 		return -ENOMEM;
2323 
2324 	aead_request_set_tfm(subreq, a_ctx->fallback_aead_tfm);
2325 	aead_request_set_callback(subreq, aead_req->base.flags,
2326 				  aead_req->base.complete, aead_req->base.data);
2327 	aead_request_set_crypt(subreq, aead_req->src, aead_req->dst,
2328 			       aead_req->cryptlen, aead_req->iv);
2329 	aead_request_set_ad(subreq, aead_req->assoclen);
2330 
2331 	if (encrypt)
2332 		ret = crypto_aead_encrypt(subreq);
2333 	else
2334 		ret = crypto_aead_decrypt(subreq);
2335 	aead_request_free(subreq);
2336 
2337 	return ret;
2338 }
2339 
sec_aead_crypto(struct aead_request * a_req,bool encrypt)2340 static int sec_aead_crypto(struct aead_request *a_req, bool encrypt)
2341 {
2342 	struct crypto_aead *tfm = crypto_aead_reqtfm(a_req);
2343 	struct sec_req *req = aead_request_ctx(a_req);
2344 	struct sec_ctx *ctx = crypto_aead_ctx(tfm);
2345 	size_t sz = crypto_aead_authsize(tfm);
2346 	bool need_fallback = false;
2347 	int ret;
2348 
2349 	req->flag = a_req->base.flags;
2350 	req->aead_req.aead_req = a_req;
2351 	req->c_req.encrypt = encrypt;
2352 	req->ctx = ctx;
2353 	req->c_req.c_len = a_req->cryptlen - (req->c_req.encrypt ? 0 : sz);
2354 
2355 	ret = sec_aead_param_check(ctx, req, &need_fallback);
2356 	if (unlikely(ret)) {
2357 		if (need_fallback)
2358 			return sec_aead_soft_crypto(ctx, a_req, encrypt);
2359 		return -EINVAL;
2360 	}
2361 
2362 	return ctx->req_op->process(ctx, req);
2363 }
2364 
sec_aead_encrypt(struct aead_request * a_req)2365 static int sec_aead_encrypt(struct aead_request *a_req)
2366 {
2367 	return sec_aead_crypto(a_req, true);
2368 }
2369 
sec_aead_decrypt(struct aead_request * a_req)2370 static int sec_aead_decrypt(struct aead_request *a_req)
2371 {
2372 	return sec_aead_crypto(a_req, false);
2373 }
2374 
2375 #define SEC_AEAD_ALG(sec_cra_name, sec_set_key, ctx_init,\
2376 			 ctx_exit, blk_size, iv_size, max_authsize)\
2377 {\
2378 	.base = {\
2379 		.cra_name = sec_cra_name,\
2380 		.cra_driver_name = "hisi_sec_"sec_cra_name,\
2381 		.cra_priority = SEC_PRIORITY,\
2382 		.cra_flags = CRYPTO_ALG_ASYNC |\
2383 		 CRYPTO_ALG_NEED_FALLBACK,\
2384 		.cra_blocksize = blk_size,\
2385 		.cra_ctxsize = sizeof(struct sec_ctx),\
2386 		.cra_module = THIS_MODULE,\
2387 	},\
2388 	.init = ctx_init,\
2389 	.exit = ctx_exit,\
2390 	.setkey = sec_set_key,\
2391 	.setauthsize = sec_aead_setauthsize,\
2392 	.decrypt = sec_aead_decrypt,\
2393 	.encrypt = sec_aead_encrypt,\
2394 	.ivsize = iv_size,\
2395 	.maxauthsize = max_authsize,\
2396 }
2397 
2398 static struct sec_aead sec_aeads[] = {
2399 	{
2400 		.alg_msk = BIT(6),
2401 		.alg = SEC_AEAD_ALG("ccm(aes)", sec_setkey_aes_ccm, sec_aead_xcm_ctx_init,
2402 				    sec_aead_xcm_ctx_exit, SEC_MIN_BLOCK_SZ, AES_BLOCK_SIZE,
2403 				    AES_BLOCK_SIZE),
2404 	},
2405 	{
2406 		.alg_msk = BIT(7),
2407 		.alg = SEC_AEAD_ALG("gcm(aes)", sec_setkey_aes_gcm, sec_aead_xcm_ctx_init,
2408 				    sec_aead_xcm_ctx_exit, SEC_MIN_BLOCK_SZ, SEC_AIV_SIZE,
2409 				    AES_BLOCK_SIZE),
2410 	},
2411 	{
2412 		.alg_msk = BIT(17),
2413 		.alg = SEC_AEAD_ALG("ccm(sm4)", sec_setkey_sm4_ccm, sec_aead_xcm_ctx_init,
2414 				    sec_aead_xcm_ctx_exit, SEC_MIN_BLOCK_SZ, AES_BLOCK_SIZE,
2415 				    AES_BLOCK_SIZE),
2416 	},
2417 	{
2418 		.alg_msk = BIT(18),
2419 		.alg = SEC_AEAD_ALG("gcm(sm4)", sec_setkey_sm4_gcm, sec_aead_xcm_ctx_init,
2420 				    sec_aead_xcm_ctx_exit, SEC_MIN_BLOCK_SZ, SEC_AIV_SIZE,
2421 				    AES_BLOCK_SIZE),
2422 	},
2423 	{
2424 		.alg_msk = BIT(43),
2425 		.alg = SEC_AEAD_ALG("authenc(hmac(sha1),cbc(aes))", sec_setkey_aes_cbc_sha1,
2426 				    sec_aead_sha1_ctx_init, sec_aead_ctx_exit, AES_BLOCK_SIZE,
2427 				    AES_BLOCK_SIZE, SHA1_DIGEST_SIZE),
2428 	},
2429 	{
2430 		.alg_msk = BIT(44),
2431 		.alg = SEC_AEAD_ALG("authenc(hmac(sha256),cbc(aes))", sec_setkey_aes_cbc_sha256,
2432 				    sec_aead_sha256_ctx_init, sec_aead_ctx_exit, AES_BLOCK_SIZE,
2433 				    AES_BLOCK_SIZE, SHA256_DIGEST_SIZE),
2434 	},
2435 	{
2436 		.alg_msk = BIT(45),
2437 		.alg = SEC_AEAD_ALG("authenc(hmac(sha512),cbc(aes))", sec_setkey_aes_cbc_sha512,
2438 				    sec_aead_sha512_ctx_init, sec_aead_ctx_exit, AES_BLOCK_SIZE,
2439 				    AES_BLOCK_SIZE, SHA512_DIGEST_SIZE),
2440 	},
2441 };
2442 
sec_unregister_skcipher(u64 alg_mask,int end)2443 static void sec_unregister_skcipher(u64 alg_mask, int end)
2444 {
2445 	int i;
2446 
2447 	for (i = 0; i < end; i++)
2448 		if (sec_skciphers[i].alg_msk & alg_mask)
2449 			crypto_unregister_skcipher(&sec_skciphers[i].alg);
2450 }
2451 
sec_register_skcipher(u64 alg_mask)2452 static int sec_register_skcipher(u64 alg_mask)
2453 {
2454 	int i, ret, count;
2455 
2456 	count = ARRAY_SIZE(sec_skciphers);
2457 
2458 	for (i = 0; i < count; i++) {
2459 		if (!(sec_skciphers[i].alg_msk & alg_mask))
2460 			continue;
2461 
2462 		ret = crypto_register_skcipher(&sec_skciphers[i].alg);
2463 		if (ret)
2464 			goto err;
2465 	}
2466 
2467 	return 0;
2468 
2469 err:
2470 	sec_unregister_skcipher(alg_mask, i);
2471 
2472 	return ret;
2473 }
2474 
sec_unregister_aead(u64 alg_mask,int end)2475 static void sec_unregister_aead(u64 alg_mask, int end)
2476 {
2477 	int i;
2478 
2479 	for (i = 0; i < end; i++)
2480 		if (sec_aeads[i].alg_msk & alg_mask)
2481 			crypto_unregister_aead(&sec_aeads[i].alg);
2482 }
2483 
sec_register_aead(u64 alg_mask)2484 static int sec_register_aead(u64 alg_mask)
2485 {
2486 	int i, ret, count;
2487 
2488 	count = ARRAY_SIZE(sec_aeads);
2489 
2490 	for (i = 0; i < count; i++) {
2491 		if (!(sec_aeads[i].alg_msk & alg_mask))
2492 			continue;
2493 
2494 		ret = crypto_register_aead(&sec_aeads[i].alg);
2495 		if (ret)
2496 			goto err;
2497 	}
2498 
2499 	return 0;
2500 
2501 err:
2502 	sec_unregister_aead(alg_mask, i);
2503 
2504 	return ret;
2505 }
2506 
sec_register_to_crypto(struct hisi_qm * qm)2507 int sec_register_to_crypto(struct hisi_qm *qm)
2508 {
2509 	u64 alg_mask;
2510 	int ret = 0;
2511 
2512 	alg_mask = sec_get_alg_bitmap(qm, SEC_DRV_ALG_BITMAP_HIGH_IDX,
2513 				      SEC_DRV_ALG_BITMAP_LOW_IDX);
2514 
2515 
2516 	ret = sec_register_skcipher(alg_mask);
2517 	if (ret)
2518 		return ret;
2519 
2520 	ret = sec_register_aead(alg_mask);
2521 	if (ret)
2522 		sec_unregister_skcipher(alg_mask, ARRAY_SIZE(sec_skciphers));
2523 
2524 	return ret;
2525 }
2526 
sec_unregister_from_crypto(struct hisi_qm * qm)2527 void sec_unregister_from_crypto(struct hisi_qm *qm)
2528 {
2529 	u64 alg_mask;
2530 
2531 	alg_mask = sec_get_alg_bitmap(qm, SEC_DRV_ALG_BITMAP_HIGH_IDX,
2532 				      SEC_DRV_ALG_BITMAP_LOW_IDX);
2533 
2534 	sec_unregister_aead(alg_mask, ARRAY_SIZE(sec_aeads));
2535 	sec_unregister_skcipher(alg_mask, ARRAY_SIZE(sec_skciphers));
2536 }
2537