xref: /dpdk/drivers/crypto/dpaa_sec/dpaa_sec.c (revision f7a5752e709c2e5d2f9fb46af2f5957fb74a8ef4)
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>
40c3e85bdcSAkhil Goyal #include <dpaa_sec_log.h>
4112e58429SThierry Herbelot #include <dpaax_iova_table.h>
42c3e85bdcSAkhil Goyal 
43c3e85bdcSAkhil Goyal enum rta_sec_era rta_sec_era;
44c3e85bdcSAkhil Goyal 
45f163231eSHemant Agrawal int dpaa_logtype_sec;
46f163231eSHemant Agrawal 
47c3e85bdcSAkhil Goyal static uint8_t cryptodev_driver_id;
48c3e85bdcSAkhil Goyal 
49c3e85bdcSAkhil Goyal static __thread struct rte_crypto_op **dpaa_sec_ops;
50c3e85bdcSAkhil Goyal static __thread int dpaa_sec_op_nb;
51c3e85bdcSAkhil Goyal 
52e79416d1SHemant Agrawal static int
53e79416d1SHemant Agrawal dpaa_sec_attach_sess_q(struct dpaa_sec_qp *qp, dpaa_sec_session *sess);
54e79416d1SHemant Agrawal 
55c3e85bdcSAkhil Goyal static inline void
56c3e85bdcSAkhil Goyal dpaa_sec_op_ending(struct dpaa_sec_op_ctx *ctx)
57c3e85bdcSAkhil Goyal {
58c3e85bdcSAkhil Goyal 	if (!ctx->fd_status) {
59c3e85bdcSAkhil Goyal 		ctx->op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
60c3e85bdcSAkhil Goyal 	} else {
61f163231eSHemant Agrawal 		DPAA_SEC_DP_WARN("SEC return err: 0x%x", ctx->fd_status);
62c3e85bdcSAkhil Goyal 		ctx->op->status = RTE_CRYPTO_OP_STATUS_ERROR;
63c3e85bdcSAkhil Goyal 	}
64c3e85bdcSAkhil Goyal 
65a1f42a9dSThierry Herbelot 	/* report op status to sym->op and then free the ctx memory  */
66c3e85bdcSAkhil Goyal 	rte_mempool_put(ctx->ctx_pool, (void *)ctx);
67c3e85bdcSAkhil Goyal }
68c3e85bdcSAkhil Goyal 
69c3e85bdcSAkhil Goyal static inline struct dpaa_sec_op_ctx *
70*f7a5752eSHemant Agrawal dpaa_sec_alloc_ctx(dpaa_sec_session *ses, int sg_count)
71c3e85bdcSAkhil Goyal {
72c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
73*f7a5752eSHemant Agrawal 	int i, retval;
74c3e85bdcSAkhil Goyal 
75c3e85bdcSAkhil Goyal 	retval = rte_mempool_get(ses->ctx_pool, (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 	 */
86*f7a5752eSHemant Agrawal 	for (i = 0; i < sg_count && i < MAX_JOB_SG_ENTRIES; i += 4)
87*f7a5752eSHemant Agrawal 		dcbz_64(&ctx->job.sg[i]);
88c3e85bdcSAkhil Goyal 
89c3e85bdcSAkhil Goyal 	ctx->ctx_pool = ses->ctx_pool;
90*f7a5752eSHemant 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) &&
2691f14d500SAkhil Goyal 		(ses->proto_alg != RTE_SECURITY_PROTOCOL_IPSEC));
2701f14d500SAkhil Goyal }
2711f14d500SAkhil Goyal 
2721f14d500SAkhil Goyal static inline int is_proto_ipsec(dpaa_sec_session *ses)
2731f14d500SAkhil Goyal {
2741f14d500SAkhil Goyal 	return (ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC);
275c3e85bdcSAkhil Goyal }
276c3e85bdcSAkhil Goyal 
277a1173d55SHemant Agrawal static inline int is_proto_pdcp(dpaa_sec_session *ses)
278a1173d55SHemant Agrawal {
279a1173d55SHemant Agrawal 	return (ses->proto_alg == RTE_SECURITY_PROTOCOL_PDCP);
280a1173d55SHemant Agrawal }
281a1173d55SHemant Agrawal 
282c3e85bdcSAkhil Goyal static inline int is_encode(dpaa_sec_session *ses)
283c3e85bdcSAkhil Goyal {
284c3e85bdcSAkhil Goyal 	return ses->dir == DIR_ENC;
285c3e85bdcSAkhil Goyal }
286c3e85bdcSAkhil Goyal 
287c3e85bdcSAkhil Goyal static inline int is_decode(dpaa_sec_session *ses)
288c3e85bdcSAkhil Goyal {
289c3e85bdcSAkhil Goyal 	return ses->dir == DIR_DEC;
290c3e85bdcSAkhil Goyal }
291c3e85bdcSAkhil Goyal 
292c3e85bdcSAkhil Goyal static inline void
293c3e85bdcSAkhil Goyal caam_auth_alg(dpaa_sec_session *ses, struct alginfo *alginfo_a)
294c3e85bdcSAkhil Goyal {
295c3e85bdcSAkhil Goyal 	switch (ses->auth_alg) {
296c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AUTH_NULL:
29705b12700SHemant Agrawal 		alginfo_a->algtype =
29805b12700SHemant Agrawal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
29905b12700SHemant Agrawal 			OP_PCL_IPSEC_HMAC_NULL : 0;
300c3e85bdcSAkhil Goyal 		ses->digest_length = 0;
301c3e85bdcSAkhil Goyal 		break;
302c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AUTH_MD5_HMAC:
3031f14d500SAkhil Goyal 		alginfo_a->algtype =
3041f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3051f14d500SAkhil Goyal 			OP_PCL_IPSEC_HMAC_MD5_96 : OP_ALG_ALGSEL_MD5;
306c3e85bdcSAkhil Goyal 		alginfo_a->algmode = OP_ALG_AAI_HMAC;
307c3e85bdcSAkhil Goyal 		break;
308c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA1_HMAC:
3091f14d500SAkhil Goyal 		alginfo_a->algtype =
3101f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3111f14d500SAkhil Goyal 			OP_PCL_IPSEC_HMAC_SHA1_96 : OP_ALG_ALGSEL_SHA1;
312c3e85bdcSAkhil Goyal 		alginfo_a->algmode = OP_ALG_AAI_HMAC;
313c3e85bdcSAkhil Goyal 		break;
314c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA224_HMAC:
3151f14d500SAkhil Goyal 		alginfo_a->algtype =
3161f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3171f14d500SAkhil Goyal 			OP_PCL_IPSEC_HMAC_SHA1_160 : OP_ALG_ALGSEL_SHA224;
318c3e85bdcSAkhil Goyal 		alginfo_a->algmode = OP_ALG_AAI_HMAC;
319c3e85bdcSAkhil Goyal 		break;
320c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA256_HMAC:
3211f14d500SAkhil Goyal 		alginfo_a->algtype =
3221f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3231f14d500SAkhil Goyal 			OP_PCL_IPSEC_HMAC_SHA2_256_128 : OP_ALG_ALGSEL_SHA256;
324c3e85bdcSAkhil Goyal 		alginfo_a->algmode = OP_ALG_AAI_HMAC;
325c3e85bdcSAkhil Goyal 		break;
326c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA384_HMAC:
3271f14d500SAkhil Goyal 		alginfo_a->algtype =
3281f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3291f14d500SAkhil Goyal 			OP_PCL_IPSEC_HMAC_SHA2_384_192 : OP_ALG_ALGSEL_SHA384;
330c3e85bdcSAkhil Goyal 		alginfo_a->algmode = OP_ALG_AAI_HMAC;
331c3e85bdcSAkhil Goyal 		break;
332c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA512_HMAC:
3331f14d500SAkhil Goyal 		alginfo_a->algtype =
3341f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3351f14d500SAkhil Goyal 			OP_PCL_IPSEC_HMAC_SHA2_512_256 : OP_ALG_ALGSEL_SHA512;
336c3e85bdcSAkhil Goyal 		alginfo_a->algmode = OP_ALG_AAI_HMAC;
337c3e85bdcSAkhil Goyal 		break;
338c3e85bdcSAkhil Goyal 	default:
339f163231eSHemant Agrawal 		DPAA_SEC_ERR("unsupported auth alg %u", ses->auth_alg);
340c3e85bdcSAkhil Goyal 	}
341c3e85bdcSAkhil Goyal }
342c3e85bdcSAkhil Goyal 
343c3e85bdcSAkhil Goyal static inline void
344c3e85bdcSAkhil Goyal caam_cipher_alg(dpaa_sec_session *ses, struct alginfo *alginfo_c)
345c3e85bdcSAkhil Goyal {
346c3e85bdcSAkhil Goyal 	switch (ses->cipher_alg) {
347c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_CIPHER_NULL:
34805b12700SHemant Agrawal 		alginfo_c->algtype =
34905b12700SHemant Agrawal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
35005b12700SHemant Agrawal 			OP_PCL_IPSEC_NULL : 0;
351c3e85bdcSAkhil Goyal 		break;
352c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_CIPHER_AES_CBC:
3531f14d500SAkhil Goyal 		alginfo_c->algtype =
3541f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3551f14d500SAkhil Goyal 			OP_PCL_IPSEC_AES_CBC : OP_ALG_ALGSEL_AES;
356c3e85bdcSAkhil Goyal 		alginfo_c->algmode = OP_ALG_AAI_CBC;
357c3e85bdcSAkhil Goyal 		break;
358c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_CIPHER_3DES_CBC:
3591f14d500SAkhil Goyal 		alginfo_c->algtype =
3601f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3611f14d500SAkhil Goyal 			OP_PCL_IPSEC_3DES : OP_ALG_ALGSEL_3DES;
362c3e85bdcSAkhil Goyal 		alginfo_c->algmode = OP_ALG_AAI_CBC;
363c3e85bdcSAkhil Goyal 		break;
364c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_CIPHER_AES_CTR:
3651f14d500SAkhil Goyal 		alginfo_c->algtype =
3661f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3671f14d500SAkhil Goyal 			OP_PCL_IPSEC_AES_CTR : OP_ALG_ALGSEL_AES;
368c3e85bdcSAkhil Goyal 		alginfo_c->algmode = OP_ALG_AAI_CTR;
369c3e85bdcSAkhil Goyal 		break;
370c3e85bdcSAkhil Goyal 	default:
371f163231eSHemant Agrawal 		DPAA_SEC_ERR("unsupported cipher alg %d", ses->cipher_alg);
372c3e85bdcSAkhil Goyal 	}
373c3e85bdcSAkhil Goyal }
374c3e85bdcSAkhil Goyal 
375c3e85bdcSAkhil Goyal static inline void
376c3e85bdcSAkhil Goyal caam_aead_alg(dpaa_sec_session *ses, struct alginfo *alginfo)
377c3e85bdcSAkhil Goyal {
378c3e85bdcSAkhil Goyal 	switch (ses->aead_alg) {
379c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AEAD_AES_GCM:
380c3e85bdcSAkhil Goyal 		alginfo->algtype = OP_ALG_ALGSEL_AES;
381c3e85bdcSAkhil Goyal 		alginfo->algmode = OP_ALG_AAI_GCM;
382c3e85bdcSAkhil Goyal 		break;
383c3e85bdcSAkhil Goyal 	default:
384f163231eSHemant Agrawal 		DPAA_SEC_ERR("unsupported AEAD alg %d", ses->aead_alg);
385c3e85bdcSAkhil Goyal 	}
386c3e85bdcSAkhil Goyal }
387c3e85bdcSAkhil Goyal 
388a1173d55SHemant Agrawal static int
389a1173d55SHemant Agrawal dpaa_sec_prep_pdcp_cdb(dpaa_sec_session *ses)
390a1173d55SHemant Agrawal {
391a1173d55SHemant Agrawal 	struct alginfo authdata = {0}, cipherdata = {0};
392a1173d55SHemant Agrawal 	struct sec_cdb *cdb = &ses->cdb;
3932e4cbdb4SVakul Garg 	struct alginfo *p_authdata = NULL;
394a1173d55SHemant Agrawal 	int32_t shared_desc_len = 0;
395a1173d55SHemant Agrawal 	int err;
396a1173d55SHemant Agrawal #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
397a1173d55SHemant Agrawal 	int swap = false;
398a1173d55SHemant Agrawal #else
399a1173d55SHemant Agrawal 	int swap = true;
400a1173d55SHemant Agrawal #endif
401a1173d55SHemant Agrawal 
402a1173d55SHemant Agrawal 	switch (ses->cipher_alg) {
403a1173d55SHemant Agrawal 	case RTE_CRYPTO_CIPHER_SNOW3G_UEA2:
404a1173d55SHemant Agrawal 		cipherdata.algtype = PDCP_CIPHER_TYPE_SNOW;
405a1173d55SHemant Agrawal 		break;
406a1173d55SHemant Agrawal 	case RTE_CRYPTO_CIPHER_ZUC_EEA3:
407a1173d55SHemant Agrawal 		cipherdata.algtype = PDCP_CIPHER_TYPE_ZUC;
408a1173d55SHemant Agrawal 		break;
409a1173d55SHemant Agrawal 	case RTE_CRYPTO_CIPHER_AES_CTR:
410a1173d55SHemant Agrawal 		cipherdata.algtype = PDCP_CIPHER_TYPE_AES;
411a1173d55SHemant Agrawal 		break;
412a1173d55SHemant Agrawal 	case RTE_CRYPTO_CIPHER_NULL:
413a1173d55SHemant Agrawal 		cipherdata.algtype = PDCP_CIPHER_TYPE_NULL;
414a1173d55SHemant Agrawal 		break;
415a1173d55SHemant Agrawal 	default:
416a1173d55SHemant Agrawal 		DPAA_SEC_ERR("Crypto: Undefined Cipher specified %u",
417a1173d55SHemant Agrawal 			      ses->cipher_alg);
418a1173d55SHemant Agrawal 		return -1;
419a1173d55SHemant Agrawal 	}
420a1173d55SHemant Agrawal 
421a1173d55SHemant Agrawal 	cipherdata.key = (size_t)ses->cipher_key.data;
422a1173d55SHemant Agrawal 	cipherdata.keylen = ses->cipher_key.length;
423a1173d55SHemant Agrawal 	cipherdata.key_enc_flags = 0;
424a1173d55SHemant Agrawal 	cipherdata.key_type = RTA_DATA_IMM;
425a1173d55SHemant Agrawal 
4262e4cbdb4SVakul Garg 	cdb->sh_desc[0] = cipherdata.keylen;
4272e4cbdb4SVakul Garg 	cdb->sh_desc[1] = 0;
4282e4cbdb4SVakul Garg 	cdb->sh_desc[2] = 0;
4292e4cbdb4SVakul Garg 
4302e4cbdb4SVakul Garg 	if (ses->auth_alg) {
431a1173d55SHemant Agrawal 		switch (ses->auth_alg) {
432a1173d55SHemant Agrawal 		case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
433a1173d55SHemant Agrawal 			authdata.algtype = PDCP_AUTH_TYPE_SNOW;
434a1173d55SHemant Agrawal 			break;
435a1173d55SHemant Agrawal 		case RTE_CRYPTO_AUTH_ZUC_EIA3:
436a1173d55SHemant Agrawal 			authdata.algtype = PDCP_AUTH_TYPE_ZUC;
437a1173d55SHemant Agrawal 			break;
438a1173d55SHemant Agrawal 		case RTE_CRYPTO_AUTH_AES_CMAC:
439a1173d55SHemant Agrawal 			authdata.algtype = PDCP_AUTH_TYPE_AES;
440a1173d55SHemant Agrawal 			break;
441a1173d55SHemant Agrawal 		case RTE_CRYPTO_AUTH_NULL:
442a1173d55SHemant Agrawal 			authdata.algtype = PDCP_AUTH_TYPE_NULL;
443a1173d55SHemant Agrawal 			break;
444a1173d55SHemant Agrawal 		default:
445a1173d55SHemant Agrawal 			DPAA_SEC_ERR("Crypto: Unsupported auth alg %u",
446a1173d55SHemant Agrawal 				      ses->auth_alg);
447a1173d55SHemant Agrawal 			return -1;
448a1173d55SHemant Agrawal 		}
449a1173d55SHemant Agrawal 
450a1173d55SHemant Agrawal 		authdata.key = (size_t)ses->auth_key.data;
451a1173d55SHemant Agrawal 		authdata.keylen = ses->auth_key.length;
452a1173d55SHemant Agrawal 		authdata.key_enc_flags = 0;
453a1173d55SHemant Agrawal 		authdata.key_type = RTA_DATA_IMM;
454a1173d55SHemant Agrawal 
4552e4cbdb4SVakul Garg 		p_authdata = &authdata;
4562e4cbdb4SVakul Garg 
457a1173d55SHemant Agrawal 		cdb->sh_desc[1] = authdata.keylen;
4582e4cbdb4SVakul Garg 	}
4592e4cbdb4SVakul Garg 
460a1173d55SHemant Agrawal 	err = rta_inline_query(IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN,
461a1173d55SHemant Agrawal 			       MIN_JOB_DESC_SIZE,
462a1173d55SHemant Agrawal 			       (unsigned int *)cdb->sh_desc,
463a1173d55SHemant Agrawal 			       &cdb->sh_desc[2], 2);
464a1173d55SHemant Agrawal 	if (err < 0) {
465a1173d55SHemant Agrawal 		DPAA_SEC_ERR("Crypto: Incorrect key lengths");
466a1173d55SHemant Agrawal 		return err;
467a1173d55SHemant Agrawal 	}
4682e4cbdb4SVakul Garg 
469a1173d55SHemant Agrawal 	if (!(cdb->sh_desc[2] & 1) && cipherdata.keylen) {
4702e4cbdb4SVakul Garg 		cipherdata.key =
4712e4cbdb4SVakul Garg 			(size_t)dpaa_mem_vtop((void *)(size_t)cipherdata.key);
472a1173d55SHemant Agrawal 		cipherdata.key_type = RTA_DATA_PTR;
473a1173d55SHemant Agrawal 	}
474a1173d55SHemant Agrawal 	if (!(cdb->sh_desc[2] & (1 << 1)) &&  authdata.keylen) {
4752e4cbdb4SVakul Garg 		authdata.key =
4762e4cbdb4SVakul Garg 			(size_t)dpaa_mem_vtop((void *)(size_t)authdata.key);
477a1173d55SHemant Agrawal 		authdata.key_type = RTA_DATA_PTR;
478a1173d55SHemant Agrawal 	}
479a1173d55SHemant Agrawal 
480a1173d55SHemant Agrawal 	cdb->sh_desc[0] = 0;
481a1173d55SHemant Agrawal 	cdb->sh_desc[1] = 0;
482a1173d55SHemant Agrawal 	cdb->sh_desc[2] = 0;
483a1173d55SHemant Agrawal 
4842e4cbdb4SVakul Garg 	if (ses->pdcp.domain == RTE_SECURITY_PDCP_MODE_CONTROL) {
485a1173d55SHemant Agrawal 		if (ses->dir == DIR_ENC)
486a1173d55SHemant Agrawal 			shared_desc_len = cnstr_shdsc_pdcp_c_plane_encap(
487a1173d55SHemant Agrawal 					cdb->sh_desc, 1, swap,
488a1173d55SHemant Agrawal 					ses->pdcp.hfn,
489eac60082SVakul Garg 					ses->pdcp.sn_size,
490a1173d55SHemant Agrawal 					ses->pdcp.bearer,
491a1173d55SHemant Agrawal 					ses->pdcp.pkt_dir,
492a1173d55SHemant Agrawal 					ses->pdcp.hfn_threshold,
493a1173d55SHemant Agrawal 					&cipherdata, &authdata,
494a1173d55SHemant Agrawal 					0);
495a1173d55SHemant Agrawal 		else if (ses->dir == DIR_DEC)
496a1173d55SHemant Agrawal 			shared_desc_len = cnstr_shdsc_pdcp_c_plane_decap(
497a1173d55SHemant Agrawal 					cdb->sh_desc, 1, swap,
498a1173d55SHemant Agrawal 					ses->pdcp.hfn,
499eac60082SVakul Garg 					ses->pdcp.sn_size,
500a1173d55SHemant Agrawal 					ses->pdcp.bearer,
501a1173d55SHemant Agrawal 					ses->pdcp.pkt_dir,
502a1173d55SHemant Agrawal 					ses->pdcp.hfn_threshold,
503a1173d55SHemant Agrawal 					&cipherdata, &authdata,
504a1173d55SHemant Agrawal 					0);
505a1173d55SHemant Agrawal 	} else {
506a1173d55SHemant Agrawal 		if (ses->dir == DIR_ENC)
507a1173d55SHemant Agrawal 			shared_desc_len = cnstr_shdsc_pdcp_u_plane_encap(
508a1173d55SHemant Agrawal 					cdb->sh_desc, 1, swap,
509a1173d55SHemant Agrawal 					ses->pdcp.sn_size,
510a1173d55SHemant Agrawal 					ses->pdcp.hfn,
511a1173d55SHemant Agrawal 					ses->pdcp.bearer,
512a1173d55SHemant Agrawal 					ses->pdcp.pkt_dir,
513a1173d55SHemant Agrawal 					ses->pdcp.hfn_threshold,
5142e4cbdb4SVakul Garg 					&cipherdata, p_authdata, 0);
515a1173d55SHemant Agrawal 		else if (ses->dir == DIR_DEC)
516a1173d55SHemant Agrawal 			shared_desc_len = cnstr_shdsc_pdcp_u_plane_decap(
517a1173d55SHemant Agrawal 					cdb->sh_desc, 1, swap,
518a1173d55SHemant Agrawal 					ses->pdcp.sn_size,
519a1173d55SHemant Agrawal 					ses->pdcp.hfn,
520a1173d55SHemant Agrawal 					ses->pdcp.bearer,
521a1173d55SHemant Agrawal 					ses->pdcp.pkt_dir,
522a1173d55SHemant Agrawal 					ses->pdcp.hfn_threshold,
5232e4cbdb4SVakul Garg 					&cipherdata, p_authdata, 0);
524a1173d55SHemant Agrawal 	}
525a1173d55SHemant Agrawal 
526a1173d55SHemant Agrawal 	return shared_desc_len;
527a1173d55SHemant Agrawal }
528a1173d55SHemant Agrawal 
52905b12700SHemant Agrawal /* prepare ipsec proto command block of the session */
53005b12700SHemant Agrawal static int
53105b12700SHemant Agrawal dpaa_sec_prep_ipsec_cdb(dpaa_sec_session *ses)
53205b12700SHemant Agrawal {
53305b12700SHemant Agrawal 	struct alginfo cipherdata = {0}, authdata = {0};
53405b12700SHemant Agrawal 	struct sec_cdb *cdb = &ses->cdb;
53505b12700SHemant Agrawal 	int32_t shared_desc_len = 0;
53605b12700SHemant Agrawal 	int err;
53705b12700SHemant Agrawal #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
53805b12700SHemant Agrawal 	int swap = false;
53905b12700SHemant Agrawal #else
54005b12700SHemant Agrawal 	int swap = true;
54105b12700SHemant Agrawal #endif
54205b12700SHemant Agrawal 
54305b12700SHemant Agrawal 	caam_cipher_alg(ses, &cipherdata);
54405b12700SHemant Agrawal 	if (cipherdata.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
54505b12700SHemant Agrawal 		DPAA_SEC_ERR("not supported cipher alg");
54605b12700SHemant Agrawal 		return -ENOTSUP;
54705b12700SHemant Agrawal 	}
54805b12700SHemant Agrawal 
54905b12700SHemant Agrawal 	cipherdata.key = (size_t)ses->cipher_key.data;
55005b12700SHemant Agrawal 	cipherdata.keylen = ses->cipher_key.length;
55105b12700SHemant Agrawal 	cipherdata.key_enc_flags = 0;
55205b12700SHemant Agrawal 	cipherdata.key_type = RTA_DATA_IMM;
55305b12700SHemant Agrawal 
55405b12700SHemant Agrawal 	caam_auth_alg(ses, &authdata);
55505b12700SHemant Agrawal 	if (authdata.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
55605b12700SHemant Agrawal 		DPAA_SEC_ERR("not supported auth alg");
55705b12700SHemant Agrawal 		return -ENOTSUP;
55805b12700SHemant Agrawal 	}
55905b12700SHemant Agrawal 
56005b12700SHemant Agrawal 	authdata.key = (size_t)ses->auth_key.data;
56105b12700SHemant Agrawal 	authdata.keylen = ses->auth_key.length;
56205b12700SHemant Agrawal 	authdata.key_enc_flags = 0;
56305b12700SHemant Agrawal 	authdata.key_type = RTA_DATA_IMM;
56405b12700SHemant Agrawal 
56505b12700SHemant Agrawal 	cdb->sh_desc[0] = cipherdata.keylen;
56605b12700SHemant Agrawal 	cdb->sh_desc[1] = authdata.keylen;
56705b12700SHemant Agrawal 	err = rta_inline_query(IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN,
56805b12700SHemant Agrawal 			       MIN_JOB_DESC_SIZE,
56905b12700SHemant Agrawal 			       (unsigned int *)cdb->sh_desc,
57005b12700SHemant Agrawal 			       &cdb->sh_desc[2], 2);
57105b12700SHemant Agrawal 
57205b12700SHemant Agrawal 	if (err < 0) {
57305b12700SHemant Agrawal 		DPAA_SEC_ERR("Crypto: Incorrect key lengths");
57405b12700SHemant Agrawal 		return err;
57505b12700SHemant Agrawal 	}
57605b12700SHemant Agrawal 	if (cdb->sh_desc[2] & 1)
57705b12700SHemant Agrawal 		cipherdata.key_type = RTA_DATA_IMM;
57805b12700SHemant Agrawal 	else {
57905b12700SHemant Agrawal 		cipherdata.key = (size_t)dpaa_mem_vtop(
58005b12700SHemant Agrawal 					(void *)(size_t)cipherdata.key);
58105b12700SHemant Agrawal 		cipherdata.key_type = RTA_DATA_PTR;
58205b12700SHemant Agrawal 	}
58305b12700SHemant Agrawal 	if (cdb->sh_desc[2] & (1<<1))
58405b12700SHemant Agrawal 		authdata.key_type = RTA_DATA_IMM;
58505b12700SHemant Agrawal 	else {
58605b12700SHemant Agrawal 		authdata.key = (size_t)dpaa_mem_vtop(
58705b12700SHemant Agrawal 					(void *)(size_t)authdata.key);
58805b12700SHemant Agrawal 		authdata.key_type = RTA_DATA_PTR;
58905b12700SHemant Agrawal 	}
59005b12700SHemant Agrawal 
59105b12700SHemant Agrawal 	cdb->sh_desc[0] = 0;
59205b12700SHemant Agrawal 	cdb->sh_desc[1] = 0;
59305b12700SHemant Agrawal 	cdb->sh_desc[2] = 0;
59405b12700SHemant Agrawal 	if (ses->dir == DIR_ENC) {
59505b12700SHemant Agrawal 		shared_desc_len = cnstr_shdsc_ipsec_new_encap(
59605b12700SHemant Agrawal 				cdb->sh_desc,
59705b12700SHemant Agrawal 				true, swap, SHR_SERIAL,
59805b12700SHemant Agrawal 				&ses->encap_pdb,
59905b12700SHemant Agrawal 				(uint8_t *)&ses->ip4_hdr,
60005b12700SHemant Agrawal 				&cipherdata, &authdata);
60105b12700SHemant Agrawal 	} else if (ses->dir == DIR_DEC) {
60205b12700SHemant Agrawal 		shared_desc_len = cnstr_shdsc_ipsec_new_decap(
60305b12700SHemant Agrawal 				cdb->sh_desc,
60405b12700SHemant Agrawal 				true, swap, SHR_SERIAL,
60505b12700SHemant Agrawal 				&ses->decap_pdb,
60605b12700SHemant Agrawal 				&cipherdata, &authdata);
60705b12700SHemant Agrawal 	}
60805b12700SHemant Agrawal 	return shared_desc_len;
60905b12700SHemant Agrawal }
610c3e85bdcSAkhil Goyal 
611c3e85bdcSAkhil Goyal /* prepare command block of the session */
612c3e85bdcSAkhil Goyal static int
613c3e85bdcSAkhil Goyal dpaa_sec_prep_cdb(dpaa_sec_session *ses)
614c3e85bdcSAkhil Goyal {
615c3e85bdcSAkhil Goyal 	struct alginfo alginfo_c = {0}, alginfo_a = {0}, alginfo = {0};
61622788c2cSSunil Kumar Kori 	int32_t shared_desc_len = 0;
617e79416d1SHemant Agrawal 	struct sec_cdb *cdb = &ses->cdb;
618c3e85bdcSAkhil Goyal 	int err;
619c3e85bdcSAkhil Goyal #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
620c3e85bdcSAkhil Goyal 	int swap = false;
621c3e85bdcSAkhil Goyal #else
622c3e85bdcSAkhil Goyal 	int swap = true;
623c3e85bdcSAkhil Goyal #endif
624c3e85bdcSAkhil Goyal 
625c3e85bdcSAkhil Goyal 	memset(cdb, 0, sizeof(struct sec_cdb));
626c3e85bdcSAkhil Goyal 
62705b12700SHemant Agrawal 	if (is_proto_ipsec(ses)) {
62805b12700SHemant Agrawal 		shared_desc_len = dpaa_sec_prep_ipsec_cdb(ses);
629a1173d55SHemant Agrawal 	} else if (is_proto_pdcp(ses)) {
630a1173d55SHemant Agrawal 		shared_desc_len = dpaa_sec_prep_pdcp_cdb(ses);
63105b12700SHemant Agrawal 	} else if (is_cipher_only(ses)) {
632c3e85bdcSAkhil Goyal 		caam_cipher_alg(ses, &alginfo_c);
633c3e85bdcSAkhil Goyal 		if (alginfo_c.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
634f163231eSHemant Agrawal 			DPAA_SEC_ERR("not supported cipher alg");
635c3e85bdcSAkhil Goyal 			return -ENOTSUP;
636c3e85bdcSAkhil Goyal 		}
637c3e85bdcSAkhil Goyal 
6380e5607e4SHemant Agrawal 		alginfo_c.key = (size_t)ses->cipher_key.data;
639c3e85bdcSAkhil Goyal 		alginfo_c.keylen = ses->cipher_key.length;
640c3e85bdcSAkhil Goyal 		alginfo_c.key_enc_flags = 0;
641c3e85bdcSAkhil Goyal 		alginfo_c.key_type = RTA_DATA_IMM;
642c3e85bdcSAkhil Goyal 
643c3e85bdcSAkhil Goyal 		shared_desc_len = cnstr_shdsc_blkcipher(
644c3e85bdcSAkhil Goyal 						cdb->sh_desc, true,
6457449390bSAkhil Goyal 						swap, SHR_NEVER, &alginfo_c,
646c3e85bdcSAkhil Goyal 						NULL,
647c3e85bdcSAkhil Goyal 						ses->iv.length,
648c3e85bdcSAkhil Goyal 						ses->dir);
649c3e85bdcSAkhil Goyal 	} else if (is_auth_only(ses)) {
650c3e85bdcSAkhil Goyal 		caam_auth_alg(ses, &alginfo_a);
651c3e85bdcSAkhil Goyal 		if (alginfo_a.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
652f163231eSHemant Agrawal 			DPAA_SEC_ERR("not supported auth alg");
653c3e85bdcSAkhil Goyal 			return -ENOTSUP;
654c3e85bdcSAkhil Goyal 		}
655c3e85bdcSAkhil Goyal 
6560e5607e4SHemant Agrawal 		alginfo_a.key = (size_t)ses->auth_key.data;
657c3e85bdcSAkhil Goyal 		alginfo_a.keylen = ses->auth_key.length;
658c3e85bdcSAkhil Goyal 		alginfo_a.key_enc_flags = 0;
659c3e85bdcSAkhil Goyal 		alginfo_a.key_type = RTA_DATA_IMM;
660c3e85bdcSAkhil Goyal 
661c3e85bdcSAkhil Goyal 		shared_desc_len = cnstr_shdsc_hmac(cdb->sh_desc, true,
6627449390bSAkhil Goyal 						   swap, SHR_NEVER, &alginfo_a,
663c3e85bdcSAkhil Goyal 						   !ses->dir,
664c3e85bdcSAkhil Goyal 						   ses->digest_length);
665c3e85bdcSAkhil Goyal 	} else if (is_aead(ses)) {
666c3e85bdcSAkhil Goyal 		caam_aead_alg(ses, &alginfo);
667c3e85bdcSAkhil Goyal 		if (alginfo.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
668f163231eSHemant Agrawal 			DPAA_SEC_ERR("not supported aead alg");
669c3e85bdcSAkhil Goyal 			return -ENOTSUP;
670c3e85bdcSAkhil Goyal 		}
6710e5607e4SHemant Agrawal 		alginfo.key = (size_t)ses->aead_key.data;
672c3e85bdcSAkhil Goyal 		alginfo.keylen = ses->aead_key.length;
673c3e85bdcSAkhil Goyal 		alginfo.key_enc_flags = 0;
674c3e85bdcSAkhil Goyal 		alginfo.key_type = RTA_DATA_IMM;
675c3e85bdcSAkhil Goyal 
676c3e85bdcSAkhil Goyal 		if (ses->dir == DIR_ENC)
677c3e85bdcSAkhil Goyal 			shared_desc_len = cnstr_shdsc_gcm_encap(
6787449390bSAkhil Goyal 					cdb->sh_desc, true, swap, SHR_NEVER,
679c3e85bdcSAkhil Goyal 					&alginfo,
680c3e85bdcSAkhil Goyal 					ses->iv.length,
681c3e85bdcSAkhil Goyal 					ses->digest_length);
682c3e85bdcSAkhil Goyal 		else
683c3e85bdcSAkhil Goyal 			shared_desc_len = cnstr_shdsc_gcm_decap(
6847449390bSAkhil Goyal 					cdb->sh_desc, true, swap, SHR_NEVER,
685c3e85bdcSAkhil Goyal 					&alginfo,
686c3e85bdcSAkhil Goyal 					ses->iv.length,
687c3e85bdcSAkhil Goyal 					ses->digest_length);
688c3e85bdcSAkhil Goyal 	} else {
689c3e85bdcSAkhil Goyal 		caam_cipher_alg(ses, &alginfo_c);
690c3e85bdcSAkhil Goyal 		if (alginfo_c.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
691f163231eSHemant Agrawal 			DPAA_SEC_ERR("not supported cipher alg");
692c3e85bdcSAkhil Goyal 			return -ENOTSUP;
693c3e85bdcSAkhil Goyal 		}
694c3e85bdcSAkhil Goyal 
6950e5607e4SHemant Agrawal 		alginfo_c.key = (size_t)ses->cipher_key.data;
696c3e85bdcSAkhil Goyal 		alginfo_c.keylen = ses->cipher_key.length;
697c3e85bdcSAkhil Goyal 		alginfo_c.key_enc_flags = 0;
698c3e85bdcSAkhil Goyal 		alginfo_c.key_type = RTA_DATA_IMM;
699c3e85bdcSAkhil Goyal 
700c3e85bdcSAkhil Goyal 		caam_auth_alg(ses, &alginfo_a);
701c3e85bdcSAkhil Goyal 		if (alginfo_a.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
702f163231eSHemant Agrawal 			DPAA_SEC_ERR("not supported auth alg");
703c3e85bdcSAkhil Goyal 			return -ENOTSUP;
704c3e85bdcSAkhil Goyal 		}
705c3e85bdcSAkhil Goyal 
7060e5607e4SHemant Agrawal 		alginfo_a.key = (size_t)ses->auth_key.data;
707c3e85bdcSAkhil Goyal 		alginfo_a.keylen = ses->auth_key.length;
708c3e85bdcSAkhil Goyal 		alginfo_a.key_enc_flags = 0;
709c3e85bdcSAkhil Goyal 		alginfo_a.key_type = RTA_DATA_IMM;
710c3e85bdcSAkhil Goyal 
711c3e85bdcSAkhil Goyal 		cdb->sh_desc[0] = alginfo_c.keylen;
712c3e85bdcSAkhil Goyal 		cdb->sh_desc[1] = alginfo_a.keylen;
713c3e85bdcSAkhil Goyal 		err = rta_inline_query(IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN,
714c3e85bdcSAkhil Goyal 				       MIN_JOB_DESC_SIZE,
715c3e85bdcSAkhil Goyal 				       (unsigned int *)cdb->sh_desc,
716c3e85bdcSAkhil Goyal 				       &cdb->sh_desc[2], 2);
717c3e85bdcSAkhil Goyal 
718c3e85bdcSAkhil Goyal 		if (err < 0) {
719f163231eSHemant Agrawal 			DPAA_SEC_ERR("Crypto: Incorrect key lengths");
720c3e85bdcSAkhil Goyal 			return err;
721c3e85bdcSAkhil Goyal 		}
722c3e85bdcSAkhil Goyal 		if (cdb->sh_desc[2] & 1)
723c3e85bdcSAkhil Goyal 			alginfo_c.key_type = RTA_DATA_IMM;
724c3e85bdcSAkhil Goyal 		else {
7250e5607e4SHemant Agrawal 			alginfo_c.key = (size_t)dpaa_mem_vtop(
7260e5607e4SHemant Agrawal 						(void *)(size_t)alginfo_c.key);
727c3e85bdcSAkhil Goyal 			alginfo_c.key_type = RTA_DATA_PTR;
728c3e85bdcSAkhil Goyal 		}
729c3e85bdcSAkhil Goyal 		if (cdb->sh_desc[2] & (1<<1))
730c3e85bdcSAkhil Goyal 			alginfo_a.key_type = RTA_DATA_IMM;
731c3e85bdcSAkhil Goyal 		else {
7320e5607e4SHemant Agrawal 			alginfo_a.key = (size_t)dpaa_mem_vtop(
7330e5607e4SHemant Agrawal 						(void *)(size_t)alginfo_a.key);
734c3e85bdcSAkhil Goyal 			alginfo_a.key_type = RTA_DATA_PTR;
735c3e85bdcSAkhil Goyal 		}
736c3e85bdcSAkhil Goyal 		cdb->sh_desc[0] = 0;
737c3e85bdcSAkhil Goyal 		cdb->sh_desc[1] = 0;
738c3e85bdcSAkhil Goyal 		cdb->sh_desc[2] = 0;
7391f14d500SAkhil Goyal 		/* Auth_only_len is set as 0 here and it will be
7401f14d500SAkhil Goyal 		 * overwritten in fd for each packet.
741c3e85bdcSAkhil Goyal 		 */
742c3e85bdcSAkhil Goyal 		shared_desc_len = cnstr_shdsc_authenc(cdb->sh_desc,
7437449390bSAkhil Goyal 				true, swap, SHR_SERIAL, &alginfo_c, &alginfo_a,
744c3e85bdcSAkhil Goyal 				ses->iv.length, 0,
745c3e85bdcSAkhil Goyal 				ses->digest_length, ses->dir);
746c3e85bdcSAkhil Goyal 	}
74722788c2cSSunil Kumar Kori 
74822788c2cSSunil Kumar Kori 	if (shared_desc_len < 0) {
749f163231eSHemant Agrawal 		DPAA_SEC_ERR("error in preparing command block");
75022788c2cSSunil Kumar Kori 		return shared_desc_len;
75122788c2cSSunil Kumar Kori 	}
75222788c2cSSunil Kumar Kori 
753c3e85bdcSAkhil Goyal 	cdb->sh_hdr.hi.field.idlen = shared_desc_len;
754c3e85bdcSAkhil Goyal 	cdb->sh_hdr.hi.word = rte_cpu_to_be_32(cdb->sh_hdr.hi.word);
755c3e85bdcSAkhil Goyal 	cdb->sh_hdr.lo.word = rte_cpu_to_be_32(cdb->sh_hdr.lo.word);
756c3e85bdcSAkhil Goyal 
757c3e85bdcSAkhil Goyal 	return 0;
758c3e85bdcSAkhil Goyal }
759c3e85bdcSAkhil Goyal 
760c3e85bdcSAkhil Goyal /* qp is lockless, should be accessed by only one thread */
761c3e85bdcSAkhil Goyal static int
762c3e85bdcSAkhil Goyal dpaa_sec_deq(struct dpaa_sec_qp *qp, struct rte_crypto_op **ops, int nb_ops)
763c3e85bdcSAkhil Goyal {
764c3e85bdcSAkhil Goyal 	struct qman_fq *fq;
7659a984458SAkhil Goyal 	unsigned int pkts = 0;
766f40d5a53SNipun Gupta 	int num_rx_bufs, ret;
7679a984458SAkhil Goyal 	struct qm_dqrr_entry *dq;
768f40d5a53SNipun Gupta 	uint32_t vdqcr_flags = 0;
769c3e85bdcSAkhil Goyal 
770c3e85bdcSAkhil Goyal 	fq = &qp->outq;
771f40d5a53SNipun Gupta 	/*
772f40d5a53SNipun Gupta 	 * Until request for four buffers, we provide exact number of buffers.
773f40d5a53SNipun Gupta 	 * Otherwise we do not set the QM_VDQCR_EXACT flag.
774f40d5a53SNipun Gupta 	 * Not setting QM_VDQCR_EXACT flag can provide two more buffers than
775f40d5a53SNipun Gupta 	 * requested, so we request two less in this case.
776f40d5a53SNipun Gupta 	 */
777f40d5a53SNipun Gupta 	if (nb_ops < 4) {
778f40d5a53SNipun Gupta 		vdqcr_flags = QM_VDQCR_EXACT;
779f40d5a53SNipun Gupta 		num_rx_bufs = nb_ops;
780f40d5a53SNipun Gupta 	} else {
781f40d5a53SNipun Gupta 		num_rx_bufs = nb_ops > DPAA_MAX_DEQUEUE_NUM_FRAMES ?
782f40d5a53SNipun Gupta 			(DPAA_MAX_DEQUEUE_NUM_FRAMES - 2) : (nb_ops - 2);
783f40d5a53SNipun Gupta 	}
784f40d5a53SNipun Gupta 	ret = qman_set_vdq(fq, num_rx_bufs, vdqcr_flags);
7859a984458SAkhil Goyal 	if (ret)
7869a984458SAkhil Goyal 		return 0;
787c3e85bdcSAkhil Goyal 
7889a984458SAkhil Goyal 	do {
7899a984458SAkhil Goyal 		const struct qm_fd *fd;
7909a984458SAkhil Goyal 		struct dpaa_sec_job *job;
7919a984458SAkhil Goyal 		struct dpaa_sec_op_ctx *ctx;
7929a984458SAkhil Goyal 		struct rte_crypto_op *op;
793c3e85bdcSAkhil Goyal 
7949a984458SAkhil Goyal 		dq = qman_dequeue(fq);
7959a984458SAkhil Goyal 		if (!dq)
7969a984458SAkhil Goyal 			continue;
7979a984458SAkhil Goyal 
7989a984458SAkhil Goyal 		fd = &dq->fd;
7999a984458SAkhil Goyal 		/* sg is embedded in an op ctx,
8009a984458SAkhil Goyal 		 * sg[0] is for output
8019a984458SAkhil Goyal 		 * sg[1] for input
8029a984458SAkhil Goyal 		 */
8039a984458SAkhil Goyal 		job = dpaa_mem_ptov(qm_fd_addr_get64(fd));
8049a984458SAkhil Goyal 
8059a984458SAkhil Goyal 		ctx = container_of(job, struct dpaa_sec_op_ctx, job);
8069a984458SAkhil Goyal 		ctx->fd_status = fd->status;
8079a984458SAkhil Goyal 		op = ctx->op;
8089a984458SAkhil Goyal 		if (op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
8099a984458SAkhil Goyal 			struct qm_sg_entry *sg_out;
8109a984458SAkhil Goyal 			uint32_t len;
811fb5c100aSAkhil Goyal 			struct rte_mbuf *mbuf = (op->sym->m_dst == NULL) ?
812fb5c100aSAkhil Goyal 						op->sym->m_src : op->sym->m_dst;
8139a984458SAkhil Goyal 
8149a984458SAkhil Goyal 			sg_out = &job->sg[0];
8159a984458SAkhil Goyal 			hw_sg_to_cpu(sg_out);
8169a984458SAkhil Goyal 			len = sg_out->length;
817fb5c100aSAkhil Goyal 			mbuf->pkt_len = len;
818fb5c100aSAkhil Goyal 			while (mbuf->next != NULL) {
819fb5c100aSAkhil Goyal 				len -= mbuf->data_len;
820fb5c100aSAkhil Goyal 				mbuf = mbuf->next;
821fb5c100aSAkhil Goyal 			}
822fb5c100aSAkhil Goyal 			mbuf->data_len = len;
8239a984458SAkhil Goyal 		}
8249a984458SAkhil Goyal 		if (!ctx->fd_status) {
8259a984458SAkhil Goyal 			op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
8269a984458SAkhil Goyal 		} else {
827f163231eSHemant Agrawal 			DPAA_SEC_DP_WARN("SEC return err:0x%x", ctx->fd_status);
8289a984458SAkhil Goyal 			op->status = RTE_CRYPTO_OP_STATUS_ERROR;
8299a984458SAkhil Goyal 		}
8309a984458SAkhil Goyal 		ops[pkts++] = op;
8319a984458SAkhil Goyal 
8329a984458SAkhil Goyal 		/* report op status to sym->op and then free the ctx memeory */
8339a984458SAkhil Goyal 		rte_mempool_put(ctx->ctx_pool, (void *)ctx);
8349a984458SAkhil Goyal 
8359a984458SAkhil Goyal 		qman_dqrr_consume(fq, dq);
8369a984458SAkhil Goyal 	} while (fq->flags & QMAN_FQ_STATE_VDQCR);
8379a984458SAkhil Goyal 
8389a984458SAkhil Goyal 	return pkts;
839c3e85bdcSAkhil Goyal }
840c3e85bdcSAkhil Goyal 
841a74af788SAkhil Goyal static inline struct dpaa_sec_job *
842a74af788SAkhil Goyal build_auth_only_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
843a74af788SAkhil Goyal {
844a74af788SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
845a74af788SAkhil Goyal 	struct rte_mbuf *mbuf = sym->m_src;
846a74af788SAkhil Goyal 	struct dpaa_sec_job *cf;
847a74af788SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
848a74af788SAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
849a74af788SAkhil Goyal 	phys_addr_t start_addr;
850a74af788SAkhil Goyal 	uint8_t *old_digest, extra_segs;
851a74af788SAkhil Goyal 
852a74af788SAkhil Goyal 	if (is_decode(ses))
853a74af788SAkhil Goyal 		extra_segs = 3;
854a74af788SAkhil Goyal 	else
855a74af788SAkhil Goyal 		extra_segs = 2;
856a74af788SAkhil Goyal 
857*f7a5752eSHemant Agrawal 	if (mbuf->nb_segs > MAX_SG_ENTRIES) {
858f163231eSHemant Agrawal 		DPAA_SEC_DP_ERR("Auth: Max sec segs supported is %d",
859a74af788SAkhil Goyal 				MAX_SG_ENTRIES);
860a74af788SAkhil Goyal 		return NULL;
861a74af788SAkhil Goyal 	}
862*f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, mbuf->nb_segs + extra_segs);
863a74af788SAkhil Goyal 	if (!ctx)
864a74af788SAkhil Goyal 		return NULL;
865a74af788SAkhil Goyal 
866a74af788SAkhil Goyal 	cf = &ctx->job;
867a74af788SAkhil Goyal 	ctx->op = op;
868a74af788SAkhil Goyal 	old_digest = ctx->digest;
869a74af788SAkhil Goyal 
870a74af788SAkhil Goyal 	/* output */
871a74af788SAkhil Goyal 	out_sg = &cf->sg[0];
872a74af788SAkhil Goyal 	qm_sg_entry_set64(out_sg, sym->auth.digest.phys_addr);
873a74af788SAkhil Goyal 	out_sg->length = ses->digest_length;
874a74af788SAkhil Goyal 	cpu_to_hw_sg(out_sg);
875a74af788SAkhil Goyal 
876a74af788SAkhil Goyal 	/* input */
877a74af788SAkhil Goyal 	in_sg = &cf->sg[1];
878a74af788SAkhil Goyal 	/* need to extend the input to a compound frame */
879a74af788SAkhil Goyal 	in_sg->extension = 1;
880a74af788SAkhil Goyal 	in_sg->final = 1;
881a74af788SAkhil Goyal 	in_sg->length = sym->auth.data.length;
88295456e89SShreyansh Jain 	qm_sg_entry_set64(in_sg, dpaa_mem_vtop(&cf->sg[2]));
883a74af788SAkhil Goyal 
884a74af788SAkhil Goyal 	/* 1st seg */
885a74af788SAkhil Goyal 	sg = in_sg + 1;
886a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
887a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->auth.data.offset;
888a74af788SAkhil Goyal 	sg->offset = sym->auth.data.offset;
889a74af788SAkhil Goyal 
890a74af788SAkhil Goyal 	/* Successive segs */
891a74af788SAkhil Goyal 	mbuf = mbuf->next;
892a74af788SAkhil Goyal 	while (mbuf) {
893a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
894a74af788SAkhil Goyal 		sg++;
895a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
896a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
897a74af788SAkhil Goyal 		mbuf = mbuf->next;
898a74af788SAkhil Goyal 	}
899a74af788SAkhil Goyal 
900a74af788SAkhil Goyal 	if (is_decode(ses)) {
901a74af788SAkhil Goyal 		/* Digest verification case */
902a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
903a74af788SAkhil Goyal 		sg++;
904a74af788SAkhil Goyal 		rte_memcpy(old_digest, sym->auth.digest.data,
905a74af788SAkhil Goyal 				ses->digest_length);
90695456e89SShreyansh Jain 		start_addr = dpaa_mem_vtop(old_digest);
907a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, start_addr);
908a74af788SAkhil Goyal 		sg->length = ses->digest_length;
909a74af788SAkhil Goyal 		in_sg->length += ses->digest_length;
910a74af788SAkhil Goyal 	} else {
911a74af788SAkhil Goyal 		/* Digest calculation case */
912a74af788SAkhil Goyal 		sg->length -= ses->digest_length;
913a74af788SAkhil Goyal 	}
914a74af788SAkhil Goyal 	sg->final = 1;
915a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
916a74af788SAkhil Goyal 	cpu_to_hw_sg(in_sg);
917a74af788SAkhil Goyal 
918a74af788SAkhil Goyal 	return cf;
919a74af788SAkhil Goyal }
920a74af788SAkhil Goyal 
921c3e85bdcSAkhil Goyal /**
922c3e85bdcSAkhil Goyal  * packet looks like:
923c3e85bdcSAkhil Goyal  *		|<----data_len------->|
924c3e85bdcSAkhil Goyal  *    |ip_header|ah_header|icv|payload|
925c3e85bdcSAkhil Goyal  *              ^
926c3e85bdcSAkhil Goyal  *		|
927c3e85bdcSAkhil Goyal  *	   mbuf->pkt.data
928c3e85bdcSAkhil Goyal  */
929c3e85bdcSAkhil Goyal static inline struct dpaa_sec_job *
930c3e85bdcSAkhil Goyal build_auth_only(struct rte_crypto_op *op, dpaa_sec_session *ses)
931c3e85bdcSAkhil Goyal {
932c3e85bdcSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
933c3e85bdcSAkhil Goyal 	struct rte_mbuf *mbuf = sym->m_src;
934c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
935c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
936c3e85bdcSAkhil Goyal 	struct qm_sg_entry *sg;
937c4509373SSantosh Shukla 	rte_iova_t start_addr;
938c3e85bdcSAkhil Goyal 	uint8_t *old_digest;
939c3e85bdcSAkhil Goyal 
940*f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, 4);
941c3e85bdcSAkhil Goyal 	if (!ctx)
942c3e85bdcSAkhil Goyal 		return NULL;
943c3e85bdcSAkhil Goyal 
944c3e85bdcSAkhil Goyal 	cf = &ctx->job;
945c3e85bdcSAkhil Goyal 	ctx->op = op;
946c3e85bdcSAkhil Goyal 	old_digest = ctx->digest;
947c3e85bdcSAkhil Goyal 
948bfa9a8a4SThomas Monjalon 	start_addr = rte_pktmbuf_iova(mbuf);
949c3e85bdcSAkhil Goyal 	/* output */
950c3e85bdcSAkhil Goyal 	sg = &cf->sg[0];
951c3e85bdcSAkhil Goyal 	qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
952c3e85bdcSAkhil Goyal 	sg->length = ses->digest_length;
953c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
954c3e85bdcSAkhil Goyal 
955c3e85bdcSAkhil Goyal 	/* input */
956c3e85bdcSAkhil Goyal 	sg = &cf->sg[1];
957c3e85bdcSAkhil Goyal 	if (is_decode(ses)) {
958c3e85bdcSAkhil Goyal 		/* need to extend the input to a compound frame */
959c3e85bdcSAkhil Goyal 		sg->extension = 1;
96095456e89SShreyansh Jain 		qm_sg_entry_set64(sg, dpaa_mem_vtop(&cf->sg[2]));
961c3e85bdcSAkhil Goyal 		sg->length = sym->auth.data.length + ses->digest_length;
962c3e85bdcSAkhil Goyal 		sg->final = 1;
963c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
964c3e85bdcSAkhil Goyal 
965c3e85bdcSAkhil Goyal 		sg = &cf->sg[2];
966c3e85bdcSAkhil Goyal 		/* hash result or digest, save digest first */
967c3e85bdcSAkhil Goyal 		rte_memcpy(old_digest, sym->auth.digest.data,
968c3e85bdcSAkhil Goyal 			   ses->digest_length);
969c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, start_addr + sym->auth.data.offset);
970c3e85bdcSAkhil Goyal 		sg->length = sym->auth.data.length;
971c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
972c3e85bdcSAkhil Goyal 
973c3e85bdcSAkhil Goyal 		/* let's check digest by hw */
97495456e89SShreyansh Jain 		start_addr = dpaa_mem_vtop(old_digest);
975c3e85bdcSAkhil Goyal 		sg++;
976c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, start_addr);
977c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
978c3e85bdcSAkhil Goyal 		sg->final = 1;
979c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
980c3e85bdcSAkhil Goyal 	} else {
981c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, start_addr + sym->auth.data.offset);
982c3e85bdcSAkhil Goyal 		sg->length = sym->auth.data.length;
983c3e85bdcSAkhil Goyal 		sg->final = 1;
984c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
985c3e85bdcSAkhil Goyal 	}
986c3e85bdcSAkhil Goyal 
987c3e85bdcSAkhil Goyal 	return cf;
988c3e85bdcSAkhil Goyal }
989c3e85bdcSAkhil Goyal 
990c3e85bdcSAkhil Goyal static inline struct dpaa_sec_job *
991a74af788SAkhil Goyal build_cipher_only_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
992a74af788SAkhil Goyal {
993a74af788SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
994a74af788SAkhil Goyal 	struct dpaa_sec_job *cf;
995a74af788SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
996a74af788SAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
997a74af788SAkhil Goyal 	struct rte_mbuf *mbuf;
998a74af788SAkhil Goyal 	uint8_t req_segs;
999a74af788SAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1000a74af788SAkhil Goyal 			ses->iv.offset);
1001a74af788SAkhil Goyal 
1002a74af788SAkhil Goyal 	if (sym->m_dst) {
1003a74af788SAkhil Goyal 		mbuf = sym->m_dst;
1004a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 3;
1005a74af788SAkhil Goyal 	} else {
1006a74af788SAkhil Goyal 		mbuf = sym->m_src;
1007a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs * 2 + 3;
1008a74af788SAkhil Goyal 	}
1009a74af788SAkhil Goyal 
1010*f7a5752eSHemant Agrawal 	if (mbuf->nb_segs > MAX_SG_ENTRIES) {
1011f163231eSHemant Agrawal 		DPAA_SEC_DP_ERR("Cipher: Max sec segs supported is %d",
1012a74af788SAkhil Goyal 				MAX_SG_ENTRIES);
1013a74af788SAkhil Goyal 		return NULL;
1014a74af788SAkhil Goyal 	}
1015a74af788SAkhil Goyal 
1016*f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, req_segs);
1017a74af788SAkhil Goyal 	if (!ctx)
1018a74af788SAkhil Goyal 		return NULL;
1019a74af788SAkhil Goyal 
1020a74af788SAkhil Goyal 	cf = &ctx->job;
1021a74af788SAkhil Goyal 	ctx->op = op;
1022a74af788SAkhil Goyal 
1023a74af788SAkhil Goyal 	/* output */
1024a74af788SAkhil Goyal 	out_sg = &cf->sg[0];
1025a74af788SAkhil Goyal 	out_sg->extension = 1;
1026a74af788SAkhil Goyal 	out_sg->length = sym->cipher.data.length;
102795456e89SShreyansh Jain 	qm_sg_entry_set64(out_sg, dpaa_mem_vtop(&cf->sg[2]));
1028a74af788SAkhil Goyal 	cpu_to_hw_sg(out_sg);
1029a74af788SAkhil Goyal 
1030a74af788SAkhil Goyal 	/* 1st seg */
1031a74af788SAkhil Goyal 	sg = &cf->sg[2];
1032a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1033a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->cipher.data.offset;
1034a74af788SAkhil Goyal 	sg->offset = sym->cipher.data.offset;
1035a74af788SAkhil Goyal 
1036a74af788SAkhil Goyal 	/* Successive segs */
1037a74af788SAkhil Goyal 	mbuf = mbuf->next;
1038a74af788SAkhil Goyal 	while (mbuf) {
1039a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1040a74af788SAkhil Goyal 		sg++;
1041a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1042a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1043a74af788SAkhil Goyal 		mbuf = mbuf->next;
1044a74af788SAkhil Goyal 	}
1045a74af788SAkhil Goyal 	sg->final = 1;
1046a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1047a74af788SAkhil Goyal 
1048a74af788SAkhil Goyal 	/* input */
1049a74af788SAkhil Goyal 	mbuf = sym->m_src;
1050a74af788SAkhil Goyal 	in_sg = &cf->sg[1];
1051a74af788SAkhil Goyal 	in_sg->extension = 1;
1052a74af788SAkhil Goyal 	in_sg->final = 1;
1053a74af788SAkhil Goyal 	in_sg->length = sym->cipher.data.length + ses->iv.length;
1054a74af788SAkhil Goyal 
1055a74af788SAkhil Goyal 	sg++;
105695456e89SShreyansh Jain 	qm_sg_entry_set64(in_sg, dpaa_mem_vtop(sg));
1057a74af788SAkhil Goyal 	cpu_to_hw_sg(in_sg);
1058a74af788SAkhil Goyal 
1059a74af788SAkhil Goyal 	/* IV */
1060a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1061a74af788SAkhil Goyal 	sg->length = ses->iv.length;
1062a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1063a74af788SAkhil Goyal 
1064a74af788SAkhil Goyal 	/* 1st seg */
1065a74af788SAkhil Goyal 	sg++;
1066a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1067a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->cipher.data.offset;
1068a74af788SAkhil Goyal 	sg->offset = sym->cipher.data.offset;
1069a74af788SAkhil Goyal 
1070a74af788SAkhil Goyal 	/* Successive segs */
1071a74af788SAkhil Goyal 	mbuf = mbuf->next;
1072a74af788SAkhil Goyal 	while (mbuf) {
1073a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1074a74af788SAkhil Goyal 		sg++;
1075a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1076a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1077a74af788SAkhil Goyal 		mbuf = mbuf->next;
1078a74af788SAkhil Goyal 	}
1079a74af788SAkhil Goyal 	sg->final = 1;
1080a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1081a74af788SAkhil Goyal 
1082a74af788SAkhil Goyal 	return cf;
1083a74af788SAkhil Goyal }
1084a74af788SAkhil Goyal 
1085a74af788SAkhil Goyal static inline struct dpaa_sec_job *
1086c3e85bdcSAkhil Goyal build_cipher_only(struct rte_crypto_op *op, dpaa_sec_session *ses)
1087c3e85bdcSAkhil Goyal {
1088c3e85bdcSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1089c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
1090c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1091c3e85bdcSAkhil Goyal 	struct qm_sg_entry *sg;
1092c4509373SSantosh Shukla 	rte_iova_t src_start_addr, dst_start_addr;
1093c3e85bdcSAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1094c3e85bdcSAkhil Goyal 			ses->iv.offset);
1095c3e85bdcSAkhil Goyal 
1096*f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, 4);
1097c3e85bdcSAkhil Goyal 	if (!ctx)
1098c3e85bdcSAkhil Goyal 		return NULL;
1099c3e85bdcSAkhil Goyal 
1100c3e85bdcSAkhil Goyal 	cf = &ctx->job;
1101c3e85bdcSAkhil Goyal 	ctx->op = op;
1102a389434eSAlok Makhariya 
1103bfa9a8a4SThomas Monjalon 	src_start_addr = rte_pktmbuf_iova(sym->m_src);
1104a389434eSAlok Makhariya 
1105a389434eSAlok Makhariya 	if (sym->m_dst)
1106bfa9a8a4SThomas Monjalon 		dst_start_addr = rte_pktmbuf_iova(sym->m_dst);
1107a389434eSAlok Makhariya 	else
1108a389434eSAlok Makhariya 		dst_start_addr = src_start_addr;
1109c3e85bdcSAkhil Goyal 
1110c3e85bdcSAkhil Goyal 	/* output */
1111c3e85bdcSAkhil Goyal 	sg = &cf->sg[0];
1112a389434eSAlok Makhariya 	qm_sg_entry_set64(sg, dst_start_addr + sym->cipher.data.offset);
1113c3e85bdcSAkhil Goyal 	sg->length = sym->cipher.data.length + ses->iv.length;
1114c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1115c3e85bdcSAkhil Goyal 
1116c3e85bdcSAkhil Goyal 	/* input */
1117c3e85bdcSAkhil Goyal 	sg = &cf->sg[1];
1118c3e85bdcSAkhil Goyal 
1119c3e85bdcSAkhil Goyal 	/* need to extend the input to a compound frame */
1120c3e85bdcSAkhil Goyal 	sg->extension = 1;
1121c3e85bdcSAkhil Goyal 	sg->final = 1;
1122c3e85bdcSAkhil Goyal 	sg->length = sym->cipher.data.length + ses->iv.length;
112395456e89SShreyansh Jain 	qm_sg_entry_set64(sg, dpaa_mem_vtop(&cf->sg[2]));
1124c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1125c3e85bdcSAkhil Goyal 
1126c3e85bdcSAkhil Goyal 	sg = &cf->sg[2];
1127c3e85bdcSAkhil Goyal 	qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1128c3e85bdcSAkhil Goyal 	sg->length = ses->iv.length;
1129c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1130c3e85bdcSAkhil Goyal 
1131c3e85bdcSAkhil Goyal 	sg++;
1132a389434eSAlok Makhariya 	qm_sg_entry_set64(sg, src_start_addr + sym->cipher.data.offset);
1133c3e85bdcSAkhil Goyal 	sg->length = sym->cipher.data.length;
1134c3e85bdcSAkhil Goyal 	sg->final = 1;
1135c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1136c3e85bdcSAkhil Goyal 
1137c3e85bdcSAkhil Goyal 	return cf;
1138c3e85bdcSAkhil Goyal }
1139c3e85bdcSAkhil Goyal 
1140c3e85bdcSAkhil Goyal static inline struct dpaa_sec_job *
1141a74af788SAkhil Goyal build_cipher_auth_gcm_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
1142a74af788SAkhil Goyal {
1143a74af788SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1144a74af788SAkhil Goyal 	struct dpaa_sec_job *cf;
1145a74af788SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1146a74af788SAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
1147a74af788SAkhil Goyal 	struct rte_mbuf *mbuf;
1148a74af788SAkhil Goyal 	uint8_t req_segs;
1149a74af788SAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1150a74af788SAkhil Goyal 			ses->iv.offset);
1151a74af788SAkhil Goyal 
1152a74af788SAkhil Goyal 	if (sym->m_dst) {
1153a74af788SAkhil Goyal 		mbuf = sym->m_dst;
1154a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 4;
1155a74af788SAkhil Goyal 	} else {
1156a74af788SAkhil Goyal 		mbuf = sym->m_src;
1157a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs * 2 + 4;
1158a74af788SAkhil Goyal 	}
1159a74af788SAkhil Goyal 
1160a74af788SAkhil Goyal 	if (ses->auth_only_len)
1161a74af788SAkhil Goyal 		req_segs++;
1162a74af788SAkhil Goyal 
1163*f7a5752eSHemant Agrawal 	if (mbuf->nb_segs > MAX_SG_ENTRIES) {
1164f163231eSHemant Agrawal 		DPAA_SEC_DP_ERR("AEAD: Max sec segs supported is %d",
1165a74af788SAkhil Goyal 				MAX_SG_ENTRIES);
1166a74af788SAkhil Goyal 		return NULL;
1167a74af788SAkhil Goyal 	}
1168a74af788SAkhil Goyal 
1169*f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, req_segs);
1170a74af788SAkhil Goyal 	if (!ctx)
1171a74af788SAkhil Goyal 		return NULL;
1172a74af788SAkhil Goyal 
1173a74af788SAkhil Goyal 	cf = &ctx->job;
1174a74af788SAkhil Goyal 	ctx->op = op;
1175a74af788SAkhil Goyal 
1176a74af788SAkhil Goyal 	rte_prefetch0(cf->sg);
1177a74af788SAkhil Goyal 
1178a74af788SAkhil Goyal 	/* output */
1179a74af788SAkhil Goyal 	out_sg = &cf->sg[0];
1180a74af788SAkhil Goyal 	out_sg->extension = 1;
1181a74af788SAkhil Goyal 	if (is_encode(ses))
1182a74af788SAkhil Goyal 		out_sg->length = sym->aead.data.length + ses->auth_only_len
1183a74af788SAkhil Goyal 						+ ses->digest_length;
1184a74af788SAkhil Goyal 	else
1185a74af788SAkhil Goyal 		out_sg->length = sym->aead.data.length + ses->auth_only_len;
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));
1194a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->aead.data.offset +
1195a74af788SAkhil Goyal 					ses->auth_only_len;
1196a74af788SAkhil Goyal 	sg->offset = sym->aead.data.offset - ses->auth_only_len;
1197a74af788SAkhil Goyal 
1198a74af788SAkhil Goyal 	/* Successive segs */
1199a74af788SAkhil Goyal 	mbuf = mbuf->next;
1200a74af788SAkhil Goyal 	while (mbuf) {
1201a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1202a74af788SAkhil Goyal 		sg++;
1203a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1204a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1205a74af788SAkhil Goyal 		mbuf = mbuf->next;
1206a74af788SAkhil Goyal 	}
1207a74af788SAkhil Goyal 	sg->length -= ses->digest_length;
1208a74af788SAkhil Goyal 
1209a74af788SAkhil Goyal 	if (is_encode(ses)) {
1210a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1211a74af788SAkhil Goyal 		/* set auth output */
1212a74af788SAkhil Goyal 		sg++;
1213a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, sym->aead.digest.phys_addr);
1214a74af788SAkhil Goyal 		sg->length = ses->digest_length;
1215a74af788SAkhil Goyal 	}
1216a74af788SAkhil Goyal 	sg->final = 1;
1217a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1218a74af788SAkhil Goyal 
1219a74af788SAkhil Goyal 	/* input */
1220a74af788SAkhil Goyal 	mbuf = sym->m_src;
1221a74af788SAkhil Goyal 	in_sg = &cf->sg[1];
1222a74af788SAkhil Goyal 	in_sg->extension = 1;
1223a74af788SAkhil Goyal 	in_sg->final = 1;
1224a74af788SAkhil Goyal 	if (is_encode(ses))
1225a74af788SAkhil Goyal 		in_sg->length = ses->iv.length + sym->aead.data.length
1226a74af788SAkhil Goyal 							+ ses->auth_only_len;
1227a74af788SAkhil Goyal 	else
1228a74af788SAkhil Goyal 		in_sg->length = ses->iv.length + sym->aead.data.length
1229a74af788SAkhil Goyal 				+ ses->auth_only_len + ses->digest_length;
1230a74af788SAkhil Goyal 
1231a74af788SAkhil Goyal 	/* input sg entries */
1232a74af788SAkhil Goyal 	sg++;
123395456e89SShreyansh Jain 	qm_sg_entry_set64(in_sg, dpaa_mem_vtop(sg));
1234a74af788SAkhil Goyal 	cpu_to_hw_sg(in_sg);
1235a74af788SAkhil Goyal 
1236a74af788SAkhil Goyal 	/* 1st seg IV */
1237a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1238a74af788SAkhil Goyal 	sg->length = ses->iv.length;
1239a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1240a74af788SAkhil Goyal 
1241a74af788SAkhil Goyal 	/* 2nd seg auth only */
1242a74af788SAkhil Goyal 	if (ses->auth_only_len) {
1243a74af788SAkhil Goyal 		sg++;
1244a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, dpaa_mem_vtop(sym->aead.aad.data));
1245a74af788SAkhil Goyal 		sg->length = ses->auth_only_len;
1246a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1247a74af788SAkhil Goyal 	}
1248a74af788SAkhil Goyal 
1249a74af788SAkhil Goyal 	/* 3rd seg */
1250a74af788SAkhil Goyal 	sg++;
1251a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1252a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->aead.data.offset;
1253a74af788SAkhil Goyal 	sg->offset = sym->aead.data.offset;
1254a74af788SAkhil Goyal 
1255a74af788SAkhil Goyal 	/* Successive segs */
1256a74af788SAkhil Goyal 	mbuf = mbuf->next;
1257a74af788SAkhil Goyal 	while (mbuf) {
1258a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1259a74af788SAkhil Goyal 		sg++;
1260a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1261a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1262a74af788SAkhil Goyal 		mbuf = mbuf->next;
1263a74af788SAkhil Goyal 	}
1264a74af788SAkhil Goyal 
1265a74af788SAkhil Goyal 	if (is_decode(ses)) {
1266a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1267a74af788SAkhil Goyal 		sg++;
1268a74af788SAkhil Goyal 		memcpy(ctx->digest, sym->aead.digest.data,
1269a74af788SAkhil Goyal 			ses->digest_length);
127095456e89SShreyansh Jain 		qm_sg_entry_set64(sg, dpaa_mem_vtop(ctx->digest));
1271a74af788SAkhil Goyal 		sg->length = ses->digest_length;
1272a74af788SAkhil Goyal 	}
1273a74af788SAkhil Goyal 	sg->final = 1;
1274a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1275a74af788SAkhil Goyal 
1276a74af788SAkhil Goyal 	return cf;
1277a74af788SAkhil Goyal }
1278a74af788SAkhil Goyal 
1279a74af788SAkhil Goyal static inline struct dpaa_sec_job *
1280c3e85bdcSAkhil Goyal build_cipher_auth_gcm(struct rte_crypto_op *op, dpaa_sec_session *ses)
1281c3e85bdcSAkhil Goyal {
1282c3e85bdcSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1283c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
1284c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1285c3e85bdcSAkhil Goyal 	struct qm_sg_entry *sg;
1286c3e85bdcSAkhil Goyal 	uint32_t length = 0;
1287c4509373SSantosh Shukla 	rte_iova_t src_start_addr, dst_start_addr;
1288c3e85bdcSAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1289c3e85bdcSAkhil Goyal 			ses->iv.offset);
1290c3e85bdcSAkhil Goyal 
1291116ff44aSHemant Agrawal 	src_start_addr = sym->m_src->buf_iova + sym->m_src->data_off;
1292a389434eSAlok Makhariya 
1293a389434eSAlok Makhariya 	if (sym->m_dst)
1294116ff44aSHemant Agrawal 		dst_start_addr = sym->m_dst->buf_iova + sym->m_dst->data_off;
1295a389434eSAlok Makhariya 	else
1296a389434eSAlok Makhariya 		dst_start_addr = src_start_addr;
1297c3e85bdcSAkhil Goyal 
1298*f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, 7);
1299c3e85bdcSAkhil Goyal 	if (!ctx)
1300c3e85bdcSAkhil Goyal 		return NULL;
1301c3e85bdcSAkhil Goyal 
1302c3e85bdcSAkhil Goyal 	cf = &ctx->job;
1303c3e85bdcSAkhil Goyal 	ctx->op = op;
1304c3e85bdcSAkhil Goyal 
1305c3e85bdcSAkhil Goyal 	/* input */
1306c3e85bdcSAkhil Goyal 	rte_prefetch0(cf->sg);
1307c3e85bdcSAkhil Goyal 	sg = &cf->sg[2];
130895456e89SShreyansh Jain 	qm_sg_entry_set64(&cf->sg[1], dpaa_mem_vtop(sg));
1309c3e85bdcSAkhil Goyal 	if (is_encode(ses)) {
1310c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1311c3e85bdcSAkhil Goyal 		sg->length = ses->iv.length;
1312c3e85bdcSAkhil Goyal 		length += sg->length;
1313c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1314c3e85bdcSAkhil Goyal 
1315c3e85bdcSAkhil Goyal 		sg++;
1316c3e85bdcSAkhil Goyal 		if (ses->auth_only_len) {
1317c3e85bdcSAkhil Goyal 			qm_sg_entry_set64(sg,
1318c3e85bdcSAkhil Goyal 					  dpaa_mem_vtop(sym->aead.aad.data));
1319c3e85bdcSAkhil Goyal 			sg->length = ses->auth_only_len;
1320c3e85bdcSAkhil Goyal 			length += sg->length;
1321c3e85bdcSAkhil Goyal 			cpu_to_hw_sg(sg);
1322c3e85bdcSAkhil Goyal 			sg++;
1323c3e85bdcSAkhil Goyal 		}
1324a389434eSAlok Makhariya 		qm_sg_entry_set64(sg, src_start_addr + sym->aead.data.offset);
1325c3e85bdcSAkhil Goyal 		sg->length = sym->aead.data.length;
1326c3e85bdcSAkhil Goyal 		length += sg->length;
1327c3e85bdcSAkhil Goyal 		sg->final = 1;
1328c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1329c3e85bdcSAkhil Goyal 	} else {
1330c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1331c3e85bdcSAkhil Goyal 		sg->length = ses->iv.length;
1332c3e85bdcSAkhil Goyal 		length += sg->length;
1333c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1334c3e85bdcSAkhil Goyal 
1335c3e85bdcSAkhil Goyal 		sg++;
1336c3e85bdcSAkhil Goyal 		if (ses->auth_only_len) {
1337c3e85bdcSAkhil Goyal 			qm_sg_entry_set64(sg,
1338c3e85bdcSAkhil Goyal 					  dpaa_mem_vtop(sym->aead.aad.data));
1339c3e85bdcSAkhil Goyal 			sg->length = ses->auth_only_len;
1340c3e85bdcSAkhil Goyal 			length += sg->length;
1341c3e85bdcSAkhil Goyal 			cpu_to_hw_sg(sg);
1342c3e85bdcSAkhil Goyal 			sg++;
1343c3e85bdcSAkhil Goyal 		}
1344a389434eSAlok Makhariya 		qm_sg_entry_set64(sg, src_start_addr + sym->aead.data.offset);
1345c3e85bdcSAkhil Goyal 		sg->length = sym->aead.data.length;
1346c3e85bdcSAkhil Goyal 		length += sg->length;
1347c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1348c3e85bdcSAkhil Goyal 
1349c3e85bdcSAkhil Goyal 		memcpy(ctx->digest, sym->aead.digest.data,
1350c3e85bdcSAkhil Goyal 		       ses->digest_length);
1351c3e85bdcSAkhil Goyal 		sg++;
1352c3e85bdcSAkhil Goyal 
135395456e89SShreyansh Jain 		qm_sg_entry_set64(sg, dpaa_mem_vtop(ctx->digest));
1354c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
1355c3e85bdcSAkhil Goyal 		length += sg->length;
1356c3e85bdcSAkhil Goyal 		sg->final = 1;
1357c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1358c3e85bdcSAkhil Goyal 	}
1359c3e85bdcSAkhil Goyal 	/* input compound frame */
1360c3e85bdcSAkhil Goyal 	cf->sg[1].length = length;
1361c3e85bdcSAkhil Goyal 	cf->sg[1].extension = 1;
1362c3e85bdcSAkhil Goyal 	cf->sg[1].final = 1;
1363c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(&cf->sg[1]);
1364c3e85bdcSAkhil Goyal 
1365c3e85bdcSAkhil Goyal 	/* output */
1366c3e85bdcSAkhil Goyal 	sg++;
136795456e89SShreyansh Jain 	qm_sg_entry_set64(&cf->sg[0], dpaa_mem_vtop(sg));
1368c3e85bdcSAkhil Goyal 	qm_sg_entry_set64(sg,
1369a389434eSAlok Makhariya 		dst_start_addr + sym->aead.data.offset - ses->auth_only_len);
1370c3e85bdcSAkhil Goyal 	sg->length = sym->aead.data.length + ses->auth_only_len;
1371c3e85bdcSAkhil Goyal 	length = sg->length;
1372c3e85bdcSAkhil Goyal 	if (is_encode(ses)) {
1373c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1374c3e85bdcSAkhil Goyal 		/* set auth output */
1375c3e85bdcSAkhil Goyal 		sg++;
1376c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, sym->aead.digest.phys_addr);
1377c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
1378c3e85bdcSAkhil Goyal 		length += sg->length;
1379c3e85bdcSAkhil Goyal 	}
1380c3e85bdcSAkhil Goyal 	sg->final = 1;
1381c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1382c3e85bdcSAkhil Goyal 
1383c3e85bdcSAkhil Goyal 	/* output compound frame */
1384c3e85bdcSAkhil Goyal 	cf->sg[0].length = length;
1385c3e85bdcSAkhil Goyal 	cf->sg[0].extension = 1;
1386c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(&cf->sg[0]);
1387c3e85bdcSAkhil Goyal 
1388c3e85bdcSAkhil Goyal 	return cf;
1389c3e85bdcSAkhil Goyal }
1390c3e85bdcSAkhil Goyal 
1391c3e85bdcSAkhil Goyal static inline struct dpaa_sec_job *
1392a74af788SAkhil Goyal build_cipher_auth_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
1393a74af788SAkhil Goyal {
1394a74af788SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1395a74af788SAkhil Goyal 	struct dpaa_sec_job *cf;
1396a74af788SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1397a74af788SAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
1398a74af788SAkhil Goyal 	struct rte_mbuf *mbuf;
1399a74af788SAkhil Goyal 	uint8_t req_segs;
1400a74af788SAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1401a74af788SAkhil Goyal 			ses->iv.offset);
1402a74af788SAkhil Goyal 
1403a74af788SAkhil Goyal 	if (sym->m_dst) {
1404a74af788SAkhil Goyal 		mbuf = sym->m_dst;
1405a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 4;
1406a74af788SAkhil Goyal 	} else {
1407a74af788SAkhil Goyal 		mbuf = sym->m_src;
1408a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs * 2 + 4;
1409a74af788SAkhil Goyal 	}
1410a74af788SAkhil Goyal 
1411*f7a5752eSHemant Agrawal 	if (mbuf->nb_segs > MAX_SG_ENTRIES) {
1412f163231eSHemant Agrawal 		DPAA_SEC_DP_ERR("Cipher-Auth: Max sec segs supported is %d",
1413a74af788SAkhil Goyal 				MAX_SG_ENTRIES);
1414a74af788SAkhil Goyal 		return NULL;
1415a74af788SAkhil Goyal 	}
1416a74af788SAkhil Goyal 
1417*f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, req_segs);
1418a74af788SAkhil Goyal 	if (!ctx)
1419a74af788SAkhil Goyal 		return NULL;
1420a74af788SAkhil Goyal 
1421a74af788SAkhil Goyal 	cf = &ctx->job;
1422a74af788SAkhil Goyal 	ctx->op = op;
1423a74af788SAkhil Goyal 
1424a74af788SAkhil Goyal 	rte_prefetch0(cf->sg);
1425a74af788SAkhil Goyal 
1426a74af788SAkhil Goyal 	/* output */
1427a74af788SAkhil Goyal 	out_sg = &cf->sg[0];
1428a74af788SAkhil Goyal 	out_sg->extension = 1;
1429a74af788SAkhil Goyal 	if (is_encode(ses))
1430a74af788SAkhil Goyal 		out_sg->length = sym->auth.data.length + ses->digest_length;
1431a74af788SAkhil Goyal 	else
1432a74af788SAkhil Goyal 		out_sg->length = sym->auth.data.length;
1433a74af788SAkhil Goyal 
1434a74af788SAkhil Goyal 	/* output sg entries */
1435a74af788SAkhil Goyal 	sg = &cf->sg[2];
143695456e89SShreyansh Jain 	qm_sg_entry_set64(out_sg, dpaa_mem_vtop(sg));
1437a74af788SAkhil Goyal 	cpu_to_hw_sg(out_sg);
1438a74af788SAkhil Goyal 
1439a74af788SAkhil Goyal 	/* 1st seg */
1440a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1441a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->auth.data.offset;
1442a74af788SAkhil Goyal 	sg->offset = sym->auth.data.offset;
1443a74af788SAkhil Goyal 
1444a74af788SAkhil Goyal 	/* Successive segs */
1445a74af788SAkhil Goyal 	mbuf = mbuf->next;
1446a74af788SAkhil Goyal 	while (mbuf) {
1447a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1448a74af788SAkhil Goyal 		sg++;
1449a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1450a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1451a74af788SAkhil Goyal 		mbuf = mbuf->next;
1452a74af788SAkhil Goyal 	}
1453a74af788SAkhil Goyal 	sg->length -= ses->digest_length;
1454a74af788SAkhil Goyal 
1455a74af788SAkhil Goyal 	if (is_encode(ses)) {
1456a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1457a74af788SAkhil Goyal 		/* set auth output */
1458a74af788SAkhil Goyal 		sg++;
1459a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
1460a74af788SAkhil Goyal 		sg->length = ses->digest_length;
1461a74af788SAkhil Goyal 	}
1462a74af788SAkhil Goyal 	sg->final = 1;
1463a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1464a74af788SAkhil Goyal 
1465a74af788SAkhil Goyal 	/* input */
1466a74af788SAkhil Goyal 	mbuf = sym->m_src;
1467a74af788SAkhil Goyal 	in_sg = &cf->sg[1];
1468a74af788SAkhil Goyal 	in_sg->extension = 1;
1469a74af788SAkhil Goyal 	in_sg->final = 1;
1470a74af788SAkhil Goyal 	if (is_encode(ses))
1471a74af788SAkhil Goyal 		in_sg->length = ses->iv.length + sym->auth.data.length;
1472a74af788SAkhil Goyal 	else
1473a74af788SAkhil Goyal 		in_sg->length = ses->iv.length + sym->auth.data.length
1474a74af788SAkhil Goyal 						+ ses->digest_length;
1475a74af788SAkhil Goyal 
1476a74af788SAkhil Goyal 	/* input sg entries */
1477a74af788SAkhil Goyal 	sg++;
147895456e89SShreyansh Jain 	qm_sg_entry_set64(in_sg, dpaa_mem_vtop(sg));
1479a74af788SAkhil Goyal 	cpu_to_hw_sg(in_sg);
1480a74af788SAkhil Goyal 
1481a74af788SAkhil Goyal 	/* 1st seg IV */
1482a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1483a74af788SAkhil Goyal 	sg->length = ses->iv.length;
1484a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1485a74af788SAkhil Goyal 
1486a74af788SAkhil Goyal 	/* 2nd seg */
1487a74af788SAkhil Goyal 	sg++;
1488a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1489a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->auth.data.offset;
1490a74af788SAkhil Goyal 	sg->offset = sym->auth.data.offset;
1491a74af788SAkhil Goyal 
1492a74af788SAkhil Goyal 	/* Successive segs */
1493a74af788SAkhil Goyal 	mbuf = mbuf->next;
1494a74af788SAkhil Goyal 	while (mbuf) {
1495a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1496a74af788SAkhil Goyal 		sg++;
1497a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1498a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1499a74af788SAkhil Goyal 		mbuf = mbuf->next;
1500a74af788SAkhil Goyal 	}
1501a74af788SAkhil Goyal 
1502a74af788SAkhil Goyal 	sg->length -= ses->digest_length;
1503a74af788SAkhil Goyal 	if (is_decode(ses)) {
1504a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1505a74af788SAkhil Goyal 		sg++;
1506a74af788SAkhil Goyal 		memcpy(ctx->digest, sym->auth.digest.data,
1507a74af788SAkhil Goyal 			ses->digest_length);
150895456e89SShreyansh Jain 		qm_sg_entry_set64(sg, dpaa_mem_vtop(ctx->digest));
1509a74af788SAkhil Goyal 		sg->length = ses->digest_length;
1510a74af788SAkhil Goyal 	}
1511a74af788SAkhil Goyal 	sg->final = 1;
1512a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1513a74af788SAkhil Goyal 
1514a74af788SAkhil Goyal 	return cf;
1515a74af788SAkhil Goyal }
1516a74af788SAkhil Goyal 
1517a74af788SAkhil Goyal static inline struct dpaa_sec_job *
1518c3e85bdcSAkhil Goyal build_cipher_auth(struct rte_crypto_op *op, dpaa_sec_session *ses)
1519c3e85bdcSAkhil Goyal {
1520c3e85bdcSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1521c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
1522c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1523c3e85bdcSAkhil Goyal 	struct qm_sg_entry *sg;
1524c4509373SSantosh Shukla 	rte_iova_t src_start_addr, dst_start_addr;
1525c3e85bdcSAkhil Goyal 	uint32_t length = 0;
1526c3e85bdcSAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1527c3e85bdcSAkhil Goyal 			ses->iv.offset);
1528c3e85bdcSAkhil Goyal 
1529455da545SSantosh Shukla 	src_start_addr = sym->m_src->buf_iova + sym->m_src->data_off;
1530a389434eSAlok Makhariya 	if (sym->m_dst)
1531455da545SSantosh Shukla 		dst_start_addr = sym->m_dst->buf_iova + sym->m_dst->data_off;
1532a389434eSAlok Makhariya 	else
1533a389434eSAlok Makhariya 		dst_start_addr = src_start_addr;
1534c3e85bdcSAkhil Goyal 
1535*f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, 7);
1536c3e85bdcSAkhil Goyal 	if (!ctx)
1537c3e85bdcSAkhil Goyal 		return NULL;
1538c3e85bdcSAkhil Goyal 
1539c3e85bdcSAkhil Goyal 	cf = &ctx->job;
1540c3e85bdcSAkhil Goyal 	ctx->op = op;
1541c3e85bdcSAkhil Goyal 
1542c3e85bdcSAkhil Goyal 	/* input */
1543c3e85bdcSAkhil Goyal 	rte_prefetch0(cf->sg);
1544c3e85bdcSAkhil Goyal 	sg = &cf->sg[2];
154595456e89SShreyansh Jain 	qm_sg_entry_set64(&cf->sg[1], dpaa_mem_vtop(sg));
1546c3e85bdcSAkhil Goyal 	if (is_encode(ses)) {
1547c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1548c3e85bdcSAkhil Goyal 		sg->length = ses->iv.length;
1549c3e85bdcSAkhil Goyal 		length += sg->length;
1550c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1551c3e85bdcSAkhil Goyal 
1552c3e85bdcSAkhil Goyal 		sg++;
1553a389434eSAlok Makhariya 		qm_sg_entry_set64(sg, src_start_addr + sym->auth.data.offset);
1554c3e85bdcSAkhil Goyal 		sg->length = sym->auth.data.length;
1555c3e85bdcSAkhil Goyal 		length += sg->length;
1556c3e85bdcSAkhil Goyal 		sg->final = 1;
1557c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1558c3e85bdcSAkhil Goyal 	} else {
1559c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1560c3e85bdcSAkhil Goyal 		sg->length = ses->iv.length;
1561c3e85bdcSAkhil Goyal 		length += sg->length;
1562c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1563c3e85bdcSAkhil Goyal 
1564c3e85bdcSAkhil Goyal 		sg++;
1565c3e85bdcSAkhil Goyal 
1566a389434eSAlok Makhariya 		qm_sg_entry_set64(sg, src_start_addr + sym->auth.data.offset);
1567c3e85bdcSAkhil Goyal 		sg->length = sym->auth.data.length;
1568c3e85bdcSAkhil Goyal 		length += sg->length;
1569c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1570c3e85bdcSAkhil Goyal 
1571c3e85bdcSAkhil Goyal 		memcpy(ctx->digest, sym->auth.digest.data,
1572c3e85bdcSAkhil Goyal 		       ses->digest_length);
1573c3e85bdcSAkhil Goyal 		sg++;
1574c3e85bdcSAkhil Goyal 
157595456e89SShreyansh Jain 		qm_sg_entry_set64(sg, dpaa_mem_vtop(ctx->digest));
1576c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
1577c3e85bdcSAkhil Goyal 		length += sg->length;
1578c3e85bdcSAkhil Goyal 		sg->final = 1;
1579c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1580c3e85bdcSAkhil Goyal 	}
1581c3e85bdcSAkhil Goyal 	/* input compound frame */
1582c3e85bdcSAkhil Goyal 	cf->sg[1].length = length;
1583c3e85bdcSAkhil Goyal 	cf->sg[1].extension = 1;
1584c3e85bdcSAkhil Goyal 	cf->sg[1].final = 1;
1585c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(&cf->sg[1]);
1586c3e85bdcSAkhil Goyal 
1587c3e85bdcSAkhil Goyal 	/* output */
1588c3e85bdcSAkhil Goyal 	sg++;
158995456e89SShreyansh Jain 	qm_sg_entry_set64(&cf->sg[0], dpaa_mem_vtop(sg));
1590a389434eSAlok Makhariya 	qm_sg_entry_set64(sg, dst_start_addr + sym->cipher.data.offset);
1591c3e85bdcSAkhil Goyal 	sg->length = sym->cipher.data.length;
1592c3e85bdcSAkhil Goyal 	length = sg->length;
1593c3e85bdcSAkhil Goyal 	if (is_encode(ses)) {
1594c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1595c3e85bdcSAkhil Goyal 		/* set auth output */
1596c3e85bdcSAkhil Goyal 		sg++;
1597c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
1598c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
1599c3e85bdcSAkhil Goyal 		length += sg->length;
1600c3e85bdcSAkhil Goyal 	}
1601c3e85bdcSAkhil Goyal 	sg->final = 1;
1602c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1603c3e85bdcSAkhil Goyal 
1604c3e85bdcSAkhil Goyal 	/* output compound frame */
1605c3e85bdcSAkhil Goyal 	cf->sg[0].length = length;
1606c3e85bdcSAkhil Goyal 	cf->sg[0].extension = 1;
1607c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(&cf->sg[0]);
1608c3e85bdcSAkhil Goyal 
1609c3e85bdcSAkhil Goyal 	return cf;
1610c3e85bdcSAkhil Goyal }
1611c3e85bdcSAkhil Goyal 
16121f14d500SAkhil Goyal static inline struct dpaa_sec_job *
16131f14d500SAkhil Goyal build_proto(struct rte_crypto_op *op, dpaa_sec_session *ses)
16141f14d500SAkhil Goyal {
16151f14d500SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
16161f14d500SAkhil Goyal 	struct dpaa_sec_job *cf;
16171f14d500SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
16181f14d500SAkhil Goyal 	struct qm_sg_entry *sg;
16191f14d500SAkhil Goyal 	phys_addr_t src_start_addr, dst_start_addr;
16201f14d500SAkhil Goyal 
1621*f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, 2);
16221f14d500SAkhil Goyal 	if (!ctx)
16231f14d500SAkhil Goyal 		return NULL;
16241f14d500SAkhil Goyal 	cf = &ctx->job;
16251f14d500SAkhil Goyal 	ctx->op = op;
16261f14d500SAkhil Goyal 
16271f14d500SAkhil Goyal 	src_start_addr = rte_pktmbuf_mtophys(sym->m_src);
16281f14d500SAkhil Goyal 
16291f14d500SAkhil Goyal 	if (sym->m_dst)
16301f14d500SAkhil Goyal 		dst_start_addr = rte_pktmbuf_mtophys(sym->m_dst);
16311f14d500SAkhil Goyal 	else
16321f14d500SAkhil Goyal 		dst_start_addr = src_start_addr;
16331f14d500SAkhil Goyal 
16341f14d500SAkhil Goyal 	/* input */
16351f14d500SAkhil Goyal 	sg = &cf->sg[1];
16361f14d500SAkhil Goyal 	qm_sg_entry_set64(sg, src_start_addr);
16371f14d500SAkhil Goyal 	sg->length = sym->m_src->pkt_len;
16381f14d500SAkhil Goyal 	sg->final = 1;
16391f14d500SAkhil Goyal 	cpu_to_hw_sg(sg);
16401f14d500SAkhil Goyal 
16411f14d500SAkhil Goyal 	sym->m_src->packet_type &= ~RTE_PTYPE_L4_MASK;
16421f14d500SAkhil Goyal 	/* output */
16431f14d500SAkhil Goyal 	sg = &cf->sg[0];
16441f14d500SAkhil Goyal 	qm_sg_entry_set64(sg, dst_start_addr);
16451f14d500SAkhil Goyal 	sg->length = sym->m_src->buf_len - sym->m_src->data_off;
16461f14d500SAkhil Goyal 	cpu_to_hw_sg(sg);
16471f14d500SAkhil Goyal 
16481f14d500SAkhil Goyal 	return cf;
16491f14d500SAkhil Goyal }
16501f14d500SAkhil Goyal 
1651fb5c100aSAkhil Goyal static inline struct dpaa_sec_job *
1652fb5c100aSAkhil Goyal build_proto_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
1653fb5c100aSAkhil Goyal {
1654fb5c100aSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1655fb5c100aSAkhil Goyal 	struct dpaa_sec_job *cf;
1656fb5c100aSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1657fb5c100aSAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
1658fb5c100aSAkhil Goyal 	struct rte_mbuf *mbuf;
1659fb5c100aSAkhil Goyal 	uint8_t req_segs;
1660fb5c100aSAkhil Goyal 	uint32_t in_len = 0, out_len = 0;
1661fb5c100aSAkhil Goyal 
1662fb5c100aSAkhil Goyal 	if (sym->m_dst)
1663fb5c100aSAkhil Goyal 		mbuf = sym->m_dst;
1664fb5c100aSAkhil Goyal 	else
1665fb5c100aSAkhil Goyal 		mbuf = sym->m_src;
1666fb5c100aSAkhil Goyal 
1667fb5c100aSAkhil Goyal 	req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 2;
1668*f7a5752eSHemant Agrawal 	if (mbuf->nb_segs > MAX_SG_ENTRIES) {
1669fb5c100aSAkhil Goyal 		DPAA_SEC_DP_ERR("Proto: Max sec segs supported is %d",
1670fb5c100aSAkhil Goyal 				MAX_SG_ENTRIES);
1671fb5c100aSAkhil Goyal 		return NULL;
1672fb5c100aSAkhil Goyal 	}
1673fb5c100aSAkhil Goyal 
1674*f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, req_segs);
1675fb5c100aSAkhil Goyal 	if (!ctx)
1676fb5c100aSAkhil Goyal 		return NULL;
1677fb5c100aSAkhil Goyal 	cf = &ctx->job;
1678fb5c100aSAkhil Goyal 	ctx->op = op;
1679fb5c100aSAkhil Goyal 	/* output */
1680fb5c100aSAkhil Goyal 	out_sg = &cf->sg[0];
1681fb5c100aSAkhil Goyal 	out_sg->extension = 1;
1682fb5c100aSAkhil Goyal 	qm_sg_entry_set64(out_sg, dpaa_mem_vtop(&cf->sg[2]));
1683fb5c100aSAkhil Goyal 
1684fb5c100aSAkhil Goyal 	/* 1st seg */
1685fb5c100aSAkhil Goyal 	sg = &cf->sg[2];
1686fb5c100aSAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1687fb5c100aSAkhil Goyal 	sg->offset = 0;
1688fb5c100aSAkhil Goyal 
1689fb5c100aSAkhil Goyal 	/* Successive segs */
1690fb5c100aSAkhil Goyal 	while (mbuf->next) {
1691fb5c100aSAkhil Goyal 		sg->length = mbuf->data_len;
1692fb5c100aSAkhil Goyal 		out_len += sg->length;
1693fb5c100aSAkhil Goyal 		mbuf = mbuf->next;
1694fb5c100aSAkhil Goyal 		cpu_to_hw_sg(sg);
1695fb5c100aSAkhil Goyal 		sg++;
1696fb5c100aSAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1697fb5c100aSAkhil Goyal 		sg->offset = 0;
1698fb5c100aSAkhil Goyal 	}
1699fb5c100aSAkhil Goyal 	sg->length = mbuf->buf_len - mbuf->data_off;
1700fb5c100aSAkhil Goyal 	out_len += sg->length;
1701fb5c100aSAkhil Goyal 	sg->final = 1;
1702fb5c100aSAkhil Goyal 	cpu_to_hw_sg(sg);
1703fb5c100aSAkhil Goyal 
1704fb5c100aSAkhil Goyal 	out_sg->length = out_len;
1705fb5c100aSAkhil Goyal 	cpu_to_hw_sg(out_sg);
1706fb5c100aSAkhil Goyal 
1707fb5c100aSAkhil Goyal 	/* input */
1708fb5c100aSAkhil Goyal 	mbuf = sym->m_src;
1709fb5c100aSAkhil Goyal 	in_sg = &cf->sg[1];
1710fb5c100aSAkhil Goyal 	in_sg->extension = 1;
1711fb5c100aSAkhil Goyal 	in_sg->final = 1;
1712fb5c100aSAkhil Goyal 	in_len = mbuf->data_len;
1713fb5c100aSAkhil Goyal 
1714fb5c100aSAkhil Goyal 	sg++;
1715fb5c100aSAkhil Goyal 	qm_sg_entry_set64(in_sg, dpaa_mem_vtop(sg));
1716fb5c100aSAkhil Goyal 
1717fb5c100aSAkhil Goyal 	/* 1st seg */
1718fb5c100aSAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1719fb5c100aSAkhil Goyal 	sg->length = mbuf->data_len;
1720fb5c100aSAkhil Goyal 	sg->offset = 0;
1721fb5c100aSAkhil Goyal 
1722fb5c100aSAkhil Goyal 	/* Successive segs */
1723fb5c100aSAkhil Goyal 	mbuf = mbuf->next;
1724fb5c100aSAkhil Goyal 	while (mbuf) {
1725fb5c100aSAkhil Goyal 		cpu_to_hw_sg(sg);
1726fb5c100aSAkhil Goyal 		sg++;
1727fb5c100aSAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1728fb5c100aSAkhil Goyal 		sg->length = mbuf->data_len;
1729fb5c100aSAkhil Goyal 		sg->offset = 0;
1730fb5c100aSAkhil Goyal 		in_len += sg->length;
1731fb5c100aSAkhil Goyal 		mbuf = mbuf->next;
1732fb5c100aSAkhil Goyal 	}
1733fb5c100aSAkhil Goyal 	sg->final = 1;
1734fb5c100aSAkhil Goyal 	cpu_to_hw_sg(sg);
1735fb5c100aSAkhil Goyal 
1736fb5c100aSAkhil Goyal 	in_sg->length = in_len;
1737fb5c100aSAkhil Goyal 	cpu_to_hw_sg(in_sg);
1738fb5c100aSAkhil Goyal 
1739fb5c100aSAkhil Goyal 	sym->m_src->packet_type &= ~RTE_PTYPE_L4_MASK;
1740fb5c100aSAkhil Goyal 
1741fb5c100aSAkhil Goyal 	return cf;
1742fb5c100aSAkhil Goyal }
1743fb5c100aSAkhil Goyal 
17449a984458SAkhil Goyal static uint16_t
17459a984458SAkhil Goyal dpaa_sec_enqueue_burst(void *qp, struct rte_crypto_op **ops,
17469a984458SAkhil Goyal 		       uint16_t nb_ops)
1747c3e85bdcSAkhil Goyal {
17489a984458SAkhil Goyal 	/* Function to transmit the frames to given device and queuepair */
17499a984458SAkhil Goyal 	uint32_t loop;
17509a984458SAkhil Goyal 	struct dpaa_sec_qp *dpaa_qp = (struct dpaa_sec_qp *)qp;
17519a984458SAkhil Goyal 	uint16_t num_tx = 0;
17529a984458SAkhil Goyal 	struct qm_fd fds[DPAA_SEC_BURST], *fd;
17539a984458SAkhil Goyal 	uint32_t frames_to_send;
17549a984458SAkhil Goyal 	struct rte_crypto_op *op;
1755c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
1756c3e85bdcSAkhil Goyal 	dpaa_sec_session *ses;
17579a984458SAkhil Goyal 	uint32_t auth_only_len;
17589a984458SAkhil Goyal 	struct qman_fq *inq[DPAA_SEC_BURST];
1759c3e85bdcSAkhil Goyal 
17609a984458SAkhil Goyal 	while (nb_ops) {
17619a984458SAkhil Goyal 		frames_to_send = (nb_ops > DPAA_SEC_BURST) ?
17629a984458SAkhil Goyal 				DPAA_SEC_BURST : nb_ops;
17639a984458SAkhil Goyal 		for (loop = 0; loop < frames_to_send; loop++) {
17649a984458SAkhil Goyal 			op = *(ops++);
17659a984458SAkhil Goyal 			switch (op->sess_type) {
17669a984458SAkhil Goyal 			case RTE_CRYPTO_OP_WITH_SESSION:
17679a984458SAkhil Goyal 				ses = (dpaa_sec_session *)
1768012c5076SPablo de Lara 					get_sym_session_private_data(
17699a984458SAkhil Goyal 							op->sym->session,
17709a984458SAkhil Goyal 							cryptodev_driver_id);
17719a984458SAkhil Goyal 				break;
17729a984458SAkhil Goyal 			case RTE_CRYPTO_OP_SECURITY_SESSION:
17739a984458SAkhil Goyal 				ses = (dpaa_sec_session *)
17749a984458SAkhil Goyal 					get_sec_session_private_data(
17751f14d500SAkhil Goyal 							op->sym->sec_session);
17769a984458SAkhil Goyal 				break;
17779a984458SAkhil Goyal 			default:
1778f163231eSHemant Agrawal 				DPAA_SEC_DP_ERR(
17799a984458SAkhil Goyal 					"sessionless crypto op not supported");
17809a984458SAkhil Goyal 				frames_to_send = loop;
17819a984458SAkhil Goyal 				nb_ops = loop;
17829a984458SAkhil Goyal 				goto send_pkts;
17839a984458SAkhil Goyal 			}
17844e694fe5SAkhil Goyal 			if (unlikely(!ses->qp[rte_lcore_id() % MAX_DPAA_CORES])) {
17859a984458SAkhil Goyal 				if (dpaa_sec_attach_sess_q(qp, ses)) {
17869a984458SAkhil Goyal 					frames_to_send = loop;
17879a984458SAkhil Goyal 					nb_ops = loop;
17889a984458SAkhil Goyal 					goto send_pkts;
17899a984458SAkhil Goyal 				}
17904e694fe5SAkhil Goyal 			} else if (unlikely(ses->qp[rte_lcore_id() %
17914e694fe5SAkhil Goyal 						MAX_DPAA_CORES] != qp)) {
17929198b2c2SAkhil Goyal 				DPAA_SEC_DP_ERR("Old:sess->qp = %p"
17934e694fe5SAkhil Goyal 					" New qp = %p\n",
17944e694fe5SAkhil Goyal 					ses->qp[rte_lcore_id() %
17954e694fe5SAkhil Goyal 					MAX_DPAA_CORES], qp);
17969198b2c2SAkhil Goyal 				frames_to_send = loop;
17979198b2c2SAkhil Goyal 				nb_ops = loop;
17989198b2c2SAkhil Goyal 				goto send_pkts;
1799c3e85bdcSAkhil Goyal 			}
1800c3e85bdcSAkhil Goyal 
18019a984458SAkhil Goyal 			auth_only_len = op->sym->auth.data.length -
18029a984458SAkhil Goyal 						op->sym->cipher.data.length;
1803fb5c100aSAkhil Goyal 			if (rte_pktmbuf_is_contiguous(op->sym->m_src) &&
1804fb5c100aSAkhil Goyal 				  ((op->sym->m_dst == NULL) ||
1805fb5c100aSAkhil Goyal 				   rte_pktmbuf_is_contiguous(op->sym->m_dst))) {
180605b12700SHemant Agrawal 				if (is_proto_ipsec(ses)) {
180705b12700SHemant Agrawal 					cf = build_proto(op, ses);
1808a1173d55SHemant Agrawal 				} else if (is_proto_pdcp(ses)) {
1809a1173d55SHemant Agrawal 					cf = build_proto(op, ses);
181005b12700SHemant Agrawal 				} else if (is_auth_only(ses)) {
1811c3e85bdcSAkhil Goyal 					cf = build_auth_only(op, ses);
1812c3e85bdcSAkhil Goyal 				} else if (is_cipher_only(ses)) {
1813c3e85bdcSAkhil Goyal 					cf = build_cipher_only(op, ses);
1814c3e85bdcSAkhil Goyal 				} else if (is_aead(ses)) {
1815c3e85bdcSAkhil Goyal 					cf = build_cipher_auth_gcm(op, ses);
1816c3e85bdcSAkhil Goyal 					auth_only_len = ses->auth_only_len;
1817c3e85bdcSAkhil Goyal 				} else if (is_auth_cipher(ses)) {
1818c3e85bdcSAkhil Goyal 					cf = build_cipher_auth(op, ses);
1819c3e85bdcSAkhil Goyal 				} else {
1820f163231eSHemant Agrawal 					DPAA_SEC_DP_ERR("not supported ops");
18219a984458SAkhil Goyal 					frames_to_send = loop;
18229a984458SAkhil Goyal 					nb_ops = loop;
18239a984458SAkhil Goyal 					goto send_pkts;
1824c3e85bdcSAkhil Goyal 				}
1825a74af788SAkhil Goyal 			} else {
1826fb5c100aSAkhil Goyal 				if (is_proto_pdcp(ses) || is_proto_ipsec(ses)) {
1827fb5c100aSAkhil Goyal 					cf = build_proto_sg(op, ses);
1828fb5c100aSAkhil Goyal 				} else if (is_auth_only(ses)) {
1829a74af788SAkhil Goyal 					cf = build_auth_only_sg(op, ses);
1830a74af788SAkhil Goyal 				} else if (is_cipher_only(ses)) {
1831a74af788SAkhil Goyal 					cf = build_cipher_only_sg(op, ses);
1832a74af788SAkhil Goyal 				} else if (is_aead(ses)) {
1833a74af788SAkhil Goyal 					cf = build_cipher_auth_gcm_sg(op, ses);
1834a74af788SAkhil Goyal 					auth_only_len = ses->auth_only_len;
1835a74af788SAkhil Goyal 				} else if (is_auth_cipher(ses)) {
1836a74af788SAkhil Goyal 					cf = build_cipher_auth_sg(op, ses);
1837a74af788SAkhil Goyal 				} else {
1838f163231eSHemant Agrawal 					DPAA_SEC_DP_ERR("not supported ops");
1839a74af788SAkhil Goyal 					frames_to_send = loop;
1840a74af788SAkhil Goyal 					nb_ops = loop;
1841a74af788SAkhil Goyal 					goto send_pkts;
1842a74af788SAkhil Goyal 				}
1843a74af788SAkhil Goyal 			}
18449a984458SAkhil Goyal 			if (unlikely(!cf)) {
18459a984458SAkhil Goyal 				frames_to_send = loop;
18469a984458SAkhil Goyal 				nb_ops = loop;
18479a984458SAkhil Goyal 				goto send_pkts;
18489a984458SAkhil Goyal 			}
1849c3e85bdcSAkhil Goyal 
18509a984458SAkhil Goyal 			fd = &fds[loop];
18514e694fe5SAkhil Goyal 			inq[loop] = ses->inq[rte_lcore_id() % MAX_DPAA_CORES];
18529a984458SAkhil Goyal 			fd->opaque_addr = 0;
18539a984458SAkhil Goyal 			fd->cmd = 0;
185495456e89SShreyansh Jain 			qm_fd_addr_set64(fd, dpaa_mem_vtop(cf->sg));
18559a984458SAkhil Goyal 			fd->_format1 = qm_fd_compound;
18569a984458SAkhil Goyal 			fd->length29 = 2 * sizeof(struct qm_sg_entry);
18579a984458SAkhil Goyal 			/* Auth_only_len is set as 0 in descriptor and it is
18589a984458SAkhil Goyal 			 * overwritten here in the fd.cmd which will update
18599a984458SAkhil Goyal 			 * the DPOVRD reg.
1860c3e85bdcSAkhil Goyal 			 */
1861c3e85bdcSAkhil Goyal 			if (auth_only_len)
18629a984458SAkhil Goyal 				fd->cmd = 0x80000000 | auth_only_len;
1863c3e85bdcSAkhil Goyal 
18646a0c9d36SAkhil Goyal 			/* In case of PDCP, per packet HFN is stored in
18656a0c9d36SAkhil Goyal 			 * mbuf priv after sym_op.
18666a0c9d36SAkhil Goyal 			 */
18676a0c9d36SAkhil Goyal 			if (is_proto_pdcp(ses) && ses->pdcp.hfn_ovd) {
18686a0c9d36SAkhil Goyal 				fd->cmd = 0x80000000 |
18696a0c9d36SAkhil Goyal 					*((uint32_t *)((uint8_t *)op +
18706a0c9d36SAkhil Goyal 					ses->pdcp.hfn_ovd_offset));
18716a0c9d36SAkhil Goyal 				DPAA_SEC_DP_DEBUG("Per packet HFN: %x, ovd:%u,%u\n",
18726a0c9d36SAkhil Goyal 					*((uint32_t *)((uint8_t *)op +
18736a0c9d36SAkhil Goyal 					ses->pdcp.hfn_ovd_offset)),
18746a0c9d36SAkhil Goyal 					ses->pdcp.hfn_ovd,
18756a0c9d36SAkhil Goyal 					is_proto_pdcp(ses));
18766a0c9d36SAkhil Goyal 			}
18776a0c9d36SAkhil Goyal 
18789a984458SAkhil Goyal 		}
18799a984458SAkhil Goyal send_pkts:
18809a984458SAkhil Goyal 		loop = 0;
18819a984458SAkhil Goyal 		while (loop < frames_to_send) {
18829a984458SAkhil Goyal 			loop += qman_enqueue_multi_fq(&inq[loop], &fds[loop],
18839a984458SAkhil Goyal 					frames_to_send - loop);
18849a984458SAkhil Goyal 		}
18859a984458SAkhil Goyal 		nb_ops -= frames_to_send;
18869a984458SAkhil Goyal 		num_tx += frames_to_send;
1887c3e85bdcSAkhil Goyal 	}
1888c3e85bdcSAkhil Goyal 
1889c3e85bdcSAkhil Goyal 	dpaa_qp->tx_pkts += num_tx;
1890c3e85bdcSAkhil Goyal 	dpaa_qp->tx_errs += nb_ops - num_tx;
1891c3e85bdcSAkhil Goyal 
1892c3e85bdcSAkhil Goyal 	return num_tx;
1893c3e85bdcSAkhil Goyal }
1894c3e85bdcSAkhil Goyal 
1895c3e85bdcSAkhil Goyal static uint16_t
1896c3e85bdcSAkhil Goyal dpaa_sec_dequeue_burst(void *qp, struct rte_crypto_op **ops,
1897c3e85bdcSAkhil Goyal 		       uint16_t nb_ops)
1898c3e85bdcSAkhil Goyal {
1899c3e85bdcSAkhil Goyal 	uint16_t num_rx;
1900c3e85bdcSAkhil Goyal 	struct dpaa_sec_qp *dpaa_qp = (struct dpaa_sec_qp *)qp;
1901c3e85bdcSAkhil Goyal 
1902c3e85bdcSAkhil Goyal 	num_rx = dpaa_sec_deq(dpaa_qp, ops, nb_ops);
1903c3e85bdcSAkhil Goyal 
1904c3e85bdcSAkhil Goyal 	dpaa_qp->rx_pkts += num_rx;
1905c3e85bdcSAkhil Goyal 	dpaa_qp->rx_errs += nb_ops - num_rx;
1906c3e85bdcSAkhil Goyal 
1907f163231eSHemant Agrawal 	DPAA_SEC_DP_DEBUG("SEC Received %d Packets\n", num_rx);
1908c3e85bdcSAkhil Goyal 
1909c3e85bdcSAkhil Goyal 	return num_rx;
1910c3e85bdcSAkhil Goyal }
1911c3e85bdcSAkhil Goyal 
1912c3e85bdcSAkhil Goyal /** Release queue pair */
1913c3e85bdcSAkhil Goyal static int
1914c3e85bdcSAkhil Goyal dpaa_sec_queue_pair_release(struct rte_cryptodev *dev,
1915c3e85bdcSAkhil Goyal 			    uint16_t qp_id)
1916c3e85bdcSAkhil Goyal {
1917c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals;
1918c3e85bdcSAkhil Goyal 	struct dpaa_sec_qp *qp = NULL;
1919c3e85bdcSAkhil Goyal 
1920c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
1921c3e85bdcSAkhil Goyal 
1922f163231eSHemant Agrawal 	DPAA_SEC_DEBUG("dev =%p, queue =%d", dev, qp_id);
1923c3e85bdcSAkhil Goyal 
1924c3e85bdcSAkhil Goyal 	internals = dev->data->dev_private;
1925c3e85bdcSAkhil Goyal 	if (qp_id >= internals->max_nb_queue_pairs) {
1926f163231eSHemant Agrawal 		DPAA_SEC_ERR("Max supported qpid %d",
1927c3e85bdcSAkhil Goyal 			     internals->max_nb_queue_pairs);
1928c3e85bdcSAkhil Goyal 		return -EINVAL;
1929c3e85bdcSAkhil Goyal 	}
1930c3e85bdcSAkhil Goyal 
1931c3e85bdcSAkhil Goyal 	qp = &internals->qps[qp_id];
1932c3e85bdcSAkhil Goyal 	qp->internals = NULL;
1933c3e85bdcSAkhil Goyal 	dev->data->queue_pairs[qp_id] = NULL;
1934c3e85bdcSAkhil Goyal 
1935c3e85bdcSAkhil Goyal 	return 0;
1936c3e85bdcSAkhil Goyal }
1937c3e85bdcSAkhil Goyal 
1938c3e85bdcSAkhil Goyal /** Setup a queue pair */
1939c3e85bdcSAkhil Goyal static int
1940c3e85bdcSAkhil Goyal dpaa_sec_queue_pair_setup(struct rte_cryptodev *dev, uint16_t qp_id,
1941c3e85bdcSAkhil Goyal 		__rte_unused const struct rte_cryptodev_qp_conf *qp_conf,
1942725d2a7fSFan Zhang 		__rte_unused int socket_id)
1943c3e85bdcSAkhil Goyal {
1944c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals;
1945c3e85bdcSAkhil Goyal 	struct dpaa_sec_qp *qp = NULL;
1946c3e85bdcSAkhil Goyal 
1947f163231eSHemant Agrawal 	DPAA_SEC_DEBUG("dev =%p, queue =%d, conf =%p", dev, qp_id, qp_conf);
1948c3e85bdcSAkhil Goyal 
1949c3e85bdcSAkhil Goyal 	internals = dev->data->dev_private;
1950c3e85bdcSAkhil Goyal 	if (qp_id >= internals->max_nb_queue_pairs) {
1951f163231eSHemant Agrawal 		DPAA_SEC_ERR("Max supported qpid %d",
1952c3e85bdcSAkhil Goyal 			     internals->max_nb_queue_pairs);
1953c3e85bdcSAkhil Goyal 		return -EINVAL;
1954c3e85bdcSAkhil Goyal 	}
1955c3e85bdcSAkhil Goyal 
1956c3e85bdcSAkhil Goyal 	qp = &internals->qps[qp_id];
1957c3e85bdcSAkhil Goyal 	qp->internals = internals;
1958c3e85bdcSAkhil Goyal 	dev->data->queue_pairs[qp_id] = qp;
1959c3e85bdcSAkhil Goyal 
1960c3e85bdcSAkhil Goyal 	return 0;
1961c3e85bdcSAkhil Goyal }
1962c3e85bdcSAkhil Goyal 
1963c3e85bdcSAkhil Goyal /** Return the number of allocated queue pairs */
1964c3e85bdcSAkhil Goyal static uint32_t
1965c3e85bdcSAkhil Goyal dpaa_sec_queue_pair_count(struct rte_cryptodev *dev)
1966c3e85bdcSAkhil Goyal {
1967c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
1968c3e85bdcSAkhil Goyal 
1969c3e85bdcSAkhil Goyal 	return dev->data->nb_queue_pairs;
1970c3e85bdcSAkhil Goyal }
1971c3e85bdcSAkhil Goyal 
1972c3e85bdcSAkhil Goyal /** Returns the size of session structure */
1973c3e85bdcSAkhil Goyal static unsigned int
1974012c5076SPablo de Lara dpaa_sec_sym_session_get_size(struct rte_cryptodev *dev __rte_unused)
1975c3e85bdcSAkhil Goyal {
1976c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
1977c3e85bdcSAkhil Goyal 
1978c3e85bdcSAkhil Goyal 	return sizeof(dpaa_sec_session);
1979c3e85bdcSAkhil Goyal }
1980c3e85bdcSAkhil Goyal 
1981c3e85bdcSAkhil Goyal static int
1982c3e85bdcSAkhil Goyal dpaa_sec_cipher_init(struct rte_cryptodev *dev __rte_unused,
1983c3e85bdcSAkhil Goyal 		     struct rte_crypto_sym_xform *xform,
1984c3e85bdcSAkhil Goyal 		     dpaa_sec_session *session)
1985c3e85bdcSAkhil Goyal {
1986c3e85bdcSAkhil Goyal 	session->cipher_alg = xform->cipher.algo;
1987c3e85bdcSAkhil Goyal 	session->iv.length = xform->cipher.iv.length;
1988c3e85bdcSAkhil Goyal 	session->iv.offset = xform->cipher.iv.offset;
1989c3e85bdcSAkhil Goyal 	session->cipher_key.data = rte_zmalloc(NULL, xform->cipher.key.length,
1990c3e85bdcSAkhil Goyal 					       RTE_CACHE_LINE_SIZE);
1991c3e85bdcSAkhil Goyal 	if (session->cipher_key.data == NULL && xform->cipher.key.length > 0) {
1992f163231eSHemant Agrawal 		DPAA_SEC_ERR("No Memory for cipher key");
1993c3e85bdcSAkhil Goyal 		return -ENOMEM;
1994c3e85bdcSAkhil Goyal 	}
1995c3e85bdcSAkhil Goyal 	session->cipher_key.length = xform->cipher.key.length;
1996c3e85bdcSAkhil Goyal 
1997c3e85bdcSAkhil Goyal 	memcpy(session->cipher_key.data, xform->cipher.key.data,
1998c3e85bdcSAkhil Goyal 	       xform->cipher.key.length);
1999c3e85bdcSAkhil Goyal 	session->dir = (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
2000c3e85bdcSAkhil Goyal 			DIR_ENC : DIR_DEC;
2001c3e85bdcSAkhil Goyal 
2002c3e85bdcSAkhil Goyal 	return 0;
2003c3e85bdcSAkhil Goyal }
2004c3e85bdcSAkhil Goyal 
2005c3e85bdcSAkhil Goyal static int
2006c3e85bdcSAkhil Goyal dpaa_sec_auth_init(struct rte_cryptodev *dev __rte_unused,
2007c3e85bdcSAkhil Goyal 		   struct rte_crypto_sym_xform *xform,
2008c3e85bdcSAkhil Goyal 		   dpaa_sec_session *session)
2009c3e85bdcSAkhil Goyal {
2010c3e85bdcSAkhil Goyal 	session->auth_alg = xform->auth.algo;
2011c3e85bdcSAkhil Goyal 	session->auth_key.data = rte_zmalloc(NULL, xform->auth.key.length,
2012c3e85bdcSAkhil Goyal 					     RTE_CACHE_LINE_SIZE);
2013c3e85bdcSAkhil Goyal 	if (session->auth_key.data == NULL && xform->auth.key.length > 0) {
2014f163231eSHemant Agrawal 		DPAA_SEC_ERR("No Memory for auth key");
2015c3e85bdcSAkhil Goyal 		return -ENOMEM;
2016c3e85bdcSAkhil Goyal 	}
2017c3e85bdcSAkhil Goyal 	session->auth_key.length = xform->auth.key.length;
2018c3e85bdcSAkhil Goyal 	session->digest_length = xform->auth.digest_length;
2019c3e85bdcSAkhil Goyal 
2020c3e85bdcSAkhil Goyal 	memcpy(session->auth_key.data, xform->auth.key.data,
2021c3e85bdcSAkhil Goyal 	       xform->auth.key.length);
2022c3e85bdcSAkhil Goyal 	session->dir = (xform->auth.op == RTE_CRYPTO_AUTH_OP_GENERATE) ?
2023c3e85bdcSAkhil Goyal 			DIR_ENC : DIR_DEC;
2024c3e85bdcSAkhil Goyal 
2025c3e85bdcSAkhil Goyal 	return 0;
2026c3e85bdcSAkhil Goyal }
2027c3e85bdcSAkhil Goyal 
2028c3e85bdcSAkhil Goyal static int
2029c3e85bdcSAkhil Goyal dpaa_sec_aead_init(struct rte_cryptodev *dev __rte_unused,
2030c3e85bdcSAkhil Goyal 		   struct rte_crypto_sym_xform *xform,
2031c3e85bdcSAkhil Goyal 		   dpaa_sec_session *session)
2032c3e85bdcSAkhil Goyal {
2033c3e85bdcSAkhil Goyal 	session->aead_alg = xform->aead.algo;
2034c3e85bdcSAkhil Goyal 	session->iv.length = xform->aead.iv.length;
2035c3e85bdcSAkhil Goyal 	session->iv.offset = xform->aead.iv.offset;
2036c3e85bdcSAkhil Goyal 	session->auth_only_len = xform->aead.aad_length;
2037c3e85bdcSAkhil Goyal 	session->aead_key.data = rte_zmalloc(NULL, xform->aead.key.length,
2038c3e85bdcSAkhil Goyal 					     RTE_CACHE_LINE_SIZE);
2039c3e85bdcSAkhil Goyal 	if (session->aead_key.data == NULL && xform->aead.key.length > 0) {
2040f163231eSHemant Agrawal 		DPAA_SEC_ERR("No Memory for aead key\n");
2041c3e85bdcSAkhil Goyal 		return -ENOMEM;
2042c3e85bdcSAkhil Goyal 	}
2043c3e85bdcSAkhil Goyal 	session->aead_key.length = xform->aead.key.length;
2044c3e85bdcSAkhil Goyal 	session->digest_length = xform->aead.digest_length;
2045c3e85bdcSAkhil Goyal 
2046c3e85bdcSAkhil Goyal 	memcpy(session->aead_key.data, xform->aead.key.data,
2047c3e85bdcSAkhil Goyal 	       xform->aead.key.length);
2048c3e85bdcSAkhil Goyal 	session->dir = (xform->aead.op == RTE_CRYPTO_AEAD_OP_ENCRYPT) ?
2049c3e85bdcSAkhil Goyal 			DIR_ENC : DIR_DEC;
2050c3e85bdcSAkhil Goyal 
2051c3e85bdcSAkhil Goyal 	return 0;
2052c3e85bdcSAkhil Goyal }
2053c3e85bdcSAkhil Goyal 
2054e79416d1SHemant Agrawal static struct qman_fq *
2055e79416d1SHemant Agrawal dpaa_sec_attach_rxq(struct dpaa_sec_dev_private *qi)
2056c3e85bdcSAkhil Goyal {
2057e79416d1SHemant Agrawal 	unsigned int i;
2058c3e85bdcSAkhil Goyal 
2059e621d970SAkhil Goyal 	for (i = 0; i < qi->max_nb_sessions * MAX_DPAA_CORES; i++) {
2060e79416d1SHemant Agrawal 		if (qi->inq_attach[i] == 0) {
2061e79416d1SHemant Agrawal 			qi->inq_attach[i] = 1;
2062e79416d1SHemant Agrawal 			return &qi->inq[i];
2063e79416d1SHemant Agrawal 		}
2064e79416d1SHemant Agrawal 	}
2065e621d970SAkhil Goyal 	DPAA_SEC_WARN("All session in use %u", qi->max_nb_sessions);
2066c3e85bdcSAkhil Goyal 
2067e79416d1SHemant Agrawal 	return NULL;
2068c3e85bdcSAkhil Goyal }
2069c3e85bdcSAkhil Goyal 
2070e79416d1SHemant Agrawal static int
2071e79416d1SHemant Agrawal dpaa_sec_detach_rxq(struct dpaa_sec_dev_private *qi, struct qman_fq *fq)
2072e79416d1SHemant Agrawal {
2073e79416d1SHemant Agrawal 	unsigned int i;
2074e79416d1SHemant Agrawal 
2075e79416d1SHemant Agrawal 	for (i = 0; i < qi->max_nb_sessions; i++) {
2076e79416d1SHemant Agrawal 		if (&qi->inq[i] == fq) {
2077b4053c4bSAlok Makhariya 			qman_retire_fq(fq, NULL);
2078b4053c4bSAlok Makhariya 			qman_oos_fq(fq);
2079e79416d1SHemant Agrawal 			qi->inq_attach[i] = 0;
2080e79416d1SHemant Agrawal 			return 0;
2081e79416d1SHemant Agrawal 		}
2082e79416d1SHemant Agrawal 	}
2083e79416d1SHemant Agrawal 	return -1;
2084e79416d1SHemant Agrawal }
2085e79416d1SHemant Agrawal 
2086e79416d1SHemant Agrawal static int
2087e79416d1SHemant Agrawal dpaa_sec_attach_sess_q(struct dpaa_sec_qp *qp, dpaa_sec_session *sess)
2088e79416d1SHemant Agrawal {
2089e79416d1SHemant Agrawal 	int ret;
2090e79416d1SHemant Agrawal 
20914e694fe5SAkhil Goyal 	sess->qp[rte_lcore_id() % MAX_DPAA_CORES] = qp;
2092e79416d1SHemant Agrawal 	ret = dpaa_sec_prep_cdb(sess);
2093e79416d1SHemant Agrawal 	if (ret) {
2094f163231eSHemant Agrawal 		DPAA_SEC_ERR("Unable to prepare sec cdb");
2095e79416d1SHemant Agrawal 		return -1;
2096e79416d1SHemant Agrawal 	}
20975b0f1bd3SAshish Jain 	if (unlikely(!RTE_PER_LCORE(dpaa_io))) {
20985b0f1bd3SAshish Jain 		ret = rte_dpaa_portal_init((void *)0);
20995b0f1bd3SAshish Jain 		if (ret) {
2100f163231eSHemant Agrawal 			DPAA_SEC_ERR("Failure in affining portal");
21015b0f1bd3SAshish Jain 			return ret;
21025b0f1bd3SAshish Jain 		}
21035b0f1bd3SAshish Jain 	}
21044e694fe5SAkhil Goyal 	ret = dpaa_sec_init_rx(sess->inq[rte_lcore_id() % MAX_DPAA_CORES],
21054e694fe5SAkhil Goyal 			       dpaa_mem_vtop(&sess->cdb),
2106e79416d1SHemant Agrawal 			       qman_fq_fqid(&qp->outq));
2107e79416d1SHemant Agrawal 	if (ret)
2108f163231eSHemant Agrawal 		DPAA_SEC_ERR("Unable to init sec queue");
2109e79416d1SHemant Agrawal 
2110e79416d1SHemant Agrawal 	return ret;
2111c3e85bdcSAkhil Goyal }
2112c3e85bdcSAkhil Goyal 
2113c3e85bdcSAkhil Goyal static int
2114c3e85bdcSAkhil Goyal dpaa_sec_set_session_parameters(struct rte_cryptodev *dev,
2115c3e85bdcSAkhil Goyal 			    struct rte_crypto_sym_xform *xform,	void *sess)
2116c3e85bdcSAkhil Goyal {
2117c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals = dev->data->dev_private;
2118c3e85bdcSAkhil Goyal 	dpaa_sec_session *session = sess;
21194e694fe5SAkhil Goyal 	uint32_t i;
2120c3e85bdcSAkhil Goyal 
2121c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2122c3e85bdcSAkhil Goyal 
2123c3e85bdcSAkhil Goyal 	if (unlikely(sess == NULL)) {
2124f163231eSHemant Agrawal 		DPAA_SEC_ERR("invalid session struct");
2125c3e85bdcSAkhil Goyal 		return -EINVAL;
2126c3e85bdcSAkhil Goyal 	}
2127b0894102SAkhil Goyal 	memset(session, 0, sizeof(dpaa_sec_session));
2128c3e85bdcSAkhil Goyal 
2129c3e85bdcSAkhil Goyal 	/* Default IV length = 0 */
2130c3e85bdcSAkhil Goyal 	session->iv.length = 0;
2131c3e85bdcSAkhil Goyal 
2132c3e85bdcSAkhil Goyal 	/* Cipher Only */
2133c3e85bdcSAkhil Goyal 	if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER && xform->next == NULL) {
2134c3e85bdcSAkhil Goyal 		session->auth_alg = RTE_CRYPTO_AUTH_NULL;
2135c3e85bdcSAkhil Goyal 		dpaa_sec_cipher_init(dev, xform, session);
2136c3e85bdcSAkhil Goyal 
2137c3e85bdcSAkhil Goyal 	/* Authentication Only */
2138c3e85bdcSAkhil Goyal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
2139c3e85bdcSAkhil Goyal 		   xform->next == NULL) {
2140c3e85bdcSAkhil Goyal 		session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
2141c3e85bdcSAkhil Goyal 		dpaa_sec_auth_init(dev, xform, session);
2142c3e85bdcSAkhil Goyal 
2143c3e85bdcSAkhil Goyal 	/* Cipher then Authenticate */
2144c3e85bdcSAkhil Goyal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
2145c3e85bdcSAkhil Goyal 		   xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
2146c3e85bdcSAkhil Goyal 		if (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) {
2147c3e85bdcSAkhil Goyal 			dpaa_sec_cipher_init(dev, xform, session);
2148c3e85bdcSAkhil Goyal 			dpaa_sec_auth_init(dev, xform->next, session);
2149c3e85bdcSAkhil Goyal 		} else {
2150f163231eSHemant Agrawal 			DPAA_SEC_ERR("Not supported: Auth then Cipher");
2151c3e85bdcSAkhil Goyal 			return -EINVAL;
2152c3e85bdcSAkhil Goyal 		}
2153c3e85bdcSAkhil Goyal 
2154c3e85bdcSAkhil Goyal 	/* Authenticate then Cipher */
2155c3e85bdcSAkhil Goyal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
2156c3e85bdcSAkhil Goyal 		   xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
2157c3e85bdcSAkhil Goyal 		if (xform->next->cipher.op == RTE_CRYPTO_CIPHER_OP_DECRYPT) {
2158c3e85bdcSAkhil Goyal 			dpaa_sec_auth_init(dev, xform, session);
2159c3e85bdcSAkhil Goyal 			dpaa_sec_cipher_init(dev, xform->next, session);
2160c3e85bdcSAkhil Goyal 		} else {
2161f163231eSHemant Agrawal 			DPAA_SEC_ERR("Not supported: Auth then Cipher");
2162c3e85bdcSAkhil Goyal 			return -EINVAL;
2163c3e85bdcSAkhil Goyal 		}
2164c3e85bdcSAkhil Goyal 
2165c3e85bdcSAkhil Goyal 	/* AEAD operation for AES-GCM kind of Algorithms */
2166c3e85bdcSAkhil Goyal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD &&
2167c3e85bdcSAkhil Goyal 		   xform->next == NULL) {
2168c3e85bdcSAkhil Goyal 		dpaa_sec_aead_init(dev, xform, session);
2169c3e85bdcSAkhil Goyal 
2170c3e85bdcSAkhil Goyal 	} else {
2171f163231eSHemant Agrawal 		DPAA_SEC_ERR("Invalid crypto type");
2172c3e85bdcSAkhil Goyal 		return -EINVAL;
2173c3e85bdcSAkhil Goyal 	}
2174c3e85bdcSAkhil Goyal 	session->ctx_pool = internals->ctx_pool;
21753b617ee7SAkhil Goyal 	rte_spinlock_lock(&internals->lock);
21764e694fe5SAkhil Goyal 	for (i = 0; i < MAX_DPAA_CORES; i++) {
21774e694fe5SAkhil Goyal 		session->inq[i] = dpaa_sec_attach_rxq(internals);
21784e694fe5SAkhil Goyal 		if (session->inq[i] == NULL) {
2179f163231eSHemant Agrawal 			DPAA_SEC_ERR("unable to attach sec queue");
21804e694fe5SAkhil Goyal 			rte_spinlock_unlock(&internals->lock);
2181e79416d1SHemant Agrawal 			goto err1;
2182e79416d1SHemant Agrawal 		}
21834e694fe5SAkhil Goyal 	}
21844e694fe5SAkhil Goyal 	rte_spinlock_unlock(&internals->lock);
2185c3e85bdcSAkhil Goyal 
2186c3e85bdcSAkhil Goyal 	return 0;
2187e79416d1SHemant Agrawal 
2188e79416d1SHemant Agrawal err1:
2189e79416d1SHemant Agrawal 	rte_free(session->cipher_key.data);
2190e79416d1SHemant Agrawal 	rte_free(session->auth_key.data);
2191e79416d1SHemant Agrawal 	memset(session, 0, sizeof(dpaa_sec_session));
2192e79416d1SHemant Agrawal 
2193e79416d1SHemant Agrawal 	return -EINVAL;
2194c3e85bdcSAkhil Goyal }
2195c3e85bdcSAkhil Goyal 
2196c3e85bdcSAkhil Goyal static int
2197012c5076SPablo de Lara dpaa_sec_sym_session_configure(struct rte_cryptodev *dev,
2198c3e85bdcSAkhil Goyal 		struct rte_crypto_sym_xform *xform,
2199c3e85bdcSAkhil Goyal 		struct rte_cryptodev_sym_session *sess,
2200c3e85bdcSAkhil Goyal 		struct rte_mempool *mempool)
2201c3e85bdcSAkhil Goyal {
2202c3e85bdcSAkhil Goyal 	void *sess_private_data;
2203c3e85bdcSAkhil Goyal 	int ret;
2204c3e85bdcSAkhil Goyal 
2205c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2206c3e85bdcSAkhil Goyal 
2207c3e85bdcSAkhil Goyal 	if (rte_mempool_get(mempool, &sess_private_data)) {
2208f163231eSHemant Agrawal 		DPAA_SEC_ERR("Couldn't get object from session mempool");
2209c3e85bdcSAkhil Goyal 		return -ENOMEM;
2210c3e85bdcSAkhil Goyal 	}
2211c3e85bdcSAkhil Goyal 
2212c3e85bdcSAkhil Goyal 	ret = dpaa_sec_set_session_parameters(dev, xform, sess_private_data);
2213c3e85bdcSAkhil Goyal 	if (ret != 0) {
2214f163231eSHemant Agrawal 		DPAA_SEC_ERR("failed to configure session parameters");
2215c3e85bdcSAkhil Goyal 
2216c3e85bdcSAkhil Goyal 		/* Return session to mempool */
2217c3e85bdcSAkhil Goyal 		rte_mempool_put(mempool, sess_private_data);
2218c3e85bdcSAkhil Goyal 		return ret;
2219c3e85bdcSAkhil Goyal 	}
2220c3e85bdcSAkhil Goyal 
2221012c5076SPablo de Lara 	set_sym_session_private_data(sess, dev->driver_id,
2222c3e85bdcSAkhil Goyal 			sess_private_data);
2223c3e85bdcSAkhil Goyal 
2224e79416d1SHemant Agrawal 
2225c3e85bdcSAkhil Goyal 	return 0;
2226c3e85bdcSAkhil Goyal }
2227c3e85bdcSAkhil Goyal 
22283d0d5332SAkhil Goyal static inline void
22293d0d5332SAkhil Goyal free_session_memory(struct rte_cryptodev *dev, dpaa_sec_session *s)
2230c3e85bdcSAkhil Goyal {
2231e79416d1SHemant Agrawal 	struct dpaa_sec_dev_private *qi = dev->data->dev_private;
22323d0d5332SAkhil Goyal 	struct rte_mempool *sess_mp = rte_mempool_from_obj((void *)s);
22333d0d5332SAkhil Goyal 	uint8_t i;
2234e79416d1SHemant Agrawal 
2235e621d970SAkhil Goyal 	for (i = 0; i < MAX_DPAA_CORES; i++) {
2236e621d970SAkhil Goyal 		if (s->inq[i])
2237e621d970SAkhil Goyal 			dpaa_sec_detach_rxq(qi, s->inq[i]);
2238e621d970SAkhil Goyal 		s->inq[i] = NULL;
2239e621d970SAkhil Goyal 		s->qp[i] = NULL;
2240e621d970SAkhil Goyal 	}
2241c3e85bdcSAkhil Goyal 	rte_free(s->cipher_key.data);
2242c3e85bdcSAkhil Goyal 	rte_free(s->auth_key.data);
2243c3e85bdcSAkhil Goyal 	memset(s, 0, sizeof(dpaa_sec_session));
22443d0d5332SAkhil Goyal 	rte_mempool_put(sess_mp, (void *)s);
22453d0d5332SAkhil Goyal }
22463d0d5332SAkhil Goyal 
22473d0d5332SAkhil Goyal /** Clear the memory of session so it doesn't leave key material behind */
22483d0d5332SAkhil Goyal static void
22493d0d5332SAkhil Goyal dpaa_sec_sym_session_clear(struct rte_cryptodev *dev,
22503d0d5332SAkhil Goyal 		struct rte_cryptodev_sym_session *sess)
22513d0d5332SAkhil Goyal {
22523d0d5332SAkhil Goyal 	PMD_INIT_FUNC_TRACE();
22533d0d5332SAkhil Goyal 	uint8_t index = dev->driver_id;
22543d0d5332SAkhil Goyal 	void *sess_priv = get_sym_session_private_data(sess, index);
22553d0d5332SAkhil Goyal 	dpaa_sec_session *s = (dpaa_sec_session *)sess_priv;
22563d0d5332SAkhil Goyal 
22573d0d5332SAkhil Goyal 	if (sess_priv) {
22583d0d5332SAkhil Goyal 		free_session_memory(dev, s);
2259012c5076SPablo de Lara 		set_sym_session_private_data(sess, index, NULL);
2260c3e85bdcSAkhil Goyal 	}
2261c3e85bdcSAkhil Goyal }
2262c3e85bdcSAkhil Goyal 
2263c3e85bdcSAkhil Goyal static int
22641f14d500SAkhil Goyal dpaa_sec_set_ipsec_session(__rte_unused struct rte_cryptodev *dev,
22651f14d500SAkhil Goyal 			   struct rte_security_session_conf *conf,
22661f14d500SAkhil Goyal 			   void *sess)
22671f14d500SAkhil Goyal {
22681f14d500SAkhil Goyal 	struct dpaa_sec_dev_private *internals = dev->data->dev_private;
22691f14d500SAkhil Goyal 	struct rte_security_ipsec_xform *ipsec_xform = &conf->ipsec;
227005b12700SHemant Agrawal 	struct rte_crypto_auth_xform *auth_xform = NULL;
227105b12700SHemant Agrawal 	struct rte_crypto_cipher_xform *cipher_xform = NULL;
22721f14d500SAkhil Goyal 	dpaa_sec_session *session = (dpaa_sec_session *)sess;
22734e694fe5SAkhil Goyal 	uint32_t i;
22741f14d500SAkhil Goyal 
22751f14d500SAkhil Goyal 	PMD_INIT_FUNC_TRACE();
22761f14d500SAkhil Goyal 
2277b0894102SAkhil Goyal 	memset(session, 0, sizeof(dpaa_sec_session));
22781f14d500SAkhil Goyal 	if (ipsec_xform->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
22791f14d500SAkhil Goyal 		cipher_xform = &conf->crypto_xform->cipher;
228005b12700SHemant Agrawal 		if (conf->crypto_xform->next)
22811f14d500SAkhil Goyal 			auth_xform = &conf->crypto_xform->next->auth;
22821f14d500SAkhil Goyal 	} else {
22831f14d500SAkhil Goyal 		auth_xform = &conf->crypto_xform->auth;
228405b12700SHemant Agrawal 		if (conf->crypto_xform->next)
22851f14d500SAkhil Goyal 			cipher_xform = &conf->crypto_xform->next->cipher;
22861f14d500SAkhil Goyal 	}
22871f14d500SAkhil Goyal 	session->proto_alg = conf->protocol;
228805b12700SHemant Agrawal 
228905b12700SHemant Agrawal 	if (cipher_xform && cipher_xform->algo != RTE_CRYPTO_CIPHER_NULL) {
22901f14d500SAkhil Goyal 		session->cipher_key.data = rte_zmalloc(NULL,
22911f14d500SAkhil Goyal 						       cipher_xform->key.length,
22921f14d500SAkhil Goyal 						       RTE_CACHE_LINE_SIZE);
22931f14d500SAkhil Goyal 		if (session->cipher_key.data == NULL &&
22941f14d500SAkhil Goyal 				cipher_xform->key.length > 0) {
2295f163231eSHemant Agrawal 			DPAA_SEC_ERR("No Memory for cipher key");
22961f14d500SAkhil Goyal 			return -ENOMEM;
22971f14d500SAkhil Goyal 		}
229805b12700SHemant Agrawal 		memcpy(session->cipher_key.data, cipher_xform->key.data,
229905b12700SHemant Agrawal 				cipher_xform->key.length);
23001f14d500SAkhil Goyal 		session->cipher_key.length = cipher_xform->key.length;
230105b12700SHemant Agrawal 
230205b12700SHemant Agrawal 		switch (cipher_xform->algo) {
230305b12700SHemant Agrawal 		case RTE_CRYPTO_CIPHER_AES_CBC:
230405b12700SHemant Agrawal 		case RTE_CRYPTO_CIPHER_3DES_CBC:
230505b12700SHemant Agrawal 		case RTE_CRYPTO_CIPHER_AES_CTR:
230605b12700SHemant Agrawal 			break;
230705b12700SHemant Agrawal 		default:
230805b12700SHemant Agrawal 			DPAA_SEC_ERR("Crypto: Unsupported Cipher alg %u",
230905b12700SHemant Agrawal 				cipher_xform->algo);
231005b12700SHemant Agrawal 			goto out;
231105b12700SHemant Agrawal 		}
231205b12700SHemant Agrawal 		session->cipher_alg = cipher_xform->algo;
231305b12700SHemant Agrawal 	} else {
231405b12700SHemant Agrawal 		session->cipher_key.data = NULL;
231505b12700SHemant Agrawal 		session->cipher_key.length = 0;
231605b12700SHemant Agrawal 		session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
231705b12700SHemant Agrawal 	}
231805b12700SHemant Agrawal 
231905b12700SHemant Agrawal 	if (auth_xform && auth_xform->algo != RTE_CRYPTO_AUTH_NULL) {
23201f14d500SAkhil Goyal 		session->auth_key.data = rte_zmalloc(NULL,
23211f14d500SAkhil Goyal 						auth_xform->key.length,
23221f14d500SAkhil Goyal 						RTE_CACHE_LINE_SIZE);
23231f14d500SAkhil Goyal 		if (session->auth_key.data == NULL &&
23241f14d500SAkhil Goyal 				auth_xform->key.length > 0) {
2325f163231eSHemant Agrawal 			DPAA_SEC_ERR("No Memory for auth key");
23261f14d500SAkhil Goyal 			rte_free(session->cipher_key.data);
23271f14d500SAkhil Goyal 			return -ENOMEM;
23281f14d500SAkhil Goyal 		}
23291f14d500SAkhil Goyal 		memcpy(session->auth_key.data, auth_xform->key.data,
23301f14d500SAkhil Goyal 				auth_xform->key.length);
233105b12700SHemant Agrawal 		session->auth_key.length = auth_xform->key.length;
23321f14d500SAkhil Goyal 
23331f14d500SAkhil Goyal 		switch (auth_xform->algo) {
23341f14d500SAkhil Goyal 		case RTE_CRYPTO_AUTH_SHA1_HMAC:
23351f14d500SAkhil Goyal 		case RTE_CRYPTO_AUTH_MD5_HMAC:
23361f14d500SAkhil Goyal 		case RTE_CRYPTO_AUTH_SHA256_HMAC:
23371f14d500SAkhil Goyal 		case RTE_CRYPTO_AUTH_SHA384_HMAC:
23381f14d500SAkhil Goyal 		case RTE_CRYPTO_AUTH_SHA512_HMAC:
23391f14d500SAkhil Goyal 		case RTE_CRYPTO_AUTH_AES_CMAC:
23401f14d500SAkhil Goyal 			break;
234105b12700SHemant Agrawal 		default:
2342f163231eSHemant Agrawal 			DPAA_SEC_ERR("Crypto: Unsupported auth alg %u",
23431f14d500SAkhil Goyal 				auth_xform->algo);
23441f14d500SAkhil Goyal 			goto out;
23451f14d500SAkhil Goyal 		}
234605b12700SHemant Agrawal 		session->auth_alg = auth_xform->algo;
234705b12700SHemant Agrawal 	} else {
234805b12700SHemant Agrawal 		session->auth_key.data = NULL;
234905b12700SHemant Agrawal 		session->auth_key.length = 0;
235005b12700SHemant Agrawal 		session->auth_alg = RTE_CRYPTO_AUTH_NULL;
23511f14d500SAkhil Goyal 	}
23521f14d500SAkhil Goyal 
23531f14d500SAkhil Goyal 	if (ipsec_xform->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
23545ab35d2eSAkhil Goyal 		if (ipsec_xform->tunnel.type ==
23555ab35d2eSAkhil Goyal 				RTE_SECURITY_IPSEC_TUNNEL_IPV4) {
23565ab35d2eSAkhil Goyal 			memset(&session->encap_pdb, 0,
23575ab35d2eSAkhil Goyal 				sizeof(struct ipsec_encap_pdb) +
23581f14d500SAkhil Goyal 				sizeof(session->ip4_hdr));
23591f14d500SAkhil Goyal 			session->ip4_hdr.ip_v = IPVERSION;
23601f14d500SAkhil Goyal 			session->ip4_hdr.ip_hl = 5;
23611f14d500SAkhil Goyal 			session->ip4_hdr.ip_len = rte_cpu_to_be_16(
23621f14d500SAkhil Goyal 						sizeof(session->ip4_hdr));
23631f14d500SAkhil Goyal 			session->ip4_hdr.ip_tos = ipsec_xform->tunnel.ipv4.dscp;
23641f14d500SAkhil Goyal 			session->ip4_hdr.ip_id = 0;
23651f14d500SAkhil Goyal 			session->ip4_hdr.ip_off = 0;
23661f14d500SAkhil Goyal 			session->ip4_hdr.ip_ttl = ipsec_xform->tunnel.ipv4.ttl;
23671f14d500SAkhil Goyal 			session->ip4_hdr.ip_p = (ipsec_xform->proto ==
23685ab35d2eSAkhil Goyal 					RTE_SECURITY_IPSEC_SA_PROTO_ESP) ?
23695ab35d2eSAkhil Goyal 					IPPROTO_ESP : IPPROTO_AH;
23701f14d500SAkhil Goyal 			session->ip4_hdr.ip_sum = 0;
23715ab35d2eSAkhil Goyal 			session->ip4_hdr.ip_src =
23725ab35d2eSAkhil Goyal 					ipsec_xform->tunnel.ipv4.src_ip;
23735ab35d2eSAkhil Goyal 			session->ip4_hdr.ip_dst =
23745ab35d2eSAkhil Goyal 					ipsec_xform->tunnel.ipv4.dst_ip;
23751f14d500SAkhil Goyal 			session->ip4_hdr.ip_sum = calc_chksum((uint16_t *)
23761f14d500SAkhil Goyal 						(void *)&session->ip4_hdr,
23771f14d500SAkhil Goyal 						sizeof(struct ip));
23785ab35d2eSAkhil Goyal 			session->encap_pdb.ip_hdr_len = sizeof(struct ip);
23795ab35d2eSAkhil Goyal 		} else if (ipsec_xform->tunnel.type ==
23805ab35d2eSAkhil Goyal 				RTE_SECURITY_IPSEC_TUNNEL_IPV6) {
23815ab35d2eSAkhil Goyal 			memset(&session->encap_pdb, 0,
23825ab35d2eSAkhil Goyal 				sizeof(struct ipsec_encap_pdb) +
23835ab35d2eSAkhil Goyal 				sizeof(session->ip6_hdr));
23845ab35d2eSAkhil Goyal 			session->ip6_hdr.vtc_flow = rte_cpu_to_be_32(
23855ab35d2eSAkhil Goyal 				DPAA_IPv6_DEFAULT_VTC_FLOW |
23865ab35d2eSAkhil Goyal 				((ipsec_xform->tunnel.ipv6.dscp <<
23875ab35d2eSAkhil Goyal 					RTE_IPV6_HDR_TC_SHIFT) &
23885ab35d2eSAkhil Goyal 					RTE_IPV6_HDR_TC_MASK) |
23895ab35d2eSAkhil Goyal 				((ipsec_xform->tunnel.ipv6.flabel <<
23905ab35d2eSAkhil Goyal 					RTE_IPV6_HDR_FL_SHIFT) &
23915ab35d2eSAkhil Goyal 					RTE_IPV6_HDR_FL_MASK));
23925ab35d2eSAkhil Goyal 			/* Payload length will be updated by HW */
23935ab35d2eSAkhil Goyal 			session->ip6_hdr.payload_len = 0;
23945ab35d2eSAkhil Goyal 			session->ip6_hdr.hop_limits =
23955ab35d2eSAkhil Goyal 					ipsec_xform->tunnel.ipv6.hlimit;
23965ab35d2eSAkhil Goyal 			session->ip6_hdr.proto = (ipsec_xform->proto ==
23975ab35d2eSAkhil Goyal 					RTE_SECURITY_IPSEC_SA_PROTO_ESP) ?
23985ab35d2eSAkhil Goyal 					IPPROTO_ESP : IPPROTO_AH;
23995ab35d2eSAkhil Goyal 			memcpy(&session->ip6_hdr.src_addr,
24005ab35d2eSAkhil Goyal 					&ipsec_xform->tunnel.ipv6.src_addr, 16);
24015ab35d2eSAkhil Goyal 			memcpy(&session->ip6_hdr.dst_addr,
24025ab35d2eSAkhil Goyal 					&ipsec_xform->tunnel.ipv6.dst_addr, 16);
24035ab35d2eSAkhil Goyal 			session->encap_pdb.ip_hdr_len =
24045ab35d2eSAkhil Goyal 						sizeof(struct rte_ipv6_hdr);
24055ab35d2eSAkhil Goyal 		}
24061f14d500SAkhil Goyal 		session->encap_pdb.options =
24071f14d500SAkhil Goyal 			(IPVERSION << PDBNH_ESP_ENCAP_SHIFT) |
24081f14d500SAkhil Goyal 			PDBOPTS_ESP_OIHI_PDB_INL |
24091f14d500SAkhil Goyal 			PDBOPTS_ESP_IVSRC |
241079fde9d0SAkhil Goyal 			PDBHMO_ESP_ENCAP_DTTL |
241179fde9d0SAkhil Goyal 			PDBHMO_ESP_SNR;
24120f318781SAkhil Goyal 		if (ipsec_xform->options.esn)
24130f318781SAkhil Goyal 			session->encap_pdb.options |= PDBOPTS_ESP_ESN;
24141f14d500SAkhil Goyal 		session->encap_pdb.spi = ipsec_xform->spi;
24151f14d500SAkhil Goyal 		session->dir = DIR_ENC;
24161f14d500SAkhil Goyal 	} else if (ipsec_xform->direction ==
24171f14d500SAkhil Goyal 			RTE_SECURITY_IPSEC_SA_DIR_INGRESS) {
24181f14d500SAkhil Goyal 		memset(&session->decap_pdb, 0, sizeof(struct ipsec_decap_pdb));
24195ab35d2eSAkhil Goyal 		if (ipsec_xform->tunnel.type == RTE_SECURITY_IPSEC_TUNNEL_IPV4)
24201f14d500SAkhil Goyal 			session->decap_pdb.options = sizeof(struct ip) << 16;
24215ab35d2eSAkhil Goyal 		else
24225ab35d2eSAkhil Goyal 			session->decap_pdb.options =
24235ab35d2eSAkhil Goyal 					sizeof(struct rte_ipv6_hdr) << 16;
24240f318781SAkhil Goyal 		if (ipsec_xform->options.esn)
24250f318781SAkhil Goyal 			session->decap_pdb.options |= PDBOPTS_ESP_ESN;
24261f14d500SAkhil Goyal 		session->dir = DIR_DEC;
24271f14d500SAkhil Goyal 	} else
24281f14d500SAkhil Goyal 		goto out;
24291f14d500SAkhil Goyal 	session->ctx_pool = internals->ctx_pool;
24303b617ee7SAkhil Goyal 	rte_spinlock_lock(&internals->lock);
24314e694fe5SAkhil Goyal 	for (i = 0; i < MAX_DPAA_CORES; i++) {
24324e694fe5SAkhil Goyal 		session->inq[i] = dpaa_sec_attach_rxq(internals);
24334e694fe5SAkhil Goyal 		if (session->inq[i] == NULL) {
2434f163231eSHemant Agrawal 			DPAA_SEC_ERR("unable to attach sec queue");
24354e694fe5SAkhil Goyal 			rte_spinlock_unlock(&internals->lock);
24361f14d500SAkhil Goyal 			goto out;
24371f14d500SAkhil Goyal 		}
24384e694fe5SAkhil Goyal 	}
24394e694fe5SAkhil Goyal 	rte_spinlock_unlock(&internals->lock);
24401f14d500SAkhil Goyal 
2441a1173d55SHemant Agrawal 	return 0;
2442a1173d55SHemant Agrawal out:
2443a1173d55SHemant Agrawal 	rte_free(session->auth_key.data);
2444a1173d55SHemant Agrawal 	rte_free(session->cipher_key.data);
2445a1173d55SHemant Agrawal 	memset(session, 0, sizeof(dpaa_sec_session));
2446a1173d55SHemant Agrawal 	return -1;
2447a1173d55SHemant Agrawal }
24481f14d500SAkhil Goyal 
2449a1173d55SHemant Agrawal static int
2450a1173d55SHemant Agrawal dpaa_sec_set_pdcp_session(struct rte_cryptodev *dev,
2451a1173d55SHemant Agrawal 			  struct rte_security_session_conf *conf,
2452a1173d55SHemant Agrawal 			  void *sess)
2453a1173d55SHemant Agrawal {
2454a1173d55SHemant Agrawal 	struct rte_security_pdcp_xform *pdcp_xform = &conf->pdcp;
2455a1173d55SHemant Agrawal 	struct rte_crypto_sym_xform *xform = conf->crypto_xform;
2456a1173d55SHemant Agrawal 	struct rte_crypto_auth_xform *auth_xform = NULL;
2457a1173d55SHemant Agrawal 	struct rte_crypto_cipher_xform *cipher_xform = NULL;
2458a1173d55SHemant Agrawal 	dpaa_sec_session *session = (dpaa_sec_session *)sess;
2459a1173d55SHemant Agrawal 	struct dpaa_sec_dev_private *dev_priv = dev->data->dev_private;
24604e694fe5SAkhil Goyal 	uint32_t i;
2461a1173d55SHemant Agrawal 
2462a1173d55SHemant Agrawal 	PMD_INIT_FUNC_TRACE();
2463a1173d55SHemant Agrawal 
2464a1173d55SHemant Agrawal 	memset(session, 0, sizeof(dpaa_sec_session));
2465a1173d55SHemant Agrawal 
2466a1173d55SHemant Agrawal 	/* find xfrm types */
2467a1173d55SHemant Agrawal 	if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
2468a1173d55SHemant Agrawal 		cipher_xform = &xform->cipher;
2469a1173d55SHemant Agrawal 		if (xform->next != NULL)
2470a1173d55SHemant Agrawal 			auth_xform = &xform->next->auth;
2471a1173d55SHemant Agrawal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
2472a1173d55SHemant Agrawal 		auth_xform = &xform->auth;
2473a1173d55SHemant Agrawal 		if (xform->next != NULL)
2474a1173d55SHemant Agrawal 			cipher_xform = &xform->next->cipher;
2475a1173d55SHemant Agrawal 	} else {
2476a1173d55SHemant Agrawal 		DPAA_SEC_ERR("Invalid crypto type");
2477a1173d55SHemant Agrawal 		return -EINVAL;
2478a1173d55SHemant Agrawal 	}
2479a1173d55SHemant Agrawal 
2480a1173d55SHemant Agrawal 	session->proto_alg = conf->protocol;
2481a1173d55SHemant Agrawal 	if (cipher_xform) {
2482a1173d55SHemant Agrawal 		session->cipher_key.data = rte_zmalloc(NULL,
2483a1173d55SHemant Agrawal 					       cipher_xform->key.length,
2484a1173d55SHemant Agrawal 					       RTE_CACHE_LINE_SIZE);
2485a1173d55SHemant Agrawal 		if (session->cipher_key.data == NULL &&
2486a1173d55SHemant Agrawal 				cipher_xform->key.length > 0) {
2487a1173d55SHemant Agrawal 			DPAA_SEC_ERR("No Memory for cipher key");
2488a1173d55SHemant Agrawal 			return -ENOMEM;
2489a1173d55SHemant Agrawal 		}
2490a1173d55SHemant Agrawal 		session->cipher_key.length = cipher_xform->key.length;
2491a1173d55SHemant Agrawal 		memcpy(session->cipher_key.data, cipher_xform->key.data,
2492a1173d55SHemant Agrawal 			cipher_xform->key.length);
2493a1173d55SHemant Agrawal 		session->dir = (cipher_xform->op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
2494a1173d55SHemant Agrawal 					DIR_ENC : DIR_DEC;
2495a1173d55SHemant Agrawal 		session->cipher_alg = cipher_xform->algo;
2496a1173d55SHemant Agrawal 	} else {
2497a1173d55SHemant Agrawal 		session->cipher_key.data = NULL;
2498a1173d55SHemant Agrawal 		session->cipher_key.length = 0;
2499a1173d55SHemant Agrawal 		session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
2500a1173d55SHemant Agrawal 		session->dir = DIR_ENC;
2501a1173d55SHemant Agrawal 	}
2502a1173d55SHemant Agrawal 
2503a1173d55SHemant Agrawal 	if (pdcp_xform->domain == RTE_SECURITY_PDCP_MODE_CONTROL) {
2504eac60082SVakul Garg 		if (pdcp_xform->sn_size != RTE_SECURITY_PDCP_SN_SIZE_5 &&
2505eac60082SVakul Garg 		    pdcp_xform->sn_size != RTE_SECURITY_PDCP_SN_SIZE_12) {
2506a1173d55SHemant Agrawal 			DPAA_SEC_ERR(
2507eac60082SVakul Garg 				"PDCP Seq Num size should be 5/12 bits for cmode");
2508a1173d55SHemant Agrawal 			goto out;
2509a1173d55SHemant Agrawal 		}
25102e4cbdb4SVakul Garg 	}
25112e4cbdb4SVakul Garg 
2512a1173d55SHemant Agrawal 	if (auth_xform) {
2513a1173d55SHemant Agrawal 		session->auth_key.data = rte_zmalloc(NULL,
2514a1173d55SHemant Agrawal 						     auth_xform->key.length,
2515a1173d55SHemant Agrawal 						     RTE_CACHE_LINE_SIZE);
25162e4cbdb4SVakul Garg 		if (!session->auth_key.data &&
2517a1173d55SHemant Agrawal 		    auth_xform->key.length > 0) {
2518a1173d55SHemant Agrawal 			DPAA_SEC_ERR("No Memory for auth key");
2519a1173d55SHemant Agrawal 			rte_free(session->cipher_key.data);
2520a1173d55SHemant Agrawal 			return -ENOMEM;
2521a1173d55SHemant Agrawal 		}
2522a1173d55SHemant Agrawal 		session->auth_key.length = auth_xform->key.length;
2523a1173d55SHemant Agrawal 		memcpy(session->auth_key.data, auth_xform->key.data,
2524a1173d55SHemant Agrawal 		       auth_xform->key.length);
2525a1173d55SHemant Agrawal 		session->auth_alg = auth_xform->algo;
2526a1173d55SHemant Agrawal 	} else {
2527a1173d55SHemant Agrawal 		session->auth_key.data = NULL;
2528a1173d55SHemant Agrawal 		session->auth_key.length = 0;
25292e4cbdb4SVakul Garg 		session->auth_alg = 0;
2530a1173d55SHemant Agrawal 	}
2531a1173d55SHemant Agrawal 	session->pdcp.domain = pdcp_xform->domain;
2532a1173d55SHemant Agrawal 	session->pdcp.bearer = pdcp_xform->bearer;
2533a1173d55SHemant Agrawal 	session->pdcp.pkt_dir = pdcp_xform->pkt_dir;
2534a1173d55SHemant Agrawal 	session->pdcp.sn_size = pdcp_xform->sn_size;
2535a1173d55SHemant Agrawal 	session->pdcp.hfn = pdcp_xform->hfn;
2536a1173d55SHemant Agrawal 	session->pdcp.hfn_threshold = pdcp_xform->hfn_threshold;
25376a0c9d36SAkhil Goyal 	session->pdcp.hfn_ovd = pdcp_xform->hfn_ovrd;
25386a0c9d36SAkhil Goyal 	session->pdcp.hfn_ovd_offset = cipher_xform->iv.offset;
2539a1173d55SHemant Agrawal 
2540a1173d55SHemant Agrawal 	session->ctx_pool = dev_priv->ctx_pool;
2541a1173d55SHemant Agrawal 	rte_spinlock_lock(&dev_priv->lock);
25424e694fe5SAkhil Goyal 	for (i = 0; i < MAX_DPAA_CORES; i++) {
25434e694fe5SAkhil Goyal 		session->inq[i] = dpaa_sec_attach_rxq(dev_priv);
25444e694fe5SAkhil Goyal 		if (session->inq[i] == NULL) {
2545a1173d55SHemant Agrawal 			DPAA_SEC_ERR("unable to attach sec queue");
25464e694fe5SAkhil Goyal 			rte_spinlock_unlock(&dev_priv->lock);
2547a1173d55SHemant Agrawal 			goto out;
2548a1173d55SHemant Agrawal 		}
25494e694fe5SAkhil Goyal 	}
25504e694fe5SAkhil Goyal 	rte_spinlock_unlock(&dev_priv->lock);
25511f14d500SAkhil Goyal 	return 0;
25521f14d500SAkhil Goyal out:
25531f14d500SAkhil Goyal 	rte_free(session->auth_key.data);
25541f14d500SAkhil Goyal 	rte_free(session->cipher_key.data);
25551f14d500SAkhil Goyal 	memset(session, 0, sizeof(dpaa_sec_session));
25561f14d500SAkhil Goyal 	return -1;
25571f14d500SAkhil Goyal }
25581f14d500SAkhil Goyal 
25591f14d500SAkhil Goyal static int
25601f14d500SAkhil Goyal dpaa_sec_security_session_create(void *dev,
25611f14d500SAkhil Goyal 				 struct rte_security_session_conf *conf,
25621f14d500SAkhil Goyal 				 struct rte_security_session *sess,
25631f14d500SAkhil Goyal 				 struct rte_mempool *mempool)
25641f14d500SAkhil Goyal {
25651f14d500SAkhil Goyal 	void *sess_private_data;
25661f14d500SAkhil Goyal 	struct rte_cryptodev *cdev = (struct rte_cryptodev *)dev;
25671f14d500SAkhil Goyal 	int ret;
25681f14d500SAkhil Goyal 
25691f14d500SAkhil Goyal 	if (rte_mempool_get(mempool, &sess_private_data)) {
2570f163231eSHemant Agrawal 		DPAA_SEC_ERR("Couldn't get object from session mempool");
25711f14d500SAkhil Goyal 		return -ENOMEM;
25721f14d500SAkhil Goyal 	}
25731f14d500SAkhil Goyal 
25741f14d500SAkhil Goyal 	switch (conf->protocol) {
25751f14d500SAkhil Goyal 	case RTE_SECURITY_PROTOCOL_IPSEC:
25761f14d500SAkhil Goyal 		ret = dpaa_sec_set_ipsec_session(cdev, conf,
25771f14d500SAkhil Goyal 				sess_private_data);
25781f14d500SAkhil Goyal 		break;
2579a1173d55SHemant Agrawal 	case RTE_SECURITY_PROTOCOL_PDCP:
2580a1173d55SHemant Agrawal 		ret = dpaa_sec_set_pdcp_session(cdev, conf,
2581a1173d55SHemant Agrawal 				sess_private_data);
2582a1173d55SHemant Agrawal 		break;
25831f14d500SAkhil Goyal 	case RTE_SECURITY_PROTOCOL_MACSEC:
25841f14d500SAkhil Goyal 		return -ENOTSUP;
25851f14d500SAkhil Goyal 	default:
25861f14d500SAkhil Goyal 		return -EINVAL;
25871f14d500SAkhil Goyal 	}
25881f14d500SAkhil Goyal 	if (ret != 0) {
2589f163231eSHemant Agrawal 		DPAA_SEC_ERR("failed to configure session parameters");
25901f14d500SAkhil Goyal 		/* Return session to mempool */
25911f14d500SAkhil Goyal 		rte_mempool_put(mempool, sess_private_data);
25921f14d500SAkhil Goyal 		return ret;
25931f14d500SAkhil Goyal 	}
25941f14d500SAkhil Goyal 
25951f14d500SAkhil Goyal 	set_sec_session_private_data(sess, sess_private_data);
25961f14d500SAkhil Goyal 
25971f14d500SAkhil Goyal 	return ret;
25981f14d500SAkhil Goyal }
25991f14d500SAkhil Goyal 
26001f14d500SAkhil Goyal /** Clear the memory of session so it doesn't leave key material behind */
26011f14d500SAkhil Goyal static int
26021f14d500SAkhil Goyal dpaa_sec_security_session_destroy(void *dev __rte_unused,
26031f14d500SAkhil Goyal 		struct rte_security_session *sess)
26041f14d500SAkhil Goyal {
26051f14d500SAkhil Goyal 	PMD_INIT_FUNC_TRACE();
26061f14d500SAkhil Goyal 	void *sess_priv = get_sec_session_private_data(sess);
26071f14d500SAkhil Goyal 	dpaa_sec_session *s = (dpaa_sec_session *)sess_priv;
26081f14d500SAkhil Goyal 
26091f14d500SAkhil Goyal 	if (sess_priv) {
26103d0d5332SAkhil Goyal 		free_session_memory((struct rte_cryptodev *)dev, s);
26111f14d500SAkhil Goyal 		set_sec_session_private_data(sess, NULL);
26121f14d500SAkhil Goyal 	}
26131f14d500SAkhil Goyal 	return 0;
26141f14d500SAkhil Goyal }
26151f14d500SAkhil Goyal 
26161f14d500SAkhil Goyal static int
26177e3e2954SAkhil Goyal dpaa_sec_dev_configure(struct rte_cryptodev *dev,
2618c3e85bdcSAkhil Goyal 		       struct rte_cryptodev_config *config __rte_unused)
2619c3e85bdcSAkhil Goyal {
26207e3e2954SAkhil Goyal 
26217e3e2954SAkhil Goyal 	char str[20];
26227e3e2954SAkhil Goyal 	struct dpaa_sec_dev_private *internals;
26237e3e2954SAkhil Goyal 
2624c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2625c3e85bdcSAkhil Goyal 
26267e3e2954SAkhil Goyal 	internals = dev->data->dev_private;
2627a1e8241aSPallantla Poornima 	snprintf(str, sizeof(str), "ctx_pool_%d", dev->data->dev_id);
26287e3e2954SAkhil Goyal 	if (!internals->ctx_pool) {
26297e3e2954SAkhil Goyal 		internals->ctx_pool = rte_mempool_create((const char *)str,
26307e3e2954SAkhil Goyal 							CTX_POOL_NUM_BUFS,
26317e3e2954SAkhil Goyal 							CTX_POOL_BUF_SIZE,
26327e3e2954SAkhil Goyal 							CTX_POOL_CACHE_SIZE, 0,
26337e3e2954SAkhil Goyal 							NULL, NULL, NULL, NULL,
26347e3e2954SAkhil Goyal 							SOCKET_ID_ANY, 0);
26357e3e2954SAkhil Goyal 		if (!internals->ctx_pool) {
2636f163231eSHemant Agrawal 			DPAA_SEC_ERR("%s create failed\n", str);
26377e3e2954SAkhil Goyal 			return -ENOMEM;
26387e3e2954SAkhil Goyal 		}
26397e3e2954SAkhil Goyal 	} else
2640f163231eSHemant Agrawal 		DPAA_SEC_INFO("mempool already created for dev_id : %d",
26417e3e2954SAkhil Goyal 				dev->data->dev_id);
26427e3e2954SAkhil Goyal 
2643c3e85bdcSAkhil Goyal 	return 0;
2644c3e85bdcSAkhil Goyal }
2645c3e85bdcSAkhil Goyal 
2646c3e85bdcSAkhil Goyal static int
2647c3e85bdcSAkhil Goyal dpaa_sec_dev_start(struct rte_cryptodev *dev __rte_unused)
2648c3e85bdcSAkhil Goyal {
2649c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2650c3e85bdcSAkhil Goyal 	return 0;
2651c3e85bdcSAkhil Goyal }
2652c3e85bdcSAkhil Goyal 
2653c3e85bdcSAkhil Goyal static void
2654c3e85bdcSAkhil Goyal dpaa_sec_dev_stop(struct rte_cryptodev *dev __rte_unused)
2655c3e85bdcSAkhil Goyal {
2656c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2657c3e85bdcSAkhil Goyal }
2658c3e85bdcSAkhil Goyal 
2659c3e85bdcSAkhil Goyal static int
26607e3e2954SAkhil Goyal dpaa_sec_dev_close(struct rte_cryptodev *dev)
2661c3e85bdcSAkhil Goyal {
26627e3e2954SAkhil Goyal 	struct dpaa_sec_dev_private *internals;
26637e3e2954SAkhil Goyal 
2664c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
26657e3e2954SAkhil Goyal 
26667e3e2954SAkhil Goyal 	if (dev == NULL)
26677e3e2954SAkhil Goyal 		return -ENOMEM;
26687e3e2954SAkhil Goyal 
26697e3e2954SAkhil Goyal 	internals = dev->data->dev_private;
26707e3e2954SAkhil Goyal 	rte_mempool_free(internals->ctx_pool);
26717e3e2954SAkhil Goyal 	internals->ctx_pool = NULL;
26727e3e2954SAkhil Goyal 
2673c3e85bdcSAkhil Goyal 	return 0;
2674c3e85bdcSAkhil Goyal }
2675c3e85bdcSAkhil Goyal 
2676c3e85bdcSAkhil Goyal static void
2677c3e85bdcSAkhil Goyal dpaa_sec_dev_infos_get(struct rte_cryptodev *dev,
2678c3e85bdcSAkhil Goyal 		       struct rte_cryptodev_info *info)
2679c3e85bdcSAkhil Goyal {
2680c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals = dev->data->dev_private;
2681c3e85bdcSAkhil Goyal 
2682c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2683c3e85bdcSAkhil Goyal 	if (info != NULL) {
2684c3e85bdcSAkhil Goyal 		info->max_nb_queue_pairs = internals->max_nb_queue_pairs;
2685c3e85bdcSAkhil Goyal 		info->feature_flags = dev->feature_flags;
2686c3e85bdcSAkhil Goyal 		info->capabilities = dpaa_sec_capabilities;
2687c3e85bdcSAkhil Goyal 		info->sym.max_nb_sessions = internals->max_nb_sessions;
2688c3e85bdcSAkhil Goyal 		info->driver_id = cryptodev_driver_id;
2689c3e85bdcSAkhil Goyal 	}
2690c3e85bdcSAkhil Goyal }
2691c3e85bdcSAkhil Goyal 
2692c3e85bdcSAkhil Goyal static struct rte_cryptodev_ops crypto_ops = {
2693c3e85bdcSAkhil Goyal 	.dev_configure	      = dpaa_sec_dev_configure,
2694c3e85bdcSAkhil Goyal 	.dev_start	      = dpaa_sec_dev_start,
2695c3e85bdcSAkhil Goyal 	.dev_stop	      = dpaa_sec_dev_stop,
2696c3e85bdcSAkhil Goyal 	.dev_close	      = dpaa_sec_dev_close,
2697c3e85bdcSAkhil Goyal 	.dev_infos_get        = dpaa_sec_dev_infos_get,
2698c3e85bdcSAkhil Goyal 	.queue_pair_setup     = dpaa_sec_queue_pair_setup,
2699c3e85bdcSAkhil Goyal 	.queue_pair_release   = dpaa_sec_queue_pair_release,
2700c3e85bdcSAkhil Goyal 	.queue_pair_count     = dpaa_sec_queue_pair_count,
2701012c5076SPablo de Lara 	.sym_session_get_size     = dpaa_sec_sym_session_get_size,
2702012c5076SPablo de Lara 	.sym_session_configure    = dpaa_sec_sym_session_configure,
2703012c5076SPablo de Lara 	.sym_session_clear        = dpaa_sec_sym_session_clear
2704c3e85bdcSAkhil Goyal };
2705c3e85bdcSAkhil Goyal 
27061f14d500SAkhil Goyal static const struct rte_security_capability *
27071f14d500SAkhil Goyal dpaa_sec_capabilities_get(void *device __rte_unused)
27081f14d500SAkhil Goyal {
27091f14d500SAkhil Goyal 	return dpaa_sec_security_cap;
27101f14d500SAkhil Goyal }
27111f14d500SAkhil Goyal 
2712b74fd6b8SFerruh Yigit static const struct rte_security_ops dpaa_sec_security_ops = {
27131f14d500SAkhil Goyal 	.session_create = dpaa_sec_security_session_create,
27141f14d500SAkhil Goyal 	.session_update = NULL,
27151f14d500SAkhil Goyal 	.session_stats_get = NULL,
27161f14d500SAkhil Goyal 	.session_destroy = dpaa_sec_security_session_destroy,
27171f14d500SAkhil Goyal 	.set_pkt_metadata = NULL,
27181f14d500SAkhil Goyal 	.capabilities_get = dpaa_sec_capabilities_get
27191f14d500SAkhil Goyal };
27201f14d500SAkhil Goyal 
2721c3e85bdcSAkhil Goyal static int
2722c3e85bdcSAkhil Goyal dpaa_sec_uninit(struct rte_cryptodev *dev)
2723c3e85bdcSAkhil Goyal {
2724debef417SShreyansh Jain 	struct dpaa_sec_dev_private *internals;
2725c3e85bdcSAkhil Goyal 
2726c3e85bdcSAkhil Goyal 	if (dev == NULL)
2727c3e85bdcSAkhil Goyal 		return -ENODEV;
2728c3e85bdcSAkhil Goyal 
2729debef417SShreyansh Jain 	internals = dev->data->dev_private;
27301f14d500SAkhil Goyal 	rte_free(dev->security_ctx);
27311f14d500SAkhil Goyal 
27327e3e2954SAkhil Goyal 	/* In case close has been called, internals->ctx_pool would be NULL */
2733c3e85bdcSAkhil Goyal 	rte_mempool_free(internals->ctx_pool);
2734c3e85bdcSAkhil Goyal 	rte_free(internals);
2735c3e85bdcSAkhil Goyal 
2736f163231eSHemant Agrawal 	DPAA_SEC_INFO("Closing DPAA_SEC device %s on numa socket %u",
2737c3e85bdcSAkhil Goyal 		      dev->data->name, rte_socket_id());
2738c3e85bdcSAkhil Goyal 
2739c3e85bdcSAkhil Goyal 	return 0;
2740c3e85bdcSAkhil Goyal }
2741c3e85bdcSAkhil Goyal 
2742c3e85bdcSAkhil Goyal static int
2743c3e85bdcSAkhil Goyal dpaa_sec_dev_init(struct rte_cryptodev *cryptodev)
2744c3e85bdcSAkhil Goyal {
2745c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals;
27461f14d500SAkhil Goyal 	struct rte_security_ctx *security_instance;
2747c3e85bdcSAkhil Goyal 	struct dpaa_sec_qp *qp;
2748e79416d1SHemant Agrawal 	uint32_t i, flags;
2749c3e85bdcSAkhil Goyal 	int ret;
2750c3e85bdcSAkhil Goyal 
2751c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2752c3e85bdcSAkhil Goyal 
2753c3e85bdcSAkhil Goyal 	cryptodev->driver_id = cryptodev_driver_id;
2754c3e85bdcSAkhil Goyal 	cryptodev->dev_ops = &crypto_ops;
2755c3e85bdcSAkhil Goyal 
2756c3e85bdcSAkhil Goyal 	cryptodev->enqueue_burst = dpaa_sec_enqueue_burst;
2757c3e85bdcSAkhil Goyal 	cryptodev->dequeue_burst = dpaa_sec_dequeue_burst;
2758c3e85bdcSAkhil Goyal 	cryptodev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
2759c3e85bdcSAkhil Goyal 			RTE_CRYPTODEV_FF_HW_ACCELERATED |
27601f14d500SAkhil Goyal 			RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
2761a74af788SAkhil Goyal 			RTE_CRYPTODEV_FF_SECURITY |
27622717246eSPablo de Lara 			RTE_CRYPTODEV_FF_IN_PLACE_SGL |
27632717246eSPablo de Lara 			RTE_CRYPTODEV_FF_OOP_SGL_IN_SGL_OUT |
27642717246eSPablo de Lara 			RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT |
27652717246eSPablo de Lara 			RTE_CRYPTODEV_FF_OOP_LB_IN_SGL_OUT |
27662717246eSPablo de Lara 			RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT;
2767c3e85bdcSAkhil Goyal 
2768c3e85bdcSAkhil Goyal 	internals = cryptodev->data->dev_private;
2769e79416d1SHemant Agrawal 	internals->max_nb_queue_pairs = RTE_DPAA_MAX_NB_SEC_QPS;
2770c3e85bdcSAkhil Goyal 	internals->max_nb_sessions = RTE_DPAA_SEC_PMD_MAX_NB_SESSIONS;
2771c3e85bdcSAkhil Goyal 
27721f14d500SAkhil Goyal 	/*
27731f14d500SAkhil Goyal 	 * For secondary processes, we don't initialise any further as primary
27741f14d500SAkhil Goyal 	 * has already done this work. Only check we don't need a different
27751f14d500SAkhil Goyal 	 * RX function
27761f14d500SAkhil Goyal 	 */
27771f14d500SAkhil Goyal 	if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
2778f163231eSHemant Agrawal 		DPAA_SEC_WARN("Device already init by primary process");
27791f14d500SAkhil Goyal 		return 0;
27801f14d500SAkhil Goyal 	}
27811f14d500SAkhil Goyal 
27821f14d500SAkhil Goyal 	/* Initialize security_ctx only for primary process*/
27831f14d500SAkhil Goyal 	security_instance = rte_malloc("rte_security_instances_ops",
27841f14d500SAkhil Goyal 				sizeof(struct rte_security_ctx), 0);
27851f14d500SAkhil Goyal 	if (security_instance == NULL)
27861f14d500SAkhil Goyal 		return -ENOMEM;
27871f14d500SAkhil Goyal 	security_instance->device = (void *)cryptodev;
27881f14d500SAkhil Goyal 	security_instance->ops = &dpaa_sec_security_ops;
27891f14d500SAkhil Goyal 	security_instance->sess_cnt = 0;
27901f14d500SAkhil Goyal 	cryptodev->security_ctx = security_instance;
27911f14d500SAkhil Goyal 
27923b617ee7SAkhil Goyal 	rte_spinlock_init(&internals->lock);
2793c3e85bdcSAkhil Goyal 	for (i = 0; i < internals->max_nb_queue_pairs; i++) {
2794c3e85bdcSAkhil Goyal 		/* init qman fq for queue pair */
2795c3e85bdcSAkhil Goyal 		qp = &internals->qps[i];
2796c3e85bdcSAkhil Goyal 		ret = dpaa_sec_init_tx(&qp->outq);
2797c3e85bdcSAkhil Goyal 		if (ret) {
2798f163231eSHemant Agrawal 			DPAA_SEC_ERR("config tx of queue pair  %d", i);
2799c3e85bdcSAkhil Goyal 			goto init_error;
2800c3e85bdcSAkhil Goyal 		}
2801e79416d1SHemant Agrawal 	}
2802e79416d1SHemant Agrawal 
2803e79416d1SHemant Agrawal 	flags = QMAN_FQ_FLAG_LOCKED | QMAN_FQ_FLAG_DYNAMIC_FQID |
2804e79416d1SHemant Agrawal 		QMAN_FQ_FLAG_TO_DCPORTAL;
28054e694fe5SAkhil Goyal 	for (i = 0; i < MAX_DPAA_CORES * internals->max_nb_sessions; i++) {
2806e79416d1SHemant Agrawal 		/* create rx qman fq for sessions*/
2807e79416d1SHemant Agrawal 		ret = qman_create_fq(0, flags, &internals->inq[i]);
2808e79416d1SHemant Agrawal 		if (unlikely(ret != 0)) {
2809f163231eSHemant Agrawal 			DPAA_SEC_ERR("sec qman_create_fq failed");
2810c3e85bdcSAkhil Goyal 			goto init_error;
2811c3e85bdcSAkhil Goyal 		}
2812c3e85bdcSAkhil Goyal 	}
2813c3e85bdcSAkhil Goyal 
2814f163231eSHemant Agrawal 	RTE_LOG(INFO, PMD, "%s cryptodev init\n", cryptodev->data->name);
2815c3e85bdcSAkhil Goyal 	return 0;
2816c3e85bdcSAkhil Goyal 
2817c3e85bdcSAkhil Goyal init_error:
2818f163231eSHemant Agrawal 	DPAA_SEC_ERR("driver %s: create failed\n", cryptodev->data->name);
2819c3e85bdcSAkhil Goyal 
2820c3e85bdcSAkhil Goyal 	dpaa_sec_uninit(cryptodev);
2821c3e85bdcSAkhil Goyal 	return -EFAULT;
2822c3e85bdcSAkhil Goyal }
2823c3e85bdcSAkhil Goyal 
2824c3e85bdcSAkhil Goyal static int
2825eb5a9a76SShreyansh Jain cryptodev_dpaa_sec_probe(struct rte_dpaa_driver *dpaa_drv __rte_unused,
2826c3e85bdcSAkhil Goyal 				struct rte_dpaa_device *dpaa_dev)
2827c3e85bdcSAkhil Goyal {
2828c3e85bdcSAkhil Goyal 	struct rte_cryptodev *cryptodev;
2829c3e85bdcSAkhil Goyal 	char cryptodev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
2830c3e85bdcSAkhil Goyal 
2831c3e85bdcSAkhil Goyal 	int retval;
2832c3e85bdcSAkhil Goyal 
2833a1e8241aSPallantla Poornima 	snprintf(cryptodev_name, sizeof(cryptodev_name), "dpaa_sec-%d",
2834a1e8241aSPallantla Poornima 			dpaa_dev->id.dev_id);
2835c3e85bdcSAkhil Goyal 
2836c3e85bdcSAkhil Goyal 	cryptodev = rte_cryptodev_pmd_allocate(cryptodev_name, rte_socket_id());
2837c3e85bdcSAkhil Goyal 	if (cryptodev == NULL)
2838c3e85bdcSAkhil Goyal 		return -ENOMEM;
2839c3e85bdcSAkhil Goyal 
2840c3e85bdcSAkhil Goyal 	if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
2841c3e85bdcSAkhil Goyal 		cryptodev->data->dev_private = rte_zmalloc_socket(
2842c3e85bdcSAkhil Goyal 					"cryptodev private structure",
2843c3e85bdcSAkhil Goyal 					sizeof(struct dpaa_sec_dev_private),
2844c3e85bdcSAkhil Goyal 					RTE_CACHE_LINE_SIZE,
2845c3e85bdcSAkhil Goyal 					rte_socket_id());
2846c3e85bdcSAkhil Goyal 
2847c3e85bdcSAkhil Goyal 		if (cryptodev->data->dev_private == NULL)
2848c3e85bdcSAkhil Goyal 			rte_panic("Cannot allocate memzone for private "
2849c3e85bdcSAkhil Goyal 					"device data");
2850c3e85bdcSAkhil Goyal 	}
2851c3e85bdcSAkhil Goyal 
2852c3e85bdcSAkhil Goyal 	dpaa_dev->crypto_dev = cryptodev;
2853c3e85bdcSAkhil Goyal 	cryptodev->device = &dpaa_dev->device;
2854c3e85bdcSAkhil Goyal 
2855c3e85bdcSAkhil Goyal 	/* init user callbacks */
2856c3e85bdcSAkhil Goyal 	TAILQ_INIT(&(cryptodev->link_intr_cbs));
2857c3e85bdcSAkhil Goyal 
2858c3e85bdcSAkhil Goyal 	/* if sec device version is not configured */
2859c3e85bdcSAkhil Goyal 	if (!rta_get_sec_era()) {
2860c3e85bdcSAkhil Goyal 		const struct device_node *caam_node;
2861c3e85bdcSAkhil Goyal 
2862c3e85bdcSAkhil Goyal 		for_each_compatible_node(caam_node, NULL, "fsl,sec-v4.0") {
2863c3e85bdcSAkhil Goyal 			const uint32_t *prop = of_get_property(caam_node,
2864c3e85bdcSAkhil Goyal 					"fsl,sec-era",
2865c3e85bdcSAkhil Goyal 					NULL);
2866c3e85bdcSAkhil Goyal 			if (prop) {
2867c3e85bdcSAkhil Goyal 				rta_set_sec_era(
2868c3e85bdcSAkhil Goyal 					INTL_SEC_ERA(rte_cpu_to_be_32(*prop)));
2869c3e85bdcSAkhil Goyal 				break;
2870c3e85bdcSAkhil Goyal 			}
2871c3e85bdcSAkhil Goyal 		}
2872c3e85bdcSAkhil Goyal 	}
2873c3e85bdcSAkhil Goyal 
2874c3e85bdcSAkhil Goyal 	/* Invoke PMD device initialization function */
2875c3e85bdcSAkhil Goyal 	retval = dpaa_sec_dev_init(cryptodev);
2876c3e85bdcSAkhil Goyal 	if (retval == 0)
2877c3e85bdcSAkhil Goyal 		return 0;
2878c3e85bdcSAkhil Goyal 
2879c3e85bdcSAkhil Goyal 	/* In case of error, cleanup is done */
2880c3e85bdcSAkhil Goyal 	if (rte_eal_process_type() == RTE_PROC_PRIMARY)
2881c3e85bdcSAkhil Goyal 		rte_free(cryptodev->data->dev_private);
2882c3e85bdcSAkhil Goyal 
2883c3e85bdcSAkhil Goyal 	rte_cryptodev_pmd_release_device(cryptodev);
2884c3e85bdcSAkhil Goyal 
2885c3e85bdcSAkhil Goyal 	return -ENXIO;
2886c3e85bdcSAkhil Goyal }
2887c3e85bdcSAkhil Goyal 
2888c3e85bdcSAkhil Goyal static int
2889c3e85bdcSAkhil Goyal cryptodev_dpaa_sec_remove(struct rte_dpaa_device *dpaa_dev)
2890c3e85bdcSAkhil Goyal {
2891c3e85bdcSAkhil Goyal 	struct rte_cryptodev *cryptodev;
2892c3e85bdcSAkhil Goyal 	int ret;
2893c3e85bdcSAkhil Goyal 
2894c3e85bdcSAkhil Goyal 	cryptodev = dpaa_dev->crypto_dev;
2895c3e85bdcSAkhil Goyal 	if (cryptodev == NULL)
2896c3e85bdcSAkhil Goyal 		return -ENODEV;
2897c3e85bdcSAkhil Goyal 
2898c3e85bdcSAkhil Goyal 	ret = dpaa_sec_uninit(cryptodev);
2899c3e85bdcSAkhil Goyal 	if (ret)
2900c3e85bdcSAkhil Goyal 		return ret;
2901c3e85bdcSAkhil Goyal 
2902f2f020d2SDeclan Doherty 	return rte_cryptodev_pmd_destroy(cryptodev);
2903c3e85bdcSAkhil Goyal }
2904c3e85bdcSAkhil Goyal 
2905c3e85bdcSAkhil Goyal static struct rte_dpaa_driver rte_dpaa_sec_driver = {
2906c3e85bdcSAkhil Goyal 	.drv_type = FSL_DPAA_CRYPTO,
2907c3e85bdcSAkhil Goyal 	.driver = {
2908c3e85bdcSAkhil Goyal 		.name = "DPAA SEC PMD"
2909c3e85bdcSAkhil Goyal 	},
2910c3e85bdcSAkhil Goyal 	.probe = cryptodev_dpaa_sec_probe,
2911c3e85bdcSAkhil Goyal 	.remove = cryptodev_dpaa_sec_remove,
2912c3e85bdcSAkhil Goyal };
2913c3e85bdcSAkhil Goyal 
2914c3e85bdcSAkhil Goyal static struct cryptodev_driver dpaa_sec_crypto_drv;
2915c3e85bdcSAkhil Goyal 
2916c3e85bdcSAkhil Goyal RTE_PMD_REGISTER_DPAA(CRYPTODEV_NAME_DPAA_SEC_PMD, rte_dpaa_sec_driver);
2917f737f5ceSFiona Trahe RTE_PMD_REGISTER_CRYPTO_DRIVER(dpaa_sec_crypto_drv, rte_dpaa_sec_driver.driver,
2918c3e85bdcSAkhil Goyal 		cryptodev_driver_id);
2919f163231eSHemant Agrawal 
2920f8e99896SThomas Monjalon RTE_INIT(dpaa_sec_init_log)
2921f163231eSHemant Agrawal {
2922f163231eSHemant Agrawal 	dpaa_logtype_sec = rte_log_register("pmd.crypto.dpaa");
2923f163231eSHemant Agrawal 	if (dpaa_logtype_sec >= 0)
2924f163231eSHemant Agrawal 		rte_log_set_level(dpaa_logtype_sec, RTE_LOG_NOTICE);
2925f163231eSHemant Agrawal }
2926