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