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 #ifdef RTE_LIBRTE_SECURITY
19 #include <rte_security_driver.h>
21 #include <rte_cycles.h>
23 #include <rte_kvargs.h>
24 #include <rte_malloc.h>
26 #include <rte_memcpy.h>
27 #include <rte_string_fns.h>
28 #include <rte_spinlock.h>
34 /* RTA header files */
35 #include <desc/common.h>
36 #include <desc/algo.h>
37 #include <desc/ipsec.h>
38 #include <desc/pdcp.h>
40 #include <rte_dpaa_bus.h>
42 #include <dpaa_sec_event.h>
43 #include <dpaa_sec_log.h>
44 #include <dpaax_iova_table.h>
46 enum rta_sec_era rta_sec_era;
50 static uint8_t cryptodev_driver_id;
52 static __thread struct rte_crypto_op **dpaa_sec_ops;
53 static __thread int dpaa_sec_op_nb;
56 dpaa_sec_attach_sess_q(struct dpaa_sec_qp *qp, dpaa_sec_session *sess);
59 dpaa_sec_op_ending(struct dpaa_sec_op_ctx *ctx)
61 if (!ctx->fd_status) {
62 ctx->op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
64 DPAA_SEC_DP_WARN("SEC return err: 0x%x", ctx->fd_status);
65 ctx->op->status = RTE_CRYPTO_OP_STATUS_ERROR;
69 static inline struct dpaa_sec_op_ctx *
70 dpaa_sec_alloc_ctx(dpaa_sec_session *ses, int sg_count)
72 struct dpaa_sec_op_ctx *ctx;
75 retval = rte_mempool_get(
76 ses->qp[rte_lcore_id() % MAX_DPAA_CORES]->ctx_pool,
79 DPAA_SEC_DP_WARN("Alloc sec descriptor failed!");
83 * Clear SG memory. There are 16 SG entries of 16 Bytes each.
84 * one call to dcbz_64() clear 64 bytes, hence calling it 4 times
85 * to clear all the SG entries. dpaa_sec_alloc_ctx() is called for
86 * each packet, memset is costlier than dcbz_64().
88 for (i = 0; i < sg_count && i < MAX_JOB_SG_ENTRIES; i += 4)
89 dcbz_64(&ctx->job.sg[i]);
91 ctx->ctx_pool = ses->qp[rte_lcore_id() % MAX_DPAA_CORES]->ctx_pool;
92 ctx->vtop_offset = (size_t) ctx - rte_mempool_virt2iova(ctx);
97 static inline rte_iova_t
98 dpaa_mem_vtop(void *vaddr)
100 const struct rte_memseg *ms;
102 ms = rte_mem_virt2memseg(vaddr, NULL);
104 dpaax_iova_table_update(ms->iova, ms->addr, ms->len);
105 return ms->iova + RTE_PTR_DIFF(vaddr, ms->addr);
111 dpaa_mem_ptov(rte_iova_t paddr)
115 va = (void *)dpaax_iova_table_get_va(paddr);
119 return rte_mem_iova2virt(paddr);
123 ern_sec_fq_handler(struct qman_portal *qm __rte_unused,
125 const struct qm_mr_entry *msg)
127 DPAA_SEC_DP_ERR("sec fq %d error, RC = %x, seqnum = %x\n",
128 fq->fqid, msg->ern.rc, msg->ern.seqnum);
131 /* initialize the queue with dest chan as caam chan so that
132 * all the packets in this queue could be dispatched into caam
135 dpaa_sec_init_rx(struct qman_fq *fq_in, rte_iova_t hwdesc,
138 struct qm_mcc_initfq fq_opts;
142 /* Clear FQ options */
143 memset(&fq_opts, 0x00, sizeof(struct qm_mcc_initfq));
145 flags = QMAN_INITFQ_FLAG_SCHED;
146 fq_opts.we_mask = QM_INITFQ_WE_DESTWQ | QM_INITFQ_WE_CONTEXTA |
147 QM_INITFQ_WE_CONTEXTB;
149 qm_fqd_context_a_set64(&fq_opts.fqd, hwdesc);
150 fq_opts.fqd.context_b = fqid_out;
151 fq_opts.fqd.dest.channel = qm_channel_caam;
152 fq_opts.fqd.dest.wq = 0;
154 fq_in->cb.ern = ern_sec_fq_handler;
156 DPAA_SEC_DEBUG("in-%x out-%x", fq_in->fqid, fqid_out);
158 ret = qman_init_fq(fq_in, flags, &fq_opts);
159 if (unlikely(ret != 0))
160 DPAA_SEC_ERR("qman_init_fq failed %d", ret);
165 /* something is put into in_fq and caam put the crypto result into out_fq */
166 static enum qman_cb_dqrr_result
167 dqrr_out_fq_cb_rx(struct qman_portal *qm __always_unused,
168 struct qman_fq *fq __always_unused,
169 const struct qm_dqrr_entry *dqrr)
171 const struct qm_fd *fd;
172 struct dpaa_sec_job *job;
173 struct dpaa_sec_op_ctx *ctx;
175 if (dpaa_sec_op_nb >= DPAA_SEC_BURST)
176 return qman_cb_dqrr_defer;
178 if (!(dqrr->stat & QM_DQRR_STAT_FD_VALID))
179 return qman_cb_dqrr_consume;
182 /* sg is embedded in an op ctx,
183 * sg[0] is for output
186 job = dpaa_mem_ptov(qm_fd_addr_get64(fd));
188 ctx = container_of(job, struct dpaa_sec_op_ctx, job);
189 ctx->fd_status = fd->status;
190 if (ctx->op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
191 struct qm_sg_entry *sg_out;
193 struct rte_mbuf *mbuf = (ctx->op->sym->m_dst == NULL) ?
194 ctx->op->sym->m_src : ctx->op->sym->m_dst;
196 sg_out = &job->sg[0];
197 hw_sg_to_cpu(sg_out);
198 len = sg_out->length;
200 while (mbuf->next != NULL) {
201 len -= mbuf->data_len;
204 mbuf->data_len = len;
206 dpaa_sec_ops[dpaa_sec_op_nb++] = ctx->op;
207 dpaa_sec_op_ending(ctx);
209 return qman_cb_dqrr_consume;
212 /* caam result is put into this queue */
214 dpaa_sec_init_tx(struct qman_fq *fq)
217 struct qm_mcc_initfq opts;
220 flags = QMAN_FQ_FLAG_NO_ENQUEUE | QMAN_FQ_FLAG_LOCKED |
221 QMAN_FQ_FLAG_DYNAMIC_FQID;
223 ret = qman_create_fq(0, flags, fq);
225 DPAA_SEC_ERR("qman_create_fq failed");
229 memset(&opts, 0, sizeof(opts));
230 opts.we_mask = QM_INITFQ_WE_DESTWQ | QM_INITFQ_WE_FQCTRL |
231 QM_INITFQ_WE_CONTEXTA | QM_INITFQ_WE_CONTEXTB;
233 /* opts.fqd.dest.channel = dpaa_sec_pool_chan; */
235 fq->cb.dqrr = dqrr_out_fq_cb_rx;
236 fq->cb.ern = ern_sec_fq_handler;
238 ret = qman_init_fq(fq, 0, &opts);
240 DPAA_SEC_ERR("unable to init caam source fq!");
247 static inline int is_encode(dpaa_sec_session *ses)
249 return ses->dir == DIR_ENC;
252 static inline int is_decode(dpaa_sec_session *ses)
254 return ses->dir == DIR_DEC;
257 #ifdef RTE_LIBRTE_SECURITY
259 dpaa_sec_prep_pdcp_cdb(dpaa_sec_session *ses)
261 struct alginfo authdata = {0}, cipherdata = {0};
262 struct sec_cdb *cdb = &ses->cdb;
263 struct alginfo *p_authdata = NULL;
264 int32_t shared_desc_len = 0;
266 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
272 cipherdata.key = (size_t)ses->cipher_key.data;
273 cipherdata.keylen = ses->cipher_key.length;
274 cipherdata.key_enc_flags = 0;
275 cipherdata.key_type = RTA_DATA_IMM;
276 cipherdata.algtype = ses->cipher_key.alg;
277 cipherdata.algmode = ses->cipher_key.algmode;
279 cdb->sh_desc[0] = cipherdata.keylen;
284 authdata.key = (size_t)ses->auth_key.data;
285 authdata.keylen = ses->auth_key.length;
286 authdata.key_enc_flags = 0;
287 authdata.key_type = RTA_DATA_IMM;
288 authdata.algtype = ses->auth_key.alg;
289 authdata.algmode = ses->auth_key.algmode;
291 p_authdata = &authdata;
293 cdb->sh_desc[1] = authdata.keylen;
296 err = rta_inline_query(IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN,
298 (unsigned int *)cdb->sh_desc,
299 &cdb->sh_desc[2], 2);
301 DPAA_SEC_ERR("Crypto: Incorrect key lengths");
305 if (!(cdb->sh_desc[2] & 1) && cipherdata.keylen) {
307 (size_t)dpaa_mem_vtop((void *)(size_t)cipherdata.key);
308 cipherdata.key_type = RTA_DATA_PTR;
310 if (!(cdb->sh_desc[2] & (1 << 1)) && authdata.keylen) {
312 (size_t)dpaa_mem_vtop((void *)(size_t)authdata.key);
313 authdata.key_type = RTA_DATA_PTR;
320 if (ses->pdcp.domain == RTE_SECURITY_PDCP_MODE_CONTROL) {
321 if (ses->dir == DIR_ENC)
322 shared_desc_len = cnstr_shdsc_pdcp_c_plane_encap(
323 cdb->sh_desc, 1, swap,
328 ses->pdcp.hfn_threshold,
329 &cipherdata, &authdata,
331 else if (ses->dir == DIR_DEC)
332 shared_desc_len = cnstr_shdsc_pdcp_c_plane_decap(
333 cdb->sh_desc, 1, swap,
338 ses->pdcp.hfn_threshold,
339 &cipherdata, &authdata,
342 if (ses->dir == DIR_ENC)
343 shared_desc_len = cnstr_shdsc_pdcp_u_plane_encap(
344 cdb->sh_desc, 1, swap,
349 ses->pdcp.hfn_threshold,
350 &cipherdata, p_authdata, 0);
351 else if (ses->dir == DIR_DEC)
352 shared_desc_len = cnstr_shdsc_pdcp_u_plane_decap(
353 cdb->sh_desc, 1, swap,
358 ses->pdcp.hfn_threshold,
359 &cipherdata, p_authdata, 0);
361 return shared_desc_len;
364 /* prepare ipsec proto command block of the session */
366 dpaa_sec_prep_ipsec_cdb(dpaa_sec_session *ses)
368 struct alginfo cipherdata = {0}, authdata = {0};
369 struct sec_cdb *cdb = &ses->cdb;
370 int32_t shared_desc_len = 0;
372 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
378 cipherdata.key = (size_t)ses->cipher_key.data;
379 cipherdata.keylen = ses->cipher_key.length;
380 cipherdata.key_enc_flags = 0;
381 cipherdata.key_type = RTA_DATA_IMM;
382 cipherdata.algtype = ses->cipher_key.alg;
383 cipherdata.algmode = ses->cipher_key.algmode;
385 authdata.key = (size_t)ses->auth_key.data;
386 authdata.keylen = ses->auth_key.length;
387 authdata.key_enc_flags = 0;
388 authdata.key_type = RTA_DATA_IMM;
389 authdata.algtype = ses->auth_key.alg;
390 authdata.algmode = ses->auth_key.algmode;
392 cdb->sh_desc[0] = cipherdata.keylen;
393 cdb->sh_desc[1] = authdata.keylen;
394 err = rta_inline_query(IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN,
396 (unsigned int *)cdb->sh_desc,
397 &cdb->sh_desc[2], 2);
400 DPAA_SEC_ERR("Crypto: Incorrect key lengths");
403 if (cdb->sh_desc[2] & 1)
404 cipherdata.key_type = RTA_DATA_IMM;
406 cipherdata.key = (size_t)dpaa_mem_vtop(
407 (void *)(size_t)cipherdata.key);
408 cipherdata.key_type = RTA_DATA_PTR;
410 if (cdb->sh_desc[2] & (1<<1))
411 authdata.key_type = RTA_DATA_IMM;
413 authdata.key = (size_t)dpaa_mem_vtop(
414 (void *)(size_t)authdata.key);
415 authdata.key_type = RTA_DATA_PTR;
421 if (ses->dir == DIR_ENC) {
422 shared_desc_len = cnstr_shdsc_ipsec_new_encap(
424 true, swap, SHR_SERIAL,
426 (uint8_t *)&ses->ip4_hdr,
427 &cipherdata, &authdata);
428 } else if (ses->dir == DIR_DEC) {
429 shared_desc_len = cnstr_shdsc_ipsec_new_decap(
431 true, swap, SHR_SERIAL,
433 &cipherdata, &authdata);
435 return shared_desc_len;
438 /* prepare command block of the session */
440 dpaa_sec_prep_cdb(dpaa_sec_session *ses)
442 struct alginfo alginfo_c = {0}, alginfo_a = {0}, alginfo = {0};
443 int32_t shared_desc_len = 0;
444 struct sec_cdb *cdb = &ses->cdb;
446 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
452 memset(cdb, 0, sizeof(struct sec_cdb));
455 #ifdef RTE_LIBRTE_SECURITY
457 shared_desc_len = dpaa_sec_prep_ipsec_cdb(ses);
460 shared_desc_len = dpaa_sec_prep_pdcp_cdb(ses);
463 case DPAA_SEC_CIPHER:
464 alginfo_c.key = (size_t)ses->cipher_key.data;
465 alginfo_c.keylen = ses->cipher_key.length;
466 alginfo_c.key_enc_flags = 0;
467 alginfo_c.key_type = RTA_DATA_IMM;
468 alginfo_c.algtype = ses->cipher_key.alg;
469 alginfo_c.algmode = ses->cipher_key.algmode;
471 switch (ses->cipher_alg) {
472 case RTE_CRYPTO_CIPHER_AES_CBC:
473 case RTE_CRYPTO_CIPHER_3DES_CBC:
474 case RTE_CRYPTO_CIPHER_AES_CTR:
475 case RTE_CRYPTO_CIPHER_3DES_CTR:
476 shared_desc_len = cnstr_shdsc_blkcipher(
478 swap, SHR_NEVER, &alginfo_c,
483 case RTE_CRYPTO_CIPHER_SNOW3G_UEA2:
484 shared_desc_len = cnstr_shdsc_snow_f8(
485 cdb->sh_desc, true, swap,
489 case RTE_CRYPTO_CIPHER_ZUC_EEA3:
490 shared_desc_len = cnstr_shdsc_zuce(
491 cdb->sh_desc, true, swap,
496 DPAA_SEC_ERR("unsupported cipher alg %d",
502 alginfo_a.key = (size_t)ses->auth_key.data;
503 alginfo_a.keylen = ses->auth_key.length;
504 alginfo_a.key_enc_flags = 0;
505 alginfo_a.key_type = RTA_DATA_IMM;
506 alginfo_a.algtype = ses->auth_key.alg;
507 alginfo_a.algmode = ses->auth_key.algmode;
508 switch (ses->auth_alg) {
509 case RTE_CRYPTO_AUTH_MD5_HMAC:
510 case RTE_CRYPTO_AUTH_SHA1_HMAC:
511 case RTE_CRYPTO_AUTH_SHA224_HMAC:
512 case RTE_CRYPTO_AUTH_SHA256_HMAC:
513 case RTE_CRYPTO_AUTH_SHA384_HMAC:
514 case RTE_CRYPTO_AUTH_SHA512_HMAC:
515 shared_desc_len = cnstr_shdsc_hmac(
517 swap, SHR_NEVER, &alginfo_a,
521 case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
522 shared_desc_len = cnstr_shdsc_snow_f9(
523 cdb->sh_desc, true, swap,
528 case RTE_CRYPTO_AUTH_ZUC_EIA3:
529 shared_desc_len = cnstr_shdsc_zuca(
530 cdb->sh_desc, true, swap,
536 DPAA_SEC_ERR("unsupported auth alg %u", ses->auth_alg);
540 if (alginfo.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
541 DPAA_SEC_ERR("not supported aead alg");
544 alginfo.key = (size_t)ses->aead_key.data;
545 alginfo.keylen = ses->aead_key.length;
546 alginfo.key_enc_flags = 0;
547 alginfo.key_type = RTA_DATA_IMM;
548 alginfo.algtype = ses->aead_key.alg;
549 alginfo.algmode = ses->aead_key.algmode;
551 if (ses->dir == DIR_ENC)
552 shared_desc_len = cnstr_shdsc_gcm_encap(
553 cdb->sh_desc, true, swap, SHR_NEVER,
558 shared_desc_len = cnstr_shdsc_gcm_decap(
559 cdb->sh_desc, true, swap, SHR_NEVER,
564 case DPAA_SEC_CIPHER_HASH:
565 alginfo_c.key = (size_t)ses->cipher_key.data;
566 alginfo_c.keylen = ses->cipher_key.length;
567 alginfo_c.key_enc_flags = 0;
568 alginfo_c.key_type = RTA_DATA_IMM;
569 alginfo_c.algtype = ses->cipher_key.alg;
570 alginfo_c.algmode = ses->cipher_key.algmode;
572 alginfo_a.key = (size_t)ses->auth_key.data;
573 alginfo_a.keylen = ses->auth_key.length;
574 alginfo_a.key_enc_flags = 0;
575 alginfo_a.key_type = RTA_DATA_IMM;
576 alginfo_a.algtype = ses->auth_key.alg;
577 alginfo_a.algmode = ses->auth_key.algmode;
579 cdb->sh_desc[0] = alginfo_c.keylen;
580 cdb->sh_desc[1] = alginfo_a.keylen;
581 err = rta_inline_query(IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN,
583 (unsigned int *)cdb->sh_desc,
584 &cdb->sh_desc[2], 2);
587 DPAA_SEC_ERR("Crypto: Incorrect key lengths");
590 if (cdb->sh_desc[2] & 1)
591 alginfo_c.key_type = RTA_DATA_IMM;
593 alginfo_c.key = (size_t)dpaa_mem_vtop(
594 (void *)(size_t)alginfo_c.key);
595 alginfo_c.key_type = RTA_DATA_PTR;
597 if (cdb->sh_desc[2] & (1<<1))
598 alginfo_a.key_type = RTA_DATA_IMM;
600 alginfo_a.key = (size_t)dpaa_mem_vtop(
601 (void *)(size_t)alginfo_a.key);
602 alginfo_a.key_type = RTA_DATA_PTR;
607 /* Auth_only_len is set as 0 here and it will be
608 * overwritten in fd for each packet.
610 shared_desc_len = cnstr_shdsc_authenc(cdb->sh_desc,
611 true, swap, SHR_SERIAL, &alginfo_c, &alginfo_a,
613 ses->digest_length, ses->dir);
615 case DPAA_SEC_HASH_CIPHER:
617 DPAA_SEC_ERR("error: Unsupported session");
621 if (shared_desc_len < 0) {
622 DPAA_SEC_ERR("error in preparing command block");
623 return shared_desc_len;
626 cdb->sh_hdr.hi.field.idlen = shared_desc_len;
627 cdb->sh_hdr.hi.word = rte_cpu_to_be_32(cdb->sh_hdr.hi.word);
628 cdb->sh_hdr.lo.word = rte_cpu_to_be_32(cdb->sh_hdr.lo.word);
633 /* qp is lockless, should be accessed by only one thread */
635 dpaa_sec_deq(struct dpaa_sec_qp *qp, struct rte_crypto_op **ops, int nb_ops)
638 unsigned int pkts = 0;
639 int num_rx_bufs, ret;
640 struct qm_dqrr_entry *dq;
641 uint32_t vdqcr_flags = 0;
645 * Until request for four buffers, we provide exact number of buffers.
646 * Otherwise we do not set the QM_VDQCR_EXACT flag.
647 * Not setting QM_VDQCR_EXACT flag can provide two more buffers than
648 * requested, so we request two less in this case.
651 vdqcr_flags = QM_VDQCR_EXACT;
652 num_rx_bufs = nb_ops;
654 num_rx_bufs = nb_ops > DPAA_MAX_DEQUEUE_NUM_FRAMES ?
655 (DPAA_MAX_DEQUEUE_NUM_FRAMES - 2) : (nb_ops - 2);
657 ret = qman_set_vdq(fq, num_rx_bufs, vdqcr_flags);
662 const struct qm_fd *fd;
663 struct dpaa_sec_job *job;
664 struct dpaa_sec_op_ctx *ctx;
665 struct rte_crypto_op *op;
667 dq = qman_dequeue(fq);
672 /* sg is embedded in an op ctx,
673 * sg[0] is for output
676 job = dpaa_mem_ptov(qm_fd_addr_get64(fd));
678 ctx = container_of(job, struct dpaa_sec_op_ctx, job);
679 ctx->fd_status = fd->status;
681 if (op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
682 struct qm_sg_entry *sg_out;
684 struct rte_mbuf *mbuf = (op->sym->m_dst == NULL) ?
685 op->sym->m_src : op->sym->m_dst;
687 sg_out = &job->sg[0];
688 hw_sg_to_cpu(sg_out);
689 len = sg_out->length;
691 while (mbuf->next != NULL) {
692 len -= mbuf->data_len;
695 mbuf->data_len = len;
697 if (!ctx->fd_status) {
698 op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
700 DPAA_SEC_DP_WARN("SEC return err:0x%x", ctx->fd_status);
701 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
705 /* report op status to sym->op and then free the ctx memeory */
706 rte_mempool_put(ctx->ctx_pool, (void *)ctx);
708 qman_dqrr_consume(fq, dq);
709 } while (fq->flags & QMAN_FQ_STATE_VDQCR);
714 static inline struct dpaa_sec_job *
715 build_auth_only_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
717 struct rte_crypto_sym_op *sym = op->sym;
718 struct rte_mbuf *mbuf = sym->m_src;
719 struct dpaa_sec_job *cf;
720 struct dpaa_sec_op_ctx *ctx;
721 struct qm_sg_entry *sg, *out_sg, *in_sg;
722 phys_addr_t start_addr;
723 uint8_t *old_digest, extra_segs;
724 int data_len, data_offset;
726 data_len = sym->auth.data.length;
727 data_offset = sym->auth.data.offset;
729 if (ses->auth_alg == RTE_CRYPTO_AUTH_SNOW3G_UIA2 ||
730 ses->auth_alg == RTE_CRYPTO_AUTH_ZUC_EIA3) {
731 if ((data_len & 7) || (data_offset & 7)) {
732 DPAA_SEC_ERR("AUTH: len/offset must be full bytes");
736 data_len = data_len >> 3;
737 data_offset = data_offset >> 3;
745 if (mbuf->nb_segs > MAX_SG_ENTRIES) {
746 DPAA_SEC_DP_ERR("Auth: Max sec segs supported is %d",
750 ctx = dpaa_sec_alloc_ctx(ses, mbuf->nb_segs + extra_segs);
756 old_digest = ctx->digest;
760 qm_sg_entry_set64(out_sg, sym->auth.digest.phys_addr);
761 out_sg->length = ses->digest_length;
762 cpu_to_hw_sg(out_sg);
766 /* need to extend the input to a compound frame */
767 in_sg->extension = 1;
769 in_sg->length = data_len;
770 qm_sg_entry_set64(in_sg, dpaa_mem_vtop(&cf->sg[2]));
775 if (ses->iv.length) {
778 iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
781 if (ses->auth_alg == RTE_CRYPTO_AUTH_SNOW3G_UIA2) {
782 iv_ptr = conv_to_snow_f9_iv(iv_ptr);
784 } else if (ses->auth_alg == RTE_CRYPTO_AUTH_ZUC_EIA3) {
785 iv_ptr = conv_to_zuc_eia_iv(iv_ptr);
788 sg->length = ses->iv.length;
790 qm_sg_entry_set64(sg, dpaa_mem_vtop(iv_ptr));
791 in_sg->length += sg->length;
796 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
797 sg->offset = data_offset;
799 if (data_len <= (mbuf->data_len - data_offset)) {
800 sg->length = data_len;
802 sg->length = mbuf->data_len - data_offset;
804 /* remaining i/p segs */
805 while ((data_len = data_len - sg->length) &&
806 (mbuf = mbuf->next)) {
809 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
810 if (data_len > mbuf->data_len)
811 sg->length = mbuf->data_len;
813 sg->length = data_len;
817 if (is_decode(ses)) {
818 /* Digest verification case */
821 rte_memcpy(old_digest, sym->auth.digest.data,
823 start_addr = dpaa_mem_vtop(old_digest);
824 qm_sg_entry_set64(sg, start_addr);
825 sg->length = ses->digest_length;
826 in_sg->length += ses->digest_length;
837 * |<----data_len------->|
838 * |ip_header|ah_header|icv|payload|
843 static inline struct dpaa_sec_job *
844 build_auth_only(struct rte_crypto_op *op, dpaa_sec_session *ses)
846 struct rte_crypto_sym_op *sym = op->sym;
847 struct rte_mbuf *mbuf = sym->m_src;
848 struct dpaa_sec_job *cf;
849 struct dpaa_sec_op_ctx *ctx;
850 struct qm_sg_entry *sg, *in_sg;
851 rte_iova_t start_addr;
853 int data_len, data_offset;
855 data_len = sym->auth.data.length;
856 data_offset = sym->auth.data.offset;
858 if (ses->auth_alg == RTE_CRYPTO_AUTH_SNOW3G_UIA2 ||
859 ses->auth_alg == RTE_CRYPTO_AUTH_ZUC_EIA3) {
860 if ((data_len & 7) || (data_offset & 7)) {
861 DPAA_SEC_ERR("AUTH: len/offset must be full bytes");
865 data_len = data_len >> 3;
866 data_offset = data_offset >> 3;
869 ctx = dpaa_sec_alloc_ctx(ses, 4);
875 old_digest = ctx->digest;
877 start_addr = rte_pktmbuf_iova(mbuf);
880 qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
881 sg->length = ses->digest_length;
886 /* need to extend the input to a compound frame */
887 in_sg->extension = 1;
889 in_sg->length = data_len;
890 qm_sg_entry_set64(in_sg, dpaa_mem_vtop(&cf->sg[2]));
893 if (ses->iv.length) {
896 iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
899 if (ses->auth_alg == RTE_CRYPTO_AUTH_SNOW3G_UIA2) {
900 iv_ptr = conv_to_snow_f9_iv(iv_ptr);
902 } else if (ses->auth_alg == RTE_CRYPTO_AUTH_ZUC_EIA3) {
903 iv_ptr = conv_to_zuc_eia_iv(iv_ptr);
906 sg->length = ses->iv.length;
908 qm_sg_entry_set64(sg, dpaa_mem_vtop(iv_ptr));
909 in_sg->length += sg->length;
914 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
915 sg->offset = data_offset;
916 sg->length = data_len;
918 if (is_decode(ses)) {
919 /* Digest verification case */
921 /* hash result or digest, save digest first */
922 rte_memcpy(old_digest, sym->auth.digest.data,
924 /* let's check digest by hw */
925 start_addr = dpaa_mem_vtop(old_digest);
927 qm_sg_entry_set64(sg, start_addr);
928 sg->length = ses->digest_length;
929 in_sg->length += ses->digest_length;
938 static inline struct dpaa_sec_job *
939 build_cipher_only_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
941 struct rte_crypto_sym_op *sym = op->sym;
942 struct dpaa_sec_job *cf;
943 struct dpaa_sec_op_ctx *ctx;
944 struct qm_sg_entry *sg, *out_sg, *in_sg;
945 struct rte_mbuf *mbuf;
947 uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
949 int data_len, data_offset;
951 data_len = sym->cipher.data.length;
952 data_offset = sym->cipher.data.offset;
954 if (ses->cipher_alg == RTE_CRYPTO_CIPHER_SNOW3G_UEA2 ||
955 ses->cipher_alg == RTE_CRYPTO_CIPHER_ZUC_EEA3) {
956 if ((data_len & 7) || (data_offset & 7)) {
957 DPAA_SEC_ERR("CIPHER: len/offset must be full bytes");
961 data_len = data_len >> 3;
962 data_offset = data_offset >> 3;
967 req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 3;
970 req_segs = mbuf->nb_segs * 2 + 3;
972 if (mbuf->nb_segs > MAX_SG_ENTRIES) {
973 DPAA_SEC_DP_ERR("Cipher: Max sec segs supported is %d",
978 ctx = dpaa_sec_alloc_ctx(ses, req_segs);
987 out_sg->extension = 1;
988 out_sg->length = data_len;
989 qm_sg_entry_set64(out_sg, dpaa_mem_vtop(&cf->sg[2]));
990 cpu_to_hw_sg(out_sg);
994 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
995 sg->length = mbuf->data_len - data_offset;
996 sg->offset = data_offset;
998 /* Successive segs */
1003 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1004 sg->length = mbuf->data_len;
1013 in_sg->extension = 1;
1015 in_sg->length = data_len + ses->iv.length;
1018 qm_sg_entry_set64(in_sg, dpaa_mem_vtop(sg));
1019 cpu_to_hw_sg(in_sg);
1022 qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1023 sg->length = ses->iv.length;
1028 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1029 sg->length = mbuf->data_len - data_offset;
1030 sg->offset = data_offset;
1032 /* Successive segs */
1037 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1038 sg->length = mbuf->data_len;
1047 static inline struct dpaa_sec_job *
1048 build_cipher_only(struct rte_crypto_op *op, dpaa_sec_session *ses)
1050 struct rte_crypto_sym_op *sym = op->sym;
1051 struct dpaa_sec_job *cf;
1052 struct dpaa_sec_op_ctx *ctx;
1053 struct qm_sg_entry *sg;
1054 rte_iova_t src_start_addr, dst_start_addr;
1055 uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1057 int data_len, data_offset;
1059 data_len = sym->cipher.data.length;
1060 data_offset = sym->cipher.data.offset;
1062 if (ses->cipher_alg == RTE_CRYPTO_CIPHER_SNOW3G_UEA2 ||
1063 ses->cipher_alg == RTE_CRYPTO_CIPHER_ZUC_EEA3) {
1064 if ((data_len & 7) || (data_offset & 7)) {
1065 DPAA_SEC_ERR("CIPHER: len/offset must be full bytes");
1069 data_len = data_len >> 3;
1070 data_offset = data_offset >> 3;
1073 ctx = dpaa_sec_alloc_ctx(ses, 4);
1080 src_start_addr = rte_pktmbuf_iova(sym->m_src);
1083 dst_start_addr = rte_pktmbuf_iova(sym->m_dst);
1085 dst_start_addr = src_start_addr;
1089 qm_sg_entry_set64(sg, dst_start_addr + data_offset);
1090 sg->length = data_len + ses->iv.length;
1096 /* need to extend the input to a compound frame */
1099 sg->length = data_len + ses->iv.length;
1100 qm_sg_entry_set64(sg, dpaa_mem_vtop(&cf->sg[2]));
1104 qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1105 sg->length = ses->iv.length;
1109 qm_sg_entry_set64(sg, src_start_addr + data_offset);
1110 sg->length = data_len;
1117 static inline struct dpaa_sec_job *
1118 build_cipher_auth_gcm_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
1120 struct rte_crypto_sym_op *sym = op->sym;
1121 struct dpaa_sec_job *cf;
1122 struct dpaa_sec_op_ctx *ctx;
1123 struct qm_sg_entry *sg, *out_sg, *in_sg;
1124 struct rte_mbuf *mbuf;
1126 uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1131 req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 4;
1134 req_segs = mbuf->nb_segs * 2 + 4;
1137 if (ses->auth_only_len)
1140 if (mbuf->nb_segs > MAX_SG_ENTRIES) {
1141 DPAA_SEC_DP_ERR("AEAD: Max sec segs supported is %d",
1146 ctx = dpaa_sec_alloc_ctx(ses, req_segs);
1153 rte_prefetch0(cf->sg);
1156 out_sg = &cf->sg[0];
1157 out_sg->extension = 1;
1159 out_sg->length = sym->aead.data.length + ses->digest_length;
1161 out_sg->length = sym->aead.data.length;
1163 /* output sg entries */
1165 qm_sg_entry_set64(out_sg, dpaa_mem_vtop(sg));
1166 cpu_to_hw_sg(out_sg);
1169 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1170 sg->length = mbuf->data_len - sym->aead.data.offset;
1171 sg->offset = sym->aead.data.offset;
1173 /* Successive segs */
1178 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1179 sg->length = mbuf->data_len;
1182 sg->length -= ses->digest_length;
1184 if (is_encode(ses)) {
1186 /* set auth output */
1188 qm_sg_entry_set64(sg, sym->aead.digest.phys_addr);
1189 sg->length = ses->digest_length;
1197 in_sg->extension = 1;
1200 in_sg->length = ses->iv.length + sym->aead.data.length
1201 + ses->auth_only_len;
1203 in_sg->length = ses->iv.length + sym->aead.data.length
1204 + ses->auth_only_len + ses->digest_length;
1206 /* input sg entries */
1208 qm_sg_entry_set64(in_sg, dpaa_mem_vtop(sg));
1209 cpu_to_hw_sg(in_sg);
1212 qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1213 sg->length = ses->iv.length;
1216 /* 2nd seg auth only */
1217 if (ses->auth_only_len) {
1219 qm_sg_entry_set64(sg, dpaa_mem_vtop(sym->aead.aad.data));
1220 sg->length = ses->auth_only_len;
1226 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1227 sg->length = mbuf->data_len - sym->aead.data.offset;
1228 sg->offset = sym->aead.data.offset;
1230 /* Successive segs */
1235 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1236 sg->length = mbuf->data_len;
1240 if (is_decode(ses)) {
1243 memcpy(ctx->digest, sym->aead.digest.data,
1244 ses->digest_length);
1245 qm_sg_entry_set64(sg, dpaa_mem_vtop(ctx->digest));
1246 sg->length = ses->digest_length;
1254 static inline struct dpaa_sec_job *
1255 build_cipher_auth_gcm(struct rte_crypto_op *op, dpaa_sec_session *ses)
1257 struct rte_crypto_sym_op *sym = op->sym;
1258 struct dpaa_sec_job *cf;
1259 struct dpaa_sec_op_ctx *ctx;
1260 struct qm_sg_entry *sg;
1261 uint32_t length = 0;
1262 rte_iova_t src_start_addr, dst_start_addr;
1263 uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1266 src_start_addr = sym->m_src->buf_iova + sym->m_src->data_off;
1269 dst_start_addr = sym->m_dst->buf_iova + sym->m_dst->data_off;
1271 dst_start_addr = src_start_addr;
1273 ctx = dpaa_sec_alloc_ctx(ses, 7);
1281 rte_prefetch0(cf->sg);
1283 qm_sg_entry_set64(&cf->sg[1], dpaa_mem_vtop(sg));
1284 if (is_encode(ses)) {
1285 qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1286 sg->length = ses->iv.length;
1287 length += sg->length;
1291 if (ses->auth_only_len) {
1292 qm_sg_entry_set64(sg,
1293 dpaa_mem_vtop(sym->aead.aad.data));
1294 sg->length = ses->auth_only_len;
1295 length += sg->length;
1299 qm_sg_entry_set64(sg, src_start_addr + sym->aead.data.offset);
1300 sg->length = sym->aead.data.length;
1301 length += sg->length;
1305 qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1306 sg->length = ses->iv.length;
1307 length += sg->length;
1311 if (ses->auth_only_len) {
1312 qm_sg_entry_set64(sg,
1313 dpaa_mem_vtop(sym->aead.aad.data));
1314 sg->length = ses->auth_only_len;
1315 length += sg->length;
1319 qm_sg_entry_set64(sg, src_start_addr + sym->aead.data.offset);
1320 sg->length = sym->aead.data.length;
1321 length += sg->length;
1324 memcpy(ctx->digest, sym->aead.digest.data,
1325 ses->digest_length);
1328 qm_sg_entry_set64(sg, dpaa_mem_vtop(ctx->digest));
1329 sg->length = ses->digest_length;
1330 length += sg->length;
1334 /* input compound frame */
1335 cf->sg[1].length = length;
1336 cf->sg[1].extension = 1;
1337 cf->sg[1].final = 1;
1338 cpu_to_hw_sg(&cf->sg[1]);
1342 qm_sg_entry_set64(&cf->sg[0], dpaa_mem_vtop(sg));
1343 qm_sg_entry_set64(sg,
1344 dst_start_addr + sym->aead.data.offset);
1345 sg->length = sym->aead.data.length;
1346 length = sg->length;
1347 if (is_encode(ses)) {
1349 /* set auth output */
1351 qm_sg_entry_set64(sg, sym->aead.digest.phys_addr);
1352 sg->length = ses->digest_length;
1353 length += sg->length;
1358 /* output compound frame */
1359 cf->sg[0].length = length;
1360 cf->sg[0].extension = 1;
1361 cpu_to_hw_sg(&cf->sg[0]);
1366 static inline struct dpaa_sec_job *
1367 build_cipher_auth_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
1369 struct rte_crypto_sym_op *sym = op->sym;
1370 struct dpaa_sec_job *cf;
1371 struct dpaa_sec_op_ctx *ctx;
1372 struct qm_sg_entry *sg, *out_sg, *in_sg;
1373 struct rte_mbuf *mbuf;
1375 uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1380 req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 4;
1383 req_segs = mbuf->nb_segs * 2 + 4;
1386 if (mbuf->nb_segs > MAX_SG_ENTRIES) {
1387 DPAA_SEC_DP_ERR("Cipher-Auth: Max sec segs supported is %d",
1392 ctx = dpaa_sec_alloc_ctx(ses, req_segs);
1399 rte_prefetch0(cf->sg);
1402 out_sg = &cf->sg[0];
1403 out_sg->extension = 1;
1405 out_sg->length = sym->auth.data.length + ses->digest_length;
1407 out_sg->length = sym->auth.data.length;
1409 /* output sg entries */
1411 qm_sg_entry_set64(out_sg, dpaa_mem_vtop(sg));
1412 cpu_to_hw_sg(out_sg);
1415 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1416 sg->length = mbuf->data_len - sym->auth.data.offset;
1417 sg->offset = sym->auth.data.offset;
1419 /* Successive segs */
1424 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1425 sg->length = mbuf->data_len;
1428 sg->length -= ses->digest_length;
1430 if (is_encode(ses)) {
1432 /* set auth output */
1434 qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
1435 sg->length = ses->digest_length;
1443 in_sg->extension = 1;
1446 in_sg->length = ses->iv.length + sym->auth.data.length;
1448 in_sg->length = ses->iv.length + sym->auth.data.length
1449 + ses->digest_length;
1451 /* input sg entries */
1453 qm_sg_entry_set64(in_sg, dpaa_mem_vtop(sg));
1454 cpu_to_hw_sg(in_sg);
1457 qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1458 sg->length = ses->iv.length;
1463 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1464 sg->length = mbuf->data_len - sym->auth.data.offset;
1465 sg->offset = sym->auth.data.offset;
1467 /* Successive segs */
1472 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1473 sg->length = mbuf->data_len;
1477 sg->length -= ses->digest_length;
1478 if (is_decode(ses)) {
1481 memcpy(ctx->digest, sym->auth.digest.data,
1482 ses->digest_length);
1483 qm_sg_entry_set64(sg, dpaa_mem_vtop(ctx->digest));
1484 sg->length = ses->digest_length;
1492 static inline struct dpaa_sec_job *
1493 build_cipher_auth(struct rte_crypto_op *op, dpaa_sec_session *ses)
1495 struct rte_crypto_sym_op *sym = op->sym;
1496 struct dpaa_sec_job *cf;
1497 struct dpaa_sec_op_ctx *ctx;
1498 struct qm_sg_entry *sg;
1499 rte_iova_t src_start_addr, dst_start_addr;
1500 uint32_t length = 0;
1501 uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1504 src_start_addr = sym->m_src->buf_iova + sym->m_src->data_off;
1506 dst_start_addr = sym->m_dst->buf_iova + sym->m_dst->data_off;
1508 dst_start_addr = src_start_addr;
1510 ctx = dpaa_sec_alloc_ctx(ses, 7);
1518 rte_prefetch0(cf->sg);
1520 qm_sg_entry_set64(&cf->sg[1], dpaa_mem_vtop(sg));
1521 if (is_encode(ses)) {
1522 qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1523 sg->length = ses->iv.length;
1524 length += sg->length;
1528 qm_sg_entry_set64(sg, src_start_addr + sym->auth.data.offset);
1529 sg->length = sym->auth.data.length;
1530 length += sg->length;
1534 qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1535 sg->length = ses->iv.length;
1536 length += sg->length;
1541 qm_sg_entry_set64(sg, src_start_addr + sym->auth.data.offset);
1542 sg->length = sym->auth.data.length;
1543 length += sg->length;
1546 memcpy(ctx->digest, sym->auth.digest.data,
1547 ses->digest_length);
1550 qm_sg_entry_set64(sg, dpaa_mem_vtop(ctx->digest));
1551 sg->length = ses->digest_length;
1552 length += sg->length;
1556 /* input compound frame */
1557 cf->sg[1].length = length;
1558 cf->sg[1].extension = 1;
1559 cf->sg[1].final = 1;
1560 cpu_to_hw_sg(&cf->sg[1]);
1564 qm_sg_entry_set64(&cf->sg[0], dpaa_mem_vtop(sg));
1565 qm_sg_entry_set64(sg, dst_start_addr + sym->cipher.data.offset);
1566 sg->length = sym->cipher.data.length;
1567 length = sg->length;
1568 if (is_encode(ses)) {
1570 /* set auth output */
1572 qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
1573 sg->length = ses->digest_length;
1574 length += sg->length;
1579 /* output compound frame */
1580 cf->sg[0].length = length;
1581 cf->sg[0].extension = 1;
1582 cpu_to_hw_sg(&cf->sg[0]);
1587 #ifdef RTE_LIBRTE_SECURITY
1588 static inline struct dpaa_sec_job *
1589 build_proto(struct rte_crypto_op *op, dpaa_sec_session *ses)
1591 struct rte_crypto_sym_op *sym = op->sym;
1592 struct dpaa_sec_job *cf;
1593 struct dpaa_sec_op_ctx *ctx;
1594 struct qm_sg_entry *sg;
1595 phys_addr_t src_start_addr, dst_start_addr;
1597 ctx = dpaa_sec_alloc_ctx(ses, 2);
1603 src_start_addr = rte_pktmbuf_mtophys(sym->m_src);
1606 dst_start_addr = rte_pktmbuf_mtophys(sym->m_dst);
1608 dst_start_addr = src_start_addr;
1612 qm_sg_entry_set64(sg, src_start_addr);
1613 sg->length = sym->m_src->pkt_len;
1617 sym->m_src->packet_type &= ~RTE_PTYPE_L4_MASK;
1620 qm_sg_entry_set64(sg, dst_start_addr);
1621 sg->length = sym->m_src->buf_len - sym->m_src->data_off;
1627 static inline struct dpaa_sec_job *
1628 build_proto_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
1630 struct rte_crypto_sym_op *sym = op->sym;
1631 struct dpaa_sec_job *cf;
1632 struct dpaa_sec_op_ctx *ctx;
1633 struct qm_sg_entry *sg, *out_sg, *in_sg;
1634 struct rte_mbuf *mbuf;
1636 uint32_t in_len = 0, out_len = 0;
1643 req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 2;
1644 if (mbuf->nb_segs > MAX_SG_ENTRIES) {
1645 DPAA_SEC_DP_ERR("Proto: Max sec segs supported is %d",
1650 ctx = dpaa_sec_alloc_ctx(ses, req_segs);
1656 out_sg = &cf->sg[0];
1657 out_sg->extension = 1;
1658 qm_sg_entry_set64(out_sg, dpaa_mem_vtop(&cf->sg[2]));
1662 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1665 /* Successive segs */
1666 while (mbuf->next) {
1667 sg->length = mbuf->data_len;
1668 out_len += sg->length;
1672 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1675 sg->length = mbuf->buf_len - mbuf->data_off;
1676 out_len += sg->length;
1680 out_sg->length = out_len;
1681 cpu_to_hw_sg(out_sg);
1686 in_sg->extension = 1;
1688 in_len = mbuf->data_len;
1691 qm_sg_entry_set64(in_sg, dpaa_mem_vtop(sg));
1694 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1695 sg->length = mbuf->data_len;
1698 /* Successive segs */
1703 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1704 sg->length = mbuf->data_len;
1706 in_len += sg->length;
1712 in_sg->length = in_len;
1713 cpu_to_hw_sg(in_sg);
1715 sym->m_src->packet_type &= ~RTE_PTYPE_L4_MASK;
1722 dpaa_sec_enqueue_burst(void *qp, struct rte_crypto_op **ops,
1725 /* Function to transmit the frames to given device and queuepair */
1727 struct dpaa_sec_qp *dpaa_qp = (struct dpaa_sec_qp *)qp;
1728 uint16_t num_tx = 0;
1729 struct qm_fd fds[DPAA_SEC_BURST], *fd;
1730 uint32_t frames_to_send;
1731 struct rte_crypto_op *op;
1732 struct dpaa_sec_job *cf;
1733 dpaa_sec_session *ses;
1734 uint16_t auth_hdr_len, auth_tail_len;
1735 uint32_t index, flags[DPAA_SEC_BURST] = {0};
1736 struct qman_fq *inq[DPAA_SEC_BURST];
1739 frames_to_send = (nb_ops > DPAA_SEC_BURST) ?
1740 DPAA_SEC_BURST : nb_ops;
1741 for (loop = 0; loop < frames_to_send; loop++) {
1743 if (op->sym->m_src->seqn != 0) {
1744 index = op->sym->m_src->seqn - 1;
1745 if (DPAA_PER_LCORE_DQRR_HELD & (1 << index)) {
1746 /* QM_EQCR_DCA_IDXMASK = 0x0f */
1747 flags[loop] = ((index & 0x0f) << 8);
1748 flags[loop] |= QMAN_ENQUEUE_FLAG_DCA;
1749 DPAA_PER_LCORE_DQRR_SIZE--;
1750 DPAA_PER_LCORE_DQRR_HELD &=
1755 switch (op->sess_type) {
1756 case RTE_CRYPTO_OP_WITH_SESSION:
1757 ses = (dpaa_sec_session *)
1758 get_sym_session_private_data(
1760 cryptodev_driver_id);
1762 #ifdef RTE_LIBRTE_SECURITY
1763 case RTE_CRYPTO_OP_SECURITY_SESSION:
1764 ses = (dpaa_sec_session *)
1765 get_sec_session_private_data(
1766 op->sym->sec_session);
1771 "sessionless crypto op not supported");
1772 frames_to_send = loop;
1778 DPAA_SEC_DP_ERR("session not available");
1779 frames_to_send = loop;
1784 if (unlikely(!ses->qp[rte_lcore_id() % MAX_DPAA_CORES])) {
1785 if (dpaa_sec_attach_sess_q(qp, ses)) {
1786 frames_to_send = loop;
1790 } else if (unlikely(ses->qp[rte_lcore_id() %
1791 MAX_DPAA_CORES] != qp)) {
1792 DPAA_SEC_DP_ERR("Old:sess->qp = %p"
1794 ses->qp[rte_lcore_id() %
1795 MAX_DPAA_CORES], qp);
1796 frames_to_send = loop;
1801 auth_hdr_len = op->sym->auth.data.length -
1802 op->sym->cipher.data.length;
1805 if (rte_pktmbuf_is_contiguous(op->sym->m_src) &&
1806 ((op->sym->m_dst == NULL) ||
1807 rte_pktmbuf_is_contiguous(op->sym->m_dst))) {
1808 switch (ses->ctxt) {
1809 #ifdef RTE_LIBRTE_SECURITY
1811 case DPAA_SEC_IPSEC:
1812 cf = build_proto(op, ses);
1816 cf = build_auth_only(op, ses);
1818 case DPAA_SEC_CIPHER:
1819 cf = build_cipher_only(op, ses);
1822 cf = build_cipher_auth_gcm(op, ses);
1823 auth_hdr_len = ses->auth_only_len;
1825 case DPAA_SEC_CIPHER_HASH:
1827 op->sym->cipher.data.offset
1828 - op->sym->auth.data.offset;
1830 op->sym->auth.data.length
1831 - op->sym->cipher.data.length
1833 cf = build_cipher_auth(op, ses);
1836 DPAA_SEC_DP_ERR("not supported ops");
1837 frames_to_send = loop;
1842 switch (ses->ctxt) {
1843 #ifdef RTE_LIBRTE_SECURITY
1845 case DPAA_SEC_IPSEC:
1846 cf = build_proto_sg(op, ses);
1850 cf = build_auth_only_sg(op, ses);
1852 case DPAA_SEC_CIPHER:
1853 cf = build_cipher_only_sg(op, ses);
1856 cf = build_cipher_auth_gcm_sg(op, ses);
1857 auth_hdr_len = ses->auth_only_len;
1859 case DPAA_SEC_CIPHER_HASH:
1861 op->sym->cipher.data.offset
1862 - op->sym->auth.data.offset;
1864 op->sym->auth.data.length
1865 - op->sym->cipher.data.length
1867 cf = build_cipher_auth_sg(op, ses);
1870 DPAA_SEC_DP_ERR("not supported ops");
1871 frames_to_send = loop;
1876 if (unlikely(!cf)) {
1877 frames_to_send = loop;
1883 inq[loop] = ses->inq[rte_lcore_id() % MAX_DPAA_CORES];
1884 fd->opaque_addr = 0;
1886 qm_fd_addr_set64(fd, dpaa_mem_vtop(cf->sg));
1887 fd->_format1 = qm_fd_compound;
1888 fd->length29 = 2 * sizeof(struct qm_sg_entry);
1890 /* Auth_only_len is set as 0 in descriptor and it is
1891 * overwritten here in the fd.cmd which will update
1894 if (auth_hdr_len || auth_tail_len) {
1895 fd->cmd = 0x80000000;
1897 ((auth_tail_len << 16) | auth_hdr_len);
1900 #ifdef RTE_LIBRTE_SECURITY
1901 /* In case of PDCP, per packet HFN is stored in
1902 * mbuf priv after sym_op.
1904 if ((ses->ctxt == DPAA_SEC_PDCP) && ses->pdcp.hfn_ovd) {
1905 fd->cmd = 0x80000000 |
1906 *((uint32_t *)((uint8_t *)op +
1907 ses->pdcp.hfn_ovd_offset));
1908 DPAA_SEC_DP_DEBUG("Per packet HFN: %x, ovd:%u\n",
1909 *((uint32_t *)((uint8_t *)op +
1910 ses->pdcp.hfn_ovd_offset)),
1917 while (loop < frames_to_send) {
1918 loop += qman_enqueue_multi_fq(&inq[loop], &fds[loop],
1919 &flags[loop], frames_to_send - loop);
1921 nb_ops -= frames_to_send;
1922 num_tx += frames_to_send;
1925 dpaa_qp->tx_pkts += num_tx;
1926 dpaa_qp->tx_errs += nb_ops - num_tx;
1932 dpaa_sec_dequeue_burst(void *qp, struct rte_crypto_op **ops,
1936 struct dpaa_sec_qp *dpaa_qp = (struct dpaa_sec_qp *)qp;
1938 num_rx = dpaa_sec_deq(dpaa_qp, ops, nb_ops);
1940 dpaa_qp->rx_pkts += num_rx;
1941 dpaa_qp->rx_errs += nb_ops - num_rx;
1943 DPAA_SEC_DP_DEBUG("SEC Received %d Packets\n", num_rx);
1948 /** Release queue pair */
1950 dpaa_sec_queue_pair_release(struct rte_cryptodev *dev,
1953 struct dpaa_sec_dev_private *internals;
1954 struct dpaa_sec_qp *qp = NULL;
1956 PMD_INIT_FUNC_TRACE();
1958 DPAA_SEC_DEBUG("dev =%p, queue =%d", dev, qp_id);
1960 internals = dev->data->dev_private;
1961 if (qp_id >= internals->max_nb_queue_pairs) {
1962 DPAA_SEC_ERR("Max supported qpid %d",
1963 internals->max_nb_queue_pairs);
1967 qp = &internals->qps[qp_id];
1968 rte_mempool_free(qp->ctx_pool);
1969 qp->internals = NULL;
1970 dev->data->queue_pairs[qp_id] = NULL;
1975 /** Setup a queue pair */
1977 dpaa_sec_queue_pair_setup(struct rte_cryptodev *dev, uint16_t qp_id,
1978 __rte_unused const struct rte_cryptodev_qp_conf *qp_conf,
1979 __rte_unused int socket_id)
1981 struct dpaa_sec_dev_private *internals;
1982 struct dpaa_sec_qp *qp = NULL;
1985 DPAA_SEC_DEBUG("dev =%p, queue =%d, conf =%p", dev, qp_id, qp_conf);
1987 internals = dev->data->dev_private;
1988 if (qp_id >= internals->max_nb_queue_pairs) {
1989 DPAA_SEC_ERR("Max supported qpid %d",
1990 internals->max_nb_queue_pairs);
1994 qp = &internals->qps[qp_id];
1995 qp->internals = internals;
1996 snprintf(str, sizeof(str), "ctx_pool_d%d_qp%d",
1997 dev->data->dev_id, qp_id);
1998 if (!qp->ctx_pool) {
1999 qp->ctx_pool = rte_mempool_create((const char *)str,
2002 CTX_POOL_CACHE_SIZE, 0,
2003 NULL, NULL, NULL, NULL,
2005 if (!qp->ctx_pool) {
2006 DPAA_SEC_ERR("%s create failed\n", str);
2010 DPAA_SEC_INFO("mempool already created for dev_id : %d, qp: %d",
2011 dev->data->dev_id, qp_id);
2012 dev->data->queue_pairs[qp_id] = qp;
2017 /** Return the number of allocated queue pairs */
2019 dpaa_sec_queue_pair_count(struct rte_cryptodev *dev)
2021 PMD_INIT_FUNC_TRACE();
2023 return dev->data->nb_queue_pairs;
2026 /** Returns the size of session structure */
2028 dpaa_sec_sym_session_get_size(struct rte_cryptodev *dev __rte_unused)
2030 PMD_INIT_FUNC_TRACE();
2032 return sizeof(dpaa_sec_session);
2036 dpaa_sec_cipher_init(struct rte_cryptodev *dev __rte_unused,
2037 struct rte_crypto_sym_xform *xform,
2038 dpaa_sec_session *session)
2040 session->cipher_alg = xform->cipher.algo;
2041 session->iv.length = xform->cipher.iv.length;
2042 session->iv.offset = xform->cipher.iv.offset;
2043 session->cipher_key.data = rte_zmalloc(NULL, xform->cipher.key.length,
2044 RTE_CACHE_LINE_SIZE);
2045 if (session->cipher_key.data == NULL && xform->cipher.key.length > 0) {
2046 DPAA_SEC_ERR("No Memory for cipher key");
2049 session->cipher_key.length = xform->cipher.key.length;
2051 memcpy(session->cipher_key.data, xform->cipher.key.data,
2052 xform->cipher.key.length);
2053 switch (xform->cipher.algo) {
2054 case RTE_CRYPTO_CIPHER_AES_CBC:
2055 session->cipher_key.alg = OP_ALG_ALGSEL_AES;
2056 session->cipher_key.algmode = OP_ALG_AAI_CBC;
2058 case RTE_CRYPTO_CIPHER_3DES_CBC:
2059 session->cipher_key.alg = OP_ALG_ALGSEL_3DES;
2060 session->cipher_key.algmode = OP_ALG_AAI_CBC;
2062 case RTE_CRYPTO_CIPHER_AES_CTR:
2063 session->cipher_key.alg = OP_ALG_ALGSEL_AES;
2064 session->cipher_key.algmode = OP_ALG_AAI_CTR;
2066 case RTE_CRYPTO_CIPHER_SNOW3G_UEA2:
2067 session->cipher_key.alg = OP_ALG_ALGSEL_SNOW_F8;
2069 case RTE_CRYPTO_CIPHER_ZUC_EEA3:
2070 session->cipher_key.alg = OP_ALG_ALGSEL_ZUCE;
2073 DPAA_SEC_ERR("Crypto: Undefined Cipher specified %u",
2074 xform->cipher.algo);
2075 rte_free(session->cipher_key.data);
2078 session->dir = (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
2085 dpaa_sec_auth_init(struct rte_cryptodev *dev __rte_unused,
2086 struct rte_crypto_sym_xform *xform,
2087 dpaa_sec_session *session)
2089 session->auth_alg = xform->auth.algo;
2090 session->auth_key.data = rte_zmalloc(NULL, xform->auth.key.length,
2091 RTE_CACHE_LINE_SIZE);
2092 if (session->auth_key.data == NULL && xform->auth.key.length > 0) {
2093 DPAA_SEC_ERR("No Memory for auth key");
2096 session->auth_key.length = xform->auth.key.length;
2097 session->digest_length = xform->auth.digest_length;
2098 if (session->cipher_alg == RTE_CRYPTO_CIPHER_NULL) {
2099 session->iv.offset = xform->auth.iv.offset;
2100 session->iv.length = xform->auth.iv.length;
2103 memcpy(session->auth_key.data, xform->auth.key.data,
2104 xform->auth.key.length);
2106 switch (xform->auth.algo) {
2107 case RTE_CRYPTO_AUTH_SHA1_HMAC:
2108 session->auth_key.alg = OP_ALG_ALGSEL_SHA1;
2109 session->auth_key.algmode = OP_ALG_AAI_HMAC;
2111 case RTE_CRYPTO_AUTH_MD5_HMAC:
2112 session->auth_key.alg = OP_ALG_ALGSEL_MD5;
2113 session->auth_key.algmode = OP_ALG_AAI_HMAC;
2115 case RTE_CRYPTO_AUTH_SHA224_HMAC:
2116 session->auth_key.alg = OP_ALG_ALGSEL_SHA224;
2117 session->auth_key.algmode = OP_ALG_AAI_HMAC;
2119 case RTE_CRYPTO_AUTH_SHA256_HMAC:
2120 session->auth_key.alg = OP_ALG_ALGSEL_SHA256;
2121 session->auth_key.algmode = OP_ALG_AAI_HMAC;
2123 case RTE_CRYPTO_AUTH_SHA384_HMAC:
2124 session->auth_key.alg = OP_ALG_ALGSEL_SHA384;
2125 session->auth_key.algmode = OP_ALG_AAI_HMAC;
2127 case RTE_CRYPTO_AUTH_SHA512_HMAC:
2128 session->auth_key.alg = OP_ALG_ALGSEL_SHA512;
2129 session->auth_key.algmode = OP_ALG_AAI_HMAC;
2131 case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
2132 session->auth_key.alg = OP_ALG_ALGSEL_SNOW_F9;
2133 session->auth_key.algmode = OP_ALG_AAI_F9;
2135 case RTE_CRYPTO_AUTH_ZUC_EIA3:
2136 session->auth_key.alg = OP_ALG_ALGSEL_ZUCA;
2137 session->auth_key.algmode = OP_ALG_AAI_F9;
2140 DPAA_SEC_ERR("Crypto: Unsupported Auth specified %u",
2142 rte_free(session->auth_key.data);
2146 session->dir = (xform->auth.op == RTE_CRYPTO_AUTH_OP_GENERATE) ?
2153 dpaa_sec_chain_init(struct rte_cryptodev *dev __rte_unused,
2154 struct rte_crypto_sym_xform *xform,
2155 dpaa_sec_session *session)
2158 struct rte_crypto_cipher_xform *cipher_xform;
2159 struct rte_crypto_auth_xform *auth_xform;
2161 if (session->auth_cipher_text) {
2162 cipher_xform = &xform->cipher;
2163 auth_xform = &xform->next->auth;
2165 cipher_xform = &xform->next->cipher;
2166 auth_xform = &xform->auth;
2169 /* Set IV parameters */
2170 session->iv.offset = cipher_xform->iv.offset;
2171 session->iv.length = cipher_xform->iv.length;
2173 session->cipher_key.data = rte_zmalloc(NULL, cipher_xform->key.length,
2174 RTE_CACHE_LINE_SIZE);
2175 if (session->cipher_key.data == NULL && cipher_xform->key.length > 0) {
2176 DPAA_SEC_ERR("No Memory for cipher key");
2179 session->cipher_key.length = cipher_xform->key.length;
2180 session->auth_key.data = rte_zmalloc(NULL, auth_xform->key.length,
2181 RTE_CACHE_LINE_SIZE);
2182 if (session->auth_key.data == NULL && auth_xform->key.length > 0) {
2183 DPAA_SEC_ERR("No Memory for auth key");
2184 rte_free(session->cipher_key.data);
2187 session->auth_key.length = auth_xform->key.length;
2188 memcpy(session->cipher_key.data, cipher_xform->key.data,
2189 cipher_xform->key.length);
2190 memcpy(session->auth_key.data, auth_xform->key.data,
2191 auth_xform->key.length);
2193 session->digest_length = auth_xform->digest_length;
2194 session->auth_alg = auth_xform->algo;
2196 switch (auth_xform->algo) {
2197 case RTE_CRYPTO_AUTH_SHA1_HMAC:
2198 session->auth_key.alg = OP_ALG_ALGSEL_SHA1;
2199 session->auth_key.algmode = OP_ALG_AAI_HMAC;
2201 case RTE_CRYPTO_AUTH_MD5_HMAC:
2202 session->auth_key.alg = OP_ALG_ALGSEL_MD5;
2203 session->auth_key.algmode = OP_ALG_AAI_HMAC;
2205 case RTE_CRYPTO_AUTH_SHA224_HMAC:
2206 session->auth_key.alg = OP_ALG_ALGSEL_SHA224;
2207 session->auth_key.algmode = OP_ALG_AAI_HMAC;
2209 case RTE_CRYPTO_AUTH_SHA256_HMAC:
2210 session->auth_key.alg = OP_ALG_ALGSEL_SHA256;
2211 session->auth_key.algmode = OP_ALG_AAI_HMAC;
2213 case RTE_CRYPTO_AUTH_SHA384_HMAC:
2214 session->auth_key.alg = OP_ALG_ALGSEL_SHA384;
2215 session->auth_key.algmode = OP_ALG_AAI_HMAC;
2217 case RTE_CRYPTO_AUTH_SHA512_HMAC:
2218 session->auth_key.alg = OP_ALG_ALGSEL_SHA512;
2219 session->auth_key.algmode = OP_ALG_AAI_HMAC;
2222 DPAA_SEC_ERR("Crypto: Unsupported Auth specified %u",
2227 session->cipher_alg = cipher_xform->algo;
2229 switch (cipher_xform->algo) {
2230 case RTE_CRYPTO_CIPHER_AES_CBC:
2231 session->cipher_key.alg = OP_ALG_ALGSEL_AES;
2232 session->cipher_key.algmode = OP_ALG_AAI_CBC;
2234 case RTE_CRYPTO_CIPHER_3DES_CBC:
2235 session->cipher_key.alg = OP_ALG_ALGSEL_3DES;
2236 session->cipher_key.algmode = OP_ALG_AAI_CBC;
2238 case RTE_CRYPTO_CIPHER_AES_CTR:
2239 session->cipher_key.alg = OP_ALG_ALGSEL_AES;
2240 session->cipher_key.algmode = OP_ALG_AAI_CTR;
2243 DPAA_SEC_ERR("Crypto: Undefined Cipher specified %u",
2244 cipher_xform->algo);
2247 session->dir = (cipher_xform->op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
2252 rte_free(session->cipher_key.data);
2253 rte_free(session->auth_key.data);
2258 dpaa_sec_aead_init(struct rte_cryptodev *dev __rte_unused,
2259 struct rte_crypto_sym_xform *xform,
2260 dpaa_sec_session *session)
2262 session->aead_alg = xform->aead.algo;
2263 session->ctxt = DPAA_SEC_AEAD;
2264 session->iv.length = xform->aead.iv.length;
2265 session->iv.offset = xform->aead.iv.offset;
2266 session->auth_only_len = xform->aead.aad_length;
2267 session->aead_key.data = rte_zmalloc(NULL, xform->aead.key.length,
2268 RTE_CACHE_LINE_SIZE);
2269 if (session->aead_key.data == NULL && xform->aead.key.length > 0) {
2270 DPAA_SEC_ERR("No Memory for aead key\n");
2273 session->aead_key.length = xform->aead.key.length;
2274 session->digest_length = xform->aead.digest_length;
2276 memcpy(session->aead_key.data, xform->aead.key.data,
2277 xform->aead.key.length);
2279 switch (session->aead_alg) {
2280 case RTE_CRYPTO_AEAD_AES_GCM:
2281 session->aead_key.alg = OP_ALG_ALGSEL_AES;
2282 session->aead_key.algmode = OP_ALG_AAI_GCM;
2285 DPAA_SEC_ERR("unsupported AEAD alg %d", session->aead_alg);
2286 rte_free(session->aead_key.data);
2290 session->dir = (xform->aead.op == RTE_CRYPTO_AEAD_OP_ENCRYPT) ?
2296 static struct qman_fq *
2297 dpaa_sec_attach_rxq(struct dpaa_sec_dev_private *qi)
2301 for (i = 0; i < qi->max_nb_sessions * MAX_DPAA_CORES; i++) {
2302 if (qi->inq_attach[i] == 0) {
2303 qi->inq_attach[i] = 1;
2307 DPAA_SEC_WARN("All session in use %u", qi->max_nb_sessions);
2313 dpaa_sec_detach_rxq(struct dpaa_sec_dev_private *qi, struct qman_fq *fq)
2317 for (i = 0; i < qi->max_nb_sessions; i++) {
2318 if (&qi->inq[i] == fq) {
2319 qman_retire_fq(fq, NULL);
2321 qi->inq_attach[i] = 0;
2329 dpaa_sec_attach_sess_q(struct dpaa_sec_qp *qp, dpaa_sec_session *sess)
2333 sess->qp[rte_lcore_id() % MAX_DPAA_CORES] = qp;
2334 ret = dpaa_sec_prep_cdb(sess);
2336 DPAA_SEC_ERR("Unable to prepare sec cdb");
2339 if (unlikely(!RTE_PER_LCORE(dpaa_io))) {
2340 ret = rte_dpaa_portal_init((void *)0);
2342 DPAA_SEC_ERR("Failure in affining portal");
2346 ret = dpaa_sec_init_rx(sess->inq[rte_lcore_id() % MAX_DPAA_CORES],
2347 dpaa_mem_vtop(&sess->cdb),
2348 qman_fq_fqid(&qp->outq));
2350 DPAA_SEC_ERR("Unable to init sec queue");
2356 dpaa_sec_set_session_parameters(struct rte_cryptodev *dev,
2357 struct rte_crypto_sym_xform *xform, void *sess)
2359 struct dpaa_sec_dev_private *internals = dev->data->dev_private;
2360 dpaa_sec_session *session = sess;
2363 PMD_INIT_FUNC_TRACE();
2365 if (unlikely(sess == NULL)) {
2366 DPAA_SEC_ERR("invalid session struct");
2369 memset(session, 0, sizeof(dpaa_sec_session));
2371 /* Default IV length = 0 */
2372 session->iv.length = 0;
2375 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER && xform->next == NULL) {
2376 session->auth_alg = RTE_CRYPTO_AUTH_NULL;
2377 session->ctxt = DPAA_SEC_CIPHER;
2378 dpaa_sec_cipher_init(dev, xform, session);
2380 /* Authentication Only */
2381 } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
2382 xform->next == NULL) {
2383 session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
2384 session->ctxt = DPAA_SEC_AUTH;
2385 dpaa_sec_auth_init(dev, xform, session);
2387 /* Cipher then Authenticate */
2388 } else if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
2389 xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
2390 if (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) {
2391 session->ctxt = DPAA_SEC_CIPHER_HASH;
2392 session->auth_cipher_text = 1;
2393 dpaa_sec_chain_init(dev, xform, session);
2395 DPAA_SEC_ERR("Not supported: Auth then Cipher");
2398 /* Authenticate then Cipher */
2399 } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
2400 xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
2401 if (xform->next->cipher.op == RTE_CRYPTO_CIPHER_OP_DECRYPT) {
2402 session->ctxt = DPAA_SEC_CIPHER_HASH;
2403 session->auth_cipher_text = 0;
2404 dpaa_sec_chain_init(dev, xform, session);
2406 DPAA_SEC_ERR("Not supported: Auth then Cipher");
2410 /* AEAD operation for AES-GCM kind of Algorithms */
2411 } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD &&
2412 xform->next == NULL) {
2413 dpaa_sec_aead_init(dev, xform, session);
2416 DPAA_SEC_ERR("Invalid crypto type");
2419 rte_spinlock_lock(&internals->lock);
2420 for (i = 0; i < MAX_DPAA_CORES; i++) {
2421 session->inq[i] = dpaa_sec_attach_rxq(internals);
2422 if (session->inq[i] == NULL) {
2423 DPAA_SEC_ERR("unable to attach sec queue");
2424 rte_spinlock_unlock(&internals->lock);
2428 rte_spinlock_unlock(&internals->lock);
2433 rte_free(session->cipher_key.data);
2434 rte_free(session->auth_key.data);
2435 memset(session, 0, sizeof(dpaa_sec_session));
2441 dpaa_sec_sym_session_configure(struct rte_cryptodev *dev,
2442 struct rte_crypto_sym_xform *xform,
2443 struct rte_cryptodev_sym_session *sess,
2444 struct rte_mempool *mempool)
2446 void *sess_private_data;
2449 PMD_INIT_FUNC_TRACE();
2451 if (rte_mempool_get(mempool, &sess_private_data)) {
2452 DPAA_SEC_ERR("Couldn't get object from session mempool");
2456 ret = dpaa_sec_set_session_parameters(dev, xform, sess_private_data);
2458 DPAA_SEC_ERR("failed to configure session parameters");
2460 /* Return session to mempool */
2461 rte_mempool_put(mempool, sess_private_data);
2465 set_sym_session_private_data(sess, dev->driver_id,
2473 free_session_memory(struct rte_cryptodev *dev, dpaa_sec_session *s)
2475 struct dpaa_sec_dev_private *qi = dev->data->dev_private;
2476 struct rte_mempool *sess_mp = rte_mempool_from_obj((void *)s);
2479 for (i = 0; i < MAX_DPAA_CORES; i++) {
2481 dpaa_sec_detach_rxq(qi, s->inq[i]);
2485 rte_free(s->cipher_key.data);
2486 rte_free(s->auth_key.data);
2487 memset(s, 0, sizeof(dpaa_sec_session));
2488 rte_mempool_put(sess_mp, (void *)s);
2491 /** Clear the memory of session so it doesn't leave key material behind */
2493 dpaa_sec_sym_session_clear(struct rte_cryptodev *dev,
2494 struct rte_cryptodev_sym_session *sess)
2496 PMD_INIT_FUNC_TRACE();
2497 uint8_t index = dev->driver_id;
2498 void *sess_priv = get_sym_session_private_data(sess, index);
2499 dpaa_sec_session *s = (dpaa_sec_session *)sess_priv;
2502 free_session_memory(dev, s);
2503 set_sym_session_private_data(sess, index, NULL);
2507 #ifdef RTE_LIBRTE_SECURITY
2509 dpaa_sec_set_ipsec_session(__rte_unused struct rte_cryptodev *dev,
2510 struct rte_security_session_conf *conf,
2513 struct dpaa_sec_dev_private *internals = dev->data->dev_private;
2514 struct rte_security_ipsec_xform *ipsec_xform = &conf->ipsec;
2515 struct rte_crypto_auth_xform *auth_xform = NULL;
2516 struct rte_crypto_cipher_xform *cipher_xform = NULL;
2517 dpaa_sec_session *session = (dpaa_sec_session *)sess;
2520 PMD_INIT_FUNC_TRACE();
2522 memset(session, 0, sizeof(dpaa_sec_session));
2523 if (ipsec_xform->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
2524 cipher_xform = &conf->crypto_xform->cipher;
2525 if (conf->crypto_xform->next)
2526 auth_xform = &conf->crypto_xform->next->auth;
2528 auth_xform = &conf->crypto_xform->auth;
2529 if (conf->crypto_xform->next)
2530 cipher_xform = &conf->crypto_xform->next->cipher;
2532 session->proto_alg = conf->protocol;
2533 session->ctxt = DPAA_SEC_IPSEC;
2535 if (cipher_xform && cipher_xform->algo != RTE_CRYPTO_CIPHER_NULL) {
2536 session->cipher_key.data = rte_zmalloc(NULL,
2537 cipher_xform->key.length,
2538 RTE_CACHE_LINE_SIZE);
2539 if (session->cipher_key.data == NULL &&
2540 cipher_xform->key.length > 0) {
2541 DPAA_SEC_ERR("No Memory for cipher key");
2544 memcpy(session->cipher_key.data, cipher_xform->key.data,
2545 cipher_xform->key.length);
2546 session->cipher_key.length = cipher_xform->key.length;
2548 switch (cipher_xform->algo) {
2549 case RTE_CRYPTO_CIPHER_NULL:
2550 session->cipher_key.alg = OP_PCL_IPSEC_NULL;
2552 case RTE_CRYPTO_CIPHER_AES_CBC:
2553 session->cipher_key.alg = OP_PCL_IPSEC_AES_CBC;
2554 session->cipher_key.algmode = OP_ALG_AAI_CBC;
2556 case RTE_CRYPTO_CIPHER_3DES_CBC:
2557 session->cipher_key.alg = OP_PCL_IPSEC_3DES;
2558 session->cipher_key.algmode = OP_ALG_AAI_CBC;
2560 case RTE_CRYPTO_CIPHER_AES_CTR:
2561 session->cipher_key.alg = OP_PCL_IPSEC_AES_CTR;
2562 session->cipher_key.algmode = OP_ALG_AAI_CTR;
2565 DPAA_SEC_ERR("Crypto: Unsupported Cipher alg %u",
2566 cipher_xform->algo);
2569 session->cipher_alg = cipher_xform->algo;
2571 session->cipher_key.data = NULL;
2572 session->cipher_key.length = 0;
2573 session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
2576 if (auth_xform && auth_xform->algo != RTE_CRYPTO_AUTH_NULL) {
2577 session->auth_key.data = rte_zmalloc(NULL,
2578 auth_xform->key.length,
2579 RTE_CACHE_LINE_SIZE);
2580 if (session->auth_key.data == NULL &&
2581 auth_xform->key.length > 0) {
2582 DPAA_SEC_ERR("No Memory for auth key");
2583 rte_free(session->cipher_key.data);
2586 memcpy(session->auth_key.data, auth_xform->key.data,
2587 auth_xform->key.length);
2588 session->auth_key.length = auth_xform->key.length;
2590 switch (auth_xform->algo) {
2591 case RTE_CRYPTO_AUTH_NULL:
2592 session->auth_key.alg = OP_PCL_IPSEC_HMAC_NULL;
2593 session->digest_length = 0;
2595 case RTE_CRYPTO_AUTH_MD5_HMAC:
2596 session->auth_key.alg = OP_PCL_IPSEC_HMAC_MD5_96;
2597 session->auth_key.algmode = OP_ALG_AAI_HMAC;
2599 case RTE_CRYPTO_AUTH_SHA1_HMAC:
2600 session->auth_key.alg = OP_PCL_IPSEC_HMAC_SHA1_96;
2601 session->auth_key.algmode = OP_ALG_AAI_HMAC;
2603 case RTE_CRYPTO_AUTH_SHA224_HMAC:
2604 session->auth_key.alg = OP_PCL_IPSEC_HMAC_SHA1_160;
2605 session->auth_key.algmode = OP_ALG_AAI_HMAC;
2607 case RTE_CRYPTO_AUTH_SHA256_HMAC:
2608 session->auth_key.alg = OP_PCL_IPSEC_HMAC_SHA2_256_128;
2609 session->auth_key.algmode = OP_ALG_AAI_HMAC;
2611 case RTE_CRYPTO_AUTH_SHA384_HMAC:
2612 session->auth_key.alg = OP_PCL_IPSEC_HMAC_SHA2_384_192;
2613 session->auth_key.algmode = OP_ALG_AAI_HMAC;
2615 case RTE_CRYPTO_AUTH_SHA512_HMAC:
2616 session->auth_key.alg = OP_PCL_IPSEC_HMAC_SHA2_512_256;
2617 session->auth_key.algmode = OP_ALG_AAI_HMAC;
2620 DPAA_SEC_ERR("Crypto: Unsupported auth alg %u",
2624 session->auth_alg = auth_xform->algo;
2626 session->auth_key.data = NULL;
2627 session->auth_key.length = 0;
2628 session->auth_alg = RTE_CRYPTO_AUTH_NULL;
2631 if (ipsec_xform->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
2632 if (ipsec_xform->tunnel.type ==
2633 RTE_SECURITY_IPSEC_TUNNEL_IPV4) {
2634 memset(&session->encap_pdb, 0,
2635 sizeof(struct ipsec_encap_pdb) +
2636 sizeof(session->ip4_hdr));
2637 session->ip4_hdr.ip_v = IPVERSION;
2638 session->ip4_hdr.ip_hl = 5;
2639 session->ip4_hdr.ip_len = rte_cpu_to_be_16(
2640 sizeof(session->ip4_hdr));
2641 session->ip4_hdr.ip_tos = ipsec_xform->tunnel.ipv4.dscp;
2642 session->ip4_hdr.ip_id = 0;
2643 session->ip4_hdr.ip_off = 0;
2644 session->ip4_hdr.ip_ttl = ipsec_xform->tunnel.ipv4.ttl;
2645 session->ip4_hdr.ip_p = (ipsec_xform->proto ==
2646 RTE_SECURITY_IPSEC_SA_PROTO_ESP) ?
2647 IPPROTO_ESP : IPPROTO_AH;
2648 session->ip4_hdr.ip_sum = 0;
2649 session->ip4_hdr.ip_src =
2650 ipsec_xform->tunnel.ipv4.src_ip;
2651 session->ip4_hdr.ip_dst =
2652 ipsec_xform->tunnel.ipv4.dst_ip;
2653 session->ip4_hdr.ip_sum = calc_chksum((uint16_t *)
2654 (void *)&session->ip4_hdr,
2656 session->encap_pdb.ip_hdr_len = sizeof(struct ip);
2657 } else if (ipsec_xform->tunnel.type ==
2658 RTE_SECURITY_IPSEC_TUNNEL_IPV6) {
2659 memset(&session->encap_pdb, 0,
2660 sizeof(struct ipsec_encap_pdb) +
2661 sizeof(session->ip6_hdr));
2662 session->ip6_hdr.vtc_flow = rte_cpu_to_be_32(
2663 DPAA_IPv6_DEFAULT_VTC_FLOW |
2664 ((ipsec_xform->tunnel.ipv6.dscp <<
2665 RTE_IPV6_HDR_TC_SHIFT) &
2666 RTE_IPV6_HDR_TC_MASK) |
2667 ((ipsec_xform->tunnel.ipv6.flabel <<
2668 RTE_IPV6_HDR_FL_SHIFT) &
2669 RTE_IPV6_HDR_FL_MASK));
2670 /* Payload length will be updated by HW */
2671 session->ip6_hdr.payload_len = 0;
2672 session->ip6_hdr.hop_limits =
2673 ipsec_xform->tunnel.ipv6.hlimit;
2674 session->ip6_hdr.proto = (ipsec_xform->proto ==
2675 RTE_SECURITY_IPSEC_SA_PROTO_ESP) ?
2676 IPPROTO_ESP : IPPROTO_AH;
2677 memcpy(&session->ip6_hdr.src_addr,
2678 &ipsec_xform->tunnel.ipv6.src_addr, 16);
2679 memcpy(&session->ip6_hdr.dst_addr,
2680 &ipsec_xform->tunnel.ipv6.dst_addr, 16);
2681 session->encap_pdb.ip_hdr_len =
2682 sizeof(struct rte_ipv6_hdr);
2684 session->encap_pdb.options =
2685 (IPVERSION << PDBNH_ESP_ENCAP_SHIFT) |
2686 PDBOPTS_ESP_OIHI_PDB_INL |
2688 PDBHMO_ESP_ENCAP_DTTL |
2690 if (ipsec_xform->options.esn)
2691 session->encap_pdb.options |= PDBOPTS_ESP_ESN;
2692 session->encap_pdb.spi = ipsec_xform->spi;
2693 session->dir = DIR_ENC;
2694 } else if (ipsec_xform->direction ==
2695 RTE_SECURITY_IPSEC_SA_DIR_INGRESS) {
2696 memset(&session->decap_pdb, 0, sizeof(struct ipsec_decap_pdb));
2697 if (ipsec_xform->tunnel.type == RTE_SECURITY_IPSEC_TUNNEL_IPV4)
2698 session->decap_pdb.options = sizeof(struct ip) << 16;
2700 session->decap_pdb.options =
2701 sizeof(struct rte_ipv6_hdr) << 16;
2702 if (ipsec_xform->options.esn)
2703 session->decap_pdb.options |= PDBOPTS_ESP_ESN;
2704 session->dir = DIR_DEC;
2707 rte_spinlock_lock(&internals->lock);
2708 for (i = 0; i < MAX_DPAA_CORES; i++) {
2709 session->inq[i] = dpaa_sec_attach_rxq(internals);
2710 if (session->inq[i] == NULL) {
2711 DPAA_SEC_ERR("unable to attach sec queue");
2712 rte_spinlock_unlock(&internals->lock);
2716 rte_spinlock_unlock(&internals->lock);
2720 rte_free(session->auth_key.data);
2721 rte_free(session->cipher_key.data);
2722 memset(session, 0, sizeof(dpaa_sec_session));
2727 dpaa_sec_set_pdcp_session(struct rte_cryptodev *dev,
2728 struct rte_security_session_conf *conf,
2731 struct rte_security_pdcp_xform *pdcp_xform = &conf->pdcp;
2732 struct rte_crypto_sym_xform *xform = conf->crypto_xform;
2733 struct rte_crypto_auth_xform *auth_xform = NULL;
2734 struct rte_crypto_cipher_xform *cipher_xform = NULL;
2735 dpaa_sec_session *session = (dpaa_sec_session *)sess;
2736 struct dpaa_sec_dev_private *dev_priv = dev->data->dev_private;
2739 PMD_INIT_FUNC_TRACE();
2741 memset(session, 0, sizeof(dpaa_sec_session));
2743 /* find xfrm types */
2744 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
2745 cipher_xform = &xform->cipher;
2746 if (xform->next != NULL)
2747 auth_xform = &xform->next->auth;
2748 } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
2749 auth_xform = &xform->auth;
2750 if (xform->next != NULL)
2751 cipher_xform = &xform->next->cipher;
2753 DPAA_SEC_ERR("Invalid crypto type");
2757 session->proto_alg = conf->protocol;
2758 session->ctxt = DPAA_SEC_PDCP;
2761 switch (cipher_xform->algo) {
2762 case RTE_CRYPTO_CIPHER_SNOW3G_UEA2:
2763 session->cipher_key.alg = PDCP_CIPHER_TYPE_SNOW;
2765 case RTE_CRYPTO_CIPHER_ZUC_EEA3:
2766 session->cipher_key.alg = PDCP_CIPHER_TYPE_ZUC;
2768 case RTE_CRYPTO_CIPHER_AES_CTR:
2769 session->cipher_key.alg = PDCP_CIPHER_TYPE_AES;
2771 case RTE_CRYPTO_CIPHER_NULL:
2772 session->cipher_key.alg = PDCP_CIPHER_TYPE_NULL;
2775 DPAA_SEC_ERR("Crypto: Undefined Cipher specified %u",
2776 session->cipher_alg);
2780 session->cipher_key.data = rte_zmalloc(NULL,
2781 cipher_xform->key.length,
2782 RTE_CACHE_LINE_SIZE);
2783 if (session->cipher_key.data == NULL &&
2784 cipher_xform->key.length > 0) {
2785 DPAA_SEC_ERR("No Memory for cipher key");
2788 session->cipher_key.length = cipher_xform->key.length;
2789 memcpy(session->cipher_key.data, cipher_xform->key.data,
2790 cipher_xform->key.length);
2791 session->dir = (cipher_xform->op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
2793 session->cipher_alg = cipher_xform->algo;
2795 session->cipher_key.data = NULL;
2796 session->cipher_key.length = 0;
2797 session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
2798 session->dir = DIR_ENC;
2801 if (pdcp_xform->domain == RTE_SECURITY_PDCP_MODE_CONTROL) {
2802 if (pdcp_xform->sn_size != RTE_SECURITY_PDCP_SN_SIZE_5 &&
2803 pdcp_xform->sn_size != RTE_SECURITY_PDCP_SN_SIZE_12) {
2805 "PDCP Seq Num size should be 5/12 bits for cmode");
2811 switch (auth_xform->algo) {
2812 case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
2813 session->auth_key.alg = PDCP_AUTH_TYPE_SNOW;
2815 case RTE_CRYPTO_AUTH_ZUC_EIA3:
2816 session->auth_key.alg = PDCP_AUTH_TYPE_ZUC;
2818 case RTE_CRYPTO_AUTH_AES_CMAC:
2819 session->auth_key.alg = PDCP_AUTH_TYPE_AES;
2821 case RTE_CRYPTO_AUTH_NULL:
2822 session->auth_key.alg = PDCP_AUTH_TYPE_NULL;
2825 DPAA_SEC_ERR("Crypto: Unsupported auth alg %u",
2827 rte_free(session->cipher_key.data);
2830 session->auth_key.data = rte_zmalloc(NULL,
2831 auth_xform->key.length,
2832 RTE_CACHE_LINE_SIZE);
2833 if (!session->auth_key.data &&
2834 auth_xform->key.length > 0) {
2835 DPAA_SEC_ERR("No Memory for auth key");
2836 rte_free(session->cipher_key.data);
2839 session->auth_key.length = auth_xform->key.length;
2840 memcpy(session->auth_key.data, auth_xform->key.data,
2841 auth_xform->key.length);
2842 session->auth_alg = auth_xform->algo;
2844 session->auth_key.data = NULL;
2845 session->auth_key.length = 0;
2846 session->auth_alg = 0;
2848 session->pdcp.domain = pdcp_xform->domain;
2849 session->pdcp.bearer = pdcp_xform->bearer;
2850 session->pdcp.pkt_dir = pdcp_xform->pkt_dir;
2851 session->pdcp.sn_size = pdcp_xform->sn_size;
2852 session->pdcp.hfn = pdcp_xform->hfn;
2853 session->pdcp.hfn_threshold = pdcp_xform->hfn_threshold;
2854 session->pdcp.hfn_ovd = pdcp_xform->hfn_ovrd;
2855 session->pdcp.hfn_ovd_offset = cipher_xform->iv.offset;
2857 rte_spinlock_lock(&dev_priv->lock);
2858 for (i = 0; i < MAX_DPAA_CORES; i++) {
2859 session->inq[i] = dpaa_sec_attach_rxq(dev_priv);
2860 if (session->inq[i] == NULL) {
2861 DPAA_SEC_ERR("unable to attach sec queue");
2862 rte_spinlock_unlock(&dev_priv->lock);
2866 rte_spinlock_unlock(&dev_priv->lock);
2869 rte_free(session->auth_key.data);
2870 rte_free(session->cipher_key.data);
2871 memset(session, 0, sizeof(dpaa_sec_session));
2876 dpaa_sec_security_session_create(void *dev,
2877 struct rte_security_session_conf *conf,
2878 struct rte_security_session *sess,
2879 struct rte_mempool *mempool)
2881 void *sess_private_data;
2882 struct rte_cryptodev *cdev = (struct rte_cryptodev *)dev;
2885 if (rte_mempool_get(mempool, &sess_private_data)) {
2886 DPAA_SEC_ERR("Couldn't get object from session mempool");
2890 switch (conf->protocol) {
2891 case RTE_SECURITY_PROTOCOL_IPSEC:
2892 ret = dpaa_sec_set_ipsec_session(cdev, conf,
2895 case RTE_SECURITY_PROTOCOL_PDCP:
2896 ret = dpaa_sec_set_pdcp_session(cdev, conf,
2899 case RTE_SECURITY_PROTOCOL_MACSEC:
2905 DPAA_SEC_ERR("failed to configure session parameters");
2906 /* Return session to mempool */
2907 rte_mempool_put(mempool, sess_private_data);
2911 set_sec_session_private_data(sess, sess_private_data);
2916 /** Clear the memory of session so it doesn't leave key material behind */
2918 dpaa_sec_security_session_destroy(void *dev __rte_unused,
2919 struct rte_security_session *sess)
2921 PMD_INIT_FUNC_TRACE();
2922 void *sess_priv = get_sec_session_private_data(sess);
2923 dpaa_sec_session *s = (dpaa_sec_session *)sess_priv;
2926 free_session_memory((struct rte_cryptodev *)dev, s);
2927 set_sec_session_private_data(sess, NULL);
2933 dpaa_sec_dev_configure(struct rte_cryptodev *dev __rte_unused,
2934 struct rte_cryptodev_config *config __rte_unused)
2936 PMD_INIT_FUNC_TRACE();
2942 dpaa_sec_dev_start(struct rte_cryptodev *dev __rte_unused)
2944 PMD_INIT_FUNC_TRACE();
2949 dpaa_sec_dev_stop(struct rte_cryptodev *dev __rte_unused)
2951 PMD_INIT_FUNC_TRACE();
2955 dpaa_sec_dev_close(struct rte_cryptodev *dev)
2957 PMD_INIT_FUNC_TRACE();
2966 dpaa_sec_dev_infos_get(struct rte_cryptodev *dev,
2967 struct rte_cryptodev_info *info)
2969 struct dpaa_sec_dev_private *internals = dev->data->dev_private;
2971 PMD_INIT_FUNC_TRACE();
2973 info->max_nb_queue_pairs = internals->max_nb_queue_pairs;
2974 info->feature_flags = dev->feature_flags;
2975 info->capabilities = dpaa_sec_capabilities;
2976 info->sym.max_nb_sessions = internals->max_nb_sessions;
2977 info->driver_id = cryptodev_driver_id;
2981 static enum qman_cb_dqrr_result
2982 dpaa_sec_process_parallel_event(void *event,
2983 struct qman_portal *qm __always_unused,
2984 struct qman_fq *outq,
2985 const struct qm_dqrr_entry *dqrr,
2988 const struct qm_fd *fd;
2989 struct dpaa_sec_job *job;
2990 struct dpaa_sec_op_ctx *ctx;
2991 struct rte_event *ev = (struct rte_event *)event;
2995 /* sg is embedded in an op ctx,
2996 * sg[0] is for output
2999 job = dpaa_mem_ptov(qm_fd_addr_get64(fd));
3001 ctx = container_of(job, struct dpaa_sec_op_ctx, job);
3002 ctx->fd_status = fd->status;
3003 if (ctx->op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
3004 struct qm_sg_entry *sg_out;
3007 sg_out = &job->sg[0];
3008 hw_sg_to_cpu(sg_out);
3009 len = sg_out->length;
3010 ctx->op->sym->m_src->pkt_len = len;
3011 ctx->op->sym->m_src->data_len = len;
3013 if (!ctx->fd_status) {
3014 ctx->op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
3016 DPAA_SEC_DP_WARN("SEC return err: 0x%x", ctx->fd_status);
3017 ctx->op->status = RTE_CRYPTO_OP_STATUS_ERROR;
3019 ev->event_ptr = (void *)ctx->op;
3021 ev->flow_id = outq->ev.flow_id;
3022 ev->sub_event_type = outq->ev.sub_event_type;
3023 ev->event_type = RTE_EVENT_TYPE_CRYPTODEV;
3024 ev->op = RTE_EVENT_OP_NEW;
3025 ev->sched_type = outq->ev.sched_type;
3026 ev->queue_id = outq->ev.queue_id;
3027 ev->priority = outq->ev.priority;
3028 *bufs = (void *)ctx->op;
3030 rte_mempool_put(ctx->ctx_pool, (void *)ctx);
3032 return qman_cb_dqrr_consume;
3035 static enum qman_cb_dqrr_result
3036 dpaa_sec_process_atomic_event(void *event,
3037 struct qman_portal *qm __rte_unused,
3038 struct qman_fq *outq,
3039 const struct qm_dqrr_entry *dqrr,
3043 const struct qm_fd *fd;
3044 struct dpaa_sec_job *job;
3045 struct dpaa_sec_op_ctx *ctx;
3046 struct rte_event *ev = (struct rte_event *)event;
3050 /* sg is embedded in an op ctx,
3051 * sg[0] is for output
3054 job = dpaa_mem_ptov(qm_fd_addr_get64(fd));
3056 ctx = container_of(job, struct dpaa_sec_op_ctx, job);
3057 ctx->fd_status = fd->status;
3058 if (ctx->op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
3059 struct qm_sg_entry *sg_out;
3062 sg_out = &job->sg[0];
3063 hw_sg_to_cpu(sg_out);
3064 len = sg_out->length;
3065 ctx->op->sym->m_src->pkt_len = len;
3066 ctx->op->sym->m_src->data_len = len;
3068 if (!ctx->fd_status) {
3069 ctx->op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
3071 DPAA_SEC_DP_WARN("SEC return err: 0x%x", ctx->fd_status);
3072 ctx->op->status = RTE_CRYPTO_OP_STATUS_ERROR;
3074 ev->event_ptr = (void *)ctx->op;
3075 ev->flow_id = outq->ev.flow_id;
3076 ev->sub_event_type = outq->ev.sub_event_type;
3077 ev->event_type = RTE_EVENT_TYPE_CRYPTODEV;
3078 ev->op = RTE_EVENT_OP_NEW;
3079 ev->sched_type = outq->ev.sched_type;
3080 ev->queue_id = outq->ev.queue_id;
3081 ev->priority = outq->ev.priority;
3083 /* Save active dqrr entries */
3084 index = ((uintptr_t)dqrr >> 6) & (16/*QM_DQRR_SIZE*/ - 1);
3085 DPAA_PER_LCORE_DQRR_SIZE++;
3086 DPAA_PER_LCORE_DQRR_HELD |= 1 << index;
3087 DPAA_PER_LCORE_DQRR_MBUF(index) = ctx->op->sym->m_src;
3088 ev->impl_opaque = index + 1;
3089 ctx->op->sym->m_src->seqn = (uint32_t)index + 1;
3090 *bufs = (void *)ctx->op;
3092 rte_mempool_put(ctx->ctx_pool, (void *)ctx);
3094 return qman_cb_dqrr_defer;
3098 dpaa_sec_eventq_attach(const struct rte_cryptodev *dev,
3101 const struct rte_event *event)
3103 struct dpaa_sec_qp *qp = dev->data->queue_pairs[qp_id];
3104 struct qm_mcc_initfq opts = {0};
3108 opts.we_mask = QM_INITFQ_WE_DESTWQ | QM_INITFQ_WE_FQCTRL |
3109 QM_INITFQ_WE_CONTEXTA | QM_INITFQ_WE_CONTEXTB;
3110 opts.fqd.dest.channel = ch_id;
3112 switch (event->sched_type) {
3113 case RTE_SCHED_TYPE_ATOMIC:
3114 opts.fqd.fq_ctrl |= QM_FQCTRL_HOLDACTIVE;
3115 /* Reset FQCTRL_AVOIDBLOCK bit as it is unnecessary
3116 * configuration with HOLD_ACTIVE setting
3118 opts.fqd.fq_ctrl &= (~QM_FQCTRL_AVOIDBLOCK);
3119 qp->outq.cb.dqrr_dpdk_cb = dpaa_sec_process_atomic_event;
3121 case RTE_SCHED_TYPE_ORDERED:
3122 DPAA_SEC_ERR("Ordered queue schedule type is not supported\n");
3125 opts.fqd.fq_ctrl |= QM_FQCTRL_AVOIDBLOCK;
3126 qp->outq.cb.dqrr_dpdk_cb = dpaa_sec_process_parallel_event;
3130 ret = qman_init_fq(&qp->outq, QMAN_INITFQ_FLAG_SCHED, &opts);
3131 if (unlikely(ret)) {
3132 DPAA_SEC_ERR("unable to init caam source fq!");
3136 memcpy(&qp->outq.ev, event, sizeof(struct rte_event));
3142 dpaa_sec_eventq_detach(const struct rte_cryptodev *dev,
3145 struct qm_mcc_initfq opts = {0};
3147 struct dpaa_sec_qp *qp = dev->data->queue_pairs[qp_id];
3149 opts.we_mask = QM_INITFQ_WE_DESTWQ | QM_INITFQ_WE_FQCTRL |
3150 QM_INITFQ_WE_CONTEXTA | QM_INITFQ_WE_CONTEXTB;
3151 qp->outq.cb.dqrr = dqrr_out_fq_cb_rx;
3152 qp->outq.cb.ern = ern_sec_fq_handler;
3153 qman_retire_fq(&qp->outq, NULL);
3154 qman_oos_fq(&qp->outq);
3155 ret = qman_init_fq(&qp->outq, 0, &opts);
3157 RTE_LOG(ERR, PMD, "Error in qman_init_fq: ret: %d\n", ret);
3158 qp->outq.cb.dqrr = NULL;
3163 static struct rte_cryptodev_ops crypto_ops = {
3164 .dev_configure = dpaa_sec_dev_configure,
3165 .dev_start = dpaa_sec_dev_start,
3166 .dev_stop = dpaa_sec_dev_stop,
3167 .dev_close = dpaa_sec_dev_close,
3168 .dev_infos_get = dpaa_sec_dev_infos_get,
3169 .queue_pair_setup = dpaa_sec_queue_pair_setup,
3170 .queue_pair_release = dpaa_sec_queue_pair_release,
3171 .queue_pair_count = dpaa_sec_queue_pair_count,
3172 .sym_session_get_size = dpaa_sec_sym_session_get_size,
3173 .sym_session_configure = dpaa_sec_sym_session_configure,
3174 .sym_session_clear = dpaa_sec_sym_session_clear
3177 #ifdef RTE_LIBRTE_SECURITY
3178 static const struct rte_security_capability *
3179 dpaa_sec_capabilities_get(void *device __rte_unused)
3181 return dpaa_sec_security_cap;
3184 static const struct rte_security_ops dpaa_sec_security_ops = {
3185 .session_create = dpaa_sec_security_session_create,
3186 .session_update = NULL,
3187 .session_stats_get = NULL,
3188 .session_destroy = dpaa_sec_security_session_destroy,
3189 .set_pkt_metadata = NULL,
3190 .capabilities_get = dpaa_sec_capabilities_get
3194 dpaa_sec_uninit(struct rte_cryptodev *dev)
3196 struct dpaa_sec_dev_private *internals;
3201 internals = dev->data->dev_private;
3202 rte_free(dev->security_ctx);
3204 rte_free(internals);
3206 DPAA_SEC_INFO("Closing DPAA_SEC device %s on numa socket %u",
3207 dev->data->name, rte_socket_id());
3213 dpaa_sec_dev_init(struct rte_cryptodev *cryptodev)
3215 struct dpaa_sec_dev_private *internals;
3216 #ifdef RTE_LIBRTE_SECURITY
3217 struct rte_security_ctx *security_instance;
3219 struct dpaa_sec_qp *qp;
3223 PMD_INIT_FUNC_TRACE();
3225 cryptodev->driver_id = cryptodev_driver_id;
3226 cryptodev->dev_ops = &crypto_ops;
3228 cryptodev->enqueue_burst = dpaa_sec_enqueue_burst;
3229 cryptodev->dequeue_burst = dpaa_sec_dequeue_burst;
3230 cryptodev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
3231 RTE_CRYPTODEV_FF_HW_ACCELERATED |
3232 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
3233 RTE_CRYPTODEV_FF_SECURITY |
3234 RTE_CRYPTODEV_FF_IN_PLACE_SGL |
3235 RTE_CRYPTODEV_FF_OOP_SGL_IN_SGL_OUT |
3236 RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT |
3237 RTE_CRYPTODEV_FF_OOP_LB_IN_SGL_OUT |
3238 RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT;
3240 internals = cryptodev->data->dev_private;
3241 internals->max_nb_queue_pairs = RTE_DPAA_MAX_NB_SEC_QPS;
3242 internals->max_nb_sessions = RTE_DPAA_SEC_PMD_MAX_NB_SESSIONS;
3245 * For secondary processes, we don't initialise any further as primary
3246 * has already done this work. Only check we don't need a different
3249 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
3250 DPAA_SEC_WARN("Device already init by primary process");
3253 #ifdef RTE_LIBRTE_SECURITY
3254 /* Initialize security_ctx only for primary process*/
3255 security_instance = rte_malloc("rte_security_instances_ops",
3256 sizeof(struct rte_security_ctx), 0);
3257 if (security_instance == NULL)
3259 security_instance->device = (void *)cryptodev;
3260 security_instance->ops = &dpaa_sec_security_ops;
3261 security_instance->sess_cnt = 0;
3262 cryptodev->security_ctx = security_instance;
3264 rte_spinlock_init(&internals->lock);
3265 for (i = 0; i < internals->max_nb_queue_pairs; i++) {
3266 /* init qman fq for queue pair */
3267 qp = &internals->qps[i];
3268 ret = dpaa_sec_init_tx(&qp->outq);
3270 DPAA_SEC_ERR("config tx of queue pair %d", i);
3275 flags = QMAN_FQ_FLAG_LOCKED | QMAN_FQ_FLAG_DYNAMIC_FQID |
3276 QMAN_FQ_FLAG_TO_DCPORTAL;
3277 for (i = 0; i < MAX_DPAA_CORES * internals->max_nb_sessions; i++) {
3278 /* create rx qman fq for sessions*/
3279 ret = qman_create_fq(0, flags, &internals->inq[i]);
3280 if (unlikely(ret != 0)) {
3281 DPAA_SEC_ERR("sec qman_create_fq failed");
3286 RTE_LOG(INFO, PMD, "%s cryptodev init\n", cryptodev->data->name);
3290 DPAA_SEC_ERR("driver %s: create failed\n", cryptodev->data->name);
3292 dpaa_sec_uninit(cryptodev);
3297 cryptodev_dpaa_sec_probe(struct rte_dpaa_driver *dpaa_drv __rte_unused,
3298 struct rte_dpaa_device *dpaa_dev)
3300 struct rte_cryptodev *cryptodev;
3301 char cryptodev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
3305 snprintf(cryptodev_name, sizeof(cryptodev_name), "%s", dpaa_dev->name);
3307 cryptodev = rte_cryptodev_pmd_allocate(cryptodev_name, rte_socket_id());
3308 if (cryptodev == NULL)
3311 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
3312 cryptodev->data->dev_private = rte_zmalloc_socket(
3313 "cryptodev private structure",
3314 sizeof(struct dpaa_sec_dev_private),
3315 RTE_CACHE_LINE_SIZE,
3318 if (cryptodev->data->dev_private == NULL)
3319 rte_panic("Cannot allocate memzone for private "
3323 dpaa_dev->crypto_dev = cryptodev;
3324 cryptodev->device = &dpaa_dev->device;
3326 /* init user callbacks */
3327 TAILQ_INIT(&(cryptodev->link_intr_cbs));
3329 /* if sec device version is not configured */
3330 if (!rta_get_sec_era()) {
3331 const struct device_node *caam_node;
3333 for_each_compatible_node(caam_node, NULL, "fsl,sec-v4.0") {
3334 const uint32_t *prop = of_get_property(caam_node,
3339 INTL_SEC_ERA(rte_cpu_to_be_32(*prop)));
3345 if (unlikely(!RTE_PER_LCORE(dpaa_io))) {
3346 retval = rte_dpaa_portal_init((void *)1);
3348 DPAA_SEC_ERR("Unable to initialize portal");
3353 /* Invoke PMD device initialization function */
3354 retval = dpaa_sec_dev_init(cryptodev);
3358 /* In case of error, cleanup is done */
3359 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
3360 rte_free(cryptodev->data->dev_private);
3362 rte_cryptodev_pmd_release_device(cryptodev);
3368 cryptodev_dpaa_sec_remove(struct rte_dpaa_device *dpaa_dev)
3370 struct rte_cryptodev *cryptodev;
3373 cryptodev = dpaa_dev->crypto_dev;
3374 if (cryptodev == NULL)
3377 ret = dpaa_sec_uninit(cryptodev);
3381 return rte_cryptodev_pmd_destroy(cryptodev);
3384 static struct rte_dpaa_driver rte_dpaa_sec_driver = {
3385 .drv_type = FSL_DPAA_CRYPTO,
3387 .name = "DPAA SEC PMD"
3389 .probe = cryptodev_dpaa_sec_probe,
3390 .remove = cryptodev_dpaa_sec_remove,
3393 static struct cryptodev_driver dpaa_sec_crypto_drv;
3395 RTE_PMD_REGISTER_DPAA(CRYPTODEV_NAME_DPAA_SEC_PMD, rte_dpaa_sec_driver);
3396 RTE_PMD_REGISTER_CRYPTO_DRIVER(dpaa_sec_crypto_drv, rte_dpaa_sec_driver.driver,
3397 cryptodev_driver_id);
3399 RTE_INIT(dpaa_sec_init_log)
3401 dpaa_logtype_sec = rte_log_register("pmd.crypto.dpaa");
3402 if (dpaa_logtype_sec >= 0)
3403 rte_log_set_level(dpaa_logtype_sec, RTE_LOG_NOTICE);