1 /* SPDX-License-Identifier: BSD-3-Clause
3 * Copyright (c) 2016 Freescale Semiconductor, Inc. All rights reserved.
4 * Copyright 2017-2019 NXP
13 #include <rte_byteorder.h>
14 #include <rte_common.h>
15 #include <rte_cryptodev_pmd.h>
16 #include <rte_crypto.h>
17 #include <rte_cryptodev.h>
18 #include <rte_security_driver.h>
19 #include <rte_cycles.h>
21 #include <rte_kvargs.h>
22 #include <rte_malloc.h>
24 #include <rte_memcpy.h>
25 #include <rte_string_fns.h>
26 #include <rte_spinlock.h>
32 /* RTA header files */
33 #include <hw/desc/common.h>
34 #include <hw/desc/algo.h>
35 #include <hw/desc/ipsec.h>
36 #include <hw/desc/pdcp.h>
38 #include <rte_dpaa_bus.h>
40 #include <dpaa_sec_log.h>
41 #include <dpaax_iova_table.h>
43 enum rta_sec_era rta_sec_era;
47 static uint8_t cryptodev_driver_id;
49 static __thread struct rte_crypto_op **dpaa_sec_ops;
50 static __thread int dpaa_sec_op_nb;
53 dpaa_sec_attach_sess_q(struct dpaa_sec_qp *qp, dpaa_sec_session *sess);
56 dpaa_sec_op_ending(struct dpaa_sec_op_ctx *ctx)
58 if (!ctx->fd_status) {
59 ctx->op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
61 DPAA_SEC_DP_WARN("SEC return err: 0x%x", ctx->fd_status);
62 ctx->op->status = RTE_CRYPTO_OP_STATUS_ERROR;
65 /* report op status to sym->op and then free the ctx memory */
66 rte_mempool_put(ctx->ctx_pool, (void *)ctx);
69 static inline struct dpaa_sec_op_ctx *
70 dpaa_sec_alloc_ctx(dpaa_sec_session *ses)
72 struct dpaa_sec_op_ctx *ctx;
75 retval = rte_mempool_get(ses->ctx_pool, (void **)(&ctx));
77 DPAA_SEC_DP_WARN("Alloc sec descriptor failed!");
81 * Clear SG memory. There are 16 SG entries of 16 Bytes each.
82 * one call to dcbz_64() clear 64 bytes, hence calling it 4 times
83 * to clear all the SG entries. dpaa_sec_alloc_ctx() is called for
84 * each packet, memset is costlier than dcbz_64().
86 dcbz_64(&ctx->job.sg[SG_CACHELINE_0]);
87 dcbz_64(&ctx->job.sg[SG_CACHELINE_1]);
88 dcbz_64(&ctx->job.sg[SG_CACHELINE_2]);
89 dcbz_64(&ctx->job.sg[SG_CACHELINE_3]);
91 ctx->ctx_pool = ses->ctx_pool;
92 ctx->vtop_offset = (size_t) ctx
93 - rte_mempool_virt2iova(ctx);
98 static inline rte_iova_t
99 dpaa_mem_vtop(void *vaddr)
101 const struct rte_memseg *ms;
103 ms = rte_mem_virt2memseg(vaddr, NULL);
105 dpaax_iova_table_update(ms->iova, ms->addr, ms->len);
106 return ms->iova + RTE_PTR_DIFF(vaddr, ms->addr);
112 dpaa_mem_ptov(rte_iova_t paddr)
116 va = (void *)dpaax_iova_table_get_va(paddr);
120 return rte_mem_iova2virt(paddr);
124 ern_sec_fq_handler(struct qman_portal *qm __rte_unused,
126 const struct qm_mr_entry *msg)
128 DPAA_SEC_DP_ERR("sec fq %d error, RC = %x, seqnum = %x\n",
129 fq->fqid, msg->ern.rc, msg->ern.seqnum);
132 /* initialize the queue with dest chan as caam chan so that
133 * all the packets in this queue could be dispatched into caam
136 dpaa_sec_init_rx(struct qman_fq *fq_in, rte_iova_t hwdesc,
139 struct qm_mcc_initfq fq_opts;
143 /* Clear FQ options */
144 memset(&fq_opts, 0x00, sizeof(struct qm_mcc_initfq));
146 flags = QMAN_INITFQ_FLAG_SCHED;
147 fq_opts.we_mask = QM_INITFQ_WE_DESTWQ | QM_INITFQ_WE_CONTEXTA |
148 QM_INITFQ_WE_CONTEXTB;
150 qm_fqd_context_a_set64(&fq_opts.fqd, hwdesc);
151 fq_opts.fqd.context_b = fqid_out;
152 fq_opts.fqd.dest.channel = qm_channel_caam;
153 fq_opts.fqd.dest.wq = 0;
155 fq_in->cb.ern = ern_sec_fq_handler;
157 DPAA_SEC_DEBUG("in-%x out-%x", fq_in->fqid, fqid_out);
159 ret = qman_init_fq(fq_in, flags, &fq_opts);
160 if (unlikely(ret != 0))
161 DPAA_SEC_ERR("qman_init_fq failed %d", ret);
166 /* something is put into in_fq and caam put the crypto result into out_fq */
167 static enum qman_cb_dqrr_result
168 dqrr_out_fq_cb_rx(struct qman_portal *qm __always_unused,
169 struct qman_fq *fq __always_unused,
170 const struct qm_dqrr_entry *dqrr)
172 const struct qm_fd *fd;
173 struct dpaa_sec_job *job;
174 struct dpaa_sec_op_ctx *ctx;
176 if (dpaa_sec_op_nb >= DPAA_SEC_BURST)
177 return qman_cb_dqrr_defer;
179 if (!(dqrr->stat & QM_DQRR_STAT_FD_VALID))
180 return qman_cb_dqrr_consume;
183 /* sg is embedded in an op ctx,
184 * sg[0] is for output
187 job = dpaa_mem_ptov(qm_fd_addr_get64(fd));
189 ctx = container_of(job, struct dpaa_sec_op_ctx, job);
190 ctx->fd_status = fd->status;
191 if (ctx->op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
192 struct qm_sg_entry *sg_out;
195 sg_out = &job->sg[0];
196 hw_sg_to_cpu(sg_out);
197 len = sg_out->length;
198 ctx->op->sym->m_src->pkt_len = len;
199 ctx->op->sym->m_src->data_len = len;
201 dpaa_sec_ops[dpaa_sec_op_nb++] = ctx->op;
202 dpaa_sec_op_ending(ctx);
204 return qman_cb_dqrr_consume;
207 /* caam result is put into this queue */
209 dpaa_sec_init_tx(struct qman_fq *fq)
212 struct qm_mcc_initfq opts;
215 flags = QMAN_FQ_FLAG_NO_ENQUEUE | QMAN_FQ_FLAG_LOCKED |
216 QMAN_FQ_FLAG_DYNAMIC_FQID;
218 ret = qman_create_fq(0, flags, fq);
220 DPAA_SEC_ERR("qman_create_fq failed");
224 memset(&opts, 0, sizeof(opts));
225 opts.we_mask = QM_INITFQ_WE_DESTWQ | QM_INITFQ_WE_FQCTRL |
226 QM_INITFQ_WE_CONTEXTA | QM_INITFQ_WE_CONTEXTB;
228 /* opts.fqd.dest.channel = dpaa_sec_pool_chan; */
230 fq->cb.dqrr = dqrr_out_fq_cb_rx;
231 fq->cb.ern = ern_sec_fq_handler;
233 ret = qman_init_fq(fq, 0, &opts);
235 DPAA_SEC_ERR("unable to init caam source fq!");
242 static inline int is_cipher_only(dpaa_sec_session *ses)
244 return ((ses->cipher_alg != RTE_CRYPTO_CIPHER_NULL) &&
245 (ses->auth_alg == RTE_CRYPTO_AUTH_NULL));
248 static inline int is_auth_only(dpaa_sec_session *ses)
250 return ((ses->cipher_alg == RTE_CRYPTO_CIPHER_NULL) &&
251 (ses->auth_alg != RTE_CRYPTO_AUTH_NULL));
254 static inline int is_aead(dpaa_sec_session *ses)
256 return ((ses->cipher_alg == 0) &&
257 (ses->auth_alg == 0) &&
258 (ses->aead_alg != 0));
261 static inline int is_auth_cipher(dpaa_sec_session *ses)
263 return ((ses->cipher_alg != RTE_CRYPTO_CIPHER_NULL) &&
264 (ses->auth_alg != RTE_CRYPTO_AUTH_NULL) &&
265 (ses->proto_alg != RTE_SECURITY_PROTOCOL_IPSEC));
268 static inline int is_proto_ipsec(dpaa_sec_session *ses)
270 return (ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC);
273 static inline int is_proto_pdcp(dpaa_sec_session *ses)
275 return (ses->proto_alg == RTE_SECURITY_PROTOCOL_PDCP);
278 static inline int is_encode(dpaa_sec_session *ses)
280 return ses->dir == DIR_ENC;
283 static inline int is_decode(dpaa_sec_session *ses)
285 return ses->dir == DIR_DEC;
289 caam_auth_alg(dpaa_sec_session *ses, struct alginfo *alginfo_a)
291 switch (ses->auth_alg) {
292 case RTE_CRYPTO_AUTH_NULL:
294 (ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
295 OP_PCL_IPSEC_HMAC_NULL : 0;
296 ses->digest_length = 0;
298 case RTE_CRYPTO_AUTH_MD5_HMAC:
300 (ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
301 OP_PCL_IPSEC_HMAC_MD5_96 : OP_ALG_ALGSEL_MD5;
302 alginfo_a->algmode = OP_ALG_AAI_HMAC;
304 case RTE_CRYPTO_AUTH_SHA1_HMAC:
306 (ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
307 OP_PCL_IPSEC_HMAC_SHA1_96 : OP_ALG_ALGSEL_SHA1;
308 alginfo_a->algmode = OP_ALG_AAI_HMAC;
310 case RTE_CRYPTO_AUTH_SHA224_HMAC:
312 (ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
313 OP_PCL_IPSEC_HMAC_SHA1_160 : OP_ALG_ALGSEL_SHA224;
314 alginfo_a->algmode = OP_ALG_AAI_HMAC;
316 case RTE_CRYPTO_AUTH_SHA256_HMAC:
318 (ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
319 OP_PCL_IPSEC_HMAC_SHA2_256_128 : OP_ALG_ALGSEL_SHA256;
320 alginfo_a->algmode = OP_ALG_AAI_HMAC;
322 case RTE_CRYPTO_AUTH_SHA384_HMAC:
324 (ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
325 OP_PCL_IPSEC_HMAC_SHA2_384_192 : OP_ALG_ALGSEL_SHA384;
326 alginfo_a->algmode = OP_ALG_AAI_HMAC;
328 case RTE_CRYPTO_AUTH_SHA512_HMAC:
330 (ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
331 OP_PCL_IPSEC_HMAC_SHA2_512_256 : OP_ALG_ALGSEL_SHA512;
332 alginfo_a->algmode = OP_ALG_AAI_HMAC;
335 DPAA_SEC_ERR("unsupported auth alg %u", ses->auth_alg);
340 caam_cipher_alg(dpaa_sec_session *ses, struct alginfo *alginfo_c)
342 switch (ses->cipher_alg) {
343 case RTE_CRYPTO_CIPHER_NULL:
345 (ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
346 OP_PCL_IPSEC_NULL : 0;
348 case RTE_CRYPTO_CIPHER_AES_CBC:
350 (ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
351 OP_PCL_IPSEC_AES_CBC : OP_ALG_ALGSEL_AES;
352 alginfo_c->algmode = OP_ALG_AAI_CBC;
354 case RTE_CRYPTO_CIPHER_3DES_CBC:
356 (ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
357 OP_PCL_IPSEC_3DES : OP_ALG_ALGSEL_3DES;
358 alginfo_c->algmode = OP_ALG_AAI_CBC;
360 case RTE_CRYPTO_CIPHER_AES_CTR:
362 (ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
363 OP_PCL_IPSEC_AES_CTR : OP_ALG_ALGSEL_AES;
364 alginfo_c->algmode = OP_ALG_AAI_CTR;
367 DPAA_SEC_ERR("unsupported cipher alg %d", ses->cipher_alg);
372 caam_aead_alg(dpaa_sec_session *ses, struct alginfo *alginfo)
374 switch (ses->aead_alg) {
375 case RTE_CRYPTO_AEAD_AES_GCM:
376 alginfo->algtype = OP_ALG_ALGSEL_AES;
377 alginfo->algmode = OP_ALG_AAI_GCM;
380 DPAA_SEC_ERR("unsupported AEAD alg %d", ses->aead_alg);
385 dpaa_sec_prep_pdcp_cdb(dpaa_sec_session *ses)
387 struct alginfo authdata = {0}, cipherdata = {0};
388 struct sec_cdb *cdb = &ses->cdb;
389 int32_t shared_desc_len = 0;
391 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
397 switch (ses->cipher_alg) {
398 case RTE_CRYPTO_CIPHER_SNOW3G_UEA2:
399 cipherdata.algtype = PDCP_CIPHER_TYPE_SNOW;
401 case RTE_CRYPTO_CIPHER_ZUC_EEA3:
402 cipherdata.algtype = PDCP_CIPHER_TYPE_ZUC;
404 case RTE_CRYPTO_CIPHER_AES_CTR:
405 cipherdata.algtype = PDCP_CIPHER_TYPE_AES;
407 case RTE_CRYPTO_CIPHER_NULL:
408 cipherdata.algtype = PDCP_CIPHER_TYPE_NULL;
411 DPAA_SEC_ERR("Crypto: Undefined Cipher specified %u",
416 cipherdata.key = (size_t)ses->cipher_key.data;
417 cipherdata.keylen = ses->cipher_key.length;
418 cipherdata.key_enc_flags = 0;
419 cipherdata.key_type = RTA_DATA_IMM;
421 if (ses->pdcp.domain == RTE_SECURITY_PDCP_MODE_CONTROL) {
422 switch (ses->auth_alg) {
423 case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
424 authdata.algtype = PDCP_AUTH_TYPE_SNOW;
426 case RTE_CRYPTO_AUTH_ZUC_EIA3:
427 authdata.algtype = PDCP_AUTH_TYPE_ZUC;
429 case RTE_CRYPTO_AUTH_AES_CMAC:
430 authdata.algtype = PDCP_AUTH_TYPE_AES;
432 case RTE_CRYPTO_AUTH_NULL:
433 authdata.algtype = PDCP_AUTH_TYPE_NULL;
436 DPAA_SEC_ERR("Crypto: Unsupported auth alg %u",
441 authdata.key = (size_t)ses->auth_key.data;
442 authdata.keylen = ses->auth_key.length;
443 authdata.key_enc_flags = 0;
444 authdata.key_type = RTA_DATA_IMM;
446 cdb->sh_desc[0] = cipherdata.keylen;
447 cdb->sh_desc[1] = authdata.keylen;
448 err = rta_inline_query(IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN,
450 (unsigned int *)cdb->sh_desc,
451 &cdb->sh_desc[2], 2);
454 DPAA_SEC_ERR("Crypto: Incorrect key lengths");
457 if (!(cdb->sh_desc[2] & 1) && cipherdata.keylen) {
458 cipherdata.key = (size_t)dpaa_mem_vtop(
459 (void *)(size_t)cipherdata.key);
460 cipherdata.key_type = RTA_DATA_PTR;
462 if (!(cdb->sh_desc[2] & (1<<1)) && authdata.keylen) {
463 authdata.key = (size_t)dpaa_mem_vtop(
464 (void *)(size_t)authdata.key);
465 authdata.key_type = RTA_DATA_PTR;
472 if (ses->dir == DIR_ENC)
473 shared_desc_len = cnstr_shdsc_pdcp_c_plane_encap(
474 cdb->sh_desc, 1, swap,
479 ses->pdcp.hfn_threshold,
480 &cipherdata, &authdata,
482 else if (ses->dir == DIR_DEC)
483 shared_desc_len = cnstr_shdsc_pdcp_c_plane_decap(
484 cdb->sh_desc, 1, swap,
489 ses->pdcp.hfn_threshold,
490 &cipherdata, &authdata,
493 cdb->sh_desc[0] = cipherdata.keylen;
494 err = rta_inline_query(IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN,
496 (unsigned int *)cdb->sh_desc,
497 &cdb->sh_desc[2], 1);
500 DPAA_SEC_ERR("Crypto: Incorrect key lengths");
503 if (!(cdb->sh_desc[2] & 1) && cipherdata.keylen) {
504 cipherdata.key = (size_t)dpaa_mem_vtop(
505 (void *)(size_t)cipherdata.key);
506 cipherdata.key_type = RTA_DATA_PTR;
512 if (ses->dir == DIR_ENC)
513 shared_desc_len = cnstr_shdsc_pdcp_u_plane_encap(
514 cdb->sh_desc, 1, swap,
519 ses->pdcp.hfn_threshold,
521 else if (ses->dir == DIR_DEC)
522 shared_desc_len = cnstr_shdsc_pdcp_u_plane_decap(
523 cdb->sh_desc, 1, swap,
528 ses->pdcp.hfn_threshold,
532 return shared_desc_len;
535 /* prepare ipsec proto command block of the session */
537 dpaa_sec_prep_ipsec_cdb(dpaa_sec_session *ses)
539 struct alginfo cipherdata = {0}, authdata = {0};
540 struct sec_cdb *cdb = &ses->cdb;
541 int32_t shared_desc_len = 0;
543 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
549 caam_cipher_alg(ses, &cipherdata);
550 if (cipherdata.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
551 DPAA_SEC_ERR("not supported cipher alg");
555 cipherdata.key = (size_t)ses->cipher_key.data;
556 cipherdata.keylen = ses->cipher_key.length;
557 cipherdata.key_enc_flags = 0;
558 cipherdata.key_type = RTA_DATA_IMM;
560 caam_auth_alg(ses, &authdata);
561 if (authdata.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
562 DPAA_SEC_ERR("not supported auth alg");
566 authdata.key = (size_t)ses->auth_key.data;
567 authdata.keylen = ses->auth_key.length;
568 authdata.key_enc_flags = 0;
569 authdata.key_type = RTA_DATA_IMM;
571 cdb->sh_desc[0] = cipherdata.keylen;
572 cdb->sh_desc[1] = authdata.keylen;
573 err = rta_inline_query(IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN,
575 (unsigned int *)cdb->sh_desc,
576 &cdb->sh_desc[2], 2);
579 DPAA_SEC_ERR("Crypto: Incorrect key lengths");
582 if (cdb->sh_desc[2] & 1)
583 cipherdata.key_type = RTA_DATA_IMM;
585 cipherdata.key = (size_t)dpaa_mem_vtop(
586 (void *)(size_t)cipherdata.key);
587 cipherdata.key_type = RTA_DATA_PTR;
589 if (cdb->sh_desc[2] & (1<<1))
590 authdata.key_type = RTA_DATA_IMM;
592 authdata.key = (size_t)dpaa_mem_vtop(
593 (void *)(size_t)authdata.key);
594 authdata.key_type = RTA_DATA_PTR;
600 if (ses->dir == DIR_ENC) {
601 shared_desc_len = cnstr_shdsc_ipsec_new_encap(
603 true, swap, SHR_SERIAL,
605 (uint8_t *)&ses->ip4_hdr,
606 &cipherdata, &authdata);
607 } else if (ses->dir == DIR_DEC) {
608 shared_desc_len = cnstr_shdsc_ipsec_new_decap(
610 true, swap, SHR_SERIAL,
612 &cipherdata, &authdata);
614 return shared_desc_len;
617 /* prepare command block of the session */
619 dpaa_sec_prep_cdb(dpaa_sec_session *ses)
621 struct alginfo alginfo_c = {0}, alginfo_a = {0}, alginfo = {0};
622 int32_t shared_desc_len = 0;
623 struct sec_cdb *cdb = &ses->cdb;
625 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
631 memset(cdb, 0, sizeof(struct sec_cdb));
633 if (is_proto_ipsec(ses)) {
634 shared_desc_len = dpaa_sec_prep_ipsec_cdb(ses);
635 } else if (is_proto_pdcp(ses)) {
636 shared_desc_len = dpaa_sec_prep_pdcp_cdb(ses);
637 } else if (is_cipher_only(ses)) {
638 caam_cipher_alg(ses, &alginfo_c);
639 if (alginfo_c.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
640 DPAA_SEC_ERR("not supported cipher alg");
644 alginfo_c.key = (size_t)ses->cipher_key.data;
645 alginfo_c.keylen = ses->cipher_key.length;
646 alginfo_c.key_enc_flags = 0;
647 alginfo_c.key_type = RTA_DATA_IMM;
649 shared_desc_len = cnstr_shdsc_blkcipher(
651 swap, SHR_NEVER, &alginfo_c,
655 } else if (is_auth_only(ses)) {
656 caam_auth_alg(ses, &alginfo_a);
657 if (alginfo_a.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
658 DPAA_SEC_ERR("not supported auth alg");
662 alginfo_a.key = (size_t)ses->auth_key.data;
663 alginfo_a.keylen = ses->auth_key.length;
664 alginfo_a.key_enc_flags = 0;
665 alginfo_a.key_type = RTA_DATA_IMM;
667 shared_desc_len = cnstr_shdsc_hmac(cdb->sh_desc, true,
668 swap, SHR_NEVER, &alginfo_a,
671 } else if (is_aead(ses)) {
672 caam_aead_alg(ses, &alginfo);
673 if (alginfo.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
674 DPAA_SEC_ERR("not supported aead alg");
677 alginfo.key = (size_t)ses->aead_key.data;
678 alginfo.keylen = ses->aead_key.length;
679 alginfo.key_enc_flags = 0;
680 alginfo.key_type = RTA_DATA_IMM;
682 if (ses->dir == DIR_ENC)
683 shared_desc_len = cnstr_shdsc_gcm_encap(
684 cdb->sh_desc, true, swap, SHR_NEVER,
689 shared_desc_len = cnstr_shdsc_gcm_decap(
690 cdb->sh_desc, true, swap, SHR_NEVER,
695 caam_cipher_alg(ses, &alginfo_c);
696 if (alginfo_c.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
697 DPAA_SEC_ERR("not supported cipher alg");
701 alginfo_c.key = (size_t)ses->cipher_key.data;
702 alginfo_c.keylen = ses->cipher_key.length;
703 alginfo_c.key_enc_flags = 0;
704 alginfo_c.key_type = RTA_DATA_IMM;
706 caam_auth_alg(ses, &alginfo_a);
707 if (alginfo_a.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
708 DPAA_SEC_ERR("not supported auth alg");
712 alginfo_a.key = (size_t)ses->auth_key.data;
713 alginfo_a.keylen = ses->auth_key.length;
714 alginfo_a.key_enc_flags = 0;
715 alginfo_a.key_type = RTA_DATA_IMM;
717 cdb->sh_desc[0] = alginfo_c.keylen;
718 cdb->sh_desc[1] = alginfo_a.keylen;
719 err = rta_inline_query(IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN,
721 (unsigned int *)cdb->sh_desc,
722 &cdb->sh_desc[2], 2);
725 DPAA_SEC_ERR("Crypto: Incorrect key lengths");
728 if (cdb->sh_desc[2] & 1)
729 alginfo_c.key_type = RTA_DATA_IMM;
731 alginfo_c.key = (size_t)dpaa_mem_vtop(
732 (void *)(size_t)alginfo_c.key);
733 alginfo_c.key_type = RTA_DATA_PTR;
735 if (cdb->sh_desc[2] & (1<<1))
736 alginfo_a.key_type = RTA_DATA_IMM;
738 alginfo_a.key = (size_t)dpaa_mem_vtop(
739 (void *)(size_t)alginfo_a.key);
740 alginfo_a.key_type = RTA_DATA_PTR;
745 /* Auth_only_len is set as 0 here and it will be
746 * overwritten in fd for each packet.
748 shared_desc_len = cnstr_shdsc_authenc(cdb->sh_desc,
749 true, swap, SHR_SERIAL, &alginfo_c, &alginfo_a,
751 ses->digest_length, ses->dir);
754 if (shared_desc_len < 0) {
755 DPAA_SEC_ERR("error in preparing command block");
756 return shared_desc_len;
759 cdb->sh_hdr.hi.field.idlen = shared_desc_len;
760 cdb->sh_hdr.hi.word = rte_cpu_to_be_32(cdb->sh_hdr.hi.word);
761 cdb->sh_hdr.lo.word = rte_cpu_to_be_32(cdb->sh_hdr.lo.word);
766 /* qp is lockless, should be accessed by only one thread */
768 dpaa_sec_deq(struct dpaa_sec_qp *qp, struct rte_crypto_op **ops, int nb_ops)
771 unsigned int pkts = 0;
772 int num_rx_bufs, ret;
773 struct qm_dqrr_entry *dq;
774 uint32_t vdqcr_flags = 0;
778 * Until request for four buffers, we provide exact number of buffers.
779 * Otherwise we do not set the QM_VDQCR_EXACT flag.
780 * Not setting QM_VDQCR_EXACT flag can provide two more buffers than
781 * requested, so we request two less in this case.
784 vdqcr_flags = QM_VDQCR_EXACT;
785 num_rx_bufs = nb_ops;
787 num_rx_bufs = nb_ops > DPAA_MAX_DEQUEUE_NUM_FRAMES ?
788 (DPAA_MAX_DEQUEUE_NUM_FRAMES - 2) : (nb_ops - 2);
790 ret = qman_set_vdq(fq, num_rx_bufs, vdqcr_flags);
795 const struct qm_fd *fd;
796 struct dpaa_sec_job *job;
797 struct dpaa_sec_op_ctx *ctx;
798 struct rte_crypto_op *op;
800 dq = qman_dequeue(fq);
805 /* sg is embedded in an op ctx,
806 * sg[0] is for output
809 job = dpaa_mem_ptov(qm_fd_addr_get64(fd));
811 ctx = container_of(job, struct dpaa_sec_op_ctx, job);
812 ctx->fd_status = fd->status;
814 if (op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
815 struct qm_sg_entry *sg_out;
818 sg_out = &job->sg[0];
819 hw_sg_to_cpu(sg_out);
820 len = sg_out->length;
821 op->sym->m_src->pkt_len = len;
822 op->sym->m_src->data_len = len;
824 if (!ctx->fd_status) {
825 op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
827 DPAA_SEC_DP_WARN("SEC return err:0x%x", ctx->fd_status);
828 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
832 /* report op status to sym->op and then free the ctx memeory */
833 rte_mempool_put(ctx->ctx_pool, (void *)ctx);
835 qman_dqrr_consume(fq, dq);
836 } while (fq->flags & QMAN_FQ_STATE_VDQCR);
841 static inline struct dpaa_sec_job *
842 build_auth_only_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
844 struct rte_crypto_sym_op *sym = op->sym;
845 struct rte_mbuf *mbuf = sym->m_src;
846 struct dpaa_sec_job *cf;
847 struct dpaa_sec_op_ctx *ctx;
848 struct qm_sg_entry *sg, *out_sg, *in_sg;
849 phys_addr_t start_addr;
850 uint8_t *old_digest, extra_segs;
857 if ((mbuf->nb_segs + extra_segs) > MAX_SG_ENTRIES) {
858 DPAA_SEC_DP_ERR("Auth: Max sec segs supported is %d",
862 ctx = dpaa_sec_alloc_ctx(ses);
868 old_digest = ctx->digest;
872 qm_sg_entry_set64(out_sg, sym->auth.digest.phys_addr);
873 out_sg->length = ses->digest_length;
874 cpu_to_hw_sg(out_sg);
878 /* need to extend the input to a compound frame */
879 in_sg->extension = 1;
881 in_sg->length = sym->auth.data.length;
882 qm_sg_entry_set64(in_sg, dpaa_mem_vtop(&cf->sg[2]));
886 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
887 sg->length = mbuf->data_len - sym->auth.data.offset;
888 sg->offset = sym->auth.data.offset;
890 /* Successive segs */
895 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
896 sg->length = mbuf->data_len;
900 if (is_decode(ses)) {
901 /* Digest verification case */
904 rte_memcpy(old_digest, sym->auth.digest.data,
906 start_addr = dpaa_mem_vtop(old_digest);
907 qm_sg_entry_set64(sg, start_addr);
908 sg->length = ses->digest_length;
909 in_sg->length += ses->digest_length;
911 /* Digest calculation case */
912 sg->length -= ses->digest_length;
923 * |<----data_len------->|
924 * |ip_header|ah_header|icv|payload|
929 static inline struct dpaa_sec_job *
930 build_auth_only(struct rte_crypto_op *op, dpaa_sec_session *ses)
932 struct rte_crypto_sym_op *sym = op->sym;
933 struct rte_mbuf *mbuf = sym->m_src;
934 struct dpaa_sec_job *cf;
935 struct dpaa_sec_op_ctx *ctx;
936 struct qm_sg_entry *sg;
937 rte_iova_t start_addr;
940 ctx = dpaa_sec_alloc_ctx(ses);
946 old_digest = ctx->digest;
948 start_addr = rte_pktmbuf_iova(mbuf);
951 qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
952 sg->length = ses->digest_length;
957 if (is_decode(ses)) {
958 /* need to extend the input to a compound frame */
960 qm_sg_entry_set64(sg, dpaa_mem_vtop(&cf->sg[2]));
961 sg->length = sym->auth.data.length + ses->digest_length;
966 /* hash result or digest, save digest first */
967 rte_memcpy(old_digest, sym->auth.digest.data,
969 qm_sg_entry_set64(sg, start_addr + sym->auth.data.offset);
970 sg->length = sym->auth.data.length;
973 /* let's check digest by hw */
974 start_addr = dpaa_mem_vtop(old_digest);
976 qm_sg_entry_set64(sg, start_addr);
977 sg->length = ses->digest_length;
981 qm_sg_entry_set64(sg, start_addr + sym->auth.data.offset);
982 sg->length = sym->auth.data.length;
990 static inline struct dpaa_sec_job *
991 build_cipher_only_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
993 struct rte_crypto_sym_op *sym = op->sym;
994 struct dpaa_sec_job *cf;
995 struct dpaa_sec_op_ctx *ctx;
996 struct qm_sg_entry *sg, *out_sg, *in_sg;
997 struct rte_mbuf *mbuf;
999 uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1004 req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 3;
1007 req_segs = mbuf->nb_segs * 2 + 3;
1010 if (req_segs > MAX_SG_ENTRIES) {
1011 DPAA_SEC_DP_ERR("Cipher: Max sec segs supported is %d",
1016 ctx = dpaa_sec_alloc_ctx(ses);
1024 out_sg = &cf->sg[0];
1025 out_sg->extension = 1;
1026 out_sg->length = sym->cipher.data.length;
1027 qm_sg_entry_set64(out_sg, dpaa_mem_vtop(&cf->sg[2]));
1028 cpu_to_hw_sg(out_sg);
1032 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1033 sg->length = mbuf->data_len - sym->cipher.data.offset;
1034 sg->offset = sym->cipher.data.offset;
1036 /* Successive segs */
1041 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1042 sg->length = mbuf->data_len;
1051 in_sg->extension = 1;
1053 in_sg->length = sym->cipher.data.length + ses->iv.length;
1056 qm_sg_entry_set64(in_sg, dpaa_mem_vtop(sg));
1057 cpu_to_hw_sg(in_sg);
1060 qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1061 sg->length = ses->iv.length;
1066 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1067 sg->length = mbuf->data_len - sym->cipher.data.offset;
1068 sg->offset = sym->cipher.data.offset;
1070 /* Successive segs */
1075 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1076 sg->length = mbuf->data_len;
1085 static inline struct dpaa_sec_job *
1086 build_cipher_only(struct rte_crypto_op *op, dpaa_sec_session *ses)
1088 struct rte_crypto_sym_op *sym = op->sym;
1089 struct dpaa_sec_job *cf;
1090 struct dpaa_sec_op_ctx *ctx;
1091 struct qm_sg_entry *sg;
1092 rte_iova_t src_start_addr, dst_start_addr;
1093 uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1096 ctx = dpaa_sec_alloc_ctx(ses);
1103 src_start_addr = rte_pktmbuf_iova(sym->m_src);
1106 dst_start_addr = rte_pktmbuf_iova(sym->m_dst);
1108 dst_start_addr = src_start_addr;
1112 qm_sg_entry_set64(sg, dst_start_addr + sym->cipher.data.offset);
1113 sg->length = sym->cipher.data.length + ses->iv.length;
1119 /* need to extend the input to a compound frame */
1122 sg->length = sym->cipher.data.length + ses->iv.length;
1123 qm_sg_entry_set64(sg, dpaa_mem_vtop(&cf->sg[2]));
1127 qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1128 sg->length = ses->iv.length;
1132 qm_sg_entry_set64(sg, src_start_addr + sym->cipher.data.offset);
1133 sg->length = sym->cipher.data.length;
1140 static inline struct dpaa_sec_job *
1141 build_cipher_auth_gcm_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
1143 struct rte_crypto_sym_op *sym = op->sym;
1144 struct dpaa_sec_job *cf;
1145 struct dpaa_sec_op_ctx *ctx;
1146 struct qm_sg_entry *sg, *out_sg, *in_sg;
1147 struct rte_mbuf *mbuf;
1149 uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1154 req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 4;
1157 req_segs = mbuf->nb_segs * 2 + 4;
1160 if (ses->auth_only_len)
1163 if (req_segs > MAX_SG_ENTRIES) {
1164 DPAA_SEC_DP_ERR("AEAD: Max sec segs supported is %d",
1169 ctx = dpaa_sec_alloc_ctx(ses);
1176 rte_prefetch0(cf->sg);
1179 out_sg = &cf->sg[0];
1180 out_sg->extension = 1;
1182 out_sg->length = sym->aead.data.length + ses->auth_only_len
1183 + ses->digest_length;
1185 out_sg->length = sym->aead.data.length + ses->auth_only_len;
1187 /* output sg entries */
1189 qm_sg_entry_set64(out_sg, dpaa_mem_vtop(sg));
1190 cpu_to_hw_sg(out_sg);
1193 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1194 sg->length = mbuf->data_len - sym->aead.data.offset +
1196 sg->offset = sym->aead.data.offset - ses->auth_only_len;
1198 /* Successive segs */
1203 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1204 sg->length = mbuf->data_len;
1207 sg->length -= ses->digest_length;
1209 if (is_encode(ses)) {
1211 /* set auth output */
1213 qm_sg_entry_set64(sg, sym->aead.digest.phys_addr);
1214 sg->length = ses->digest_length;
1222 in_sg->extension = 1;
1225 in_sg->length = ses->iv.length + sym->aead.data.length
1226 + ses->auth_only_len;
1228 in_sg->length = ses->iv.length + sym->aead.data.length
1229 + ses->auth_only_len + ses->digest_length;
1231 /* input sg entries */
1233 qm_sg_entry_set64(in_sg, dpaa_mem_vtop(sg));
1234 cpu_to_hw_sg(in_sg);
1237 qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1238 sg->length = ses->iv.length;
1241 /* 2nd seg auth only */
1242 if (ses->auth_only_len) {
1244 qm_sg_entry_set64(sg, dpaa_mem_vtop(sym->aead.aad.data));
1245 sg->length = ses->auth_only_len;
1251 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1252 sg->length = mbuf->data_len - sym->aead.data.offset;
1253 sg->offset = sym->aead.data.offset;
1255 /* Successive segs */
1260 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1261 sg->length = mbuf->data_len;
1265 if (is_decode(ses)) {
1268 memcpy(ctx->digest, sym->aead.digest.data,
1269 ses->digest_length);
1270 qm_sg_entry_set64(sg, dpaa_mem_vtop(ctx->digest));
1271 sg->length = ses->digest_length;
1279 static inline struct dpaa_sec_job *
1280 build_cipher_auth_gcm(struct rte_crypto_op *op, dpaa_sec_session *ses)
1282 struct rte_crypto_sym_op *sym = op->sym;
1283 struct dpaa_sec_job *cf;
1284 struct dpaa_sec_op_ctx *ctx;
1285 struct qm_sg_entry *sg;
1286 uint32_t length = 0;
1287 rte_iova_t src_start_addr, dst_start_addr;
1288 uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1291 src_start_addr = sym->m_src->buf_iova + sym->m_src->data_off;
1294 dst_start_addr = sym->m_dst->buf_iova + sym->m_dst->data_off;
1296 dst_start_addr = src_start_addr;
1298 ctx = dpaa_sec_alloc_ctx(ses);
1306 rte_prefetch0(cf->sg);
1308 qm_sg_entry_set64(&cf->sg[1], dpaa_mem_vtop(sg));
1309 if (is_encode(ses)) {
1310 qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1311 sg->length = ses->iv.length;
1312 length += sg->length;
1316 if (ses->auth_only_len) {
1317 qm_sg_entry_set64(sg,
1318 dpaa_mem_vtop(sym->aead.aad.data));
1319 sg->length = ses->auth_only_len;
1320 length += sg->length;
1324 qm_sg_entry_set64(sg, src_start_addr + sym->aead.data.offset);
1325 sg->length = sym->aead.data.length;
1326 length += sg->length;
1330 qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1331 sg->length = ses->iv.length;
1332 length += sg->length;
1336 if (ses->auth_only_len) {
1337 qm_sg_entry_set64(sg,
1338 dpaa_mem_vtop(sym->aead.aad.data));
1339 sg->length = ses->auth_only_len;
1340 length += sg->length;
1344 qm_sg_entry_set64(sg, src_start_addr + sym->aead.data.offset);
1345 sg->length = sym->aead.data.length;
1346 length += sg->length;
1349 memcpy(ctx->digest, sym->aead.digest.data,
1350 ses->digest_length);
1353 qm_sg_entry_set64(sg, dpaa_mem_vtop(ctx->digest));
1354 sg->length = ses->digest_length;
1355 length += sg->length;
1359 /* input compound frame */
1360 cf->sg[1].length = length;
1361 cf->sg[1].extension = 1;
1362 cf->sg[1].final = 1;
1363 cpu_to_hw_sg(&cf->sg[1]);
1367 qm_sg_entry_set64(&cf->sg[0], dpaa_mem_vtop(sg));
1368 qm_sg_entry_set64(sg,
1369 dst_start_addr + sym->aead.data.offset - ses->auth_only_len);
1370 sg->length = sym->aead.data.length + ses->auth_only_len;
1371 length = sg->length;
1372 if (is_encode(ses)) {
1374 /* set auth output */
1376 qm_sg_entry_set64(sg, sym->aead.digest.phys_addr);
1377 sg->length = ses->digest_length;
1378 length += sg->length;
1383 /* output compound frame */
1384 cf->sg[0].length = length;
1385 cf->sg[0].extension = 1;
1386 cpu_to_hw_sg(&cf->sg[0]);
1391 static inline struct dpaa_sec_job *
1392 build_cipher_auth_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
1394 struct rte_crypto_sym_op *sym = op->sym;
1395 struct dpaa_sec_job *cf;
1396 struct dpaa_sec_op_ctx *ctx;
1397 struct qm_sg_entry *sg, *out_sg, *in_sg;
1398 struct rte_mbuf *mbuf;
1400 uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1405 req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 4;
1408 req_segs = mbuf->nb_segs * 2 + 4;
1411 if (req_segs > MAX_SG_ENTRIES) {
1412 DPAA_SEC_DP_ERR("Cipher-Auth: Max sec segs supported is %d",
1417 ctx = dpaa_sec_alloc_ctx(ses);
1424 rte_prefetch0(cf->sg);
1427 out_sg = &cf->sg[0];
1428 out_sg->extension = 1;
1430 out_sg->length = sym->auth.data.length + ses->digest_length;
1432 out_sg->length = sym->auth.data.length;
1434 /* output sg entries */
1436 qm_sg_entry_set64(out_sg, dpaa_mem_vtop(sg));
1437 cpu_to_hw_sg(out_sg);
1440 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1441 sg->length = mbuf->data_len - sym->auth.data.offset;
1442 sg->offset = sym->auth.data.offset;
1444 /* Successive segs */
1449 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1450 sg->length = mbuf->data_len;
1453 sg->length -= ses->digest_length;
1455 if (is_encode(ses)) {
1457 /* set auth output */
1459 qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
1460 sg->length = ses->digest_length;
1468 in_sg->extension = 1;
1471 in_sg->length = ses->iv.length + sym->auth.data.length;
1473 in_sg->length = ses->iv.length + sym->auth.data.length
1474 + ses->digest_length;
1476 /* input sg entries */
1478 qm_sg_entry_set64(in_sg, dpaa_mem_vtop(sg));
1479 cpu_to_hw_sg(in_sg);
1482 qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1483 sg->length = ses->iv.length;
1488 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1489 sg->length = mbuf->data_len - sym->auth.data.offset;
1490 sg->offset = sym->auth.data.offset;
1492 /* Successive segs */
1497 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1498 sg->length = mbuf->data_len;
1502 sg->length -= ses->digest_length;
1503 if (is_decode(ses)) {
1506 memcpy(ctx->digest, sym->auth.digest.data,
1507 ses->digest_length);
1508 qm_sg_entry_set64(sg, dpaa_mem_vtop(ctx->digest));
1509 sg->length = ses->digest_length;
1517 static inline struct dpaa_sec_job *
1518 build_cipher_auth(struct rte_crypto_op *op, dpaa_sec_session *ses)
1520 struct rte_crypto_sym_op *sym = op->sym;
1521 struct dpaa_sec_job *cf;
1522 struct dpaa_sec_op_ctx *ctx;
1523 struct qm_sg_entry *sg;
1524 rte_iova_t src_start_addr, dst_start_addr;
1525 uint32_t length = 0;
1526 uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1529 src_start_addr = sym->m_src->buf_iova + sym->m_src->data_off;
1531 dst_start_addr = sym->m_dst->buf_iova + sym->m_dst->data_off;
1533 dst_start_addr = src_start_addr;
1535 ctx = dpaa_sec_alloc_ctx(ses);
1543 rte_prefetch0(cf->sg);
1545 qm_sg_entry_set64(&cf->sg[1], dpaa_mem_vtop(sg));
1546 if (is_encode(ses)) {
1547 qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1548 sg->length = ses->iv.length;
1549 length += sg->length;
1553 qm_sg_entry_set64(sg, src_start_addr + sym->auth.data.offset);
1554 sg->length = sym->auth.data.length;
1555 length += sg->length;
1559 qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1560 sg->length = ses->iv.length;
1561 length += sg->length;
1566 qm_sg_entry_set64(sg, src_start_addr + sym->auth.data.offset);
1567 sg->length = sym->auth.data.length;
1568 length += sg->length;
1571 memcpy(ctx->digest, sym->auth.digest.data,
1572 ses->digest_length);
1575 qm_sg_entry_set64(sg, dpaa_mem_vtop(ctx->digest));
1576 sg->length = ses->digest_length;
1577 length += sg->length;
1581 /* input compound frame */
1582 cf->sg[1].length = length;
1583 cf->sg[1].extension = 1;
1584 cf->sg[1].final = 1;
1585 cpu_to_hw_sg(&cf->sg[1]);
1589 qm_sg_entry_set64(&cf->sg[0], dpaa_mem_vtop(sg));
1590 qm_sg_entry_set64(sg, dst_start_addr + sym->cipher.data.offset);
1591 sg->length = sym->cipher.data.length;
1592 length = sg->length;
1593 if (is_encode(ses)) {
1595 /* set auth output */
1597 qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
1598 sg->length = ses->digest_length;
1599 length += sg->length;
1604 /* output compound frame */
1605 cf->sg[0].length = length;
1606 cf->sg[0].extension = 1;
1607 cpu_to_hw_sg(&cf->sg[0]);
1612 static inline struct dpaa_sec_job *
1613 build_proto(struct rte_crypto_op *op, dpaa_sec_session *ses)
1615 struct rte_crypto_sym_op *sym = op->sym;
1616 struct dpaa_sec_job *cf;
1617 struct dpaa_sec_op_ctx *ctx;
1618 struct qm_sg_entry *sg;
1619 phys_addr_t src_start_addr, dst_start_addr;
1621 ctx = dpaa_sec_alloc_ctx(ses);
1627 src_start_addr = rte_pktmbuf_mtophys(sym->m_src);
1630 dst_start_addr = rte_pktmbuf_mtophys(sym->m_dst);
1632 dst_start_addr = src_start_addr;
1636 qm_sg_entry_set64(sg, src_start_addr);
1637 sg->length = sym->m_src->pkt_len;
1641 sym->m_src->packet_type &= ~RTE_PTYPE_L4_MASK;
1644 qm_sg_entry_set64(sg, dst_start_addr);
1645 sg->length = sym->m_src->buf_len - sym->m_src->data_off;
1652 dpaa_sec_enqueue_burst(void *qp, struct rte_crypto_op **ops,
1655 /* Function to transmit the frames to given device and queuepair */
1657 struct dpaa_sec_qp *dpaa_qp = (struct dpaa_sec_qp *)qp;
1658 uint16_t num_tx = 0;
1659 struct qm_fd fds[DPAA_SEC_BURST], *fd;
1660 uint32_t frames_to_send;
1661 struct rte_crypto_op *op;
1662 struct dpaa_sec_job *cf;
1663 dpaa_sec_session *ses;
1664 uint32_t auth_only_len;
1665 struct qman_fq *inq[DPAA_SEC_BURST];
1668 frames_to_send = (nb_ops > DPAA_SEC_BURST) ?
1669 DPAA_SEC_BURST : nb_ops;
1670 for (loop = 0; loop < frames_to_send; loop++) {
1672 switch (op->sess_type) {
1673 case RTE_CRYPTO_OP_WITH_SESSION:
1674 ses = (dpaa_sec_session *)
1675 get_sym_session_private_data(
1677 cryptodev_driver_id);
1679 case RTE_CRYPTO_OP_SECURITY_SESSION:
1680 ses = (dpaa_sec_session *)
1681 get_sec_session_private_data(
1682 op->sym->sec_session);
1686 "sessionless crypto op not supported");
1687 frames_to_send = loop;
1691 if (unlikely(!ses->qp[rte_lcore_id() % MAX_DPAA_CORES])) {
1692 if (dpaa_sec_attach_sess_q(qp, ses)) {
1693 frames_to_send = loop;
1697 } else if (unlikely(ses->qp[rte_lcore_id() %
1698 MAX_DPAA_CORES] != qp)) {
1699 DPAA_SEC_DP_ERR("Old:sess->qp = %p"
1701 ses->qp[rte_lcore_id() %
1702 MAX_DPAA_CORES], qp);
1703 frames_to_send = loop;
1708 auth_only_len = op->sym->auth.data.length -
1709 op->sym->cipher.data.length;
1710 if (rte_pktmbuf_is_contiguous(op->sym->m_src)) {
1711 if (is_proto_ipsec(ses)) {
1712 cf = build_proto(op, ses);
1713 } else if (is_proto_pdcp(ses)) {
1714 cf = build_proto(op, ses);
1715 } else if (is_auth_only(ses)) {
1716 cf = build_auth_only(op, ses);
1717 } else if (is_cipher_only(ses)) {
1718 cf = build_cipher_only(op, ses);
1719 } else if (is_aead(ses)) {
1720 cf = build_cipher_auth_gcm(op, ses);
1721 auth_only_len = ses->auth_only_len;
1722 } else if (is_auth_cipher(ses)) {
1723 cf = build_cipher_auth(op, ses);
1725 DPAA_SEC_DP_ERR("not supported ops");
1726 frames_to_send = loop;
1731 if (is_auth_only(ses)) {
1732 cf = build_auth_only_sg(op, ses);
1733 } else if (is_cipher_only(ses)) {
1734 cf = build_cipher_only_sg(op, ses);
1735 } else if (is_aead(ses)) {
1736 cf = build_cipher_auth_gcm_sg(op, ses);
1737 auth_only_len = ses->auth_only_len;
1738 } else if (is_auth_cipher(ses)) {
1739 cf = build_cipher_auth_sg(op, ses);
1741 DPAA_SEC_DP_ERR("not supported ops");
1742 frames_to_send = loop;
1747 if (unlikely(!cf)) {
1748 frames_to_send = loop;
1754 inq[loop] = ses->inq[rte_lcore_id() % MAX_DPAA_CORES];
1755 fd->opaque_addr = 0;
1757 qm_fd_addr_set64(fd, dpaa_mem_vtop(cf->sg));
1758 fd->_format1 = qm_fd_compound;
1759 fd->length29 = 2 * sizeof(struct qm_sg_entry);
1760 /* Auth_only_len is set as 0 in descriptor and it is
1761 * overwritten here in the fd.cmd which will update
1765 fd->cmd = 0x80000000 | auth_only_len;
1770 while (loop < frames_to_send) {
1771 loop += qman_enqueue_multi_fq(&inq[loop], &fds[loop],
1772 frames_to_send - loop);
1774 nb_ops -= frames_to_send;
1775 num_tx += frames_to_send;
1778 dpaa_qp->tx_pkts += num_tx;
1779 dpaa_qp->tx_errs += nb_ops - num_tx;
1785 dpaa_sec_dequeue_burst(void *qp, struct rte_crypto_op **ops,
1789 struct dpaa_sec_qp *dpaa_qp = (struct dpaa_sec_qp *)qp;
1791 num_rx = dpaa_sec_deq(dpaa_qp, ops, nb_ops);
1793 dpaa_qp->rx_pkts += num_rx;
1794 dpaa_qp->rx_errs += nb_ops - num_rx;
1796 DPAA_SEC_DP_DEBUG("SEC Received %d Packets\n", num_rx);
1801 /** Release queue pair */
1803 dpaa_sec_queue_pair_release(struct rte_cryptodev *dev,
1806 struct dpaa_sec_dev_private *internals;
1807 struct dpaa_sec_qp *qp = NULL;
1809 PMD_INIT_FUNC_TRACE();
1811 DPAA_SEC_DEBUG("dev =%p, queue =%d", dev, qp_id);
1813 internals = dev->data->dev_private;
1814 if (qp_id >= internals->max_nb_queue_pairs) {
1815 DPAA_SEC_ERR("Max supported qpid %d",
1816 internals->max_nb_queue_pairs);
1820 qp = &internals->qps[qp_id];
1821 qp->internals = NULL;
1822 dev->data->queue_pairs[qp_id] = NULL;
1827 /** Setup a queue pair */
1829 dpaa_sec_queue_pair_setup(struct rte_cryptodev *dev, uint16_t qp_id,
1830 __rte_unused const struct rte_cryptodev_qp_conf *qp_conf,
1831 __rte_unused int socket_id)
1833 struct dpaa_sec_dev_private *internals;
1834 struct dpaa_sec_qp *qp = NULL;
1836 DPAA_SEC_DEBUG("dev =%p, queue =%d, conf =%p", dev, qp_id, qp_conf);
1838 internals = dev->data->dev_private;
1839 if (qp_id >= internals->max_nb_queue_pairs) {
1840 DPAA_SEC_ERR("Max supported qpid %d",
1841 internals->max_nb_queue_pairs);
1845 qp = &internals->qps[qp_id];
1846 qp->internals = internals;
1847 dev->data->queue_pairs[qp_id] = qp;
1852 /** Return the number of allocated queue pairs */
1854 dpaa_sec_queue_pair_count(struct rte_cryptodev *dev)
1856 PMD_INIT_FUNC_TRACE();
1858 return dev->data->nb_queue_pairs;
1861 /** Returns the size of session structure */
1863 dpaa_sec_sym_session_get_size(struct rte_cryptodev *dev __rte_unused)
1865 PMD_INIT_FUNC_TRACE();
1867 return sizeof(dpaa_sec_session);
1871 dpaa_sec_cipher_init(struct rte_cryptodev *dev __rte_unused,
1872 struct rte_crypto_sym_xform *xform,
1873 dpaa_sec_session *session)
1875 session->cipher_alg = xform->cipher.algo;
1876 session->iv.length = xform->cipher.iv.length;
1877 session->iv.offset = xform->cipher.iv.offset;
1878 session->cipher_key.data = rte_zmalloc(NULL, xform->cipher.key.length,
1879 RTE_CACHE_LINE_SIZE);
1880 if (session->cipher_key.data == NULL && xform->cipher.key.length > 0) {
1881 DPAA_SEC_ERR("No Memory for cipher key");
1884 session->cipher_key.length = xform->cipher.key.length;
1886 memcpy(session->cipher_key.data, xform->cipher.key.data,
1887 xform->cipher.key.length);
1888 session->dir = (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
1895 dpaa_sec_auth_init(struct rte_cryptodev *dev __rte_unused,
1896 struct rte_crypto_sym_xform *xform,
1897 dpaa_sec_session *session)
1899 session->auth_alg = xform->auth.algo;
1900 session->auth_key.data = rte_zmalloc(NULL, xform->auth.key.length,
1901 RTE_CACHE_LINE_SIZE);
1902 if (session->auth_key.data == NULL && xform->auth.key.length > 0) {
1903 DPAA_SEC_ERR("No Memory for auth key");
1906 session->auth_key.length = xform->auth.key.length;
1907 session->digest_length = xform->auth.digest_length;
1909 memcpy(session->auth_key.data, xform->auth.key.data,
1910 xform->auth.key.length);
1911 session->dir = (xform->auth.op == RTE_CRYPTO_AUTH_OP_GENERATE) ?
1918 dpaa_sec_aead_init(struct rte_cryptodev *dev __rte_unused,
1919 struct rte_crypto_sym_xform *xform,
1920 dpaa_sec_session *session)
1922 session->aead_alg = xform->aead.algo;
1923 session->iv.length = xform->aead.iv.length;
1924 session->iv.offset = xform->aead.iv.offset;
1925 session->auth_only_len = xform->aead.aad_length;
1926 session->aead_key.data = rte_zmalloc(NULL, xform->aead.key.length,
1927 RTE_CACHE_LINE_SIZE);
1928 if (session->aead_key.data == NULL && xform->aead.key.length > 0) {
1929 DPAA_SEC_ERR("No Memory for aead key\n");
1932 session->aead_key.length = xform->aead.key.length;
1933 session->digest_length = xform->aead.digest_length;
1935 memcpy(session->aead_key.data, xform->aead.key.data,
1936 xform->aead.key.length);
1937 session->dir = (xform->aead.op == RTE_CRYPTO_AEAD_OP_ENCRYPT) ?
1943 static struct qman_fq *
1944 dpaa_sec_attach_rxq(struct dpaa_sec_dev_private *qi)
1948 for (i = 0; i < qi->max_nb_sessions * MAX_DPAA_CORES; i++) {
1949 if (qi->inq_attach[i] == 0) {
1950 qi->inq_attach[i] = 1;
1954 DPAA_SEC_WARN("All session in use %u", qi->max_nb_sessions);
1960 dpaa_sec_detach_rxq(struct dpaa_sec_dev_private *qi, struct qman_fq *fq)
1964 for (i = 0; i < qi->max_nb_sessions; i++) {
1965 if (&qi->inq[i] == fq) {
1966 qman_retire_fq(fq, NULL);
1968 qi->inq_attach[i] = 0;
1976 dpaa_sec_attach_sess_q(struct dpaa_sec_qp *qp, dpaa_sec_session *sess)
1980 sess->qp[rte_lcore_id() % MAX_DPAA_CORES] = qp;
1981 ret = dpaa_sec_prep_cdb(sess);
1983 DPAA_SEC_ERR("Unable to prepare sec cdb");
1986 if (unlikely(!RTE_PER_LCORE(dpaa_io))) {
1987 ret = rte_dpaa_portal_init((void *)0);
1989 DPAA_SEC_ERR("Failure in affining portal");
1993 ret = dpaa_sec_init_rx(sess->inq[rte_lcore_id() % MAX_DPAA_CORES],
1994 dpaa_mem_vtop(&sess->cdb),
1995 qman_fq_fqid(&qp->outq));
1997 DPAA_SEC_ERR("Unable to init sec queue");
2003 dpaa_sec_set_session_parameters(struct rte_cryptodev *dev,
2004 struct rte_crypto_sym_xform *xform, void *sess)
2006 struct dpaa_sec_dev_private *internals = dev->data->dev_private;
2007 dpaa_sec_session *session = sess;
2010 PMD_INIT_FUNC_TRACE();
2012 if (unlikely(sess == NULL)) {
2013 DPAA_SEC_ERR("invalid session struct");
2016 memset(session, 0, sizeof(dpaa_sec_session));
2018 /* Default IV length = 0 */
2019 session->iv.length = 0;
2022 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER && xform->next == NULL) {
2023 session->auth_alg = RTE_CRYPTO_AUTH_NULL;
2024 dpaa_sec_cipher_init(dev, xform, session);
2026 /* Authentication Only */
2027 } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
2028 xform->next == NULL) {
2029 session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
2030 dpaa_sec_auth_init(dev, xform, session);
2032 /* Cipher then Authenticate */
2033 } else if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
2034 xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
2035 if (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) {
2036 dpaa_sec_cipher_init(dev, xform, session);
2037 dpaa_sec_auth_init(dev, xform->next, session);
2039 DPAA_SEC_ERR("Not supported: Auth then Cipher");
2043 /* Authenticate then Cipher */
2044 } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
2045 xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
2046 if (xform->next->cipher.op == RTE_CRYPTO_CIPHER_OP_DECRYPT) {
2047 dpaa_sec_auth_init(dev, xform, session);
2048 dpaa_sec_cipher_init(dev, xform->next, session);
2050 DPAA_SEC_ERR("Not supported: Auth then Cipher");
2054 /* AEAD operation for AES-GCM kind of Algorithms */
2055 } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD &&
2056 xform->next == NULL) {
2057 dpaa_sec_aead_init(dev, xform, session);
2060 DPAA_SEC_ERR("Invalid crypto type");
2063 session->ctx_pool = internals->ctx_pool;
2064 rte_spinlock_lock(&internals->lock);
2065 for (i = 0; i < MAX_DPAA_CORES; i++) {
2066 session->inq[i] = dpaa_sec_attach_rxq(internals);
2067 if (session->inq[i] == NULL) {
2068 DPAA_SEC_ERR("unable to attach sec queue");
2069 rte_spinlock_unlock(&internals->lock);
2073 rte_spinlock_unlock(&internals->lock);
2078 rte_free(session->cipher_key.data);
2079 rte_free(session->auth_key.data);
2080 memset(session, 0, sizeof(dpaa_sec_session));
2086 dpaa_sec_sym_session_configure(struct rte_cryptodev *dev,
2087 struct rte_crypto_sym_xform *xform,
2088 struct rte_cryptodev_sym_session *sess,
2089 struct rte_mempool *mempool)
2091 void *sess_private_data;
2094 PMD_INIT_FUNC_TRACE();
2096 if (rte_mempool_get(mempool, &sess_private_data)) {
2097 DPAA_SEC_ERR("Couldn't get object from session mempool");
2101 ret = dpaa_sec_set_session_parameters(dev, xform, sess_private_data);
2103 DPAA_SEC_ERR("failed to configure session parameters");
2105 /* Return session to mempool */
2106 rte_mempool_put(mempool, sess_private_data);
2110 set_sym_session_private_data(sess, dev->driver_id,
2118 free_session_memory(struct rte_cryptodev *dev, dpaa_sec_session *s)
2120 struct dpaa_sec_dev_private *qi = dev->data->dev_private;
2121 struct rte_mempool *sess_mp = rte_mempool_from_obj((void *)s);
2124 for (i = 0; i < MAX_DPAA_CORES; i++) {
2126 dpaa_sec_detach_rxq(qi, s->inq[i]);
2130 rte_free(s->cipher_key.data);
2131 rte_free(s->auth_key.data);
2132 memset(s, 0, sizeof(dpaa_sec_session));
2133 rte_mempool_put(sess_mp, (void *)s);
2136 /** Clear the memory of session so it doesn't leave key material behind */
2138 dpaa_sec_sym_session_clear(struct rte_cryptodev *dev,
2139 struct rte_cryptodev_sym_session *sess)
2141 PMD_INIT_FUNC_TRACE();
2142 uint8_t index = dev->driver_id;
2143 void *sess_priv = get_sym_session_private_data(sess, index);
2144 dpaa_sec_session *s = (dpaa_sec_session *)sess_priv;
2147 free_session_memory(dev, s);
2148 set_sym_session_private_data(sess, index, NULL);
2153 dpaa_sec_set_ipsec_session(__rte_unused struct rte_cryptodev *dev,
2154 struct rte_security_session_conf *conf,
2157 struct dpaa_sec_dev_private *internals = dev->data->dev_private;
2158 struct rte_security_ipsec_xform *ipsec_xform = &conf->ipsec;
2159 struct rte_crypto_auth_xform *auth_xform = NULL;
2160 struct rte_crypto_cipher_xform *cipher_xform = NULL;
2161 dpaa_sec_session *session = (dpaa_sec_session *)sess;
2164 PMD_INIT_FUNC_TRACE();
2166 memset(session, 0, sizeof(dpaa_sec_session));
2167 if (ipsec_xform->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
2168 cipher_xform = &conf->crypto_xform->cipher;
2169 if (conf->crypto_xform->next)
2170 auth_xform = &conf->crypto_xform->next->auth;
2172 auth_xform = &conf->crypto_xform->auth;
2173 if (conf->crypto_xform->next)
2174 cipher_xform = &conf->crypto_xform->next->cipher;
2176 session->proto_alg = conf->protocol;
2178 if (cipher_xform && cipher_xform->algo != RTE_CRYPTO_CIPHER_NULL) {
2179 session->cipher_key.data = rte_zmalloc(NULL,
2180 cipher_xform->key.length,
2181 RTE_CACHE_LINE_SIZE);
2182 if (session->cipher_key.data == NULL &&
2183 cipher_xform->key.length > 0) {
2184 DPAA_SEC_ERR("No Memory for cipher key");
2187 memcpy(session->cipher_key.data, cipher_xform->key.data,
2188 cipher_xform->key.length);
2189 session->cipher_key.length = cipher_xform->key.length;
2191 switch (cipher_xform->algo) {
2192 case RTE_CRYPTO_CIPHER_AES_CBC:
2193 case RTE_CRYPTO_CIPHER_3DES_CBC:
2194 case RTE_CRYPTO_CIPHER_AES_CTR:
2197 DPAA_SEC_ERR("Crypto: Unsupported Cipher alg %u",
2198 cipher_xform->algo);
2201 session->cipher_alg = cipher_xform->algo;
2203 session->cipher_key.data = NULL;
2204 session->cipher_key.length = 0;
2205 session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
2208 if (auth_xform && auth_xform->algo != RTE_CRYPTO_AUTH_NULL) {
2209 session->auth_key.data = rte_zmalloc(NULL,
2210 auth_xform->key.length,
2211 RTE_CACHE_LINE_SIZE);
2212 if (session->auth_key.data == NULL &&
2213 auth_xform->key.length > 0) {
2214 DPAA_SEC_ERR("No Memory for auth key");
2215 rte_free(session->cipher_key.data);
2218 memcpy(session->auth_key.data, auth_xform->key.data,
2219 auth_xform->key.length);
2220 session->auth_key.length = auth_xform->key.length;
2222 switch (auth_xform->algo) {
2223 case RTE_CRYPTO_AUTH_SHA1_HMAC:
2224 case RTE_CRYPTO_AUTH_MD5_HMAC:
2225 case RTE_CRYPTO_AUTH_SHA256_HMAC:
2226 case RTE_CRYPTO_AUTH_SHA384_HMAC:
2227 case RTE_CRYPTO_AUTH_SHA512_HMAC:
2228 case RTE_CRYPTO_AUTH_AES_CMAC:
2231 DPAA_SEC_ERR("Crypto: Unsupported auth alg %u",
2235 session->auth_alg = auth_xform->algo;
2237 session->auth_key.data = NULL;
2238 session->auth_key.length = 0;
2239 session->auth_alg = RTE_CRYPTO_AUTH_NULL;
2242 if (ipsec_xform->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
2243 if (ipsec_xform->tunnel.type ==
2244 RTE_SECURITY_IPSEC_TUNNEL_IPV4) {
2245 memset(&session->encap_pdb, 0,
2246 sizeof(struct ipsec_encap_pdb) +
2247 sizeof(session->ip4_hdr));
2248 session->ip4_hdr.ip_v = IPVERSION;
2249 session->ip4_hdr.ip_hl = 5;
2250 session->ip4_hdr.ip_len = rte_cpu_to_be_16(
2251 sizeof(session->ip4_hdr));
2252 session->ip4_hdr.ip_tos = ipsec_xform->tunnel.ipv4.dscp;
2253 session->ip4_hdr.ip_id = 0;
2254 session->ip4_hdr.ip_off = 0;
2255 session->ip4_hdr.ip_ttl = ipsec_xform->tunnel.ipv4.ttl;
2256 session->ip4_hdr.ip_p = (ipsec_xform->proto ==
2257 RTE_SECURITY_IPSEC_SA_PROTO_ESP) ?
2258 IPPROTO_ESP : IPPROTO_AH;
2259 session->ip4_hdr.ip_sum = 0;
2260 session->ip4_hdr.ip_src =
2261 ipsec_xform->tunnel.ipv4.src_ip;
2262 session->ip4_hdr.ip_dst =
2263 ipsec_xform->tunnel.ipv4.dst_ip;
2264 session->ip4_hdr.ip_sum = calc_chksum((uint16_t *)
2265 (void *)&session->ip4_hdr,
2267 session->encap_pdb.ip_hdr_len = sizeof(struct ip);
2268 } else if (ipsec_xform->tunnel.type ==
2269 RTE_SECURITY_IPSEC_TUNNEL_IPV6) {
2270 memset(&session->encap_pdb, 0,
2271 sizeof(struct ipsec_encap_pdb) +
2272 sizeof(session->ip6_hdr));
2273 session->ip6_hdr.vtc_flow = rte_cpu_to_be_32(
2274 DPAA_IPv6_DEFAULT_VTC_FLOW |
2275 ((ipsec_xform->tunnel.ipv6.dscp <<
2276 RTE_IPV6_HDR_TC_SHIFT) &
2277 RTE_IPV6_HDR_TC_MASK) |
2278 ((ipsec_xform->tunnel.ipv6.flabel <<
2279 RTE_IPV6_HDR_FL_SHIFT) &
2280 RTE_IPV6_HDR_FL_MASK));
2281 /* Payload length will be updated by HW */
2282 session->ip6_hdr.payload_len = 0;
2283 session->ip6_hdr.hop_limits =
2284 ipsec_xform->tunnel.ipv6.hlimit;
2285 session->ip6_hdr.proto = (ipsec_xform->proto ==
2286 RTE_SECURITY_IPSEC_SA_PROTO_ESP) ?
2287 IPPROTO_ESP : IPPROTO_AH;
2288 memcpy(&session->ip6_hdr.src_addr,
2289 &ipsec_xform->tunnel.ipv6.src_addr, 16);
2290 memcpy(&session->ip6_hdr.dst_addr,
2291 &ipsec_xform->tunnel.ipv6.dst_addr, 16);
2292 session->encap_pdb.ip_hdr_len =
2293 sizeof(struct rte_ipv6_hdr);
2295 session->encap_pdb.options =
2296 (IPVERSION << PDBNH_ESP_ENCAP_SHIFT) |
2297 PDBOPTS_ESP_OIHI_PDB_INL |
2299 PDBHMO_ESP_ENCAP_DTTL |
2301 if (ipsec_xform->options.esn)
2302 session->encap_pdb.options |= PDBOPTS_ESP_ESN;
2303 session->encap_pdb.spi = ipsec_xform->spi;
2304 session->dir = DIR_ENC;
2305 } else if (ipsec_xform->direction ==
2306 RTE_SECURITY_IPSEC_SA_DIR_INGRESS) {
2307 memset(&session->decap_pdb, 0, sizeof(struct ipsec_decap_pdb));
2308 if (ipsec_xform->tunnel.type == RTE_SECURITY_IPSEC_TUNNEL_IPV4)
2309 session->decap_pdb.options = sizeof(struct ip) << 16;
2311 session->decap_pdb.options =
2312 sizeof(struct rte_ipv6_hdr) << 16;
2313 if (ipsec_xform->options.esn)
2314 session->decap_pdb.options |= PDBOPTS_ESP_ESN;
2315 session->dir = DIR_DEC;
2318 session->ctx_pool = internals->ctx_pool;
2319 rte_spinlock_lock(&internals->lock);
2320 for (i = 0; i < MAX_DPAA_CORES; i++) {
2321 session->inq[i] = dpaa_sec_attach_rxq(internals);
2322 if (session->inq[i] == NULL) {
2323 DPAA_SEC_ERR("unable to attach sec queue");
2324 rte_spinlock_unlock(&internals->lock);
2328 rte_spinlock_unlock(&internals->lock);
2332 rte_free(session->auth_key.data);
2333 rte_free(session->cipher_key.data);
2334 memset(session, 0, sizeof(dpaa_sec_session));
2339 dpaa_sec_set_pdcp_session(struct rte_cryptodev *dev,
2340 struct rte_security_session_conf *conf,
2343 struct rte_security_pdcp_xform *pdcp_xform = &conf->pdcp;
2344 struct rte_crypto_sym_xform *xform = conf->crypto_xform;
2345 struct rte_crypto_auth_xform *auth_xform = NULL;
2346 struct rte_crypto_cipher_xform *cipher_xform = NULL;
2347 dpaa_sec_session *session = (dpaa_sec_session *)sess;
2348 struct dpaa_sec_dev_private *dev_priv = dev->data->dev_private;
2351 PMD_INIT_FUNC_TRACE();
2353 memset(session, 0, sizeof(dpaa_sec_session));
2355 /* find xfrm types */
2356 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
2357 cipher_xform = &xform->cipher;
2358 if (xform->next != NULL)
2359 auth_xform = &xform->next->auth;
2360 } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
2361 auth_xform = &xform->auth;
2362 if (xform->next != NULL)
2363 cipher_xform = &xform->next->cipher;
2365 DPAA_SEC_ERR("Invalid crypto type");
2369 session->proto_alg = conf->protocol;
2371 session->cipher_key.data = rte_zmalloc(NULL,
2372 cipher_xform->key.length,
2373 RTE_CACHE_LINE_SIZE);
2374 if (session->cipher_key.data == NULL &&
2375 cipher_xform->key.length > 0) {
2376 DPAA_SEC_ERR("No Memory for cipher key");
2379 session->cipher_key.length = cipher_xform->key.length;
2380 memcpy(session->cipher_key.data, cipher_xform->key.data,
2381 cipher_xform->key.length);
2382 session->dir = (cipher_xform->op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
2384 session->cipher_alg = cipher_xform->algo;
2386 session->cipher_key.data = NULL;
2387 session->cipher_key.length = 0;
2388 session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
2389 session->dir = DIR_ENC;
2392 /* Auth is only applicable for control mode operation. */
2393 if (pdcp_xform->domain == RTE_SECURITY_PDCP_MODE_CONTROL) {
2394 if (pdcp_xform->sn_size != RTE_SECURITY_PDCP_SN_SIZE_5 &&
2395 pdcp_xform->sn_size != RTE_SECURITY_PDCP_SN_SIZE_12) {
2397 "PDCP Seq Num size should be 5/12 bits for cmode");
2401 session->auth_key.data = rte_zmalloc(NULL,
2402 auth_xform->key.length,
2403 RTE_CACHE_LINE_SIZE);
2404 if (session->auth_key.data == NULL &&
2405 auth_xform->key.length > 0) {
2406 DPAA_SEC_ERR("No Memory for auth key");
2407 rte_free(session->cipher_key.data);
2410 session->auth_key.length = auth_xform->key.length;
2411 memcpy(session->auth_key.data, auth_xform->key.data,
2412 auth_xform->key.length);
2413 session->auth_alg = auth_xform->algo;
2415 session->auth_key.data = NULL;
2416 session->auth_key.length = 0;
2417 session->auth_alg = RTE_CRYPTO_AUTH_NULL;
2420 session->pdcp.domain = pdcp_xform->domain;
2421 session->pdcp.bearer = pdcp_xform->bearer;
2422 session->pdcp.pkt_dir = pdcp_xform->pkt_dir;
2423 session->pdcp.sn_size = pdcp_xform->sn_size;
2424 #ifdef ENABLE_HFN_OVERRIDE
2425 session->pdcp.hfn_ovd = pdcp_xform->hfn_ovd;
2427 session->pdcp.hfn = pdcp_xform->hfn;
2428 session->pdcp.hfn_threshold = pdcp_xform->hfn_threshold;
2430 session->ctx_pool = dev_priv->ctx_pool;
2431 rte_spinlock_lock(&dev_priv->lock);
2432 for (i = 0; i < MAX_DPAA_CORES; i++) {
2433 session->inq[i] = dpaa_sec_attach_rxq(dev_priv);
2434 if (session->inq[i] == NULL) {
2435 DPAA_SEC_ERR("unable to attach sec queue");
2436 rte_spinlock_unlock(&dev_priv->lock);
2440 rte_spinlock_unlock(&dev_priv->lock);
2443 rte_free(session->auth_key.data);
2444 rte_free(session->cipher_key.data);
2445 memset(session, 0, sizeof(dpaa_sec_session));
2450 dpaa_sec_security_session_create(void *dev,
2451 struct rte_security_session_conf *conf,
2452 struct rte_security_session *sess,
2453 struct rte_mempool *mempool)
2455 void *sess_private_data;
2456 struct rte_cryptodev *cdev = (struct rte_cryptodev *)dev;
2459 if (rte_mempool_get(mempool, &sess_private_data)) {
2460 DPAA_SEC_ERR("Couldn't get object from session mempool");
2464 switch (conf->protocol) {
2465 case RTE_SECURITY_PROTOCOL_IPSEC:
2466 ret = dpaa_sec_set_ipsec_session(cdev, conf,
2469 case RTE_SECURITY_PROTOCOL_PDCP:
2470 ret = dpaa_sec_set_pdcp_session(cdev, conf,
2473 case RTE_SECURITY_PROTOCOL_MACSEC:
2479 DPAA_SEC_ERR("failed to configure session parameters");
2480 /* Return session to mempool */
2481 rte_mempool_put(mempool, sess_private_data);
2485 set_sec_session_private_data(sess, sess_private_data);
2490 /** Clear the memory of session so it doesn't leave key material behind */
2492 dpaa_sec_security_session_destroy(void *dev __rte_unused,
2493 struct rte_security_session *sess)
2495 PMD_INIT_FUNC_TRACE();
2496 void *sess_priv = get_sec_session_private_data(sess);
2497 dpaa_sec_session *s = (dpaa_sec_session *)sess_priv;
2500 free_session_memory((struct rte_cryptodev *)dev, s);
2501 set_sec_session_private_data(sess, NULL);
2507 dpaa_sec_dev_configure(struct rte_cryptodev *dev,
2508 struct rte_cryptodev_config *config __rte_unused)
2512 struct dpaa_sec_dev_private *internals;
2514 PMD_INIT_FUNC_TRACE();
2516 internals = dev->data->dev_private;
2517 snprintf(str, sizeof(str), "ctx_pool_%d", dev->data->dev_id);
2518 if (!internals->ctx_pool) {
2519 internals->ctx_pool = rte_mempool_create((const char *)str,
2522 CTX_POOL_CACHE_SIZE, 0,
2523 NULL, NULL, NULL, NULL,
2525 if (!internals->ctx_pool) {
2526 DPAA_SEC_ERR("%s create failed\n", str);
2530 DPAA_SEC_INFO("mempool already created for dev_id : %d",
2537 dpaa_sec_dev_start(struct rte_cryptodev *dev __rte_unused)
2539 PMD_INIT_FUNC_TRACE();
2544 dpaa_sec_dev_stop(struct rte_cryptodev *dev __rte_unused)
2546 PMD_INIT_FUNC_TRACE();
2550 dpaa_sec_dev_close(struct rte_cryptodev *dev)
2552 struct dpaa_sec_dev_private *internals;
2554 PMD_INIT_FUNC_TRACE();
2559 internals = dev->data->dev_private;
2560 rte_mempool_free(internals->ctx_pool);
2561 internals->ctx_pool = NULL;
2567 dpaa_sec_dev_infos_get(struct rte_cryptodev *dev,
2568 struct rte_cryptodev_info *info)
2570 struct dpaa_sec_dev_private *internals = dev->data->dev_private;
2572 PMD_INIT_FUNC_TRACE();
2574 info->max_nb_queue_pairs = internals->max_nb_queue_pairs;
2575 info->feature_flags = dev->feature_flags;
2576 info->capabilities = dpaa_sec_capabilities;
2577 info->sym.max_nb_sessions = internals->max_nb_sessions;
2578 info->driver_id = cryptodev_driver_id;
2582 static struct rte_cryptodev_ops crypto_ops = {
2583 .dev_configure = dpaa_sec_dev_configure,
2584 .dev_start = dpaa_sec_dev_start,
2585 .dev_stop = dpaa_sec_dev_stop,
2586 .dev_close = dpaa_sec_dev_close,
2587 .dev_infos_get = dpaa_sec_dev_infos_get,
2588 .queue_pair_setup = dpaa_sec_queue_pair_setup,
2589 .queue_pair_release = dpaa_sec_queue_pair_release,
2590 .queue_pair_count = dpaa_sec_queue_pair_count,
2591 .sym_session_get_size = dpaa_sec_sym_session_get_size,
2592 .sym_session_configure = dpaa_sec_sym_session_configure,
2593 .sym_session_clear = dpaa_sec_sym_session_clear
2596 static const struct rte_security_capability *
2597 dpaa_sec_capabilities_get(void *device __rte_unused)
2599 return dpaa_sec_security_cap;
2602 static const struct rte_security_ops dpaa_sec_security_ops = {
2603 .session_create = dpaa_sec_security_session_create,
2604 .session_update = NULL,
2605 .session_stats_get = NULL,
2606 .session_destroy = dpaa_sec_security_session_destroy,
2607 .set_pkt_metadata = NULL,
2608 .capabilities_get = dpaa_sec_capabilities_get
2612 dpaa_sec_uninit(struct rte_cryptodev *dev)
2614 struct dpaa_sec_dev_private *internals;
2619 internals = dev->data->dev_private;
2620 rte_free(dev->security_ctx);
2622 /* In case close has been called, internals->ctx_pool would be NULL */
2623 rte_mempool_free(internals->ctx_pool);
2624 rte_free(internals);
2626 DPAA_SEC_INFO("Closing DPAA_SEC device %s on numa socket %u",
2627 dev->data->name, rte_socket_id());
2633 dpaa_sec_dev_init(struct rte_cryptodev *cryptodev)
2635 struct dpaa_sec_dev_private *internals;
2636 struct rte_security_ctx *security_instance;
2637 struct dpaa_sec_qp *qp;
2641 PMD_INIT_FUNC_TRACE();
2643 cryptodev->driver_id = cryptodev_driver_id;
2644 cryptodev->dev_ops = &crypto_ops;
2646 cryptodev->enqueue_burst = dpaa_sec_enqueue_burst;
2647 cryptodev->dequeue_burst = dpaa_sec_dequeue_burst;
2648 cryptodev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
2649 RTE_CRYPTODEV_FF_HW_ACCELERATED |
2650 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
2651 RTE_CRYPTODEV_FF_SECURITY |
2652 RTE_CRYPTODEV_FF_IN_PLACE_SGL |
2653 RTE_CRYPTODEV_FF_OOP_SGL_IN_SGL_OUT |
2654 RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT |
2655 RTE_CRYPTODEV_FF_OOP_LB_IN_SGL_OUT |
2656 RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT;
2658 internals = cryptodev->data->dev_private;
2659 internals->max_nb_queue_pairs = RTE_DPAA_MAX_NB_SEC_QPS;
2660 internals->max_nb_sessions = RTE_DPAA_SEC_PMD_MAX_NB_SESSIONS;
2663 * For secondary processes, we don't initialise any further as primary
2664 * has already done this work. Only check we don't need a different
2667 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
2668 DPAA_SEC_WARN("Device already init by primary process");
2672 /* Initialize security_ctx only for primary process*/
2673 security_instance = rte_malloc("rte_security_instances_ops",
2674 sizeof(struct rte_security_ctx), 0);
2675 if (security_instance == NULL)
2677 security_instance->device = (void *)cryptodev;
2678 security_instance->ops = &dpaa_sec_security_ops;
2679 security_instance->sess_cnt = 0;
2680 cryptodev->security_ctx = security_instance;
2682 rte_spinlock_init(&internals->lock);
2683 for (i = 0; i < internals->max_nb_queue_pairs; i++) {
2684 /* init qman fq for queue pair */
2685 qp = &internals->qps[i];
2686 ret = dpaa_sec_init_tx(&qp->outq);
2688 DPAA_SEC_ERR("config tx of queue pair %d", i);
2693 flags = QMAN_FQ_FLAG_LOCKED | QMAN_FQ_FLAG_DYNAMIC_FQID |
2694 QMAN_FQ_FLAG_TO_DCPORTAL;
2695 for (i = 0; i < MAX_DPAA_CORES * internals->max_nb_sessions; i++) {
2696 /* create rx qman fq for sessions*/
2697 ret = qman_create_fq(0, flags, &internals->inq[i]);
2698 if (unlikely(ret != 0)) {
2699 DPAA_SEC_ERR("sec qman_create_fq failed");
2704 RTE_LOG(INFO, PMD, "%s cryptodev init\n", cryptodev->data->name);
2708 DPAA_SEC_ERR("driver %s: create failed\n", cryptodev->data->name);
2710 dpaa_sec_uninit(cryptodev);
2715 cryptodev_dpaa_sec_probe(struct rte_dpaa_driver *dpaa_drv __rte_unused,
2716 struct rte_dpaa_device *dpaa_dev)
2718 struct rte_cryptodev *cryptodev;
2719 char cryptodev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
2723 snprintf(cryptodev_name, sizeof(cryptodev_name), "dpaa_sec-%d",
2724 dpaa_dev->id.dev_id);
2726 cryptodev = rte_cryptodev_pmd_allocate(cryptodev_name, rte_socket_id());
2727 if (cryptodev == NULL)
2730 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
2731 cryptodev->data->dev_private = rte_zmalloc_socket(
2732 "cryptodev private structure",
2733 sizeof(struct dpaa_sec_dev_private),
2734 RTE_CACHE_LINE_SIZE,
2737 if (cryptodev->data->dev_private == NULL)
2738 rte_panic("Cannot allocate memzone for private "
2742 dpaa_dev->crypto_dev = cryptodev;
2743 cryptodev->device = &dpaa_dev->device;
2745 /* init user callbacks */
2746 TAILQ_INIT(&(cryptodev->link_intr_cbs));
2748 /* if sec device version is not configured */
2749 if (!rta_get_sec_era()) {
2750 const struct device_node *caam_node;
2752 for_each_compatible_node(caam_node, NULL, "fsl,sec-v4.0") {
2753 const uint32_t *prop = of_get_property(caam_node,
2758 INTL_SEC_ERA(rte_cpu_to_be_32(*prop)));
2764 /* Invoke PMD device initialization function */
2765 retval = dpaa_sec_dev_init(cryptodev);
2769 /* In case of error, cleanup is done */
2770 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
2771 rte_free(cryptodev->data->dev_private);
2773 rte_cryptodev_pmd_release_device(cryptodev);
2779 cryptodev_dpaa_sec_remove(struct rte_dpaa_device *dpaa_dev)
2781 struct rte_cryptodev *cryptodev;
2784 cryptodev = dpaa_dev->crypto_dev;
2785 if (cryptodev == NULL)
2788 ret = dpaa_sec_uninit(cryptodev);
2792 return rte_cryptodev_pmd_destroy(cryptodev);
2795 static struct rte_dpaa_driver rte_dpaa_sec_driver = {
2796 .drv_type = FSL_DPAA_CRYPTO,
2798 .name = "DPAA SEC PMD"
2800 .probe = cryptodev_dpaa_sec_probe,
2801 .remove = cryptodev_dpaa_sec_remove,
2804 static struct cryptodev_driver dpaa_sec_crypto_drv;
2806 RTE_PMD_REGISTER_DPAA(CRYPTODEV_NAME_DPAA_SEC_PMD, rte_dpaa_sec_driver);
2807 RTE_PMD_REGISTER_CRYPTO_DRIVER(dpaa_sec_crypto_drv, rte_dpaa_sec_driver.driver,
2808 cryptodev_driver_id);
2810 RTE_INIT(dpaa_sec_init_log)
2812 dpaa_logtype_sec = rte_log_register("pmd.crypto.dpaa");
2813 if (dpaa_logtype_sec >= 0)
2814 rte_log_set_level(dpaa_logtype_sec, RTE_LOG_NOTICE);