xref: /dpdk/drivers/crypto/dpaa_sec/dpaa_sec.c (revision 5a7dbb934d75395aa1a03dfd0ad2a66028a62c1b)
1d81734caSHemant Agrawal /* SPDX-License-Identifier: BSD-3-Clause
2c3e85bdcSAkhil Goyal  *
3c3e85bdcSAkhil Goyal  *   Copyright (c) 2016 Freescale Semiconductor, Inc. All rights reserved.
4f163231eSHemant Agrawal  *   Copyright 2017-2018 NXP
5c3e85bdcSAkhil Goyal  *
6c3e85bdcSAkhil Goyal  */
7c3e85bdcSAkhil Goyal 
8c3e85bdcSAkhil Goyal #include <fcntl.h>
9c3e85bdcSAkhil Goyal #include <unistd.h>
10c3e85bdcSAkhil Goyal #include <sched.h>
11c3e85bdcSAkhil Goyal #include <net/if.h>
12c3e85bdcSAkhil Goyal 
13c3e85bdcSAkhil Goyal #include <rte_byteorder.h>
14c3e85bdcSAkhil Goyal #include <rte_common.h>
15c3e85bdcSAkhil Goyal #include <rte_cryptodev_pmd.h>
16c3e85bdcSAkhil Goyal #include <rte_crypto.h>
17c3e85bdcSAkhil Goyal #include <rte_cryptodev.h>
181f14d500SAkhil Goyal #include <rte_security_driver.h>
19c3e85bdcSAkhil Goyal #include <rte_cycles.h>
20c3e85bdcSAkhil Goyal #include <rte_dev.h>
21c3e85bdcSAkhil Goyal #include <rte_kvargs.h>
22c3e85bdcSAkhil Goyal #include <rte_malloc.h>
23c3e85bdcSAkhil Goyal #include <rte_mbuf.h>
24c3e85bdcSAkhil Goyal #include <rte_memcpy.h>
25c3e85bdcSAkhil Goyal #include <rte_string_fns.h>
263b617ee7SAkhil Goyal #include <rte_spinlock.h>
27c3e85bdcSAkhil Goyal 
28c3e85bdcSAkhil Goyal #include <fsl_usd.h>
29c3e85bdcSAkhil Goyal #include <fsl_qman.h>
30c3e85bdcSAkhil Goyal #include <of.h>
31c3e85bdcSAkhil Goyal 
32c3e85bdcSAkhil Goyal /* RTA header files */
33c3e85bdcSAkhil Goyal #include <hw/desc/common.h>
34c3e85bdcSAkhil Goyal #include <hw/desc/algo.h>
35c3e85bdcSAkhil Goyal #include <hw/desc/ipsec.h>
36c3e85bdcSAkhil Goyal 
37c3e85bdcSAkhil Goyal #include <rte_dpaa_bus.h>
38c3e85bdcSAkhil Goyal #include <dpaa_sec.h>
39c3e85bdcSAkhil Goyal #include <dpaa_sec_log.h>
40c3e85bdcSAkhil Goyal 
41c3e85bdcSAkhil Goyal enum rta_sec_era rta_sec_era;
42c3e85bdcSAkhil Goyal 
43f163231eSHemant Agrawal int dpaa_logtype_sec;
44f163231eSHemant Agrawal 
45c3e85bdcSAkhil Goyal static uint8_t cryptodev_driver_id;
46c3e85bdcSAkhil Goyal 
47c3e85bdcSAkhil Goyal static __thread struct rte_crypto_op **dpaa_sec_ops;
48c3e85bdcSAkhil Goyal static __thread int dpaa_sec_op_nb;
49c3e85bdcSAkhil Goyal 
50e79416d1SHemant Agrawal static int
51e79416d1SHemant Agrawal dpaa_sec_attach_sess_q(struct dpaa_sec_qp *qp, dpaa_sec_session *sess);
52e79416d1SHemant Agrawal 
53c3e85bdcSAkhil Goyal static inline void
54c3e85bdcSAkhil Goyal dpaa_sec_op_ending(struct dpaa_sec_op_ctx *ctx)
55c3e85bdcSAkhil Goyal {
56c3e85bdcSAkhil Goyal 	if (!ctx->fd_status) {
57c3e85bdcSAkhil Goyal 		ctx->op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
58c3e85bdcSAkhil Goyal 	} else {
59f163231eSHemant Agrawal 		DPAA_SEC_DP_WARN("SEC return err: 0x%x", ctx->fd_status);
60c3e85bdcSAkhil Goyal 		ctx->op->status = RTE_CRYPTO_OP_STATUS_ERROR;
61c3e85bdcSAkhil Goyal 	}
62c3e85bdcSAkhil Goyal 
63c3e85bdcSAkhil Goyal 	/* report op status to sym->op and then free the ctx memeory  */
64c3e85bdcSAkhil Goyal 	rte_mempool_put(ctx->ctx_pool, (void *)ctx);
65c3e85bdcSAkhil Goyal }
66c3e85bdcSAkhil Goyal 
67c3e85bdcSAkhil Goyal static inline struct dpaa_sec_op_ctx *
68c3e85bdcSAkhil Goyal dpaa_sec_alloc_ctx(dpaa_sec_session *ses)
69c3e85bdcSAkhil Goyal {
70c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
71c3e85bdcSAkhil Goyal 	int retval;
72c3e85bdcSAkhil Goyal 
73c3e85bdcSAkhil Goyal 	retval = rte_mempool_get(ses->ctx_pool, (void **)(&ctx));
74c3e85bdcSAkhil Goyal 	if (!ctx || retval) {
75f163231eSHemant Agrawal 		DPAA_SEC_DP_WARN("Alloc sec descriptor failed!");
76c3e85bdcSAkhil Goyal 		return NULL;
77c3e85bdcSAkhil Goyal 	}
78c3e85bdcSAkhil Goyal 	/*
79c3e85bdcSAkhil Goyal 	 * Clear SG memory. There are 16 SG entries of 16 Bytes each.
80c3e85bdcSAkhil Goyal 	 * one call to dcbz_64() clear 64 bytes, hence calling it 4 times
81c3e85bdcSAkhil Goyal 	 * to clear all the SG entries. dpaa_sec_alloc_ctx() is called for
82c3e85bdcSAkhil Goyal 	 * each packet, memset is costlier than dcbz_64().
83c3e85bdcSAkhil Goyal 	 */
84c3e85bdcSAkhil Goyal 	dcbz_64(&ctx->job.sg[SG_CACHELINE_0]);
85c3e85bdcSAkhil Goyal 	dcbz_64(&ctx->job.sg[SG_CACHELINE_1]);
86c3e85bdcSAkhil Goyal 	dcbz_64(&ctx->job.sg[SG_CACHELINE_2]);
87c3e85bdcSAkhil Goyal 	dcbz_64(&ctx->job.sg[SG_CACHELINE_3]);
88c3e85bdcSAkhil Goyal 
89c3e85bdcSAkhil Goyal 	ctx->ctx_pool = ses->ctx_pool;
900e5607e4SHemant Agrawal 	ctx->vtop_offset = (size_t) ctx
91fcf67029SHemant Agrawal 				- rte_mempool_virt2iova(ctx);
92c3e85bdcSAkhil Goyal 
93c3e85bdcSAkhil Goyal 	return ctx;
94c3e85bdcSAkhil Goyal }
95c3e85bdcSAkhil Goyal 
96c4509373SSantosh Shukla static inline rte_iova_t
97c3e85bdcSAkhil Goyal dpaa_mem_vtop(void *vaddr)
98c3e85bdcSAkhil Goyal {
9929f3c9e5SAnatoly Burakov 	const struct rte_memseg *ms;
100c3e85bdcSAkhil Goyal 
10166cc45e2SAnatoly Burakov 	ms = rte_mem_virt2memseg(vaddr, NULL);
10229f3c9e5SAnatoly Burakov 	if (ms)
10329f3c9e5SAnatoly Burakov 		return ms->iova + RTE_PTR_DIFF(vaddr, ms->addr);
1040e5607e4SHemant Agrawal 	return (size_t)NULL;
105c3e85bdcSAkhil Goyal }
106c3e85bdcSAkhil Goyal 
107c3e85bdcSAkhil Goyal static inline void *
108c4509373SSantosh Shukla dpaa_mem_ptov(rte_iova_t paddr)
109c3e85bdcSAkhil Goyal {
110*5a7dbb93SShreyansh Jain 	void *va;
111*5a7dbb93SShreyansh Jain 
112*5a7dbb93SShreyansh Jain 	va = (void *)dpaax_iova_table_get_va(paddr);
113*5a7dbb93SShreyansh Jain 	if (likely(va))
114*5a7dbb93SShreyansh Jain 		return va;
115*5a7dbb93SShreyansh Jain 
11611d2f002SAnatoly Burakov 	return rte_mem_iova2virt(paddr);
117c3e85bdcSAkhil Goyal }
118c3e85bdcSAkhil Goyal 
119c3e85bdcSAkhil Goyal static void
120c3e85bdcSAkhil Goyal ern_sec_fq_handler(struct qman_portal *qm __rte_unused,
121c3e85bdcSAkhil Goyal 		   struct qman_fq *fq,
122c3e85bdcSAkhil Goyal 		   const struct qm_mr_entry *msg)
123c3e85bdcSAkhil Goyal {
124f163231eSHemant Agrawal 	DPAA_SEC_DP_ERR("sec fq %d error, RC = %x, seqnum = %x\n",
125c3e85bdcSAkhil Goyal 			fq->fqid, msg->ern.rc, msg->ern.seqnum);
126c3e85bdcSAkhil Goyal }
127c3e85bdcSAkhil Goyal 
128c3e85bdcSAkhil Goyal /* initialize the queue with dest chan as caam chan so that
129c3e85bdcSAkhil Goyal  * all the packets in this queue could be dispatched into caam
130c3e85bdcSAkhil Goyal  */
131c3e85bdcSAkhil Goyal static int
132c4509373SSantosh Shukla dpaa_sec_init_rx(struct qman_fq *fq_in, rte_iova_t hwdesc,
133c3e85bdcSAkhil Goyal 		 uint32_t fqid_out)
134c3e85bdcSAkhil Goyal {
135c3e85bdcSAkhil Goyal 	struct qm_mcc_initfq fq_opts;
136c3e85bdcSAkhil Goyal 	uint32_t flags;
137c3e85bdcSAkhil Goyal 	int ret = -1;
138c3e85bdcSAkhil Goyal 
139c3e85bdcSAkhil Goyal 	/* Clear FQ options */
140c3e85bdcSAkhil Goyal 	memset(&fq_opts, 0x00, sizeof(struct qm_mcc_initfq));
141c3e85bdcSAkhil Goyal 
142c3e85bdcSAkhil Goyal 	flags = QMAN_INITFQ_FLAG_SCHED;
143c3e85bdcSAkhil Goyal 	fq_opts.we_mask = QM_INITFQ_WE_DESTWQ | QM_INITFQ_WE_CONTEXTA |
144c3e85bdcSAkhil Goyal 			  QM_INITFQ_WE_CONTEXTB;
145c3e85bdcSAkhil Goyal 
146c3e85bdcSAkhil Goyal 	qm_fqd_context_a_set64(&fq_opts.fqd, hwdesc);
147c3e85bdcSAkhil Goyal 	fq_opts.fqd.context_b = fqid_out;
148c3e85bdcSAkhil Goyal 	fq_opts.fqd.dest.channel = qm_channel_caam;
149c3e85bdcSAkhil Goyal 	fq_opts.fqd.dest.wq = 0;
150c3e85bdcSAkhil Goyal 
151c3e85bdcSAkhil Goyal 	fq_in->cb.ern  = ern_sec_fq_handler;
152c3e85bdcSAkhil Goyal 
153f163231eSHemant Agrawal 	DPAA_SEC_DEBUG("in-%x out-%x", fq_in->fqid, fqid_out);
154e79416d1SHemant Agrawal 
155c3e85bdcSAkhil Goyal 	ret = qman_init_fq(fq_in, flags, &fq_opts);
156c3e85bdcSAkhil Goyal 	if (unlikely(ret != 0))
157f163231eSHemant Agrawal 		DPAA_SEC_ERR("qman_init_fq failed %d", ret);
158c3e85bdcSAkhil Goyal 
159c3e85bdcSAkhil Goyal 	return ret;
160c3e85bdcSAkhil Goyal }
161c3e85bdcSAkhil Goyal 
162c3e85bdcSAkhil Goyal /* something is put into in_fq and caam put the crypto result into out_fq */
163c3e85bdcSAkhil Goyal static enum qman_cb_dqrr_result
164c3e85bdcSAkhil Goyal dqrr_out_fq_cb_rx(struct qman_portal *qm __always_unused,
165c3e85bdcSAkhil Goyal 		  struct qman_fq *fq __always_unused,
166c3e85bdcSAkhil Goyal 		  const struct qm_dqrr_entry *dqrr)
167c3e85bdcSAkhil Goyal {
168c3e85bdcSAkhil Goyal 	const struct qm_fd *fd;
169c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *job;
170c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
171c3e85bdcSAkhil Goyal 
172c3e85bdcSAkhil Goyal 	if (dpaa_sec_op_nb >= DPAA_SEC_BURST)
173c3e85bdcSAkhil Goyal 		return qman_cb_dqrr_defer;
174c3e85bdcSAkhil Goyal 
175c3e85bdcSAkhil Goyal 	if (!(dqrr->stat & QM_DQRR_STAT_FD_VALID))
176c3e85bdcSAkhil Goyal 		return qman_cb_dqrr_consume;
177c3e85bdcSAkhil Goyal 
178c3e85bdcSAkhil Goyal 	fd = &dqrr->fd;
179c3e85bdcSAkhil Goyal 	/* sg is embedded in an op ctx,
180c3e85bdcSAkhil Goyal 	 * sg[0] is for output
181c3e85bdcSAkhil Goyal 	 * sg[1] for input
182c3e85bdcSAkhil Goyal 	 */
183c3e85bdcSAkhil Goyal 	job = dpaa_mem_ptov(qm_fd_addr_get64(fd));
1841f14d500SAkhil Goyal 
185c3e85bdcSAkhil Goyal 	ctx = container_of(job, struct dpaa_sec_op_ctx, job);
186c3e85bdcSAkhil Goyal 	ctx->fd_status = fd->status;
1871f14d500SAkhil Goyal 	if (ctx->op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
1881f14d500SAkhil Goyal 		struct qm_sg_entry *sg_out;
1891f14d500SAkhil Goyal 		uint32_t len;
1901f14d500SAkhil Goyal 
1911f14d500SAkhil Goyal 		sg_out = &job->sg[0];
1921f14d500SAkhil Goyal 		hw_sg_to_cpu(sg_out);
1931f14d500SAkhil Goyal 		len = sg_out->length;
1941f14d500SAkhil Goyal 		ctx->op->sym->m_src->pkt_len = len;
1951f14d500SAkhil Goyal 		ctx->op->sym->m_src->data_len = len;
1961f14d500SAkhil Goyal 	}
197c3e85bdcSAkhil Goyal 	dpaa_sec_ops[dpaa_sec_op_nb++] = ctx->op;
198c3e85bdcSAkhil Goyal 	dpaa_sec_op_ending(ctx);
199c3e85bdcSAkhil Goyal 
200c3e85bdcSAkhil Goyal 	return qman_cb_dqrr_consume;
201c3e85bdcSAkhil Goyal }
202c3e85bdcSAkhil Goyal 
203c3e85bdcSAkhil Goyal /* caam result is put into this queue */
204c3e85bdcSAkhil Goyal static int
205c3e85bdcSAkhil Goyal dpaa_sec_init_tx(struct qman_fq *fq)
206c3e85bdcSAkhil Goyal {
207c3e85bdcSAkhil Goyal 	int ret;
208c3e85bdcSAkhil Goyal 	struct qm_mcc_initfq opts;
209c3e85bdcSAkhil Goyal 	uint32_t flags;
210c3e85bdcSAkhil Goyal 
211c3e85bdcSAkhil Goyal 	flags = QMAN_FQ_FLAG_NO_ENQUEUE | QMAN_FQ_FLAG_LOCKED |
212c3e85bdcSAkhil Goyal 		QMAN_FQ_FLAG_DYNAMIC_FQID;
213c3e85bdcSAkhil Goyal 
214c3e85bdcSAkhil Goyal 	ret = qman_create_fq(0, flags, fq);
215c3e85bdcSAkhil Goyal 	if (unlikely(ret)) {
216f163231eSHemant Agrawal 		DPAA_SEC_ERR("qman_create_fq failed");
217c3e85bdcSAkhil Goyal 		return ret;
218c3e85bdcSAkhil Goyal 	}
219c3e85bdcSAkhil Goyal 
220c3e85bdcSAkhil Goyal 	memset(&opts, 0, sizeof(opts));
221c3e85bdcSAkhil Goyal 	opts.we_mask = QM_INITFQ_WE_DESTWQ | QM_INITFQ_WE_FQCTRL |
222c3e85bdcSAkhil Goyal 		       QM_INITFQ_WE_CONTEXTA | QM_INITFQ_WE_CONTEXTB;
223c3e85bdcSAkhil Goyal 
224c3e85bdcSAkhil Goyal 	/* opts.fqd.dest.channel = dpaa_sec_pool_chan; */
225c3e85bdcSAkhil Goyal 
226c3e85bdcSAkhil Goyal 	fq->cb.dqrr = dqrr_out_fq_cb_rx;
227c3e85bdcSAkhil Goyal 	fq->cb.ern  = ern_sec_fq_handler;
228c3e85bdcSAkhil Goyal 
229c3e85bdcSAkhil Goyal 	ret = qman_init_fq(fq, 0, &opts);
230c3e85bdcSAkhil Goyal 	if (unlikely(ret)) {
231f163231eSHemant Agrawal 		DPAA_SEC_ERR("unable to init caam source fq!");
232c3e85bdcSAkhil Goyal 		return ret;
233c3e85bdcSAkhil Goyal 	}
234c3e85bdcSAkhil Goyal 
235c3e85bdcSAkhil Goyal 	return ret;
236c3e85bdcSAkhil Goyal }
237c3e85bdcSAkhil Goyal 
238c3e85bdcSAkhil Goyal static inline int is_cipher_only(dpaa_sec_session *ses)
239c3e85bdcSAkhil Goyal {
240c3e85bdcSAkhil Goyal 	return ((ses->cipher_alg != RTE_CRYPTO_CIPHER_NULL) &&
241c3e85bdcSAkhil Goyal 		(ses->auth_alg == RTE_CRYPTO_AUTH_NULL));
242c3e85bdcSAkhil Goyal }
243c3e85bdcSAkhil Goyal 
244c3e85bdcSAkhil Goyal static inline int is_auth_only(dpaa_sec_session *ses)
245c3e85bdcSAkhil Goyal {
246c3e85bdcSAkhil Goyal 	return ((ses->cipher_alg == RTE_CRYPTO_CIPHER_NULL) &&
247c3e85bdcSAkhil Goyal 		(ses->auth_alg != RTE_CRYPTO_AUTH_NULL));
248c3e85bdcSAkhil Goyal }
249c3e85bdcSAkhil Goyal 
250c3e85bdcSAkhil Goyal static inline int is_aead(dpaa_sec_session *ses)
251c3e85bdcSAkhil Goyal {
252c3e85bdcSAkhil Goyal 	return ((ses->cipher_alg == 0) &&
253c3e85bdcSAkhil Goyal 		(ses->auth_alg == 0) &&
254c3e85bdcSAkhil Goyal 		(ses->aead_alg != 0));
255c3e85bdcSAkhil Goyal }
256c3e85bdcSAkhil Goyal 
257c3e85bdcSAkhil Goyal static inline int is_auth_cipher(dpaa_sec_session *ses)
258c3e85bdcSAkhil Goyal {
259c3e85bdcSAkhil Goyal 	return ((ses->cipher_alg != RTE_CRYPTO_CIPHER_NULL) &&
2601f14d500SAkhil Goyal 		(ses->auth_alg != RTE_CRYPTO_AUTH_NULL) &&
2611f14d500SAkhil Goyal 		(ses->proto_alg != RTE_SECURITY_PROTOCOL_IPSEC));
2621f14d500SAkhil Goyal }
2631f14d500SAkhil Goyal 
2641f14d500SAkhil Goyal static inline int is_proto_ipsec(dpaa_sec_session *ses)
2651f14d500SAkhil Goyal {
2661f14d500SAkhil Goyal 	return (ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC);
267c3e85bdcSAkhil Goyal }
268c3e85bdcSAkhil Goyal 
269c3e85bdcSAkhil Goyal static inline int is_encode(dpaa_sec_session *ses)
270c3e85bdcSAkhil Goyal {
271c3e85bdcSAkhil Goyal 	return ses->dir == DIR_ENC;
272c3e85bdcSAkhil Goyal }
273c3e85bdcSAkhil Goyal 
274c3e85bdcSAkhil Goyal static inline int is_decode(dpaa_sec_session *ses)
275c3e85bdcSAkhil Goyal {
276c3e85bdcSAkhil Goyal 	return ses->dir == DIR_DEC;
277c3e85bdcSAkhil Goyal }
278c3e85bdcSAkhil Goyal 
279c3e85bdcSAkhil Goyal static inline void
280c3e85bdcSAkhil Goyal caam_auth_alg(dpaa_sec_session *ses, struct alginfo *alginfo_a)
281c3e85bdcSAkhil Goyal {
282c3e85bdcSAkhil Goyal 	switch (ses->auth_alg) {
283c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AUTH_NULL:
28405b12700SHemant Agrawal 		alginfo_a->algtype =
28505b12700SHemant Agrawal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
28605b12700SHemant Agrawal 			OP_PCL_IPSEC_HMAC_NULL : 0;
287c3e85bdcSAkhil Goyal 		ses->digest_length = 0;
288c3e85bdcSAkhil Goyal 		break;
289c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AUTH_MD5_HMAC:
2901f14d500SAkhil Goyal 		alginfo_a->algtype =
2911f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
2921f14d500SAkhil Goyal 			OP_PCL_IPSEC_HMAC_MD5_96 : OP_ALG_ALGSEL_MD5;
293c3e85bdcSAkhil Goyal 		alginfo_a->algmode = OP_ALG_AAI_HMAC;
294c3e85bdcSAkhil Goyal 		break;
295c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA1_HMAC:
2961f14d500SAkhil Goyal 		alginfo_a->algtype =
2971f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
2981f14d500SAkhil Goyal 			OP_PCL_IPSEC_HMAC_SHA1_96 : OP_ALG_ALGSEL_SHA1;
299c3e85bdcSAkhil Goyal 		alginfo_a->algmode = OP_ALG_AAI_HMAC;
300c3e85bdcSAkhil Goyal 		break;
301c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA224_HMAC:
3021f14d500SAkhil Goyal 		alginfo_a->algtype =
3031f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3041f14d500SAkhil Goyal 			OP_PCL_IPSEC_HMAC_SHA1_160 : OP_ALG_ALGSEL_SHA224;
305c3e85bdcSAkhil Goyal 		alginfo_a->algmode = OP_ALG_AAI_HMAC;
306c3e85bdcSAkhil Goyal 		break;
307c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA256_HMAC:
3081f14d500SAkhil Goyal 		alginfo_a->algtype =
3091f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3101f14d500SAkhil Goyal 			OP_PCL_IPSEC_HMAC_SHA2_256_128 : OP_ALG_ALGSEL_SHA256;
311c3e85bdcSAkhil Goyal 		alginfo_a->algmode = OP_ALG_AAI_HMAC;
312c3e85bdcSAkhil Goyal 		break;
313c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA384_HMAC:
3141f14d500SAkhil Goyal 		alginfo_a->algtype =
3151f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3161f14d500SAkhil Goyal 			OP_PCL_IPSEC_HMAC_SHA2_384_192 : OP_ALG_ALGSEL_SHA384;
317c3e85bdcSAkhil Goyal 		alginfo_a->algmode = OP_ALG_AAI_HMAC;
318c3e85bdcSAkhil Goyal 		break;
319c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA512_HMAC:
3201f14d500SAkhil Goyal 		alginfo_a->algtype =
3211f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3221f14d500SAkhil Goyal 			OP_PCL_IPSEC_HMAC_SHA2_512_256 : OP_ALG_ALGSEL_SHA512;
323c3e85bdcSAkhil Goyal 		alginfo_a->algmode = OP_ALG_AAI_HMAC;
324c3e85bdcSAkhil Goyal 		break;
325c3e85bdcSAkhil Goyal 	default:
326f163231eSHemant Agrawal 		DPAA_SEC_ERR("unsupported auth alg %u", ses->auth_alg);
327c3e85bdcSAkhil Goyal 	}
328c3e85bdcSAkhil Goyal }
329c3e85bdcSAkhil Goyal 
330c3e85bdcSAkhil Goyal static inline void
331c3e85bdcSAkhil Goyal caam_cipher_alg(dpaa_sec_session *ses, struct alginfo *alginfo_c)
332c3e85bdcSAkhil Goyal {
333c3e85bdcSAkhil Goyal 	switch (ses->cipher_alg) {
334c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_CIPHER_NULL:
33505b12700SHemant Agrawal 		alginfo_c->algtype =
33605b12700SHemant Agrawal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
33705b12700SHemant Agrawal 			OP_PCL_IPSEC_NULL : 0;
338c3e85bdcSAkhil Goyal 		break;
339c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_CIPHER_AES_CBC:
3401f14d500SAkhil Goyal 		alginfo_c->algtype =
3411f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3421f14d500SAkhil Goyal 			OP_PCL_IPSEC_AES_CBC : OP_ALG_ALGSEL_AES;
343c3e85bdcSAkhil Goyal 		alginfo_c->algmode = OP_ALG_AAI_CBC;
344c3e85bdcSAkhil Goyal 		break;
345c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_CIPHER_3DES_CBC:
3461f14d500SAkhil Goyal 		alginfo_c->algtype =
3471f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3481f14d500SAkhil Goyal 			OP_PCL_IPSEC_3DES : OP_ALG_ALGSEL_3DES;
349c3e85bdcSAkhil Goyal 		alginfo_c->algmode = OP_ALG_AAI_CBC;
350c3e85bdcSAkhil Goyal 		break;
351c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_CIPHER_AES_CTR:
3521f14d500SAkhil Goyal 		alginfo_c->algtype =
3531f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3541f14d500SAkhil Goyal 			OP_PCL_IPSEC_AES_CTR : OP_ALG_ALGSEL_AES;
355c3e85bdcSAkhil Goyal 		alginfo_c->algmode = OP_ALG_AAI_CTR;
356c3e85bdcSAkhil Goyal 		break;
357c3e85bdcSAkhil Goyal 	default:
358f163231eSHemant Agrawal 		DPAA_SEC_ERR("unsupported cipher alg %d", ses->cipher_alg);
359c3e85bdcSAkhil Goyal 	}
360c3e85bdcSAkhil Goyal }
361c3e85bdcSAkhil Goyal 
362c3e85bdcSAkhil Goyal static inline void
363c3e85bdcSAkhil Goyal caam_aead_alg(dpaa_sec_session *ses, struct alginfo *alginfo)
364c3e85bdcSAkhil Goyal {
365c3e85bdcSAkhil Goyal 	switch (ses->aead_alg) {
366c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AEAD_AES_GCM:
367c3e85bdcSAkhil Goyal 		alginfo->algtype = OP_ALG_ALGSEL_AES;
368c3e85bdcSAkhil Goyal 		alginfo->algmode = OP_ALG_AAI_GCM;
369c3e85bdcSAkhil Goyal 		break;
370c3e85bdcSAkhil Goyal 	default:
371f163231eSHemant Agrawal 		DPAA_SEC_ERR("unsupported AEAD alg %d", ses->aead_alg);
372c3e85bdcSAkhil Goyal 	}
373c3e85bdcSAkhil Goyal }
374c3e85bdcSAkhil Goyal 
37505b12700SHemant Agrawal /* prepare ipsec proto command block of the session */
37605b12700SHemant Agrawal static int
37705b12700SHemant Agrawal dpaa_sec_prep_ipsec_cdb(dpaa_sec_session *ses)
37805b12700SHemant Agrawal {
37905b12700SHemant Agrawal 	struct alginfo cipherdata = {0}, authdata = {0};
38005b12700SHemant Agrawal 	struct sec_cdb *cdb = &ses->cdb;
38105b12700SHemant Agrawal 	int32_t shared_desc_len = 0;
38205b12700SHemant Agrawal 	int err;
38305b12700SHemant Agrawal #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
38405b12700SHemant Agrawal 	int swap = false;
38505b12700SHemant Agrawal #else
38605b12700SHemant Agrawal 	int swap = true;
38705b12700SHemant Agrawal #endif
38805b12700SHemant Agrawal 
38905b12700SHemant Agrawal 	caam_cipher_alg(ses, &cipherdata);
39005b12700SHemant Agrawal 	if (cipherdata.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
39105b12700SHemant Agrawal 		DPAA_SEC_ERR("not supported cipher alg");
39205b12700SHemant Agrawal 		return -ENOTSUP;
39305b12700SHemant Agrawal 	}
39405b12700SHemant Agrawal 
39505b12700SHemant Agrawal 	cipherdata.key = (size_t)ses->cipher_key.data;
39605b12700SHemant Agrawal 	cipherdata.keylen = ses->cipher_key.length;
39705b12700SHemant Agrawal 	cipherdata.key_enc_flags = 0;
39805b12700SHemant Agrawal 	cipherdata.key_type = RTA_DATA_IMM;
39905b12700SHemant Agrawal 
40005b12700SHemant Agrawal 	caam_auth_alg(ses, &authdata);
40105b12700SHemant Agrawal 	if (authdata.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
40205b12700SHemant Agrawal 		DPAA_SEC_ERR("not supported auth alg");
40305b12700SHemant Agrawal 		return -ENOTSUP;
40405b12700SHemant Agrawal 	}
40505b12700SHemant Agrawal 
40605b12700SHemant Agrawal 	authdata.key = (size_t)ses->auth_key.data;
40705b12700SHemant Agrawal 	authdata.keylen = ses->auth_key.length;
40805b12700SHemant Agrawal 	authdata.key_enc_flags = 0;
40905b12700SHemant Agrawal 	authdata.key_type = RTA_DATA_IMM;
41005b12700SHemant Agrawal 
41105b12700SHemant Agrawal 	cdb->sh_desc[0] = cipherdata.keylen;
41205b12700SHemant Agrawal 	cdb->sh_desc[1] = authdata.keylen;
41305b12700SHemant Agrawal 	err = rta_inline_query(IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN,
41405b12700SHemant Agrawal 			       MIN_JOB_DESC_SIZE,
41505b12700SHemant Agrawal 			       (unsigned int *)cdb->sh_desc,
41605b12700SHemant Agrawal 			       &cdb->sh_desc[2], 2);
41705b12700SHemant Agrawal 
41805b12700SHemant Agrawal 	if (err < 0) {
41905b12700SHemant Agrawal 		DPAA_SEC_ERR("Crypto: Incorrect key lengths");
42005b12700SHemant Agrawal 		return err;
42105b12700SHemant Agrawal 	}
42205b12700SHemant Agrawal 	if (cdb->sh_desc[2] & 1)
42305b12700SHemant Agrawal 		cipherdata.key_type = RTA_DATA_IMM;
42405b12700SHemant Agrawal 	else {
42505b12700SHemant Agrawal 		cipherdata.key = (size_t)dpaa_mem_vtop(
42605b12700SHemant Agrawal 					(void *)(size_t)cipherdata.key);
42705b12700SHemant Agrawal 		cipherdata.key_type = RTA_DATA_PTR;
42805b12700SHemant Agrawal 	}
42905b12700SHemant Agrawal 	if (cdb->sh_desc[2] & (1<<1))
43005b12700SHemant Agrawal 		authdata.key_type = RTA_DATA_IMM;
43105b12700SHemant Agrawal 	else {
43205b12700SHemant Agrawal 		authdata.key = (size_t)dpaa_mem_vtop(
43305b12700SHemant Agrawal 					(void *)(size_t)authdata.key);
43405b12700SHemant Agrawal 		authdata.key_type = RTA_DATA_PTR;
43505b12700SHemant Agrawal 	}
43605b12700SHemant Agrawal 
43705b12700SHemant Agrawal 	cdb->sh_desc[0] = 0;
43805b12700SHemant Agrawal 	cdb->sh_desc[1] = 0;
43905b12700SHemant Agrawal 	cdb->sh_desc[2] = 0;
44005b12700SHemant Agrawal 	if (ses->dir == DIR_ENC) {
44105b12700SHemant Agrawal 		shared_desc_len = cnstr_shdsc_ipsec_new_encap(
44205b12700SHemant Agrawal 				cdb->sh_desc,
44305b12700SHemant Agrawal 				true, swap, SHR_SERIAL,
44405b12700SHemant Agrawal 				&ses->encap_pdb,
44505b12700SHemant Agrawal 				(uint8_t *)&ses->ip4_hdr,
44605b12700SHemant Agrawal 				&cipherdata, &authdata);
44705b12700SHemant Agrawal 	} else if (ses->dir == DIR_DEC) {
44805b12700SHemant Agrawal 		shared_desc_len = cnstr_shdsc_ipsec_new_decap(
44905b12700SHemant Agrawal 				cdb->sh_desc,
45005b12700SHemant Agrawal 				true, swap, SHR_SERIAL,
45105b12700SHemant Agrawal 				&ses->decap_pdb,
45205b12700SHemant Agrawal 				&cipherdata, &authdata);
45305b12700SHemant Agrawal 	}
45405b12700SHemant Agrawal 	return shared_desc_len;
45505b12700SHemant Agrawal }
456c3e85bdcSAkhil Goyal 
457c3e85bdcSAkhil Goyal /* prepare command block of the session */
458c3e85bdcSAkhil Goyal static int
459c3e85bdcSAkhil Goyal dpaa_sec_prep_cdb(dpaa_sec_session *ses)
460c3e85bdcSAkhil Goyal {
461c3e85bdcSAkhil Goyal 	struct alginfo alginfo_c = {0}, alginfo_a = {0}, alginfo = {0};
46222788c2cSSunil Kumar Kori 	int32_t shared_desc_len = 0;
463e79416d1SHemant Agrawal 	struct sec_cdb *cdb = &ses->cdb;
464c3e85bdcSAkhil Goyal 	int err;
465c3e85bdcSAkhil Goyal #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
466c3e85bdcSAkhil Goyal 	int swap = false;
467c3e85bdcSAkhil Goyal #else
468c3e85bdcSAkhil Goyal 	int swap = true;
469c3e85bdcSAkhil Goyal #endif
470c3e85bdcSAkhil Goyal 
471c3e85bdcSAkhil Goyal 	memset(cdb, 0, sizeof(struct sec_cdb));
472c3e85bdcSAkhil Goyal 
47305b12700SHemant Agrawal 	if (is_proto_ipsec(ses)) {
47405b12700SHemant Agrawal 		shared_desc_len = dpaa_sec_prep_ipsec_cdb(ses);
47505b12700SHemant Agrawal 	} else if (is_cipher_only(ses)) {
476c3e85bdcSAkhil Goyal 		caam_cipher_alg(ses, &alginfo_c);
477c3e85bdcSAkhil Goyal 		if (alginfo_c.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
478f163231eSHemant Agrawal 			DPAA_SEC_ERR("not supported cipher alg");
479c3e85bdcSAkhil Goyal 			return -ENOTSUP;
480c3e85bdcSAkhil Goyal 		}
481c3e85bdcSAkhil Goyal 
4820e5607e4SHemant Agrawal 		alginfo_c.key = (size_t)ses->cipher_key.data;
483c3e85bdcSAkhil Goyal 		alginfo_c.keylen = ses->cipher_key.length;
484c3e85bdcSAkhil Goyal 		alginfo_c.key_enc_flags = 0;
485c3e85bdcSAkhil Goyal 		alginfo_c.key_type = RTA_DATA_IMM;
486c3e85bdcSAkhil Goyal 
487c3e85bdcSAkhil Goyal 		shared_desc_len = cnstr_shdsc_blkcipher(
488c3e85bdcSAkhil Goyal 						cdb->sh_desc, true,
489c3e85bdcSAkhil Goyal 						swap, &alginfo_c,
490c3e85bdcSAkhil Goyal 						NULL,
491c3e85bdcSAkhil Goyal 						ses->iv.length,
492c3e85bdcSAkhil Goyal 						ses->dir);
493c3e85bdcSAkhil Goyal 	} else if (is_auth_only(ses)) {
494c3e85bdcSAkhil Goyal 		caam_auth_alg(ses, &alginfo_a);
495c3e85bdcSAkhil Goyal 		if (alginfo_a.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
496f163231eSHemant Agrawal 			DPAA_SEC_ERR("not supported auth alg");
497c3e85bdcSAkhil Goyal 			return -ENOTSUP;
498c3e85bdcSAkhil Goyal 		}
499c3e85bdcSAkhil Goyal 
5000e5607e4SHemant Agrawal 		alginfo_a.key = (size_t)ses->auth_key.data;
501c3e85bdcSAkhil Goyal 		alginfo_a.keylen = ses->auth_key.length;
502c3e85bdcSAkhil Goyal 		alginfo_a.key_enc_flags = 0;
503c3e85bdcSAkhil Goyal 		alginfo_a.key_type = RTA_DATA_IMM;
504c3e85bdcSAkhil Goyal 
505c3e85bdcSAkhil Goyal 		shared_desc_len = cnstr_shdsc_hmac(cdb->sh_desc, true,
506c3e85bdcSAkhil Goyal 						   swap, &alginfo_a,
507c3e85bdcSAkhil Goyal 						   !ses->dir,
508c3e85bdcSAkhil Goyal 						   ses->digest_length);
509c3e85bdcSAkhil Goyal 	} else if (is_aead(ses)) {
510c3e85bdcSAkhil Goyal 		caam_aead_alg(ses, &alginfo);
511c3e85bdcSAkhil Goyal 		if (alginfo.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
512f163231eSHemant Agrawal 			DPAA_SEC_ERR("not supported aead alg");
513c3e85bdcSAkhil Goyal 			return -ENOTSUP;
514c3e85bdcSAkhil Goyal 		}
5150e5607e4SHemant Agrawal 		alginfo.key = (size_t)ses->aead_key.data;
516c3e85bdcSAkhil Goyal 		alginfo.keylen = ses->aead_key.length;
517c3e85bdcSAkhil Goyal 		alginfo.key_enc_flags = 0;
518c3e85bdcSAkhil Goyal 		alginfo.key_type = RTA_DATA_IMM;
519c3e85bdcSAkhil Goyal 
520c3e85bdcSAkhil Goyal 		if (ses->dir == DIR_ENC)
521c3e85bdcSAkhil Goyal 			shared_desc_len = cnstr_shdsc_gcm_encap(
522c3e85bdcSAkhil Goyal 					cdb->sh_desc, true, swap,
523c3e85bdcSAkhil Goyal 					&alginfo,
524c3e85bdcSAkhil Goyal 					ses->iv.length,
525c3e85bdcSAkhil Goyal 					ses->digest_length);
526c3e85bdcSAkhil Goyal 		else
527c3e85bdcSAkhil Goyal 			shared_desc_len = cnstr_shdsc_gcm_decap(
528c3e85bdcSAkhil Goyal 					cdb->sh_desc, true, swap,
529c3e85bdcSAkhil Goyal 					&alginfo,
530c3e85bdcSAkhil Goyal 					ses->iv.length,
531c3e85bdcSAkhil Goyal 					ses->digest_length);
532c3e85bdcSAkhil Goyal 	} else {
533c3e85bdcSAkhil Goyal 		caam_cipher_alg(ses, &alginfo_c);
534c3e85bdcSAkhil Goyal 		if (alginfo_c.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
535f163231eSHemant Agrawal 			DPAA_SEC_ERR("not supported cipher alg");
536c3e85bdcSAkhil Goyal 			return -ENOTSUP;
537c3e85bdcSAkhil Goyal 		}
538c3e85bdcSAkhil Goyal 
5390e5607e4SHemant Agrawal 		alginfo_c.key = (size_t)ses->cipher_key.data;
540c3e85bdcSAkhil Goyal 		alginfo_c.keylen = ses->cipher_key.length;
541c3e85bdcSAkhil Goyal 		alginfo_c.key_enc_flags = 0;
542c3e85bdcSAkhil Goyal 		alginfo_c.key_type = RTA_DATA_IMM;
543c3e85bdcSAkhil Goyal 
544c3e85bdcSAkhil Goyal 		caam_auth_alg(ses, &alginfo_a);
545c3e85bdcSAkhil Goyal 		if (alginfo_a.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
546f163231eSHemant Agrawal 			DPAA_SEC_ERR("not supported auth alg");
547c3e85bdcSAkhil Goyal 			return -ENOTSUP;
548c3e85bdcSAkhil Goyal 		}
549c3e85bdcSAkhil Goyal 
5500e5607e4SHemant Agrawal 		alginfo_a.key = (size_t)ses->auth_key.data;
551c3e85bdcSAkhil Goyal 		alginfo_a.keylen = ses->auth_key.length;
552c3e85bdcSAkhil Goyal 		alginfo_a.key_enc_flags = 0;
553c3e85bdcSAkhil Goyal 		alginfo_a.key_type = RTA_DATA_IMM;
554c3e85bdcSAkhil Goyal 
555c3e85bdcSAkhil Goyal 		cdb->sh_desc[0] = alginfo_c.keylen;
556c3e85bdcSAkhil Goyal 		cdb->sh_desc[1] = alginfo_a.keylen;
557c3e85bdcSAkhil Goyal 		err = rta_inline_query(IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN,
558c3e85bdcSAkhil Goyal 				       MIN_JOB_DESC_SIZE,
559c3e85bdcSAkhil Goyal 				       (unsigned int *)cdb->sh_desc,
560c3e85bdcSAkhil Goyal 				       &cdb->sh_desc[2], 2);
561c3e85bdcSAkhil Goyal 
562c3e85bdcSAkhil Goyal 		if (err < 0) {
563f163231eSHemant Agrawal 			DPAA_SEC_ERR("Crypto: Incorrect key lengths");
564c3e85bdcSAkhil Goyal 			return err;
565c3e85bdcSAkhil Goyal 		}
566c3e85bdcSAkhil Goyal 		if (cdb->sh_desc[2] & 1)
567c3e85bdcSAkhil Goyal 			alginfo_c.key_type = RTA_DATA_IMM;
568c3e85bdcSAkhil Goyal 		else {
5690e5607e4SHemant Agrawal 			alginfo_c.key = (size_t)dpaa_mem_vtop(
5700e5607e4SHemant Agrawal 						(void *)(size_t)alginfo_c.key);
571c3e85bdcSAkhil Goyal 			alginfo_c.key_type = RTA_DATA_PTR;
572c3e85bdcSAkhil Goyal 		}
573c3e85bdcSAkhil Goyal 		if (cdb->sh_desc[2] & (1<<1))
574c3e85bdcSAkhil Goyal 			alginfo_a.key_type = RTA_DATA_IMM;
575c3e85bdcSAkhil Goyal 		else {
5760e5607e4SHemant Agrawal 			alginfo_a.key = (size_t)dpaa_mem_vtop(
5770e5607e4SHemant Agrawal 						(void *)(size_t)alginfo_a.key);
578c3e85bdcSAkhil Goyal 			alginfo_a.key_type = RTA_DATA_PTR;
579c3e85bdcSAkhil Goyal 		}
580c3e85bdcSAkhil Goyal 		cdb->sh_desc[0] = 0;
581c3e85bdcSAkhil Goyal 		cdb->sh_desc[1] = 0;
582c3e85bdcSAkhil Goyal 		cdb->sh_desc[2] = 0;
5831f14d500SAkhil Goyal 		/* Auth_only_len is set as 0 here and it will be
5841f14d500SAkhil Goyal 		 * overwritten in fd for each packet.
585c3e85bdcSAkhil Goyal 		 */
586c3e85bdcSAkhil Goyal 		shared_desc_len = cnstr_shdsc_authenc(cdb->sh_desc,
587c3e85bdcSAkhil Goyal 				true, swap, &alginfo_c, &alginfo_a,
588c3e85bdcSAkhil Goyal 				ses->iv.length, 0,
589c3e85bdcSAkhil Goyal 				ses->digest_length, ses->dir);
590c3e85bdcSAkhil Goyal 	}
59122788c2cSSunil Kumar Kori 
59222788c2cSSunil Kumar Kori 	if (shared_desc_len < 0) {
593f163231eSHemant Agrawal 		DPAA_SEC_ERR("error in preparing command block");
59422788c2cSSunil Kumar Kori 		return shared_desc_len;
59522788c2cSSunil Kumar Kori 	}
59622788c2cSSunil Kumar Kori 
597c3e85bdcSAkhil Goyal 	cdb->sh_hdr.hi.field.idlen = shared_desc_len;
598c3e85bdcSAkhil Goyal 	cdb->sh_hdr.hi.word = rte_cpu_to_be_32(cdb->sh_hdr.hi.word);
599c3e85bdcSAkhil Goyal 	cdb->sh_hdr.lo.word = rte_cpu_to_be_32(cdb->sh_hdr.lo.word);
600c3e85bdcSAkhil Goyal 
601c3e85bdcSAkhil Goyal 	return 0;
602c3e85bdcSAkhil Goyal }
603c3e85bdcSAkhil Goyal 
604c3e85bdcSAkhil Goyal /* qp is lockless, should be accessed by only one thread */
605c3e85bdcSAkhil Goyal static int
606c3e85bdcSAkhil Goyal dpaa_sec_deq(struct dpaa_sec_qp *qp, struct rte_crypto_op **ops, int nb_ops)
607c3e85bdcSAkhil Goyal {
608c3e85bdcSAkhil Goyal 	struct qman_fq *fq;
6099a984458SAkhil Goyal 	unsigned int pkts = 0;
610f40d5a53SNipun Gupta 	int num_rx_bufs, ret;
6119a984458SAkhil Goyal 	struct qm_dqrr_entry *dq;
612f40d5a53SNipun Gupta 	uint32_t vdqcr_flags = 0;
613c3e85bdcSAkhil Goyal 
614c3e85bdcSAkhil Goyal 	fq = &qp->outq;
615f40d5a53SNipun Gupta 	/*
616f40d5a53SNipun Gupta 	 * Until request for four buffers, we provide exact number of buffers.
617f40d5a53SNipun Gupta 	 * Otherwise we do not set the QM_VDQCR_EXACT flag.
618f40d5a53SNipun Gupta 	 * Not setting QM_VDQCR_EXACT flag can provide two more buffers than
619f40d5a53SNipun Gupta 	 * requested, so we request two less in this case.
620f40d5a53SNipun Gupta 	 */
621f40d5a53SNipun Gupta 	if (nb_ops < 4) {
622f40d5a53SNipun Gupta 		vdqcr_flags = QM_VDQCR_EXACT;
623f40d5a53SNipun Gupta 		num_rx_bufs = nb_ops;
624f40d5a53SNipun Gupta 	} else {
625f40d5a53SNipun Gupta 		num_rx_bufs = nb_ops > DPAA_MAX_DEQUEUE_NUM_FRAMES ?
626f40d5a53SNipun Gupta 			(DPAA_MAX_DEQUEUE_NUM_FRAMES - 2) : (nb_ops - 2);
627f40d5a53SNipun Gupta 	}
628f40d5a53SNipun Gupta 	ret = qman_set_vdq(fq, num_rx_bufs, vdqcr_flags);
6299a984458SAkhil Goyal 	if (ret)
6309a984458SAkhil Goyal 		return 0;
631c3e85bdcSAkhil Goyal 
6329a984458SAkhil Goyal 	do {
6339a984458SAkhil Goyal 		const struct qm_fd *fd;
6349a984458SAkhil Goyal 		struct dpaa_sec_job *job;
6359a984458SAkhil Goyal 		struct dpaa_sec_op_ctx *ctx;
6369a984458SAkhil Goyal 		struct rte_crypto_op *op;
637c3e85bdcSAkhil Goyal 
6389a984458SAkhil Goyal 		dq = qman_dequeue(fq);
6399a984458SAkhil Goyal 		if (!dq)
6409a984458SAkhil Goyal 			continue;
6419a984458SAkhil Goyal 
6429a984458SAkhil Goyal 		fd = &dq->fd;
6439a984458SAkhil Goyal 		/* sg is embedded in an op ctx,
6449a984458SAkhil Goyal 		 * sg[0] is for output
6459a984458SAkhil Goyal 		 * sg[1] for input
6469a984458SAkhil Goyal 		 */
6479a984458SAkhil Goyal 		job = dpaa_mem_ptov(qm_fd_addr_get64(fd));
6489a984458SAkhil Goyal 
6499a984458SAkhil Goyal 		ctx = container_of(job, struct dpaa_sec_op_ctx, job);
6509a984458SAkhil Goyal 		ctx->fd_status = fd->status;
6519a984458SAkhil Goyal 		op = ctx->op;
6529a984458SAkhil Goyal 		if (op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
6539a984458SAkhil Goyal 			struct qm_sg_entry *sg_out;
6549a984458SAkhil Goyal 			uint32_t len;
6559a984458SAkhil Goyal 
6569a984458SAkhil Goyal 			sg_out = &job->sg[0];
6579a984458SAkhil Goyal 			hw_sg_to_cpu(sg_out);
6589a984458SAkhil Goyal 			len = sg_out->length;
6599a984458SAkhil Goyal 			op->sym->m_src->pkt_len = len;
6609a984458SAkhil Goyal 			op->sym->m_src->data_len = len;
6619a984458SAkhil Goyal 		}
6629a984458SAkhil Goyal 		if (!ctx->fd_status) {
6639a984458SAkhil Goyal 			op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
6649a984458SAkhil Goyal 		} else {
665f163231eSHemant Agrawal 			DPAA_SEC_DP_WARN("SEC return err:0x%x", ctx->fd_status);
6669a984458SAkhil Goyal 			op->status = RTE_CRYPTO_OP_STATUS_ERROR;
6679a984458SAkhil Goyal 		}
6689a984458SAkhil Goyal 		ops[pkts++] = op;
6699a984458SAkhil Goyal 
6709a984458SAkhil Goyal 		/* report op status to sym->op and then free the ctx memeory */
6719a984458SAkhil Goyal 		rte_mempool_put(ctx->ctx_pool, (void *)ctx);
6729a984458SAkhil Goyal 
6739a984458SAkhil Goyal 		qman_dqrr_consume(fq, dq);
6749a984458SAkhil Goyal 	} while (fq->flags & QMAN_FQ_STATE_VDQCR);
6759a984458SAkhil Goyal 
6769a984458SAkhil Goyal 	return pkts;
677c3e85bdcSAkhil Goyal }
678c3e85bdcSAkhil Goyal 
679a74af788SAkhil Goyal static inline struct dpaa_sec_job *
680a74af788SAkhil Goyal build_auth_only_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
681a74af788SAkhil Goyal {
682a74af788SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
683a74af788SAkhil Goyal 	struct rte_mbuf *mbuf = sym->m_src;
684a74af788SAkhil Goyal 	struct dpaa_sec_job *cf;
685a74af788SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
686a74af788SAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
687a74af788SAkhil Goyal 	phys_addr_t start_addr;
688a74af788SAkhil Goyal 	uint8_t *old_digest, extra_segs;
689a74af788SAkhil Goyal 
690a74af788SAkhil Goyal 	if (is_decode(ses))
691a74af788SAkhil Goyal 		extra_segs = 3;
692a74af788SAkhil Goyal 	else
693a74af788SAkhil Goyal 		extra_segs = 2;
694a74af788SAkhil Goyal 
695a74af788SAkhil Goyal 	if ((mbuf->nb_segs + extra_segs) > MAX_SG_ENTRIES) {
696f163231eSHemant Agrawal 		DPAA_SEC_DP_ERR("Auth: Max sec segs supported is %d",
697a74af788SAkhil Goyal 				MAX_SG_ENTRIES);
698a74af788SAkhil Goyal 		return NULL;
699a74af788SAkhil Goyal 	}
700a74af788SAkhil Goyal 	ctx = dpaa_sec_alloc_ctx(ses);
701a74af788SAkhil Goyal 	if (!ctx)
702a74af788SAkhil Goyal 		return NULL;
703a74af788SAkhil Goyal 
704a74af788SAkhil Goyal 	cf = &ctx->job;
705a74af788SAkhil Goyal 	ctx->op = op;
706a74af788SAkhil Goyal 	old_digest = ctx->digest;
707a74af788SAkhil Goyal 
708a74af788SAkhil Goyal 	/* output */
709a74af788SAkhil Goyal 	out_sg = &cf->sg[0];
710a74af788SAkhil Goyal 	qm_sg_entry_set64(out_sg, sym->auth.digest.phys_addr);
711a74af788SAkhil Goyal 	out_sg->length = ses->digest_length;
712a74af788SAkhil Goyal 	cpu_to_hw_sg(out_sg);
713a74af788SAkhil Goyal 
714a74af788SAkhil Goyal 	/* input */
715a74af788SAkhil Goyal 	in_sg = &cf->sg[1];
716a74af788SAkhil Goyal 	/* need to extend the input to a compound frame */
717a74af788SAkhil Goyal 	in_sg->extension = 1;
718a74af788SAkhil Goyal 	in_sg->final = 1;
719a74af788SAkhil Goyal 	in_sg->length = sym->auth.data.length;
72095456e89SShreyansh Jain 	qm_sg_entry_set64(in_sg, dpaa_mem_vtop(&cf->sg[2]));
721a74af788SAkhil Goyal 
722a74af788SAkhil Goyal 	/* 1st seg */
723a74af788SAkhil Goyal 	sg = in_sg + 1;
724a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
725a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->auth.data.offset;
726a74af788SAkhil Goyal 	sg->offset = sym->auth.data.offset;
727a74af788SAkhil Goyal 
728a74af788SAkhil Goyal 	/* Successive segs */
729a74af788SAkhil Goyal 	mbuf = mbuf->next;
730a74af788SAkhil Goyal 	while (mbuf) {
731a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
732a74af788SAkhil Goyal 		sg++;
733a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
734a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
735a74af788SAkhil Goyal 		mbuf = mbuf->next;
736a74af788SAkhil Goyal 	}
737a74af788SAkhil Goyal 
738a74af788SAkhil Goyal 	if (is_decode(ses)) {
739a74af788SAkhil Goyal 		/* Digest verification case */
740a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
741a74af788SAkhil Goyal 		sg++;
742a74af788SAkhil Goyal 		rte_memcpy(old_digest, sym->auth.digest.data,
743a74af788SAkhil Goyal 				ses->digest_length);
74495456e89SShreyansh Jain 		start_addr = dpaa_mem_vtop(old_digest);
745a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, start_addr);
746a74af788SAkhil Goyal 		sg->length = ses->digest_length;
747a74af788SAkhil Goyal 		in_sg->length += ses->digest_length;
748a74af788SAkhil Goyal 	} else {
749a74af788SAkhil Goyal 		/* Digest calculation case */
750a74af788SAkhil Goyal 		sg->length -= ses->digest_length;
751a74af788SAkhil Goyal 	}
752a74af788SAkhil Goyal 	sg->final = 1;
753a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
754a74af788SAkhil Goyal 	cpu_to_hw_sg(in_sg);
755a74af788SAkhil Goyal 
756a74af788SAkhil Goyal 	return cf;
757a74af788SAkhil Goyal }
758a74af788SAkhil Goyal 
759c3e85bdcSAkhil Goyal /**
760c3e85bdcSAkhil Goyal  * packet looks like:
761c3e85bdcSAkhil Goyal  *		|<----data_len------->|
762c3e85bdcSAkhil Goyal  *    |ip_header|ah_header|icv|payload|
763c3e85bdcSAkhil Goyal  *              ^
764c3e85bdcSAkhil Goyal  *		|
765c3e85bdcSAkhil Goyal  *	   mbuf->pkt.data
766c3e85bdcSAkhil Goyal  */
767c3e85bdcSAkhil Goyal static inline struct dpaa_sec_job *
768c3e85bdcSAkhil Goyal build_auth_only(struct rte_crypto_op *op, dpaa_sec_session *ses)
769c3e85bdcSAkhil Goyal {
770c3e85bdcSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
771c3e85bdcSAkhil Goyal 	struct rte_mbuf *mbuf = sym->m_src;
772c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
773c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
774c3e85bdcSAkhil Goyal 	struct qm_sg_entry *sg;
775c4509373SSantosh Shukla 	rte_iova_t start_addr;
776c3e85bdcSAkhil Goyal 	uint8_t *old_digest;
777c3e85bdcSAkhil Goyal 
778c3e85bdcSAkhil Goyal 	ctx = dpaa_sec_alloc_ctx(ses);
779c3e85bdcSAkhil Goyal 	if (!ctx)
780c3e85bdcSAkhil Goyal 		return NULL;
781c3e85bdcSAkhil Goyal 
782c3e85bdcSAkhil Goyal 	cf = &ctx->job;
783c3e85bdcSAkhil Goyal 	ctx->op = op;
784c3e85bdcSAkhil Goyal 	old_digest = ctx->digest;
785c3e85bdcSAkhil Goyal 
786bfa9a8a4SThomas Monjalon 	start_addr = rte_pktmbuf_iova(mbuf);
787c3e85bdcSAkhil Goyal 	/* output */
788c3e85bdcSAkhil Goyal 	sg = &cf->sg[0];
789c3e85bdcSAkhil Goyal 	qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
790c3e85bdcSAkhil Goyal 	sg->length = ses->digest_length;
791c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
792c3e85bdcSAkhil Goyal 
793c3e85bdcSAkhil Goyal 	/* input */
794c3e85bdcSAkhil Goyal 	sg = &cf->sg[1];
795c3e85bdcSAkhil Goyal 	if (is_decode(ses)) {
796c3e85bdcSAkhil Goyal 		/* need to extend the input to a compound frame */
797c3e85bdcSAkhil Goyal 		sg->extension = 1;
79895456e89SShreyansh Jain 		qm_sg_entry_set64(sg, dpaa_mem_vtop(&cf->sg[2]));
799c3e85bdcSAkhil Goyal 		sg->length = sym->auth.data.length + ses->digest_length;
800c3e85bdcSAkhil Goyal 		sg->final = 1;
801c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
802c3e85bdcSAkhil Goyal 
803c3e85bdcSAkhil Goyal 		sg = &cf->sg[2];
804c3e85bdcSAkhil Goyal 		/* hash result or digest, save digest first */
805c3e85bdcSAkhil Goyal 		rte_memcpy(old_digest, sym->auth.digest.data,
806c3e85bdcSAkhil Goyal 			   ses->digest_length);
807c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, start_addr + sym->auth.data.offset);
808c3e85bdcSAkhil Goyal 		sg->length = sym->auth.data.length;
809c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
810c3e85bdcSAkhil Goyal 
811c3e85bdcSAkhil Goyal 		/* let's check digest by hw */
81295456e89SShreyansh Jain 		start_addr = dpaa_mem_vtop(old_digest);
813c3e85bdcSAkhil Goyal 		sg++;
814c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, start_addr);
815c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
816c3e85bdcSAkhil Goyal 		sg->final = 1;
817c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
818c3e85bdcSAkhil Goyal 	} else {
819c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, start_addr + sym->auth.data.offset);
820c3e85bdcSAkhil Goyal 		sg->length = sym->auth.data.length;
821c3e85bdcSAkhil Goyal 		sg->final = 1;
822c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
823c3e85bdcSAkhil Goyal 	}
824c3e85bdcSAkhil Goyal 
825c3e85bdcSAkhil Goyal 	return cf;
826c3e85bdcSAkhil Goyal }
827c3e85bdcSAkhil Goyal 
828c3e85bdcSAkhil Goyal static inline struct dpaa_sec_job *
829a74af788SAkhil Goyal build_cipher_only_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
830a74af788SAkhil Goyal {
831a74af788SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
832a74af788SAkhil Goyal 	struct dpaa_sec_job *cf;
833a74af788SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
834a74af788SAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
835a74af788SAkhil Goyal 	struct rte_mbuf *mbuf;
836a74af788SAkhil Goyal 	uint8_t req_segs;
837a74af788SAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
838a74af788SAkhil Goyal 			ses->iv.offset);
839a74af788SAkhil Goyal 
840a74af788SAkhil Goyal 	if (sym->m_dst) {
841a74af788SAkhil Goyal 		mbuf = sym->m_dst;
842a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 3;
843a74af788SAkhil Goyal 	} else {
844a74af788SAkhil Goyal 		mbuf = sym->m_src;
845a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs * 2 + 3;
846a74af788SAkhil Goyal 	}
847a74af788SAkhil Goyal 
848a74af788SAkhil Goyal 	if (req_segs > MAX_SG_ENTRIES) {
849f163231eSHemant Agrawal 		DPAA_SEC_DP_ERR("Cipher: Max sec segs supported is %d",
850a74af788SAkhil Goyal 				MAX_SG_ENTRIES);
851a74af788SAkhil Goyal 		return NULL;
852a74af788SAkhil Goyal 	}
853a74af788SAkhil Goyal 
854a74af788SAkhil Goyal 	ctx = dpaa_sec_alloc_ctx(ses);
855a74af788SAkhil Goyal 	if (!ctx)
856a74af788SAkhil Goyal 		return NULL;
857a74af788SAkhil Goyal 
858a74af788SAkhil Goyal 	cf = &ctx->job;
859a74af788SAkhil Goyal 	ctx->op = op;
860a74af788SAkhil Goyal 
861a74af788SAkhil Goyal 	/* output */
862a74af788SAkhil Goyal 	out_sg = &cf->sg[0];
863a74af788SAkhil Goyal 	out_sg->extension = 1;
864a74af788SAkhil Goyal 	out_sg->length = sym->cipher.data.length;
86595456e89SShreyansh Jain 	qm_sg_entry_set64(out_sg, dpaa_mem_vtop(&cf->sg[2]));
866a74af788SAkhil Goyal 	cpu_to_hw_sg(out_sg);
867a74af788SAkhil Goyal 
868a74af788SAkhil Goyal 	/* 1st seg */
869a74af788SAkhil Goyal 	sg = &cf->sg[2];
870a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
871a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->cipher.data.offset;
872a74af788SAkhil Goyal 	sg->offset = sym->cipher.data.offset;
873a74af788SAkhil Goyal 
874a74af788SAkhil Goyal 	/* Successive segs */
875a74af788SAkhil Goyal 	mbuf = mbuf->next;
876a74af788SAkhil Goyal 	while (mbuf) {
877a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
878a74af788SAkhil Goyal 		sg++;
879a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
880a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
881a74af788SAkhil Goyal 		mbuf = mbuf->next;
882a74af788SAkhil Goyal 	}
883a74af788SAkhil Goyal 	sg->final = 1;
884a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
885a74af788SAkhil Goyal 
886a74af788SAkhil Goyal 	/* input */
887a74af788SAkhil Goyal 	mbuf = sym->m_src;
888a74af788SAkhil Goyal 	in_sg = &cf->sg[1];
889a74af788SAkhil Goyal 	in_sg->extension = 1;
890a74af788SAkhil Goyal 	in_sg->final = 1;
891a74af788SAkhil Goyal 	in_sg->length = sym->cipher.data.length + ses->iv.length;
892a74af788SAkhil Goyal 
893a74af788SAkhil Goyal 	sg++;
89495456e89SShreyansh Jain 	qm_sg_entry_set64(in_sg, dpaa_mem_vtop(sg));
895a74af788SAkhil Goyal 	cpu_to_hw_sg(in_sg);
896a74af788SAkhil Goyal 
897a74af788SAkhil Goyal 	/* IV */
898a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
899a74af788SAkhil Goyal 	sg->length = ses->iv.length;
900a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
901a74af788SAkhil Goyal 
902a74af788SAkhil Goyal 	/* 1st seg */
903a74af788SAkhil Goyal 	sg++;
904a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
905a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->cipher.data.offset;
906a74af788SAkhil Goyal 	sg->offset = sym->cipher.data.offset;
907a74af788SAkhil Goyal 
908a74af788SAkhil Goyal 	/* Successive segs */
909a74af788SAkhil Goyal 	mbuf = mbuf->next;
910a74af788SAkhil Goyal 	while (mbuf) {
911a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
912a74af788SAkhil Goyal 		sg++;
913a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
914a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
915a74af788SAkhil Goyal 		mbuf = mbuf->next;
916a74af788SAkhil Goyal 	}
917a74af788SAkhil Goyal 	sg->final = 1;
918a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
919a74af788SAkhil Goyal 
920a74af788SAkhil Goyal 	return cf;
921a74af788SAkhil Goyal }
922a74af788SAkhil Goyal 
923a74af788SAkhil Goyal static inline struct dpaa_sec_job *
924c3e85bdcSAkhil Goyal build_cipher_only(struct rte_crypto_op *op, dpaa_sec_session *ses)
925c3e85bdcSAkhil Goyal {
926c3e85bdcSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
927c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
928c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
929c3e85bdcSAkhil Goyal 	struct qm_sg_entry *sg;
930c4509373SSantosh Shukla 	rte_iova_t src_start_addr, dst_start_addr;
931c3e85bdcSAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
932c3e85bdcSAkhil Goyal 			ses->iv.offset);
933c3e85bdcSAkhil Goyal 
934c3e85bdcSAkhil Goyal 	ctx = dpaa_sec_alloc_ctx(ses);
935c3e85bdcSAkhil Goyal 	if (!ctx)
936c3e85bdcSAkhil Goyal 		return NULL;
937c3e85bdcSAkhil Goyal 
938c3e85bdcSAkhil Goyal 	cf = &ctx->job;
939c3e85bdcSAkhil Goyal 	ctx->op = op;
940a389434eSAlok Makhariya 
941bfa9a8a4SThomas Monjalon 	src_start_addr = rte_pktmbuf_iova(sym->m_src);
942a389434eSAlok Makhariya 
943a389434eSAlok Makhariya 	if (sym->m_dst)
944bfa9a8a4SThomas Monjalon 		dst_start_addr = rte_pktmbuf_iova(sym->m_dst);
945a389434eSAlok Makhariya 	else
946a389434eSAlok Makhariya 		dst_start_addr = src_start_addr;
947c3e85bdcSAkhil Goyal 
948c3e85bdcSAkhil Goyal 	/* output */
949c3e85bdcSAkhil Goyal 	sg = &cf->sg[0];
950a389434eSAlok Makhariya 	qm_sg_entry_set64(sg, dst_start_addr + sym->cipher.data.offset);
951c3e85bdcSAkhil Goyal 	sg->length = sym->cipher.data.length + ses->iv.length;
952c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
953c3e85bdcSAkhil Goyal 
954c3e85bdcSAkhil Goyal 	/* input */
955c3e85bdcSAkhil Goyal 	sg = &cf->sg[1];
956c3e85bdcSAkhil Goyal 
957c3e85bdcSAkhil Goyal 	/* need to extend the input to a compound frame */
958c3e85bdcSAkhil Goyal 	sg->extension = 1;
959c3e85bdcSAkhil Goyal 	sg->final = 1;
960c3e85bdcSAkhil Goyal 	sg->length = sym->cipher.data.length + ses->iv.length;
96195456e89SShreyansh Jain 	qm_sg_entry_set64(sg, dpaa_mem_vtop(&cf->sg[2]));
962c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
963c3e85bdcSAkhil Goyal 
964c3e85bdcSAkhil Goyal 	sg = &cf->sg[2];
965c3e85bdcSAkhil Goyal 	qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
966c3e85bdcSAkhil Goyal 	sg->length = ses->iv.length;
967c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
968c3e85bdcSAkhil Goyal 
969c3e85bdcSAkhil Goyal 	sg++;
970a389434eSAlok Makhariya 	qm_sg_entry_set64(sg, src_start_addr + sym->cipher.data.offset);
971c3e85bdcSAkhil Goyal 	sg->length = sym->cipher.data.length;
972c3e85bdcSAkhil Goyal 	sg->final = 1;
973c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
974c3e85bdcSAkhil Goyal 
975c3e85bdcSAkhil Goyal 	return cf;
976c3e85bdcSAkhil Goyal }
977c3e85bdcSAkhil Goyal 
978c3e85bdcSAkhil Goyal static inline struct dpaa_sec_job *
979a74af788SAkhil Goyal build_cipher_auth_gcm_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
980a74af788SAkhil Goyal {
981a74af788SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
982a74af788SAkhil Goyal 	struct dpaa_sec_job *cf;
983a74af788SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
984a74af788SAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
985a74af788SAkhil Goyal 	struct rte_mbuf *mbuf;
986a74af788SAkhil Goyal 	uint8_t req_segs;
987a74af788SAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
988a74af788SAkhil Goyal 			ses->iv.offset);
989a74af788SAkhil Goyal 
990a74af788SAkhil Goyal 	if (sym->m_dst) {
991a74af788SAkhil Goyal 		mbuf = sym->m_dst;
992a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 4;
993a74af788SAkhil Goyal 	} else {
994a74af788SAkhil Goyal 		mbuf = sym->m_src;
995a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs * 2 + 4;
996a74af788SAkhil Goyal 	}
997a74af788SAkhil Goyal 
998a74af788SAkhil Goyal 	if (ses->auth_only_len)
999a74af788SAkhil Goyal 		req_segs++;
1000a74af788SAkhil Goyal 
1001a74af788SAkhil Goyal 	if (req_segs > MAX_SG_ENTRIES) {
1002f163231eSHemant Agrawal 		DPAA_SEC_DP_ERR("AEAD: Max sec segs supported is %d",
1003a74af788SAkhil Goyal 				MAX_SG_ENTRIES);
1004a74af788SAkhil Goyal 		return NULL;
1005a74af788SAkhil Goyal 	}
1006a74af788SAkhil Goyal 
1007a74af788SAkhil Goyal 	ctx = dpaa_sec_alloc_ctx(ses);
1008a74af788SAkhil Goyal 	if (!ctx)
1009a74af788SAkhil Goyal 		return NULL;
1010a74af788SAkhil Goyal 
1011a74af788SAkhil Goyal 	cf = &ctx->job;
1012a74af788SAkhil Goyal 	ctx->op = op;
1013a74af788SAkhil Goyal 
1014a74af788SAkhil Goyal 	rte_prefetch0(cf->sg);
1015a74af788SAkhil Goyal 
1016a74af788SAkhil Goyal 	/* output */
1017a74af788SAkhil Goyal 	out_sg = &cf->sg[0];
1018a74af788SAkhil Goyal 	out_sg->extension = 1;
1019a74af788SAkhil Goyal 	if (is_encode(ses))
1020a74af788SAkhil Goyal 		out_sg->length = sym->aead.data.length + ses->auth_only_len
1021a74af788SAkhil Goyal 						+ ses->digest_length;
1022a74af788SAkhil Goyal 	else
1023a74af788SAkhil Goyal 		out_sg->length = sym->aead.data.length + ses->auth_only_len;
1024a74af788SAkhil Goyal 
1025a74af788SAkhil Goyal 	/* output sg entries */
1026a74af788SAkhil Goyal 	sg = &cf->sg[2];
102795456e89SShreyansh Jain 	qm_sg_entry_set64(out_sg, dpaa_mem_vtop(sg));
1028a74af788SAkhil Goyal 	cpu_to_hw_sg(out_sg);
1029a74af788SAkhil Goyal 
1030a74af788SAkhil Goyal 	/* 1st seg */
1031a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1032a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->aead.data.offset +
1033a74af788SAkhil Goyal 					ses->auth_only_len;
1034a74af788SAkhil Goyal 	sg->offset = sym->aead.data.offset - ses->auth_only_len;
1035a74af788SAkhil Goyal 
1036a74af788SAkhil Goyal 	/* Successive segs */
1037a74af788SAkhil Goyal 	mbuf = mbuf->next;
1038a74af788SAkhil Goyal 	while (mbuf) {
1039a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1040a74af788SAkhil Goyal 		sg++;
1041a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1042a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1043a74af788SAkhil Goyal 		mbuf = mbuf->next;
1044a74af788SAkhil Goyal 	}
1045a74af788SAkhil Goyal 	sg->length -= ses->digest_length;
1046a74af788SAkhil Goyal 
1047a74af788SAkhil Goyal 	if (is_encode(ses)) {
1048a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1049a74af788SAkhil Goyal 		/* set auth output */
1050a74af788SAkhil Goyal 		sg++;
1051a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, sym->aead.digest.phys_addr);
1052a74af788SAkhil Goyal 		sg->length = ses->digest_length;
1053a74af788SAkhil Goyal 	}
1054a74af788SAkhil Goyal 	sg->final = 1;
1055a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1056a74af788SAkhil Goyal 
1057a74af788SAkhil Goyal 	/* input */
1058a74af788SAkhil Goyal 	mbuf = sym->m_src;
1059a74af788SAkhil Goyal 	in_sg = &cf->sg[1];
1060a74af788SAkhil Goyal 	in_sg->extension = 1;
1061a74af788SAkhil Goyal 	in_sg->final = 1;
1062a74af788SAkhil Goyal 	if (is_encode(ses))
1063a74af788SAkhil Goyal 		in_sg->length = ses->iv.length + sym->aead.data.length
1064a74af788SAkhil Goyal 							+ ses->auth_only_len;
1065a74af788SAkhil Goyal 	else
1066a74af788SAkhil Goyal 		in_sg->length = ses->iv.length + sym->aead.data.length
1067a74af788SAkhil Goyal 				+ ses->auth_only_len + ses->digest_length;
1068a74af788SAkhil Goyal 
1069a74af788SAkhil Goyal 	/* input sg entries */
1070a74af788SAkhil Goyal 	sg++;
107195456e89SShreyansh Jain 	qm_sg_entry_set64(in_sg, dpaa_mem_vtop(sg));
1072a74af788SAkhil Goyal 	cpu_to_hw_sg(in_sg);
1073a74af788SAkhil Goyal 
1074a74af788SAkhil Goyal 	/* 1st seg IV */
1075a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1076a74af788SAkhil Goyal 	sg->length = ses->iv.length;
1077a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1078a74af788SAkhil Goyal 
1079a74af788SAkhil Goyal 	/* 2nd seg auth only */
1080a74af788SAkhil Goyal 	if (ses->auth_only_len) {
1081a74af788SAkhil Goyal 		sg++;
1082a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, dpaa_mem_vtop(sym->aead.aad.data));
1083a74af788SAkhil Goyal 		sg->length = ses->auth_only_len;
1084a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1085a74af788SAkhil Goyal 	}
1086a74af788SAkhil Goyal 
1087a74af788SAkhil Goyal 	/* 3rd seg */
1088a74af788SAkhil Goyal 	sg++;
1089a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1090a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->aead.data.offset;
1091a74af788SAkhil Goyal 	sg->offset = sym->aead.data.offset;
1092a74af788SAkhil Goyal 
1093a74af788SAkhil Goyal 	/* Successive segs */
1094a74af788SAkhil Goyal 	mbuf = mbuf->next;
1095a74af788SAkhil Goyal 	while (mbuf) {
1096a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1097a74af788SAkhil Goyal 		sg++;
1098a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1099a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1100a74af788SAkhil Goyal 		mbuf = mbuf->next;
1101a74af788SAkhil Goyal 	}
1102a74af788SAkhil Goyal 
1103a74af788SAkhil Goyal 	if (is_decode(ses)) {
1104a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1105a74af788SAkhil Goyal 		sg++;
1106a74af788SAkhil Goyal 		memcpy(ctx->digest, sym->aead.digest.data,
1107a74af788SAkhil Goyal 			ses->digest_length);
110895456e89SShreyansh Jain 		qm_sg_entry_set64(sg, dpaa_mem_vtop(ctx->digest));
1109a74af788SAkhil Goyal 		sg->length = ses->digest_length;
1110a74af788SAkhil Goyal 	}
1111a74af788SAkhil Goyal 	sg->final = 1;
1112a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1113a74af788SAkhil Goyal 
1114a74af788SAkhil Goyal 	return cf;
1115a74af788SAkhil Goyal }
1116a74af788SAkhil Goyal 
1117a74af788SAkhil Goyal static inline struct dpaa_sec_job *
1118c3e85bdcSAkhil Goyal build_cipher_auth_gcm(struct rte_crypto_op *op, dpaa_sec_session *ses)
1119c3e85bdcSAkhil Goyal {
1120c3e85bdcSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1121c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
1122c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1123c3e85bdcSAkhil Goyal 	struct qm_sg_entry *sg;
1124c3e85bdcSAkhil Goyal 	uint32_t length = 0;
1125c4509373SSantosh Shukla 	rte_iova_t src_start_addr, dst_start_addr;
1126c3e85bdcSAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1127c3e85bdcSAkhil Goyal 			ses->iv.offset);
1128c3e85bdcSAkhil Goyal 
1129116ff44aSHemant Agrawal 	src_start_addr = sym->m_src->buf_iova + sym->m_src->data_off;
1130a389434eSAlok Makhariya 
1131a389434eSAlok Makhariya 	if (sym->m_dst)
1132116ff44aSHemant Agrawal 		dst_start_addr = sym->m_dst->buf_iova + sym->m_dst->data_off;
1133a389434eSAlok Makhariya 	else
1134a389434eSAlok Makhariya 		dst_start_addr = src_start_addr;
1135c3e85bdcSAkhil Goyal 
1136c3e85bdcSAkhil Goyal 	ctx = dpaa_sec_alloc_ctx(ses);
1137c3e85bdcSAkhil Goyal 	if (!ctx)
1138c3e85bdcSAkhil Goyal 		return NULL;
1139c3e85bdcSAkhil Goyal 
1140c3e85bdcSAkhil Goyal 	cf = &ctx->job;
1141c3e85bdcSAkhil Goyal 	ctx->op = op;
1142c3e85bdcSAkhil Goyal 
1143c3e85bdcSAkhil Goyal 	/* input */
1144c3e85bdcSAkhil Goyal 	rte_prefetch0(cf->sg);
1145c3e85bdcSAkhil Goyal 	sg = &cf->sg[2];
114695456e89SShreyansh Jain 	qm_sg_entry_set64(&cf->sg[1], dpaa_mem_vtop(sg));
1147c3e85bdcSAkhil Goyal 	if (is_encode(ses)) {
1148c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1149c3e85bdcSAkhil Goyal 		sg->length = ses->iv.length;
1150c3e85bdcSAkhil Goyal 		length += sg->length;
1151c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1152c3e85bdcSAkhil Goyal 
1153c3e85bdcSAkhil Goyal 		sg++;
1154c3e85bdcSAkhil Goyal 		if (ses->auth_only_len) {
1155c3e85bdcSAkhil Goyal 			qm_sg_entry_set64(sg,
1156c3e85bdcSAkhil Goyal 					  dpaa_mem_vtop(sym->aead.aad.data));
1157c3e85bdcSAkhil Goyal 			sg->length = ses->auth_only_len;
1158c3e85bdcSAkhil Goyal 			length += sg->length;
1159c3e85bdcSAkhil Goyal 			cpu_to_hw_sg(sg);
1160c3e85bdcSAkhil Goyal 			sg++;
1161c3e85bdcSAkhil Goyal 		}
1162a389434eSAlok Makhariya 		qm_sg_entry_set64(sg, src_start_addr + sym->aead.data.offset);
1163c3e85bdcSAkhil Goyal 		sg->length = sym->aead.data.length;
1164c3e85bdcSAkhil Goyal 		length += sg->length;
1165c3e85bdcSAkhil Goyal 		sg->final = 1;
1166c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1167c3e85bdcSAkhil Goyal 	} else {
1168c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1169c3e85bdcSAkhil Goyal 		sg->length = ses->iv.length;
1170c3e85bdcSAkhil Goyal 		length += sg->length;
1171c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1172c3e85bdcSAkhil Goyal 
1173c3e85bdcSAkhil Goyal 		sg++;
1174c3e85bdcSAkhil Goyal 		if (ses->auth_only_len) {
1175c3e85bdcSAkhil Goyal 			qm_sg_entry_set64(sg,
1176c3e85bdcSAkhil Goyal 					  dpaa_mem_vtop(sym->aead.aad.data));
1177c3e85bdcSAkhil Goyal 			sg->length = ses->auth_only_len;
1178c3e85bdcSAkhil Goyal 			length += sg->length;
1179c3e85bdcSAkhil Goyal 			cpu_to_hw_sg(sg);
1180c3e85bdcSAkhil Goyal 			sg++;
1181c3e85bdcSAkhil Goyal 		}
1182a389434eSAlok Makhariya 		qm_sg_entry_set64(sg, src_start_addr + sym->aead.data.offset);
1183c3e85bdcSAkhil Goyal 		sg->length = sym->aead.data.length;
1184c3e85bdcSAkhil Goyal 		length += sg->length;
1185c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1186c3e85bdcSAkhil Goyal 
1187c3e85bdcSAkhil Goyal 		memcpy(ctx->digest, sym->aead.digest.data,
1188c3e85bdcSAkhil Goyal 		       ses->digest_length);
1189c3e85bdcSAkhil Goyal 		sg++;
1190c3e85bdcSAkhil Goyal 
119195456e89SShreyansh Jain 		qm_sg_entry_set64(sg, dpaa_mem_vtop(ctx->digest));
1192c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
1193c3e85bdcSAkhil Goyal 		length += sg->length;
1194c3e85bdcSAkhil Goyal 		sg->final = 1;
1195c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1196c3e85bdcSAkhil Goyal 	}
1197c3e85bdcSAkhil Goyal 	/* input compound frame */
1198c3e85bdcSAkhil Goyal 	cf->sg[1].length = length;
1199c3e85bdcSAkhil Goyal 	cf->sg[1].extension = 1;
1200c3e85bdcSAkhil Goyal 	cf->sg[1].final = 1;
1201c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(&cf->sg[1]);
1202c3e85bdcSAkhil Goyal 
1203c3e85bdcSAkhil Goyal 	/* output */
1204c3e85bdcSAkhil Goyal 	sg++;
120595456e89SShreyansh Jain 	qm_sg_entry_set64(&cf->sg[0], dpaa_mem_vtop(sg));
1206c3e85bdcSAkhil Goyal 	qm_sg_entry_set64(sg,
1207a389434eSAlok Makhariya 		dst_start_addr + sym->aead.data.offset - ses->auth_only_len);
1208c3e85bdcSAkhil Goyal 	sg->length = sym->aead.data.length + ses->auth_only_len;
1209c3e85bdcSAkhil Goyal 	length = sg->length;
1210c3e85bdcSAkhil Goyal 	if (is_encode(ses)) {
1211c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1212c3e85bdcSAkhil Goyal 		/* set auth output */
1213c3e85bdcSAkhil Goyal 		sg++;
1214c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, sym->aead.digest.phys_addr);
1215c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
1216c3e85bdcSAkhil Goyal 		length += sg->length;
1217c3e85bdcSAkhil Goyal 	}
1218c3e85bdcSAkhil Goyal 	sg->final = 1;
1219c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1220c3e85bdcSAkhil Goyal 
1221c3e85bdcSAkhil Goyal 	/* output compound frame */
1222c3e85bdcSAkhil Goyal 	cf->sg[0].length = length;
1223c3e85bdcSAkhil Goyal 	cf->sg[0].extension = 1;
1224c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(&cf->sg[0]);
1225c3e85bdcSAkhil Goyal 
1226c3e85bdcSAkhil Goyal 	return cf;
1227c3e85bdcSAkhil Goyal }
1228c3e85bdcSAkhil Goyal 
1229c3e85bdcSAkhil Goyal static inline struct dpaa_sec_job *
1230a74af788SAkhil Goyal build_cipher_auth_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
1231a74af788SAkhil Goyal {
1232a74af788SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1233a74af788SAkhil Goyal 	struct dpaa_sec_job *cf;
1234a74af788SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1235a74af788SAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
1236a74af788SAkhil Goyal 	struct rte_mbuf *mbuf;
1237a74af788SAkhil Goyal 	uint8_t req_segs;
1238a74af788SAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1239a74af788SAkhil Goyal 			ses->iv.offset);
1240a74af788SAkhil Goyal 
1241a74af788SAkhil Goyal 	if (sym->m_dst) {
1242a74af788SAkhil Goyal 		mbuf = sym->m_dst;
1243a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 4;
1244a74af788SAkhil Goyal 	} else {
1245a74af788SAkhil Goyal 		mbuf = sym->m_src;
1246a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs * 2 + 4;
1247a74af788SAkhil Goyal 	}
1248a74af788SAkhil Goyal 
1249a74af788SAkhil Goyal 	if (req_segs > MAX_SG_ENTRIES) {
1250f163231eSHemant Agrawal 		DPAA_SEC_DP_ERR("Cipher-Auth: Max sec segs supported is %d",
1251a74af788SAkhil Goyal 				MAX_SG_ENTRIES);
1252a74af788SAkhil Goyal 		return NULL;
1253a74af788SAkhil Goyal 	}
1254a74af788SAkhil Goyal 
1255a74af788SAkhil Goyal 	ctx = dpaa_sec_alloc_ctx(ses);
1256a74af788SAkhil Goyal 	if (!ctx)
1257a74af788SAkhil Goyal 		return NULL;
1258a74af788SAkhil Goyal 
1259a74af788SAkhil Goyal 	cf = &ctx->job;
1260a74af788SAkhil Goyal 	ctx->op = op;
1261a74af788SAkhil Goyal 
1262a74af788SAkhil Goyal 	rte_prefetch0(cf->sg);
1263a74af788SAkhil Goyal 
1264a74af788SAkhil Goyal 	/* output */
1265a74af788SAkhil Goyal 	out_sg = &cf->sg[0];
1266a74af788SAkhil Goyal 	out_sg->extension = 1;
1267a74af788SAkhil Goyal 	if (is_encode(ses))
1268a74af788SAkhil Goyal 		out_sg->length = sym->auth.data.length + ses->digest_length;
1269a74af788SAkhil Goyal 	else
1270a74af788SAkhil Goyal 		out_sg->length = sym->auth.data.length;
1271a74af788SAkhil Goyal 
1272a74af788SAkhil Goyal 	/* output sg entries */
1273a74af788SAkhil Goyal 	sg = &cf->sg[2];
127495456e89SShreyansh Jain 	qm_sg_entry_set64(out_sg, dpaa_mem_vtop(sg));
1275a74af788SAkhil Goyal 	cpu_to_hw_sg(out_sg);
1276a74af788SAkhil Goyal 
1277a74af788SAkhil Goyal 	/* 1st seg */
1278a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1279a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->auth.data.offset;
1280a74af788SAkhil Goyal 	sg->offset = sym->auth.data.offset;
1281a74af788SAkhil Goyal 
1282a74af788SAkhil Goyal 	/* Successive segs */
1283a74af788SAkhil Goyal 	mbuf = mbuf->next;
1284a74af788SAkhil Goyal 	while (mbuf) {
1285a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1286a74af788SAkhil Goyal 		sg++;
1287a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1288a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1289a74af788SAkhil Goyal 		mbuf = mbuf->next;
1290a74af788SAkhil Goyal 	}
1291a74af788SAkhil Goyal 	sg->length -= ses->digest_length;
1292a74af788SAkhil Goyal 
1293a74af788SAkhil Goyal 	if (is_encode(ses)) {
1294a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1295a74af788SAkhil Goyal 		/* set auth output */
1296a74af788SAkhil Goyal 		sg++;
1297a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
1298a74af788SAkhil Goyal 		sg->length = ses->digest_length;
1299a74af788SAkhil Goyal 	}
1300a74af788SAkhil Goyal 	sg->final = 1;
1301a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1302a74af788SAkhil Goyal 
1303a74af788SAkhil Goyal 	/* input */
1304a74af788SAkhil Goyal 	mbuf = sym->m_src;
1305a74af788SAkhil Goyal 	in_sg = &cf->sg[1];
1306a74af788SAkhil Goyal 	in_sg->extension = 1;
1307a74af788SAkhil Goyal 	in_sg->final = 1;
1308a74af788SAkhil Goyal 	if (is_encode(ses))
1309a74af788SAkhil Goyal 		in_sg->length = ses->iv.length + sym->auth.data.length;
1310a74af788SAkhil Goyal 	else
1311a74af788SAkhil Goyal 		in_sg->length = ses->iv.length + sym->auth.data.length
1312a74af788SAkhil Goyal 						+ ses->digest_length;
1313a74af788SAkhil Goyal 
1314a74af788SAkhil Goyal 	/* input sg entries */
1315a74af788SAkhil Goyal 	sg++;
131695456e89SShreyansh Jain 	qm_sg_entry_set64(in_sg, dpaa_mem_vtop(sg));
1317a74af788SAkhil Goyal 	cpu_to_hw_sg(in_sg);
1318a74af788SAkhil Goyal 
1319a74af788SAkhil Goyal 	/* 1st seg IV */
1320a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1321a74af788SAkhil Goyal 	sg->length = ses->iv.length;
1322a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1323a74af788SAkhil Goyal 
1324a74af788SAkhil Goyal 	/* 2nd seg */
1325a74af788SAkhil Goyal 	sg++;
1326a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1327a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->auth.data.offset;
1328a74af788SAkhil Goyal 	sg->offset = sym->auth.data.offset;
1329a74af788SAkhil Goyal 
1330a74af788SAkhil Goyal 	/* Successive segs */
1331a74af788SAkhil Goyal 	mbuf = mbuf->next;
1332a74af788SAkhil Goyal 	while (mbuf) {
1333a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1334a74af788SAkhil Goyal 		sg++;
1335a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1336a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1337a74af788SAkhil Goyal 		mbuf = mbuf->next;
1338a74af788SAkhil Goyal 	}
1339a74af788SAkhil Goyal 
1340a74af788SAkhil Goyal 	sg->length -= ses->digest_length;
1341a74af788SAkhil Goyal 	if (is_decode(ses)) {
1342a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1343a74af788SAkhil Goyal 		sg++;
1344a74af788SAkhil Goyal 		memcpy(ctx->digest, sym->auth.digest.data,
1345a74af788SAkhil Goyal 			ses->digest_length);
134695456e89SShreyansh Jain 		qm_sg_entry_set64(sg, dpaa_mem_vtop(ctx->digest));
1347a74af788SAkhil Goyal 		sg->length = ses->digest_length;
1348a74af788SAkhil Goyal 	}
1349a74af788SAkhil Goyal 	sg->final = 1;
1350a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1351a74af788SAkhil Goyal 
1352a74af788SAkhil Goyal 	return cf;
1353a74af788SAkhil Goyal }
1354a74af788SAkhil Goyal 
1355a74af788SAkhil Goyal static inline struct dpaa_sec_job *
1356c3e85bdcSAkhil Goyal build_cipher_auth(struct rte_crypto_op *op, dpaa_sec_session *ses)
1357c3e85bdcSAkhil Goyal {
1358c3e85bdcSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1359c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
1360c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1361c3e85bdcSAkhil Goyal 	struct qm_sg_entry *sg;
1362c4509373SSantosh Shukla 	rte_iova_t src_start_addr, dst_start_addr;
1363c3e85bdcSAkhil Goyal 	uint32_t length = 0;
1364c3e85bdcSAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1365c3e85bdcSAkhil Goyal 			ses->iv.offset);
1366c3e85bdcSAkhil Goyal 
1367455da545SSantosh Shukla 	src_start_addr = sym->m_src->buf_iova + sym->m_src->data_off;
1368a389434eSAlok Makhariya 	if (sym->m_dst)
1369455da545SSantosh Shukla 		dst_start_addr = sym->m_dst->buf_iova + sym->m_dst->data_off;
1370a389434eSAlok Makhariya 	else
1371a389434eSAlok Makhariya 		dst_start_addr = src_start_addr;
1372c3e85bdcSAkhil Goyal 
1373c3e85bdcSAkhil Goyal 	ctx = dpaa_sec_alloc_ctx(ses);
1374c3e85bdcSAkhil Goyal 	if (!ctx)
1375c3e85bdcSAkhil Goyal 		return NULL;
1376c3e85bdcSAkhil Goyal 
1377c3e85bdcSAkhil Goyal 	cf = &ctx->job;
1378c3e85bdcSAkhil Goyal 	ctx->op = op;
1379c3e85bdcSAkhil Goyal 
1380c3e85bdcSAkhil Goyal 	/* input */
1381c3e85bdcSAkhil Goyal 	rte_prefetch0(cf->sg);
1382c3e85bdcSAkhil Goyal 	sg = &cf->sg[2];
138395456e89SShreyansh Jain 	qm_sg_entry_set64(&cf->sg[1], dpaa_mem_vtop(sg));
1384c3e85bdcSAkhil Goyal 	if (is_encode(ses)) {
1385c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1386c3e85bdcSAkhil Goyal 		sg->length = ses->iv.length;
1387c3e85bdcSAkhil Goyal 		length += sg->length;
1388c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1389c3e85bdcSAkhil Goyal 
1390c3e85bdcSAkhil Goyal 		sg++;
1391a389434eSAlok Makhariya 		qm_sg_entry_set64(sg, src_start_addr + sym->auth.data.offset);
1392c3e85bdcSAkhil Goyal 		sg->length = sym->auth.data.length;
1393c3e85bdcSAkhil Goyal 		length += sg->length;
1394c3e85bdcSAkhil Goyal 		sg->final = 1;
1395c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1396c3e85bdcSAkhil Goyal 	} else {
1397c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1398c3e85bdcSAkhil Goyal 		sg->length = ses->iv.length;
1399c3e85bdcSAkhil Goyal 		length += sg->length;
1400c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1401c3e85bdcSAkhil Goyal 
1402c3e85bdcSAkhil Goyal 		sg++;
1403c3e85bdcSAkhil Goyal 
1404a389434eSAlok Makhariya 		qm_sg_entry_set64(sg, src_start_addr + sym->auth.data.offset);
1405c3e85bdcSAkhil Goyal 		sg->length = sym->auth.data.length;
1406c3e85bdcSAkhil Goyal 		length += sg->length;
1407c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1408c3e85bdcSAkhil Goyal 
1409c3e85bdcSAkhil Goyal 		memcpy(ctx->digest, sym->auth.digest.data,
1410c3e85bdcSAkhil Goyal 		       ses->digest_length);
1411c3e85bdcSAkhil Goyal 		sg++;
1412c3e85bdcSAkhil Goyal 
141395456e89SShreyansh Jain 		qm_sg_entry_set64(sg, dpaa_mem_vtop(ctx->digest));
1414c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
1415c3e85bdcSAkhil Goyal 		length += sg->length;
1416c3e85bdcSAkhil Goyal 		sg->final = 1;
1417c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1418c3e85bdcSAkhil Goyal 	}
1419c3e85bdcSAkhil Goyal 	/* input compound frame */
1420c3e85bdcSAkhil Goyal 	cf->sg[1].length = length;
1421c3e85bdcSAkhil Goyal 	cf->sg[1].extension = 1;
1422c3e85bdcSAkhil Goyal 	cf->sg[1].final = 1;
1423c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(&cf->sg[1]);
1424c3e85bdcSAkhil Goyal 
1425c3e85bdcSAkhil Goyal 	/* output */
1426c3e85bdcSAkhil Goyal 	sg++;
142795456e89SShreyansh Jain 	qm_sg_entry_set64(&cf->sg[0], dpaa_mem_vtop(sg));
1428a389434eSAlok Makhariya 	qm_sg_entry_set64(sg, dst_start_addr + sym->cipher.data.offset);
1429c3e85bdcSAkhil Goyal 	sg->length = sym->cipher.data.length;
1430c3e85bdcSAkhil Goyal 	length = sg->length;
1431c3e85bdcSAkhil Goyal 	if (is_encode(ses)) {
1432c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1433c3e85bdcSAkhil Goyal 		/* set auth output */
1434c3e85bdcSAkhil Goyal 		sg++;
1435c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
1436c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
1437c3e85bdcSAkhil Goyal 		length += sg->length;
1438c3e85bdcSAkhil Goyal 	}
1439c3e85bdcSAkhil Goyal 	sg->final = 1;
1440c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1441c3e85bdcSAkhil Goyal 
1442c3e85bdcSAkhil Goyal 	/* output compound frame */
1443c3e85bdcSAkhil Goyal 	cf->sg[0].length = length;
1444c3e85bdcSAkhil Goyal 	cf->sg[0].extension = 1;
1445c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(&cf->sg[0]);
1446c3e85bdcSAkhil Goyal 
1447c3e85bdcSAkhil Goyal 	return cf;
1448c3e85bdcSAkhil Goyal }
1449c3e85bdcSAkhil Goyal 
14501f14d500SAkhil Goyal static inline struct dpaa_sec_job *
14511f14d500SAkhil Goyal build_proto(struct rte_crypto_op *op, dpaa_sec_session *ses)
14521f14d500SAkhil Goyal {
14531f14d500SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
14541f14d500SAkhil Goyal 	struct dpaa_sec_job *cf;
14551f14d500SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
14561f14d500SAkhil Goyal 	struct qm_sg_entry *sg;
14571f14d500SAkhil Goyal 	phys_addr_t src_start_addr, dst_start_addr;
14581f14d500SAkhil Goyal 
14591f14d500SAkhil Goyal 	ctx = dpaa_sec_alloc_ctx(ses);
14601f14d500SAkhil Goyal 	if (!ctx)
14611f14d500SAkhil Goyal 		return NULL;
14621f14d500SAkhil Goyal 	cf = &ctx->job;
14631f14d500SAkhil Goyal 	ctx->op = op;
14641f14d500SAkhil Goyal 
14651f14d500SAkhil Goyal 	src_start_addr = rte_pktmbuf_mtophys(sym->m_src);
14661f14d500SAkhil Goyal 
14671f14d500SAkhil Goyal 	if (sym->m_dst)
14681f14d500SAkhil Goyal 		dst_start_addr = rte_pktmbuf_mtophys(sym->m_dst);
14691f14d500SAkhil Goyal 	else
14701f14d500SAkhil Goyal 		dst_start_addr = src_start_addr;
14711f14d500SAkhil Goyal 
14721f14d500SAkhil Goyal 	/* input */
14731f14d500SAkhil Goyal 	sg = &cf->sg[1];
14741f14d500SAkhil Goyal 	qm_sg_entry_set64(sg, src_start_addr);
14751f14d500SAkhil Goyal 	sg->length = sym->m_src->pkt_len;
14761f14d500SAkhil Goyal 	sg->final = 1;
14771f14d500SAkhil Goyal 	cpu_to_hw_sg(sg);
14781f14d500SAkhil Goyal 
14791f14d500SAkhil Goyal 	sym->m_src->packet_type &= ~RTE_PTYPE_L4_MASK;
14801f14d500SAkhil Goyal 	/* output */
14811f14d500SAkhil Goyal 	sg = &cf->sg[0];
14821f14d500SAkhil Goyal 	qm_sg_entry_set64(sg, dst_start_addr);
14831f14d500SAkhil Goyal 	sg->length = sym->m_src->buf_len - sym->m_src->data_off;
14841f14d500SAkhil Goyal 	cpu_to_hw_sg(sg);
14851f14d500SAkhil Goyal 
14861f14d500SAkhil Goyal 	return cf;
14871f14d500SAkhil Goyal }
14881f14d500SAkhil Goyal 
14899a984458SAkhil Goyal static uint16_t
14909a984458SAkhil Goyal dpaa_sec_enqueue_burst(void *qp, struct rte_crypto_op **ops,
14919a984458SAkhil Goyal 		       uint16_t nb_ops)
1492c3e85bdcSAkhil Goyal {
14939a984458SAkhil Goyal 	/* Function to transmit the frames to given device and queuepair */
14949a984458SAkhil Goyal 	uint32_t loop;
14959a984458SAkhil Goyal 	struct dpaa_sec_qp *dpaa_qp = (struct dpaa_sec_qp *)qp;
14969a984458SAkhil Goyal 	uint16_t num_tx = 0;
14979a984458SAkhil Goyal 	struct qm_fd fds[DPAA_SEC_BURST], *fd;
14989a984458SAkhil Goyal 	uint32_t frames_to_send;
14999a984458SAkhil Goyal 	struct rte_crypto_op *op;
1500c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
1501c3e85bdcSAkhil Goyal 	dpaa_sec_session *ses;
15029a984458SAkhil Goyal 	uint32_t auth_only_len;
15039a984458SAkhil Goyal 	struct qman_fq *inq[DPAA_SEC_BURST];
1504c3e85bdcSAkhil Goyal 
15059a984458SAkhil Goyal 	while (nb_ops) {
15069a984458SAkhil Goyal 		frames_to_send = (nb_ops > DPAA_SEC_BURST) ?
15079a984458SAkhil Goyal 				DPAA_SEC_BURST : nb_ops;
15089a984458SAkhil Goyal 		for (loop = 0; loop < frames_to_send; loop++) {
15099a984458SAkhil Goyal 			op = *(ops++);
15109a984458SAkhil Goyal 			switch (op->sess_type) {
15119a984458SAkhil Goyal 			case RTE_CRYPTO_OP_WITH_SESSION:
15129a984458SAkhil Goyal 				ses = (dpaa_sec_session *)
1513012c5076SPablo de Lara 					get_sym_session_private_data(
15149a984458SAkhil Goyal 							op->sym->session,
15159a984458SAkhil Goyal 							cryptodev_driver_id);
15169a984458SAkhil Goyal 				break;
15179a984458SAkhil Goyal 			case RTE_CRYPTO_OP_SECURITY_SESSION:
15189a984458SAkhil Goyal 				ses = (dpaa_sec_session *)
15199a984458SAkhil Goyal 					get_sec_session_private_data(
15201f14d500SAkhil Goyal 							op->sym->sec_session);
15219a984458SAkhil Goyal 				break;
15229a984458SAkhil Goyal 			default:
1523f163231eSHemant Agrawal 				DPAA_SEC_DP_ERR(
15249a984458SAkhil Goyal 					"sessionless crypto op not supported");
15259a984458SAkhil Goyal 				frames_to_send = loop;
15269a984458SAkhil Goyal 				nb_ops = loop;
15279a984458SAkhil Goyal 				goto send_pkts;
15289a984458SAkhil Goyal 			}
15299198b2c2SAkhil Goyal 			if (unlikely(!ses->qp)) {
15309a984458SAkhil Goyal 				if (dpaa_sec_attach_sess_q(qp, ses)) {
15319a984458SAkhil Goyal 					frames_to_send = loop;
15329a984458SAkhil Goyal 					nb_ops = loop;
15339a984458SAkhil Goyal 					goto send_pkts;
15349a984458SAkhil Goyal 				}
15359198b2c2SAkhil Goyal 			} else if (unlikely(ses->qp != qp)) {
15369198b2c2SAkhil Goyal 				DPAA_SEC_DP_ERR("Old:sess->qp = %p"
15379198b2c2SAkhil Goyal 					" New qp = %p\n", ses->qp, qp);
15389198b2c2SAkhil Goyal 				frames_to_send = loop;
15399198b2c2SAkhil Goyal 				nb_ops = loop;
15409198b2c2SAkhil Goyal 				goto send_pkts;
1541c3e85bdcSAkhil Goyal 			}
1542c3e85bdcSAkhil Goyal 
15439a984458SAkhil Goyal 			auth_only_len = op->sym->auth.data.length -
15449a984458SAkhil Goyal 						op->sym->cipher.data.length;
1545a74af788SAkhil Goyal 			if (rte_pktmbuf_is_contiguous(op->sym->m_src)) {
154605b12700SHemant Agrawal 				if (is_proto_ipsec(ses)) {
154705b12700SHemant Agrawal 					cf = build_proto(op, ses);
154805b12700SHemant Agrawal 				} else if (is_auth_only(ses)) {
1549c3e85bdcSAkhil Goyal 					cf = build_auth_only(op, ses);
1550c3e85bdcSAkhil Goyal 				} else if (is_cipher_only(ses)) {
1551c3e85bdcSAkhil Goyal 					cf = build_cipher_only(op, ses);
1552c3e85bdcSAkhil Goyal 				} else if (is_aead(ses)) {
1553c3e85bdcSAkhil Goyal 					cf = build_cipher_auth_gcm(op, ses);
1554c3e85bdcSAkhil Goyal 					auth_only_len = ses->auth_only_len;
1555c3e85bdcSAkhil Goyal 				} else if (is_auth_cipher(ses)) {
1556c3e85bdcSAkhil Goyal 					cf = build_cipher_auth(op, ses);
1557c3e85bdcSAkhil Goyal 				} else {
1558f163231eSHemant Agrawal 					DPAA_SEC_DP_ERR("not supported ops");
15599a984458SAkhil Goyal 					frames_to_send = loop;
15609a984458SAkhil Goyal 					nb_ops = loop;
15619a984458SAkhil Goyal 					goto send_pkts;
1562c3e85bdcSAkhil Goyal 				}
1563a74af788SAkhil Goyal 			} else {
1564a74af788SAkhil Goyal 				if (is_auth_only(ses)) {
1565a74af788SAkhil Goyal 					cf = build_auth_only_sg(op, ses);
1566a74af788SAkhil Goyal 				} else if (is_cipher_only(ses)) {
1567a74af788SAkhil Goyal 					cf = build_cipher_only_sg(op, ses);
1568a74af788SAkhil Goyal 				} else if (is_aead(ses)) {
1569a74af788SAkhil Goyal 					cf = build_cipher_auth_gcm_sg(op, ses);
1570a74af788SAkhil Goyal 					auth_only_len = ses->auth_only_len;
1571a74af788SAkhil Goyal 				} else if (is_auth_cipher(ses)) {
1572a74af788SAkhil Goyal 					cf = build_cipher_auth_sg(op, ses);
1573a74af788SAkhil Goyal 				} else {
1574f163231eSHemant Agrawal 					DPAA_SEC_DP_ERR("not supported ops");
1575a74af788SAkhil Goyal 					frames_to_send = loop;
1576a74af788SAkhil Goyal 					nb_ops = loop;
1577a74af788SAkhil Goyal 					goto send_pkts;
1578a74af788SAkhil Goyal 				}
1579a74af788SAkhil Goyal 			}
15809a984458SAkhil Goyal 			if (unlikely(!cf)) {
15819a984458SAkhil Goyal 				frames_to_send = loop;
15829a984458SAkhil Goyal 				nb_ops = loop;
15839a984458SAkhil Goyal 				goto send_pkts;
15849a984458SAkhil Goyal 			}
1585c3e85bdcSAkhil Goyal 
15869a984458SAkhil Goyal 			fd = &fds[loop];
15879a984458SAkhil Goyal 			inq[loop] = ses->inq;
15889a984458SAkhil Goyal 			fd->opaque_addr = 0;
15899a984458SAkhil Goyal 			fd->cmd = 0;
159095456e89SShreyansh Jain 			qm_fd_addr_set64(fd, dpaa_mem_vtop(cf->sg));
15919a984458SAkhil Goyal 			fd->_format1 = qm_fd_compound;
15929a984458SAkhil Goyal 			fd->length29 = 2 * sizeof(struct qm_sg_entry);
15939a984458SAkhil Goyal 			/* Auth_only_len is set as 0 in descriptor and it is
15949a984458SAkhil Goyal 			 * overwritten here in the fd.cmd which will update
15959a984458SAkhil Goyal 			 * the DPOVRD reg.
1596c3e85bdcSAkhil Goyal 			 */
1597c3e85bdcSAkhil Goyal 			if (auth_only_len)
15989a984458SAkhil Goyal 				fd->cmd = 0x80000000 | auth_only_len;
1599c3e85bdcSAkhil Goyal 
16009a984458SAkhil Goyal 		}
16019a984458SAkhil Goyal send_pkts:
16029a984458SAkhil Goyal 		loop = 0;
16039a984458SAkhil Goyal 		while (loop < frames_to_send) {
16049a984458SAkhil Goyal 			loop += qman_enqueue_multi_fq(&inq[loop], &fds[loop],
16059a984458SAkhil Goyal 					frames_to_send - loop);
16069a984458SAkhil Goyal 		}
16079a984458SAkhil Goyal 		nb_ops -= frames_to_send;
16089a984458SAkhil Goyal 		num_tx += frames_to_send;
1609c3e85bdcSAkhil Goyal 	}
1610c3e85bdcSAkhil Goyal 
1611c3e85bdcSAkhil Goyal 	dpaa_qp->tx_pkts += num_tx;
1612c3e85bdcSAkhil Goyal 	dpaa_qp->tx_errs += nb_ops - num_tx;
1613c3e85bdcSAkhil Goyal 
1614c3e85bdcSAkhil Goyal 	return num_tx;
1615c3e85bdcSAkhil Goyal }
1616c3e85bdcSAkhil Goyal 
1617c3e85bdcSAkhil Goyal static uint16_t
1618c3e85bdcSAkhil Goyal dpaa_sec_dequeue_burst(void *qp, struct rte_crypto_op **ops,
1619c3e85bdcSAkhil Goyal 		       uint16_t nb_ops)
1620c3e85bdcSAkhil Goyal {
1621c3e85bdcSAkhil Goyal 	uint16_t num_rx;
1622c3e85bdcSAkhil Goyal 	struct dpaa_sec_qp *dpaa_qp = (struct dpaa_sec_qp *)qp;
1623c3e85bdcSAkhil Goyal 
1624c3e85bdcSAkhil Goyal 	num_rx = dpaa_sec_deq(dpaa_qp, ops, nb_ops);
1625c3e85bdcSAkhil Goyal 
1626c3e85bdcSAkhil Goyal 	dpaa_qp->rx_pkts += num_rx;
1627c3e85bdcSAkhil Goyal 	dpaa_qp->rx_errs += nb_ops - num_rx;
1628c3e85bdcSAkhil Goyal 
1629f163231eSHemant Agrawal 	DPAA_SEC_DP_DEBUG("SEC Received %d Packets\n", num_rx);
1630c3e85bdcSAkhil Goyal 
1631c3e85bdcSAkhil Goyal 	return num_rx;
1632c3e85bdcSAkhil Goyal }
1633c3e85bdcSAkhil Goyal 
1634c3e85bdcSAkhil Goyal /** Release queue pair */
1635c3e85bdcSAkhil Goyal static int
1636c3e85bdcSAkhil Goyal dpaa_sec_queue_pair_release(struct rte_cryptodev *dev,
1637c3e85bdcSAkhil Goyal 			    uint16_t qp_id)
1638c3e85bdcSAkhil Goyal {
1639c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals;
1640c3e85bdcSAkhil Goyal 	struct dpaa_sec_qp *qp = NULL;
1641c3e85bdcSAkhil Goyal 
1642c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
1643c3e85bdcSAkhil Goyal 
1644f163231eSHemant Agrawal 	DPAA_SEC_DEBUG("dev =%p, queue =%d", dev, qp_id);
1645c3e85bdcSAkhil Goyal 
1646c3e85bdcSAkhil Goyal 	internals = dev->data->dev_private;
1647c3e85bdcSAkhil Goyal 	if (qp_id >= internals->max_nb_queue_pairs) {
1648f163231eSHemant Agrawal 		DPAA_SEC_ERR("Max supported qpid %d",
1649c3e85bdcSAkhil Goyal 			     internals->max_nb_queue_pairs);
1650c3e85bdcSAkhil Goyal 		return -EINVAL;
1651c3e85bdcSAkhil Goyal 	}
1652c3e85bdcSAkhil Goyal 
1653c3e85bdcSAkhil Goyal 	qp = &internals->qps[qp_id];
1654c3e85bdcSAkhil Goyal 	qp->internals = NULL;
1655c3e85bdcSAkhil Goyal 	dev->data->queue_pairs[qp_id] = NULL;
1656c3e85bdcSAkhil Goyal 
1657c3e85bdcSAkhil Goyal 	return 0;
1658c3e85bdcSAkhil Goyal }
1659c3e85bdcSAkhil Goyal 
1660c3e85bdcSAkhil Goyal /** Setup a queue pair */
1661c3e85bdcSAkhil Goyal static int
1662c3e85bdcSAkhil Goyal dpaa_sec_queue_pair_setup(struct rte_cryptodev *dev, uint16_t qp_id,
1663c3e85bdcSAkhil Goyal 		__rte_unused const struct rte_cryptodev_qp_conf *qp_conf,
1664c3e85bdcSAkhil Goyal 		__rte_unused int socket_id,
1665c3e85bdcSAkhil Goyal 		__rte_unused struct rte_mempool *session_pool)
1666c3e85bdcSAkhil Goyal {
1667c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals;
1668c3e85bdcSAkhil Goyal 	struct dpaa_sec_qp *qp = NULL;
1669c3e85bdcSAkhil Goyal 
1670f163231eSHemant Agrawal 	DPAA_SEC_DEBUG("dev =%p, queue =%d, conf =%p", dev, qp_id, qp_conf);
1671c3e85bdcSAkhil Goyal 
1672c3e85bdcSAkhil Goyal 	internals = dev->data->dev_private;
1673c3e85bdcSAkhil Goyal 	if (qp_id >= internals->max_nb_queue_pairs) {
1674f163231eSHemant Agrawal 		DPAA_SEC_ERR("Max supported qpid %d",
1675c3e85bdcSAkhil Goyal 			     internals->max_nb_queue_pairs);
1676c3e85bdcSAkhil Goyal 		return -EINVAL;
1677c3e85bdcSAkhil Goyal 	}
1678c3e85bdcSAkhil Goyal 
1679c3e85bdcSAkhil Goyal 	qp = &internals->qps[qp_id];
1680c3e85bdcSAkhil Goyal 	qp->internals = internals;
1681c3e85bdcSAkhil Goyal 	dev->data->queue_pairs[qp_id] = qp;
1682c3e85bdcSAkhil Goyal 
1683c3e85bdcSAkhil Goyal 	return 0;
1684c3e85bdcSAkhil Goyal }
1685c3e85bdcSAkhil Goyal 
1686c3e85bdcSAkhil Goyal /** Return the number of allocated queue pairs */
1687c3e85bdcSAkhil Goyal static uint32_t
1688c3e85bdcSAkhil Goyal dpaa_sec_queue_pair_count(struct rte_cryptodev *dev)
1689c3e85bdcSAkhil Goyal {
1690c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
1691c3e85bdcSAkhil Goyal 
1692c3e85bdcSAkhil Goyal 	return dev->data->nb_queue_pairs;
1693c3e85bdcSAkhil Goyal }
1694c3e85bdcSAkhil Goyal 
1695c3e85bdcSAkhil Goyal /** Returns the size of session structure */
1696c3e85bdcSAkhil Goyal static unsigned int
1697012c5076SPablo de Lara dpaa_sec_sym_session_get_size(struct rte_cryptodev *dev __rte_unused)
1698c3e85bdcSAkhil Goyal {
1699c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
1700c3e85bdcSAkhil Goyal 
1701c3e85bdcSAkhil Goyal 	return sizeof(dpaa_sec_session);
1702c3e85bdcSAkhil Goyal }
1703c3e85bdcSAkhil Goyal 
1704c3e85bdcSAkhil Goyal static int
1705c3e85bdcSAkhil Goyal dpaa_sec_cipher_init(struct rte_cryptodev *dev __rte_unused,
1706c3e85bdcSAkhil Goyal 		     struct rte_crypto_sym_xform *xform,
1707c3e85bdcSAkhil Goyal 		     dpaa_sec_session *session)
1708c3e85bdcSAkhil Goyal {
1709c3e85bdcSAkhil Goyal 	session->cipher_alg = xform->cipher.algo;
1710c3e85bdcSAkhil Goyal 	session->iv.length = xform->cipher.iv.length;
1711c3e85bdcSAkhil Goyal 	session->iv.offset = xform->cipher.iv.offset;
1712c3e85bdcSAkhil Goyal 	session->cipher_key.data = rte_zmalloc(NULL, xform->cipher.key.length,
1713c3e85bdcSAkhil Goyal 					       RTE_CACHE_LINE_SIZE);
1714c3e85bdcSAkhil Goyal 	if (session->cipher_key.data == NULL && xform->cipher.key.length > 0) {
1715f163231eSHemant Agrawal 		DPAA_SEC_ERR("No Memory for cipher key");
1716c3e85bdcSAkhil Goyal 		return -ENOMEM;
1717c3e85bdcSAkhil Goyal 	}
1718c3e85bdcSAkhil Goyal 	session->cipher_key.length = xform->cipher.key.length;
1719c3e85bdcSAkhil Goyal 
1720c3e85bdcSAkhil Goyal 	memcpy(session->cipher_key.data, xform->cipher.key.data,
1721c3e85bdcSAkhil Goyal 	       xform->cipher.key.length);
1722c3e85bdcSAkhil Goyal 	session->dir = (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
1723c3e85bdcSAkhil Goyal 			DIR_ENC : DIR_DEC;
1724c3e85bdcSAkhil Goyal 
1725c3e85bdcSAkhil Goyal 	return 0;
1726c3e85bdcSAkhil Goyal }
1727c3e85bdcSAkhil Goyal 
1728c3e85bdcSAkhil Goyal static int
1729c3e85bdcSAkhil Goyal dpaa_sec_auth_init(struct rte_cryptodev *dev __rte_unused,
1730c3e85bdcSAkhil Goyal 		   struct rte_crypto_sym_xform *xform,
1731c3e85bdcSAkhil Goyal 		   dpaa_sec_session *session)
1732c3e85bdcSAkhil Goyal {
1733c3e85bdcSAkhil Goyal 	session->auth_alg = xform->auth.algo;
1734c3e85bdcSAkhil Goyal 	session->auth_key.data = rte_zmalloc(NULL, xform->auth.key.length,
1735c3e85bdcSAkhil Goyal 					     RTE_CACHE_LINE_SIZE);
1736c3e85bdcSAkhil Goyal 	if (session->auth_key.data == NULL && xform->auth.key.length > 0) {
1737f163231eSHemant Agrawal 		DPAA_SEC_ERR("No Memory for auth key");
1738c3e85bdcSAkhil Goyal 		return -ENOMEM;
1739c3e85bdcSAkhil Goyal 	}
1740c3e85bdcSAkhil Goyal 	session->auth_key.length = xform->auth.key.length;
1741c3e85bdcSAkhil Goyal 	session->digest_length = xform->auth.digest_length;
1742c3e85bdcSAkhil Goyal 
1743c3e85bdcSAkhil Goyal 	memcpy(session->auth_key.data, xform->auth.key.data,
1744c3e85bdcSAkhil Goyal 	       xform->auth.key.length);
1745c3e85bdcSAkhil Goyal 	session->dir = (xform->auth.op == RTE_CRYPTO_AUTH_OP_GENERATE) ?
1746c3e85bdcSAkhil Goyal 			DIR_ENC : DIR_DEC;
1747c3e85bdcSAkhil Goyal 
1748c3e85bdcSAkhil Goyal 	return 0;
1749c3e85bdcSAkhil Goyal }
1750c3e85bdcSAkhil Goyal 
1751c3e85bdcSAkhil Goyal static int
1752c3e85bdcSAkhil Goyal dpaa_sec_aead_init(struct rte_cryptodev *dev __rte_unused,
1753c3e85bdcSAkhil Goyal 		   struct rte_crypto_sym_xform *xform,
1754c3e85bdcSAkhil Goyal 		   dpaa_sec_session *session)
1755c3e85bdcSAkhil Goyal {
1756c3e85bdcSAkhil Goyal 	session->aead_alg = xform->aead.algo;
1757c3e85bdcSAkhil Goyal 	session->iv.length = xform->aead.iv.length;
1758c3e85bdcSAkhil Goyal 	session->iv.offset = xform->aead.iv.offset;
1759c3e85bdcSAkhil Goyal 	session->auth_only_len = xform->aead.aad_length;
1760c3e85bdcSAkhil Goyal 	session->aead_key.data = rte_zmalloc(NULL, xform->aead.key.length,
1761c3e85bdcSAkhil Goyal 					     RTE_CACHE_LINE_SIZE);
1762c3e85bdcSAkhil Goyal 	if (session->aead_key.data == NULL && xform->aead.key.length > 0) {
1763f163231eSHemant Agrawal 		DPAA_SEC_ERR("No Memory for aead key\n");
1764c3e85bdcSAkhil Goyal 		return -ENOMEM;
1765c3e85bdcSAkhil Goyal 	}
1766c3e85bdcSAkhil Goyal 	session->aead_key.length = xform->aead.key.length;
1767c3e85bdcSAkhil Goyal 	session->digest_length = xform->aead.digest_length;
1768c3e85bdcSAkhil Goyal 
1769c3e85bdcSAkhil Goyal 	memcpy(session->aead_key.data, xform->aead.key.data,
1770c3e85bdcSAkhil Goyal 	       xform->aead.key.length);
1771c3e85bdcSAkhil Goyal 	session->dir = (xform->aead.op == RTE_CRYPTO_AEAD_OP_ENCRYPT) ?
1772c3e85bdcSAkhil Goyal 			DIR_ENC : DIR_DEC;
1773c3e85bdcSAkhil Goyal 
1774c3e85bdcSAkhil Goyal 	return 0;
1775c3e85bdcSAkhil Goyal }
1776c3e85bdcSAkhil Goyal 
1777e79416d1SHemant Agrawal static struct qman_fq *
1778e79416d1SHemant Agrawal dpaa_sec_attach_rxq(struct dpaa_sec_dev_private *qi)
1779c3e85bdcSAkhil Goyal {
1780e79416d1SHemant Agrawal 	unsigned int i;
1781c3e85bdcSAkhil Goyal 
1782e79416d1SHemant Agrawal 	for (i = 0; i < qi->max_nb_sessions; i++) {
1783e79416d1SHemant Agrawal 		if (qi->inq_attach[i] == 0) {
1784e79416d1SHemant Agrawal 			qi->inq_attach[i] = 1;
1785e79416d1SHemant Agrawal 			return &qi->inq[i];
1786e79416d1SHemant Agrawal 		}
1787e79416d1SHemant Agrawal 	}
1788f163231eSHemant Agrawal 	DPAA_SEC_WARN("All ses session in use %x", qi->max_nb_sessions);
1789c3e85bdcSAkhil Goyal 
1790e79416d1SHemant Agrawal 	return NULL;
1791c3e85bdcSAkhil Goyal }
1792c3e85bdcSAkhil Goyal 
1793e79416d1SHemant Agrawal static int
1794e79416d1SHemant Agrawal dpaa_sec_detach_rxq(struct dpaa_sec_dev_private *qi, struct qman_fq *fq)
1795e79416d1SHemant Agrawal {
1796e79416d1SHemant Agrawal 	unsigned int i;
1797e79416d1SHemant Agrawal 
1798e79416d1SHemant Agrawal 	for (i = 0; i < qi->max_nb_sessions; i++) {
1799e79416d1SHemant Agrawal 		if (&qi->inq[i] == fq) {
1800b4053c4bSAlok Makhariya 			qman_retire_fq(fq, NULL);
1801b4053c4bSAlok Makhariya 			qman_oos_fq(fq);
1802e79416d1SHemant Agrawal 			qi->inq_attach[i] = 0;
1803e79416d1SHemant Agrawal 			return 0;
1804e79416d1SHemant Agrawal 		}
1805e79416d1SHemant Agrawal 	}
1806e79416d1SHemant Agrawal 	return -1;
1807e79416d1SHemant Agrawal }
1808e79416d1SHemant Agrawal 
1809e79416d1SHemant Agrawal static int
1810e79416d1SHemant Agrawal dpaa_sec_attach_sess_q(struct dpaa_sec_qp *qp, dpaa_sec_session *sess)
1811e79416d1SHemant Agrawal {
1812e79416d1SHemant Agrawal 	int ret;
1813e79416d1SHemant Agrawal 
1814c3e85bdcSAkhil Goyal 	sess->qp = qp;
1815e79416d1SHemant Agrawal 	ret = dpaa_sec_prep_cdb(sess);
1816e79416d1SHemant Agrawal 	if (ret) {
1817f163231eSHemant Agrawal 		DPAA_SEC_ERR("Unable to prepare sec cdb");
1818e79416d1SHemant Agrawal 		return -1;
1819e79416d1SHemant Agrawal 	}
18205b0f1bd3SAshish Jain 	if (unlikely(!RTE_PER_LCORE(dpaa_io))) {
18215b0f1bd3SAshish Jain 		ret = rte_dpaa_portal_init((void *)0);
18225b0f1bd3SAshish Jain 		if (ret) {
1823f163231eSHemant Agrawal 			DPAA_SEC_ERR("Failure in affining portal");
18245b0f1bd3SAshish Jain 			return ret;
18255b0f1bd3SAshish Jain 		}
18265b0f1bd3SAshish Jain 	}
1827e79416d1SHemant Agrawal 	ret = dpaa_sec_init_rx(sess->inq, dpaa_mem_vtop(&sess->cdb),
1828e79416d1SHemant Agrawal 			       qman_fq_fqid(&qp->outq));
1829e79416d1SHemant Agrawal 	if (ret)
1830f163231eSHemant Agrawal 		DPAA_SEC_ERR("Unable to init sec queue");
1831e79416d1SHemant Agrawal 
1832e79416d1SHemant Agrawal 	return ret;
1833c3e85bdcSAkhil Goyal }
1834c3e85bdcSAkhil Goyal 
1835c3e85bdcSAkhil Goyal static int
1836c3e85bdcSAkhil Goyal dpaa_sec_set_session_parameters(struct rte_cryptodev *dev,
1837c3e85bdcSAkhil Goyal 			    struct rte_crypto_sym_xform *xform,	void *sess)
1838c3e85bdcSAkhil Goyal {
1839c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals = dev->data->dev_private;
1840c3e85bdcSAkhil Goyal 	dpaa_sec_session *session = sess;
1841c3e85bdcSAkhil Goyal 
1842c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
1843c3e85bdcSAkhil Goyal 
1844c3e85bdcSAkhil Goyal 	if (unlikely(sess == NULL)) {
1845f163231eSHemant Agrawal 		DPAA_SEC_ERR("invalid session struct");
1846c3e85bdcSAkhil Goyal 		return -EINVAL;
1847c3e85bdcSAkhil Goyal 	}
1848b0894102SAkhil Goyal 	memset(session, 0, sizeof(dpaa_sec_session));
1849c3e85bdcSAkhil Goyal 
1850c3e85bdcSAkhil Goyal 	/* Default IV length = 0 */
1851c3e85bdcSAkhil Goyal 	session->iv.length = 0;
1852c3e85bdcSAkhil Goyal 
1853c3e85bdcSAkhil Goyal 	/* Cipher Only */
1854c3e85bdcSAkhil Goyal 	if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER && xform->next == NULL) {
1855c3e85bdcSAkhil Goyal 		session->auth_alg = RTE_CRYPTO_AUTH_NULL;
1856c3e85bdcSAkhil Goyal 		dpaa_sec_cipher_init(dev, xform, session);
1857c3e85bdcSAkhil Goyal 
1858c3e85bdcSAkhil Goyal 	/* Authentication Only */
1859c3e85bdcSAkhil Goyal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
1860c3e85bdcSAkhil Goyal 		   xform->next == NULL) {
1861c3e85bdcSAkhil Goyal 		session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
1862c3e85bdcSAkhil Goyal 		dpaa_sec_auth_init(dev, xform, session);
1863c3e85bdcSAkhil Goyal 
1864c3e85bdcSAkhil Goyal 	/* Cipher then Authenticate */
1865c3e85bdcSAkhil Goyal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
1866c3e85bdcSAkhil Goyal 		   xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
1867c3e85bdcSAkhil Goyal 		if (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) {
1868c3e85bdcSAkhil Goyal 			dpaa_sec_cipher_init(dev, xform, session);
1869c3e85bdcSAkhil Goyal 			dpaa_sec_auth_init(dev, xform->next, session);
1870c3e85bdcSAkhil Goyal 		} else {
1871f163231eSHemant Agrawal 			DPAA_SEC_ERR("Not supported: Auth then Cipher");
1872c3e85bdcSAkhil Goyal 			return -EINVAL;
1873c3e85bdcSAkhil Goyal 		}
1874c3e85bdcSAkhil Goyal 
1875c3e85bdcSAkhil Goyal 	/* Authenticate then Cipher */
1876c3e85bdcSAkhil Goyal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
1877c3e85bdcSAkhil Goyal 		   xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
1878c3e85bdcSAkhil Goyal 		if (xform->next->cipher.op == RTE_CRYPTO_CIPHER_OP_DECRYPT) {
1879c3e85bdcSAkhil Goyal 			dpaa_sec_auth_init(dev, xform, session);
1880c3e85bdcSAkhil Goyal 			dpaa_sec_cipher_init(dev, xform->next, session);
1881c3e85bdcSAkhil Goyal 		} else {
1882f163231eSHemant Agrawal 			DPAA_SEC_ERR("Not supported: Auth then Cipher");
1883c3e85bdcSAkhil Goyal 			return -EINVAL;
1884c3e85bdcSAkhil Goyal 		}
1885c3e85bdcSAkhil Goyal 
1886c3e85bdcSAkhil Goyal 	/* AEAD operation for AES-GCM kind of Algorithms */
1887c3e85bdcSAkhil Goyal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD &&
1888c3e85bdcSAkhil Goyal 		   xform->next == NULL) {
1889c3e85bdcSAkhil Goyal 		dpaa_sec_aead_init(dev, xform, session);
1890c3e85bdcSAkhil Goyal 
1891c3e85bdcSAkhil Goyal 	} else {
1892f163231eSHemant Agrawal 		DPAA_SEC_ERR("Invalid crypto type");
1893c3e85bdcSAkhil Goyal 		return -EINVAL;
1894c3e85bdcSAkhil Goyal 	}
1895c3e85bdcSAkhil Goyal 	session->ctx_pool = internals->ctx_pool;
18963b617ee7SAkhil Goyal 	rte_spinlock_lock(&internals->lock);
1897e79416d1SHemant Agrawal 	session->inq = dpaa_sec_attach_rxq(internals);
18983b617ee7SAkhil Goyal 	rte_spinlock_unlock(&internals->lock);
1899e79416d1SHemant Agrawal 	if (session->inq == NULL) {
1900f163231eSHemant Agrawal 		DPAA_SEC_ERR("unable to attach sec queue");
1901e79416d1SHemant Agrawal 		goto err1;
1902e79416d1SHemant Agrawal 	}
1903c3e85bdcSAkhil Goyal 
1904c3e85bdcSAkhil Goyal 	return 0;
1905e79416d1SHemant Agrawal 
1906e79416d1SHemant Agrawal err1:
1907e79416d1SHemant Agrawal 	rte_free(session->cipher_key.data);
1908e79416d1SHemant Agrawal 	rte_free(session->auth_key.data);
1909e79416d1SHemant Agrawal 	memset(session, 0, sizeof(dpaa_sec_session));
1910e79416d1SHemant Agrawal 
1911e79416d1SHemant Agrawal 	return -EINVAL;
1912c3e85bdcSAkhil Goyal }
1913c3e85bdcSAkhil Goyal 
1914c3e85bdcSAkhil Goyal static int
1915012c5076SPablo de Lara dpaa_sec_sym_session_configure(struct rte_cryptodev *dev,
1916c3e85bdcSAkhil Goyal 		struct rte_crypto_sym_xform *xform,
1917c3e85bdcSAkhil Goyal 		struct rte_cryptodev_sym_session *sess,
1918c3e85bdcSAkhil Goyal 		struct rte_mempool *mempool)
1919c3e85bdcSAkhil Goyal {
1920c3e85bdcSAkhil Goyal 	void *sess_private_data;
1921c3e85bdcSAkhil Goyal 	int ret;
1922c3e85bdcSAkhil Goyal 
1923c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
1924c3e85bdcSAkhil Goyal 
1925c3e85bdcSAkhil Goyal 	if (rte_mempool_get(mempool, &sess_private_data)) {
1926f163231eSHemant Agrawal 		DPAA_SEC_ERR("Couldn't get object from session mempool");
1927c3e85bdcSAkhil Goyal 		return -ENOMEM;
1928c3e85bdcSAkhil Goyal 	}
1929c3e85bdcSAkhil Goyal 
1930c3e85bdcSAkhil Goyal 	ret = dpaa_sec_set_session_parameters(dev, xform, sess_private_data);
1931c3e85bdcSAkhil Goyal 	if (ret != 0) {
1932f163231eSHemant Agrawal 		DPAA_SEC_ERR("failed to configure session parameters");
1933c3e85bdcSAkhil Goyal 
1934c3e85bdcSAkhil Goyal 		/* Return session to mempool */
1935c3e85bdcSAkhil Goyal 		rte_mempool_put(mempool, sess_private_data);
1936c3e85bdcSAkhil Goyal 		return ret;
1937c3e85bdcSAkhil Goyal 	}
1938c3e85bdcSAkhil Goyal 
1939012c5076SPablo de Lara 	set_sym_session_private_data(sess, dev->driver_id,
1940c3e85bdcSAkhil Goyal 			sess_private_data);
1941c3e85bdcSAkhil Goyal 
1942e79416d1SHemant Agrawal 
1943c3e85bdcSAkhil Goyal 	return 0;
1944c3e85bdcSAkhil Goyal }
1945c3e85bdcSAkhil Goyal 
1946c3e85bdcSAkhil Goyal /** Clear the memory of session so it doesn't leave key material behind */
1947c3e85bdcSAkhil Goyal static void
1948012c5076SPablo de Lara dpaa_sec_sym_session_clear(struct rte_cryptodev *dev,
1949c3e85bdcSAkhil Goyal 		struct rte_cryptodev_sym_session *sess)
1950c3e85bdcSAkhil Goyal {
1951e79416d1SHemant Agrawal 	struct dpaa_sec_dev_private *qi = dev->data->dev_private;
1952c3e85bdcSAkhil Goyal 	uint8_t index = dev->driver_id;
1953012c5076SPablo de Lara 	void *sess_priv = get_sym_session_private_data(sess, index);
1954e79416d1SHemant Agrawal 
1955e79416d1SHemant Agrawal 	PMD_INIT_FUNC_TRACE();
1956e79416d1SHemant Agrawal 
1957c3e85bdcSAkhil Goyal 	dpaa_sec_session *s = (dpaa_sec_session *)sess_priv;
1958c3e85bdcSAkhil Goyal 
1959c3e85bdcSAkhil Goyal 	if (sess_priv) {
1960e79416d1SHemant Agrawal 		struct rte_mempool *sess_mp = rte_mempool_from_obj(sess_priv);
1961e79416d1SHemant Agrawal 
1962e79416d1SHemant Agrawal 		if (s->inq)
1963e79416d1SHemant Agrawal 			dpaa_sec_detach_rxq(qi, s->inq);
1964c3e85bdcSAkhil Goyal 		rte_free(s->cipher_key.data);
1965c3e85bdcSAkhil Goyal 		rte_free(s->auth_key.data);
1966c3e85bdcSAkhil Goyal 		memset(s, 0, sizeof(dpaa_sec_session));
1967012c5076SPablo de Lara 		set_sym_session_private_data(sess, index, NULL);
1968c3e85bdcSAkhil Goyal 		rte_mempool_put(sess_mp, sess_priv);
1969c3e85bdcSAkhil Goyal 	}
1970c3e85bdcSAkhil Goyal }
1971c3e85bdcSAkhil Goyal 
1972c3e85bdcSAkhil Goyal static int
19731f14d500SAkhil Goyal dpaa_sec_set_ipsec_session(__rte_unused struct rte_cryptodev *dev,
19741f14d500SAkhil Goyal 			   struct rte_security_session_conf *conf,
19751f14d500SAkhil Goyal 			   void *sess)
19761f14d500SAkhil Goyal {
19771f14d500SAkhil Goyal 	struct dpaa_sec_dev_private *internals = dev->data->dev_private;
19781f14d500SAkhil Goyal 	struct rte_security_ipsec_xform *ipsec_xform = &conf->ipsec;
197905b12700SHemant Agrawal 	struct rte_crypto_auth_xform *auth_xform = NULL;
198005b12700SHemant Agrawal 	struct rte_crypto_cipher_xform *cipher_xform = NULL;
19811f14d500SAkhil Goyal 	dpaa_sec_session *session = (dpaa_sec_session *)sess;
19821f14d500SAkhil Goyal 
19831f14d500SAkhil Goyal 	PMD_INIT_FUNC_TRACE();
19841f14d500SAkhil Goyal 
1985b0894102SAkhil Goyal 	memset(session, 0, sizeof(dpaa_sec_session));
19861f14d500SAkhil Goyal 	if (ipsec_xform->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
19871f14d500SAkhil Goyal 		cipher_xform = &conf->crypto_xform->cipher;
198805b12700SHemant Agrawal 		if (conf->crypto_xform->next)
19891f14d500SAkhil Goyal 			auth_xform = &conf->crypto_xform->next->auth;
19901f14d500SAkhil Goyal 	} else {
19911f14d500SAkhil Goyal 		auth_xform = &conf->crypto_xform->auth;
199205b12700SHemant Agrawal 		if (conf->crypto_xform->next)
19931f14d500SAkhil Goyal 			cipher_xform = &conf->crypto_xform->next->cipher;
19941f14d500SAkhil Goyal 	}
19951f14d500SAkhil Goyal 	session->proto_alg = conf->protocol;
199605b12700SHemant Agrawal 
199705b12700SHemant Agrawal 	if (cipher_xform && cipher_xform->algo != RTE_CRYPTO_CIPHER_NULL) {
19981f14d500SAkhil Goyal 		session->cipher_key.data = rte_zmalloc(NULL,
19991f14d500SAkhil Goyal 						       cipher_xform->key.length,
20001f14d500SAkhil Goyal 						       RTE_CACHE_LINE_SIZE);
20011f14d500SAkhil Goyal 		if (session->cipher_key.data == NULL &&
20021f14d500SAkhil Goyal 				cipher_xform->key.length > 0) {
2003f163231eSHemant Agrawal 			DPAA_SEC_ERR("No Memory for cipher key");
20041f14d500SAkhil Goyal 			return -ENOMEM;
20051f14d500SAkhil Goyal 		}
200605b12700SHemant Agrawal 		memcpy(session->cipher_key.data, cipher_xform->key.data,
200705b12700SHemant Agrawal 				cipher_xform->key.length);
20081f14d500SAkhil Goyal 		session->cipher_key.length = cipher_xform->key.length;
200905b12700SHemant Agrawal 
201005b12700SHemant Agrawal 		switch (cipher_xform->algo) {
201105b12700SHemant Agrawal 		case RTE_CRYPTO_CIPHER_AES_CBC:
201205b12700SHemant Agrawal 		case RTE_CRYPTO_CIPHER_3DES_CBC:
201305b12700SHemant Agrawal 		case RTE_CRYPTO_CIPHER_AES_CTR:
201405b12700SHemant Agrawal 			break;
201505b12700SHemant Agrawal 		default:
201605b12700SHemant Agrawal 			DPAA_SEC_ERR("Crypto: Unsupported Cipher alg %u",
201705b12700SHemant Agrawal 				cipher_xform->algo);
201805b12700SHemant Agrawal 			goto out;
201905b12700SHemant Agrawal 		}
202005b12700SHemant Agrawal 		session->cipher_alg = cipher_xform->algo;
202105b12700SHemant Agrawal 	} else {
202205b12700SHemant Agrawal 		session->cipher_key.data = NULL;
202305b12700SHemant Agrawal 		session->cipher_key.length = 0;
202405b12700SHemant Agrawal 		session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
202505b12700SHemant Agrawal 	}
202605b12700SHemant Agrawal 
202705b12700SHemant Agrawal 	if (auth_xform && auth_xform->algo != RTE_CRYPTO_AUTH_NULL) {
20281f14d500SAkhil Goyal 		session->auth_key.data = rte_zmalloc(NULL,
20291f14d500SAkhil Goyal 						auth_xform->key.length,
20301f14d500SAkhil Goyal 						RTE_CACHE_LINE_SIZE);
20311f14d500SAkhil Goyal 		if (session->auth_key.data == NULL &&
20321f14d500SAkhil Goyal 				auth_xform->key.length > 0) {
2033f163231eSHemant Agrawal 			DPAA_SEC_ERR("No Memory for auth key");
20341f14d500SAkhil Goyal 			rte_free(session->cipher_key.data);
20351f14d500SAkhil Goyal 			return -ENOMEM;
20361f14d500SAkhil Goyal 		}
20371f14d500SAkhil Goyal 		memcpy(session->auth_key.data, auth_xform->key.data,
20381f14d500SAkhil Goyal 				auth_xform->key.length);
203905b12700SHemant Agrawal 		session->auth_key.length = auth_xform->key.length;
20401f14d500SAkhil Goyal 
20411f14d500SAkhil Goyal 		switch (auth_xform->algo) {
20421f14d500SAkhil Goyal 		case RTE_CRYPTO_AUTH_SHA1_HMAC:
20431f14d500SAkhil Goyal 		case RTE_CRYPTO_AUTH_MD5_HMAC:
20441f14d500SAkhil Goyal 		case RTE_CRYPTO_AUTH_SHA256_HMAC:
20451f14d500SAkhil Goyal 		case RTE_CRYPTO_AUTH_SHA384_HMAC:
20461f14d500SAkhil Goyal 		case RTE_CRYPTO_AUTH_SHA512_HMAC:
20471f14d500SAkhil Goyal 		case RTE_CRYPTO_AUTH_AES_CMAC:
20481f14d500SAkhil Goyal 			break;
204905b12700SHemant Agrawal 		default:
2050f163231eSHemant Agrawal 			DPAA_SEC_ERR("Crypto: Unsupported auth alg %u",
20511f14d500SAkhil Goyal 				auth_xform->algo);
20521f14d500SAkhil Goyal 			goto out;
20531f14d500SAkhil Goyal 		}
205405b12700SHemant Agrawal 		session->auth_alg = auth_xform->algo;
205505b12700SHemant Agrawal 	} else {
205605b12700SHemant Agrawal 		session->auth_key.data = NULL;
205705b12700SHemant Agrawal 		session->auth_key.length = 0;
205805b12700SHemant Agrawal 		session->auth_alg = RTE_CRYPTO_AUTH_NULL;
20591f14d500SAkhil Goyal 	}
20601f14d500SAkhil Goyal 
20611f14d500SAkhil Goyal 	if (ipsec_xform->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
20621f14d500SAkhil Goyal 		memset(&session->encap_pdb, 0, sizeof(struct ipsec_encap_pdb) +
20631f14d500SAkhil Goyal 				sizeof(session->ip4_hdr));
20641f14d500SAkhil Goyal 		session->ip4_hdr.ip_v = IPVERSION;
20651f14d500SAkhil Goyal 		session->ip4_hdr.ip_hl = 5;
20661f14d500SAkhil Goyal 		session->ip4_hdr.ip_len = rte_cpu_to_be_16(
20671f14d500SAkhil Goyal 						sizeof(session->ip4_hdr));
20681f14d500SAkhil Goyal 		session->ip4_hdr.ip_tos = ipsec_xform->tunnel.ipv4.dscp;
20691f14d500SAkhil Goyal 		session->ip4_hdr.ip_id = 0;
20701f14d500SAkhil Goyal 		session->ip4_hdr.ip_off = 0;
20711f14d500SAkhil Goyal 		session->ip4_hdr.ip_ttl = ipsec_xform->tunnel.ipv4.ttl;
20721f14d500SAkhil Goyal 		session->ip4_hdr.ip_p = (ipsec_xform->proto ==
20731f14d500SAkhil Goyal 				RTE_SECURITY_IPSEC_SA_PROTO_ESP) ? IPPROTO_ESP
20741f14d500SAkhil Goyal 				: IPPROTO_AH;
20751f14d500SAkhil Goyal 		session->ip4_hdr.ip_sum = 0;
20761f14d500SAkhil Goyal 		session->ip4_hdr.ip_src = ipsec_xform->tunnel.ipv4.src_ip;
20771f14d500SAkhil Goyal 		session->ip4_hdr.ip_dst = ipsec_xform->tunnel.ipv4.dst_ip;
20781f14d500SAkhil Goyal 		session->ip4_hdr.ip_sum = calc_chksum((uint16_t *)
20791f14d500SAkhil Goyal 						(void *)&session->ip4_hdr,
20801f14d500SAkhil Goyal 						sizeof(struct ip));
20811f14d500SAkhil Goyal 
20821f14d500SAkhil Goyal 		session->encap_pdb.options =
20831f14d500SAkhil Goyal 			(IPVERSION << PDBNH_ESP_ENCAP_SHIFT) |
20841f14d500SAkhil Goyal 			PDBOPTS_ESP_OIHI_PDB_INL |
20851f14d500SAkhil Goyal 			PDBOPTS_ESP_IVSRC |
208679fde9d0SAkhil Goyal 			PDBHMO_ESP_ENCAP_DTTL |
208779fde9d0SAkhil Goyal 			PDBHMO_ESP_SNR;
20881f14d500SAkhil Goyal 		session->encap_pdb.spi = ipsec_xform->spi;
20891f14d500SAkhil Goyal 		session->encap_pdb.ip_hdr_len = sizeof(struct ip);
20901f14d500SAkhil Goyal 
20911f14d500SAkhil Goyal 		session->dir = DIR_ENC;
20921f14d500SAkhil Goyal 	} else if (ipsec_xform->direction ==
20931f14d500SAkhil Goyal 			RTE_SECURITY_IPSEC_SA_DIR_INGRESS) {
20941f14d500SAkhil Goyal 		memset(&session->decap_pdb, 0, sizeof(struct ipsec_decap_pdb));
20951f14d500SAkhil Goyal 		session->decap_pdb.options = sizeof(struct ip) << 16;
20961f14d500SAkhil Goyal 		session->dir = DIR_DEC;
20971f14d500SAkhil Goyal 	} else
20981f14d500SAkhil Goyal 		goto out;
20991f14d500SAkhil Goyal 	session->ctx_pool = internals->ctx_pool;
21003b617ee7SAkhil Goyal 	rte_spinlock_lock(&internals->lock);
21011f14d500SAkhil Goyal 	session->inq = dpaa_sec_attach_rxq(internals);
21023b617ee7SAkhil Goyal 	rte_spinlock_unlock(&internals->lock);
21031f14d500SAkhil Goyal 	if (session->inq == NULL) {
2104f163231eSHemant Agrawal 		DPAA_SEC_ERR("unable to attach sec queue");
21051f14d500SAkhil Goyal 		goto out;
21061f14d500SAkhil Goyal 	}
21071f14d500SAkhil Goyal 
21081f14d500SAkhil Goyal 
21091f14d500SAkhil Goyal 	return 0;
21101f14d500SAkhil Goyal out:
21111f14d500SAkhil Goyal 	rte_free(session->auth_key.data);
21121f14d500SAkhil Goyal 	rte_free(session->cipher_key.data);
21131f14d500SAkhil Goyal 	memset(session, 0, sizeof(dpaa_sec_session));
21141f14d500SAkhil Goyal 	return -1;
21151f14d500SAkhil Goyal }
21161f14d500SAkhil Goyal 
21171f14d500SAkhil Goyal static int
21181f14d500SAkhil Goyal dpaa_sec_security_session_create(void *dev,
21191f14d500SAkhil Goyal 				 struct rte_security_session_conf *conf,
21201f14d500SAkhil Goyal 				 struct rte_security_session *sess,
21211f14d500SAkhil Goyal 				 struct rte_mempool *mempool)
21221f14d500SAkhil Goyal {
21231f14d500SAkhil Goyal 	void *sess_private_data;
21241f14d500SAkhil Goyal 	struct rte_cryptodev *cdev = (struct rte_cryptodev *)dev;
21251f14d500SAkhil Goyal 	int ret;
21261f14d500SAkhil Goyal 
21271f14d500SAkhil Goyal 	if (rte_mempool_get(mempool, &sess_private_data)) {
2128f163231eSHemant Agrawal 		DPAA_SEC_ERR("Couldn't get object from session mempool");
21291f14d500SAkhil Goyal 		return -ENOMEM;
21301f14d500SAkhil Goyal 	}
21311f14d500SAkhil Goyal 
21321f14d500SAkhil Goyal 	switch (conf->protocol) {
21331f14d500SAkhil Goyal 	case RTE_SECURITY_PROTOCOL_IPSEC:
21341f14d500SAkhil Goyal 		ret = dpaa_sec_set_ipsec_session(cdev, conf,
21351f14d500SAkhil Goyal 				sess_private_data);
21361f14d500SAkhil Goyal 		break;
21371f14d500SAkhil Goyal 	case RTE_SECURITY_PROTOCOL_MACSEC:
21381f14d500SAkhil Goyal 		return -ENOTSUP;
21391f14d500SAkhil Goyal 	default:
21401f14d500SAkhil Goyal 		return -EINVAL;
21411f14d500SAkhil Goyal 	}
21421f14d500SAkhil Goyal 	if (ret != 0) {
2143f163231eSHemant Agrawal 		DPAA_SEC_ERR("failed to configure session parameters");
21441f14d500SAkhil Goyal 		/* Return session to mempool */
21451f14d500SAkhil Goyal 		rte_mempool_put(mempool, sess_private_data);
21461f14d500SAkhil Goyal 		return ret;
21471f14d500SAkhil Goyal 	}
21481f14d500SAkhil Goyal 
21491f14d500SAkhil Goyal 	set_sec_session_private_data(sess, sess_private_data);
21501f14d500SAkhil Goyal 
21511f14d500SAkhil Goyal 	return ret;
21521f14d500SAkhil Goyal }
21531f14d500SAkhil Goyal 
21541f14d500SAkhil Goyal /** Clear the memory of session so it doesn't leave key material behind */
21551f14d500SAkhil Goyal static int
21561f14d500SAkhil Goyal dpaa_sec_security_session_destroy(void *dev __rte_unused,
21571f14d500SAkhil Goyal 		struct rte_security_session *sess)
21581f14d500SAkhil Goyal {
21591f14d500SAkhil Goyal 	PMD_INIT_FUNC_TRACE();
21601f14d500SAkhil Goyal 	void *sess_priv = get_sec_session_private_data(sess);
21611f14d500SAkhil Goyal 
21621f14d500SAkhil Goyal 	dpaa_sec_session *s = (dpaa_sec_session *)sess_priv;
21631f14d500SAkhil Goyal 
21641f14d500SAkhil Goyal 	if (sess_priv) {
21651f14d500SAkhil Goyal 		struct rte_mempool *sess_mp = rte_mempool_from_obj(sess_priv);
21661f14d500SAkhil Goyal 
21671f14d500SAkhil Goyal 		rte_free(s->cipher_key.data);
21681f14d500SAkhil Goyal 		rte_free(s->auth_key.data);
21691f14d500SAkhil Goyal 		memset(sess, 0, sizeof(dpaa_sec_session));
21701f14d500SAkhil Goyal 		set_sec_session_private_data(sess, NULL);
21711f14d500SAkhil Goyal 		rte_mempool_put(sess_mp, sess_priv);
21721f14d500SAkhil Goyal 	}
21731f14d500SAkhil Goyal 	return 0;
21741f14d500SAkhil Goyal }
21751f14d500SAkhil Goyal 
21761f14d500SAkhil Goyal 
21771f14d500SAkhil Goyal static int
21787e3e2954SAkhil Goyal dpaa_sec_dev_configure(struct rte_cryptodev *dev,
2179c3e85bdcSAkhil Goyal 		       struct rte_cryptodev_config *config __rte_unused)
2180c3e85bdcSAkhil Goyal {
21817e3e2954SAkhil Goyal 
21827e3e2954SAkhil Goyal 	char str[20];
21837e3e2954SAkhil Goyal 	struct dpaa_sec_dev_private *internals;
21847e3e2954SAkhil Goyal 
2185c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2186c3e85bdcSAkhil Goyal 
21877e3e2954SAkhil Goyal 	internals = dev->data->dev_private;
21887e3e2954SAkhil Goyal 	sprintf(str, "ctx_pool_%d", dev->data->dev_id);
21897e3e2954SAkhil Goyal 	if (!internals->ctx_pool) {
21907e3e2954SAkhil Goyal 		internals->ctx_pool = rte_mempool_create((const char *)str,
21917e3e2954SAkhil Goyal 							CTX_POOL_NUM_BUFS,
21927e3e2954SAkhil Goyal 							CTX_POOL_BUF_SIZE,
21937e3e2954SAkhil Goyal 							CTX_POOL_CACHE_SIZE, 0,
21947e3e2954SAkhil Goyal 							NULL, NULL, NULL, NULL,
21957e3e2954SAkhil Goyal 							SOCKET_ID_ANY, 0);
21967e3e2954SAkhil Goyal 		if (!internals->ctx_pool) {
2197f163231eSHemant Agrawal 			DPAA_SEC_ERR("%s create failed\n", str);
21987e3e2954SAkhil Goyal 			return -ENOMEM;
21997e3e2954SAkhil Goyal 		}
22007e3e2954SAkhil Goyal 	} else
2201f163231eSHemant Agrawal 		DPAA_SEC_INFO("mempool already created for dev_id : %d",
22027e3e2954SAkhil Goyal 				dev->data->dev_id);
22037e3e2954SAkhil Goyal 
2204c3e85bdcSAkhil Goyal 	return 0;
2205c3e85bdcSAkhil Goyal }
2206c3e85bdcSAkhil Goyal 
2207c3e85bdcSAkhil Goyal static int
2208c3e85bdcSAkhil Goyal dpaa_sec_dev_start(struct rte_cryptodev *dev __rte_unused)
2209c3e85bdcSAkhil Goyal {
2210c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2211c3e85bdcSAkhil Goyal 	return 0;
2212c3e85bdcSAkhil Goyal }
2213c3e85bdcSAkhil Goyal 
2214c3e85bdcSAkhil Goyal static void
2215c3e85bdcSAkhil Goyal dpaa_sec_dev_stop(struct rte_cryptodev *dev __rte_unused)
2216c3e85bdcSAkhil Goyal {
2217c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2218c3e85bdcSAkhil Goyal }
2219c3e85bdcSAkhil Goyal 
2220c3e85bdcSAkhil Goyal static int
22217e3e2954SAkhil Goyal dpaa_sec_dev_close(struct rte_cryptodev *dev)
2222c3e85bdcSAkhil Goyal {
22237e3e2954SAkhil Goyal 	struct dpaa_sec_dev_private *internals;
22247e3e2954SAkhil Goyal 
2225c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
22267e3e2954SAkhil Goyal 
22277e3e2954SAkhil Goyal 	if (dev == NULL)
22287e3e2954SAkhil Goyal 		return -ENOMEM;
22297e3e2954SAkhil Goyal 
22307e3e2954SAkhil Goyal 	internals = dev->data->dev_private;
22317e3e2954SAkhil Goyal 	rte_mempool_free(internals->ctx_pool);
22327e3e2954SAkhil Goyal 	internals->ctx_pool = NULL;
22337e3e2954SAkhil Goyal 
2234c3e85bdcSAkhil Goyal 	return 0;
2235c3e85bdcSAkhil Goyal }
2236c3e85bdcSAkhil Goyal 
2237c3e85bdcSAkhil Goyal static void
2238c3e85bdcSAkhil Goyal dpaa_sec_dev_infos_get(struct rte_cryptodev *dev,
2239c3e85bdcSAkhil Goyal 		       struct rte_cryptodev_info *info)
2240c3e85bdcSAkhil Goyal {
2241c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals = dev->data->dev_private;
2242c3e85bdcSAkhil Goyal 
2243c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2244c3e85bdcSAkhil Goyal 	if (info != NULL) {
2245c3e85bdcSAkhil Goyal 		info->max_nb_queue_pairs = internals->max_nb_queue_pairs;
2246c3e85bdcSAkhil Goyal 		info->feature_flags = dev->feature_flags;
2247c3e85bdcSAkhil Goyal 		info->capabilities = dpaa_sec_capabilities;
2248c3e85bdcSAkhil Goyal 		info->sym.max_nb_sessions = internals->max_nb_sessions;
2249c3e85bdcSAkhil Goyal 		info->driver_id = cryptodev_driver_id;
2250c3e85bdcSAkhil Goyal 	}
2251c3e85bdcSAkhil Goyal }
2252c3e85bdcSAkhil Goyal 
2253c3e85bdcSAkhil Goyal static struct rte_cryptodev_ops crypto_ops = {
2254c3e85bdcSAkhil Goyal 	.dev_configure	      = dpaa_sec_dev_configure,
2255c3e85bdcSAkhil Goyal 	.dev_start	      = dpaa_sec_dev_start,
2256c3e85bdcSAkhil Goyal 	.dev_stop	      = dpaa_sec_dev_stop,
2257c3e85bdcSAkhil Goyal 	.dev_close	      = dpaa_sec_dev_close,
2258c3e85bdcSAkhil Goyal 	.dev_infos_get        = dpaa_sec_dev_infos_get,
2259c3e85bdcSAkhil Goyal 	.queue_pair_setup     = dpaa_sec_queue_pair_setup,
2260c3e85bdcSAkhil Goyal 	.queue_pair_release   = dpaa_sec_queue_pair_release,
2261c3e85bdcSAkhil Goyal 	.queue_pair_count     = dpaa_sec_queue_pair_count,
2262012c5076SPablo de Lara 	.sym_session_get_size     = dpaa_sec_sym_session_get_size,
2263012c5076SPablo de Lara 	.sym_session_configure    = dpaa_sec_sym_session_configure,
2264012c5076SPablo de Lara 	.sym_session_clear        = dpaa_sec_sym_session_clear
2265c3e85bdcSAkhil Goyal };
2266c3e85bdcSAkhil Goyal 
22671f14d500SAkhil Goyal static const struct rte_security_capability *
22681f14d500SAkhil Goyal dpaa_sec_capabilities_get(void *device __rte_unused)
22691f14d500SAkhil Goyal {
22701f14d500SAkhil Goyal 	return dpaa_sec_security_cap;
22711f14d500SAkhil Goyal }
22721f14d500SAkhil Goyal 
22731f14d500SAkhil Goyal struct rte_security_ops dpaa_sec_security_ops = {
22741f14d500SAkhil Goyal 	.session_create = dpaa_sec_security_session_create,
22751f14d500SAkhil Goyal 	.session_update = NULL,
22761f14d500SAkhil Goyal 	.session_stats_get = NULL,
22771f14d500SAkhil Goyal 	.session_destroy = dpaa_sec_security_session_destroy,
22781f14d500SAkhil Goyal 	.set_pkt_metadata = NULL,
22791f14d500SAkhil Goyal 	.capabilities_get = dpaa_sec_capabilities_get
22801f14d500SAkhil Goyal };
22811f14d500SAkhil Goyal 
2282c3e85bdcSAkhil Goyal static int
2283c3e85bdcSAkhil Goyal dpaa_sec_uninit(struct rte_cryptodev *dev)
2284c3e85bdcSAkhil Goyal {
2285debef417SShreyansh Jain 	struct dpaa_sec_dev_private *internals;
2286c3e85bdcSAkhil Goyal 
2287c3e85bdcSAkhil Goyal 	if (dev == NULL)
2288c3e85bdcSAkhil Goyal 		return -ENODEV;
2289c3e85bdcSAkhil Goyal 
2290debef417SShreyansh Jain 	internals = dev->data->dev_private;
22911f14d500SAkhil Goyal 	rte_free(dev->security_ctx);
22921f14d500SAkhil Goyal 
22937e3e2954SAkhil Goyal 	/* In case close has been called, internals->ctx_pool would be NULL */
2294c3e85bdcSAkhil Goyal 	rte_mempool_free(internals->ctx_pool);
2295c3e85bdcSAkhil Goyal 	rte_free(internals);
2296c3e85bdcSAkhil Goyal 
2297f163231eSHemant Agrawal 	DPAA_SEC_INFO("Closing DPAA_SEC device %s on numa socket %u",
2298c3e85bdcSAkhil Goyal 		      dev->data->name, rte_socket_id());
2299c3e85bdcSAkhil Goyal 
2300c3e85bdcSAkhil Goyal 	return 0;
2301c3e85bdcSAkhil Goyal }
2302c3e85bdcSAkhil Goyal 
2303c3e85bdcSAkhil Goyal static int
2304c3e85bdcSAkhil Goyal dpaa_sec_dev_init(struct rte_cryptodev *cryptodev)
2305c3e85bdcSAkhil Goyal {
2306c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals;
23071f14d500SAkhil Goyal 	struct rte_security_ctx *security_instance;
2308c3e85bdcSAkhil Goyal 	struct dpaa_sec_qp *qp;
2309e79416d1SHemant Agrawal 	uint32_t i, flags;
2310c3e85bdcSAkhil Goyal 	int ret;
2311c3e85bdcSAkhil Goyal 
2312c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2313c3e85bdcSAkhil Goyal 
2314c3e85bdcSAkhil Goyal 	cryptodev->driver_id = cryptodev_driver_id;
2315c3e85bdcSAkhil Goyal 	cryptodev->dev_ops = &crypto_ops;
2316c3e85bdcSAkhil Goyal 
2317c3e85bdcSAkhil Goyal 	cryptodev->enqueue_burst = dpaa_sec_enqueue_burst;
2318c3e85bdcSAkhil Goyal 	cryptodev->dequeue_burst = dpaa_sec_dequeue_burst;
2319c3e85bdcSAkhil Goyal 	cryptodev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
2320c3e85bdcSAkhil Goyal 			RTE_CRYPTODEV_FF_HW_ACCELERATED |
23211f14d500SAkhil Goyal 			RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
2322a74af788SAkhil Goyal 			RTE_CRYPTODEV_FF_SECURITY |
23232717246eSPablo de Lara 			RTE_CRYPTODEV_FF_IN_PLACE_SGL |
23242717246eSPablo de Lara 			RTE_CRYPTODEV_FF_OOP_SGL_IN_SGL_OUT |
23252717246eSPablo de Lara 			RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT |
23262717246eSPablo de Lara 			RTE_CRYPTODEV_FF_OOP_LB_IN_SGL_OUT |
23272717246eSPablo de Lara 			RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT;
2328c3e85bdcSAkhil Goyal 
2329c3e85bdcSAkhil Goyal 	internals = cryptodev->data->dev_private;
2330e79416d1SHemant Agrawal 	internals->max_nb_queue_pairs = RTE_DPAA_MAX_NB_SEC_QPS;
2331c3e85bdcSAkhil Goyal 	internals->max_nb_sessions = RTE_DPAA_SEC_PMD_MAX_NB_SESSIONS;
2332c3e85bdcSAkhil Goyal 
23331f14d500SAkhil Goyal 	/*
23341f14d500SAkhil Goyal 	 * For secondary processes, we don't initialise any further as primary
23351f14d500SAkhil Goyal 	 * has already done this work. Only check we don't need a different
23361f14d500SAkhil Goyal 	 * RX function
23371f14d500SAkhil Goyal 	 */
23381f14d500SAkhil Goyal 	if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
2339f163231eSHemant Agrawal 		DPAA_SEC_WARN("Device already init by primary process");
23401f14d500SAkhil Goyal 		return 0;
23411f14d500SAkhil Goyal 	}
23421f14d500SAkhil Goyal 
23431f14d500SAkhil Goyal 	/* Initialize security_ctx only for primary process*/
23441f14d500SAkhil Goyal 	security_instance = rte_malloc("rte_security_instances_ops",
23451f14d500SAkhil Goyal 				sizeof(struct rte_security_ctx), 0);
23461f14d500SAkhil Goyal 	if (security_instance == NULL)
23471f14d500SAkhil Goyal 		return -ENOMEM;
23481f14d500SAkhil Goyal 	security_instance->device = (void *)cryptodev;
23491f14d500SAkhil Goyal 	security_instance->ops = &dpaa_sec_security_ops;
23501f14d500SAkhil Goyal 	security_instance->sess_cnt = 0;
23511f14d500SAkhil Goyal 	cryptodev->security_ctx = security_instance;
23521f14d500SAkhil Goyal 
23533b617ee7SAkhil Goyal 	rte_spinlock_init(&internals->lock);
2354c3e85bdcSAkhil Goyal 	for (i = 0; i < internals->max_nb_queue_pairs; i++) {
2355c3e85bdcSAkhil Goyal 		/* init qman fq for queue pair */
2356c3e85bdcSAkhil Goyal 		qp = &internals->qps[i];
2357c3e85bdcSAkhil Goyal 		ret = dpaa_sec_init_tx(&qp->outq);
2358c3e85bdcSAkhil Goyal 		if (ret) {
2359f163231eSHemant Agrawal 			DPAA_SEC_ERR("config tx of queue pair  %d", i);
2360c3e85bdcSAkhil Goyal 			goto init_error;
2361c3e85bdcSAkhil Goyal 		}
2362e79416d1SHemant Agrawal 	}
2363e79416d1SHemant Agrawal 
2364e79416d1SHemant Agrawal 	flags = QMAN_FQ_FLAG_LOCKED | QMAN_FQ_FLAG_DYNAMIC_FQID |
2365e79416d1SHemant Agrawal 		QMAN_FQ_FLAG_TO_DCPORTAL;
2366e79416d1SHemant Agrawal 	for (i = 0; i < internals->max_nb_sessions; i++) {
2367e79416d1SHemant Agrawal 		/* create rx qman fq for sessions*/
2368e79416d1SHemant Agrawal 		ret = qman_create_fq(0, flags, &internals->inq[i]);
2369e79416d1SHemant Agrawal 		if (unlikely(ret != 0)) {
2370f163231eSHemant Agrawal 			DPAA_SEC_ERR("sec qman_create_fq failed");
2371c3e85bdcSAkhil Goyal 			goto init_error;
2372c3e85bdcSAkhil Goyal 		}
2373c3e85bdcSAkhil Goyal 	}
2374c3e85bdcSAkhil Goyal 
2375f163231eSHemant Agrawal 	RTE_LOG(INFO, PMD, "%s cryptodev init\n", cryptodev->data->name);
2376c3e85bdcSAkhil Goyal 	return 0;
2377c3e85bdcSAkhil Goyal 
2378c3e85bdcSAkhil Goyal init_error:
2379f163231eSHemant Agrawal 	DPAA_SEC_ERR("driver %s: create failed\n", cryptodev->data->name);
2380c3e85bdcSAkhil Goyal 
2381c3e85bdcSAkhil Goyal 	dpaa_sec_uninit(cryptodev);
2382c3e85bdcSAkhil Goyal 	return -EFAULT;
2383c3e85bdcSAkhil Goyal }
2384c3e85bdcSAkhil Goyal 
2385c3e85bdcSAkhil Goyal static int
2386eb5a9a76SShreyansh Jain cryptodev_dpaa_sec_probe(struct rte_dpaa_driver *dpaa_drv __rte_unused,
2387c3e85bdcSAkhil Goyal 				struct rte_dpaa_device *dpaa_dev)
2388c3e85bdcSAkhil Goyal {
2389c3e85bdcSAkhil Goyal 	struct rte_cryptodev *cryptodev;
2390c3e85bdcSAkhil Goyal 	char cryptodev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
2391c3e85bdcSAkhil Goyal 
2392c3e85bdcSAkhil Goyal 	int retval;
2393c3e85bdcSAkhil Goyal 
2394c3e85bdcSAkhil Goyal 	sprintf(cryptodev_name, "dpaa_sec-%d", dpaa_dev->id.dev_id);
2395c3e85bdcSAkhil Goyal 
2396c3e85bdcSAkhil Goyal 	cryptodev = rte_cryptodev_pmd_allocate(cryptodev_name, rte_socket_id());
2397c3e85bdcSAkhil Goyal 	if (cryptodev == NULL)
2398c3e85bdcSAkhil Goyal 		return -ENOMEM;
2399c3e85bdcSAkhil Goyal 
2400c3e85bdcSAkhil Goyal 	if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
2401c3e85bdcSAkhil Goyal 		cryptodev->data->dev_private = rte_zmalloc_socket(
2402c3e85bdcSAkhil Goyal 					"cryptodev private structure",
2403c3e85bdcSAkhil Goyal 					sizeof(struct dpaa_sec_dev_private),
2404c3e85bdcSAkhil Goyal 					RTE_CACHE_LINE_SIZE,
2405c3e85bdcSAkhil Goyal 					rte_socket_id());
2406c3e85bdcSAkhil Goyal 
2407c3e85bdcSAkhil Goyal 		if (cryptodev->data->dev_private == NULL)
2408c3e85bdcSAkhil Goyal 			rte_panic("Cannot allocate memzone for private "
2409c3e85bdcSAkhil Goyal 					"device data");
2410c3e85bdcSAkhil Goyal 	}
2411c3e85bdcSAkhil Goyal 
2412c3e85bdcSAkhil Goyal 	dpaa_dev->crypto_dev = cryptodev;
2413c3e85bdcSAkhil Goyal 	cryptodev->device = &dpaa_dev->device;
2414c3e85bdcSAkhil Goyal 
2415c3e85bdcSAkhil Goyal 	/* init user callbacks */
2416c3e85bdcSAkhil Goyal 	TAILQ_INIT(&(cryptodev->link_intr_cbs));
2417c3e85bdcSAkhil Goyal 
2418c3e85bdcSAkhil Goyal 	/* if sec device version is not configured */
2419c3e85bdcSAkhil Goyal 	if (!rta_get_sec_era()) {
2420c3e85bdcSAkhil Goyal 		const struct device_node *caam_node;
2421c3e85bdcSAkhil Goyal 
2422c3e85bdcSAkhil Goyal 		for_each_compatible_node(caam_node, NULL, "fsl,sec-v4.0") {
2423c3e85bdcSAkhil Goyal 			const uint32_t *prop = of_get_property(caam_node,
2424c3e85bdcSAkhil Goyal 					"fsl,sec-era",
2425c3e85bdcSAkhil Goyal 					NULL);
2426c3e85bdcSAkhil Goyal 			if (prop) {
2427c3e85bdcSAkhil Goyal 				rta_set_sec_era(
2428c3e85bdcSAkhil Goyal 					INTL_SEC_ERA(rte_cpu_to_be_32(*prop)));
2429c3e85bdcSAkhil Goyal 				break;
2430c3e85bdcSAkhil Goyal 			}
2431c3e85bdcSAkhil Goyal 		}
2432c3e85bdcSAkhil Goyal 	}
2433c3e85bdcSAkhil Goyal 
2434c3e85bdcSAkhil Goyal 	/* Invoke PMD device initialization function */
2435c3e85bdcSAkhil Goyal 	retval = dpaa_sec_dev_init(cryptodev);
2436c3e85bdcSAkhil Goyal 	if (retval == 0)
2437c3e85bdcSAkhil Goyal 		return 0;
2438c3e85bdcSAkhil Goyal 
2439c3e85bdcSAkhil Goyal 	/* In case of error, cleanup is done */
2440c3e85bdcSAkhil Goyal 	if (rte_eal_process_type() == RTE_PROC_PRIMARY)
2441c3e85bdcSAkhil Goyal 		rte_free(cryptodev->data->dev_private);
2442c3e85bdcSAkhil Goyal 
2443c3e85bdcSAkhil Goyal 	rte_cryptodev_pmd_release_device(cryptodev);
2444c3e85bdcSAkhil Goyal 
2445c3e85bdcSAkhil Goyal 	return -ENXIO;
2446c3e85bdcSAkhil Goyal }
2447c3e85bdcSAkhil Goyal 
2448c3e85bdcSAkhil Goyal static int
2449c3e85bdcSAkhil Goyal cryptodev_dpaa_sec_remove(struct rte_dpaa_device *dpaa_dev)
2450c3e85bdcSAkhil Goyal {
2451c3e85bdcSAkhil Goyal 	struct rte_cryptodev *cryptodev;
2452c3e85bdcSAkhil Goyal 	int ret;
2453c3e85bdcSAkhil Goyal 
2454c3e85bdcSAkhil Goyal 	cryptodev = dpaa_dev->crypto_dev;
2455c3e85bdcSAkhil Goyal 	if (cryptodev == NULL)
2456c3e85bdcSAkhil Goyal 		return -ENODEV;
2457c3e85bdcSAkhil Goyal 
2458c3e85bdcSAkhil Goyal 	ret = dpaa_sec_uninit(cryptodev);
2459c3e85bdcSAkhil Goyal 	if (ret)
2460c3e85bdcSAkhil Goyal 		return ret;
2461c3e85bdcSAkhil Goyal 
2462f2f020d2SDeclan Doherty 	return rte_cryptodev_pmd_destroy(cryptodev);
2463c3e85bdcSAkhil Goyal }
2464c3e85bdcSAkhil Goyal 
2465c3e85bdcSAkhil Goyal static struct rte_dpaa_driver rte_dpaa_sec_driver = {
2466c3e85bdcSAkhil Goyal 	.drv_type = FSL_DPAA_CRYPTO,
2467c3e85bdcSAkhil Goyal 	.driver = {
2468c3e85bdcSAkhil Goyal 		.name = "DPAA SEC PMD"
2469c3e85bdcSAkhil Goyal 	},
2470c3e85bdcSAkhil Goyal 	.probe = cryptodev_dpaa_sec_probe,
2471c3e85bdcSAkhil Goyal 	.remove = cryptodev_dpaa_sec_remove,
2472c3e85bdcSAkhil Goyal };
2473c3e85bdcSAkhil Goyal 
2474c3e85bdcSAkhil Goyal static struct cryptodev_driver dpaa_sec_crypto_drv;
2475c3e85bdcSAkhil Goyal 
2476c3e85bdcSAkhil Goyal RTE_PMD_REGISTER_DPAA(CRYPTODEV_NAME_DPAA_SEC_PMD, rte_dpaa_sec_driver);
2477f737f5ceSFiona Trahe RTE_PMD_REGISTER_CRYPTO_DRIVER(dpaa_sec_crypto_drv, rte_dpaa_sec_driver.driver,
2478c3e85bdcSAkhil Goyal 		cryptodev_driver_id);
2479f163231eSHemant Agrawal 
2480f8e99896SThomas Monjalon RTE_INIT(dpaa_sec_init_log)
2481f163231eSHemant Agrawal {
2482f163231eSHemant Agrawal 	dpaa_logtype_sec = rte_log_register("pmd.crypto.dpaa");
2483f163231eSHemant Agrawal 	if (dpaa_logtype_sec >= 0)
2484f163231eSHemant Agrawal 		rte_log_set_level(dpaa_logtype_sec, RTE_LOG_NOTICE);
2485f163231eSHemant Agrawal }
2486