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 struct sec_skcipher {
108 	u64 alg_msk;
109 	struct skcipher_alg alg;
110 };
111 
112 struct sec_aead {
113 	u64 alg_msk;
114 	struct aead_alg alg;
115 };
116 
117 /* Get an en/de-cipher queue cyclically to balance load over queues of TFM */
118 static inline int sec_alloc_queue_id(struct sec_ctx *ctx, struct sec_req *req)
119 {
120 	if (req->c_req.encrypt)
121 		return (u32)atomic_inc_return(&ctx->enc_qcyclic) %
122 				 ctx->hlf_q_num;
123 
124 	return (u32)atomic_inc_return(&ctx->dec_qcyclic) % ctx->hlf_q_num +
125 				 ctx->hlf_q_num;
126 }
127 
128 static inline void sec_free_queue_id(struct sec_ctx *ctx, struct sec_req *req)
129 {
130 	if (req->c_req.encrypt)
131 		atomic_dec(&ctx->enc_qcyclic);
132 	else
133 		atomic_dec(&ctx->dec_qcyclic);
134 }
135 
136 static int sec_alloc_req_id(struct sec_req *req, struct sec_qp_ctx *qp_ctx)
137 {
138 	int req_id;
139 
140 	spin_lock_bh(&qp_ctx->req_lock);
141 	req_id = idr_alloc_cyclic(&qp_ctx->req_idr, NULL, 0, qp_ctx->qp->sq_depth, GFP_ATOMIC);
142 	spin_unlock_bh(&qp_ctx->req_lock);
143 	if (unlikely(req_id < 0)) {
144 		dev_err(req->ctx->dev, "alloc req id fail!\n");
145 		return req_id;
146 	}
147 
148 	req->qp_ctx = qp_ctx;
149 	qp_ctx->req_list[req_id] = req;
150 
151 	return req_id;
152 }
153 
154 static void sec_free_req_id(struct sec_req *req)
155 {
156 	struct sec_qp_ctx *qp_ctx = req->qp_ctx;
157 	int req_id = req->req_id;
158 
159 	if (unlikely(req_id < 0 || req_id >= qp_ctx->qp->sq_depth)) {
160 		dev_err(req->ctx->dev, "free request id invalid!\n");
161 		return;
162 	}
163 
164 	qp_ctx->req_list[req_id] = NULL;
165 	req->qp_ctx = NULL;
166 
167 	spin_lock_bh(&qp_ctx->req_lock);
168 	idr_remove(&qp_ctx->req_idr, req_id);
169 	spin_unlock_bh(&qp_ctx->req_lock);
170 }
171 
172 static u8 pre_parse_finished_bd(struct bd_status *status, void *resp)
173 {
174 	struct sec_sqe *bd = resp;
175 
176 	status->done = le16_to_cpu(bd->type2.done_flag) & SEC_DONE_MASK;
177 	status->icv = (le16_to_cpu(bd->type2.done_flag) & SEC_ICV_MASK) >> 1;
178 	status->flag = (le16_to_cpu(bd->type2.done_flag) &
179 					SEC_FLAG_MASK) >> SEC_FLAG_OFFSET;
180 	status->tag = le16_to_cpu(bd->type2.tag);
181 	status->err_type = bd->type2.error_type;
182 
183 	return bd->type_cipher_auth & SEC_TYPE_MASK;
184 }
185 
186 static u8 pre_parse_finished_bd3(struct bd_status *status, void *resp)
187 {
188 	struct sec_sqe3 *bd3 = resp;
189 
190 	status->done = le16_to_cpu(bd3->done_flag) & SEC_DONE_MASK;
191 	status->icv = (le16_to_cpu(bd3->done_flag) & SEC_ICV_MASK) >> 1;
192 	status->flag = (le16_to_cpu(bd3->done_flag) &
193 					SEC_FLAG_MASK) >> SEC_FLAG_OFFSET;
194 	status->tag = le64_to_cpu(bd3->tag);
195 	status->err_type = bd3->error_type;
196 
197 	return le32_to_cpu(bd3->bd_param) & SEC_TYPE_MASK;
198 }
199 
200 static int sec_cb_status_check(struct sec_req *req,
201 			       struct bd_status *status)
202 {
203 	struct sec_ctx *ctx = req->ctx;
204 
205 	if (unlikely(req->err_type || status->done != SEC_SQE_DONE)) {
206 		dev_err_ratelimited(ctx->dev, "err_type[%d], done[%u]\n",
207 				    req->err_type, status->done);
208 		return -EIO;
209 	}
210 
211 	if (unlikely(ctx->alg_type == SEC_SKCIPHER)) {
212 		if (unlikely(status->flag != SEC_SQE_CFLAG)) {
213 			dev_err_ratelimited(ctx->dev, "flag[%u]\n",
214 					    status->flag);
215 			return -EIO;
216 		}
217 	} else if (unlikely(ctx->alg_type == SEC_AEAD)) {
218 		if (unlikely(status->flag != SEC_SQE_AEAD_FLAG ||
219 			     status->icv == SEC_ICV_ERR)) {
220 			dev_err_ratelimited(ctx->dev,
221 					    "flag[%u], icv[%u]\n",
222 					    status->flag, status->icv);
223 			return -EBADMSG;
224 		}
225 	}
226 
227 	return 0;
228 }
229 
230 static void sec_req_cb(struct hisi_qp *qp, void *resp)
231 {
232 	struct sec_qp_ctx *qp_ctx = qp->qp_ctx;
233 	struct sec_dfx *dfx = &qp_ctx->ctx->sec->debug.dfx;
234 	u8 type_supported = qp_ctx->ctx->type_supported;
235 	struct bd_status status;
236 	struct sec_ctx *ctx;
237 	struct sec_req *req;
238 	int err;
239 	u8 type;
240 
241 	if (type_supported == SEC_BD_TYPE2) {
242 		type = pre_parse_finished_bd(&status, resp);
243 		req = qp_ctx->req_list[status.tag];
244 	} else {
245 		type = pre_parse_finished_bd3(&status, resp);
246 		req = (void *)(uintptr_t)status.tag;
247 	}
248 
249 	if (unlikely(type != type_supported)) {
250 		atomic64_inc(&dfx->err_bd_cnt);
251 		pr_err("err bd type [%u]\n", type);
252 		return;
253 	}
254 
255 	if (unlikely(!req)) {
256 		atomic64_inc(&dfx->invalid_req_cnt);
257 		atomic_inc(&qp->qp_status.used);
258 		return;
259 	}
260 
261 	req->err_type = status.err_type;
262 	ctx = req->ctx;
263 	err = sec_cb_status_check(req, &status);
264 	if (err)
265 		atomic64_inc(&dfx->done_flag_cnt);
266 
267 	atomic64_inc(&dfx->recv_cnt);
268 
269 	ctx->req_op->buf_unmap(ctx, req);
270 
271 	ctx->req_op->callback(ctx, req, err);
272 }
273 
274 static int sec_bd_send(struct sec_ctx *ctx, struct sec_req *req)
275 {
276 	struct sec_qp_ctx *qp_ctx = req->qp_ctx;
277 	int ret;
278 
279 	if (ctx->fake_req_limit <=
280 	    atomic_read(&qp_ctx->qp->qp_status.used) &&
281 	    !(req->flag & CRYPTO_TFM_REQ_MAY_BACKLOG))
282 		return -EBUSY;
283 
284 	spin_lock_bh(&qp_ctx->req_lock);
285 	ret = hisi_qp_send(qp_ctx->qp, &req->sec_sqe);
286 
287 	if (ctx->fake_req_limit <=
288 	    atomic_read(&qp_ctx->qp->qp_status.used) && !ret) {
289 		list_add_tail(&req->backlog_head, &qp_ctx->backlog);
290 		atomic64_inc(&ctx->sec->debug.dfx.send_cnt);
291 		atomic64_inc(&ctx->sec->debug.dfx.send_busy_cnt);
292 		spin_unlock_bh(&qp_ctx->req_lock);
293 		return -EBUSY;
294 	}
295 	spin_unlock_bh(&qp_ctx->req_lock);
296 
297 	if (unlikely(ret == -EBUSY))
298 		return -ENOBUFS;
299 
300 	if (likely(!ret)) {
301 		ret = -EINPROGRESS;
302 		atomic64_inc(&ctx->sec->debug.dfx.send_cnt);
303 	}
304 
305 	return ret;
306 }
307 
308 /* Get DMA memory resources */
309 static int sec_alloc_civ_resource(struct device *dev, struct sec_alg_res *res)
310 {
311 	u16 q_depth = res->depth;
312 	int i;
313 
314 	res->c_ivin = dma_alloc_coherent(dev, SEC_TOTAL_IV_SZ(q_depth),
315 					 &res->c_ivin_dma, GFP_KERNEL);
316 	if (!res->c_ivin)
317 		return -ENOMEM;
318 
319 	for (i = 1; i < q_depth; i++) {
320 		res[i].c_ivin_dma = res->c_ivin_dma + i * SEC_IV_SIZE;
321 		res[i].c_ivin = res->c_ivin + i * SEC_IV_SIZE;
322 	}
323 
324 	return 0;
325 }
326 
327 static void sec_free_civ_resource(struct device *dev, struct sec_alg_res *res)
328 {
329 	if (res->c_ivin)
330 		dma_free_coherent(dev, SEC_TOTAL_IV_SZ(res->depth),
331 				  res->c_ivin, res->c_ivin_dma);
332 }
333 
334 static int sec_alloc_aiv_resource(struct device *dev, struct sec_alg_res *res)
335 {
336 	u16 q_depth = res->depth;
337 	int i;
338 
339 	res->a_ivin = dma_alloc_coherent(dev, SEC_TOTAL_IV_SZ(q_depth),
340 					 &res->a_ivin_dma, GFP_KERNEL);
341 	if (!res->a_ivin)
342 		return -ENOMEM;
343 
344 	for (i = 1; i < q_depth; i++) {
345 		res[i].a_ivin_dma = res->a_ivin_dma + i * SEC_IV_SIZE;
346 		res[i].a_ivin = res->a_ivin + i * SEC_IV_SIZE;
347 	}
348 
349 	return 0;
350 }
351 
352 static void sec_free_aiv_resource(struct device *dev, struct sec_alg_res *res)
353 {
354 	if (res->a_ivin)
355 		dma_free_coherent(dev, SEC_TOTAL_IV_SZ(res->depth),
356 				  res->a_ivin, res->a_ivin_dma);
357 }
358 
359 static int sec_alloc_mac_resource(struct device *dev, struct sec_alg_res *res)
360 {
361 	u16 q_depth = res->depth;
362 	int i;
363 
364 	res->out_mac = dma_alloc_coherent(dev, SEC_TOTAL_MAC_SZ(q_depth) << 1,
365 					  &res->out_mac_dma, GFP_KERNEL);
366 	if (!res->out_mac)
367 		return -ENOMEM;
368 
369 	for (i = 1; i < q_depth; i++) {
370 		res[i].out_mac_dma = res->out_mac_dma +
371 				     i * (SEC_MAX_MAC_LEN << 1);
372 		res[i].out_mac = res->out_mac + i * (SEC_MAX_MAC_LEN << 1);
373 	}
374 
375 	return 0;
376 }
377 
378 static void sec_free_mac_resource(struct device *dev, struct sec_alg_res *res)
379 {
380 	if (res->out_mac)
381 		dma_free_coherent(dev, SEC_TOTAL_MAC_SZ(res->depth) << 1,
382 				  res->out_mac, res->out_mac_dma);
383 }
384 
385 static void sec_free_pbuf_resource(struct device *dev, struct sec_alg_res *res)
386 {
387 	if (res->pbuf)
388 		dma_free_coherent(dev, SEC_TOTAL_PBUF_SZ(res->depth),
389 				  res->pbuf, res->pbuf_dma);
390 }
391 
392 /*
393  * To improve performance, pbuffer is used for
394  * small packets (< 512Bytes) as IOMMU translation using.
395  */
396 static int sec_alloc_pbuf_resource(struct device *dev, struct sec_alg_res *res)
397 {
398 	u16 q_depth = res->depth;
399 	int size = SEC_PBUF_PAGE_NUM(q_depth);
400 	int pbuf_page_offset;
401 	int i, j, k;
402 
403 	res->pbuf = dma_alloc_coherent(dev, SEC_TOTAL_PBUF_SZ(q_depth),
404 				&res->pbuf_dma, GFP_KERNEL);
405 	if (!res->pbuf)
406 		return -ENOMEM;
407 
408 	/*
409 	 * SEC_PBUF_PKG contains data pbuf, iv and
410 	 * out_mac : <SEC_PBUF|SEC_IV|SEC_MAC>
411 	 * Every PAGE contains six SEC_PBUF_PKG
412 	 * The sec_qp_ctx contains QM_Q_DEPTH numbers of SEC_PBUF_PKG
413 	 * So we need SEC_PBUF_PAGE_NUM numbers of PAGE
414 	 * for the SEC_TOTAL_PBUF_SZ
415 	 */
416 	for (i = 0; i <= size; i++) {
417 		pbuf_page_offset = PAGE_SIZE * i;
418 		for (j = 0; j < SEC_PBUF_NUM; j++) {
419 			k = i * SEC_PBUF_NUM + j;
420 			if (k == q_depth)
421 				break;
422 			res[k].pbuf = res->pbuf +
423 				j * SEC_PBUF_PKG + pbuf_page_offset;
424 			res[k].pbuf_dma = res->pbuf_dma +
425 				j * SEC_PBUF_PKG + pbuf_page_offset;
426 		}
427 	}
428 
429 	return 0;
430 }
431 
432 static int sec_alg_resource_alloc(struct sec_ctx *ctx,
433 				  struct sec_qp_ctx *qp_ctx)
434 {
435 	struct sec_alg_res *res = qp_ctx->res;
436 	struct device *dev = ctx->dev;
437 	int ret;
438 
439 	ret = sec_alloc_civ_resource(dev, res);
440 	if (ret)
441 		return ret;
442 
443 	if (ctx->alg_type == SEC_AEAD) {
444 		ret = sec_alloc_aiv_resource(dev, res);
445 		if (ret)
446 			goto alloc_aiv_fail;
447 
448 		ret = sec_alloc_mac_resource(dev, res);
449 		if (ret)
450 			goto alloc_mac_fail;
451 	}
452 	if (ctx->pbuf_supported) {
453 		ret = sec_alloc_pbuf_resource(dev, res);
454 		if (ret) {
455 			dev_err(dev, "fail to alloc pbuf dma resource!\n");
456 			goto alloc_pbuf_fail;
457 		}
458 	}
459 
460 	return 0;
461 
462 alloc_pbuf_fail:
463 	if (ctx->alg_type == SEC_AEAD)
464 		sec_free_mac_resource(dev, qp_ctx->res);
465 alloc_mac_fail:
466 	if (ctx->alg_type == SEC_AEAD)
467 		sec_free_aiv_resource(dev, res);
468 alloc_aiv_fail:
469 	sec_free_civ_resource(dev, res);
470 	return ret;
471 }
472 
473 static void sec_alg_resource_free(struct sec_ctx *ctx,
474 				  struct sec_qp_ctx *qp_ctx)
475 {
476 	struct device *dev = ctx->dev;
477 
478 	sec_free_civ_resource(dev, qp_ctx->res);
479 
480 	if (ctx->pbuf_supported)
481 		sec_free_pbuf_resource(dev, qp_ctx->res);
482 	if (ctx->alg_type == SEC_AEAD)
483 		sec_free_mac_resource(dev, qp_ctx->res);
484 }
485 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 	/* Currently, only XTS mode need fallback tfm when using 192bit key */
693 	if (likely(strncmp(alg, "xts", SEC_XTS_NAME_SZ)))
694 		return 0;
695 
696 	c_ctx->fbtfm = crypto_alloc_sync_skcipher(alg, 0,
697 						  CRYPTO_ALG_NEED_FALLBACK);
698 	if (IS_ERR(c_ctx->fbtfm)) {
699 		pr_err("failed to alloc xts mode fallback tfm!\n");
700 		return PTR_ERR(c_ctx->fbtfm);
701 	}
702 
703 	return 0;
704 }
705 
706 static int sec_skcipher_init(struct crypto_skcipher *tfm)
707 {
708 	struct sec_ctx *ctx = crypto_skcipher_ctx(tfm);
709 	int ret;
710 
711 	ctx->alg_type = SEC_SKCIPHER;
712 	crypto_skcipher_set_reqsize(tfm, sizeof(struct sec_req));
713 	ctx->c_ctx.ivsize = crypto_skcipher_ivsize(tfm);
714 	if (ctx->c_ctx.ivsize > SEC_IV_SIZE) {
715 		pr_err("get error skcipher iv size!\n");
716 		return -EINVAL;
717 	}
718 
719 	ret = sec_ctx_base_init(ctx);
720 	if (ret)
721 		return ret;
722 
723 	ret = sec_cipher_init(ctx);
724 	if (ret)
725 		goto err_cipher_init;
726 
727 	ret = sec_skcipher_fbtfm_init(tfm);
728 	if (ret)
729 		goto err_fbtfm_init;
730 
731 	return 0;
732 
733 err_fbtfm_init:
734 	sec_cipher_uninit(ctx);
735 err_cipher_init:
736 	sec_ctx_base_uninit(ctx);
737 	return ret;
738 }
739 
740 static void sec_skcipher_uninit(struct crypto_skcipher *tfm)
741 {
742 	struct sec_ctx *ctx = crypto_skcipher_ctx(tfm);
743 
744 	if (ctx->c_ctx.fbtfm)
745 		crypto_free_sync_skcipher(ctx->c_ctx.fbtfm);
746 
747 	sec_cipher_uninit(ctx);
748 	sec_ctx_base_uninit(ctx);
749 }
750 
751 static int sec_skcipher_3des_setkey(struct crypto_skcipher *tfm, const u8 *key,
752 				    const u32 keylen,
753 				    const enum sec_cmode c_mode)
754 {
755 	struct sec_ctx *ctx = crypto_skcipher_ctx(tfm);
756 	struct sec_cipher_ctx *c_ctx = &ctx->c_ctx;
757 	int ret;
758 
759 	ret = verify_skcipher_des3_key(tfm, key);
760 	if (ret)
761 		return ret;
762 
763 	switch (keylen) {
764 	case SEC_DES3_2KEY_SIZE:
765 		c_ctx->c_key_len = SEC_CKEY_3DES_2KEY;
766 		break;
767 	case SEC_DES3_3KEY_SIZE:
768 		c_ctx->c_key_len = SEC_CKEY_3DES_3KEY;
769 		break;
770 	default:
771 		return -EINVAL;
772 	}
773 
774 	return 0;
775 }
776 
777 static int sec_skcipher_aes_sm4_setkey(struct sec_cipher_ctx *c_ctx,
778 				       const u32 keylen,
779 				       const enum sec_cmode c_mode)
780 {
781 	if (c_mode == SEC_CMODE_XTS) {
782 		switch (keylen) {
783 		case SEC_XTS_MIN_KEY_SIZE:
784 			c_ctx->c_key_len = SEC_CKEY_128BIT;
785 			break;
786 		case SEC_XTS_MID_KEY_SIZE:
787 			c_ctx->fallback = true;
788 			break;
789 		case SEC_XTS_MAX_KEY_SIZE:
790 			c_ctx->c_key_len = SEC_CKEY_256BIT;
791 			break;
792 		default:
793 			pr_err("hisi_sec2: xts mode key error!\n");
794 			return -EINVAL;
795 		}
796 	} else {
797 		if (c_ctx->c_alg == SEC_CALG_SM4 &&
798 		    keylen != AES_KEYSIZE_128) {
799 			pr_err("hisi_sec2: sm4 key error!\n");
800 			return -EINVAL;
801 		} else {
802 			switch (keylen) {
803 			case AES_KEYSIZE_128:
804 				c_ctx->c_key_len = SEC_CKEY_128BIT;
805 				break;
806 			case AES_KEYSIZE_192:
807 				c_ctx->c_key_len = SEC_CKEY_192BIT;
808 				break;
809 			case AES_KEYSIZE_256:
810 				c_ctx->c_key_len = SEC_CKEY_256BIT;
811 				break;
812 			default:
813 				pr_err("hisi_sec2: aes key error!\n");
814 				return -EINVAL;
815 			}
816 		}
817 	}
818 
819 	return 0;
820 }
821 
822 static int sec_skcipher_setkey(struct crypto_skcipher *tfm, const u8 *key,
823 			       const u32 keylen, const enum sec_calg c_alg,
824 			       const enum sec_cmode c_mode)
825 {
826 	struct sec_ctx *ctx = crypto_skcipher_ctx(tfm);
827 	struct sec_cipher_ctx *c_ctx = &ctx->c_ctx;
828 	struct device *dev = ctx->dev;
829 	int ret;
830 
831 	if (c_mode == SEC_CMODE_XTS) {
832 		ret = xts_verify_key(tfm, key, keylen);
833 		if (ret) {
834 			dev_err(dev, "xts mode key err!\n");
835 			return ret;
836 		}
837 	}
838 
839 	c_ctx->c_alg  = c_alg;
840 	c_ctx->c_mode = c_mode;
841 
842 	switch (c_alg) {
843 	case SEC_CALG_3DES:
844 		ret = sec_skcipher_3des_setkey(tfm, key, keylen, c_mode);
845 		break;
846 	case SEC_CALG_AES:
847 	case SEC_CALG_SM4:
848 		ret = sec_skcipher_aes_sm4_setkey(c_ctx, keylen, c_mode);
849 		break;
850 	default:
851 		return -EINVAL;
852 	}
853 
854 	if (ret) {
855 		dev_err(dev, "set sec key err!\n");
856 		return ret;
857 	}
858 
859 	memcpy(c_ctx->c_key, key, keylen);
860 	if (c_ctx->fallback && c_ctx->fbtfm) {
861 		ret = crypto_sync_skcipher_setkey(c_ctx->fbtfm, key, keylen);
862 		if (ret) {
863 			dev_err(dev, "failed to set fallback skcipher key!\n");
864 			return ret;
865 		}
866 	}
867 	return 0;
868 }
869 
870 #define GEN_SEC_SETKEY_FUNC(name, c_alg, c_mode)			\
871 static int sec_setkey_##name(struct crypto_skcipher *tfm, const u8 *key,\
872 	u32 keylen)							\
873 {									\
874 	return sec_skcipher_setkey(tfm, key, keylen, c_alg, c_mode);	\
875 }
876 
877 GEN_SEC_SETKEY_FUNC(aes_ecb, SEC_CALG_AES, SEC_CMODE_ECB)
878 GEN_SEC_SETKEY_FUNC(aes_cbc, SEC_CALG_AES, SEC_CMODE_CBC)
879 GEN_SEC_SETKEY_FUNC(aes_xts, SEC_CALG_AES, SEC_CMODE_XTS)
880 GEN_SEC_SETKEY_FUNC(aes_ofb, SEC_CALG_AES, SEC_CMODE_OFB)
881 GEN_SEC_SETKEY_FUNC(aes_cfb, SEC_CALG_AES, SEC_CMODE_CFB)
882 GEN_SEC_SETKEY_FUNC(aes_ctr, SEC_CALG_AES, SEC_CMODE_CTR)
883 GEN_SEC_SETKEY_FUNC(3des_ecb, SEC_CALG_3DES, SEC_CMODE_ECB)
884 GEN_SEC_SETKEY_FUNC(3des_cbc, SEC_CALG_3DES, SEC_CMODE_CBC)
885 GEN_SEC_SETKEY_FUNC(sm4_xts, SEC_CALG_SM4, SEC_CMODE_XTS)
886 GEN_SEC_SETKEY_FUNC(sm4_cbc, SEC_CALG_SM4, SEC_CMODE_CBC)
887 GEN_SEC_SETKEY_FUNC(sm4_ofb, SEC_CALG_SM4, SEC_CMODE_OFB)
888 GEN_SEC_SETKEY_FUNC(sm4_cfb, SEC_CALG_SM4, SEC_CMODE_CFB)
889 GEN_SEC_SETKEY_FUNC(sm4_ctr, SEC_CALG_SM4, SEC_CMODE_CTR)
890 
891 static int sec_cipher_pbuf_map(struct sec_ctx *ctx, struct sec_req *req,
892 			struct scatterlist *src)
893 {
894 	struct sec_aead_req *a_req = &req->aead_req;
895 	struct aead_request *aead_req = a_req->aead_req;
896 	struct sec_cipher_req *c_req = &req->c_req;
897 	struct sec_qp_ctx *qp_ctx = req->qp_ctx;
898 	struct device *dev = ctx->dev;
899 	int copy_size, pbuf_length;
900 	int req_id = req->req_id;
901 	struct crypto_aead *tfm;
902 	size_t authsize;
903 	u8 *mac_offset;
904 
905 	if (ctx->alg_type == SEC_AEAD)
906 		copy_size = aead_req->cryptlen + aead_req->assoclen;
907 	else
908 		copy_size = c_req->c_len;
909 
910 	pbuf_length = sg_copy_to_buffer(src, sg_nents(src),
911 			qp_ctx->res[req_id].pbuf, copy_size);
912 	if (unlikely(pbuf_length != copy_size)) {
913 		dev_err(dev, "copy src data to pbuf error!\n");
914 		return -EINVAL;
915 	}
916 	if (!c_req->encrypt && ctx->alg_type == SEC_AEAD) {
917 		tfm = crypto_aead_reqtfm(aead_req);
918 		authsize = crypto_aead_authsize(tfm);
919 		mac_offset = qp_ctx->res[req_id].pbuf + copy_size - authsize;
920 		memcpy(a_req->out_mac, mac_offset, authsize);
921 	}
922 
923 	req->in_dma = qp_ctx->res[req_id].pbuf_dma;
924 	c_req->c_out_dma = req->in_dma;
925 
926 	return 0;
927 }
928 
929 static void sec_cipher_pbuf_unmap(struct sec_ctx *ctx, struct sec_req *req,
930 			struct scatterlist *dst)
931 {
932 	struct aead_request *aead_req = req->aead_req.aead_req;
933 	struct sec_cipher_req *c_req = &req->c_req;
934 	struct sec_qp_ctx *qp_ctx = req->qp_ctx;
935 	int copy_size, pbuf_length;
936 	int req_id = req->req_id;
937 
938 	if (ctx->alg_type == SEC_AEAD)
939 		copy_size = c_req->c_len + aead_req->assoclen;
940 	else
941 		copy_size = c_req->c_len;
942 
943 	pbuf_length = sg_copy_from_buffer(dst, sg_nents(dst),
944 			qp_ctx->res[req_id].pbuf, copy_size);
945 	if (unlikely(pbuf_length != copy_size))
946 		dev_err(ctx->dev, "copy pbuf data to dst error!\n");
947 }
948 
949 static int sec_aead_mac_init(struct sec_aead_req *req)
950 {
951 	struct aead_request *aead_req = req->aead_req;
952 	struct crypto_aead *tfm = crypto_aead_reqtfm(aead_req);
953 	size_t authsize = crypto_aead_authsize(tfm);
954 	u8 *mac_out = req->out_mac;
955 	struct scatterlist *sgl = aead_req->src;
956 	size_t copy_size;
957 	off_t skip_size;
958 
959 	/* Copy input mac */
960 	skip_size = aead_req->assoclen + aead_req->cryptlen - authsize;
961 	copy_size = sg_pcopy_to_buffer(sgl, sg_nents(sgl), mac_out,
962 				       authsize, skip_size);
963 	if (unlikely(copy_size != authsize))
964 		return -EINVAL;
965 
966 	return 0;
967 }
968 
969 static int sec_cipher_map(struct sec_ctx *ctx, struct sec_req *req,
970 			  struct scatterlist *src, struct scatterlist *dst)
971 {
972 	struct sec_cipher_req *c_req = &req->c_req;
973 	struct sec_aead_req *a_req = &req->aead_req;
974 	struct sec_qp_ctx *qp_ctx = req->qp_ctx;
975 	struct sec_alg_res *res = &qp_ctx->res[req->req_id];
976 	struct device *dev = ctx->dev;
977 	int ret;
978 
979 	if (req->use_pbuf) {
980 		c_req->c_ivin = res->pbuf + SEC_PBUF_IV_OFFSET;
981 		c_req->c_ivin_dma = res->pbuf_dma + SEC_PBUF_IV_OFFSET;
982 		if (ctx->alg_type == SEC_AEAD) {
983 			a_req->a_ivin = res->a_ivin;
984 			a_req->a_ivin_dma = res->a_ivin_dma;
985 			a_req->out_mac = res->pbuf + SEC_PBUF_MAC_OFFSET;
986 			a_req->out_mac_dma = res->pbuf_dma +
987 					SEC_PBUF_MAC_OFFSET;
988 		}
989 		ret = sec_cipher_pbuf_map(ctx, req, src);
990 
991 		return ret;
992 	}
993 	c_req->c_ivin = res->c_ivin;
994 	c_req->c_ivin_dma = res->c_ivin_dma;
995 	if (ctx->alg_type == SEC_AEAD) {
996 		a_req->a_ivin = res->a_ivin;
997 		a_req->a_ivin_dma = res->a_ivin_dma;
998 		a_req->out_mac = res->out_mac;
999 		a_req->out_mac_dma = res->out_mac_dma;
1000 	}
1001 
1002 	req->in = hisi_acc_sg_buf_map_to_hw_sgl(dev, src,
1003 						qp_ctx->c_in_pool,
1004 						req->req_id,
1005 						&req->in_dma);
1006 	if (IS_ERR(req->in)) {
1007 		dev_err(dev, "fail to dma map input sgl buffers!\n");
1008 		return PTR_ERR(req->in);
1009 	}
1010 
1011 	if (!c_req->encrypt && ctx->alg_type == SEC_AEAD) {
1012 		ret = sec_aead_mac_init(a_req);
1013 		if (unlikely(ret)) {
1014 			dev_err(dev, "fail to init mac data for ICV!\n");
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 	if (crypto_authenc_extractkeys(&keys, key, keylen))
1177 		goto bad_key;
1178 
1179 	ret = sec_aead_aes_set_key(c_ctx, &keys);
1180 	if (ret) {
1181 		dev_err(dev, "set sec cipher key err!\n");
1182 		goto bad_key;
1183 	}
1184 
1185 	ret = sec_aead_auth_set_key(&ctx->a_ctx, &keys);
1186 	if (ret) {
1187 		dev_err(dev, "set sec auth key err!\n");
1188 		goto bad_key;
1189 	}
1190 
1191 	if ((ctx->a_ctx.mac_len & SEC_SQE_LEN_RATE_MASK)  ||
1192 	    (ctx->a_ctx.a_key_len & SEC_SQE_LEN_RATE_MASK)) {
1193 		dev_err(dev, "MAC or AUTH key length error!\n");
1194 		goto bad_key;
1195 	}
1196 
1197 	return 0;
1198 
1199 bad_key:
1200 	memzero_explicit(&keys, sizeof(struct crypto_authenc_keys));
1201 	return -EINVAL;
1202 }
1203 
1204 
1205 #define GEN_SEC_AEAD_SETKEY_FUNC(name, aalg, calg, maclen, cmode)	\
1206 static int sec_setkey_##name(struct crypto_aead *tfm, const u8 *key,	\
1207 	u32 keylen)							\
1208 {									\
1209 	return sec_aead_setkey(tfm, key, keylen, aalg, calg, maclen, cmode);\
1210 }
1211 
1212 GEN_SEC_AEAD_SETKEY_FUNC(aes_cbc_sha1, SEC_A_HMAC_SHA1,
1213 			 SEC_CALG_AES, SEC_HMAC_SHA1_MAC, SEC_CMODE_CBC)
1214 GEN_SEC_AEAD_SETKEY_FUNC(aes_cbc_sha256, SEC_A_HMAC_SHA256,
1215 			 SEC_CALG_AES, SEC_HMAC_SHA256_MAC, SEC_CMODE_CBC)
1216 GEN_SEC_AEAD_SETKEY_FUNC(aes_cbc_sha512, SEC_A_HMAC_SHA512,
1217 			 SEC_CALG_AES, SEC_HMAC_SHA512_MAC, SEC_CMODE_CBC)
1218 GEN_SEC_AEAD_SETKEY_FUNC(aes_ccm, 0, SEC_CALG_AES,
1219 			 SEC_HMAC_CCM_MAC, SEC_CMODE_CCM)
1220 GEN_SEC_AEAD_SETKEY_FUNC(aes_gcm, 0, SEC_CALG_AES,
1221 			 SEC_HMAC_GCM_MAC, SEC_CMODE_GCM)
1222 GEN_SEC_AEAD_SETKEY_FUNC(sm4_ccm, 0, SEC_CALG_SM4,
1223 			 SEC_HMAC_CCM_MAC, SEC_CMODE_CCM)
1224 GEN_SEC_AEAD_SETKEY_FUNC(sm4_gcm, 0, SEC_CALG_SM4,
1225 			 SEC_HMAC_GCM_MAC, SEC_CMODE_GCM)
1226 
1227 static int sec_aead_sgl_map(struct sec_ctx *ctx, struct sec_req *req)
1228 {
1229 	struct aead_request *aq = req->aead_req.aead_req;
1230 
1231 	return sec_cipher_map(ctx, req, aq->src, aq->dst);
1232 }
1233 
1234 static void sec_aead_sgl_unmap(struct sec_ctx *ctx, struct sec_req *req)
1235 {
1236 	struct aead_request *aq = req->aead_req.aead_req;
1237 
1238 	sec_cipher_unmap(ctx, req, aq->src, aq->dst);
1239 }
1240 
1241 static int sec_request_transfer(struct sec_ctx *ctx, struct sec_req *req)
1242 {
1243 	int ret;
1244 
1245 	ret = ctx->req_op->buf_map(ctx, req);
1246 	if (unlikely(ret))
1247 		return ret;
1248 
1249 	ctx->req_op->do_transfer(ctx, req);
1250 
1251 	ret = ctx->req_op->bd_fill(ctx, req);
1252 	if (unlikely(ret))
1253 		goto unmap_req_buf;
1254 
1255 	return ret;
1256 
1257 unmap_req_buf:
1258 	ctx->req_op->buf_unmap(ctx, req);
1259 	return ret;
1260 }
1261 
1262 static void sec_request_untransfer(struct sec_ctx *ctx, struct sec_req *req)
1263 {
1264 	ctx->req_op->buf_unmap(ctx, req);
1265 }
1266 
1267 static void sec_skcipher_copy_iv(struct sec_ctx *ctx, struct sec_req *req)
1268 {
1269 	struct skcipher_request *sk_req = req->c_req.sk_req;
1270 	struct sec_cipher_req *c_req = &req->c_req;
1271 
1272 	memcpy(c_req->c_ivin, sk_req->iv, ctx->c_ctx.ivsize);
1273 }
1274 
1275 static int sec_skcipher_bd_fill(struct sec_ctx *ctx, struct sec_req *req)
1276 {
1277 	struct sec_cipher_ctx *c_ctx = &ctx->c_ctx;
1278 	struct sec_cipher_req *c_req = &req->c_req;
1279 	struct sec_sqe *sec_sqe = &req->sec_sqe;
1280 	u8 scene, sa_type, da_type;
1281 	u8 bd_type, cipher;
1282 	u8 de = 0;
1283 
1284 	memset(sec_sqe, 0, sizeof(struct sec_sqe));
1285 
1286 	sec_sqe->type2.c_key_addr = cpu_to_le64(c_ctx->c_key_dma);
1287 	sec_sqe->type2.c_ivin_addr = cpu_to_le64(c_req->c_ivin_dma);
1288 	sec_sqe->type2.data_src_addr = cpu_to_le64(req->in_dma);
1289 	sec_sqe->type2.data_dst_addr = cpu_to_le64(c_req->c_out_dma);
1290 
1291 	sec_sqe->type2.icvw_kmode |= cpu_to_le16(((u16)c_ctx->c_mode) <<
1292 						SEC_CMODE_OFFSET);
1293 	sec_sqe->type2.c_alg = c_ctx->c_alg;
1294 	sec_sqe->type2.icvw_kmode |= cpu_to_le16(((u16)c_ctx->c_key_len) <<
1295 						SEC_CKEY_OFFSET);
1296 
1297 	bd_type = SEC_BD_TYPE2;
1298 	if (c_req->encrypt)
1299 		cipher = SEC_CIPHER_ENC << SEC_CIPHER_OFFSET;
1300 	else
1301 		cipher = SEC_CIPHER_DEC << SEC_CIPHER_OFFSET;
1302 	sec_sqe->type_cipher_auth = bd_type | cipher;
1303 
1304 	/* Set destination and source address type */
1305 	if (req->use_pbuf) {
1306 		sa_type = SEC_PBUF << SEC_SRC_SGL_OFFSET;
1307 		da_type = SEC_PBUF << SEC_DST_SGL_OFFSET;
1308 	} else {
1309 		sa_type = SEC_SGL << SEC_SRC_SGL_OFFSET;
1310 		da_type = SEC_SGL << SEC_DST_SGL_OFFSET;
1311 	}
1312 
1313 	sec_sqe->sdm_addr_type |= da_type;
1314 	scene = SEC_COMM_SCENE << SEC_SCENE_OFFSET;
1315 	if (req->in_dma != c_req->c_out_dma)
1316 		de = 0x1 << SEC_DE_OFFSET;
1317 
1318 	sec_sqe->sds_sa_type = (de | scene | sa_type);
1319 
1320 	sec_sqe->type2.clen_ivhlen |= cpu_to_le32(c_req->c_len);
1321 	sec_sqe->type2.tag = cpu_to_le16((u16)req->req_id);
1322 
1323 	return 0;
1324 }
1325 
1326 static int sec_skcipher_bd_fill_v3(struct sec_ctx *ctx, struct sec_req *req)
1327 {
1328 	struct sec_sqe3 *sec_sqe3 = &req->sec_sqe3;
1329 	struct sec_cipher_ctx *c_ctx = &ctx->c_ctx;
1330 	struct sec_cipher_req *c_req = &req->c_req;
1331 	u32 bd_param = 0;
1332 	u16 cipher;
1333 
1334 	memset(sec_sqe3, 0, sizeof(struct sec_sqe3));
1335 
1336 	sec_sqe3->c_key_addr = cpu_to_le64(c_ctx->c_key_dma);
1337 	sec_sqe3->no_scene.c_ivin_addr = cpu_to_le64(c_req->c_ivin_dma);
1338 	sec_sqe3->data_src_addr = cpu_to_le64(req->in_dma);
1339 	sec_sqe3->data_dst_addr = cpu_to_le64(c_req->c_out_dma);
1340 
1341 	sec_sqe3->c_mode_alg = ((u8)c_ctx->c_alg << SEC_CALG_OFFSET_V3) |
1342 						c_ctx->c_mode;
1343 	sec_sqe3->c_icv_key |= cpu_to_le16(((u16)c_ctx->c_key_len) <<
1344 						SEC_CKEY_OFFSET_V3);
1345 
1346 	if (c_req->encrypt)
1347 		cipher = SEC_CIPHER_ENC;
1348 	else
1349 		cipher = SEC_CIPHER_DEC;
1350 	sec_sqe3->c_icv_key |= cpu_to_le16(cipher);
1351 
1352 	/* Set the CTR counter mode is 128bit rollover */
1353 	sec_sqe3->auth_mac_key = cpu_to_le32((u32)SEC_CTR_CNT_ROLLOVER <<
1354 					SEC_CTR_CNT_OFFSET);
1355 
1356 	if (req->use_pbuf) {
1357 		bd_param |= SEC_PBUF << SEC_SRC_SGL_OFFSET_V3;
1358 		bd_param |= SEC_PBUF << SEC_DST_SGL_OFFSET_V3;
1359 	} else {
1360 		bd_param |= SEC_SGL << SEC_SRC_SGL_OFFSET_V3;
1361 		bd_param |= SEC_SGL << SEC_DST_SGL_OFFSET_V3;
1362 	}
1363 
1364 	bd_param |= SEC_COMM_SCENE << SEC_SCENE_OFFSET_V3;
1365 	if (req->in_dma != c_req->c_out_dma)
1366 		bd_param |= 0x1 << SEC_DE_OFFSET_V3;
1367 
1368 	bd_param |= SEC_BD_TYPE3;
1369 	sec_sqe3->bd_param = cpu_to_le32(bd_param);
1370 
1371 	sec_sqe3->c_len_ivin |= cpu_to_le32(c_req->c_len);
1372 	sec_sqe3->tag = cpu_to_le64(req);
1373 
1374 	return 0;
1375 }
1376 
1377 /* increment counter (128-bit int) */
1378 static void ctr_iv_inc(__u8 *counter, __u8 bits, __u32 nums)
1379 {
1380 	do {
1381 		--bits;
1382 		nums += counter[bits];
1383 		counter[bits] = nums & BITS_MASK;
1384 		nums >>= BYTE_BITS;
1385 	} while (bits && nums);
1386 }
1387 
1388 static void sec_update_iv(struct sec_req *req, enum sec_alg_type alg_type)
1389 {
1390 	struct aead_request *aead_req = req->aead_req.aead_req;
1391 	struct skcipher_request *sk_req = req->c_req.sk_req;
1392 	u32 iv_size = req->ctx->c_ctx.ivsize;
1393 	struct scatterlist *sgl;
1394 	unsigned int cryptlen;
1395 	size_t sz;
1396 	u8 *iv;
1397 
1398 	if (req->c_req.encrypt)
1399 		sgl = alg_type == SEC_SKCIPHER ? sk_req->dst : aead_req->dst;
1400 	else
1401 		sgl = alg_type == SEC_SKCIPHER ? sk_req->src : aead_req->src;
1402 
1403 	if (alg_type == SEC_SKCIPHER) {
1404 		iv = sk_req->iv;
1405 		cryptlen = sk_req->cryptlen;
1406 	} else {
1407 		iv = aead_req->iv;
1408 		cryptlen = aead_req->cryptlen;
1409 	}
1410 
1411 	if (req->ctx->c_ctx.c_mode == SEC_CMODE_CBC) {
1412 		sz = sg_pcopy_to_buffer(sgl, sg_nents(sgl), iv, iv_size,
1413 					cryptlen - iv_size);
1414 		if (unlikely(sz != iv_size))
1415 			dev_err(req->ctx->dev, "copy output iv error!\n");
1416 	} else {
1417 		sz = cryptlen / iv_size;
1418 		if (cryptlen % iv_size)
1419 			sz += 1;
1420 		ctr_iv_inc(iv, iv_size, sz);
1421 	}
1422 }
1423 
1424 static struct sec_req *sec_back_req_clear(struct sec_ctx *ctx,
1425 				struct sec_qp_ctx *qp_ctx)
1426 {
1427 	struct sec_req *backlog_req = NULL;
1428 
1429 	spin_lock_bh(&qp_ctx->req_lock);
1430 	if (ctx->fake_req_limit >=
1431 	    atomic_read(&qp_ctx->qp->qp_status.used) &&
1432 	    !list_empty(&qp_ctx->backlog)) {
1433 		backlog_req = list_first_entry(&qp_ctx->backlog,
1434 				typeof(*backlog_req), backlog_head);
1435 		list_del(&backlog_req->backlog_head);
1436 	}
1437 	spin_unlock_bh(&qp_ctx->req_lock);
1438 
1439 	return backlog_req;
1440 }
1441 
1442 static void sec_skcipher_callback(struct sec_ctx *ctx, struct sec_req *req,
1443 				  int err)
1444 {
1445 	struct skcipher_request *sk_req = req->c_req.sk_req;
1446 	struct sec_qp_ctx *qp_ctx = req->qp_ctx;
1447 	struct skcipher_request *backlog_sk_req;
1448 	struct sec_req *backlog_req;
1449 
1450 	sec_free_req_id(req);
1451 
1452 	/* IV output at encrypto of CBC/CTR mode */
1453 	if (!err && (ctx->c_ctx.c_mode == SEC_CMODE_CBC ||
1454 	    ctx->c_ctx.c_mode == SEC_CMODE_CTR) && req->c_req.encrypt)
1455 		sec_update_iv(req, SEC_SKCIPHER);
1456 
1457 	while (1) {
1458 		backlog_req = sec_back_req_clear(ctx, qp_ctx);
1459 		if (!backlog_req)
1460 			break;
1461 
1462 		backlog_sk_req = backlog_req->c_req.sk_req;
1463 		backlog_sk_req->base.complete(&backlog_sk_req->base,
1464 						-EINPROGRESS);
1465 		atomic64_inc(&ctx->sec->debug.dfx.recv_busy_cnt);
1466 	}
1467 
1468 	sk_req->base.complete(&sk_req->base, err);
1469 }
1470 
1471 static void set_aead_auth_iv(struct sec_ctx *ctx, struct sec_req *req)
1472 {
1473 	struct aead_request *aead_req = req->aead_req.aead_req;
1474 	struct sec_cipher_req *c_req = &req->c_req;
1475 	struct sec_aead_req *a_req = &req->aead_req;
1476 	size_t authsize = ctx->a_ctx.mac_len;
1477 	u32 data_size = aead_req->cryptlen;
1478 	u8 flage = 0;
1479 	u8 cm, cl;
1480 
1481 	/* the specification has been checked in aead_iv_demension_check() */
1482 	cl = c_req->c_ivin[0] + 1;
1483 	c_req->c_ivin[ctx->c_ctx.ivsize - cl] = 0x00;
1484 	memset(&c_req->c_ivin[ctx->c_ctx.ivsize - cl], 0, cl);
1485 	c_req->c_ivin[ctx->c_ctx.ivsize - IV_LAST_BYTE1] = IV_CTR_INIT;
1486 
1487 	/* the last 3bit is L' */
1488 	flage |= c_req->c_ivin[0] & IV_CL_MASK;
1489 
1490 	/* the M' is bit3~bit5, the Flags is bit6 */
1491 	cm = (authsize - IV_CM_CAL_NUM) / IV_CM_CAL_NUM;
1492 	flage |= cm << IV_CM_OFFSET;
1493 	if (aead_req->assoclen)
1494 		flage |= 0x01 << IV_FLAGS_OFFSET;
1495 
1496 	memcpy(a_req->a_ivin, c_req->c_ivin, ctx->c_ctx.ivsize);
1497 	a_req->a_ivin[0] = flage;
1498 
1499 	/*
1500 	 * the last 32bit is counter's initial number,
1501 	 * but the nonce uses the first 16bit
1502 	 * the tail 16bit fill with the cipher length
1503 	 */
1504 	if (!c_req->encrypt)
1505 		data_size = aead_req->cryptlen - authsize;
1506 
1507 	a_req->a_ivin[ctx->c_ctx.ivsize - IV_LAST_BYTE1] =
1508 			data_size & IV_LAST_BYTE_MASK;
1509 	data_size >>= IV_BYTE_OFFSET;
1510 	a_req->a_ivin[ctx->c_ctx.ivsize - IV_LAST_BYTE2] =
1511 			data_size & IV_LAST_BYTE_MASK;
1512 }
1513 
1514 static void sec_aead_set_iv(struct sec_ctx *ctx, struct sec_req *req)
1515 {
1516 	struct aead_request *aead_req = req->aead_req.aead_req;
1517 	struct crypto_aead *tfm = crypto_aead_reqtfm(aead_req);
1518 	size_t authsize = crypto_aead_authsize(tfm);
1519 	struct sec_cipher_req *c_req = &req->c_req;
1520 	struct sec_aead_req *a_req = &req->aead_req;
1521 
1522 	memcpy(c_req->c_ivin, aead_req->iv, ctx->c_ctx.ivsize);
1523 
1524 	if (ctx->c_ctx.c_mode == SEC_CMODE_CCM) {
1525 		/*
1526 		 * CCM 16Byte Cipher_IV: {1B_Flage,13B_IV,2B_counter},
1527 		 * the  counter must set to 0x01
1528 		 */
1529 		ctx->a_ctx.mac_len = authsize;
1530 		/* CCM 16Byte Auth_IV: {1B_AFlage,13B_IV,2B_Ptext_length} */
1531 		set_aead_auth_iv(ctx, req);
1532 	}
1533 
1534 	/* GCM 12Byte Cipher_IV == Auth_IV */
1535 	if (ctx->c_ctx.c_mode == SEC_CMODE_GCM) {
1536 		ctx->a_ctx.mac_len = authsize;
1537 		memcpy(a_req->a_ivin, c_req->c_ivin, SEC_AIV_SIZE);
1538 	}
1539 }
1540 
1541 static void sec_auth_bd_fill_xcm(struct sec_auth_ctx *ctx, int dir,
1542 				 struct sec_req *req, struct sec_sqe *sec_sqe)
1543 {
1544 	struct sec_aead_req *a_req = &req->aead_req;
1545 	struct aead_request *aq = a_req->aead_req;
1546 
1547 	/* C_ICV_Len is MAC size, 0x4 ~ 0x10 */
1548 	sec_sqe->type2.icvw_kmode |= cpu_to_le16((u16)ctx->mac_len);
1549 
1550 	/* mode set to CCM/GCM, don't set {A_Alg, AKey_Len, MAC_Len} */
1551 	sec_sqe->type2.a_key_addr = sec_sqe->type2.c_key_addr;
1552 	sec_sqe->type2.a_ivin_addr = cpu_to_le64(a_req->a_ivin_dma);
1553 	sec_sqe->type_cipher_auth |= SEC_NO_AUTH << SEC_AUTH_OFFSET;
1554 
1555 	if (dir)
1556 		sec_sqe->sds_sa_type &= SEC_CIPHER_AUTH;
1557 	else
1558 		sec_sqe->sds_sa_type |= SEC_AUTH_CIPHER;
1559 
1560 	sec_sqe->type2.alen_ivllen = cpu_to_le32(aq->assoclen);
1561 	sec_sqe->type2.auth_src_offset = cpu_to_le16(0x0);
1562 	sec_sqe->type2.cipher_src_offset = cpu_to_le16((u16)aq->assoclen);
1563 
1564 	sec_sqe->type2.mac_addr = cpu_to_le64(a_req->out_mac_dma);
1565 }
1566 
1567 static void sec_auth_bd_fill_xcm_v3(struct sec_auth_ctx *ctx, int dir,
1568 				    struct sec_req *req, struct sec_sqe3 *sqe3)
1569 {
1570 	struct sec_aead_req *a_req = &req->aead_req;
1571 	struct aead_request *aq = a_req->aead_req;
1572 
1573 	/* C_ICV_Len is MAC size, 0x4 ~ 0x10 */
1574 	sqe3->c_icv_key |= cpu_to_le16((u16)ctx->mac_len << SEC_MAC_OFFSET_V3);
1575 
1576 	/* mode set to CCM/GCM, don't set {A_Alg, AKey_Len, MAC_Len} */
1577 	sqe3->a_key_addr = sqe3->c_key_addr;
1578 	sqe3->auth_ivin.a_ivin_addr = cpu_to_le64(a_req->a_ivin_dma);
1579 	sqe3->auth_mac_key |= SEC_NO_AUTH;
1580 
1581 	if (dir)
1582 		sqe3->huk_iv_seq &= SEC_CIPHER_AUTH_V3;
1583 	else
1584 		sqe3->huk_iv_seq |= SEC_AUTH_CIPHER_V3;
1585 
1586 	sqe3->a_len_key = cpu_to_le32(aq->assoclen);
1587 	sqe3->auth_src_offset = cpu_to_le16(0x0);
1588 	sqe3->cipher_src_offset = cpu_to_le16((u16)aq->assoclen);
1589 	sqe3->mac_addr = cpu_to_le64(a_req->out_mac_dma);
1590 }
1591 
1592 static void sec_auth_bd_fill_ex(struct sec_auth_ctx *ctx, int dir,
1593 			       struct sec_req *req, struct sec_sqe *sec_sqe)
1594 {
1595 	struct sec_aead_req *a_req = &req->aead_req;
1596 	struct sec_cipher_req *c_req = &req->c_req;
1597 	struct aead_request *aq = a_req->aead_req;
1598 
1599 	sec_sqe->type2.a_key_addr = cpu_to_le64(ctx->a_key_dma);
1600 
1601 	sec_sqe->type2.mac_key_alg =
1602 			cpu_to_le32(ctx->mac_len / SEC_SQE_LEN_RATE);
1603 
1604 	sec_sqe->type2.mac_key_alg |=
1605 			cpu_to_le32((u32)((ctx->a_key_len) /
1606 			SEC_SQE_LEN_RATE) << SEC_AKEY_OFFSET);
1607 
1608 	sec_sqe->type2.mac_key_alg |=
1609 			cpu_to_le32((u32)(ctx->a_alg) << SEC_AEAD_ALG_OFFSET);
1610 
1611 	if (dir) {
1612 		sec_sqe->type_cipher_auth |= SEC_AUTH_TYPE1 << SEC_AUTH_OFFSET;
1613 		sec_sqe->sds_sa_type &= SEC_CIPHER_AUTH;
1614 	} else {
1615 		sec_sqe->type_cipher_auth |= SEC_AUTH_TYPE2 << SEC_AUTH_OFFSET;
1616 		sec_sqe->sds_sa_type |= SEC_AUTH_CIPHER;
1617 	}
1618 	sec_sqe->type2.alen_ivllen = cpu_to_le32(c_req->c_len + aq->assoclen);
1619 
1620 	sec_sqe->type2.cipher_src_offset = cpu_to_le16((u16)aq->assoclen);
1621 
1622 	sec_sqe->type2.mac_addr = cpu_to_le64(a_req->out_mac_dma);
1623 }
1624 
1625 static int sec_aead_bd_fill(struct sec_ctx *ctx, struct sec_req *req)
1626 {
1627 	struct sec_auth_ctx *auth_ctx = &ctx->a_ctx;
1628 	struct sec_sqe *sec_sqe = &req->sec_sqe;
1629 	int ret;
1630 
1631 	ret = sec_skcipher_bd_fill(ctx, req);
1632 	if (unlikely(ret)) {
1633 		dev_err(ctx->dev, "skcipher bd fill is error!\n");
1634 		return ret;
1635 	}
1636 
1637 	if (ctx->c_ctx.c_mode == SEC_CMODE_CCM ||
1638 	    ctx->c_ctx.c_mode == SEC_CMODE_GCM)
1639 		sec_auth_bd_fill_xcm(auth_ctx, req->c_req.encrypt, req, sec_sqe);
1640 	else
1641 		sec_auth_bd_fill_ex(auth_ctx, req->c_req.encrypt, req, sec_sqe);
1642 
1643 	return 0;
1644 }
1645 
1646 static void sec_auth_bd_fill_ex_v3(struct sec_auth_ctx *ctx, int dir,
1647 				   struct sec_req *req, struct sec_sqe3 *sqe3)
1648 {
1649 	struct sec_aead_req *a_req = &req->aead_req;
1650 	struct sec_cipher_req *c_req = &req->c_req;
1651 	struct aead_request *aq = a_req->aead_req;
1652 
1653 	sqe3->a_key_addr = cpu_to_le64(ctx->a_key_dma);
1654 
1655 	sqe3->auth_mac_key |=
1656 			cpu_to_le32((u32)(ctx->mac_len /
1657 			SEC_SQE_LEN_RATE) << SEC_MAC_OFFSET_V3);
1658 
1659 	sqe3->auth_mac_key |=
1660 			cpu_to_le32((u32)(ctx->a_key_len /
1661 			SEC_SQE_LEN_RATE) << SEC_AKEY_OFFSET_V3);
1662 
1663 	sqe3->auth_mac_key |=
1664 			cpu_to_le32((u32)(ctx->a_alg) << SEC_AUTH_ALG_OFFSET_V3);
1665 
1666 	if (dir) {
1667 		sqe3->auth_mac_key |= cpu_to_le32((u32)SEC_AUTH_TYPE1);
1668 		sqe3->huk_iv_seq &= SEC_CIPHER_AUTH_V3;
1669 	} else {
1670 		sqe3->auth_mac_key |= cpu_to_le32((u32)SEC_AUTH_TYPE2);
1671 		sqe3->huk_iv_seq |= SEC_AUTH_CIPHER_V3;
1672 	}
1673 	sqe3->a_len_key = cpu_to_le32(c_req->c_len + aq->assoclen);
1674 
1675 	sqe3->cipher_src_offset = cpu_to_le16((u16)aq->assoclen);
1676 
1677 	sqe3->mac_addr = cpu_to_le64(a_req->out_mac_dma);
1678 }
1679 
1680 static int sec_aead_bd_fill_v3(struct sec_ctx *ctx, struct sec_req *req)
1681 {
1682 	struct sec_auth_ctx *auth_ctx = &ctx->a_ctx;
1683 	struct sec_sqe3 *sec_sqe3 = &req->sec_sqe3;
1684 	int ret;
1685 
1686 	ret = sec_skcipher_bd_fill_v3(ctx, req);
1687 	if (unlikely(ret)) {
1688 		dev_err(ctx->dev, "skcipher bd3 fill is error!\n");
1689 		return ret;
1690 	}
1691 
1692 	if (ctx->c_ctx.c_mode == SEC_CMODE_CCM ||
1693 	    ctx->c_ctx.c_mode == SEC_CMODE_GCM)
1694 		sec_auth_bd_fill_xcm_v3(auth_ctx, req->c_req.encrypt,
1695 					req, sec_sqe3);
1696 	else
1697 		sec_auth_bd_fill_ex_v3(auth_ctx, req->c_req.encrypt,
1698 				       req, sec_sqe3);
1699 
1700 	return 0;
1701 }
1702 
1703 static void sec_aead_callback(struct sec_ctx *c, struct sec_req *req, int err)
1704 {
1705 	struct aead_request *a_req = req->aead_req.aead_req;
1706 	struct crypto_aead *tfm = crypto_aead_reqtfm(a_req);
1707 	struct sec_aead_req *aead_req = &req->aead_req;
1708 	struct sec_cipher_req *c_req = &req->c_req;
1709 	size_t authsize = crypto_aead_authsize(tfm);
1710 	struct sec_qp_ctx *qp_ctx = req->qp_ctx;
1711 	struct aead_request *backlog_aead_req;
1712 	struct sec_req *backlog_req;
1713 	size_t sz;
1714 
1715 	if (!err && c->c_ctx.c_mode == SEC_CMODE_CBC && c_req->encrypt)
1716 		sec_update_iv(req, SEC_AEAD);
1717 
1718 	/* Copy output mac */
1719 	if (!err && c_req->encrypt) {
1720 		struct scatterlist *sgl = a_req->dst;
1721 
1722 		sz = sg_pcopy_from_buffer(sgl, sg_nents(sgl),
1723 					  aead_req->out_mac,
1724 					  authsize, a_req->cryptlen +
1725 					  a_req->assoclen);
1726 		if (unlikely(sz != authsize)) {
1727 			dev_err(c->dev, "copy out mac err!\n");
1728 			err = -EINVAL;
1729 		}
1730 	}
1731 
1732 	sec_free_req_id(req);
1733 
1734 	while (1) {
1735 		backlog_req = sec_back_req_clear(c, qp_ctx);
1736 		if (!backlog_req)
1737 			break;
1738 
1739 		backlog_aead_req = backlog_req->aead_req.aead_req;
1740 		backlog_aead_req->base.complete(&backlog_aead_req->base,
1741 						-EINPROGRESS);
1742 		atomic64_inc(&c->sec->debug.dfx.recv_busy_cnt);
1743 	}
1744 
1745 	a_req->base.complete(&a_req->base, 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_ckeck(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_CFB:
2035 	case SEC_CMODE_OFB:
2036 	case SEC_CMODE_CTR:
2037 		if (unlikely(ctx->sec->qm.ver < QM_HW_V3)) {
2038 			dev_err(dev, "skcipher HW version error!\n");
2039 			ret = -EINVAL;
2040 		}
2041 		break;
2042 	default:
2043 		ret = -EINVAL;
2044 	}
2045 
2046 	return ret;
2047 }
2048 
2049 static int sec_skcipher_param_check(struct sec_ctx *ctx, struct sec_req *sreq)
2050 {
2051 	struct skcipher_request *sk_req = sreq->c_req.sk_req;
2052 	struct device *dev = ctx->dev;
2053 	u8 c_alg = ctx->c_ctx.c_alg;
2054 
2055 	if (unlikely(!sk_req->src || !sk_req->dst ||
2056 		     sk_req->cryptlen > MAX_INPUT_DATA_LEN)) {
2057 		dev_err(dev, "skcipher input param error!\n");
2058 		return -EINVAL;
2059 	}
2060 	sreq->c_req.c_len = sk_req->cryptlen;
2061 
2062 	if (ctx->pbuf_supported && sk_req->cryptlen <= SEC_PBUF_SZ)
2063 		sreq->use_pbuf = true;
2064 	else
2065 		sreq->use_pbuf = false;
2066 
2067 	if (c_alg == SEC_CALG_3DES) {
2068 		if (unlikely(sk_req->cryptlen & (DES3_EDE_BLOCK_SIZE - 1))) {
2069 			dev_err(dev, "skcipher 3des input length error!\n");
2070 			return -EINVAL;
2071 		}
2072 		return 0;
2073 	} else if (c_alg == SEC_CALG_AES || c_alg == SEC_CALG_SM4) {
2074 		return sec_skcipher_cryptlen_ckeck(ctx, sreq);
2075 	}
2076 
2077 	dev_err(dev, "skcipher algorithm error!\n");
2078 
2079 	return -EINVAL;
2080 }
2081 
2082 static int sec_skcipher_soft_crypto(struct sec_ctx *ctx,
2083 				    struct skcipher_request *sreq, bool encrypt)
2084 {
2085 	struct sec_cipher_ctx *c_ctx = &ctx->c_ctx;
2086 	SYNC_SKCIPHER_REQUEST_ON_STACK(subreq, c_ctx->fbtfm);
2087 	struct device *dev = ctx->dev;
2088 	int ret;
2089 
2090 	if (!c_ctx->fbtfm) {
2091 		dev_err_ratelimited(dev, "the soft tfm isn't supported in the current system.\n");
2092 		return -EINVAL;
2093 	}
2094 
2095 	skcipher_request_set_sync_tfm(subreq, c_ctx->fbtfm);
2096 
2097 	/* software need sync mode to do crypto */
2098 	skcipher_request_set_callback(subreq, sreq->base.flags,
2099 				      NULL, NULL);
2100 	skcipher_request_set_crypt(subreq, sreq->src, sreq->dst,
2101 				   sreq->cryptlen, sreq->iv);
2102 	if (encrypt)
2103 		ret = crypto_skcipher_encrypt(subreq);
2104 	else
2105 		ret = crypto_skcipher_decrypt(subreq);
2106 
2107 	skcipher_request_zero(subreq);
2108 
2109 	return ret;
2110 }
2111 
2112 static int sec_skcipher_crypto(struct skcipher_request *sk_req, bool encrypt)
2113 {
2114 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(sk_req);
2115 	struct sec_req *req = skcipher_request_ctx(sk_req);
2116 	struct sec_ctx *ctx = crypto_skcipher_ctx(tfm);
2117 	int ret;
2118 
2119 	if (!sk_req->cryptlen) {
2120 		if (ctx->c_ctx.c_mode == SEC_CMODE_XTS)
2121 			return -EINVAL;
2122 		return 0;
2123 	}
2124 
2125 	req->flag = sk_req->base.flags;
2126 	req->c_req.sk_req = sk_req;
2127 	req->c_req.encrypt = encrypt;
2128 	req->ctx = ctx;
2129 
2130 	ret = sec_skcipher_param_check(ctx, req);
2131 	if (unlikely(ret))
2132 		return -EINVAL;
2133 
2134 	if (unlikely(ctx->c_ctx.fallback))
2135 		return sec_skcipher_soft_crypto(ctx, sk_req, encrypt);
2136 
2137 	return ctx->req_op->process(ctx, req);
2138 }
2139 
2140 static int sec_skcipher_encrypt(struct skcipher_request *sk_req)
2141 {
2142 	return sec_skcipher_crypto(sk_req, true);
2143 }
2144 
2145 static int sec_skcipher_decrypt(struct skcipher_request *sk_req)
2146 {
2147 	return sec_skcipher_crypto(sk_req, false);
2148 }
2149 
2150 #define SEC_SKCIPHER_GEN_ALG(sec_cra_name, sec_set_key, sec_min_key_size, \
2151 	sec_max_key_size, ctx_init, ctx_exit, blk_size, iv_size)\
2152 {\
2153 	.base = {\
2154 		.cra_name = sec_cra_name,\
2155 		.cra_driver_name = "hisi_sec_"sec_cra_name,\
2156 		.cra_priority = SEC_PRIORITY,\
2157 		.cra_flags = CRYPTO_ALG_ASYNC |\
2158 		 CRYPTO_ALG_NEED_FALLBACK,\
2159 		.cra_blocksize = blk_size,\
2160 		.cra_ctxsize = sizeof(struct sec_ctx),\
2161 		.cra_module = THIS_MODULE,\
2162 	},\
2163 	.init = ctx_init,\
2164 	.exit = ctx_exit,\
2165 	.setkey = sec_set_key,\
2166 	.decrypt = sec_skcipher_decrypt,\
2167 	.encrypt = sec_skcipher_encrypt,\
2168 	.min_keysize = sec_min_key_size,\
2169 	.max_keysize = sec_max_key_size,\
2170 	.ivsize = iv_size,\
2171 }
2172 
2173 #define SEC_SKCIPHER_ALG(name, key_func, min_key_size, \
2174 	max_key_size, blk_size, iv_size) \
2175 	SEC_SKCIPHER_GEN_ALG(name, key_func, min_key_size, max_key_size, \
2176 	sec_skcipher_ctx_init, sec_skcipher_ctx_exit, blk_size, iv_size)
2177 
2178 static struct sec_skcipher sec_skciphers[] = {
2179 	{
2180 		.alg_msk = BIT(0),
2181 		.alg = SEC_SKCIPHER_ALG("ecb(aes)", sec_setkey_aes_ecb, AES_MIN_KEY_SIZE,
2182 					AES_MAX_KEY_SIZE, AES_BLOCK_SIZE, 0),
2183 	},
2184 	{
2185 		.alg_msk = BIT(1),
2186 		.alg = SEC_SKCIPHER_ALG("cbc(aes)", sec_setkey_aes_cbc, AES_MIN_KEY_SIZE,
2187 					AES_MAX_KEY_SIZE, AES_BLOCK_SIZE, AES_BLOCK_SIZE),
2188 	},
2189 	{
2190 		.alg_msk = BIT(2),
2191 		.alg = SEC_SKCIPHER_ALG("ctr(aes)", sec_setkey_aes_ctr,	AES_MIN_KEY_SIZE,
2192 					AES_MAX_KEY_SIZE, SEC_MIN_BLOCK_SZ, AES_BLOCK_SIZE),
2193 	},
2194 	{
2195 		.alg_msk = BIT(3),
2196 		.alg = SEC_SKCIPHER_ALG("xts(aes)", sec_setkey_aes_xts,	SEC_XTS_MIN_KEY_SIZE,
2197 					SEC_XTS_MAX_KEY_SIZE, AES_BLOCK_SIZE, AES_BLOCK_SIZE),
2198 	},
2199 	{
2200 		.alg_msk = BIT(4),
2201 		.alg = SEC_SKCIPHER_ALG("ofb(aes)", sec_setkey_aes_ofb,	AES_MIN_KEY_SIZE,
2202 					AES_MAX_KEY_SIZE, SEC_MIN_BLOCK_SZ, AES_BLOCK_SIZE),
2203 	},
2204 	{
2205 		.alg_msk = BIT(5),
2206 		.alg = SEC_SKCIPHER_ALG("cfb(aes)", sec_setkey_aes_cfb,	AES_MIN_KEY_SIZE,
2207 					AES_MAX_KEY_SIZE, SEC_MIN_BLOCK_SZ, AES_BLOCK_SIZE),
2208 	},
2209 	{
2210 		.alg_msk = BIT(12),
2211 		.alg = SEC_SKCIPHER_ALG("cbc(sm4)", sec_setkey_sm4_cbc,	AES_MIN_KEY_SIZE,
2212 					AES_MIN_KEY_SIZE, AES_BLOCK_SIZE, AES_BLOCK_SIZE),
2213 	},
2214 	{
2215 		.alg_msk = BIT(13),
2216 		.alg = SEC_SKCIPHER_ALG("ctr(sm4)", sec_setkey_sm4_ctr, AES_MIN_KEY_SIZE,
2217 					AES_MIN_KEY_SIZE, SEC_MIN_BLOCK_SZ, AES_BLOCK_SIZE),
2218 	},
2219 	{
2220 		.alg_msk = BIT(14),
2221 		.alg = SEC_SKCIPHER_ALG("xts(sm4)", sec_setkey_sm4_xts,	SEC_XTS_MIN_KEY_SIZE,
2222 					SEC_XTS_MIN_KEY_SIZE, AES_BLOCK_SIZE, AES_BLOCK_SIZE),
2223 	},
2224 	{
2225 		.alg_msk = BIT(15),
2226 		.alg = SEC_SKCIPHER_ALG("ofb(sm4)", sec_setkey_sm4_ofb,	AES_MIN_KEY_SIZE,
2227 					AES_MIN_KEY_SIZE, SEC_MIN_BLOCK_SZ, AES_BLOCK_SIZE),
2228 	},
2229 	{
2230 		.alg_msk = BIT(16),
2231 		.alg = SEC_SKCIPHER_ALG("cfb(sm4)", sec_setkey_sm4_cfb,	AES_MIN_KEY_SIZE,
2232 					AES_MIN_KEY_SIZE, SEC_MIN_BLOCK_SZ, AES_BLOCK_SIZE),
2233 	},
2234 	{
2235 		.alg_msk = BIT(23),
2236 		.alg = SEC_SKCIPHER_ALG("ecb(des3_ede)", sec_setkey_3des_ecb, SEC_DES3_3KEY_SIZE,
2237 					SEC_DES3_3KEY_SIZE, DES3_EDE_BLOCK_SIZE, 0),
2238 	},
2239 	{
2240 		.alg_msk = BIT(24),
2241 		.alg = SEC_SKCIPHER_ALG("cbc(des3_ede)", sec_setkey_3des_cbc, SEC_DES3_3KEY_SIZE,
2242 					SEC_DES3_3KEY_SIZE, DES3_EDE_BLOCK_SIZE,
2243 					DES3_EDE_BLOCK_SIZE),
2244 	},
2245 };
2246 
2247 static int aead_iv_demension_check(struct aead_request *aead_req)
2248 {
2249 	u8 cl;
2250 
2251 	cl = aead_req->iv[0] + 1;
2252 	if (cl < IV_CL_MIN || cl > IV_CL_MAX)
2253 		return -EINVAL;
2254 
2255 	if (cl < IV_CL_MID && aead_req->cryptlen >> (BYTE_BITS * cl))
2256 		return -EOVERFLOW;
2257 
2258 	return 0;
2259 }
2260 
2261 static int sec_aead_spec_check(struct sec_ctx *ctx, struct sec_req *sreq)
2262 {
2263 	struct aead_request *req = sreq->aead_req.aead_req;
2264 	struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2265 	size_t authsize = crypto_aead_authsize(tfm);
2266 	u8 c_mode = ctx->c_ctx.c_mode;
2267 	struct device *dev = ctx->dev;
2268 	int ret;
2269 
2270 	if (unlikely(req->cryptlen + req->assoclen > MAX_INPUT_DATA_LEN ||
2271 	    req->assoclen > SEC_MAX_AAD_LEN)) {
2272 		dev_err(dev, "aead input spec error!\n");
2273 		return -EINVAL;
2274 	}
2275 
2276 	if (unlikely((c_mode == SEC_CMODE_GCM && authsize < DES_BLOCK_SIZE) ||
2277 	   (c_mode == SEC_CMODE_CCM && (authsize < MIN_MAC_LEN ||
2278 		authsize & MAC_LEN_MASK)))) {
2279 		dev_err(dev, "aead input mac length error!\n");
2280 		return -EINVAL;
2281 	}
2282 
2283 	if (c_mode == SEC_CMODE_CCM) {
2284 		if (unlikely(req->assoclen > SEC_MAX_CCM_AAD_LEN)) {
2285 			dev_err_ratelimited(dev, "CCM input aad parameter is too long!\n");
2286 			return -EINVAL;
2287 		}
2288 		ret = aead_iv_demension_check(req);
2289 		if (ret) {
2290 			dev_err(dev, "aead input iv param error!\n");
2291 			return ret;
2292 		}
2293 	}
2294 
2295 	if (sreq->c_req.encrypt)
2296 		sreq->c_req.c_len = req->cryptlen;
2297 	else
2298 		sreq->c_req.c_len = req->cryptlen - authsize;
2299 	if (c_mode == SEC_CMODE_CBC) {
2300 		if (unlikely(sreq->c_req.c_len & (AES_BLOCK_SIZE - 1))) {
2301 			dev_err(dev, "aead crypto length error!\n");
2302 			return -EINVAL;
2303 		}
2304 	}
2305 
2306 	return 0;
2307 }
2308 
2309 static int sec_aead_param_check(struct sec_ctx *ctx, struct sec_req *sreq)
2310 {
2311 	struct aead_request *req = sreq->aead_req.aead_req;
2312 	struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2313 	size_t authsize = crypto_aead_authsize(tfm);
2314 	struct device *dev = ctx->dev;
2315 	u8 c_alg = ctx->c_ctx.c_alg;
2316 
2317 	if (unlikely(!req->src || !req->dst)) {
2318 		dev_err(dev, "aead input param error!\n");
2319 		return -EINVAL;
2320 	}
2321 
2322 	if (ctx->sec->qm.ver == QM_HW_V2) {
2323 		if (unlikely(!req->cryptlen || (!sreq->c_req.encrypt &&
2324 		    req->cryptlen <= authsize))) {
2325 			ctx->a_ctx.fallback = true;
2326 			return -EINVAL;
2327 		}
2328 	}
2329 
2330 	/* Support AES or SM4 */
2331 	if (unlikely(c_alg != SEC_CALG_AES && c_alg != SEC_CALG_SM4)) {
2332 		dev_err(dev, "aead crypto alg error!\n");
2333 		return -EINVAL;
2334 	}
2335 
2336 	if (unlikely(sec_aead_spec_check(ctx, sreq)))
2337 		return -EINVAL;
2338 
2339 	if (ctx->pbuf_supported && (req->cryptlen + req->assoclen) <=
2340 		SEC_PBUF_SZ)
2341 		sreq->use_pbuf = true;
2342 	else
2343 		sreq->use_pbuf = false;
2344 
2345 	return 0;
2346 }
2347 
2348 static int sec_aead_soft_crypto(struct sec_ctx *ctx,
2349 				struct aead_request *aead_req,
2350 				bool encrypt)
2351 {
2352 	struct sec_auth_ctx *a_ctx = &ctx->a_ctx;
2353 	struct device *dev = ctx->dev;
2354 	struct aead_request *subreq;
2355 	int ret;
2356 
2357 	/* Kunpeng920 aead mode not support input 0 size */
2358 	if (!a_ctx->fallback_aead_tfm) {
2359 		dev_err(dev, "aead fallback tfm is NULL!\n");
2360 		return -EINVAL;
2361 	}
2362 
2363 	subreq = aead_request_alloc(a_ctx->fallback_aead_tfm, GFP_KERNEL);
2364 	if (!subreq)
2365 		return -ENOMEM;
2366 
2367 	aead_request_set_tfm(subreq, a_ctx->fallback_aead_tfm);
2368 	aead_request_set_callback(subreq, aead_req->base.flags,
2369 				  aead_req->base.complete, aead_req->base.data);
2370 	aead_request_set_crypt(subreq, aead_req->src, aead_req->dst,
2371 			       aead_req->cryptlen, aead_req->iv);
2372 	aead_request_set_ad(subreq, aead_req->assoclen);
2373 
2374 	if (encrypt)
2375 		ret = crypto_aead_encrypt(subreq);
2376 	else
2377 		ret = crypto_aead_decrypt(subreq);
2378 	aead_request_free(subreq);
2379 
2380 	return ret;
2381 }
2382 
2383 static int sec_aead_crypto(struct aead_request *a_req, bool encrypt)
2384 {
2385 	struct crypto_aead *tfm = crypto_aead_reqtfm(a_req);
2386 	struct sec_req *req = aead_request_ctx(a_req);
2387 	struct sec_ctx *ctx = crypto_aead_ctx(tfm);
2388 	int ret;
2389 
2390 	req->flag = a_req->base.flags;
2391 	req->aead_req.aead_req = a_req;
2392 	req->c_req.encrypt = encrypt;
2393 	req->ctx = ctx;
2394 
2395 	ret = sec_aead_param_check(ctx, req);
2396 	if (unlikely(ret)) {
2397 		if (ctx->a_ctx.fallback)
2398 			return sec_aead_soft_crypto(ctx, a_req, encrypt);
2399 		return -EINVAL;
2400 	}
2401 
2402 	return ctx->req_op->process(ctx, req);
2403 }
2404 
2405 static int sec_aead_encrypt(struct aead_request *a_req)
2406 {
2407 	return sec_aead_crypto(a_req, true);
2408 }
2409 
2410 static int sec_aead_decrypt(struct aead_request *a_req)
2411 {
2412 	return sec_aead_crypto(a_req, false);
2413 }
2414 
2415 #define SEC_AEAD_ALG(sec_cra_name, sec_set_key, ctx_init,\
2416 			 ctx_exit, blk_size, iv_size, max_authsize)\
2417 {\
2418 	.base = {\
2419 		.cra_name = sec_cra_name,\
2420 		.cra_driver_name = "hisi_sec_"sec_cra_name,\
2421 		.cra_priority = SEC_PRIORITY,\
2422 		.cra_flags = CRYPTO_ALG_ASYNC |\
2423 		 CRYPTO_ALG_NEED_FALLBACK,\
2424 		.cra_blocksize = blk_size,\
2425 		.cra_ctxsize = sizeof(struct sec_ctx),\
2426 		.cra_module = THIS_MODULE,\
2427 	},\
2428 	.init = ctx_init,\
2429 	.exit = ctx_exit,\
2430 	.setkey = sec_set_key,\
2431 	.setauthsize = sec_aead_setauthsize,\
2432 	.decrypt = sec_aead_decrypt,\
2433 	.encrypt = sec_aead_encrypt,\
2434 	.ivsize = iv_size,\
2435 	.maxauthsize = max_authsize,\
2436 }
2437 
2438 static struct sec_aead sec_aeads[] = {
2439 	{
2440 		.alg_msk = BIT(6),
2441 		.alg = SEC_AEAD_ALG("ccm(aes)", sec_setkey_aes_ccm, sec_aead_xcm_ctx_init,
2442 				    sec_aead_xcm_ctx_exit, SEC_MIN_BLOCK_SZ, AES_BLOCK_SIZE,
2443 				    AES_BLOCK_SIZE),
2444 	},
2445 	{
2446 		.alg_msk = BIT(7),
2447 		.alg = SEC_AEAD_ALG("gcm(aes)", sec_setkey_aes_gcm, sec_aead_xcm_ctx_init,
2448 				    sec_aead_xcm_ctx_exit, SEC_MIN_BLOCK_SZ, SEC_AIV_SIZE,
2449 				    AES_BLOCK_SIZE),
2450 	},
2451 	{
2452 		.alg_msk = BIT(17),
2453 		.alg = SEC_AEAD_ALG("ccm(sm4)", sec_setkey_sm4_ccm, sec_aead_xcm_ctx_init,
2454 				    sec_aead_xcm_ctx_exit, SEC_MIN_BLOCK_SZ, AES_BLOCK_SIZE,
2455 				    AES_BLOCK_SIZE),
2456 	},
2457 	{
2458 		.alg_msk = BIT(18),
2459 		.alg = SEC_AEAD_ALG("gcm(sm4)", sec_setkey_sm4_gcm, sec_aead_xcm_ctx_init,
2460 				    sec_aead_xcm_ctx_exit, SEC_MIN_BLOCK_SZ, SEC_AIV_SIZE,
2461 				    AES_BLOCK_SIZE),
2462 	},
2463 	{
2464 		.alg_msk = BIT(43),
2465 		.alg = SEC_AEAD_ALG("authenc(hmac(sha1),cbc(aes))", sec_setkey_aes_cbc_sha1,
2466 				    sec_aead_sha1_ctx_init, sec_aead_ctx_exit, AES_BLOCK_SIZE,
2467 				    AES_BLOCK_SIZE, SHA1_DIGEST_SIZE),
2468 	},
2469 	{
2470 		.alg_msk = BIT(44),
2471 		.alg = SEC_AEAD_ALG("authenc(hmac(sha256),cbc(aes))", sec_setkey_aes_cbc_sha256,
2472 				    sec_aead_sha256_ctx_init, sec_aead_ctx_exit, AES_BLOCK_SIZE,
2473 				    AES_BLOCK_SIZE, SHA256_DIGEST_SIZE),
2474 	},
2475 	{
2476 		.alg_msk = BIT(45),
2477 		.alg = SEC_AEAD_ALG("authenc(hmac(sha512),cbc(aes))", sec_setkey_aes_cbc_sha512,
2478 				    sec_aead_sha512_ctx_init, sec_aead_ctx_exit, AES_BLOCK_SIZE,
2479 				    AES_BLOCK_SIZE, SHA512_DIGEST_SIZE),
2480 	},
2481 };
2482 
2483 static void sec_unregister_skcipher(u64 alg_mask, int end)
2484 {
2485 	int i;
2486 
2487 	for (i = 0; i < end; i++)
2488 		if (sec_skciphers[i].alg_msk & alg_mask)
2489 			crypto_unregister_skcipher(&sec_skciphers[i].alg);
2490 }
2491 
2492 static int sec_register_skcipher(u64 alg_mask)
2493 {
2494 	int i, ret, count;
2495 
2496 	count = ARRAY_SIZE(sec_skciphers);
2497 
2498 	for (i = 0; i < count; i++) {
2499 		if (!(sec_skciphers[i].alg_msk & alg_mask))
2500 			continue;
2501 
2502 		ret = crypto_register_skcipher(&sec_skciphers[i].alg);
2503 		if (ret)
2504 			goto err;
2505 	}
2506 
2507 	return 0;
2508 
2509 err:
2510 	sec_unregister_skcipher(alg_mask, i);
2511 
2512 	return ret;
2513 }
2514 
2515 static void sec_unregister_aead(u64 alg_mask, int end)
2516 {
2517 	int i;
2518 
2519 	for (i = 0; i < end; i++)
2520 		if (sec_aeads[i].alg_msk & alg_mask)
2521 			crypto_unregister_aead(&sec_aeads[i].alg);
2522 }
2523 
2524 static int sec_register_aead(u64 alg_mask)
2525 {
2526 	int i, ret, count;
2527 
2528 	count = ARRAY_SIZE(sec_aeads);
2529 
2530 	for (i = 0; i < count; i++) {
2531 		if (!(sec_aeads[i].alg_msk & alg_mask))
2532 			continue;
2533 
2534 		ret = crypto_register_aead(&sec_aeads[i].alg);
2535 		if (ret)
2536 			goto err;
2537 	}
2538 
2539 	return 0;
2540 
2541 err:
2542 	sec_unregister_aead(alg_mask, i);
2543 
2544 	return ret;
2545 }
2546 
2547 int sec_register_to_crypto(struct hisi_qm *qm)
2548 {
2549 	u64 alg_mask = sec_get_alg_bitmap(qm, SEC_DRV_ALG_BITMAP_HIGH, SEC_DRV_ALG_BITMAP_LOW);
2550 	int ret;
2551 
2552 	ret = sec_register_skcipher(alg_mask);
2553 	if (ret)
2554 		return ret;
2555 
2556 	ret = sec_register_aead(alg_mask);
2557 	if (ret)
2558 		sec_unregister_skcipher(alg_mask, ARRAY_SIZE(sec_skciphers));
2559 
2560 	return ret;
2561 }
2562 
2563 void sec_unregister_from_crypto(struct hisi_qm *qm)
2564 {
2565 	u64 alg_mask = sec_get_alg_bitmap(qm, SEC_DRV_ALG_BITMAP_HIGH, SEC_DRV_ALG_BITMAP_LOW);
2566 
2567 	sec_unregister_aead(alg_mask, ARRAY_SIZE(sec_aeads));
2568 	sec_unregister_skcipher(alg_mask, ARRAY_SIZE(sec_skciphers));
2569 }
2570