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);
98 ern_sec_fq_handler(struct qman_portal *qm __rte_unused,
100 const struct qm_mr_entry *msg)
102 DPAA_SEC_DP_ERR("sec fq %d error, RC = %x, seqnum = %x\n",
103 fq->fqid, msg->ern.rc, msg->ern.seqnum);
106 /* initialize the queue with dest chan as caam chan so that
107 * all the packets in this queue could be dispatched into caam
110 dpaa_sec_init_rx(struct qman_fq *fq_in, rte_iova_t hwdesc,
113 struct qm_mcc_initfq fq_opts;
117 /* Clear FQ options */
118 memset(&fq_opts, 0x00, sizeof(struct qm_mcc_initfq));
120 flags = QMAN_INITFQ_FLAG_SCHED;
121 fq_opts.we_mask = QM_INITFQ_WE_DESTWQ | QM_INITFQ_WE_CONTEXTA |
122 QM_INITFQ_WE_CONTEXTB;
124 qm_fqd_context_a_set64(&fq_opts.fqd, hwdesc);
125 fq_opts.fqd.context_b = fqid_out;
126 fq_opts.fqd.dest.channel = qm_channel_caam;
127 fq_opts.fqd.dest.wq = 0;
129 fq_in->cb.ern = ern_sec_fq_handler;
131 DPAA_SEC_DEBUG("in-%x out-%x", fq_in->fqid, fqid_out);
133 ret = qman_init_fq(fq_in, flags, &fq_opts);
134 if (unlikely(ret != 0))
135 DPAA_SEC_ERR("qman_init_fq failed %d", ret);
140 /* something is put into in_fq and caam put the crypto result into out_fq */
141 static enum qman_cb_dqrr_result
142 dqrr_out_fq_cb_rx(struct qman_portal *qm __always_unused,
143 struct qman_fq *fq __always_unused,
144 const struct qm_dqrr_entry *dqrr)
146 const struct qm_fd *fd;
147 struct dpaa_sec_job *job;
148 struct dpaa_sec_op_ctx *ctx;
150 if (dpaa_sec_op_nb >= DPAA_SEC_BURST)
151 return qman_cb_dqrr_defer;
153 if (!(dqrr->stat & QM_DQRR_STAT_FD_VALID))
154 return qman_cb_dqrr_consume;
157 /* sg is embedded in an op ctx,
158 * sg[0] is for output
161 job = rte_dpaa_mem_ptov(qm_fd_addr_get64(fd));
163 ctx = container_of(job, struct dpaa_sec_op_ctx, job);
164 ctx->fd_status = fd->status;
165 if (ctx->op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
166 struct qm_sg_entry *sg_out;
168 struct rte_mbuf *mbuf = (ctx->op->sym->m_dst == NULL) ?
169 ctx->op->sym->m_src : ctx->op->sym->m_dst;
171 sg_out = &job->sg[0];
172 hw_sg_to_cpu(sg_out);
173 len = sg_out->length;
175 while (mbuf->next != NULL) {
176 len -= mbuf->data_len;
179 mbuf->data_len = len;
181 dpaa_sec_ops[dpaa_sec_op_nb++] = ctx->op;
182 dpaa_sec_op_ending(ctx);
184 return qman_cb_dqrr_consume;
187 /* caam result is put into this queue */
189 dpaa_sec_init_tx(struct qman_fq *fq)
192 struct qm_mcc_initfq opts;
195 flags = QMAN_FQ_FLAG_NO_ENQUEUE | QMAN_FQ_FLAG_LOCKED |
196 QMAN_FQ_FLAG_DYNAMIC_FQID;
198 ret = qman_create_fq(0, flags, fq);
200 DPAA_SEC_ERR("qman_create_fq failed");
204 memset(&opts, 0, sizeof(opts));
205 opts.we_mask = QM_INITFQ_WE_DESTWQ | QM_INITFQ_WE_FQCTRL |
206 QM_INITFQ_WE_CONTEXTA | QM_INITFQ_WE_CONTEXTB;
208 /* opts.fqd.dest.channel = dpaa_sec_pool_chan; */
210 fq->cb.dqrr = dqrr_out_fq_cb_rx;
211 fq->cb.ern = ern_sec_fq_handler;
213 ret = qman_init_fq(fq, 0, &opts);
215 DPAA_SEC_ERR("unable to init caam source fq!");
222 static inline int is_encode(dpaa_sec_session *ses)
224 return ses->dir == DIR_ENC;
227 static inline int is_decode(dpaa_sec_session *ses)
229 return ses->dir == DIR_DEC;
232 #ifdef RTE_LIBRTE_SECURITY
234 dpaa_sec_prep_pdcp_cdb(dpaa_sec_session *ses)
236 struct alginfo authdata = {0}, cipherdata = {0};
237 struct sec_cdb *cdb = &ses->cdb;
238 struct alginfo *p_authdata = NULL;
239 int32_t shared_desc_len = 0;
241 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
247 cipherdata.key = (size_t)ses->cipher_key.data;
248 cipherdata.keylen = ses->cipher_key.length;
249 cipherdata.key_enc_flags = 0;
250 cipherdata.key_type = RTA_DATA_IMM;
251 cipherdata.algtype = ses->cipher_key.alg;
252 cipherdata.algmode = ses->cipher_key.algmode;
254 cdb->sh_desc[0] = cipherdata.keylen;
259 authdata.key = (size_t)ses->auth_key.data;
260 authdata.keylen = ses->auth_key.length;
261 authdata.key_enc_flags = 0;
262 authdata.key_type = RTA_DATA_IMM;
263 authdata.algtype = ses->auth_key.alg;
264 authdata.algmode = ses->auth_key.algmode;
266 p_authdata = &authdata;
268 cdb->sh_desc[1] = authdata.keylen;
271 err = rta_inline_query(IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN,
273 (unsigned int *)cdb->sh_desc,
274 &cdb->sh_desc[2], 2);
276 DPAA_SEC_ERR("Crypto: Incorrect key lengths");
280 if (!(cdb->sh_desc[2] & 1) && cipherdata.keylen) {
282 (size_t)rte_dpaa_mem_vtop((void *)(size_t)cipherdata.key);
283 cipherdata.key_type = RTA_DATA_PTR;
285 if (!(cdb->sh_desc[2] & (1 << 1)) && authdata.keylen) {
287 (size_t)rte_dpaa_mem_vtop((void *)(size_t)authdata.key);
288 authdata.key_type = RTA_DATA_PTR;
295 if (ses->pdcp.domain == RTE_SECURITY_PDCP_MODE_CONTROL) {
296 if (ses->dir == DIR_ENC)
297 shared_desc_len = cnstr_shdsc_pdcp_c_plane_encap(
298 cdb->sh_desc, 1, swap,
303 ses->pdcp.hfn_threshold,
304 &cipherdata, &authdata,
306 else if (ses->dir == DIR_DEC)
307 shared_desc_len = cnstr_shdsc_pdcp_c_plane_decap(
308 cdb->sh_desc, 1, swap,
313 ses->pdcp.hfn_threshold,
314 &cipherdata, &authdata,
317 if (ses->dir == DIR_ENC)
318 shared_desc_len = cnstr_shdsc_pdcp_u_plane_encap(
319 cdb->sh_desc, 1, swap,
324 ses->pdcp.hfn_threshold,
325 &cipherdata, p_authdata, 0);
326 else if (ses->dir == DIR_DEC)
327 shared_desc_len = cnstr_shdsc_pdcp_u_plane_decap(
328 cdb->sh_desc, 1, swap,
333 ses->pdcp.hfn_threshold,
334 &cipherdata, p_authdata, 0);
336 return shared_desc_len;
339 /* prepare ipsec proto command block of the session */
341 dpaa_sec_prep_ipsec_cdb(dpaa_sec_session *ses)
343 struct alginfo cipherdata = {0}, authdata = {0};
344 struct sec_cdb *cdb = &ses->cdb;
345 int32_t shared_desc_len = 0;
347 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
353 cipherdata.key = (size_t)ses->cipher_key.data;
354 cipherdata.keylen = ses->cipher_key.length;
355 cipherdata.key_enc_flags = 0;
356 cipherdata.key_type = RTA_DATA_IMM;
357 cipherdata.algtype = ses->cipher_key.alg;
358 cipherdata.algmode = ses->cipher_key.algmode;
360 if (ses->auth_key.length) {
361 authdata.key = (size_t)ses->auth_key.data;
362 authdata.keylen = ses->auth_key.length;
363 authdata.key_enc_flags = 0;
364 authdata.key_type = RTA_DATA_IMM;
365 authdata.algtype = ses->auth_key.alg;
366 authdata.algmode = ses->auth_key.algmode;
369 cdb->sh_desc[0] = cipherdata.keylen;
370 cdb->sh_desc[1] = authdata.keylen;
371 err = rta_inline_query(IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN,
373 (unsigned int *)cdb->sh_desc,
374 &cdb->sh_desc[2], 2);
377 DPAA_SEC_ERR("Crypto: Incorrect key lengths");
380 if (cdb->sh_desc[2] & 1)
381 cipherdata.key_type = RTA_DATA_IMM;
383 cipherdata.key = (size_t)rte_dpaa_mem_vtop(
384 (void *)(size_t)cipherdata.key);
385 cipherdata.key_type = RTA_DATA_PTR;
387 if (cdb->sh_desc[2] & (1<<1))
388 authdata.key_type = RTA_DATA_IMM;
390 authdata.key = (size_t)rte_dpaa_mem_vtop(
391 (void *)(size_t)authdata.key);
392 authdata.key_type = RTA_DATA_PTR;
398 if (ses->dir == DIR_ENC) {
399 shared_desc_len = cnstr_shdsc_ipsec_new_encap(
401 true, swap, SHR_SERIAL,
403 (uint8_t *)&ses->ip4_hdr,
404 &cipherdata, &authdata);
405 } else if (ses->dir == DIR_DEC) {
406 shared_desc_len = cnstr_shdsc_ipsec_new_decap(
408 true, swap, SHR_SERIAL,
410 &cipherdata, &authdata);
412 return shared_desc_len;
415 /* prepare command block of the session */
417 dpaa_sec_prep_cdb(dpaa_sec_session *ses)
419 struct alginfo alginfo_c = {0}, alginfo_a = {0}, alginfo = {0};
420 int32_t shared_desc_len = 0;
421 struct sec_cdb *cdb = &ses->cdb;
423 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
429 memset(cdb, 0, sizeof(struct sec_cdb));
432 #ifdef RTE_LIBRTE_SECURITY
434 shared_desc_len = dpaa_sec_prep_ipsec_cdb(ses);
437 shared_desc_len = dpaa_sec_prep_pdcp_cdb(ses);
440 case DPAA_SEC_CIPHER:
441 alginfo_c.key = (size_t)ses->cipher_key.data;
442 alginfo_c.keylen = ses->cipher_key.length;
443 alginfo_c.key_enc_flags = 0;
444 alginfo_c.key_type = RTA_DATA_IMM;
445 alginfo_c.algtype = ses->cipher_key.alg;
446 alginfo_c.algmode = ses->cipher_key.algmode;
448 switch (ses->cipher_alg) {
449 case RTE_CRYPTO_CIPHER_AES_CBC:
450 case RTE_CRYPTO_CIPHER_3DES_CBC:
451 case RTE_CRYPTO_CIPHER_AES_CTR:
452 case RTE_CRYPTO_CIPHER_3DES_CTR:
453 shared_desc_len = cnstr_shdsc_blkcipher(
455 swap, SHR_NEVER, &alginfo_c,
459 case RTE_CRYPTO_CIPHER_SNOW3G_UEA2:
460 shared_desc_len = cnstr_shdsc_snow_f8(
461 cdb->sh_desc, true, swap,
465 case RTE_CRYPTO_CIPHER_ZUC_EEA3:
466 shared_desc_len = cnstr_shdsc_zuce(
467 cdb->sh_desc, true, swap,
472 DPAA_SEC_ERR("unsupported cipher alg %d",
478 alginfo_a.key = (size_t)ses->auth_key.data;
479 alginfo_a.keylen = ses->auth_key.length;
480 alginfo_a.key_enc_flags = 0;
481 alginfo_a.key_type = RTA_DATA_IMM;
482 alginfo_a.algtype = ses->auth_key.alg;
483 alginfo_a.algmode = ses->auth_key.algmode;
484 switch (ses->auth_alg) {
485 case RTE_CRYPTO_AUTH_MD5_HMAC:
486 case RTE_CRYPTO_AUTH_SHA1_HMAC:
487 case RTE_CRYPTO_AUTH_SHA224_HMAC:
488 case RTE_CRYPTO_AUTH_SHA256_HMAC:
489 case RTE_CRYPTO_AUTH_SHA384_HMAC:
490 case RTE_CRYPTO_AUTH_SHA512_HMAC:
491 shared_desc_len = cnstr_shdsc_hmac(
493 swap, SHR_NEVER, &alginfo_a,
497 case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
498 shared_desc_len = cnstr_shdsc_snow_f9(
499 cdb->sh_desc, true, swap,
504 case RTE_CRYPTO_AUTH_ZUC_EIA3:
505 shared_desc_len = cnstr_shdsc_zuca(
506 cdb->sh_desc, true, swap,
512 DPAA_SEC_ERR("unsupported auth alg %u", ses->auth_alg);
516 if (alginfo.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
517 DPAA_SEC_ERR("not supported aead alg");
520 alginfo.key = (size_t)ses->aead_key.data;
521 alginfo.keylen = ses->aead_key.length;
522 alginfo.key_enc_flags = 0;
523 alginfo.key_type = RTA_DATA_IMM;
524 alginfo.algtype = ses->aead_key.alg;
525 alginfo.algmode = ses->aead_key.algmode;
527 if (ses->dir == DIR_ENC)
528 shared_desc_len = cnstr_shdsc_gcm_encap(
529 cdb->sh_desc, true, swap, SHR_NEVER,
534 shared_desc_len = cnstr_shdsc_gcm_decap(
535 cdb->sh_desc, true, swap, SHR_NEVER,
540 case DPAA_SEC_CIPHER_HASH:
541 alginfo_c.key = (size_t)ses->cipher_key.data;
542 alginfo_c.keylen = ses->cipher_key.length;
543 alginfo_c.key_enc_flags = 0;
544 alginfo_c.key_type = RTA_DATA_IMM;
545 alginfo_c.algtype = ses->cipher_key.alg;
546 alginfo_c.algmode = ses->cipher_key.algmode;
548 alginfo_a.key = (size_t)ses->auth_key.data;
549 alginfo_a.keylen = ses->auth_key.length;
550 alginfo_a.key_enc_flags = 0;
551 alginfo_a.key_type = RTA_DATA_IMM;
552 alginfo_a.algtype = ses->auth_key.alg;
553 alginfo_a.algmode = ses->auth_key.algmode;
555 cdb->sh_desc[0] = alginfo_c.keylen;
556 cdb->sh_desc[1] = alginfo_a.keylen;
557 err = rta_inline_query(IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN,
559 (unsigned int *)cdb->sh_desc,
560 &cdb->sh_desc[2], 2);
563 DPAA_SEC_ERR("Crypto: Incorrect key lengths");
566 if (cdb->sh_desc[2] & 1)
567 alginfo_c.key_type = RTA_DATA_IMM;
569 alginfo_c.key = (size_t)rte_dpaa_mem_vtop(
570 (void *)(size_t)alginfo_c.key);
571 alginfo_c.key_type = RTA_DATA_PTR;
573 if (cdb->sh_desc[2] & (1<<1))
574 alginfo_a.key_type = RTA_DATA_IMM;
576 alginfo_a.key = (size_t)rte_dpaa_mem_vtop(
577 (void *)(size_t)alginfo_a.key);
578 alginfo_a.key_type = RTA_DATA_PTR;
583 /* Auth_only_len is set as 0 here and it will be
584 * overwritten in fd for each packet.
586 shared_desc_len = cnstr_shdsc_authenc(cdb->sh_desc,
587 true, swap, SHR_SERIAL, &alginfo_c, &alginfo_a,
589 ses->digest_length, ses->dir);
591 case DPAA_SEC_HASH_CIPHER:
593 DPAA_SEC_ERR("error: Unsupported session");
597 if (shared_desc_len < 0) {
598 DPAA_SEC_ERR("error in preparing command block");
599 return shared_desc_len;
602 cdb->sh_hdr.hi.field.idlen = shared_desc_len;
603 cdb->sh_hdr.hi.word = rte_cpu_to_be_32(cdb->sh_hdr.hi.word);
604 cdb->sh_hdr.lo.word = rte_cpu_to_be_32(cdb->sh_hdr.lo.word);
609 /* qp is lockless, should be accessed by only one thread */
611 dpaa_sec_deq(struct dpaa_sec_qp *qp, struct rte_crypto_op **ops, int nb_ops)
614 unsigned int pkts = 0;
615 int num_rx_bufs, ret;
616 struct qm_dqrr_entry *dq;
617 uint32_t vdqcr_flags = 0;
621 * Until request for four buffers, we provide exact number of buffers.
622 * Otherwise we do not set the QM_VDQCR_EXACT flag.
623 * Not setting QM_VDQCR_EXACT flag can provide two more buffers than
624 * requested, so we request two less in this case.
627 vdqcr_flags = QM_VDQCR_EXACT;
628 num_rx_bufs = nb_ops;
630 num_rx_bufs = nb_ops > DPAA_MAX_DEQUEUE_NUM_FRAMES ?
631 (DPAA_MAX_DEQUEUE_NUM_FRAMES - 2) : (nb_ops - 2);
633 ret = qman_set_vdq(fq, num_rx_bufs, vdqcr_flags);
638 const struct qm_fd *fd;
639 struct dpaa_sec_job *job;
640 struct dpaa_sec_op_ctx *ctx;
641 struct rte_crypto_op *op;
643 dq = qman_dequeue(fq);
648 /* sg is embedded in an op ctx,
649 * sg[0] is for output
652 job = rte_dpaa_mem_ptov(qm_fd_addr_get64(fd));
654 ctx = container_of(job, struct dpaa_sec_op_ctx, job);
655 ctx->fd_status = fd->status;
657 if (op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
658 struct qm_sg_entry *sg_out;
660 struct rte_mbuf *mbuf = (op->sym->m_dst == NULL) ?
661 op->sym->m_src : op->sym->m_dst;
663 sg_out = &job->sg[0];
664 hw_sg_to_cpu(sg_out);
665 len = sg_out->length;
667 while (mbuf->next != NULL) {
668 len -= mbuf->data_len;
671 mbuf->data_len = len;
673 if (!ctx->fd_status) {
674 op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
676 DPAA_SEC_DP_WARN("SEC return err:0x%x", ctx->fd_status);
677 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
681 /* report op status to sym->op and then free the ctx memeory */
682 rte_mempool_put(ctx->ctx_pool, (void *)ctx);
684 qman_dqrr_consume(fq, dq);
685 } while (fq->flags & QMAN_FQ_STATE_VDQCR);
690 static inline struct dpaa_sec_job *
691 build_auth_only_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
693 struct rte_crypto_sym_op *sym = op->sym;
694 struct rte_mbuf *mbuf = sym->m_src;
695 struct dpaa_sec_job *cf;
696 struct dpaa_sec_op_ctx *ctx;
697 struct qm_sg_entry *sg, *out_sg, *in_sg;
698 phys_addr_t start_addr;
699 uint8_t *old_digest, extra_segs;
700 int data_len, data_offset;
702 data_len = sym->auth.data.length;
703 data_offset = sym->auth.data.offset;
705 if (ses->auth_alg == RTE_CRYPTO_AUTH_SNOW3G_UIA2 ||
706 ses->auth_alg == RTE_CRYPTO_AUTH_ZUC_EIA3) {
707 if ((data_len & 7) || (data_offset & 7)) {
708 DPAA_SEC_ERR("AUTH: len/offset must be full bytes");
712 data_len = data_len >> 3;
713 data_offset = data_offset >> 3;
721 if (mbuf->nb_segs > MAX_SG_ENTRIES) {
722 DPAA_SEC_DP_ERR("Auth: Max sec segs supported is %d",
726 ctx = dpaa_sec_alloc_ctx(ses, mbuf->nb_segs + extra_segs);
732 old_digest = ctx->digest;
736 qm_sg_entry_set64(out_sg, sym->auth.digest.phys_addr);
737 out_sg->length = ses->digest_length;
738 cpu_to_hw_sg(out_sg);
742 /* need to extend the input to a compound frame */
743 in_sg->extension = 1;
745 in_sg->length = data_len;
746 qm_sg_entry_set64(in_sg, rte_dpaa_mem_vtop(&cf->sg[2]));
751 if (ses->iv.length) {
754 iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
757 if (ses->auth_alg == RTE_CRYPTO_AUTH_SNOW3G_UIA2) {
758 iv_ptr = conv_to_snow_f9_iv(iv_ptr);
760 } else if (ses->auth_alg == RTE_CRYPTO_AUTH_ZUC_EIA3) {
761 iv_ptr = conv_to_zuc_eia_iv(iv_ptr);
764 sg->length = ses->iv.length;
766 qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(iv_ptr));
767 in_sg->length += sg->length;
772 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
773 sg->offset = data_offset;
775 if (data_len <= (mbuf->data_len - data_offset)) {
776 sg->length = data_len;
778 sg->length = mbuf->data_len - data_offset;
780 /* remaining i/p segs */
781 while ((data_len = data_len - sg->length) &&
782 (mbuf = mbuf->next)) {
785 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
786 if (data_len > mbuf->data_len)
787 sg->length = mbuf->data_len;
789 sg->length = data_len;
793 if (is_decode(ses)) {
794 /* Digest verification case */
797 rte_memcpy(old_digest, sym->auth.digest.data,
799 start_addr = rte_dpaa_mem_vtop(old_digest);
800 qm_sg_entry_set64(sg, start_addr);
801 sg->length = ses->digest_length;
802 in_sg->length += ses->digest_length;
813 * |<----data_len------->|
814 * |ip_header|ah_header|icv|payload|
819 static inline struct dpaa_sec_job *
820 build_auth_only(struct rte_crypto_op *op, dpaa_sec_session *ses)
822 struct rte_crypto_sym_op *sym = op->sym;
823 struct rte_mbuf *mbuf = sym->m_src;
824 struct dpaa_sec_job *cf;
825 struct dpaa_sec_op_ctx *ctx;
826 struct qm_sg_entry *sg, *in_sg;
827 rte_iova_t start_addr;
829 int data_len, data_offset;
831 data_len = sym->auth.data.length;
832 data_offset = sym->auth.data.offset;
834 if (ses->auth_alg == RTE_CRYPTO_AUTH_SNOW3G_UIA2 ||
835 ses->auth_alg == RTE_CRYPTO_AUTH_ZUC_EIA3) {
836 if ((data_len & 7) || (data_offset & 7)) {
837 DPAA_SEC_ERR("AUTH: len/offset must be full bytes");
841 data_len = data_len >> 3;
842 data_offset = data_offset >> 3;
845 ctx = dpaa_sec_alloc_ctx(ses, 4);
851 old_digest = ctx->digest;
853 start_addr = rte_pktmbuf_iova(mbuf);
856 qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
857 sg->length = ses->digest_length;
862 /* need to extend the input to a compound frame */
863 in_sg->extension = 1;
865 in_sg->length = data_len;
866 qm_sg_entry_set64(in_sg, rte_dpaa_mem_vtop(&cf->sg[2]));
869 if (ses->iv.length) {
872 iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
875 if (ses->auth_alg == RTE_CRYPTO_AUTH_SNOW3G_UIA2) {
876 iv_ptr = conv_to_snow_f9_iv(iv_ptr);
878 } else if (ses->auth_alg == RTE_CRYPTO_AUTH_ZUC_EIA3) {
879 iv_ptr = conv_to_zuc_eia_iv(iv_ptr);
882 sg->length = ses->iv.length;
884 qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(iv_ptr));
885 in_sg->length += sg->length;
890 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
891 sg->offset = data_offset;
892 sg->length = data_len;
894 if (is_decode(ses)) {
895 /* Digest verification case */
897 /* hash result or digest, save digest first */
898 rte_memcpy(old_digest, sym->auth.digest.data,
900 /* let's check digest by hw */
901 start_addr = rte_dpaa_mem_vtop(old_digest);
903 qm_sg_entry_set64(sg, start_addr);
904 sg->length = ses->digest_length;
905 in_sg->length += ses->digest_length;
914 static inline struct dpaa_sec_job *
915 build_cipher_only_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
917 struct rte_crypto_sym_op *sym = op->sym;
918 struct dpaa_sec_job *cf;
919 struct dpaa_sec_op_ctx *ctx;
920 struct qm_sg_entry *sg, *out_sg, *in_sg;
921 struct rte_mbuf *mbuf;
923 uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
925 int data_len, data_offset;
927 data_len = sym->cipher.data.length;
928 data_offset = sym->cipher.data.offset;
930 if (ses->cipher_alg == RTE_CRYPTO_CIPHER_SNOW3G_UEA2 ||
931 ses->cipher_alg == RTE_CRYPTO_CIPHER_ZUC_EEA3) {
932 if ((data_len & 7) || (data_offset & 7)) {
933 DPAA_SEC_ERR("CIPHER: len/offset must be full bytes");
937 data_len = data_len >> 3;
938 data_offset = data_offset >> 3;
943 req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 3;
946 req_segs = mbuf->nb_segs * 2 + 3;
948 if (mbuf->nb_segs > MAX_SG_ENTRIES) {
949 DPAA_SEC_DP_ERR("Cipher: Max sec segs supported is %d",
954 ctx = dpaa_sec_alloc_ctx(ses, req_segs);
963 out_sg->extension = 1;
964 out_sg->length = data_len;
965 qm_sg_entry_set64(out_sg, rte_dpaa_mem_vtop(&cf->sg[2]));
966 cpu_to_hw_sg(out_sg);
970 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
971 sg->length = mbuf->data_len - data_offset;
972 sg->offset = data_offset;
974 /* Successive segs */
979 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
980 sg->length = mbuf->data_len;
989 in_sg->extension = 1;
991 in_sg->length = data_len + ses->iv.length;
994 qm_sg_entry_set64(in_sg, rte_dpaa_mem_vtop(sg));
998 qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(IV_ptr));
999 sg->length = ses->iv.length;
1004 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1005 sg->length = mbuf->data_len - data_offset;
1006 sg->offset = data_offset;
1008 /* Successive segs */
1013 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1014 sg->length = mbuf->data_len;
1023 static inline struct dpaa_sec_job *
1024 build_cipher_only(struct rte_crypto_op *op, dpaa_sec_session *ses)
1026 struct rte_crypto_sym_op *sym = op->sym;
1027 struct dpaa_sec_job *cf;
1028 struct dpaa_sec_op_ctx *ctx;
1029 struct qm_sg_entry *sg;
1030 rte_iova_t src_start_addr, dst_start_addr;
1031 uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1033 int data_len, data_offset;
1035 data_len = sym->cipher.data.length;
1036 data_offset = sym->cipher.data.offset;
1038 if (ses->cipher_alg == RTE_CRYPTO_CIPHER_SNOW3G_UEA2 ||
1039 ses->cipher_alg == RTE_CRYPTO_CIPHER_ZUC_EEA3) {
1040 if ((data_len & 7) || (data_offset & 7)) {
1041 DPAA_SEC_ERR("CIPHER: len/offset must be full bytes");
1045 data_len = data_len >> 3;
1046 data_offset = data_offset >> 3;
1049 ctx = dpaa_sec_alloc_ctx(ses, 4);
1056 src_start_addr = rte_pktmbuf_iova(sym->m_src);
1059 dst_start_addr = rte_pktmbuf_iova(sym->m_dst);
1061 dst_start_addr = src_start_addr;
1065 qm_sg_entry_set64(sg, dst_start_addr + data_offset);
1066 sg->length = data_len + ses->iv.length;
1072 /* need to extend the input to a compound frame */
1075 sg->length = data_len + ses->iv.length;
1076 qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(&cf->sg[2]));
1080 qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(IV_ptr));
1081 sg->length = ses->iv.length;
1085 qm_sg_entry_set64(sg, src_start_addr + data_offset);
1086 sg->length = data_len;
1093 static inline struct dpaa_sec_job *
1094 build_cipher_auth_gcm_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
1096 struct rte_crypto_sym_op *sym = op->sym;
1097 struct dpaa_sec_job *cf;
1098 struct dpaa_sec_op_ctx *ctx;
1099 struct qm_sg_entry *sg, *out_sg, *in_sg;
1100 struct rte_mbuf *mbuf;
1102 uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1107 req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 4;
1110 req_segs = mbuf->nb_segs * 2 + 4;
1113 if (ses->auth_only_len)
1116 if (mbuf->nb_segs > MAX_SG_ENTRIES) {
1117 DPAA_SEC_DP_ERR("AEAD: Max sec segs supported is %d",
1122 ctx = dpaa_sec_alloc_ctx(ses, req_segs);
1129 rte_prefetch0(cf->sg);
1132 out_sg = &cf->sg[0];
1133 out_sg->extension = 1;
1135 out_sg->length = sym->aead.data.length + ses->digest_length;
1137 out_sg->length = sym->aead.data.length;
1139 /* output sg entries */
1141 qm_sg_entry_set64(out_sg, rte_dpaa_mem_vtop(sg));
1142 cpu_to_hw_sg(out_sg);
1145 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1146 sg->length = mbuf->data_len - sym->aead.data.offset;
1147 sg->offset = sym->aead.data.offset;
1149 /* Successive segs */
1154 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1155 sg->length = mbuf->data_len;
1158 sg->length -= ses->digest_length;
1160 if (is_encode(ses)) {
1162 /* set auth output */
1164 qm_sg_entry_set64(sg, sym->aead.digest.phys_addr);
1165 sg->length = ses->digest_length;
1173 in_sg->extension = 1;
1176 in_sg->length = ses->iv.length + sym->aead.data.length
1177 + ses->auth_only_len;
1179 in_sg->length = ses->iv.length + sym->aead.data.length
1180 + ses->auth_only_len + ses->digest_length;
1182 /* input sg entries */
1184 qm_sg_entry_set64(in_sg, rte_dpaa_mem_vtop(sg));
1185 cpu_to_hw_sg(in_sg);
1188 qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(IV_ptr));
1189 sg->length = ses->iv.length;
1192 /* 2nd seg auth only */
1193 if (ses->auth_only_len) {
1195 qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(sym->aead.aad.data));
1196 sg->length = ses->auth_only_len;
1202 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1203 sg->length = mbuf->data_len - sym->aead.data.offset;
1204 sg->offset = sym->aead.data.offset;
1206 /* Successive segs */
1211 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1212 sg->length = mbuf->data_len;
1216 if (is_decode(ses)) {
1219 memcpy(ctx->digest, sym->aead.digest.data,
1220 ses->digest_length);
1221 qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(ctx->digest));
1222 sg->length = ses->digest_length;
1230 static inline struct dpaa_sec_job *
1231 build_cipher_auth_gcm(struct rte_crypto_op *op, dpaa_sec_session *ses)
1233 struct rte_crypto_sym_op *sym = op->sym;
1234 struct dpaa_sec_job *cf;
1235 struct dpaa_sec_op_ctx *ctx;
1236 struct qm_sg_entry *sg;
1237 uint32_t length = 0;
1238 rte_iova_t src_start_addr, dst_start_addr;
1239 uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1242 src_start_addr = sym->m_src->buf_iova + sym->m_src->data_off;
1245 dst_start_addr = sym->m_dst->buf_iova + sym->m_dst->data_off;
1247 dst_start_addr = src_start_addr;
1249 ctx = dpaa_sec_alloc_ctx(ses, 7);
1257 rte_prefetch0(cf->sg);
1259 qm_sg_entry_set64(&cf->sg[1], rte_dpaa_mem_vtop(sg));
1260 if (is_encode(ses)) {
1261 qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(IV_ptr));
1262 sg->length = ses->iv.length;
1263 length += sg->length;
1267 if (ses->auth_only_len) {
1268 qm_sg_entry_set64(sg,
1269 rte_dpaa_mem_vtop(sym->aead.aad.data));
1270 sg->length = ses->auth_only_len;
1271 length += sg->length;
1275 qm_sg_entry_set64(sg, src_start_addr + sym->aead.data.offset);
1276 sg->length = sym->aead.data.length;
1277 length += sg->length;
1281 qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(IV_ptr));
1282 sg->length = ses->iv.length;
1283 length += sg->length;
1287 if (ses->auth_only_len) {
1288 qm_sg_entry_set64(sg,
1289 rte_dpaa_mem_vtop(sym->aead.aad.data));
1290 sg->length = ses->auth_only_len;
1291 length += sg->length;
1295 qm_sg_entry_set64(sg, src_start_addr + sym->aead.data.offset);
1296 sg->length = sym->aead.data.length;
1297 length += sg->length;
1300 memcpy(ctx->digest, sym->aead.digest.data,
1301 ses->digest_length);
1304 qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(ctx->digest));
1305 sg->length = ses->digest_length;
1306 length += sg->length;
1310 /* input compound frame */
1311 cf->sg[1].length = length;
1312 cf->sg[1].extension = 1;
1313 cf->sg[1].final = 1;
1314 cpu_to_hw_sg(&cf->sg[1]);
1318 qm_sg_entry_set64(&cf->sg[0], rte_dpaa_mem_vtop(sg));
1319 qm_sg_entry_set64(sg,
1320 dst_start_addr + sym->aead.data.offset);
1321 sg->length = sym->aead.data.length;
1322 length = sg->length;
1323 if (is_encode(ses)) {
1325 /* set auth output */
1327 qm_sg_entry_set64(sg, sym->aead.digest.phys_addr);
1328 sg->length = ses->digest_length;
1329 length += sg->length;
1334 /* output compound frame */
1335 cf->sg[0].length = length;
1336 cf->sg[0].extension = 1;
1337 cpu_to_hw_sg(&cf->sg[0]);
1342 static inline struct dpaa_sec_job *
1343 build_cipher_auth_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
1345 struct rte_crypto_sym_op *sym = op->sym;
1346 struct dpaa_sec_job *cf;
1347 struct dpaa_sec_op_ctx *ctx;
1348 struct qm_sg_entry *sg, *out_sg, *in_sg;
1349 struct rte_mbuf *mbuf;
1351 uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1356 req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 4;
1359 req_segs = mbuf->nb_segs * 2 + 4;
1362 if (mbuf->nb_segs > MAX_SG_ENTRIES) {
1363 DPAA_SEC_DP_ERR("Cipher-Auth: Max sec segs supported is %d",
1368 ctx = dpaa_sec_alloc_ctx(ses, req_segs);
1375 rte_prefetch0(cf->sg);
1378 out_sg = &cf->sg[0];
1379 out_sg->extension = 1;
1381 out_sg->length = sym->auth.data.length + ses->digest_length;
1383 out_sg->length = sym->auth.data.length;
1385 /* output sg entries */
1387 qm_sg_entry_set64(out_sg, rte_dpaa_mem_vtop(sg));
1388 cpu_to_hw_sg(out_sg);
1391 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1392 sg->length = mbuf->data_len - sym->auth.data.offset;
1393 sg->offset = sym->auth.data.offset;
1395 /* Successive segs */
1400 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1401 sg->length = mbuf->data_len;
1404 sg->length -= ses->digest_length;
1406 if (is_encode(ses)) {
1408 /* set auth output */
1410 qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
1411 sg->length = ses->digest_length;
1419 in_sg->extension = 1;
1422 in_sg->length = ses->iv.length + sym->auth.data.length;
1424 in_sg->length = ses->iv.length + sym->auth.data.length
1425 + ses->digest_length;
1427 /* input sg entries */
1429 qm_sg_entry_set64(in_sg, rte_dpaa_mem_vtop(sg));
1430 cpu_to_hw_sg(in_sg);
1433 qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(IV_ptr));
1434 sg->length = ses->iv.length;
1439 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1440 sg->length = mbuf->data_len - sym->auth.data.offset;
1441 sg->offset = sym->auth.data.offset;
1443 /* Successive segs */
1448 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1449 sg->length = mbuf->data_len;
1453 sg->length -= ses->digest_length;
1454 if (is_decode(ses)) {
1457 memcpy(ctx->digest, sym->auth.digest.data,
1458 ses->digest_length);
1459 qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(ctx->digest));
1460 sg->length = ses->digest_length;
1468 static inline struct dpaa_sec_job *
1469 build_cipher_auth(struct rte_crypto_op *op, dpaa_sec_session *ses)
1471 struct rte_crypto_sym_op *sym = op->sym;
1472 struct dpaa_sec_job *cf;
1473 struct dpaa_sec_op_ctx *ctx;
1474 struct qm_sg_entry *sg;
1475 rte_iova_t src_start_addr, dst_start_addr;
1476 uint32_t length = 0;
1477 uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1480 src_start_addr = sym->m_src->buf_iova + sym->m_src->data_off;
1482 dst_start_addr = sym->m_dst->buf_iova + sym->m_dst->data_off;
1484 dst_start_addr = src_start_addr;
1486 ctx = dpaa_sec_alloc_ctx(ses, 7);
1494 rte_prefetch0(cf->sg);
1496 qm_sg_entry_set64(&cf->sg[1], rte_dpaa_mem_vtop(sg));
1497 if (is_encode(ses)) {
1498 qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(IV_ptr));
1499 sg->length = ses->iv.length;
1500 length += sg->length;
1504 qm_sg_entry_set64(sg, src_start_addr + sym->auth.data.offset);
1505 sg->length = sym->auth.data.length;
1506 length += sg->length;
1510 qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(IV_ptr));
1511 sg->length = ses->iv.length;
1512 length += sg->length;
1517 qm_sg_entry_set64(sg, src_start_addr + sym->auth.data.offset);
1518 sg->length = sym->auth.data.length;
1519 length += sg->length;
1522 memcpy(ctx->digest, sym->auth.digest.data,
1523 ses->digest_length);
1526 qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(ctx->digest));
1527 sg->length = ses->digest_length;
1528 length += sg->length;
1532 /* input compound frame */
1533 cf->sg[1].length = length;
1534 cf->sg[1].extension = 1;
1535 cf->sg[1].final = 1;
1536 cpu_to_hw_sg(&cf->sg[1]);
1540 qm_sg_entry_set64(&cf->sg[0], rte_dpaa_mem_vtop(sg));
1541 qm_sg_entry_set64(sg, dst_start_addr + sym->cipher.data.offset);
1542 sg->length = sym->cipher.data.length;
1543 length = sg->length;
1544 if (is_encode(ses)) {
1546 /* set auth output */
1548 qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
1549 sg->length = ses->digest_length;
1550 length += sg->length;
1555 /* output compound frame */
1556 cf->sg[0].length = length;
1557 cf->sg[0].extension = 1;
1558 cpu_to_hw_sg(&cf->sg[0]);
1563 #ifdef RTE_LIBRTE_SECURITY
1564 static inline struct dpaa_sec_job *
1565 build_proto(struct rte_crypto_op *op, dpaa_sec_session *ses)
1567 struct rte_crypto_sym_op *sym = op->sym;
1568 struct dpaa_sec_job *cf;
1569 struct dpaa_sec_op_ctx *ctx;
1570 struct qm_sg_entry *sg;
1571 phys_addr_t src_start_addr, dst_start_addr;
1573 ctx = dpaa_sec_alloc_ctx(ses, 2);
1579 src_start_addr = rte_pktmbuf_mtophys(sym->m_src);
1582 dst_start_addr = rte_pktmbuf_mtophys(sym->m_dst);
1584 dst_start_addr = src_start_addr;
1588 qm_sg_entry_set64(sg, src_start_addr);
1589 sg->length = sym->m_src->pkt_len;
1593 sym->m_src->packet_type &= ~RTE_PTYPE_L4_MASK;
1596 qm_sg_entry_set64(sg, dst_start_addr);
1597 sg->length = sym->m_src->buf_len - sym->m_src->data_off;
1603 static inline struct dpaa_sec_job *
1604 build_proto_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
1606 struct rte_crypto_sym_op *sym = op->sym;
1607 struct dpaa_sec_job *cf;
1608 struct dpaa_sec_op_ctx *ctx;
1609 struct qm_sg_entry *sg, *out_sg, *in_sg;
1610 struct rte_mbuf *mbuf;
1612 uint32_t in_len = 0, out_len = 0;
1619 req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 2;
1620 if (mbuf->nb_segs > MAX_SG_ENTRIES) {
1621 DPAA_SEC_DP_ERR("Proto: Max sec segs supported is %d",
1626 ctx = dpaa_sec_alloc_ctx(ses, req_segs);
1632 out_sg = &cf->sg[0];
1633 out_sg->extension = 1;
1634 qm_sg_entry_set64(out_sg, rte_dpaa_mem_vtop(&cf->sg[2]));
1638 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1641 /* Successive segs */
1642 while (mbuf->next) {
1643 sg->length = mbuf->data_len;
1644 out_len += sg->length;
1648 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1651 sg->length = mbuf->buf_len - mbuf->data_off;
1652 out_len += sg->length;
1656 out_sg->length = out_len;
1657 cpu_to_hw_sg(out_sg);
1662 in_sg->extension = 1;
1664 in_len = mbuf->data_len;
1667 qm_sg_entry_set64(in_sg, rte_dpaa_mem_vtop(sg));
1670 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1671 sg->length = mbuf->data_len;
1674 /* Successive segs */
1679 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1680 sg->length = mbuf->data_len;
1682 in_len += sg->length;
1688 in_sg->length = in_len;
1689 cpu_to_hw_sg(in_sg);
1691 sym->m_src->packet_type &= ~RTE_PTYPE_L4_MASK;
1698 dpaa_sec_enqueue_burst(void *qp, struct rte_crypto_op **ops,
1701 /* Function to transmit the frames to given device and queuepair */
1703 struct dpaa_sec_qp *dpaa_qp = (struct dpaa_sec_qp *)qp;
1704 uint16_t num_tx = 0;
1705 struct qm_fd fds[DPAA_SEC_BURST], *fd;
1706 uint32_t frames_to_send;
1707 struct rte_crypto_op *op;
1708 struct dpaa_sec_job *cf;
1709 dpaa_sec_session *ses;
1710 uint16_t auth_hdr_len, auth_tail_len;
1711 uint32_t index, flags[DPAA_SEC_BURST] = {0};
1712 struct qman_fq *inq[DPAA_SEC_BURST];
1715 frames_to_send = (nb_ops > DPAA_SEC_BURST) ?
1716 DPAA_SEC_BURST : nb_ops;
1717 for (loop = 0; loop < frames_to_send; loop++) {
1719 if (op->sym->m_src->seqn != 0) {
1720 index = op->sym->m_src->seqn - 1;
1721 if (DPAA_PER_LCORE_DQRR_HELD & (1 << index)) {
1722 /* QM_EQCR_DCA_IDXMASK = 0x0f */
1723 flags[loop] = ((index & 0x0f) << 8);
1724 flags[loop] |= QMAN_ENQUEUE_FLAG_DCA;
1725 DPAA_PER_LCORE_DQRR_SIZE--;
1726 DPAA_PER_LCORE_DQRR_HELD &=
1731 switch (op->sess_type) {
1732 case RTE_CRYPTO_OP_WITH_SESSION:
1733 ses = (dpaa_sec_session *)
1734 get_sym_session_private_data(
1736 cryptodev_driver_id);
1738 #ifdef RTE_LIBRTE_SECURITY
1739 case RTE_CRYPTO_OP_SECURITY_SESSION:
1740 ses = (dpaa_sec_session *)
1741 get_sec_session_private_data(
1742 op->sym->sec_session);
1747 "sessionless crypto op not supported");
1748 frames_to_send = loop;
1754 DPAA_SEC_DP_ERR("session not available");
1755 frames_to_send = loop;
1760 if (unlikely(!ses->qp[rte_lcore_id() % MAX_DPAA_CORES])) {
1761 if (dpaa_sec_attach_sess_q(qp, ses)) {
1762 frames_to_send = loop;
1766 } else if (unlikely(ses->qp[rte_lcore_id() %
1767 MAX_DPAA_CORES] != qp)) {
1768 DPAA_SEC_DP_ERR("Old:sess->qp = %p"
1770 ses->qp[rte_lcore_id() %
1771 MAX_DPAA_CORES], qp);
1772 frames_to_send = loop;
1777 auth_hdr_len = op->sym->auth.data.length -
1778 op->sym->cipher.data.length;
1781 if (rte_pktmbuf_is_contiguous(op->sym->m_src) &&
1782 ((op->sym->m_dst == NULL) ||
1783 rte_pktmbuf_is_contiguous(op->sym->m_dst))) {
1784 switch (ses->ctxt) {
1785 #ifdef RTE_LIBRTE_SECURITY
1787 case DPAA_SEC_IPSEC:
1788 cf = build_proto(op, ses);
1792 cf = build_auth_only(op, ses);
1794 case DPAA_SEC_CIPHER:
1795 cf = build_cipher_only(op, ses);
1798 cf = build_cipher_auth_gcm(op, ses);
1799 auth_hdr_len = ses->auth_only_len;
1801 case DPAA_SEC_CIPHER_HASH:
1803 op->sym->cipher.data.offset
1804 - op->sym->auth.data.offset;
1806 op->sym->auth.data.length
1807 - op->sym->cipher.data.length
1809 cf = build_cipher_auth(op, ses);
1812 DPAA_SEC_DP_ERR("not supported ops");
1813 frames_to_send = loop;
1818 switch (ses->ctxt) {
1819 #ifdef RTE_LIBRTE_SECURITY
1821 case DPAA_SEC_IPSEC:
1822 cf = build_proto_sg(op, ses);
1826 cf = build_auth_only_sg(op, ses);
1828 case DPAA_SEC_CIPHER:
1829 cf = build_cipher_only_sg(op, ses);
1832 cf = build_cipher_auth_gcm_sg(op, ses);
1833 auth_hdr_len = ses->auth_only_len;
1835 case DPAA_SEC_CIPHER_HASH:
1837 op->sym->cipher.data.offset
1838 - op->sym->auth.data.offset;
1840 op->sym->auth.data.length
1841 - op->sym->cipher.data.length
1843 cf = build_cipher_auth_sg(op, ses);
1846 DPAA_SEC_DP_ERR("not supported ops");
1847 frames_to_send = loop;
1852 if (unlikely(!cf)) {
1853 frames_to_send = loop;
1859 inq[loop] = ses->inq[rte_lcore_id() % MAX_DPAA_CORES];
1860 fd->opaque_addr = 0;
1862 qm_fd_addr_set64(fd, rte_dpaa_mem_vtop(cf->sg));
1863 fd->_format1 = qm_fd_compound;
1864 fd->length29 = 2 * sizeof(struct qm_sg_entry);
1866 /* Auth_only_len is set as 0 in descriptor and it is
1867 * overwritten here in the fd.cmd which will update
1870 if (auth_hdr_len || auth_tail_len) {
1871 fd->cmd = 0x80000000;
1873 ((auth_tail_len << 16) | auth_hdr_len);
1876 #ifdef RTE_LIBRTE_SECURITY
1877 /* In case of PDCP, per packet HFN is stored in
1878 * mbuf priv after sym_op.
1880 if ((ses->ctxt == DPAA_SEC_PDCP) && ses->pdcp.hfn_ovd) {
1881 fd->cmd = 0x80000000 |
1882 *((uint32_t *)((uint8_t *)op +
1883 ses->pdcp.hfn_ovd_offset));
1884 DPAA_SEC_DP_DEBUG("Per packet HFN: %x, ovd:%u\n",
1885 *((uint32_t *)((uint8_t *)op +
1886 ses->pdcp.hfn_ovd_offset)),
1893 while (loop < frames_to_send) {
1894 loop += qman_enqueue_multi_fq(&inq[loop], &fds[loop],
1895 &flags[loop], frames_to_send - loop);
1897 nb_ops -= frames_to_send;
1898 num_tx += frames_to_send;
1901 dpaa_qp->tx_pkts += num_tx;
1902 dpaa_qp->tx_errs += nb_ops - num_tx;
1908 dpaa_sec_dequeue_burst(void *qp, struct rte_crypto_op **ops,
1912 struct dpaa_sec_qp *dpaa_qp = (struct dpaa_sec_qp *)qp;
1914 num_rx = dpaa_sec_deq(dpaa_qp, ops, nb_ops);
1916 dpaa_qp->rx_pkts += num_rx;
1917 dpaa_qp->rx_errs += nb_ops - num_rx;
1919 DPAA_SEC_DP_DEBUG("SEC Received %d Packets\n", num_rx);
1924 /** Release queue pair */
1926 dpaa_sec_queue_pair_release(struct rte_cryptodev *dev,
1929 struct dpaa_sec_dev_private *internals;
1930 struct dpaa_sec_qp *qp = NULL;
1932 PMD_INIT_FUNC_TRACE();
1934 DPAA_SEC_DEBUG("dev =%p, queue =%d", dev, qp_id);
1936 internals = dev->data->dev_private;
1937 if (qp_id >= internals->max_nb_queue_pairs) {
1938 DPAA_SEC_ERR("Max supported qpid %d",
1939 internals->max_nb_queue_pairs);
1943 qp = &internals->qps[qp_id];
1944 rte_mempool_free(qp->ctx_pool);
1945 qp->internals = NULL;
1946 dev->data->queue_pairs[qp_id] = NULL;
1951 /** Setup a queue pair */
1953 dpaa_sec_queue_pair_setup(struct rte_cryptodev *dev, uint16_t qp_id,
1954 __rte_unused const struct rte_cryptodev_qp_conf *qp_conf,
1955 __rte_unused int socket_id)
1957 struct dpaa_sec_dev_private *internals;
1958 struct dpaa_sec_qp *qp = NULL;
1961 DPAA_SEC_DEBUG("dev =%p, queue =%d, conf =%p", dev, qp_id, qp_conf);
1963 internals = dev->data->dev_private;
1964 if (qp_id >= internals->max_nb_queue_pairs) {
1965 DPAA_SEC_ERR("Max supported qpid %d",
1966 internals->max_nb_queue_pairs);
1970 qp = &internals->qps[qp_id];
1971 qp->internals = internals;
1972 snprintf(str, sizeof(str), "ctx_pool_d%d_qp%d",
1973 dev->data->dev_id, qp_id);
1974 if (!qp->ctx_pool) {
1975 qp->ctx_pool = rte_mempool_create((const char *)str,
1978 CTX_POOL_CACHE_SIZE, 0,
1979 NULL, NULL, NULL, NULL,
1981 if (!qp->ctx_pool) {
1982 DPAA_SEC_ERR("%s create failed\n", str);
1986 DPAA_SEC_INFO("mempool already created for dev_id : %d, qp: %d",
1987 dev->data->dev_id, qp_id);
1988 dev->data->queue_pairs[qp_id] = qp;
1993 /** Returns the size of session structure */
1995 dpaa_sec_sym_session_get_size(struct rte_cryptodev *dev __rte_unused)
1997 PMD_INIT_FUNC_TRACE();
1999 return sizeof(dpaa_sec_session);
2003 dpaa_sec_cipher_init(struct rte_cryptodev *dev __rte_unused,
2004 struct rte_crypto_sym_xform *xform,
2005 dpaa_sec_session *session)
2007 session->ctxt = DPAA_SEC_CIPHER;
2008 session->cipher_alg = xform->cipher.algo;
2009 session->iv.length = xform->cipher.iv.length;
2010 session->iv.offset = xform->cipher.iv.offset;
2011 session->cipher_key.data = rte_zmalloc(NULL, xform->cipher.key.length,
2012 RTE_CACHE_LINE_SIZE);
2013 if (session->cipher_key.data == NULL && xform->cipher.key.length > 0) {
2014 DPAA_SEC_ERR("No Memory for cipher key");
2017 session->cipher_key.length = xform->cipher.key.length;
2019 memcpy(session->cipher_key.data, xform->cipher.key.data,
2020 xform->cipher.key.length);
2021 switch (xform->cipher.algo) {
2022 case RTE_CRYPTO_CIPHER_AES_CBC:
2023 session->cipher_key.alg = OP_ALG_ALGSEL_AES;
2024 session->cipher_key.algmode = OP_ALG_AAI_CBC;
2026 case RTE_CRYPTO_CIPHER_3DES_CBC:
2027 session->cipher_key.alg = OP_ALG_ALGSEL_3DES;
2028 session->cipher_key.algmode = OP_ALG_AAI_CBC;
2030 case RTE_CRYPTO_CIPHER_AES_CTR:
2031 session->cipher_key.alg = OP_ALG_ALGSEL_AES;
2032 session->cipher_key.algmode = OP_ALG_AAI_CTR;
2034 case RTE_CRYPTO_CIPHER_SNOW3G_UEA2:
2035 session->cipher_key.alg = OP_ALG_ALGSEL_SNOW_F8;
2037 case RTE_CRYPTO_CIPHER_ZUC_EEA3:
2038 session->cipher_key.alg = OP_ALG_ALGSEL_ZUCE;
2041 DPAA_SEC_ERR("Crypto: Undefined Cipher specified %u",
2042 xform->cipher.algo);
2043 rte_free(session->cipher_key.data);
2046 session->dir = (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
2053 dpaa_sec_auth_init(struct rte_cryptodev *dev __rte_unused,
2054 struct rte_crypto_sym_xform *xform,
2055 dpaa_sec_session *session)
2057 session->ctxt = DPAA_SEC_AUTH;
2058 session->auth_alg = xform->auth.algo;
2059 session->auth_key.data = rte_zmalloc(NULL, xform->auth.key.length,
2060 RTE_CACHE_LINE_SIZE);
2061 if (session->auth_key.data == NULL && xform->auth.key.length > 0) {
2062 DPAA_SEC_ERR("No Memory for auth key");
2065 session->auth_key.length = xform->auth.key.length;
2066 session->digest_length = xform->auth.digest_length;
2067 if (session->cipher_alg == RTE_CRYPTO_CIPHER_NULL) {
2068 session->iv.offset = xform->auth.iv.offset;
2069 session->iv.length = xform->auth.iv.length;
2072 memcpy(session->auth_key.data, xform->auth.key.data,
2073 xform->auth.key.length);
2075 switch (xform->auth.algo) {
2076 case RTE_CRYPTO_AUTH_SHA1_HMAC:
2077 session->auth_key.alg = OP_ALG_ALGSEL_SHA1;
2078 session->auth_key.algmode = OP_ALG_AAI_HMAC;
2080 case RTE_CRYPTO_AUTH_MD5_HMAC:
2081 session->auth_key.alg = OP_ALG_ALGSEL_MD5;
2082 session->auth_key.algmode = OP_ALG_AAI_HMAC;
2084 case RTE_CRYPTO_AUTH_SHA224_HMAC:
2085 session->auth_key.alg = OP_ALG_ALGSEL_SHA224;
2086 session->auth_key.algmode = OP_ALG_AAI_HMAC;
2088 case RTE_CRYPTO_AUTH_SHA256_HMAC:
2089 session->auth_key.alg = OP_ALG_ALGSEL_SHA256;
2090 session->auth_key.algmode = OP_ALG_AAI_HMAC;
2092 case RTE_CRYPTO_AUTH_SHA384_HMAC:
2093 session->auth_key.alg = OP_ALG_ALGSEL_SHA384;
2094 session->auth_key.algmode = OP_ALG_AAI_HMAC;
2096 case RTE_CRYPTO_AUTH_SHA512_HMAC:
2097 session->auth_key.alg = OP_ALG_ALGSEL_SHA512;
2098 session->auth_key.algmode = OP_ALG_AAI_HMAC;
2100 case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
2101 session->auth_key.alg = OP_ALG_ALGSEL_SNOW_F9;
2102 session->auth_key.algmode = OP_ALG_AAI_F9;
2104 case RTE_CRYPTO_AUTH_ZUC_EIA3:
2105 session->auth_key.alg = OP_ALG_ALGSEL_ZUCA;
2106 session->auth_key.algmode = OP_ALG_AAI_F9;
2109 DPAA_SEC_ERR("Crypto: Unsupported Auth specified %u",
2111 rte_free(session->auth_key.data);
2115 session->dir = (xform->auth.op == RTE_CRYPTO_AUTH_OP_GENERATE) ?
2122 dpaa_sec_chain_init(struct rte_cryptodev *dev __rte_unused,
2123 struct rte_crypto_sym_xform *xform,
2124 dpaa_sec_session *session)
2127 struct rte_crypto_cipher_xform *cipher_xform;
2128 struct rte_crypto_auth_xform *auth_xform;
2130 session->ctxt = DPAA_SEC_CIPHER_HASH;
2131 if (session->auth_cipher_text) {
2132 cipher_xform = &xform->cipher;
2133 auth_xform = &xform->next->auth;
2135 cipher_xform = &xform->next->cipher;
2136 auth_xform = &xform->auth;
2139 /* Set IV parameters */
2140 session->iv.offset = cipher_xform->iv.offset;
2141 session->iv.length = cipher_xform->iv.length;
2143 session->cipher_key.data = rte_zmalloc(NULL, cipher_xform->key.length,
2144 RTE_CACHE_LINE_SIZE);
2145 if (session->cipher_key.data == NULL && cipher_xform->key.length > 0) {
2146 DPAA_SEC_ERR("No Memory for cipher key");
2149 session->cipher_key.length = cipher_xform->key.length;
2150 session->auth_key.data = rte_zmalloc(NULL, auth_xform->key.length,
2151 RTE_CACHE_LINE_SIZE);
2152 if (session->auth_key.data == NULL && auth_xform->key.length > 0) {
2153 DPAA_SEC_ERR("No Memory for auth key");
2154 rte_free(session->cipher_key.data);
2157 session->auth_key.length = auth_xform->key.length;
2158 memcpy(session->cipher_key.data, cipher_xform->key.data,
2159 cipher_xform->key.length);
2160 memcpy(session->auth_key.data, auth_xform->key.data,
2161 auth_xform->key.length);
2163 session->digest_length = auth_xform->digest_length;
2164 session->auth_alg = auth_xform->algo;
2166 switch (auth_xform->algo) {
2167 case RTE_CRYPTO_AUTH_SHA1_HMAC:
2168 session->auth_key.alg = OP_ALG_ALGSEL_SHA1;
2169 session->auth_key.algmode = OP_ALG_AAI_HMAC;
2171 case RTE_CRYPTO_AUTH_MD5_HMAC:
2172 session->auth_key.alg = OP_ALG_ALGSEL_MD5;
2173 session->auth_key.algmode = OP_ALG_AAI_HMAC;
2175 case RTE_CRYPTO_AUTH_SHA224_HMAC:
2176 session->auth_key.alg = OP_ALG_ALGSEL_SHA224;
2177 session->auth_key.algmode = OP_ALG_AAI_HMAC;
2179 case RTE_CRYPTO_AUTH_SHA256_HMAC:
2180 session->auth_key.alg = OP_ALG_ALGSEL_SHA256;
2181 session->auth_key.algmode = OP_ALG_AAI_HMAC;
2183 case RTE_CRYPTO_AUTH_SHA384_HMAC:
2184 session->auth_key.alg = OP_ALG_ALGSEL_SHA384;
2185 session->auth_key.algmode = OP_ALG_AAI_HMAC;
2187 case RTE_CRYPTO_AUTH_SHA512_HMAC:
2188 session->auth_key.alg = OP_ALG_ALGSEL_SHA512;
2189 session->auth_key.algmode = OP_ALG_AAI_HMAC;
2192 DPAA_SEC_ERR("Crypto: Unsupported Auth specified %u",
2197 session->cipher_alg = cipher_xform->algo;
2199 switch (cipher_xform->algo) {
2200 case RTE_CRYPTO_CIPHER_AES_CBC:
2201 session->cipher_key.alg = OP_ALG_ALGSEL_AES;
2202 session->cipher_key.algmode = OP_ALG_AAI_CBC;
2204 case RTE_CRYPTO_CIPHER_3DES_CBC:
2205 session->cipher_key.alg = OP_ALG_ALGSEL_3DES;
2206 session->cipher_key.algmode = OP_ALG_AAI_CBC;
2208 case RTE_CRYPTO_CIPHER_AES_CTR:
2209 session->cipher_key.alg = OP_ALG_ALGSEL_AES;
2210 session->cipher_key.algmode = OP_ALG_AAI_CTR;
2213 DPAA_SEC_ERR("Crypto: Undefined Cipher specified %u",
2214 cipher_xform->algo);
2217 session->dir = (cipher_xform->op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
2222 rte_free(session->cipher_key.data);
2223 rte_free(session->auth_key.data);
2228 dpaa_sec_aead_init(struct rte_cryptodev *dev __rte_unused,
2229 struct rte_crypto_sym_xform *xform,
2230 dpaa_sec_session *session)
2232 session->aead_alg = xform->aead.algo;
2233 session->ctxt = DPAA_SEC_AEAD;
2234 session->iv.length = xform->aead.iv.length;
2235 session->iv.offset = xform->aead.iv.offset;
2236 session->auth_only_len = xform->aead.aad_length;
2237 session->aead_key.data = rte_zmalloc(NULL, xform->aead.key.length,
2238 RTE_CACHE_LINE_SIZE);
2239 if (session->aead_key.data == NULL && xform->aead.key.length > 0) {
2240 DPAA_SEC_ERR("No Memory for aead key\n");
2243 session->aead_key.length = xform->aead.key.length;
2244 session->digest_length = xform->aead.digest_length;
2246 memcpy(session->aead_key.data, xform->aead.key.data,
2247 xform->aead.key.length);
2249 switch (session->aead_alg) {
2250 case RTE_CRYPTO_AEAD_AES_GCM:
2251 session->aead_key.alg = OP_ALG_ALGSEL_AES;
2252 session->aead_key.algmode = OP_ALG_AAI_GCM;
2255 DPAA_SEC_ERR("unsupported AEAD alg %d", session->aead_alg);
2256 rte_free(session->aead_key.data);
2260 session->dir = (xform->aead.op == RTE_CRYPTO_AEAD_OP_ENCRYPT) ?
2266 static struct qman_fq *
2267 dpaa_sec_attach_rxq(struct dpaa_sec_dev_private *qi)
2271 for (i = 0; i < RTE_DPAA_MAX_RX_QUEUE; i++) {
2272 if (qi->inq_attach[i] == 0) {
2273 qi->inq_attach[i] = 1;
2277 DPAA_SEC_WARN("All session in use %u", qi->max_nb_sessions);
2283 dpaa_sec_detach_rxq(struct dpaa_sec_dev_private *qi, struct qman_fq *fq)
2287 for (i = 0; i < RTE_DPAA_MAX_RX_QUEUE; i++) {
2288 if (&qi->inq[i] == fq) {
2289 if (qman_retire_fq(fq, NULL) != 0)
2290 DPAA_SEC_WARN("Queue is not retired\n");
2292 qi->inq_attach[i] = 0;
2300 dpaa_sec_attach_sess_q(struct dpaa_sec_qp *qp, dpaa_sec_session *sess)
2304 sess->qp[rte_lcore_id() % MAX_DPAA_CORES] = qp;
2305 ret = dpaa_sec_prep_cdb(sess);
2307 DPAA_SEC_ERR("Unable to prepare sec cdb");
2310 if (unlikely(!RTE_PER_LCORE(dpaa_io))) {
2311 ret = rte_dpaa_portal_init((void *)0);
2313 DPAA_SEC_ERR("Failure in affining portal");
2317 ret = dpaa_sec_init_rx(sess->inq[rte_lcore_id() % MAX_DPAA_CORES],
2318 rte_dpaa_mem_vtop(&sess->cdb),
2319 qman_fq_fqid(&qp->outq));
2321 DPAA_SEC_ERR("Unable to init sec queue");
2327 dpaa_sec_set_session_parameters(struct rte_cryptodev *dev,
2328 struct rte_crypto_sym_xform *xform, void *sess)
2330 struct dpaa_sec_dev_private *internals = dev->data->dev_private;
2331 dpaa_sec_session *session = sess;
2335 PMD_INIT_FUNC_TRACE();
2337 if (unlikely(sess == NULL)) {
2338 DPAA_SEC_ERR("invalid session struct");
2341 memset(session, 0, sizeof(dpaa_sec_session));
2343 /* Default IV length = 0 */
2344 session->iv.length = 0;
2347 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER && xform->next == NULL) {
2348 session->auth_alg = RTE_CRYPTO_AUTH_NULL;
2349 ret = dpaa_sec_cipher_init(dev, xform, session);
2351 /* Authentication Only */
2352 } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
2353 xform->next == NULL) {
2354 session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
2355 session->ctxt = DPAA_SEC_AUTH;
2356 ret = dpaa_sec_auth_init(dev, xform, session);
2358 /* Cipher then Authenticate */
2359 } else if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
2360 xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
2361 if (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) {
2362 session->auth_cipher_text = 1;
2363 if (xform->cipher.algo == RTE_CRYPTO_CIPHER_NULL)
2364 ret = dpaa_sec_auth_init(dev, xform, session);
2365 else if (xform->next->auth.algo == RTE_CRYPTO_AUTH_NULL)
2366 ret = dpaa_sec_cipher_init(dev, xform, session);
2368 ret = dpaa_sec_chain_init(dev, xform, session);
2370 DPAA_SEC_ERR("Not supported: Auth then Cipher");
2373 /* Authenticate then Cipher */
2374 } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
2375 xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
2376 if (xform->next->cipher.op == RTE_CRYPTO_CIPHER_OP_DECRYPT) {
2377 session->auth_cipher_text = 0;
2378 if (xform->auth.algo == RTE_CRYPTO_AUTH_NULL)
2379 ret = dpaa_sec_cipher_init(dev, xform, session);
2380 else if (xform->next->cipher.algo
2381 == RTE_CRYPTO_CIPHER_NULL)
2382 ret = dpaa_sec_auth_init(dev, xform, session);
2384 ret = dpaa_sec_chain_init(dev, xform, session);
2386 DPAA_SEC_ERR("Not supported: Auth then Cipher");
2390 /* AEAD operation for AES-GCM kind of Algorithms */
2391 } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD &&
2392 xform->next == NULL) {
2393 ret = dpaa_sec_aead_init(dev, xform, session);
2396 DPAA_SEC_ERR("Invalid crypto type");
2400 DPAA_SEC_ERR("unable to init session");
2404 rte_spinlock_lock(&internals->lock);
2405 for (i = 0; i < MAX_DPAA_CORES; i++) {
2406 session->inq[i] = dpaa_sec_attach_rxq(internals);
2407 if (session->inq[i] == NULL) {
2408 DPAA_SEC_ERR("unable to attach sec queue");
2409 rte_spinlock_unlock(&internals->lock);
2413 rte_spinlock_unlock(&internals->lock);
2418 rte_free(session->cipher_key.data);
2419 rte_free(session->auth_key.data);
2420 memset(session, 0, sizeof(dpaa_sec_session));
2426 dpaa_sec_sym_session_configure(struct rte_cryptodev *dev,
2427 struct rte_crypto_sym_xform *xform,
2428 struct rte_cryptodev_sym_session *sess,
2429 struct rte_mempool *mempool)
2431 void *sess_private_data;
2434 PMD_INIT_FUNC_TRACE();
2436 if (rte_mempool_get(mempool, &sess_private_data)) {
2437 DPAA_SEC_ERR("Couldn't get object from session mempool");
2441 ret = dpaa_sec_set_session_parameters(dev, xform, sess_private_data);
2443 DPAA_SEC_ERR("failed to configure session parameters");
2445 /* Return session to mempool */
2446 rte_mempool_put(mempool, sess_private_data);
2450 set_sym_session_private_data(sess, dev->driver_id,
2458 free_session_memory(struct rte_cryptodev *dev, dpaa_sec_session *s)
2460 struct dpaa_sec_dev_private *qi = dev->data->dev_private;
2461 struct rte_mempool *sess_mp = rte_mempool_from_obj((void *)s);
2464 for (i = 0; i < MAX_DPAA_CORES; i++) {
2466 dpaa_sec_detach_rxq(qi, s->inq[i]);
2470 rte_free(s->cipher_key.data);
2471 rte_free(s->auth_key.data);
2472 memset(s, 0, sizeof(dpaa_sec_session));
2473 rte_mempool_put(sess_mp, (void *)s);
2476 /** Clear the memory of session so it doesn't leave key material behind */
2478 dpaa_sec_sym_session_clear(struct rte_cryptodev *dev,
2479 struct rte_cryptodev_sym_session *sess)
2481 PMD_INIT_FUNC_TRACE();
2482 uint8_t index = dev->driver_id;
2483 void *sess_priv = get_sym_session_private_data(sess, index);
2484 dpaa_sec_session *s = (dpaa_sec_session *)sess_priv;
2487 free_session_memory(dev, s);
2488 set_sym_session_private_data(sess, index, NULL);
2492 #ifdef RTE_LIBRTE_SECURITY
2494 dpaa_sec_ipsec_aead_init(struct rte_crypto_aead_xform *aead_xform,
2495 struct rte_security_ipsec_xform *ipsec_xform,
2496 dpaa_sec_session *session)
2498 PMD_INIT_FUNC_TRACE();
2500 session->aead_key.data = rte_zmalloc(NULL, aead_xform->key.length,
2501 RTE_CACHE_LINE_SIZE);
2502 if (session->aead_key.data == NULL && aead_xform->key.length > 0) {
2503 DPAA_SEC_ERR("No Memory for aead key");
2506 memcpy(session->aead_key.data, aead_xform->key.data,
2507 aead_xform->key.length);
2509 session->digest_length = aead_xform->digest_length;
2510 session->aead_key.length = aead_xform->key.length;
2512 switch (aead_xform->algo) {
2513 case RTE_CRYPTO_AEAD_AES_GCM:
2514 switch (session->digest_length) {
2516 session->aead_key.alg = OP_PCL_IPSEC_AES_GCM8;
2519 session->aead_key.alg = OP_PCL_IPSEC_AES_GCM12;
2522 session->aead_key.alg = OP_PCL_IPSEC_AES_GCM16;
2525 DPAA_SEC_ERR("Crypto: Undefined GCM digest %d",
2526 session->digest_length);
2529 if (session->dir == DIR_ENC) {
2530 memcpy(session->encap_pdb.gcm.salt,
2531 (uint8_t *)&(ipsec_xform->salt), 4);
2533 memcpy(session->decap_pdb.gcm.salt,
2534 (uint8_t *)&(ipsec_xform->salt), 4);
2536 session->aead_key.algmode = OP_ALG_AAI_GCM;
2537 session->aead_alg = RTE_CRYPTO_AEAD_AES_GCM;
2540 DPAA_SEC_ERR("Crypto: Undefined AEAD specified %u",
2548 dpaa_sec_ipsec_proto_init(struct rte_crypto_cipher_xform *cipher_xform,
2549 struct rte_crypto_auth_xform *auth_xform,
2550 struct rte_security_ipsec_xform *ipsec_xform,
2551 dpaa_sec_session *session)
2554 session->cipher_key.data = rte_zmalloc(NULL,
2555 cipher_xform->key.length,
2556 RTE_CACHE_LINE_SIZE);
2557 if (session->cipher_key.data == NULL &&
2558 cipher_xform->key.length > 0) {
2559 DPAA_SEC_ERR("No Memory for cipher key");
2563 session->cipher_key.length = cipher_xform->key.length;
2564 memcpy(session->cipher_key.data, cipher_xform->key.data,
2565 cipher_xform->key.length);
2566 session->cipher_alg = cipher_xform->algo;
2568 session->cipher_key.data = NULL;
2569 session->cipher_key.length = 0;
2570 session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
2574 session->auth_key.data = rte_zmalloc(NULL,
2575 auth_xform->key.length,
2576 RTE_CACHE_LINE_SIZE);
2577 if (session->auth_key.data == NULL &&
2578 auth_xform->key.length > 0) {
2579 DPAA_SEC_ERR("No Memory for auth key");
2582 session->auth_key.length = auth_xform->key.length;
2583 memcpy(session->auth_key.data, auth_xform->key.data,
2584 auth_xform->key.length);
2585 session->auth_alg = auth_xform->algo;
2586 session->digest_length = auth_xform->digest_length;
2588 session->auth_key.data = NULL;
2589 session->auth_key.length = 0;
2590 session->auth_alg = RTE_CRYPTO_AUTH_NULL;
2593 switch (session->auth_alg) {
2594 case RTE_CRYPTO_AUTH_SHA1_HMAC:
2595 session->auth_key.alg = OP_PCL_IPSEC_HMAC_SHA1_96;
2596 session->auth_key.algmode = OP_ALG_AAI_HMAC;
2598 case RTE_CRYPTO_AUTH_MD5_HMAC:
2599 session->auth_key.alg = OP_PCL_IPSEC_HMAC_MD5_96;
2600 session->auth_key.algmode = OP_ALG_AAI_HMAC;
2602 case RTE_CRYPTO_AUTH_SHA256_HMAC:
2603 session->auth_key.alg = OP_PCL_IPSEC_HMAC_SHA2_256_128;
2604 session->auth_key.algmode = OP_ALG_AAI_HMAC;
2605 if (session->digest_length != 16)
2607 "+++Using sha256-hmac truncated len is non-standard,"
2608 "it will not work with lookaside proto");
2610 case RTE_CRYPTO_AUTH_SHA384_HMAC:
2611 session->auth_key.alg = OP_PCL_IPSEC_HMAC_SHA2_384_192;
2612 session->auth_key.algmode = OP_ALG_AAI_HMAC;
2614 case RTE_CRYPTO_AUTH_SHA512_HMAC:
2615 session->auth_key.alg = OP_PCL_IPSEC_HMAC_SHA2_512_256;
2616 session->auth_key.algmode = OP_ALG_AAI_HMAC;
2618 case RTE_CRYPTO_AUTH_AES_CMAC:
2619 session->auth_key.alg = OP_PCL_IPSEC_AES_CMAC_96;
2621 case RTE_CRYPTO_AUTH_NULL:
2622 session->auth_key.alg = OP_PCL_IPSEC_HMAC_NULL;
2624 case RTE_CRYPTO_AUTH_SHA224_HMAC:
2625 case RTE_CRYPTO_AUTH_AES_XCBC_MAC:
2626 case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
2627 case RTE_CRYPTO_AUTH_SHA1:
2628 case RTE_CRYPTO_AUTH_SHA256:
2629 case RTE_CRYPTO_AUTH_SHA512:
2630 case RTE_CRYPTO_AUTH_SHA224:
2631 case RTE_CRYPTO_AUTH_SHA384:
2632 case RTE_CRYPTO_AUTH_MD5:
2633 case RTE_CRYPTO_AUTH_AES_GMAC:
2634 case RTE_CRYPTO_AUTH_KASUMI_F9:
2635 case RTE_CRYPTO_AUTH_AES_CBC_MAC:
2636 case RTE_CRYPTO_AUTH_ZUC_EIA3:
2637 DPAA_SEC_ERR("Crypto: Unsupported auth alg %u",
2641 DPAA_SEC_ERR("Crypto: Undefined Auth specified %u",
2646 switch (session->cipher_alg) {
2647 case RTE_CRYPTO_CIPHER_AES_CBC:
2648 session->cipher_key.alg = OP_PCL_IPSEC_AES_CBC;
2649 session->cipher_key.algmode = OP_ALG_AAI_CBC;
2651 case RTE_CRYPTO_CIPHER_3DES_CBC:
2652 session->cipher_key.alg = OP_PCL_IPSEC_3DES;
2653 session->cipher_key.algmode = OP_ALG_AAI_CBC;
2655 case RTE_CRYPTO_CIPHER_AES_CTR:
2656 session->cipher_key.alg = OP_PCL_IPSEC_AES_CTR;
2657 session->cipher_key.algmode = OP_ALG_AAI_CTR;
2658 if (session->dir == DIR_ENC) {
2659 session->encap_pdb.ctr.ctr_initial = 0x00000001;
2660 session->encap_pdb.ctr.ctr_nonce = ipsec_xform->salt;
2662 session->decap_pdb.ctr.ctr_initial = 0x00000001;
2663 session->decap_pdb.ctr.ctr_nonce = ipsec_xform->salt;
2666 case RTE_CRYPTO_CIPHER_NULL:
2667 session->cipher_key.alg = OP_PCL_IPSEC_NULL;
2669 case RTE_CRYPTO_CIPHER_SNOW3G_UEA2:
2670 case RTE_CRYPTO_CIPHER_ZUC_EEA3:
2671 case RTE_CRYPTO_CIPHER_3DES_ECB:
2672 case RTE_CRYPTO_CIPHER_AES_ECB:
2673 case RTE_CRYPTO_CIPHER_KASUMI_F8:
2674 DPAA_SEC_ERR("Crypto: Unsupported Cipher alg %u",
2675 session->cipher_alg);
2678 DPAA_SEC_ERR("Crypto: Undefined Cipher specified %u",
2679 session->cipher_alg);
2687 dpaa_sec_set_ipsec_session(__rte_unused struct rte_cryptodev *dev,
2688 struct rte_security_session_conf *conf,
2691 struct dpaa_sec_dev_private *internals = dev->data->dev_private;
2692 struct rte_security_ipsec_xform *ipsec_xform = &conf->ipsec;
2693 struct rte_crypto_auth_xform *auth_xform = NULL;
2694 struct rte_crypto_cipher_xform *cipher_xform = NULL;
2695 struct rte_crypto_aead_xform *aead_xform = NULL;
2696 dpaa_sec_session *session = (dpaa_sec_session *)sess;
2700 PMD_INIT_FUNC_TRACE();
2702 memset(session, 0, sizeof(dpaa_sec_session));
2703 session->proto_alg = conf->protocol;
2704 session->ctxt = DPAA_SEC_IPSEC;
2706 if (ipsec_xform->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS)
2707 session->dir = DIR_ENC;
2709 session->dir = DIR_DEC;
2711 if (conf->crypto_xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
2712 cipher_xform = &conf->crypto_xform->cipher;
2713 if (conf->crypto_xform->next)
2714 auth_xform = &conf->crypto_xform->next->auth;
2715 ret = dpaa_sec_ipsec_proto_init(cipher_xform, auth_xform,
2716 ipsec_xform, session);
2717 } else if (conf->crypto_xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
2718 auth_xform = &conf->crypto_xform->auth;
2719 if (conf->crypto_xform->next)
2720 cipher_xform = &conf->crypto_xform->next->cipher;
2721 ret = dpaa_sec_ipsec_proto_init(cipher_xform, auth_xform,
2722 ipsec_xform, session);
2723 } else if (conf->crypto_xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
2724 aead_xform = &conf->crypto_xform->aead;
2725 ret = dpaa_sec_ipsec_aead_init(aead_xform,
2726 ipsec_xform, session);
2728 DPAA_SEC_ERR("XFORM not specified");
2733 DPAA_SEC_ERR("Failed to process xform");
2737 if (ipsec_xform->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
2738 if (ipsec_xform->tunnel.type ==
2739 RTE_SECURITY_IPSEC_TUNNEL_IPV4) {
2740 session->ip4_hdr.ip_v = IPVERSION;
2741 session->ip4_hdr.ip_hl = 5;
2742 session->ip4_hdr.ip_len = rte_cpu_to_be_16(
2743 sizeof(session->ip4_hdr));
2744 session->ip4_hdr.ip_tos = ipsec_xform->tunnel.ipv4.dscp;
2745 session->ip4_hdr.ip_id = 0;
2746 session->ip4_hdr.ip_off = 0;
2747 session->ip4_hdr.ip_ttl = ipsec_xform->tunnel.ipv4.ttl;
2748 session->ip4_hdr.ip_p = (ipsec_xform->proto ==
2749 RTE_SECURITY_IPSEC_SA_PROTO_ESP) ?
2750 IPPROTO_ESP : IPPROTO_AH;
2751 session->ip4_hdr.ip_sum = 0;
2752 session->ip4_hdr.ip_src =
2753 ipsec_xform->tunnel.ipv4.src_ip;
2754 session->ip4_hdr.ip_dst =
2755 ipsec_xform->tunnel.ipv4.dst_ip;
2756 session->ip4_hdr.ip_sum = calc_chksum((uint16_t *)
2757 (void *)&session->ip4_hdr,
2759 session->encap_pdb.ip_hdr_len = sizeof(struct ip);
2760 } else if (ipsec_xform->tunnel.type ==
2761 RTE_SECURITY_IPSEC_TUNNEL_IPV6) {
2762 session->ip6_hdr.vtc_flow = rte_cpu_to_be_32(
2763 DPAA_IPv6_DEFAULT_VTC_FLOW |
2764 ((ipsec_xform->tunnel.ipv6.dscp <<
2765 RTE_IPV6_HDR_TC_SHIFT) &
2766 RTE_IPV6_HDR_TC_MASK) |
2767 ((ipsec_xform->tunnel.ipv6.flabel <<
2768 RTE_IPV6_HDR_FL_SHIFT) &
2769 RTE_IPV6_HDR_FL_MASK));
2770 /* Payload length will be updated by HW */
2771 session->ip6_hdr.payload_len = 0;
2772 session->ip6_hdr.hop_limits =
2773 ipsec_xform->tunnel.ipv6.hlimit;
2774 session->ip6_hdr.proto = (ipsec_xform->proto ==
2775 RTE_SECURITY_IPSEC_SA_PROTO_ESP) ?
2776 IPPROTO_ESP : IPPROTO_AH;
2777 memcpy(&session->ip6_hdr.src_addr,
2778 &ipsec_xform->tunnel.ipv6.src_addr, 16);
2779 memcpy(&session->ip6_hdr.dst_addr,
2780 &ipsec_xform->tunnel.ipv6.dst_addr, 16);
2781 session->encap_pdb.ip_hdr_len =
2782 sizeof(struct rte_ipv6_hdr);
2784 session->encap_pdb.options =
2785 (IPVERSION << PDBNH_ESP_ENCAP_SHIFT) |
2786 PDBOPTS_ESP_OIHI_PDB_INL |
2788 PDBHMO_ESP_ENCAP_DTTL |
2790 if (ipsec_xform->options.esn)
2791 session->encap_pdb.options |= PDBOPTS_ESP_ESN;
2792 session->encap_pdb.spi = ipsec_xform->spi;
2794 } else if (ipsec_xform->direction ==
2795 RTE_SECURITY_IPSEC_SA_DIR_INGRESS) {
2796 if (ipsec_xform->tunnel.type == RTE_SECURITY_IPSEC_TUNNEL_IPV4)
2797 session->decap_pdb.options = sizeof(struct ip) << 16;
2799 session->decap_pdb.options =
2800 sizeof(struct rte_ipv6_hdr) << 16;
2801 if (ipsec_xform->options.esn)
2802 session->decap_pdb.options |= PDBOPTS_ESP_ESN;
2803 if (ipsec_xform->replay_win_sz) {
2805 win_sz = rte_align32pow2(ipsec_xform->replay_win_sz);
2814 session->decap_pdb.options |= PDBOPTS_ESP_ARS32;
2817 session->decap_pdb.options |= PDBOPTS_ESP_ARS64;
2820 session->decap_pdb.options |=
2826 rte_spinlock_lock(&internals->lock);
2827 for (i = 0; i < MAX_DPAA_CORES; i++) {
2828 session->inq[i] = dpaa_sec_attach_rxq(internals);
2829 if (session->inq[i] == NULL) {
2830 DPAA_SEC_ERR("unable to attach sec queue");
2831 rte_spinlock_unlock(&internals->lock);
2835 rte_spinlock_unlock(&internals->lock);
2839 rte_free(session->auth_key.data);
2840 rte_free(session->cipher_key.data);
2841 memset(session, 0, sizeof(dpaa_sec_session));
2846 dpaa_sec_set_pdcp_session(struct rte_cryptodev *dev,
2847 struct rte_security_session_conf *conf,
2850 struct rte_security_pdcp_xform *pdcp_xform = &conf->pdcp;
2851 struct rte_crypto_sym_xform *xform = conf->crypto_xform;
2852 struct rte_crypto_auth_xform *auth_xform = NULL;
2853 struct rte_crypto_cipher_xform *cipher_xform = NULL;
2854 dpaa_sec_session *session = (dpaa_sec_session *)sess;
2855 struct dpaa_sec_dev_private *dev_priv = dev->data->dev_private;
2858 PMD_INIT_FUNC_TRACE();
2860 memset(session, 0, sizeof(dpaa_sec_session));
2862 /* find xfrm types */
2863 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
2864 cipher_xform = &xform->cipher;
2865 if (xform->next != NULL)
2866 auth_xform = &xform->next->auth;
2867 } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
2868 auth_xform = &xform->auth;
2869 if (xform->next != NULL)
2870 cipher_xform = &xform->next->cipher;
2872 DPAA_SEC_ERR("Invalid crypto type");
2876 session->proto_alg = conf->protocol;
2877 session->ctxt = DPAA_SEC_PDCP;
2880 switch (cipher_xform->algo) {
2881 case RTE_CRYPTO_CIPHER_SNOW3G_UEA2:
2882 session->cipher_key.alg = PDCP_CIPHER_TYPE_SNOW;
2884 case RTE_CRYPTO_CIPHER_ZUC_EEA3:
2885 session->cipher_key.alg = PDCP_CIPHER_TYPE_ZUC;
2887 case RTE_CRYPTO_CIPHER_AES_CTR:
2888 session->cipher_key.alg = PDCP_CIPHER_TYPE_AES;
2890 case RTE_CRYPTO_CIPHER_NULL:
2891 session->cipher_key.alg = PDCP_CIPHER_TYPE_NULL;
2894 DPAA_SEC_ERR("Crypto: Undefined Cipher specified %u",
2895 session->cipher_alg);
2899 session->cipher_key.data = rte_zmalloc(NULL,
2900 cipher_xform->key.length,
2901 RTE_CACHE_LINE_SIZE);
2902 if (session->cipher_key.data == NULL &&
2903 cipher_xform->key.length > 0) {
2904 DPAA_SEC_ERR("No Memory for cipher key");
2907 session->cipher_key.length = cipher_xform->key.length;
2908 memcpy(session->cipher_key.data, cipher_xform->key.data,
2909 cipher_xform->key.length);
2910 session->dir = (cipher_xform->op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
2912 session->cipher_alg = cipher_xform->algo;
2914 session->cipher_key.data = NULL;
2915 session->cipher_key.length = 0;
2916 session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
2917 session->dir = DIR_ENC;
2920 if (pdcp_xform->domain == RTE_SECURITY_PDCP_MODE_CONTROL) {
2921 if (pdcp_xform->sn_size != RTE_SECURITY_PDCP_SN_SIZE_5 &&
2922 pdcp_xform->sn_size != RTE_SECURITY_PDCP_SN_SIZE_12) {
2924 "PDCP Seq Num size should be 5/12 bits for cmode");
2930 switch (auth_xform->algo) {
2931 case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
2932 session->auth_key.alg = PDCP_AUTH_TYPE_SNOW;
2934 case RTE_CRYPTO_AUTH_ZUC_EIA3:
2935 session->auth_key.alg = PDCP_AUTH_TYPE_ZUC;
2937 case RTE_CRYPTO_AUTH_AES_CMAC:
2938 session->auth_key.alg = PDCP_AUTH_TYPE_AES;
2940 case RTE_CRYPTO_AUTH_NULL:
2941 session->auth_key.alg = PDCP_AUTH_TYPE_NULL;
2944 DPAA_SEC_ERR("Crypto: Unsupported auth alg %u",
2946 rte_free(session->cipher_key.data);
2949 session->auth_key.data = rte_zmalloc(NULL,
2950 auth_xform->key.length,
2951 RTE_CACHE_LINE_SIZE);
2952 if (!session->auth_key.data &&
2953 auth_xform->key.length > 0) {
2954 DPAA_SEC_ERR("No Memory for auth key");
2955 rte_free(session->cipher_key.data);
2958 session->auth_key.length = auth_xform->key.length;
2959 memcpy(session->auth_key.data, auth_xform->key.data,
2960 auth_xform->key.length);
2961 session->auth_alg = auth_xform->algo;
2963 session->auth_key.data = NULL;
2964 session->auth_key.length = 0;
2965 session->auth_alg = 0;
2967 session->pdcp.domain = pdcp_xform->domain;
2968 session->pdcp.bearer = pdcp_xform->bearer;
2969 session->pdcp.pkt_dir = pdcp_xform->pkt_dir;
2970 session->pdcp.sn_size = pdcp_xform->sn_size;
2971 session->pdcp.hfn = pdcp_xform->hfn;
2972 session->pdcp.hfn_threshold = pdcp_xform->hfn_threshold;
2973 session->pdcp.hfn_ovd = pdcp_xform->hfn_ovrd;
2974 session->pdcp.hfn_ovd_offset = cipher_xform->iv.offset;
2976 rte_spinlock_lock(&dev_priv->lock);
2977 for (i = 0; i < MAX_DPAA_CORES; i++) {
2978 session->inq[i] = dpaa_sec_attach_rxq(dev_priv);
2979 if (session->inq[i] == NULL) {
2980 DPAA_SEC_ERR("unable to attach sec queue");
2981 rte_spinlock_unlock(&dev_priv->lock);
2985 rte_spinlock_unlock(&dev_priv->lock);
2988 rte_free(session->auth_key.data);
2989 rte_free(session->cipher_key.data);
2990 memset(session, 0, sizeof(dpaa_sec_session));
2995 dpaa_sec_security_session_create(void *dev,
2996 struct rte_security_session_conf *conf,
2997 struct rte_security_session *sess,
2998 struct rte_mempool *mempool)
3000 void *sess_private_data;
3001 struct rte_cryptodev *cdev = (struct rte_cryptodev *)dev;
3004 if (rte_mempool_get(mempool, &sess_private_data)) {
3005 DPAA_SEC_ERR("Couldn't get object from session mempool");
3009 switch (conf->protocol) {
3010 case RTE_SECURITY_PROTOCOL_IPSEC:
3011 ret = dpaa_sec_set_ipsec_session(cdev, conf,
3014 case RTE_SECURITY_PROTOCOL_PDCP:
3015 ret = dpaa_sec_set_pdcp_session(cdev, conf,
3018 case RTE_SECURITY_PROTOCOL_MACSEC:
3024 DPAA_SEC_ERR("failed to configure session parameters");
3025 /* Return session to mempool */
3026 rte_mempool_put(mempool, sess_private_data);
3030 set_sec_session_private_data(sess, sess_private_data);
3035 /** Clear the memory of session so it doesn't leave key material behind */
3037 dpaa_sec_security_session_destroy(void *dev __rte_unused,
3038 struct rte_security_session *sess)
3040 PMD_INIT_FUNC_TRACE();
3041 void *sess_priv = get_sec_session_private_data(sess);
3042 dpaa_sec_session *s = (dpaa_sec_session *)sess_priv;
3045 free_session_memory((struct rte_cryptodev *)dev, s);
3046 set_sec_session_private_data(sess, NULL);
3052 dpaa_sec_dev_configure(struct rte_cryptodev *dev __rte_unused,
3053 struct rte_cryptodev_config *config __rte_unused)
3055 PMD_INIT_FUNC_TRACE();
3061 dpaa_sec_dev_start(struct rte_cryptodev *dev __rte_unused)
3063 PMD_INIT_FUNC_TRACE();
3068 dpaa_sec_dev_stop(struct rte_cryptodev *dev __rte_unused)
3070 PMD_INIT_FUNC_TRACE();
3074 dpaa_sec_dev_close(struct rte_cryptodev *dev)
3076 PMD_INIT_FUNC_TRACE();
3085 dpaa_sec_dev_infos_get(struct rte_cryptodev *dev,
3086 struct rte_cryptodev_info *info)
3088 struct dpaa_sec_dev_private *internals = dev->data->dev_private;
3090 PMD_INIT_FUNC_TRACE();
3092 info->max_nb_queue_pairs = internals->max_nb_queue_pairs;
3093 info->feature_flags = dev->feature_flags;
3094 info->capabilities = dpaa_sec_capabilities;
3095 info->sym.max_nb_sessions = internals->max_nb_sessions;
3096 info->driver_id = cryptodev_driver_id;
3100 static enum qman_cb_dqrr_result
3101 dpaa_sec_process_parallel_event(void *event,
3102 struct qman_portal *qm __always_unused,
3103 struct qman_fq *outq,
3104 const struct qm_dqrr_entry *dqrr,
3107 const struct qm_fd *fd;
3108 struct dpaa_sec_job *job;
3109 struct dpaa_sec_op_ctx *ctx;
3110 struct rte_event *ev = (struct rte_event *)event;
3114 /* sg is embedded in an op ctx,
3115 * sg[0] is for output
3118 job = rte_dpaa_mem_ptov(qm_fd_addr_get64(fd));
3120 ctx = container_of(job, struct dpaa_sec_op_ctx, job);
3121 ctx->fd_status = fd->status;
3122 if (ctx->op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
3123 struct qm_sg_entry *sg_out;
3126 sg_out = &job->sg[0];
3127 hw_sg_to_cpu(sg_out);
3128 len = sg_out->length;
3129 ctx->op->sym->m_src->pkt_len = len;
3130 ctx->op->sym->m_src->data_len = len;
3132 if (!ctx->fd_status) {
3133 ctx->op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
3135 DPAA_SEC_DP_WARN("SEC return err: 0x%x", ctx->fd_status);
3136 ctx->op->status = RTE_CRYPTO_OP_STATUS_ERROR;
3138 ev->event_ptr = (void *)ctx->op;
3140 ev->flow_id = outq->ev.flow_id;
3141 ev->sub_event_type = outq->ev.sub_event_type;
3142 ev->event_type = RTE_EVENT_TYPE_CRYPTODEV;
3143 ev->op = RTE_EVENT_OP_NEW;
3144 ev->sched_type = outq->ev.sched_type;
3145 ev->queue_id = outq->ev.queue_id;
3146 ev->priority = outq->ev.priority;
3147 *bufs = (void *)ctx->op;
3149 rte_mempool_put(ctx->ctx_pool, (void *)ctx);
3151 return qman_cb_dqrr_consume;
3154 static enum qman_cb_dqrr_result
3155 dpaa_sec_process_atomic_event(void *event,
3156 struct qman_portal *qm __rte_unused,
3157 struct qman_fq *outq,
3158 const struct qm_dqrr_entry *dqrr,
3162 const struct qm_fd *fd;
3163 struct dpaa_sec_job *job;
3164 struct dpaa_sec_op_ctx *ctx;
3165 struct rte_event *ev = (struct rte_event *)event;
3169 /* sg is embedded in an op ctx,
3170 * sg[0] is for output
3173 job = rte_dpaa_mem_ptov(qm_fd_addr_get64(fd));
3175 ctx = container_of(job, struct dpaa_sec_op_ctx, job);
3176 ctx->fd_status = fd->status;
3177 if (ctx->op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
3178 struct qm_sg_entry *sg_out;
3181 sg_out = &job->sg[0];
3182 hw_sg_to_cpu(sg_out);
3183 len = sg_out->length;
3184 ctx->op->sym->m_src->pkt_len = len;
3185 ctx->op->sym->m_src->data_len = len;
3187 if (!ctx->fd_status) {
3188 ctx->op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
3190 DPAA_SEC_DP_WARN("SEC return err: 0x%x", ctx->fd_status);
3191 ctx->op->status = RTE_CRYPTO_OP_STATUS_ERROR;
3193 ev->event_ptr = (void *)ctx->op;
3194 ev->flow_id = outq->ev.flow_id;
3195 ev->sub_event_type = outq->ev.sub_event_type;
3196 ev->event_type = RTE_EVENT_TYPE_CRYPTODEV;
3197 ev->op = RTE_EVENT_OP_NEW;
3198 ev->sched_type = outq->ev.sched_type;
3199 ev->queue_id = outq->ev.queue_id;
3200 ev->priority = outq->ev.priority;
3202 /* Save active dqrr entries */
3203 index = ((uintptr_t)dqrr >> 6) & (16/*QM_DQRR_SIZE*/ - 1);
3204 DPAA_PER_LCORE_DQRR_SIZE++;
3205 DPAA_PER_LCORE_DQRR_HELD |= 1 << index;
3206 DPAA_PER_LCORE_DQRR_MBUF(index) = ctx->op->sym->m_src;
3207 ev->impl_opaque = index + 1;
3208 ctx->op->sym->m_src->seqn = (uint32_t)index + 1;
3209 *bufs = (void *)ctx->op;
3211 rte_mempool_put(ctx->ctx_pool, (void *)ctx);
3213 return qman_cb_dqrr_defer;
3217 dpaa_sec_eventq_attach(const struct rte_cryptodev *dev,
3220 const struct rte_event *event)
3222 struct dpaa_sec_qp *qp = dev->data->queue_pairs[qp_id];
3223 struct qm_mcc_initfq opts = {0};
3227 opts.we_mask = QM_INITFQ_WE_DESTWQ | QM_INITFQ_WE_FQCTRL |
3228 QM_INITFQ_WE_CONTEXTA | QM_INITFQ_WE_CONTEXTB;
3229 opts.fqd.dest.channel = ch_id;
3231 switch (event->sched_type) {
3232 case RTE_SCHED_TYPE_ATOMIC:
3233 opts.fqd.fq_ctrl |= QM_FQCTRL_HOLDACTIVE;
3234 /* Reset FQCTRL_AVOIDBLOCK bit as it is unnecessary
3235 * configuration with HOLD_ACTIVE setting
3237 opts.fqd.fq_ctrl &= (~QM_FQCTRL_AVOIDBLOCK);
3238 qp->outq.cb.dqrr_dpdk_cb = dpaa_sec_process_atomic_event;
3240 case RTE_SCHED_TYPE_ORDERED:
3241 DPAA_SEC_ERR("Ordered queue schedule type is not supported\n");
3244 opts.fqd.fq_ctrl |= QM_FQCTRL_AVOIDBLOCK;
3245 qp->outq.cb.dqrr_dpdk_cb = dpaa_sec_process_parallel_event;
3249 ret = qman_init_fq(&qp->outq, QMAN_INITFQ_FLAG_SCHED, &opts);
3250 if (unlikely(ret)) {
3251 DPAA_SEC_ERR("unable to init caam source fq!");
3255 memcpy(&qp->outq.ev, event, sizeof(struct rte_event));
3261 dpaa_sec_eventq_detach(const struct rte_cryptodev *dev,
3264 struct qm_mcc_initfq opts = {0};
3266 struct dpaa_sec_qp *qp = dev->data->queue_pairs[qp_id];
3268 opts.we_mask = QM_INITFQ_WE_DESTWQ | QM_INITFQ_WE_FQCTRL |
3269 QM_INITFQ_WE_CONTEXTA | QM_INITFQ_WE_CONTEXTB;
3270 qp->outq.cb.dqrr = dqrr_out_fq_cb_rx;
3271 qp->outq.cb.ern = ern_sec_fq_handler;
3272 qman_retire_fq(&qp->outq, NULL);
3273 qman_oos_fq(&qp->outq);
3274 ret = qman_init_fq(&qp->outq, 0, &opts);
3276 RTE_LOG(ERR, PMD, "Error in qman_init_fq: ret: %d\n", ret);
3277 qp->outq.cb.dqrr = NULL;
3282 static struct rte_cryptodev_ops crypto_ops = {
3283 .dev_configure = dpaa_sec_dev_configure,
3284 .dev_start = dpaa_sec_dev_start,
3285 .dev_stop = dpaa_sec_dev_stop,
3286 .dev_close = dpaa_sec_dev_close,
3287 .dev_infos_get = dpaa_sec_dev_infos_get,
3288 .queue_pair_setup = dpaa_sec_queue_pair_setup,
3289 .queue_pair_release = dpaa_sec_queue_pair_release,
3290 .sym_session_get_size = dpaa_sec_sym_session_get_size,
3291 .sym_session_configure = dpaa_sec_sym_session_configure,
3292 .sym_session_clear = dpaa_sec_sym_session_clear
3295 #ifdef RTE_LIBRTE_SECURITY
3296 static const struct rte_security_capability *
3297 dpaa_sec_capabilities_get(void *device __rte_unused)
3299 return dpaa_sec_security_cap;
3302 static const struct rte_security_ops dpaa_sec_security_ops = {
3303 .session_create = dpaa_sec_security_session_create,
3304 .session_update = NULL,
3305 .session_stats_get = NULL,
3306 .session_destroy = dpaa_sec_security_session_destroy,
3307 .set_pkt_metadata = NULL,
3308 .capabilities_get = dpaa_sec_capabilities_get
3312 dpaa_sec_uninit(struct rte_cryptodev *dev)
3314 struct dpaa_sec_dev_private *internals;
3319 internals = dev->data->dev_private;
3320 rte_free(dev->security_ctx);
3322 rte_free(internals);
3324 DPAA_SEC_INFO("Closing DPAA_SEC device %s on numa socket %u",
3325 dev->data->name, rte_socket_id());
3331 dpaa_sec_dev_init(struct rte_cryptodev *cryptodev)
3333 struct dpaa_sec_dev_private *internals;
3334 #ifdef RTE_LIBRTE_SECURITY
3335 struct rte_security_ctx *security_instance;
3337 struct dpaa_sec_qp *qp;
3341 PMD_INIT_FUNC_TRACE();
3343 cryptodev->driver_id = cryptodev_driver_id;
3344 cryptodev->dev_ops = &crypto_ops;
3346 cryptodev->enqueue_burst = dpaa_sec_enqueue_burst;
3347 cryptodev->dequeue_burst = dpaa_sec_dequeue_burst;
3348 cryptodev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
3349 RTE_CRYPTODEV_FF_HW_ACCELERATED |
3350 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
3351 RTE_CRYPTODEV_FF_SECURITY |
3352 RTE_CRYPTODEV_FF_IN_PLACE_SGL |
3353 RTE_CRYPTODEV_FF_OOP_SGL_IN_SGL_OUT |
3354 RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT |
3355 RTE_CRYPTODEV_FF_OOP_LB_IN_SGL_OUT |
3356 RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT;
3358 internals = cryptodev->data->dev_private;
3359 internals->max_nb_queue_pairs = RTE_DPAA_MAX_NB_SEC_QPS;
3360 internals->max_nb_sessions = RTE_DPAA_SEC_PMD_MAX_NB_SESSIONS;
3363 * For secondary processes, we don't initialise any further as primary
3364 * has already done this work. Only check we don't need a different
3367 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
3368 DPAA_SEC_WARN("Device already init by primary process");
3371 #ifdef RTE_LIBRTE_SECURITY
3372 /* Initialize security_ctx only for primary process*/
3373 security_instance = rte_malloc("rte_security_instances_ops",
3374 sizeof(struct rte_security_ctx), 0);
3375 if (security_instance == NULL)
3377 security_instance->device = (void *)cryptodev;
3378 security_instance->ops = &dpaa_sec_security_ops;
3379 security_instance->sess_cnt = 0;
3380 cryptodev->security_ctx = security_instance;
3382 rte_spinlock_init(&internals->lock);
3383 for (i = 0; i < internals->max_nb_queue_pairs; i++) {
3384 /* init qman fq for queue pair */
3385 qp = &internals->qps[i];
3386 ret = dpaa_sec_init_tx(&qp->outq);
3388 DPAA_SEC_ERR("config tx of queue pair %d", i);
3393 flags = QMAN_FQ_FLAG_LOCKED | QMAN_FQ_FLAG_DYNAMIC_FQID |
3394 QMAN_FQ_FLAG_TO_DCPORTAL;
3395 for (i = 0; i < RTE_DPAA_MAX_RX_QUEUE; i++) {
3396 /* create rx qman fq for sessions*/
3397 ret = qman_create_fq(0, flags, &internals->inq[i]);
3398 if (unlikely(ret != 0)) {
3399 DPAA_SEC_ERR("sec qman_create_fq failed");
3404 RTE_LOG(INFO, PMD, "%s cryptodev init\n", cryptodev->data->name);
3408 DPAA_SEC_ERR("driver %s: create failed\n", cryptodev->data->name);
3410 dpaa_sec_uninit(cryptodev);
3415 cryptodev_dpaa_sec_probe(struct rte_dpaa_driver *dpaa_drv __rte_unused,
3416 struct rte_dpaa_device *dpaa_dev)
3418 struct rte_cryptodev *cryptodev;
3419 char cryptodev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
3423 snprintf(cryptodev_name, sizeof(cryptodev_name), "%s", dpaa_dev->name);
3425 cryptodev = rte_cryptodev_pmd_allocate(cryptodev_name, rte_socket_id());
3426 if (cryptodev == NULL)
3429 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
3430 cryptodev->data->dev_private = rte_zmalloc_socket(
3431 "cryptodev private structure",
3432 sizeof(struct dpaa_sec_dev_private),
3433 RTE_CACHE_LINE_SIZE,
3436 if (cryptodev->data->dev_private == NULL)
3437 rte_panic("Cannot allocate memzone for private "
3441 dpaa_dev->crypto_dev = cryptodev;
3442 cryptodev->device = &dpaa_dev->device;
3444 /* init user callbacks */
3445 TAILQ_INIT(&(cryptodev->link_intr_cbs));
3447 /* if sec device version is not configured */
3448 if (!rta_get_sec_era()) {
3449 const struct device_node *caam_node;
3451 for_each_compatible_node(caam_node, NULL, "fsl,sec-v4.0") {
3452 const uint32_t *prop = of_get_property(caam_node,
3457 INTL_SEC_ERA(rte_cpu_to_be_32(*prop)));
3463 if (unlikely(!RTE_PER_LCORE(dpaa_io))) {
3464 retval = rte_dpaa_portal_init((void *)1);
3466 DPAA_SEC_ERR("Unable to initialize portal");
3471 /* Invoke PMD device initialization function */
3472 retval = dpaa_sec_dev_init(cryptodev);
3476 /* In case of error, cleanup is done */
3477 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
3478 rte_free(cryptodev->data->dev_private);
3480 rte_cryptodev_pmd_release_device(cryptodev);
3486 cryptodev_dpaa_sec_remove(struct rte_dpaa_device *dpaa_dev)
3488 struct rte_cryptodev *cryptodev;
3491 cryptodev = dpaa_dev->crypto_dev;
3492 if (cryptodev == NULL)
3495 ret = dpaa_sec_uninit(cryptodev);
3499 return rte_cryptodev_pmd_destroy(cryptodev);
3502 static struct rte_dpaa_driver rte_dpaa_sec_driver = {
3503 .drv_type = FSL_DPAA_CRYPTO,
3505 .name = "DPAA SEC PMD"
3507 .probe = cryptodev_dpaa_sec_probe,
3508 .remove = cryptodev_dpaa_sec_remove,
3511 static struct cryptodev_driver dpaa_sec_crypto_drv;
3513 RTE_PMD_REGISTER_DPAA(CRYPTODEV_NAME_DPAA_SEC_PMD, rte_dpaa_sec_driver);
3514 RTE_PMD_REGISTER_CRYPTO_DRIVER(dpaa_sec_crypto_drv, rte_dpaa_sec_driver.driver,
3515 cryptodev_driver_id);
3517 RTE_INIT(dpaa_sec_init_log)
3519 dpaa_logtype_sec = rte_log_register("pmd.crypto.dpaa");
3520 if (dpaa_logtype_sec >= 0)
3521 rte_log_set_level(dpaa_logtype_sec, RTE_LOG_NOTICE);