xref: /dpdk/drivers/crypto/dpaa_sec/dpaa_sec.c (revision 7a4a6da47007a1ab1abeb83633d35db29e13cbef)
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)) {
633c3e85bdcSAkhil Goyal 		caam_cipher_alg(ses, &alginfo_c);
634c3e85bdcSAkhil Goyal 		if (alginfo_c.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
635f163231eSHemant Agrawal 			DPAA_SEC_ERR("not supported cipher alg");
636c3e85bdcSAkhil Goyal 			return -ENOTSUP;
637c3e85bdcSAkhil Goyal 		}
638c3e85bdcSAkhil Goyal 
6390e5607e4SHemant Agrawal 		alginfo_c.key = (size_t)ses->cipher_key.data;
640c3e85bdcSAkhil Goyal 		alginfo_c.keylen = ses->cipher_key.length;
641c3e85bdcSAkhil Goyal 		alginfo_c.key_enc_flags = 0;
642c3e85bdcSAkhil Goyal 		alginfo_c.key_type = RTA_DATA_IMM;
643c3e85bdcSAkhil Goyal 
644c3e85bdcSAkhil Goyal 		shared_desc_len = cnstr_shdsc_blkcipher(
645c3e85bdcSAkhil Goyal 						cdb->sh_desc, true,
6467449390bSAkhil Goyal 						swap, SHR_NEVER, &alginfo_c,
647c3e85bdcSAkhil Goyal 						NULL,
648c3e85bdcSAkhil Goyal 						ses->iv.length,
649c3e85bdcSAkhil Goyal 						ses->dir);
650c3e85bdcSAkhil Goyal 	} else if (is_auth_only(ses)) {
651c3e85bdcSAkhil Goyal 		caam_auth_alg(ses, &alginfo_a);
652c3e85bdcSAkhil Goyal 		if (alginfo_a.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
653f163231eSHemant Agrawal 			DPAA_SEC_ERR("not supported auth alg");
654c3e85bdcSAkhil Goyal 			return -ENOTSUP;
655c3e85bdcSAkhil Goyal 		}
656c3e85bdcSAkhil Goyal 
6570e5607e4SHemant Agrawal 		alginfo_a.key = (size_t)ses->auth_key.data;
658c3e85bdcSAkhil Goyal 		alginfo_a.keylen = ses->auth_key.length;
659c3e85bdcSAkhil Goyal 		alginfo_a.key_enc_flags = 0;
660c3e85bdcSAkhil Goyal 		alginfo_a.key_type = RTA_DATA_IMM;
661c3e85bdcSAkhil Goyal 
662c3e85bdcSAkhil Goyal 		shared_desc_len = cnstr_shdsc_hmac(cdb->sh_desc, true,
6637449390bSAkhil Goyal 						   swap, SHR_NEVER, &alginfo_a,
664c3e85bdcSAkhil Goyal 						   !ses->dir,
665c3e85bdcSAkhil Goyal 						   ses->digest_length);
666c3e85bdcSAkhil Goyal 	} else if (is_aead(ses)) {
667c3e85bdcSAkhil Goyal 		caam_aead_alg(ses, &alginfo);
668c3e85bdcSAkhil Goyal 		if (alginfo.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
669f163231eSHemant Agrawal 			DPAA_SEC_ERR("not supported aead alg");
670c3e85bdcSAkhil Goyal 			return -ENOTSUP;
671c3e85bdcSAkhil Goyal 		}
6720e5607e4SHemant Agrawal 		alginfo.key = (size_t)ses->aead_key.data;
673c3e85bdcSAkhil Goyal 		alginfo.keylen = ses->aead_key.length;
674c3e85bdcSAkhil Goyal 		alginfo.key_enc_flags = 0;
675c3e85bdcSAkhil Goyal 		alginfo.key_type = RTA_DATA_IMM;
676c3e85bdcSAkhil Goyal 
677c3e85bdcSAkhil Goyal 		if (ses->dir == DIR_ENC)
678c3e85bdcSAkhil Goyal 			shared_desc_len = cnstr_shdsc_gcm_encap(
6797449390bSAkhil Goyal 					cdb->sh_desc, true, swap, SHR_NEVER,
680c3e85bdcSAkhil Goyal 					&alginfo,
681c3e85bdcSAkhil Goyal 					ses->iv.length,
682c3e85bdcSAkhil Goyal 					ses->digest_length);
683c3e85bdcSAkhil Goyal 		else
684c3e85bdcSAkhil Goyal 			shared_desc_len = cnstr_shdsc_gcm_decap(
6857449390bSAkhil Goyal 					cdb->sh_desc, true, swap, SHR_NEVER,
686c3e85bdcSAkhil Goyal 					&alginfo,
687c3e85bdcSAkhil Goyal 					ses->iv.length,
688c3e85bdcSAkhil Goyal 					ses->digest_length);
689c3e85bdcSAkhil Goyal 	} else {
690c3e85bdcSAkhil Goyal 		caam_cipher_alg(ses, &alginfo_c);
691c3e85bdcSAkhil Goyal 		if (alginfo_c.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
692f163231eSHemant Agrawal 			DPAA_SEC_ERR("not supported cipher alg");
693c3e85bdcSAkhil Goyal 			return -ENOTSUP;
694c3e85bdcSAkhil Goyal 		}
695c3e85bdcSAkhil Goyal 
6960e5607e4SHemant Agrawal 		alginfo_c.key = (size_t)ses->cipher_key.data;
697c3e85bdcSAkhil Goyal 		alginfo_c.keylen = ses->cipher_key.length;
698c3e85bdcSAkhil Goyal 		alginfo_c.key_enc_flags = 0;
699c3e85bdcSAkhil Goyal 		alginfo_c.key_type = RTA_DATA_IMM;
700c3e85bdcSAkhil Goyal 
701c3e85bdcSAkhil Goyal 		caam_auth_alg(ses, &alginfo_a);
702c3e85bdcSAkhil Goyal 		if (alginfo_a.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
703f163231eSHemant Agrawal 			DPAA_SEC_ERR("not supported auth alg");
704c3e85bdcSAkhil Goyal 			return -ENOTSUP;
705c3e85bdcSAkhil Goyal 		}
706c3e85bdcSAkhil Goyal 
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;
711c3e85bdcSAkhil Goyal 
712c3e85bdcSAkhil Goyal 		cdb->sh_desc[0] = alginfo_c.keylen;
713c3e85bdcSAkhil Goyal 		cdb->sh_desc[1] = alginfo_a.keylen;
714c3e85bdcSAkhil Goyal 		err = rta_inline_query(IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN,
715c3e85bdcSAkhil Goyal 				       MIN_JOB_DESC_SIZE,
716c3e85bdcSAkhil Goyal 				       (unsigned int *)cdb->sh_desc,
717c3e85bdcSAkhil Goyal 				       &cdb->sh_desc[2], 2);
718c3e85bdcSAkhil Goyal 
719c3e85bdcSAkhil Goyal 		if (err < 0) {
720f163231eSHemant Agrawal 			DPAA_SEC_ERR("Crypto: Incorrect key lengths");
721c3e85bdcSAkhil Goyal 			return err;
722c3e85bdcSAkhil Goyal 		}
723c3e85bdcSAkhil Goyal 		if (cdb->sh_desc[2] & 1)
724c3e85bdcSAkhil Goyal 			alginfo_c.key_type = RTA_DATA_IMM;
725c3e85bdcSAkhil Goyal 		else {
7260e5607e4SHemant Agrawal 			alginfo_c.key = (size_t)dpaa_mem_vtop(
7270e5607e4SHemant Agrawal 						(void *)(size_t)alginfo_c.key);
728c3e85bdcSAkhil Goyal 			alginfo_c.key_type = RTA_DATA_PTR;
729c3e85bdcSAkhil Goyal 		}
730c3e85bdcSAkhil Goyal 		if (cdb->sh_desc[2] & (1<<1))
731c3e85bdcSAkhil Goyal 			alginfo_a.key_type = RTA_DATA_IMM;
732c3e85bdcSAkhil Goyal 		else {
7330e5607e4SHemant Agrawal 			alginfo_a.key = (size_t)dpaa_mem_vtop(
7340e5607e4SHemant Agrawal 						(void *)(size_t)alginfo_a.key);
735c3e85bdcSAkhil Goyal 			alginfo_a.key_type = RTA_DATA_PTR;
736c3e85bdcSAkhil Goyal 		}
737c3e85bdcSAkhil Goyal 		cdb->sh_desc[0] = 0;
738c3e85bdcSAkhil Goyal 		cdb->sh_desc[1] = 0;
739c3e85bdcSAkhil Goyal 		cdb->sh_desc[2] = 0;
7401f14d500SAkhil Goyal 		/* Auth_only_len is set as 0 here and it will be
7411f14d500SAkhil Goyal 		 * overwritten in fd for each packet.
742c3e85bdcSAkhil Goyal 		 */
743c3e85bdcSAkhil Goyal 		shared_desc_len = cnstr_shdsc_authenc(cdb->sh_desc,
7447449390bSAkhil Goyal 				true, swap, SHR_SERIAL, &alginfo_c, &alginfo_a,
745c3e85bdcSAkhil Goyal 				ses->iv.length, 0,
746c3e85bdcSAkhil Goyal 				ses->digest_length, ses->dir);
747c3e85bdcSAkhil Goyal 	}
74822788c2cSSunil Kumar Kori 
74922788c2cSSunil Kumar Kori 	if (shared_desc_len < 0) {
750f163231eSHemant Agrawal 		DPAA_SEC_ERR("error in preparing command block");
75122788c2cSSunil Kumar Kori 		return shared_desc_len;
75222788c2cSSunil Kumar Kori 	}
75322788c2cSSunil Kumar Kori 
754c3e85bdcSAkhil Goyal 	cdb->sh_hdr.hi.field.idlen = shared_desc_len;
755c3e85bdcSAkhil Goyal 	cdb->sh_hdr.hi.word = rte_cpu_to_be_32(cdb->sh_hdr.hi.word);
756c3e85bdcSAkhil Goyal 	cdb->sh_hdr.lo.word = rte_cpu_to_be_32(cdb->sh_hdr.lo.word);
757c3e85bdcSAkhil Goyal 
758c3e85bdcSAkhil Goyal 	return 0;
759c3e85bdcSAkhil Goyal }
760c3e85bdcSAkhil Goyal 
761c3e85bdcSAkhil Goyal /* qp is lockless, should be accessed by only one thread */
762c3e85bdcSAkhil Goyal static int
763c3e85bdcSAkhil Goyal dpaa_sec_deq(struct dpaa_sec_qp *qp, struct rte_crypto_op **ops, int nb_ops)
764c3e85bdcSAkhil Goyal {
765c3e85bdcSAkhil Goyal 	struct qman_fq *fq;
7669a984458SAkhil Goyal 	unsigned int pkts = 0;
767f40d5a53SNipun Gupta 	int num_rx_bufs, ret;
7689a984458SAkhil Goyal 	struct qm_dqrr_entry *dq;
769f40d5a53SNipun Gupta 	uint32_t vdqcr_flags = 0;
770c3e85bdcSAkhil Goyal 
771c3e85bdcSAkhil Goyal 	fq = &qp->outq;
772f40d5a53SNipun Gupta 	/*
773f40d5a53SNipun Gupta 	 * Until request for four buffers, we provide exact number of buffers.
774f40d5a53SNipun Gupta 	 * Otherwise we do not set the QM_VDQCR_EXACT flag.
775f40d5a53SNipun Gupta 	 * Not setting QM_VDQCR_EXACT flag can provide two more buffers than
776f40d5a53SNipun Gupta 	 * requested, so we request two less in this case.
777f40d5a53SNipun Gupta 	 */
778f40d5a53SNipun Gupta 	if (nb_ops < 4) {
779f40d5a53SNipun Gupta 		vdqcr_flags = QM_VDQCR_EXACT;
780f40d5a53SNipun Gupta 		num_rx_bufs = nb_ops;
781f40d5a53SNipun Gupta 	} else {
782f40d5a53SNipun Gupta 		num_rx_bufs = nb_ops > DPAA_MAX_DEQUEUE_NUM_FRAMES ?
783f40d5a53SNipun Gupta 			(DPAA_MAX_DEQUEUE_NUM_FRAMES - 2) : (nb_ops - 2);
784f40d5a53SNipun Gupta 	}
785f40d5a53SNipun Gupta 	ret = qman_set_vdq(fq, num_rx_bufs, vdqcr_flags);
7869a984458SAkhil Goyal 	if (ret)
7879a984458SAkhil Goyal 		return 0;
788c3e85bdcSAkhil Goyal 
7899a984458SAkhil Goyal 	do {
7909a984458SAkhil Goyal 		const struct qm_fd *fd;
7919a984458SAkhil Goyal 		struct dpaa_sec_job *job;
7929a984458SAkhil Goyal 		struct dpaa_sec_op_ctx *ctx;
7939a984458SAkhil Goyal 		struct rte_crypto_op *op;
794c3e85bdcSAkhil Goyal 
7959a984458SAkhil Goyal 		dq = qman_dequeue(fq);
7969a984458SAkhil Goyal 		if (!dq)
7979a984458SAkhil Goyal 			continue;
7989a984458SAkhil Goyal 
7999a984458SAkhil Goyal 		fd = &dq->fd;
8009a984458SAkhil Goyal 		/* sg is embedded in an op ctx,
8019a984458SAkhil Goyal 		 * sg[0] is for output
8029a984458SAkhil Goyal 		 * sg[1] for input
8039a984458SAkhil Goyal 		 */
8049a984458SAkhil Goyal 		job = dpaa_mem_ptov(qm_fd_addr_get64(fd));
8059a984458SAkhil Goyal 
8069a984458SAkhil Goyal 		ctx = container_of(job, struct dpaa_sec_op_ctx, job);
8079a984458SAkhil Goyal 		ctx->fd_status = fd->status;
8089a984458SAkhil Goyal 		op = ctx->op;
8099a984458SAkhil Goyal 		if (op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
8109a984458SAkhil Goyal 			struct qm_sg_entry *sg_out;
8119a984458SAkhil Goyal 			uint32_t len;
812fb5c100aSAkhil Goyal 			struct rte_mbuf *mbuf = (op->sym->m_dst == NULL) ?
813fb5c100aSAkhil Goyal 						op->sym->m_src : op->sym->m_dst;
8149a984458SAkhil Goyal 
8159a984458SAkhil Goyal 			sg_out = &job->sg[0];
8169a984458SAkhil Goyal 			hw_sg_to_cpu(sg_out);
8179a984458SAkhil Goyal 			len = sg_out->length;
818fb5c100aSAkhil Goyal 			mbuf->pkt_len = len;
819fb5c100aSAkhil Goyal 			while (mbuf->next != NULL) {
820fb5c100aSAkhil Goyal 				len -= mbuf->data_len;
821fb5c100aSAkhil Goyal 				mbuf = mbuf->next;
822fb5c100aSAkhil Goyal 			}
823fb5c100aSAkhil Goyal 			mbuf->data_len = len;
8249a984458SAkhil Goyal 		}
8259a984458SAkhil Goyal 		if (!ctx->fd_status) {
8269a984458SAkhil Goyal 			op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
8279a984458SAkhil Goyal 		} else {
828f163231eSHemant Agrawal 			DPAA_SEC_DP_WARN("SEC return err:0x%x", ctx->fd_status);
8299a984458SAkhil Goyal 			op->status = RTE_CRYPTO_OP_STATUS_ERROR;
8309a984458SAkhil Goyal 		}
8319a984458SAkhil Goyal 		ops[pkts++] = op;
8329a984458SAkhil Goyal 
8339a984458SAkhil Goyal 		/* report op status to sym->op and then free the ctx memeory */
8349a984458SAkhil Goyal 		rte_mempool_put(ctx->ctx_pool, (void *)ctx);
8359a984458SAkhil Goyal 
8369a984458SAkhil Goyal 		qman_dqrr_consume(fq, dq);
8379a984458SAkhil Goyal 	} while (fq->flags & QMAN_FQ_STATE_VDQCR);
8389a984458SAkhil Goyal 
8399a984458SAkhil Goyal 	return pkts;
840c3e85bdcSAkhil Goyal }
841c3e85bdcSAkhil Goyal 
842a74af788SAkhil Goyal static inline struct dpaa_sec_job *
843a74af788SAkhil Goyal build_auth_only_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
844a74af788SAkhil Goyal {
845a74af788SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
846a74af788SAkhil Goyal 	struct rte_mbuf *mbuf = sym->m_src;
847a74af788SAkhil Goyal 	struct dpaa_sec_job *cf;
848a74af788SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
849a74af788SAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
850a74af788SAkhil Goyal 	phys_addr_t start_addr;
851a74af788SAkhil Goyal 	uint8_t *old_digest, extra_segs;
852a74af788SAkhil Goyal 
853a74af788SAkhil Goyal 	if (is_decode(ses))
854a74af788SAkhil Goyal 		extra_segs = 3;
855a74af788SAkhil Goyal 	else
856a74af788SAkhil Goyal 		extra_segs = 2;
857a74af788SAkhil Goyal 
858f7a5752eSHemant Agrawal 	if (mbuf->nb_segs > MAX_SG_ENTRIES) {
859f163231eSHemant Agrawal 		DPAA_SEC_DP_ERR("Auth: Max sec segs supported is %d",
860a74af788SAkhil Goyal 				MAX_SG_ENTRIES);
861a74af788SAkhil Goyal 		return NULL;
862a74af788SAkhil Goyal 	}
863f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, mbuf->nb_segs + extra_segs);
864a74af788SAkhil Goyal 	if (!ctx)
865a74af788SAkhil Goyal 		return NULL;
866a74af788SAkhil Goyal 
867a74af788SAkhil Goyal 	cf = &ctx->job;
868a74af788SAkhil Goyal 	ctx->op = op;
869a74af788SAkhil Goyal 	old_digest = ctx->digest;
870a74af788SAkhil Goyal 
871a74af788SAkhil Goyal 	/* output */
872a74af788SAkhil Goyal 	out_sg = &cf->sg[0];
873a74af788SAkhil Goyal 	qm_sg_entry_set64(out_sg, sym->auth.digest.phys_addr);
874a74af788SAkhil Goyal 	out_sg->length = ses->digest_length;
875a74af788SAkhil Goyal 	cpu_to_hw_sg(out_sg);
876a74af788SAkhil Goyal 
877a74af788SAkhil Goyal 	/* input */
878a74af788SAkhil Goyal 	in_sg = &cf->sg[1];
879a74af788SAkhil Goyal 	/* need to extend the input to a compound frame */
880a74af788SAkhil Goyal 	in_sg->extension = 1;
881a74af788SAkhil Goyal 	in_sg->final = 1;
882a74af788SAkhil Goyal 	in_sg->length = sym->auth.data.length;
88395456e89SShreyansh Jain 	qm_sg_entry_set64(in_sg, dpaa_mem_vtop(&cf->sg[2]));
884a74af788SAkhil Goyal 
885a74af788SAkhil Goyal 	/* 1st seg */
886a74af788SAkhil Goyal 	sg = in_sg + 1;
887a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
888a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->auth.data.offset;
889a74af788SAkhil Goyal 	sg->offset = sym->auth.data.offset;
890a74af788SAkhil Goyal 
891a74af788SAkhil Goyal 	/* Successive segs */
892a74af788SAkhil Goyal 	mbuf = mbuf->next;
893a74af788SAkhil Goyal 	while (mbuf) {
894a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
895a74af788SAkhil Goyal 		sg++;
896a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
897a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
898a74af788SAkhil Goyal 		mbuf = mbuf->next;
899a74af788SAkhil Goyal 	}
900a74af788SAkhil Goyal 
901a74af788SAkhil Goyal 	if (is_decode(ses)) {
902a74af788SAkhil Goyal 		/* Digest verification case */
903a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
904a74af788SAkhil Goyal 		sg++;
905a74af788SAkhil Goyal 		rte_memcpy(old_digest, sym->auth.digest.data,
906a74af788SAkhil Goyal 				ses->digest_length);
90795456e89SShreyansh Jain 		start_addr = dpaa_mem_vtop(old_digest);
908a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, start_addr);
909a74af788SAkhil Goyal 		sg->length = ses->digest_length;
910a74af788SAkhil Goyal 		in_sg->length += ses->digest_length;
911a74af788SAkhil Goyal 	} else {
912a74af788SAkhil Goyal 		/* Digest calculation case */
913a74af788SAkhil Goyal 		sg->length -= ses->digest_length;
914a74af788SAkhil Goyal 	}
915a74af788SAkhil Goyal 	sg->final = 1;
916a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
917a74af788SAkhil Goyal 	cpu_to_hw_sg(in_sg);
918a74af788SAkhil Goyal 
919a74af788SAkhil Goyal 	return cf;
920a74af788SAkhil Goyal }
921a74af788SAkhil Goyal 
922c3e85bdcSAkhil Goyal /**
923c3e85bdcSAkhil Goyal  * packet looks like:
924c3e85bdcSAkhil Goyal  *		|<----data_len------->|
925c3e85bdcSAkhil Goyal  *    |ip_header|ah_header|icv|payload|
926c3e85bdcSAkhil Goyal  *              ^
927c3e85bdcSAkhil Goyal  *		|
928c3e85bdcSAkhil Goyal  *	   mbuf->pkt.data
929c3e85bdcSAkhil Goyal  */
930c3e85bdcSAkhil Goyal static inline struct dpaa_sec_job *
931c3e85bdcSAkhil Goyal build_auth_only(struct rte_crypto_op *op, dpaa_sec_session *ses)
932c3e85bdcSAkhil Goyal {
933c3e85bdcSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
934c3e85bdcSAkhil Goyal 	struct rte_mbuf *mbuf = sym->m_src;
935c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
936c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
937c3e85bdcSAkhil Goyal 	struct qm_sg_entry *sg;
938c4509373SSantosh Shukla 	rte_iova_t start_addr;
939c3e85bdcSAkhil Goyal 	uint8_t *old_digest;
940c3e85bdcSAkhil Goyal 
941f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, 4);
942c3e85bdcSAkhil Goyal 	if (!ctx)
943c3e85bdcSAkhil Goyal 		return NULL;
944c3e85bdcSAkhil Goyal 
945c3e85bdcSAkhil Goyal 	cf = &ctx->job;
946c3e85bdcSAkhil Goyal 	ctx->op = op;
947c3e85bdcSAkhil Goyal 	old_digest = ctx->digest;
948c3e85bdcSAkhil Goyal 
949bfa9a8a4SThomas Monjalon 	start_addr = rte_pktmbuf_iova(mbuf);
950c3e85bdcSAkhil Goyal 	/* output */
951c3e85bdcSAkhil Goyal 	sg = &cf->sg[0];
952c3e85bdcSAkhil Goyal 	qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
953c3e85bdcSAkhil Goyal 	sg->length = ses->digest_length;
954c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
955c3e85bdcSAkhil Goyal 
956c3e85bdcSAkhil Goyal 	/* input */
957c3e85bdcSAkhil Goyal 	sg = &cf->sg[1];
958c3e85bdcSAkhil Goyal 	if (is_decode(ses)) {
959c3e85bdcSAkhil Goyal 		/* need to extend the input to a compound frame */
960c3e85bdcSAkhil Goyal 		sg->extension = 1;
96195456e89SShreyansh Jain 		qm_sg_entry_set64(sg, dpaa_mem_vtop(&cf->sg[2]));
962c3e85bdcSAkhil Goyal 		sg->length = sym->auth.data.length + ses->digest_length;
963c3e85bdcSAkhil Goyal 		sg->final = 1;
964c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
965c3e85bdcSAkhil Goyal 
966c3e85bdcSAkhil Goyal 		sg = &cf->sg[2];
967c3e85bdcSAkhil Goyal 		/* hash result or digest, save digest first */
968c3e85bdcSAkhil Goyal 		rte_memcpy(old_digest, sym->auth.digest.data,
969c3e85bdcSAkhil Goyal 			   ses->digest_length);
970c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, start_addr + sym->auth.data.offset);
971c3e85bdcSAkhil Goyal 		sg->length = sym->auth.data.length;
972c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
973c3e85bdcSAkhil Goyal 
974c3e85bdcSAkhil Goyal 		/* let's check digest by hw */
97595456e89SShreyansh Jain 		start_addr = dpaa_mem_vtop(old_digest);
976c3e85bdcSAkhil Goyal 		sg++;
977c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, start_addr);
978c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
979c3e85bdcSAkhil Goyal 		sg->final = 1;
980c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
981c3e85bdcSAkhil Goyal 	} else {
982c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, start_addr + sym->auth.data.offset);
983c3e85bdcSAkhil Goyal 		sg->length = sym->auth.data.length;
984c3e85bdcSAkhil Goyal 		sg->final = 1;
985c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
986c3e85bdcSAkhil Goyal 	}
987c3e85bdcSAkhil Goyal 
988c3e85bdcSAkhil Goyal 	return cf;
989c3e85bdcSAkhil Goyal }
990c3e85bdcSAkhil Goyal 
991c3e85bdcSAkhil Goyal static inline struct dpaa_sec_job *
992a74af788SAkhil Goyal build_cipher_only_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
993a74af788SAkhil Goyal {
994a74af788SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
995a74af788SAkhil Goyal 	struct dpaa_sec_job *cf;
996a74af788SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
997a74af788SAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
998a74af788SAkhil Goyal 	struct rte_mbuf *mbuf;
999a74af788SAkhil Goyal 	uint8_t req_segs;
1000a74af788SAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1001a74af788SAkhil Goyal 			ses->iv.offset);
1002a74af788SAkhil Goyal 
1003a74af788SAkhil Goyal 	if (sym->m_dst) {
1004a74af788SAkhil Goyal 		mbuf = sym->m_dst;
1005a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 3;
1006a74af788SAkhil Goyal 	} else {
1007a74af788SAkhil Goyal 		mbuf = sym->m_src;
1008a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs * 2 + 3;
1009a74af788SAkhil Goyal 	}
1010a74af788SAkhil Goyal 
1011f7a5752eSHemant Agrawal 	if (mbuf->nb_segs > MAX_SG_ENTRIES) {
1012f163231eSHemant Agrawal 		DPAA_SEC_DP_ERR("Cipher: Max sec segs supported is %d",
1013a74af788SAkhil Goyal 				MAX_SG_ENTRIES);
1014a74af788SAkhil Goyal 		return NULL;
1015a74af788SAkhil Goyal 	}
1016a74af788SAkhil Goyal 
1017f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, req_segs);
1018a74af788SAkhil Goyal 	if (!ctx)
1019a74af788SAkhil Goyal 		return NULL;
1020a74af788SAkhil Goyal 
1021a74af788SAkhil Goyal 	cf = &ctx->job;
1022a74af788SAkhil Goyal 	ctx->op = op;
1023a74af788SAkhil Goyal 
1024a74af788SAkhil Goyal 	/* output */
1025a74af788SAkhil Goyal 	out_sg = &cf->sg[0];
1026a74af788SAkhil Goyal 	out_sg->extension = 1;
1027a74af788SAkhil Goyal 	out_sg->length = sym->cipher.data.length;
102895456e89SShreyansh Jain 	qm_sg_entry_set64(out_sg, dpaa_mem_vtop(&cf->sg[2]));
1029a74af788SAkhil Goyal 	cpu_to_hw_sg(out_sg);
1030a74af788SAkhil Goyal 
1031a74af788SAkhil Goyal 	/* 1st seg */
1032a74af788SAkhil Goyal 	sg = &cf->sg[2];
1033a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1034a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->cipher.data.offset;
1035a74af788SAkhil Goyal 	sg->offset = sym->cipher.data.offset;
1036a74af788SAkhil Goyal 
1037a74af788SAkhil Goyal 	/* Successive segs */
1038a74af788SAkhil Goyal 	mbuf = mbuf->next;
1039a74af788SAkhil Goyal 	while (mbuf) {
1040a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1041a74af788SAkhil Goyal 		sg++;
1042a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1043a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1044a74af788SAkhil Goyal 		mbuf = mbuf->next;
1045a74af788SAkhil Goyal 	}
1046a74af788SAkhil Goyal 	sg->final = 1;
1047a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1048a74af788SAkhil Goyal 
1049a74af788SAkhil Goyal 	/* input */
1050a74af788SAkhil Goyal 	mbuf = sym->m_src;
1051a74af788SAkhil Goyal 	in_sg = &cf->sg[1];
1052a74af788SAkhil Goyal 	in_sg->extension = 1;
1053a74af788SAkhil Goyal 	in_sg->final = 1;
1054a74af788SAkhil Goyal 	in_sg->length = sym->cipher.data.length + ses->iv.length;
1055a74af788SAkhil Goyal 
1056a74af788SAkhil Goyal 	sg++;
105795456e89SShreyansh Jain 	qm_sg_entry_set64(in_sg, dpaa_mem_vtop(sg));
1058a74af788SAkhil Goyal 	cpu_to_hw_sg(in_sg);
1059a74af788SAkhil Goyal 
1060a74af788SAkhil Goyal 	/* IV */
1061a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1062a74af788SAkhil Goyal 	sg->length = ses->iv.length;
1063a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1064a74af788SAkhil Goyal 
1065a74af788SAkhil Goyal 	/* 1st seg */
1066a74af788SAkhil Goyal 	sg++;
1067a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1068a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->cipher.data.offset;
1069a74af788SAkhil Goyal 	sg->offset = sym->cipher.data.offset;
1070a74af788SAkhil Goyal 
1071a74af788SAkhil Goyal 	/* Successive segs */
1072a74af788SAkhil Goyal 	mbuf = mbuf->next;
1073a74af788SAkhil Goyal 	while (mbuf) {
1074a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1075a74af788SAkhil Goyal 		sg++;
1076a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1077a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1078a74af788SAkhil Goyal 		mbuf = mbuf->next;
1079a74af788SAkhil Goyal 	}
1080a74af788SAkhil Goyal 	sg->final = 1;
1081a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1082a74af788SAkhil Goyal 
1083a74af788SAkhil Goyal 	return cf;
1084a74af788SAkhil Goyal }
1085a74af788SAkhil Goyal 
1086a74af788SAkhil Goyal static inline struct dpaa_sec_job *
1087c3e85bdcSAkhil Goyal build_cipher_only(struct rte_crypto_op *op, dpaa_sec_session *ses)
1088c3e85bdcSAkhil Goyal {
1089c3e85bdcSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1090c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
1091c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1092c3e85bdcSAkhil Goyal 	struct qm_sg_entry *sg;
1093c4509373SSantosh Shukla 	rte_iova_t src_start_addr, dst_start_addr;
1094c3e85bdcSAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1095c3e85bdcSAkhil Goyal 			ses->iv.offset);
1096c3e85bdcSAkhil Goyal 
1097f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, 4);
1098c3e85bdcSAkhil Goyal 	if (!ctx)
1099c3e85bdcSAkhil Goyal 		return NULL;
1100c3e85bdcSAkhil Goyal 
1101c3e85bdcSAkhil Goyal 	cf = &ctx->job;
1102c3e85bdcSAkhil Goyal 	ctx->op = op;
1103a389434eSAlok Makhariya 
1104bfa9a8a4SThomas Monjalon 	src_start_addr = rte_pktmbuf_iova(sym->m_src);
1105a389434eSAlok Makhariya 
1106a389434eSAlok Makhariya 	if (sym->m_dst)
1107bfa9a8a4SThomas Monjalon 		dst_start_addr = rte_pktmbuf_iova(sym->m_dst);
1108a389434eSAlok Makhariya 	else
1109a389434eSAlok Makhariya 		dst_start_addr = src_start_addr;
1110c3e85bdcSAkhil Goyal 
1111c3e85bdcSAkhil Goyal 	/* output */
1112c3e85bdcSAkhil Goyal 	sg = &cf->sg[0];
1113a389434eSAlok Makhariya 	qm_sg_entry_set64(sg, dst_start_addr + sym->cipher.data.offset);
1114c3e85bdcSAkhil Goyal 	sg->length = sym->cipher.data.length + ses->iv.length;
1115c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1116c3e85bdcSAkhil Goyal 
1117c3e85bdcSAkhil Goyal 	/* input */
1118c3e85bdcSAkhil Goyal 	sg = &cf->sg[1];
1119c3e85bdcSAkhil Goyal 
1120c3e85bdcSAkhil Goyal 	/* need to extend the input to a compound frame */
1121c3e85bdcSAkhil Goyal 	sg->extension = 1;
1122c3e85bdcSAkhil Goyal 	sg->final = 1;
1123c3e85bdcSAkhil Goyal 	sg->length = sym->cipher.data.length + ses->iv.length;
112495456e89SShreyansh Jain 	qm_sg_entry_set64(sg, dpaa_mem_vtop(&cf->sg[2]));
1125c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1126c3e85bdcSAkhil Goyal 
1127c3e85bdcSAkhil Goyal 	sg = &cf->sg[2];
1128c3e85bdcSAkhil Goyal 	qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1129c3e85bdcSAkhil Goyal 	sg->length = ses->iv.length;
1130c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1131c3e85bdcSAkhil Goyal 
1132c3e85bdcSAkhil Goyal 	sg++;
1133a389434eSAlok Makhariya 	qm_sg_entry_set64(sg, src_start_addr + sym->cipher.data.offset);
1134c3e85bdcSAkhil Goyal 	sg->length = sym->cipher.data.length;
1135c3e85bdcSAkhil Goyal 	sg->final = 1;
1136c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1137c3e85bdcSAkhil Goyal 
1138c3e85bdcSAkhil Goyal 	return cf;
1139c3e85bdcSAkhil Goyal }
1140c3e85bdcSAkhil Goyal 
1141c3e85bdcSAkhil Goyal static inline struct dpaa_sec_job *
1142a74af788SAkhil Goyal build_cipher_auth_gcm_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
1143a74af788SAkhil Goyal {
1144a74af788SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1145a74af788SAkhil Goyal 	struct dpaa_sec_job *cf;
1146a74af788SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1147a74af788SAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
1148a74af788SAkhil Goyal 	struct rte_mbuf *mbuf;
1149a74af788SAkhil Goyal 	uint8_t req_segs;
1150a74af788SAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1151a74af788SAkhil Goyal 			ses->iv.offset);
1152a74af788SAkhil Goyal 
1153a74af788SAkhil Goyal 	if (sym->m_dst) {
1154a74af788SAkhil Goyal 		mbuf = sym->m_dst;
1155a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 4;
1156a74af788SAkhil Goyal 	} else {
1157a74af788SAkhil Goyal 		mbuf = sym->m_src;
1158a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs * 2 + 4;
1159a74af788SAkhil Goyal 	}
1160a74af788SAkhil Goyal 
1161a74af788SAkhil Goyal 	if (ses->auth_only_len)
1162a74af788SAkhil Goyal 		req_segs++;
1163a74af788SAkhil Goyal 
1164f7a5752eSHemant Agrawal 	if (mbuf->nb_segs > MAX_SG_ENTRIES) {
1165f163231eSHemant Agrawal 		DPAA_SEC_DP_ERR("AEAD: Max sec segs supported is %d",
1166a74af788SAkhil Goyal 				MAX_SG_ENTRIES);
1167a74af788SAkhil Goyal 		return NULL;
1168a74af788SAkhil Goyal 	}
1169a74af788SAkhil Goyal 
1170f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, req_segs);
1171a74af788SAkhil Goyal 	if (!ctx)
1172a74af788SAkhil Goyal 		return NULL;
1173a74af788SAkhil Goyal 
1174a74af788SAkhil Goyal 	cf = &ctx->job;
1175a74af788SAkhil Goyal 	ctx->op = op;
1176a74af788SAkhil Goyal 
1177a74af788SAkhil Goyal 	rte_prefetch0(cf->sg);
1178a74af788SAkhil Goyal 
1179a74af788SAkhil Goyal 	/* output */
1180a74af788SAkhil Goyal 	out_sg = &cf->sg[0];
1181a74af788SAkhil Goyal 	out_sg->extension = 1;
1182a74af788SAkhil Goyal 	if (is_encode(ses))
1183*7a4a6da4SVakul Garg 		out_sg->length = sym->aead.data.length + ses->digest_length;
1184a74af788SAkhil Goyal 	else
1185*7a4a6da4SVakul Garg 		out_sg->length = sym->aead.data.length;
1186a74af788SAkhil Goyal 
1187a74af788SAkhil Goyal 	/* output sg entries */
1188a74af788SAkhil Goyal 	sg = &cf->sg[2];
118995456e89SShreyansh Jain 	qm_sg_entry_set64(out_sg, dpaa_mem_vtop(sg));
1190a74af788SAkhil Goyal 	cpu_to_hw_sg(out_sg);
1191a74af788SAkhil Goyal 
1192a74af788SAkhil Goyal 	/* 1st seg */
1193a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1194*7a4a6da4SVakul Garg 	sg->length = mbuf->data_len - sym->aead.data.offset;
1195*7a4a6da4SVakul Garg 	sg->offset = sym->aead.data.offset;
1196a74af788SAkhil Goyal 
1197a74af788SAkhil Goyal 	/* Successive segs */
1198a74af788SAkhil Goyal 	mbuf = mbuf->next;
1199a74af788SAkhil Goyal 	while (mbuf) {
1200a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1201a74af788SAkhil Goyal 		sg++;
1202a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1203a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1204a74af788SAkhil Goyal 		mbuf = mbuf->next;
1205a74af788SAkhil Goyal 	}
1206a74af788SAkhil Goyal 	sg->length -= ses->digest_length;
1207a74af788SAkhil Goyal 
1208a74af788SAkhil Goyal 	if (is_encode(ses)) {
1209a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1210a74af788SAkhil Goyal 		/* set auth output */
1211a74af788SAkhil Goyal 		sg++;
1212a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, sym->aead.digest.phys_addr);
1213a74af788SAkhil Goyal 		sg->length = ses->digest_length;
1214a74af788SAkhil Goyal 	}
1215a74af788SAkhil Goyal 	sg->final = 1;
1216a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1217a74af788SAkhil Goyal 
1218a74af788SAkhil Goyal 	/* input */
1219a74af788SAkhil Goyal 	mbuf = sym->m_src;
1220a74af788SAkhil Goyal 	in_sg = &cf->sg[1];
1221a74af788SAkhil Goyal 	in_sg->extension = 1;
1222a74af788SAkhil Goyal 	in_sg->final = 1;
1223a74af788SAkhil Goyal 	if (is_encode(ses))
1224a74af788SAkhil Goyal 		in_sg->length = ses->iv.length + sym->aead.data.length
1225a74af788SAkhil Goyal 							+ ses->auth_only_len;
1226a74af788SAkhil Goyal 	else
1227a74af788SAkhil Goyal 		in_sg->length = ses->iv.length + sym->aead.data.length
1228a74af788SAkhil Goyal 				+ ses->auth_only_len + ses->digest_length;
1229a74af788SAkhil Goyal 
1230a74af788SAkhil Goyal 	/* input sg entries */
1231a74af788SAkhil Goyal 	sg++;
123295456e89SShreyansh Jain 	qm_sg_entry_set64(in_sg, dpaa_mem_vtop(sg));
1233a74af788SAkhil Goyal 	cpu_to_hw_sg(in_sg);
1234a74af788SAkhil Goyal 
1235a74af788SAkhil Goyal 	/* 1st seg IV */
1236a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1237a74af788SAkhil Goyal 	sg->length = ses->iv.length;
1238a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1239a74af788SAkhil Goyal 
1240a74af788SAkhil Goyal 	/* 2nd seg auth only */
1241a74af788SAkhil Goyal 	if (ses->auth_only_len) {
1242a74af788SAkhil Goyal 		sg++;
1243a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, dpaa_mem_vtop(sym->aead.aad.data));
1244a74af788SAkhil Goyal 		sg->length = ses->auth_only_len;
1245a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1246a74af788SAkhil Goyal 	}
1247a74af788SAkhil Goyal 
1248a74af788SAkhil Goyal 	/* 3rd seg */
1249a74af788SAkhil Goyal 	sg++;
1250a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1251a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->aead.data.offset;
1252a74af788SAkhil Goyal 	sg->offset = sym->aead.data.offset;
1253a74af788SAkhil Goyal 
1254a74af788SAkhil Goyal 	/* Successive segs */
1255a74af788SAkhil Goyal 	mbuf = mbuf->next;
1256a74af788SAkhil Goyal 	while (mbuf) {
1257a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1258a74af788SAkhil Goyal 		sg++;
1259a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1260a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1261a74af788SAkhil Goyal 		mbuf = mbuf->next;
1262a74af788SAkhil Goyal 	}
1263a74af788SAkhil Goyal 
1264a74af788SAkhil Goyal 	if (is_decode(ses)) {
1265a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1266a74af788SAkhil Goyal 		sg++;
1267a74af788SAkhil Goyal 		memcpy(ctx->digest, sym->aead.digest.data,
1268a74af788SAkhil Goyal 			ses->digest_length);
126995456e89SShreyansh Jain 		qm_sg_entry_set64(sg, dpaa_mem_vtop(ctx->digest));
1270a74af788SAkhil Goyal 		sg->length = ses->digest_length;
1271a74af788SAkhil Goyal 	}
1272a74af788SAkhil Goyal 	sg->final = 1;
1273a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1274a74af788SAkhil Goyal 
1275a74af788SAkhil Goyal 	return cf;
1276a74af788SAkhil Goyal }
1277a74af788SAkhil Goyal 
1278a74af788SAkhil Goyal static inline struct dpaa_sec_job *
1279c3e85bdcSAkhil Goyal build_cipher_auth_gcm(struct rte_crypto_op *op, dpaa_sec_session *ses)
1280c3e85bdcSAkhil Goyal {
1281c3e85bdcSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1282c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
1283c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1284c3e85bdcSAkhil Goyal 	struct qm_sg_entry *sg;
1285c3e85bdcSAkhil Goyal 	uint32_t length = 0;
1286c4509373SSantosh Shukla 	rte_iova_t src_start_addr, dst_start_addr;
1287c3e85bdcSAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1288c3e85bdcSAkhil Goyal 			ses->iv.offset);
1289c3e85bdcSAkhil Goyal 
1290116ff44aSHemant Agrawal 	src_start_addr = sym->m_src->buf_iova + sym->m_src->data_off;
1291a389434eSAlok Makhariya 
1292a389434eSAlok Makhariya 	if (sym->m_dst)
1293116ff44aSHemant Agrawal 		dst_start_addr = sym->m_dst->buf_iova + sym->m_dst->data_off;
1294a389434eSAlok Makhariya 	else
1295a389434eSAlok Makhariya 		dst_start_addr = src_start_addr;
1296c3e85bdcSAkhil Goyal 
1297f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, 7);
1298c3e85bdcSAkhil Goyal 	if (!ctx)
1299c3e85bdcSAkhil Goyal 		return NULL;
1300c3e85bdcSAkhil Goyal 
1301c3e85bdcSAkhil Goyal 	cf = &ctx->job;
1302c3e85bdcSAkhil Goyal 	ctx->op = op;
1303c3e85bdcSAkhil Goyal 
1304c3e85bdcSAkhil Goyal 	/* input */
1305c3e85bdcSAkhil Goyal 	rte_prefetch0(cf->sg);
1306c3e85bdcSAkhil Goyal 	sg = &cf->sg[2];
130795456e89SShreyansh Jain 	qm_sg_entry_set64(&cf->sg[1], dpaa_mem_vtop(sg));
1308c3e85bdcSAkhil Goyal 	if (is_encode(ses)) {
1309c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1310c3e85bdcSAkhil Goyal 		sg->length = ses->iv.length;
1311c3e85bdcSAkhil Goyal 		length += sg->length;
1312c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1313c3e85bdcSAkhil Goyal 
1314c3e85bdcSAkhil Goyal 		sg++;
1315c3e85bdcSAkhil Goyal 		if (ses->auth_only_len) {
1316c3e85bdcSAkhil Goyal 			qm_sg_entry_set64(sg,
1317c3e85bdcSAkhil Goyal 					  dpaa_mem_vtop(sym->aead.aad.data));
1318c3e85bdcSAkhil Goyal 			sg->length = ses->auth_only_len;
1319c3e85bdcSAkhil Goyal 			length += sg->length;
1320c3e85bdcSAkhil Goyal 			cpu_to_hw_sg(sg);
1321c3e85bdcSAkhil Goyal 			sg++;
1322c3e85bdcSAkhil Goyal 		}
1323a389434eSAlok Makhariya 		qm_sg_entry_set64(sg, src_start_addr + sym->aead.data.offset);
1324c3e85bdcSAkhil Goyal 		sg->length = sym->aead.data.length;
1325c3e85bdcSAkhil Goyal 		length += sg->length;
1326c3e85bdcSAkhil Goyal 		sg->final = 1;
1327c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1328c3e85bdcSAkhil Goyal 	} else {
1329c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1330c3e85bdcSAkhil Goyal 		sg->length = ses->iv.length;
1331c3e85bdcSAkhil Goyal 		length += sg->length;
1332c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1333c3e85bdcSAkhil Goyal 
1334c3e85bdcSAkhil Goyal 		sg++;
1335c3e85bdcSAkhil Goyal 		if (ses->auth_only_len) {
1336c3e85bdcSAkhil Goyal 			qm_sg_entry_set64(sg,
1337c3e85bdcSAkhil Goyal 					  dpaa_mem_vtop(sym->aead.aad.data));
1338c3e85bdcSAkhil Goyal 			sg->length = ses->auth_only_len;
1339c3e85bdcSAkhil Goyal 			length += sg->length;
1340c3e85bdcSAkhil Goyal 			cpu_to_hw_sg(sg);
1341c3e85bdcSAkhil Goyal 			sg++;
1342c3e85bdcSAkhil Goyal 		}
1343a389434eSAlok Makhariya 		qm_sg_entry_set64(sg, src_start_addr + sym->aead.data.offset);
1344c3e85bdcSAkhil Goyal 		sg->length = sym->aead.data.length;
1345c3e85bdcSAkhil Goyal 		length += sg->length;
1346c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1347c3e85bdcSAkhil Goyal 
1348c3e85bdcSAkhil Goyal 		memcpy(ctx->digest, sym->aead.digest.data,
1349c3e85bdcSAkhil Goyal 		       ses->digest_length);
1350c3e85bdcSAkhil Goyal 		sg++;
1351c3e85bdcSAkhil Goyal 
135295456e89SShreyansh Jain 		qm_sg_entry_set64(sg, dpaa_mem_vtop(ctx->digest));
1353c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
1354c3e85bdcSAkhil Goyal 		length += sg->length;
1355c3e85bdcSAkhil Goyal 		sg->final = 1;
1356c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1357c3e85bdcSAkhil Goyal 	}
1358c3e85bdcSAkhil Goyal 	/* input compound frame */
1359c3e85bdcSAkhil Goyal 	cf->sg[1].length = length;
1360c3e85bdcSAkhil Goyal 	cf->sg[1].extension = 1;
1361c3e85bdcSAkhil Goyal 	cf->sg[1].final = 1;
1362c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(&cf->sg[1]);
1363c3e85bdcSAkhil Goyal 
1364c3e85bdcSAkhil Goyal 	/* output */
1365c3e85bdcSAkhil Goyal 	sg++;
136695456e89SShreyansh Jain 	qm_sg_entry_set64(&cf->sg[0], dpaa_mem_vtop(sg));
1367c3e85bdcSAkhil Goyal 	qm_sg_entry_set64(sg,
1368*7a4a6da4SVakul Garg 		dst_start_addr + sym->aead.data.offset);
1369*7a4a6da4SVakul Garg 	sg->length = sym->aead.data.length;
1370c3e85bdcSAkhil Goyal 	length = sg->length;
1371c3e85bdcSAkhil Goyal 	if (is_encode(ses)) {
1372c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1373c3e85bdcSAkhil Goyal 		/* set auth output */
1374c3e85bdcSAkhil Goyal 		sg++;
1375c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, sym->aead.digest.phys_addr);
1376c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
1377c3e85bdcSAkhil Goyal 		length += sg->length;
1378c3e85bdcSAkhil Goyal 	}
1379c3e85bdcSAkhil Goyal 	sg->final = 1;
1380c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1381c3e85bdcSAkhil Goyal 
1382c3e85bdcSAkhil Goyal 	/* output compound frame */
1383c3e85bdcSAkhil Goyal 	cf->sg[0].length = length;
1384c3e85bdcSAkhil Goyal 	cf->sg[0].extension = 1;
1385c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(&cf->sg[0]);
1386c3e85bdcSAkhil Goyal 
1387c3e85bdcSAkhil Goyal 	return cf;
1388c3e85bdcSAkhil Goyal }
1389c3e85bdcSAkhil Goyal 
1390c3e85bdcSAkhil Goyal static inline struct dpaa_sec_job *
1391a74af788SAkhil Goyal build_cipher_auth_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
1392a74af788SAkhil Goyal {
1393a74af788SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1394a74af788SAkhil Goyal 	struct dpaa_sec_job *cf;
1395a74af788SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1396a74af788SAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
1397a74af788SAkhil Goyal 	struct rte_mbuf *mbuf;
1398a74af788SAkhil Goyal 	uint8_t req_segs;
1399a74af788SAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1400a74af788SAkhil Goyal 			ses->iv.offset);
1401a74af788SAkhil Goyal 
1402a74af788SAkhil Goyal 	if (sym->m_dst) {
1403a74af788SAkhil Goyal 		mbuf = sym->m_dst;
1404a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 4;
1405a74af788SAkhil Goyal 	} else {
1406a74af788SAkhil Goyal 		mbuf = sym->m_src;
1407a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs * 2 + 4;
1408a74af788SAkhil Goyal 	}
1409a74af788SAkhil Goyal 
1410f7a5752eSHemant Agrawal 	if (mbuf->nb_segs > MAX_SG_ENTRIES) {
1411f163231eSHemant Agrawal 		DPAA_SEC_DP_ERR("Cipher-Auth: Max sec segs supported is %d",
1412a74af788SAkhil Goyal 				MAX_SG_ENTRIES);
1413a74af788SAkhil Goyal 		return NULL;
1414a74af788SAkhil Goyal 	}
1415a74af788SAkhil Goyal 
1416f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, req_segs);
1417a74af788SAkhil Goyal 	if (!ctx)
1418a74af788SAkhil Goyal 		return NULL;
1419a74af788SAkhil Goyal 
1420a74af788SAkhil Goyal 	cf = &ctx->job;
1421a74af788SAkhil Goyal 	ctx->op = op;
1422a74af788SAkhil Goyal 
1423a74af788SAkhil Goyal 	rte_prefetch0(cf->sg);
1424a74af788SAkhil Goyal 
1425a74af788SAkhil Goyal 	/* output */
1426a74af788SAkhil Goyal 	out_sg = &cf->sg[0];
1427a74af788SAkhil Goyal 	out_sg->extension = 1;
1428a74af788SAkhil Goyal 	if (is_encode(ses))
1429a74af788SAkhil Goyal 		out_sg->length = sym->auth.data.length + ses->digest_length;
1430a74af788SAkhil Goyal 	else
1431a74af788SAkhil Goyal 		out_sg->length = sym->auth.data.length;
1432a74af788SAkhil Goyal 
1433a74af788SAkhil Goyal 	/* output sg entries */
1434a74af788SAkhil Goyal 	sg = &cf->sg[2];
143595456e89SShreyansh Jain 	qm_sg_entry_set64(out_sg, dpaa_mem_vtop(sg));
1436a74af788SAkhil Goyal 	cpu_to_hw_sg(out_sg);
1437a74af788SAkhil Goyal 
1438a74af788SAkhil Goyal 	/* 1st seg */
1439a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1440a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->auth.data.offset;
1441a74af788SAkhil Goyal 	sg->offset = sym->auth.data.offset;
1442a74af788SAkhil Goyal 
1443a74af788SAkhil Goyal 	/* Successive segs */
1444a74af788SAkhil Goyal 	mbuf = mbuf->next;
1445a74af788SAkhil Goyal 	while (mbuf) {
1446a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1447a74af788SAkhil Goyal 		sg++;
1448a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1449a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1450a74af788SAkhil Goyal 		mbuf = mbuf->next;
1451a74af788SAkhil Goyal 	}
1452a74af788SAkhil Goyal 	sg->length -= ses->digest_length;
1453a74af788SAkhil Goyal 
1454a74af788SAkhil Goyal 	if (is_encode(ses)) {
1455a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1456a74af788SAkhil Goyal 		/* set auth output */
1457a74af788SAkhil Goyal 		sg++;
1458a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
1459a74af788SAkhil Goyal 		sg->length = ses->digest_length;
1460a74af788SAkhil Goyal 	}
1461a74af788SAkhil Goyal 	sg->final = 1;
1462a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1463a74af788SAkhil Goyal 
1464a74af788SAkhil Goyal 	/* input */
1465a74af788SAkhil Goyal 	mbuf = sym->m_src;
1466a74af788SAkhil Goyal 	in_sg = &cf->sg[1];
1467a74af788SAkhil Goyal 	in_sg->extension = 1;
1468a74af788SAkhil Goyal 	in_sg->final = 1;
1469a74af788SAkhil Goyal 	if (is_encode(ses))
1470a74af788SAkhil Goyal 		in_sg->length = ses->iv.length + sym->auth.data.length;
1471a74af788SAkhil Goyal 	else
1472a74af788SAkhil Goyal 		in_sg->length = ses->iv.length + sym->auth.data.length
1473a74af788SAkhil Goyal 						+ ses->digest_length;
1474a74af788SAkhil Goyal 
1475a74af788SAkhil Goyal 	/* input sg entries */
1476a74af788SAkhil Goyal 	sg++;
147795456e89SShreyansh Jain 	qm_sg_entry_set64(in_sg, dpaa_mem_vtop(sg));
1478a74af788SAkhil Goyal 	cpu_to_hw_sg(in_sg);
1479a74af788SAkhil Goyal 
1480a74af788SAkhil Goyal 	/* 1st seg IV */
1481a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1482a74af788SAkhil Goyal 	sg->length = ses->iv.length;
1483a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1484a74af788SAkhil Goyal 
1485a74af788SAkhil Goyal 	/* 2nd seg */
1486a74af788SAkhil Goyal 	sg++;
1487a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1488a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->auth.data.offset;
1489a74af788SAkhil Goyal 	sg->offset = sym->auth.data.offset;
1490a74af788SAkhil Goyal 
1491a74af788SAkhil Goyal 	/* Successive segs */
1492a74af788SAkhil Goyal 	mbuf = mbuf->next;
1493a74af788SAkhil Goyal 	while (mbuf) {
1494a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1495a74af788SAkhil Goyal 		sg++;
1496a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1497a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1498a74af788SAkhil Goyal 		mbuf = mbuf->next;
1499a74af788SAkhil Goyal 	}
1500a74af788SAkhil Goyal 
1501a74af788SAkhil Goyal 	sg->length -= ses->digest_length;
1502a74af788SAkhil Goyal 	if (is_decode(ses)) {
1503a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1504a74af788SAkhil Goyal 		sg++;
1505a74af788SAkhil Goyal 		memcpy(ctx->digest, sym->auth.digest.data,
1506a74af788SAkhil Goyal 			ses->digest_length);
150795456e89SShreyansh Jain 		qm_sg_entry_set64(sg, dpaa_mem_vtop(ctx->digest));
1508a74af788SAkhil Goyal 		sg->length = ses->digest_length;
1509a74af788SAkhil Goyal 	}
1510a74af788SAkhil Goyal 	sg->final = 1;
1511a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1512a74af788SAkhil Goyal 
1513a74af788SAkhil Goyal 	return cf;
1514a74af788SAkhil Goyal }
1515a74af788SAkhil Goyal 
1516a74af788SAkhil Goyal static inline struct dpaa_sec_job *
1517c3e85bdcSAkhil Goyal build_cipher_auth(struct rte_crypto_op *op, dpaa_sec_session *ses)
1518c3e85bdcSAkhil Goyal {
1519c3e85bdcSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1520c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
1521c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1522c3e85bdcSAkhil Goyal 	struct qm_sg_entry *sg;
1523c4509373SSantosh Shukla 	rte_iova_t src_start_addr, dst_start_addr;
1524c3e85bdcSAkhil Goyal 	uint32_t length = 0;
1525c3e85bdcSAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1526c3e85bdcSAkhil Goyal 			ses->iv.offset);
1527c3e85bdcSAkhil Goyal 
1528455da545SSantosh Shukla 	src_start_addr = sym->m_src->buf_iova + sym->m_src->data_off;
1529a389434eSAlok Makhariya 	if (sym->m_dst)
1530455da545SSantosh Shukla 		dst_start_addr = sym->m_dst->buf_iova + sym->m_dst->data_off;
1531a389434eSAlok Makhariya 	else
1532a389434eSAlok Makhariya 		dst_start_addr = src_start_addr;
1533c3e85bdcSAkhil Goyal 
1534f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, 7);
1535c3e85bdcSAkhil Goyal 	if (!ctx)
1536c3e85bdcSAkhil Goyal 		return NULL;
1537c3e85bdcSAkhil Goyal 
1538c3e85bdcSAkhil Goyal 	cf = &ctx->job;
1539c3e85bdcSAkhil Goyal 	ctx->op = op;
1540c3e85bdcSAkhil Goyal 
1541c3e85bdcSAkhil Goyal 	/* input */
1542c3e85bdcSAkhil Goyal 	rte_prefetch0(cf->sg);
1543c3e85bdcSAkhil Goyal 	sg = &cf->sg[2];
154495456e89SShreyansh Jain 	qm_sg_entry_set64(&cf->sg[1], dpaa_mem_vtop(sg));
1545c3e85bdcSAkhil Goyal 	if (is_encode(ses)) {
1546c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1547c3e85bdcSAkhil Goyal 		sg->length = ses->iv.length;
1548c3e85bdcSAkhil Goyal 		length += sg->length;
1549c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1550c3e85bdcSAkhil Goyal 
1551c3e85bdcSAkhil Goyal 		sg++;
1552a389434eSAlok Makhariya 		qm_sg_entry_set64(sg, src_start_addr + sym->auth.data.offset);
1553c3e85bdcSAkhil Goyal 		sg->length = sym->auth.data.length;
1554c3e85bdcSAkhil Goyal 		length += sg->length;
1555c3e85bdcSAkhil Goyal 		sg->final = 1;
1556c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1557c3e85bdcSAkhil Goyal 	} else {
1558c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1559c3e85bdcSAkhil Goyal 		sg->length = ses->iv.length;
1560c3e85bdcSAkhil Goyal 		length += sg->length;
1561c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1562c3e85bdcSAkhil Goyal 
1563c3e85bdcSAkhil Goyal 		sg++;
1564c3e85bdcSAkhil Goyal 
1565a389434eSAlok Makhariya 		qm_sg_entry_set64(sg, src_start_addr + sym->auth.data.offset);
1566c3e85bdcSAkhil Goyal 		sg->length = sym->auth.data.length;
1567c3e85bdcSAkhil Goyal 		length += sg->length;
1568c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1569c3e85bdcSAkhil Goyal 
1570c3e85bdcSAkhil Goyal 		memcpy(ctx->digest, sym->auth.digest.data,
1571c3e85bdcSAkhil Goyal 		       ses->digest_length);
1572c3e85bdcSAkhil Goyal 		sg++;
1573c3e85bdcSAkhil Goyal 
157495456e89SShreyansh Jain 		qm_sg_entry_set64(sg, dpaa_mem_vtop(ctx->digest));
1575c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
1576c3e85bdcSAkhil Goyal 		length += sg->length;
1577c3e85bdcSAkhil Goyal 		sg->final = 1;
1578c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1579c3e85bdcSAkhil Goyal 	}
1580c3e85bdcSAkhil Goyal 	/* input compound frame */
1581c3e85bdcSAkhil Goyal 	cf->sg[1].length = length;
1582c3e85bdcSAkhil Goyal 	cf->sg[1].extension = 1;
1583c3e85bdcSAkhil Goyal 	cf->sg[1].final = 1;
1584c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(&cf->sg[1]);
1585c3e85bdcSAkhil Goyal 
1586c3e85bdcSAkhil Goyal 	/* output */
1587c3e85bdcSAkhil Goyal 	sg++;
158895456e89SShreyansh Jain 	qm_sg_entry_set64(&cf->sg[0], dpaa_mem_vtop(sg));
1589a389434eSAlok Makhariya 	qm_sg_entry_set64(sg, dst_start_addr + sym->cipher.data.offset);
1590c3e85bdcSAkhil Goyal 	sg->length = sym->cipher.data.length;
1591c3e85bdcSAkhil Goyal 	length = sg->length;
1592c3e85bdcSAkhil Goyal 	if (is_encode(ses)) {
1593c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1594c3e85bdcSAkhil Goyal 		/* set auth output */
1595c3e85bdcSAkhil Goyal 		sg++;
1596c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
1597c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
1598c3e85bdcSAkhil Goyal 		length += sg->length;
1599c3e85bdcSAkhil Goyal 	}
1600c3e85bdcSAkhil Goyal 	sg->final = 1;
1601c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1602c3e85bdcSAkhil Goyal 
1603c3e85bdcSAkhil Goyal 	/* output compound frame */
1604c3e85bdcSAkhil Goyal 	cf->sg[0].length = length;
1605c3e85bdcSAkhil Goyal 	cf->sg[0].extension = 1;
1606c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(&cf->sg[0]);
1607c3e85bdcSAkhil Goyal 
1608c3e85bdcSAkhil Goyal 	return cf;
1609c3e85bdcSAkhil Goyal }
1610c3e85bdcSAkhil Goyal 
16111f14d500SAkhil Goyal static inline struct dpaa_sec_job *
16121f14d500SAkhil Goyal build_proto(struct rte_crypto_op *op, dpaa_sec_session *ses)
16131f14d500SAkhil Goyal {
16141f14d500SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
16151f14d500SAkhil Goyal 	struct dpaa_sec_job *cf;
16161f14d500SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
16171f14d500SAkhil Goyal 	struct qm_sg_entry *sg;
16181f14d500SAkhil Goyal 	phys_addr_t src_start_addr, dst_start_addr;
16191f14d500SAkhil Goyal 
1620f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, 2);
16211f14d500SAkhil Goyal 	if (!ctx)
16221f14d500SAkhil Goyal 		return NULL;
16231f14d500SAkhil Goyal 	cf = &ctx->job;
16241f14d500SAkhil Goyal 	ctx->op = op;
16251f14d500SAkhil Goyal 
16261f14d500SAkhil Goyal 	src_start_addr = rte_pktmbuf_mtophys(sym->m_src);
16271f14d500SAkhil Goyal 
16281f14d500SAkhil Goyal 	if (sym->m_dst)
16291f14d500SAkhil Goyal 		dst_start_addr = rte_pktmbuf_mtophys(sym->m_dst);
16301f14d500SAkhil Goyal 	else
16311f14d500SAkhil Goyal 		dst_start_addr = src_start_addr;
16321f14d500SAkhil Goyal 
16331f14d500SAkhil Goyal 	/* input */
16341f14d500SAkhil Goyal 	sg = &cf->sg[1];
16351f14d500SAkhil Goyal 	qm_sg_entry_set64(sg, src_start_addr);
16361f14d500SAkhil Goyal 	sg->length = sym->m_src->pkt_len;
16371f14d500SAkhil Goyal 	sg->final = 1;
16381f14d500SAkhil Goyal 	cpu_to_hw_sg(sg);
16391f14d500SAkhil Goyal 
16401f14d500SAkhil Goyal 	sym->m_src->packet_type &= ~RTE_PTYPE_L4_MASK;
16411f14d500SAkhil Goyal 	/* output */
16421f14d500SAkhil Goyal 	sg = &cf->sg[0];
16431f14d500SAkhil Goyal 	qm_sg_entry_set64(sg, dst_start_addr);
16441f14d500SAkhil Goyal 	sg->length = sym->m_src->buf_len - sym->m_src->data_off;
16451f14d500SAkhil Goyal 	cpu_to_hw_sg(sg);
16461f14d500SAkhil Goyal 
16471f14d500SAkhil Goyal 	return cf;
16481f14d500SAkhil Goyal }
16491f14d500SAkhil Goyal 
1650fb5c100aSAkhil Goyal static inline struct dpaa_sec_job *
1651fb5c100aSAkhil Goyal build_proto_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
1652fb5c100aSAkhil Goyal {
1653fb5c100aSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1654fb5c100aSAkhil Goyal 	struct dpaa_sec_job *cf;
1655fb5c100aSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1656fb5c100aSAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
1657fb5c100aSAkhil Goyal 	struct rte_mbuf *mbuf;
1658fb5c100aSAkhil Goyal 	uint8_t req_segs;
1659fb5c100aSAkhil Goyal 	uint32_t in_len = 0, out_len = 0;
1660fb5c100aSAkhil Goyal 
1661fb5c100aSAkhil Goyal 	if (sym->m_dst)
1662fb5c100aSAkhil Goyal 		mbuf = sym->m_dst;
1663fb5c100aSAkhil Goyal 	else
1664fb5c100aSAkhil Goyal 		mbuf = sym->m_src;
1665fb5c100aSAkhil Goyal 
1666fb5c100aSAkhil Goyal 	req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 2;
1667f7a5752eSHemant Agrawal 	if (mbuf->nb_segs > MAX_SG_ENTRIES) {
1668fb5c100aSAkhil Goyal 		DPAA_SEC_DP_ERR("Proto: Max sec segs supported is %d",
1669fb5c100aSAkhil Goyal 				MAX_SG_ENTRIES);
1670fb5c100aSAkhil Goyal 		return NULL;
1671fb5c100aSAkhil Goyal 	}
1672fb5c100aSAkhil Goyal 
1673f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, req_segs);
1674fb5c100aSAkhil Goyal 	if (!ctx)
1675fb5c100aSAkhil Goyal 		return NULL;
1676fb5c100aSAkhil Goyal 	cf = &ctx->job;
1677fb5c100aSAkhil Goyal 	ctx->op = op;
1678fb5c100aSAkhil Goyal 	/* output */
1679fb5c100aSAkhil Goyal 	out_sg = &cf->sg[0];
1680fb5c100aSAkhil Goyal 	out_sg->extension = 1;
1681fb5c100aSAkhil Goyal 	qm_sg_entry_set64(out_sg, dpaa_mem_vtop(&cf->sg[2]));
1682fb5c100aSAkhil Goyal 
1683fb5c100aSAkhil Goyal 	/* 1st seg */
1684fb5c100aSAkhil Goyal 	sg = &cf->sg[2];
1685fb5c100aSAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1686fb5c100aSAkhil Goyal 	sg->offset = 0;
1687fb5c100aSAkhil Goyal 
1688fb5c100aSAkhil Goyal 	/* Successive segs */
1689fb5c100aSAkhil Goyal 	while (mbuf->next) {
1690fb5c100aSAkhil Goyal 		sg->length = mbuf->data_len;
1691fb5c100aSAkhil Goyal 		out_len += sg->length;
1692fb5c100aSAkhil Goyal 		mbuf = mbuf->next;
1693fb5c100aSAkhil Goyal 		cpu_to_hw_sg(sg);
1694fb5c100aSAkhil Goyal 		sg++;
1695fb5c100aSAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1696fb5c100aSAkhil Goyal 		sg->offset = 0;
1697fb5c100aSAkhil Goyal 	}
1698fb5c100aSAkhil Goyal 	sg->length = mbuf->buf_len - mbuf->data_off;
1699fb5c100aSAkhil Goyal 	out_len += sg->length;
1700fb5c100aSAkhil Goyal 	sg->final = 1;
1701fb5c100aSAkhil Goyal 	cpu_to_hw_sg(sg);
1702fb5c100aSAkhil Goyal 
1703fb5c100aSAkhil Goyal 	out_sg->length = out_len;
1704fb5c100aSAkhil Goyal 	cpu_to_hw_sg(out_sg);
1705fb5c100aSAkhil Goyal 
1706fb5c100aSAkhil Goyal 	/* input */
1707fb5c100aSAkhil Goyal 	mbuf = sym->m_src;
1708fb5c100aSAkhil Goyal 	in_sg = &cf->sg[1];
1709fb5c100aSAkhil Goyal 	in_sg->extension = 1;
1710fb5c100aSAkhil Goyal 	in_sg->final = 1;
1711fb5c100aSAkhil Goyal 	in_len = mbuf->data_len;
1712fb5c100aSAkhil Goyal 
1713fb5c100aSAkhil Goyal 	sg++;
1714fb5c100aSAkhil Goyal 	qm_sg_entry_set64(in_sg, dpaa_mem_vtop(sg));
1715fb5c100aSAkhil Goyal 
1716fb5c100aSAkhil Goyal 	/* 1st seg */
1717fb5c100aSAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1718fb5c100aSAkhil Goyal 	sg->length = mbuf->data_len;
1719fb5c100aSAkhil Goyal 	sg->offset = 0;
1720fb5c100aSAkhil Goyal 
1721fb5c100aSAkhil Goyal 	/* Successive segs */
1722fb5c100aSAkhil Goyal 	mbuf = mbuf->next;
1723fb5c100aSAkhil Goyal 	while (mbuf) {
1724fb5c100aSAkhil Goyal 		cpu_to_hw_sg(sg);
1725fb5c100aSAkhil Goyal 		sg++;
1726fb5c100aSAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1727fb5c100aSAkhil Goyal 		sg->length = mbuf->data_len;
1728fb5c100aSAkhil Goyal 		sg->offset = 0;
1729fb5c100aSAkhil Goyal 		in_len += sg->length;
1730fb5c100aSAkhil Goyal 		mbuf = mbuf->next;
1731fb5c100aSAkhil Goyal 	}
1732fb5c100aSAkhil Goyal 	sg->final = 1;
1733fb5c100aSAkhil Goyal 	cpu_to_hw_sg(sg);
1734fb5c100aSAkhil Goyal 
1735fb5c100aSAkhil Goyal 	in_sg->length = in_len;
1736fb5c100aSAkhil Goyal 	cpu_to_hw_sg(in_sg);
1737fb5c100aSAkhil Goyal 
1738fb5c100aSAkhil Goyal 	sym->m_src->packet_type &= ~RTE_PTYPE_L4_MASK;
1739fb5c100aSAkhil Goyal 
1740fb5c100aSAkhil Goyal 	return cf;
1741fb5c100aSAkhil Goyal }
1742fb5c100aSAkhil Goyal 
17439a984458SAkhil Goyal static uint16_t
17449a984458SAkhil Goyal dpaa_sec_enqueue_burst(void *qp, struct rte_crypto_op **ops,
17459a984458SAkhil Goyal 		       uint16_t nb_ops)
1746c3e85bdcSAkhil Goyal {
17479a984458SAkhil Goyal 	/* Function to transmit the frames to given device and queuepair */
17489a984458SAkhil Goyal 	uint32_t loop;
17499a984458SAkhil Goyal 	struct dpaa_sec_qp *dpaa_qp = (struct dpaa_sec_qp *)qp;
17509a984458SAkhil Goyal 	uint16_t num_tx = 0;
17519a984458SAkhil Goyal 	struct qm_fd fds[DPAA_SEC_BURST], *fd;
17529a984458SAkhil Goyal 	uint32_t frames_to_send;
17539a984458SAkhil Goyal 	struct rte_crypto_op *op;
1754c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
1755c3e85bdcSAkhil Goyal 	dpaa_sec_session *ses;
1756fe3688baSAkhil Goyal 	uint32_t auth_only_len, index, flags[DPAA_SEC_BURST] = {0};
17579a984458SAkhil Goyal 	struct qman_fq *inq[DPAA_SEC_BURST];
1758c3e85bdcSAkhil Goyal 
17599a984458SAkhil Goyal 	while (nb_ops) {
17609a984458SAkhil Goyal 		frames_to_send = (nb_ops > DPAA_SEC_BURST) ?
17619a984458SAkhil Goyal 				DPAA_SEC_BURST : nb_ops;
17629a984458SAkhil Goyal 		for (loop = 0; loop < frames_to_send; loop++) {
17639a984458SAkhil Goyal 			op = *(ops++);
1764fe3688baSAkhil Goyal 			if (op->sym->m_src->seqn != 0) {
1765fe3688baSAkhil Goyal 				index = op->sym->m_src->seqn - 1;
1766fe3688baSAkhil Goyal 				if (DPAA_PER_LCORE_DQRR_HELD & (1 << index)) {
1767fe3688baSAkhil Goyal 					/* QM_EQCR_DCA_IDXMASK = 0x0f */
1768fe3688baSAkhil Goyal 					flags[loop] = ((index & 0x0f) << 8);
1769fe3688baSAkhil Goyal 					flags[loop] |= QMAN_ENQUEUE_FLAG_DCA;
1770fe3688baSAkhil Goyal 					DPAA_PER_LCORE_DQRR_SIZE--;
1771fe3688baSAkhil Goyal 					DPAA_PER_LCORE_DQRR_HELD &=
1772fe3688baSAkhil Goyal 								~(1 << index);
1773fe3688baSAkhil Goyal 				}
1774fe3688baSAkhil Goyal 			}
1775fe3688baSAkhil Goyal 
17769a984458SAkhil Goyal 			switch (op->sess_type) {
17779a984458SAkhil Goyal 			case RTE_CRYPTO_OP_WITH_SESSION:
17789a984458SAkhil Goyal 				ses = (dpaa_sec_session *)
1779012c5076SPablo de Lara 					get_sym_session_private_data(
17809a984458SAkhil Goyal 							op->sym->session,
17819a984458SAkhil Goyal 							cryptodev_driver_id);
17829a984458SAkhil Goyal 				break;
17839a984458SAkhil Goyal 			case RTE_CRYPTO_OP_SECURITY_SESSION:
17849a984458SAkhil Goyal 				ses = (dpaa_sec_session *)
17859a984458SAkhil Goyal 					get_sec_session_private_data(
17861f14d500SAkhil Goyal 							op->sym->sec_session);
17879a984458SAkhil Goyal 				break;
17889a984458SAkhil Goyal 			default:
1789f163231eSHemant Agrawal 				DPAA_SEC_DP_ERR(
17909a984458SAkhil Goyal 					"sessionless crypto op not supported");
17919a984458SAkhil Goyal 				frames_to_send = loop;
17929a984458SAkhil Goyal 				nb_ops = loop;
17939a984458SAkhil Goyal 				goto send_pkts;
17949a984458SAkhil Goyal 			}
17954e694fe5SAkhil Goyal 			if (unlikely(!ses->qp[rte_lcore_id() % MAX_DPAA_CORES])) {
17969a984458SAkhil Goyal 				if (dpaa_sec_attach_sess_q(qp, ses)) {
17979a984458SAkhil Goyal 					frames_to_send = loop;
17989a984458SAkhil Goyal 					nb_ops = loop;
17999a984458SAkhil Goyal 					goto send_pkts;
18009a984458SAkhil Goyal 				}
18014e694fe5SAkhil Goyal 			} else if (unlikely(ses->qp[rte_lcore_id() %
18024e694fe5SAkhil Goyal 						MAX_DPAA_CORES] != qp)) {
18039198b2c2SAkhil Goyal 				DPAA_SEC_DP_ERR("Old:sess->qp = %p"
18044e694fe5SAkhil Goyal 					" New qp = %p\n",
18054e694fe5SAkhil Goyal 					ses->qp[rte_lcore_id() %
18064e694fe5SAkhil Goyal 					MAX_DPAA_CORES], qp);
18079198b2c2SAkhil Goyal 				frames_to_send = loop;
18089198b2c2SAkhil Goyal 				nb_ops = loop;
18099198b2c2SAkhil Goyal 				goto send_pkts;
1810c3e85bdcSAkhil Goyal 			}
1811c3e85bdcSAkhil Goyal 
18129a984458SAkhil Goyal 			auth_only_len = op->sym->auth.data.length -
18139a984458SAkhil Goyal 						op->sym->cipher.data.length;
1814fb5c100aSAkhil Goyal 			if (rte_pktmbuf_is_contiguous(op->sym->m_src) &&
1815fb5c100aSAkhil Goyal 				  ((op->sym->m_dst == NULL) ||
1816fb5c100aSAkhil Goyal 				   rte_pktmbuf_is_contiguous(op->sym->m_dst))) {
181705b12700SHemant Agrawal 				if (is_proto_ipsec(ses)) {
181805b12700SHemant Agrawal 					cf = build_proto(op, ses);
1819a1173d55SHemant Agrawal 				} else if (is_proto_pdcp(ses)) {
1820a1173d55SHemant Agrawal 					cf = build_proto(op, ses);
182105b12700SHemant Agrawal 				} else if (is_auth_only(ses)) {
1822c3e85bdcSAkhil Goyal 					cf = build_auth_only(op, ses);
1823c3e85bdcSAkhil Goyal 				} else if (is_cipher_only(ses)) {
1824c3e85bdcSAkhil Goyal 					cf = build_cipher_only(op, ses);
1825c3e85bdcSAkhil Goyal 				} else if (is_aead(ses)) {
1826c3e85bdcSAkhil Goyal 					cf = build_cipher_auth_gcm(op, ses);
1827c3e85bdcSAkhil Goyal 					auth_only_len = ses->auth_only_len;
1828c3e85bdcSAkhil Goyal 				} else if (is_auth_cipher(ses)) {
1829c3e85bdcSAkhil Goyal 					cf = build_cipher_auth(op, ses);
1830c3e85bdcSAkhil Goyal 				} else {
1831f163231eSHemant Agrawal 					DPAA_SEC_DP_ERR("not supported ops");
18329a984458SAkhil Goyal 					frames_to_send = loop;
18339a984458SAkhil Goyal 					nb_ops = loop;
18349a984458SAkhil Goyal 					goto send_pkts;
1835c3e85bdcSAkhil Goyal 				}
1836a74af788SAkhil Goyal 			} else {
1837fb5c100aSAkhil Goyal 				if (is_proto_pdcp(ses) || is_proto_ipsec(ses)) {
1838fb5c100aSAkhil Goyal 					cf = build_proto_sg(op, ses);
1839fb5c100aSAkhil Goyal 				} else if (is_auth_only(ses)) {
1840a74af788SAkhil Goyal 					cf = build_auth_only_sg(op, ses);
1841a74af788SAkhil Goyal 				} else if (is_cipher_only(ses)) {
1842a74af788SAkhil Goyal 					cf = build_cipher_only_sg(op, ses);
1843a74af788SAkhil Goyal 				} else if (is_aead(ses)) {
1844a74af788SAkhil Goyal 					cf = build_cipher_auth_gcm_sg(op, ses);
1845a74af788SAkhil Goyal 					auth_only_len = ses->auth_only_len;
1846a74af788SAkhil Goyal 				} else if (is_auth_cipher(ses)) {
1847a74af788SAkhil Goyal 					cf = build_cipher_auth_sg(op, ses);
1848a74af788SAkhil Goyal 				} else {
1849f163231eSHemant Agrawal 					DPAA_SEC_DP_ERR("not supported ops");
1850a74af788SAkhil Goyal 					frames_to_send = loop;
1851a74af788SAkhil Goyal 					nb_ops = loop;
1852a74af788SAkhil Goyal 					goto send_pkts;
1853a74af788SAkhil Goyal 				}
1854a74af788SAkhil Goyal 			}
18559a984458SAkhil Goyal 			if (unlikely(!cf)) {
18569a984458SAkhil Goyal 				frames_to_send = loop;
18579a984458SAkhil Goyal 				nb_ops = loop;
18589a984458SAkhil Goyal 				goto send_pkts;
18599a984458SAkhil Goyal 			}
1860c3e85bdcSAkhil Goyal 
18619a984458SAkhil Goyal 			fd = &fds[loop];
18624e694fe5SAkhil Goyal 			inq[loop] = ses->inq[rte_lcore_id() % MAX_DPAA_CORES];
18639a984458SAkhil Goyal 			fd->opaque_addr = 0;
18649a984458SAkhil Goyal 			fd->cmd = 0;
186595456e89SShreyansh Jain 			qm_fd_addr_set64(fd, dpaa_mem_vtop(cf->sg));
18669a984458SAkhil Goyal 			fd->_format1 = qm_fd_compound;
18679a984458SAkhil Goyal 			fd->length29 = 2 * sizeof(struct qm_sg_entry);
18689a984458SAkhil Goyal 			/* Auth_only_len is set as 0 in descriptor and it is
18699a984458SAkhil Goyal 			 * overwritten here in the fd.cmd which will update
18709a984458SAkhil Goyal 			 * the DPOVRD reg.
1871c3e85bdcSAkhil Goyal 			 */
1872c3e85bdcSAkhil Goyal 			if (auth_only_len)
18739a984458SAkhil Goyal 				fd->cmd = 0x80000000 | auth_only_len;
1874c3e85bdcSAkhil Goyal 
18756a0c9d36SAkhil Goyal 			/* In case of PDCP, per packet HFN is stored in
18766a0c9d36SAkhil Goyal 			 * mbuf priv after sym_op.
18776a0c9d36SAkhil Goyal 			 */
18786a0c9d36SAkhil Goyal 			if (is_proto_pdcp(ses) && ses->pdcp.hfn_ovd) {
18796a0c9d36SAkhil Goyal 				fd->cmd = 0x80000000 |
18806a0c9d36SAkhil Goyal 					*((uint32_t *)((uint8_t *)op +
18816a0c9d36SAkhil Goyal 					ses->pdcp.hfn_ovd_offset));
18826a0c9d36SAkhil Goyal 				DPAA_SEC_DP_DEBUG("Per packet HFN: %x, ovd:%u,%u\n",
18836a0c9d36SAkhil Goyal 					*((uint32_t *)((uint8_t *)op +
18846a0c9d36SAkhil Goyal 					ses->pdcp.hfn_ovd_offset)),
18856a0c9d36SAkhil Goyal 					ses->pdcp.hfn_ovd,
18866a0c9d36SAkhil Goyal 					is_proto_pdcp(ses));
18876a0c9d36SAkhil Goyal 			}
18886a0c9d36SAkhil Goyal 
18899a984458SAkhil Goyal 		}
18909a984458SAkhil Goyal send_pkts:
18919a984458SAkhil Goyal 		loop = 0;
18929a984458SAkhil Goyal 		while (loop < frames_to_send) {
18939a984458SAkhil Goyal 			loop += qman_enqueue_multi_fq(&inq[loop], &fds[loop],
1894fe3688baSAkhil Goyal 					&flags[loop], frames_to_send - loop);
18959a984458SAkhil Goyal 		}
18969a984458SAkhil Goyal 		nb_ops -= frames_to_send;
18979a984458SAkhil Goyal 		num_tx += frames_to_send;
1898c3e85bdcSAkhil Goyal 	}
1899c3e85bdcSAkhil Goyal 
1900c3e85bdcSAkhil Goyal 	dpaa_qp->tx_pkts += num_tx;
1901c3e85bdcSAkhil Goyal 	dpaa_qp->tx_errs += nb_ops - num_tx;
1902c3e85bdcSAkhil Goyal 
1903c3e85bdcSAkhil Goyal 	return num_tx;
1904c3e85bdcSAkhil Goyal }
1905c3e85bdcSAkhil Goyal 
1906c3e85bdcSAkhil Goyal static uint16_t
1907c3e85bdcSAkhil Goyal dpaa_sec_dequeue_burst(void *qp, struct rte_crypto_op **ops,
1908c3e85bdcSAkhil Goyal 		       uint16_t nb_ops)
1909c3e85bdcSAkhil Goyal {
1910c3e85bdcSAkhil Goyal 	uint16_t num_rx;
1911c3e85bdcSAkhil Goyal 	struct dpaa_sec_qp *dpaa_qp = (struct dpaa_sec_qp *)qp;
1912c3e85bdcSAkhil Goyal 
1913c3e85bdcSAkhil Goyal 	num_rx = dpaa_sec_deq(dpaa_qp, ops, nb_ops);
1914c3e85bdcSAkhil Goyal 
1915c3e85bdcSAkhil Goyal 	dpaa_qp->rx_pkts += num_rx;
1916c3e85bdcSAkhil Goyal 	dpaa_qp->rx_errs += nb_ops - num_rx;
1917c3e85bdcSAkhil Goyal 
1918f163231eSHemant Agrawal 	DPAA_SEC_DP_DEBUG("SEC Received %d Packets\n", num_rx);
1919c3e85bdcSAkhil Goyal 
1920c3e85bdcSAkhil Goyal 	return num_rx;
1921c3e85bdcSAkhil Goyal }
1922c3e85bdcSAkhil Goyal 
1923c3e85bdcSAkhil Goyal /** Release queue pair */
1924c3e85bdcSAkhil Goyal static int
1925c3e85bdcSAkhil Goyal dpaa_sec_queue_pair_release(struct rte_cryptodev *dev,
1926c3e85bdcSAkhil Goyal 			    uint16_t qp_id)
1927c3e85bdcSAkhil Goyal {
1928c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals;
1929c3e85bdcSAkhil Goyal 	struct dpaa_sec_qp *qp = NULL;
1930c3e85bdcSAkhil Goyal 
1931c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
1932c3e85bdcSAkhil Goyal 
1933f163231eSHemant Agrawal 	DPAA_SEC_DEBUG("dev =%p, queue =%d", dev, qp_id);
1934c3e85bdcSAkhil Goyal 
1935c3e85bdcSAkhil Goyal 	internals = dev->data->dev_private;
1936c3e85bdcSAkhil Goyal 	if (qp_id >= internals->max_nb_queue_pairs) {
1937f163231eSHemant Agrawal 		DPAA_SEC_ERR("Max supported qpid %d",
1938c3e85bdcSAkhil Goyal 			     internals->max_nb_queue_pairs);
1939c3e85bdcSAkhil Goyal 		return -EINVAL;
1940c3e85bdcSAkhil Goyal 	}
1941c3e85bdcSAkhil Goyal 
1942c3e85bdcSAkhil Goyal 	qp = &internals->qps[qp_id];
19432ffb940eSAkhil Goyal 	rte_mempool_free(qp->ctx_pool);
1944c3e85bdcSAkhil Goyal 	qp->internals = NULL;
1945c3e85bdcSAkhil Goyal 	dev->data->queue_pairs[qp_id] = NULL;
1946c3e85bdcSAkhil Goyal 
1947c3e85bdcSAkhil Goyal 	return 0;
1948c3e85bdcSAkhil Goyal }
1949c3e85bdcSAkhil Goyal 
1950c3e85bdcSAkhil Goyal /** Setup a queue pair */
1951c3e85bdcSAkhil Goyal static int
1952c3e85bdcSAkhil Goyal dpaa_sec_queue_pair_setup(struct rte_cryptodev *dev, uint16_t qp_id,
1953c3e85bdcSAkhil Goyal 		__rte_unused const struct rte_cryptodev_qp_conf *qp_conf,
1954725d2a7fSFan Zhang 		__rte_unused int socket_id)
1955c3e85bdcSAkhil Goyal {
1956c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals;
1957c3e85bdcSAkhil Goyal 	struct dpaa_sec_qp *qp = NULL;
19582ffb940eSAkhil Goyal 	char str[20];
1959c3e85bdcSAkhil Goyal 
1960f163231eSHemant Agrawal 	DPAA_SEC_DEBUG("dev =%p, queue =%d, conf =%p", dev, qp_id, qp_conf);
1961c3e85bdcSAkhil Goyal 
1962c3e85bdcSAkhil Goyal 	internals = dev->data->dev_private;
1963c3e85bdcSAkhil Goyal 	if (qp_id >= internals->max_nb_queue_pairs) {
1964f163231eSHemant Agrawal 		DPAA_SEC_ERR("Max supported qpid %d",
1965c3e85bdcSAkhil Goyal 			     internals->max_nb_queue_pairs);
1966c3e85bdcSAkhil Goyal 		return -EINVAL;
1967c3e85bdcSAkhil Goyal 	}
1968c3e85bdcSAkhil Goyal 
1969c3e85bdcSAkhil Goyal 	qp = &internals->qps[qp_id];
1970c3e85bdcSAkhil Goyal 	qp->internals = internals;
19712ffb940eSAkhil Goyal 	snprintf(str, sizeof(str), "ctx_pool_d%d_qp%d",
19722ffb940eSAkhil Goyal 			dev->data->dev_id, qp_id);
19732ffb940eSAkhil Goyal 	if (!qp->ctx_pool) {
19742ffb940eSAkhil Goyal 		qp->ctx_pool = rte_mempool_create((const char *)str,
19752ffb940eSAkhil Goyal 							CTX_POOL_NUM_BUFS,
19762ffb940eSAkhil Goyal 							CTX_POOL_BUF_SIZE,
19772ffb940eSAkhil Goyal 							CTX_POOL_CACHE_SIZE, 0,
19782ffb940eSAkhil Goyal 							NULL, NULL, NULL, NULL,
19792ffb940eSAkhil Goyal 							SOCKET_ID_ANY, 0);
19802ffb940eSAkhil Goyal 		if (!qp->ctx_pool) {
19812ffb940eSAkhil Goyal 			DPAA_SEC_ERR("%s create failed\n", str);
19822ffb940eSAkhil Goyal 			return -ENOMEM;
19832ffb940eSAkhil Goyal 		}
19842ffb940eSAkhil Goyal 	} else
19852ffb940eSAkhil Goyal 		DPAA_SEC_INFO("mempool already created for dev_id : %d, qp: %d",
19862ffb940eSAkhil Goyal 				dev->data->dev_id, qp_id);
1987c3e85bdcSAkhil Goyal 	dev->data->queue_pairs[qp_id] = qp;
1988c3e85bdcSAkhil Goyal 
1989c3e85bdcSAkhil Goyal 	return 0;
1990c3e85bdcSAkhil Goyal }
1991c3e85bdcSAkhil Goyal 
1992c3e85bdcSAkhil Goyal /** Return the number of allocated queue pairs */
1993c3e85bdcSAkhil Goyal static uint32_t
1994c3e85bdcSAkhil Goyal dpaa_sec_queue_pair_count(struct rte_cryptodev *dev)
1995c3e85bdcSAkhil Goyal {
1996c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
1997c3e85bdcSAkhil Goyal 
1998c3e85bdcSAkhil Goyal 	return dev->data->nb_queue_pairs;
1999c3e85bdcSAkhil Goyal }
2000c3e85bdcSAkhil Goyal 
2001c3e85bdcSAkhil Goyal /** Returns the size of session structure */
2002c3e85bdcSAkhil Goyal static unsigned int
2003012c5076SPablo de Lara dpaa_sec_sym_session_get_size(struct rte_cryptodev *dev __rte_unused)
2004c3e85bdcSAkhil Goyal {
2005c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2006c3e85bdcSAkhil Goyal 
2007c3e85bdcSAkhil Goyal 	return sizeof(dpaa_sec_session);
2008c3e85bdcSAkhil Goyal }
2009c3e85bdcSAkhil Goyal 
2010c3e85bdcSAkhil Goyal static int
2011c3e85bdcSAkhil Goyal dpaa_sec_cipher_init(struct rte_cryptodev *dev __rte_unused,
2012c3e85bdcSAkhil Goyal 		     struct rte_crypto_sym_xform *xform,
2013c3e85bdcSAkhil Goyal 		     dpaa_sec_session *session)
2014c3e85bdcSAkhil Goyal {
2015c3e85bdcSAkhil Goyal 	session->cipher_alg = xform->cipher.algo;
2016c3e85bdcSAkhil Goyal 	session->iv.length = xform->cipher.iv.length;
2017c3e85bdcSAkhil Goyal 	session->iv.offset = xform->cipher.iv.offset;
2018c3e85bdcSAkhil Goyal 	session->cipher_key.data = rte_zmalloc(NULL, xform->cipher.key.length,
2019c3e85bdcSAkhil Goyal 					       RTE_CACHE_LINE_SIZE);
2020c3e85bdcSAkhil Goyal 	if (session->cipher_key.data == NULL && xform->cipher.key.length > 0) {
2021f163231eSHemant Agrawal 		DPAA_SEC_ERR("No Memory for cipher key");
2022c3e85bdcSAkhil Goyal 		return -ENOMEM;
2023c3e85bdcSAkhil Goyal 	}
2024c3e85bdcSAkhil Goyal 	session->cipher_key.length = xform->cipher.key.length;
2025c3e85bdcSAkhil Goyal 
2026c3e85bdcSAkhil Goyal 	memcpy(session->cipher_key.data, xform->cipher.key.data,
2027c3e85bdcSAkhil Goyal 	       xform->cipher.key.length);
2028c3e85bdcSAkhil Goyal 	session->dir = (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
2029c3e85bdcSAkhil Goyal 			DIR_ENC : DIR_DEC;
2030c3e85bdcSAkhil Goyal 
2031c3e85bdcSAkhil Goyal 	return 0;
2032c3e85bdcSAkhil Goyal }
2033c3e85bdcSAkhil Goyal 
2034c3e85bdcSAkhil Goyal static int
2035c3e85bdcSAkhil Goyal dpaa_sec_auth_init(struct rte_cryptodev *dev __rte_unused,
2036c3e85bdcSAkhil Goyal 		   struct rte_crypto_sym_xform *xform,
2037c3e85bdcSAkhil Goyal 		   dpaa_sec_session *session)
2038c3e85bdcSAkhil Goyal {
2039c3e85bdcSAkhil Goyal 	session->auth_alg = xform->auth.algo;
2040c3e85bdcSAkhil Goyal 	session->auth_key.data = rte_zmalloc(NULL, xform->auth.key.length,
2041c3e85bdcSAkhil Goyal 					     RTE_CACHE_LINE_SIZE);
2042c3e85bdcSAkhil Goyal 	if (session->auth_key.data == NULL && xform->auth.key.length > 0) {
2043f163231eSHemant Agrawal 		DPAA_SEC_ERR("No Memory for auth key");
2044c3e85bdcSAkhil Goyal 		return -ENOMEM;
2045c3e85bdcSAkhil Goyal 	}
2046c3e85bdcSAkhil Goyal 	session->auth_key.length = xform->auth.key.length;
2047c3e85bdcSAkhil Goyal 	session->digest_length = xform->auth.digest_length;
2048c3e85bdcSAkhil Goyal 
2049c3e85bdcSAkhil Goyal 	memcpy(session->auth_key.data, xform->auth.key.data,
2050c3e85bdcSAkhil Goyal 	       xform->auth.key.length);
2051c3e85bdcSAkhil Goyal 	session->dir = (xform->auth.op == RTE_CRYPTO_AUTH_OP_GENERATE) ?
2052c3e85bdcSAkhil Goyal 			DIR_ENC : DIR_DEC;
2053c3e85bdcSAkhil Goyal 
2054c3e85bdcSAkhil Goyal 	return 0;
2055c3e85bdcSAkhil Goyal }
2056c3e85bdcSAkhil Goyal 
2057c3e85bdcSAkhil Goyal static int
2058c3e85bdcSAkhil Goyal dpaa_sec_aead_init(struct rte_cryptodev *dev __rte_unused,
2059c3e85bdcSAkhil Goyal 		   struct rte_crypto_sym_xform *xform,
2060c3e85bdcSAkhil Goyal 		   dpaa_sec_session *session)
2061c3e85bdcSAkhil Goyal {
2062c3e85bdcSAkhil Goyal 	session->aead_alg = xform->aead.algo;
2063c3e85bdcSAkhil Goyal 	session->iv.length = xform->aead.iv.length;
2064c3e85bdcSAkhil Goyal 	session->iv.offset = xform->aead.iv.offset;
2065c3e85bdcSAkhil Goyal 	session->auth_only_len = xform->aead.aad_length;
2066c3e85bdcSAkhil Goyal 	session->aead_key.data = rte_zmalloc(NULL, xform->aead.key.length,
2067c3e85bdcSAkhil Goyal 					     RTE_CACHE_LINE_SIZE);
2068c3e85bdcSAkhil Goyal 	if (session->aead_key.data == NULL && xform->aead.key.length > 0) {
2069f163231eSHemant Agrawal 		DPAA_SEC_ERR("No Memory for aead key\n");
2070c3e85bdcSAkhil Goyal 		return -ENOMEM;
2071c3e85bdcSAkhil Goyal 	}
2072c3e85bdcSAkhil Goyal 	session->aead_key.length = xform->aead.key.length;
2073c3e85bdcSAkhil Goyal 	session->digest_length = xform->aead.digest_length;
2074c3e85bdcSAkhil Goyal 
2075c3e85bdcSAkhil Goyal 	memcpy(session->aead_key.data, xform->aead.key.data,
2076c3e85bdcSAkhil Goyal 	       xform->aead.key.length);
2077c3e85bdcSAkhil Goyal 	session->dir = (xform->aead.op == RTE_CRYPTO_AEAD_OP_ENCRYPT) ?
2078c3e85bdcSAkhil Goyal 			DIR_ENC : DIR_DEC;
2079c3e85bdcSAkhil Goyal 
2080c3e85bdcSAkhil Goyal 	return 0;
2081c3e85bdcSAkhil Goyal }
2082c3e85bdcSAkhil Goyal 
2083e79416d1SHemant Agrawal static struct qman_fq *
2084e79416d1SHemant Agrawal dpaa_sec_attach_rxq(struct dpaa_sec_dev_private *qi)
2085c3e85bdcSAkhil Goyal {
2086e79416d1SHemant Agrawal 	unsigned int i;
2087c3e85bdcSAkhil Goyal 
2088e621d970SAkhil Goyal 	for (i = 0; i < qi->max_nb_sessions * MAX_DPAA_CORES; i++) {
2089e79416d1SHemant Agrawal 		if (qi->inq_attach[i] == 0) {
2090e79416d1SHemant Agrawal 			qi->inq_attach[i] = 1;
2091e79416d1SHemant Agrawal 			return &qi->inq[i];
2092e79416d1SHemant Agrawal 		}
2093e79416d1SHemant Agrawal 	}
2094e621d970SAkhil Goyal 	DPAA_SEC_WARN("All session in use %u", qi->max_nb_sessions);
2095c3e85bdcSAkhil Goyal 
2096e79416d1SHemant Agrawal 	return NULL;
2097c3e85bdcSAkhil Goyal }
2098c3e85bdcSAkhil Goyal 
2099e79416d1SHemant Agrawal static int
2100e79416d1SHemant Agrawal dpaa_sec_detach_rxq(struct dpaa_sec_dev_private *qi, struct qman_fq *fq)
2101e79416d1SHemant Agrawal {
2102e79416d1SHemant Agrawal 	unsigned int i;
2103e79416d1SHemant Agrawal 
2104e79416d1SHemant Agrawal 	for (i = 0; i < qi->max_nb_sessions; i++) {
2105e79416d1SHemant Agrawal 		if (&qi->inq[i] == fq) {
2106b4053c4bSAlok Makhariya 			qman_retire_fq(fq, NULL);
2107b4053c4bSAlok Makhariya 			qman_oos_fq(fq);
2108e79416d1SHemant Agrawal 			qi->inq_attach[i] = 0;
2109e79416d1SHemant Agrawal 			return 0;
2110e79416d1SHemant Agrawal 		}
2111e79416d1SHemant Agrawal 	}
2112e79416d1SHemant Agrawal 	return -1;
2113e79416d1SHemant Agrawal }
2114e79416d1SHemant Agrawal 
2115e79416d1SHemant Agrawal static int
2116e79416d1SHemant Agrawal dpaa_sec_attach_sess_q(struct dpaa_sec_qp *qp, dpaa_sec_session *sess)
2117e79416d1SHemant Agrawal {
2118e79416d1SHemant Agrawal 	int ret;
2119e79416d1SHemant Agrawal 
21204e694fe5SAkhil Goyal 	sess->qp[rte_lcore_id() % MAX_DPAA_CORES] = qp;
2121e79416d1SHemant Agrawal 	ret = dpaa_sec_prep_cdb(sess);
2122e79416d1SHemant Agrawal 	if (ret) {
2123f163231eSHemant Agrawal 		DPAA_SEC_ERR("Unable to prepare sec cdb");
2124e79416d1SHemant Agrawal 		return -1;
2125e79416d1SHemant Agrawal 	}
21265b0f1bd3SAshish Jain 	if (unlikely(!RTE_PER_LCORE(dpaa_io))) {
21275b0f1bd3SAshish Jain 		ret = rte_dpaa_portal_init((void *)0);
21285b0f1bd3SAshish Jain 		if (ret) {
2129f163231eSHemant Agrawal 			DPAA_SEC_ERR("Failure in affining portal");
21305b0f1bd3SAshish Jain 			return ret;
21315b0f1bd3SAshish Jain 		}
21325b0f1bd3SAshish Jain 	}
21334e694fe5SAkhil Goyal 	ret = dpaa_sec_init_rx(sess->inq[rte_lcore_id() % MAX_DPAA_CORES],
21344e694fe5SAkhil Goyal 			       dpaa_mem_vtop(&sess->cdb),
2135e79416d1SHemant Agrawal 			       qman_fq_fqid(&qp->outq));
2136e79416d1SHemant Agrawal 	if (ret)
2137f163231eSHemant Agrawal 		DPAA_SEC_ERR("Unable to init sec queue");
2138e79416d1SHemant Agrawal 
2139e79416d1SHemant Agrawal 	return ret;
2140c3e85bdcSAkhil Goyal }
2141c3e85bdcSAkhil Goyal 
2142c3e85bdcSAkhil Goyal static int
2143c3e85bdcSAkhil Goyal dpaa_sec_set_session_parameters(struct rte_cryptodev *dev,
2144c3e85bdcSAkhil Goyal 			    struct rte_crypto_sym_xform *xform,	void *sess)
2145c3e85bdcSAkhil Goyal {
2146c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals = dev->data->dev_private;
2147c3e85bdcSAkhil Goyal 	dpaa_sec_session *session = sess;
21484e694fe5SAkhil Goyal 	uint32_t i;
2149c3e85bdcSAkhil Goyal 
2150c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2151c3e85bdcSAkhil Goyal 
2152c3e85bdcSAkhil Goyal 	if (unlikely(sess == NULL)) {
2153f163231eSHemant Agrawal 		DPAA_SEC_ERR("invalid session struct");
2154c3e85bdcSAkhil Goyal 		return -EINVAL;
2155c3e85bdcSAkhil Goyal 	}
2156b0894102SAkhil Goyal 	memset(session, 0, sizeof(dpaa_sec_session));
2157c3e85bdcSAkhil Goyal 
2158c3e85bdcSAkhil Goyal 	/* Default IV length = 0 */
2159c3e85bdcSAkhil Goyal 	session->iv.length = 0;
2160c3e85bdcSAkhil Goyal 
2161c3e85bdcSAkhil Goyal 	/* Cipher Only */
2162c3e85bdcSAkhil Goyal 	if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER && xform->next == NULL) {
2163c3e85bdcSAkhil Goyal 		session->auth_alg = RTE_CRYPTO_AUTH_NULL;
2164c3e85bdcSAkhil Goyal 		dpaa_sec_cipher_init(dev, xform, session);
2165c3e85bdcSAkhil Goyal 
2166c3e85bdcSAkhil Goyal 	/* Authentication Only */
2167c3e85bdcSAkhil Goyal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
2168c3e85bdcSAkhil Goyal 		   xform->next == NULL) {
2169c3e85bdcSAkhil Goyal 		session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
2170c3e85bdcSAkhil Goyal 		dpaa_sec_auth_init(dev, xform, session);
2171c3e85bdcSAkhil Goyal 
2172c3e85bdcSAkhil Goyal 	/* Cipher then Authenticate */
2173c3e85bdcSAkhil Goyal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
2174c3e85bdcSAkhil Goyal 		   xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
2175c3e85bdcSAkhil Goyal 		if (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) {
2176c3e85bdcSAkhil Goyal 			dpaa_sec_cipher_init(dev, xform, session);
2177c3e85bdcSAkhil Goyal 			dpaa_sec_auth_init(dev, xform->next, session);
2178c3e85bdcSAkhil Goyal 		} else {
2179f163231eSHemant Agrawal 			DPAA_SEC_ERR("Not supported: Auth then Cipher");
2180c3e85bdcSAkhil Goyal 			return -EINVAL;
2181c3e85bdcSAkhil Goyal 		}
2182c3e85bdcSAkhil Goyal 
2183c3e85bdcSAkhil Goyal 	/* Authenticate then Cipher */
2184c3e85bdcSAkhil Goyal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
2185c3e85bdcSAkhil Goyal 		   xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
2186c3e85bdcSAkhil Goyal 		if (xform->next->cipher.op == RTE_CRYPTO_CIPHER_OP_DECRYPT) {
2187c3e85bdcSAkhil Goyal 			dpaa_sec_auth_init(dev, xform, session);
2188c3e85bdcSAkhil Goyal 			dpaa_sec_cipher_init(dev, xform->next, session);
2189c3e85bdcSAkhil Goyal 		} else {
2190f163231eSHemant Agrawal 			DPAA_SEC_ERR("Not supported: Auth then Cipher");
2191c3e85bdcSAkhil Goyal 			return -EINVAL;
2192c3e85bdcSAkhil Goyal 		}
2193c3e85bdcSAkhil Goyal 
2194c3e85bdcSAkhil Goyal 	/* AEAD operation for AES-GCM kind of Algorithms */
2195c3e85bdcSAkhil Goyal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD &&
2196c3e85bdcSAkhil Goyal 		   xform->next == NULL) {
2197c3e85bdcSAkhil Goyal 		dpaa_sec_aead_init(dev, xform, session);
2198c3e85bdcSAkhil Goyal 
2199c3e85bdcSAkhil Goyal 	} else {
2200f163231eSHemant Agrawal 		DPAA_SEC_ERR("Invalid crypto type");
2201c3e85bdcSAkhil Goyal 		return -EINVAL;
2202c3e85bdcSAkhil Goyal 	}
22033b617ee7SAkhil Goyal 	rte_spinlock_lock(&internals->lock);
22044e694fe5SAkhil Goyal 	for (i = 0; i < MAX_DPAA_CORES; i++) {
22054e694fe5SAkhil Goyal 		session->inq[i] = dpaa_sec_attach_rxq(internals);
22064e694fe5SAkhil Goyal 		if (session->inq[i] == NULL) {
2207f163231eSHemant Agrawal 			DPAA_SEC_ERR("unable to attach sec queue");
22084e694fe5SAkhil Goyal 			rte_spinlock_unlock(&internals->lock);
2209e79416d1SHemant Agrawal 			goto err1;
2210e79416d1SHemant Agrawal 		}
22114e694fe5SAkhil Goyal 	}
22124e694fe5SAkhil Goyal 	rte_spinlock_unlock(&internals->lock);
2213c3e85bdcSAkhil Goyal 
2214c3e85bdcSAkhil Goyal 	return 0;
2215e79416d1SHemant Agrawal 
2216e79416d1SHemant Agrawal err1:
2217e79416d1SHemant Agrawal 	rte_free(session->cipher_key.data);
2218e79416d1SHemant Agrawal 	rte_free(session->auth_key.data);
2219e79416d1SHemant Agrawal 	memset(session, 0, sizeof(dpaa_sec_session));
2220e79416d1SHemant Agrawal 
2221e79416d1SHemant Agrawal 	return -EINVAL;
2222c3e85bdcSAkhil Goyal }
2223c3e85bdcSAkhil Goyal 
2224c3e85bdcSAkhil Goyal static int
2225012c5076SPablo de Lara dpaa_sec_sym_session_configure(struct rte_cryptodev *dev,
2226c3e85bdcSAkhil Goyal 		struct rte_crypto_sym_xform *xform,
2227c3e85bdcSAkhil Goyal 		struct rte_cryptodev_sym_session *sess,
2228c3e85bdcSAkhil Goyal 		struct rte_mempool *mempool)
2229c3e85bdcSAkhil Goyal {
2230c3e85bdcSAkhil Goyal 	void *sess_private_data;
2231c3e85bdcSAkhil Goyal 	int ret;
2232c3e85bdcSAkhil Goyal 
2233c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2234c3e85bdcSAkhil Goyal 
2235c3e85bdcSAkhil Goyal 	if (rte_mempool_get(mempool, &sess_private_data)) {
2236f163231eSHemant Agrawal 		DPAA_SEC_ERR("Couldn't get object from session mempool");
2237c3e85bdcSAkhil Goyal 		return -ENOMEM;
2238c3e85bdcSAkhil Goyal 	}
2239c3e85bdcSAkhil Goyal 
2240c3e85bdcSAkhil Goyal 	ret = dpaa_sec_set_session_parameters(dev, xform, sess_private_data);
2241c3e85bdcSAkhil Goyal 	if (ret != 0) {
2242f163231eSHemant Agrawal 		DPAA_SEC_ERR("failed to configure session parameters");
2243c3e85bdcSAkhil Goyal 
2244c3e85bdcSAkhil Goyal 		/* Return session to mempool */
2245c3e85bdcSAkhil Goyal 		rte_mempool_put(mempool, sess_private_data);
2246c3e85bdcSAkhil Goyal 		return ret;
2247c3e85bdcSAkhil Goyal 	}
2248c3e85bdcSAkhil Goyal 
2249012c5076SPablo de Lara 	set_sym_session_private_data(sess, dev->driver_id,
2250c3e85bdcSAkhil Goyal 			sess_private_data);
2251c3e85bdcSAkhil Goyal 
2252e79416d1SHemant Agrawal 
2253c3e85bdcSAkhil Goyal 	return 0;
2254c3e85bdcSAkhil Goyal }
2255c3e85bdcSAkhil Goyal 
22563d0d5332SAkhil Goyal static inline void
22573d0d5332SAkhil Goyal free_session_memory(struct rte_cryptodev *dev, dpaa_sec_session *s)
2258c3e85bdcSAkhil Goyal {
2259e79416d1SHemant Agrawal 	struct dpaa_sec_dev_private *qi = dev->data->dev_private;
22603d0d5332SAkhil Goyal 	struct rte_mempool *sess_mp = rte_mempool_from_obj((void *)s);
22613d0d5332SAkhil Goyal 	uint8_t i;
2262e79416d1SHemant Agrawal 
2263e621d970SAkhil Goyal 	for (i = 0; i < MAX_DPAA_CORES; i++) {
2264e621d970SAkhil Goyal 		if (s->inq[i])
2265e621d970SAkhil Goyal 			dpaa_sec_detach_rxq(qi, s->inq[i]);
2266e621d970SAkhil Goyal 		s->inq[i] = NULL;
2267e621d970SAkhil Goyal 		s->qp[i] = NULL;
2268e621d970SAkhil Goyal 	}
2269c3e85bdcSAkhil Goyal 	rte_free(s->cipher_key.data);
2270c3e85bdcSAkhil Goyal 	rte_free(s->auth_key.data);
2271c3e85bdcSAkhil Goyal 	memset(s, 0, sizeof(dpaa_sec_session));
22723d0d5332SAkhil Goyal 	rte_mempool_put(sess_mp, (void *)s);
22733d0d5332SAkhil Goyal }
22743d0d5332SAkhil Goyal 
22753d0d5332SAkhil Goyal /** Clear the memory of session so it doesn't leave key material behind */
22763d0d5332SAkhil Goyal static void
22773d0d5332SAkhil Goyal dpaa_sec_sym_session_clear(struct rte_cryptodev *dev,
22783d0d5332SAkhil Goyal 		struct rte_cryptodev_sym_session *sess)
22793d0d5332SAkhil Goyal {
22803d0d5332SAkhil Goyal 	PMD_INIT_FUNC_TRACE();
22813d0d5332SAkhil Goyal 	uint8_t index = dev->driver_id;
22823d0d5332SAkhil Goyal 	void *sess_priv = get_sym_session_private_data(sess, index);
22833d0d5332SAkhil Goyal 	dpaa_sec_session *s = (dpaa_sec_session *)sess_priv;
22843d0d5332SAkhil Goyal 
22853d0d5332SAkhil Goyal 	if (sess_priv) {
22863d0d5332SAkhil Goyal 		free_session_memory(dev, s);
2287012c5076SPablo de Lara 		set_sym_session_private_data(sess, index, NULL);
2288c3e85bdcSAkhil Goyal 	}
2289c3e85bdcSAkhil Goyal }
2290c3e85bdcSAkhil Goyal 
2291c3e85bdcSAkhil Goyal static int
22921f14d500SAkhil Goyal dpaa_sec_set_ipsec_session(__rte_unused struct rte_cryptodev *dev,
22931f14d500SAkhil Goyal 			   struct rte_security_session_conf *conf,
22941f14d500SAkhil Goyal 			   void *sess)
22951f14d500SAkhil Goyal {
22961f14d500SAkhil Goyal 	struct dpaa_sec_dev_private *internals = dev->data->dev_private;
22971f14d500SAkhil Goyal 	struct rte_security_ipsec_xform *ipsec_xform = &conf->ipsec;
229805b12700SHemant Agrawal 	struct rte_crypto_auth_xform *auth_xform = NULL;
229905b12700SHemant Agrawal 	struct rte_crypto_cipher_xform *cipher_xform = NULL;
23001f14d500SAkhil Goyal 	dpaa_sec_session *session = (dpaa_sec_session *)sess;
23014e694fe5SAkhil Goyal 	uint32_t i;
23021f14d500SAkhil Goyal 
23031f14d500SAkhil Goyal 	PMD_INIT_FUNC_TRACE();
23041f14d500SAkhil Goyal 
2305b0894102SAkhil Goyal 	memset(session, 0, sizeof(dpaa_sec_session));
23061f14d500SAkhil Goyal 	if (ipsec_xform->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
23071f14d500SAkhil Goyal 		cipher_xform = &conf->crypto_xform->cipher;
230805b12700SHemant Agrawal 		if (conf->crypto_xform->next)
23091f14d500SAkhil Goyal 			auth_xform = &conf->crypto_xform->next->auth;
23101f14d500SAkhil Goyal 	} else {
23111f14d500SAkhil Goyal 		auth_xform = &conf->crypto_xform->auth;
231205b12700SHemant Agrawal 		if (conf->crypto_xform->next)
23131f14d500SAkhil Goyal 			cipher_xform = &conf->crypto_xform->next->cipher;
23141f14d500SAkhil Goyal 	}
23151f14d500SAkhil Goyal 	session->proto_alg = conf->protocol;
231605b12700SHemant Agrawal 
231705b12700SHemant Agrawal 	if (cipher_xform && cipher_xform->algo != RTE_CRYPTO_CIPHER_NULL) {
23181f14d500SAkhil Goyal 		session->cipher_key.data = rte_zmalloc(NULL,
23191f14d500SAkhil Goyal 						       cipher_xform->key.length,
23201f14d500SAkhil Goyal 						       RTE_CACHE_LINE_SIZE);
23211f14d500SAkhil Goyal 		if (session->cipher_key.data == NULL &&
23221f14d500SAkhil Goyal 				cipher_xform->key.length > 0) {
2323f163231eSHemant Agrawal 			DPAA_SEC_ERR("No Memory for cipher key");
23241f14d500SAkhil Goyal 			return -ENOMEM;
23251f14d500SAkhil Goyal 		}
232605b12700SHemant Agrawal 		memcpy(session->cipher_key.data, cipher_xform->key.data,
232705b12700SHemant Agrawal 				cipher_xform->key.length);
23281f14d500SAkhil Goyal 		session->cipher_key.length = cipher_xform->key.length;
232905b12700SHemant Agrawal 
233005b12700SHemant Agrawal 		switch (cipher_xform->algo) {
233105b12700SHemant Agrawal 		case RTE_CRYPTO_CIPHER_AES_CBC:
233205b12700SHemant Agrawal 		case RTE_CRYPTO_CIPHER_3DES_CBC:
233305b12700SHemant Agrawal 		case RTE_CRYPTO_CIPHER_AES_CTR:
233405b12700SHemant Agrawal 			break;
233505b12700SHemant Agrawal 		default:
233605b12700SHemant Agrawal 			DPAA_SEC_ERR("Crypto: Unsupported Cipher alg %u",
233705b12700SHemant Agrawal 				cipher_xform->algo);
233805b12700SHemant Agrawal 			goto out;
233905b12700SHemant Agrawal 		}
234005b12700SHemant Agrawal 		session->cipher_alg = cipher_xform->algo;
234105b12700SHemant Agrawal 	} else {
234205b12700SHemant Agrawal 		session->cipher_key.data = NULL;
234305b12700SHemant Agrawal 		session->cipher_key.length = 0;
234405b12700SHemant Agrawal 		session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
234505b12700SHemant Agrawal 	}
234605b12700SHemant Agrawal 
234705b12700SHemant Agrawal 	if (auth_xform && auth_xform->algo != RTE_CRYPTO_AUTH_NULL) {
23481f14d500SAkhil Goyal 		session->auth_key.data = rte_zmalloc(NULL,
23491f14d500SAkhil Goyal 						auth_xform->key.length,
23501f14d500SAkhil Goyal 						RTE_CACHE_LINE_SIZE);
23511f14d500SAkhil Goyal 		if (session->auth_key.data == NULL &&
23521f14d500SAkhil Goyal 				auth_xform->key.length > 0) {
2353f163231eSHemant Agrawal 			DPAA_SEC_ERR("No Memory for auth key");
23541f14d500SAkhil Goyal 			rte_free(session->cipher_key.data);
23551f14d500SAkhil Goyal 			return -ENOMEM;
23561f14d500SAkhil Goyal 		}
23571f14d500SAkhil Goyal 		memcpy(session->auth_key.data, auth_xform->key.data,
23581f14d500SAkhil Goyal 				auth_xform->key.length);
235905b12700SHemant Agrawal 		session->auth_key.length = auth_xform->key.length;
23601f14d500SAkhil Goyal 
23611f14d500SAkhil Goyal 		switch (auth_xform->algo) {
23621f14d500SAkhil Goyal 		case RTE_CRYPTO_AUTH_SHA1_HMAC:
23631f14d500SAkhil Goyal 		case RTE_CRYPTO_AUTH_MD5_HMAC:
23641f14d500SAkhil Goyal 		case RTE_CRYPTO_AUTH_SHA256_HMAC:
23651f14d500SAkhil Goyal 		case RTE_CRYPTO_AUTH_SHA384_HMAC:
23661f14d500SAkhil Goyal 		case RTE_CRYPTO_AUTH_SHA512_HMAC:
23671f14d500SAkhil Goyal 		case RTE_CRYPTO_AUTH_AES_CMAC:
23681f14d500SAkhil Goyal 			break;
236905b12700SHemant Agrawal 		default:
2370f163231eSHemant Agrawal 			DPAA_SEC_ERR("Crypto: Unsupported auth alg %u",
23711f14d500SAkhil Goyal 				auth_xform->algo);
23721f14d500SAkhil Goyal 			goto out;
23731f14d500SAkhil Goyal 		}
237405b12700SHemant Agrawal 		session->auth_alg = auth_xform->algo;
237505b12700SHemant Agrawal 	} else {
237605b12700SHemant Agrawal 		session->auth_key.data = NULL;
237705b12700SHemant Agrawal 		session->auth_key.length = 0;
237805b12700SHemant Agrawal 		session->auth_alg = RTE_CRYPTO_AUTH_NULL;
23791f14d500SAkhil Goyal 	}
23801f14d500SAkhil Goyal 
23811f14d500SAkhil Goyal 	if (ipsec_xform->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
23825ab35d2eSAkhil Goyal 		if (ipsec_xform->tunnel.type ==
23835ab35d2eSAkhil Goyal 				RTE_SECURITY_IPSEC_TUNNEL_IPV4) {
23845ab35d2eSAkhil Goyal 			memset(&session->encap_pdb, 0,
23855ab35d2eSAkhil Goyal 				sizeof(struct ipsec_encap_pdb) +
23861f14d500SAkhil Goyal 				sizeof(session->ip4_hdr));
23871f14d500SAkhil Goyal 			session->ip4_hdr.ip_v = IPVERSION;
23881f14d500SAkhil Goyal 			session->ip4_hdr.ip_hl = 5;
23891f14d500SAkhil Goyal 			session->ip4_hdr.ip_len = rte_cpu_to_be_16(
23901f14d500SAkhil Goyal 						sizeof(session->ip4_hdr));
23911f14d500SAkhil Goyal 			session->ip4_hdr.ip_tos = ipsec_xform->tunnel.ipv4.dscp;
23921f14d500SAkhil Goyal 			session->ip4_hdr.ip_id = 0;
23931f14d500SAkhil Goyal 			session->ip4_hdr.ip_off = 0;
23941f14d500SAkhil Goyal 			session->ip4_hdr.ip_ttl = ipsec_xform->tunnel.ipv4.ttl;
23951f14d500SAkhil Goyal 			session->ip4_hdr.ip_p = (ipsec_xform->proto ==
23965ab35d2eSAkhil Goyal 					RTE_SECURITY_IPSEC_SA_PROTO_ESP) ?
23975ab35d2eSAkhil Goyal 					IPPROTO_ESP : IPPROTO_AH;
23981f14d500SAkhil Goyal 			session->ip4_hdr.ip_sum = 0;
23995ab35d2eSAkhil Goyal 			session->ip4_hdr.ip_src =
24005ab35d2eSAkhil Goyal 					ipsec_xform->tunnel.ipv4.src_ip;
24015ab35d2eSAkhil Goyal 			session->ip4_hdr.ip_dst =
24025ab35d2eSAkhil Goyal 					ipsec_xform->tunnel.ipv4.dst_ip;
24031f14d500SAkhil Goyal 			session->ip4_hdr.ip_sum = calc_chksum((uint16_t *)
24041f14d500SAkhil Goyal 						(void *)&session->ip4_hdr,
24051f14d500SAkhil Goyal 						sizeof(struct ip));
24065ab35d2eSAkhil Goyal 			session->encap_pdb.ip_hdr_len = sizeof(struct ip);
24075ab35d2eSAkhil Goyal 		} else if (ipsec_xform->tunnel.type ==
24085ab35d2eSAkhil Goyal 				RTE_SECURITY_IPSEC_TUNNEL_IPV6) {
24095ab35d2eSAkhil Goyal 			memset(&session->encap_pdb, 0,
24105ab35d2eSAkhil Goyal 				sizeof(struct ipsec_encap_pdb) +
24115ab35d2eSAkhil Goyal 				sizeof(session->ip6_hdr));
24125ab35d2eSAkhil Goyal 			session->ip6_hdr.vtc_flow = rte_cpu_to_be_32(
24135ab35d2eSAkhil Goyal 				DPAA_IPv6_DEFAULT_VTC_FLOW |
24145ab35d2eSAkhil Goyal 				((ipsec_xform->tunnel.ipv6.dscp <<
24155ab35d2eSAkhil Goyal 					RTE_IPV6_HDR_TC_SHIFT) &
24165ab35d2eSAkhil Goyal 					RTE_IPV6_HDR_TC_MASK) |
24175ab35d2eSAkhil Goyal 				((ipsec_xform->tunnel.ipv6.flabel <<
24185ab35d2eSAkhil Goyal 					RTE_IPV6_HDR_FL_SHIFT) &
24195ab35d2eSAkhil Goyal 					RTE_IPV6_HDR_FL_MASK));
24205ab35d2eSAkhil Goyal 			/* Payload length will be updated by HW */
24215ab35d2eSAkhil Goyal 			session->ip6_hdr.payload_len = 0;
24225ab35d2eSAkhil Goyal 			session->ip6_hdr.hop_limits =
24235ab35d2eSAkhil Goyal 					ipsec_xform->tunnel.ipv6.hlimit;
24245ab35d2eSAkhil Goyal 			session->ip6_hdr.proto = (ipsec_xform->proto ==
24255ab35d2eSAkhil Goyal 					RTE_SECURITY_IPSEC_SA_PROTO_ESP) ?
24265ab35d2eSAkhil Goyal 					IPPROTO_ESP : IPPROTO_AH;
24275ab35d2eSAkhil Goyal 			memcpy(&session->ip6_hdr.src_addr,
24285ab35d2eSAkhil Goyal 					&ipsec_xform->tunnel.ipv6.src_addr, 16);
24295ab35d2eSAkhil Goyal 			memcpy(&session->ip6_hdr.dst_addr,
24305ab35d2eSAkhil Goyal 					&ipsec_xform->tunnel.ipv6.dst_addr, 16);
24315ab35d2eSAkhil Goyal 			session->encap_pdb.ip_hdr_len =
24325ab35d2eSAkhil Goyal 						sizeof(struct rte_ipv6_hdr);
24335ab35d2eSAkhil Goyal 		}
24341f14d500SAkhil Goyal 		session->encap_pdb.options =
24351f14d500SAkhil Goyal 			(IPVERSION << PDBNH_ESP_ENCAP_SHIFT) |
24361f14d500SAkhil Goyal 			PDBOPTS_ESP_OIHI_PDB_INL |
24371f14d500SAkhil Goyal 			PDBOPTS_ESP_IVSRC |
243879fde9d0SAkhil Goyal 			PDBHMO_ESP_ENCAP_DTTL |
243979fde9d0SAkhil Goyal 			PDBHMO_ESP_SNR;
24400f318781SAkhil Goyal 		if (ipsec_xform->options.esn)
24410f318781SAkhil Goyal 			session->encap_pdb.options |= PDBOPTS_ESP_ESN;
24421f14d500SAkhil Goyal 		session->encap_pdb.spi = ipsec_xform->spi;
24431f14d500SAkhil Goyal 		session->dir = DIR_ENC;
24441f14d500SAkhil Goyal 	} else if (ipsec_xform->direction ==
24451f14d500SAkhil Goyal 			RTE_SECURITY_IPSEC_SA_DIR_INGRESS) {
24461f14d500SAkhil Goyal 		memset(&session->decap_pdb, 0, sizeof(struct ipsec_decap_pdb));
24475ab35d2eSAkhil Goyal 		if (ipsec_xform->tunnel.type == RTE_SECURITY_IPSEC_TUNNEL_IPV4)
24481f14d500SAkhil Goyal 			session->decap_pdb.options = sizeof(struct ip) << 16;
24495ab35d2eSAkhil Goyal 		else
24505ab35d2eSAkhil Goyal 			session->decap_pdb.options =
24515ab35d2eSAkhil Goyal 					sizeof(struct rte_ipv6_hdr) << 16;
24520f318781SAkhil Goyal 		if (ipsec_xform->options.esn)
24530f318781SAkhil Goyal 			session->decap_pdb.options |= PDBOPTS_ESP_ESN;
24541f14d500SAkhil Goyal 		session->dir = DIR_DEC;
24551f14d500SAkhil Goyal 	} else
24561f14d500SAkhil Goyal 		goto out;
24573b617ee7SAkhil Goyal 	rte_spinlock_lock(&internals->lock);
24584e694fe5SAkhil Goyal 	for (i = 0; i < MAX_DPAA_CORES; i++) {
24594e694fe5SAkhil Goyal 		session->inq[i] = dpaa_sec_attach_rxq(internals);
24604e694fe5SAkhil Goyal 		if (session->inq[i] == NULL) {
2461f163231eSHemant Agrawal 			DPAA_SEC_ERR("unable to attach sec queue");
24624e694fe5SAkhil Goyal 			rte_spinlock_unlock(&internals->lock);
24631f14d500SAkhil Goyal 			goto out;
24641f14d500SAkhil Goyal 		}
24654e694fe5SAkhil Goyal 	}
24664e694fe5SAkhil Goyal 	rte_spinlock_unlock(&internals->lock);
24671f14d500SAkhil Goyal 
2468a1173d55SHemant Agrawal 	return 0;
2469a1173d55SHemant Agrawal out:
2470a1173d55SHemant Agrawal 	rte_free(session->auth_key.data);
2471a1173d55SHemant Agrawal 	rte_free(session->cipher_key.data);
2472a1173d55SHemant Agrawal 	memset(session, 0, sizeof(dpaa_sec_session));
2473a1173d55SHemant Agrawal 	return -1;
2474a1173d55SHemant Agrawal }
24751f14d500SAkhil Goyal 
2476a1173d55SHemant Agrawal static int
2477a1173d55SHemant Agrawal dpaa_sec_set_pdcp_session(struct rte_cryptodev *dev,
2478a1173d55SHemant Agrawal 			  struct rte_security_session_conf *conf,
2479a1173d55SHemant Agrawal 			  void *sess)
2480a1173d55SHemant Agrawal {
2481a1173d55SHemant Agrawal 	struct rte_security_pdcp_xform *pdcp_xform = &conf->pdcp;
2482a1173d55SHemant Agrawal 	struct rte_crypto_sym_xform *xform = conf->crypto_xform;
2483a1173d55SHemant Agrawal 	struct rte_crypto_auth_xform *auth_xform = NULL;
2484a1173d55SHemant Agrawal 	struct rte_crypto_cipher_xform *cipher_xform = NULL;
2485a1173d55SHemant Agrawal 	dpaa_sec_session *session = (dpaa_sec_session *)sess;
2486a1173d55SHemant Agrawal 	struct dpaa_sec_dev_private *dev_priv = dev->data->dev_private;
24874e694fe5SAkhil Goyal 	uint32_t i;
2488a1173d55SHemant Agrawal 
2489a1173d55SHemant Agrawal 	PMD_INIT_FUNC_TRACE();
2490a1173d55SHemant Agrawal 
2491a1173d55SHemant Agrawal 	memset(session, 0, sizeof(dpaa_sec_session));
2492a1173d55SHemant Agrawal 
2493a1173d55SHemant Agrawal 	/* find xfrm types */
2494a1173d55SHemant Agrawal 	if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
2495a1173d55SHemant Agrawal 		cipher_xform = &xform->cipher;
2496a1173d55SHemant Agrawal 		if (xform->next != NULL)
2497a1173d55SHemant Agrawal 			auth_xform = &xform->next->auth;
2498a1173d55SHemant Agrawal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
2499a1173d55SHemant Agrawal 		auth_xform = &xform->auth;
2500a1173d55SHemant Agrawal 		if (xform->next != NULL)
2501a1173d55SHemant Agrawal 			cipher_xform = &xform->next->cipher;
2502a1173d55SHemant Agrawal 	} else {
2503a1173d55SHemant Agrawal 		DPAA_SEC_ERR("Invalid crypto type");
2504a1173d55SHemant Agrawal 		return -EINVAL;
2505a1173d55SHemant Agrawal 	}
2506a1173d55SHemant Agrawal 
2507a1173d55SHemant Agrawal 	session->proto_alg = conf->protocol;
2508a1173d55SHemant Agrawal 	if (cipher_xform) {
2509a1173d55SHemant Agrawal 		session->cipher_key.data = rte_zmalloc(NULL,
2510a1173d55SHemant Agrawal 					       cipher_xform->key.length,
2511a1173d55SHemant Agrawal 					       RTE_CACHE_LINE_SIZE);
2512a1173d55SHemant Agrawal 		if (session->cipher_key.data == NULL &&
2513a1173d55SHemant Agrawal 				cipher_xform->key.length > 0) {
2514a1173d55SHemant Agrawal 			DPAA_SEC_ERR("No Memory for cipher key");
2515a1173d55SHemant Agrawal 			return -ENOMEM;
2516a1173d55SHemant Agrawal 		}
2517a1173d55SHemant Agrawal 		session->cipher_key.length = cipher_xform->key.length;
2518a1173d55SHemant Agrawal 		memcpy(session->cipher_key.data, cipher_xform->key.data,
2519a1173d55SHemant Agrawal 			cipher_xform->key.length);
2520a1173d55SHemant Agrawal 		session->dir = (cipher_xform->op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
2521a1173d55SHemant Agrawal 					DIR_ENC : DIR_DEC;
2522a1173d55SHemant Agrawal 		session->cipher_alg = cipher_xform->algo;
2523a1173d55SHemant Agrawal 	} else {
2524a1173d55SHemant Agrawal 		session->cipher_key.data = NULL;
2525a1173d55SHemant Agrawal 		session->cipher_key.length = 0;
2526a1173d55SHemant Agrawal 		session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
2527a1173d55SHemant Agrawal 		session->dir = DIR_ENC;
2528a1173d55SHemant Agrawal 	}
2529a1173d55SHemant Agrawal 
2530a1173d55SHemant Agrawal 	if (pdcp_xform->domain == RTE_SECURITY_PDCP_MODE_CONTROL) {
2531eac60082SVakul Garg 		if (pdcp_xform->sn_size != RTE_SECURITY_PDCP_SN_SIZE_5 &&
2532eac60082SVakul Garg 		    pdcp_xform->sn_size != RTE_SECURITY_PDCP_SN_SIZE_12) {
2533a1173d55SHemant Agrawal 			DPAA_SEC_ERR(
2534eac60082SVakul Garg 				"PDCP Seq Num size should be 5/12 bits for cmode");
2535a1173d55SHemant Agrawal 			goto out;
2536a1173d55SHemant Agrawal 		}
25372e4cbdb4SVakul Garg 	}
25382e4cbdb4SVakul Garg 
2539a1173d55SHemant Agrawal 	if (auth_xform) {
2540a1173d55SHemant Agrawal 		session->auth_key.data = rte_zmalloc(NULL,
2541a1173d55SHemant Agrawal 						     auth_xform->key.length,
2542a1173d55SHemant Agrawal 						     RTE_CACHE_LINE_SIZE);
25432e4cbdb4SVakul Garg 		if (!session->auth_key.data &&
2544a1173d55SHemant Agrawal 		    auth_xform->key.length > 0) {
2545a1173d55SHemant Agrawal 			DPAA_SEC_ERR("No Memory for auth key");
2546a1173d55SHemant Agrawal 			rte_free(session->cipher_key.data);
2547a1173d55SHemant Agrawal 			return -ENOMEM;
2548a1173d55SHemant Agrawal 		}
2549a1173d55SHemant Agrawal 		session->auth_key.length = auth_xform->key.length;
2550a1173d55SHemant Agrawal 		memcpy(session->auth_key.data, auth_xform->key.data,
2551a1173d55SHemant Agrawal 		       auth_xform->key.length);
2552a1173d55SHemant Agrawal 		session->auth_alg = auth_xform->algo;
2553a1173d55SHemant Agrawal 	} else {
2554a1173d55SHemant Agrawal 		session->auth_key.data = NULL;
2555a1173d55SHemant Agrawal 		session->auth_key.length = 0;
25562e4cbdb4SVakul Garg 		session->auth_alg = 0;
2557a1173d55SHemant Agrawal 	}
2558a1173d55SHemant Agrawal 	session->pdcp.domain = pdcp_xform->domain;
2559a1173d55SHemant Agrawal 	session->pdcp.bearer = pdcp_xform->bearer;
2560a1173d55SHemant Agrawal 	session->pdcp.pkt_dir = pdcp_xform->pkt_dir;
2561a1173d55SHemant Agrawal 	session->pdcp.sn_size = pdcp_xform->sn_size;
2562a1173d55SHemant Agrawal 	session->pdcp.hfn = pdcp_xform->hfn;
2563a1173d55SHemant Agrawal 	session->pdcp.hfn_threshold = pdcp_xform->hfn_threshold;
25646a0c9d36SAkhil Goyal 	session->pdcp.hfn_ovd = pdcp_xform->hfn_ovrd;
25656a0c9d36SAkhil Goyal 	session->pdcp.hfn_ovd_offset = cipher_xform->iv.offset;
2566a1173d55SHemant Agrawal 
2567a1173d55SHemant Agrawal 	rte_spinlock_lock(&dev_priv->lock);
25684e694fe5SAkhil Goyal 	for (i = 0; i < MAX_DPAA_CORES; i++) {
25694e694fe5SAkhil Goyal 		session->inq[i] = dpaa_sec_attach_rxq(dev_priv);
25704e694fe5SAkhil Goyal 		if (session->inq[i] == NULL) {
2571a1173d55SHemant Agrawal 			DPAA_SEC_ERR("unable to attach sec queue");
25724e694fe5SAkhil Goyal 			rte_spinlock_unlock(&dev_priv->lock);
2573a1173d55SHemant Agrawal 			goto out;
2574a1173d55SHemant Agrawal 		}
25754e694fe5SAkhil Goyal 	}
25764e694fe5SAkhil Goyal 	rte_spinlock_unlock(&dev_priv->lock);
25771f14d500SAkhil Goyal 	return 0;
25781f14d500SAkhil Goyal out:
25791f14d500SAkhil Goyal 	rte_free(session->auth_key.data);
25801f14d500SAkhil Goyal 	rte_free(session->cipher_key.data);
25811f14d500SAkhil Goyal 	memset(session, 0, sizeof(dpaa_sec_session));
25821f14d500SAkhil Goyal 	return -1;
25831f14d500SAkhil Goyal }
25841f14d500SAkhil Goyal 
25851f14d500SAkhil Goyal static int
25861f14d500SAkhil Goyal dpaa_sec_security_session_create(void *dev,
25871f14d500SAkhil Goyal 				 struct rte_security_session_conf *conf,
25881f14d500SAkhil Goyal 				 struct rte_security_session *sess,
25891f14d500SAkhil Goyal 				 struct rte_mempool *mempool)
25901f14d500SAkhil Goyal {
25911f14d500SAkhil Goyal 	void *sess_private_data;
25921f14d500SAkhil Goyal 	struct rte_cryptodev *cdev = (struct rte_cryptodev *)dev;
25931f14d500SAkhil Goyal 	int ret;
25941f14d500SAkhil Goyal 
25951f14d500SAkhil Goyal 	if (rte_mempool_get(mempool, &sess_private_data)) {
2596f163231eSHemant Agrawal 		DPAA_SEC_ERR("Couldn't get object from session mempool");
25971f14d500SAkhil Goyal 		return -ENOMEM;
25981f14d500SAkhil Goyal 	}
25991f14d500SAkhil Goyal 
26001f14d500SAkhil Goyal 	switch (conf->protocol) {
26011f14d500SAkhil Goyal 	case RTE_SECURITY_PROTOCOL_IPSEC:
26021f14d500SAkhil Goyal 		ret = dpaa_sec_set_ipsec_session(cdev, conf,
26031f14d500SAkhil Goyal 				sess_private_data);
26041f14d500SAkhil Goyal 		break;
2605a1173d55SHemant Agrawal 	case RTE_SECURITY_PROTOCOL_PDCP:
2606a1173d55SHemant Agrawal 		ret = dpaa_sec_set_pdcp_session(cdev, conf,
2607a1173d55SHemant Agrawal 				sess_private_data);
2608a1173d55SHemant Agrawal 		break;
26091f14d500SAkhil Goyal 	case RTE_SECURITY_PROTOCOL_MACSEC:
26101f14d500SAkhil Goyal 		return -ENOTSUP;
26111f14d500SAkhil Goyal 	default:
26121f14d500SAkhil Goyal 		return -EINVAL;
26131f14d500SAkhil Goyal 	}
26141f14d500SAkhil Goyal 	if (ret != 0) {
2615f163231eSHemant Agrawal 		DPAA_SEC_ERR("failed to configure session parameters");
26161f14d500SAkhil Goyal 		/* Return session to mempool */
26171f14d500SAkhil Goyal 		rte_mempool_put(mempool, sess_private_data);
26181f14d500SAkhil Goyal 		return ret;
26191f14d500SAkhil Goyal 	}
26201f14d500SAkhil Goyal 
26211f14d500SAkhil Goyal 	set_sec_session_private_data(sess, sess_private_data);
26221f14d500SAkhil Goyal 
26231f14d500SAkhil Goyal 	return ret;
26241f14d500SAkhil Goyal }
26251f14d500SAkhil Goyal 
26261f14d500SAkhil Goyal /** Clear the memory of session so it doesn't leave key material behind */
26271f14d500SAkhil Goyal static int
26281f14d500SAkhil Goyal dpaa_sec_security_session_destroy(void *dev __rte_unused,
26291f14d500SAkhil Goyal 		struct rte_security_session *sess)
26301f14d500SAkhil Goyal {
26311f14d500SAkhil Goyal 	PMD_INIT_FUNC_TRACE();
26321f14d500SAkhil Goyal 	void *sess_priv = get_sec_session_private_data(sess);
26331f14d500SAkhil Goyal 	dpaa_sec_session *s = (dpaa_sec_session *)sess_priv;
26341f14d500SAkhil Goyal 
26351f14d500SAkhil Goyal 	if (sess_priv) {
26363d0d5332SAkhil Goyal 		free_session_memory((struct rte_cryptodev *)dev, s);
26371f14d500SAkhil Goyal 		set_sec_session_private_data(sess, NULL);
26381f14d500SAkhil Goyal 	}
26391f14d500SAkhil Goyal 	return 0;
26401f14d500SAkhil Goyal }
26411f14d500SAkhil Goyal 
26421f14d500SAkhil Goyal static int
26432ffb940eSAkhil Goyal dpaa_sec_dev_configure(struct rte_cryptodev *dev __rte_unused,
2644c3e85bdcSAkhil Goyal 		       struct rte_cryptodev_config *config __rte_unused)
2645c3e85bdcSAkhil Goyal {
2646c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2647c3e85bdcSAkhil Goyal 
2648c3e85bdcSAkhil Goyal 	return 0;
2649c3e85bdcSAkhil Goyal }
2650c3e85bdcSAkhil Goyal 
2651c3e85bdcSAkhil Goyal static int
2652c3e85bdcSAkhil Goyal dpaa_sec_dev_start(struct rte_cryptodev *dev __rte_unused)
2653c3e85bdcSAkhil Goyal {
2654c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2655c3e85bdcSAkhil Goyal 	return 0;
2656c3e85bdcSAkhil Goyal }
2657c3e85bdcSAkhil Goyal 
2658c3e85bdcSAkhil Goyal static void
2659c3e85bdcSAkhil Goyal dpaa_sec_dev_stop(struct rte_cryptodev *dev __rte_unused)
2660c3e85bdcSAkhil Goyal {
2661c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2662c3e85bdcSAkhil Goyal }
2663c3e85bdcSAkhil Goyal 
2664c3e85bdcSAkhil Goyal static int
26657e3e2954SAkhil Goyal dpaa_sec_dev_close(struct rte_cryptodev *dev)
2666c3e85bdcSAkhil Goyal {
2667c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
26687e3e2954SAkhil Goyal 
26697e3e2954SAkhil Goyal 	if (dev == NULL)
26707e3e2954SAkhil Goyal 		return -ENOMEM;
26717e3e2954SAkhil Goyal 
2672c3e85bdcSAkhil Goyal 	return 0;
2673c3e85bdcSAkhil Goyal }
2674c3e85bdcSAkhil Goyal 
2675c3e85bdcSAkhil Goyal static void
2676c3e85bdcSAkhil Goyal dpaa_sec_dev_infos_get(struct rte_cryptodev *dev,
2677c3e85bdcSAkhil Goyal 		       struct rte_cryptodev_info *info)
2678c3e85bdcSAkhil Goyal {
2679c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals = dev->data->dev_private;
2680c3e85bdcSAkhil Goyal 
2681c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2682c3e85bdcSAkhil Goyal 	if (info != NULL) {
2683c3e85bdcSAkhil Goyal 		info->max_nb_queue_pairs = internals->max_nb_queue_pairs;
2684c3e85bdcSAkhil Goyal 		info->feature_flags = dev->feature_flags;
2685c3e85bdcSAkhil Goyal 		info->capabilities = dpaa_sec_capabilities;
2686c3e85bdcSAkhil Goyal 		info->sym.max_nb_sessions = internals->max_nb_sessions;
2687c3e85bdcSAkhil Goyal 		info->driver_id = cryptodev_driver_id;
2688c3e85bdcSAkhil Goyal 	}
2689c3e85bdcSAkhil Goyal }
2690c3e85bdcSAkhil Goyal 
2691fe3688baSAkhil Goyal static enum qman_cb_dqrr_result
2692fe3688baSAkhil Goyal dpaa_sec_process_parallel_event(void *event,
2693fe3688baSAkhil Goyal 			struct qman_portal *qm __always_unused,
2694fe3688baSAkhil Goyal 			struct qman_fq *outq,
2695fe3688baSAkhil Goyal 			const struct qm_dqrr_entry *dqrr,
2696fe3688baSAkhil Goyal 			void **bufs)
2697fe3688baSAkhil Goyal {
2698fe3688baSAkhil Goyal 	const struct qm_fd *fd;
2699fe3688baSAkhil Goyal 	struct dpaa_sec_job *job;
2700fe3688baSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
2701fe3688baSAkhil Goyal 	struct rte_event *ev = (struct rte_event *)event;
2702fe3688baSAkhil Goyal 
2703fe3688baSAkhil Goyal 	fd = &dqrr->fd;
2704fe3688baSAkhil Goyal 
2705fe3688baSAkhil Goyal 	/* sg is embedded in an op ctx,
2706fe3688baSAkhil Goyal 	 * sg[0] is for output
2707fe3688baSAkhil Goyal 	 * sg[1] for input
2708fe3688baSAkhil Goyal 	 */
2709fe3688baSAkhil Goyal 	job = dpaa_mem_ptov(qm_fd_addr_get64(fd));
2710fe3688baSAkhil Goyal 
2711fe3688baSAkhil Goyal 	ctx = container_of(job, struct dpaa_sec_op_ctx, job);
2712fe3688baSAkhil Goyal 	ctx->fd_status = fd->status;
2713fe3688baSAkhil Goyal 	if (ctx->op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
2714fe3688baSAkhil Goyal 		struct qm_sg_entry *sg_out;
2715fe3688baSAkhil Goyal 		uint32_t len;
2716fe3688baSAkhil Goyal 
2717fe3688baSAkhil Goyal 		sg_out = &job->sg[0];
2718fe3688baSAkhil Goyal 		hw_sg_to_cpu(sg_out);
2719fe3688baSAkhil Goyal 		len = sg_out->length;
2720fe3688baSAkhil Goyal 		ctx->op->sym->m_src->pkt_len = len;
2721fe3688baSAkhil Goyal 		ctx->op->sym->m_src->data_len = len;
2722fe3688baSAkhil Goyal 	}
2723fe3688baSAkhil Goyal 	if (!ctx->fd_status) {
2724fe3688baSAkhil Goyal 		ctx->op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
2725fe3688baSAkhil Goyal 	} else {
2726fe3688baSAkhil Goyal 		DPAA_SEC_DP_WARN("SEC return err: 0x%x", ctx->fd_status);
2727fe3688baSAkhil Goyal 		ctx->op->status = RTE_CRYPTO_OP_STATUS_ERROR;
2728fe3688baSAkhil Goyal 	}
2729fe3688baSAkhil Goyal 	ev->event_ptr = (void *)ctx->op;
2730fe3688baSAkhil Goyal 
2731fe3688baSAkhil Goyal 	ev->flow_id = outq->ev.flow_id;
2732fe3688baSAkhil Goyal 	ev->sub_event_type = outq->ev.sub_event_type;
2733fe3688baSAkhil Goyal 	ev->event_type = RTE_EVENT_TYPE_CRYPTODEV;
2734fe3688baSAkhil Goyal 	ev->op = RTE_EVENT_OP_NEW;
2735fe3688baSAkhil Goyal 	ev->sched_type = outq->ev.sched_type;
2736fe3688baSAkhil Goyal 	ev->queue_id = outq->ev.queue_id;
2737fe3688baSAkhil Goyal 	ev->priority = outq->ev.priority;
2738fe3688baSAkhil Goyal 	*bufs = (void *)ctx->op;
2739fe3688baSAkhil Goyal 
2740fe3688baSAkhil Goyal 	rte_mempool_put(ctx->ctx_pool, (void *)ctx);
2741fe3688baSAkhil Goyal 
2742fe3688baSAkhil Goyal 	return qman_cb_dqrr_consume;
2743fe3688baSAkhil Goyal }
2744fe3688baSAkhil Goyal 
2745fe3688baSAkhil Goyal static enum qman_cb_dqrr_result
2746fe3688baSAkhil Goyal dpaa_sec_process_atomic_event(void *event,
2747fe3688baSAkhil Goyal 			struct qman_portal *qm __rte_unused,
2748fe3688baSAkhil Goyal 			struct qman_fq *outq,
2749fe3688baSAkhil Goyal 			const struct qm_dqrr_entry *dqrr,
2750fe3688baSAkhil Goyal 			void **bufs)
2751fe3688baSAkhil Goyal {
2752fe3688baSAkhil Goyal 	u8 index;
2753fe3688baSAkhil Goyal 	const struct qm_fd *fd;
2754fe3688baSAkhil Goyal 	struct dpaa_sec_job *job;
2755fe3688baSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
2756fe3688baSAkhil Goyal 	struct rte_event *ev = (struct rte_event *)event;
2757fe3688baSAkhil Goyal 
2758fe3688baSAkhil Goyal 	fd = &dqrr->fd;
2759fe3688baSAkhil Goyal 
2760fe3688baSAkhil Goyal 	/* sg is embedded in an op ctx,
2761fe3688baSAkhil Goyal 	 * sg[0] is for output
2762fe3688baSAkhil Goyal 	 * sg[1] for input
2763fe3688baSAkhil Goyal 	 */
2764fe3688baSAkhil Goyal 	job = dpaa_mem_ptov(qm_fd_addr_get64(fd));
2765fe3688baSAkhil Goyal 
2766fe3688baSAkhil Goyal 	ctx = container_of(job, struct dpaa_sec_op_ctx, job);
2767fe3688baSAkhil Goyal 	ctx->fd_status = fd->status;
2768fe3688baSAkhil Goyal 	if (ctx->op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
2769fe3688baSAkhil Goyal 		struct qm_sg_entry *sg_out;
2770fe3688baSAkhil Goyal 		uint32_t len;
2771fe3688baSAkhil Goyal 
2772fe3688baSAkhil Goyal 		sg_out = &job->sg[0];
2773fe3688baSAkhil Goyal 		hw_sg_to_cpu(sg_out);
2774fe3688baSAkhil Goyal 		len = sg_out->length;
2775fe3688baSAkhil Goyal 		ctx->op->sym->m_src->pkt_len = len;
2776fe3688baSAkhil Goyal 		ctx->op->sym->m_src->data_len = len;
2777fe3688baSAkhil Goyal 	}
2778fe3688baSAkhil Goyal 	if (!ctx->fd_status) {
2779fe3688baSAkhil Goyal 		ctx->op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
2780fe3688baSAkhil Goyal 	} else {
2781fe3688baSAkhil Goyal 		DPAA_SEC_DP_WARN("SEC return err: 0x%x", ctx->fd_status);
2782fe3688baSAkhil Goyal 		ctx->op->status = RTE_CRYPTO_OP_STATUS_ERROR;
2783fe3688baSAkhil Goyal 	}
2784fe3688baSAkhil Goyal 	ev->event_ptr = (void *)ctx->op;
2785fe3688baSAkhil Goyal 	ev->flow_id = outq->ev.flow_id;
2786fe3688baSAkhil Goyal 	ev->sub_event_type = outq->ev.sub_event_type;
2787fe3688baSAkhil Goyal 	ev->event_type = RTE_EVENT_TYPE_CRYPTODEV;
2788fe3688baSAkhil Goyal 	ev->op = RTE_EVENT_OP_NEW;
2789fe3688baSAkhil Goyal 	ev->sched_type = outq->ev.sched_type;
2790fe3688baSAkhil Goyal 	ev->queue_id = outq->ev.queue_id;
2791fe3688baSAkhil Goyal 	ev->priority = outq->ev.priority;
2792fe3688baSAkhil Goyal 
2793fe3688baSAkhil Goyal 	/* Save active dqrr entries */
2794fe3688baSAkhil Goyal 	index = ((uintptr_t)dqrr >> 6) & (16/*QM_DQRR_SIZE*/ - 1);
2795fe3688baSAkhil Goyal 	DPAA_PER_LCORE_DQRR_SIZE++;
2796fe3688baSAkhil Goyal 	DPAA_PER_LCORE_DQRR_HELD |= 1 << index;
2797fe3688baSAkhil Goyal 	DPAA_PER_LCORE_DQRR_MBUF(index) = ctx->op->sym->m_src;
2798fe3688baSAkhil Goyal 	ev->impl_opaque = index + 1;
2799fe3688baSAkhil Goyal 	ctx->op->sym->m_src->seqn = (uint32_t)index + 1;
2800fe3688baSAkhil Goyal 	*bufs = (void *)ctx->op;
2801fe3688baSAkhil Goyal 
2802fe3688baSAkhil Goyal 	rte_mempool_put(ctx->ctx_pool, (void *)ctx);
2803fe3688baSAkhil Goyal 
2804fe3688baSAkhil Goyal 	return qman_cb_dqrr_defer;
2805fe3688baSAkhil Goyal }
2806fe3688baSAkhil Goyal 
2807fe3688baSAkhil Goyal int
2808fe3688baSAkhil Goyal dpaa_sec_eventq_attach(const struct rte_cryptodev *dev,
2809fe3688baSAkhil Goyal 		int qp_id,
2810fe3688baSAkhil Goyal 		uint16_t ch_id,
2811fe3688baSAkhil Goyal 		const struct rte_event *event)
2812fe3688baSAkhil Goyal {
2813fe3688baSAkhil Goyal 	struct dpaa_sec_qp *qp = dev->data->queue_pairs[qp_id];
2814fe3688baSAkhil Goyal 	struct qm_mcc_initfq opts = {0};
2815fe3688baSAkhil Goyal 
2816fe3688baSAkhil Goyal 	int ret;
2817fe3688baSAkhil Goyal 
2818fe3688baSAkhil Goyal 	opts.we_mask = QM_INITFQ_WE_DESTWQ | QM_INITFQ_WE_FQCTRL |
2819fe3688baSAkhil Goyal 		       QM_INITFQ_WE_CONTEXTA | QM_INITFQ_WE_CONTEXTB;
2820fe3688baSAkhil Goyal 	opts.fqd.dest.channel = ch_id;
2821fe3688baSAkhil Goyal 
2822fe3688baSAkhil Goyal 	switch (event->sched_type) {
2823fe3688baSAkhil Goyal 	case RTE_SCHED_TYPE_ATOMIC:
2824fe3688baSAkhil Goyal 		opts.fqd.fq_ctrl |= QM_FQCTRL_HOLDACTIVE;
2825fe3688baSAkhil Goyal 		/* Reset FQCTRL_AVOIDBLOCK bit as it is unnecessary
2826fe3688baSAkhil Goyal 		 * configuration with HOLD_ACTIVE setting
2827fe3688baSAkhil Goyal 		 */
2828fe3688baSAkhil Goyal 		opts.fqd.fq_ctrl &= (~QM_FQCTRL_AVOIDBLOCK);
2829fe3688baSAkhil Goyal 		qp->outq.cb.dqrr_dpdk_cb = dpaa_sec_process_atomic_event;
2830fe3688baSAkhil Goyal 		break;
2831fe3688baSAkhil Goyal 	case RTE_SCHED_TYPE_ORDERED:
2832fe3688baSAkhil Goyal 		DPAA_SEC_ERR("Ordered queue schedule type is not supported\n");
2833fe3688baSAkhil Goyal 		return -1;
2834fe3688baSAkhil Goyal 	default:
2835fe3688baSAkhil Goyal 		opts.fqd.fq_ctrl |= QM_FQCTRL_AVOIDBLOCK;
2836fe3688baSAkhil Goyal 		qp->outq.cb.dqrr_dpdk_cb = dpaa_sec_process_parallel_event;
2837fe3688baSAkhil Goyal 		break;
2838fe3688baSAkhil Goyal 	}
2839fe3688baSAkhil Goyal 
2840fe3688baSAkhil Goyal 	ret = qman_init_fq(&qp->outq, QMAN_INITFQ_FLAG_SCHED, &opts);
2841fe3688baSAkhil Goyal 	if (unlikely(ret)) {
2842fe3688baSAkhil Goyal 		DPAA_SEC_ERR("unable to init caam source fq!");
2843fe3688baSAkhil Goyal 		return ret;
2844fe3688baSAkhil Goyal 	}
2845fe3688baSAkhil Goyal 
2846fe3688baSAkhil Goyal 	memcpy(&qp->outq.ev, event, sizeof(struct rte_event));
2847fe3688baSAkhil Goyal 
2848fe3688baSAkhil Goyal 	return 0;
2849fe3688baSAkhil Goyal }
2850fe3688baSAkhil Goyal 
2851fe3688baSAkhil Goyal int
2852fe3688baSAkhil Goyal dpaa_sec_eventq_detach(const struct rte_cryptodev *dev,
2853fe3688baSAkhil Goyal 			int qp_id)
2854fe3688baSAkhil Goyal {
2855fe3688baSAkhil Goyal 	struct qm_mcc_initfq opts = {0};
2856fe3688baSAkhil Goyal 	int ret;
2857fe3688baSAkhil Goyal 	struct dpaa_sec_qp *qp = dev->data->queue_pairs[qp_id];
2858fe3688baSAkhil Goyal 
2859fe3688baSAkhil Goyal 	opts.we_mask = QM_INITFQ_WE_DESTWQ | QM_INITFQ_WE_FQCTRL |
2860fe3688baSAkhil Goyal 		       QM_INITFQ_WE_CONTEXTA | QM_INITFQ_WE_CONTEXTB;
2861fe3688baSAkhil Goyal 	qp->outq.cb.dqrr = dqrr_out_fq_cb_rx;
2862fe3688baSAkhil Goyal 	qp->outq.cb.ern  = ern_sec_fq_handler;
2863fe3688baSAkhil Goyal 	qman_retire_fq(&qp->outq, NULL);
2864fe3688baSAkhil Goyal 	qman_oos_fq(&qp->outq);
2865fe3688baSAkhil Goyal 	ret = qman_init_fq(&qp->outq, 0, &opts);
2866fe3688baSAkhil Goyal 	if (ret)
2867fe3688baSAkhil Goyal 		RTE_LOG(ERR, PMD, "Error in qman_init_fq: ret: %d\n", ret);
2868fe3688baSAkhil Goyal 	qp->outq.cb.dqrr = NULL;
2869fe3688baSAkhil Goyal 
2870fe3688baSAkhil Goyal 	return ret;
2871fe3688baSAkhil Goyal }
2872fe3688baSAkhil Goyal 
2873c3e85bdcSAkhil Goyal static struct rte_cryptodev_ops crypto_ops = {
2874c3e85bdcSAkhil Goyal 	.dev_configure	      = dpaa_sec_dev_configure,
2875c3e85bdcSAkhil Goyal 	.dev_start	      = dpaa_sec_dev_start,
2876c3e85bdcSAkhil Goyal 	.dev_stop	      = dpaa_sec_dev_stop,
2877c3e85bdcSAkhil Goyal 	.dev_close	      = dpaa_sec_dev_close,
2878c3e85bdcSAkhil Goyal 	.dev_infos_get        = dpaa_sec_dev_infos_get,
2879c3e85bdcSAkhil Goyal 	.queue_pair_setup     = dpaa_sec_queue_pair_setup,
2880c3e85bdcSAkhil Goyal 	.queue_pair_release   = dpaa_sec_queue_pair_release,
2881c3e85bdcSAkhil Goyal 	.queue_pair_count     = dpaa_sec_queue_pair_count,
2882012c5076SPablo de Lara 	.sym_session_get_size     = dpaa_sec_sym_session_get_size,
2883012c5076SPablo de Lara 	.sym_session_configure    = dpaa_sec_sym_session_configure,
2884012c5076SPablo de Lara 	.sym_session_clear        = dpaa_sec_sym_session_clear
2885c3e85bdcSAkhil Goyal };
2886c3e85bdcSAkhil Goyal 
28871f14d500SAkhil Goyal static const struct rte_security_capability *
28881f14d500SAkhil Goyal dpaa_sec_capabilities_get(void *device __rte_unused)
28891f14d500SAkhil Goyal {
28901f14d500SAkhil Goyal 	return dpaa_sec_security_cap;
28911f14d500SAkhil Goyal }
28921f14d500SAkhil Goyal 
2893b74fd6b8SFerruh Yigit static const struct rte_security_ops dpaa_sec_security_ops = {
28941f14d500SAkhil Goyal 	.session_create = dpaa_sec_security_session_create,
28951f14d500SAkhil Goyal 	.session_update = NULL,
28961f14d500SAkhil Goyal 	.session_stats_get = NULL,
28971f14d500SAkhil Goyal 	.session_destroy = dpaa_sec_security_session_destroy,
28981f14d500SAkhil Goyal 	.set_pkt_metadata = NULL,
28991f14d500SAkhil Goyal 	.capabilities_get = dpaa_sec_capabilities_get
29001f14d500SAkhil Goyal };
29011f14d500SAkhil Goyal 
2902c3e85bdcSAkhil Goyal static int
2903c3e85bdcSAkhil Goyal dpaa_sec_uninit(struct rte_cryptodev *dev)
2904c3e85bdcSAkhil Goyal {
2905debef417SShreyansh Jain 	struct dpaa_sec_dev_private *internals;
2906c3e85bdcSAkhil Goyal 
2907c3e85bdcSAkhil Goyal 	if (dev == NULL)
2908c3e85bdcSAkhil Goyal 		return -ENODEV;
2909c3e85bdcSAkhil Goyal 
2910debef417SShreyansh Jain 	internals = dev->data->dev_private;
29111f14d500SAkhil Goyal 	rte_free(dev->security_ctx);
29121f14d500SAkhil Goyal 
2913c3e85bdcSAkhil Goyal 	rte_free(internals);
2914c3e85bdcSAkhil Goyal 
2915f163231eSHemant Agrawal 	DPAA_SEC_INFO("Closing DPAA_SEC device %s on numa socket %u",
2916c3e85bdcSAkhil Goyal 		      dev->data->name, rte_socket_id());
2917c3e85bdcSAkhil Goyal 
2918c3e85bdcSAkhil Goyal 	return 0;
2919c3e85bdcSAkhil Goyal }
2920c3e85bdcSAkhil Goyal 
2921c3e85bdcSAkhil Goyal static int
2922c3e85bdcSAkhil Goyal dpaa_sec_dev_init(struct rte_cryptodev *cryptodev)
2923c3e85bdcSAkhil Goyal {
2924c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals;
29251f14d500SAkhil Goyal 	struct rte_security_ctx *security_instance;
2926c3e85bdcSAkhil Goyal 	struct dpaa_sec_qp *qp;
2927e79416d1SHemant Agrawal 	uint32_t i, flags;
2928c3e85bdcSAkhil Goyal 	int ret;
2929c3e85bdcSAkhil Goyal 
2930c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2931c3e85bdcSAkhil Goyal 
2932c3e85bdcSAkhil Goyal 	cryptodev->driver_id = cryptodev_driver_id;
2933c3e85bdcSAkhil Goyal 	cryptodev->dev_ops = &crypto_ops;
2934c3e85bdcSAkhil Goyal 
2935c3e85bdcSAkhil Goyal 	cryptodev->enqueue_burst = dpaa_sec_enqueue_burst;
2936c3e85bdcSAkhil Goyal 	cryptodev->dequeue_burst = dpaa_sec_dequeue_burst;
2937c3e85bdcSAkhil Goyal 	cryptodev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
2938c3e85bdcSAkhil Goyal 			RTE_CRYPTODEV_FF_HW_ACCELERATED |
29391f14d500SAkhil Goyal 			RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
2940a74af788SAkhil Goyal 			RTE_CRYPTODEV_FF_SECURITY |
29412717246eSPablo de Lara 			RTE_CRYPTODEV_FF_IN_PLACE_SGL |
29422717246eSPablo de Lara 			RTE_CRYPTODEV_FF_OOP_SGL_IN_SGL_OUT |
29432717246eSPablo de Lara 			RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT |
29442717246eSPablo de Lara 			RTE_CRYPTODEV_FF_OOP_LB_IN_SGL_OUT |
29452717246eSPablo de Lara 			RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT;
2946c3e85bdcSAkhil Goyal 
2947c3e85bdcSAkhil Goyal 	internals = cryptodev->data->dev_private;
2948e79416d1SHemant Agrawal 	internals->max_nb_queue_pairs = RTE_DPAA_MAX_NB_SEC_QPS;
2949c3e85bdcSAkhil Goyal 	internals->max_nb_sessions = RTE_DPAA_SEC_PMD_MAX_NB_SESSIONS;
2950c3e85bdcSAkhil Goyal 
29511f14d500SAkhil Goyal 	/*
29521f14d500SAkhil Goyal 	 * For secondary processes, we don't initialise any further as primary
29531f14d500SAkhil Goyal 	 * has already done this work. Only check we don't need a different
29541f14d500SAkhil Goyal 	 * RX function
29551f14d500SAkhil Goyal 	 */
29561f14d500SAkhil Goyal 	if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
2957f163231eSHemant Agrawal 		DPAA_SEC_WARN("Device already init by primary process");
29581f14d500SAkhil Goyal 		return 0;
29591f14d500SAkhil Goyal 	}
29601f14d500SAkhil Goyal 
29611f14d500SAkhil Goyal 	/* Initialize security_ctx only for primary process*/
29621f14d500SAkhil Goyal 	security_instance = rte_malloc("rte_security_instances_ops",
29631f14d500SAkhil Goyal 				sizeof(struct rte_security_ctx), 0);
29641f14d500SAkhil Goyal 	if (security_instance == NULL)
29651f14d500SAkhil Goyal 		return -ENOMEM;
29661f14d500SAkhil Goyal 	security_instance->device = (void *)cryptodev;
29671f14d500SAkhil Goyal 	security_instance->ops = &dpaa_sec_security_ops;
29681f14d500SAkhil Goyal 	security_instance->sess_cnt = 0;
29691f14d500SAkhil Goyal 	cryptodev->security_ctx = security_instance;
29701f14d500SAkhil Goyal 
29713b617ee7SAkhil Goyal 	rte_spinlock_init(&internals->lock);
2972c3e85bdcSAkhil Goyal 	for (i = 0; i < internals->max_nb_queue_pairs; i++) {
2973c3e85bdcSAkhil Goyal 		/* init qman fq for queue pair */
2974c3e85bdcSAkhil Goyal 		qp = &internals->qps[i];
2975c3e85bdcSAkhil Goyal 		ret = dpaa_sec_init_tx(&qp->outq);
2976c3e85bdcSAkhil Goyal 		if (ret) {
2977f163231eSHemant Agrawal 			DPAA_SEC_ERR("config tx of queue pair  %d", i);
2978c3e85bdcSAkhil Goyal 			goto init_error;
2979c3e85bdcSAkhil Goyal 		}
2980e79416d1SHemant Agrawal 	}
2981e79416d1SHemant Agrawal 
2982e79416d1SHemant Agrawal 	flags = QMAN_FQ_FLAG_LOCKED | QMAN_FQ_FLAG_DYNAMIC_FQID |
2983e79416d1SHemant Agrawal 		QMAN_FQ_FLAG_TO_DCPORTAL;
29844e694fe5SAkhil Goyal 	for (i = 0; i < MAX_DPAA_CORES * internals->max_nb_sessions; i++) {
2985e79416d1SHemant Agrawal 		/* create rx qman fq for sessions*/
2986e79416d1SHemant Agrawal 		ret = qman_create_fq(0, flags, &internals->inq[i]);
2987e79416d1SHemant Agrawal 		if (unlikely(ret != 0)) {
2988f163231eSHemant Agrawal 			DPAA_SEC_ERR("sec qman_create_fq failed");
2989c3e85bdcSAkhil Goyal 			goto init_error;
2990c3e85bdcSAkhil Goyal 		}
2991c3e85bdcSAkhil Goyal 	}
2992c3e85bdcSAkhil Goyal 
2993f163231eSHemant Agrawal 	RTE_LOG(INFO, PMD, "%s cryptodev init\n", cryptodev->data->name);
2994c3e85bdcSAkhil Goyal 	return 0;
2995c3e85bdcSAkhil Goyal 
2996c3e85bdcSAkhil Goyal init_error:
2997f163231eSHemant Agrawal 	DPAA_SEC_ERR("driver %s: create failed\n", cryptodev->data->name);
2998c3e85bdcSAkhil Goyal 
2999c3e85bdcSAkhil Goyal 	dpaa_sec_uninit(cryptodev);
3000c3e85bdcSAkhil Goyal 	return -EFAULT;
3001c3e85bdcSAkhil Goyal }
3002c3e85bdcSAkhil Goyal 
3003c3e85bdcSAkhil Goyal static int
3004eb5a9a76SShreyansh Jain cryptodev_dpaa_sec_probe(struct rte_dpaa_driver *dpaa_drv __rte_unused,
3005c3e85bdcSAkhil Goyal 				struct rte_dpaa_device *dpaa_dev)
3006c3e85bdcSAkhil Goyal {
3007c3e85bdcSAkhil Goyal 	struct rte_cryptodev *cryptodev;
3008c3e85bdcSAkhil Goyal 	char cryptodev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
3009c3e85bdcSAkhil Goyal 
3010c3e85bdcSAkhil Goyal 	int retval;
3011c3e85bdcSAkhil Goyal 
30120964a951SHemant Agrawal 	snprintf(cryptodev_name, sizeof(cryptodev_name), "%s", dpaa_dev->name);
3013c3e85bdcSAkhil Goyal 
3014c3e85bdcSAkhil Goyal 	cryptodev = rte_cryptodev_pmd_allocate(cryptodev_name, rte_socket_id());
3015c3e85bdcSAkhil Goyal 	if (cryptodev == NULL)
3016c3e85bdcSAkhil Goyal 		return -ENOMEM;
3017c3e85bdcSAkhil Goyal 
3018c3e85bdcSAkhil Goyal 	if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
3019c3e85bdcSAkhil Goyal 		cryptodev->data->dev_private = rte_zmalloc_socket(
3020c3e85bdcSAkhil Goyal 					"cryptodev private structure",
3021c3e85bdcSAkhil Goyal 					sizeof(struct dpaa_sec_dev_private),
3022c3e85bdcSAkhil Goyal 					RTE_CACHE_LINE_SIZE,
3023c3e85bdcSAkhil Goyal 					rte_socket_id());
3024c3e85bdcSAkhil Goyal 
3025c3e85bdcSAkhil Goyal 		if (cryptodev->data->dev_private == NULL)
3026c3e85bdcSAkhil Goyal 			rte_panic("Cannot allocate memzone for private "
3027c3e85bdcSAkhil Goyal 					"device data");
3028c3e85bdcSAkhil Goyal 	}
3029c3e85bdcSAkhil Goyal 
3030c3e85bdcSAkhil Goyal 	dpaa_dev->crypto_dev = cryptodev;
3031c3e85bdcSAkhil Goyal 	cryptodev->device = &dpaa_dev->device;
3032c3e85bdcSAkhil Goyal 
3033c3e85bdcSAkhil Goyal 	/* init user callbacks */
3034c3e85bdcSAkhil Goyal 	TAILQ_INIT(&(cryptodev->link_intr_cbs));
3035c3e85bdcSAkhil Goyal 
3036c3e85bdcSAkhil Goyal 	/* if sec device version is not configured */
3037c3e85bdcSAkhil Goyal 	if (!rta_get_sec_era()) {
3038c3e85bdcSAkhil Goyal 		const struct device_node *caam_node;
3039c3e85bdcSAkhil Goyal 
3040c3e85bdcSAkhil Goyal 		for_each_compatible_node(caam_node, NULL, "fsl,sec-v4.0") {
3041c3e85bdcSAkhil Goyal 			const uint32_t *prop = of_get_property(caam_node,
3042c3e85bdcSAkhil Goyal 					"fsl,sec-era",
3043c3e85bdcSAkhil Goyal 					NULL);
3044c3e85bdcSAkhil Goyal 			if (prop) {
3045c3e85bdcSAkhil Goyal 				rta_set_sec_era(
3046c3e85bdcSAkhil Goyal 					INTL_SEC_ERA(rte_cpu_to_be_32(*prop)));
3047c3e85bdcSAkhil Goyal 				break;
3048c3e85bdcSAkhil Goyal 			}
3049c3e85bdcSAkhil Goyal 		}
3050c3e85bdcSAkhil Goyal 	}
3051c3e85bdcSAkhil Goyal 
3052c3e85bdcSAkhil Goyal 	/* Invoke PMD device initialization function */
3053c3e85bdcSAkhil Goyal 	retval = dpaa_sec_dev_init(cryptodev);
3054c3e85bdcSAkhil Goyal 	if (retval == 0)
3055c3e85bdcSAkhil Goyal 		return 0;
3056c3e85bdcSAkhil Goyal 
3057c3e85bdcSAkhil Goyal 	/* In case of error, cleanup is done */
3058c3e85bdcSAkhil Goyal 	if (rte_eal_process_type() == RTE_PROC_PRIMARY)
3059c3e85bdcSAkhil Goyal 		rte_free(cryptodev->data->dev_private);
3060c3e85bdcSAkhil Goyal 
3061c3e85bdcSAkhil Goyal 	rte_cryptodev_pmd_release_device(cryptodev);
3062c3e85bdcSAkhil Goyal 
3063c3e85bdcSAkhil Goyal 	return -ENXIO;
3064c3e85bdcSAkhil Goyal }
3065c3e85bdcSAkhil Goyal 
3066c3e85bdcSAkhil Goyal static int
3067c3e85bdcSAkhil Goyal cryptodev_dpaa_sec_remove(struct rte_dpaa_device *dpaa_dev)
3068c3e85bdcSAkhil Goyal {
3069c3e85bdcSAkhil Goyal 	struct rte_cryptodev *cryptodev;
3070c3e85bdcSAkhil Goyal 	int ret;
3071c3e85bdcSAkhil Goyal 
3072c3e85bdcSAkhil Goyal 	cryptodev = dpaa_dev->crypto_dev;
3073c3e85bdcSAkhil Goyal 	if (cryptodev == NULL)
3074c3e85bdcSAkhil Goyal 		return -ENODEV;
3075c3e85bdcSAkhil Goyal 
3076c3e85bdcSAkhil Goyal 	ret = dpaa_sec_uninit(cryptodev);
3077c3e85bdcSAkhil Goyal 	if (ret)
3078c3e85bdcSAkhil Goyal 		return ret;
3079c3e85bdcSAkhil Goyal 
3080f2f020d2SDeclan Doherty 	return rte_cryptodev_pmd_destroy(cryptodev);
3081c3e85bdcSAkhil Goyal }
3082c3e85bdcSAkhil Goyal 
3083c3e85bdcSAkhil Goyal static struct rte_dpaa_driver rte_dpaa_sec_driver = {
3084c3e85bdcSAkhil Goyal 	.drv_type = FSL_DPAA_CRYPTO,
3085c3e85bdcSAkhil Goyal 	.driver = {
3086c3e85bdcSAkhil Goyal 		.name = "DPAA SEC PMD"
3087c3e85bdcSAkhil Goyal 	},
3088c3e85bdcSAkhil Goyal 	.probe = cryptodev_dpaa_sec_probe,
3089c3e85bdcSAkhil Goyal 	.remove = cryptodev_dpaa_sec_remove,
3090c3e85bdcSAkhil Goyal };
3091c3e85bdcSAkhil Goyal 
3092c3e85bdcSAkhil Goyal static struct cryptodev_driver dpaa_sec_crypto_drv;
3093c3e85bdcSAkhil Goyal 
3094c3e85bdcSAkhil Goyal RTE_PMD_REGISTER_DPAA(CRYPTODEV_NAME_DPAA_SEC_PMD, rte_dpaa_sec_driver);
3095f737f5ceSFiona Trahe RTE_PMD_REGISTER_CRYPTO_DRIVER(dpaa_sec_crypto_drv, rte_dpaa_sec_driver.driver,
3096c3e85bdcSAkhil Goyal 		cryptodev_driver_id);
3097f163231eSHemant Agrawal 
3098f8e99896SThomas Monjalon RTE_INIT(dpaa_sec_init_log)
3099f163231eSHemant Agrawal {
3100f163231eSHemant Agrawal 	dpaa_logtype_sec = rte_log_register("pmd.crypto.dpaa");
3101f163231eSHemant Agrawal 	if (dpaa_logtype_sec >= 0)
3102f163231eSHemant Agrawal 		rte_log_set_level(dpaa_logtype_sec, RTE_LOG_NOTICE);
3103f163231eSHemant Agrawal }
3104