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 <cryptodev_pmd.h>
16 #include <rte_crypto.h>
17 #include <rte_cryptodev.h>
18 #ifdef RTE_LIB_SECURITY
19 #include <rte_security_driver.h>
21 #include <rte_cycles.h>
24 #include <rte_kvargs.h>
25 #include <rte_malloc.h>
27 #include <rte_memcpy.h>
28 #include <rte_string_fns.h>
29 #include <rte_spinlock.h>
35 /* RTA header files */
36 #include <desc/common.h>
37 #include <desc/algo.h>
38 #include <desc/ipsec.h>
39 #include <desc/pdcp.h>
40 #include <desc/sdap.h>
42 #include <rte_dpaa_bus.h>
44 #include <dpaa_sec_event.h>
45 #include <dpaa_sec_log.h>
46 #include <dpaax_iova_table.h>
48 static uint8_t cryptodev_driver_id;
51 dpaa_sec_attach_sess_q(struct dpaa_sec_qp *qp, dpaa_sec_session *sess);
54 dpaa_sec_op_ending(struct dpaa_sec_op_ctx *ctx)
56 if (!ctx->fd_status) {
57 ctx->op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
59 DPAA_SEC_DP_WARN("SEC return err: 0x%x", ctx->fd_status);
60 ctx->op->status = RTE_CRYPTO_OP_STATUS_ERROR;
64 static inline struct dpaa_sec_op_ctx *
65 dpaa_sec_alloc_ctx(dpaa_sec_session *ses, int sg_count)
67 struct dpaa_sec_op_ctx *ctx;
70 retval = rte_mempool_get(
71 ses->qp[rte_lcore_id() % MAX_DPAA_CORES]->ctx_pool,
74 DPAA_SEC_DP_WARN("Alloc sec descriptor failed!");
78 * Clear SG memory. There are 16 SG entries of 16 Bytes each.
79 * one call to dcbz_64() clear 64 bytes, hence calling it 4 times
80 * to clear all the SG entries. dpaa_sec_alloc_ctx() is called for
81 * each packet, memset is costlier than dcbz_64().
83 for (i = 0; i < sg_count && i < MAX_JOB_SG_ENTRIES; i += 4)
84 dcbz_64(&ctx->job.sg[i]);
86 ctx->ctx_pool = ses->qp[rte_lcore_id() % MAX_DPAA_CORES]->ctx_pool;
87 ctx->vtop_offset = (size_t) ctx - rte_mempool_virt2iova(ctx);
93 ern_sec_fq_handler(struct qman_portal *qm __rte_unused,
95 const struct qm_mr_entry *msg)
97 DPAA_SEC_DP_ERR("sec fq %d error, RC = %x, seqnum = %x\n",
98 fq->fqid, msg->ern.rc, msg->ern.seqnum);
101 /* initialize the queue with dest chan as caam chan so that
102 * all the packets in this queue could be dispatched into caam
105 dpaa_sec_init_rx(struct qman_fq *fq_in, rte_iova_t hwdesc,
108 struct qm_mcc_initfq fq_opts;
112 /* Clear FQ options */
113 memset(&fq_opts, 0x00, sizeof(struct qm_mcc_initfq));
115 flags = QMAN_INITFQ_FLAG_SCHED;
116 fq_opts.we_mask = QM_INITFQ_WE_DESTWQ | QM_INITFQ_WE_CONTEXTA |
117 QM_INITFQ_WE_CONTEXTB;
119 qm_fqd_context_a_set64(&fq_opts.fqd, hwdesc);
120 fq_opts.fqd.context_b = fqid_out;
121 fq_opts.fqd.dest.channel = dpaa_get_qm_channel_caam();
122 fq_opts.fqd.dest.wq = 0;
124 fq_in->cb.ern = ern_sec_fq_handler;
126 DPAA_SEC_DEBUG("in-%x out-%x", fq_in->fqid, fqid_out);
128 ret = qman_init_fq(fq_in, flags, &fq_opts);
129 if (unlikely(ret != 0))
130 DPAA_SEC_ERR("qman_init_fq failed %d", ret);
135 /* something is put into in_fq and caam put the crypto result into out_fq */
136 static enum qman_cb_dqrr_result
137 dqrr_out_fq_cb_rx(struct qman_portal *qm __always_unused,
138 struct qman_fq *fq __always_unused,
139 const struct qm_dqrr_entry *dqrr)
141 const struct qm_fd *fd;
142 struct dpaa_sec_job *job;
143 struct dpaa_sec_op_ctx *ctx;
145 if (DPAA_PER_LCORE_DPAA_SEC_OP_NB >= DPAA_SEC_BURST)
146 return qman_cb_dqrr_defer;
148 if (!(dqrr->stat & QM_DQRR_STAT_FD_VALID))
149 return qman_cb_dqrr_consume;
152 /* sg is embedded in an op ctx,
153 * sg[0] is for output
156 job = rte_dpaa_mem_ptov(qm_fd_addr_get64(fd));
158 ctx = container_of(job, struct dpaa_sec_op_ctx, job);
159 ctx->fd_status = fd->status;
160 if (ctx->op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
161 struct qm_sg_entry *sg_out;
163 struct rte_mbuf *mbuf = (ctx->op->sym->m_dst == NULL) ?
164 ctx->op->sym->m_src : ctx->op->sym->m_dst;
166 sg_out = &job->sg[0];
167 hw_sg_to_cpu(sg_out);
168 len = sg_out->length;
170 while (mbuf->next != NULL) {
171 len -= mbuf->data_len;
174 mbuf->data_len = len;
176 DPAA_PER_LCORE_RTE_CRYPTO_OP[DPAA_PER_LCORE_DPAA_SEC_OP_NB++] = ctx->op;
177 dpaa_sec_op_ending(ctx);
179 return qman_cb_dqrr_consume;
182 /* caam result is put into this queue */
184 dpaa_sec_init_tx(struct qman_fq *fq)
187 struct qm_mcc_initfq opts;
190 flags = QMAN_FQ_FLAG_NO_ENQUEUE | QMAN_FQ_FLAG_LOCKED |
191 QMAN_FQ_FLAG_DYNAMIC_FQID;
193 ret = qman_create_fq(0, flags, fq);
195 DPAA_SEC_ERR("qman_create_fq failed");
199 memset(&opts, 0, sizeof(opts));
200 opts.we_mask = QM_INITFQ_WE_DESTWQ | QM_INITFQ_WE_FQCTRL |
201 QM_INITFQ_WE_CONTEXTA | QM_INITFQ_WE_CONTEXTB;
203 /* opts.fqd.dest.channel = dpaa_sec_pool_chan; */
205 fq->cb.dqrr = dqrr_out_fq_cb_rx;
206 fq->cb.ern = ern_sec_fq_handler;
208 ret = qman_init_fq(fq, 0, &opts);
210 DPAA_SEC_ERR("unable to init caam source fq!");
217 static inline int is_aead(dpaa_sec_session *ses)
219 return ((ses->cipher_alg == 0) &&
220 (ses->auth_alg == 0) &&
221 (ses->aead_alg != 0));
224 static inline int is_encode(dpaa_sec_session *ses)
226 return ses->dir == DIR_ENC;
229 static inline int is_decode(dpaa_sec_session *ses)
231 return ses->dir == DIR_DEC;
234 #ifdef RTE_LIB_SECURITY
236 dpaa_sec_prep_pdcp_cdb(dpaa_sec_session *ses)
238 struct alginfo authdata = {0}, cipherdata = {0};
239 struct sec_cdb *cdb = &ses->cdb;
240 struct alginfo *p_authdata = NULL;
241 int32_t shared_desc_len = 0;
242 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
248 cipherdata.key = (size_t)ses->cipher_key.data;
249 cipherdata.keylen = ses->cipher_key.length;
250 cipherdata.key_enc_flags = 0;
251 cipherdata.key_type = RTA_DATA_IMM;
252 cipherdata.algtype = ses->cipher_key.alg;
253 cipherdata.algmode = ses->cipher_key.algmode;
256 authdata.key = (size_t)ses->auth_key.data;
257 authdata.keylen = ses->auth_key.length;
258 authdata.key_enc_flags = 0;
259 authdata.key_type = RTA_DATA_IMM;
260 authdata.algtype = ses->auth_key.alg;
261 authdata.algmode = ses->auth_key.algmode;
263 p_authdata = &authdata;
266 if (rta_inline_pdcp_query(authdata.algtype,
269 ses->pdcp.hfn_ovd)) {
271 (size_t)rte_dpaa_mem_vtop((void *)
272 (size_t)cipherdata.key);
273 cipherdata.key_type = RTA_DATA_PTR;
276 if (ses->pdcp.domain == RTE_SECURITY_PDCP_MODE_CONTROL) {
277 if (ses->dir == DIR_ENC)
278 shared_desc_len = cnstr_shdsc_pdcp_c_plane_encap(
279 cdb->sh_desc, 1, swap,
284 ses->pdcp.hfn_threshold,
285 &cipherdata, &authdata,
287 else if (ses->dir == DIR_DEC)
288 shared_desc_len = cnstr_shdsc_pdcp_c_plane_decap(
289 cdb->sh_desc, 1, swap,
294 ses->pdcp.hfn_threshold,
295 &cipherdata, &authdata,
298 if (ses->dir == DIR_ENC) {
299 if (ses->pdcp.sdap_enabled)
301 cnstr_shdsc_pdcp_sdap_u_plane_encap(
302 cdb->sh_desc, 1, swap,
307 ses->pdcp.hfn_threshold,
308 &cipherdata, p_authdata, 0);
311 cnstr_shdsc_pdcp_u_plane_encap(
312 cdb->sh_desc, 1, swap,
317 ses->pdcp.hfn_threshold,
318 &cipherdata, p_authdata, 0);
319 } else if (ses->dir == DIR_DEC) {
320 if (ses->pdcp.sdap_enabled)
322 cnstr_shdsc_pdcp_sdap_u_plane_decap(
323 cdb->sh_desc, 1, swap,
328 ses->pdcp.hfn_threshold,
329 &cipherdata, p_authdata, 0);
332 cnstr_shdsc_pdcp_u_plane_decap(
333 cdb->sh_desc, 1, swap,
338 ses->pdcp.hfn_threshold,
339 &cipherdata, p_authdata, 0);
342 return shared_desc_len;
345 /* prepare ipsec proto command block of the session */
347 dpaa_sec_prep_ipsec_cdb(dpaa_sec_session *ses)
349 struct alginfo cipherdata = {0}, authdata = {0};
350 struct sec_cdb *cdb = &ses->cdb;
351 int32_t shared_desc_len = 0;
353 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
359 cipherdata.key = (size_t)ses->cipher_key.data;
360 cipherdata.keylen = ses->cipher_key.length;
361 cipherdata.key_enc_flags = 0;
362 cipherdata.key_type = RTA_DATA_IMM;
363 cipherdata.algtype = ses->cipher_key.alg;
364 cipherdata.algmode = ses->cipher_key.algmode;
366 if (ses->auth_key.length) {
367 authdata.key = (size_t)ses->auth_key.data;
368 authdata.keylen = ses->auth_key.length;
369 authdata.key_enc_flags = 0;
370 authdata.key_type = RTA_DATA_IMM;
371 authdata.algtype = ses->auth_key.alg;
372 authdata.algmode = ses->auth_key.algmode;
375 cdb->sh_desc[0] = cipherdata.keylen;
376 cdb->sh_desc[1] = authdata.keylen;
377 err = rta_inline_query(IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN,
379 (unsigned int *)cdb->sh_desc,
380 &cdb->sh_desc[2], 2);
383 DPAA_SEC_ERR("Crypto: Incorrect key lengths");
386 if (cdb->sh_desc[2] & 1)
387 cipherdata.key_type = RTA_DATA_IMM;
389 cipherdata.key = (size_t)rte_dpaa_mem_vtop(
390 (void *)(size_t)cipherdata.key);
391 cipherdata.key_type = RTA_DATA_PTR;
393 if (cdb->sh_desc[2] & (1<<1))
394 authdata.key_type = RTA_DATA_IMM;
396 authdata.key = (size_t)rte_dpaa_mem_vtop(
397 (void *)(size_t)authdata.key);
398 authdata.key_type = RTA_DATA_PTR;
404 if (ses->dir == DIR_ENC) {
405 shared_desc_len = cnstr_shdsc_ipsec_new_encap(
407 true, swap, SHR_SERIAL,
409 (uint8_t *)&ses->ip4_hdr,
410 &cipherdata, &authdata);
411 } else if (ses->dir == DIR_DEC) {
412 shared_desc_len = cnstr_shdsc_ipsec_new_decap(
414 true, swap, SHR_SERIAL,
416 &cipherdata, &authdata);
418 return shared_desc_len;
421 /* prepare command block of the session */
423 dpaa_sec_prep_cdb(dpaa_sec_session *ses)
425 struct alginfo alginfo_c = {0}, alginfo_a = {0}, alginfo = {0};
426 int32_t shared_desc_len = 0;
427 struct sec_cdb *cdb = &ses->cdb;
429 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
435 memset(cdb, 0, sizeof(struct sec_cdb));
438 #ifdef RTE_LIB_SECURITY
440 shared_desc_len = dpaa_sec_prep_ipsec_cdb(ses);
443 shared_desc_len = dpaa_sec_prep_pdcp_cdb(ses);
446 case DPAA_SEC_CIPHER:
447 alginfo_c.key = (size_t)ses->cipher_key.data;
448 alginfo_c.keylen = ses->cipher_key.length;
449 alginfo_c.key_enc_flags = 0;
450 alginfo_c.key_type = RTA_DATA_IMM;
451 alginfo_c.algtype = ses->cipher_key.alg;
452 alginfo_c.algmode = ses->cipher_key.algmode;
454 switch (ses->cipher_alg) {
455 case RTE_CRYPTO_CIPHER_AES_CBC:
456 case RTE_CRYPTO_CIPHER_3DES_CBC:
457 case RTE_CRYPTO_CIPHER_DES_CBC:
458 case RTE_CRYPTO_CIPHER_AES_CTR:
459 case RTE_CRYPTO_CIPHER_3DES_CTR:
460 shared_desc_len = cnstr_shdsc_blkcipher(
462 swap, SHR_NEVER, &alginfo_c,
466 case RTE_CRYPTO_CIPHER_SNOW3G_UEA2:
467 shared_desc_len = cnstr_shdsc_snow_f8(
468 cdb->sh_desc, true, swap,
472 case RTE_CRYPTO_CIPHER_ZUC_EEA3:
473 shared_desc_len = cnstr_shdsc_zuce(
474 cdb->sh_desc, true, swap,
479 DPAA_SEC_ERR("unsupported cipher alg %d",
485 alginfo_a.key = (size_t)ses->auth_key.data;
486 alginfo_a.keylen = ses->auth_key.length;
487 alginfo_a.key_enc_flags = 0;
488 alginfo_a.key_type = RTA_DATA_IMM;
489 alginfo_a.algtype = ses->auth_key.alg;
490 alginfo_a.algmode = ses->auth_key.algmode;
491 switch (ses->auth_alg) {
492 case RTE_CRYPTO_AUTH_MD5:
493 case RTE_CRYPTO_AUTH_SHA1:
494 case RTE_CRYPTO_AUTH_SHA224:
495 case RTE_CRYPTO_AUTH_SHA256:
496 case RTE_CRYPTO_AUTH_SHA384:
497 case RTE_CRYPTO_AUTH_SHA512:
498 shared_desc_len = cnstr_shdsc_hash(
500 swap, SHR_NEVER, &alginfo_a,
504 case RTE_CRYPTO_AUTH_MD5_HMAC:
505 case RTE_CRYPTO_AUTH_SHA1_HMAC:
506 case RTE_CRYPTO_AUTH_SHA224_HMAC:
507 case RTE_CRYPTO_AUTH_SHA256_HMAC:
508 case RTE_CRYPTO_AUTH_SHA384_HMAC:
509 case RTE_CRYPTO_AUTH_SHA512_HMAC:
510 shared_desc_len = cnstr_shdsc_hmac(
512 swap, SHR_NEVER, &alginfo_a,
516 case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
517 shared_desc_len = cnstr_shdsc_snow_f9(
518 cdb->sh_desc, true, swap,
523 case RTE_CRYPTO_AUTH_ZUC_EIA3:
524 shared_desc_len = cnstr_shdsc_zuca(
525 cdb->sh_desc, true, swap,
530 case RTE_CRYPTO_AUTH_AES_XCBC_MAC:
531 shared_desc_len = cnstr_shdsc_aes_mac(
533 true, swap, SHR_NEVER,
539 DPAA_SEC_ERR("unsupported auth alg %u", ses->auth_alg);
543 if (alginfo.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
544 DPAA_SEC_ERR("not supported aead alg");
547 alginfo.key = (size_t)ses->aead_key.data;
548 alginfo.keylen = ses->aead_key.length;
549 alginfo.key_enc_flags = 0;
550 alginfo.key_type = RTA_DATA_IMM;
551 alginfo.algtype = ses->aead_key.alg;
552 alginfo.algmode = ses->aead_key.algmode;
554 if (ses->dir == DIR_ENC)
555 shared_desc_len = cnstr_shdsc_gcm_encap(
556 cdb->sh_desc, true, swap, SHR_NEVER,
561 shared_desc_len = cnstr_shdsc_gcm_decap(
562 cdb->sh_desc, true, swap, SHR_NEVER,
567 case DPAA_SEC_CIPHER_HASH:
568 alginfo_c.key = (size_t)ses->cipher_key.data;
569 alginfo_c.keylen = ses->cipher_key.length;
570 alginfo_c.key_enc_flags = 0;
571 alginfo_c.key_type = RTA_DATA_IMM;
572 alginfo_c.algtype = ses->cipher_key.alg;
573 alginfo_c.algmode = ses->cipher_key.algmode;
575 alginfo_a.key = (size_t)ses->auth_key.data;
576 alginfo_a.keylen = ses->auth_key.length;
577 alginfo_a.key_enc_flags = 0;
578 alginfo_a.key_type = RTA_DATA_IMM;
579 alginfo_a.algtype = ses->auth_key.alg;
580 alginfo_a.algmode = ses->auth_key.algmode;
582 cdb->sh_desc[0] = alginfo_c.keylen;
583 cdb->sh_desc[1] = alginfo_a.keylen;
584 err = rta_inline_query(IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN,
586 (unsigned int *)cdb->sh_desc,
587 &cdb->sh_desc[2], 2);
590 DPAA_SEC_ERR("Crypto: Incorrect key lengths");
593 if (cdb->sh_desc[2] & 1)
594 alginfo_c.key_type = RTA_DATA_IMM;
596 alginfo_c.key = (size_t)rte_dpaa_mem_vtop(
597 (void *)(size_t)alginfo_c.key);
598 alginfo_c.key_type = RTA_DATA_PTR;
600 if (cdb->sh_desc[2] & (1<<1))
601 alginfo_a.key_type = RTA_DATA_IMM;
603 alginfo_a.key = (size_t)rte_dpaa_mem_vtop(
604 (void *)(size_t)alginfo_a.key);
605 alginfo_a.key_type = RTA_DATA_PTR;
610 /* Auth_only_len is set as 0 here and it will be
611 * overwritten in fd for each packet.
613 shared_desc_len = cnstr_shdsc_authenc(cdb->sh_desc,
614 true, swap, SHR_SERIAL, &alginfo_c, &alginfo_a,
616 ses->digest_length, ses->dir);
618 case DPAA_SEC_HASH_CIPHER:
620 DPAA_SEC_ERR("error: Unsupported session");
624 if (shared_desc_len < 0) {
625 DPAA_SEC_ERR("error in preparing command block");
626 return shared_desc_len;
629 cdb->sh_hdr.hi.field.idlen = shared_desc_len;
630 cdb->sh_hdr.hi.word = rte_cpu_to_be_32(cdb->sh_hdr.hi.word);
631 cdb->sh_hdr.lo.word = rte_cpu_to_be_32(cdb->sh_hdr.lo.word);
636 /* qp is lockless, should be accessed by only one thread */
638 dpaa_sec_deq(struct dpaa_sec_qp *qp, struct rte_crypto_op **ops, int nb_ops)
641 unsigned int pkts = 0;
642 int num_rx_bufs, ret;
643 struct qm_dqrr_entry *dq;
644 uint32_t vdqcr_flags = 0;
648 * Until request for four buffers, we provide exact number of buffers.
649 * Otherwise we do not set the QM_VDQCR_EXACT flag.
650 * Not setting QM_VDQCR_EXACT flag can provide two more buffers than
651 * requested, so we request two less in this case.
654 vdqcr_flags = QM_VDQCR_EXACT;
655 num_rx_bufs = nb_ops;
657 num_rx_bufs = nb_ops > DPAA_MAX_DEQUEUE_NUM_FRAMES ?
658 (DPAA_MAX_DEQUEUE_NUM_FRAMES - 2) : (nb_ops - 2);
660 ret = qman_set_vdq(fq, num_rx_bufs, vdqcr_flags);
665 const struct qm_fd *fd;
666 struct dpaa_sec_job *job;
667 struct dpaa_sec_op_ctx *ctx;
668 struct rte_crypto_op *op;
670 dq = qman_dequeue(fq);
675 /* sg is embedded in an op ctx,
676 * sg[0] is for output
679 job = rte_dpaa_mem_ptov(qm_fd_addr_get64(fd));
681 ctx = container_of(job, struct dpaa_sec_op_ctx, job);
682 ctx->fd_status = fd->status;
684 if (op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
685 struct qm_sg_entry *sg_out;
687 struct rte_mbuf *mbuf = (op->sym->m_dst == NULL) ?
688 op->sym->m_src : op->sym->m_dst;
690 sg_out = &job->sg[0];
691 hw_sg_to_cpu(sg_out);
692 len = sg_out->length;
694 while (mbuf->next != NULL) {
695 len -= mbuf->data_len;
698 mbuf->data_len = len;
700 if (!ctx->fd_status) {
701 op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
703 DPAA_SEC_DP_WARN("SEC return err:0x%x", ctx->fd_status);
704 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
708 /* report op status to sym->op and then free the ctx memeory */
709 rte_mempool_put(ctx->ctx_pool, (void *)ctx);
711 qman_dqrr_consume(fq, dq);
712 } while (fq->flags & QMAN_FQ_STATE_VDQCR);
717 static inline struct dpaa_sec_job *
718 build_auth_only_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
720 struct rte_crypto_sym_op *sym = op->sym;
721 struct rte_mbuf *mbuf = sym->m_src;
722 struct dpaa_sec_job *cf;
723 struct dpaa_sec_op_ctx *ctx;
724 struct qm_sg_entry *sg, *out_sg, *in_sg;
725 phys_addr_t start_addr;
726 uint8_t *old_digest, extra_segs;
727 int data_len, data_offset;
729 data_len = sym->auth.data.length;
730 data_offset = sym->auth.data.offset;
732 if (ses->auth_alg == RTE_CRYPTO_AUTH_SNOW3G_UIA2 ||
733 ses->auth_alg == RTE_CRYPTO_AUTH_ZUC_EIA3) {
734 if ((data_len & 7) || (data_offset & 7)) {
735 DPAA_SEC_ERR("AUTH: len/offset must be full bytes");
739 data_len = data_len >> 3;
740 data_offset = data_offset >> 3;
748 if (mbuf->nb_segs > MAX_SG_ENTRIES) {
749 DPAA_SEC_DP_ERR("Auth: Max sec segs supported is %d",
753 ctx = dpaa_sec_alloc_ctx(ses, mbuf->nb_segs + extra_segs);
759 old_digest = ctx->digest;
763 qm_sg_entry_set64(out_sg, sym->auth.digest.phys_addr);
764 out_sg->length = ses->digest_length;
765 cpu_to_hw_sg(out_sg);
769 /* need to extend the input to a compound frame */
770 in_sg->extension = 1;
772 in_sg->length = data_len;
773 qm_sg_entry_set64(in_sg, rte_dpaa_mem_vtop(&cf->sg[2]));
778 if (ses->iv.length) {
781 iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
784 if (ses->auth_alg == RTE_CRYPTO_AUTH_SNOW3G_UIA2) {
785 iv_ptr = conv_to_snow_f9_iv(iv_ptr);
787 } else if (ses->auth_alg == RTE_CRYPTO_AUTH_ZUC_EIA3) {
788 iv_ptr = conv_to_zuc_eia_iv(iv_ptr);
791 sg->length = ses->iv.length;
793 qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(iv_ptr));
794 in_sg->length += sg->length;
799 qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
800 sg->offset = data_offset;
802 if (data_len <= (mbuf->data_len - data_offset)) {
803 sg->length = data_len;
805 sg->length = mbuf->data_len - data_offset;
807 /* remaining i/p segs */
808 while ((data_len = data_len - sg->length) &&
809 (mbuf = mbuf->next)) {
812 qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
813 if (data_len > mbuf->data_len)
814 sg->length = mbuf->data_len;
816 sg->length = data_len;
820 if (is_decode(ses)) {
821 /* Digest verification case */
824 rte_memcpy(old_digest, sym->auth.digest.data,
826 start_addr = rte_dpaa_mem_vtop(old_digest);
827 qm_sg_entry_set64(sg, start_addr);
828 sg->length = ses->digest_length;
829 in_sg->length += ses->digest_length;
840 * |<----data_len------->|
841 * |ip_header|ah_header|icv|payload|
846 static inline struct dpaa_sec_job *
847 build_auth_only(struct rte_crypto_op *op, dpaa_sec_session *ses)
849 struct rte_crypto_sym_op *sym = op->sym;
850 struct rte_mbuf *mbuf = sym->m_src;
851 struct dpaa_sec_job *cf;
852 struct dpaa_sec_op_ctx *ctx;
853 struct qm_sg_entry *sg, *in_sg;
854 rte_iova_t start_addr;
856 int data_len, data_offset;
858 data_len = sym->auth.data.length;
859 data_offset = sym->auth.data.offset;
861 if (ses->auth_alg == RTE_CRYPTO_AUTH_SNOW3G_UIA2 ||
862 ses->auth_alg == RTE_CRYPTO_AUTH_ZUC_EIA3) {
863 if ((data_len & 7) || (data_offset & 7)) {
864 DPAA_SEC_ERR("AUTH: len/offset must be full bytes");
868 data_len = data_len >> 3;
869 data_offset = data_offset >> 3;
872 ctx = dpaa_sec_alloc_ctx(ses, 4);
878 old_digest = ctx->digest;
880 start_addr = rte_pktmbuf_iova(mbuf);
883 qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
884 sg->length = ses->digest_length;
889 /* need to extend the input to a compound frame */
890 in_sg->extension = 1;
892 in_sg->length = data_len;
893 qm_sg_entry_set64(in_sg, rte_dpaa_mem_vtop(&cf->sg[2]));
896 if (ses->iv.length) {
899 iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
902 if (ses->auth_alg == RTE_CRYPTO_AUTH_SNOW3G_UIA2) {
903 iv_ptr = conv_to_snow_f9_iv(iv_ptr);
905 } else if (ses->auth_alg == RTE_CRYPTO_AUTH_ZUC_EIA3) {
906 iv_ptr = conv_to_zuc_eia_iv(iv_ptr);
909 sg->length = ses->iv.length;
911 qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(iv_ptr));
912 in_sg->length += sg->length;
917 qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
918 sg->offset = data_offset;
919 sg->length = data_len;
921 if (is_decode(ses)) {
922 /* Digest verification case */
924 /* hash result or digest, save digest first */
925 rte_memcpy(old_digest, sym->auth.digest.data,
927 /* let's check digest by hw */
928 start_addr = rte_dpaa_mem_vtop(old_digest);
930 qm_sg_entry_set64(sg, start_addr);
931 sg->length = ses->digest_length;
932 in_sg->length += ses->digest_length;
941 static inline struct dpaa_sec_job *
942 build_cipher_only_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
944 struct rte_crypto_sym_op *sym = op->sym;
945 struct dpaa_sec_job *cf;
946 struct dpaa_sec_op_ctx *ctx;
947 struct qm_sg_entry *sg, *out_sg, *in_sg;
948 struct rte_mbuf *mbuf;
950 uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
952 int data_len, data_offset;
954 data_len = sym->cipher.data.length;
955 data_offset = sym->cipher.data.offset;
957 if (ses->cipher_alg == RTE_CRYPTO_CIPHER_SNOW3G_UEA2 ||
958 ses->cipher_alg == RTE_CRYPTO_CIPHER_ZUC_EEA3) {
959 if ((data_len & 7) || (data_offset & 7)) {
960 DPAA_SEC_ERR("CIPHER: len/offset must be full bytes");
964 data_len = data_len >> 3;
965 data_offset = data_offset >> 3;
970 req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 3;
973 req_segs = mbuf->nb_segs * 2 + 3;
975 if (mbuf->nb_segs > MAX_SG_ENTRIES) {
976 DPAA_SEC_DP_ERR("Cipher: Max sec segs supported is %d",
981 ctx = dpaa_sec_alloc_ctx(ses, req_segs);
990 out_sg->extension = 1;
991 out_sg->length = data_len;
992 qm_sg_entry_set64(out_sg, rte_dpaa_mem_vtop(&cf->sg[2]));
993 cpu_to_hw_sg(out_sg);
997 qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
998 sg->length = mbuf->data_len - data_offset;
999 sg->offset = data_offset;
1001 /* Successive segs */
1006 qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1007 sg->length = mbuf->data_len;
1016 in_sg->extension = 1;
1018 in_sg->length = data_len + ses->iv.length;
1021 qm_sg_entry_set64(in_sg, rte_dpaa_mem_vtop(sg));
1022 cpu_to_hw_sg(in_sg);
1025 qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(IV_ptr));
1026 sg->length = ses->iv.length;
1031 qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1032 sg->length = mbuf->data_len - data_offset;
1033 sg->offset = data_offset;
1035 /* Successive segs */
1040 qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1041 sg->length = mbuf->data_len;
1050 static inline struct dpaa_sec_job *
1051 build_cipher_only(struct rte_crypto_op *op, dpaa_sec_session *ses)
1053 struct rte_crypto_sym_op *sym = op->sym;
1054 struct dpaa_sec_job *cf;
1055 struct dpaa_sec_op_ctx *ctx;
1056 struct qm_sg_entry *sg;
1057 rte_iova_t src_start_addr, dst_start_addr;
1058 uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1060 int data_len, data_offset;
1062 data_len = sym->cipher.data.length;
1063 data_offset = sym->cipher.data.offset;
1065 if (ses->cipher_alg == RTE_CRYPTO_CIPHER_SNOW3G_UEA2 ||
1066 ses->cipher_alg == RTE_CRYPTO_CIPHER_ZUC_EEA3) {
1067 if ((data_len & 7) || (data_offset & 7)) {
1068 DPAA_SEC_ERR("CIPHER: len/offset must be full bytes");
1072 data_len = data_len >> 3;
1073 data_offset = data_offset >> 3;
1076 ctx = dpaa_sec_alloc_ctx(ses, 4);
1083 src_start_addr = rte_pktmbuf_iova(sym->m_src);
1086 dst_start_addr = rte_pktmbuf_iova(sym->m_dst);
1088 dst_start_addr = src_start_addr;
1092 qm_sg_entry_set64(sg, dst_start_addr + data_offset);
1093 sg->length = data_len + ses->iv.length;
1099 /* need to extend the input to a compound frame */
1102 sg->length = data_len + ses->iv.length;
1103 qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(&cf->sg[2]));
1107 qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(IV_ptr));
1108 sg->length = ses->iv.length;
1112 qm_sg_entry_set64(sg, src_start_addr + data_offset);
1113 sg->length = data_len;
1120 static inline struct dpaa_sec_job *
1121 build_cipher_auth_gcm_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
1123 struct rte_crypto_sym_op *sym = op->sym;
1124 struct dpaa_sec_job *cf;
1125 struct dpaa_sec_op_ctx *ctx;
1126 struct qm_sg_entry *sg, *out_sg, *in_sg;
1127 struct rte_mbuf *mbuf;
1129 uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1134 req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 4;
1137 req_segs = mbuf->nb_segs * 2 + 4;
1140 if (ses->auth_only_len)
1143 if (mbuf->nb_segs > MAX_SG_ENTRIES) {
1144 DPAA_SEC_DP_ERR("AEAD: Max sec segs supported is %d",
1149 ctx = dpaa_sec_alloc_ctx(ses, req_segs);
1156 rte_prefetch0(cf->sg);
1159 out_sg = &cf->sg[0];
1160 out_sg->extension = 1;
1162 out_sg->length = sym->aead.data.length + ses->digest_length;
1164 out_sg->length = sym->aead.data.length;
1166 /* output sg entries */
1168 qm_sg_entry_set64(out_sg, rte_dpaa_mem_vtop(sg));
1169 cpu_to_hw_sg(out_sg);
1172 qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1173 sg->length = mbuf->data_len - sym->aead.data.offset;
1174 sg->offset = sym->aead.data.offset;
1176 /* Successive segs */
1181 qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1182 sg->length = mbuf->data_len;
1185 sg->length -= ses->digest_length;
1187 if (is_encode(ses)) {
1189 /* set auth output */
1191 qm_sg_entry_set64(sg, sym->aead.digest.phys_addr);
1192 sg->length = ses->digest_length;
1200 in_sg->extension = 1;
1203 in_sg->length = ses->iv.length + sym->aead.data.length
1204 + ses->auth_only_len;
1206 in_sg->length = ses->iv.length + sym->aead.data.length
1207 + ses->auth_only_len + ses->digest_length;
1209 /* input sg entries */
1211 qm_sg_entry_set64(in_sg, rte_dpaa_mem_vtop(sg));
1212 cpu_to_hw_sg(in_sg);
1215 qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(IV_ptr));
1216 sg->length = ses->iv.length;
1219 /* 2nd seg auth only */
1220 if (ses->auth_only_len) {
1222 qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(sym->aead.aad.data));
1223 sg->length = ses->auth_only_len;
1229 qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1230 sg->length = mbuf->data_len - sym->aead.data.offset;
1231 sg->offset = sym->aead.data.offset;
1233 /* Successive segs */
1238 qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1239 sg->length = mbuf->data_len;
1243 if (is_decode(ses)) {
1246 memcpy(ctx->digest, sym->aead.digest.data,
1247 ses->digest_length);
1248 qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(ctx->digest));
1249 sg->length = ses->digest_length;
1257 static inline struct dpaa_sec_job *
1258 build_cipher_auth_gcm(struct rte_crypto_op *op, dpaa_sec_session *ses)
1260 struct rte_crypto_sym_op *sym = op->sym;
1261 struct dpaa_sec_job *cf;
1262 struct dpaa_sec_op_ctx *ctx;
1263 struct qm_sg_entry *sg;
1264 uint32_t length = 0;
1265 rte_iova_t src_start_addr, dst_start_addr;
1266 uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1269 src_start_addr = sym->m_src->buf_iova + sym->m_src->data_off;
1272 dst_start_addr = sym->m_dst->buf_iova + sym->m_dst->data_off;
1274 dst_start_addr = src_start_addr;
1276 ctx = dpaa_sec_alloc_ctx(ses, 7);
1284 rte_prefetch0(cf->sg);
1286 qm_sg_entry_set64(&cf->sg[1], rte_dpaa_mem_vtop(sg));
1287 if (is_encode(ses)) {
1288 qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(IV_ptr));
1289 sg->length = ses->iv.length;
1290 length += sg->length;
1294 if (ses->auth_only_len) {
1295 qm_sg_entry_set64(sg,
1296 rte_dpaa_mem_vtop(sym->aead.aad.data));
1297 sg->length = ses->auth_only_len;
1298 length += sg->length;
1302 qm_sg_entry_set64(sg, src_start_addr + sym->aead.data.offset);
1303 sg->length = sym->aead.data.length;
1304 length += sg->length;
1308 qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(IV_ptr));
1309 sg->length = ses->iv.length;
1310 length += sg->length;
1314 if (ses->auth_only_len) {
1315 qm_sg_entry_set64(sg,
1316 rte_dpaa_mem_vtop(sym->aead.aad.data));
1317 sg->length = ses->auth_only_len;
1318 length += sg->length;
1322 qm_sg_entry_set64(sg, src_start_addr + sym->aead.data.offset);
1323 sg->length = sym->aead.data.length;
1324 length += sg->length;
1327 memcpy(ctx->digest, sym->aead.digest.data,
1328 ses->digest_length);
1331 qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(ctx->digest));
1332 sg->length = ses->digest_length;
1333 length += sg->length;
1337 /* input compound frame */
1338 cf->sg[1].length = length;
1339 cf->sg[1].extension = 1;
1340 cf->sg[1].final = 1;
1341 cpu_to_hw_sg(&cf->sg[1]);
1345 qm_sg_entry_set64(&cf->sg[0], rte_dpaa_mem_vtop(sg));
1346 qm_sg_entry_set64(sg,
1347 dst_start_addr + sym->aead.data.offset);
1348 sg->length = sym->aead.data.length;
1349 length = sg->length;
1350 if (is_encode(ses)) {
1352 /* set auth output */
1354 qm_sg_entry_set64(sg, sym->aead.digest.phys_addr);
1355 sg->length = ses->digest_length;
1356 length += sg->length;
1361 /* output compound frame */
1362 cf->sg[0].length = length;
1363 cf->sg[0].extension = 1;
1364 cpu_to_hw_sg(&cf->sg[0]);
1369 static inline struct dpaa_sec_job *
1370 build_cipher_auth_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
1372 struct rte_crypto_sym_op *sym = op->sym;
1373 struct dpaa_sec_job *cf;
1374 struct dpaa_sec_op_ctx *ctx;
1375 struct qm_sg_entry *sg, *out_sg, *in_sg;
1376 struct rte_mbuf *mbuf;
1378 uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1383 req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 4;
1386 req_segs = mbuf->nb_segs * 2 + 4;
1389 if (mbuf->nb_segs > MAX_SG_ENTRIES) {
1390 DPAA_SEC_DP_ERR("Cipher-Auth: Max sec segs supported is %d",
1395 ctx = dpaa_sec_alloc_ctx(ses, req_segs);
1402 rte_prefetch0(cf->sg);
1405 out_sg = &cf->sg[0];
1406 out_sg->extension = 1;
1408 out_sg->length = sym->auth.data.length + ses->digest_length;
1410 out_sg->length = sym->auth.data.length;
1412 /* output sg entries */
1414 qm_sg_entry_set64(out_sg, rte_dpaa_mem_vtop(sg));
1415 cpu_to_hw_sg(out_sg);
1418 qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1419 sg->length = mbuf->data_len - sym->auth.data.offset;
1420 sg->offset = sym->auth.data.offset;
1422 /* Successive segs */
1427 qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1428 sg->length = mbuf->data_len;
1431 sg->length -= ses->digest_length;
1433 if (is_encode(ses)) {
1435 /* set auth output */
1437 qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
1438 sg->length = ses->digest_length;
1446 in_sg->extension = 1;
1449 in_sg->length = ses->iv.length + sym->auth.data.length;
1451 in_sg->length = ses->iv.length + sym->auth.data.length
1452 + ses->digest_length;
1454 /* input sg entries */
1456 qm_sg_entry_set64(in_sg, rte_dpaa_mem_vtop(sg));
1457 cpu_to_hw_sg(in_sg);
1460 qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(IV_ptr));
1461 sg->length = ses->iv.length;
1466 qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1467 sg->length = mbuf->data_len - sym->auth.data.offset;
1468 sg->offset = sym->auth.data.offset;
1470 /* Successive segs */
1475 qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1476 sg->length = mbuf->data_len;
1480 sg->length -= ses->digest_length;
1481 if (is_decode(ses)) {
1484 memcpy(ctx->digest, sym->auth.digest.data,
1485 ses->digest_length);
1486 qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(ctx->digest));
1487 sg->length = ses->digest_length;
1495 static inline struct dpaa_sec_job *
1496 build_cipher_auth(struct rte_crypto_op *op, dpaa_sec_session *ses)
1498 struct rte_crypto_sym_op *sym = op->sym;
1499 struct dpaa_sec_job *cf;
1500 struct dpaa_sec_op_ctx *ctx;
1501 struct qm_sg_entry *sg;
1502 rte_iova_t src_start_addr, dst_start_addr;
1503 uint32_t length = 0;
1504 uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1507 src_start_addr = sym->m_src->buf_iova + sym->m_src->data_off;
1509 dst_start_addr = sym->m_dst->buf_iova + sym->m_dst->data_off;
1511 dst_start_addr = src_start_addr;
1513 ctx = dpaa_sec_alloc_ctx(ses, 7);
1521 rte_prefetch0(cf->sg);
1523 qm_sg_entry_set64(&cf->sg[1], rte_dpaa_mem_vtop(sg));
1524 if (is_encode(ses)) {
1525 qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(IV_ptr));
1526 sg->length = ses->iv.length;
1527 length += sg->length;
1531 qm_sg_entry_set64(sg, src_start_addr + sym->auth.data.offset);
1532 sg->length = sym->auth.data.length;
1533 length += sg->length;
1537 qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(IV_ptr));
1538 sg->length = ses->iv.length;
1539 length += sg->length;
1544 qm_sg_entry_set64(sg, src_start_addr + sym->auth.data.offset);
1545 sg->length = sym->auth.data.length;
1546 length += sg->length;
1549 memcpy(ctx->digest, sym->auth.digest.data,
1550 ses->digest_length);
1553 qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(ctx->digest));
1554 sg->length = ses->digest_length;
1555 length += sg->length;
1559 /* input compound frame */
1560 cf->sg[1].length = length;
1561 cf->sg[1].extension = 1;
1562 cf->sg[1].final = 1;
1563 cpu_to_hw_sg(&cf->sg[1]);
1567 qm_sg_entry_set64(&cf->sg[0], rte_dpaa_mem_vtop(sg));
1568 qm_sg_entry_set64(sg, dst_start_addr + sym->cipher.data.offset);
1569 sg->length = sym->cipher.data.length;
1570 length = sg->length;
1571 if (is_encode(ses)) {
1573 /* set auth output */
1575 qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
1576 sg->length = ses->digest_length;
1577 length += sg->length;
1582 /* output compound frame */
1583 cf->sg[0].length = length;
1584 cf->sg[0].extension = 1;
1585 cpu_to_hw_sg(&cf->sg[0]);
1590 #ifdef RTE_LIB_SECURITY
1591 static inline struct dpaa_sec_job *
1592 build_proto(struct rte_crypto_op *op, dpaa_sec_session *ses)
1594 struct rte_crypto_sym_op *sym = op->sym;
1595 struct dpaa_sec_job *cf;
1596 struct dpaa_sec_op_ctx *ctx;
1597 struct qm_sg_entry *sg;
1598 phys_addr_t src_start_addr, dst_start_addr;
1600 ctx = dpaa_sec_alloc_ctx(ses, 2);
1606 src_start_addr = rte_pktmbuf_iova(sym->m_src);
1609 dst_start_addr = rte_pktmbuf_iova(sym->m_dst);
1611 dst_start_addr = src_start_addr;
1615 qm_sg_entry_set64(sg, src_start_addr);
1616 sg->length = sym->m_src->pkt_len;
1620 sym->m_src->packet_type &= ~RTE_PTYPE_L4_MASK;
1623 qm_sg_entry_set64(sg, dst_start_addr);
1624 sg->length = sym->m_src->buf_len - sym->m_src->data_off;
1630 static inline struct dpaa_sec_job *
1631 build_proto_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
1633 struct rte_crypto_sym_op *sym = op->sym;
1634 struct dpaa_sec_job *cf;
1635 struct dpaa_sec_op_ctx *ctx;
1636 struct qm_sg_entry *sg, *out_sg, *in_sg;
1637 struct rte_mbuf *mbuf;
1639 uint32_t in_len = 0, out_len = 0;
1646 req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 2;
1647 if (mbuf->nb_segs > MAX_SG_ENTRIES) {
1648 DPAA_SEC_DP_ERR("Proto: Max sec segs supported is %d",
1653 ctx = dpaa_sec_alloc_ctx(ses, req_segs);
1659 out_sg = &cf->sg[0];
1660 out_sg->extension = 1;
1661 qm_sg_entry_set64(out_sg, rte_dpaa_mem_vtop(&cf->sg[2]));
1665 qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1668 /* Successive segs */
1669 while (mbuf->next) {
1670 sg->length = mbuf->data_len;
1671 out_len += sg->length;
1675 qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1678 sg->length = mbuf->buf_len - mbuf->data_off;
1679 out_len += sg->length;
1683 out_sg->length = out_len;
1684 cpu_to_hw_sg(out_sg);
1689 in_sg->extension = 1;
1691 in_len = mbuf->data_len;
1694 qm_sg_entry_set64(in_sg, rte_dpaa_mem_vtop(sg));
1697 qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1698 sg->length = mbuf->data_len;
1701 /* Successive segs */
1706 qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1707 sg->length = mbuf->data_len;
1709 in_len += sg->length;
1715 in_sg->length = in_len;
1716 cpu_to_hw_sg(in_sg);
1718 sym->m_src->packet_type &= ~RTE_PTYPE_L4_MASK;
1725 dpaa_sec_enqueue_burst(void *qp, struct rte_crypto_op **ops,
1728 /* Function to transmit the frames to given device and queuepair */
1730 struct dpaa_sec_qp *dpaa_qp = (struct dpaa_sec_qp *)qp;
1731 uint16_t num_tx = 0;
1732 struct qm_fd fds[DPAA_SEC_BURST], *fd;
1733 uint32_t frames_to_send;
1734 struct rte_crypto_op *op;
1735 struct dpaa_sec_job *cf;
1736 dpaa_sec_session *ses;
1737 uint16_t auth_hdr_len, auth_tail_len;
1738 uint32_t index, flags[DPAA_SEC_BURST] = {0};
1739 struct qman_fq *inq[DPAA_SEC_BURST];
1741 if (unlikely(!DPAA_PER_LCORE_PORTAL)) {
1742 if (rte_dpaa_portal_init((void *)0)) {
1743 DPAA_SEC_ERR("Failure in affining portal");
1749 frames_to_send = (nb_ops > DPAA_SEC_BURST) ?
1750 DPAA_SEC_BURST : nb_ops;
1751 for (loop = 0; loop < frames_to_send; loop++) {
1753 if (*dpaa_seqn(op->sym->m_src) != 0) {
1754 index = *dpaa_seqn(op->sym->m_src) - 1;
1755 if (DPAA_PER_LCORE_DQRR_HELD & (1 << index)) {
1756 /* QM_EQCR_DCA_IDXMASK = 0x0f */
1757 flags[loop] = ((index & 0x0f) << 8);
1758 flags[loop] |= QMAN_ENQUEUE_FLAG_DCA;
1759 DPAA_PER_LCORE_DQRR_SIZE--;
1760 DPAA_PER_LCORE_DQRR_HELD &=
1765 switch (op->sess_type) {
1766 case RTE_CRYPTO_OP_WITH_SESSION:
1767 ses = (dpaa_sec_session *)
1768 get_sym_session_private_data(
1770 cryptodev_driver_id);
1772 #ifdef RTE_LIB_SECURITY
1773 case RTE_CRYPTO_OP_SECURITY_SESSION:
1774 ses = (dpaa_sec_session *)
1775 get_sec_session_private_data(
1776 op->sym->sec_session);
1781 "sessionless crypto op not supported");
1782 frames_to_send = loop;
1788 DPAA_SEC_DP_ERR("session not available");
1789 frames_to_send = loop;
1794 if (unlikely(!ses->qp[rte_lcore_id() % MAX_DPAA_CORES])) {
1795 if (dpaa_sec_attach_sess_q(qp, ses)) {
1796 frames_to_send = loop;
1800 } else if (unlikely(ses->qp[rte_lcore_id() %
1801 MAX_DPAA_CORES] != qp)) {
1802 DPAA_SEC_DP_ERR("Old:sess->qp = %p"
1804 ses->qp[rte_lcore_id() %
1805 MAX_DPAA_CORES], qp);
1806 frames_to_send = loop;
1811 auth_hdr_len = op->sym->auth.data.length -
1812 op->sym->cipher.data.length;
1815 if (rte_pktmbuf_is_contiguous(op->sym->m_src) &&
1816 ((op->sym->m_dst == NULL) ||
1817 rte_pktmbuf_is_contiguous(op->sym->m_dst))) {
1818 switch (ses->ctxt) {
1819 #ifdef RTE_LIB_SECURITY
1821 case DPAA_SEC_IPSEC:
1822 cf = build_proto(op, ses);
1826 cf = build_auth_only(op, ses);
1828 case DPAA_SEC_CIPHER:
1829 cf = build_cipher_only(op, ses);
1832 cf = build_cipher_auth_gcm(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(op, ses);
1846 DPAA_SEC_DP_ERR("not supported ops");
1847 frames_to_send = loop;
1852 switch (ses->ctxt) {
1853 #ifdef RTE_LIB_SECURITY
1855 case DPAA_SEC_IPSEC:
1856 cf = build_proto_sg(op, ses);
1860 cf = build_auth_only_sg(op, ses);
1862 case DPAA_SEC_CIPHER:
1863 cf = build_cipher_only_sg(op, ses);
1866 cf = build_cipher_auth_gcm_sg(op, ses);
1867 auth_hdr_len = ses->auth_only_len;
1869 case DPAA_SEC_CIPHER_HASH:
1871 op->sym->cipher.data.offset
1872 - op->sym->auth.data.offset;
1874 op->sym->auth.data.length
1875 - op->sym->cipher.data.length
1877 cf = build_cipher_auth_sg(op, ses);
1880 DPAA_SEC_DP_ERR("not supported ops");
1881 frames_to_send = loop;
1886 if (unlikely(!cf)) {
1887 frames_to_send = loop;
1893 inq[loop] = ses->inq[rte_lcore_id() % MAX_DPAA_CORES];
1894 fd->opaque_addr = 0;
1896 qm_fd_addr_set64(fd, rte_dpaa_mem_vtop(cf->sg));
1897 fd->_format1 = qm_fd_compound;
1898 fd->length29 = 2 * sizeof(struct qm_sg_entry);
1900 /* Auth_only_len is set as 0 in descriptor and it is
1901 * overwritten here in the fd.cmd which will update
1904 if (auth_hdr_len || auth_tail_len) {
1905 fd->cmd = 0x80000000;
1907 ((auth_tail_len << 16) | auth_hdr_len);
1910 #ifdef RTE_LIB_SECURITY
1911 /* In case of PDCP, per packet HFN is stored in
1912 * mbuf priv after sym_op.
1914 if ((ses->ctxt == DPAA_SEC_PDCP) && ses->pdcp.hfn_ovd) {
1915 fd->cmd = 0x80000000 |
1916 *((uint32_t *)((uint8_t *)op +
1917 ses->pdcp.hfn_ovd_offset));
1918 DPAA_SEC_DP_DEBUG("Per packet HFN: %x, ovd:%u\n",
1919 *((uint32_t *)((uint8_t *)op +
1920 ses->pdcp.hfn_ovd_offset)),
1927 while (loop < frames_to_send) {
1928 loop += qman_enqueue_multi_fq(&inq[loop], &fds[loop],
1929 &flags[loop], frames_to_send - loop);
1931 nb_ops -= frames_to_send;
1932 num_tx += frames_to_send;
1935 dpaa_qp->tx_pkts += num_tx;
1936 dpaa_qp->tx_errs += nb_ops - num_tx;
1942 dpaa_sec_dequeue_burst(void *qp, struct rte_crypto_op **ops,
1946 struct dpaa_sec_qp *dpaa_qp = (struct dpaa_sec_qp *)qp;
1948 if (unlikely(!DPAA_PER_LCORE_PORTAL)) {
1949 if (rte_dpaa_portal_init((void *)0)) {
1950 DPAA_SEC_ERR("Failure in affining portal");
1955 num_rx = dpaa_sec_deq(dpaa_qp, ops, nb_ops);
1957 dpaa_qp->rx_pkts += num_rx;
1958 dpaa_qp->rx_errs += nb_ops - num_rx;
1960 DPAA_SEC_DP_DEBUG("SEC Received %d Packets\n", num_rx);
1965 /** Release queue pair */
1967 dpaa_sec_queue_pair_release(struct rte_cryptodev *dev,
1970 struct dpaa_sec_dev_private *internals;
1971 struct dpaa_sec_qp *qp = NULL;
1973 PMD_INIT_FUNC_TRACE();
1975 DPAA_SEC_DEBUG("dev =%p, queue =%d", dev, qp_id);
1977 internals = dev->data->dev_private;
1978 if (qp_id >= internals->max_nb_queue_pairs) {
1979 DPAA_SEC_ERR("Max supported qpid %d",
1980 internals->max_nb_queue_pairs);
1984 qp = &internals->qps[qp_id];
1985 rte_mempool_free(qp->ctx_pool);
1986 qp->internals = NULL;
1987 dev->data->queue_pairs[qp_id] = NULL;
1992 /** Setup a queue pair */
1994 dpaa_sec_queue_pair_setup(struct rte_cryptodev *dev, uint16_t qp_id,
1995 __rte_unused const struct rte_cryptodev_qp_conf *qp_conf,
1996 __rte_unused int socket_id)
1998 struct dpaa_sec_dev_private *internals;
1999 struct dpaa_sec_qp *qp = NULL;
2002 DPAA_SEC_DEBUG("dev =%p, queue =%d, conf =%p", dev, qp_id, qp_conf);
2004 internals = dev->data->dev_private;
2005 if (qp_id >= internals->max_nb_queue_pairs) {
2006 DPAA_SEC_ERR("Max supported qpid %d",
2007 internals->max_nb_queue_pairs);
2011 qp = &internals->qps[qp_id];
2012 qp->internals = internals;
2013 snprintf(str, sizeof(str), "ctx_pool_d%d_qp%d",
2014 dev->data->dev_id, qp_id);
2015 if (!qp->ctx_pool) {
2016 qp->ctx_pool = rte_mempool_create((const char *)str,
2019 CTX_POOL_CACHE_SIZE, 0,
2020 NULL, NULL, NULL, NULL,
2022 if (!qp->ctx_pool) {
2023 DPAA_SEC_ERR("%s create failed\n", str);
2027 DPAA_SEC_INFO("mempool already created for dev_id : %d, qp: %d",
2028 dev->data->dev_id, qp_id);
2029 dev->data->queue_pairs[qp_id] = qp;
2034 /** Returns the size of session structure */
2036 dpaa_sec_sym_session_get_size(struct rte_cryptodev *dev __rte_unused)
2038 PMD_INIT_FUNC_TRACE();
2040 return sizeof(dpaa_sec_session);
2044 dpaa_sec_cipher_init(struct rte_cryptodev *dev __rte_unused,
2045 struct rte_crypto_sym_xform *xform,
2046 dpaa_sec_session *session)
2048 session->ctxt = DPAA_SEC_CIPHER;
2049 session->cipher_alg = xform->cipher.algo;
2050 session->iv.length = xform->cipher.iv.length;
2051 session->iv.offset = xform->cipher.iv.offset;
2052 session->cipher_key.data = rte_zmalloc(NULL, xform->cipher.key.length,
2053 RTE_CACHE_LINE_SIZE);
2054 if (session->cipher_key.data == NULL && xform->cipher.key.length > 0) {
2055 DPAA_SEC_ERR("No Memory for cipher key");
2058 session->cipher_key.length = xform->cipher.key.length;
2060 memcpy(session->cipher_key.data, xform->cipher.key.data,
2061 xform->cipher.key.length);
2062 switch (xform->cipher.algo) {
2063 case RTE_CRYPTO_CIPHER_AES_CBC:
2064 session->cipher_key.alg = OP_ALG_ALGSEL_AES;
2065 session->cipher_key.algmode = OP_ALG_AAI_CBC;
2067 case RTE_CRYPTO_CIPHER_DES_CBC:
2068 session->cipher_key.alg = OP_ALG_ALGSEL_DES;
2069 session->cipher_key.algmode = OP_ALG_AAI_CBC;
2071 case RTE_CRYPTO_CIPHER_3DES_CBC:
2072 session->cipher_key.alg = OP_ALG_ALGSEL_3DES;
2073 session->cipher_key.algmode = OP_ALG_AAI_CBC;
2075 case RTE_CRYPTO_CIPHER_AES_CTR:
2076 session->cipher_key.alg = OP_ALG_ALGSEL_AES;
2077 session->cipher_key.algmode = OP_ALG_AAI_CTR;
2079 case RTE_CRYPTO_CIPHER_SNOW3G_UEA2:
2080 session->cipher_key.alg = OP_ALG_ALGSEL_SNOW_F8;
2082 case RTE_CRYPTO_CIPHER_ZUC_EEA3:
2083 session->cipher_key.alg = OP_ALG_ALGSEL_ZUCE;
2086 DPAA_SEC_ERR("Crypto: Undefined Cipher specified %u",
2087 xform->cipher.algo);
2090 session->dir = (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
2097 dpaa_sec_auth_init(struct rte_cryptodev *dev __rte_unused,
2098 struct rte_crypto_sym_xform *xform,
2099 dpaa_sec_session *session)
2101 session->ctxt = DPAA_SEC_AUTH;
2102 session->auth_alg = xform->auth.algo;
2103 session->auth_key.length = xform->auth.key.length;
2104 if (xform->auth.key.length) {
2105 session->auth_key.data =
2106 rte_zmalloc(NULL, xform->auth.key.length,
2107 RTE_CACHE_LINE_SIZE);
2108 if (session->auth_key.data == NULL) {
2109 DPAA_SEC_ERR("No Memory for auth key");
2112 memcpy(session->auth_key.data, xform->auth.key.data,
2113 xform->auth.key.length);
2116 session->digest_length = xform->auth.digest_length;
2117 if (session->cipher_alg == RTE_CRYPTO_CIPHER_NULL) {
2118 session->iv.offset = xform->auth.iv.offset;
2119 session->iv.length = xform->auth.iv.length;
2122 switch (xform->auth.algo) {
2123 case RTE_CRYPTO_AUTH_SHA1:
2124 session->auth_key.alg = OP_ALG_ALGSEL_SHA1;
2125 session->auth_key.algmode = OP_ALG_AAI_HASH;
2127 case RTE_CRYPTO_AUTH_SHA1_HMAC:
2128 session->auth_key.alg = OP_ALG_ALGSEL_SHA1;
2129 session->auth_key.algmode = OP_ALG_AAI_HMAC;
2131 case RTE_CRYPTO_AUTH_MD5:
2132 session->auth_key.alg = OP_ALG_ALGSEL_MD5;
2133 session->auth_key.algmode = OP_ALG_AAI_HASH;
2135 case RTE_CRYPTO_AUTH_MD5_HMAC:
2136 session->auth_key.alg = OP_ALG_ALGSEL_MD5;
2137 session->auth_key.algmode = OP_ALG_AAI_HMAC;
2139 case RTE_CRYPTO_AUTH_SHA224:
2140 session->auth_key.alg = OP_ALG_ALGSEL_SHA224;
2141 session->auth_key.algmode = OP_ALG_AAI_HASH;
2143 case RTE_CRYPTO_AUTH_SHA224_HMAC:
2144 session->auth_key.alg = OP_ALG_ALGSEL_SHA224;
2145 session->auth_key.algmode = OP_ALG_AAI_HMAC;
2147 case RTE_CRYPTO_AUTH_SHA256:
2148 session->auth_key.alg = OP_ALG_ALGSEL_SHA256;
2149 session->auth_key.algmode = OP_ALG_AAI_HASH;
2151 case RTE_CRYPTO_AUTH_SHA256_HMAC:
2152 session->auth_key.alg = OP_ALG_ALGSEL_SHA256;
2153 session->auth_key.algmode = OP_ALG_AAI_HMAC;
2155 case RTE_CRYPTO_AUTH_SHA384:
2156 session->auth_key.alg = OP_ALG_ALGSEL_SHA384;
2157 session->auth_key.algmode = OP_ALG_AAI_HASH;
2159 case RTE_CRYPTO_AUTH_SHA384_HMAC:
2160 session->auth_key.alg = OP_ALG_ALGSEL_SHA384;
2161 session->auth_key.algmode = OP_ALG_AAI_HMAC;
2163 case RTE_CRYPTO_AUTH_SHA512:
2164 session->auth_key.alg = OP_ALG_ALGSEL_SHA512;
2165 session->auth_key.algmode = OP_ALG_AAI_HASH;
2167 case RTE_CRYPTO_AUTH_SHA512_HMAC:
2168 session->auth_key.alg = OP_ALG_ALGSEL_SHA512;
2169 session->auth_key.algmode = OP_ALG_AAI_HMAC;
2171 case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
2172 session->auth_key.alg = OP_ALG_ALGSEL_SNOW_F9;
2173 session->auth_key.algmode = OP_ALG_AAI_F9;
2175 case RTE_CRYPTO_AUTH_ZUC_EIA3:
2176 session->auth_key.alg = OP_ALG_ALGSEL_ZUCA;
2177 session->auth_key.algmode = OP_ALG_AAI_F9;
2179 case RTE_CRYPTO_AUTH_AES_XCBC_MAC:
2180 session->auth_key.alg = OP_ALG_ALGSEL_AES;
2181 session->auth_key.algmode = OP_ALG_AAI_XCBC_MAC;
2184 DPAA_SEC_ERR("Crypto: Unsupported Auth specified %u",
2189 session->dir = (xform->auth.op == RTE_CRYPTO_AUTH_OP_GENERATE) ?
2196 dpaa_sec_chain_init(struct rte_cryptodev *dev __rte_unused,
2197 struct rte_crypto_sym_xform *xform,
2198 dpaa_sec_session *session)
2201 struct rte_crypto_cipher_xform *cipher_xform;
2202 struct rte_crypto_auth_xform *auth_xform;
2204 session->ctxt = DPAA_SEC_CIPHER_HASH;
2205 if (session->auth_cipher_text) {
2206 cipher_xform = &xform->cipher;
2207 auth_xform = &xform->next->auth;
2209 cipher_xform = &xform->next->cipher;
2210 auth_xform = &xform->auth;
2213 /* Set IV parameters */
2214 session->iv.offset = cipher_xform->iv.offset;
2215 session->iv.length = cipher_xform->iv.length;
2217 session->cipher_key.data = rte_zmalloc(NULL, cipher_xform->key.length,
2218 RTE_CACHE_LINE_SIZE);
2219 if (session->cipher_key.data == NULL && cipher_xform->key.length > 0) {
2220 DPAA_SEC_ERR("No Memory for cipher key");
2223 session->cipher_key.length = cipher_xform->key.length;
2224 session->auth_key.data = rte_zmalloc(NULL, auth_xform->key.length,
2225 RTE_CACHE_LINE_SIZE);
2226 if (session->auth_key.data == NULL && auth_xform->key.length > 0) {
2227 DPAA_SEC_ERR("No Memory for auth key");
2230 session->auth_key.length = auth_xform->key.length;
2231 memcpy(session->cipher_key.data, cipher_xform->key.data,
2232 cipher_xform->key.length);
2233 memcpy(session->auth_key.data, auth_xform->key.data,
2234 auth_xform->key.length);
2236 session->digest_length = auth_xform->digest_length;
2237 session->auth_alg = auth_xform->algo;
2239 switch (auth_xform->algo) {
2240 case RTE_CRYPTO_AUTH_SHA1_HMAC:
2241 session->auth_key.alg = OP_ALG_ALGSEL_SHA1;
2242 session->auth_key.algmode = OP_ALG_AAI_HMAC;
2244 case RTE_CRYPTO_AUTH_MD5_HMAC:
2245 session->auth_key.alg = OP_ALG_ALGSEL_MD5;
2246 session->auth_key.algmode = OP_ALG_AAI_HMAC;
2248 case RTE_CRYPTO_AUTH_SHA224_HMAC:
2249 session->auth_key.alg = OP_ALG_ALGSEL_SHA224;
2250 session->auth_key.algmode = OP_ALG_AAI_HMAC;
2252 case RTE_CRYPTO_AUTH_SHA256_HMAC:
2253 session->auth_key.alg = OP_ALG_ALGSEL_SHA256;
2254 session->auth_key.algmode = OP_ALG_AAI_HMAC;
2256 case RTE_CRYPTO_AUTH_SHA384_HMAC:
2257 session->auth_key.alg = OP_ALG_ALGSEL_SHA384;
2258 session->auth_key.algmode = OP_ALG_AAI_HMAC;
2260 case RTE_CRYPTO_AUTH_SHA512_HMAC:
2261 session->auth_key.alg = OP_ALG_ALGSEL_SHA512;
2262 session->auth_key.algmode = OP_ALG_AAI_HMAC;
2264 case RTE_CRYPTO_AUTH_AES_XCBC_MAC:
2265 session->auth_key.alg = OP_ALG_ALGSEL_AES;
2266 session->auth_key.algmode = OP_ALG_AAI_XCBC_MAC;
2269 DPAA_SEC_ERR("Crypto: Unsupported Auth specified %u",
2274 session->cipher_alg = cipher_xform->algo;
2276 switch (cipher_xform->algo) {
2277 case RTE_CRYPTO_CIPHER_AES_CBC:
2278 session->cipher_key.alg = OP_ALG_ALGSEL_AES;
2279 session->cipher_key.algmode = OP_ALG_AAI_CBC;
2281 case RTE_CRYPTO_CIPHER_DES_CBC:
2282 session->cipher_key.alg = OP_ALG_ALGSEL_DES;
2283 session->cipher_key.algmode = OP_ALG_AAI_CBC;
2285 case RTE_CRYPTO_CIPHER_3DES_CBC:
2286 session->cipher_key.alg = OP_ALG_ALGSEL_3DES;
2287 session->cipher_key.algmode = OP_ALG_AAI_CBC;
2289 case RTE_CRYPTO_CIPHER_AES_CTR:
2290 session->cipher_key.alg = OP_ALG_ALGSEL_AES;
2291 session->cipher_key.algmode = OP_ALG_AAI_CTR;
2294 DPAA_SEC_ERR("Crypto: Undefined Cipher specified %u",
2295 cipher_xform->algo);
2298 session->dir = (cipher_xform->op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
2304 dpaa_sec_aead_init(struct rte_cryptodev *dev __rte_unused,
2305 struct rte_crypto_sym_xform *xform,
2306 dpaa_sec_session *session)
2308 session->aead_alg = xform->aead.algo;
2309 session->ctxt = DPAA_SEC_AEAD;
2310 session->iv.length = xform->aead.iv.length;
2311 session->iv.offset = xform->aead.iv.offset;
2312 session->auth_only_len = xform->aead.aad_length;
2313 session->aead_key.data = rte_zmalloc(NULL, xform->aead.key.length,
2314 RTE_CACHE_LINE_SIZE);
2315 if (session->aead_key.data == NULL && xform->aead.key.length > 0) {
2316 DPAA_SEC_ERR("No Memory for aead key\n");
2319 session->aead_key.length = xform->aead.key.length;
2320 session->digest_length = xform->aead.digest_length;
2322 memcpy(session->aead_key.data, xform->aead.key.data,
2323 xform->aead.key.length);
2325 switch (session->aead_alg) {
2326 case RTE_CRYPTO_AEAD_AES_GCM:
2327 session->aead_key.alg = OP_ALG_ALGSEL_AES;
2328 session->aead_key.algmode = OP_ALG_AAI_GCM;
2331 DPAA_SEC_ERR("unsupported AEAD alg %d", session->aead_alg);
2335 session->dir = (xform->aead.op == RTE_CRYPTO_AEAD_OP_ENCRYPT) ?
2341 static struct qman_fq *
2342 dpaa_sec_attach_rxq(struct dpaa_sec_dev_private *qi)
2346 for (i = 0; i < RTE_DPAA_MAX_RX_QUEUE; i++) {
2347 if (qi->inq_attach[i] == 0) {
2348 qi->inq_attach[i] = 1;
2352 DPAA_SEC_WARN("All session in use %u", qi->max_nb_sessions);
2358 dpaa_sec_detach_rxq(struct dpaa_sec_dev_private *qi, struct qman_fq *fq)
2362 for (i = 0; i < RTE_DPAA_MAX_RX_QUEUE; i++) {
2363 if (&qi->inq[i] == fq) {
2364 if (qman_retire_fq(fq, NULL) != 0)
2365 DPAA_SEC_DEBUG("Queue is not retired\n");
2367 qi->inq_attach[i] = 0;
2375 dpaa_sec_attach_sess_q(struct dpaa_sec_qp *qp, dpaa_sec_session *sess)
2379 sess->qp[rte_lcore_id() % MAX_DPAA_CORES] = qp;
2380 ret = dpaa_sec_prep_cdb(sess);
2382 DPAA_SEC_ERR("Unable to prepare sec cdb");
2385 if (unlikely(!DPAA_PER_LCORE_PORTAL)) {
2386 ret = rte_dpaa_portal_init((void *)0);
2388 DPAA_SEC_ERR("Failure in affining portal");
2392 ret = dpaa_sec_init_rx(sess->inq[rte_lcore_id() % MAX_DPAA_CORES],
2393 rte_dpaa_mem_vtop(&sess->cdb),
2394 qman_fq_fqid(&qp->outq));
2396 DPAA_SEC_ERR("Unable to init sec queue");
2402 free_session_data(dpaa_sec_session *s)
2405 rte_free(s->aead_key.data);
2407 rte_free(s->auth_key.data);
2408 rte_free(s->cipher_key.data);
2410 memset(s, 0, sizeof(dpaa_sec_session));
2414 dpaa_sec_set_session_parameters(struct rte_cryptodev *dev,
2415 struct rte_crypto_sym_xform *xform, void *sess)
2417 struct dpaa_sec_dev_private *internals = dev->data->dev_private;
2418 dpaa_sec_session *session = sess;
2422 PMD_INIT_FUNC_TRACE();
2424 if (unlikely(sess == NULL)) {
2425 DPAA_SEC_ERR("invalid session struct");
2428 memset(session, 0, sizeof(dpaa_sec_session));
2430 /* Default IV length = 0 */
2431 session->iv.length = 0;
2434 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER && xform->next == NULL) {
2435 session->auth_alg = RTE_CRYPTO_AUTH_NULL;
2436 ret = dpaa_sec_cipher_init(dev, xform, session);
2438 /* Authentication Only */
2439 } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
2440 xform->next == NULL) {
2441 session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
2442 session->ctxt = DPAA_SEC_AUTH;
2443 ret = dpaa_sec_auth_init(dev, xform, session);
2445 /* Cipher then Authenticate */
2446 } else if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
2447 xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
2448 if (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) {
2449 session->auth_cipher_text = 1;
2450 if (xform->cipher.algo == RTE_CRYPTO_CIPHER_NULL)
2451 ret = dpaa_sec_auth_init(dev, xform, session);
2452 else if (xform->next->auth.algo == RTE_CRYPTO_AUTH_NULL)
2453 ret = dpaa_sec_cipher_init(dev, xform, session);
2455 ret = dpaa_sec_chain_init(dev, xform, session);
2457 DPAA_SEC_ERR("Not supported: Auth then Cipher");
2460 /* Authenticate then Cipher */
2461 } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
2462 xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
2463 if (xform->next->cipher.op == RTE_CRYPTO_CIPHER_OP_DECRYPT) {
2464 session->auth_cipher_text = 0;
2465 if (xform->auth.algo == RTE_CRYPTO_AUTH_NULL)
2466 ret = dpaa_sec_cipher_init(dev, xform, session);
2467 else if (xform->next->cipher.algo
2468 == RTE_CRYPTO_CIPHER_NULL)
2469 ret = dpaa_sec_auth_init(dev, xform, session);
2471 ret = dpaa_sec_chain_init(dev, xform, session);
2473 DPAA_SEC_ERR("Not supported: Auth then Cipher");
2477 /* AEAD operation for AES-GCM kind of Algorithms */
2478 } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD &&
2479 xform->next == NULL) {
2480 ret = dpaa_sec_aead_init(dev, xform, session);
2483 DPAA_SEC_ERR("Invalid crypto type");
2487 DPAA_SEC_ERR("unable to init session");
2491 rte_spinlock_lock(&internals->lock);
2492 for (i = 0; i < MAX_DPAA_CORES; i++) {
2493 session->inq[i] = dpaa_sec_attach_rxq(internals);
2494 if (session->inq[i] == NULL) {
2495 DPAA_SEC_ERR("unable to attach sec queue");
2496 rte_spinlock_unlock(&internals->lock);
2501 rte_spinlock_unlock(&internals->lock);
2506 free_session_data(session);
2511 dpaa_sec_sym_session_configure(struct rte_cryptodev *dev,
2512 struct rte_crypto_sym_xform *xform,
2513 struct rte_cryptodev_sym_session *sess,
2514 struct rte_mempool *mempool)
2516 void *sess_private_data;
2519 PMD_INIT_FUNC_TRACE();
2521 if (rte_mempool_get(mempool, &sess_private_data)) {
2522 DPAA_SEC_ERR("Couldn't get object from session mempool");
2526 ret = dpaa_sec_set_session_parameters(dev, xform, sess_private_data);
2528 DPAA_SEC_ERR("failed to configure session parameters");
2530 /* Return session to mempool */
2531 rte_mempool_put(mempool, sess_private_data);
2535 set_sym_session_private_data(sess, dev->driver_id,
2543 free_session_memory(struct rte_cryptodev *dev, dpaa_sec_session *s)
2545 struct dpaa_sec_dev_private *qi = dev->data->dev_private;
2546 struct rte_mempool *sess_mp = rte_mempool_from_obj((void *)s);
2549 for (i = 0; i < MAX_DPAA_CORES; i++) {
2551 dpaa_sec_detach_rxq(qi, s->inq[i]);
2555 free_session_data(s);
2556 rte_mempool_put(sess_mp, (void *)s);
2559 /** Clear the memory of session so it doesn't leave key material behind */
2561 dpaa_sec_sym_session_clear(struct rte_cryptodev *dev,
2562 struct rte_cryptodev_sym_session *sess)
2564 PMD_INIT_FUNC_TRACE();
2565 uint8_t index = dev->driver_id;
2566 void *sess_priv = get_sym_session_private_data(sess, index);
2567 dpaa_sec_session *s = (dpaa_sec_session *)sess_priv;
2570 free_session_memory(dev, s);
2571 set_sym_session_private_data(sess, index, NULL);
2575 #ifdef RTE_LIB_SECURITY
2577 dpaa_sec_ipsec_aead_init(struct rte_crypto_aead_xform *aead_xform,
2578 struct rte_security_ipsec_xform *ipsec_xform,
2579 dpaa_sec_session *session)
2581 PMD_INIT_FUNC_TRACE();
2583 session->aead_key.data = rte_zmalloc(NULL, aead_xform->key.length,
2584 RTE_CACHE_LINE_SIZE);
2585 if (session->aead_key.data == NULL && aead_xform->key.length > 0) {
2586 DPAA_SEC_ERR("No Memory for aead key");
2589 memcpy(session->aead_key.data, aead_xform->key.data,
2590 aead_xform->key.length);
2592 session->digest_length = aead_xform->digest_length;
2593 session->aead_key.length = aead_xform->key.length;
2595 switch (aead_xform->algo) {
2596 case RTE_CRYPTO_AEAD_AES_GCM:
2597 switch (session->digest_length) {
2599 session->aead_key.alg = OP_PCL_IPSEC_AES_GCM8;
2602 session->aead_key.alg = OP_PCL_IPSEC_AES_GCM12;
2605 session->aead_key.alg = OP_PCL_IPSEC_AES_GCM16;
2608 DPAA_SEC_ERR("Crypto: Undefined GCM digest %d",
2609 session->digest_length);
2612 if (session->dir == DIR_ENC) {
2613 memcpy(session->encap_pdb.gcm.salt,
2614 (uint8_t *)&(ipsec_xform->salt), 4);
2616 memcpy(session->decap_pdb.gcm.salt,
2617 (uint8_t *)&(ipsec_xform->salt), 4);
2619 session->aead_key.algmode = OP_ALG_AAI_GCM;
2620 session->aead_alg = RTE_CRYPTO_AEAD_AES_GCM;
2623 DPAA_SEC_ERR("Crypto: Undefined AEAD specified %u",
2631 dpaa_sec_ipsec_proto_init(struct rte_crypto_cipher_xform *cipher_xform,
2632 struct rte_crypto_auth_xform *auth_xform,
2633 struct rte_security_ipsec_xform *ipsec_xform,
2634 dpaa_sec_session *session)
2637 session->cipher_key.data = rte_zmalloc(NULL,
2638 cipher_xform->key.length,
2639 RTE_CACHE_LINE_SIZE);
2640 if (session->cipher_key.data == NULL &&
2641 cipher_xform->key.length > 0) {
2642 DPAA_SEC_ERR("No Memory for cipher key");
2646 session->cipher_key.length = cipher_xform->key.length;
2647 memcpy(session->cipher_key.data, cipher_xform->key.data,
2648 cipher_xform->key.length);
2649 session->cipher_alg = cipher_xform->algo;
2651 session->cipher_key.data = NULL;
2652 session->cipher_key.length = 0;
2653 session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
2657 session->auth_key.data = rte_zmalloc(NULL,
2658 auth_xform->key.length,
2659 RTE_CACHE_LINE_SIZE);
2660 if (session->auth_key.data == NULL &&
2661 auth_xform->key.length > 0) {
2662 DPAA_SEC_ERR("No Memory for auth key");
2665 session->auth_key.length = auth_xform->key.length;
2666 memcpy(session->auth_key.data, auth_xform->key.data,
2667 auth_xform->key.length);
2668 session->auth_alg = auth_xform->algo;
2669 session->digest_length = auth_xform->digest_length;
2671 session->auth_key.data = NULL;
2672 session->auth_key.length = 0;
2673 session->auth_alg = RTE_CRYPTO_AUTH_NULL;
2676 switch (session->auth_alg) {
2677 case RTE_CRYPTO_AUTH_SHA1_HMAC:
2678 session->auth_key.alg = OP_PCL_IPSEC_HMAC_SHA1_96;
2679 session->auth_key.algmode = OP_ALG_AAI_HMAC;
2681 case RTE_CRYPTO_AUTH_MD5_HMAC:
2682 session->auth_key.alg = OP_PCL_IPSEC_HMAC_MD5_96;
2683 session->auth_key.algmode = OP_ALG_AAI_HMAC;
2685 case RTE_CRYPTO_AUTH_SHA256_HMAC:
2686 session->auth_key.alg = OP_PCL_IPSEC_HMAC_SHA2_256_128;
2687 session->auth_key.algmode = OP_ALG_AAI_HMAC;
2688 if (session->digest_length != 16)
2690 "+++Using sha256-hmac truncated len is non-standard,"
2691 "it will not work with lookaside proto");
2693 case RTE_CRYPTO_AUTH_SHA384_HMAC:
2694 session->auth_key.alg = OP_PCL_IPSEC_HMAC_SHA2_384_192;
2695 session->auth_key.algmode = OP_ALG_AAI_HMAC;
2697 case RTE_CRYPTO_AUTH_SHA512_HMAC:
2698 session->auth_key.alg = OP_PCL_IPSEC_HMAC_SHA2_512_256;
2699 session->auth_key.algmode = OP_ALG_AAI_HMAC;
2701 case RTE_CRYPTO_AUTH_AES_CMAC:
2702 session->auth_key.alg = OP_PCL_IPSEC_AES_CMAC_96;
2704 case RTE_CRYPTO_AUTH_NULL:
2705 session->auth_key.alg = OP_PCL_IPSEC_HMAC_NULL;
2707 case RTE_CRYPTO_AUTH_AES_XCBC_MAC:
2708 session->auth_key.alg = OP_PCL_IPSEC_AES_XCBC_MAC_96;
2709 session->auth_key.algmode = OP_ALG_AAI_XCBC_MAC;
2711 case RTE_CRYPTO_AUTH_SHA224_HMAC:
2712 case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
2713 case RTE_CRYPTO_AUTH_SHA1:
2714 case RTE_CRYPTO_AUTH_SHA256:
2715 case RTE_CRYPTO_AUTH_SHA512:
2716 case RTE_CRYPTO_AUTH_SHA224:
2717 case RTE_CRYPTO_AUTH_SHA384:
2718 case RTE_CRYPTO_AUTH_MD5:
2719 case RTE_CRYPTO_AUTH_AES_GMAC:
2720 case RTE_CRYPTO_AUTH_KASUMI_F9:
2721 case RTE_CRYPTO_AUTH_AES_CBC_MAC:
2722 case RTE_CRYPTO_AUTH_ZUC_EIA3:
2723 DPAA_SEC_ERR("Crypto: Unsupported auth alg %u",
2727 DPAA_SEC_ERR("Crypto: Undefined Auth specified %u",
2732 switch (session->cipher_alg) {
2733 case RTE_CRYPTO_CIPHER_AES_CBC:
2734 session->cipher_key.alg = OP_PCL_IPSEC_AES_CBC;
2735 session->cipher_key.algmode = OP_ALG_AAI_CBC;
2737 case RTE_CRYPTO_CIPHER_DES_CBC:
2738 session->cipher_key.alg = OP_PCL_IPSEC_DES;
2739 session->cipher_key.algmode = OP_ALG_AAI_CBC;
2741 case RTE_CRYPTO_CIPHER_3DES_CBC:
2742 session->cipher_key.alg = OP_PCL_IPSEC_3DES;
2743 session->cipher_key.algmode = OP_ALG_AAI_CBC;
2745 case RTE_CRYPTO_CIPHER_AES_CTR:
2746 session->cipher_key.alg = OP_PCL_IPSEC_AES_CTR;
2747 session->cipher_key.algmode = OP_ALG_AAI_CTR;
2748 if (session->dir == DIR_ENC) {
2749 session->encap_pdb.ctr.ctr_initial = 0x00000001;
2750 session->encap_pdb.ctr.ctr_nonce = ipsec_xform->salt;
2752 session->decap_pdb.ctr.ctr_initial = 0x00000001;
2753 session->decap_pdb.ctr.ctr_nonce = ipsec_xform->salt;
2756 case RTE_CRYPTO_CIPHER_NULL:
2757 session->cipher_key.alg = OP_PCL_IPSEC_NULL;
2759 case RTE_CRYPTO_CIPHER_SNOW3G_UEA2:
2760 case RTE_CRYPTO_CIPHER_ZUC_EEA3:
2761 case RTE_CRYPTO_CIPHER_3DES_ECB:
2762 case RTE_CRYPTO_CIPHER_AES_ECB:
2763 case RTE_CRYPTO_CIPHER_KASUMI_F8:
2764 DPAA_SEC_ERR("Crypto: Unsupported Cipher alg %u",
2765 session->cipher_alg);
2768 DPAA_SEC_ERR("Crypto: Undefined Cipher specified %u",
2769 session->cipher_alg);
2777 dpaa_sec_set_ipsec_session(__rte_unused struct rte_cryptodev *dev,
2778 struct rte_security_session_conf *conf,
2781 struct dpaa_sec_dev_private *internals = dev->data->dev_private;
2782 struct rte_security_ipsec_xform *ipsec_xform = &conf->ipsec;
2783 struct rte_crypto_auth_xform *auth_xform = NULL;
2784 struct rte_crypto_cipher_xform *cipher_xform = NULL;
2785 struct rte_crypto_aead_xform *aead_xform = NULL;
2786 dpaa_sec_session *session = (dpaa_sec_session *)sess;
2790 PMD_INIT_FUNC_TRACE();
2792 memset(session, 0, sizeof(dpaa_sec_session));
2793 session->proto_alg = conf->protocol;
2794 session->ctxt = DPAA_SEC_IPSEC;
2796 if (ipsec_xform->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS)
2797 session->dir = DIR_ENC;
2799 session->dir = DIR_DEC;
2801 if (conf->crypto_xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
2802 cipher_xform = &conf->crypto_xform->cipher;
2803 if (conf->crypto_xform->next)
2804 auth_xform = &conf->crypto_xform->next->auth;
2805 ret = dpaa_sec_ipsec_proto_init(cipher_xform, auth_xform,
2806 ipsec_xform, session);
2807 } else if (conf->crypto_xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
2808 auth_xform = &conf->crypto_xform->auth;
2809 if (conf->crypto_xform->next)
2810 cipher_xform = &conf->crypto_xform->next->cipher;
2811 ret = dpaa_sec_ipsec_proto_init(cipher_xform, auth_xform,
2812 ipsec_xform, session);
2813 } else if (conf->crypto_xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
2814 aead_xform = &conf->crypto_xform->aead;
2815 ret = dpaa_sec_ipsec_aead_init(aead_xform,
2816 ipsec_xform, session);
2818 DPAA_SEC_ERR("XFORM not specified");
2823 DPAA_SEC_ERR("Failed to process xform");
2827 if (ipsec_xform->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
2828 if (ipsec_xform->tunnel.type ==
2829 RTE_SECURITY_IPSEC_TUNNEL_IPV4) {
2830 session->ip4_hdr.ip_v = IPVERSION;
2831 session->ip4_hdr.ip_hl = 5;
2832 session->ip4_hdr.ip_len = rte_cpu_to_be_16(
2833 sizeof(session->ip4_hdr));
2834 session->ip4_hdr.ip_tos = ipsec_xform->tunnel.ipv4.dscp;
2835 session->ip4_hdr.ip_id = 0;
2836 session->ip4_hdr.ip_off = 0;
2837 session->ip4_hdr.ip_ttl = ipsec_xform->tunnel.ipv4.ttl;
2838 session->ip4_hdr.ip_p = (ipsec_xform->proto ==
2839 RTE_SECURITY_IPSEC_SA_PROTO_ESP) ?
2840 IPPROTO_ESP : IPPROTO_AH;
2841 session->ip4_hdr.ip_sum = 0;
2842 session->ip4_hdr.ip_src =
2843 ipsec_xform->tunnel.ipv4.src_ip;
2844 session->ip4_hdr.ip_dst =
2845 ipsec_xform->tunnel.ipv4.dst_ip;
2846 session->ip4_hdr.ip_sum = calc_chksum((uint16_t *)
2847 (void *)&session->ip4_hdr,
2849 session->encap_pdb.ip_hdr_len = sizeof(struct ip);
2850 } else if (ipsec_xform->tunnel.type ==
2851 RTE_SECURITY_IPSEC_TUNNEL_IPV6) {
2852 session->ip6_hdr.vtc_flow = rte_cpu_to_be_32(
2853 DPAA_IPv6_DEFAULT_VTC_FLOW |
2854 ((ipsec_xform->tunnel.ipv6.dscp <<
2855 RTE_IPV6_HDR_TC_SHIFT) &
2856 RTE_IPV6_HDR_TC_MASK) |
2857 ((ipsec_xform->tunnel.ipv6.flabel <<
2858 RTE_IPV6_HDR_FL_SHIFT) &
2859 RTE_IPV6_HDR_FL_MASK));
2860 /* Payload length will be updated by HW */
2861 session->ip6_hdr.payload_len = 0;
2862 session->ip6_hdr.hop_limits =
2863 ipsec_xform->tunnel.ipv6.hlimit;
2864 session->ip6_hdr.proto = (ipsec_xform->proto ==
2865 RTE_SECURITY_IPSEC_SA_PROTO_ESP) ?
2866 IPPROTO_ESP : IPPROTO_AH;
2867 memcpy(&session->ip6_hdr.src_addr,
2868 &ipsec_xform->tunnel.ipv6.src_addr, 16);
2869 memcpy(&session->ip6_hdr.dst_addr,
2870 &ipsec_xform->tunnel.ipv6.dst_addr, 16);
2871 session->encap_pdb.ip_hdr_len =
2872 sizeof(struct rte_ipv6_hdr);
2874 session->encap_pdb.options =
2875 (IPVERSION << PDBNH_ESP_ENCAP_SHIFT) |
2876 PDBOPTS_ESP_OIHI_PDB_INL |
2878 PDBHMO_ESP_ENCAP_DTTL |
2880 if (ipsec_xform->options.esn)
2881 session->encap_pdb.options |= PDBOPTS_ESP_ESN;
2882 session->encap_pdb.spi = ipsec_xform->spi;
2884 } else if (ipsec_xform->direction ==
2885 RTE_SECURITY_IPSEC_SA_DIR_INGRESS) {
2886 if (ipsec_xform->tunnel.type == RTE_SECURITY_IPSEC_TUNNEL_IPV4)
2887 session->decap_pdb.options = sizeof(struct ip) << 16;
2889 session->decap_pdb.options =
2890 sizeof(struct rte_ipv6_hdr) << 16;
2891 if (ipsec_xform->options.esn)
2892 session->decap_pdb.options |= PDBOPTS_ESP_ESN;
2893 if (ipsec_xform->replay_win_sz) {
2895 win_sz = rte_align32pow2(ipsec_xform->replay_win_sz);
2904 session->decap_pdb.options |= PDBOPTS_ESP_ARS32;
2907 session->decap_pdb.options |= PDBOPTS_ESP_ARS64;
2910 session->decap_pdb.options |=
2916 rte_spinlock_lock(&internals->lock);
2917 for (i = 0; i < MAX_DPAA_CORES; i++) {
2918 session->inq[i] = dpaa_sec_attach_rxq(internals);
2919 if (session->inq[i] == NULL) {
2920 DPAA_SEC_ERR("unable to attach sec queue");
2921 rte_spinlock_unlock(&internals->lock);
2925 rte_spinlock_unlock(&internals->lock);
2929 free_session_data(session);
2934 dpaa_sec_set_pdcp_session(struct rte_cryptodev *dev,
2935 struct rte_security_session_conf *conf,
2938 struct rte_security_pdcp_xform *pdcp_xform = &conf->pdcp;
2939 struct rte_crypto_sym_xform *xform = conf->crypto_xform;
2940 struct rte_crypto_auth_xform *auth_xform = NULL;
2941 struct rte_crypto_cipher_xform *cipher_xform = NULL;
2942 dpaa_sec_session *session = (dpaa_sec_session *)sess;
2943 struct dpaa_sec_dev_private *dev_priv = dev->data->dev_private;
2947 PMD_INIT_FUNC_TRACE();
2949 memset(session, 0, sizeof(dpaa_sec_session));
2951 /* find xfrm types */
2952 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
2953 cipher_xform = &xform->cipher;
2954 if (xform->next != NULL)
2955 auth_xform = &xform->next->auth;
2956 } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
2957 auth_xform = &xform->auth;
2958 if (xform->next != NULL)
2959 cipher_xform = &xform->next->cipher;
2961 DPAA_SEC_ERR("Invalid crypto type");
2965 session->proto_alg = conf->protocol;
2966 session->ctxt = DPAA_SEC_PDCP;
2969 switch (cipher_xform->algo) {
2970 case RTE_CRYPTO_CIPHER_SNOW3G_UEA2:
2971 session->cipher_key.alg = PDCP_CIPHER_TYPE_SNOW;
2973 case RTE_CRYPTO_CIPHER_ZUC_EEA3:
2974 session->cipher_key.alg = PDCP_CIPHER_TYPE_ZUC;
2976 case RTE_CRYPTO_CIPHER_AES_CTR:
2977 session->cipher_key.alg = PDCP_CIPHER_TYPE_AES;
2979 case RTE_CRYPTO_CIPHER_NULL:
2980 session->cipher_key.alg = PDCP_CIPHER_TYPE_NULL;
2983 DPAA_SEC_ERR("Crypto: Undefined Cipher specified %u",
2984 session->cipher_alg);
2988 session->cipher_key.data = rte_zmalloc(NULL,
2989 cipher_xform->key.length,
2990 RTE_CACHE_LINE_SIZE);
2991 if (session->cipher_key.data == NULL &&
2992 cipher_xform->key.length > 0) {
2993 DPAA_SEC_ERR("No Memory for cipher key");
2996 session->cipher_key.length = cipher_xform->key.length;
2997 memcpy(session->cipher_key.data, cipher_xform->key.data,
2998 cipher_xform->key.length);
2999 session->dir = (cipher_xform->op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
3001 session->cipher_alg = cipher_xform->algo;
3003 session->cipher_key.data = NULL;
3004 session->cipher_key.length = 0;
3005 session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
3006 session->dir = DIR_ENC;
3009 if (pdcp_xform->domain == RTE_SECURITY_PDCP_MODE_CONTROL) {
3010 if (pdcp_xform->sn_size != RTE_SECURITY_PDCP_SN_SIZE_5 &&
3011 pdcp_xform->sn_size != RTE_SECURITY_PDCP_SN_SIZE_12) {
3013 "PDCP Seq Num size should be 5/12 bits for cmode");
3020 switch (auth_xform->algo) {
3021 case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
3022 session->auth_key.alg = PDCP_AUTH_TYPE_SNOW;
3024 case RTE_CRYPTO_AUTH_ZUC_EIA3:
3025 session->auth_key.alg = PDCP_AUTH_TYPE_ZUC;
3027 case RTE_CRYPTO_AUTH_AES_CMAC:
3028 session->auth_key.alg = PDCP_AUTH_TYPE_AES;
3030 case RTE_CRYPTO_AUTH_NULL:
3031 session->auth_key.alg = PDCP_AUTH_TYPE_NULL;
3034 DPAA_SEC_ERR("Crypto: Unsupported auth alg %u",
3036 rte_free(session->cipher_key.data);
3039 session->auth_key.data = rte_zmalloc(NULL,
3040 auth_xform->key.length,
3041 RTE_CACHE_LINE_SIZE);
3042 if (!session->auth_key.data &&
3043 auth_xform->key.length > 0) {
3044 DPAA_SEC_ERR("No Memory for auth key");
3045 rte_free(session->cipher_key.data);
3048 session->auth_key.length = auth_xform->key.length;
3049 memcpy(session->auth_key.data, auth_xform->key.data,
3050 auth_xform->key.length);
3051 session->auth_alg = auth_xform->algo;
3053 session->auth_key.data = NULL;
3054 session->auth_key.length = 0;
3055 session->auth_alg = 0;
3057 session->pdcp.domain = pdcp_xform->domain;
3058 session->pdcp.bearer = pdcp_xform->bearer;
3059 session->pdcp.pkt_dir = pdcp_xform->pkt_dir;
3060 session->pdcp.sn_size = pdcp_xform->sn_size;
3061 session->pdcp.hfn = pdcp_xform->hfn;
3062 session->pdcp.hfn_threshold = pdcp_xform->hfn_threshold;
3063 session->pdcp.hfn_ovd = pdcp_xform->hfn_ovrd;
3064 session->pdcp.sdap_enabled = pdcp_xform->sdap_enabled;
3066 session->pdcp.hfn_ovd_offset = cipher_xform->iv.offset;
3068 rte_spinlock_lock(&dev_priv->lock);
3069 for (i = 0; i < MAX_DPAA_CORES; i++) {
3070 session->inq[i] = dpaa_sec_attach_rxq(dev_priv);
3071 if (session->inq[i] == NULL) {
3072 DPAA_SEC_ERR("unable to attach sec queue");
3073 rte_spinlock_unlock(&dev_priv->lock);
3078 rte_spinlock_unlock(&dev_priv->lock);
3081 rte_free(session->auth_key.data);
3082 rte_free(session->cipher_key.data);
3083 memset(session, 0, sizeof(dpaa_sec_session));
3088 dpaa_sec_security_session_create(void *dev,
3089 struct rte_security_session_conf *conf,
3090 struct rte_security_session *sess,
3091 struct rte_mempool *mempool)
3093 void *sess_private_data;
3094 struct rte_cryptodev *cdev = (struct rte_cryptodev *)dev;
3097 if (rte_mempool_get(mempool, &sess_private_data)) {
3098 DPAA_SEC_ERR("Couldn't get object from session mempool");
3102 switch (conf->protocol) {
3103 case RTE_SECURITY_PROTOCOL_IPSEC:
3104 ret = dpaa_sec_set_ipsec_session(cdev, conf,
3107 case RTE_SECURITY_PROTOCOL_PDCP:
3108 ret = dpaa_sec_set_pdcp_session(cdev, conf,
3111 case RTE_SECURITY_PROTOCOL_MACSEC:
3117 DPAA_SEC_ERR("failed to configure session parameters");
3118 /* Return session to mempool */
3119 rte_mempool_put(mempool, sess_private_data);
3123 set_sec_session_private_data(sess, sess_private_data);
3128 /** Clear the memory of session so it doesn't leave key material behind */
3130 dpaa_sec_security_session_destroy(void *dev __rte_unused,
3131 struct rte_security_session *sess)
3133 PMD_INIT_FUNC_TRACE();
3134 void *sess_priv = get_sec_session_private_data(sess);
3135 dpaa_sec_session *s = (dpaa_sec_session *)sess_priv;
3138 free_session_memory((struct rte_cryptodev *)dev, s);
3139 set_sec_session_private_data(sess, NULL);
3145 dpaa_sec_dev_configure(struct rte_cryptodev *dev __rte_unused,
3146 struct rte_cryptodev_config *config __rte_unused)
3148 PMD_INIT_FUNC_TRACE();
3154 dpaa_sec_dev_start(struct rte_cryptodev *dev __rte_unused)
3156 PMD_INIT_FUNC_TRACE();
3161 dpaa_sec_dev_stop(struct rte_cryptodev *dev __rte_unused)
3163 PMD_INIT_FUNC_TRACE();
3167 dpaa_sec_dev_close(struct rte_cryptodev *dev)
3169 PMD_INIT_FUNC_TRACE();
3178 dpaa_sec_dev_infos_get(struct rte_cryptodev *dev,
3179 struct rte_cryptodev_info *info)
3181 struct dpaa_sec_dev_private *internals = dev->data->dev_private;
3183 PMD_INIT_FUNC_TRACE();
3185 info->max_nb_queue_pairs = internals->max_nb_queue_pairs;
3186 info->feature_flags = dev->feature_flags;
3187 info->capabilities = dpaa_sec_capabilities;
3188 info->sym.max_nb_sessions = internals->max_nb_sessions;
3189 info->driver_id = cryptodev_driver_id;
3193 static enum qman_cb_dqrr_result
3194 dpaa_sec_process_parallel_event(void *event,
3195 struct qman_portal *qm __always_unused,
3196 struct qman_fq *outq,
3197 const struct qm_dqrr_entry *dqrr,
3200 const struct qm_fd *fd;
3201 struct dpaa_sec_job *job;
3202 struct dpaa_sec_op_ctx *ctx;
3203 struct rte_event *ev = (struct rte_event *)event;
3207 /* sg is embedded in an op ctx,
3208 * sg[0] is for output
3211 job = rte_dpaa_mem_ptov(qm_fd_addr_get64(fd));
3213 ctx = container_of(job, struct dpaa_sec_op_ctx, job);
3214 ctx->fd_status = fd->status;
3215 if (ctx->op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
3216 struct qm_sg_entry *sg_out;
3219 sg_out = &job->sg[0];
3220 hw_sg_to_cpu(sg_out);
3221 len = sg_out->length;
3222 ctx->op->sym->m_src->pkt_len = len;
3223 ctx->op->sym->m_src->data_len = len;
3225 if (!ctx->fd_status) {
3226 ctx->op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
3228 DPAA_SEC_DP_WARN("SEC return err: 0x%x", ctx->fd_status);
3229 ctx->op->status = RTE_CRYPTO_OP_STATUS_ERROR;
3231 ev->event_ptr = (void *)ctx->op;
3233 ev->flow_id = outq->ev.flow_id;
3234 ev->sub_event_type = outq->ev.sub_event_type;
3235 ev->event_type = RTE_EVENT_TYPE_CRYPTODEV;
3236 ev->op = RTE_EVENT_OP_NEW;
3237 ev->sched_type = outq->ev.sched_type;
3238 ev->queue_id = outq->ev.queue_id;
3239 ev->priority = outq->ev.priority;
3240 *bufs = (void *)ctx->op;
3242 rte_mempool_put(ctx->ctx_pool, (void *)ctx);
3244 return qman_cb_dqrr_consume;
3247 static enum qman_cb_dqrr_result
3248 dpaa_sec_process_atomic_event(void *event,
3249 struct qman_portal *qm __rte_unused,
3250 struct qman_fq *outq,
3251 const struct qm_dqrr_entry *dqrr,
3255 const struct qm_fd *fd;
3256 struct dpaa_sec_job *job;
3257 struct dpaa_sec_op_ctx *ctx;
3258 struct rte_event *ev = (struct rte_event *)event;
3262 /* sg is embedded in an op ctx,
3263 * sg[0] is for output
3266 job = rte_dpaa_mem_ptov(qm_fd_addr_get64(fd));
3268 ctx = container_of(job, struct dpaa_sec_op_ctx, job);
3269 ctx->fd_status = fd->status;
3270 if (ctx->op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
3271 struct qm_sg_entry *sg_out;
3274 sg_out = &job->sg[0];
3275 hw_sg_to_cpu(sg_out);
3276 len = sg_out->length;
3277 ctx->op->sym->m_src->pkt_len = len;
3278 ctx->op->sym->m_src->data_len = len;
3280 if (!ctx->fd_status) {
3281 ctx->op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
3283 DPAA_SEC_DP_WARN("SEC return err: 0x%x", ctx->fd_status);
3284 ctx->op->status = RTE_CRYPTO_OP_STATUS_ERROR;
3286 ev->event_ptr = (void *)ctx->op;
3287 ev->flow_id = outq->ev.flow_id;
3288 ev->sub_event_type = outq->ev.sub_event_type;
3289 ev->event_type = RTE_EVENT_TYPE_CRYPTODEV;
3290 ev->op = RTE_EVENT_OP_NEW;
3291 ev->sched_type = outq->ev.sched_type;
3292 ev->queue_id = outq->ev.queue_id;
3293 ev->priority = outq->ev.priority;
3295 /* Save active dqrr entries */
3296 index = ((uintptr_t)dqrr >> 6) & (16/*QM_DQRR_SIZE*/ - 1);
3297 DPAA_PER_LCORE_DQRR_SIZE++;
3298 DPAA_PER_LCORE_DQRR_HELD |= 1 << index;
3299 DPAA_PER_LCORE_DQRR_MBUF(index) = ctx->op->sym->m_src;
3300 ev->impl_opaque = index + 1;
3301 *dpaa_seqn(ctx->op->sym->m_src) = (uint32_t)index + 1;
3302 *bufs = (void *)ctx->op;
3304 rte_mempool_put(ctx->ctx_pool, (void *)ctx);
3306 return qman_cb_dqrr_defer;
3310 dpaa_sec_eventq_attach(const struct rte_cryptodev *dev,
3313 const struct rte_event *event)
3315 struct dpaa_sec_qp *qp = dev->data->queue_pairs[qp_id];
3316 struct qm_mcc_initfq opts = {0};
3320 opts.we_mask = QM_INITFQ_WE_DESTWQ | QM_INITFQ_WE_FQCTRL |
3321 QM_INITFQ_WE_CONTEXTA | QM_INITFQ_WE_CONTEXTB;
3322 opts.fqd.dest.channel = ch_id;
3324 switch (event->sched_type) {
3325 case RTE_SCHED_TYPE_ATOMIC:
3326 opts.fqd.fq_ctrl |= QM_FQCTRL_HOLDACTIVE;
3327 /* Reset FQCTRL_AVOIDBLOCK bit as it is unnecessary
3328 * configuration with HOLD_ACTIVE setting
3330 opts.fqd.fq_ctrl &= (~QM_FQCTRL_AVOIDBLOCK);
3331 qp->outq.cb.dqrr_dpdk_cb = dpaa_sec_process_atomic_event;
3333 case RTE_SCHED_TYPE_ORDERED:
3334 DPAA_SEC_ERR("Ordered queue schedule type is not supported\n");
3337 opts.fqd.fq_ctrl |= QM_FQCTRL_AVOIDBLOCK;
3338 qp->outq.cb.dqrr_dpdk_cb = dpaa_sec_process_parallel_event;
3342 ret = qman_init_fq(&qp->outq, QMAN_INITFQ_FLAG_SCHED, &opts);
3343 if (unlikely(ret)) {
3344 DPAA_SEC_ERR("unable to init caam source fq!");
3348 memcpy(&qp->outq.ev, event, sizeof(struct rte_event));
3354 dpaa_sec_eventq_detach(const struct rte_cryptodev *dev,
3357 struct qm_mcc_initfq opts = {0};
3359 struct dpaa_sec_qp *qp = dev->data->queue_pairs[qp_id];
3361 opts.we_mask = QM_INITFQ_WE_DESTWQ | QM_INITFQ_WE_FQCTRL |
3362 QM_INITFQ_WE_CONTEXTA | QM_INITFQ_WE_CONTEXTB;
3363 qp->outq.cb.dqrr = dqrr_out_fq_cb_rx;
3364 qp->outq.cb.ern = ern_sec_fq_handler;
3365 qman_retire_fq(&qp->outq, NULL);
3366 qman_oos_fq(&qp->outq);
3367 ret = qman_init_fq(&qp->outq, 0, &opts);
3369 RTE_LOG(ERR, PMD, "Error in qman_init_fq: ret: %d\n", ret);
3370 qp->outq.cb.dqrr = NULL;
3375 static struct rte_cryptodev_ops crypto_ops = {
3376 .dev_configure = dpaa_sec_dev_configure,
3377 .dev_start = dpaa_sec_dev_start,
3378 .dev_stop = dpaa_sec_dev_stop,
3379 .dev_close = dpaa_sec_dev_close,
3380 .dev_infos_get = dpaa_sec_dev_infos_get,
3381 .queue_pair_setup = dpaa_sec_queue_pair_setup,
3382 .queue_pair_release = dpaa_sec_queue_pair_release,
3383 .sym_session_get_size = dpaa_sec_sym_session_get_size,
3384 .sym_session_configure = dpaa_sec_sym_session_configure,
3385 .sym_session_clear = dpaa_sec_sym_session_clear
3388 #ifdef RTE_LIB_SECURITY
3389 static const struct rte_security_capability *
3390 dpaa_sec_capabilities_get(void *device __rte_unused)
3392 return dpaa_sec_security_cap;
3395 static const struct rte_security_ops dpaa_sec_security_ops = {
3396 .session_create = dpaa_sec_security_session_create,
3397 .session_update = NULL,
3398 .session_stats_get = NULL,
3399 .session_destroy = dpaa_sec_security_session_destroy,
3400 .set_pkt_metadata = NULL,
3401 .capabilities_get = dpaa_sec_capabilities_get
3405 dpaa_sec_uninit(struct rte_cryptodev *dev)
3407 struct dpaa_sec_dev_private *internals;
3412 internals = dev->data->dev_private;
3413 rte_free(dev->security_ctx);
3415 rte_free(internals);
3417 DPAA_SEC_INFO("Closing DPAA_SEC device %s on numa socket %u",
3418 dev->data->name, rte_socket_id());
3424 dpaa_sec_dev_init(struct rte_cryptodev *cryptodev)
3426 struct dpaa_sec_dev_private *internals;
3427 #ifdef RTE_LIB_SECURITY
3428 struct rte_security_ctx *security_instance;
3430 struct dpaa_sec_qp *qp;
3434 PMD_INIT_FUNC_TRACE();
3436 cryptodev->driver_id = cryptodev_driver_id;
3437 cryptodev->dev_ops = &crypto_ops;
3439 cryptodev->enqueue_burst = dpaa_sec_enqueue_burst;
3440 cryptodev->dequeue_burst = dpaa_sec_dequeue_burst;
3441 cryptodev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
3442 RTE_CRYPTODEV_FF_HW_ACCELERATED |
3443 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
3444 RTE_CRYPTODEV_FF_SECURITY |
3445 RTE_CRYPTODEV_FF_IN_PLACE_SGL |
3446 RTE_CRYPTODEV_FF_OOP_SGL_IN_SGL_OUT |
3447 RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT |
3448 RTE_CRYPTODEV_FF_OOP_LB_IN_SGL_OUT |
3449 RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT;
3451 internals = cryptodev->data->dev_private;
3452 internals->max_nb_queue_pairs = RTE_DPAA_MAX_NB_SEC_QPS;
3453 internals->max_nb_sessions = RTE_DPAA_SEC_PMD_MAX_NB_SESSIONS;
3456 * For secondary processes, we don't initialise any further as primary
3457 * has already done this work. Only check we don't need a different
3460 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
3461 DPAA_SEC_WARN("Device already init by primary process");
3464 #ifdef RTE_LIB_SECURITY
3465 /* Initialize security_ctx only for primary process*/
3466 security_instance = rte_malloc("rte_security_instances_ops",
3467 sizeof(struct rte_security_ctx), 0);
3468 if (security_instance == NULL)
3470 security_instance->device = (void *)cryptodev;
3471 security_instance->ops = &dpaa_sec_security_ops;
3472 security_instance->sess_cnt = 0;
3473 cryptodev->security_ctx = security_instance;
3475 rte_spinlock_init(&internals->lock);
3476 for (i = 0; i < internals->max_nb_queue_pairs; i++) {
3477 /* init qman fq for queue pair */
3478 qp = &internals->qps[i];
3479 ret = dpaa_sec_init_tx(&qp->outq);
3481 DPAA_SEC_ERR("config tx of queue pair %d", i);
3486 flags = QMAN_FQ_FLAG_LOCKED | QMAN_FQ_FLAG_DYNAMIC_FQID |
3487 QMAN_FQ_FLAG_TO_DCPORTAL;
3488 for (i = 0; i < RTE_DPAA_MAX_RX_QUEUE; i++) {
3489 /* create rx qman fq for sessions*/
3490 ret = qman_create_fq(0, flags, &internals->inq[i]);
3491 if (unlikely(ret != 0)) {
3492 DPAA_SEC_ERR("sec qman_create_fq failed");
3497 RTE_LOG(INFO, PMD, "%s cryptodev init\n", cryptodev->data->name);
3501 DPAA_SEC_ERR("driver %s: create failed\n", cryptodev->data->name);
3503 rte_free(cryptodev->security_ctx);
3508 cryptodev_dpaa_sec_probe(struct rte_dpaa_driver *dpaa_drv __rte_unused,
3509 struct rte_dpaa_device *dpaa_dev)
3511 struct rte_cryptodev *cryptodev;
3512 char cryptodev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
3516 snprintf(cryptodev_name, sizeof(cryptodev_name), "%s", dpaa_dev->name);
3518 cryptodev = rte_cryptodev_pmd_allocate(cryptodev_name, rte_socket_id());
3519 if (cryptodev == NULL)
3522 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
3523 cryptodev->data->dev_private = rte_zmalloc_socket(
3524 "cryptodev private structure",
3525 sizeof(struct dpaa_sec_dev_private),
3526 RTE_CACHE_LINE_SIZE,
3529 if (cryptodev->data->dev_private == NULL)
3530 rte_panic("Cannot allocate memzone for private "
3534 dpaa_dev->crypto_dev = cryptodev;
3535 cryptodev->device = &dpaa_dev->device;
3537 /* init user callbacks */
3538 TAILQ_INIT(&(cryptodev->link_intr_cbs));
3540 /* if sec device version is not configured */
3541 if (!rta_get_sec_era()) {
3542 const struct device_node *caam_node;
3544 for_each_compatible_node(caam_node, NULL, "fsl,sec-v4.0") {
3545 const uint32_t *prop = of_get_property(caam_node,
3550 INTL_SEC_ERA(rte_cpu_to_be_32(*prop)));
3556 if (unlikely(!DPAA_PER_LCORE_PORTAL)) {
3557 retval = rte_dpaa_portal_init((void *)1);
3559 DPAA_SEC_ERR("Unable to initialize portal");
3564 /* Invoke PMD device initialization function */
3565 retval = dpaa_sec_dev_init(cryptodev);
3571 /* In case of error, cleanup is done */
3572 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
3573 rte_free(cryptodev->data->dev_private);
3575 rte_cryptodev_pmd_release_device(cryptodev);
3581 cryptodev_dpaa_sec_remove(struct rte_dpaa_device *dpaa_dev)
3583 struct rte_cryptodev *cryptodev;
3586 cryptodev = dpaa_dev->crypto_dev;
3587 if (cryptodev == NULL)
3590 ret = dpaa_sec_uninit(cryptodev);
3594 return rte_cryptodev_pmd_destroy(cryptodev);
3597 static struct rte_dpaa_driver rte_dpaa_sec_driver = {
3598 .drv_type = FSL_DPAA_CRYPTO,
3600 .name = "DPAA SEC PMD"
3602 .probe = cryptodev_dpaa_sec_probe,
3603 .remove = cryptodev_dpaa_sec_remove,
3606 static struct cryptodev_driver dpaa_sec_crypto_drv;
3608 RTE_PMD_REGISTER_DPAA(CRYPTODEV_NAME_DPAA_SEC_PMD, rte_dpaa_sec_driver);
3609 RTE_PMD_REGISTER_CRYPTO_DRIVER(dpaa_sec_crypto_drv, rte_dpaa_sec_driver.driver,
3610 cryptodev_driver_id);
3611 RTE_LOG_REGISTER(dpaa_logtype_sec, pmd.crypto.dpaa, NOTICE);