1 /* SPDX-License-Identifier: BSD-3-Clause
3 * Copyright (c) 2016 Freescale Semiconductor, Inc. All rights reserved.
4 * Copyright 2017-2019 NXP
13 #include <rte_byteorder.h>
14 #include <rte_common.h>
15 #include <rte_cryptodev_pmd.h>
16 #include <rte_crypto.h>
17 #include <rte_cryptodev.h>
18 #include <rte_security_driver.h>
19 #include <rte_cycles.h>
21 #include <rte_kvargs.h>
22 #include <rte_malloc.h>
24 #include <rte_memcpy.h>
25 #include <rte_string_fns.h>
26 #include <rte_spinlock.h>
32 /* RTA header files */
33 #include <hw/desc/common.h>
34 #include <hw/desc/algo.h>
35 #include <hw/desc/ipsec.h>
36 #include <hw/desc/pdcp.h>
38 #include <rte_dpaa_bus.h>
40 #include <dpaa_sec_log.h>
41 #include <dpaax_iova_table.h>
43 enum rta_sec_era rta_sec_era;
47 static uint8_t cryptodev_driver_id;
49 static __thread struct rte_crypto_op **dpaa_sec_ops;
50 static __thread int dpaa_sec_op_nb;
53 dpaa_sec_attach_sess_q(struct dpaa_sec_qp *qp, dpaa_sec_session *sess);
56 dpaa_sec_op_ending(struct dpaa_sec_op_ctx *ctx)
58 if (!ctx->fd_status) {
59 ctx->op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
61 DPAA_SEC_DP_WARN("SEC return err: 0x%x", ctx->fd_status);
62 ctx->op->status = RTE_CRYPTO_OP_STATUS_ERROR;
65 /* report op status to sym->op and then free the ctx memory */
66 rte_mempool_put(ctx->ctx_pool, (void *)ctx);
69 static inline struct dpaa_sec_op_ctx *
70 dpaa_sec_alloc_ctx(dpaa_sec_session *ses, int sg_count)
72 struct dpaa_sec_op_ctx *ctx;
75 retval = rte_mempool_get(ses->ctx_pool, (void **)(&ctx));
77 DPAA_SEC_DP_WARN("Alloc sec descriptor failed!");
81 * Clear SG memory. There are 16 SG entries of 16 Bytes each.
82 * one call to dcbz_64() clear 64 bytes, hence calling it 4 times
83 * to clear all the SG entries. dpaa_sec_alloc_ctx() is called for
84 * each packet, memset is costlier than dcbz_64().
86 for (i = 0; i < sg_count && i < MAX_JOB_SG_ENTRIES; i += 4)
87 dcbz_64(&ctx->job.sg[i]);
89 ctx->ctx_pool = ses->ctx_pool;
90 ctx->vtop_offset = (size_t) ctx - rte_mempool_virt2iova(ctx);
95 static inline rte_iova_t
96 dpaa_mem_vtop(void *vaddr)
98 const struct rte_memseg *ms;
100 ms = rte_mem_virt2memseg(vaddr, NULL);
102 dpaax_iova_table_update(ms->iova, ms->addr, ms->len);
103 return ms->iova + RTE_PTR_DIFF(vaddr, ms->addr);
109 dpaa_mem_ptov(rte_iova_t paddr)
113 va = (void *)dpaax_iova_table_get_va(paddr);
117 return rte_mem_iova2virt(paddr);
121 ern_sec_fq_handler(struct qman_portal *qm __rte_unused,
123 const struct qm_mr_entry *msg)
125 DPAA_SEC_DP_ERR("sec fq %d error, RC = %x, seqnum = %x\n",
126 fq->fqid, msg->ern.rc, msg->ern.seqnum);
129 /* initialize the queue with dest chan as caam chan so that
130 * all the packets in this queue could be dispatched into caam
133 dpaa_sec_init_rx(struct qman_fq *fq_in, rte_iova_t hwdesc,
136 struct qm_mcc_initfq fq_opts;
140 /* Clear FQ options */
141 memset(&fq_opts, 0x00, sizeof(struct qm_mcc_initfq));
143 flags = QMAN_INITFQ_FLAG_SCHED;
144 fq_opts.we_mask = QM_INITFQ_WE_DESTWQ | QM_INITFQ_WE_CONTEXTA |
145 QM_INITFQ_WE_CONTEXTB;
147 qm_fqd_context_a_set64(&fq_opts.fqd, hwdesc);
148 fq_opts.fqd.context_b = fqid_out;
149 fq_opts.fqd.dest.channel = qm_channel_caam;
150 fq_opts.fqd.dest.wq = 0;
152 fq_in->cb.ern = ern_sec_fq_handler;
154 DPAA_SEC_DEBUG("in-%x out-%x", fq_in->fqid, fqid_out);
156 ret = qman_init_fq(fq_in, flags, &fq_opts);
157 if (unlikely(ret != 0))
158 DPAA_SEC_ERR("qman_init_fq failed %d", ret);
163 /* something is put into in_fq and caam put the crypto result into out_fq */
164 static enum qman_cb_dqrr_result
165 dqrr_out_fq_cb_rx(struct qman_portal *qm __always_unused,
166 struct qman_fq *fq __always_unused,
167 const struct qm_dqrr_entry *dqrr)
169 const struct qm_fd *fd;
170 struct dpaa_sec_job *job;
171 struct dpaa_sec_op_ctx *ctx;
173 if (dpaa_sec_op_nb >= DPAA_SEC_BURST)
174 return qman_cb_dqrr_defer;
176 if (!(dqrr->stat & QM_DQRR_STAT_FD_VALID))
177 return qman_cb_dqrr_consume;
180 /* sg is embedded in an op ctx,
181 * sg[0] is for output
184 job = dpaa_mem_ptov(qm_fd_addr_get64(fd));
186 ctx = container_of(job, struct dpaa_sec_op_ctx, job);
187 ctx->fd_status = fd->status;
188 if (ctx->op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
189 struct qm_sg_entry *sg_out;
191 struct rte_mbuf *mbuf = (ctx->op->sym->m_dst == NULL) ?
192 ctx->op->sym->m_src : ctx->op->sym->m_dst;
194 sg_out = &job->sg[0];
195 hw_sg_to_cpu(sg_out);
196 len = sg_out->length;
198 while (mbuf->next != NULL) {
199 len -= mbuf->data_len;
202 mbuf->data_len = len;
204 dpaa_sec_ops[dpaa_sec_op_nb++] = ctx->op;
205 dpaa_sec_op_ending(ctx);
207 return qman_cb_dqrr_consume;
210 /* caam result is put into this queue */
212 dpaa_sec_init_tx(struct qman_fq *fq)
215 struct qm_mcc_initfq opts;
218 flags = QMAN_FQ_FLAG_NO_ENQUEUE | QMAN_FQ_FLAG_LOCKED |
219 QMAN_FQ_FLAG_DYNAMIC_FQID;
221 ret = qman_create_fq(0, flags, fq);
223 DPAA_SEC_ERR("qman_create_fq failed");
227 memset(&opts, 0, sizeof(opts));
228 opts.we_mask = QM_INITFQ_WE_DESTWQ | QM_INITFQ_WE_FQCTRL |
229 QM_INITFQ_WE_CONTEXTA | QM_INITFQ_WE_CONTEXTB;
231 /* opts.fqd.dest.channel = dpaa_sec_pool_chan; */
233 fq->cb.dqrr = dqrr_out_fq_cb_rx;
234 fq->cb.ern = ern_sec_fq_handler;
236 ret = qman_init_fq(fq, 0, &opts);
238 DPAA_SEC_ERR("unable to init caam source fq!");
245 static inline int is_cipher_only(dpaa_sec_session *ses)
247 return ((ses->cipher_alg != RTE_CRYPTO_CIPHER_NULL) &&
248 (ses->auth_alg == RTE_CRYPTO_AUTH_NULL));
251 static inline int is_auth_only(dpaa_sec_session *ses)
253 return ((ses->cipher_alg == RTE_CRYPTO_CIPHER_NULL) &&
254 (ses->auth_alg != RTE_CRYPTO_AUTH_NULL));
257 static inline int is_aead(dpaa_sec_session *ses)
259 return ((ses->cipher_alg == 0) &&
260 (ses->auth_alg == 0) &&
261 (ses->aead_alg != 0));
264 static inline int is_auth_cipher(dpaa_sec_session *ses)
266 return ((ses->cipher_alg != RTE_CRYPTO_CIPHER_NULL) &&
267 (ses->auth_alg != RTE_CRYPTO_AUTH_NULL) &&
268 (ses->proto_alg != RTE_SECURITY_PROTOCOL_PDCP) &&
269 (ses->proto_alg != RTE_SECURITY_PROTOCOL_IPSEC));
272 static inline int is_proto_ipsec(dpaa_sec_session *ses)
274 return (ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC);
277 static inline int is_proto_pdcp(dpaa_sec_session *ses)
279 return (ses->proto_alg == RTE_SECURITY_PROTOCOL_PDCP);
282 static inline int is_encode(dpaa_sec_session *ses)
284 return ses->dir == DIR_ENC;
287 static inline int is_decode(dpaa_sec_session *ses)
289 return ses->dir == DIR_DEC;
293 caam_auth_alg(dpaa_sec_session *ses, struct alginfo *alginfo_a)
295 switch (ses->auth_alg) {
296 case RTE_CRYPTO_AUTH_NULL:
298 (ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
299 OP_PCL_IPSEC_HMAC_NULL : 0;
300 ses->digest_length = 0;
302 case RTE_CRYPTO_AUTH_MD5_HMAC:
304 (ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
305 OP_PCL_IPSEC_HMAC_MD5_96 : OP_ALG_ALGSEL_MD5;
306 alginfo_a->algmode = OP_ALG_AAI_HMAC;
308 case RTE_CRYPTO_AUTH_SHA1_HMAC:
310 (ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
311 OP_PCL_IPSEC_HMAC_SHA1_96 : OP_ALG_ALGSEL_SHA1;
312 alginfo_a->algmode = OP_ALG_AAI_HMAC;
314 case RTE_CRYPTO_AUTH_SHA224_HMAC:
316 (ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
317 OP_PCL_IPSEC_HMAC_SHA1_160 : OP_ALG_ALGSEL_SHA224;
318 alginfo_a->algmode = OP_ALG_AAI_HMAC;
320 case RTE_CRYPTO_AUTH_SHA256_HMAC:
322 (ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
323 OP_PCL_IPSEC_HMAC_SHA2_256_128 : OP_ALG_ALGSEL_SHA256;
324 alginfo_a->algmode = OP_ALG_AAI_HMAC;
326 case RTE_CRYPTO_AUTH_SHA384_HMAC:
328 (ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
329 OP_PCL_IPSEC_HMAC_SHA2_384_192 : OP_ALG_ALGSEL_SHA384;
330 alginfo_a->algmode = OP_ALG_AAI_HMAC;
332 case RTE_CRYPTO_AUTH_SHA512_HMAC:
334 (ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
335 OP_PCL_IPSEC_HMAC_SHA2_512_256 : OP_ALG_ALGSEL_SHA512;
336 alginfo_a->algmode = OP_ALG_AAI_HMAC;
339 DPAA_SEC_ERR("unsupported auth alg %u", ses->auth_alg);
344 caam_cipher_alg(dpaa_sec_session *ses, struct alginfo *alginfo_c)
346 switch (ses->cipher_alg) {
347 case RTE_CRYPTO_CIPHER_NULL:
349 (ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
350 OP_PCL_IPSEC_NULL : 0;
352 case RTE_CRYPTO_CIPHER_AES_CBC:
354 (ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
355 OP_PCL_IPSEC_AES_CBC : OP_ALG_ALGSEL_AES;
356 alginfo_c->algmode = OP_ALG_AAI_CBC;
358 case RTE_CRYPTO_CIPHER_3DES_CBC:
360 (ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
361 OP_PCL_IPSEC_3DES : OP_ALG_ALGSEL_3DES;
362 alginfo_c->algmode = OP_ALG_AAI_CBC;
364 case RTE_CRYPTO_CIPHER_AES_CTR:
366 (ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
367 OP_PCL_IPSEC_AES_CTR : OP_ALG_ALGSEL_AES;
368 alginfo_c->algmode = OP_ALG_AAI_CTR;
371 DPAA_SEC_ERR("unsupported cipher alg %d", ses->cipher_alg);
376 caam_aead_alg(dpaa_sec_session *ses, struct alginfo *alginfo)
378 switch (ses->aead_alg) {
379 case RTE_CRYPTO_AEAD_AES_GCM:
380 alginfo->algtype = OP_ALG_ALGSEL_AES;
381 alginfo->algmode = OP_ALG_AAI_GCM;
384 DPAA_SEC_ERR("unsupported AEAD alg %d", ses->aead_alg);
389 dpaa_sec_prep_pdcp_cdb(dpaa_sec_session *ses)
391 struct alginfo authdata = {0}, cipherdata = {0};
392 struct sec_cdb *cdb = &ses->cdb;
393 struct alginfo *p_authdata = NULL;
394 int32_t shared_desc_len = 0;
396 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
402 switch (ses->cipher_alg) {
403 case RTE_CRYPTO_CIPHER_SNOW3G_UEA2:
404 cipherdata.algtype = PDCP_CIPHER_TYPE_SNOW;
406 case RTE_CRYPTO_CIPHER_ZUC_EEA3:
407 cipherdata.algtype = PDCP_CIPHER_TYPE_ZUC;
409 case RTE_CRYPTO_CIPHER_AES_CTR:
410 cipherdata.algtype = PDCP_CIPHER_TYPE_AES;
412 case RTE_CRYPTO_CIPHER_NULL:
413 cipherdata.algtype = PDCP_CIPHER_TYPE_NULL;
416 DPAA_SEC_ERR("Crypto: Undefined Cipher specified %u",
421 cipherdata.key = (size_t)ses->cipher_key.data;
422 cipherdata.keylen = ses->cipher_key.length;
423 cipherdata.key_enc_flags = 0;
424 cipherdata.key_type = RTA_DATA_IMM;
426 cdb->sh_desc[0] = cipherdata.keylen;
431 switch (ses->auth_alg) {
432 case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
433 authdata.algtype = PDCP_AUTH_TYPE_SNOW;
435 case RTE_CRYPTO_AUTH_ZUC_EIA3:
436 authdata.algtype = PDCP_AUTH_TYPE_ZUC;
438 case RTE_CRYPTO_AUTH_AES_CMAC:
439 authdata.algtype = PDCP_AUTH_TYPE_AES;
441 case RTE_CRYPTO_AUTH_NULL:
442 authdata.algtype = PDCP_AUTH_TYPE_NULL;
445 DPAA_SEC_ERR("Crypto: Unsupported auth alg %u",
450 authdata.key = (size_t)ses->auth_key.data;
451 authdata.keylen = ses->auth_key.length;
452 authdata.key_enc_flags = 0;
453 authdata.key_type = RTA_DATA_IMM;
455 p_authdata = &authdata;
457 cdb->sh_desc[1] = authdata.keylen;
460 err = rta_inline_query(IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN,
462 (unsigned int *)cdb->sh_desc,
463 &cdb->sh_desc[2], 2);
465 DPAA_SEC_ERR("Crypto: Incorrect key lengths");
469 if (!(cdb->sh_desc[2] & 1) && cipherdata.keylen) {
471 (size_t)dpaa_mem_vtop((void *)(size_t)cipherdata.key);
472 cipherdata.key_type = RTA_DATA_PTR;
474 if (!(cdb->sh_desc[2] & (1 << 1)) && authdata.keylen) {
476 (size_t)dpaa_mem_vtop((void *)(size_t)authdata.key);
477 authdata.key_type = RTA_DATA_PTR;
484 if (ses->pdcp.domain == RTE_SECURITY_PDCP_MODE_CONTROL) {
485 if (ses->dir == DIR_ENC)
486 shared_desc_len = cnstr_shdsc_pdcp_c_plane_encap(
487 cdb->sh_desc, 1, swap,
492 ses->pdcp.hfn_threshold,
493 &cipherdata, &authdata,
495 else if (ses->dir == DIR_DEC)
496 shared_desc_len = cnstr_shdsc_pdcp_c_plane_decap(
497 cdb->sh_desc, 1, swap,
502 ses->pdcp.hfn_threshold,
503 &cipherdata, &authdata,
506 if (ses->dir == DIR_ENC)
507 shared_desc_len = cnstr_shdsc_pdcp_u_plane_encap(
508 cdb->sh_desc, 1, swap,
513 ses->pdcp.hfn_threshold,
514 &cipherdata, p_authdata, 0);
515 else if (ses->dir == DIR_DEC)
516 shared_desc_len = cnstr_shdsc_pdcp_u_plane_decap(
517 cdb->sh_desc, 1, swap,
522 ses->pdcp.hfn_threshold,
523 &cipherdata, p_authdata, 0);
526 return shared_desc_len;
529 /* prepare ipsec proto command block of the session */
531 dpaa_sec_prep_ipsec_cdb(dpaa_sec_session *ses)
533 struct alginfo cipherdata = {0}, authdata = {0};
534 struct sec_cdb *cdb = &ses->cdb;
535 int32_t shared_desc_len = 0;
537 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
543 caam_cipher_alg(ses, &cipherdata);
544 if (cipherdata.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
545 DPAA_SEC_ERR("not supported cipher alg");
549 cipherdata.key = (size_t)ses->cipher_key.data;
550 cipherdata.keylen = ses->cipher_key.length;
551 cipherdata.key_enc_flags = 0;
552 cipherdata.key_type = RTA_DATA_IMM;
554 caam_auth_alg(ses, &authdata);
555 if (authdata.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
556 DPAA_SEC_ERR("not supported auth alg");
560 authdata.key = (size_t)ses->auth_key.data;
561 authdata.keylen = ses->auth_key.length;
562 authdata.key_enc_flags = 0;
563 authdata.key_type = RTA_DATA_IMM;
565 cdb->sh_desc[0] = cipherdata.keylen;
566 cdb->sh_desc[1] = authdata.keylen;
567 err = rta_inline_query(IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN,
569 (unsigned int *)cdb->sh_desc,
570 &cdb->sh_desc[2], 2);
573 DPAA_SEC_ERR("Crypto: Incorrect key lengths");
576 if (cdb->sh_desc[2] & 1)
577 cipherdata.key_type = RTA_DATA_IMM;
579 cipherdata.key = (size_t)dpaa_mem_vtop(
580 (void *)(size_t)cipherdata.key);
581 cipherdata.key_type = RTA_DATA_PTR;
583 if (cdb->sh_desc[2] & (1<<1))
584 authdata.key_type = RTA_DATA_IMM;
586 authdata.key = (size_t)dpaa_mem_vtop(
587 (void *)(size_t)authdata.key);
588 authdata.key_type = RTA_DATA_PTR;
594 if (ses->dir == DIR_ENC) {
595 shared_desc_len = cnstr_shdsc_ipsec_new_encap(
597 true, swap, SHR_SERIAL,
599 (uint8_t *)&ses->ip4_hdr,
600 &cipherdata, &authdata);
601 } else if (ses->dir == DIR_DEC) {
602 shared_desc_len = cnstr_shdsc_ipsec_new_decap(
604 true, swap, SHR_SERIAL,
606 &cipherdata, &authdata);
608 return shared_desc_len;
611 /* prepare command block of the session */
613 dpaa_sec_prep_cdb(dpaa_sec_session *ses)
615 struct alginfo alginfo_c = {0}, alginfo_a = {0}, alginfo = {0};
616 int32_t shared_desc_len = 0;
617 struct sec_cdb *cdb = &ses->cdb;
619 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
625 memset(cdb, 0, sizeof(struct sec_cdb));
627 if (is_proto_ipsec(ses)) {
628 shared_desc_len = dpaa_sec_prep_ipsec_cdb(ses);
629 } else if (is_proto_pdcp(ses)) {
630 shared_desc_len = dpaa_sec_prep_pdcp_cdb(ses);
631 } else if (is_cipher_only(ses)) {
632 caam_cipher_alg(ses, &alginfo_c);
633 if (alginfo_c.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
634 DPAA_SEC_ERR("not supported cipher alg");
638 alginfo_c.key = (size_t)ses->cipher_key.data;
639 alginfo_c.keylen = ses->cipher_key.length;
640 alginfo_c.key_enc_flags = 0;
641 alginfo_c.key_type = RTA_DATA_IMM;
643 shared_desc_len = cnstr_shdsc_blkcipher(
645 swap, SHR_NEVER, &alginfo_c,
649 } else if (is_auth_only(ses)) {
650 caam_auth_alg(ses, &alginfo_a);
651 if (alginfo_a.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
652 DPAA_SEC_ERR("not supported auth alg");
656 alginfo_a.key = (size_t)ses->auth_key.data;
657 alginfo_a.keylen = ses->auth_key.length;
658 alginfo_a.key_enc_flags = 0;
659 alginfo_a.key_type = RTA_DATA_IMM;
661 shared_desc_len = cnstr_shdsc_hmac(cdb->sh_desc, true,
662 swap, SHR_NEVER, &alginfo_a,
665 } else if (is_aead(ses)) {
666 caam_aead_alg(ses, &alginfo);
667 if (alginfo.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
668 DPAA_SEC_ERR("not supported aead alg");
671 alginfo.key = (size_t)ses->aead_key.data;
672 alginfo.keylen = ses->aead_key.length;
673 alginfo.key_enc_flags = 0;
674 alginfo.key_type = RTA_DATA_IMM;
676 if (ses->dir == DIR_ENC)
677 shared_desc_len = cnstr_shdsc_gcm_encap(
678 cdb->sh_desc, true, swap, SHR_NEVER,
683 shared_desc_len = cnstr_shdsc_gcm_decap(
684 cdb->sh_desc, true, swap, SHR_NEVER,
689 caam_cipher_alg(ses, &alginfo_c);
690 if (alginfo_c.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
691 DPAA_SEC_ERR("not supported cipher alg");
695 alginfo_c.key = (size_t)ses->cipher_key.data;
696 alginfo_c.keylen = ses->cipher_key.length;
697 alginfo_c.key_enc_flags = 0;
698 alginfo_c.key_type = RTA_DATA_IMM;
700 caam_auth_alg(ses, &alginfo_a);
701 if (alginfo_a.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
702 DPAA_SEC_ERR("not supported auth alg");
706 alginfo_a.key = (size_t)ses->auth_key.data;
707 alginfo_a.keylen = ses->auth_key.length;
708 alginfo_a.key_enc_flags = 0;
709 alginfo_a.key_type = RTA_DATA_IMM;
711 cdb->sh_desc[0] = alginfo_c.keylen;
712 cdb->sh_desc[1] = alginfo_a.keylen;
713 err = rta_inline_query(IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN,
715 (unsigned int *)cdb->sh_desc,
716 &cdb->sh_desc[2], 2);
719 DPAA_SEC_ERR("Crypto: Incorrect key lengths");
722 if (cdb->sh_desc[2] & 1)
723 alginfo_c.key_type = RTA_DATA_IMM;
725 alginfo_c.key = (size_t)dpaa_mem_vtop(
726 (void *)(size_t)alginfo_c.key);
727 alginfo_c.key_type = RTA_DATA_PTR;
729 if (cdb->sh_desc[2] & (1<<1))
730 alginfo_a.key_type = RTA_DATA_IMM;
732 alginfo_a.key = (size_t)dpaa_mem_vtop(
733 (void *)(size_t)alginfo_a.key);
734 alginfo_a.key_type = RTA_DATA_PTR;
739 /* Auth_only_len is set as 0 here and it will be
740 * overwritten in fd for each packet.
742 shared_desc_len = cnstr_shdsc_authenc(cdb->sh_desc,
743 true, swap, SHR_SERIAL, &alginfo_c, &alginfo_a,
745 ses->digest_length, ses->dir);
748 if (shared_desc_len < 0) {
749 DPAA_SEC_ERR("error in preparing command block");
750 return shared_desc_len;
753 cdb->sh_hdr.hi.field.idlen = shared_desc_len;
754 cdb->sh_hdr.hi.word = rte_cpu_to_be_32(cdb->sh_hdr.hi.word);
755 cdb->sh_hdr.lo.word = rte_cpu_to_be_32(cdb->sh_hdr.lo.word);
760 /* qp is lockless, should be accessed by only one thread */
762 dpaa_sec_deq(struct dpaa_sec_qp *qp, struct rte_crypto_op **ops, int nb_ops)
765 unsigned int pkts = 0;
766 int num_rx_bufs, ret;
767 struct qm_dqrr_entry *dq;
768 uint32_t vdqcr_flags = 0;
772 * Until request for four buffers, we provide exact number of buffers.
773 * Otherwise we do not set the QM_VDQCR_EXACT flag.
774 * Not setting QM_VDQCR_EXACT flag can provide two more buffers than
775 * requested, so we request two less in this case.
778 vdqcr_flags = QM_VDQCR_EXACT;
779 num_rx_bufs = nb_ops;
781 num_rx_bufs = nb_ops > DPAA_MAX_DEQUEUE_NUM_FRAMES ?
782 (DPAA_MAX_DEQUEUE_NUM_FRAMES - 2) : (nb_ops - 2);
784 ret = qman_set_vdq(fq, num_rx_bufs, vdqcr_flags);
789 const struct qm_fd *fd;
790 struct dpaa_sec_job *job;
791 struct dpaa_sec_op_ctx *ctx;
792 struct rte_crypto_op *op;
794 dq = qman_dequeue(fq);
799 /* sg is embedded in an op ctx,
800 * sg[0] is for output
803 job = dpaa_mem_ptov(qm_fd_addr_get64(fd));
805 ctx = container_of(job, struct dpaa_sec_op_ctx, job);
806 ctx->fd_status = fd->status;
808 if (op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
809 struct qm_sg_entry *sg_out;
811 struct rte_mbuf *mbuf = (op->sym->m_dst == NULL) ?
812 op->sym->m_src : op->sym->m_dst;
814 sg_out = &job->sg[0];
815 hw_sg_to_cpu(sg_out);
816 len = sg_out->length;
818 while (mbuf->next != NULL) {
819 len -= mbuf->data_len;
822 mbuf->data_len = len;
824 if (!ctx->fd_status) {
825 op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
827 DPAA_SEC_DP_WARN("SEC return err:0x%x", ctx->fd_status);
828 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
832 /* report op status to sym->op and then free the ctx memeory */
833 rte_mempool_put(ctx->ctx_pool, (void *)ctx);
835 qman_dqrr_consume(fq, dq);
836 } while (fq->flags & QMAN_FQ_STATE_VDQCR);
841 static inline struct dpaa_sec_job *
842 build_auth_only_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
844 struct rte_crypto_sym_op *sym = op->sym;
845 struct rte_mbuf *mbuf = sym->m_src;
846 struct dpaa_sec_job *cf;
847 struct dpaa_sec_op_ctx *ctx;
848 struct qm_sg_entry *sg, *out_sg, *in_sg;
849 phys_addr_t start_addr;
850 uint8_t *old_digest, extra_segs;
857 if (mbuf->nb_segs > MAX_SG_ENTRIES) {
858 DPAA_SEC_DP_ERR("Auth: Max sec segs supported is %d",
862 ctx = dpaa_sec_alloc_ctx(ses, mbuf->nb_segs + extra_segs);
868 old_digest = ctx->digest;
872 qm_sg_entry_set64(out_sg, sym->auth.digest.phys_addr);
873 out_sg->length = ses->digest_length;
874 cpu_to_hw_sg(out_sg);
878 /* need to extend the input to a compound frame */
879 in_sg->extension = 1;
881 in_sg->length = sym->auth.data.length;
882 qm_sg_entry_set64(in_sg, dpaa_mem_vtop(&cf->sg[2]));
886 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
887 sg->length = mbuf->data_len - sym->auth.data.offset;
888 sg->offset = sym->auth.data.offset;
890 /* Successive segs */
895 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
896 sg->length = mbuf->data_len;
900 if (is_decode(ses)) {
901 /* Digest verification case */
904 rte_memcpy(old_digest, sym->auth.digest.data,
906 start_addr = dpaa_mem_vtop(old_digest);
907 qm_sg_entry_set64(sg, start_addr);
908 sg->length = ses->digest_length;
909 in_sg->length += ses->digest_length;
911 /* Digest calculation case */
912 sg->length -= ses->digest_length;
923 * |<----data_len------->|
924 * |ip_header|ah_header|icv|payload|
929 static inline struct dpaa_sec_job *
930 build_auth_only(struct rte_crypto_op *op, dpaa_sec_session *ses)
932 struct rte_crypto_sym_op *sym = op->sym;
933 struct rte_mbuf *mbuf = sym->m_src;
934 struct dpaa_sec_job *cf;
935 struct dpaa_sec_op_ctx *ctx;
936 struct qm_sg_entry *sg;
937 rte_iova_t start_addr;
940 ctx = dpaa_sec_alloc_ctx(ses, 4);
946 old_digest = ctx->digest;
948 start_addr = rte_pktmbuf_iova(mbuf);
951 qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
952 sg->length = ses->digest_length;
957 if (is_decode(ses)) {
958 /* need to extend the input to a compound frame */
960 qm_sg_entry_set64(sg, dpaa_mem_vtop(&cf->sg[2]));
961 sg->length = sym->auth.data.length + ses->digest_length;
966 /* hash result or digest, save digest first */
967 rte_memcpy(old_digest, sym->auth.digest.data,
969 qm_sg_entry_set64(sg, start_addr + sym->auth.data.offset);
970 sg->length = sym->auth.data.length;
973 /* let's check digest by hw */
974 start_addr = dpaa_mem_vtop(old_digest);
976 qm_sg_entry_set64(sg, start_addr);
977 sg->length = ses->digest_length;
981 qm_sg_entry_set64(sg, start_addr + sym->auth.data.offset);
982 sg->length = sym->auth.data.length;
990 static inline struct dpaa_sec_job *
991 build_cipher_only_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
993 struct rte_crypto_sym_op *sym = op->sym;
994 struct dpaa_sec_job *cf;
995 struct dpaa_sec_op_ctx *ctx;
996 struct qm_sg_entry *sg, *out_sg, *in_sg;
997 struct rte_mbuf *mbuf;
999 uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1004 req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 3;
1007 req_segs = mbuf->nb_segs * 2 + 3;
1010 if (mbuf->nb_segs > MAX_SG_ENTRIES) {
1011 DPAA_SEC_DP_ERR("Cipher: Max sec segs supported is %d",
1016 ctx = dpaa_sec_alloc_ctx(ses, req_segs);
1024 out_sg = &cf->sg[0];
1025 out_sg->extension = 1;
1026 out_sg->length = sym->cipher.data.length;
1027 qm_sg_entry_set64(out_sg, dpaa_mem_vtop(&cf->sg[2]));
1028 cpu_to_hw_sg(out_sg);
1032 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1033 sg->length = mbuf->data_len - sym->cipher.data.offset;
1034 sg->offset = sym->cipher.data.offset;
1036 /* Successive segs */
1041 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1042 sg->length = mbuf->data_len;
1051 in_sg->extension = 1;
1053 in_sg->length = sym->cipher.data.length + ses->iv.length;
1056 qm_sg_entry_set64(in_sg, dpaa_mem_vtop(sg));
1057 cpu_to_hw_sg(in_sg);
1060 qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1061 sg->length = ses->iv.length;
1066 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1067 sg->length = mbuf->data_len - sym->cipher.data.offset;
1068 sg->offset = sym->cipher.data.offset;
1070 /* Successive segs */
1075 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1076 sg->length = mbuf->data_len;
1085 static inline struct dpaa_sec_job *
1086 build_cipher_only(struct rte_crypto_op *op, dpaa_sec_session *ses)
1088 struct rte_crypto_sym_op *sym = op->sym;
1089 struct dpaa_sec_job *cf;
1090 struct dpaa_sec_op_ctx *ctx;
1091 struct qm_sg_entry *sg;
1092 rte_iova_t src_start_addr, dst_start_addr;
1093 uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1096 ctx = dpaa_sec_alloc_ctx(ses, 4);
1103 src_start_addr = rte_pktmbuf_iova(sym->m_src);
1106 dst_start_addr = rte_pktmbuf_iova(sym->m_dst);
1108 dst_start_addr = src_start_addr;
1112 qm_sg_entry_set64(sg, dst_start_addr + sym->cipher.data.offset);
1113 sg->length = sym->cipher.data.length + ses->iv.length;
1119 /* need to extend the input to a compound frame */
1122 sg->length = sym->cipher.data.length + ses->iv.length;
1123 qm_sg_entry_set64(sg, dpaa_mem_vtop(&cf->sg[2]));
1127 qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1128 sg->length = ses->iv.length;
1132 qm_sg_entry_set64(sg, src_start_addr + sym->cipher.data.offset);
1133 sg->length = sym->cipher.data.length;
1140 static inline struct dpaa_sec_job *
1141 build_cipher_auth_gcm_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
1143 struct rte_crypto_sym_op *sym = op->sym;
1144 struct dpaa_sec_job *cf;
1145 struct dpaa_sec_op_ctx *ctx;
1146 struct qm_sg_entry *sg, *out_sg, *in_sg;
1147 struct rte_mbuf *mbuf;
1149 uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1154 req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 4;
1157 req_segs = mbuf->nb_segs * 2 + 4;
1160 if (ses->auth_only_len)
1163 if (mbuf->nb_segs > MAX_SG_ENTRIES) {
1164 DPAA_SEC_DP_ERR("AEAD: Max sec segs supported is %d",
1169 ctx = dpaa_sec_alloc_ctx(ses, req_segs);
1176 rte_prefetch0(cf->sg);
1179 out_sg = &cf->sg[0];
1180 out_sg->extension = 1;
1182 out_sg->length = sym->aead.data.length + ses->auth_only_len
1183 + ses->digest_length;
1185 out_sg->length = sym->aead.data.length + ses->auth_only_len;
1187 /* output sg entries */
1189 qm_sg_entry_set64(out_sg, dpaa_mem_vtop(sg));
1190 cpu_to_hw_sg(out_sg);
1193 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1194 sg->length = mbuf->data_len - sym->aead.data.offset +
1196 sg->offset = sym->aead.data.offset - ses->auth_only_len;
1198 /* Successive segs */
1203 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1204 sg->length = mbuf->data_len;
1207 sg->length -= ses->digest_length;
1209 if (is_encode(ses)) {
1211 /* set auth output */
1213 qm_sg_entry_set64(sg, sym->aead.digest.phys_addr);
1214 sg->length = ses->digest_length;
1222 in_sg->extension = 1;
1225 in_sg->length = ses->iv.length + sym->aead.data.length
1226 + ses->auth_only_len;
1228 in_sg->length = ses->iv.length + sym->aead.data.length
1229 + ses->auth_only_len + ses->digest_length;
1231 /* input sg entries */
1233 qm_sg_entry_set64(in_sg, dpaa_mem_vtop(sg));
1234 cpu_to_hw_sg(in_sg);
1237 qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1238 sg->length = ses->iv.length;
1241 /* 2nd seg auth only */
1242 if (ses->auth_only_len) {
1244 qm_sg_entry_set64(sg, dpaa_mem_vtop(sym->aead.aad.data));
1245 sg->length = ses->auth_only_len;
1251 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1252 sg->length = mbuf->data_len - sym->aead.data.offset;
1253 sg->offset = sym->aead.data.offset;
1255 /* Successive segs */
1260 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1261 sg->length = mbuf->data_len;
1265 if (is_decode(ses)) {
1268 memcpy(ctx->digest, sym->aead.digest.data,
1269 ses->digest_length);
1270 qm_sg_entry_set64(sg, dpaa_mem_vtop(ctx->digest));
1271 sg->length = ses->digest_length;
1279 static inline struct dpaa_sec_job *
1280 build_cipher_auth_gcm(struct rte_crypto_op *op, dpaa_sec_session *ses)
1282 struct rte_crypto_sym_op *sym = op->sym;
1283 struct dpaa_sec_job *cf;
1284 struct dpaa_sec_op_ctx *ctx;
1285 struct qm_sg_entry *sg;
1286 uint32_t length = 0;
1287 rte_iova_t src_start_addr, dst_start_addr;
1288 uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1291 src_start_addr = sym->m_src->buf_iova + sym->m_src->data_off;
1294 dst_start_addr = sym->m_dst->buf_iova + sym->m_dst->data_off;
1296 dst_start_addr = src_start_addr;
1298 ctx = dpaa_sec_alloc_ctx(ses, 7);
1306 rte_prefetch0(cf->sg);
1308 qm_sg_entry_set64(&cf->sg[1], dpaa_mem_vtop(sg));
1309 if (is_encode(ses)) {
1310 qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1311 sg->length = ses->iv.length;
1312 length += sg->length;
1316 if (ses->auth_only_len) {
1317 qm_sg_entry_set64(sg,
1318 dpaa_mem_vtop(sym->aead.aad.data));
1319 sg->length = ses->auth_only_len;
1320 length += sg->length;
1324 qm_sg_entry_set64(sg, src_start_addr + sym->aead.data.offset);
1325 sg->length = sym->aead.data.length;
1326 length += sg->length;
1330 qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1331 sg->length = ses->iv.length;
1332 length += sg->length;
1336 if (ses->auth_only_len) {
1337 qm_sg_entry_set64(sg,
1338 dpaa_mem_vtop(sym->aead.aad.data));
1339 sg->length = ses->auth_only_len;
1340 length += sg->length;
1344 qm_sg_entry_set64(sg, src_start_addr + sym->aead.data.offset);
1345 sg->length = sym->aead.data.length;
1346 length += sg->length;
1349 memcpy(ctx->digest, sym->aead.digest.data,
1350 ses->digest_length);
1353 qm_sg_entry_set64(sg, dpaa_mem_vtop(ctx->digest));
1354 sg->length = ses->digest_length;
1355 length += sg->length;
1359 /* input compound frame */
1360 cf->sg[1].length = length;
1361 cf->sg[1].extension = 1;
1362 cf->sg[1].final = 1;
1363 cpu_to_hw_sg(&cf->sg[1]);
1367 qm_sg_entry_set64(&cf->sg[0], dpaa_mem_vtop(sg));
1368 qm_sg_entry_set64(sg,
1369 dst_start_addr + sym->aead.data.offset - ses->auth_only_len);
1370 sg->length = sym->aead.data.length + ses->auth_only_len;
1371 length = sg->length;
1372 if (is_encode(ses)) {
1374 /* set auth output */
1376 qm_sg_entry_set64(sg, sym->aead.digest.phys_addr);
1377 sg->length = ses->digest_length;
1378 length += sg->length;
1383 /* output compound frame */
1384 cf->sg[0].length = length;
1385 cf->sg[0].extension = 1;
1386 cpu_to_hw_sg(&cf->sg[0]);
1391 static inline struct dpaa_sec_job *
1392 build_cipher_auth_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
1394 struct rte_crypto_sym_op *sym = op->sym;
1395 struct dpaa_sec_job *cf;
1396 struct dpaa_sec_op_ctx *ctx;
1397 struct qm_sg_entry *sg, *out_sg, *in_sg;
1398 struct rte_mbuf *mbuf;
1400 uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1405 req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 4;
1408 req_segs = mbuf->nb_segs * 2 + 4;
1411 if (mbuf->nb_segs > MAX_SG_ENTRIES) {
1412 DPAA_SEC_DP_ERR("Cipher-Auth: Max sec segs supported is %d",
1417 ctx = dpaa_sec_alloc_ctx(ses, req_segs);
1424 rte_prefetch0(cf->sg);
1427 out_sg = &cf->sg[0];
1428 out_sg->extension = 1;
1430 out_sg->length = sym->auth.data.length + ses->digest_length;
1432 out_sg->length = sym->auth.data.length;
1434 /* output sg entries */
1436 qm_sg_entry_set64(out_sg, dpaa_mem_vtop(sg));
1437 cpu_to_hw_sg(out_sg);
1440 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1441 sg->length = mbuf->data_len - sym->auth.data.offset;
1442 sg->offset = sym->auth.data.offset;
1444 /* Successive segs */
1449 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1450 sg->length = mbuf->data_len;
1453 sg->length -= ses->digest_length;
1455 if (is_encode(ses)) {
1457 /* set auth output */
1459 qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
1460 sg->length = ses->digest_length;
1468 in_sg->extension = 1;
1471 in_sg->length = ses->iv.length + sym->auth.data.length;
1473 in_sg->length = ses->iv.length + sym->auth.data.length
1474 + ses->digest_length;
1476 /* input sg entries */
1478 qm_sg_entry_set64(in_sg, dpaa_mem_vtop(sg));
1479 cpu_to_hw_sg(in_sg);
1482 qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1483 sg->length = ses->iv.length;
1488 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1489 sg->length = mbuf->data_len - sym->auth.data.offset;
1490 sg->offset = sym->auth.data.offset;
1492 /* Successive segs */
1497 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1498 sg->length = mbuf->data_len;
1502 sg->length -= ses->digest_length;
1503 if (is_decode(ses)) {
1506 memcpy(ctx->digest, sym->auth.digest.data,
1507 ses->digest_length);
1508 qm_sg_entry_set64(sg, dpaa_mem_vtop(ctx->digest));
1509 sg->length = ses->digest_length;
1517 static inline struct dpaa_sec_job *
1518 build_cipher_auth(struct rte_crypto_op *op, dpaa_sec_session *ses)
1520 struct rte_crypto_sym_op *sym = op->sym;
1521 struct dpaa_sec_job *cf;
1522 struct dpaa_sec_op_ctx *ctx;
1523 struct qm_sg_entry *sg;
1524 rte_iova_t src_start_addr, dst_start_addr;
1525 uint32_t length = 0;
1526 uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1529 src_start_addr = sym->m_src->buf_iova + sym->m_src->data_off;
1531 dst_start_addr = sym->m_dst->buf_iova + sym->m_dst->data_off;
1533 dst_start_addr = src_start_addr;
1535 ctx = dpaa_sec_alloc_ctx(ses, 7);
1543 rte_prefetch0(cf->sg);
1545 qm_sg_entry_set64(&cf->sg[1], dpaa_mem_vtop(sg));
1546 if (is_encode(ses)) {
1547 qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1548 sg->length = ses->iv.length;
1549 length += sg->length;
1553 qm_sg_entry_set64(sg, src_start_addr + sym->auth.data.offset);
1554 sg->length = sym->auth.data.length;
1555 length += sg->length;
1559 qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1560 sg->length = ses->iv.length;
1561 length += sg->length;
1566 qm_sg_entry_set64(sg, src_start_addr + sym->auth.data.offset);
1567 sg->length = sym->auth.data.length;
1568 length += sg->length;
1571 memcpy(ctx->digest, sym->auth.digest.data,
1572 ses->digest_length);
1575 qm_sg_entry_set64(sg, dpaa_mem_vtop(ctx->digest));
1576 sg->length = ses->digest_length;
1577 length += sg->length;
1581 /* input compound frame */
1582 cf->sg[1].length = length;
1583 cf->sg[1].extension = 1;
1584 cf->sg[1].final = 1;
1585 cpu_to_hw_sg(&cf->sg[1]);
1589 qm_sg_entry_set64(&cf->sg[0], dpaa_mem_vtop(sg));
1590 qm_sg_entry_set64(sg, dst_start_addr + sym->cipher.data.offset);
1591 sg->length = sym->cipher.data.length;
1592 length = sg->length;
1593 if (is_encode(ses)) {
1595 /* set auth output */
1597 qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
1598 sg->length = ses->digest_length;
1599 length += sg->length;
1604 /* output compound frame */
1605 cf->sg[0].length = length;
1606 cf->sg[0].extension = 1;
1607 cpu_to_hw_sg(&cf->sg[0]);
1612 static inline struct dpaa_sec_job *
1613 build_proto(struct rte_crypto_op *op, dpaa_sec_session *ses)
1615 struct rte_crypto_sym_op *sym = op->sym;
1616 struct dpaa_sec_job *cf;
1617 struct dpaa_sec_op_ctx *ctx;
1618 struct qm_sg_entry *sg;
1619 phys_addr_t src_start_addr, dst_start_addr;
1621 ctx = dpaa_sec_alloc_ctx(ses, 2);
1627 src_start_addr = rte_pktmbuf_mtophys(sym->m_src);
1630 dst_start_addr = rte_pktmbuf_mtophys(sym->m_dst);
1632 dst_start_addr = src_start_addr;
1636 qm_sg_entry_set64(sg, src_start_addr);
1637 sg->length = sym->m_src->pkt_len;
1641 sym->m_src->packet_type &= ~RTE_PTYPE_L4_MASK;
1644 qm_sg_entry_set64(sg, dst_start_addr);
1645 sg->length = sym->m_src->buf_len - sym->m_src->data_off;
1651 static inline struct dpaa_sec_job *
1652 build_proto_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
1654 struct rte_crypto_sym_op *sym = op->sym;
1655 struct dpaa_sec_job *cf;
1656 struct dpaa_sec_op_ctx *ctx;
1657 struct qm_sg_entry *sg, *out_sg, *in_sg;
1658 struct rte_mbuf *mbuf;
1660 uint32_t in_len = 0, out_len = 0;
1667 req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 2;
1668 if (mbuf->nb_segs > MAX_SG_ENTRIES) {
1669 DPAA_SEC_DP_ERR("Proto: Max sec segs supported is %d",
1674 ctx = dpaa_sec_alloc_ctx(ses, req_segs);
1680 out_sg = &cf->sg[0];
1681 out_sg->extension = 1;
1682 qm_sg_entry_set64(out_sg, dpaa_mem_vtop(&cf->sg[2]));
1686 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1689 /* Successive segs */
1690 while (mbuf->next) {
1691 sg->length = mbuf->data_len;
1692 out_len += sg->length;
1696 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1699 sg->length = mbuf->buf_len - mbuf->data_off;
1700 out_len += sg->length;
1704 out_sg->length = out_len;
1705 cpu_to_hw_sg(out_sg);
1710 in_sg->extension = 1;
1712 in_len = mbuf->data_len;
1715 qm_sg_entry_set64(in_sg, dpaa_mem_vtop(sg));
1718 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1719 sg->length = mbuf->data_len;
1722 /* Successive segs */
1727 qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1728 sg->length = mbuf->data_len;
1730 in_len += sg->length;
1736 in_sg->length = in_len;
1737 cpu_to_hw_sg(in_sg);
1739 sym->m_src->packet_type &= ~RTE_PTYPE_L4_MASK;
1745 dpaa_sec_enqueue_burst(void *qp, struct rte_crypto_op **ops,
1748 /* Function to transmit the frames to given device and queuepair */
1750 struct dpaa_sec_qp *dpaa_qp = (struct dpaa_sec_qp *)qp;
1751 uint16_t num_tx = 0;
1752 struct qm_fd fds[DPAA_SEC_BURST], *fd;
1753 uint32_t frames_to_send;
1754 struct rte_crypto_op *op;
1755 struct dpaa_sec_job *cf;
1756 dpaa_sec_session *ses;
1757 uint32_t auth_only_len;
1758 struct qman_fq *inq[DPAA_SEC_BURST];
1761 frames_to_send = (nb_ops > DPAA_SEC_BURST) ?
1762 DPAA_SEC_BURST : nb_ops;
1763 for (loop = 0; loop < frames_to_send; loop++) {
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 case RTE_CRYPTO_OP_SECURITY_SESSION:
1773 ses = (dpaa_sec_session *)
1774 get_sec_session_private_data(
1775 op->sym->sec_session);
1779 "sessionless crypto op not supported");
1780 frames_to_send = loop;
1784 if (unlikely(!ses->qp[rte_lcore_id() % MAX_DPAA_CORES])) {
1785 if (dpaa_sec_attach_sess_q(qp, ses)) {
1786 frames_to_send = loop;
1790 } else if (unlikely(ses->qp[rte_lcore_id() %
1791 MAX_DPAA_CORES] != qp)) {
1792 DPAA_SEC_DP_ERR("Old:sess->qp = %p"
1794 ses->qp[rte_lcore_id() %
1795 MAX_DPAA_CORES], qp);
1796 frames_to_send = loop;
1801 auth_only_len = op->sym->auth.data.length -
1802 op->sym->cipher.data.length;
1803 if (rte_pktmbuf_is_contiguous(op->sym->m_src) &&
1804 ((op->sym->m_dst == NULL) ||
1805 rte_pktmbuf_is_contiguous(op->sym->m_dst))) {
1806 if (is_proto_ipsec(ses)) {
1807 cf = build_proto(op, ses);
1808 } else if (is_proto_pdcp(ses)) {
1809 cf = build_proto(op, ses);
1810 } else if (is_auth_only(ses)) {
1811 cf = build_auth_only(op, ses);
1812 } else if (is_cipher_only(ses)) {
1813 cf = build_cipher_only(op, ses);
1814 } else if (is_aead(ses)) {
1815 cf = build_cipher_auth_gcm(op, ses);
1816 auth_only_len = ses->auth_only_len;
1817 } else if (is_auth_cipher(ses)) {
1818 cf = build_cipher_auth(op, ses);
1820 DPAA_SEC_DP_ERR("not supported ops");
1821 frames_to_send = loop;
1826 if (is_proto_pdcp(ses) || is_proto_ipsec(ses)) {
1827 cf = build_proto_sg(op, ses);
1828 } else if (is_auth_only(ses)) {
1829 cf = build_auth_only_sg(op, ses);
1830 } else if (is_cipher_only(ses)) {
1831 cf = build_cipher_only_sg(op, ses);
1832 } else if (is_aead(ses)) {
1833 cf = build_cipher_auth_gcm_sg(op, ses);
1834 auth_only_len = ses->auth_only_len;
1835 } else if (is_auth_cipher(ses)) {
1836 cf = build_cipher_auth_sg(op, ses);
1838 DPAA_SEC_DP_ERR("not supported ops");
1839 frames_to_send = loop;
1844 if (unlikely(!cf)) {
1845 frames_to_send = loop;
1851 inq[loop] = ses->inq[rte_lcore_id() % MAX_DPAA_CORES];
1852 fd->opaque_addr = 0;
1854 qm_fd_addr_set64(fd, dpaa_mem_vtop(cf->sg));
1855 fd->_format1 = qm_fd_compound;
1856 fd->length29 = 2 * sizeof(struct qm_sg_entry);
1857 /* Auth_only_len is set as 0 in descriptor and it is
1858 * overwritten here in the fd.cmd which will update
1862 fd->cmd = 0x80000000 | auth_only_len;
1864 /* In case of PDCP, per packet HFN is stored in
1865 * mbuf priv after sym_op.
1867 if (is_proto_pdcp(ses) && ses->pdcp.hfn_ovd) {
1868 fd->cmd = 0x80000000 |
1869 *((uint32_t *)((uint8_t *)op +
1870 ses->pdcp.hfn_ovd_offset));
1871 DPAA_SEC_DP_DEBUG("Per packet HFN: %x, ovd:%u,%u\n",
1872 *((uint32_t *)((uint8_t *)op +
1873 ses->pdcp.hfn_ovd_offset)),
1875 is_proto_pdcp(ses));
1881 while (loop < frames_to_send) {
1882 loop += qman_enqueue_multi_fq(&inq[loop], &fds[loop],
1883 frames_to_send - loop);
1885 nb_ops -= frames_to_send;
1886 num_tx += frames_to_send;
1889 dpaa_qp->tx_pkts += num_tx;
1890 dpaa_qp->tx_errs += nb_ops - num_tx;
1896 dpaa_sec_dequeue_burst(void *qp, struct rte_crypto_op **ops,
1900 struct dpaa_sec_qp *dpaa_qp = (struct dpaa_sec_qp *)qp;
1902 num_rx = dpaa_sec_deq(dpaa_qp, ops, nb_ops);
1904 dpaa_qp->rx_pkts += num_rx;
1905 dpaa_qp->rx_errs += nb_ops - num_rx;
1907 DPAA_SEC_DP_DEBUG("SEC Received %d Packets\n", num_rx);
1912 /** Release queue pair */
1914 dpaa_sec_queue_pair_release(struct rte_cryptodev *dev,
1917 struct dpaa_sec_dev_private *internals;
1918 struct dpaa_sec_qp *qp = NULL;
1920 PMD_INIT_FUNC_TRACE();
1922 DPAA_SEC_DEBUG("dev =%p, queue =%d", dev, qp_id);
1924 internals = dev->data->dev_private;
1925 if (qp_id >= internals->max_nb_queue_pairs) {
1926 DPAA_SEC_ERR("Max supported qpid %d",
1927 internals->max_nb_queue_pairs);
1931 qp = &internals->qps[qp_id];
1932 qp->internals = NULL;
1933 dev->data->queue_pairs[qp_id] = NULL;
1938 /** Setup a queue pair */
1940 dpaa_sec_queue_pair_setup(struct rte_cryptodev *dev, uint16_t qp_id,
1941 __rte_unused const struct rte_cryptodev_qp_conf *qp_conf,
1942 __rte_unused int socket_id)
1944 struct dpaa_sec_dev_private *internals;
1945 struct dpaa_sec_qp *qp = NULL;
1947 DPAA_SEC_DEBUG("dev =%p, queue =%d, conf =%p", dev, qp_id, qp_conf);
1949 internals = dev->data->dev_private;
1950 if (qp_id >= internals->max_nb_queue_pairs) {
1951 DPAA_SEC_ERR("Max supported qpid %d",
1952 internals->max_nb_queue_pairs);
1956 qp = &internals->qps[qp_id];
1957 qp->internals = internals;
1958 dev->data->queue_pairs[qp_id] = qp;
1963 /** Return the number of allocated queue pairs */
1965 dpaa_sec_queue_pair_count(struct rte_cryptodev *dev)
1967 PMD_INIT_FUNC_TRACE();
1969 return dev->data->nb_queue_pairs;
1972 /** Returns the size of session structure */
1974 dpaa_sec_sym_session_get_size(struct rte_cryptodev *dev __rte_unused)
1976 PMD_INIT_FUNC_TRACE();
1978 return sizeof(dpaa_sec_session);
1982 dpaa_sec_cipher_init(struct rte_cryptodev *dev __rte_unused,
1983 struct rte_crypto_sym_xform *xform,
1984 dpaa_sec_session *session)
1986 session->cipher_alg = xform->cipher.algo;
1987 session->iv.length = xform->cipher.iv.length;
1988 session->iv.offset = xform->cipher.iv.offset;
1989 session->cipher_key.data = rte_zmalloc(NULL, xform->cipher.key.length,
1990 RTE_CACHE_LINE_SIZE);
1991 if (session->cipher_key.data == NULL && xform->cipher.key.length > 0) {
1992 DPAA_SEC_ERR("No Memory for cipher key");
1995 session->cipher_key.length = xform->cipher.key.length;
1997 memcpy(session->cipher_key.data, xform->cipher.key.data,
1998 xform->cipher.key.length);
1999 session->dir = (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
2006 dpaa_sec_auth_init(struct rte_cryptodev *dev __rte_unused,
2007 struct rte_crypto_sym_xform *xform,
2008 dpaa_sec_session *session)
2010 session->auth_alg = xform->auth.algo;
2011 session->auth_key.data = rte_zmalloc(NULL, xform->auth.key.length,
2012 RTE_CACHE_LINE_SIZE);
2013 if (session->auth_key.data == NULL && xform->auth.key.length > 0) {
2014 DPAA_SEC_ERR("No Memory for auth key");
2017 session->auth_key.length = xform->auth.key.length;
2018 session->digest_length = xform->auth.digest_length;
2020 memcpy(session->auth_key.data, xform->auth.key.data,
2021 xform->auth.key.length);
2022 session->dir = (xform->auth.op == RTE_CRYPTO_AUTH_OP_GENERATE) ?
2029 dpaa_sec_aead_init(struct rte_cryptodev *dev __rte_unused,
2030 struct rte_crypto_sym_xform *xform,
2031 dpaa_sec_session *session)
2033 session->aead_alg = xform->aead.algo;
2034 session->iv.length = xform->aead.iv.length;
2035 session->iv.offset = xform->aead.iv.offset;
2036 session->auth_only_len = xform->aead.aad_length;
2037 session->aead_key.data = rte_zmalloc(NULL, xform->aead.key.length,
2038 RTE_CACHE_LINE_SIZE);
2039 if (session->aead_key.data == NULL && xform->aead.key.length > 0) {
2040 DPAA_SEC_ERR("No Memory for aead key\n");
2043 session->aead_key.length = xform->aead.key.length;
2044 session->digest_length = xform->aead.digest_length;
2046 memcpy(session->aead_key.data, xform->aead.key.data,
2047 xform->aead.key.length);
2048 session->dir = (xform->aead.op == RTE_CRYPTO_AEAD_OP_ENCRYPT) ?
2054 static struct qman_fq *
2055 dpaa_sec_attach_rxq(struct dpaa_sec_dev_private *qi)
2059 for (i = 0; i < qi->max_nb_sessions * MAX_DPAA_CORES; i++) {
2060 if (qi->inq_attach[i] == 0) {
2061 qi->inq_attach[i] = 1;
2065 DPAA_SEC_WARN("All session in use %u", qi->max_nb_sessions);
2071 dpaa_sec_detach_rxq(struct dpaa_sec_dev_private *qi, struct qman_fq *fq)
2075 for (i = 0; i < qi->max_nb_sessions; i++) {
2076 if (&qi->inq[i] == fq) {
2077 qman_retire_fq(fq, NULL);
2079 qi->inq_attach[i] = 0;
2087 dpaa_sec_attach_sess_q(struct dpaa_sec_qp *qp, dpaa_sec_session *sess)
2091 sess->qp[rte_lcore_id() % MAX_DPAA_CORES] = qp;
2092 ret = dpaa_sec_prep_cdb(sess);
2094 DPAA_SEC_ERR("Unable to prepare sec cdb");
2097 if (unlikely(!RTE_PER_LCORE(dpaa_io))) {
2098 ret = rte_dpaa_portal_init((void *)0);
2100 DPAA_SEC_ERR("Failure in affining portal");
2104 ret = dpaa_sec_init_rx(sess->inq[rte_lcore_id() % MAX_DPAA_CORES],
2105 dpaa_mem_vtop(&sess->cdb),
2106 qman_fq_fqid(&qp->outq));
2108 DPAA_SEC_ERR("Unable to init sec queue");
2114 dpaa_sec_set_session_parameters(struct rte_cryptodev *dev,
2115 struct rte_crypto_sym_xform *xform, void *sess)
2117 struct dpaa_sec_dev_private *internals = dev->data->dev_private;
2118 dpaa_sec_session *session = sess;
2121 PMD_INIT_FUNC_TRACE();
2123 if (unlikely(sess == NULL)) {
2124 DPAA_SEC_ERR("invalid session struct");
2127 memset(session, 0, sizeof(dpaa_sec_session));
2129 /* Default IV length = 0 */
2130 session->iv.length = 0;
2133 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER && xform->next == NULL) {
2134 session->auth_alg = RTE_CRYPTO_AUTH_NULL;
2135 dpaa_sec_cipher_init(dev, xform, session);
2137 /* Authentication Only */
2138 } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
2139 xform->next == NULL) {
2140 session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
2141 dpaa_sec_auth_init(dev, xform, session);
2143 /* Cipher then Authenticate */
2144 } else if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
2145 xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
2146 if (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) {
2147 dpaa_sec_cipher_init(dev, xform, session);
2148 dpaa_sec_auth_init(dev, xform->next, session);
2150 DPAA_SEC_ERR("Not supported: Auth then Cipher");
2154 /* Authenticate then Cipher */
2155 } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
2156 xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
2157 if (xform->next->cipher.op == RTE_CRYPTO_CIPHER_OP_DECRYPT) {
2158 dpaa_sec_auth_init(dev, xform, session);
2159 dpaa_sec_cipher_init(dev, xform->next, session);
2161 DPAA_SEC_ERR("Not supported: Auth then Cipher");
2165 /* AEAD operation for AES-GCM kind of Algorithms */
2166 } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD &&
2167 xform->next == NULL) {
2168 dpaa_sec_aead_init(dev, xform, session);
2171 DPAA_SEC_ERR("Invalid crypto type");
2174 session->ctx_pool = internals->ctx_pool;
2175 rte_spinlock_lock(&internals->lock);
2176 for (i = 0; i < MAX_DPAA_CORES; i++) {
2177 session->inq[i] = dpaa_sec_attach_rxq(internals);
2178 if (session->inq[i] == NULL) {
2179 DPAA_SEC_ERR("unable to attach sec queue");
2180 rte_spinlock_unlock(&internals->lock);
2184 rte_spinlock_unlock(&internals->lock);
2189 rte_free(session->cipher_key.data);
2190 rte_free(session->auth_key.data);
2191 memset(session, 0, sizeof(dpaa_sec_session));
2197 dpaa_sec_sym_session_configure(struct rte_cryptodev *dev,
2198 struct rte_crypto_sym_xform *xform,
2199 struct rte_cryptodev_sym_session *sess,
2200 struct rte_mempool *mempool)
2202 void *sess_private_data;
2205 PMD_INIT_FUNC_TRACE();
2207 if (rte_mempool_get(mempool, &sess_private_data)) {
2208 DPAA_SEC_ERR("Couldn't get object from session mempool");
2212 ret = dpaa_sec_set_session_parameters(dev, xform, sess_private_data);
2214 DPAA_SEC_ERR("failed to configure session parameters");
2216 /* Return session to mempool */
2217 rte_mempool_put(mempool, sess_private_data);
2221 set_sym_session_private_data(sess, dev->driver_id,
2229 free_session_memory(struct rte_cryptodev *dev, dpaa_sec_session *s)
2231 struct dpaa_sec_dev_private *qi = dev->data->dev_private;
2232 struct rte_mempool *sess_mp = rte_mempool_from_obj((void *)s);
2235 for (i = 0; i < MAX_DPAA_CORES; i++) {
2237 dpaa_sec_detach_rxq(qi, s->inq[i]);
2241 rte_free(s->cipher_key.data);
2242 rte_free(s->auth_key.data);
2243 memset(s, 0, sizeof(dpaa_sec_session));
2244 rte_mempool_put(sess_mp, (void *)s);
2247 /** Clear the memory of session so it doesn't leave key material behind */
2249 dpaa_sec_sym_session_clear(struct rte_cryptodev *dev,
2250 struct rte_cryptodev_sym_session *sess)
2252 PMD_INIT_FUNC_TRACE();
2253 uint8_t index = dev->driver_id;
2254 void *sess_priv = get_sym_session_private_data(sess, index);
2255 dpaa_sec_session *s = (dpaa_sec_session *)sess_priv;
2258 free_session_memory(dev, s);
2259 set_sym_session_private_data(sess, index, NULL);
2264 dpaa_sec_set_ipsec_session(__rte_unused struct rte_cryptodev *dev,
2265 struct rte_security_session_conf *conf,
2268 struct dpaa_sec_dev_private *internals = dev->data->dev_private;
2269 struct rte_security_ipsec_xform *ipsec_xform = &conf->ipsec;
2270 struct rte_crypto_auth_xform *auth_xform = NULL;
2271 struct rte_crypto_cipher_xform *cipher_xform = NULL;
2272 dpaa_sec_session *session = (dpaa_sec_session *)sess;
2275 PMD_INIT_FUNC_TRACE();
2277 memset(session, 0, sizeof(dpaa_sec_session));
2278 if (ipsec_xform->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
2279 cipher_xform = &conf->crypto_xform->cipher;
2280 if (conf->crypto_xform->next)
2281 auth_xform = &conf->crypto_xform->next->auth;
2283 auth_xform = &conf->crypto_xform->auth;
2284 if (conf->crypto_xform->next)
2285 cipher_xform = &conf->crypto_xform->next->cipher;
2287 session->proto_alg = conf->protocol;
2289 if (cipher_xform && cipher_xform->algo != RTE_CRYPTO_CIPHER_NULL) {
2290 session->cipher_key.data = rte_zmalloc(NULL,
2291 cipher_xform->key.length,
2292 RTE_CACHE_LINE_SIZE);
2293 if (session->cipher_key.data == NULL &&
2294 cipher_xform->key.length > 0) {
2295 DPAA_SEC_ERR("No Memory for cipher key");
2298 memcpy(session->cipher_key.data, cipher_xform->key.data,
2299 cipher_xform->key.length);
2300 session->cipher_key.length = cipher_xform->key.length;
2302 switch (cipher_xform->algo) {
2303 case RTE_CRYPTO_CIPHER_AES_CBC:
2304 case RTE_CRYPTO_CIPHER_3DES_CBC:
2305 case RTE_CRYPTO_CIPHER_AES_CTR:
2308 DPAA_SEC_ERR("Crypto: Unsupported Cipher alg %u",
2309 cipher_xform->algo);
2312 session->cipher_alg = cipher_xform->algo;
2314 session->cipher_key.data = NULL;
2315 session->cipher_key.length = 0;
2316 session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
2319 if (auth_xform && auth_xform->algo != RTE_CRYPTO_AUTH_NULL) {
2320 session->auth_key.data = rte_zmalloc(NULL,
2321 auth_xform->key.length,
2322 RTE_CACHE_LINE_SIZE);
2323 if (session->auth_key.data == NULL &&
2324 auth_xform->key.length > 0) {
2325 DPAA_SEC_ERR("No Memory for auth key");
2326 rte_free(session->cipher_key.data);
2329 memcpy(session->auth_key.data, auth_xform->key.data,
2330 auth_xform->key.length);
2331 session->auth_key.length = auth_xform->key.length;
2333 switch (auth_xform->algo) {
2334 case RTE_CRYPTO_AUTH_SHA1_HMAC:
2335 case RTE_CRYPTO_AUTH_MD5_HMAC:
2336 case RTE_CRYPTO_AUTH_SHA256_HMAC:
2337 case RTE_CRYPTO_AUTH_SHA384_HMAC:
2338 case RTE_CRYPTO_AUTH_SHA512_HMAC:
2339 case RTE_CRYPTO_AUTH_AES_CMAC:
2342 DPAA_SEC_ERR("Crypto: Unsupported auth alg %u",
2346 session->auth_alg = auth_xform->algo;
2348 session->auth_key.data = NULL;
2349 session->auth_key.length = 0;
2350 session->auth_alg = RTE_CRYPTO_AUTH_NULL;
2353 if (ipsec_xform->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
2354 if (ipsec_xform->tunnel.type ==
2355 RTE_SECURITY_IPSEC_TUNNEL_IPV4) {
2356 memset(&session->encap_pdb, 0,
2357 sizeof(struct ipsec_encap_pdb) +
2358 sizeof(session->ip4_hdr));
2359 session->ip4_hdr.ip_v = IPVERSION;
2360 session->ip4_hdr.ip_hl = 5;
2361 session->ip4_hdr.ip_len = rte_cpu_to_be_16(
2362 sizeof(session->ip4_hdr));
2363 session->ip4_hdr.ip_tos = ipsec_xform->tunnel.ipv4.dscp;
2364 session->ip4_hdr.ip_id = 0;
2365 session->ip4_hdr.ip_off = 0;
2366 session->ip4_hdr.ip_ttl = ipsec_xform->tunnel.ipv4.ttl;
2367 session->ip4_hdr.ip_p = (ipsec_xform->proto ==
2368 RTE_SECURITY_IPSEC_SA_PROTO_ESP) ?
2369 IPPROTO_ESP : IPPROTO_AH;
2370 session->ip4_hdr.ip_sum = 0;
2371 session->ip4_hdr.ip_src =
2372 ipsec_xform->tunnel.ipv4.src_ip;
2373 session->ip4_hdr.ip_dst =
2374 ipsec_xform->tunnel.ipv4.dst_ip;
2375 session->ip4_hdr.ip_sum = calc_chksum((uint16_t *)
2376 (void *)&session->ip4_hdr,
2378 session->encap_pdb.ip_hdr_len = sizeof(struct ip);
2379 } else if (ipsec_xform->tunnel.type ==
2380 RTE_SECURITY_IPSEC_TUNNEL_IPV6) {
2381 memset(&session->encap_pdb, 0,
2382 sizeof(struct ipsec_encap_pdb) +
2383 sizeof(session->ip6_hdr));
2384 session->ip6_hdr.vtc_flow = rte_cpu_to_be_32(
2385 DPAA_IPv6_DEFAULT_VTC_FLOW |
2386 ((ipsec_xform->tunnel.ipv6.dscp <<
2387 RTE_IPV6_HDR_TC_SHIFT) &
2388 RTE_IPV6_HDR_TC_MASK) |
2389 ((ipsec_xform->tunnel.ipv6.flabel <<
2390 RTE_IPV6_HDR_FL_SHIFT) &
2391 RTE_IPV6_HDR_FL_MASK));
2392 /* Payload length will be updated by HW */
2393 session->ip6_hdr.payload_len = 0;
2394 session->ip6_hdr.hop_limits =
2395 ipsec_xform->tunnel.ipv6.hlimit;
2396 session->ip6_hdr.proto = (ipsec_xform->proto ==
2397 RTE_SECURITY_IPSEC_SA_PROTO_ESP) ?
2398 IPPROTO_ESP : IPPROTO_AH;
2399 memcpy(&session->ip6_hdr.src_addr,
2400 &ipsec_xform->tunnel.ipv6.src_addr, 16);
2401 memcpy(&session->ip6_hdr.dst_addr,
2402 &ipsec_xform->tunnel.ipv6.dst_addr, 16);
2403 session->encap_pdb.ip_hdr_len =
2404 sizeof(struct rte_ipv6_hdr);
2406 session->encap_pdb.options =
2407 (IPVERSION << PDBNH_ESP_ENCAP_SHIFT) |
2408 PDBOPTS_ESP_OIHI_PDB_INL |
2410 PDBHMO_ESP_ENCAP_DTTL |
2412 if (ipsec_xform->options.esn)
2413 session->encap_pdb.options |= PDBOPTS_ESP_ESN;
2414 session->encap_pdb.spi = ipsec_xform->spi;
2415 session->dir = DIR_ENC;
2416 } else if (ipsec_xform->direction ==
2417 RTE_SECURITY_IPSEC_SA_DIR_INGRESS) {
2418 memset(&session->decap_pdb, 0, sizeof(struct ipsec_decap_pdb));
2419 if (ipsec_xform->tunnel.type == RTE_SECURITY_IPSEC_TUNNEL_IPV4)
2420 session->decap_pdb.options = sizeof(struct ip) << 16;
2422 session->decap_pdb.options =
2423 sizeof(struct rte_ipv6_hdr) << 16;
2424 if (ipsec_xform->options.esn)
2425 session->decap_pdb.options |= PDBOPTS_ESP_ESN;
2426 session->dir = DIR_DEC;
2429 session->ctx_pool = internals->ctx_pool;
2430 rte_spinlock_lock(&internals->lock);
2431 for (i = 0; i < MAX_DPAA_CORES; i++) {
2432 session->inq[i] = dpaa_sec_attach_rxq(internals);
2433 if (session->inq[i] == NULL) {
2434 DPAA_SEC_ERR("unable to attach sec queue");
2435 rte_spinlock_unlock(&internals->lock);
2439 rte_spinlock_unlock(&internals->lock);
2443 rte_free(session->auth_key.data);
2444 rte_free(session->cipher_key.data);
2445 memset(session, 0, sizeof(dpaa_sec_session));
2450 dpaa_sec_set_pdcp_session(struct rte_cryptodev *dev,
2451 struct rte_security_session_conf *conf,
2454 struct rte_security_pdcp_xform *pdcp_xform = &conf->pdcp;
2455 struct rte_crypto_sym_xform *xform = conf->crypto_xform;
2456 struct rte_crypto_auth_xform *auth_xform = NULL;
2457 struct rte_crypto_cipher_xform *cipher_xform = NULL;
2458 dpaa_sec_session *session = (dpaa_sec_session *)sess;
2459 struct dpaa_sec_dev_private *dev_priv = dev->data->dev_private;
2462 PMD_INIT_FUNC_TRACE();
2464 memset(session, 0, sizeof(dpaa_sec_session));
2466 /* find xfrm types */
2467 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
2468 cipher_xform = &xform->cipher;
2469 if (xform->next != NULL)
2470 auth_xform = &xform->next->auth;
2471 } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
2472 auth_xform = &xform->auth;
2473 if (xform->next != NULL)
2474 cipher_xform = &xform->next->cipher;
2476 DPAA_SEC_ERR("Invalid crypto type");
2480 session->proto_alg = conf->protocol;
2482 session->cipher_key.data = rte_zmalloc(NULL,
2483 cipher_xform->key.length,
2484 RTE_CACHE_LINE_SIZE);
2485 if (session->cipher_key.data == NULL &&
2486 cipher_xform->key.length > 0) {
2487 DPAA_SEC_ERR("No Memory for cipher key");
2490 session->cipher_key.length = cipher_xform->key.length;
2491 memcpy(session->cipher_key.data, cipher_xform->key.data,
2492 cipher_xform->key.length);
2493 session->dir = (cipher_xform->op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
2495 session->cipher_alg = cipher_xform->algo;
2497 session->cipher_key.data = NULL;
2498 session->cipher_key.length = 0;
2499 session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
2500 session->dir = DIR_ENC;
2503 if (pdcp_xform->domain == RTE_SECURITY_PDCP_MODE_CONTROL) {
2504 if (pdcp_xform->sn_size != RTE_SECURITY_PDCP_SN_SIZE_5 &&
2505 pdcp_xform->sn_size != RTE_SECURITY_PDCP_SN_SIZE_12) {
2507 "PDCP Seq Num size should be 5/12 bits for cmode");
2513 session->auth_key.data = rte_zmalloc(NULL,
2514 auth_xform->key.length,
2515 RTE_CACHE_LINE_SIZE);
2516 if (!session->auth_key.data &&
2517 auth_xform->key.length > 0) {
2518 DPAA_SEC_ERR("No Memory for auth key");
2519 rte_free(session->cipher_key.data);
2522 session->auth_key.length = auth_xform->key.length;
2523 memcpy(session->auth_key.data, auth_xform->key.data,
2524 auth_xform->key.length);
2525 session->auth_alg = auth_xform->algo;
2527 session->auth_key.data = NULL;
2528 session->auth_key.length = 0;
2529 session->auth_alg = 0;
2531 session->pdcp.domain = pdcp_xform->domain;
2532 session->pdcp.bearer = pdcp_xform->bearer;
2533 session->pdcp.pkt_dir = pdcp_xform->pkt_dir;
2534 session->pdcp.sn_size = pdcp_xform->sn_size;
2535 session->pdcp.hfn = pdcp_xform->hfn;
2536 session->pdcp.hfn_threshold = pdcp_xform->hfn_threshold;
2537 session->pdcp.hfn_ovd = pdcp_xform->hfn_ovrd;
2538 session->pdcp.hfn_ovd_offset = cipher_xform->iv.offset;
2540 session->ctx_pool = dev_priv->ctx_pool;
2541 rte_spinlock_lock(&dev_priv->lock);
2542 for (i = 0; i < MAX_DPAA_CORES; i++) {
2543 session->inq[i] = dpaa_sec_attach_rxq(dev_priv);
2544 if (session->inq[i] == NULL) {
2545 DPAA_SEC_ERR("unable to attach sec queue");
2546 rte_spinlock_unlock(&dev_priv->lock);
2550 rte_spinlock_unlock(&dev_priv->lock);
2553 rte_free(session->auth_key.data);
2554 rte_free(session->cipher_key.data);
2555 memset(session, 0, sizeof(dpaa_sec_session));
2560 dpaa_sec_security_session_create(void *dev,
2561 struct rte_security_session_conf *conf,
2562 struct rte_security_session *sess,
2563 struct rte_mempool *mempool)
2565 void *sess_private_data;
2566 struct rte_cryptodev *cdev = (struct rte_cryptodev *)dev;
2569 if (rte_mempool_get(mempool, &sess_private_data)) {
2570 DPAA_SEC_ERR("Couldn't get object from session mempool");
2574 switch (conf->protocol) {
2575 case RTE_SECURITY_PROTOCOL_IPSEC:
2576 ret = dpaa_sec_set_ipsec_session(cdev, conf,
2579 case RTE_SECURITY_PROTOCOL_PDCP:
2580 ret = dpaa_sec_set_pdcp_session(cdev, conf,
2583 case RTE_SECURITY_PROTOCOL_MACSEC:
2589 DPAA_SEC_ERR("failed to configure session parameters");
2590 /* Return session to mempool */
2591 rte_mempool_put(mempool, sess_private_data);
2595 set_sec_session_private_data(sess, sess_private_data);
2600 /** Clear the memory of session so it doesn't leave key material behind */
2602 dpaa_sec_security_session_destroy(void *dev __rte_unused,
2603 struct rte_security_session *sess)
2605 PMD_INIT_FUNC_TRACE();
2606 void *sess_priv = get_sec_session_private_data(sess);
2607 dpaa_sec_session *s = (dpaa_sec_session *)sess_priv;
2610 free_session_memory((struct rte_cryptodev *)dev, s);
2611 set_sec_session_private_data(sess, NULL);
2617 dpaa_sec_dev_configure(struct rte_cryptodev *dev,
2618 struct rte_cryptodev_config *config __rte_unused)
2622 struct dpaa_sec_dev_private *internals;
2624 PMD_INIT_FUNC_TRACE();
2626 internals = dev->data->dev_private;
2627 snprintf(str, sizeof(str), "ctx_pool_%d", dev->data->dev_id);
2628 if (!internals->ctx_pool) {
2629 internals->ctx_pool = rte_mempool_create((const char *)str,
2632 CTX_POOL_CACHE_SIZE, 0,
2633 NULL, NULL, NULL, NULL,
2635 if (!internals->ctx_pool) {
2636 DPAA_SEC_ERR("%s create failed\n", str);
2640 DPAA_SEC_INFO("mempool already created for dev_id : %d",
2647 dpaa_sec_dev_start(struct rte_cryptodev *dev __rte_unused)
2649 PMD_INIT_FUNC_TRACE();
2654 dpaa_sec_dev_stop(struct rte_cryptodev *dev __rte_unused)
2656 PMD_INIT_FUNC_TRACE();
2660 dpaa_sec_dev_close(struct rte_cryptodev *dev)
2662 struct dpaa_sec_dev_private *internals;
2664 PMD_INIT_FUNC_TRACE();
2669 internals = dev->data->dev_private;
2670 rte_mempool_free(internals->ctx_pool);
2671 internals->ctx_pool = NULL;
2677 dpaa_sec_dev_infos_get(struct rte_cryptodev *dev,
2678 struct rte_cryptodev_info *info)
2680 struct dpaa_sec_dev_private *internals = dev->data->dev_private;
2682 PMD_INIT_FUNC_TRACE();
2684 info->max_nb_queue_pairs = internals->max_nb_queue_pairs;
2685 info->feature_flags = dev->feature_flags;
2686 info->capabilities = dpaa_sec_capabilities;
2687 info->sym.max_nb_sessions = internals->max_nb_sessions;
2688 info->driver_id = cryptodev_driver_id;
2692 static struct rte_cryptodev_ops crypto_ops = {
2693 .dev_configure = dpaa_sec_dev_configure,
2694 .dev_start = dpaa_sec_dev_start,
2695 .dev_stop = dpaa_sec_dev_stop,
2696 .dev_close = dpaa_sec_dev_close,
2697 .dev_infos_get = dpaa_sec_dev_infos_get,
2698 .queue_pair_setup = dpaa_sec_queue_pair_setup,
2699 .queue_pair_release = dpaa_sec_queue_pair_release,
2700 .queue_pair_count = dpaa_sec_queue_pair_count,
2701 .sym_session_get_size = dpaa_sec_sym_session_get_size,
2702 .sym_session_configure = dpaa_sec_sym_session_configure,
2703 .sym_session_clear = dpaa_sec_sym_session_clear
2706 static const struct rte_security_capability *
2707 dpaa_sec_capabilities_get(void *device __rte_unused)
2709 return dpaa_sec_security_cap;
2712 static const struct rte_security_ops dpaa_sec_security_ops = {
2713 .session_create = dpaa_sec_security_session_create,
2714 .session_update = NULL,
2715 .session_stats_get = NULL,
2716 .session_destroy = dpaa_sec_security_session_destroy,
2717 .set_pkt_metadata = NULL,
2718 .capabilities_get = dpaa_sec_capabilities_get
2722 dpaa_sec_uninit(struct rte_cryptodev *dev)
2724 struct dpaa_sec_dev_private *internals;
2729 internals = dev->data->dev_private;
2730 rte_free(dev->security_ctx);
2732 /* In case close has been called, internals->ctx_pool would be NULL */
2733 rte_mempool_free(internals->ctx_pool);
2734 rte_free(internals);
2736 DPAA_SEC_INFO("Closing DPAA_SEC device %s on numa socket %u",
2737 dev->data->name, rte_socket_id());
2743 dpaa_sec_dev_init(struct rte_cryptodev *cryptodev)
2745 struct dpaa_sec_dev_private *internals;
2746 struct rte_security_ctx *security_instance;
2747 struct dpaa_sec_qp *qp;
2751 PMD_INIT_FUNC_TRACE();
2753 cryptodev->driver_id = cryptodev_driver_id;
2754 cryptodev->dev_ops = &crypto_ops;
2756 cryptodev->enqueue_burst = dpaa_sec_enqueue_burst;
2757 cryptodev->dequeue_burst = dpaa_sec_dequeue_burst;
2758 cryptodev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
2759 RTE_CRYPTODEV_FF_HW_ACCELERATED |
2760 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
2761 RTE_CRYPTODEV_FF_SECURITY |
2762 RTE_CRYPTODEV_FF_IN_PLACE_SGL |
2763 RTE_CRYPTODEV_FF_OOP_SGL_IN_SGL_OUT |
2764 RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT |
2765 RTE_CRYPTODEV_FF_OOP_LB_IN_SGL_OUT |
2766 RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT;
2768 internals = cryptodev->data->dev_private;
2769 internals->max_nb_queue_pairs = RTE_DPAA_MAX_NB_SEC_QPS;
2770 internals->max_nb_sessions = RTE_DPAA_SEC_PMD_MAX_NB_SESSIONS;
2773 * For secondary processes, we don't initialise any further as primary
2774 * has already done this work. Only check we don't need a different
2777 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
2778 DPAA_SEC_WARN("Device already init by primary process");
2782 /* Initialize security_ctx only for primary process*/
2783 security_instance = rte_malloc("rte_security_instances_ops",
2784 sizeof(struct rte_security_ctx), 0);
2785 if (security_instance == NULL)
2787 security_instance->device = (void *)cryptodev;
2788 security_instance->ops = &dpaa_sec_security_ops;
2789 security_instance->sess_cnt = 0;
2790 cryptodev->security_ctx = security_instance;
2792 rte_spinlock_init(&internals->lock);
2793 for (i = 0; i < internals->max_nb_queue_pairs; i++) {
2794 /* init qman fq for queue pair */
2795 qp = &internals->qps[i];
2796 ret = dpaa_sec_init_tx(&qp->outq);
2798 DPAA_SEC_ERR("config tx of queue pair %d", i);
2803 flags = QMAN_FQ_FLAG_LOCKED | QMAN_FQ_FLAG_DYNAMIC_FQID |
2804 QMAN_FQ_FLAG_TO_DCPORTAL;
2805 for (i = 0; i < MAX_DPAA_CORES * internals->max_nb_sessions; i++) {
2806 /* create rx qman fq for sessions*/
2807 ret = qman_create_fq(0, flags, &internals->inq[i]);
2808 if (unlikely(ret != 0)) {
2809 DPAA_SEC_ERR("sec qman_create_fq failed");
2814 RTE_LOG(INFO, PMD, "%s cryptodev init\n", cryptodev->data->name);
2818 DPAA_SEC_ERR("driver %s: create failed\n", cryptodev->data->name);
2820 dpaa_sec_uninit(cryptodev);
2825 cryptodev_dpaa_sec_probe(struct rte_dpaa_driver *dpaa_drv __rte_unused,
2826 struct rte_dpaa_device *dpaa_dev)
2828 struct rte_cryptodev *cryptodev;
2829 char cryptodev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
2833 snprintf(cryptodev_name, sizeof(cryptodev_name), "dpaa_sec-%d",
2834 dpaa_dev->id.dev_id);
2836 cryptodev = rte_cryptodev_pmd_allocate(cryptodev_name, rte_socket_id());
2837 if (cryptodev == NULL)
2840 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
2841 cryptodev->data->dev_private = rte_zmalloc_socket(
2842 "cryptodev private structure",
2843 sizeof(struct dpaa_sec_dev_private),
2844 RTE_CACHE_LINE_SIZE,
2847 if (cryptodev->data->dev_private == NULL)
2848 rte_panic("Cannot allocate memzone for private "
2852 dpaa_dev->crypto_dev = cryptodev;
2853 cryptodev->device = &dpaa_dev->device;
2855 /* init user callbacks */
2856 TAILQ_INIT(&(cryptodev->link_intr_cbs));
2858 /* if sec device version is not configured */
2859 if (!rta_get_sec_era()) {
2860 const struct device_node *caam_node;
2862 for_each_compatible_node(caam_node, NULL, "fsl,sec-v4.0") {
2863 const uint32_t *prop = of_get_property(caam_node,
2868 INTL_SEC_ERA(rte_cpu_to_be_32(*prop)));
2874 /* Invoke PMD device initialization function */
2875 retval = dpaa_sec_dev_init(cryptodev);
2879 /* In case of error, cleanup is done */
2880 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
2881 rte_free(cryptodev->data->dev_private);
2883 rte_cryptodev_pmd_release_device(cryptodev);
2889 cryptodev_dpaa_sec_remove(struct rte_dpaa_device *dpaa_dev)
2891 struct rte_cryptodev *cryptodev;
2894 cryptodev = dpaa_dev->crypto_dev;
2895 if (cryptodev == NULL)
2898 ret = dpaa_sec_uninit(cryptodev);
2902 return rte_cryptodev_pmd_destroy(cryptodev);
2905 static struct rte_dpaa_driver rte_dpaa_sec_driver = {
2906 .drv_type = FSL_DPAA_CRYPTO,
2908 .name = "DPAA SEC PMD"
2910 .probe = cryptodev_dpaa_sec_probe,
2911 .remove = cryptodev_dpaa_sec_remove,
2914 static struct cryptodev_driver dpaa_sec_crypto_drv;
2916 RTE_PMD_REGISTER_DPAA(CRYPTODEV_NAME_DPAA_SEC_PMD, rte_dpaa_sec_driver);
2917 RTE_PMD_REGISTER_CRYPTO_DRIVER(dpaa_sec_crypto_drv, rte_dpaa_sec_driver.driver,
2918 cryptodev_driver_id);
2920 RTE_INIT(dpaa_sec_init_log)
2922 dpaa_logtype_sec = rte_log_register("pmd.crypto.dpaa");
2923 if (dpaa_logtype_sec >= 0)
2924 rte_log_set_level(dpaa_logtype_sec, RTE_LOG_NOTICE);