xref: /dpdk/drivers/crypto/dpaa_sec/dpaa_sec.c (revision c5788a1097ac998dc47727cc29dba312d190923a)
1d81734caSHemant Agrawal /* SPDX-License-Identifier: BSD-3-Clause
2c3e85bdcSAkhil Goyal  *
3c3e85bdcSAkhil Goyal  *   Copyright (c) 2016 Freescale Semiconductor, Inc. All rights reserved.
4e621d970SAkhil Goyal  *   Copyright 2017-2019 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>
36a1173d55SHemant Agrawal #include <hw/desc/pdcp.h>
37c3e85bdcSAkhil Goyal 
38c3e85bdcSAkhil Goyal #include <rte_dpaa_bus.h>
39c3e85bdcSAkhil Goyal #include <dpaa_sec.h>
40fe3688baSAkhil Goyal #include <dpaa_sec_event.h>
41c3e85bdcSAkhil Goyal #include <dpaa_sec_log.h>
4212e58429SThierry Herbelot #include <dpaax_iova_table.h>
43c3e85bdcSAkhil Goyal 
44c3e85bdcSAkhil Goyal enum rta_sec_era rta_sec_era;
45c3e85bdcSAkhil Goyal 
46f163231eSHemant Agrawal int dpaa_logtype_sec;
47f163231eSHemant Agrawal 
48c3e85bdcSAkhil Goyal static uint8_t cryptodev_driver_id;
49c3e85bdcSAkhil Goyal 
50c3e85bdcSAkhil Goyal static __thread struct rte_crypto_op **dpaa_sec_ops;
51c3e85bdcSAkhil Goyal static __thread int dpaa_sec_op_nb;
52c3e85bdcSAkhil Goyal 
53e79416d1SHemant Agrawal static int
54e79416d1SHemant Agrawal dpaa_sec_attach_sess_q(struct dpaa_sec_qp *qp, dpaa_sec_session *sess);
55e79416d1SHemant Agrawal 
56c3e85bdcSAkhil Goyal static inline void
57c3e85bdcSAkhil Goyal dpaa_sec_op_ending(struct dpaa_sec_op_ctx *ctx)
58c3e85bdcSAkhil Goyal {
59c3e85bdcSAkhil Goyal 	if (!ctx->fd_status) {
60c3e85bdcSAkhil Goyal 		ctx->op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
61c3e85bdcSAkhil Goyal 	} else {
62f163231eSHemant Agrawal 		DPAA_SEC_DP_WARN("SEC return err: 0x%x", ctx->fd_status);
63c3e85bdcSAkhil Goyal 		ctx->op->status = RTE_CRYPTO_OP_STATUS_ERROR;
64c3e85bdcSAkhil Goyal 	}
65c3e85bdcSAkhil Goyal }
66c3e85bdcSAkhil Goyal 
67c3e85bdcSAkhil Goyal static inline struct dpaa_sec_op_ctx *
68f7a5752eSHemant Agrawal dpaa_sec_alloc_ctx(dpaa_sec_session *ses, int sg_count)
69c3e85bdcSAkhil Goyal {
70c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
71f7a5752eSHemant Agrawal 	int i, retval;
72c3e85bdcSAkhil Goyal 
732ffb940eSAkhil Goyal 	retval = rte_mempool_get(
742ffb940eSAkhil Goyal 			ses->qp[rte_lcore_id() % MAX_DPAA_CORES]->ctx_pool,
752ffb940eSAkhil Goyal 			(void **)(&ctx));
76c3e85bdcSAkhil Goyal 	if (!ctx || retval) {
77f163231eSHemant Agrawal 		DPAA_SEC_DP_WARN("Alloc sec descriptor failed!");
78c3e85bdcSAkhil Goyal 		return NULL;
79c3e85bdcSAkhil Goyal 	}
80c3e85bdcSAkhil Goyal 	/*
81c3e85bdcSAkhil Goyal 	 * Clear SG memory. There are 16 SG entries of 16 Bytes each.
82c3e85bdcSAkhil Goyal 	 * one call to dcbz_64() clear 64 bytes, hence calling it 4 times
83c3e85bdcSAkhil Goyal 	 * to clear all the SG entries. dpaa_sec_alloc_ctx() is called for
84c3e85bdcSAkhil Goyal 	 * each packet, memset is costlier than dcbz_64().
85c3e85bdcSAkhil Goyal 	 */
86f7a5752eSHemant Agrawal 	for (i = 0; i < sg_count && i < MAX_JOB_SG_ENTRIES; i += 4)
87f7a5752eSHemant Agrawal 		dcbz_64(&ctx->job.sg[i]);
88c3e85bdcSAkhil Goyal 
892ffb940eSAkhil Goyal 	ctx->ctx_pool = ses->qp[rte_lcore_id() % MAX_DPAA_CORES]->ctx_pool;
90f7a5752eSHemant Agrawal 	ctx->vtop_offset = (size_t) ctx - rte_mempool_virt2iova(ctx);
91c3e85bdcSAkhil Goyal 
92c3e85bdcSAkhil Goyal 	return ctx;
93c3e85bdcSAkhil Goyal }
94c3e85bdcSAkhil Goyal 
95c4509373SSantosh Shukla static inline rte_iova_t
96c3e85bdcSAkhil Goyal dpaa_mem_vtop(void *vaddr)
97c3e85bdcSAkhil Goyal {
9829f3c9e5SAnatoly Burakov 	const struct rte_memseg *ms;
99c3e85bdcSAkhil Goyal 
10066cc45e2SAnatoly Burakov 	ms = rte_mem_virt2memseg(vaddr, NULL);
10112e58429SThierry Herbelot 	if (ms) {
10212e58429SThierry Herbelot 		dpaax_iova_table_update(ms->iova, ms->addr, ms->len);
10329f3c9e5SAnatoly Burakov 		return ms->iova + RTE_PTR_DIFF(vaddr, ms->addr);
10412e58429SThierry Herbelot 	}
1050e5607e4SHemant Agrawal 	return (size_t)NULL;
106c3e85bdcSAkhil Goyal }
107c3e85bdcSAkhil Goyal 
108c3e85bdcSAkhil Goyal static inline void *
109c4509373SSantosh Shukla dpaa_mem_ptov(rte_iova_t paddr)
110c3e85bdcSAkhil Goyal {
1115a7dbb93SShreyansh Jain 	void *va;
1125a7dbb93SShreyansh Jain 
1135a7dbb93SShreyansh Jain 	va = (void *)dpaax_iova_table_get_va(paddr);
1145a7dbb93SShreyansh Jain 	if (likely(va))
1155a7dbb93SShreyansh Jain 		return va;
1165a7dbb93SShreyansh Jain 
11711d2f002SAnatoly Burakov 	return rte_mem_iova2virt(paddr);
118c3e85bdcSAkhil Goyal }
119c3e85bdcSAkhil Goyal 
120c3e85bdcSAkhil Goyal static void
121c3e85bdcSAkhil Goyal ern_sec_fq_handler(struct qman_portal *qm __rte_unused,
122c3e85bdcSAkhil Goyal 		   struct qman_fq *fq,
123c3e85bdcSAkhil Goyal 		   const struct qm_mr_entry *msg)
124c3e85bdcSAkhil Goyal {
125f163231eSHemant Agrawal 	DPAA_SEC_DP_ERR("sec fq %d error, RC = %x, seqnum = %x\n",
126c3e85bdcSAkhil Goyal 			fq->fqid, msg->ern.rc, msg->ern.seqnum);
127c3e85bdcSAkhil Goyal }
128c3e85bdcSAkhil Goyal 
129c3e85bdcSAkhil Goyal /* initialize the queue with dest chan as caam chan so that
130c3e85bdcSAkhil Goyal  * all the packets in this queue could be dispatched into caam
131c3e85bdcSAkhil Goyal  */
132c3e85bdcSAkhil Goyal static int
133c4509373SSantosh Shukla dpaa_sec_init_rx(struct qman_fq *fq_in, rte_iova_t hwdesc,
134c3e85bdcSAkhil Goyal 		 uint32_t fqid_out)
135c3e85bdcSAkhil Goyal {
136c3e85bdcSAkhil Goyal 	struct qm_mcc_initfq fq_opts;
137c3e85bdcSAkhil Goyal 	uint32_t flags;
138c3e85bdcSAkhil Goyal 	int ret = -1;
139c3e85bdcSAkhil Goyal 
140c3e85bdcSAkhil Goyal 	/* Clear FQ options */
141c3e85bdcSAkhil Goyal 	memset(&fq_opts, 0x00, sizeof(struct qm_mcc_initfq));
142c3e85bdcSAkhil Goyal 
143c3e85bdcSAkhil Goyal 	flags = QMAN_INITFQ_FLAG_SCHED;
144c3e85bdcSAkhil Goyal 	fq_opts.we_mask = QM_INITFQ_WE_DESTWQ | QM_INITFQ_WE_CONTEXTA |
145c3e85bdcSAkhil Goyal 			  QM_INITFQ_WE_CONTEXTB;
146c3e85bdcSAkhil Goyal 
147c3e85bdcSAkhil Goyal 	qm_fqd_context_a_set64(&fq_opts.fqd, hwdesc);
148c3e85bdcSAkhil Goyal 	fq_opts.fqd.context_b = fqid_out;
149c3e85bdcSAkhil Goyal 	fq_opts.fqd.dest.channel = qm_channel_caam;
150c3e85bdcSAkhil Goyal 	fq_opts.fqd.dest.wq = 0;
151c3e85bdcSAkhil Goyal 
152c3e85bdcSAkhil Goyal 	fq_in->cb.ern  = ern_sec_fq_handler;
153c3e85bdcSAkhil Goyal 
154f163231eSHemant Agrawal 	DPAA_SEC_DEBUG("in-%x out-%x", fq_in->fqid, fqid_out);
155e79416d1SHemant Agrawal 
156c3e85bdcSAkhil Goyal 	ret = qman_init_fq(fq_in, flags, &fq_opts);
157c3e85bdcSAkhil Goyal 	if (unlikely(ret != 0))
158f163231eSHemant Agrawal 		DPAA_SEC_ERR("qman_init_fq failed %d", ret);
159c3e85bdcSAkhil Goyal 
160c3e85bdcSAkhil Goyal 	return ret;
161c3e85bdcSAkhil Goyal }
162c3e85bdcSAkhil Goyal 
163c3e85bdcSAkhil Goyal /* something is put into in_fq and caam put the crypto result into out_fq */
164c3e85bdcSAkhil Goyal static enum qman_cb_dqrr_result
165c3e85bdcSAkhil Goyal dqrr_out_fq_cb_rx(struct qman_portal *qm __always_unused,
166c3e85bdcSAkhil Goyal 		  struct qman_fq *fq __always_unused,
167c3e85bdcSAkhil Goyal 		  const struct qm_dqrr_entry *dqrr)
168c3e85bdcSAkhil Goyal {
169c3e85bdcSAkhil Goyal 	const struct qm_fd *fd;
170c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *job;
171c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
172c3e85bdcSAkhil Goyal 
173c3e85bdcSAkhil Goyal 	if (dpaa_sec_op_nb >= DPAA_SEC_BURST)
174c3e85bdcSAkhil Goyal 		return qman_cb_dqrr_defer;
175c3e85bdcSAkhil Goyal 
176c3e85bdcSAkhil Goyal 	if (!(dqrr->stat & QM_DQRR_STAT_FD_VALID))
177c3e85bdcSAkhil Goyal 		return qman_cb_dqrr_consume;
178c3e85bdcSAkhil Goyal 
179c3e85bdcSAkhil Goyal 	fd = &dqrr->fd;
180c3e85bdcSAkhil Goyal 	/* sg is embedded in an op ctx,
181c3e85bdcSAkhil Goyal 	 * sg[0] is for output
182c3e85bdcSAkhil Goyal 	 * sg[1] for input
183c3e85bdcSAkhil Goyal 	 */
184c3e85bdcSAkhil Goyal 	job = dpaa_mem_ptov(qm_fd_addr_get64(fd));
1851f14d500SAkhil Goyal 
186c3e85bdcSAkhil Goyal 	ctx = container_of(job, struct dpaa_sec_op_ctx, job);
187c3e85bdcSAkhil Goyal 	ctx->fd_status = fd->status;
1881f14d500SAkhil Goyal 	if (ctx->op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
1891f14d500SAkhil Goyal 		struct qm_sg_entry *sg_out;
1901f14d500SAkhil Goyal 		uint32_t len;
191fb5c100aSAkhil Goyal 		struct rte_mbuf *mbuf = (ctx->op->sym->m_dst == NULL) ?
192fb5c100aSAkhil Goyal 				ctx->op->sym->m_src : ctx->op->sym->m_dst;
1931f14d500SAkhil Goyal 
1941f14d500SAkhil Goyal 		sg_out = &job->sg[0];
1951f14d500SAkhil Goyal 		hw_sg_to_cpu(sg_out);
1961f14d500SAkhil Goyal 		len = sg_out->length;
197fb5c100aSAkhil Goyal 		mbuf->pkt_len = len;
198fb5c100aSAkhil Goyal 		while (mbuf->next != NULL) {
199fb5c100aSAkhil Goyal 			len -= mbuf->data_len;
200fb5c100aSAkhil Goyal 			mbuf = mbuf->next;
201fb5c100aSAkhil Goyal 		}
202fb5c100aSAkhil Goyal 		mbuf->data_len = len;
2031f14d500SAkhil Goyal 	}
204c3e85bdcSAkhil Goyal 	dpaa_sec_ops[dpaa_sec_op_nb++] = ctx->op;
205c3e85bdcSAkhil Goyal 	dpaa_sec_op_ending(ctx);
206c3e85bdcSAkhil Goyal 
207c3e85bdcSAkhil Goyal 	return qman_cb_dqrr_consume;
208c3e85bdcSAkhil Goyal }
209c3e85bdcSAkhil Goyal 
210c3e85bdcSAkhil Goyal /* caam result is put into this queue */
211c3e85bdcSAkhil Goyal static int
212c3e85bdcSAkhil Goyal dpaa_sec_init_tx(struct qman_fq *fq)
213c3e85bdcSAkhil Goyal {
214c3e85bdcSAkhil Goyal 	int ret;
215c3e85bdcSAkhil Goyal 	struct qm_mcc_initfq opts;
216c3e85bdcSAkhil Goyal 	uint32_t flags;
217c3e85bdcSAkhil Goyal 
218c3e85bdcSAkhil Goyal 	flags = QMAN_FQ_FLAG_NO_ENQUEUE | QMAN_FQ_FLAG_LOCKED |
219c3e85bdcSAkhil Goyal 		QMAN_FQ_FLAG_DYNAMIC_FQID;
220c3e85bdcSAkhil Goyal 
221c3e85bdcSAkhil Goyal 	ret = qman_create_fq(0, flags, fq);
222c3e85bdcSAkhil Goyal 	if (unlikely(ret)) {
223f163231eSHemant Agrawal 		DPAA_SEC_ERR("qman_create_fq failed");
224c3e85bdcSAkhil Goyal 		return ret;
225c3e85bdcSAkhil Goyal 	}
226c3e85bdcSAkhil Goyal 
227c3e85bdcSAkhil Goyal 	memset(&opts, 0, sizeof(opts));
228c3e85bdcSAkhil Goyal 	opts.we_mask = QM_INITFQ_WE_DESTWQ | QM_INITFQ_WE_FQCTRL |
229c3e85bdcSAkhil Goyal 		       QM_INITFQ_WE_CONTEXTA | QM_INITFQ_WE_CONTEXTB;
230c3e85bdcSAkhil Goyal 
231c3e85bdcSAkhil Goyal 	/* opts.fqd.dest.channel = dpaa_sec_pool_chan; */
232c3e85bdcSAkhil Goyal 
233c3e85bdcSAkhil Goyal 	fq->cb.dqrr = dqrr_out_fq_cb_rx;
234c3e85bdcSAkhil Goyal 	fq->cb.ern  = ern_sec_fq_handler;
235c3e85bdcSAkhil Goyal 
236c3e85bdcSAkhil Goyal 	ret = qman_init_fq(fq, 0, &opts);
237c3e85bdcSAkhil Goyal 	if (unlikely(ret)) {
238f163231eSHemant Agrawal 		DPAA_SEC_ERR("unable to init caam source fq!");
239c3e85bdcSAkhil Goyal 		return ret;
240c3e85bdcSAkhil Goyal 	}
241c3e85bdcSAkhil Goyal 
242c3e85bdcSAkhil Goyal 	return ret;
243c3e85bdcSAkhil Goyal }
244c3e85bdcSAkhil Goyal 
245c3e85bdcSAkhil Goyal static inline int is_cipher_only(dpaa_sec_session *ses)
246c3e85bdcSAkhil Goyal {
247c3e85bdcSAkhil Goyal 	return ((ses->cipher_alg != RTE_CRYPTO_CIPHER_NULL) &&
248c3e85bdcSAkhil Goyal 		(ses->auth_alg == RTE_CRYPTO_AUTH_NULL));
249c3e85bdcSAkhil Goyal }
250c3e85bdcSAkhil Goyal 
251c3e85bdcSAkhil Goyal static inline int is_auth_only(dpaa_sec_session *ses)
252c3e85bdcSAkhil Goyal {
253c3e85bdcSAkhil Goyal 	return ((ses->cipher_alg == RTE_CRYPTO_CIPHER_NULL) &&
254c3e85bdcSAkhil Goyal 		(ses->auth_alg != RTE_CRYPTO_AUTH_NULL));
255c3e85bdcSAkhil Goyal }
256c3e85bdcSAkhil Goyal 
257c3e85bdcSAkhil Goyal static inline int is_aead(dpaa_sec_session *ses)
258c3e85bdcSAkhil Goyal {
259c3e85bdcSAkhil Goyal 	return ((ses->cipher_alg == 0) &&
260c3e85bdcSAkhil Goyal 		(ses->auth_alg == 0) &&
261c3e85bdcSAkhil Goyal 		(ses->aead_alg != 0));
262c3e85bdcSAkhil Goyal }
263c3e85bdcSAkhil Goyal 
264c3e85bdcSAkhil Goyal static inline int is_auth_cipher(dpaa_sec_session *ses)
265c3e85bdcSAkhil Goyal {
266c3e85bdcSAkhil Goyal 	return ((ses->cipher_alg != RTE_CRYPTO_CIPHER_NULL) &&
2671f14d500SAkhil Goyal 		(ses->auth_alg != RTE_CRYPTO_AUTH_NULL) &&
268fb5c100aSAkhil Goyal 		(ses->proto_alg != RTE_SECURITY_PROTOCOL_PDCP) &&
26998e84273SVakul Garg 		(ses->proto_alg != RTE_SECURITY_PROTOCOL_IPSEC) &&
27098e84273SVakul Garg 		(ses->aead_alg == 0));
2711f14d500SAkhil Goyal }
2721f14d500SAkhil Goyal 
2731f14d500SAkhil Goyal static inline int is_proto_ipsec(dpaa_sec_session *ses)
2741f14d500SAkhil Goyal {
2751f14d500SAkhil Goyal 	return (ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC);
276c3e85bdcSAkhil Goyal }
277c3e85bdcSAkhil Goyal 
278a1173d55SHemant Agrawal static inline int is_proto_pdcp(dpaa_sec_session *ses)
279a1173d55SHemant Agrawal {
280a1173d55SHemant Agrawal 	return (ses->proto_alg == RTE_SECURITY_PROTOCOL_PDCP);
281a1173d55SHemant Agrawal }
282a1173d55SHemant Agrawal 
283c3e85bdcSAkhil Goyal static inline int is_encode(dpaa_sec_session *ses)
284c3e85bdcSAkhil Goyal {
285c3e85bdcSAkhil Goyal 	return ses->dir == DIR_ENC;
286c3e85bdcSAkhil Goyal }
287c3e85bdcSAkhil Goyal 
288c3e85bdcSAkhil Goyal static inline int is_decode(dpaa_sec_session *ses)
289c3e85bdcSAkhil Goyal {
290c3e85bdcSAkhil Goyal 	return ses->dir == DIR_DEC;
291c3e85bdcSAkhil Goyal }
292c3e85bdcSAkhil Goyal 
293c3e85bdcSAkhil Goyal static inline void
294c3e85bdcSAkhil Goyal caam_auth_alg(dpaa_sec_session *ses, struct alginfo *alginfo_a)
295c3e85bdcSAkhil Goyal {
296c3e85bdcSAkhil Goyal 	switch (ses->auth_alg) {
297c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AUTH_NULL:
29805b12700SHemant Agrawal 		alginfo_a->algtype =
29905b12700SHemant Agrawal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
30005b12700SHemant Agrawal 			OP_PCL_IPSEC_HMAC_NULL : 0;
301c3e85bdcSAkhil Goyal 		ses->digest_length = 0;
302c3e85bdcSAkhil Goyal 		break;
303c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AUTH_MD5_HMAC:
3041f14d500SAkhil Goyal 		alginfo_a->algtype =
3051f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3061f14d500SAkhil Goyal 			OP_PCL_IPSEC_HMAC_MD5_96 : OP_ALG_ALGSEL_MD5;
307c3e85bdcSAkhil Goyal 		alginfo_a->algmode = OP_ALG_AAI_HMAC;
308c3e85bdcSAkhil Goyal 		break;
309c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA1_HMAC:
3101f14d500SAkhil Goyal 		alginfo_a->algtype =
3111f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3121f14d500SAkhil Goyal 			OP_PCL_IPSEC_HMAC_SHA1_96 : OP_ALG_ALGSEL_SHA1;
313c3e85bdcSAkhil Goyal 		alginfo_a->algmode = OP_ALG_AAI_HMAC;
314c3e85bdcSAkhil Goyal 		break;
315c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA224_HMAC:
3161f14d500SAkhil Goyal 		alginfo_a->algtype =
3171f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3181f14d500SAkhil Goyal 			OP_PCL_IPSEC_HMAC_SHA1_160 : OP_ALG_ALGSEL_SHA224;
319c3e85bdcSAkhil Goyal 		alginfo_a->algmode = OP_ALG_AAI_HMAC;
320c3e85bdcSAkhil Goyal 		break;
321c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA256_HMAC:
3221f14d500SAkhil Goyal 		alginfo_a->algtype =
3231f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3241f14d500SAkhil Goyal 			OP_PCL_IPSEC_HMAC_SHA2_256_128 : OP_ALG_ALGSEL_SHA256;
325c3e85bdcSAkhil Goyal 		alginfo_a->algmode = OP_ALG_AAI_HMAC;
326c3e85bdcSAkhil Goyal 		break;
327c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA384_HMAC:
3281f14d500SAkhil Goyal 		alginfo_a->algtype =
3291f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3301f14d500SAkhil Goyal 			OP_PCL_IPSEC_HMAC_SHA2_384_192 : OP_ALG_ALGSEL_SHA384;
331c3e85bdcSAkhil Goyal 		alginfo_a->algmode = OP_ALG_AAI_HMAC;
332c3e85bdcSAkhil Goyal 		break;
333c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA512_HMAC:
3341f14d500SAkhil Goyal 		alginfo_a->algtype =
3351f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3361f14d500SAkhil Goyal 			OP_PCL_IPSEC_HMAC_SHA2_512_256 : OP_ALG_ALGSEL_SHA512;
337c3e85bdcSAkhil Goyal 		alginfo_a->algmode = OP_ALG_AAI_HMAC;
338c3e85bdcSAkhil Goyal 		break;
339c3e85bdcSAkhil Goyal 	default:
340f163231eSHemant Agrawal 		DPAA_SEC_ERR("unsupported auth alg %u", ses->auth_alg);
341c3e85bdcSAkhil Goyal 	}
342c3e85bdcSAkhil Goyal }
343c3e85bdcSAkhil Goyal 
344c3e85bdcSAkhil Goyal static inline void
345c3e85bdcSAkhil Goyal caam_cipher_alg(dpaa_sec_session *ses, struct alginfo *alginfo_c)
346c3e85bdcSAkhil Goyal {
347c3e85bdcSAkhil Goyal 	switch (ses->cipher_alg) {
348c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_CIPHER_NULL:
34905b12700SHemant Agrawal 		alginfo_c->algtype =
35005b12700SHemant Agrawal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
35105b12700SHemant Agrawal 			OP_PCL_IPSEC_NULL : 0;
352c3e85bdcSAkhil Goyal 		break;
353c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_CIPHER_AES_CBC:
3541f14d500SAkhil Goyal 		alginfo_c->algtype =
3551f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3561f14d500SAkhil Goyal 			OP_PCL_IPSEC_AES_CBC : OP_ALG_ALGSEL_AES;
357c3e85bdcSAkhil Goyal 		alginfo_c->algmode = OP_ALG_AAI_CBC;
358c3e85bdcSAkhil Goyal 		break;
359c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_CIPHER_3DES_CBC:
3601f14d500SAkhil Goyal 		alginfo_c->algtype =
3611f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3621f14d500SAkhil Goyal 			OP_PCL_IPSEC_3DES : OP_ALG_ALGSEL_3DES;
363c3e85bdcSAkhil Goyal 		alginfo_c->algmode = OP_ALG_AAI_CBC;
364c3e85bdcSAkhil Goyal 		break;
365c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_CIPHER_AES_CTR:
3661f14d500SAkhil Goyal 		alginfo_c->algtype =
3671f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3681f14d500SAkhil Goyal 			OP_PCL_IPSEC_AES_CTR : OP_ALG_ALGSEL_AES;
369c3e85bdcSAkhil Goyal 		alginfo_c->algmode = OP_ALG_AAI_CTR;
370c3e85bdcSAkhil Goyal 		break;
371c3e85bdcSAkhil Goyal 	default:
372f163231eSHemant Agrawal 		DPAA_SEC_ERR("unsupported cipher alg %d", ses->cipher_alg);
373c3e85bdcSAkhil Goyal 	}
374c3e85bdcSAkhil Goyal }
375c3e85bdcSAkhil Goyal 
376c3e85bdcSAkhil Goyal static inline void
377c3e85bdcSAkhil Goyal caam_aead_alg(dpaa_sec_session *ses, struct alginfo *alginfo)
378c3e85bdcSAkhil Goyal {
379c3e85bdcSAkhil Goyal 	switch (ses->aead_alg) {
380c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AEAD_AES_GCM:
381c3e85bdcSAkhil Goyal 		alginfo->algtype = OP_ALG_ALGSEL_AES;
382c3e85bdcSAkhil Goyal 		alginfo->algmode = OP_ALG_AAI_GCM;
383c3e85bdcSAkhil Goyal 		break;
384c3e85bdcSAkhil Goyal 	default:
385f163231eSHemant Agrawal 		DPAA_SEC_ERR("unsupported AEAD alg %d", ses->aead_alg);
386c3e85bdcSAkhil Goyal 	}
387c3e85bdcSAkhil Goyal }
388c3e85bdcSAkhil Goyal 
389a1173d55SHemant Agrawal static int
390a1173d55SHemant Agrawal dpaa_sec_prep_pdcp_cdb(dpaa_sec_session *ses)
391a1173d55SHemant Agrawal {
392a1173d55SHemant Agrawal 	struct alginfo authdata = {0}, cipherdata = {0};
393a1173d55SHemant Agrawal 	struct sec_cdb *cdb = &ses->cdb;
3942e4cbdb4SVakul Garg 	struct alginfo *p_authdata = NULL;
395a1173d55SHemant Agrawal 	int32_t shared_desc_len = 0;
396a1173d55SHemant Agrawal 	int err;
397a1173d55SHemant Agrawal #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
398a1173d55SHemant Agrawal 	int swap = false;
399a1173d55SHemant Agrawal #else
400a1173d55SHemant Agrawal 	int swap = true;
401a1173d55SHemant Agrawal #endif
402a1173d55SHemant Agrawal 
403a1173d55SHemant Agrawal 	switch (ses->cipher_alg) {
404a1173d55SHemant Agrawal 	case RTE_CRYPTO_CIPHER_SNOW3G_UEA2:
405a1173d55SHemant Agrawal 		cipherdata.algtype = PDCP_CIPHER_TYPE_SNOW;
406a1173d55SHemant Agrawal 		break;
407a1173d55SHemant Agrawal 	case RTE_CRYPTO_CIPHER_ZUC_EEA3:
408a1173d55SHemant Agrawal 		cipherdata.algtype = PDCP_CIPHER_TYPE_ZUC;
409a1173d55SHemant Agrawal 		break;
410a1173d55SHemant Agrawal 	case RTE_CRYPTO_CIPHER_AES_CTR:
411a1173d55SHemant Agrawal 		cipherdata.algtype = PDCP_CIPHER_TYPE_AES;
412a1173d55SHemant Agrawal 		break;
413a1173d55SHemant Agrawal 	case RTE_CRYPTO_CIPHER_NULL:
414a1173d55SHemant Agrawal 		cipherdata.algtype = PDCP_CIPHER_TYPE_NULL;
415a1173d55SHemant Agrawal 		break;
416a1173d55SHemant Agrawal 	default:
417a1173d55SHemant Agrawal 		DPAA_SEC_ERR("Crypto: Undefined Cipher specified %u",
418a1173d55SHemant Agrawal 			      ses->cipher_alg);
419a1173d55SHemant Agrawal 		return -1;
420a1173d55SHemant Agrawal 	}
421a1173d55SHemant Agrawal 
422a1173d55SHemant Agrawal 	cipherdata.key = (size_t)ses->cipher_key.data;
423a1173d55SHemant Agrawal 	cipherdata.keylen = ses->cipher_key.length;
424a1173d55SHemant Agrawal 	cipherdata.key_enc_flags = 0;
425a1173d55SHemant Agrawal 	cipherdata.key_type = RTA_DATA_IMM;
426a1173d55SHemant Agrawal 
4272e4cbdb4SVakul Garg 	cdb->sh_desc[0] = cipherdata.keylen;
4282e4cbdb4SVakul Garg 	cdb->sh_desc[1] = 0;
4292e4cbdb4SVakul Garg 	cdb->sh_desc[2] = 0;
4302e4cbdb4SVakul Garg 
4312e4cbdb4SVakul Garg 	if (ses->auth_alg) {
432a1173d55SHemant Agrawal 		switch (ses->auth_alg) {
433a1173d55SHemant Agrawal 		case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
434a1173d55SHemant Agrawal 			authdata.algtype = PDCP_AUTH_TYPE_SNOW;
435a1173d55SHemant Agrawal 			break;
436a1173d55SHemant Agrawal 		case RTE_CRYPTO_AUTH_ZUC_EIA3:
437a1173d55SHemant Agrawal 			authdata.algtype = PDCP_AUTH_TYPE_ZUC;
438a1173d55SHemant Agrawal 			break;
439a1173d55SHemant Agrawal 		case RTE_CRYPTO_AUTH_AES_CMAC:
440a1173d55SHemant Agrawal 			authdata.algtype = PDCP_AUTH_TYPE_AES;
441a1173d55SHemant Agrawal 			break;
442a1173d55SHemant Agrawal 		case RTE_CRYPTO_AUTH_NULL:
443a1173d55SHemant Agrawal 			authdata.algtype = PDCP_AUTH_TYPE_NULL;
444a1173d55SHemant Agrawal 			break;
445a1173d55SHemant Agrawal 		default:
446a1173d55SHemant Agrawal 			DPAA_SEC_ERR("Crypto: Unsupported auth alg %u",
447a1173d55SHemant Agrawal 				      ses->auth_alg);
448a1173d55SHemant Agrawal 			return -1;
449a1173d55SHemant Agrawal 		}
450a1173d55SHemant Agrawal 
451a1173d55SHemant Agrawal 		authdata.key = (size_t)ses->auth_key.data;
452a1173d55SHemant Agrawal 		authdata.keylen = ses->auth_key.length;
453a1173d55SHemant Agrawal 		authdata.key_enc_flags = 0;
454a1173d55SHemant Agrawal 		authdata.key_type = RTA_DATA_IMM;
455a1173d55SHemant Agrawal 
4562e4cbdb4SVakul Garg 		p_authdata = &authdata;
4572e4cbdb4SVakul Garg 
458a1173d55SHemant Agrawal 		cdb->sh_desc[1] = authdata.keylen;
4592e4cbdb4SVakul Garg 	}
4602e4cbdb4SVakul Garg 
461a1173d55SHemant Agrawal 	err = rta_inline_query(IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN,
462a1173d55SHemant Agrawal 			       MIN_JOB_DESC_SIZE,
463a1173d55SHemant Agrawal 			       (unsigned int *)cdb->sh_desc,
464a1173d55SHemant Agrawal 			       &cdb->sh_desc[2], 2);
465a1173d55SHemant Agrawal 	if (err < 0) {
466a1173d55SHemant Agrawal 		DPAA_SEC_ERR("Crypto: Incorrect key lengths");
467a1173d55SHemant Agrawal 		return err;
468a1173d55SHemant Agrawal 	}
4692e4cbdb4SVakul Garg 
470a1173d55SHemant Agrawal 	if (!(cdb->sh_desc[2] & 1) && cipherdata.keylen) {
4712e4cbdb4SVakul Garg 		cipherdata.key =
4722e4cbdb4SVakul Garg 			(size_t)dpaa_mem_vtop((void *)(size_t)cipherdata.key);
473a1173d55SHemant Agrawal 		cipherdata.key_type = RTA_DATA_PTR;
474a1173d55SHemant Agrawal 	}
475a1173d55SHemant Agrawal 	if (!(cdb->sh_desc[2] & (1 << 1)) &&  authdata.keylen) {
4762e4cbdb4SVakul Garg 		authdata.key =
4772e4cbdb4SVakul Garg 			(size_t)dpaa_mem_vtop((void *)(size_t)authdata.key);
478a1173d55SHemant Agrawal 		authdata.key_type = RTA_DATA_PTR;
479a1173d55SHemant Agrawal 	}
480a1173d55SHemant Agrawal 
481a1173d55SHemant Agrawal 	cdb->sh_desc[0] = 0;
482a1173d55SHemant Agrawal 	cdb->sh_desc[1] = 0;
483a1173d55SHemant Agrawal 	cdb->sh_desc[2] = 0;
484a1173d55SHemant Agrawal 
4852e4cbdb4SVakul Garg 	if (ses->pdcp.domain == RTE_SECURITY_PDCP_MODE_CONTROL) {
486a1173d55SHemant Agrawal 		if (ses->dir == DIR_ENC)
487a1173d55SHemant Agrawal 			shared_desc_len = cnstr_shdsc_pdcp_c_plane_encap(
488a1173d55SHemant Agrawal 					cdb->sh_desc, 1, swap,
489a1173d55SHemant Agrawal 					ses->pdcp.hfn,
490eac60082SVakul Garg 					ses->pdcp.sn_size,
491a1173d55SHemant Agrawal 					ses->pdcp.bearer,
492a1173d55SHemant Agrawal 					ses->pdcp.pkt_dir,
493a1173d55SHemant Agrawal 					ses->pdcp.hfn_threshold,
494a1173d55SHemant Agrawal 					&cipherdata, &authdata,
495a1173d55SHemant Agrawal 					0);
496a1173d55SHemant Agrawal 		else if (ses->dir == DIR_DEC)
497a1173d55SHemant Agrawal 			shared_desc_len = cnstr_shdsc_pdcp_c_plane_decap(
498a1173d55SHemant Agrawal 					cdb->sh_desc, 1, swap,
499a1173d55SHemant Agrawal 					ses->pdcp.hfn,
500eac60082SVakul Garg 					ses->pdcp.sn_size,
501a1173d55SHemant Agrawal 					ses->pdcp.bearer,
502a1173d55SHemant Agrawal 					ses->pdcp.pkt_dir,
503a1173d55SHemant Agrawal 					ses->pdcp.hfn_threshold,
504a1173d55SHemant Agrawal 					&cipherdata, &authdata,
505a1173d55SHemant Agrawal 					0);
506a1173d55SHemant Agrawal 	} else {
507a1173d55SHemant Agrawal 		if (ses->dir == DIR_ENC)
508a1173d55SHemant Agrawal 			shared_desc_len = cnstr_shdsc_pdcp_u_plane_encap(
509a1173d55SHemant Agrawal 					cdb->sh_desc, 1, swap,
510a1173d55SHemant Agrawal 					ses->pdcp.sn_size,
511a1173d55SHemant Agrawal 					ses->pdcp.hfn,
512a1173d55SHemant Agrawal 					ses->pdcp.bearer,
513a1173d55SHemant Agrawal 					ses->pdcp.pkt_dir,
514a1173d55SHemant Agrawal 					ses->pdcp.hfn_threshold,
5152e4cbdb4SVakul Garg 					&cipherdata, p_authdata, 0);
516a1173d55SHemant Agrawal 		else if (ses->dir == DIR_DEC)
517a1173d55SHemant Agrawal 			shared_desc_len = cnstr_shdsc_pdcp_u_plane_decap(
518a1173d55SHemant Agrawal 					cdb->sh_desc, 1, swap,
519a1173d55SHemant Agrawal 					ses->pdcp.sn_size,
520a1173d55SHemant Agrawal 					ses->pdcp.hfn,
521a1173d55SHemant Agrawal 					ses->pdcp.bearer,
522a1173d55SHemant Agrawal 					ses->pdcp.pkt_dir,
523a1173d55SHemant Agrawal 					ses->pdcp.hfn_threshold,
5242e4cbdb4SVakul Garg 					&cipherdata, p_authdata, 0);
525a1173d55SHemant Agrawal 	}
526a1173d55SHemant Agrawal 
527a1173d55SHemant Agrawal 	return shared_desc_len;
528a1173d55SHemant Agrawal }
529a1173d55SHemant Agrawal 
53005b12700SHemant Agrawal /* prepare ipsec proto command block of the session */
53105b12700SHemant Agrawal static int
53205b12700SHemant Agrawal dpaa_sec_prep_ipsec_cdb(dpaa_sec_session *ses)
53305b12700SHemant Agrawal {
53405b12700SHemant Agrawal 	struct alginfo cipherdata = {0}, authdata = {0};
53505b12700SHemant Agrawal 	struct sec_cdb *cdb = &ses->cdb;
53605b12700SHemant Agrawal 	int32_t shared_desc_len = 0;
53705b12700SHemant Agrawal 	int err;
53805b12700SHemant Agrawal #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
53905b12700SHemant Agrawal 	int swap = false;
54005b12700SHemant Agrawal #else
54105b12700SHemant Agrawal 	int swap = true;
54205b12700SHemant Agrawal #endif
54305b12700SHemant Agrawal 
54405b12700SHemant Agrawal 	caam_cipher_alg(ses, &cipherdata);
54505b12700SHemant Agrawal 	if (cipherdata.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
54605b12700SHemant Agrawal 		DPAA_SEC_ERR("not supported cipher alg");
54705b12700SHemant Agrawal 		return -ENOTSUP;
54805b12700SHemant Agrawal 	}
54905b12700SHemant Agrawal 
55005b12700SHemant Agrawal 	cipherdata.key = (size_t)ses->cipher_key.data;
55105b12700SHemant Agrawal 	cipherdata.keylen = ses->cipher_key.length;
55205b12700SHemant Agrawal 	cipherdata.key_enc_flags = 0;
55305b12700SHemant Agrawal 	cipherdata.key_type = RTA_DATA_IMM;
55405b12700SHemant Agrawal 
55505b12700SHemant Agrawal 	caam_auth_alg(ses, &authdata);
55605b12700SHemant Agrawal 	if (authdata.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
55705b12700SHemant Agrawal 		DPAA_SEC_ERR("not supported auth alg");
55805b12700SHemant Agrawal 		return -ENOTSUP;
55905b12700SHemant Agrawal 	}
56005b12700SHemant Agrawal 
56105b12700SHemant Agrawal 	authdata.key = (size_t)ses->auth_key.data;
56205b12700SHemant Agrawal 	authdata.keylen = ses->auth_key.length;
56305b12700SHemant Agrawal 	authdata.key_enc_flags = 0;
56405b12700SHemant Agrawal 	authdata.key_type = RTA_DATA_IMM;
56505b12700SHemant Agrawal 
56605b12700SHemant Agrawal 	cdb->sh_desc[0] = cipherdata.keylen;
56705b12700SHemant Agrawal 	cdb->sh_desc[1] = authdata.keylen;
56805b12700SHemant Agrawal 	err = rta_inline_query(IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN,
56905b12700SHemant Agrawal 			       MIN_JOB_DESC_SIZE,
57005b12700SHemant Agrawal 			       (unsigned int *)cdb->sh_desc,
57105b12700SHemant Agrawal 			       &cdb->sh_desc[2], 2);
57205b12700SHemant Agrawal 
57305b12700SHemant Agrawal 	if (err < 0) {
57405b12700SHemant Agrawal 		DPAA_SEC_ERR("Crypto: Incorrect key lengths");
57505b12700SHemant Agrawal 		return err;
57605b12700SHemant Agrawal 	}
57705b12700SHemant Agrawal 	if (cdb->sh_desc[2] & 1)
57805b12700SHemant Agrawal 		cipherdata.key_type = RTA_DATA_IMM;
57905b12700SHemant Agrawal 	else {
58005b12700SHemant Agrawal 		cipherdata.key = (size_t)dpaa_mem_vtop(
58105b12700SHemant Agrawal 					(void *)(size_t)cipherdata.key);
58205b12700SHemant Agrawal 		cipherdata.key_type = RTA_DATA_PTR;
58305b12700SHemant Agrawal 	}
58405b12700SHemant Agrawal 	if (cdb->sh_desc[2] & (1<<1))
58505b12700SHemant Agrawal 		authdata.key_type = RTA_DATA_IMM;
58605b12700SHemant Agrawal 	else {
58705b12700SHemant Agrawal 		authdata.key = (size_t)dpaa_mem_vtop(
58805b12700SHemant Agrawal 					(void *)(size_t)authdata.key);
58905b12700SHemant Agrawal 		authdata.key_type = RTA_DATA_PTR;
59005b12700SHemant Agrawal 	}
59105b12700SHemant Agrawal 
59205b12700SHemant Agrawal 	cdb->sh_desc[0] = 0;
59305b12700SHemant Agrawal 	cdb->sh_desc[1] = 0;
59405b12700SHemant Agrawal 	cdb->sh_desc[2] = 0;
59505b12700SHemant Agrawal 	if (ses->dir == DIR_ENC) {
59605b12700SHemant Agrawal 		shared_desc_len = cnstr_shdsc_ipsec_new_encap(
59705b12700SHemant Agrawal 				cdb->sh_desc,
59805b12700SHemant Agrawal 				true, swap, SHR_SERIAL,
59905b12700SHemant Agrawal 				&ses->encap_pdb,
60005b12700SHemant Agrawal 				(uint8_t *)&ses->ip4_hdr,
60105b12700SHemant Agrawal 				&cipherdata, &authdata);
60205b12700SHemant Agrawal 	} else if (ses->dir == DIR_DEC) {
60305b12700SHemant Agrawal 		shared_desc_len = cnstr_shdsc_ipsec_new_decap(
60405b12700SHemant Agrawal 				cdb->sh_desc,
60505b12700SHemant Agrawal 				true, swap, SHR_SERIAL,
60605b12700SHemant Agrawal 				&ses->decap_pdb,
60705b12700SHemant Agrawal 				&cipherdata, &authdata);
60805b12700SHemant Agrawal 	}
60905b12700SHemant Agrawal 	return shared_desc_len;
61005b12700SHemant Agrawal }
611c3e85bdcSAkhil Goyal 
612c3e85bdcSAkhil Goyal /* prepare command block of the session */
613c3e85bdcSAkhil Goyal static int
614c3e85bdcSAkhil Goyal dpaa_sec_prep_cdb(dpaa_sec_session *ses)
615c3e85bdcSAkhil Goyal {
616c3e85bdcSAkhil Goyal 	struct alginfo alginfo_c = {0}, alginfo_a = {0}, alginfo = {0};
61722788c2cSSunil Kumar Kori 	int32_t shared_desc_len = 0;
618e79416d1SHemant Agrawal 	struct sec_cdb *cdb = &ses->cdb;
619c3e85bdcSAkhil Goyal 	int err;
620c3e85bdcSAkhil Goyal #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
621c3e85bdcSAkhil Goyal 	int swap = false;
622c3e85bdcSAkhil Goyal #else
623c3e85bdcSAkhil Goyal 	int swap = true;
624c3e85bdcSAkhil Goyal #endif
625c3e85bdcSAkhil Goyal 
626c3e85bdcSAkhil Goyal 	memset(cdb, 0, sizeof(struct sec_cdb));
627c3e85bdcSAkhil Goyal 
62805b12700SHemant Agrawal 	if (is_proto_ipsec(ses)) {
62905b12700SHemant Agrawal 		shared_desc_len = dpaa_sec_prep_ipsec_cdb(ses);
630a1173d55SHemant Agrawal 	} else if (is_proto_pdcp(ses)) {
631a1173d55SHemant Agrawal 		shared_desc_len = dpaa_sec_prep_pdcp_cdb(ses);
63205b12700SHemant Agrawal 	} else if (is_cipher_only(ses)) {
6330e5607e4SHemant Agrawal 		alginfo_c.key = (size_t)ses->cipher_key.data;
634c3e85bdcSAkhil Goyal 		alginfo_c.keylen = ses->cipher_key.length;
635c3e85bdcSAkhil Goyal 		alginfo_c.key_enc_flags = 0;
636c3e85bdcSAkhil Goyal 		alginfo_c.key_type = RTA_DATA_IMM;
637*c5788a10SHemant Agrawal 		switch (ses->cipher_alg) {
638*c5788a10SHemant Agrawal 		case RTE_CRYPTO_CIPHER_NULL:
639*c5788a10SHemant Agrawal 			alginfo_c.algtype = 0;
640c3e85bdcSAkhil Goyal 			shared_desc_len = cnstr_shdsc_blkcipher(
641c3e85bdcSAkhil Goyal 					cdb->sh_desc, true,
6427449390bSAkhil Goyal 					swap, SHR_NEVER, &alginfo_c,
643c3e85bdcSAkhil Goyal 					NULL,
644c3e85bdcSAkhil Goyal 					ses->iv.length,
645c3e85bdcSAkhil Goyal 					ses->dir);
646*c5788a10SHemant Agrawal 			break;
647*c5788a10SHemant Agrawal 		case RTE_CRYPTO_CIPHER_AES_CBC:
648*c5788a10SHemant Agrawal 			alginfo_c.algtype = OP_ALG_ALGSEL_AES;
649*c5788a10SHemant Agrawal 			alginfo_c.algmode = OP_ALG_AAI_CBC;
650*c5788a10SHemant Agrawal 			shared_desc_len = cnstr_shdsc_blkcipher(
651*c5788a10SHemant Agrawal 					cdb->sh_desc, true,
652*c5788a10SHemant Agrawal 					swap, SHR_NEVER, &alginfo_c,
653*c5788a10SHemant Agrawal 					NULL,
654*c5788a10SHemant Agrawal 					ses->iv.length,
655*c5788a10SHemant Agrawal 					ses->dir);
656*c5788a10SHemant Agrawal 			break;
657*c5788a10SHemant Agrawal 		case RTE_CRYPTO_CIPHER_3DES_CBC:
658*c5788a10SHemant Agrawal 			alginfo_c.algtype = OP_ALG_ALGSEL_3DES;
659*c5788a10SHemant Agrawal 			alginfo_c.algmode = OP_ALG_AAI_CBC;
660*c5788a10SHemant Agrawal 			shared_desc_len = cnstr_shdsc_blkcipher(
661*c5788a10SHemant Agrawal 					cdb->sh_desc, true,
662*c5788a10SHemant Agrawal 					swap, SHR_NEVER, &alginfo_c,
663*c5788a10SHemant Agrawal 					NULL,
664*c5788a10SHemant Agrawal 					ses->iv.length,
665*c5788a10SHemant Agrawal 					ses->dir);
666*c5788a10SHemant Agrawal 			break;
667*c5788a10SHemant Agrawal 		case RTE_CRYPTO_CIPHER_AES_CTR:
668*c5788a10SHemant Agrawal 			alginfo_c.algtype = OP_ALG_ALGSEL_AES;
669*c5788a10SHemant Agrawal 			alginfo_c.algmode = OP_ALG_AAI_CTR;
670*c5788a10SHemant Agrawal 			shared_desc_len = cnstr_shdsc_blkcipher(
671*c5788a10SHemant Agrawal 					cdb->sh_desc, true,
672*c5788a10SHemant Agrawal 					swap, SHR_NEVER, &alginfo_c,
673*c5788a10SHemant Agrawal 					NULL,
674*c5788a10SHemant Agrawal 					ses->iv.length,
675*c5788a10SHemant Agrawal 					ses->dir);
676*c5788a10SHemant Agrawal 			break;
677*c5788a10SHemant Agrawal 		case RTE_CRYPTO_CIPHER_3DES_CTR:
678*c5788a10SHemant Agrawal 			alginfo_c.algtype = OP_ALG_ALGSEL_3DES;
679*c5788a10SHemant Agrawal 			alginfo_c.algmode = OP_ALG_AAI_CTR;
680*c5788a10SHemant Agrawal 			shared_desc_len = cnstr_shdsc_blkcipher(
681*c5788a10SHemant Agrawal 					cdb->sh_desc, true,
682*c5788a10SHemant Agrawal 					swap, SHR_NEVER, &alginfo_c,
683*c5788a10SHemant Agrawal 					NULL,
684*c5788a10SHemant Agrawal 					ses->iv.length,
685*c5788a10SHemant Agrawal 					ses->dir);
686*c5788a10SHemant Agrawal 			break;
687*c5788a10SHemant Agrawal 		case RTE_CRYPTO_CIPHER_SNOW3G_UEA2:
688*c5788a10SHemant Agrawal 			alginfo_c.algtype = OP_ALG_ALGSEL_SNOW_F8;
689*c5788a10SHemant Agrawal 			shared_desc_len = cnstr_shdsc_snow_f8(
690*c5788a10SHemant Agrawal 					cdb->sh_desc, true, swap,
691*c5788a10SHemant Agrawal 					&alginfo_c,
692*c5788a10SHemant Agrawal 					ses->dir);
693*c5788a10SHemant Agrawal 			break;
694*c5788a10SHemant Agrawal 		case RTE_CRYPTO_CIPHER_ZUC_EEA3:
695*c5788a10SHemant Agrawal 			alginfo_c.algtype = OP_ALG_ALGSEL_ZUCE;
696*c5788a10SHemant Agrawal 			shared_desc_len = cnstr_shdsc_zuce(
697*c5788a10SHemant Agrawal 					cdb->sh_desc, true, swap,
698*c5788a10SHemant Agrawal 					&alginfo_c,
699*c5788a10SHemant Agrawal 					ses->dir);
700*c5788a10SHemant Agrawal 			break;
701*c5788a10SHemant Agrawal 		default:
702*c5788a10SHemant Agrawal 			DPAA_SEC_ERR("unsupported cipher alg %d",
703*c5788a10SHemant Agrawal 				     ses->cipher_alg);
704c3e85bdcSAkhil Goyal 			return -ENOTSUP;
705c3e85bdcSAkhil Goyal 		}
706*c5788a10SHemant Agrawal 	} else if (is_auth_only(ses)) {
7070e5607e4SHemant Agrawal 		alginfo_a.key = (size_t)ses->auth_key.data;
708c3e85bdcSAkhil Goyal 		alginfo_a.keylen = ses->auth_key.length;
709c3e85bdcSAkhil Goyal 		alginfo_a.key_enc_flags = 0;
710c3e85bdcSAkhil Goyal 		alginfo_a.key_type = RTA_DATA_IMM;
711*c5788a10SHemant Agrawal 		switch (ses->auth_alg) {
712*c5788a10SHemant Agrawal 		case RTE_CRYPTO_AUTH_NULL:
713*c5788a10SHemant Agrawal 			alginfo_a.algtype = 0;
714*c5788a10SHemant Agrawal 			ses->digest_length = 0;
715*c5788a10SHemant Agrawal 			shared_desc_len = cnstr_shdsc_hmac(
716*c5788a10SHemant Agrawal 						cdb->sh_desc, true,
7177449390bSAkhil Goyal 						swap, SHR_NEVER, &alginfo_a,
718c3e85bdcSAkhil Goyal 						!ses->dir,
719c3e85bdcSAkhil Goyal 						ses->digest_length);
720*c5788a10SHemant Agrawal 			break;
721*c5788a10SHemant Agrawal 		case RTE_CRYPTO_AUTH_MD5_HMAC:
722*c5788a10SHemant Agrawal 			alginfo_a.algtype = OP_ALG_ALGSEL_MD5;
723*c5788a10SHemant Agrawal 			alginfo_a.algmode = OP_ALG_AAI_HMAC;
724*c5788a10SHemant Agrawal 			shared_desc_len = cnstr_shdsc_hmac(
725*c5788a10SHemant Agrawal 						cdb->sh_desc, true,
726*c5788a10SHemant Agrawal 						swap, SHR_NEVER, &alginfo_a,
727*c5788a10SHemant Agrawal 						!ses->dir,
728*c5788a10SHemant Agrawal 						ses->digest_length);
729*c5788a10SHemant Agrawal 			break;
730*c5788a10SHemant Agrawal 		case RTE_CRYPTO_AUTH_SHA1_HMAC:
731*c5788a10SHemant Agrawal 			alginfo_a.algtype = OP_ALG_ALGSEL_SHA1;
732*c5788a10SHemant Agrawal 			alginfo_a.algmode = OP_ALG_AAI_HMAC;
733*c5788a10SHemant Agrawal 			shared_desc_len = cnstr_shdsc_hmac(
734*c5788a10SHemant Agrawal 						cdb->sh_desc, true,
735*c5788a10SHemant Agrawal 						swap, SHR_NEVER, &alginfo_a,
736*c5788a10SHemant Agrawal 						!ses->dir,
737*c5788a10SHemant Agrawal 						ses->digest_length);
738*c5788a10SHemant Agrawal 			break;
739*c5788a10SHemant Agrawal 		case RTE_CRYPTO_AUTH_SHA224_HMAC:
740*c5788a10SHemant Agrawal 			alginfo_a.algtype = OP_ALG_ALGSEL_SHA224;
741*c5788a10SHemant Agrawal 			alginfo_a.algmode = OP_ALG_AAI_HMAC;
742*c5788a10SHemant Agrawal 			shared_desc_len = cnstr_shdsc_hmac(
743*c5788a10SHemant Agrawal 						cdb->sh_desc, true,
744*c5788a10SHemant Agrawal 						swap, SHR_NEVER, &alginfo_a,
745*c5788a10SHemant Agrawal 						!ses->dir,
746*c5788a10SHemant Agrawal 						ses->digest_length);
747*c5788a10SHemant Agrawal 			break;
748*c5788a10SHemant Agrawal 		case RTE_CRYPTO_AUTH_SHA256_HMAC:
749*c5788a10SHemant Agrawal 			alginfo_a.algtype = OP_ALG_ALGSEL_SHA256;
750*c5788a10SHemant Agrawal 			alginfo_a.algmode = OP_ALG_AAI_HMAC;
751*c5788a10SHemant Agrawal 			shared_desc_len = cnstr_shdsc_hmac(
752*c5788a10SHemant Agrawal 						cdb->sh_desc, true,
753*c5788a10SHemant Agrawal 						swap, SHR_NEVER, &alginfo_a,
754*c5788a10SHemant Agrawal 						!ses->dir,
755*c5788a10SHemant Agrawal 						ses->digest_length);
756*c5788a10SHemant Agrawal 			break;
757*c5788a10SHemant Agrawal 		case RTE_CRYPTO_AUTH_SHA384_HMAC:
758*c5788a10SHemant Agrawal 			alginfo_a.algtype = OP_ALG_ALGSEL_SHA384;
759*c5788a10SHemant Agrawal 			alginfo_a.algmode = OP_ALG_AAI_HMAC;
760*c5788a10SHemant Agrawal 			shared_desc_len = cnstr_shdsc_hmac(
761*c5788a10SHemant Agrawal 						cdb->sh_desc, true,
762*c5788a10SHemant Agrawal 						swap, SHR_NEVER, &alginfo_a,
763*c5788a10SHemant Agrawal 						!ses->dir,
764*c5788a10SHemant Agrawal 						ses->digest_length);
765*c5788a10SHemant Agrawal 			break;
766*c5788a10SHemant Agrawal 		case RTE_CRYPTO_AUTH_SHA512_HMAC:
767*c5788a10SHemant Agrawal 			alginfo_a.algtype = OP_ALG_ALGSEL_SHA512;
768*c5788a10SHemant Agrawal 			alginfo_a.algmode = OP_ALG_AAI_HMAC;
769*c5788a10SHemant Agrawal 			shared_desc_len = cnstr_shdsc_hmac(
770*c5788a10SHemant Agrawal 						cdb->sh_desc, true,
771*c5788a10SHemant Agrawal 						swap, SHR_NEVER, &alginfo_a,
772*c5788a10SHemant Agrawal 						!ses->dir,
773*c5788a10SHemant Agrawal 						ses->digest_length);
774*c5788a10SHemant Agrawal 			break;
775*c5788a10SHemant Agrawal 		case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
776*c5788a10SHemant Agrawal 			alginfo_a.algtype = OP_ALG_ALGSEL_SNOW_F9;
777*c5788a10SHemant Agrawal 			alginfo_a.algmode = OP_ALG_AAI_F9;
778*c5788a10SHemant Agrawal 			ses->auth_alg = RTE_CRYPTO_AUTH_SNOW3G_UIA2;
779*c5788a10SHemant Agrawal 			shared_desc_len = cnstr_shdsc_snow_f9(
780*c5788a10SHemant Agrawal 						cdb->sh_desc, true, swap,
781*c5788a10SHemant Agrawal 						&alginfo_a,
782*c5788a10SHemant Agrawal 						!ses->dir,
783*c5788a10SHemant Agrawal 						ses->digest_length);
784*c5788a10SHemant Agrawal 			break;
785*c5788a10SHemant Agrawal 		case RTE_CRYPTO_AUTH_ZUC_EIA3:
786*c5788a10SHemant Agrawal 			alginfo_a.algtype = OP_ALG_ALGSEL_ZUCA;
787*c5788a10SHemant Agrawal 			alginfo_a.algmode = OP_ALG_AAI_F9;
788*c5788a10SHemant Agrawal 			ses->auth_alg = RTE_CRYPTO_AUTH_ZUC_EIA3;
789*c5788a10SHemant Agrawal 			shared_desc_len = cnstr_shdsc_zuca(
790*c5788a10SHemant Agrawal 						cdb->sh_desc, true, swap,
791*c5788a10SHemant Agrawal 						&alginfo_a,
792*c5788a10SHemant Agrawal 						!ses->dir,
793*c5788a10SHemant Agrawal 						ses->digest_length);
794*c5788a10SHemant Agrawal 			break;
795*c5788a10SHemant Agrawal 		default:
796*c5788a10SHemant Agrawal 			DPAA_SEC_ERR("unsupported auth alg %u", ses->auth_alg);
797*c5788a10SHemant Agrawal 		}
798c3e85bdcSAkhil Goyal 	} else if (is_aead(ses)) {
799c3e85bdcSAkhil Goyal 		caam_aead_alg(ses, &alginfo);
800c3e85bdcSAkhil Goyal 		if (alginfo.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
801f163231eSHemant Agrawal 			DPAA_SEC_ERR("not supported aead alg");
802c3e85bdcSAkhil Goyal 			return -ENOTSUP;
803c3e85bdcSAkhil Goyal 		}
8040e5607e4SHemant Agrawal 		alginfo.key = (size_t)ses->aead_key.data;
805c3e85bdcSAkhil Goyal 		alginfo.keylen = ses->aead_key.length;
806c3e85bdcSAkhil Goyal 		alginfo.key_enc_flags = 0;
807c3e85bdcSAkhil Goyal 		alginfo.key_type = RTA_DATA_IMM;
808c3e85bdcSAkhil Goyal 
809c3e85bdcSAkhil Goyal 		if (ses->dir == DIR_ENC)
810c3e85bdcSAkhil Goyal 			shared_desc_len = cnstr_shdsc_gcm_encap(
8117449390bSAkhil Goyal 					cdb->sh_desc, true, swap, SHR_NEVER,
812c3e85bdcSAkhil Goyal 					&alginfo,
813c3e85bdcSAkhil Goyal 					ses->iv.length,
814c3e85bdcSAkhil Goyal 					ses->digest_length);
815c3e85bdcSAkhil Goyal 		else
816c3e85bdcSAkhil Goyal 			shared_desc_len = cnstr_shdsc_gcm_decap(
8177449390bSAkhil Goyal 					cdb->sh_desc, true, swap, SHR_NEVER,
818c3e85bdcSAkhil Goyal 					&alginfo,
819c3e85bdcSAkhil Goyal 					ses->iv.length,
820c3e85bdcSAkhil Goyal 					ses->digest_length);
821c3e85bdcSAkhil Goyal 	} else {
822c3e85bdcSAkhil Goyal 		caam_cipher_alg(ses, &alginfo_c);
823c3e85bdcSAkhil Goyal 		if (alginfo_c.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
824f163231eSHemant Agrawal 			DPAA_SEC_ERR("not supported cipher alg");
825c3e85bdcSAkhil Goyal 			return -ENOTSUP;
826c3e85bdcSAkhil Goyal 		}
827c3e85bdcSAkhil Goyal 
8280e5607e4SHemant Agrawal 		alginfo_c.key = (size_t)ses->cipher_key.data;
829c3e85bdcSAkhil Goyal 		alginfo_c.keylen = ses->cipher_key.length;
830c3e85bdcSAkhil Goyal 		alginfo_c.key_enc_flags = 0;
831c3e85bdcSAkhil Goyal 		alginfo_c.key_type = RTA_DATA_IMM;
832c3e85bdcSAkhil Goyal 
833c3e85bdcSAkhil Goyal 		caam_auth_alg(ses, &alginfo_a);
834c3e85bdcSAkhil Goyal 		if (alginfo_a.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
835f163231eSHemant Agrawal 			DPAA_SEC_ERR("not supported auth alg");
836c3e85bdcSAkhil Goyal 			return -ENOTSUP;
837c3e85bdcSAkhil Goyal 		}
838c3e85bdcSAkhil Goyal 
8390e5607e4SHemant Agrawal 		alginfo_a.key = (size_t)ses->auth_key.data;
840c3e85bdcSAkhil Goyal 		alginfo_a.keylen = ses->auth_key.length;
841c3e85bdcSAkhil Goyal 		alginfo_a.key_enc_flags = 0;
842c3e85bdcSAkhil Goyal 		alginfo_a.key_type = RTA_DATA_IMM;
843c3e85bdcSAkhil Goyal 
844c3e85bdcSAkhil Goyal 		cdb->sh_desc[0] = alginfo_c.keylen;
845c3e85bdcSAkhil Goyal 		cdb->sh_desc[1] = alginfo_a.keylen;
846c3e85bdcSAkhil Goyal 		err = rta_inline_query(IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN,
847c3e85bdcSAkhil Goyal 				       MIN_JOB_DESC_SIZE,
848c3e85bdcSAkhil Goyal 				       (unsigned int *)cdb->sh_desc,
849c3e85bdcSAkhil Goyal 				       &cdb->sh_desc[2], 2);
850c3e85bdcSAkhil Goyal 
851c3e85bdcSAkhil Goyal 		if (err < 0) {
852f163231eSHemant Agrawal 			DPAA_SEC_ERR("Crypto: Incorrect key lengths");
853c3e85bdcSAkhil Goyal 			return err;
854c3e85bdcSAkhil Goyal 		}
855c3e85bdcSAkhil Goyal 		if (cdb->sh_desc[2] & 1)
856c3e85bdcSAkhil Goyal 			alginfo_c.key_type = RTA_DATA_IMM;
857c3e85bdcSAkhil Goyal 		else {
8580e5607e4SHemant Agrawal 			alginfo_c.key = (size_t)dpaa_mem_vtop(
8590e5607e4SHemant Agrawal 						(void *)(size_t)alginfo_c.key);
860c3e85bdcSAkhil Goyal 			alginfo_c.key_type = RTA_DATA_PTR;
861c3e85bdcSAkhil Goyal 		}
862c3e85bdcSAkhil Goyal 		if (cdb->sh_desc[2] & (1<<1))
863c3e85bdcSAkhil Goyal 			alginfo_a.key_type = RTA_DATA_IMM;
864c3e85bdcSAkhil Goyal 		else {
8650e5607e4SHemant Agrawal 			alginfo_a.key = (size_t)dpaa_mem_vtop(
8660e5607e4SHemant Agrawal 						(void *)(size_t)alginfo_a.key);
867c3e85bdcSAkhil Goyal 			alginfo_a.key_type = RTA_DATA_PTR;
868c3e85bdcSAkhil Goyal 		}
869c3e85bdcSAkhil Goyal 		cdb->sh_desc[0] = 0;
870c3e85bdcSAkhil Goyal 		cdb->sh_desc[1] = 0;
871c3e85bdcSAkhil Goyal 		cdb->sh_desc[2] = 0;
8721f14d500SAkhil Goyal 		/* Auth_only_len is set as 0 here and it will be
8731f14d500SAkhil Goyal 		 * overwritten in fd for each packet.
874c3e85bdcSAkhil Goyal 		 */
875c3e85bdcSAkhil Goyal 		shared_desc_len = cnstr_shdsc_authenc(cdb->sh_desc,
8767449390bSAkhil Goyal 				true, swap, SHR_SERIAL, &alginfo_c, &alginfo_a,
8773394ed47SVakul Garg 				ses->iv.length,
878c3e85bdcSAkhil Goyal 				ses->digest_length, ses->dir);
879c3e85bdcSAkhil Goyal 	}
88022788c2cSSunil Kumar Kori 
88122788c2cSSunil Kumar Kori 	if (shared_desc_len < 0) {
882f163231eSHemant Agrawal 		DPAA_SEC_ERR("error in preparing command block");
88322788c2cSSunil Kumar Kori 		return shared_desc_len;
88422788c2cSSunil Kumar Kori 	}
88522788c2cSSunil Kumar Kori 
886c3e85bdcSAkhil Goyal 	cdb->sh_hdr.hi.field.idlen = shared_desc_len;
887c3e85bdcSAkhil Goyal 	cdb->sh_hdr.hi.word = rte_cpu_to_be_32(cdb->sh_hdr.hi.word);
888c3e85bdcSAkhil Goyal 	cdb->sh_hdr.lo.word = rte_cpu_to_be_32(cdb->sh_hdr.lo.word);
889c3e85bdcSAkhil Goyal 
890c3e85bdcSAkhil Goyal 	return 0;
891c3e85bdcSAkhil Goyal }
892c3e85bdcSAkhil Goyal 
893c3e85bdcSAkhil Goyal /* qp is lockless, should be accessed by only one thread */
894c3e85bdcSAkhil Goyal static int
895c3e85bdcSAkhil Goyal dpaa_sec_deq(struct dpaa_sec_qp *qp, struct rte_crypto_op **ops, int nb_ops)
896c3e85bdcSAkhil Goyal {
897c3e85bdcSAkhil Goyal 	struct qman_fq *fq;
8989a984458SAkhil Goyal 	unsigned int pkts = 0;
899f40d5a53SNipun Gupta 	int num_rx_bufs, ret;
9009a984458SAkhil Goyal 	struct qm_dqrr_entry *dq;
901f40d5a53SNipun Gupta 	uint32_t vdqcr_flags = 0;
902c3e85bdcSAkhil Goyal 
903c3e85bdcSAkhil Goyal 	fq = &qp->outq;
904f40d5a53SNipun Gupta 	/*
905f40d5a53SNipun Gupta 	 * Until request for four buffers, we provide exact number of buffers.
906f40d5a53SNipun Gupta 	 * Otherwise we do not set the QM_VDQCR_EXACT flag.
907f40d5a53SNipun Gupta 	 * Not setting QM_VDQCR_EXACT flag can provide two more buffers than
908f40d5a53SNipun Gupta 	 * requested, so we request two less in this case.
909f40d5a53SNipun Gupta 	 */
910f40d5a53SNipun Gupta 	if (nb_ops < 4) {
911f40d5a53SNipun Gupta 		vdqcr_flags = QM_VDQCR_EXACT;
912f40d5a53SNipun Gupta 		num_rx_bufs = nb_ops;
913f40d5a53SNipun Gupta 	} else {
914f40d5a53SNipun Gupta 		num_rx_bufs = nb_ops > DPAA_MAX_DEQUEUE_NUM_FRAMES ?
915f40d5a53SNipun Gupta 			(DPAA_MAX_DEQUEUE_NUM_FRAMES - 2) : (nb_ops - 2);
916f40d5a53SNipun Gupta 	}
917f40d5a53SNipun Gupta 	ret = qman_set_vdq(fq, num_rx_bufs, vdqcr_flags);
9189a984458SAkhil Goyal 	if (ret)
9199a984458SAkhil Goyal 		return 0;
920c3e85bdcSAkhil Goyal 
9219a984458SAkhil Goyal 	do {
9229a984458SAkhil Goyal 		const struct qm_fd *fd;
9239a984458SAkhil Goyal 		struct dpaa_sec_job *job;
9249a984458SAkhil Goyal 		struct dpaa_sec_op_ctx *ctx;
9259a984458SAkhil Goyal 		struct rte_crypto_op *op;
926c3e85bdcSAkhil Goyal 
9279a984458SAkhil Goyal 		dq = qman_dequeue(fq);
9289a984458SAkhil Goyal 		if (!dq)
9299a984458SAkhil Goyal 			continue;
9309a984458SAkhil Goyal 
9319a984458SAkhil Goyal 		fd = &dq->fd;
9329a984458SAkhil Goyal 		/* sg is embedded in an op ctx,
9339a984458SAkhil Goyal 		 * sg[0] is for output
9349a984458SAkhil Goyal 		 * sg[1] for input
9359a984458SAkhil Goyal 		 */
9369a984458SAkhil Goyal 		job = dpaa_mem_ptov(qm_fd_addr_get64(fd));
9379a984458SAkhil Goyal 
9389a984458SAkhil Goyal 		ctx = container_of(job, struct dpaa_sec_op_ctx, job);
9399a984458SAkhil Goyal 		ctx->fd_status = fd->status;
9409a984458SAkhil Goyal 		op = ctx->op;
9419a984458SAkhil Goyal 		if (op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
9429a984458SAkhil Goyal 			struct qm_sg_entry *sg_out;
9439a984458SAkhil Goyal 			uint32_t len;
944fb5c100aSAkhil Goyal 			struct rte_mbuf *mbuf = (op->sym->m_dst == NULL) ?
945fb5c100aSAkhil Goyal 						op->sym->m_src : op->sym->m_dst;
9469a984458SAkhil Goyal 
9479a984458SAkhil Goyal 			sg_out = &job->sg[0];
9489a984458SAkhil Goyal 			hw_sg_to_cpu(sg_out);
9499a984458SAkhil Goyal 			len = sg_out->length;
950fb5c100aSAkhil Goyal 			mbuf->pkt_len = len;
951fb5c100aSAkhil Goyal 			while (mbuf->next != NULL) {
952fb5c100aSAkhil Goyal 				len -= mbuf->data_len;
953fb5c100aSAkhil Goyal 				mbuf = mbuf->next;
954fb5c100aSAkhil Goyal 			}
955fb5c100aSAkhil Goyal 			mbuf->data_len = len;
9569a984458SAkhil Goyal 		}
9579a984458SAkhil Goyal 		if (!ctx->fd_status) {
9589a984458SAkhil Goyal 			op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
9599a984458SAkhil Goyal 		} else {
960f163231eSHemant Agrawal 			DPAA_SEC_DP_WARN("SEC return err:0x%x", ctx->fd_status);
9619a984458SAkhil Goyal 			op->status = RTE_CRYPTO_OP_STATUS_ERROR;
9629a984458SAkhil Goyal 		}
9639a984458SAkhil Goyal 		ops[pkts++] = op;
9649a984458SAkhil Goyal 
9659a984458SAkhil Goyal 		/* report op status to sym->op and then free the ctx memeory */
9669a984458SAkhil Goyal 		rte_mempool_put(ctx->ctx_pool, (void *)ctx);
9679a984458SAkhil Goyal 
9689a984458SAkhil Goyal 		qman_dqrr_consume(fq, dq);
9699a984458SAkhil Goyal 	} while (fq->flags & QMAN_FQ_STATE_VDQCR);
9709a984458SAkhil Goyal 
9719a984458SAkhil Goyal 	return pkts;
972c3e85bdcSAkhil Goyal }
973c3e85bdcSAkhil Goyal 
974a74af788SAkhil Goyal static inline struct dpaa_sec_job *
975a74af788SAkhil Goyal build_auth_only_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
976a74af788SAkhil Goyal {
977a74af788SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
978a74af788SAkhil Goyal 	struct rte_mbuf *mbuf = sym->m_src;
979a74af788SAkhil Goyal 	struct dpaa_sec_job *cf;
980a74af788SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
981a74af788SAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
982a74af788SAkhil Goyal 	phys_addr_t start_addr;
983a74af788SAkhil Goyal 	uint8_t *old_digest, extra_segs;
984*c5788a10SHemant Agrawal 	int data_len, data_offset;
985*c5788a10SHemant Agrawal 
986*c5788a10SHemant Agrawal 	data_len = sym->auth.data.length;
987*c5788a10SHemant Agrawal 	data_offset = sym->auth.data.offset;
988*c5788a10SHemant Agrawal 
989*c5788a10SHemant Agrawal 	if (ses->auth_alg == RTE_CRYPTO_AUTH_SNOW3G_UIA2 ||
990*c5788a10SHemant Agrawal 	    ses->auth_alg == RTE_CRYPTO_AUTH_ZUC_EIA3) {
991*c5788a10SHemant Agrawal 		if ((data_len & 7) || (data_offset & 7)) {
992*c5788a10SHemant Agrawal 			DPAA_SEC_ERR("AUTH: len/offset must be full bytes");
993*c5788a10SHemant Agrawal 			return NULL;
994*c5788a10SHemant Agrawal 		}
995*c5788a10SHemant Agrawal 
996*c5788a10SHemant Agrawal 		data_len = data_len >> 3;
997*c5788a10SHemant Agrawal 		data_offset = data_offset >> 3;
998*c5788a10SHemant Agrawal 	}
999a74af788SAkhil Goyal 
1000a74af788SAkhil Goyal 	if (is_decode(ses))
1001a74af788SAkhil Goyal 		extra_segs = 3;
1002a74af788SAkhil Goyal 	else
1003a74af788SAkhil Goyal 		extra_segs = 2;
1004a74af788SAkhil Goyal 
1005f7a5752eSHemant Agrawal 	if (mbuf->nb_segs > MAX_SG_ENTRIES) {
1006f163231eSHemant Agrawal 		DPAA_SEC_DP_ERR("Auth: Max sec segs supported is %d",
1007a74af788SAkhil Goyal 				MAX_SG_ENTRIES);
1008a74af788SAkhil Goyal 		return NULL;
1009a74af788SAkhil Goyal 	}
1010f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, mbuf->nb_segs + extra_segs);
1011a74af788SAkhil Goyal 	if (!ctx)
1012a74af788SAkhil Goyal 		return NULL;
1013a74af788SAkhil Goyal 
1014a74af788SAkhil Goyal 	cf = &ctx->job;
1015a74af788SAkhil Goyal 	ctx->op = op;
1016a74af788SAkhil Goyal 	old_digest = ctx->digest;
1017a74af788SAkhil Goyal 
1018a74af788SAkhil Goyal 	/* output */
1019a74af788SAkhil Goyal 	out_sg = &cf->sg[0];
1020a74af788SAkhil Goyal 	qm_sg_entry_set64(out_sg, sym->auth.digest.phys_addr);
1021a74af788SAkhil Goyal 	out_sg->length = ses->digest_length;
1022a74af788SAkhil Goyal 	cpu_to_hw_sg(out_sg);
1023a74af788SAkhil Goyal 
1024a74af788SAkhil Goyal 	/* input */
1025a74af788SAkhil Goyal 	in_sg = &cf->sg[1];
1026a74af788SAkhil Goyal 	/* need to extend the input to a compound frame */
1027a74af788SAkhil Goyal 	in_sg->extension = 1;
1028a74af788SAkhil Goyal 	in_sg->final = 1;
1029*c5788a10SHemant Agrawal 	in_sg->length = data_len;
103095456e89SShreyansh Jain 	qm_sg_entry_set64(in_sg, dpaa_mem_vtop(&cf->sg[2]));
1031a74af788SAkhil Goyal 
1032a74af788SAkhil Goyal 	/* 1st seg */
1033a74af788SAkhil Goyal 	sg = in_sg + 1;
1034a74af788SAkhil Goyal 
1035*c5788a10SHemant Agrawal 	if (ses->iv.length) {
1036*c5788a10SHemant Agrawal 		uint8_t *iv_ptr;
1037*c5788a10SHemant Agrawal 
1038*c5788a10SHemant Agrawal 		iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1039*c5788a10SHemant Agrawal 						   ses->iv.offset);
1040*c5788a10SHemant Agrawal 
1041*c5788a10SHemant Agrawal 		if (ses->auth_alg == RTE_CRYPTO_AUTH_SNOW3G_UIA2) {
1042*c5788a10SHemant Agrawal 			iv_ptr = conv_to_snow_f9_iv(iv_ptr);
1043*c5788a10SHemant Agrawal 			sg->length = 12;
1044*c5788a10SHemant Agrawal 		} else if (ses->auth_alg == RTE_CRYPTO_AUTH_ZUC_EIA3) {
1045*c5788a10SHemant Agrawal 			iv_ptr = conv_to_zuc_eia_iv(iv_ptr);
1046*c5788a10SHemant Agrawal 			sg->length = 8;
1047*c5788a10SHemant Agrawal 		} else {
1048*c5788a10SHemant Agrawal 			sg->length = ses->iv.length;
1049*c5788a10SHemant Agrawal 		}
1050*c5788a10SHemant Agrawal 		qm_sg_entry_set64(sg, dpaa_mem_vtop(iv_ptr));
1051*c5788a10SHemant Agrawal 		in_sg->length += sg->length;
1052*c5788a10SHemant Agrawal 		cpu_to_hw_sg(sg);
1053*c5788a10SHemant Agrawal 		sg++;
1054*c5788a10SHemant Agrawal 	}
1055*c5788a10SHemant Agrawal 
1056*c5788a10SHemant Agrawal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1057*c5788a10SHemant Agrawal 	sg->offset = data_offset;
1058*c5788a10SHemant Agrawal 
1059*c5788a10SHemant Agrawal 	if (data_len <= (mbuf->data_len - data_offset)) {
1060*c5788a10SHemant Agrawal 		sg->length = data_len;
1061*c5788a10SHemant Agrawal 	} else {
1062*c5788a10SHemant Agrawal 		sg->length = mbuf->data_len - data_offset;
1063*c5788a10SHemant Agrawal 
1064*c5788a10SHemant Agrawal 		/* remaining i/p segs */
1065*c5788a10SHemant Agrawal 		while ((data_len = data_len - sg->length) &&
1066*c5788a10SHemant Agrawal 		       (mbuf = mbuf->next)) {
1067a74af788SAkhil Goyal 			cpu_to_hw_sg(sg);
1068a74af788SAkhil Goyal 			sg++;
1069a74af788SAkhil Goyal 			qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1070*c5788a10SHemant Agrawal 			if (data_len > mbuf->data_len)
1071a74af788SAkhil Goyal 				sg->length = mbuf->data_len;
1072*c5788a10SHemant Agrawal 			else
1073*c5788a10SHemant Agrawal 				sg->length = data_len;
1074*c5788a10SHemant Agrawal 		}
1075a74af788SAkhil Goyal 	}
1076a74af788SAkhil Goyal 
1077a74af788SAkhil Goyal 	if (is_decode(ses)) {
1078a74af788SAkhil Goyal 		/* Digest verification case */
1079a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1080a74af788SAkhil Goyal 		sg++;
1081a74af788SAkhil Goyal 		rte_memcpy(old_digest, sym->auth.digest.data,
1082a74af788SAkhil Goyal 				ses->digest_length);
108395456e89SShreyansh Jain 		start_addr = dpaa_mem_vtop(old_digest);
1084a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, start_addr);
1085a74af788SAkhil Goyal 		sg->length = ses->digest_length;
1086a74af788SAkhil Goyal 		in_sg->length += ses->digest_length;
1087a74af788SAkhil Goyal 	}
1088a74af788SAkhil Goyal 	sg->final = 1;
1089a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1090a74af788SAkhil Goyal 	cpu_to_hw_sg(in_sg);
1091a74af788SAkhil Goyal 
1092a74af788SAkhil Goyal 	return cf;
1093a74af788SAkhil Goyal }
1094a74af788SAkhil Goyal 
1095c3e85bdcSAkhil Goyal /**
1096c3e85bdcSAkhil Goyal  * packet looks like:
1097c3e85bdcSAkhil Goyal  *		|<----data_len------->|
1098c3e85bdcSAkhil Goyal  *    |ip_header|ah_header|icv|payload|
1099c3e85bdcSAkhil Goyal  *              ^
1100c3e85bdcSAkhil Goyal  *		|
1101c3e85bdcSAkhil Goyal  *	   mbuf->pkt.data
1102c3e85bdcSAkhil Goyal  */
1103c3e85bdcSAkhil Goyal static inline struct dpaa_sec_job *
1104c3e85bdcSAkhil Goyal build_auth_only(struct rte_crypto_op *op, dpaa_sec_session *ses)
1105c3e85bdcSAkhil Goyal {
1106c3e85bdcSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1107c3e85bdcSAkhil Goyal 	struct rte_mbuf *mbuf = sym->m_src;
1108c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
1109c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1110*c5788a10SHemant Agrawal 	struct qm_sg_entry *sg, *in_sg;
1111c4509373SSantosh Shukla 	rte_iova_t start_addr;
1112c3e85bdcSAkhil Goyal 	uint8_t *old_digest;
1113*c5788a10SHemant Agrawal 	int data_len, data_offset;
1114*c5788a10SHemant Agrawal 
1115*c5788a10SHemant Agrawal 	data_len = sym->auth.data.length;
1116*c5788a10SHemant Agrawal 	data_offset = sym->auth.data.offset;
1117*c5788a10SHemant Agrawal 
1118*c5788a10SHemant Agrawal 	if (ses->auth_alg == RTE_CRYPTO_AUTH_SNOW3G_UIA2 ||
1119*c5788a10SHemant Agrawal 	    ses->auth_alg == RTE_CRYPTO_AUTH_ZUC_EIA3) {
1120*c5788a10SHemant Agrawal 		if ((data_len & 7) || (data_offset & 7)) {
1121*c5788a10SHemant Agrawal 			DPAA_SEC_ERR("AUTH: len/offset must be full bytes");
1122*c5788a10SHemant Agrawal 			return NULL;
1123*c5788a10SHemant Agrawal 		}
1124*c5788a10SHemant Agrawal 
1125*c5788a10SHemant Agrawal 		data_len = data_len >> 3;
1126*c5788a10SHemant Agrawal 		data_offset = data_offset >> 3;
1127*c5788a10SHemant Agrawal 	}
1128c3e85bdcSAkhil Goyal 
1129f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, 4);
1130c3e85bdcSAkhil Goyal 	if (!ctx)
1131c3e85bdcSAkhil Goyal 		return NULL;
1132c3e85bdcSAkhil Goyal 
1133c3e85bdcSAkhil Goyal 	cf = &ctx->job;
1134c3e85bdcSAkhil Goyal 	ctx->op = op;
1135c3e85bdcSAkhil Goyal 	old_digest = ctx->digest;
1136c3e85bdcSAkhil Goyal 
1137bfa9a8a4SThomas Monjalon 	start_addr = rte_pktmbuf_iova(mbuf);
1138c3e85bdcSAkhil Goyal 	/* output */
1139c3e85bdcSAkhil Goyal 	sg = &cf->sg[0];
1140c3e85bdcSAkhil Goyal 	qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
1141c3e85bdcSAkhil Goyal 	sg->length = ses->digest_length;
1142c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1143c3e85bdcSAkhil Goyal 
1144c3e85bdcSAkhil Goyal 	/* input */
1145*c5788a10SHemant Agrawal 	in_sg = &cf->sg[1];
1146c3e85bdcSAkhil Goyal 	/* need to extend the input to a compound frame */
1147*c5788a10SHemant Agrawal 	in_sg->extension = 1;
1148*c5788a10SHemant Agrawal 	in_sg->final = 1;
1149*c5788a10SHemant Agrawal 	in_sg->length = data_len;
1150*c5788a10SHemant Agrawal 	qm_sg_entry_set64(in_sg, dpaa_mem_vtop(&cf->sg[2]));
1151c3e85bdcSAkhil Goyal 	sg = &cf->sg[2];
1152*c5788a10SHemant Agrawal 
1153*c5788a10SHemant Agrawal 	if (ses->iv.length) {
1154*c5788a10SHemant Agrawal 		uint8_t *iv_ptr;
1155*c5788a10SHemant Agrawal 
1156*c5788a10SHemant Agrawal 		iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1157*c5788a10SHemant Agrawal 						   ses->iv.offset);
1158*c5788a10SHemant Agrawal 
1159*c5788a10SHemant Agrawal 		if (ses->auth_alg == RTE_CRYPTO_AUTH_SNOW3G_UIA2) {
1160*c5788a10SHemant Agrawal 			iv_ptr = conv_to_snow_f9_iv(iv_ptr);
1161*c5788a10SHemant Agrawal 			sg->length = 12;
1162*c5788a10SHemant Agrawal 		} else if (ses->auth_alg == RTE_CRYPTO_AUTH_ZUC_EIA3) {
1163*c5788a10SHemant Agrawal 			iv_ptr = conv_to_zuc_eia_iv(iv_ptr);
1164*c5788a10SHemant Agrawal 			sg->length = 8;
1165*c5788a10SHemant Agrawal 		} else {
1166*c5788a10SHemant Agrawal 			sg->length = ses->iv.length;
1167*c5788a10SHemant Agrawal 		}
1168*c5788a10SHemant Agrawal 		qm_sg_entry_set64(sg, dpaa_mem_vtop(iv_ptr));
1169*c5788a10SHemant Agrawal 		in_sg->length += sg->length;
1170*c5788a10SHemant Agrawal 		cpu_to_hw_sg(sg);
1171*c5788a10SHemant Agrawal 		sg++;
1172*c5788a10SHemant Agrawal 	}
1173*c5788a10SHemant Agrawal 
1174*c5788a10SHemant Agrawal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1175*c5788a10SHemant Agrawal 	sg->offset = data_offset;
1176*c5788a10SHemant Agrawal 	sg->length = data_len;
1177*c5788a10SHemant Agrawal 
1178*c5788a10SHemant Agrawal 	if (is_decode(ses)) {
1179*c5788a10SHemant Agrawal 		/* Digest verification case */
1180*c5788a10SHemant Agrawal 		cpu_to_hw_sg(sg);
1181c3e85bdcSAkhil Goyal 		/* hash result or digest, save digest first */
1182c3e85bdcSAkhil Goyal 		rte_memcpy(old_digest, sym->auth.digest.data,
1183c3e85bdcSAkhil Goyal 				ses->digest_length);
1184c3e85bdcSAkhil Goyal 		/* let's check digest by hw */
118595456e89SShreyansh Jain 		start_addr = dpaa_mem_vtop(old_digest);
1186c3e85bdcSAkhil Goyal 		sg++;
1187c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, start_addr);
1188c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
1189*c5788a10SHemant Agrawal 		in_sg->length += ses->digest_length;
1190c3e85bdcSAkhil Goyal 	}
1191*c5788a10SHemant Agrawal 	sg->final = 1;
1192*c5788a10SHemant Agrawal 	cpu_to_hw_sg(sg);
1193*c5788a10SHemant Agrawal 	cpu_to_hw_sg(in_sg);
1194c3e85bdcSAkhil Goyal 
1195c3e85bdcSAkhil Goyal 	return cf;
1196c3e85bdcSAkhil Goyal }
1197c3e85bdcSAkhil Goyal 
1198c3e85bdcSAkhil Goyal static inline struct dpaa_sec_job *
1199a74af788SAkhil Goyal build_cipher_only_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
1200a74af788SAkhil Goyal {
1201a74af788SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1202a74af788SAkhil Goyal 	struct dpaa_sec_job *cf;
1203a74af788SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1204a74af788SAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
1205a74af788SAkhil Goyal 	struct rte_mbuf *mbuf;
1206a74af788SAkhil Goyal 	uint8_t req_segs;
1207a74af788SAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1208a74af788SAkhil Goyal 			ses->iv.offset);
1209*c5788a10SHemant Agrawal 	int data_len, data_offset;
1210*c5788a10SHemant Agrawal 
1211*c5788a10SHemant Agrawal 	data_len = sym->cipher.data.length;
1212*c5788a10SHemant Agrawal 	data_offset = sym->cipher.data.offset;
1213*c5788a10SHemant Agrawal 
1214*c5788a10SHemant Agrawal 	if (ses->cipher_alg == RTE_CRYPTO_CIPHER_SNOW3G_UEA2 ||
1215*c5788a10SHemant Agrawal 		ses->cipher_alg == RTE_CRYPTO_CIPHER_ZUC_EEA3) {
1216*c5788a10SHemant Agrawal 		if ((data_len & 7) || (data_offset & 7)) {
1217*c5788a10SHemant Agrawal 			DPAA_SEC_ERR("CIPHER: len/offset must be full bytes");
1218*c5788a10SHemant Agrawal 			return NULL;
1219*c5788a10SHemant Agrawal 		}
1220*c5788a10SHemant Agrawal 
1221*c5788a10SHemant Agrawal 		data_len = data_len >> 3;
1222*c5788a10SHemant Agrawal 		data_offset = data_offset >> 3;
1223*c5788a10SHemant Agrawal 	}
1224a74af788SAkhil Goyal 
1225a74af788SAkhil Goyal 	if (sym->m_dst) {
1226a74af788SAkhil Goyal 		mbuf = sym->m_dst;
1227a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 3;
1228a74af788SAkhil Goyal 	} else {
1229a74af788SAkhil Goyal 		mbuf = sym->m_src;
1230a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs * 2 + 3;
1231a74af788SAkhil Goyal 	}
1232f7a5752eSHemant Agrawal 	if (mbuf->nb_segs > MAX_SG_ENTRIES) {
1233f163231eSHemant Agrawal 		DPAA_SEC_DP_ERR("Cipher: Max sec segs supported is %d",
1234a74af788SAkhil Goyal 				MAX_SG_ENTRIES);
1235a74af788SAkhil Goyal 		return NULL;
1236a74af788SAkhil Goyal 	}
1237a74af788SAkhil Goyal 
1238f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, req_segs);
1239a74af788SAkhil Goyal 	if (!ctx)
1240a74af788SAkhil Goyal 		return NULL;
1241a74af788SAkhil Goyal 
1242a74af788SAkhil Goyal 	cf = &ctx->job;
1243a74af788SAkhil Goyal 	ctx->op = op;
1244a74af788SAkhil Goyal 
1245a74af788SAkhil Goyal 	/* output */
1246a74af788SAkhil Goyal 	out_sg = &cf->sg[0];
1247a74af788SAkhil Goyal 	out_sg->extension = 1;
1248*c5788a10SHemant Agrawal 	out_sg->length = data_len;
124995456e89SShreyansh Jain 	qm_sg_entry_set64(out_sg, dpaa_mem_vtop(&cf->sg[2]));
1250a74af788SAkhil Goyal 	cpu_to_hw_sg(out_sg);
1251a74af788SAkhil Goyal 
1252a74af788SAkhil Goyal 	/* 1st seg */
1253a74af788SAkhil Goyal 	sg = &cf->sg[2];
1254a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1255*c5788a10SHemant Agrawal 	sg->length = mbuf->data_len - data_offset;
1256*c5788a10SHemant Agrawal 	sg->offset = data_offset;
1257a74af788SAkhil Goyal 
1258a74af788SAkhil Goyal 	/* Successive segs */
1259a74af788SAkhil Goyal 	mbuf = mbuf->next;
1260a74af788SAkhil Goyal 	while (mbuf) {
1261a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1262a74af788SAkhil Goyal 		sg++;
1263a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1264a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1265a74af788SAkhil Goyal 		mbuf = mbuf->next;
1266a74af788SAkhil Goyal 	}
1267a74af788SAkhil Goyal 	sg->final = 1;
1268a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1269a74af788SAkhil Goyal 
1270a74af788SAkhil Goyal 	/* input */
1271a74af788SAkhil Goyal 	mbuf = sym->m_src;
1272a74af788SAkhil Goyal 	in_sg = &cf->sg[1];
1273a74af788SAkhil Goyal 	in_sg->extension = 1;
1274a74af788SAkhil Goyal 	in_sg->final = 1;
1275*c5788a10SHemant Agrawal 	in_sg->length = data_len + ses->iv.length;
1276a74af788SAkhil Goyal 
1277a74af788SAkhil Goyal 	sg++;
127895456e89SShreyansh Jain 	qm_sg_entry_set64(in_sg, dpaa_mem_vtop(sg));
1279a74af788SAkhil Goyal 	cpu_to_hw_sg(in_sg);
1280a74af788SAkhil Goyal 
1281a74af788SAkhil Goyal 	/* IV */
1282a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1283a74af788SAkhil Goyal 	sg->length = ses->iv.length;
1284a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1285a74af788SAkhil Goyal 
1286a74af788SAkhil Goyal 	/* 1st seg */
1287a74af788SAkhil Goyal 	sg++;
1288a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1289*c5788a10SHemant Agrawal 	sg->length = mbuf->data_len - data_offset;
1290*c5788a10SHemant Agrawal 	sg->offset = data_offset;
1291a74af788SAkhil Goyal 
1292a74af788SAkhil Goyal 	/* Successive segs */
1293a74af788SAkhil Goyal 	mbuf = mbuf->next;
1294a74af788SAkhil Goyal 	while (mbuf) {
1295a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1296a74af788SAkhil Goyal 		sg++;
1297a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1298a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1299a74af788SAkhil Goyal 		mbuf = mbuf->next;
1300a74af788SAkhil Goyal 	}
1301a74af788SAkhil Goyal 	sg->final = 1;
1302a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1303a74af788SAkhil Goyal 
1304a74af788SAkhil Goyal 	return cf;
1305a74af788SAkhil Goyal }
1306a74af788SAkhil Goyal 
1307a74af788SAkhil Goyal static inline struct dpaa_sec_job *
1308c3e85bdcSAkhil Goyal build_cipher_only(struct rte_crypto_op *op, dpaa_sec_session *ses)
1309c3e85bdcSAkhil Goyal {
1310c3e85bdcSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1311c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
1312c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1313c3e85bdcSAkhil Goyal 	struct qm_sg_entry *sg;
1314c4509373SSantosh Shukla 	rte_iova_t src_start_addr, dst_start_addr;
1315c3e85bdcSAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1316c3e85bdcSAkhil Goyal 			ses->iv.offset);
1317*c5788a10SHemant Agrawal 	int data_len, data_offset;
1318*c5788a10SHemant Agrawal 
1319*c5788a10SHemant Agrawal 	data_len = sym->cipher.data.length;
1320*c5788a10SHemant Agrawal 	data_offset = sym->cipher.data.offset;
1321*c5788a10SHemant Agrawal 
1322*c5788a10SHemant Agrawal 	if (ses->cipher_alg == RTE_CRYPTO_CIPHER_SNOW3G_UEA2 ||
1323*c5788a10SHemant Agrawal 		ses->cipher_alg == RTE_CRYPTO_CIPHER_ZUC_EEA3) {
1324*c5788a10SHemant Agrawal 		if ((data_len & 7) || (data_offset & 7)) {
1325*c5788a10SHemant Agrawal 			DPAA_SEC_ERR("CIPHER: len/offset must be full bytes");
1326*c5788a10SHemant Agrawal 			return NULL;
1327*c5788a10SHemant Agrawal 		}
1328*c5788a10SHemant Agrawal 
1329*c5788a10SHemant Agrawal 		data_len = data_len >> 3;
1330*c5788a10SHemant Agrawal 		data_offset = data_offset >> 3;
1331*c5788a10SHemant Agrawal 	}
1332c3e85bdcSAkhil Goyal 
1333f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, 4);
1334c3e85bdcSAkhil Goyal 	if (!ctx)
1335c3e85bdcSAkhil Goyal 		return NULL;
1336c3e85bdcSAkhil Goyal 
1337c3e85bdcSAkhil Goyal 	cf = &ctx->job;
1338c3e85bdcSAkhil Goyal 	ctx->op = op;
1339a389434eSAlok Makhariya 
1340bfa9a8a4SThomas Monjalon 	src_start_addr = rte_pktmbuf_iova(sym->m_src);
1341a389434eSAlok Makhariya 
1342a389434eSAlok Makhariya 	if (sym->m_dst)
1343bfa9a8a4SThomas Monjalon 		dst_start_addr = rte_pktmbuf_iova(sym->m_dst);
1344a389434eSAlok Makhariya 	else
1345a389434eSAlok Makhariya 		dst_start_addr = src_start_addr;
1346c3e85bdcSAkhil Goyal 
1347c3e85bdcSAkhil Goyal 	/* output */
1348c3e85bdcSAkhil Goyal 	sg = &cf->sg[0];
1349*c5788a10SHemant Agrawal 	qm_sg_entry_set64(sg, dst_start_addr + data_offset);
1350*c5788a10SHemant Agrawal 	sg->length = data_len + ses->iv.length;
1351c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1352c3e85bdcSAkhil Goyal 
1353c3e85bdcSAkhil Goyal 	/* input */
1354c3e85bdcSAkhil Goyal 	sg = &cf->sg[1];
1355c3e85bdcSAkhil Goyal 
1356c3e85bdcSAkhil Goyal 	/* need to extend the input to a compound frame */
1357c3e85bdcSAkhil Goyal 	sg->extension = 1;
1358c3e85bdcSAkhil Goyal 	sg->final = 1;
1359*c5788a10SHemant Agrawal 	sg->length = data_len + ses->iv.length;
136095456e89SShreyansh Jain 	qm_sg_entry_set64(sg, dpaa_mem_vtop(&cf->sg[2]));
1361c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1362c3e85bdcSAkhil Goyal 
1363c3e85bdcSAkhil Goyal 	sg = &cf->sg[2];
1364c3e85bdcSAkhil Goyal 	qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1365c3e85bdcSAkhil Goyal 	sg->length = ses->iv.length;
1366c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1367c3e85bdcSAkhil Goyal 
1368c3e85bdcSAkhil Goyal 	sg++;
1369*c5788a10SHemant Agrawal 	qm_sg_entry_set64(sg, src_start_addr + data_offset);
1370*c5788a10SHemant Agrawal 	sg->length = data_len;
1371c3e85bdcSAkhil Goyal 	sg->final = 1;
1372c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1373c3e85bdcSAkhil Goyal 
1374c3e85bdcSAkhil Goyal 	return cf;
1375c3e85bdcSAkhil Goyal }
1376c3e85bdcSAkhil Goyal 
1377c3e85bdcSAkhil Goyal static inline struct dpaa_sec_job *
1378a74af788SAkhil Goyal build_cipher_auth_gcm_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
1379a74af788SAkhil Goyal {
1380a74af788SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1381a74af788SAkhil Goyal 	struct dpaa_sec_job *cf;
1382a74af788SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1383a74af788SAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
1384a74af788SAkhil Goyal 	struct rte_mbuf *mbuf;
1385a74af788SAkhil Goyal 	uint8_t req_segs;
1386a74af788SAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1387a74af788SAkhil Goyal 			ses->iv.offset);
1388a74af788SAkhil Goyal 
1389a74af788SAkhil Goyal 	if (sym->m_dst) {
1390a74af788SAkhil Goyal 		mbuf = sym->m_dst;
1391a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 4;
1392a74af788SAkhil Goyal 	} else {
1393a74af788SAkhil Goyal 		mbuf = sym->m_src;
1394a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs * 2 + 4;
1395a74af788SAkhil Goyal 	}
1396a74af788SAkhil Goyal 
1397a74af788SAkhil Goyal 	if (ses->auth_only_len)
1398a74af788SAkhil Goyal 		req_segs++;
1399a74af788SAkhil Goyal 
1400f7a5752eSHemant Agrawal 	if (mbuf->nb_segs > MAX_SG_ENTRIES) {
1401f163231eSHemant Agrawal 		DPAA_SEC_DP_ERR("AEAD: Max sec segs supported is %d",
1402a74af788SAkhil Goyal 				MAX_SG_ENTRIES);
1403a74af788SAkhil Goyal 		return NULL;
1404a74af788SAkhil Goyal 	}
1405a74af788SAkhil Goyal 
1406f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, req_segs);
1407a74af788SAkhil Goyal 	if (!ctx)
1408a74af788SAkhil Goyal 		return NULL;
1409a74af788SAkhil Goyal 
1410a74af788SAkhil Goyal 	cf = &ctx->job;
1411a74af788SAkhil Goyal 	ctx->op = op;
1412a74af788SAkhil Goyal 
1413a74af788SAkhil Goyal 	rte_prefetch0(cf->sg);
1414a74af788SAkhil Goyal 
1415a74af788SAkhil Goyal 	/* output */
1416a74af788SAkhil Goyal 	out_sg = &cf->sg[0];
1417a74af788SAkhil Goyal 	out_sg->extension = 1;
1418a74af788SAkhil Goyal 	if (is_encode(ses))
14197a4a6da4SVakul Garg 		out_sg->length = sym->aead.data.length + ses->digest_length;
1420a74af788SAkhil Goyal 	else
14217a4a6da4SVakul Garg 		out_sg->length = sym->aead.data.length;
1422a74af788SAkhil Goyal 
1423a74af788SAkhil Goyal 	/* output sg entries */
1424a74af788SAkhil Goyal 	sg = &cf->sg[2];
142595456e89SShreyansh Jain 	qm_sg_entry_set64(out_sg, dpaa_mem_vtop(sg));
1426a74af788SAkhil Goyal 	cpu_to_hw_sg(out_sg);
1427a74af788SAkhil Goyal 
1428a74af788SAkhil Goyal 	/* 1st seg */
1429a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
14307a4a6da4SVakul Garg 	sg->length = mbuf->data_len - sym->aead.data.offset;
14317a4a6da4SVakul Garg 	sg->offset = sym->aead.data.offset;
1432a74af788SAkhil Goyal 
1433a74af788SAkhil Goyal 	/* Successive segs */
1434a74af788SAkhil Goyal 	mbuf = mbuf->next;
1435a74af788SAkhil Goyal 	while (mbuf) {
1436a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1437a74af788SAkhil Goyal 		sg++;
1438a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1439a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1440a74af788SAkhil Goyal 		mbuf = mbuf->next;
1441a74af788SAkhil Goyal 	}
1442a74af788SAkhil Goyal 	sg->length -= ses->digest_length;
1443a74af788SAkhil Goyal 
1444a74af788SAkhil Goyal 	if (is_encode(ses)) {
1445a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1446a74af788SAkhil Goyal 		/* set auth output */
1447a74af788SAkhil Goyal 		sg++;
1448a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, sym->aead.digest.phys_addr);
1449a74af788SAkhil Goyal 		sg->length = ses->digest_length;
1450a74af788SAkhil Goyal 	}
1451a74af788SAkhil Goyal 	sg->final = 1;
1452a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1453a74af788SAkhil Goyal 
1454a74af788SAkhil Goyal 	/* input */
1455a74af788SAkhil Goyal 	mbuf = sym->m_src;
1456a74af788SAkhil Goyal 	in_sg = &cf->sg[1];
1457a74af788SAkhil Goyal 	in_sg->extension = 1;
1458a74af788SAkhil Goyal 	in_sg->final = 1;
1459a74af788SAkhil Goyal 	if (is_encode(ses))
1460a74af788SAkhil Goyal 		in_sg->length = ses->iv.length + sym->aead.data.length
1461a74af788SAkhil Goyal 							+ ses->auth_only_len;
1462a74af788SAkhil Goyal 	else
1463a74af788SAkhil Goyal 		in_sg->length = ses->iv.length + sym->aead.data.length
1464a74af788SAkhil Goyal 				+ ses->auth_only_len + ses->digest_length;
1465a74af788SAkhil Goyal 
1466a74af788SAkhil Goyal 	/* input sg entries */
1467a74af788SAkhil Goyal 	sg++;
146895456e89SShreyansh Jain 	qm_sg_entry_set64(in_sg, dpaa_mem_vtop(sg));
1469a74af788SAkhil Goyal 	cpu_to_hw_sg(in_sg);
1470a74af788SAkhil Goyal 
1471a74af788SAkhil Goyal 	/* 1st seg IV */
1472a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1473a74af788SAkhil Goyal 	sg->length = ses->iv.length;
1474a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1475a74af788SAkhil Goyal 
1476a74af788SAkhil Goyal 	/* 2nd seg auth only */
1477a74af788SAkhil Goyal 	if (ses->auth_only_len) {
1478a74af788SAkhil Goyal 		sg++;
1479a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, dpaa_mem_vtop(sym->aead.aad.data));
1480a74af788SAkhil Goyal 		sg->length = ses->auth_only_len;
1481a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1482a74af788SAkhil Goyal 	}
1483a74af788SAkhil Goyal 
1484a74af788SAkhil Goyal 	/* 3rd seg */
1485a74af788SAkhil Goyal 	sg++;
1486a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1487a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->aead.data.offset;
1488a74af788SAkhil Goyal 	sg->offset = sym->aead.data.offset;
1489a74af788SAkhil Goyal 
1490a74af788SAkhil Goyal 	/* Successive segs */
1491a74af788SAkhil Goyal 	mbuf = mbuf->next;
1492a74af788SAkhil Goyal 	while (mbuf) {
1493a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1494a74af788SAkhil Goyal 		sg++;
1495a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1496a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1497a74af788SAkhil Goyal 		mbuf = mbuf->next;
1498a74af788SAkhil Goyal 	}
1499a74af788SAkhil Goyal 
1500a74af788SAkhil Goyal 	if (is_decode(ses)) {
1501a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1502a74af788SAkhil Goyal 		sg++;
1503a74af788SAkhil Goyal 		memcpy(ctx->digest, sym->aead.digest.data,
1504a74af788SAkhil Goyal 			ses->digest_length);
150595456e89SShreyansh Jain 		qm_sg_entry_set64(sg, dpaa_mem_vtop(ctx->digest));
1506a74af788SAkhil Goyal 		sg->length = ses->digest_length;
1507a74af788SAkhil Goyal 	}
1508a74af788SAkhil Goyal 	sg->final = 1;
1509a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1510a74af788SAkhil Goyal 
1511a74af788SAkhil Goyal 	return cf;
1512a74af788SAkhil Goyal }
1513a74af788SAkhil Goyal 
1514a74af788SAkhil Goyal static inline struct dpaa_sec_job *
1515c3e85bdcSAkhil Goyal build_cipher_auth_gcm(struct rte_crypto_op *op, dpaa_sec_session *ses)
1516c3e85bdcSAkhil Goyal {
1517c3e85bdcSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1518c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
1519c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1520c3e85bdcSAkhil Goyal 	struct qm_sg_entry *sg;
1521c3e85bdcSAkhil Goyal 	uint32_t length = 0;
1522c4509373SSantosh Shukla 	rte_iova_t src_start_addr, dst_start_addr;
1523c3e85bdcSAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1524c3e85bdcSAkhil Goyal 			ses->iv.offset);
1525c3e85bdcSAkhil Goyal 
1526116ff44aSHemant Agrawal 	src_start_addr = sym->m_src->buf_iova + sym->m_src->data_off;
1527a389434eSAlok Makhariya 
1528a389434eSAlok Makhariya 	if (sym->m_dst)
1529116ff44aSHemant Agrawal 		dst_start_addr = sym->m_dst->buf_iova + sym->m_dst->data_off;
1530a389434eSAlok Makhariya 	else
1531a389434eSAlok Makhariya 		dst_start_addr = src_start_addr;
1532c3e85bdcSAkhil Goyal 
1533f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, 7);
1534c3e85bdcSAkhil Goyal 	if (!ctx)
1535c3e85bdcSAkhil Goyal 		return NULL;
1536c3e85bdcSAkhil Goyal 
1537c3e85bdcSAkhil Goyal 	cf = &ctx->job;
1538c3e85bdcSAkhil Goyal 	ctx->op = op;
1539c3e85bdcSAkhil Goyal 
1540c3e85bdcSAkhil Goyal 	/* input */
1541c3e85bdcSAkhil Goyal 	rte_prefetch0(cf->sg);
1542c3e85bdcSAkhil Goyal 	sg = &cf->sg[2];
154395456e89SShreyansh Jain 	qm_sg_entry_set64(&cf->sg[1], dpaa_mem_vtop(sg));
1544c3e85bdcSAkhil Goyal 	if (is_encode(ses)) {
1545c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1546c3e85bdcSAkhil Goyal 		sg->length = ses->iv.length;
1547c3e85bdcSAkhil Goyal 		length += sg->length;
1548c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1549c3e85bdcSAkhil Goyal 
1550c3e85bdcSAkhil Goyal 		sg++;
1551c3e85bdcSAkhil Goyal 		if (ses->auth_only_len) {
1552c3e85bdcSAkhil Goyal 			qm_sg_entry_set64(sg,
1553c3e85bdcSAkhil Goyal 					  dpaa_mem_vtop(sym->aead.aad.data));
1554c3e85bdcSAkhil Goyal 			sg->length = ses->auth_only_len;
1555c3e85bdcSAkhil Goyal 			length += sg->length;
1556c3e85bdcSAkhil Goyal 			cpu_to_hw_sg(sg);
1557c3e85bdcSAkhil Goyal 			sg++;
1558c3e85bdcSAkhil Goyal 		}
1559a389434eSAlok Makhariya 		qm_sg_entry_set64(sg, src_start_addr + sym->aead.data.offset);
1560c3e85bdcSAkhil Goyal 		sg->length = sym->aead.data.length;
1561c3e85bdcSAkhil Goyal 		length += sg->length;
1562c3e85bdcSAkhil Goyal 		sg->final = 1;
1563c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1564c3e85bdcSAkhil Goyal 	} else {
1565c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1566c3e85bdcSAkhil Goyal 		sg->length = ses->iv.length;
1567c3e85bdcSAkhil Goyal 		length += sg->length;
1568c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1569c3e85bdcSAkhil Goyal 
1570c3e85bdcSAkhil Goyal 		sg++;
1571c3e85bdcSAkhil Goyal 		if (ses->auth_only_len) {
1572c3e85bdcSAkhil Goyal 			qm_sg_entry_set64(sg,
1573c3e85bdcSAkhil Goyal 					  dpaa_mem_vtop(sym->aead.aad.data));
1574c3e85bdcSAkhil Goyal 			sg->length = ses->auth_only_len;
1575c3e85bdcSAkhil Goyal 			length += sg->length;
1576c3e85bdcSAkhil Goyal 			cpu_to_hw_sg(sg);
1577c3e85bdcSAkhil Goyal 			sg++;
1578c3e85bdcSAkhil Goyal 		}
1579a389434eSAlok Makhariya 		qm_sg_entry_set64(sg, src_start_addr + sym->aead.data.offset);
1580c3e85bdcSAkhil Goyal 		sg->length = sym->aead.data.length;
1581c3e85bdcSAkhil Goyal 		length += sg->length;
1582c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1583c3e85bdcSAkhil Goyal 
1584c3e85bdcSAkhil Goyal 		memcpy(ctx->digest, sym->aead.digest.data,
1585c3e85bdcSAkhil Goyal 		       ses->digest_length);
1586c3e85bdcSAkhil Goyal 		sg++;
1587c3e85bdcSAkhil Goyal 
158895456e89SShreyansh Jain 		qm_sg_entry_set64(sg, dpaa_mem_vtop(ctx->digest));
1589c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
1590c3e85bdcSAkhil Goyal 		length += sg->length;
1591c3e85bdcSAkhil Goyal 		sg->final = 1;
1592c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1593c3e85bdcSAkhil Goyal 	}
1594c3e85bdcSAkhil Goyal 	/* input compound frame */
1595c3e85bdcSAkhil Goyal 	cf->sg[1].length = length;
1596c3e85bdcSAkhil Goyal 	cf->sg[1].extension = 1;
1597c3e85bdcSAkhil Goyal 	cf->sg[1].final = 1;
1598c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(&cf->sg[1]);
1599c3e85bdcSAkhil Goyal 
1600c3e85bdcSAkhil Goyal 	/* output */
1601c3e85bdcSAkhil Goyal 	sg++;
160295456e89SShreyansh Jain 	qm_sg_entry_set64(&cf->sg[0], dpaa_mem_vtop(sg));
1603c3e85bdcSAkhil Goyal 	qm_sg_entry_set64(sg,
16047a4a6da4SVakul Garg 		dst_start_addr + sym->aead.data.offset);
16057a4a6da4SVakul Garg 	sg->length = sym->aead.data.length;
1606c3e85bdcSAkhil Goyal 	length = sg->length;
1607c3e85bdcSAkhil Goyal 	if (is_encode(ses)) {
1608c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1609c3e85bdcSAkhil Goyal 		/* set auth output */
1610c3e85bdcSAkhil Goyal 		sg++;
1611c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, sym->aead.digest.phys_addr);
1612c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
1613c3e85bdcSAkhil Goyal 		length += sg->length;
1614c3e85bdcSAkhil Goyal 	}
1615c3e85bdcSAkhil Goyal 	sg->final = 1;
1616c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1617c3e85bdcSAkhil Goyal 
1618c3e85bdcSAkhil Goyal 	/* output compound frame */
1619c3e85bdcSAkhil Goyal 	cf->sg[0].length = length;
1620c3e85bdcSAkhil Goyal 	cf->sg[0].extension = 1;
1621c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(&cf->sg[0]);
1622c3e85bdcSAkhil Goyal 
1623c3e85bdcSAkhil Goyal 	return cf;
1624c3e85bdcSAkhil Goyal }
1625c3e85bdcSAkhil Goyal 
1626c3e85bdcSAkhil Goyal static inline struct dpaa_sec_job *
1627a74af788SAkhil Goyal build_cipher_auth_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
1628a74af788SAkhil Goyal {
1629a74af788SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1630a74af788SAkhil Goyal 	struct dpaa_sec_job *cf;
1631a74af788SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1632a74af788SAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
1633a74af788SAkhil Goyal 	struct rte_mbuf *mbuf;
1634a74af788SAkhil Goyal 	uint8_t req_segs;
1635a74af788SAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1636a74af788SAkhil Goyal 			ses->iv.offset);
1637a74af788SAkhil Goyal 
1638a74af788SAkhil Goyal 	if (sym->m_dst) {
1639a74af788SAkhil Goyal 		mbuf = sym->m_dst;
1640a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 4;
1641a74af788SAkhil Goyal 	} else {
1642a74af788SAkhil Goyal 		mbuf = sym->m_src;
1643a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs * 2 + 4;
1644a74af788SAkhil Goyal 	}
1645a74af788SAkhil Goyal 
1646f7a5752eSHemant Agrawal 	if (mbuf->nb_segs > MAX_SG_ENTRIES) {
1647f163231eSHemant Agrawal 		DPAA_SEC_DP_ERR("Cipher-Auth: Max sec segs supported is %d",
1648a74af788SAkhil Goyal 				MAX_SG_ENTRIES);
1649a74af788SAkhil Goyal 		return NULL;
1650a74af788SAkhil Goyal 	}
1651a74af788SAkhil Goyal 
1652f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, req_segs);
1653a74af788SAkhil Goyal 	if (!ctx)
1654a74af788SAkhil Goyal 		return NULL;
1655a74af788SAkhil Goyal 
1656a74af788SAkhil Goyal 	cf = &ctx->job;
1657a74af788SAkhil Goyal 	ctx->op = op;
1658a74af788SAkhil Goyal 
1659a74af788SAkhil Goyal 	rte_prefetch0(cf->sg);
1660a74af788SAkhil Goyal 
1661a74af788SAkhil Goyal 	/* output */
1662a74af788SAkhil Goyal 	out_sg = &cf->sg[0];
1663a74af788SAkhil Goyal 	out_sg->extension = 1;
1664a74af788SAkhil Goyal 	if (is_encode(ses))
1665a74af788SAkhil Goyal 		out_sg->length = sym->auth.data.length + ses->digest_length;
1666a74af788SAkhil Goyal 	else
1667a74af788SAkhil Goyal 		out_sg->length = sym->auth.data.length;
1668a74af788SAkhil Goyal 
1669a74af788SAkhil Goyal 	/* output sg entries */
1670a74af788SAkhil Goyal 	sg = &cf->sg[2];
167195456e89SShreyansh Jain 	qm_sg_entry_set64(out_sg, dpaa_mem_vtop(sg));
1672a74af788SAkhil Goyal 	cpu_to_hw_sg(out_sg);
1673a74af788SAkhil Goyal 
1674a74af788SAkhil Goyal 	/* 1st seg */
1675a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1676a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->auth.data.offset;
1677a74af788SAkhil Goyal 	sg->offset = sym->auth.data.offset;
1678a74af788SAkhil Goyal 
1679a74af788SAkhil Goyal 	/* Successive segs */
1680a74af788SAkhil Goyal 	mbuf = mbuf->next;
1681a74af788SAkhil Goyal 	while (mbuf) {
1682a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1683a74af788SAkhil Goyal 		sg++;
1684a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1685a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1686a74af788SAkhil Goyal 		mbuf = mbuf->next;
1687a74af788SAkhil Goyal 	}
1688a74af788SAkhil Goyal 	sg->length -= ses->digest_length;
1689a74af788SAkhil Goyal 
1690a74af788SAkhil Goyal 	if (is_encode(ses)) {
1691a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1692a74af788SAkhil Goyal 		/* set auth output */
1693a74af788SAkhil Goyal 		sg++;
1694a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
1695a74af788SAkhil Goyal 		sg->length = ses->digest_length;
1696a74af788SAkhil Goyal 	}
1697a74af788SAkhil Goyal 	sg->final = 1;
1698a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1699a74af788SAkhil Goyal 
1700a74af788SAkhil Goyal 	/* input */
1701a74af788SAkhil Goyal 	mbuf = sym->m_src;
1702a74af788SAkhil Goyal 	in_sg = &cf->sg[1];
1703a74af788SAkhil Goyal 	in_sg->extension = 1;
1704a74af788SAkhil Goyal 	in_sg->final = 1;
1705a74af788SAkhil Goyal 	if (is_encode(ses))
1706a74af788SAkhil Goyal 		in_sg->length = ses->iv.length + sym->auth.data.length;
1707a74af788SAkhil Goyal 	else
1708a74af788SAkhil Goyal 		in_sg->length = ses->iv.length + sym->auth.data.length
1709a74af788SAkhil Goyal 						+ ses->digest_length;
1710a74af788SAkhil Goyal 
1711a74af788SAkhil Goyal 	/* input sg entries */
1712a74af788SAkhil Goyal 	sg++;
171395456e89SShreyansh Jain 	qm_sg_entry_set64(in_sg, dpaa_mem_vtop(sg));
1714a74af788SAkhil Goyal 	cpu_to_hw_sg(in_sg);
1715a74af788SAkhil Goyal 
1716a74af788SAkhil Goyal 	/* 1st seg IV */
1717a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1718a74af788SAkhil Goyal 	sg->length = ses->iv.length;
1719a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1720a74af788SAkhil Goyal 
1721a74af788SAkhil Goyal 	/* 2nd seg */
1722a74af788SAkhil Goyal 	sg++;
1723a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1724a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->auth.data.offset;
1725a74af788SAkhil Goyal 	sg->offset = sym->auth.data.offset;
1726a74af788SAkhil Goyal 
1727a74af788SAkhil Goyal 	/* Successive segs */
1728a74af788SAkhil Goyal 	mbuf = mbuf->next;
1729a74af788SAkhil Goyal 	while (mbuf) {
1730a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1731a74af788SAkhil Goyal 		sg++;
1732a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1733a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1734a74af788SAkhil Goyal 		mbuf = mbuf->next;
1735a74af788SAkhil Goyal 	}
1736a74af788SAkhil Goyal 
1737a74af788SAkhil Goyal 	sg->length -= ses->digest_length;
1738a74af788SAkhil Goyal 	if (is_decode(ses)) {
1739a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1740a74af788SAkhil Goyal 		sg++;
1741a74af788SAkhil Goyal 		memcpy(ctx->digest, sym->auth.digest.data,
1742a74af788SAkhil Goyal 			ses->digest_length);
174395456e89SShreyansh Jain 		qm_sg_entry_set64(sg, dpaa_mem_vtop(ctx->digest));
1744a74af788SAkhil Goyal 		sg->length = ses->digest_length;
1745a74af788SAkhil Goyal 	}
1746a74af788SAkhil Goyal 	sg->final = 1;
1747a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1748a74af788SAkhil Goyal 
1749a74af788SAkhil Goyal 	return cf;
1750a74af788SAkhil Goyal }
1751a74af788SAkhil Goyal 
1752a74af788SAkhil Goyal static inline struct dpaa_sec_job *
1753c3e85bdcSAkhil Goyal build_cipher_auth(struct rte_crypto_op *op, dpaa_sec_session *ses)
1754c3e85bdcSAkhil Goyal {
1755c3e85bdcSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1756c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
1757c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1758c3e85bdcSAkhil Goyal 	struct qm_sg_entry *sg;
1759c4509373SSantosh Shukla 	rte_iova_t src_start_addr, dst_start_addr;
1760c3e85bdcSAkhil Goyal 	uint32_t length = 0;
1761c3e85bdcSAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1762c3e85bdcSAkhil Goyal 			ses->iv.offset);
1763c3e85bdcSAkhil Goyal 
1764455da545SSantosh Shukla 	src_start_addr = sym->m_src->buf_iova + sym->m_src->data_off;
1765a389434eSAlok Makhariya 	if (sym->m_dst)
1766455da545SSantosh Shukla 		dst_start_addr = sym->m_dst->buf_iova + sym->m_dst->data_off;
1767a389434eSAlok Makhariya 	else
1768a389434eSAlok Makhariya 		dst_start_addr = src_start_addr;
1769c3e85bdcSAkhil Goyal 
1770f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, 7);
1771c3e85bdcSAkhil Goyal 	if (!ctx)
1772c3e85bdcSAkhil Goyal 		return NULL;
1773c3e85bdcSAkhil Goyal 
1774c3e85bdcSAkhil Goyal 	cf = &ctx->job;
1775c3e85bdcSAkhil Goyal 	ctx->op = op;
1776c3e85bdcSAkhil Goyal 
1777c3e85bdcSAkhil Goyal 	/* input */
1778c3e85bdcSAkhil Goyal 	rte_prefetch0(cf->sg);
1779c3e85bdcSAkhil Goyal 	sg = &cf->sg[2];
178095456e89SShreyansh Jain 	qm_sg_entry_set64(&cf->sg[1], dpaa_mem_vtop(sg));
1781c3e85bdcSAkhil Goyal 	if (is_encode(ses)) {
1782c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1783c3e85bdcSAkhil Goyal 		sg->length = ses->iv.length;
1784c3e85bdcSAkhil Goyal 		length += sg->length;
1785c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1786c3e85bdcSAkhil Goyal 
1787c3e85bdcSAkhil Goyal 		sg++;
1788a389434eSAlok Makhariya 		qm_sg_entry_set64(sg, src_start_addr + sym->auth.data.offset);
1789c3e85bdcSAkhil Goyal 		sg->length = sym->auth.data.length;
1790c3e85bdcSAkhil Goyal 		length += sg->length;
1791c3e85bdcSAkhil Goyal 		sg->final = 1;
1792c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1793c3e85bdcSAkhil Goyal 	} else {
1794c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1795c3e85bdcSAkhil Goyal 		sg->length = ses->iv.length;
1796c3e85bdcSAkhil Goyal 		length += sg->length;
1797c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1798c3e85bdcSAkhil Goyal 
1799c3e85bdcSAkhil Goyal 		sg++;
1800c3e85bdcSAkhil Goyal 
1801a389434eSAlok Makhariya 		qm_sg_entry_set64(sg, src_start_addr + sym->auth.data.offset);
1802c3e85bdcSAkhil Goyal 		sg->length = sym->auth.data.length;
1803c3e85bdcSAkhil Goyal 		length += sg->length;
1804c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1805c3e85bdcSAkhil Goyal 
1806c3e85bdcSAkhil Goyal 		memcpy(ctx->digest, sym->auth.digest.data,
1807c3e85bdcSAkhil Goyal 		       ses->digest_length);
1808c3e85bdcSAkhil Goyal 		sg++;
1809c3e85bdcSAkhil Goyal 
181095456e89SShreyansh Jain 		qm_sg_entry_set64(sg, dpaa_mem_vtop(ctx->digest));
1811c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
1812c3e85bdcSAkhil Goyal 		length += sg->length;
1813c3e85bdcSAkhil Goyal 		sg->final = 1;
1814c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1815c3e85bdcSAkhil Goyal 	}
1816c3e85bdcSAkhil Goyal 	/* input compound frame */
1817c3e85bdcSAkhil Goyal 	cf->sg[1].length = length;
1818c3e85bdcSAkhil Goyal 	cf->sg[1].extension = 1;
1819c3e85bdcSAkhil Goyal 	cf->sg[1].final = 1;
1820c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(&cf->sg[1]);
1821c3e85bdcSAkhil Goyal 
1822c3e85bdcSAkhil Goyal 	/* output */
1823c3e85bdcSAkhil Goyal 	sg++;
182495456e89SShreyansh Jain 	qm_sg_entry_set64(&cf->sg[0], dpaa_mem_vtop(sg));
1825a389434eSAlok Makhariya 	qm_sg_entry_set64(sg, dst_start_addr + sym->cipher.data.offset);
1826c3e85bdcSAkhil Goyal 	sg->length = sym->cipher.data.length;
1827c3e85bdcSAkhil Goyal 	length = sg->length;
1828c3e85bdcSAkhil Goyal 	if (is_encode(ses)) {
1829c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1830c3e85bdcSAkhil Goyal 		/* set auth output */
1831c3e85bdcSAkhil Goyal 		sg++;
1832c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
1833c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
1834c3e85bdcSAkhil Goyal 		length += sg->length;
1835c3e85bdcSAkhil Goyal 	}
1836c3e85bdcSAkhil Goyal 	sg->final = 1;
1837c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1838c3e85bdcSAkhil Goyal 
1839c3e85bdcSAkhil Goyal 	/* output compound frame */
1840c3e85bdcSAkhil Goyal 	cf->sg[0].length = length;
1841c3e85bdcSAkhil Goyal 	cf->sg[0].extension = 1;
1842c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(&cf->sg[0]);
1843c3e85bdcSAkhil Goyal 
1844c3e85bdcSAkhil Goyal 	return cf;
1845c3e85bdcSAkhil Goyal }
1846c3e85bdcSAkhil Goyal 
18471f14d500SAkhil Goyal static inline struct dpaa_sec_job *
18481f14d500SAkhil Goyal build_proto(struct rte_crypto_op *op, dpaa_sec_session *ses)
18491f14d500SAkhil Goyal {
18501f14d500SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
18511f14d500SAkhil Goyal 	struct dpaa_sec_job *cf;
18521f14d500SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
18531f14d500SAkhil Goyal 	struct qm_sg_entry *sg;
18541f14d500SAkhil Goyal 	phys_addr_t src_start_addr, dst_start_addr;
18551f14d500SAkhil Goyal 
1856f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, 2);
18571f14d500SAkhil Goyal 	if (!ctx)
18581f14d500SAkhil Goyal 		return NULL;
18591f14d500SAkhil Goyal 	cf = &ctx->job;
18601f14d500SAkhil Goyal 	ctx->op = op;
18611f14d500SAkhil Goyal 
18621f14d500SAkhil Goyal 	src_start_addr = rte_pktmbuf_mtophys(sym->m_src);
18631f14d500SAkhil Goyal 
18641f14d500SAkhil Goyal 	if (sym->m_dst)
18651f14d500SAkhil Goyal 		dst_start_addr = rte_pktmbuf_mtophys(sym->m_dst);
18661f14d500SAkhil Goyal 	else
18671f14d500SAkhil Goyal 		dst_start_addr = src_start_addr;
18681f14d500SAkhil Goyal 
18691f14d500SAkhil Goyal 	/* input */
18701f14d500SAkhil Goyal 	sg = &cf->sg[1];
18711f14d500SAkhil Goyal 	qm_sg_entry_set64(sg, src_start_addr);
18721f14d500SAkhil Goyal 	sg->length = sym->m_src->pkt_len;
18731f14d500SAkhil Goyal 	sg->final = 1;
18741f14d500SAkhil Goyal 	cpu_to_hw_sg(sg);
18751f14d500SAkhil Goyal 
18761f14d500SAkhil Goyal 	sym->m_src->packet_type &= ~RTE_PTYPE_L4_MASK;
18771f14d500SAkhil Goyal 	/* output */
18781f14d500SAkhil Goyal 	sg = &cf->sg[0];
18791f14d500SAkhil Goyal 	qm_sg_entry_set64(sg, dst_start_addr);
18801f14d500SAkhil Goyal 	sg->length = sym->m_src->buf_len - sym->m_src->data_off;
18811f14d500SAkhil Goyal 	cpu_to_hw_sg(sg);
18821f14d500SAkhil Goyal 
18831f14d500SAkhil Goyal 	return cf;
18841f14d500SAkhil Goyal }
18851f14d500SAkhil Goyal 
1886fb5c100aSAkhil Goyal static inline struct dpaa_sec_job *
1887fb5c100aSAkhil Goyal build_proto_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
1888fb5c100aSAkhil Goyal {
1889fb5c100aSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1890fb5c100aSAkhil Goyal 	struct dpaa_sec_job *cf;
1891fb5c100aSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1892fb5c100aSAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
1893fb5c100aSAkhil Goyal 	struct rte_mbuf *mbuf;
1894fb5c100aSAkhil Goyal 	uint8_t req_segs;
1895fb5c100aSAkhil Goyal 	uint32_t in_len = 0, out_len = 0;
1896fb5c100aSAkhil Goyal 
1897fb5c100aSAkhil Goyal 	if (sym->m_dst)
1898fb5c100aSAkhil Goyal 		mbuf = sym->m_dst;
1899fb5c100aSAkhil Goyal 	else
1900fb5c100aSAkhil Goyal 		mbuf = sym->m_src;
1901fb5c100aSAkhil Goyal 
1902fb5c100aSAkhil Goyal 	req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 2;
1903f7a5752eSHemant Agrawal 	if (mbuf->nb_segs > MAX_SG_ENTRIES) {
1904fb5c100aSAkhil Goyal 		DPAA_SEC_DP_ERR("Proto: Max sec segs supported is %d",
1905fb5c100aSAkhil Goyal 				MAX_SG_ENTRIES);
1906fb5c100aSAkhil Goyal 		return NULL;
1907fb5c100aSAkhil Goyal 	}
1908fb5c100aSAkhil Goyal 
1909f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, req_segs);
1910fb5c100aSAkhil Goyal 	if (!ctx)
1911fb5c100aSAkhil Goyal 		return NULL;
1912fb5c100aSAkhil Goyal 	cf = &ctx->job;
1913fb5c100aSAkhil Goyal 	ctx->op = op;
1914fb5c100aSAkhil Goyal 	/* output */
1915fb5c100aSAkhil Goyal 	out_sg = &cf->sg[0];
1916fb5c100aSAkhil Goyal 	out_sg->extension = 1;
1917fb5c100aSAkhil Goyal 	qm_sg_entry_set64(out_sg, dpaa_mem_vtop(&cf->sg[2]));
1918fb5c100aSAkhil Goyal 
1919fb5c100aSAkhil Goyal 	/* 1st seg */
1920fb5c100aSAkhil Goyal 	sg = &cf->sg[2];
1921fb5c100aSAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1922fb5c100aSAkhil Goyal 	sg->offset = 0;
1923fb5c100aSAkhil Goyal 
1924fb5c100aSAkhil Goyal 	/* Successive segs */
1925fb5c100aSAkhil Goyal 	while (mbuf->next) {
1926fb5c100aSAkhil Goyal 		sg->length = mbuf->data_len;
1927fb5c100aSAkhil Goyal 		out_len += sg->length;
1928fb5c100aSAkhil Goyal 		mbuf = mbuf->next;
1929fb5c100aSAkhil Goyal 		cpu_to_hw_sg(sg);
1930fb5c100aSAkhil Goyal 		sg++;
1931fb5c100aSAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1932fb5c100aSAkhil Goyal 		sg->offset = 0;
1933fb5c100aSAkhil Goyal 	}
1934fb5c100aSAkhil Goyal 	sg->length = mbuf->buf_len - mbuf->data_off;
1935fb5c100aSAkhil Goyal 	out_len += sg->length;
1936fb5c100aSAkhil Goyal 	sg->final = 1;
1937fb5c100aSAkhil Goyal 	cpu_to_hw_sg(sg);
1938fb5c100aSAkhil Goyal 
1939fb5c100aSAkhil Goyal 	out_sg->length = out_len;
1940fb5c100aSAkhil Goyal 	cpu_to_hw_sg(out_sg);
1941fb5c100aSAkhil Goyal 
1942fb5c100aSAkhil Goyal 	/* input */
1943fb5c100aSAkhil Goyal 	mbuf = sym->m_src;
1944fb5c100aSAkhil Goyal 	in_sg = &cf->sg[1];
1945fb5c100aSAkhil Goyal 	in_sg->extension = 1;
1946fb5c100aSAkhil Goyal 	in_sg->final = 1;
1947fb5c100aSAkhil Goyal 	in_len = mbuf->data_len;
1948fb5c100aSAkhil Goyal 
1949fb5c100aSAkhil Goyal 	sg++;
1950fb5c100aSAkhil Goyal 	qm_sg_entry_set64(in_sg, dpaa_mem_vtop(sg));
1951fb5c100aSAkhil Goyal 
1952fb5c100aSAkhil Goyal 	/* 1st seg */
1953fb5c100aSAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1954fb5c100aSAkhil Goyal 	sg->length = mbuf->data_len;
1955fb5c100aSAkhil Goyal 	sg->offset = 0;
1956fb5c100aSAkhil Goyal 
1957fb5c100aSAkhil Goyal 	/* Successive segs */
1958fb5c100aSAkhil Goyal 	mbuf = mbuf->next;
1959fb5c100aSAkhil Goyal 	while (mbuf) {
1960fb5c100aSAkhil Goyal 		cpu_to_hw_sg(sg);
1961fb5c100aSAkhil Goyal 		sg++;
1962fb5c100aSAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1963fb5c100aSAkhil Goyal 		sg->length = mbuf->data_len;
1964fb5c100aSAkhil Goyal 		sg->offset = 0;
1965fb5c100aSAkhil Goyal 		in_len += sg->length;
1966fb5c100aSAkhil Goyal 		mbuf = mbuf->next;
1967fb5c100aSAkhil Goyal 	}
1968fb5c100aSAkhil Goyal 	sg->final = 1;
1969fb5c100aSAkhil Goyal 	cpu_to_hw_sg(sg);
1970fb5c100aSAkhil Goyal 
1971fb5c100aSAkhil Goyal 	in_sg->length = in_len;
1972fb5c100aSAkhil Goyal 	cpu_to_hw_sg(in_sg);
1973fb5c100aSAkhil Goyal 
1974fb5c100aSAkhil Goyal 	sym->m_src->packet_type &= ~RTE_PTYPE_L4_MASK;
1975fb5c100aSAkhil Goyal 
1976fb5c100aSAkhil Goyal 	return cf;
1977fb5c100aSAkhil Goyal }
1978fb5c100aSAkhil Goyal 
19799a984458SAkhil Goyal static uint16_t
19809a984458SAkhil Goyal dpaa_sec_enqueue_burst(void *qp, struct rte_crypto_op **ops,
19819a984458SAkhil Goyal 		       uint16_t nb_ops)
1982c3e85bdcSAkhil Goyal {
19839a984458SAkhil Goyal 	/* Function to transmit the frames to given device and queuepair */
19849a984458SAkhil Goyal 	uint32_t loop;
19859a984458SAkhil Goyal 	struct dpaa_sec_qp *dpaa_qp = (struct dpaa_sec_qp *)qp;
19869a984458SAkhil Goyal 	uint16_t num_tx = 0;
19879a984458SAkhil Goyal 	struct qm_fd fds[DPAA_SEC_BURST], *fd;
19889a984458SAkhil Goyal 	uint32_t frames_to_send;
19899a984458SAkhil Goyal 	struct rte_crypto_op *op;
1990c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
1991c3e85bdcSAkhil Goyal 	dpaa_sec_session *ses;
19923394ed47SVakul Garg 	uint16_t auth_hdr_len, auth_tail_len;
19933394ed47SVakul Garg 	uint32_t index, flags[DPAA_SEC_BURST] = {0};
19949a984458SAkhil Goyal 	struct qman_fq *inq[DPAA_SEC_BURST];
1995c3e85bdcSAkhil Goyal 
19969a984458SAkhil Goyal 	while (nb_ops) {
19979a984458SAkhil Goyal 		frames_to_send = (nb_ops > DPAA_SEC_BURST) ?
19989a984458SAkhil Goyal 				DPAA_SEC_BURST : nb_ops;
19999a984458SAkhil Goyal 		for (loop = 0; loop < frames_to_send; loop++) {
20009a984458SAkhil Goyal 			op = *(ops++);
2001fe3688baSAkhil Goyal 			if (op->sym->m_src->seqn != 0) {
2002fe3688baSAkhil Goyal 				index = op->sym->m_src->seqn - 1;
2003fe3688baSAkhil Goyal 				if (DPAA_PER_LCORE_DQRR_HELD & (1 << index)) {
2004fe3688baSAkhil Goyal 					/* QM_EQCR_DCA_IDXMASK = 0x0f */
2005fe3688baSAkhil Goyal 					flags[loop] = ((index & 0x0f) << 8);
2006fe3688baSAkhil Goyal 					flags[loop] |= QMAN_ENQUEUE_FLAG_DCA;
2007fe3688baSAkhil Goyal 					DPAA_PER_LCORE_DQRR_SIZE--;
2008fe3688baSAkhil Goyal 					DPAA_PER_LCORE_DQRR_HELD &=
2009fe3688baSAkhil Goyal 								~(1 << index);
2010fe3688baSAkhil Goyal 				}
2011fe3688baSAkhil Goyal 			}
2012fe3688baSAkhil Goyal 
20139a984458SAkhil Goyal 			switch (op->sess_type) {
20149a984458SAkhil Goyal 			case RTE_CRYPTO_OP_WITH_SESSION:
20159a984458SAkhil Goyal 				ses = (dpaa_sec_session *)
2016012c5076SPablo de Lara 					get_sym_session_private_data(
20179a984458SAkhil Goyal 							op->sym->session,
20189a984458SAkhil Goyal 							cryptodev_driver_id);
20199a984458SAkhil Goyal 				break;
20209a984458SAkhil Goyal 			case RTE_CRYPTO_OP_SECURITY_SESSION:
20219a984458SAkhil Goyal 				ses = (dpaa_sec_session *)
20229a984458SAkhil Goyal 					get_sec_session_private_data(
20231f14d500SAkhil Goyal 							op->sym->sec_session);
20249a984458SAkhil Goyal 				break;
20259a984458SAkhil Goyal 			default:
2026f163231eSHemant Agrawal 				DPAA_SEC_DP_ERR(
20279a984458SAkhil Goyal 					"sessionless crypto op not supported");
20289a984458SAkhil Goyal 				frames_to_send = loop;
20299a984458SAkhil Goyal 				nb_ops = loop;
20309a984458SAkhil Goyal 				goto send_pkts;
20319a984458SAkhil Goyal 			}
20324e694fe5SAkhil Goyal 			if (unlikely(!ses->qp[rte_lcore_id() % MAX_DPAA_CORES])) {
20339a984458SAkhil Goyal 				if (dpaa_sec_attach_sess_q(qp, ses)) {
20349a984458SAkhil Goyal 					frames_to_send = loop;
20359a984458SAkhil Goyal 					nb_ops = loop;
20369a984458SAkhil Goyal 					goto send_pkts;
20379a984458SAkhil Goyal 				}
20384e694fe5SAkhil Goyal 			} else if (unlikely(ses->qp[rte_lcore_id() %
20394e694fe5SAkhil Goyal 						MAX_DPAA_CORES] != qp)) {
20409198b2c2SAkhil Goyal 				DPAA_SEC_DP_ERR("Old:sess->qp = %p"
20414e694fe5SAkhil Goyal 					" New qp = %p\n",
20424e694fe5SAkhil Goyal 					ses->qp[rte_lcore_id() %
20434e694fe5SAkhil Goyal 					MAX_DPAA_CORES], qp);
20449198b2c2SAkhil Goyal 				frames_to_send = loop;
20459198b2c2SAkhil Goyal 				nb_ops = loop;
20469198b2c2SAkhil Goyal 				goto send_pkts;
2047c3e85bdcSAkhil Goyal 			}
2048c3e85bdcSAkhil Goyal 
20493394ed47SVakul Garg 			auth_hdr_len = op->sym->auth.data.length -
20509a984458SAkhil Goyal 						op->sym->cipher.data.length;
20513394ed47SVakul Garg 			auth_tail_len = 0;
20523394ed47SVakul Garg 
2053fb5c100aSAkhil Goyal 			if (rte_pktmbuf_is_contiguous(op->sym->m_src) &&
2054fb5c100aSAkhil Goyal 				  ((op->sym->m_dst == NULL) ||
2055fb5c100aSAkhil Goyal 				   rte_pktmbuf_is_contiguous(op->sym->m_dst))) {
205605b12700SHemant Agrawal 				if (is_proto_ipsec(ses)) {
205705b12700SHemant Agrawal 					cf = build_proto(op, ses);
2058a1173d55SHemant Agrawal 				} else if (is_proto_pdcp(ses)) {
2059a1173d55SHemant Agrawal 					cf = build_proto(op, ses);
206005b12700SHemant Agrawal 				} else if (is_auth_only(ses)) {
2061c3e85bdcSAkhil Goyal 					cf = build_auth_only(op, ses);
2062c3e85bdcSAkhil Goyal 				} else if (is_cipher_only(ses)) {
2063c3e85bdcSAkhil Goyal 					cf = build_cipher_only(op, ses);
2064c3e85bdcSAkhil Goyal 				} else if (is_aead(ses)) {
2065c3e85bdcSAkhil Goyal 					cf = build_cipher_auth_gcm(op, ses);
20663394ed47SVakul Garg 					auth_hdr_len = ses->auth_only_len;
2067c3e85bdcSAkhil Goyal 				} else if (is_auth_cipher(ses)) {
20683394ed47SVakul Garg 					auth_hdr_len =
20693394ed47SVakul Garg 						op->sym->cipher.data.offset
20703394ed47SVakul Garg 						- op->sym->auth.data.offset;
20713394ed47SVakul Garg 					auth_tail_len =
20723394ed47SVakul Garg 						op->sym->auth.data.length
20733394ed47SVakul Garg 						- op->sym->cipher.data.length
20743394ed47SVakul Garg 						- auth_hdr_len;
2075c3e85bdcSAkhil Goyal 					cf = build_cipher_auth(op, ses);
2076c3e85bdcSAkhil Goyal 				} else {
2077f163231eSHemant Agrawal 					DPAA_SEC_DP_ERR("not supported ops");
20789a984458SAkhil Goyal 					frames_to_send = loop;
20799a984458SAkhil Goyal 					nb_ops = loop;
20809a984458SAkhil Goyal 					goto send_pkts;
2081c3e85bdcSAkhil Goyal 				}
2082a74af788SAkhil Goyal 			} else {
2083fb5c100aSAkhil Goyal 				if (is_proto_pdcp(ses) || is_proto_ipsec(ses)) {
2084fb5c100aSAkhil Goyal 					cf = build_proto_sg(op, ses);
2085fb5c100aSAkhil Goyal 				} else if (is_auth_only(ses)) {
2086a74af788SAkhil Goyal 					cf = build_auth_only_sg(op, ses);
2087a74af788SAkhil Goyal 				} else if (is_cipher_only(ses)) {
2088a74af788SAkhil Goyal 					cf = build_cipher_only_sg(op, ses);
2089a74af788SAkhil Goyal 				} else if (is_aead(ses)) {
2090a74af788SAkhil Goyal 					cf = build_cipher_auth_gcm_sg(op, ses);
20913394ed47SVakul Garg 					auth_hdr_len = ses->auth_only_len;
2092a74af788SAkhil Goyal 				} else if (is_auth_cipher(ses)) {
20933394ed47SVakul Garg 					auth_hdr_len =
20943394ed47SVakul Garg 						op->sym->cipher.data.offset
20953394ed47SVakul Garg 						- op->sym->auth.data.offset;
20963394ed47SVakul Garg 					auth_tail_len =
20973394ed47SVakul Garg 						op->sym->auth.data.length
20983394ed47SVakul Garg 						- op->sym->cipher.data.length
20993394ed47SVakul Garg 						- auth_hdr_len;
2100a74af788SAkhil Goyal 					cf = build_cipher_auth_sg(op, ses);
2101a74af788SAkhil Goyal 				} else {
2102f163231eSHemant Agrawal 					DPAA_SEC_DP_ERR("not supported ops");
2103a74af788SAkhil Goyal 					frames_to_send = loop;
2104a74af788SAkhil Goyal 					nb_ops = loop;
2105a74af788SAkhil Goyal 					goto send_pkts;
2106a74af788SAkhil Goyal 				}
2107a74af788SAkhil Goyal 			}
21089a984458SAkhil Goyal 			if (unlikely(!cf)) {
21099a984458SAkhil Goyal 				frames_to_send = loop;
21109a984458SAkhil Goyal 				nb_ops = loop;
21119a984458SAkhil Goyal 				goto send_pkts;
21129a984458SAkhil Goyal 			}
2113c3e85bdcSAkhil Goyal 
21149a984458SAkhil Goyal 			fd = &fds[loop];
21154e694fe5SAkhil Goyal 			inq[loop] = ses->inq[rte_lcore_id() % MAX_DPAA_CORES];
21169a984458SAkhil Goyal 			fd->opaque_addr = 0;
21179a984458SAkhil Goyal 			fd->cmd = 0;
211895456e89SShreyansh Jain 			qm_fd_addr_set64(fd, dpaa_mem_vtop(cf->sg));
21199a984458SAkhil Goyal 			fd->_format1 = qm_fd_compound;
21209a984458SAkhil Goyal 			fd->length29 = 2 * sizeof(struct qm_sg_entry);
21213394ed47SVakul Garg 
21229a984458SAkhil Goyal 			/* Auth_only_len is set as 0 in descriptor and it is
21239a984458SAkhil Goyal 			 * overwritten here in the fd.cmd which will update
21249a984458SAkhil Goyal 			 * the DPOVRD reg.
2125c3e85bdcSAkhil Goyal 			 */
21263394ed47SVakul Garg 			if (auth_hdr_len || auth_tail_len) {
21273394ed47SVakul Garg 				fd->cmd = 0x80000000;
21283394ed47SVakul Garg 				fd->cmd |=
21293394ed47SVakul Garg 					((auth_tail_len << 16) | auth_hdr_len);
21303394ed47SVakul Garg 			}
2131c3e85bdcSAkhil Goyal 
21326a0c9d36SAkhil Goyal 			/* In case of PDCP, per packet HFN is stored in
21336a0c9d36SAkhil Goyal 			 * mbuf priv after sym_op.
21346a0c9d36SAkhil Goyal 			 */
21356a0c9d36SAkhil Goyal 			if (is_proto_pdcp(ses) && ses->pdcp.hfn_ovd) {
21366a0c9d36SAkhil Goyal 				fd->cmd = 0x80000000 |
21376a0c9d36SAkhil Goyal 					*((uint32_t *)((uint8_t *)op +
21386a0c9d36SAkhil Goyal 					ses->pdcp.hfn_ovd_offset));
21396a0c9d36SAkhil Goyal 				DPAA_SEC_DP_DEBUG("Per packet HFN: %x, ovd:%u,%u\n",
21406a0c9d36SAkhil Goyal 					*((uint32_t *)((uint8_t *)op +
21416a0c9d36SAkhil Goyal 					ses->pdcp.hfn_ovd_offset)),
21426a0c9d36SAkhil Goyal 					ses->pdcp.hfn_ovd,
21436a0c9d36SAkhil Goyal 					is_proto_pdcp(ses));
21446a0c9d36SAkhil Goyal 			}
21456a0c9d36SAkhil Goyal 
21469a984458SAkhil Goyal 		}
21479a984458SAkhil Goyal send_pkts:
21489a984458SAkhil Goyal 		loop = 0;
21499a984458SAkhil Goyal 		while (loop < frames_to_send) {
21509a984458SAkhil Goyal 			loop += qman_enqueue_multi_fq(&inq[loop], &fds[loop],
2151fe3688baSAkhil Goyal 					&flags[loop], frames_to_send - loop);
21529a984458SAkhil Goyal 		}
21539a984458SAkhil Goyal 		nb_ops -= frames_to_send;
21549a984458SAkhil Goyal 		num_tx += frames_to_send;
2155c3e85bdcSAkhil Goyal 	}
2156c3e85bdcSAkhil Goyal 
2157c3e85bdcSAkhil Goyal 	dpaa_qp->tx_pkts += num_tx;
2158c3e85bdcSAkhil Goyal 	dpaa_qp->tx_errs += nb_ops - num_tx;
2159c3e85bdcSAkhil Goyal 
2160c3e85bdcSAkhil Goyal 	return num_tx;
2161c3e85bdcSAkhil Goyal }
2162c3e85bdcSAkhil Goyal 
2163c3e85bdcSAkhil Goyal static uint16_t
2164c3e85bdcSAkhil Goyal dpaa_sec_dequeue_burst(void *qp, struct rte_crypto_op **ops,
2165c3e85bdcSAkhil Goyal 		       uint16_t nb_ops)
2166c3e85bdcSAkhil Goyal {
2167c3e85bdcSAkhil Goyal 	uint16_t num_rx;
2168c3e85bdcSAkhil Goyal 	struct dpaa_sec_qp *dpaa_qp = (struct dpaa_sec_qp *)qp;
2169c3e85bdcSAkhil Goyal 
2170c3e85bdcSAkhil Goyal 	num_rx = dpaa_sec_deq(dpaa_qp, ops, nb_ops);
2171c3e85bdcSAkhil Goyal 
2172c3e85bdcSAkhil Goyal 	dpaa_qp->rx_pkts += num_rx;
2173c3e85bdcSAkhil Goyal 	dpaa_qp->rx_errs += nb_ops - num_rx;
2174c3e85bdcSAkhil Goyal 
2175f163231eSHemant Agrawal 	DPAA_SEC_DP_DEBUG("SEC Received %d Packets\n", num_rx);
2176c3e85bdcSAkhil Goyal 
2177c3e85bdcSAkhil Goyal 	return num_rx;
2178c3e85bdcSAkhil Goyal }
2179c3e85bdcSAkhil Goyal 
2180c3e85bdcSAkhil Goyal /** Release queue pair */
2181c3e85bdcSAkhil Goyal static int
2182c3e85bdcSAkhil Goyal dpaa_sec_queue_pair_release(struct rte_cryptodev *dev,
2183c3e85bdcSAkhil Goyal 			    uint16_t qp_id)
2184c3e85bdcSAkhil Goyal {
2185c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals;
2186c3e85bdcSAkhil Goyal 	struct dpaa_sec_qp *qp = NULL;
2187c3e85bdcSAkhil Goyal 
2188c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2189c3e85bdcSAkhil Goyal 
2190f163231eSHemant Agrawal 	DPAA_SEC_DEBUG("dev =%p, queue =%d", dev, qp_id);
2191c3e85bdcSAkhil Goyal 
2192c3e85bdcSAkhil Goyal 	internals = dev->data->dev_private;
2193c3e85bdcSAkhil Goyal 	if (qp_id >= internals->max_nb_queue_pairs) {
2194f163231eSHemant Agrawal 		DPAA_SEC_ERR("Max supported qpid %d",
2195c3e85bdcSAkhil Goyal 			     internals->max_nb_queue_pairs);
2196c3e85bdcSAkhil Goyal 		return -EINVAL;
2197c3e85bdcSAkhil Goyal 	}
2198c3e85bdcSAkhil Goyal 
2199c3e85bdcSAkhil Goyal 	qp = &internals->qps[qp_id];
22002ffb940eSAkhil Goyal 	rte_mempool_free(qp->ctx_pool);
2201c3e85bdcSAkhil Goyal 	qp->internals = NULL;
2202c3e85bdcSAkhil Goyal 	dev->data->queue_pairs[qp_id] = NULL;
2203c3e85bdcSAkhil Goyal 
2204c3e85bdcSAkhil Goyal 	return 0;
2205c3e85bdcSAkhil Goyal }
2206c3e85bdcSAkhil Goyal 
2207c3e85bdcSAkhil Goyal /** Setup a queue pair */
2208c3e85bdcSAkhil Goyal static int
2209c3e85bdcSAkhil Goyal dpaa_sec_queue_pair_setup(struct rte_cryptodev *dev, uint16_t qp_id,
2210c3e85bdcSAkhil Goyal 		__rte_unused const struct rte_cryptodev_qp_conf *qp_conf,
2211725d2a7fSFan Zhang 		__rte_unused int socket_id)
2212c3e85bdcSAkhil Goyal {
2213c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals;
2214c3e85bdcSAkhil Goyal 	struct dpaa_sec_qp *qp = NULL;
22152ffb940eSAkhil Goyal 	char str[20];
2216c3e85bdcSAkhil Goyal 
2217f163231eSHemant Agrawal 	DPAA_SEC_DEBUG("dev =%p, queue =%d, conf =%p", dev, qp_id, qp_conf);
2218c3e85bdcSAkhil Goyal 
2219c3e85bdcSAkhil Goyal 	internals = dev->data->dev_private;
2220c3e85bdcSAkhil Goyal 	if (qp_id >= internals->max_nb_queue_pairs) {
2221f163231eSHemant Agrawal 		DPAA_SEC_ERR("Max supported qpid %d",
2222c3e85bdcSAkhil Goyal 			     internals->max_nb_queue_pairs);
2223c3e85bdcSAkhil Goyal 		return -EINVAL;
2224c3e85bdcSAkhil Goyal 	}
2225c3e85bdcSAkhil Goyal 
2226c3e85bdcSAkhil Goyal 	qp = &internals->qps[qp_id];
2227c3e85bdcSAkhil Goyal 	qp->internals = internals;
22282ffb940eSAkhil Goyal 	snprintf(str, sizeof(str), "ctx_pool_d%d_qp%d",
22292ffb940eSAkhil Goyal 			dev->data->dev_id, qp_id);
22302ffb940eSAkhil Goyal 	if (!qp->ctx_pool) {
22312ffb940eSAkhil Goyal 		qp->ctx_pool = rte_mempool_create((const char *)str,
22322ffb940eSAkhil Goyal 							CTX_POOL_NUM_BUFS,
22332ffb940eSAkhil Goyal 							CTX_POOL_BUF_SIZE,
22342ffb940eSAkhil Goyal 							CTX_POOL_CACHE_SIZE, 0,
22352ffb940eSAkhil Goyal 							NULL, NULL, NULL, NULL,
22362ffb940eSAkhil Goyal 							SOCKET_ID_ANY, 0);
22372ffb940eSAkhil Goyal 		if (!qp->ctx_pool) {
22382ffb940eSAkhil Goyal 			DPAA_SEC_ERR("%s create failed\n", str);
22392ffb940eSAkhil Goyal 			return -ENOMEM;
22402ffb940eSAkhil Goyal 		}
22412ffb940eSAkhil Goyal 	} else
22422ffb940eSAkhil Goyal 		DPAA_SEC_INFO("mempool already created for dev_id : %d, qp: %d",
22432ffb940eSAkhil Goyal 				dev->data->dev_id, qp_id);
2244c3e85bdcSAkhil Goyal 	dev->data->queue_pairs[qp_id] = qp;
2245c3e85bdcSAkhil Goyal 
2246c3e85bdcSAkhil Goyal 	return 0;
2247c3e85bdcSAkhil Goyal }
2248c3e85bdcSAkhil Goyal 
2249c3e85bdcSAkhil Goyal /** Return the number of allocated queue pairs */
2250c3e85bdcSAkhil Goyal static uint32_t
2251c3e85bdcSAkhil Goyal dpaa_sec_queue_pair_count(struct rte_cryptodev *dev)
2252c3e85bdcSAkhil Goyal {
2253c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2254c3e85bdcSAkhil Goyal 
2255c3e85bdcSAkhil Goyal 	return dev->data->nb_queue_pairs;
2256c3e85bdcSAkhil Goyal }
2257c3e85bdcSAkhil Goyal 
2258c3e85bdcSAkhil Goyal /** Returns the size of session structure */
2259c3e85bdcSAkhil Goyal static unsigned int
2260012c5076SPablo de Lara dpaa_sec_sym_session_get_size(struct rte_cryptodev *dev __rte_unused)
2261c3e85bdcSAkhil Goyal {
2262c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2263c3e85bdcSAkhil Goyal 
2264c3e85bdcSAkhil Goyal 	return sizeof(dpaa_sec_session);
2265c3e85bdcSAkhil Goyal }
2266c3e85bdcSAkhil Goyal 
2267c3e85bdcSAkhil Goyal static int
2268c3e85bdcSAkhil Goyal dpaa_sec_cipher_init(struct rte_cryptodev *dev __rte_unused,
2269c3e85bdcSAkhil Goyal 		     struct rte_crypto_sym_xform *xform,
2270c3e85bdcSAkhil Goyal 		     dpaa_sec_session *session)
2271c3e85bdcSAkhil Goyal {
2272c3e85bdcSAkhil Goyal 	session->cipher_alg = xform->cipher.algo;
2273c3e85bdcSAkhil Goyal 	session->iv.length = xform->cipher.iv.length;
2274c3e85bdcSAkhil Goyal 	session->iv.offset = xform->cipher.iv.offset;
2275c3e85bdcSAkhil Goyal 	session->cipher_key.data = rte_zmalloc(NULL, xform->cipher.key.length,
2276c3e85bdcSAkhil Goyal 					       RTE_CACHE_LINE_SIZE);
2277c3e85bdcSAkhil Goyal 	if (session->cipher_key.data == NULL && xform->cipher.key.length > 0) {
2278f163231eSHemant Agrawal 		DPAA_SEC_ERR("No Memory for cipher key");
2279c3e85bdcSAkhil Goyal 		return -ENOMEM;
2280c3e85bdcSAkhil Goyal 	}
2281c3e85bdcSAkhil Goyal 	session->cipher_key.length = xform->cipher.key.length;
2282c3e85bdcSAkhil Goyal 
2283c3e85bdcSAkhil Goyal 	memcpy(session->cipher_key.data, xform->cipher.key.data,
2284c3e85bdcSAkhil Goyal 	       xform->cipher.key.length);
2285c3e85bdcSAkhil Goyal 	session->dir = (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
2286c3e85bdcSAkhil Goyal 			DIR_ENC : DIR_DEC;
2287c3e85bdcSAkhil Goyal 
2288c3e85bdcSAkhil Goyal 	return 0;
2289c3e85bdcSAkhil Goyal }
2290c3e85bdcSAkhil Goyal 
2291c3e85bdcSAkhil Goyal static int
2292c3e85bdcSAkhil Goyal dpaa_sec_auth_init(struct rte_cryptodev *dev __rte_unused,
2293c3e85bdcSAkhil Goyal 		   struct rte_crypto_sym_xform *xform,
2294c3e85bdcSAkhil Goyal 		   dpaa_sec_session *session)
2295c3e85bdcSAkhil Goyal {
2296c3e85bdcSAkhil Goyal 	session->auth_alg = xform->auth.algo;
2297c3e85bdcSAkhil Goyal 	session->auth_key.data = rte_zmalloc(NULL, xform->auth.key.length,
2298c3e85bdcSAkhil Goyal 					     RTE_CACHE_LINE_SIZE);
2299c3e85bdcSAkhil Goyal 	if (session->auth_key.data == NULL && xform->auth.key.length > 0) {
2300f163231eSHemant Agrawal 		DPAA_SEC_ERR("No Memory for auth key");
2301c3e85bdcSAkhil Goyal 		return -ENOMEM;
2302c3e85bdcSAkhil Goyal 	}
2303c3e85bdcSAkhil Goyal 	session->auth_key.length = xform->auth.key.length;
2304c3e85bdcSAkhil Goyal 	session->digest_length = xform->auth.digest_length;
2305*c5788a10SHemant Agrawal 	if (session->cipher_alg == RTE_CRYPTO_CIPHER_NULL) {
2306*c5788a10SHemant Agrawal 		session->iv.offset = xform->auth.iv.offset;
2307*c5788a10SHemant Agrawal 		session->iv.length = xform->auth.iv.length;
2308*c5788a10SHemant Agrawal 	}
2309c3e85bdcSAkhil Goyal 
2310c3e85bdcSAkhil Goyal 	memcpy(session->auth_key.data, xform->auth.key.data,
2311c3e85bdcSAkhil Goyal 	       xform->auth.key.length);
2312c3e85bdcSAkhil Goyal 	session->dir = (xform->auth.op == RTE_CRYPTO_AUTH_OP_GENERATE) ?
2313c3e85bdcSAkhil Goyal 			DIR_ENC : DIR_DEC;
2314c3e85bdcSAkhil Goyal 
2315c3e85bdcSAkhil Goyal 	return 0;
2316c3e85bdcSAkhil Goyal }
2317c3e85bdcSAkhil Goyal 
2318c3e85bdcSAkhil Goyal static int
2319c3e85bdcSAkhil Goyal dpaa_sec_aead_init(struct rte_cryptodev *dev __rte_unused,
2320c3e85bdcSAkhil Goyal 		   struct rte_crypto_sym_xform *xform,
2321c3e85bdcSAkhil Goyal 		   dpaa_sec_session *session)
2322c3e85bdcSAkhil Goyal {
2323c3e85bdcSAkhil Goyal 	session->aead_alg = xform->aead.algo;
2324c3e85bdcSAkhil Goyal 	session->iv.length = xform->aead.iv.length;
2325c3e85bdcSAkhil Goyal 	session->iv.offset = xform->aead.iv.offset;
2326c3e85bdcSAkhil Goyal 	session->auth_only_len = xform->aead.aad_length;
2327c3e85bdcSAkhil Goyal 	session->aead_key.data = rte_zmalloc(NULL, xform->aead.key.length,
2328c3e85bdcSAkhil Goyal 					     RTE_CACHE_LINE_SIZE);
2329c3e85bdcSAkhil Goyal 	if (session->aead_key.data == NULL && xform->aead.key.length > 0) {
2330f163231eSHemant Agrawal 		DPAA_SEC_ERR("No Memory for aead key\n");
2331c3e85bdcSAkhil Goyal 		return -ENOMEM;
2332c3e85bdcSAkhil Goyal 	}
2333c3e85bdcSAkhil Goyal 	session->aead_key.length = xform->aead.key.length;
2334c3e85bdcSAkhil Goyal 	session->digest_length = xform->aead.digest_length;
2335c3e85bdcSAkhil Goyal 
2336c3e85bdcSAkhil Goyal 	memcpy(session->aead_key.data, xform->aead.key.data,
2337c3e85bdcSAkhil Goyal 	       xform->aead.key.length);
2338c3e85bdcSAkhil Goyal 	session->dir = (xform->aead.op == RTE_CRYPTO_AEAD_OP_ENCRYPT) ?
2339c3e85bdcSAkhil Goyal 			DIR_ENC : DIR_DEC;
2340c3e85bdcSAkhil Goyal 
2341c3e85bdcSAkhil Goyal 	return 0;
2342c3e85bdcSAkhil Goyal }
2343c3e85bdcSAkhil Goyal 
2344e79416d1SHemant Agrawal static struct qman_fq *
2345e79416d1SHemant Agrawal dpaa_sec_attach_rxq(struct dpaa_sec_dev_private *qi)
2346c3e85bdcSAkhil Goyal {
2347e79416d1SHemant Agrawal 	unsigned int i;
2348c3e85bdcSAkhil Goyal 
2349e621d970SAkhil Goyal 	for (i = 0; i < qi->max_nb_sessions * MAX_DPAA_CORES; i++) {
2350e79416d1SHemant Agrawal 		if (qi->inq_attach[i] == 0) {
2351e79416d1SHemant Agrawal 			qi->inq_attach[i] = 1;
2352e79416d1SHemant Agrawal 			return &qi->inq[i];
2353e79416d1SHemant Agrawal 		}
2354e79416d1SHemant Agrawal 	}
2355e621d970SAkhil Goyal 	DPAA_SEC_WARN("All session in use %u", qi->max_nb_sessions);
2356c3e85bdcSAkhil Goyal 
2357e79416d1SHemant Agrawal 	return NULL;
2358c3e85bdcSAkhil Goyal }
2359c3e85bdcSAkhil Goyal 
2360e79416d1SHemant Agrawal static int
2361e79416d1SHemant Agrawal dpaa_sec_detach_rxq(struct dpaa_sec_dev_private *qi, struct qman_fq *fq)
2362e79416d1SHemant Agrawal {
2363e79416d1SHemant Agrawal 	unsigned int i;
2364e79416d1SHemant Agrawal 
2365e79416d1SHemant Agrawal 	for (i = 0; i < qi->max_nb_sessions; i++) {
2366e79416d1SHemant Agrawal 		if (&qi->inq[i] == fq) {
2367b4053c4bSAlok Makhariya 			qman_retire_fq(fq, NULL);
2368b4053c4bSAlok Makhariya 			qman_oos_fq(fq);
2369e79416d1SHemant Agrawal 			qi->inq_attach[i] = 0;
2370e79416d1SHemant Agrawal 			return 0;
2371e79416d1SHemant Agrawal 		}
2372e79416d1SHemant Agrawal 	}
2373e79416d1SHemant Agrawal 	return -1;
2374e79416d1SHemant Agrawal }
2375e79416d1SHemant Agrawal 
2376e79416d1SHemant Agrawal static int
2377e79416d1SHemant Agrawal dpaa_sec_attach_sess_q(struct dpaa_sec_qp *qp, dpaa_sec_session *sess)
2378e79416d1SHemant Agrawal {
2379e79416d1SHemant Agrawal 	int ret;
2380e79416d1SHemant Agrawal 
23814e694fe5SAkhil Goyal 	sess->qp[rte_lcore_id() % MAX_DPAA_CORES] = qp;
2382e79416d1SHemant Agrawal 	ret = dpaa_sec_prep_cdb(sess);
2383e79416d1SHemant Agrawal 	if (ret) {
2384f163231eSHemant Agrawal 		DPAA_SEC_ERR("Unable to prepare sec cdb");
2385e79416d1SHemant Agrawal 		return -1;
2386e79416d1SHemant Agrawal 	}
23875b0f1bd3SAshish Jain 	if (unlikely(!RTE_PER_LCORE(dpaa_io))) {
23885b0f1bd3SAshish Jain 		ret = rte_dpaa_portal_init((void *)0);
23895b0f1bd3SAshish Jain 		if (ret) {
2390f163231eSHemant Agrawal 			DPAA_SEC_ERR("Failure in affining portal");
23915b0f1bd3SAshish Jain 			return ret;
23925b0f1bd3SAshish Jain 		}
23935b0f1bd3SAshish Jain 	}
23944e694fe5SAkhil Goyal 	ret = dpaa_sec_init_rx(sess->inq[rte_lcore_id() % MAX_DPAA_CORES],
23954e694fe5SAkhil Goyal 			       dpaa_mem_vtop(&sess->cdb),
2396e79416d1SHemant Agrawal 			       qman_fq_fqid(&qp->outq));
2397e79416d1SHemant Agrawal 	if (ret)
2398f163231eSHemant Agrawal 		DPAA_SEC_ERR("Unable to init sec queue");
2399e79416d1SHemant Agrawal 
2400e79416d1SHemant Agrawal 	return ret;
2401c3e85bdcSAkhil Goyal }
2402c3e85bdcSAkhil Goyal 
2403c3e85bdcSAkhil Goyal static int
2404c3e85bdcSAkhil Goyal dpaa_sec_set_session_parameters(struct rte_cryptodev *dev,
2405c3e85bdcSAkhil Goyal 			    struct rte_crypto_sym_xform *xform,	void *sess)
2406c3e85bdcSAkhil Goyal {
2407c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals = dev->data->dev_private;
2408c3e85bdcSAkhil Goyal 	dpaa_sec_session *session = sess;
24094e694fe5SAkhil Goyal 	uint32_t i;
2410c3e85bdcSAkhil Goyal 
2411c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2412c3e85bdcSAkhil Goyal 
2413c3e85bdcSAkhil Goyal 	if (unlikely(sess == NULL)) {
2414f163231eSHemant Agrawal 		DPAA_SEC_ERR("invalid session struct");
2415c3e85bdcSAkhil Goyal 		return -EINVAL;
2416c3e85bdcSAkhil Goyal 	}
2417b0894102SAkhil Goyal 	memset(session, 0, sizeof(dpaa_sec_session));
2418c3e85bdcSAkhil Goyal 
2419c3e85bdcSAkhil Goyal 	/* Default IV length = 0 */
2420c3e85bdcSAkhil Goyal 	session->iv.length = 0;
2421c3e85bdcSAkhil Goyal 
2422c3e85bdcSAkhil Goyal 	/* Cipher Only */
2423c3e85bdcSAkhil Goyal 	if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER && xform->next == NULL) {
2424c3e85bdcSAkhil Goyal 		session->auth_alg = RTE_CRYPTO_AUTH_NULL;
2425c3e85bdcSAkhil Goyal 		dpaa_sec_cipher_init(dev, xform, session);
2426c3e85bdcSAkhil Goyal 
2427c3e85bdcSAkhil Goyal 	/* Authentication Only */
2428c3e85bdcSAkhil Goyal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
2429c3e85bdcSAkhil Goyal 		   xform->next == NULL) {
2430c3e85bdcSAkhil Goyal 		session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
2431c3e85bdcSAkhil Goyal 		dpaa_sec_auth_init(dev, xform, session);
2432c3e85bdcSAkhil Goyal 
2433c3e85bdcSAkhil Goyal 	/* Cipher then Authenticate */
2434c3e85bdcSAkhil Goyal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
2435c3e85bdcSAkhil Goyal 		   xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
2436c3e85bdcSAkhil Goyal 		if (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) {
2437c3e85bdcSAkhil Goyal 			dpaa_sec_cipher_init(dev, xform, session);
2438c3e85bdcSAkhil Goyal 			dpaa_sec_auth_init(dev, xform->next, session);
2439c3e85bdcSAkhil Goyal 		} else {
2440f163231eSHemant Agrawal 			DPAA_SEC_ERR("Not supported: Auth then Cipher");
2441c3e85bdcSAkhil Goyal 			return -EINVAL;
2442c3e85bdcSAkhil Goyal 		}
2443c3e85bdcSAkhil Goyal 
2444c3e85bdcSAkhil Goyal 	/* Authenticate then Cipher */
2445c3e85bdcSAkhil Goyal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
2446c3e85bdcSAkhil Goyal 		   xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
2447c3e85bdcSAkhil Goyal 		if (xform->next->cipher.op == RTE_CRYPTO_CIPHER_OP_DECRYPT) {
2448c3e85bdcSAkhil Goyal 			dpaa_sec_auth_init(dev, xform, session);
2449c3e85bdcSAkhil Goyal 			dpaa_sec_cipher_init(dev, xform->next, session);
2450c3e85bdcSAkhil Goyal 		} else {
2451f163231eSHemant Agrawal 			DPAA_SEC_ERR("Not supported: Auth then Cipher");
2452c3e85bdcSAkhil Goyal 			return -EINVAL;
2453c3e85bdcSAkhil Goyal 		}
2454c3e85bdcSAkhil Goyal 
2455c3e85bdcSAkhil Goyal 	/* AEAD operation for AES-GCM kind of Algorithms */
2456c3e85bdcSAkhil Goyal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD &&
2457c3e85bdcSAkhil Goyal 		   xform->next == NULL) {
2458c3e85bdcSAkhil Goyal 		dpaa_sec_aead_init(dev, xform, session);
2459c3e85bdcSAkhil Goyal 
2460c3e85bdcSAkhil Goyal 	} else {
2461f163231eSHemant Agrawal 		DPAA_SEC_ERR("Invalid crypto type");
2462c3e85bdcSAkhil Goyal 		return -EINVAL;
2463c3e85bdcSAkhil Goyal 	}
24643b617ee7SAkhil Goyal 	rte_spinlock_lock(&internals->lock);
24654e694fe5SAkhil Goyal 	for (i = 0; i < MAX_DPAA_CORES; i++) {
24664e694fe5SAkhil Goyal 		session->inq[i] = dpaa_sec_attach_rxq(internals);
24674e694fe5SAkhil Goyal 		if (session->inq[i] == NULL) {
2468f163231eSHemant Agrawal 			DPAA_SEC_ERR("unable to attach sec queue");
24694e694fe5SAkhil Goyal 			rte_spinlock_unlock(&internals->lock);
2470e79416d1SHemant Agrawal 			goto err1;
2471e79416d1SHemant Agrawal 		}
24724e694fe5SAkhil Goyal 	}
24734e694fe5SAkhil Goyal 	rte_spinlock_unlock(&internals->lock);
2474c3e85bdcSAkhil Goyal 
2475c3e85bdcSAkhil Goyal 	return 0;
2476e79416d1SHemant Agrawal 
2477e79416d1SHemant Agrawal err1:
2478e79416d1SHemant Agrawal 	rte_free(session->cipher_key.data);
2479e79416d1SHemant Agrawal 	rte_free(session->auth_key.data);
2480e79416d1SHemant Agrawal 	memset(session, 0, sizeof(dpaa_sec_session));
2481e79416d1SHemant Agrawal 
2482e79416d1SHemant Agrawal 	return -EINVAL;
2483c3e85bdcSAkhil Goyal }
2484c3e85bdcSAkhil Goyal 
2485c3e85bdcSAkhil Goyal static int
2486012c5076SPablo de Lara dpaa_sec_sym_session_configure(struct rte_cryptodev *dev,
2487c3e85bdcSAkhil Goyal 		struct rte_crypto_sym_xform *xform,
2488c3e85bdcSAkhil Goyal 		struct rte_cryptodev_sym_session *sess,
2489c3e85bdcSAkhil Goyal 		struct rte_mempool *mempool)
2490c3e85bdcSAkhil Goyal {
2491c3e85bdcSAkhil Goyal 	void *sess_private_data;
2492c3e85bdcSAkhil Goyal 	int ret;
2493c3e85bdcSAkhil Goyal 
2494c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2495c3e85bdcSAkhil Goyal 
2496c3e85bdcSAkhil Goyal 	if (rte_mempool_get(mempool, &sess_private_data)) {
2497f163231eSHemant Agrawal 		DPAA_SEC_ERR("Couldn't get object from session mempool");
2498c3e85bdcSAkhil Goyal 		return -ENOMEM;
2499c3e85bdcSAkhil Goyal 	}
2500c3e85bdcSAkhil Goyal 
2501c3e85bdcSAkhil Goyal 	ret = dpaa_sec_set_session_parameters(dev, xform, sess_private_data);
2502c3e85bdcSAkhil Goyal 	if (ret != 0) {
2503f163231eSHemant Agrawal 		DPAA_SEC_ERR("failed to configure session parameters");
2504c3e85bdcSAkhil Goyal 
2505c3e85bdcSAkhil Goyal 		/* Return session to mempool */
2506c3e85bdcSAkhil Goyal 		rte_mempool_put(mempool, sess_private_data);
2507c3e85bdcSAkhil Goyal 		return ret;
2508c3e85bdcSAkhil Goyal 	}
2509c3e85bdcSAkhil Goyal 
2510012c5076SPablo de Lara 	set_sym_session_private_data(sess, dev->driver_id,
2511c3e85bdcSAkhil Goyal 			sess_private_data);
2512c3e85bdcSAkhil Goyal 
2513e79416d1SHemant Agrawal 
2514c3e85bdcSAkhil Goyal 	return 0;
2515c3e85bdcSAkhil Goyal }
2516c3e85bdcSAkhil Goyal 
25173d0d5332SAkhil Goyal static inline void
25183d0d5332SAkhil Goyal free_session_memory(struct rte_cryptodev *dev, dpaa_sec_session *s)
2519c3e85bdcSAkhil Goyal {
2520e79416d1SHemant Agrawal 	struct dpaa_sec_dev_private *qi = dev->data->dev_private;
25213d0d5332SAkhil Goyal 	struct rte_mempool *sess_mp = rte_mempool_from_obj((void *)s);
25223d0d5332SAkhil Goyal 	uint8_t i;
2523e79416d1SHemant Agrawal 
2524e621d970SAkhil Goyal 	for (i = 0; i < MAX_DPAA_CORES; i++) {
2525e621d970SAkhil Goyal 		if (s->inq[i])
2526e621d970SAkhil Goyal 			dpaa_sec_detach_rxq(qi, s->inq[i]);
2527e621d970SAkhil Goyal 		s->inq[i] = NULL;
2528e621d970SAkhil Goyal 		s->qp[i] = NULL;
2529e621d970SAkhil Goyal 	}
2530c3e85bdcSAkhil Goyal 	rte_free(s->cipher_key.data);
2531c3e85bdcSAkhil Goyal 	rte_free(s->auth_key.data);
2532c3e85bdcSAkhil Goyal 	memset(s, 0, sizeof(dpaa_sec_session));
25333d0d5332SAkhil Goyal 	rte_mempool_put(sess_mp, (void *)s);
25343d0d5332SAkhil Goyal }
25353d0d5332SAkhil Goyal 
25363d0d5332SAkhil Goyal /** Clear the memory of session so it doesn't leave key material behind */
25373d0d5332SAkhil Goyal static void
25383d0d5332SAkhil Goyal dpaa_sec_sym_session_clear(struct rte_cryptodev *dev,
25393d0d5332SAkhil Goyal 		struct rte_cryptodev_sym_session *sess)
25403d0d5332SAkhil Goyal {
25413d0d5332SAkhil Goyal 	PMD_INIT_FUNC_TRACE();
25423d0d5332SAkhil Goyal 	uint8_t index = dev->driver_id;
25433d0d5332SAkhil Goyal 	void *sess_priv = get_sym_session_private_data(sess, index);
25443d0d5332SAkhil Goyal 	dpaa_sec_session *s = (dpaa_sec_session *)sess_priv;
25453d0d5332SAkhil Goyal 
25463d0d5332SAkhil Goyal 	if (sess_priv) {
25473d0d5332SAkhil Goyal 		free_session_memory(dev, s);
2548012c5076SPablo de Lara 		set_sym_session_private_data(sess, index, NULL);
2549c3e85bdcSAkhil Goyal 	}
2550c3e85bdcSAkhil Goyal }
2551c3e85bdcSAkhil Goyal 
2552c3e85bdcSAkhil Goyal static int
25531f14d500SAkhil Goyal dpaa_sec_set_ipsec_session(__rte_unused struct rte_cryptodev *dev,
25541f14d500SAkhil Goyal 			   struct rte_security_session_conf *conf,
25551f14d500SAkhil Goyal 			   void *sess)
25561f14d500SAkhil Goyal {
25571f14d500SAkhil Goyal 	struct dpaa_sec_dev_private *internals = dev->data->dev_private;
25581f14d500SAkhil Goyal 	struct rte_security_ipsec_xform *ipsec_xform = &conf->ipsec;
255905b12700SHemant Agrawal 	struct rte_crypto_auth_xform *auth_xform = NULL;
256005b12700SHemant Agrawal 	struct rte_crypto_cipher_xform *cipher_xform = NULL;
25611f14d500SAkhil Goyal 	dpaa_sec_session *session = (dpaa_sec_session *)sess;
25624e694fe5SAkhil Goyal 	uint32_t i;
25631f14d500SAkhil Goyal 
25641f14d500SAkhil Goyal 	PMD_INIT_FUNC_TRACE();
25651f14d500SAkhil Goyal 
2566b0894102SAkhil Goyal 	memset(session, 0, sizeof(dpaa_sec_session));
25671f14d500SAkhil Goyal 	if (ipsec_xform->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
25681f14d500SAkhil Goyal 		cipher_xform = &conf->crypto_xform->cipher;
256905b12700SHemant Agrawal 		if (conf->crypto_xform->next)
25701f14d500SAkhil Goyal 			auth_xform = &conf->crypto_xform->next->auth;
25711f14d500SAkhil Goyal 	} else {
25721f14d500SAkhil Goyal 		auth_xform = &conf->crypto_xform->auth;
257305b12700SHemant Agrawal 		if (conf->crypto_xform->next)
25741f14d500SAkhil Goyal 			cipher_xform = &conf->crypto_xform->next->cipher;
25751f14d500SAkhil Goyal 	}
25761f14d500SAkhil Goyal 	session->proto_alg = conf->protocol;
257705b12700SHemant Agrawal 
257805b12700SHemant Agrawal 	if (cipher_xform && cipher_xform->algo != RTE_CRYPTO_CIPHER_NULL) {
25791f14d500SAkhil Goyal 		session->cipher_key.data = rte_zmalloc(NULL,
25801f14d500SAkhil Goyal 						       cipher_xform->key.length,
25811f14d500SAkhil Goyal 						       RTE_CACHE_LINE_SIZE);
25821f14d500SAkhil Goyal 		if (session->cipher_key.data == NULL &&
25831f14d500SAkhil Goyal 				cipher_xform->key.length > 0) {
2584f163231eSHemant Agrawal 			DPAA_SEC_ERR("No Memory for cipher key");
25851f14d500SAkhil Goyal 			return -ENOMEM;
25861f14d500SAkhil Goyal 		}
258705b12700SHemant Agrawal 		memcpy(session->cipher_key.data, cipher_xform->key.data,
258805b12700SHemant Agrawal 				cipher_xform->key.length);
25891f14d500SAkhil Goyal 		session->cipher_key.length = cipher_xform->key.length;
259005b12700SHemant Agrawal 
259105b12700SHemant Agrawal 		switch (cipher_xform->algo) {
259205b12700SHemant Agrawal 		case RTE_CRYPTO_CIPHER_AES_CBC:
259305b12700SHemant Agrawal 		case RTE_CRYPTO_CIPHER_3DES_CBC:
259405b12700SHemant Agrawal 		case RTE_CRYPTO_CIPHER_AES_CTR:
259505b12700SHemant Agrawal 			break;
259605b12700SHemant Agrawal 		default:
259705b12700SHemant Agrawal 			DPAA_SEC_ERR("Crypto: Unsupported Cipher alg %u",
259805b12700SHemant Agrawal 				cipher_xform->algo);
259905b12700SHemant Agrawal 			goto out;
260005b12700SHemant Agrawal 		}
260105b12700SHemant Agrawal 		session->cipher_alg = cipher_xform->algo;
260205b12700SHemant Agrawal 	} else {
260305b12700SHemant Agrawal 		session->cipher_key.data = NULL;
260405b12700SHemant Agrawal 		session->cipher_key.length = 0;
260505b12700SHemant Agrawal 		session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
260605b12700SHemant Agrawal 	}
260705b12700SHemant Agrawal 
260805b12700SHemant Agrawal 	if (auth_xform && auth_xform->algo != RTE_CRYPTO_AUTH_NULL) {
26091f14d500SAkhil Goyal 		session->auth_key.data = rte_zmalloc(NULL,
26101f14d500SAkhil Goyal 						auth_xform->key.length,
26111f14d500SAkhil Goyal 						RTE_CACHE_LINE_SIZE);
26121f14d500SAkhil Goyal 		if (session->auth_key.data == NULL &&
26131f14d500SAkhil Goyal 				auth_xform->key.length > 0) {
2614f163231eSHemant Agrawal 			DPAA_SEC_ERR("No Memory for auth key");
26151f14d500SAkhil Goyal 			rte_free(session->cipher_key.data);
26161f14d500SAkhil Goyal 			return -ENOMEM;
26171f14d500SAkhil Goyal 		}
26181f14d500SAkhil Goyal 		memcpy(session->auth_key.data, auth_xform->key.data,
26191f14d500SAkhil Goyal 				auth_xform->key.length);
262005b12700SHemant Agrawal 		session->auth_key.length = auth_xform->key.length;
26211f14d500SAkhil Goyal 
26221f14d500SAkhil Goyal 		switch (auth_xform->algo) {
26231f14d500SAkhil Goyal 		case RTE_CRYPTO_AUTH_SHA1_HMAC:
26241f14d500SAkhil Goyal 		case RTE_CRYPTO_AUTH_MD5_HMAC:
26251f14d500SAkhil Goyal 		case RTE_CRYPTO_AUTH_SHA256_HMAC:
26261f14d500SAkhil Goyal 		case RTE_CRYPTO_AUTH_SHA384_HMAC:
26271f14d500SAkhil Goyal 		case RTE_CRYPTO_AUTH_SHA512_HMAC:
26281f14d500SAkhil Goyal 		case RTE_CRYPTO_AUTH_AES_CMAC:
26291f14d500SAkhil Goyal 			break;
263005b12700SHemant Agrawal 		default:
2631f163231eSHemant Agrawal 			DPAA_SEC_ERR("Crypto: Unsupported auth alg %u",
26321f14d500SAkhil Goyal 				auth_xform->algo);
26331f14d500SAkhil Goyal 			goto out;
26341f14d500SAkhil Goyal 		}
263505b12700SHemant Agrawal 		session->auth_alg = auth_xform->algo;
263605b12700SHemant Agrawal 	} else {
263705b12700SHemant Agrawal 		session->auth_key.data = NULL;
263805b12700SHemant Agrawal 		session->auth_key.length = 0;
263905b12700SHemant Agrawal 		session->auth_alg = RTE_CRYPTO_AUTH_NULL;
26401f14d500SAkhil Goyal 	}
26411f14d500SAkhil Goyal 
26421f14d500SAkhil Goyal 	if (ipsec_xform->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
26435ab35d2eSAkhil Goyal 		if (ipsec_xform->tunnel.type ==
26445ab35d2eSAkhil Goyal 				RTE_SECURITY_IPSEC_TUNNEL_IPV4) {
26455ab35d2eSAkhil Goyal 			memset(&session->encap_pdb, 0,
26465ab35d2eSAkhil Goyal 				sizeof(struct ipsec_encap_pdb) +
26471f14d500SAkhil Goyal 				sizeof(session->ip4_hdr));
26481f14d500SAkhil Goyal 			session->ip4_hdr.ip_v = IPVERSION;
26491f14d500SAkhil Goyal 			session->ip4_hdr.ip_hl = 5;
26501f14d500SAkhil Goyal 			session->ip4_hdr.ip_len = rte_cpu_to_be_16(
26511f14d500SAkhil Goyal 						sizeof(session->ip4_hdr));
26521f14d500SAkhil Goyal 			session->ip4_hdr.ip_tos = ipsec_xform->tunnel.ipv4.dscp;
26531f14d500SAkhil Goyal 			session->ip4_hdr.ip_id = 0;
26541f14d500SAkhil Goyal 			session->ip4_hdr.ip_off = 0;
26551f14d500SAkhil Goyal 			session->ip4_hdr.ip_ttl = ipsec_xform->tunnel.ipv4.ttl;
26561f14d500SAkhil Goyal 			session->ip4_hdr.ip_p = (ipsec_xform->proto ==
26575ab35d2eSAkhil Goyal 					RTE_SECURITY_IPSEC_SA_PROTO_ESP) ?
26585ab35d2eSAkhil Goyal 					IPPROTO_ESP : IPPROTO_AH;
26591f14d500SAkhil Goyal 			session->ip4_hdr.ip_sum = 0;
26605ab35d2eSAkhil Goyal 			session->ip4_hdr.ip_src =
26615ab35d2eSAkhil Goyal 					ipsec_xform->tunnel.ipv4.src_ip;
26625ab35d2eSAkhil Goyal 			session->ip4_hdr.ip_dst =
26635ab35d2eSAkhil Goyal 					ipsec_xform->tunnel.ipv4.dst_ip;
26641f14d500SAkhil Goyal 			session->ip4_hdr.ip_sum = calc_chksum((uint16_t *)
26651f14d500SAkhil Goyal 						(void *)&session->ip4_hdr,
26661f14d500SAkhil Goyal 						sizeof(struct ip));
26675ab35d2eSAkhil Goyal 			session->encap_pdb.ip_hdr_len = sizeof(struct ip);
26685ab35d2eSAkhil Goyal 		} else if (ipsec_xform->tunnel.type ==
26695ab35d2eSAkhil Goyal 				RTE_SECURITY_IPSEC_TUNNEL_IPV6) {
26705ab35d2eSAkhil Goyal 			memset(&session->encap_pdb, 0,
26715ab35d2eSAkhil Goyal 				sizeof(struct ipsec_encap_pdb) +
26725ab35d2eSAkhil Goyal 				sizeof(session->ip6_hdr));
26735ab35d2eSAkhil Goyal 			session->ip6_hdr.vtc_flow = rte_cpu_to_be_32(
26745ab35d2eSAkhil Goyal 				DPAA_IPv6_DEFAULT_VTC_FLOW |
26755ab35d2eSAkhil Goyal 				((ipsec_xform->tunnel.ipv6.dscp <<
26765ab35d2eSAkhil Goyal 					RTE_IPV6_HDR_TC_SHIFT) &
26775ab35d2eSAkhil Goyal 					RTE_IPV6_HDR_TC_MASK) |
26785ab35d2eSAkhil Goyal 				((ipsec_xform->tunnel.ipv6.flabel <<
26795ab35d2eSAkhil Goyal 					RTE_IPV6_HDR_FL_SHIFT) &
26805ab35d2eSAkhil Goyal 					RTE_IPV6_HDR_FL_MASK));
26815ab35d2eSAkhil Goyal 			/* Payload length will be updated by HW */
26825ab35d2eSAkhil Goyal 			session->ip6_hdr.payload_len = 0;
26835ab35d2eSAkhil Goyal 			session->ip6_hdr.hop_limits =
26845ab35d2eSAkhil Goyal 					ipsec_xform->tunnel.ipv6.hlimit;
26855ab35d2eSAkhil Goyal 			session->ip6_hdr.proto = (ipsec_xform->proto ==
26865ab35d2eSAkhil Goyal 					RTE_SECURITY_IPSEC_SA_PROTO_ESP) ?
26875ab35d2eSAkhil Goyal 					IPPROTO_ESP : IPPROTO_AH;
26885ab35d2eSAkhil Goyal 			memcpy(&session->ip6_hdr.src_addr,
26895ab35d2eSAkhil Goyal 					&ipsec_xform->tunnel.ipv6.src_addr, 16);
26905ab35d2eSAkhil Goyal 			memcpy(&session->ip6_hdr.dst_addr,
26915ab35d2eSAkhil Goyal 					&ipsec_xform->tunnel.ipv6.dst_addr, 16);
26925ab35d2eSAkhil Goyal 			session->encap_pdb.ip_hdr_len =
26935ab35d2eSAkhil Goyal 						sizeof(struct rte_ipv6_hdr);
26945ab35d2eSAkhil Goyal 		}
26951f14d500SAkhil Goyal 		session->encap_pdb.options =
26961f14d500SAkhil Goyal 			(IPVERSION << PDBNH_ESP_ENCAP_SHIFT) |
26971f14d500SAkhil Goyal 			PDBOPTS_ESP_OIHI_PDB_INL |
26981f14d500SAkhil Goyal 			PDBOPTS_ESP_IVSRC |
269979fde9d0SAkhil Goyal 			PDBHMO_ESP_ENCAP_DTTL |
270079fde9d0SAkhil Goyal 			PDBHMO_ESP_SNR;
27010f318781SAkhil Goyal 		if (ipsec_xform->options.esn)
27020f318781SAkhil Goyal 			session->encap_pdb.options |= PDBOPTS_ESP_ESN;
27031f14d500SAkhil Goyal 		session->encap_pdb.spi = ipsec_xform->spi;
27041f14d500SAkhil Goyal 		session->dir = DIR_ENC;
27051f14d500SAkhil Goyal 	} else if (ipsec_xform->direction ==
27061f14d500SAkhil Goyal 			RTE_SECURITY_IPSEC_SA_DIR_INGRESS) {
27071f14d500SAkhil Goyal 		memset(&session->decap_pdb, 0, sizeof(struct ipsec_decap_pdb));
27085ab35d2eSAkhil Goyal 		if (ipsec_xform->tunnel.type == RTE_SECURITY_IPSEC_TUNNEL_IPV4)
27091f14d500SAkhil Goyal 			session->decap_pdb.options = sizeof(struct ip) << 16;
27105ab35d2eSAkhil Goyal 		else
27115ab35d2eSAkhil Goyal 			session->decap_pdb.options =
27125ab35d2eSAkhil Goyal 					sizeof(struct rte_ipv6_hdr) << 16;
27130f318781SAkhil Goyal 		if (ipsec_xform->options.esn)
27140f318781SAkhil Goyal 			session->decap_pdb.options |= PDBOPTS_ESP_ESN;
27151f14d500SAkhil Goyal 		session->dir = DIR_DEC;
27161f14d500SAkhil Goyal 	} else
27171f14d500SAkhil Goyal 		goto out;
27183b617ee7SAkhil Goyal 	rte_spinlock_lock(&internals->lock);
27194e694fe5SAkhil Goyal 	for (i = 0; i < MAX_DPAA_CORES; i++) {
27204e694fe5SAkhil Goyal 		session->inq[i] = dpaa_sec_attach_rxq(internals);
27214e694fe5SAkhil Goyal 		if (session->inq[i] == NULL) {
2722f163231eSHemant Agrawal 			DPAA_SEC_ERR("unable to attach sec queue");
27234e694fe5SAkhil Goyal 			rte_spinlock_unlock(&internals->lock);
27241f14d500SAkhil Goyal 			goto out;
27251f14d500SAkhil Goyal 		}
27264e694fe5SAkhil Goyal 	}
27274e694fe5SAkhil Goyal 	rte_spinlock_unlock(&internals->lock);
27281f14d500SAkhil Goyal 
2729a1173d55SHemant Agrawal 	return 0;
2730a1173d55SHemant Agrawal out:
2731a1173d55SHemant Agrawal 	rte_free(session->auth_key.data);
2732a1173d55SHemant Agrawal 	rte_free(session->cipher_key.data);
2733a1173d55SHemant Agrawal 	memset(session, 0, sizeof(dpaa_sec_session));
2734a1173d55SHemant Agrawal 	return -1;
2735a1173d55SHemant Agrawal }
27361f14d500SAkhil Goyal 
2737a1173d55SHemant Agrawal static int
2738a1173d55SHemant Agrawal dpaa_sec_set_pdcp_session(struct rte_cryptodev *dev,
2739a1173d55SHemant Agrawal 			  struct rte_security_session_conf *conf,
2740a1173d55SHemant Agrawal 			  void *sess)
2741a1173d55SHemant Agrawal {
2742a1173d55SHemant Agrawal 	struct rte_security_pdcp_xform *pdcp_xform = &conf->pdcp;
2743a1173d55SHemant Agrawal 	struct rte_crypto_sym_xform *xform = conf->crypto_xform;
2744a1173d55SHemant Agrawal 	struct rte_crypto_auth_xform *auth_xform = NULL;
2745a1173d55SHemant Agrawal 	struct rte_crypto_cipher_xform *cipher_xform = NULL;
2746a1173d55SHemant Agrawal 	dpaa_sec_session *session = (dpaa_sec_session *)sess;
2747a1173d55SHemant Agrawal 	struct dpaa_sec_dev_private *dev_priv = dev->data->dev_private;
27484e694fe5SAkhil Goyal 	uint32_t i;
2749a1173d55SHemant Agrawal 
2750a1173d55SHemant Agrawal 	PMD_INIT_FUNC_TRACE();
2751a1173d55SHemant Agrawal 
2752a1173d55SHemant Agrawal 	memset(session, 0, sizeof(dpaa_sec_session));
2753a1173d55SHemant Agrawal 
2754a1173d55SHemant Agrawal 	/* find xfrm types */
2755a1173d55SHemant Agrawal 	if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
2756a1173d55SHemant Agrawal 		cipher_xform = &xform->cipher;
2757a1173d55SHemant Agrawal 		if (xform->next != NULL)
2758a1173d55SHemant Agrawal 			auth_xform = &xform->next->auth;
2759a1173d55SHemant Agrawal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
2760a1173d55SHemant Agrawal 		auth_xform = &xform->auth;
2761a1173d55SHemant Agrawal 		if (xform->next != NULL)
2762a1173d55SHemant Agrawal 			cipher_xform = &xform->next->cipher;
2763a1173d55SHemant Agrawal 	} else {
2764a1173d55SHemant Agrawal 		DPAA_SEC_ERR("Invalid crypto type");
2765a1173d55SHemant Agrawal 		return -EINVAL;
2766a1173d55SHemant Agrawal 	}
2767a1173d55SHemant Agrawal 
2768a1173d55SHemant Agrawal 	session->proto_alg = conf->protocol;
2769a1173d55SHemant Agrawal 	if (cipher_xform) {
2770a1173d55SHemant Agrawal 		session->cipher_key.data = rte_zmalloc(NULL,
2771a1173d55SHemant Agrawal 					       cipher_xform->key.length,
2772a1173d55SHemant Agrawal 					       RTE_CACHE_LINE_SIZE);
2773a1173d55SHemant Agrawal 		if (session->cipher_key.data == NULL &&
2774a1173d55SHemant Agrawal 				cipher_xform->key.length > 0) {
2775a1173d55SHemant Agrawal 			DPAA_SEC_ERR("No Memory for cipher key");
2776a1173d55SHemant Agrawal 			return -ENOMEM;
2777a1173d55SHemant Agrawal 		}
2778a1173d55SHemant Agrawal 		session->cipher_key.length = cipher_xform->key.length;
2779a1173d55SHemant Agrawal 		memcpy(session->cipher_key.data, cipher_xform->key.data,
2780a1173d55SHemant Agrawal 			cipher_xform->key.length);
2781a1173d55SHemant Agrawal 		session->dir = (cipher_xform->op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
2782a1173d55SHemant Agrawal 					DIR_ENC : DIR_DEC;
2783a1173d55SHemant Agrawal 		session->cipher_alg = cipher_xform->algo;
2784a1173d55SHemant Agrawal 	} else {
2785a1173d55SHemant Agrawal 		session->cipher_key.data = NULL;
2786a1173d55SHemant Agrawal 		session->cipher_key.length = 0;
2787a1173d55SHemant Agrawal 		session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
2788a1173d55SHemant Agrawal 		session->dir = DIR_ENC;
2789a1173d55SHemant Agrawal 	}
2790a1173d55SHemant Agrawal 
2791a1173d55SHemant Agrawal 	if (pdcp_xform->domain == RTE_SECURITY_PDCP_MODE_CONTROL) {
2792eac60082SVakul Garg 		if (pdcp_xform->sn_size != RTE_SECURITY_PDCP_SN_SIZE_5 &&
2793eac60082SVakul Garg 		    pdcp_xform->sn_size != RTE_SECURITY_PDCP_SN_SIZE_12) {
2794a1173d55SHemant Agrawal 			DPAA_SEC_ERR(
2795eac60082SVakul Garg 				"PDCP Seq Num size should be 5/12 bits for cmode");
2796a1173d55SHemant Agrawal 			goto out;
2797a1173d55SHemant Agrawal 		}
27982e4cbdb4SVakul Garg 	}
27992e4cbdb4SVakul Garg 
2800a1173d55SHemant Agrawal 	if (auth_xform) {
2801a1173d55SHemant Agrawal 		session->auth_key.data = rte_zmalloc(NULL,
2802a1173d55SHemant Agrawal 						     auth_xform->key.length,
2803a1173d55SHemant Agrawal 						     RTE_CACHE_LINE_SIZE);
28042e4cbdb4SVakul Garg 		if (!session->auth_key.data &&
2805a1173d55SHemant Agrawal 		    auth_xform->key.length > 0) {
2806a1173d55SHemant Agrawal 			DPAA_SEC_ERR("No Memory for auth key");
2807a1173d55SHemant Agrawal 			rte_free(session->cipher_key.data);
2808a1173d55SHemant Agrawal 			return -ENOMEM;
2809a1173d55SHemant Agrawal 		}
2810a1173d55SHemant Agrawal 		session->auth_key.length = auth_xform->key.length;
2811a1173d55SHemant Agrawal 		memcpy(session->auth_key.data, auth_xform->key.data,
2812a1173d55SHemant Agrawal 		       auth_xform->key.length);
2813a1173d55SHemant Agrawal 		session->auth_alg = auth_xform->algo;
2814a1173d55SHemant Agrawal 	} else {
2815a1173d55SHemant Agrawal 		session->auth_key.data = NULL;
2816a1173d55SHemant Agrawal 		session->auth_key.length = 0;
28172e4cbdb4SVakul Garg 		session->auth_alg = 0;
2818a1173d55SHemant Agrawal 	}
2819a1173d55SHemant Agrawal 	session->pdcp.domain = pdcp_xform->domain;
2820a1173d55SHemant Agrawal 	session->pdcp.bearer = pdcp_xform->bearer;
2821a1173d55SHemant Agrawal 	session->pdcp.pkt_dir = pdcp_xform->pkt_dir;
2822a1173d55SHemant Agrawal 	session->pdcp.sn_size = pdcp_xform->sn_size;
2823a1173d55SHemant Agrawal 	session->pdcp.hfn = pdcp_xform->hfn;
2824a1173d55SHemant Agrawal 	session->pdcp.hfn_threshold = pdcp_xform->hfn_threshold;
28256a0c9d36SAkhil Goyal 	session->pdcp.hfn_ovd = pdcp_xform->hfn_ovrd;
28266a0c9d36SAkhil Goyal 	session->pdcp.hfn_ovd_offset = cipher_xform->iv.offset;
2827a1173d55SHemant Agrawal 
2828a1173d55SHemant Agrawal 	rte_spinlock_lock(&dev_priv->lock);
28294e694fe5SAkhil Goyal 	for (i = 0; i < MAX_DPAA_CORES; i++) {
28304e694fe5SAkhil Goyal 		session->inq[i] = dpaa_sec_attach_rxq(dev_priv);
28314e694fe5SAkhil Goyal 		if (session->inq[i] == NULL) {
2832a1173d55SHemant Agrawal 			DPAA_SEC_ERR("unable to attach sec queue");
28334e694fe5SAkhil Goyal 			rte_spinlock_unlock(&dev_priv->lock);
2834a1173d55SHemant Agrawal 			goto out;
2835a1173d55SHemant Agrawal 		}
28364e694fe5SAkhil Goyal 	}
28374e694fe5SAkhil Goyal 	rte_spinlock_unlock(&dev_priv->lock);
28381f14d500SAkhil Goyal 	return 0;
28391f14d500SAkhil Goyal out:
28401f14d500SAkhil Goyal 	rte_free(session->auth_key.data);
28411f14d500SAkhil Goyal 	rte_free(session->cipher_key.data);
28421f14d500SAkhil Goyal 	memset(session, 0, sizeof(dpaa_sec_session));
28431f14d500SAkhil Goyal 	return -1;
28441f14d500SAkhil Goyal }
28451f14d500SAkhil Goyal 
28461f14d500SAkhil Goyal static int
28471f14d500SAkhil Goyal dpaa_sec_security_session_create(void *dev,
28481f14d500SAkhil Goyal 				 struct rte_security_session_conf *conf,
28491f14d500SAkhil Goyal 				 struct rte_security_session *sess,
28501f14d500SAkhil Goyal 				 struct rte_mempool *mempool)
28511f14d500SAkhil Goyal {
28521f14d500SAkhil Goyal 	void *sess_private_data;
28531f14d500SAkhil Goyal 	struct rte_cryptodev *cdev = (struct rte_cryptodev *)dev;
28541f14d500SAkhil Goyal 	int ret;
28551f14d500SAkhil Goyal 
28561f14d500SAkhil Goyal 	if (rte_mempool_get(mempool, &sess_private_data)) {
2857f163231eSHemant Agrawal 		DPAA_SEC_ERR("Couldn't get object from session mempool");
28581f14d500SAkhil Goyal 		return -ENOMEM;
28591f14d500SAkhil Goyal 	}
28601f14d500SAkhil Goyal 
28611f14d500SAkhil Goyal 	switch (conf->protocol) {
28621f14d500SAkhil Goyal 	case RTE_SECURITY_PROTOCOL_IPSEC:
28631f14d500SAkhil Goyal 		ret = dpaa_sec_set_ipsec_session(cdev, conf,
28641f14d500SAkhil Goyal 				sess_private_data);
28651f14d500SAkhil Goyal 		break;
2866a1173d55SHemant Agrawal 	case RTE_SECURITY_PROTOCOL_PDCP:
2867a1173d55SHemant Agrawal 		ret = dpaa_sec_set_pdcp_session(cdev, conf,
2868a1173d55SHemant Agrawal 				sess_private_data);
2869a1173d55SHemant Agrawal 		break;
28701f14d500SAkhil Goyal 	case RTE_SECURITY_PROTOCOL_MACSEC:
28711f14d500SAkhil Goyal 		return -ENOTSUP;
28721f14d500SAkhil Goyal 	default:
28731f14d500SAkhil Goyal 		return -EINVAL;
28741f14d500SAkhil Goyal 	}
28751f14d500SAkhil Goyal 	if (ret != 0) {
2876f163231eSHemant Agrawal 		DPAA_SEC_ERR("failed to configure session parameters");
28771f14d500SAkhil Goyal 		/* Return session to mempool */
28781f14d500SAkhil Goyal 		rte_mempool_put(mempool, sess_private_data);
28791f14d500SAkhil Goyal 		return ret;
28801f14d500SAkhil Goyal 	}
28811f14d500SAkhil Goyal 
28821f14d500SAkhil Goyal 	set_sec_session_private_data(sess, sess_private_data);
28831f14d500SAkhil Goyal 
28841f14d500SAkhil Goyal 	return ret;
28851f14d500SAkhil Goyal }
28861f14d500SAkhil Goyal 
28871f14d500SAkhil Goyal /** Clear the memory of session so it doesn't leave key material behind */
28881f14d500SAkhil Goyal static int
28891f14d500SAkhil Goyal dpaa_sec_security_session_destroy(void *dev __rte_unused,
28901f14d500SAkhil Goyal 		struct rte_security_session *sess)
28911f14d500SAkhil Goyal {
28921f14d500SAkhil Goyal 	PMD_INIT_FUNC_TRACE();
28931f14d500SAkhil Goyal 	void *sess_priv = get_sec_session_private_data(sess);
28941f14d500SAkhil Goyal 	dpaa_sec_session *s = (dpaa_sec_session *)sess_priv;
28951f14d500SAkhil Goyal 
28961f14d500SAkhil Goyal 	if (sess_priv) {
28973d0d5332SAkhil Goyal 		free_session_memory((struct rte_cryptodev *)dev, s);
28981f14d500SAkhil Goyal 		set_sec_session_private_data(sess, NULL);
28991f14d500SAkhil Goyal 	}
29001f14d500SAkhil Goyal 	return 0;
29011f14d500SAkhil Goyal }
29021f14d500SAkhil Goyal 
29031f14d500SAkhil Goyal static int
29042ffb940eSAkhil Goyal dpaa_sec_dev_configure(struct rte_cryptodev *dev __rte_unused,
2905c3e85bdcSAkhil Goyal 		       struct rte_cryptodev_config *config __rte_unused)
2906c3e85bdcSAkhil Goyal {
2907c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2908c3e85bdcSAkhil Goyal 
2909c3e85bdcSAkhil Goyal 	return 0;
2910c3e85bdcSAkhil Goyal }
2911c3e85bdcSAkhil Goyal 
2912c3e85bdcSAkhil Goyal static int
2913c3e85bdcSAkhil Goyal dpaa_sec_dev_start(struct rte_cryptodev *dev __rte_unused)
2914c3e85bdcSAkhil Goyal {
2915c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2916c3e85bdcSAkhil Goyal 	return 0;
2917c3e85bdcSAkhil Goyal }
2918c3e85bdcSAkhil Goyal 
2919c3e85bdcSAkhil Goyal static void
2920c3e85bdcSAkhil Goyal dpaa_sec_dev_stop(struct rte_cryptodev *dev __rte_unused)
2921c3e85bdcSAkhil Goyal {
2922c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2923c3e85bdcSAkhil Goyal }
2924c3e85bdcSAkhil Goyal 
2925c3e85bdcSAkhil Goyal static int
29267e3e2954SAkhil Goyal dpaa_sec_dev_close(struct rte_cryptodev *dev)
2927c3e85bdcSAkhil Goyal {
2928c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
29297e3e2954SAkhil Goyal 
29307e3e2954SAkhil Goyal 	if (dev == NULL)
29317e3e2954SAkhil Goyal 		return -ENOMEM;
29327e3e2954SAkhil Goyal 
2933c3e85bdcSAkhil Goyal 	return 0;
2934c3e85bdcSAkhil Goyal }
2935c3e85bdcSAkhil Goyal 
2936c3e85bdcSAkhil Goyal static void
2937c3e85bdcSAkhil Goyal dpaa_sec_dev_infos_get(struct rte_cryptodev *dev,
2938c3e85bdcSAkhil Goyal 		       struct rte_cryptodev_info *info)
2939c3e85bdcSAkhil Goyal {
2940c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals = dev->data->dev_private;
2941c3e85bdcSAkhil Goyal 
2942c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2943c3e85bdcSAkhil Goyal 	if (info != NULL) {
2944c3e85bdcSAkhil Goyal 		info->max_nb_queue_pairs = internals->max_nb_queue_pairs;
2945c3e85bdcSAkhil Goyal 		info->feature_flags = dev->feature_flags;
2946c3e85bdcSAkhil Goyal 		info->capabilities = dpaa_sec_capabilities;
2947c3e85bdcSAkhil Goyal 		info->sym.max_nb_sessions = internals->max_nb_sessions;
2948c3e85bdcSAkhil Goyal 		info->driver_id = cryptodev_driver_id;
2949c3e85bdcSAkhil Goyal 	}
2950c3e85bdcSAkhil Goyal }
2951c3e85bdcSAkhil Goyal 
2952fe3688baSAkhil Goyal static enum qman_cb_dqrr_result
2953fe3688baSAkhil Goyal dpaa_sec_process_parallel_event(void *event,
2954fe3688baSAkhil Goyal 			struct qman_portal *qm __always_unused,
2955fe3688baSAkhil Goyal 			struct qman_fq *outq,
2956fe3688baSAkhil Goyal 			const struct qm_dqrr_entry *dqrr,
2957fe3688baSAkhil Goyal 			void **bufs)
2958fe3688baSAkhil Goyal {
2959fe3688baSAkhil Goyal 	const struct qm_fd *fd;
2960fe3688baSAkhil Goyal 	struct dpaa_sec_job *job;
2961fe3688baSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
2962fe3688baSAkhil Goyal 	struct rte_event *ev = (struct rte_event *)event;
2963fe3688baSAkhil Goyal 
2964fe3688baSAkhil Goyal 	fd = &dqrr->fd;
2965fe3688baSAkhil Goyal 
2966fe3688baSAkhil Goyal 	/* sg is embedded in an op ctx,
2967fe3688baSAkhil Goyal 	 * sg[0] is for output
2968fe3688baSAkhil Goyal 	 * sg[1] for input
2969fe3688baSAkhil Goyal 	 */
2970fe3688baSAkhil Goyal 	job = dpaa_mem_ptov(qm_fd_addr_get64(fd));
2971fe3688baSAkhil Goyal 
2972fe3688baSAkhil Goyal 	ctx = container_of(job, struct dpaa_sec_op_ctx, job);
2973fe3688baSAkhil Goyal 	ctx->fd_status = fd->status;
2974fe3688baSAkhil Goyal 	if (ctx->op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
2975fe3688baSAkhil Goyal 		struct qm_sg_entry *sg_out;
2976fe3688baSAkhil Goyal 		uint32_t len;
2977fe3688baSAkhil Goyal 
2978fe3688baSAkhil Goyal 		sg_out = &job->sg[0];
2979fe3688baSAkhil Goyal 		hw_sg_to_cpu(sg_out);
2980fe3688baSAkhil Goyal 		len = sg_out->length;
2981fe3688baSAkhil Goyal 		ctx->op->sym->m_src->pkt_len = len;
2982fe3688baSAkhil Goyal 		ctx->op->sym->m_src->data_len = len;
2983fe3688baSAkhil Goyal 	}
2984fe3688baSAkhil Goyal 	if (!ctx->fd_status) {
2985fe3688baSAkhil Goyal 		ctx->op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
2986fe3688baSAkhil Goyal 	} else {
2987fe3688baSAkhil Goyal 		DPAA_SEC_DP_WARN("SEC return err: 0x%x", ctx->fd_status);
2988fe3688baSAkhil Goyal 		ctx->op->status = RTE_CRYPTO_OP_STATUS_ERROR;
2989fe3688baSAkhil Goyal 	}
2990fe3688baSAkhil Goyal 	ev->event_ptr = (void *)ctx->op;
2991fe3688baSAkhil Goyal 
2992fe3688baSAkhil Goyal 	ev->flow_id = outq->ev.flow_id;
2993fe3688baSAkhil Goyal 	ev->sub_event_type = outq->ev.sub_event_type;
2994fe3688baSAkhil Goyal 	ev->event_type = RTE_EVENT_TYPE_CRYPTODEV;
2995fe3688baSAkhil Goyal 	ev->op = RTE_EVENT_OP_NEW;
2996fe3688baSAkhil Goyal 	ev->sched_type = outq->ev.sched_type;
2997fe3688baSAkhil Goyal 	ev->queue_id = outq->ev.queue_id;
2998fe3688baSAkhil Goyal 	ev->priority = outq->ev.priority;
2999fe3688baSAkhil Goyal 	*bufs = (void *)ctx->op;
3000fe3688baSAkhil Goyal 
3001fe3688baSAkhil Goyal 	rte_mempool_put(ctx->ctx_pool, (void *)ctx);
3002fe3688baSAkhil Goyal 
3003fe3688baSAkhil Goyal 	return qman_cb_dqrr_consume;
3004fe3688baSAkhil Goyal }
3005fe3688baSAkhil Goyal 
3006fe3688baSAkhil Goyal static enum qman_cb_dqrr_result
3007fe3688baSAkhil Goyal dpaa_sec_process_atomic_event(void *event,
3008fe3688baSAkhil Goyal 			struct qman_portal *qm __rte_unused,
3009fe3688baSAkhil Goyal 			struct qman_fq *outq,
3010fe3688baSAkhil Goyal 			const struct qm_dqrr_entry *dqrr,
3011fe3688baSAkhil Goyal 			void **bufs)
3012fe3688baSAkhil Goyal {
3013fe3688baSAkhil Goyal 	u8 index;
3014fe3688baSAkhil Goyal 	const struct qm_fd *fd;
3015fe3688baSAkhil Goyal 	struct dpaa_sec_job *job;
3016fe3688baSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
3017fe3688baSAkhil Goyal 	struct rte_event *ev = (struct rte_event *)event;
3018fe3688baSAkhil Goyal 
3019fe3688baSAkhil Goyal 	fd = &dqrr->fd;
3020fe3688baSAkhil Goyal 
3021fe3688baSAkhil Goyal 	/* sg is embedded in an op ctx,
3022fe3688baSAkhil Goyal 	 * sg[0] is for output
3023fe3688baSAkhil Goyal 	 * sg[1] for input
3024fe3688baSAkhil Goyal 	 */
3025fe3688baSAkhil Goyal 	job = dpaa_mem_ptov(qm_fd_addr_get64(fd));
3026fe3688baSAkhil Goyal 
3027fe3688baSAkhil Goyal 	ctx = container_of(job, struct dpaa_sec_op_ctx, job);
3028fe3688baSAkhil Goyal 	ctx->fd_status = fd->status;
3029fe3688baSAkhil Goyal 	if (ctx->op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
3030fe3688baSAkhil Goyal 		struct qm_sg_entry *sg_out;
3031fe3688baSAkhil Goyal 		uint32_t len;
3032fe3688baSAkhil Goyal 
3033fe3688baSAkhil Goyal 		sg_out = &job->sg[0];
3034fe3688baSAkhil Goyal 		hw_sg_to_cpu(sg_out);
3035fe3688baSAkhil Goyal 		len = sg_out->length;
3036fe3688baSAkhil Goyal 		ctx->op->sym->m_src->pkt_len = len;
3037fe3688baSAkhil Goyal 		ctx->op->sym->m_src->data_len = len;
3038fe3688baSAkhil Goyal 	}
3039fe3688baSAkhil Goyal 	if (!ctx->fd_status) {
3040fe3688baSAkhil Goyal 		ctx->op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
3041fe3688baSAkhil Goyal 	} else {
3042fe3688baSAkhil Goyal 		DPAA_SEC_DP_WARN("SEC return err: 0x%x", ctx->fd_status);
3043fe3688baSAkhil Goyal 		ctx->op->status = RTE_CRYPTO_OP_STATUS_ERROR;
3044fe3688baSAkhil Goyal 	}
3045fe3688baSAkhil Goyal 	ev->event_ptr = (void *)ctx->op;
3046fe3688baSAkhil Goyal 	ev->flow_id = outq->ev.flow_id;
3047fe3688baSAkhil Goyal 	ev->sub_event_type = outq->ev.sub_event_type;
3048fe3688baSAkhil Goyal 	ev->event_type = RTE_EVENT_TYPE_CRYPTODEV;
3049fe3688baSAkhil Goyal 	ev->op = RTE_EVENT_OP_NEW;
3050fe3688baSAkhil Goyal 	ev->sched_type = outq->ev.sched_type;
3051fe3688baSAkhil Goyal 	ev->queue_id = outq->ev.queue_id;
3052fe3688baSAkhil Goyal 	ev->priority = outq->ev.priority;
3053fe3688baSAkhil Goyal 
3054fe3688baSAkhil Goyal 	/* Save active dqrr entries */
3055fe3688baSAkhil Goyal 	index = ((uintptr_t)dqrr >> 6) & (16/*QM_DQRR_SIZE*/ - 1);
3056fe3688baSAkhil Goyal 	DPAA_PER_LCORE_DQRR_SIZE++;
3057fe3688baSAkhil Goyal 	DPAA_PER_LCORE_DQRR_HELD |= 1 << index;
3058fe3688baSAkhil Goyal 	DPAA_PER_LCORE_DQRR_MBUF(index) = ctx->op->sym->m_src;
3059fe3688baSAkhil Goyal 	ev->impl_opaque = index + 1;
3060fe3688baSAkhil Goyal 	ctx->op->sym->m_src->seqn = (uint32_t)index + 1;
3061fe3688baSAkhil Goyal 	*bufs = (void *)ctx->op;
3062fe3688baSAkhil Goyal 
3063fe3688baSAkhil Goyal 	rte_mempool_put(ctx->ctx_pool, (void *)ctx);
3064fe3688baSAkhil Goyal 
3065fe3688baSAkhil Goyal 	return qman_cb_dqrr_defer;
3066fe3688baSAkhil Goyal }
3067fe3688baSAkhil Goyal 
3068fe3688baSAkhil Goyal int
3069fe3688baSAkhil Goyal dpaa_sec_eventq_attach(const struct rte_cryptodev *dev,
3070fe3688baSAkhil Goyal 		int qp_id,
3071fe3688baSAkhil Goyal 		uint16_t ch_id,
3072fe3688baSAkhil Goyal 		const struct rte_event *event)
3073fe3688baSAkhil Goyal {
3074fe3688baSAkhil Goyal 	struct dpaa_sec_qp *qp = dev->data->queue_pairs[qp_id];
3075fe3688baSAkhil Goyal 	struct qm_mcc_initfq opts = {0};
3076fe3688baSAkhil Goyal 
3077fe3688baSAkhil Goyal 	int ret;
3078fe3688baSAkhil Goyal 
3079fe3688baSAkhil Goyal 	opts.we_mask = QM_INITFQ_WE_DESTWQ | QM_INITFQ_WE_FQCTRL |
3080fe3688baSAkhil Goyal 		       QM_INITFQ_WE_CONTEXTA | QM_INITFQ_WE_CONTEXTB;
3081fe3688baSAkhil Goyal 	opts.fqd.dest.channel = ch_id;
3082fe3688baSAkhil Goyal 
3083fe3688baSAkhil Goyal 	switch (event->sched_type) {
3084fe3688baSAkhil Goyal 	case RTE_SCHED_TYPE_ATOMIC:
3085fe3688baSAkhil Goyal 		opts.fqd.fq_ctrl |= QM_FQCTRL_HOLDACTIVE;
3086fe3688baSAkhil Goyal 		/* Reset FQCTRL_AVOIDBLOCK bit as it is unnecessary
3087fe3688baSAkhil Goyal 		 * configuration with HOLD_ACTIVE setting
3088fe3688baSAkhil Goyal 		 */
3089fe3688baSAkhil Goyal 		opts.fqd.fq_ctrl &= (~QM_FQCTRL_AVOIDBLOCK);
3090fe3688baSAkhil Goyal 		qp->outq.cb.dqrr_dpdk_cb = dpaa_sec_process_atomic_event;
3091fe3688baSAkhil Goyal 		break;
3092fe3688baSAkhil Goyal 	case RTE_SCHED_TYPE_ORDERED:
3093fe3688baSAkhil Goyal 		DPAA_SEC_ERR("Ordered queue schedule type is not supported\n");
3094fe3688baSAkhil Goyal 		return -1;
3095fe3688baSAkhil Goyal 	default:
3096fe3688baSAkhil Goyal 		opts.fqd.fq_ctrl |= QM_FQCTRL_AVOIDBLOCK;
3097fe3688baSAkhil Goyal 		qp->outq.cb.dqrr_dpdk_cb = dpaa_sec_process_parallel_event;
3098fe3688baSAkhil Goyal 		break;
3099fe3688baSAkhil Goyal 	}
3100fe3688baSAkhil Goyal 
3101fe3688baSAkhil Goyal 	ret = qman_init_fq(&qp->outq, QMAN_INITFQ_FLAG_SCHED, &opts);
3102fe3688baSAkhil Goyal 	if (unlikely(ret)) {
3103fe3688baSAkhil Goyal 		DPAA_SEC_ERR("unable to init caam source fq!");
3104fe3688baSAkhil Goyal 		return ret;
3105fe3688baSAkhil Goyal 	}
3106fe3688baSAkhil Goyal 
3107fe3688baSAkhil Goyal 	memcpy(&qp->outq.ev, event, sizeof(struct rte_event));
3108fe3688baSAkhil Goyal 
3109fe3688baSAkhil Goyal 	return 0;
3110fe3688baSAkhil Goyal }
3111fe3688baSAkhil Goyal 
3112fe3688baSAkhil Goyal int
3113fe3688baSAkhil Goyal dpaa_sec_eventq_detach(const struct rte_cryptodev *dev,
3114fe3688baSAkhil Goyal 			int qp_id)
3115fe3688baSAkhil Goyal {
3116fe3688baSAkhil Goyal 	struct qm_mcc_initfq opts = {0};
3117fe3688baSAkhil Goyal 	int ret;
3118fe3688baSAkhil Goyal 	struct dpaa_sec_qp *qp = dev->data->queue_pairs[qp_id];
3119fe3688baSAkhil Goyal 
3120fe3688baSAkhil Goyal 	opts.we_mask = QM_INITFQ_WE_DESTWQ | QM_INITFQ_WE_FQCTRL |
3121fe3688baSAkhil Goyal 		       QM_INITFQ_WE_CONTEXTA | QM_INITFQ_WE_CONTEXTB;
3122fe3688baSAkhil Goyal 	qp->outq.cb.dqrr = dqrr_out_fq_cb_rx;
3123fe3688baSAkhil Goyal 	qp->outq.cb.ern  = ern_sec_fq_handler;
3124fe3688baSAkhil Goyal 	qman_retire_fq(&qp->outq, NULL);
3125fe3688baSAkhil Goyal 	qman_oos_fq(&qp->outq);
3126fe3688baSAkhil Goyal 	ret = qman_init_fq(&qp->outq, 0, &opts);
3127fe3688baSAkhil Goyal 	if (ret)
3128fe3688baSAkhil Goyal 		RTE_LOG(ERR, PMD, "Error in qman_init_fq: ret: %d\n", ret);
3129fe3688baSAkhil Goyal 	qp->outq.cb.dqrr = NULL;
3130fe3688baSAkhil Goyal 
3131fe3688baSAkhil Goyal 	return ret;
3132fe3688baSAkhil Goyal }
3133fe3688baSAkhil Goyal 
3134c3e85bdcSAkhil Goyal static struct rte_cryptodev_ops crypto_ops = {
3135c3e85bdcSAkhil Goyal 	.dev_configure	      = dpaa_sec_dev_configure,
3136c3e85bdcSAkhil Goyal 	.dev_start	      = dpaa_sec_dev_start,
3137c3e85bdcSAkhil Goyal 	.dev_stop	      = dpaa_sec_dev_stop,
3138c3e85bdcSAkhil Goyal 	.dev_close	      = dpaa_sec_dev_close,
3139c3e85bdcSAkhil Goyal 	.dev_infos_get        = dpaa_sec_dev_infos_get,
3140c3e85bdcSAkhil Goyal 	.queue_pair_setup     = dpaa_sec_queue_pair_setup,
3141c3e85bdcSAkhil Goyal 	.queue_pair_release   = dpaa_sec_queue_pair_release,
3142c3e85bdcSAkhil Goyal 	.queue_pair_count     = dpaa_sec_queue_pair_count,
3143012c5076SPablo de Lara 	.sym_session_get_size     = dpaa_sec_sym_session_get_size,
3144012c5076SPablo de Lara 	.sym_session_configure    = dpaa_sec_sym_session_configure,
3145012c5076SPablo de Lara 	.sym_session_clear        = dpaa_sec_sym_session_clear
3146c3e85bdcSAkhil Goyal };
3147c3e85bdcSAkhil Goyal 
31481f14d500SAkhil Goyal static const struct rte_security_capability *
31491f14d500SAkhil Goyal dpaa_sec_capabilities_get(void *device __rte_unused)
31501f14d500SAkhil Goyal {
31511f14d500SAkhil Goyal 	return dpaa_sec_security_cap;
31521f14d500SAkhil Goyal }
31531f14d500SAkhil Goyal 
3154b74fd6b8SFerruh Yigit static const struct rte_security_ops dpaa_sec_security_ops = {
31551f14d500SAkhil Goyal 	.session_create = dpaa_sec_security_session_create,
31561f14d500SAkhil Goyal 	.session_update = NULL,
31571f14d500SAkhil Goyal 	.session_stats_get = NULL,
31581f14d500SAkhil Goyal 	.session_destroy = dpaa_sec_security_session_destroy,
31591f14d500SAkhil Goyal 	.set_pkt_metadata = NULL,
31601f14d500SAkhil Goyal 	.capabilities_get = dpaa_sec_capabilities_get
31611f14d500SAkhil Goyal };
31621f14d500SAkhil Goyal 
3163c3e85bdcSAkhil Goyal static int
3164c3e85bdcSAkhil Goyal dpaa_sec_uninit(struct rte_cryptodev *dev)
3165c3e85bdcSAkhil Goyal {
3166debef417SShreyansh Jain 	struct dpaa_sec_dev_private *internals;
3167c3e85bdcSAkhil Goyal 
3168c3e85bdcSAkhil Goyal 	if (dev == NULL)
3169c3e85bdcSAkhil Goyal 		return -ENODEV;
3170c3e85bdcSAkhil Goyal 
3171debef417SShreyansh Jain 	internals = dev->data->dev_private;
31721f14d500SAkhil Goyal 	rte_free(dev->security_ctx);
31731f14d500SAkhil Goyal 
3174c3e85bdcSAkhil Goyal 	rte_free(internals);
3175c3e85bdcSAkhil Goyal 
3176f163231eSHemant Agrawal 	DPAA_SEC_INFO("Closing DPAA_SEC device %s on numa socket %u",
3177c3e85bdcSAkhil Goyal 		      dev->data->name, rte_socket_id());
3178c3e85bdcSAkhil Goyal 
3179c3e85bdcSAkhil Goyal 	return 0;
3180c3e85bdcSAkhil Goyal }
3181c3e85bdcSAkhil Goyal 
3182c3e85bdcSAkhil Goyal static int
3183c3e85bdcSAkhil Goyal dpaa_sec_dev_init(struct rte_cryptodev *cryptodev)
3184c3e85bdcSAkhil Goyal {
3185c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals;
31861f14d500SAkhil Goyal 	struct rte_security_ctx *security_instance;
3187c3e85bdcSAkhil Goyal 	struct dpaa_sec_qp *qp;
3188e79416d1SHemant Agrawal 	uint32_t i, flags;
3189c3e85bdcSAkhil Goyal 	int ret;
3190c3e85bdcSAkhil Goyal 
3191c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
3192c3e85bdcSAkhil Goyal 
3193c3e85bdcSAkhil Goyal 	cryptodev->driver_id = cryptodev_driver_id;
3194c3e85bdcSAkhil Goyal 	cryptodev->dev_ops = &crypto_ops;
3195c3e85bdcSAkhil Goyal 
3196c3e85bdcSAkhil Goyal 	cryptodev->enqueue_burst = dpaa_sec_enqueue_burst;
3197c3e85bdcSAkhil Goyal 	cryptodev->dequeue_burst = dpaa_sec_dequeue_burst;
3198c3e85bdcSAkhil Goyal 	cryptodev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
3199c3e85bdcSAkhil Goyal 			RTE_CRYPTODEV_FF_HW_ACCELERATED |
32001f14d500SAkhil Goyal 			RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
3201a74af788SAkhil Goyal 			RTE_CRYPTODEV_FF_SECURITY |
32022717246eSPablo de Lara 			RTE_CRYPTODEV_FF_IN_PLACE_SGL |
32032717246eSPablo de Lara 			RTE_CRYPTODEV_FF_OOP_SGL_IN_SGL_OUT |
32042717246eSPablo de Lara 			RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT |
32052717246eSPablo de Lara 			RTE_CRYPTODEV_FF_OOP_LB_IN_SGL_OUT |
32062717246eSPablo de Lara 			RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT;
3207c3e85bdcSAkhil Goyal 
3208c3e85bdcSAkhil Goyal 	internals = cryptodev->data->dev_private;
3209e79416d1SHemant Agrawal 	internals->max_nb_queue_pairs = RTE_DPAA_MAX_NB_SEC_QPS;
3210c3e85bdcSAkhil Goyal 	internals->max_nb_sessions = RTE_DPAA_SEC_PMD_MAX_NB_SESSIONS;
3211c3e85bdcSAkhil Goyal 
32121f14d500SAkhil Goyal 	/*
32131f14d500SAkhil Goyal 	 * For secondary processes, we don't initialise any further as primary
32141f14d500SAkhil Goyal 	 * has already done this work. Only check we don't need a different
32151f14d500SAkhil Goyal 	 * RX function
32161f14d500SAkhil Goyal 	 */
32171f14d500SAkhil Goyal 	if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
3218f163231eSHemant Agrawal 		DPAA_SEC_WARN("Device already init by primary process");
32191f14d500SAkhil Goyal 		return 0;
32201f14d500SAkhil Goyal 	}
32211f14d500SAkhil Goyal 
32221f14d500SAkhil Goyal 	/* Initialize security_ctx only for primary process*/
32231f14d500SAkhil Goyal 	security_instance = rte_malloc("rte_security_instances_ops",
32241f14d500SAkhil Goyal 				sizeof(struct rte_security_ctx), 0);
32251f14d500SAkhil Goyal 	if (security_instance == NULL)
32261f14d500SAkhil Goyal 		return -ENOMEM;
32271f14d500SAkhil Goyal 	security_instance->device = (void *)cryptodev;
32281f14d500SAkhil Goyal 	security_instance->ops = &dpaa_sec_security_ops;
32291f14d500SAkhil Goyal 	security_instance->sess_cnt = 0;
32301f14d500SAkhil Goyal 	cryptodev->security_ctx = security_instance;
32311f14d500SAkhil Goyal 
32323b617ee7SAkhil Goyal 	rte_spinlock_init(&internals->lock);
3233c3e85bdcSAkhil Goyal 	for (i = 0; i < internals->max_nb_queue_pairs; i++) {
3234c3e85bdcSAkhil Goyal 		/* init qman fq for queue pair */
3235c3e85bdcSAkhil Goyal 		qp = &internals->qps[i];
3236c3e85bdcSAkhil Goyal 		ret = dpaa_sec_init_tx(&qp->outq);
3237c3e85bdcSAkhil Goyal 		if (ret) {
3238f163231eSHemant Agrawal 			DPAA_SEC_ERR("config tx of queue pair  %d", i);
3239c3e85bdcSAkhil Goyal 			goto init_error;
3240c3e85bdcSAkhil Goyal 		}
3241e79416d1SHemant Agrawal 	}
3242e79416d1SHemant Agrawal 
3243e79416d1SHemant Agrawal 	flags = QMAN_FQ_FLAG_LOCKED | QMAN_FQ_FLAG_DYNAMIC_FQID |
3244e79416d1SHemant Agrawal 		QMAN_FQ_FLAG_TO_DCPORTAL;
32454e694fe5SAkhil Goyal 	for (i = 0; i < MAX_DPAA_CORES * internals->max_nb_sessions; i++) {
3246e79416d1SHemant Agrawal 		/* create rx qman fq for sessions*/
3247e79416d1SHemant Agrawal 		ret = qman_create_fq(0, flags, &internals->inq[i]);
3248e79416d1SHemant Agrawal 		if (unlikely(ret != 0)) {
3249f163231eSHemant Agrawal 			DPAA_SEC_ERR("sec qman_create_fq failed");
3250c3e85bdcSAkhil Goyal 			goto init_error;
3251c3e85bdcSAkhil Goyal 		}
3252c3e85bdcSAkhil Goyal 	}
3253c3e85bdcSAkhil Goyal 
3254f163231eSHemant Agrawal 	RTE_LOG(INFO, PMD, "%s cryptodev init\n", cryptodev->data->name);
3255c3e85bdcSAkhil Goyal 	return 0;
3256c3e85bdcSAkhil Goyal 
3257c3e85bdcSAkhil Goyal init_error:
3258f163231eSHemant Agrawal 	DPAA_SEC_ERR("driver %s: create failed\n", cryptodev->data->name);
3259c3e85bdcSAkhil Goyal 
3260c3e85bdcSAkhil Goyal 	dpaa_sec_uninit(cryptodev);
3261c3e85bdcSAkhil Goyal 	return -EFAULT;
3262c3e85bdcSAkhil Goyal }
3263c3e85bdcSAkhil Goyal 
3264c3e85bdcSAkhil Goyal static int
3265eb5a9a76SShreyansh Jain cryptodev_dpaa_sec_probe(struct rte_dpaa_driver *dpaa_drv __rte_unused,
3266c3e85bdcSAkhil Goyal 				struct rte_dpaa_device *dpaa_dev)
3267c3e85bdcSAkhil Goyal {
3268c3e85bdcSAkhil Goyal 	struct rte_cryptodev *cryptodev;
3269c3e85bdcSAkhil Goyal 	char cryptodev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
3270c3e85bdcSAkhil Goyal 
3271c3e85bdcSAkhil Goyal 	int retval;
3272c3e85bdcSAkhil Goyal 
32730964a951SHemant Agrawal 	snprintf(cryptodev_name, sizeof(cryptodev_name), "%s", dpaa_dev->name);
3274c3e85bdcSAkhil Goyal 
3275c3e85bdcSAkhil Goyal 	cryptodev = rte_cryptodev_pmd_allocate(cryptodev_name, rte_socket_id());
3276c3e85bdcSAkhil Goyal 	if (cryptodev == NULL)
3277c3e85bdcSAkhil Goyal 		return -ENOMEM;
3278c3e85bdcSAkhil Goyal 
3279c3e85bdcSAkhil Goyal 	if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
3280c3e85bdcSAkhil Goyal 		cryptodev->data->dev_private = rte_zmalloc_socket(
3281c3e85bdcSAkhil Goyal 					"cryptodev private structure",
3282c3e85bdcSAkhil Goyal 					sizeof(struct dpaa_sec_dev_private),
3283c3e85bdcSAkhil Goyal 					RTE_CACHE_LINE_SIZE,
3284c3e85bdcSAkhil Goyal 					rte_socket_id());
3285c3e85bdcSAkhil Goyal 
3286c3e85bdcSAkhil Goyal 		if (cryptodev->data->dev_private == NULL)
3287c3e85bdcSAkhil Goyal 			rte_panic("Cannot allocate memzone for private "
3288c3e85bdcSAkhil Goyal 					"device data");
3289c3e85bdcSAkhil Goyal 	}
3290c3e85bdcSAkhil Goyal 
3291c3e85bdcSAkhil Goyal 	dpaa_dev->crypto_dev = cryptodev;
3292c3e85bdcSAkhil Goyal 	cryptodev->device = &dpaa_dev->device;
3293c3e85bdcSAkhil Goyal 
3294c3e85bdcSAkhil Goyal 	/* init user callbacks */
3295c3e85bdcSAkhil Goyal 	TAILQ_INIT(&(cryptodev->link_intr_cbs));
3296c3e85bdcSAkhil Goyal 
3297c3e85bdcSAkhil Goyal 	/* if sec device version is not configured */
3298c3e85bdcSAkhil Goyal 	if (!rta_get_sec_era()) {
3299c3e85bdcSAkhil Goyal 		const struct device_node *caam_node;
3300c3e85bdcSAkhil Goyal 
3301c3e85bdcSAkhil Goyal 		for_each_compatible_node(caam_node, NULL, "fsl,sec-v4.0") {
3302c3e85bdcSAkhil Goyal 			const uint32_t *prop = of_get_property(caam_node,
3303c3e85bdcSAkhil Goyal 					"fsl,sec-era",
3304c3e85bdcSAkhil Goyal 					NULL);
3305c3e85bdcSAkhil Goyal 			if (prop) {
3306c3e85bdcSAkhil Goyal 				rta_set_sec_era(
3307c3e85bdcSAkhil Goyal 					INTL_SEC_ERA(rte_cpu_to_be_32(*prop)));
3308c3e85bdcSAkhil Goyal 				break;
3309c3e85bdcSAkhil Goyal 			}
3310c3e85bdcSAkhil Goyal 		}
3311c3e85bdcSAkhil Goyal 	}
3312c3e85bdcSAkhil Goyal 
3313c3e85bdcSAkhil Goyal 	/* Invoke PMD device initialization function */
3314c3e85bdcSAkhil Goyal 	retval = dpaa_sec_dev_init(cryptodev);
3315c3e85bdcSAkhil Goyal 	if (retval == 0)
3316c3e85bdcSAkhil Goyal 		return 0;
3317c3e85bdcSAkhil Goyal 
3318c3e85bdcSAkhil Goyal 	/* In case of error, cleanup is done */
3319c3e85bdcSAkhil Goyal 	if (rte_eal_process_type() == RTE_PROC_PRIMARY)
3320c3e85bdcSAkhil Goyal 		rte_free(cryptodev->data->dev_private);
3321c3e85bdcSAkhil Goyal 
3322c3e85bdcSAkhil Goyal 	rte_cryptodev_pmd_release_device(cryptodev);
3323c3e85bdcSAkhil Goyal 
3324c3e85bdcSAkhil Goyal 	return -ENXIO;
3325c3e85bdcSAkhil Goyal }
3326c3e85bdcSAkhil Goyal 
3327c3e85bdcSAkhil Goyal static int
3328c3e85bdcSAkhil Goyal cryptodev_dpaa_sec_remove(struct rte_dpaa_device *dpaa_dev)
3329c3e85bdcSAkhil Goyal {
3330c3e85bdcSAkhil Goyal 	struct rte_cryptodev *cryptodev;
3331c3e85bdcSAkhil Goyal 	int ret;
3332c3e85bdcSAkhil Goyal 
3333c3e85bdcSAkhil Goyal 	cryptodev = dpaa_dev->crypto_dev;
3334c3e85bdcSAkhil Goyal 	if (cryptodev == NULL)
3335c3e85bdcSAkhil Goyal 		return -ENODEV;
3336c3e85bdcSAkhil Goyal 
3337c3e85bdcSAkhil Goyal 	ret = dpaa_sec_uninit(cryptodev);
3338c3e85bdcSAkhil Goyal 	if (ret)
3339c3e85bdcSAkhil Goyal 		return ret;
3340c3e85bdcSAkhil Goyal 
3341f2f020d2SDeclan Doherty 	return rte_cryptodev_pmd_destroy(cryptodev);
3342c3e85bdcSAkhil Goyal }
3343c3e85bdcSAkhil Goyal 
3344c3e85bdcSAkhil Goyal static struct rte_dpaa_driver rte_dpaa_sec_driver = {
3345c3e85bdcSAkhil Goyal 	.drv_type = FSL_DPAA_CRYPTO,
3346c3e85bdcSAkhil Goyal 	.driver = {
3347c3e85bdcSAkhil Goyal 		.name = "DPAA SEC PMD"
3348c3e85bdcSAkhil Goyal 	},
3349c3e85bdcSAkhil Goyal 	.probe = cryptodev_dpaa_sec_probe,
3350c3e85bdcSAkhil Goyal 	.remove = cryptodev_dpaa_sec_remove,
3351c3e85bdcSAkhil Goyal };
3352c3e85bdcSAkhil Goyal 
3353c3e85bdcSAkhil Goyal static struct cryptodev_driver dpaa_sec_crypto_drv;
3354c3e85bdcSAkhil Goyal 
3355c3e85bdcSAkhil Goyal RTE_PMD_REGISTER_DPAA(CRYPTODEV_NAME_DPAA_SEC_PMD, rte_dpaa_sec_driver);
3356f737f5ceSFiona Trahe RTE_PMD_REGISTER_CRYPTO_DRIVER(dpaa_sec_crypto_drv, rte_dpaa_sec_driver.driver,
3357c3e85bdcSAkhil Goyal 		cryptodev_driver_id);
3358f163231eSHemant Agrawal 
3359f8e99896SThomas Monjalon RTE_INIT(dpaa_sec_init_log)
3360f163231eSHemant Agrawal {
3361f163231eSHemant Agrawal 	dpaa_logtype_sec = rte_log_register("pmd.crypto.dpaa");
3362f163231eSHemant Agrawal 	if (dpaa_logtype_sec >= 0)
3363f163231eSHemant Agrawal 		rte_log_set_level(dpaa_logtype_sec, RTE_LOG_NOTICE);
3364f163231eSHemant Agrawal }
3365