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,
478 ses->pdcp.hfn_threshold,
479 &cipherdata, &authdata,
481 else if (ses->dir == DIR_DEC)
482 shared_desc_len = cnstr_shdsc_pdcp_c_plane_decap(
483 cdb->sh_desc, 1, swap,
487 ses->pdcp.hfn_threshold,
488 &cipherdata, &authdata,
491 cdb->sh_desc[0] = cipherdata.keylen;
492 err = rta_inline_query(IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN,
494 (unsigned int *)cdb->sh_desc,
495 &cdb->sh_desc[2], 1);
498 DPAA_SEC_ERR("Crypto: Incorrect key lengths");
501 if (!(cdb->sh_desc[2] & 1) && cipherdata.keylen) {
502 cipherdata.key = (size_t)dpaa_mem_vtop(
503 (void *)(size_t)cipherdata.key);
504 cipherdata.key_type = RTA_DATA_PTR;
510 if (ses->dir == DIR_ENC)
511 shared_desc_len = cnstr_shdsc_pdcp_u_plane_encap(
512 cdb->sh_desc, 1, swap,
517 ses->pdcp.hfn_threshold,
519 else if (ses->dir == DIR_DEC)
520 shared_desc_len = cnstr_shdsc_pdcp_u_plane_decap(
521 cdb->sh_desc, 1, swap,
526 ses->pdcp.hfn_threshold,
530 return shared_desc_len;
533 /* prepare ipsec proto command block of the session */
535 dpaa_sec_prep_ipsec_cdb(dpaa_sec_session *ses)
537 struct alginfo cipherdata = {0}, authdata = {0};
538 struct sec_cdb *cdb = &ses->cdb;
539 int32_t shared_desc_len = 0;
541 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
547 caam_cipher_alg(ses, &cipherdata);
548 if (cipherdata.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
549 DPAA_SEC_ERR("not supported cipher alg");
553 cipherdata.key = (size_t)ses->cipher_key.data;
554 cipherdata.keylen = ses->cipher_key.length;
555 cipherdata.key_enc_flags = 0;
556 cipherdata.key_type = RTA_DATA_IMM;
558 caam_auth_alg(ses, &authdata);
559 if (authdata.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
560 DPAA_SEC_ERR("not supported auth alg");
564 authdata.key = (size_t)ses->auth_key.data;
565 authdata.keylen = ses->auth_key.length;
566 authdata.key_enc_flags = 0;
567 authdata.key_type = RTA_DATA_IMM;
569 cdb->sh_desc[0] = cipherdata.keylen;
570 cdb->sh_desc[1] = authdata.keylen;
571 err = rta_inline_query(IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN,
573 (unsigned int *)cdb->sh_desc,
574 &cdb->sh_desc[2], 2);
577 DPAA_SEC_ERR("Crypto: Incorrect key lengths");
580 if (cdb->sh_desc[2] & 1)
581 cipherdata.key_type = RTA_DATA_IMM;
583 cipherdata.key = (size_t)dpaa_mem_vtop(
584 (void *)(size_t)cipherdata.key);
585 cipherdata.key_type = RTA_DATA_PTR;
587 if (cdb->sh_desc[2] & (1<<1))
588 authdata.key_type = RTA_DATA_IMM;
590 authdata.key = (size_t)dpaa_mem_vtop(
591 (void *)(size_t)authdata.key);
592 authdata.key_type = RTA_DATA_PTR;
598 if (ses->dir == DIR_ENC) {
599 shared_desc_len = cnstr_shdsc_ipsec_new_encap(
601 true, swap, SHR_SERIAL,
603 (uint8_t *)&ses->ip4_hdr,
604 &cipherdata, &authdata);
605 } else if (ses->dir == DIR_DEC) {
606 shared_desc_len = cnstr_shdsc_ipsec_new_decap(
608 true, swap, SHR_SERIAL,
610 &cipherdata, &authdata);
612 return shared_desc_len;
615 /* prepare command block of the session */
617 dpaa_sec_prep_cdb(dpaa_sec_session *ses)
619 struct alginfo alginfo_c = {0}, alginfo_a = {0}, alginfo = {0};
620 int32_t shared_desc_len = 0;
621 struct sec_cdb *cdb = &ses->cdb;
623 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
629 memset(cdb, 0, sizeof(struct sec_cdb));
631 if (is_proto_ipsec(ses)) {
632 shared_desc_len = dpaa_sec_prep_ipsec_cdb(ses);
633 } else if (is_proto_pdcp(ses)) {
634 shared_desc_len = dpaa_sec_prep_pdcp_cdb(ses);
635 } else if (is_cipher_only(ses)) {
636 caam_cipher_alg(ses, &alginfo_c);
637 if (alginfo_c.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
638 DPAA_SEC_ERR("not supported cipher alg");
642 alginfo_c.key = (size_t)ses->cipher_key.data;
643 alginfo_c.keylen = ses->cipher_key.length;
644 alginfo_c.key_enc_flags = 0;
645 alginfo_c.key_type = RTA_DATA_IMM;
647 shared_desc_len = cnstr_shdsc_blkcipher(
649 swap, SHR_NEVER, &alginfo_c,
653 } else if (is_auth_only(ses)) {
654 caam_auth_alg(ses, &alginfo_a);
655 if (alginfo_a.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
656 DPAA_SEC_ERR("not supported auth alg");
660 alginfo_a.key = (size_t)ses->auth_key.data;
661 alginfo_a.keylen = ses->auth_key.length;
662 alginfo_a.key_enc_flags = 0;
663 alginfo_a.key_type = RTA_DATA_IMM;
665 shared_desc_len = cnstr_shdsc_hmac(cdb->sh_desc, true,
666 swap, SHR_NEVER, &alginfo_a,
669 } else if (is_aead(ses)) {
670 caam_aead_alg(ses, &alginfo);
671 if (alginfo.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
672 DPAA_SEC_ERR("not supported aead alg");
675 alginfo.key = (size_t)ses->aead_key.data;
676 alginfo.keylen = ses->aead_key.length;
677 alginfo.key_enc_flags = 0;
678 alginfo.key_type = RTA_DATA_IMM;
680 if (ses->dir == DIR_ENC)
681 shared_desc_len = cnstr_shdsc_gcm_encap(
682 cdb->sh_desc, true, swap, SHR_NEVER,
687 shared_desc_len = cnstr_shdsc_gcm_decap(
688 cdb->sh_desc, true, swap, SHR_NEVER,
693 caam_cipher_alg(ses, &alginfo_c);
694 if (alginfo_c.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
695 DPAA_SEC_ERR("not supported cipher alg");
699 alginfo_c.key = (size_t)ses->cipher_key.data;
700 alginfo_c.keylen = ses->cipher_key.length;
701 alginfo_c.key_enc_flags = 0;
702 alginfo_c.key_type = RTA_DATA_IMM;
704 caam_auth_alg(ses, &alginfo_a);
705 if (alginfo_a.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
706 DPAA_SEC_ERR("not supported auth alg");
710 alginfo_a.key = (size_t)ses->auth_key.data;
711 alginfo_a.keylen = ses->auth_key.length;
712 alginfo_a.key_enc_flags = 0;
713 alginfo_a.key_type = RTA_DATA_IMM;
715 cdb->sh_desc[0] = alginfo_c.keylen;
716 cdb->sh_desc[1] = alginfo_a.keylen;
717 err = rta_inline_query(IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN,
719 (unsigned int *)cdb->sh_desc,
720 &cdb->sh_desc[2], 2);
723 DPAA_SEC_ERR("Crypto: Incorrect key lengths");
726 if (cdb->sh_desc[2] & 1)
727 alginfo_c.key_type = RTA_DATA_IMM;
729 alginfo_c.key = (size_t)dpaa_mem_vtop(
730 (void *)(size_t)alginfo_c.key);
731 alginfo_c.key_type = RTA_DATA_PTR;
733 if (cdb->sh_desc[2] & (1<<1))
734 alginfo_a.key_type = RTA_DATA_IMM;
736 alginfo_a.key = (size_t)dpaa_mem_vtop(
737 (void *)(size_t)alginfo_a.key);
738 alginfo_a.key_type = RTA_DATA_PTR;
743 /* Auth_only_len is set as 0 here and it will be
744 * overwritten in fd for each packet.
746 shared_desc_len = cnstr_shdsc_authenc(cdb->sh_desc,
747 true, swap, SHR_SERIAL, &alginfo_c, &alginfo_a,
749 ses->digest_length, ses->dir);
752 if (shared_desc_len < 0) {
753 DPAA_SEC_ERR("error in preparing command block");
754 return shared_desc_len;
757 cdb->sh_hdr.hi.field.idlen = shared_desc_len;
758 cdb->sh_hdr.hi.word = rte_cpu_to_be_32(cdb->sh_hdr.hi.word);
759 cdb->sh_hdr.lo.word = rte_cpu_to_be_32(cdb->sh_hdr.lo.word);
764 /* qp is lockless, should be accessed by only one thread */
766 dpaa_sec_deq(struct dpaa_sec_qp *qp, struct rte_crypto_op **ops, int nb_ops)
769 unsigned int pkts = 0;
770 int num_rx_bufs, ret;
771 struct qm_dqrr_entry *dq;
772 uint32_t vdqcr_flags = 0;
776 * Until request for four buffers, we provide exact number of buffers.
777 * Otherwise we do not set the QM_VDQCR_EXACT flag.
778 * Not setting QM_VDQCR_EXACT flag can provide two more buffers than
779 * requested, so we request two less in this case.
782 vdqcr_flags = QM_VDQCR_EXACT;
783 num_rx_bufs = nb_ops;
785 num_rx_bufs = nb_ops > DPAA_MAX_DEQUEUE_NUM_FRAMES ?
786 (DPAA_MAX_DEQUEUE_NUM_FRAMES - 2) : (nb_ops - 2);
788 ret = qman_set_vdq(fq, num_rx_bufs, vdqcr_flags);
793 const struct qm_fd *fd;
794 struct dpaa_sec_job *job;
795 struct dpaa_sec_op_ctx *ctx;
796 struct rte_crypto_op *op;
798 dq = qman_dequeue(fq);
803 /* sg is embedded in an op ctx,
804 * sg[0] is for output
807 job = dpaa_mem_ptov(qm_fd_addr_get64(fd));
809 ctx = container_of(job, struct dpaa_sec_op_ctx, job);
810 ctx->fd_status = fd->status;
812 if (op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
813 struct qm_sg_entry *sg_out;
816 sg_out = &job->sg[0];
817 hw_sg_to_cpu(sg_out);
818 len = sg_out->length;
819 op->sym->m_src->pkt_len = len;
820 op->sym->m_src->data_len = len;
822 if (!ctx->fd_status) {
823 op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
825 DPAA_SEC_DP_WARN("SEC return err:0x%x", ctx->fd_status);
826 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
830 /* report op status to sym->op and then free the ctx memeory */
831 rte_mempool_put(ctx->ctx_pool, (void *)ctx);
833 qman_dqrr_consume(fq, dq);
834 } while (fq->flags & QMAN_FQ_STATE_VDQCR);
839 static inline struct dpaa_sec_job *
840 build_auth_only_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
842 struct rte_crypto_sym_op *sym = op->sym;
843 struct rte_mbuf *mbuf = sym->m_src;
844 struct dpaa_sec_job *cf;
845 struct dpaa_sec_op_ctx *ctx;
846 struct qm_sg_entry *sg, *out_sg, *in_sg;
847 phys_addr_t start_addr;
848 uint8_t *old_digest, extra_segs;
855 if ((mbuf->nb_segs + extra_segs) > MAX_SG_ENTRIES) {
856 DPAA_SEC_DP_ERR("Auth: Max sec segs supported is %d",
860 ctx = dpaa_sec_alloc_ctx(ses);
866 old_digest = ctx->digest;
870 qm_sg_entry_set64(out_sg, sym->auth.digest.phys_addr);
871 out_sg->length = ses->digest_length;
872 cpu_to_hw_sg(out_sg);
876 /* need to extend the input to a compound frame */
877 in_sg->extension = 1;
879 in_sg->length = sym->auth.data.length;
880 qm_sg_entry_set64(in_sg, dpaa_mem_vtop(&cf->sg[2]));
884 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
885 sg->length = mbuf->data_len - sym->auth.data.offset;
886 sg->offset = sym->auth.data.offset;
888 /* Successive segs */
893 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
894 sg->length = mbuf->data_len;
898 if (is_decode(ses)) {
899 /* Digest verification case */
902 rte_memcpy(old_digest, sym->auth.digest.data,
904 start_addr = dpaa_mem_vtop(old_digest);
905 qm_sg_entry_set64(sg, start_addr);
906 sg->length = ses->digest_length;
907 in_sg->length += ses->digest_length;
909 /* Digest calculation case */
910 sg->length -= ses->digest_length;
921 * |<----data_len------->|
922 * |ip_header|ah_header|icv|payload|
927 static inline struct dpaa_sec_job *
928 build_auth_only(struct rte_crypto_op *op, dpaa_sec_session *ses)
930 struct rte_crypto_sym_op *sym = op->sym;
931 struct rte_mbuf *mbuf = sym->m_src;
932 struct dpaa_sec_job *cf;
933 struct dpaa_sec_op_ctx *ctx;
934 struct qm_sg_entry *sg;
935 rte_iova_t start_addr;
938 ctx = dpaa_sec_alloc_ctx(ses);
944 old_digest = ctx->digest;
946 start_addr = rte_pktmbuf_iova(mbuf);
949 qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
950 sg->length = ses->digest_length;
955 if (is_decode(ses)) {
956 /* need to extend the input to a compound frame */
958 qm_sg_entry_set64(sg, dpaa_mem_vtop(&cf->sg[2]));
959 sg->length = sym->auth.data.length + ses->digest_length;
964 /* hash result or digest, save digest first */
965 rte_memcpy(old_digest, sym->auth.digest.data,
967 qm_sg_entry_set64(sg, start_addr + sym->auth.data.offset);
968 sg->length = sym->auth.data.length;
971 /* let's check digest by hw */
972 start_addr = dpaa_mem_vtop(old_digest);
974 qm_sg_entry_set64(sg, start_addr);
975 sg->length = ses->digest_length;
979 qm_sg_entry_set64(sg, start_addr + sym->auth.data.offset);
980 sg->length = sym->auth.data.length;
988 static inline struct dpaa_sec_job *
989 build_cipher_only_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
991 struct rte_crypto_sym_op *sym = op->sym;
992 struct dpaa_sec_job *cf;
993 struct dpaa_sec_op_ctx *ctx;
994 struct qm_sg_entry *sg, *out_sg, *in_sg;
995 struct rte_mbuf *mbuf;
997 uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1002 req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 3;
1005 req_segs = mbuf->nb_segs * 2 + 3;
1008 if (req_segs > MAX_SG_ENTRIES) {
1009 DPAA_SEC_DP_ERR("Cipher: Max sec segs supported is %d",
1014 ctx = dpaa_sec_alloc_ctx(ses);
1022 out_sg = &cf->sg[0];
1023 out_sg->extension = 1;
1024 out_sg->length = sym->cipher.data.length;
1025 qm_sg_entry_set64(out_sg, dpaa_mem_vtop(&cf->sg[2]));
1026 cpu_to_hw_sg(out_sg);
1030 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1031 sg->length = mbuf->data_len - sym->cipher.data.offset;
1032 sg->offset = sym->cipher.data.offset;
1034 /* Successive segs */
1039 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1040 sg->length = mbuf->data_len;
1049 in_sg->extension = 1;
1051 in_sg->length = sym->cipher.data.length + ses->iv.length;
1054 qm_sg_entry_set64(in_sg, dpaa_mem_vtop(sg));
1055 cpu_to_hw_sg(in_sg);
1058 qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1059 sg->length = ses->iv.length;
1064 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1065 sg->length = mbuf->data_len - sym->cipher.data.offset;
1066 sg->offset = sym->cipher.data.offset;
1068 /* Successive segs */
1073 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1074 sg->length = mbuf->data_len;
1083 static inline struct dpaa_sec_job *
1084 build_cipher_only(struct rte_crypto_op *op, dpaa_sec_session *ses)
1086 struct rte_crypto_sym_op *sym = op->sym;
1087 struct dpaa_sec_job *cf;
1088 struct dpaa_sec_op_ctx *ctx;
1089 struct qm_sg_entry *sg;
1090 rte_iova_t src_start_addr, dst_start_addr;
1091 uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1094 ctx = dpaa_sec_alloc_ctx(ses);
1101 src_start_addr = rte_pktmbuf_iova(sym->m_src);
1104 dst_start_addr = rte_pktmbuf_iova(sym->m_dst);
1106 dst_start_addr = src_start_addr;
1110 qm_sg_entry_set64(sg, dst_start_addr + sym->cipher.data.offset);
1111 sg->length = sym->cipher.data.length + ses->iv.length;
1117 /* need to extend the input to a compound frame */
1120 sg->length = sym->cipher.data.length + ses->iv.length;
1121 qm_sg_entry_set64(sg, dpaa_mem_vtop(&cf->sg[2]));
1125 qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1126 sg->length = ses->iv.length;
1130 qm_sg_entry_set64(sg, src_start_addr + sym->cipher.data.offset);
1131 sg->length = sym->cipher.data.length;
1138 static inline struct dpaa_sec_job *
1139 build_cipher_auth_gcm_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
1141 struct rte_crypto_sym_op *sym = op->sym;
1142 struct dpaa_sec_job *cf;
1143 struct dpaa_sec_op_ctx *ctx;
1144 struct qm_sg_entry *sg, *out_sg, *in_sg;
1145 struct rte_mbuf *mbuf;
1147 uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1152 req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 4;
1155 req_segs = mbuf->nb_segs * 2 + 4;
1158 if (ses->auth_only_len)
1161 if (req_segs > MAX_SG_ENTRIES) {
1162 DPAA_SEC_DP_ERR("AEAD: Max sec segs supported is %d",
1167 ctx = dpaa_sec_alloc_ctx(ses);
1174 rte_prefetch0(cf->sg);
1177 out_sg = &cf->sg[0];
1178 out_sg->extension = 1;
1180 out_sg->length = sym->aead.data.length + ses->auth_only_len
1181 + ses->digest_length;
1183 out_sg->length = sym->aead.data.length + ses->auth_only_len;
1185 /* output sg entries */
1187 qm_sg_entry_set64(out_sg, dpaa_mem_vtop(sg));
1188 cpu_to_hw_sg(out_sg);
1191 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1192 sg->length = mbuf->data_len - sym->aead.data.offset +
1194 sg->offset = sym->aead.data.offset - ses->auth_only_len;
1196 /* Successive segs */
1201 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1202 sg->length = mbuf->data_len;
1205 sg->length -= ses->digest_length;
1207 if (is_encode(ses)) {
1209 /* set auth output */
1211 qm_sg_entry_set64(sg, sym->aead.digest.phys_addr);
1212 sg->length = ses->digest_length;
1220 in_sg->extension = 1;
1223 in_sg->length = ses->iv.length + sym->aead.data.length
1224 + ses->auth_only_len;
1226 in_sg->length = ses->iv.length + sym->aead.data.length
1227 + ses->auth_only_len + ses->digest_length;
1229 /* input sg entries */
1231 qm_sg_entry_set64(in_sg, dpaa_mem_vtop(sg));
1232 cpu_to_hw_sg(in_sg);
1235 qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1236 sg->length = ses->iv.length;
1239 /* 2nd seg auth only */
1240 if (ses->auth_only_len) {
1242 qm_sg_entry_set64(sg, dpaa_mem_vtop(sym->aead.aad.data));
1243 sg->length = ses->auth_only_len;
1249 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1250 sg->length = mbuf->data_len - sym->aead.data.offset;
1251 sg->offset = sym->aead.data.offset;
1253 /* Successive segs */
1258 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1259 sg->length = mbuf->data_len;
1263 if (is_decode(ses)) {
1266 memcpy(ctx->digest, sym->aead.digest.data,
1267 ses->digest_length);
1268 qm_sg_entry_set64(sg, dpaa_mem_vtop(ctx->digest));
1269 sg->length = ses->digest_length;
1277 static inline struct dpaa_sec_job *
1278 build_cipher_auth_gcm(struct rte_crypto_op *op, dpaa_sec_session *ses)
1280 struct rte_crypto_sym_op *sym = op->sym;
1281 struct dpaa_sec_job *cf;
1282 struct dpaa_sec_op_ctx *ctx;
1283 struct qm_sg_entry *sg;
1284 uint32_t length = 0;
1285 rte_iova_t src_start_addr, dst_start_addr;
1286 uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1289 src_start_addr = sym->m_src->buf_iova + sym->m_src->data_off;
1292 dst_start_addr = sym->m_dst->buf_iova + sym->m_dst->data_off;
1294 dst_start_addr = src_start_addr;
1296 ctx = dpaa_sec_alloc_ctx(ses);
1304 rte_prefetch0(cf->sg);
1306 qm_sg_entry_set64(&cf->sg[1], dpaa_mem_vtop(sg));
1307 if (is_encode(ses)) {
1308 qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1309 sg->length = ses->iv.length;
1310 length += sg->length;
1314 if (ses->auth_only_len) {
1315 qm_sg_entry_set64(sg,
1316 dpaa_mem_vtop(sym->aead.aad.data));
1317 sg->length = ses->auth_only_len;
1318 length += sg->length;
1322 qm_sg_entry_set64(sg, src_start_addr + sym->aead.data.offset);
1323 sg->length = sym->aead.data.length;
1324 length += sg->length;
1328 qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1329 sg->length = ses->iv.length;
1330 length += sg->length;
1334 if (ses->auth_only_len) {
1335 qm_sg_entry_set64(sg,
1336 dpaa_mem_vtop(sym->aead.aad.data));
1337 sg->length = ses->auth_only_len;
1338 length += sg->length;
1342 qm_sg_entry_set64(sg, src_start_addr + sym->aead.data.offset);
1343 sg->length = sym->aead.data.length;
1344 length += sg->length;
1347 memcpy(ctx->digest, sym->aead.digest.data,
1348 ses->digest_length);
1351 qm_sg_entry_set64(sg, dpaa_mem_vtop(ctx->digest));
1352 sg->length = ses->digest_length;
1353 length += sg->length;
1357 /* input compound frame */
1358 cf->sg[1].length = length;
1359 cf->sg[1].extension = 1;
1360 cf->sg[1].final = 1;
1361 cpu_to_hw_sg(&cf->sg[1]);
1365 qm_sg_entry_set64(&cf->sg[0], dpaa_mem_vtop(sg));
1366 qm_sg_entry_set64(sg,
1367 dst_start_addr + sym->aead.data.offset - ses->auth_only_len);
1368 sg->length = sym->aead.data.length + ses->auth_only_len;
1369 length = sg->length;
1370 if (is_encode(ses)) {
1372 /* set auth output */
1374 qm_sg_entry_set64(sg, sym->aead.digest.phys_addr);
1375 sg->length = ses->digest_length;
1376 length += sg->length;
1381 /* output compound frame */
1382 cf->sg[0].length = length;
1383 cf->sg[0].extension = 1;
1384 cpu_to_hw_sg(&cf->sg[0]);
1389 static inline struct dpaa_sec_job *
1390 build_cipher_auth_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
1392 struct rte_crypto_sym_op *sym = op->sym;
1393 struct dpaa_sec_job *cf;
1394 struct dpaa_sec_op_ctx *ctx;
1395 struct qm_sg_entry *sg, *out_sg, *in_sg;
1396 struct rte_mbuf *mbuf;
1398 uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1403 req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 4;
1406 req_segs = mbuf->nb_segs * 2 + 4;
1409 if (req_segs > MAX_SG_ENTRIES) {
1410 DPAA_SEC_DP_ERR("Cipher-Auth: Max sec segs supported is %d",
1415 ctx = dpaa_sec_alloc_ctx(ses);
1422 rte_prefetch0(cf->sg);
1425 out_sg = &cf->sg[0];
1426 out_sg->extension = 1;
1428 out_sg->length = sym->auth.data.length + ses->digest_length;
1430 out_sg->length = sym->auth.data.length;
1432 /* output sg entries */
1434 qm_sg_entry_set64(out_sg, dpaa_mem_vtop(sg));
1435 cpu_to_hw_sg(out_sg);
1438 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1439 sg->length = mbuf->data_len - sym->auth.data.offset;
1440 sg->offset = sym->auth.data.offset;
1442 /* Successive segs */
1447 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1448 sg->length = mbuf->data_len;
1451 sg->length -= ses->digest_length;
1453 if (is_encode(ses)) {
1455 /* set auth output */
1457 qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
1458 sg->length = ses->digest_length;
1466 in_sg->extension = 1;
1469 in_sg->length = ses->iv.length + sym->auth.data.length;
1471 in_sg->length = ses->iv.length + sym->auth.data.length
1472 + ses->digest_length;
1474 /* input sg entries */
1476 qm_sg_entry_set64(in_sg, dpaa_mem_vtop(sg));
1477 cpu_to_hw_sg(in_sg);
1480 qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1481 sg->length = ses->iv.length;
1486 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1487 sg->length = mbuf->data_len - sym->auth.data.offset;
1488 sg->offset = sym->auth.data.offset;
1490 /* Successive segs */
1495 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1496 sg->length = mbuf->data_len;
1500 sg->length -= ses->digest_length;
1501 if (is_decode(ses)) {
1504 memcpy(ctx->digest, sym->auth.digest.data,
1505 ses->digest_length);
1506 qm_sg_entry_set64(sg, dpaa_mem_vtop(ctx->digest));
1507 sg->length = ses->digest_length;
1515 static inline struct dpaa_sec_job *
1516 build_cipher_auth(struct rte_crypto_op *op, dpaa_sec_session *ses)
1518 struct rte_crypto_sym_op *sym = op->sym;
1519 struct dpaa_sec_job *cf;
1520 struct dpaa_sec_op_ctx *ctx;
1521 struct qm_sg_entry *sg;
1522 rte_iova_t src_start_addr, dst_start_addr;
1523 uint32_t length = 0;
1524 uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1527 src_start_addr = sym->m_src->buf_iova + sym->m_src->data_off;
1529 dst_start_addr = sym->m_dst->buf_iova + sym->m_dst->data_off;
1531 dst_start_addr = src_start_addr;
1533 ctx = dpaa_sec_alloc_ctx(ses);
1541 rte_prefetch0(cf->sg);
1543 qm_sg_entry_set64(&cf->sg[1], dpaa_mem_vtop(sg));
1544 if (is_encode(ses)) {
1545 qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1546 sg->length = ses->iv.length;
1547 length += sg->length;
1551 qm_sg_entry_set64(sg, src_start_addr + sym->auth.data.offset);
1552 sg->length = sym->auth.data.length;
1553 length += sg->length;
1557 qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1558 sg->length = ses->iv.length;
1559 length += sg->length;
1564 qm_sg_entry_set64(sg, src_start_addr + sym->auth.data.offset);
1565 sg->length = sym->auth.data.length;
1566 length += sg->length;
1569 memcpy(ctx->digest, sym->auth.digest.data,
1570 ses->digest_length);
1573 qm_sg_entry_set64(sg, dpaa_mem_vtop(ctx->digest));
1574 sg->length = ses->digest_length;
1575 length += sg->length;
1579 /* input compound frame */
1580 cf->sg[1].length = length;
1581 cf->sg[1].extension = 1;
1582 cf->sg[1].final = 1;
1583 cpu_to_hw_sg(&cf->sg[1]);
1587 qm_sg_entry_set64(&cf->sg[0], dpaa_mem_vtop(sg));
1588 qm_sg_entry_set64(sg, dst_start_addr + sym->cipher.data.offset);
1589 sg->length = sym->cipher.data.length;
1590 length = sg->length;
1591 if (is_encode(ses)) {
1593 /* set auth output */
1595 qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
1596 sg->length = ses->digest_length;
1597 length += sg->length;
1602 /* output compound frame */
1603 cf->sg[0].length = length;
1604 cf->sg[0].extension = 1;
1605 cpu_to_hw_sg(&cf->sg[0]);
1610 static inline struct dpaa_sec_job *
1611 build_proto(struct rte_crypto_op *op, dpaa_sec_session *ses)
1613 struct rte_crypto_sym_op *sym = op->sym;
1614 struct dpaa_sec_job *cf;
1615 struct dpaa_sec_op_ctx *ctx;
1616 struct qm_sg_entry *sg;
1617 phys_addr_t src_start_addr, dst_start_addr;
1619 ctx = dpaa_sec_alloc_ctx(ses);
1625 src_start_addr = rte_pktmbuf_mtophys(sym->m_src);
1628 dst_start_addr = rte_pktmbuf_mtophys(sym->m_dst);
1630 dst_start_addr = src_start_addr;
1634 qm_sg_entry_set64(sg, src_start_addr);
1635 sg->length = sym->m_src->pkt_len;
1639 sym->m_src->packet_type &= ~RTE_PTYPE_L4_MASK;
1642 qm_sg_entry_set64(sg, dst_start_addr);
1643 sg->length = sym->m_src->buf_len - sym->m_src->data_off;
1650 dpaa_sec_enqueue_burst(void *qp, struct rte_crypto_op **ops,
1653 /* Function to transmit the frames to given device and queuepair */
1655 struct dpaa_sec_qp *dpaa_qp = (struct dpaa_sec_qp *)qp;
1656 uint16_t num_tx = 0;
1657 struct qm_fd fds[DPAA_SEC_BURST], *fd;
1658 uint32_t frames_to_send;
1659 struct rte_crypto_op *op;
1660 struct dpaa_sec_job *cf;
1661 dpaa_sec_session *ses;
1662 uint32_t auth_only_len;
1663 struct qman_fq *inq[DPAA_SEC_BURST];
1666 frames_to_send = (nb_ops > DPAA_SEC_BURST) ?
1667 DPAA_SEC_BURST : nb_ops;
1668 for (loop = 0; loop < frames_to_send; loop++) {
1670 switch (op->sess_type) {
1671 case RTE_CRYPTO_OP_WITH_SESSION:
1672 ses = (dpaa_sec_session *)
1673 get_sym_session_private_data(
1675 cryptodev_driver_id);
1677 case RTE_CRYPTO_OP_SECURITY_SESSION:
1678 ses = (dpaa_sec_session *)
1679 get_sec_session_private_data(
1680 op->sym->sec_session);
1684 "sessionless crypto op not supported");
1685 frames_to_send = loop;
1689 if (unlikely(!ses->qp[rte_lcore_id() % MAX_DPAA_CORES])) {
1690 if (dpaa_sec_attach_sess_q(qp, ses)) {
1691 frames_to_send = loop;
1695 } else if (unlikely(ses->qp[rte_lcore_id() %
1696 MAX_DPAA_CORES] != qp)) {
1697 DPAA_SEC_DP_ERR("Old:sess->qp = %p"
1699 ses->qp[rte_lcore_id() %
1700 MAX_DPAA_CORES], qp);
1701 frames_to_send = loop;
1706 auth_only_len = op->sym->auth.data.length -
1707 op->sym->cipher.data.length;
1708 if (rte_pktmbuf_is_contiguous(op->sym->m_src)) {
1709 if (is_proto_ipsec(ses)) {
1710 cf = build_proto(op, ses);
1711 } else if (is_proto_pdcp(ses)) {
1712 cf = build_proto(op, ses);
1713 } else if (is_auth_only(ses)) {
1714 cf = build_auth_only(op, ses);
1715 } else if (is_cipher_only(ses)) {
1716 cf = build_cipher_only(op, ses);
1717 } else if (is_aead(ses)) {
1718 cf = build_cipher_auth_gcm(op, ses);
1719 auth_only_len = ses->auth_only_len;
1720 } else if (is_auth_cipher(ses)) {
1721 cf = build_cipher_auth(op, ses);
1723 DPAA_SEC_DP_ERR("not supported ops");
1724 frames_to_send = loop;
1729 if (is_auth_only(ses)) {
1730 cf = build_auth_only_sg(op, ses);
1731 } else if (is_cipher_only(ses)) {
1732 cf = build_cipher_only_sg(op, ses);
1733 } else if (is_aead(ses)) {
1734 cf = build_cipher_auth_gcm_sg(op, ses);
1735 auth_only_len = ses->auth_only_len;
1736 } else if (is_auth_cipher(ses)) {
1737 cf = build_cipher_auth_sg(op, ses);
1739 DPAA_SEC_DP_ERR("not supported ops");
1740 frames_to_send = loop;
1745 if (unlikely(!cf)) {
1746 frames_to_send = loop;
1752 inq[loop] = ses->inq[rte_lcore_id() % MAX_DPAA_CORES];
1753 fd->opaque_addr = 0;
1755 qm_fd_addr_set64(fd, dpaa_mem_vtop(cf->sg));
1756 fd->_format1 = qm_fd_compound;
1757 fd->length29 = 2 * sizeof(struct qm_sg_entry);
1758 /* Auth_only_len is set as 0 in descriptor and it is
1759 * overwritten here in the fd.cmd which will update
1763 fd->cmd = 0x80000000 | auth_only_len;
1768 while (loop < frames_to_send) {
1769 loop += qman_enqueue_multi_fq(&inq[loop], &fds[loop],
1770 frames_to_send - loop);
1772 nb_ops -= frames_to_send;
1773 num_tx += frames_to_send;
1776 dpaa_qp->tx_pkts += num_tx;
1777 dpaa_qp->tx_errs += nb_ops - num_tx;
1783 dpaa_sec_dequeue_burst(void *qp, struct rte_crypto_op **ops,
1787 struct dpaa_sec_qp *dpaa_qp = (struct dpaa_sec_qp *)qp;
1789 num_rx = dpaa_sec_deq(dpaa_qp, ops, nb_ops);
1791 dpaa_qp->rx_pkts += num_rx;
1792 dpaa_qp->rx_errs += nb_ops - num_rx;
1794 DPAA_SEC_DP_DEBUG("SEC Received %d Packets\n", num_rx);
1799 /** Release queue pair */
1801 dpaa_sec_queue_pair_release(struct rte_cryptodev *dev,
1804 struct dpaa_sec_dev_private *internals;
1805 struct dpaa_sec_qp *qp = NULL;
1807 PMD_INIT_FUNC_TRACE();
1809 DPAA_SEC_DEBUG("dev =%p, queue =%d", dev, qp_id);
1811 internals = dev->data->dev_private;
1812 if (qp_id >= internals->max_nb_queue_pairs) {
1813 DPAA_SEC_ERR("Max supported qpid %d",
1814 internals->max_nb_queue_pairs);
1818 qp = &internals->qps[qp_id];
1819 qp->internals = NULL;
1820 dev->data->queue_pairs[qp_id] = NULL;
1825 /** Setup a queue pair */
1827 dpaa_sec_queue_pair_setup(struct rte_cryptodev *dev, uint16_t qp_id,
1828 __rte_unused const struct rte_cryptodev_qp_conf *qp_conf,
1829 __rte_unused int socket_id)
1831 struct dpaa_sec_dev_private *internals;
1832 struct dpaa_sec_qp *qp = NULL;
1834 DPAA_SEC_DEBUG("dev =%p, queue =%d, conf =%p", dev, qp_id, qp_conf);
1836 internals = dev->data->dev_private;
1837 if (qp_id >= internals->max_nb_queue_pairs) {
1838 DPAA_SEC_ERR("Max supported qpid %d",
1839 internals->max_nb_queue_pairs);
1843 qp = &internals->qps[qp_id];
1844 qp->internals = internals;
1845 dev->data->queue_pairs[qp_id] = qp;
1850 /** Return the number of allocated queue pairs */
1852 dpaa_sec_queue_pair_count(struct rte_cryptodev *dev)
1854 PMD_INIT_FUNC_TRACE();
1856 return dev->data->nb_queue_pairs;
1859 /** Returns the size of session structure */
1861 dpaa_sec_sym_session_get_size(struct rte_cryptodev *dev __rte_unused)
1863 PMD_INIT_FUNC_TRACE();
1865 return sizeof(dpaa_sec_session);
1869 dpaa_sec_cipher_init(struct rte_cryptodev *dev __rte_unused,
1870 struct rte_crypto_sym_xform *xform,
1871 dpaa_sec_session *session)
1873 session->cipher_alg = xform->cipher.algo;
1874 session->iv.length = xform->cipher.iv.length;
1875 session->iv.offset = xform->cipher.iv.offset;
1876 session->cipher_key.data = rte_zmalloc(NULL, xform->cipher.key.length,
1877 RTE_CACHE_LINE_SIZE);
1878 if (session->cipher_key.data == NULL && xform->cipher.key.length > 0) {
1879 DPAA_SEC_ERR("No Memory for cipher key");
1882 session->cipher_key.length = xform->cipher.key.length;
1884 memcpy(session->cipher_key.data, xform->cipher.key.data,
1885 xform->cipher.key.length);
1886 session->dir = (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
1893 dpaa_sec_auth_init(struct rte_cryptodev *dev __rte_unused,
1894 struct rte_crypto_sym_xform *xform,
1895 dpaa_sec_session *session)
1897 session->auth_alg = xform->auth.algo;
1898 session->auth_key.data = rte_zmalloc(NULL, xform->auth.key.length,
1899 RTE_CACHE_LINE_SIZE);
1900 if (session->auth_key.data == NULL && xform->auth.key.length > 0) {
1901 DPAA_SEC_ERR("No Memory for auth key");
1904 session->auth_key.length = xform->auth.key.length;
1905 session->digest_length = xform->auth.digest_length;
1907 memcpy(session->auth_key.data, xform->auth.key.data,
1908 xform->auth.key.length);
1909 session->dir = (xform->auth.op == RTE_CRYPTO_AUTH_OP_GENERATE) ?
1916 dpaa_sec_aead_init(struct rte_cryptodev *dev __rte_unused,
1917 struct rte_crypto_sym_xform *xform,
1918 dpaa_sec_session *session)
1920 session->aead_alg = xform->aead.algo;
1921 session->iv.length = xform->aead.iv.length;
1922 session->iv.offset = xform->aead.iv.offset;
1923 session->auth_only_len = xform->aead.aad_length;
1924 session->aead_key.data = rte_zmalloc(NULL, xform->aead.key.length,
1925 RTE_CACHE_LINE_SIZE);
1926 if (session->aead_key.data == NULL && xform->aead.key.length > 0) {
1927 DPAA_SEC_ERR("No Memory for aead key\n");
1930 session->aead_key.length = xform->aead.key.length;
1931 session->digest_length = xform->aead.digest_length;
1933 memcpy(session->aead_key.data, xform->aead.key.data,
1934 xform->aead.key.length);
1935 session->dir = (xform->aead.op == RTE_CRYPTO_AEAD_OP_ENCRYPT) ?
1941 static struct qman_fq *
1942 dpaa_sec_attach_rxq(struct dpaa_sec_dev_private *qi)
1946 for (i = 0; i < qi->max_nb_sessions * MAX_DPAA_CORES; i++) {
1947 if (qi->inq_attach[i] == 0) {
1948 qi->inq_attach[i] = 1;
1952 DPAA_SEC_WARN("All session in use %u", qi->max_nb_sessions);
1958 dpaa_sec_detach_rxq(struct dpaa_sec_dev_private *qi, struct qman_fq *fq)
1962 for (i = 0; i < qi->max_nb_sessions; i++) {
1963 if (&qi->inq[i] == fq) {
1964 qman_retire_fq(fq, NULL);
1966 qi->inq_attach[i] = 0;
1974 dpaa_sec_attach_sess_q(struct dpaa_sec_qp *qp, dpaa_sec_session *sess)
1978 sess->qp[rte_lcore_id() % MAX_DPAA_CORES] = qp;
1979 ret = dpaa_sec_prep_cdb(sess);
1981 DPAA_SEC_ERR("Unable to prepare sec cdb");
1984 if (unlikely(!RTE_PER_LCORE(dpaa_io))) {
1985 ret = rte_dpaa_portal_init((void *)0);
1987 DPAA_SEC_ERR("Failure in affining portal");
1991 ret = dpaa_sec_init_rx(sess->inq[rte_lcore_id() % MAX_DPAA_CORES],
1992 dpaa_mem_vtop(&sess->cdb),
1993 qman_fq_fqid(&qp->outq));
1995 DPAA_SEC_ERR("Unable to init sec queue");
2001 dpaa_sec_set_session_parameters(struct rte_cryptodev *dev,
2002 struct rte_crypto_sym_xform *xform, void *sess)
2004 struct dpaa_sec_dev_private *internals = dev->data->dev_private;
2005 dpaa_sec_session *session = sess;
2008 PMD_INIT_FUNC_TRACE();
2010 if (unlikely(sess == NULL)) {
2011 DPAA_SEC_ERR("invalid session struct");
2014 memset(session, 0, sizeof(dpaa_sec_session));
2016 /* Default IV length = 0 */
2017 session->iv.length = 0;
2020 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER && xform->next == NULL) {
2021 session->auth_alg = RTE_CRYPTO_AUTH_NULL;
2022 dpaa_sec_cipher_init(dev, xform, session);
2024 /* Authentication Only */
2025 } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
2026 xform->next == NULL) {
2027 session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
2028 dpaa_sec_auth_init(dev, xform, session);
2030 /* Cipher then Authenticate */
2031 } else if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
2032 xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
2033 if (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) {
2034 dpaa_sec_cipher_init(dev, xform, session);
2035 dpaa_sec_auth_init(dev, xform->next, session);
2037 DPAA_SEC_ERR("Not supported: Auth then Cipher");
2041 /* Authenticate then Cipher */
2042 } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
2043 xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
2044 if (xform->next->cipher.op == RTE_CRYPTO_CIPHER_OP_DECRYPT) {
2045 dpaa_sec_auth_init(dev, xform, session);
2046 dpaa_sec_cipher_init(dev, xform->next, session);
2048 DPAA_SEC_ERR("Not supported: Auth then Cipher");
2052 /* AEAD operation for AES-GCM kind of Algorithms */
2053 } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD &&
2054 xform->next == NULL) {
2055 dpaa_sec_aead_init(dev, xform, session);
2058 DPAA_SEC_ERR("Invalid crypto type");
2061 session->ctx_pool = internals->ctx_pool;
2062 rte_spinlock_lock(&internals->lock);
2063 for (i = 0; i < MAX_DPAA_CORES; i++) {
2064 session->inq[i] = dpaa_sec_attach_rxq(internals);
2065 if (session->inq[i] == NULL) {
2066 DPAA_SEC_ERR("unable to attach sec queue");
2067 rte_spinlock_unlock(&internals->lock);
2071 rte_spinlock_unlock(&internals->lock);
2076 rte_free(session->cipher_key.data);
2077 rte_free(session->auth_key.data);
2078 memset(session, 0, sizeof(dpaa_sec_session));
2084 dpaa_sec_sym_session_configure(struct rte_cryptodev *dev,
2085 struct rte_crypto_sym_xform *xform,
2086 struct rte_cryptodev_sym_session *sess,
2087 struct rte_mempool *mempool)
2089 void *sess_private_data;
2092 PMD_INIT_FUNC_TRACE();
2094 if (rte_mempool_get(mempool, &sess_private_data)) {
2095 DPAA_SEC_ERR("Couldn't get object from session mempool");
2099 ret = dpaa_sec_set_session_parameters(dev, xform, sess_private_data);
2101 DPAA_SEC_ERR("failed to configure session parameters");
2103 /* Return session to mempool */
2104 rte_mempool_put(mempool, sess_private_data);
2108 set_sym_session_private_data(sess, dev->driver_id,
2116 free_session_memory(struct rte_cryptodev *dev, dpaa_sec_session *s)
2118 struct dpaa_sec_dev_private *qi = dev->data->dev_private;
2119 struct rte_mempool *sess_mp = rte_mempool_from_obj((void *)s);
2122 for (i = 0; i < MAX_DPAA_CORES; i++) {
2124 dpaa_sec_detach_rxq(qi, s->inq[i]);
2128 rte_free(s->cipher_key.data);
2129 rte_free(s->auth_key.data);
2130 memset(s, 0, sizeof(dpaa_sec_session));
2131 rte_mempool_put(sess_mp, (void *)s);
2134 /** Clear the memory of session so it doesn't leave key material behind */
2136 dpaa_sec_sym_session_clear(struct rte_cryptodev *dev,
2137 struct rte_cryptodev_sym_session *sess)
2139 PMD_INIT_FUNC_TRACE();
2140 uint8_t index = dev->driver_id;
2141 void *sess_priv = get_sym_session_private_data(sess, index);
2142 dpaa_sec_session *s = (dpaa_sec_session *)sess_priv;
2145 free_session_memory(dev, s);
2146 set_sym_session_private_data(sess, index, NULL);
2151 dpaa_sec_set_ipsec_session(__rte_unused struct rte_cryptodev *dev,
2152 struct rte_security_session_conf *conf,
2155 struct dpaa_sec_dev_private *internals = dev->data->dev_private;
2156 struct rte_security_ipsec_xform *ipsec_xform = &conf->ipsec;
2157 struct rte_crypto_auth_xform *auth_xform = NULL;
2158 struct rte_crypto_cipher_xform *cipher_xform = NULL;
2159 dpaa_sec_session *session = (dpaa_sec_session *)sess;
2162 PMD_INIT_FUNC_TRACE();
2164 memset(session, 0, sizeof(dpaa_sec_session));
2165 if (ipsec_xform->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
2166 cipher_xform = &conf->crypto_xform->cipher;
2167 if (conf->crypto_xform->next)
2168 auth_xform = &conf->crypto_xform->next->auth;
2170 auth_xform = &conf->crypto_xform->auth;
2171 if (conf->crypto_xform->next)
2172 cipher_xform = &conf->crypto_xform->next->cipher;
2174 session->proto_alg = conf->protocol;
2176 if (cipher_xform && cipher_xform->algo != RTE_CRYPTO_CIPHER_NULL) {
2177 session->cipher_key.data = rte_zmalloc(NULL,
2178 cipher_xform->key.length,
2179 RTE_CACHE_LINE_SIZE);
2180 if (session->cipher_key.data == NULL &&
2181 cipher_xform->key.length > 0) {
2182 DPAA_SEC_ERR("No Memory for cipher key");
2185 memcpy(session->cipher_key.data, cipher_xform->key.data,
2186 cipher_xform->key.length);
2187 session->cipher_key.length = cipher_xform->key.length;
2189 switch (cipher_xform->algo) {
2190 case RTE_CRYPTO_CIPHER_AES_CBC:
2191 case RTE_CRYPTO_CIPHER_3DES_CBC:
2192 case RTE_CRYPTO_CIPHER_AES_CTR:
2195 DPAA_SEC_ERR("Crypto: Unsupported Cipher alg %u",
2196 cipher_xform->algo);
2199 session->cipher_alg = cipher_xform->algo;
2201 session->cipher_key.data = NULL;
2202 session->cipher_key.length = 0;
2203 session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
2206 if (auth_xform && auth_xform->algo != RTE_CRYPTO_AUTH_NULL) {
2207 session->auth_key.data = rte_zmalloc(NULL,
2208 auth_xform->key.length,
2209 RTE_CACHE_LINE_SIZE);
2210 if (session->auth_key.data == NULL &&
2211 auth_xform->key.length > 0) {
2212 DPAA_SEC_ERR("No Memory for auth key");
2213 rte_free(session->cipher_key.data);
2216 memcpy(session->auth_key.data, auth_xform->key.data,
2217 auth_xform->key.length);
2218 session->auth_key.length = auth_xform->key.length;
2220 switch (auth_xform->algo) {
2221 case RTE_CRYPTO_AUTH_SHA1_HMAC:
2222 case RTE_CRYPTO_AUTH_MD5_HMAC:
2223 case RTE_CRYPTO_AUTH_SHA256_HMAC:
2224 case RTE_CRYPTO_AUTH_SHA384_HMAC:
2225 case RTE_CRYPTO_AUTH_SHA512_HMAC:
2226 case RTE_CRYPTO_AUTH_AES_CMAC:
2229 DPAA_SEC_ERR("Crypto: Unsupported auth alg %u",
2233 session->auth_alg = auth_xform->algo;
2235 session->auth_key.data = NULL;
2236 session->auth_key.length = 0;
2237 session->auth_alg = RTE_CRYPTO_AUTH_NULL;
2240 if (ipsec_xform->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
2241 memset(&session->encap_pdb, 0, sizeof(struct ipsec_encap_pdb) +
2242 sizeof(session->ip4_hdr));
2243 session->ip4_hdr.ip_v = IPVERSION;
2244 session->ip4_hdr.ip_hl = 5;
2245 session->ip4_hdr.ip_len = rte_cpu_to_be_16(
2246 sizeof(session->ip4_hdr));
2247 session->ip4_hdr.ip_tos = ipsec_xform->tunnel.ipv4.dscp;
2248 session->ip4_hdr.ip_id = 0;
2249 session->ip4_hdr.ip_off = 0;
2250 session->ip4_hdr.ip_ttl = ipsec_xform->tunnel.ipv4.ttl;
2251 session->ip4_hdr.ip_p = (ipsec_xform->proto ==
2252 RTE_SECURITY_IPSEC_SA_PROTO_ESP) ? IPPROTO_ESP
2254 session->ip4_hdr.ip_sum = 0;
2255 session->ip4_hdr.ip_src = ipsec_xform->tunnel.ipv4.src_ip;
2256 session->ip4_hdr.ip_dst = ipsec_xform->tunnel.ipv4.dst_ip;
2257 session->ip4_hdr.ip_sum = calc_chksum((uint16_t *)
2258 (void *)&session->ip4_hdr,
2261 session->encap_pdb.options =
2262 (IPVERSION << PDBNH_ESP_ENCAP_SHIFT) |
2263 PDBOPTS_ESP_OIHI_PDB_INL |
2265 PDBHMO_ESP_ENCAP_DTTL |
2267 if (ipsec_xform->options.esn)
2268 session->encap_pdb.options |= PDBOPTS_ESP_ESN;
2270 session->encap_pdb.spi = ipsec_xform->spi;
2271 session->encap_pdb.ip_hdr_len = sizeof(struct ip);
2273 session->dir = DIR_ENC;
2274 } else if (ipsec_xform->direction ==
2275 RTE_SECURITY_IPSEC_SA_DIR_INGRESS) {
2276 memset(&session->decap_pdb, 0, sizeof(struct ipsec_decap_pdb));
2277 session->decap_pdb.options = sizeof(struct ip) << 16;
2278 if (ipsec_xform->options.esn)
2279 session->decap_pdb.options |= PDBOPTS_ESP_ESN;
2280 session->dir = DIR_DEC;
2283 session->ctx_pool = internals->ctx_pool;
2284 rte_spinlock_lock(&internals->lock);
2285 for (i = 0; i < MAX_DPAA_CORES; i++) {
2286 session->inq[i] = dpaa_sec_attach_rxq(internals);
2287 if (session->inq[i] == NULL) {
2288 DPAA_SEC_ERR("unable to attach sec queue");
2289 rte_spinlock_unlock(&internals->lock);
2293 rte_spinlock_unlock(&internals->lock);
2297 rte_free(session->auth_key.data);
2298 rte_free(session->cipher_key.data);
2299 memset(session, 0, sizeof(dpaa_sec_session));
2304 dpaa_sec_set_pdcp_session(struct rte_cryptodev *dev,
2305 struct rte_security_session_conf *conf,
2308 struct rte_security_pdcp_xform *pdcp_xform = &conf->pdcp;
2309 struct rte_crypto_sym_xform *xform = conf->crypto_xform;
2310 struct rte_crypto_auth_xform *auth_xform = NULL;
2311 struct rte_crypto_cipher_xform *cipher_xform = NULL;
2312 dpaa_sec_session *session = (dpaa_sec_session *)sess;
2313 struct dpaa_sec_dev_private *dev_priv = dev->data->dev_private;
2316 PMD_INIT_FUNC_TRACE();
2318 memset(session, 0, sizeof(dpaa_sec_session));
2320 /* find xfrm types */
2321 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
2322 cipher_xform = &xform->cipher;
2323 if (xform->next != NULL)
2324 auth_xform = &xform->next->auth;
2325 } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
2326 auth_xform = &xform->auth;
2327 if (xform->next != NULL)
2328 cipher_xform = &xform->next->cipher;
2330 DPAA_SEC_ERR("Invalid crypto type");
2334 session->proto_alg = conf->protocol;
2336 session->cipher_key.data = rte_zmalloc(NULL,
2337 cipher_xform->key.length,
2338 RTE_CACHE_LINE_SIZE);
2339 if (session->cipher_key.data == NULL &&
2340 cipher_xform->key.length > 0) {
2341 DPAA_SEC_ERR("No Memory for cipher key");
2344 session->cipher_key.length = cipher_xform->key.length;
2345 memcpy(session->cipher_key.data, cipher_xform->key.data,
2346 cipher_xform->key.length);
2347 session->dir = (cipher_xform->op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
2349 session->cipher_alg = cipher_xform->algo;
2351 session->cipher_key.data = NULL;
2352 session->cipher_key.length = 0;
2353 session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
2354 session->dir = DIR_ENC;
2357 /* Auth is only applicable for control mode operation. */
2358 if (pdcp_xform->domain == RTE_SECURITY_PDCP_MODE_CONTROL) {
2359 if (pdcp_xform->sn_size != RTE_SECURITY_PDCP_SN_SIZE_5) {
2361 "PDCP Seq Num size should be 5 bits for cmode");
2365 session->auth_key.data = rte_zmalloc(NULL,
2366 auth_xform->key.length,
2367 RTE_CACHE_LINE_SIZE);
2368 if (session->auth_key.data == NULL &&
2369 auth_xform->key.length > 0) {
2370 DPAA_SEC_ERR("No Memory for auth key");
2371 rte_free(session->cipher_key.data);
2374 session->auth_key.length = auth_xform->key.length;
2375 memcpy(session->auth_key.data, auth_xform->key.data,
2376 auth_xform->key.length);
2377 session->auth_alg = auth_xform->algo;
2379 session->auth_key.data = NULL;
2380 session->auth_key.length = 0;
2381 session->auth_alg = RTE_CRYPTO_AUTH_NULL;
2384 session->pdcp.domain = pdcp_xform->domain;
2385 session->pdcp.bearer = pdcp_xform->bearer;
2386 session->pdcp.pkt_dir = pdcp_xform->pkt_dir;
2387 session->pdcp.sn_size = pdcp_xform->sn_size;
2388 #ifdef ENABLE_HFN_OVERRIDE
2389 session->pdcp.hfn_ovd = pdcp_xform->hfn_ovd;
2391 session->pdcp.hfn = pdcp_xform->hfn;
2392 session->pdcp.hfn_threshold = pdcp_xform->hfn_threshold;
2394 session->ctx_pool = dev_priv->ctx_pool;
2395 rte_spinlock_lock(&dev_priv->lock);
2396 for (i = 0; i < MAX_DPAA_CORES; i++) {
2397 session->inq[i] = dpaa_sec_attach_rxq(dev_priv);
2398 if (session->inq[i] == NULL) {
2399 DPAA_SEC_ERR("unable to attach sec queue");
2400 rte_spinlock_unlock(&dev_priv->lock);
2404 rte_spinlock_unlock(&dev_priv->lock);
2407 rte_free(session->auth_key.data);
2408 rte_free(session->cipher_key.data);
2409 memset(session, 0, sizeof(dpaa_sec_session));
2414 dpaa_sec_security_session_create(void *dev,
2415 struct rte_security_session_conf *conf,
2416 struct rte_security_session *sess,
2417 struct rte_mempool *mempool)
2419 void *sess_private_data;
2420 struct rte_cryptodev *cdev = (struct rte_cryptodev *)dev;
2423 if (rte_mempool_get(mempool, &sess_private_data)) {
2424 DPAA_SEC_ERR("Couldn't get object from session mempool");
2428 switch (conf->protocol) {
2429 case RTE_SECURITY_PROTOCOL_IPSEC:
2430 ret = dpaa_sec_set_ipsec_session(cdev, conf,
2433 case RTE_SECURITY_PROTOCOL_PDCP:
2434 ret = dpaa_sec_set_pdcp_session(cdev, conf,
2437 case RTE_SECURITY_PROTOCOL_MACSEC:
2443 DPAA_SEC_ERR("failed to configure session parameters");
2444 /* Return session to mempool */
2445 rte_mempool_put(mempool, sess_private_data);
2449 set_sec_session_private_data(sess, sess_private_data);
2454 /** Clear the memory of session so it doesn't leave key material behind */
2456 dpaa_sec_security_session_destroy(void *dev __rte_unused,
2457 struct rte_security_session *sess)
2459 PMD_INIT_FUNC_TRACE();
2460 void *sess_priv = get_sec_session_private_data(sess);
2461 dpaa_sec_session *s = (dpaa_sec_session *)sess_priv;
2464 free_session_memory((struct rte_cryptodev *)dev, s);
2465 set_sec_session_private_data(sess, NULL);
2471 dpaa_sec_dev_configure(struct rte_cryptodev *dev,
2472 struct rte_cryptodev_config *config __rte_unused)
2476 struct dpaa_sec_dev_private *internals;
2478 PMD_INIT_FUNC_TRACE();
2480 internals = dev->data->dev_private;
2481 snprintf(str, sizeof(str), "ctx_pool_%d", dev->data->dev_id);
2482 if (!internals->ctx_pool) {
2483 internals->ctx_pool = rte_mempool_create((const char *)str,
2486 CTX_POOL_CACHE_SIZE, 0,
2487 NULL, NULL, NULL, NULL,
2489 if (!internals->ctx_pool) {
2490 DPAA_SEC_ERR("%s create failed\n", str);
2494 DPAA_SEC_INFO("mempool already created for dev_id : %d",
2501 dpaa_sec_dev_start(struct rte_cryptodev *dev __rte_unused)
2503 PMD_INIT_FUNC_TRACE();
2508 dpaa_sec_dev_stop(struct rte_cryptodev *dev __rte_unused)
2510 PMD_INIT_FUNC_TRACE();
2514 dpaa_sec_dev_close(struct rte_cryptodev *dev)
2516 struct dpaa_sec_dev_private *internals;
2518 PMD_INIT_FUNC_TRACE();
2523 internals = dev->data->dev_private;
2524 rte_mempool_free(internals->ctx_pool);
2525 internals->ctx_pool = NULL;
2531 dpaa_sec_dev_infos_get(struct rte_cryptodev *dev,
2532 struct rte_cryptodev_info *info)
2534 struct dpaa_sec_dev_private *internals = dev->data->dev_private;
2536 PMD_INIT_FUNC_TRACE();
2538 info->max_nb_queue_pairs = internals->max_nb_queue_pairs;
2539 info->feature_flags = dev->feature_flags;
2540 info->capabilities = dpaa_sec_capabilities;
2541 info->sym.max_nb_sessions = internals->max_nb_sessions;
2542 info->driver_id = cryptodev_driver_id;
2546 static struct rte_cryptodev_ops crypto_ops = {
2547 .dev_configure = dpaa_sec_dev_configure,
2548 .dev_start = dpaa_sec_dev_start,
2549 .dev_stop = dpaa_sec_dev_stop,
2550 .dev_close = dpaa_sec_dev_close,
2551 .dev_infos_get = dpaa_sec_dev_infos_get,
2552 .queue_pair_setup = dpaa_sec_queue_pair_setup,
2553 .queue_pair_release = dpaa_sec_queue_pair_release,
2554 .queue_pair_count = dpaa_sec_queue_pair_count,
2555 .sym_session_get_size = dpaa_sec_sym_session_get_size,
2556 .sym_session_configure = dpaa_sec_sym_session_configure,
2557 .sym_session_clear = dpaa_sec_sym_session_clear
2560 static const struct rte_security_capability *
2561 dpaa_sec_capabilities_get(void *device __rte_unused)
2563 return dpaa_sec_security_cap;
2566 static const struct rte_security_ops dpaa_sec_security_ops = {
2567 .session_create = dpaa_sec_security_session_create,
2568 .session_update = NULL,
2569 .session_stats_get = NULL,
2570 .session_destroy = dpaa_sec_security_session_destroy,
2571 .set_pkt_metadata = NULL,
2572 .capabilities_get = dpaa_sec_capabilities_get
2576 dpaa_sec_uninit(struct rte_cryptodev *dev)
2578 struct dpaa_sec_dev_private *internals;
2583 internals = dev->data->dev_private;
2584 rte_free(dev->security_ctx);
2586 /* In case close has been called, internals->ctx_pool would be NULL */
2587 rte_mempool_free(internals->ctx_pool);
2588 rte_free(internals);
2590 DPAA_SEC_INFO("Closing DPAA_SEC device %s on numa socket %u",
2591 dev->data->name, rte_socket_id());
2597 dpaa_sec_dev_init(struct rte_cryptodev *cryptodev)
2599 struct dpaa_sec_dev_private *internals;
2600 struct rte_security_ctx *security_instance;
2601 struct dpaa_sec_qp *qp;
2605 PMD_INIT_FUNC_TRACE();
2607 cryptodev->driver_id = cryptodev_driver_id;
2608 cryptodev->dev_ops = &crypto_ops;
2610 cryptodev->enqueue_burst = dpaa_sec_enqueue_burst;
2611 cryptodev->dequeue_burst = dpaa_sec_dequeue_burst;
2612 cryptodev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
2613 RTE_CRYPTODEV_FF_HW_ACCELERATED |
2614 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
2615 RTE_CRYPTODEV_FF_SECURITY |
2616 RTE_CRYPTODEV_FF_IN_PLACE_SGL |
2617 RTE_CRYPTODEV_FF_OOP_SGL_IN_SGL_OUT |
2618 RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT |
2619 RTE_CRYPTODEV_FF_OOP_LB_IN_SGL_OUT |
2620 RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT;
2622 internals = cryptodev->data->dev_private;
2623 internals->max_nb_queue_pairs = RTE_DPAA_MAX_NB_SEC_QPS;
2624 internals->max_nb_sessions = RTE_DPAA_SEC_PMD_MAX_NB_SESSIONS;
2627 * For secondary processes, we don't initialise any further as primary
2628 * has already done this work. Only check we don't need a different
2631 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
2632 DPAA_SEC_WARN("Device already init by primary process");
2636 /* Initialize security_ctx only for primary process*/
2637 security_instance = rte_malloc("rte_security_instances_ops",
2638 sizeof(struct rte_security_ctx), 0);
2639 if (security_instance == NULL)
2641 security_instance->device = (void *)cryptodev;
2642 security_instance->ops = &dpaa_sec_security_ops;
2643 security_instance->sess_cnt = 0;
2644 cryptodev->security_ctx = security_instance;
2646 rte_spinlock_init(&internals->lock);
2647 for (i = 0; i < internals->max_nb_queue_pairs; i++) {
2648 /* init qman fq for queue pair */
2649 qp = &internals->qps[i];
2650 ret = dpaa_sec_init_tx(&qp->outq);
2652 DPAA_SEC_ERR("config tx of queue pair %d", i);
2657 flags = QMAN_FQ_FLAG_LOCKED | QMAN_FQ_FLAG_DYNAMIC_FQID |
2658 QMAN_FQ_FLAG_TO_DCPORTAL;
2659 for (i = 0; i < MAX_DPAA_CORES * internals->max_nb_sessions; i++) {
2660 /* create rx qman fq for sessions*/
2661 ret = qman_create_fq(0, flags, &internals->inq[i]);
2662 if (unlikely(ret != 0)) {
2663 DPAA_SEC_ERR("sec qman_create_fq failed");
2668 RTE_LOG(INFO, PMD, "%s cryptodev init\n", cryptodev->data->name);
2672 DPAA_SEC_ERR("driver %s: create failed\n", cryptodev->data->name);
2674 dpaa_sec_uninit(cryptodev);
2679 cryptodev_dpaa_sec_probe(struct rte_dpaa_driver *dpaa_drv __rte_unused,
2680 struct rte_dpaa_device *dpaa_dev)
2682 struct rte_cryptodev *cryptodev;
2683 char cryptodev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
2687 snprintf(cryptodev_name, sizeof(cryptodev_name), "dpaa_sec-%d",
2688 dpaa_dev->id.dev_id);
2690 cryptodev = rte_cryptodev_pmd_allocate(cryptodev_name, rte_socket_id());
2691 if (cryptodev == NULL)
2694 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
2695 cryptodev->data->dev_private = rte_zmalloc_socket(
2696 "cryptodev private structure",
2697 sizeof(struct dpaa_sec_dev_private),
2698 RTE_CACHE_LINE_SIZE,
2701 if (cryptodev->data->dev_private == NULL)
2702 rte_panic("Cannot allocate memzone for private "
2706 dpaa_dev->crypto_dev = cryptodev;
2707 cryptodev->device = &dpaa_dev->device;
2709 /* init user callbacks */
2710 TAILQ_INIT(&(cryptodev->link_intr_cbs));
2712 /* if sec device version is not configured */
2713 if (!rta_get_sec_era()) {
2714 const struct device_node *caam_node;
2716 for_each_compatible_node(caam_node, NULL, "fsl,sec-v4.0") {
2717 const uint32_t *prop = of_get_property(caam_node,
2722 INTL_SEC_ERA(rte_cpu_to_be_32(*prop)));
2728 /* Invoke PMD device initialization function */
2729 retval = dpaa_sec_dev_init(cryptodev);
2733 /* In case of error, cleanup is done */
2734 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
2735 rte_free(cryptodev->data->dev_private);
2737 rte_cryptodev_pmd_release_device(cryptodev);
2743 cryptodev_dpaa_sec_remove(struct rte_dpaa_device *dpaa_dev)
2745 struct rte_cryptodev *cryptodev;
2748 cryptodev = dpaa_dev->crypto_dev;
2749 if (cryptodev == NULL)
2752 ret = dpaa_sec_uninit(cryptodev);
2756 return rte_cryptodev_pmd_destroy(cryptodev);
2759 static struct rte_dpaa_driver rte_dpaa_sec_driver = {
2760 .drv_type = FSL_DPAA_CRYPTO,
2762 .name = "DPAA SEC PMD"
2764 .probe = cryptodev_dpaa_sec_probe,
2765 .remove = cryptodev_dpaa_sec_remove,
2768 static struct cryptodev_driver dpaa_sec_crypto_drv;
2770 RTE_PMD_REGISTER_DPAA(CRYPTODEV_NAME_DPAA_SEC_PMD, rte_dpaa_sec_driver);
2771 RTE_PMD_REGISTER_CRYPTO_DRIVER(dpaa_sec_crypto_drv, rte_dpaa_sec_driver.driver,
2772 cryptodev_driver_id);
2774 RTE_INIT(dpaa_sec_init_log)
2776 dpaa_logtype_sec = rte_log_register("pmd.crypto.dpaa");
2777 if (dpaa_logtype_sec >= 0)
2778 rte_log_set_level(dpaa_logtype_sec, RTE_LOG_NOTICE);