xref: /dpdk/drivers/crypto/dpaa_sec/dpaa_sec.c (revision a1173d55598c5dec9f1d305db9df743e784c9c55)
1d81734caSHemant Agrawal /* SPDX-License-Identifier: BSD-3-Clause
2c3e85bdcSAkhil Goyal  *
3c3e85bdcSAkhil Goyal  *   Copyright (c) 2016 Freescale Semiconductor, Inc. All rights reserved.
4f163231eSHemant Agrawal  *   Copyright 2017-2018 NXP
5c3e85bdcSAkhil Goyal  *
6c3e85bdcSAkhil Goyal  */
7c3e85bdcSAkhil Goyal 
8c3e85bdcSAkhil Goyal #include <fcntl.h>
9c3e85bdcSAkhil Goyal #include <unistd.h>
10c3e85bdcSAkhil Goyal #include <sched.h>
11c3e85bdcSAkhil Goyal #include <net/if.h>
12c3e85bdcSAkhil Goyal 
13c3e85bdcSAkhil Goyal #include <rte_byteorder.h>
14c3e85bdcSAkhil Goyal #include <rte_common.h>
15c3e85bdcSAkhil Goyal #include <rte_cryptodev_pmd.h>
16c3e85bdcSAkhil Goyal #include <rte_crypto.h>
17c3e85bdcSAkhil Goyal #include <rte_cryptodev.h>
181f14d500SAkhil Goyal #include <rte_security_driver.h>
19c3e85bdcSAkhil Goyal #include <rte_cycles.h>
20c3e85bdcSAkhil Goyal #include <rte_dev.h>
21c3e85bdcSAkhil Goyal #include <rte_kvargs.h>
22c3e85bdcSAkhil Goyal #include <rte_malloc.h>
23c3e85bdcSAkhil Goyal #include <rte_mbuf.h>
24c3e85bdcSAkhil Goyal #include <rte_memcpy.h>
25c3e85bdcSAkhil Goyal #include <rte_string_fns.h>
263b617ee7SAkhil Goyal #include <rte_spinlock.h>
27c3e85bdcSAkhil Goyal 
28c3e85bdcSAkhil Goyal #include <fsl_usd.h>
29c3e85bdcSAkhil Goyal #include <fsl_qman.h>
30c3e85bdcSAkhil Goyal #include <of.h>
31c3e85bdcSAkhil Goyal 
32c3e85bdcSAkhil Goyal /* RTA header files */
33c3e85bdcSAkhil Goyal #include <hw/desc/common.h>
34c3e85bdcSAkhil Goyal #include <hw/desc/algo.h>
35c3e85bdcSAkhil Goyal #include <hw/desc/ipsec.h>
36*a1173d55SHemant 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>
41c3e85bdcSAkhil Goyal 
42c3e85bdcSAkhil Goyal enum rta_sec_era rta_sec_era;
43c3e85bdcSAkhil Goyal 
44f163231eSHemant Agrawal int dpaa_logtype_sec;
45f163231eSHemant Agrawal 
46c3e85bdcSAkhil Goyal static uint8_t cryptodev_driver_id;
47c3e85bdcSAkhil Goyal 
48c3e85bdcSAkhil Goyal static __thread struct rte_crypto_op **dpaa_sec_ops;
49c3e85bdcSAkhil Goyal static __thread int dpaa_sec_op_nb;
50c3e85bdcSAkhil Goyal 
51e79416d1SHemant Agrawal static int
52e79416d1SHemant Agrawal dpaa_sec_attach_sess_q(struct dpaa_sec_qp *qp, dpaa_sec_session *sess);
53e79416d1SHemant Agrawal 
54c3e85bdcSAkhil Goyal static inline void
55c3e85bdcSAkhil Goyal dpaa_sec_op_ending(struct dpaa_sec_op_ctx *ctx)
56c3e85bdcSAkhil Goyal {
57c3e85bdcSAkhil Goyal 	if (!ctx->fd_status) {
58c3e85bdcSAkhil Goyal 		ctx->op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
59c3e85bdcSAkhil Goyal 	} else {
60f163231eSHemant Agrawal 		DPAA_SEC_DP_WARN("SEC return err: 0x%x", ctx->fd_status);
61c3e85bdcSAkhil Goyal 		ctx->op->status = RTE_CRYPTO_OP_STATUS_ERROR;
62c3e85bdcSAkhil Goyal 	}
63c3e85bdcSAkhil Goyal 
64c3e85bdcSAkhil Goyal 	/* report op status to sym->op and then free the ctx memeory  */
65c3e85bdcSAkhil Goyal 	rte_mempool_put(ctx->ctx_pool, (void *)ctx);
66c3e85bdcSAkhil Goyal }
67c3e85bdcSAkhil Goyal 
68c3e85bdcSAkhil Goyal static inline struct dpaa_sec_op_ctx *
69c3e85bdcSAkhil Goyal dpaa_sec_alloc_ctx(dpaa_sec_session *ses)
70c3e85bdcSAkhil Goyal {
71c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
72c3e85bdcSAkhil Goyal 	int retval;
73c3e85bdcSAkhil Goyal 
74c3e85bdcSAkhil Goyal 	retval = rte_mempool_get(ses->ctx_pool, (void **)(&ctx));
75c3e85bdcSAkhil Goyal 	if (!ctx || retval) {
76f163231eSHemant Agrawal 		DPAA_SEC_DP_WARN("Alloc sec descriptor failed!");
77c3e85bdcSAkhil Goyal 		return NULL;
78c3e85bdcSAkhil Goyal 	}
79c3e85bdcSAkhil Goyal 	/*
80c3e85bdcSAkhil Goyal 	 * Clear SG memory. There are 16 SG entries of 16 Bytes each.
81c3e85bdcSAkhil Goyal 	 * one call to dcbz_64() clear 64 bytes, hence calling it 4 times
82c3e85bdcSAkhil Goyal 	 * to clear all the SG entries. dpaa_sec_alloc_ctx() is called for
83c3e85bdcSAkhil Goyal 	 * each packet, memset is costlier than dcbz_64().
84c3e85bdcSAkhil Goyal 	 */
85c3e85bdcSAkhil Goyal 	dcbz_64(&ctx->job.sg[SG_CACHELINE_0]);
86c3e85bdcSAkhil Goyal 	dcbz_64(&ctx->job.sg[SG_CACHELINE_1]);
87c3e85bdcSAkhil Goyal 	dcbz_64(&ctx->job.sg[SG_CACHELINE_2]);
88c3e85bdcSAkhil Goyal 	dcbz_64(&ctx->job.sg[SG_CACHELINE_3]);
89c3e85bdcSAkhil Goyal 
90c3e85bdcSAkhil Goyal 	ctx->ctx_pool = ses->ctx_pool;
910e5607e4SHemant Agrawal 	ctx->vtop_offset = (size_t) ctx
92fcf67029SHemant Agrawal 				- rte_mempool_virt2iova(ctx);
93c3e85bdcSAkhil Goyal 
94c3e85bdcSAkhil Goyal 	return ctx;
95c3e85bdcSAkhil Goyal }
96c3e85bdcSAkhil Goyal 
97c4509373SSantosh Shukla static inline rte_iova_t
98c3e85bdcSAkhil Goyal dpaa_mem_vtop(void *vaddr)
99c3e85bdcSAkhil Goyal {
10029f3c9e5SAnatoly Burakov 	const struct rte_memseg *ms;
101c3e85bdcSAkhil Goyal 
10266cc45e2SAnatoly Burakov 	ms = rte_mem_virt2memseg(vaddr, NULL);
10329f3c9e5SAnatoly Burakov 	if (ms)
10429f3c9e5SAnatoly Burakov 		return ms->iova + RTE_PTR_DIFF(vaddr, ms->addr);
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;
1911f14d500SAkhil Goyal 
1921f14d500SAkhil Goyal 		sg_out = &job->sg[0];
1931f14d500SAkhil Goyal 		hw_sg_to_cpu(sg_out);
1941f14d500SAkhil Goyal 		len = sg_out->length;
1951f14d500SAkhil Goyal 		ctx->op->sym->m_src->pkt_len = len;
1961f14d500SAkhil Goyal 		ctx->op->sym->m_src->data_len = len;
1971f14d500SAkhil Goyal 	}
198c3e85bdcSAkhil Goyal 	dpaa_sec_ops[dpaa_sec_op_nb++] = ctx->op;
199c3e85bdcSAkhil Goyal 	dpaa_sec_op_ending(ctx);
200c3e85bdcSAkhil Goyal 
201c3e85bdcSAkhil Goyal 	return qman_cb_dqrr_consume;
202c3e85bdcSAkhil Goyal }
203c3e85bdcSAkhil Goyal 
204c3e85bdcSAkhil Goyal /* caam result is put into this queue */
205c3e85bdcSAkhil Goyal static int
206c3e85bdcSAkhil Goyal dpaa_sec_init_tx(struct qman_fq *fq)
207c3e85bdcSAkhil Goyal {
208c3e85bdcSAkhil Goyal 	int ret;
209c3e85bdcSAkhil Goyal 	struct qm_mcc_initfq opts;
210c3e85bdcSAkhil Goyal 	uint32_t flags;
211c3e85bdcSAkhil Goyal 
212c3e85bdcSAkhil Goyal 	flags = QMAN_FQ_FLAG_NO_ENQUEUE | QMAN_FQ_FLAG_LOCKED |
213c3e85bdcSAkhil Goyal 		QMAN_FQ_FLAG_DYNAMIC_FQID;
214c3e85bdcSAkhil Goyal 
215c3e85bdcSAkhil Goyal 	ret = qman_create_fq(0, flags, fq);
216c3e85bdcSAkhil Goyal 	if (unlikely(ret)) {
217f163231eSHemant Agrawal 		DPAA_SEC_ERR("qman_create_fq failed");
218c3e85bdcSAkhil Goyal 		return ret;
219c3e85bdcSAkhil Goyal 	}
220c3e85bdcSAkhil Goyal 
221c3e85bdcSAkhil Goyal 	memset(&opts, 0, sizeof(opts));
222c3e85bdcSAkhil Goyal 	opts.we_mask = QM_INITFQ_WE_DESTWQ | QM_INITFQ_WE_FQCTRL |
223c3e85bdcSAkhil Goyal 		       QM_INITFQ_WE_CONTEXTA | QM_INITFQ_WE_CONTEXTB;
224c3e85bdcSAkhil Goyal 
225c3e85bdcSAkhil Goyal 	/* opts.fqd.dest.channel = dpaa_sec_pool_chan; */
226c3e85bdcSAkhil Goyal 
227c3e85bdcSAkhil Goyal 	fq->cb.dqrr = dqrr_out_fq_cb_rx;
228c3e85bdcSAkhil Goyal 	fq->cb.ern  = ern_sec_fq_handler;
229c3e85bdcSAkhil Goyal 
230c3e85bdcSAkhil Goyal 	ret = qman_init_fq(fq, 0, &opts);
231c3e85bdcSAkhil Goyal 	if (unlikely(ret)) {
232f163231eSHemant Agrawal 		DPAA_SEC_ERR("unable to init caam source fq!");
233c3e85bdcSAkhil Goyal 		return ret;
234c3e85bdcSAkhil Goyal 	}
235c3e85bdcSAkhil Goyal 
236c3e85bdcSAkhil Goyal 	return ret;
237c3e85bdcSAkhil Goyal }
238c3e85bdcSAkhil Goyal 
239c3e85bdcSAkhil Goyal static inline int is_cipher_only(dpaa_sec_session *ses)
240c3e85bdcSAkhil Goyal {
241c3e85bdcSAkhil Goyal 	return ((ses->cipher_alg != RTE_CRYPTO_CIPHER_NULL) &&
242c3e85bdcSAkhil Goyal 		(ses->auth_alg == RTE_CRYPTO_AUTH_NULL));
243c3e85bdcSAkhil Goyal }
244c3e85bdcSAkhil Goyal 
245c3e85bdcSAkhil Goyal static inline int is_auth_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_aead(dpaa_sec_session *ses)
252c3e85bdcSAkhil Goyal {
253c3e85bdcSAkhil Goyal 	return ((ses->cipher_alg == 0) &&
254c3e85bdcSAkhil Goyal 		(ses->auth_alg == 0) &&
255c3e85bdcSAkhil Goyal 		(ses->aead_alg != 0));
256c3e85bdcSAkhil Goyal }
257c3e85bdcSAkhil Goyal 
258c3e85bdcSAkhil Goyal static inline int is_auth_cipher(dpaa_sec_session *ses)
259c3e85bdcSAkhil Goyal {
260c3e85bdcSAkhil Goyal 	return ((ses->cipher_alg != RTE_CRYPTO_CIPHER_NULL) &&
2611f14d500SAkhil Goyal 		(ses->auth_alg != RTE_CRYPTO_AUTH_NULL) &&
2621f14d500SAkhil Goyal 		(ses->proto_alg != RTE_SECURITY_PROTOCOL_IPSEC));
2631f14d500SAkhil Goyal }
2641f14d500SAkhil Goyal 
2651f14d500SAkhil Goyal static inline int is_proto_ipsec(dpaa_sec_session *ses)
2661f14d500SAkhil Goyal {
2671f14d500SAkhil Goyal 	return (ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC);
268c3e85bdcSAkhil Goyal }
269c3e85bdcSAkhil Goyal 
270*a1173d55SHemant Agrawal static inline int is_proto_pdcp(dpaa_sec_session *ses)
271*a1173d55SHemant Agrawal {
272*a1173d55SHemant Agrawal 	return (ses->proto_alg == RTE_SECURITY_PROTOCOL_PDCP);
273*a1173d55SHemant Agrawal }
274*a1173d55SHemant Agrawal 
275c3e85bdcSAkhil Goyal static inline int is_encode(dpaa_sec_session *ses)
276c3e85bdcSAkhil Goyal {
277c3e85bdcSAkhil Goyal 	return ses->dir == DIR_ENC;
278c3e85bdcSAkhil Goyal }
279c3e85bdcSAkhil Goyal 
280c3e85bdcSAkhil Goyal static inline int is_decode(dpaa_sec_session *ses)
281c3e85bdcSAkhil Goyal {
282c3e85bdcSAkhil Goyal 	return ses->dir == DIR_DEC;
283c3e85bdcSAkhil Goyal }
284c3e85bdcSAkhil Goyal 
285c3e85bdcSAkhil Goyal static inline void
286c3e85bdcSAkhil Goyal caam_auth_alg(dpaa_sec_session *ses, struct alginfo *alginfo_a)
287c3e85bdcSAkhil Goyal {
288c3e85bdcSAkhil Goyal 	switch (ses->auth_alg) {
289c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AUTH_NULL:
29005b12700SHemant Agrawal 		alginfo_a->algtype =
29105b12700SHemant Agrawal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
29205b12700SHemant Agrawal 			OP_PCL_IPSEC_HMAC_NULL : 0;
293c3e85bdcSAkhil Goyal 		ses->digest_length = 0;
294c3e85bdcSAkhil Goyal 		break;
295c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AUTH_MD5_HMAC:
2961f14d500SAkhil Goyal 		alginfo_a->algtype =
2971f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
2981f14d500SAkhil Goyal 			OP_PCL_IPSEC_HMAC_MD5_96 : OP_ALG_ALGSEL_MD5;
299c3e85bdcSAkhil Goyal 		alginfo_a->algmode = OP_ALG_AAI_HMAC;
300c3e85bdcSAkhil Goyal 		break;
301c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA1_HMAC:
3021f14d500SAkhil Goyal 		alginfo_a->algtype =
3031f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3041f14d500SAkhil Goyal 			OP_PCL_IPSEC_HMAC_SHA1_96 : OP_ALG_ALGSEL_SHA1;
305c3e85bdcSAkhil Goyal 		alginfo_a->algmode = OP_ALG_AAI_HMAC;
306c3e85bdcSAkhil Goyal 		break;
307c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA224_HMAC:
3081f14d500SAkhil Goyal 		alginfo_a->algtype =
3091f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3101f14d500SAkhil Goyal 			OP_PCL_IPSEC_HMAC_SHA1_160 : OP_ALG_ALGSEL_SHA224;
311c3e85bdcSAkhil Goyal 		alginfo_a->algmode = OP_ALG_AAI_HMAC;
312c3e85bdcSAkhil Goyal 		break;
313c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA256_HMAC:
3141f14d500SAkhil Goyal 		alginfo_a->algtype =
3151f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3161f14d500SAkhil Goyal 			OP_PCL_IPSEC_HMAC_SHA2_256_128 : OP_ALG_ALGSEL_SHA256;
317c3e85bdcSAkhil Goyal 		alginfo_a->algmode = OP_ALG_AAI_HMAC;
318c3e85bdcSAkhil Goyal 		break;
319c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA384_HMAC:
3201f14d500SAkhil Goyal 		alginfo_a->algtype =
3211f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3221f14d500SAkhil Goyal 			OP_PCL_IPSEC_HMAC_SHA2_384_192 : OP_ALG_ALGSEL_SHA384;
323c3e85bdcSAkhil Goyal 		alginfo_a->algmode = OP_ALG_AAI_HMAC;
324c3e85bdcSAkhil Goyal 		break;
325c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA512_HMAC:
3261f14d500SAkhil Goyal 		alginfo_a->algtype =
3271f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3281f14d500SAkhil Goyal 			OP_PCL_IPSEC_HMAC_SHA2_512_256 : OP_ALG_ALGSEL_SHA512;
329c3e85bdcSAkhil Goyal 		alginfo_a->algmode = OP_ALG_AAI_HMAC;
330c3e85bdcSAkhil Goyal 		break;
331c3e85bdcSAkhil Goyal 	default:
332f163231eSHemant Agrawal 		DPAA_SEC_ERR("unsupported auth alg %u", ses->auth_alg);
333c3e85bdcSAkhil Goyal 	}
334c3e85bdcSAkhil Goyal }
335c3e85bdcSAkhil Goyal 
336c3e85bdcSAkhil Goyal static inline void
337c3e85bdcSAkhil Goyal caam_cipher_alg(dpaa_sec_session *ses, struct alginfo *alginfo_c)
338c3e85bdcSAkhil Goyal {
339c3e85bdcSAkhil Goyal 	switch (ses->cipher_alg) {
340c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_CIPHER_NULL:
34105b12700SHemant Agrawal 		alginfo_c->algtype =
34205b12700SHemant Agrawal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
34305b12700SHemant Agrawal 			OP_PCL_IPSEC_NULL : 0;
344c3e85bdcSAkhil Goyal 		break;
345c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_CIPHER_AES_CBC:
3461f14d500SAkhil Goyal 		alginfo_c->algtype =
3471f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3481f14d500SAkhil Goyal 			OP_PCL_IPSEC_AES_CBC : OP_ALG_ALGSEL_AES;
349c3e85bdcSAkhil Goyal 		alginfo_c->algmode = OP_ALG_AAI_CBC;
350c3e85bdcSAkhil Goyal 		break;
351c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_CIPHER_3DES_CBC:
3521f14d500SAkhil Goyal 		alginfo_c->algtype =
3531f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3541f14d500SAkhil Goyal 			OP_PCL_IPSEC_3DES : OP_ALG_ALGSEL_3DES;
355c3e85bdcSAkhil Goyal 		alginfo_c->algmode = OP_ALG_AAI_CBC;
356c3e85bdcSAkhil Goyal 		break;
357c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_CIPHER_AES_CTR:
3581f14d500SAkhil Goyal 		alginfo_c->algtype =
3591f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3601f14d500SAkhil Goyal 			OP_PCL_IPSEC_AES_CTR : OP_ALG_ALGSEL_AES;
361c3e85bdcSAkhil Goyal 		alginfo_c->algmode = OP_ALG_AAI_CTR;
362c3e85bdcSAkhil Goyal 		break;
363c3e85bdcSAkhil Goyal 	default:
364f163231eSHemant Agrawal 		DPAA_SEC_ERR("unsupported cipher alg %d", ses->cipher_alg);
365c3e85bdcSAkhil Goyal 	}
366c3e85bdcSAkhil Goyal }
367c3e85bdcSAkhil Goyal 
368c3e85bdcSAkhil Goyal static inline void
369c3e85bdcSAkhil Goyal caam_aead_alg(dpaa_sec_session *ses, struct alginfo *alginfo)
370c3e85bdcSAkhil Goyal {
371c3e85bdcSAkhil Goyal 	switch (ses->aead_alg) {
372c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AEAD_AES_GCM:
373c3e85bdcSAkhil Goyal 		alginfo->algtype = OP_ALG_ALGSEL_AES;
374c3e85bdcSAkhil Goyal 		alginfo->algmode = OP_ALG_AAI_GCM;
375c3e85bdcSAkhil Goyal 		break;
376c3e85bdcSAkhil Goyal 	default:
377f163231eSHemant Agrawal 		DPAA_SEC_ERR("unsupported AEAD alg %d", ses->aead_alg);
378c3e85bdcSAkhil Goyal 	}
379c3e85bdcSAkhil Goyal }
380c3e85bdcSAkhil Goyal 
381*a1173d55SHemant Agrawal static int
382*a1173d55SHemant Agrawal dpaa_sec_prep_pdcp_cdb(dpaa_sec_session *ses)
383*a1173d55SHemant Agrawal {
384*a1173d55SHemant Agrawal 	struct alginfo authdata = {0}, cipherdata = {0};
385*a1173d55SHemant Agrawal 	struct sec_cdb *cdb = &ses->cdb;
386*a1173d55SHemant Agrawal 	int32_t shared_desc_len = 0;
387*a1173d55SHemant Agrawal 	int err;
388*a1173d55SHemant Agrawal #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
389*a1173d55SHemant Agrawal 	int swap = false;
390*a1173d55SHemant Agrawal #else
391*a1173d55SHemant Agrawal 	int swap = true;
392*a1173d55SHemant Agrawal #endif
393*a1173d55SHemant Agrawal 
394*a1173d55SHemant Agrawal 	switch (ses->cipher_alg) {
395*a1173d55SHemant Agrawal 	case RTE_CRYPTO_CIPHER_SNOW3G_UEA2:
396*a1173d55SHemant Agrawal 		cipherdata.algtype = PDCP_CIPHER_TYPE_SNOW;
397*a1173d55SHemant Agrawal 		break;
398*a1173d55SHemant Agrawal 	case RTE_CRYPTO_CIPHER_ZUC_EEA3:
399*a1173d55SHemant Agrawal 		cipherdata.algtype = PDCP_CIPHER_TYPE_ZUC;
400*a1173d55SHemant Agrawal 		break;
401*a1173d55SHemant Agrawal 	case RTE_CRYPTO_CIPHER_AES_CTR:
402*a1173d55SHemant Agrawal 		cipherdata.algtype = PDCP_CIPHER_TYPE_AES;
403*a1173d55SHemant Agrawal 		break;
404*a1173d55SHemant Agrawal 	case RTE_CRYPTO_CIPHER_NULL:
405*a1173d55SHemant Agrawal 		cipherdata.algtype = PDCP_CIPHER_TYPE_NULL;
406*a1173d55SHemant Agrawal 		break;
407*a1173d55SHemant Agrawal 	default:
408*a1173d55SHemant Agrawal 		DPAA_SEC_ERR("Crypto: Undefined Cipher specified %u",
409*a1173d55SHemant Agrawal 			      ses->cipher_alg);
410*a1173d55SHemant Agrawal 		return -1;
411*a1173d55SHemant Agrawal 	}
412*a1173d55SHemant Agrawal 
413*a1173d55SHemant Agrawal 	cipherdata.key = (size_t)ses->cipher_key.data;
414*a1173d55SHemant Agrawal 	cipherdata.keylen = ses->cipher_key.length;
415*a1173d55SHemant Agrawal 	cipherdata.key_enc_flags = 0;
416*a1173d55SHemant Agrawal 	cipherdata.key_type = RTA_DATA_IMM;
417*a1173d55SHemant Agrawal 
418*a1173d55SHemant Agrawal 	if (ses->pdcp.domain == RTE_SECURITY_PDCP_MODE_CONTROL) {
419*a1173d55SHemant Agrawal 		switch (ses->auth_alg) {
420*a1173d55SHemant Agrawal 		case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
421*a1173d55SHemant Agrawal 			authdata.algtype = PDCP_AUTH_TYPE_SNOW;
422*a1173d55SHemant Agrawal 			break;
423*a1173d55SHemant Agrawal 		case RTE_CRYPTO_AUTH_ZUC_EIA3:
424*a1173d55SHemant Agrawal 			authdata.algtype = PDCP_AUTH_TYPE_ZUC;
425*a1173d55SHemant Agrawal 			break;
426*a1173d55SHemant Agrawal 		case RTE_CRYPTO_AUTH_AES_CMAC:
427*a1173d55SHemant Agrawal 			authdata.algtype = PDCP_AUTH_TYPE_AES;
428*a1173d55SHemant Agrawal 			break;
429*a1173d55SHemant Agrawal 		case RTE_CRYPTO_AUTH_NULL:
430*a1173d55SHemant Agrawal 			authdata.algtype = PDCP_AUTH_TYPE_NULL;
431*a1173d55SHemant Agrawal 			break;
432*a1173d55SHemant Agrawal 		default:
433*a1173d55SHemant Agrawal 			DPAA_SEC_ERR("Crypto: Unsupported auth alg %u",
434*a1173d55SHemant Agrawal 				      ses->auth_alg);
435*a1173d55SHemant Agrawal 			return -1;
436*a1173d55SHemant Agrawal 		}
437*a1173d55SHemant Agrawal 
438*a1173d55SHemant Agrawal 		authdata.key = (size_t)ses->auth_key.data;
439*a1173d55SHemant Agrawal 		authdata.keylen = ses->auth_key.length;
440*a1173d55SHemant Agrawal 		authdata.key_enc_flags = 0;
441*a1173d55SHemant Agrawal 		authdata.key_type = RTA_DATA_IMM;
442*a1173d55SHemant Agrawal 
443*a1173d55SHemant Agrawal 		cdb->sh_desc[0] = cipherdata.keylen;
444*a1173d55SHemant Agrawal 		cdb->sh_desc[1] = authdata.keylen;
445*a1173d55SHemant Agrawal 		err = rta_inline_query(IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN,
446*a1173d55SHemant Agrawal 				       MIN_JOB_DESC_SIZE,
447*a1173d55SHemant Agrawal 				       (unsigned int *)cdb->sh_desc,
448*a1173d55SHemant Agrawal 				       &cdb->sh_desc[2], 2);
449*a1173d55SHemant Agrawal 
450*a1173d55SHemant Agrawal 		if (err < 0) {
451*a1173d55SHemant Agrawal 			DPAA_SEC_ERR("Crypto: Incorrect key lengths");
452*a1173d55SHemant Agrawal 			return err;
453*a1173d55SHemant Agrawal 		}
454*a1173d55SHemant Agrawal 		if (!(cdb->sh_desc[2] & 1) && cipherdata.keylen) {
455*a1173d55SHemant Agrawal 			cipherdata.key = (size_t)dpaa_mem_vtop(
456*a1173d55SHemant Agrawal 						(void *)(size_t)cipherdata.key);
457*a1173d55SHemant Agrawal 			cipherdata.key_type = RTA_DATA_PTR;
458*a1173d55SHemant Agrawal 		}
459*a1173d55SHemant Agrawal 		if (!(cdb->sh_desc[2] & (1<<1)) &&  authdata.keylen) {
460*a1173d55SHemant Agrawal 			authdata.key = (size_t)dpaa_mem_vtop(
461*a1173d55SHemant Agrawal 						(void *)(size_t)authdata.key);
462*a1173d55SHemant Agrawal 			authdata.key_type = RTA_DATA_PTR;
463*a1173d55SHemant Agrawal 		}
464*a1173d55SHemant Agrawal 
465*a1173d55SHemant Agrawal 		cdb->sh_desc[0] = 0;
466*a1173d55SHemant Agrawal 		cdb->sh_desc[1] = 0;
467*a1173d55SHemant Agrawal 		cdb->sh_desc[2] = 0;
468*a1173d55SHemant Agrawal 
469*a1173d55SHemant Agrawal 		if (ses->dir == DIR_ENC)
470*a1173d55SHemant Agrawal 			shared_desc_len = cnstr_shdsc_pdcp_c_plane_encap(
471*a1173d55SHemant Agrawal 					cdb->sh_desc, 1, swap,
472*a1173d55SHemant Agrawal 					ses->pdcp.hfn,
473*a1173d55SHemant Agrawal 					ses->pdcp.bearer,
474*a1173d55SHemant Agrawal 					ses->pdcp.pkt_dir,
475*a1173d55SHemant Agrawal 					ses->pdcp.hfn_threshold,
476*a1173d55SHemant Agrawal 					&cipherdata, &authdata,
477*a1173d55SHemant Agrawal 					0);
478*a1173d55SHemant Agrawal 		else if (ses->dir == DIR_DEC)
479*a1173d55SHemant Agrawal 			shared_desc_len = cnstr_shdsc_pdcp_c_plane_decap(
480*a1173d55SHemant Agrawal 					cdb->sh_desc, 1, swap,
481*a1173d55SHemant Agrawal 					ses->pdcp.hfn,
482*a1173d55SHemant Agrawal 					ses->pdcp.bearer,
483*a1173d55SHemant Agrawal 					ses->pdcp.pkt_dir,
484*a1173d55SHemant Agrawal 					ses->pdcp.hfn_threshold,
485*a1173d55SHemant Agrawal 					&cipherdata, &authdata,
486*a1173d55SHemant Agrawal 					0);
487*a1173d55SHemant Agrawal 	} else {
488*a1173d55SHemant Agrawal 		cdb->sh_desc[0] = cipherdata.keylen;
489*a1173d55SHemant Agrawal 		err = rta_inline_query(IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN,
490*a1173d55SHemant Agrawal 				       MIN_JOB_DESC_SIZE,
491*a1173d55SHemant Agrawal 				       (unsigned int *)cdb->sh_desc,
492*a1173d55SHemant Agrawal 				       &cdb->sh_desc[2], 1);
493*a1173d55SHemant Agrawal 
494*a1173d55SHemant Agrawal 		if (err < 0) {
495*a1173d55SHemant Agrawal 			DPAA_SEC_ERR("Crypto: Incorrect key lengths");
496*a1173d55SHemant Agrawal 			return err;
497*a1173d55SHemant Agrawal 		}
498*a1173d55SHemant Agrawal 		if (!(cdb->sh_desc[2] & 1) && cipherdata.keylen) {
499*a1173d55SHemant Agrawal 			cipherdata.key = (size_t)dpaa_mem_vtop(
500*a1173d55SHemant Agrawal 						(void *)(size_t)cipherdata.key);
501*a1173d55SHemant Agrawal 			cipherdata.key_type = RTA_DATA_PTR;
502*a1173d55SHemant Agrawal 		}
503*a1173d55SHemant Agrawal 		cdb->sh_desc[0] = 0;
504*a1173d55SHemant Agrawal 		cdb->sh_desc[1] = 0;
505*a1173d55SHemant Agrawal 		cdb->sh_desc[2] = 0;
506*a1173d55SHemant Agrawal 
507*a1173d55SHemant Agrawal 		if (ses->dir == DIR_ENC)
508*a1173d55SHemant Agrawal 			shared_desc_len = cnstr_shdsc_pdcp_u_plane_encap(
509*a1173d55SHemant Agrawal 					cdb->sh_desc, 1, swap,
510*a1173d55SHemant Agrawal 					ses->pdcp.sn_size,
511*a1173d55SHemant Agrawal 					ses->pdcp.hfn,
512*a1173d55SHemant Agrawal 					ses->pdcp.bearer,
513*a1173d55SHemant Agrawal 					ses->pdcp.pkt_dir,
514*a1173d55SHemant Agrawal 					ses->pdcp.hfn_threshold,
515*a1173d55SHemant Agrawal 					&cipherdata, 0);
516*a1173d55SHemant Agrawal 		else if (ses->dir == DIR_DEC)
517*a1173d55SHemant Agrawal 			shared_desc_len = cnstr_shdsc_pdcp_u_plane_decap(
518*a1173d55SHemant Agrawal 					cdb->sh_desc, 1, swap,
519*a1173d55SHemant Agrawal 					ses->pdcp.sn_size,
520*a1173d55SHemant Agrawal 					ses->pdcp.hfn,
521*a1173d55SHemant Agrawal 					ses->pdcp.bearer,
522*a1173d55SHemant Agrawal 					ses->pdcp.pkt_dir,
523*a1173d55SHemant Agrawal 					ses->pdcp.hfn_threshold,
524*a1173d55SHemant Agrawal 					&cipherdata, 0);
525*a1173d55SHemant Agrawal 	}
526*a1173d55SHemant Agrawal 
527*a1173d55SHemant Agrawal 	return shared_desc_len;
528*a1173d55SHemant Agrawal }
529*a1173d55SHemant Agrawal 
53005b12700SHemant Agrawal /* prepare ipsec proto command block of the session */
53105b12700SHemant Agrawal static int
53205b12700SHemant Agrawal dpaa_sec_prep_ipsec_cdb(dpaa_sec_session *ses)
53305b12700SHemant Agrawal {
53405b12700SHemant Agrawal 	struct alginfo cipherdata = {0}, authdata = {0};
53505b12700SHemant Agrawal 	struct sec_cdb *cdb = &ses->cdb;
53605b12700SHemant Agrawal 	int32_t shared_desc_len = 0;
53705b12700SHemant Agrawal 	int err;
53805b12700SHemant Agrawal #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
53905b12700SHemant Agrawal 	int swap = false;
54005b12700SHemant Agrawal #else
54105b12700SHemant Agrawal 	int swap = true;
54205b12700SHemant Agrawal #endif
54305b12700SHemant Agrawal 
54405b12700SHemant Agrawal 	caam_cipher_alg(ses, &cipherdata);
54505b12700SHemant Agrawal 	if (cipherdata.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
54605b12700SHemant Agrawal 		DPAA_SEC_ERR("not supported cipher alg");
54705b12700SHemant Agrawal 		return -ENOTSUP;
54805b12700SHemant Agrawal 	}
54905b12700SHemant Agrawal 
55005b12700SHemant Agrawal 	cipherdata.key = (size_t)ses->cipher_key.data;
55105b12700SHemant Agrawal 	cipherdata.keylen = ses->cipher_key.length;
55205b12700SHemant Agrawal 	cipherdata.key_enc_flags = 0;
55305b12700SHemant Agrawal 	cipherdata.key_type = RTA_DATA_IMM;
55405b12700SHemant Agrawal 
55505b12700SHemant Agrawal 	caam_auth_alg(ses, &authdata);
55605b12700SHemant Agrawal 	if (authdata.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
55705b12700SHemant Agrawal 		DPAA_SEC_ERR("not supported auth alg");
55805b12700SHemant Agrawal 		return -ENOTSUP;
55905b12700SHemant Agrawal 	}
56005b12700SHemant Agrawal 
56105b12700SHemant Agrawal 	authdata.key = (size_t)ses->auth_key.data;
56205b12700SHemant Agrawal 	authdata.keylen = ses->auth_key.length;
56305b12700SHemant Agrawal 	authdata.key_enc_flags = 0;
56405b12700SHemant Agrawal 	authdata.key_type = RTA_DATA_IMM;
56505b12700SHemant Agrawal 
56605b12700SHemant Agrawal 	cdb->sh_desc[0] = cipherdata.keylen;
56705b12700SHemant Agrawal 	cdb->sh_desc[1] = authdata.keylen;
56805b12700SHemant Agrawal 	err = rta_inline_query(IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN,
56905b12700SHemant Agrawal 			       MIN_JOB_DESC_SIZE,
57005b12700SHemant Agrawal 			       (unsigned int *)cdb->sh_desc,
57105b12700SHemant Agrawal 			       &cdb->sh_desc[2], 2);
57205b12700SHemant Agrawal 
57305b12700SHemant Agrawal 	if (err < 0) {
57405b12700SHemant Agrawal 		DPAA_SEC_ERR("Crypto: Incorrect key lengths");
57505b12700SHemant Agrawal 		return err;
57605b12700SHemant Agrawal 	}
57705b12700SHemant Agrawal 	if (cdb->sh_desc[2] & 1)
57805b12700SHemant Agrawal 		cipherdata.key_type = RTA_DATA_IMM;
57905b12700SHemant Agrawal 	else {
58005b12700SHemant Agrawal 		cipherdata.key = (size_t)dpaa_mem_vtop(
58105b12700SHemant Agrawal 					(void *)(size_t)cipherdata.key);
58205b12700SHemant Agrawal 		cipherdata.key_type = RTA_DATA_PTR;
58305b12700SHemant Agrawal 	}
58405b12700SHemant Agrawal 	if (cdb->sh_desc[2] & (1<<1))
58505b12700SHemant Agrawal 		authdata.key_type = RTA_DATA_IMM;
58605b12700SHemant Agrawal 	else {
58705b12700SHemant Agrawal 		authdata.key = (size_t)dpaa_mem_vtop(
58805b12700SHemant Agrawal 					(void *)(size_t)authdata.key);
58905b12700SHemant Agrawal 		authdata.key_type = RTA_DATA_PTR;
59005b12700SHemant Agrawal 	}
59105b12700SHemant Agrawal 
59205b12700SHemant Agrawal 	cdb->sh_desc[0] = 0;
59305b12700SHemant Agrawal 	cdb->sh_desc[1] = 0;
59405b12700SHemant Agrawal 	cdb->sh_desc[2] = 0;
59505b12700SHemant Agrawal 	if (ses->dir == DIR_ENC) {
59605b12700SHemant Agrawal 		shared_desc_len = cnstr_shdsc_ipsec_new_encap(
59705b12700SHemant Agrawal 				cdb->sh_desc,
59805b12700SHemant Agrawal 				true, swap, SHR_SERIAL,
59905b12700SHemant Agrawal 				&ses->encap_pdb,
60005b12700SHemant Agrawal 				(uint8_t *)&ses->ip4_hdr,
60105b12700SHemant Agrawal 				&cipherdata, &authdata);
60205b12700SHemant Agrawal 	} else if (ses->dir == DIR_DEC) {
60305b12700SHemant Agrawal 		shared_desc_len = cnstr_shdsc_ipsec_new_decap(
60405b12700SHemant Agrawal 				cdb->sh_desc,
60505b12700SHemant Agrawal 				true, swap, SHR_SERIAL,
60605b12700SHemant Agrawal 				&ses->decap_pdb,
60705b12700SHemant Agrawal 				&cipherdata, &authdata);
60805b12700SHemant Agrawal 	}
60905b12700SHemant Agrawal 	return shared_desc_len;
61005b12700SHemant Agrawal }
611c3e85bdcSAkhil Goyal 
612c3e85bdcSAkhil Goyal /* prepare command block of the session */
613c3e85bdcSAkhil Goyal static int
614c3e85bdcSAkhil Goyal dpaa_sec_prep_cdb(dpaa_sec_session *ses)
615c3e85bdcSAkhil Goyal {
616c3e85bdcSAkhil Goyal 	struct alginfo alginfo_c = {0}, alginfo_a = {0}, alginfo = {0};
61722788c2cSSunil Kumar Kori 	int32_t shared_desc_len = 0;
618e79416d1SHemant Agrawal 	struct sec_cdb *cdb = &ses->cdb;
619c3e85bdcSAkhil Goyal 	int err;
620c3e85bdcSAkhil Goyal #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
621c3e85bdcSAkhil Goyal 	int swap = false;
622c3e85bdcSAkhil Goyal #else
623c3e85bdcSAkhil Goyal 	int swap = true;
624c3e85bdcSAkhil Goyal #endif
625c3e85bdcSAkhil Goyal 
626c3e85bdcSAkhil Goyal 	memset(cdb, 0, sizeof(struct sec_cdb));
627c3e85bdcSAkhil Goyal 
62805b12700SHemant Agrawal 	if (is_proto_ipsec(ses)) {
62905b12700SHemant Agrawal 		shared_desc_len = dpaa_sec_prep_ipsec_cdb(ses);
630*a1173d55SHemant Agrawal 	} else if (is_proto_pdcp(ses)) {
631*a1173d55SHemant Agrawal 		shared_desc_len = dpaa_sec_prep_pdcp_cdb(ses);
63205b12700SHemant Agrawal 	} else if (is_cipher_only(ses)) {
633c3e85bdcSAkhil Goyal 		caam_cipher_alg(ses, &alginfo_c);
634c3e85bdcSAkhil Goyal 		if (alginfo_c.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
635f163231eSHemant Agrawal 			DPAA_SEC_ERR("not supported cipher alg");
636c3e85bdcSAkhil Goyal 			return -ENOTSUP;
637c3e85bdcSAkhil Goyal 		}
638c3e85bdcSAkhil Goyal 
6390e5607e4SHemant Agrawal 		alginfo_c.key = (size_t)ses->cipher_key.data;
640c3e85bdcSAkhil Goyal 		alginfo_c.keylen = ses->cipher_key.length;
641c3e85bdcSAkhil Goyal 		alginfo_c.key_enc_flags = 0;
642c3e85bdcSAkhil Goyal 		alginfo_c.key_type = RTA_DATA_IMM;
643c3e85bdcSAkhil Goyal 
644c3e85bdcSAkhil Goyal 		shared_desc_len = cnstr_shdsc_blkcipher(
645c3e85bdcSAkhil Goyal 						cdb->sh_desc, true,
646c3e85bdcSAkhil Goyal 						swap, &alginfo_c,
647c3e85bdcSAkhil Goyal 						NULL,
648c3e85bdcSAkhil Goyal 						ses->iv.length,
649c3e85bdcSAkhil Goyal 						ses->dir);
650c3e85bdcSAkhil Goyal 	} else if (is_auth_only(ses)) {
651c3e85bdcSAkhil Goyal 		caam_auth_alg(ses, &alginfo_a);
652c3e85bdcSAkhil Goyal 		if (alginfo_a.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
653f163231eSHemant Agrawal 			DPAA_SEC_ERR("not supported auth alg");
654c3e85bdcSAkhil Goyal 			return -ENOTSUP;
655c3e85bdcSAkhil Goyal 		}
656c3e85bdcSAkhil Goyal 
6570e5607e4SHemant Agrawal 		alginfo_a.key = (size_t)ses->auth_key.data;
658c3e85bdcSAkhil Goyal 		alginfo_a.keylen = ses->auth_key.length;
659c3e85bdcSAkhil Goyal 		alginfo_a.key_enc_flags = 0;
660c3e85bdcSAkhil Goyal 		alginfo_a.key_type = RTA_DATA_IMM;
661c3e85bdcSAkhil Goyal 
662c3e85bdcSAkhil Goyal 		shared_desc_len = cnstr_shdsc_hmac(cdb->sh_desc, true,
663c3e85bdcSAkhil Goyal 						   swap, &alginfo_a,
664c3e85bdcSAkhil Goyal 						   !ses->dir,
665c3e85bdcSAkhil Goyal 						   ses->digest_length);
666c3e85bdcSAkhil Goyal 	} else if (is_aead(ses)) {
667c3e85bdcSAkhil Goyal 		caam_aead_alg(ses, &alginfo);
668c3e85bdcSAkhil Goyal 		if (alginfo.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
669f163231eSHemant Agrawal 			DPAA_SEC_ERR("not supported aead alg");
670c3e85bdcSAkhil Goyal 			return -ENOTSUP;
671c3e85bdcSAkhil Goyal 		}
6720e5607e4SHemant Agrawal 		alginfo.key = (size_t)ses->aead_key.data;
673c3e85bdcSAkhil Goyal 		alginfo.keylen = ses->aead_key.length;
674c3e85bdcSAkhil Goyal 		alginfo.key_enc_flags = 0;
675c3e85bdcSAkhil Goyal 		alginfo.key_type = RTA_DATA_IMM;
676c3e85bdcSAkhil Goyal 
677c3e85bdcSAkhil Goyal 		if (ses->dir == DIR_ENC)
678c3e85bdcSAkhil Goyal 			shared_desc_len = cnstr_shdsc_gcm_encap(
679c3e85bdcSAkhil Goyal 					cdb->sh_desc, true, swap,
680c3e85bdcSAkhil Goyal 					&alginfo,
681c3e85bdcSAkhil Goyal 					ses->iv.length,
682c3e85bdcSAkhil Goyal 					ses->digest_length);
683c3e85bdcSAkhil Goyal 		else
684c3e85bdcSAkhil Goyal 			shared_desc_len = cnstr_shdsc_gcm_decap(
685c3e85bdcSAkhil Goyal 					cdb->sh_desc, true, swap,
686c3e85bdcSAkhil Goyal 					&alginfo,
687c3e85bdcSAkhil Goyal 					ses->iv.length,
688c3e85bdcSAkhil Goyal 					ses->digest_length);
689c3e85bdcSAkhil Goyal 	} else {
690c3e85bdcSAkhil Goyal 		caam_cipher_alg(ses, &alginfo_c);
691c3e85bdcSAkhil Goyal 		if (alginfo_c.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
692f163231eSHemant Agrawal 			DPAA_SEC_ERR("not supported cipher alg");
693c3e85bdcSAkhil Goyal 			return -ENOTSUP;
694c3e85bdcSAkhil Goyal 		}
695c3e85bdcSAkhil Goyal 
6960e5607e4SHemant Agrawal 		alginfo_c.key = (size_t)ses->cipher_key.data;
697c3e85bdcSAkhil Goyal 		alginfo_c.keylen = ses->cipher_key.length;
698c3e85bdcSAkhil Goyal 		alginfo_c.key_enc_flags = 0;
699c3e85bdcSAkhil Goyal 		alginfo_c.key_type = RTA_DATA_IMM;
700c3e85bdcSAkhil Goyal 
701c3e85bdcSAkhil Goyal 		caam_auth_alg(ses, &alginfo_a);
702c3e85bdcSAkhil Goyal 		if (alginfo_a.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
703f163231eSHemant Agrawal 			DPAA_SEC_ERR("not supported auth alg");
704c3e85bdcSAkhil Goyal 			return -ENOTSUP;
705c3e85bdcSAkhil Goyal 		}
706c3e85bdcSAkhil Goyal 
7070e5607e4SHemant Agrawal 		alginfo_a.key = (size_t)ses->auth_key.data;
708c3e85bdcSAkhil Goyal 		alginfo_a.keylen = ses->auth_key.length;
709c3e85bdcSAkhil Goyal 		alginfo_a.key_enc_flags = 0;
710c3e85bdcSAkhil Goyal 		alginfo_a.key_type = RTA_DATA_IMM;
711c3e85bdcSAkhil Goyal 
712c3e85bdcSAkhil Goyal 		cdb->sh_desc[0] = alginfo_c.keylen;
713c3e85bdcSAkhil Goyal 		cdb->sh_desc[1] = alginfo_a.keylen;
714c3e85bdcSAkhil Goyal 		err = rta_inline_query(IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN,
715c3e85bdcSAkhil Goyal 				       MIN_JOB_DESC_SIZE,
716c3e85bdcSAkhil Goyal 				       (unsigned int *)cdb->sh_desc,
717c3e85bdcSAkhil Goyal 				       &cdb->sh_desc[2], 2);
718c3e85bdcSAkhil Goyal 
719c3e85bdcSAkhil Goyal 		if (err < 0) {
720f163231eSHemant Agrawal 			DPAA_SEC_ERR("Crypto: Incorrect key lengths");
721c3e85bdcSAkhil Goyal 			return err;
722c3e85bdcSAkhil Goyal 		}
723c3e85bdcSAkhil Goyal 		if (cdb->sh_desc[2] & 1)
724c3e85bdcSAkhil Goyal 			alginfo_c.key_type = RTA_DATA_IMM;
725c3e85bdcSAkhil Goyal 		else {
7260e5607e4SHemant Agrawal 			alginfo_c.key = (size_t)dpaa_mem_vtop(
7270e5607e4SHemant Agrawal 						(void *)(size_t)alginfo_c.key);
728c3e85bdcSAkhil Goyal 			alginfo_c.key_type = RTA_DATA_PTR;
729c3e85bdcSAkhil Goyal 		}
730c3e85bdcSAkhil Goyal 		if (cdb->sh_desc[2] & (1<<1))
731c3e85bdcSAkhil Goyal 			alginfo_a.key_type = RTA_DATA_IMM;
732c3e85bdcSAkhil Goyal 		else {
7330e5607e4SHemant Agrawal 			alginfo_a.key = (size_t)dpaa_mem_vtop(
7340e5607e4SHemant Agrawal 						(void *)(size_t)alginfo_a.key);
735c3e85bdcSAkhil Goyal 			alginfo_a.key_type = RTA_DATA_PTR;
736c3e85bdcSAkhil Goyal 		}
737c3e85bdcSAkhil Goyal 		cdb->sh_desc[0] = 0;
738c3e85bdcSAkhil Goyal 		cdb->sh_desc[1] = 0;
739c3e85bdcSAkhil Goyal 		cdb->sh_desc[2] = 0;
7401f14d500SAkhil Goyal 		/* Auth_only_len is set as 0 here and it will be
7411f14d500SAkhil Goyal 		 * overwritten in fd for each packet.
742c3e85bdcSAkhil Goyal 		 */
743c3e85bdcSAkhil Goyal 		shared_desc_len = cnstr_shdsc_authenc(cdb->sh_desc,
744c3e85bdcSAkhil Goyal 				true, swap, &alginfo_c, &alginfo_a,
745c3e85bdcSAkhil Goyal 				ses->iv.length, 0,
746c3e85bdcSAkhil Goyal 				ses->digest_length, ses->dir);
747c3e85bdcSAkhil Goyal 	}
74822788c2cSSunil Kumar Kori 
74922788c2cSSunil Kumar Kori 	if (shared_desc_len < 0) {
750f163231eSHemant Agrawal 		DPAA_SEC_ERR("error in preparing command block");
75122788c2cSSunil Kumar Kori 		return shared_desc_len;
75222788c2cSSunil Kumar Kori 	}
75322788c2cSSunil Kumar Kori 
754c3e85bdcSAkhil Goyal 	cdb->sh_hdr.hi.field.idlen = shared_desc_len;
755c3e85bdcSAkhil Goyal 	cdb->sh_hdr.hi.word = rte_cpu_to_be_32(cdb->sh_hdr.hi.word);
756c3e85bdcSAkhil Goyal 	cdb->sh_hdr.lo.word = rte_cpu_to_be_32(cdb->sh_hdr.lo.word);
757c3e85bdcSAkhil Goyal 
758c3e85bdcSAkhil Goyal 	return 0;
759c3e85bdcSAkhil Goyal }
760c3e85bdcSAkhil Goyal 
761c3e85bdcSAkhil Goyal /* qp is lockless, should be accessed by only one thread */
762c3e85bdcSAkhil Goyal static int
763c3e85bdcSAkhil Goyal dpaa_sec_deq(struct dpaa_sec_qp *qp, struct rte_crypto_op **ops, int nb_ops)
764c3e85bdcSAkhil Goyal {
765c3e85bdcSAkhil Goyal 	struct qman_fq *fq;
7669a984458SAkhil Goyal 	unsigned int pkts = 0;
767f40d5a53SNipun Gupta 	int num_rx_bufs, ret;
7689a984458SAkhil Goyal 	struct qm_dqrr_entry *dq;
769f40d5a53SNipun Gupta 	uint32_t vdqcr_flags = 0;
770c3e85bdcSAkhil Goyal 
771c3e85bdcSAkhil Goyal 	fq = &qp->outq;
772f40d5a53SNipun Gupta 	/*
773f40d5a53SNipun Gupta 	 * Until request for four buffers, we provide exact number of buffers.
774f40d5a53SNipun Gupta 	 * Otherwise we do not set the QM_VDQCR_EXACT flag.
775f40d5a53SNipun Gupta 	 * Not setting QM_VDQCR_EXACT flag can provide two more buffers than
776f40d5a53SNipun Gupta 	 * requested, so we request two less in this case.
777f40d5a53SNipun Gupta 	 */
778f40d5a53SNipun Gupta 	if (nb_ops < 4) {
779f40d5a53SNipun Gupta 		vdqcr_flags = QM_VDQCR_EXACT;
780f40d5a53SNipun Gupta 		num_rx_bufs = nb_ops;
781f40d5a53SNipun Gupta 	} else {
782f40d5a53SNipun Gupta 		num_rx_bufs = nb_ops > DPAA_MAX_DEQUEUE_NUM_FRAMES ?
783f40d5a53SNipun Gupta 			(DPAA_MAX_DEQUEUE_NUM_FRAMES - 2) : (nb_ops - 2);
784f40d5a53SNipun Gupta 	}
785f40d5a53SNipun Gupta 	ret = qman_set_vdq(fq, num_rx_bufs, vdqcr_flags);
7869a984458SAkhil Goyal 	if (ret)
7879a984458SAkhil Goyal 		return 0;
788c3e85bdcSAkhil Goyal 
7899a984458SAkhil Goyal 	do {
7909a984458SAkhil Goyal 		const struct qm_fd *fd;
7919a984458SAkhil Goyal 		struct dpaa_sec_job *job;
7929a984458SAkhil Goyal 		struct dpaa_sec_op_ctx *ctx;
7939a984458SAkhil Goyal 		struct rte_crypto_op *op;
794c3e85bdcSAkhil Goyal 
7959a984458SAkhil Goyal 		dq = qman_dequeue(fq);
7969a984458SAkhil Goyal 		if (!dq)
7979a984458SAkhil Goyal 			continue;
7989a984458SAkhil Goyal 
7999a984458SAkhil Goyal 		fd = &dq->fd;
8009a984458SAkhil Goyal 		/* sg is embedded in an op ctx,
8019a984458SAkhil Goyal 		 * sg[0] is for output
8029a984458SAkhil Goyal 		 * sg[1] for input
8039a984458SAkhil Goyal 		 */
8049a984458SAkhil Goyal 		job = dpaa_mem_ptov(qm_fd_addr_get64(fd));
8059a984458SAkhil Goyal 
8069a984458SAkhil Goyal 		ctx = container_of(job, struct dpaa_sec_op_ctx, job);
8079a984458SAkhil Goyal 		ctx->fd_status = fd->status;
8089a984458SAkhil Goyal 		op = ctx->op;
8099a984458SAkhil Goyal 		if (op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
8109a984458SAkhil Goyal 			struct qm_sg_entry *sg_out;
8119a984458SAkhil Goyal 			uint32_t len;
8129a984458SAkhil Goyal 
8139a984458SAkhil Goyal 			sg_out = &job->sg[0];
8149a984458SAkhil Goyal 			hw_sg_to_cpu(sg_out);
8159a984458SAkhil Goyal 			len = sg_out->length;
8169a984458SAkhil Goyal 			op->sym->m_src->pkt_len = len;
8179a984458SAkhil Goyal 			op->sym->m_src->data_len = len;
8189a984458SAkhil Goyal 		}
8199a984458SAkhil Goyal 		if (!ctx->fd_status) {
8209a984458SAkhil Goyal 			op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
8219a984458SAkhil Goyal 		} else {
822f163231eSHemant Agrawal 			DPAA_SEC_DP_WARN("SEC return err:0x%x", ctx->fd_status);
8239a984458SAkhil Goyal 			op->status = RTE_CRYPTO_OP_STATUS_ERROR;
8249a984458SAkhil Goyal 		}
8259a984458SAkhil Goyal 		ops[pkts++] = op;
8269a984458SAkhil Goyal 
8279a984458SAkhil Goyal 		/* report op status to sym->op and then free the ctx memeory */
8289a984458SAkhil Goyal 		rte_mempool_put(ctx->ctx_pool, (void *)ctx);
8299a984458SAkhil Goyal 
8309a984458SAkhil Goyal 		qman_dqrr_consume(fq, dq);
8319a984458SAkhil Goyal 	} while (fq->flags & QMAN_FQ_STATE_VDQCR);
8329a984458SAkhil Goyal 
8339a984458SAkhil Goyal 	return pkts;
834c3e85bdcSAkhil Goyal }
835c3e85bdcSAkhil Goyal 
836a74af788SAkhil Goyal static inline struct dpaa_sec_job *
837a74af788SAkhil Goyal build_auth_only_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
838a74af788SAkhil Goyal {
839a74af788SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
840a74af788SAkhil Goyal 	struct rte_mbuf *mbuf = sym->m_src;
841a74af788SAkhil Goyal 	struct dpaa_sec_job *cf;
842a74af788SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
843a74af788SAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
844a74af788SAkhil Goyal 	phys_addr_t start_addr;
845a74af788SAkhil Goyal 	uint8_t *old_digest, extra_segs;
846a74af788SAkhil Goyal 
847a74af788SAkhil Goyal 	if (is_decode(ses))
848a74af788SAkhil Goyal 		extra_segs = 3;
849a74af788SAkhil Goyal 	else
850a74af788SAkhil Goyal 		extra_segs = 2;
851a74af788SAkhil Goyal 
852a74af788SAkhil Goyal 	if ((mbuf->nb_segs + extra_segs) > MAX_SG_ENTRIES) {
853f163231eSHemant Agrawal 		DPAA_SEC_DP_ERR("Auth: Max sec segs supported is %d",
854a74af788SAkhil Goyal 				MAX_SG_ENTRIES);
855a74af788SAkhil Goyal 		return NULL;
856a74af788SAkhil Goyal 	}
857a74af788SAkhil Goyal 	ctx = dpaa_sec_alloc_ctx(ses);
858a74af788SAkhil Goyal 	if (!ctx)
859a74af788SAkhil Goyal 		return NULL;
860a74af788SAkhil Goyal 
861a74af788SAkhil Goyal 	cf = &ctx->job;
862a74af788SAkhil Goyal 	ctx->op = op;
863a74af788SAkhil Goyal 	old_digest = ctx->digest;
864a74af788SAkhil Goyal 
865a74af788SAkhil Goyal 	/* output */
866a74af788SAkhil Goyal 	out_sg = &cf->sg[0];
867a74af788SAkhil Goyal 	qm_sg_entry_set64(out_sg, sym->auth.digest.phys_addr);
868a74af788SAkhil Goyal 	out_sg->length = ses->digest_length;
869a74af788SAkhil Goyal 	cpu_to_hw_sg(out_sg);
870a74af788SAkhil Goyal 
871a74af788SAkhil Goyal 	/* input */
872a74af788SAkhil Goyal 	in_sg = &cf->sg[1];
873a74af788SAkhil Goyal 	/* need to extend the input to a compound frame */
874a74af788SAkhil Goyal 	in_sg->extension = 1;
875a74af788SAkhil Goyal 	in_sg->final = 1;
876a74af788SAkhil Goyal 	in_sg->length = sym->auth.data.length;
87795456e89SShreyansh Jain 	qm_sg_entry_set64(in_sg, dpaa_mem_vtop(&cf->sg[2]));
878a74af788SAkhil Goyal 
879a74af788SAkhil Goyal 	/* 1st seg */
880a74af788SAkhil Goyal 	sg = in_sg + 1;
881a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
882a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->auth.data.offset;
883a74af788SAkhil Goyal 	sg->offset = sym->auth.data.offset;
884a74af788SAkhil Goyal 
885a74af788SAkhil Goyal 	/* Successive segs */
886a74af788SAkhil Goyal 	mbuf = mbuf->next;
887a74af788SAkhil Goyal 	while (mbuf) {
888a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
889a74af788SAkhil Goyal 		sg++;
890a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
891a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
892a74af788SAkhil Goyal 		mbuf = mbuf->next;
893a74af788SAkhil Goyal 	}
894a74af788SAkhil Goyal 
895a74af788SAkhil Goyal 	if (is_decode(ses)) {
896a74af788SAkhil Goyal 		/* Digest verification case */
897a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
898a74af788SAkhil Goyal 		sg++;
899a74af788SAkhil Goyal 		rte_memcpy(old_digest, sym->auth.digest.data,
900a74af788SAkhil Goyal 				ses->digest_length);
90195456e89SShreyansh Jain 		start_addr = dpaa_mem_vtop(old_digest);
902a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, start_addr);
903a74af788SAkhil Goyal 		sg->length = ses->digest_length;
904a74af788SAkhil Goyal 		in_sg->length += ses->digest_length;
905a74af788SAkhil Goyal 	} else {
906a74af788SAkhil Goyal 		/* Digest calculation case */
907a74af788SAkhil Goyal 		sg->length -= ses->digest_length;
908a74af788SAkhil Goyal 	}
909a74af788SAkhil Goyal 	sg->final = 1;
910a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
911a74af788SAkhil Goyal 	cpu_to_hw_sg(in_sg);
912a74af788SAkhil Goyal 
913a74af788SAkhil Goyal 	return cf;
914a74af788SAkhil Goyal }
915a74af788SAkhil Goyal 
916c3e85bdcSAkhil Goyal /**
917c3e85bdcSAkhil Goyal  * packet looks like:
918c3e85bdcSAkhil Goyal  *		|<----data_len------->|
919c3e85bdcSAkhil Goyal  *    |ip_header|ah_header|icv|payload|
920c3e85bdcSAkhil Goyal  *              ^
921c3e85bdcSAkhil Goyal  *		|
922c3e85bdcSAkhil Goyal  *	   mbuf->pkt.data
923c3e85bdcSAkhil Goyal  */
924c3e85bdcSAkhil Goyal static inline struct dpaa_sec_job *
925c3e85bdcSAkhil Goyal build_auth_only(struct rte_crypto_op *op, dpaa_sec_session *ses)
926c3e85bdcSAkhil Goyal {
927c3e85bdcSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
928c3e85bdcSAkhil Goyal 	struct rte_mbuf *mbuf = sym->m_src;
929c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
930c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
931c3e85bdcSAkhil Goyal 	struct qm_sg_entry *sg;
932c4509373SSantosh Shukla 	rte_iova_t start_addr;
933c3e85bdcSAkhil Goyal 	uint8_t *old_digest;
934c3e85bdcSAkhil Goyal 
935c3e85bdcSAkhil Goyal 	ctx = dpaa_sec_alloc_ctx(ses);
936c3e85bdcSAkhil Goyal 	if (!ctx)
937c3e85bdcSAkhil Goyal 		return NULL;
938c3e85bdcSAkhil Goyal 
939c3e85bdcSAkhil Goyal 	cf = &ctx->job;
940c3e85bdcSAkhil Goyal 	ctx->op = op;
941c3e85bdcSAkhil Goyal 	old_digest = ctx->digest;
942c3e85bdcSAkhil Goyal 
943bfa9a8a4SThomas Monjalon 	start_addr = rte_pktmbuf_iova(mbuf);
944c3e85bdcSAkhil Goyal 	/* output */
945c3e85bdcSAkhil Goyal 	sg = &cf->sg[0];
946c3e85bdcSAkhil Goyal 	qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
947c3e85bdcSAkhil Goyal 	sg->length = ses->digest_length;
948c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
949c3e85bdcSAkhil Goyal 
950c3e85bdcSAkhil Goyal 	/* input */
951c3e85bdcSAkhil Goyal 	sg = &cf->sg[1];
952c3e85bdcSAkhil Goyal 	if (is_decode(ses)) {
953c3e85bdcSAkhil Goyal 		/* need to extend the input to a compound frame */
954c3e85bdcSAkhil Goyal 		sg->extension = 1;
95595456e89SShreyansh Jain 		qm_sg_entry_set64(sg, dpaa_mem_vtop(&cf->sg[2]));
956c3e85bdcSAkhil Goyal 		sg->length = sym->auth.data.length + ses->digest_length;
957c3e85bdcSAkhil Goyal 		sg->final = 1;
958c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
959c3e85bdcSAkhil Goyal 
960c3e85bdcSAkhil Goyal 		sg = &cf->sg[2];
961c3e85bdcSAkhil Goyal 		/* hash result or digest, save digest first */
962c3e85bdcSAkhil Goyal 		rte_memcpy(old_digest, sym->auth.digest.data,
963c3e85bdcSAkhil Goyal 			   ses->digest_length);
964c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, start_addr + sym->auth.data.offset);
965c3e85bdcSAkhil Goyal 		sg->length = sym->auth.data.length;
966c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
967c3e85bdcSAkhil Goyal 
968c3e85bdcSAkhil Goyal 		/* let's check digest by hw */
96995456e89SShreyansh Jain 		start_addr = dpaa_mem_vtop(old_digest);
970c3e85bdcSAkhil Goyal 		sg++;
971c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, start_addr);
972c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
973c3e85bdcSAkhil Goyal 		sg->final = 1;
974c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
975c3e85bdcSAkhil Goyal 	} else {
976c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, start_addr + sym->auth.data.offset);
977c3e85bdcSAkhil Goyal 		sg->length = sym->auth.data.length;
978c3e85bdcSAkhil Goyal 		sg->final = 1;
979c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
980c3e85bdcSAkhil Goyal 	}
981c3e85bdcSAkhil Goyal 
982c3e85bdcSAkhil Goyal 	return cf;
983c3e85bdcSAkhil Goyal }
984c3e85bdcSAkhil Goyal 
985c3e85bdcSAkhil Goyal static inline struct dpaa_sec_job *
986a74af788SAkhil Goyal build_cipher_only_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
987a74af788SAkhil Goyal {
988a74af788SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
989a74af788SAkhil Goyal 	struct dpaa_sec_job *cf;
990a74af788SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
991a74af788SAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
992a74af788SAkhil Goyal 	struct rte_mbuf *mbuf;
993a74af788SAkhil Goyal 	uint8_t req_segs;
994a74af788SAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
995a74af788SAkhil Goyal 			ses->iv.offset);
996a74af788SAkhil Goyal 
997a74af788SAkhil Goyal 	if (sym->m_dst) {
998a74af788SAkhil Goyal 		mbuf = sym->m_dst;
999a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 3;
1000a74af788SAkhil Goyal 	} else {
1001a74af788SAkhil Goyal 		mbuf = sym->m_src;
1002a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs * 2 + 3;
1003a74af788SAkhil Goyal 	}
1004a74af788SAkhil Goyal 
1005a74af788SAkhil Goyal 	if (req_segs > MAX_SG_ENTRIES) {
1006f163231eSHemant Agrawal 		DPAA_SEC_DP_ERR("Cipher: Max sec segs supported is %d",
1007a74af788SAkhil Goyal 				MAX_SG_ENTRIES);
1008a74af788SAkhil Goyal 		return NULL;
1009a74af788SAkhil Goyal 	}
1010a74af788SAkhil Goyal 
1011a74af788SAkhil Goyal 	ctx = dpaa_sec_alloc_ctx(ses);
1012a74af788SAkhil Goyal 	if (!ctx)
1013a74af788SAkhil Goyal 		return NULL;
1014a74af788SAkhil Goyal 
1015a74af788SAkhil Goyal 	cf = &ctx->job;
1016a74af788SAkhil Goyal 	ctx->op = op;
1017a74af788SAkhil Goyal 
1018a74af788SAkhil Goyal 	/* output */
1019a74af788SAkhil Goyal 	out_sg = &cf->sg[0];
1020a74af788SAkhil Goyal 	out_sg->extension = 1;
1021a74af788SAkhil Goyal 	out_sg->length = sym->cipher.data.length;
102295456e89SShreyansh Jain 	qm_sg_entry_set64(out_sg, dpaa_mem_vtop(&cf->sg[2]));
1023a74af788SAkhil Goyal 	cpu_to_hw_sg(out_sg);
1024a74af788SAkhil Goyal 
1025a74af788SAkhil Goyal 	/* 1st seg */
1026a74af788SAkhil Goyal 	sg = &cf->sg[2];
1027a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1028a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->cipher.data.offset;
1029a74af788SAkhil Goyal 	sg->offset = sym->cipher.data.offset;
1030a74af788SAkhil Goyal 
1031a74af788SAkhil Goyal 	/* Successive segs */
1032a74af788SAkhil Goyal 	mbuf = mbuf->next;
1033a74af788SAkhil Goyal 	while (mbuf) {
1034a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1035a74af788SAkhil Goyal 		sg++;
1036a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1037a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1038a74af788SAkhil Goyal 		mbuf = mbuf->next;
1039a74af788SAkhil Goyal 	}
1040a74af788SAkhil Goyal 	sg->final = 1;
1041a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1042a74af788SAkhil Goyal 
1043a74af788SAkhil Goyal 	/* input */
1044a74af788SAkhil Goyal 	mbuf = sym->m_src;
1045a74af788SAkhil Goyal 	in_sg = &cf->sg[1];
1046a74af788SAkhil Goyal 	in_sg->extension = 1;
1047a74af788SAkhil Goyal 	in_sg->final = 1;
1048a74af788SAkhil Goyal 	in_sg->length = sym->cipher.data.length + ses->iv.length;
1049a74af788SAkhil Goyal 
1050a74af788SAkhil Goyal 	sg++;
105195456e89SShreyansh Jain 	qm_sg_entry_set64(in_sg, dpaa_mem_vtop(sg));
1052a74af788SAkhil Goyal 	cpu_to_hw_sg(in_sg);
1053a74af788SAkhil Goyal 
1054a74af788SAkhil Goyal 	/* IV */
1055a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1056a74af788SAkhil Goyal 	sg->length = ses->iv.length;
1057a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1058a74af788SAkhil Goyal 
1059a74af788SAkhil Goyal 	/* 1st seg */
1060a74af788SAkhil Goyal 	sg++;
1061a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1062a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->cipher.data.offset;
1063a74af788SAkhil Goyal 	sg->offset = sym->cipher.data.offset;
1064a74af788SAkhil Goyal 
1065a74af788SAkhil Goyal 	/* Successive segs */
1066a74af788SAkhil Goyal 	mbuf = mbuf->next;
1067a74af788SAkhil Goyal 	while (mbuf) {
1068a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1069a74af788SAkhil Goyal 		sg++;
1070a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1071a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1072a74af788SAkhil Goyal 		mbuf = mbuf->next;
1073a74af788SAkhil Goyal 	}
1074a74af788SAkhil Goyal 	sg->final = 1;
1075a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1076a74af788SAkhil Goyal 
1077a74af788SAkhil Goyal 	return cf;
1078a74af788SAkhil Goyal }
1079a74af788SAkhil Goyal 
1080a74af788SAkhil Goyal static inline struct dpaa_sec_job *
1081c3e85bdcSAkhil Goyal build_cipher_only(struct rte_crypto_op *op, dpaa_sec_session *ses)
1082c3e85bdcSAkhil Goyal {
1083c3e85bdcSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1084c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
1085c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1086c3e85bdcSAkhil Goyal 	struct qm_sg_entry *sg;
1087c4509373SSantosh Shukla 	rte_iova_t src_start_addr, dst_start_addr;
1088c3e85bdcSAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1089c3e85bdcSAkhil Goyal 			ses->iv.offset);
1090c3e85bdcSAkhil Goyal 
1091c3e85bdcSAkhil Goyal 	ctx = dpaa_sec_alloc_ctx(ses);
1092c3e85bdcSAkhil Goyal 	if (!ctx)
1093c3e85bdcSAkhil Goyal 		return NULL;
1094c3e85bdcSAkhil Goyal 
1095c3e85bdcSAkhil Goyal 	cf = &ctx->job;
1096c3e85bdcSAkhil Goyal 	ctx->op = op;
1097a389434eSAlok Makhariya 
1098bfa9a8a4SThomas Monjalon 	src_start_addr = rte_pktmbuf_iova(sym->m_src);
1099a389434eSAlok Makhariya 
1100a389434eSAlok Makhariya 	if (sym->m_dst)
1101bfa9a8a4SThomas Monjalon 		dst_start_addr = rte_pktmbuf_iova(sym->m_dst);
1102a389434eSAlok Makhariya 	else
1103a389434eSAlok Makhariya 		dst_start_addr = src_start_addr;
1104c3e85bdcSAkhil Goyal 
1105c3e85bdcSAkhil Goyal 	/* output */
1106c3e85bdcSAkhil Goyal 	sg = &cf->sg[0];
1107a389434eSAlok Makhariya 	qm_sg_entry_set64(sg, dst_start_addr + sym->cipher.data.offset);
1108c3e85bdcSAkhil Goyal 	sg->length = sym->cipher.data.length + ses->iv.length;
1109c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1110c3e85bdcSAkhil Goyal 
1111c3e85bdcSAkhil Goyal 	/* input */
1112c3e85bdcSAkhil Goyal 	sg = &cf->sg[1];
1113c3e85bdcSAkhil Goyal 
1114c3e85bdcSAkhil Goyal 	/* need to extend the input to a compound frame */
1115c3e85bdcSAkhil Goyal 	sg->extension = 1;
1116c3e85bdcSAkhil Goyal 	sg->final = 1;
1117c3e85bdcSAkhil Goyal 	sg->length = sym->cipher.data.length + ses->iv.length;
111895456e89SShreyansh Jain 	qm_sg_entry_set64(sg, dpaa_mem_vtop(&cf->sg[2]));
1119c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1120c3e85bdcSAkhil Goyal 
1121c3e85bdcSAkhil Goyal 	sg = &cf->sg[2];
1122c3e85bdcSAkhil Goyal 	qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1123c3e85bdcSAkhil Goyal 	sg->length = ses->iv.length;
1124c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1125c3e85bdcSAkhil Goyal 
1126c3e85bdcSAkhil Goyal 	sg++;
1127a389434eSAlok Makhariya 	qm_sg_entry_set64(sg, src_start_addr + sym->cipher.data.offset);
1128c3e85bdcSAkhil Goyal 	sg->length = sym->cipher.data.length;
1129c3e85bdcSAkhil Goyal 	sg->final = 1;
1130c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1131c3e85bdcSAkhil Goyal 
1132c3e85bdcSAkhil Goyal 	return cf;
1133c3e85bdcSAkhil Goyal }
1134c3e85bdcSAkhil Goyal 
1135c3e85bdcSAkhil Goyal static inline struct dpaa_sec_job *
1136a74af788SAkhil Goyal build_cipher_auth_gcm_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
1137a74af788SAkhil Goyal {
1138a74af788SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1139a74af788SAkhil Goyal 	struct dpaa_sec_job *cf;
1140a74af788SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1141a74af788SAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
1142a74af788SAkhil Goyal 	struct rte_mbuf *mbuf;
1143a74af788SAkhil Goyal 	uint8_t req_segs;
1144a74af788SAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1145a74af788SAkhil Goyal 			ses->iv.offset);
1146a74af788SAkhil Goyal 
1147a74af788SAkhil Goyal 	if (sym->m_dst) {
1148a74af788SAkhil Goyal 		mbuf = sym->m_dst;
1149a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 4;
1150a74af788SAkhil Goyal 	} else {
1151a74af788SAkhil Goyal 		mbuf = sym->m_src;
1152a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs * 2 + 4;
1153a74af788SAkhil Goyal 	}
1154a74af788SAkhil Goyal 
1155a74af788SAkhil Goyal 	if (ses->auth_only_len)
1156a74af788SAkhil Goyal 		req_segs++;
1157a74af788SAkhil Goyal 
1158a74af788SAkhil Goyal 	if (req_segs > MAX_SG_ENTRIES) {
1159f163231eSHemant Agrawal 		DPAA_SEC_DP_ERR("AEAD: Max sec segs supported is %d",
1160a74af788SAkhil Goyal 				MAX_SG_ENTRIES);
1161a74af788SAkhil Goyal 		return NULL;
1162a74af788SAkhil Goyal 	}
1163a74af788SAkhil Goyal 
1164a74af788SAkhil Goyal 	ctx = dpaa_sec_alloc_ctx(ses);
1165a74af788SAkhil Goyal 	if (!ctx)
1166a74af788SAkhil Goyal 		return NULL;
1167a74af788SAkhil Goyal 
1168a74af788SAkhil Goyal 	cf = &ctx->job;
1169a74af788SAkhil Goyal 	ctx->op = op;
1170a74af788SAkhil Goyal 
1171a74af788SAkhil Goyal 	rte_prefetch0(cf->sg);
1172a74af788SAkhil Goyal 
1173a74af788SAkhil Goyal 	/* output */
1174a74af788SAkhil Goyal 	out_sg = &cf->sg[0];
1175a74af788SAkhil Goyal 	out_sg->extension = 1;
1176a74af788SAkhil Goyal 	if (is_encode(ses))
1177a74af788SAkhil Goyal 		out_sg->length = sym->aead.data.length + ses->auth_only_len
1178a74af788SAkhil Goyal 						+ ses->digest_length;
1179a74af788SAkhil Goyal 	else
1180a74af788SAkhil Goyal 		out_sg->length = sym->aead.data.length + ses->auth_only_len;
1181a74af788SAkhil Goyal 
1182a74af788SAkhil Goyal 	/* output sg entries */
1183a74af788SAkhil Goyal 	sg = &cf->sg[2];
118495456e89SShreyansh Jain 	qm_sg_entry_set64(out_sg, dpaa_mem_vtop(sg));
1185a74af788SAkhil Goyal 	cpu_to_hw_sg(out_sg);
1186a74af788SAkhil Goyal 
1187a74af788SAkhil Goyal 	/* 1st seg */
1188a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1189a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->aead.data.offset +
1190a74af788SAkhil Goyal 					ses->auth_only_len;
1191a74af788SAkhil Goyal 	sg->offset = sym->aead.data.offset - ses->auth_only_len;
1192a74af788SAkhil Goyal 
1193a74af788SAkhil Goyal 	/* Successive segs */
1194a74af788SAkhil Goyal 	mbuf = mbuf->next;
1195a74af788SAkhil Goyal 	while (mbuf) {
1196a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1197a74af788SAkhil Goyal 		sg++;
1198a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1199a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1200a74af788SAkhil Goyal 		mbuf = mbuf->next;
1201a74af788SAkhil Goyal 	}
1202a74af788SAkhil Goyal 	sg->length -= ses->digest_length;
1203a74af788SAkhil Goyal 
1204a74af788SAkhil Goyal 	if (is_encode(ses)) {
1205a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1206a74af788SAkhil Goyal 		/* set auth output */
1207a74af788SAkhil Goyal 		sg++;
1208a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, sym->aead.digest.phys_addr);
1209a74af788SAkhil Goyal 		sg->length = ses->digest_length;
1210a74af788SAkhil Goyal 	}
1211a74af788SAkhil Goyal 	sg->final = 1;
1212a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1213a74af788SAkhil Goyal 
1214a74af788SAkhil Goyal 	/* input */
1215a74af788SAkhil Goyal 	mbuf = sym->m_src;
1216a74af788SAkhil Goyal 	in_sg = &cf->sg[1];
1217a74af788SAkhil Goyal 	in_sg->extension = 1;
1218a74af788SAkhil Goyal 	in_sg->final = 1;
1219a74af788SAkhil Goyal 	if (is_encode(ses))
1220a74af788SAkhil Goyal 		in_sg->length = ses->iv.length + sym->aead.data.length
1221a74af788SAkhil Goyal 							+ ses->auth_only_len;
1222a74af788SAkhil Goyal 	else
1223a74af788SAkhil Goyal 		in_sg->length = ses->iv.length + sym->aead.data.length
1224a74af788SAkhil Goyal 				+ ses->auth_only_len + ses->digest_length;
1225a74af788SAkhil Goyal 
1226a74af788SAkhil Goyal 	/* input sg entries */
1227a74af788SAkhil Goyal 	sg++;
122895456e89SShreyansh Jain 	qm_sg_entry_set64(in_sg, dpaa_mem_vtop(sg));
1229a74af788SAkhil Goyal 	cpu_to_hw_sg(in_sg);
1230a74af788SAkhil Goyal 
1231a74af788SAkhil Goyal 	/* 1st seg IV */
1232a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1233a74af788SAkhil Goyal 	sg->length = ses->iv.length;
1234a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1235a74af788SAkhil Goyal 
1236a74af788SAkhil Goyal 	/* 2nd seg auth only */
1237a74af788SAkhil Goyal 	if (ses->auth_only_len) {
1238a74af788SAkhil Goyal 		sg++;
1239a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, dpaa_mem_vtop(sym->aead.aad.data));
1240a74af788SAkhil Goyal 		sg->length = ses->auth_only_len;
1241a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1242a74af788SAkhil Goyal 	}
1243a74af788SAkhil Goyal 
1244a74af788SAkhil Goyal 	/* 3rd seg */
1245a74af788SAkhil Goyal 	sg++;
1246a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1247a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->aead.data.offset;
1248a74af788SAkhil Goyal 	sg->offset = sym->aead.data.offset;
1249a74af788SAkhil Goyal 
1250a74af788SAkhil Goyal 	/* Successive segs */
1251a74af788SAkhil Goyal 	mbuf = mbuf->next;
1252a74af788SAkhil Goyal 	while (mbuf) {
1253a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1254a74af788SAkhil Goyal 		sg++;
1255a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1256a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1257a74af788SAkhil Goyal 		mbuf = mbuf->next;
1258a74af788SAkhil Goyal 	}
1259a74af788SAkhil Goyal 
1260a74af788SAkhil Goyal 	if (is_decode(ses)) {
1261a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1262a74af788SAkhil Goyal 		sg++;
1263a74af788SAkhil Goyal 		memcpy(ctx->digest, sym->aead.digest.data,
1264a74af788SAkhil Goyal 			ses->digest_length);
126595456e89SShreyansh Jain 		qm_sg_entry_set64(sg, dpaa_mem_vtop(ctx->digest));
1266a74af788SAkhil Goyal 		sg->length = ses->digest_length;
1267a74af788SAkhil Goyal 	}
1268a74af788SAkhil Goyal 	sg->final = 1;
1269a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1270a74af788SAkhil Goyal 
1271a74af788SAkhil Goyal 	return cf;
1272a74af788SAkhil Goyal }
1273a74af788SAkhil Goyal 
1274a74af788SAkhil Goyal static inline struct dpaa_sec_job *
1275c3e85bdcSAkhil Goyal build_cipher_auth_gcm(struct rte_crypto_op *op, dpaa_sec_session *ses)
1276c3e85bdcSAkhil Goyal {
1277c3e85bdcSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1278c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
1279c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1280c3e85bdcSAkhil Goyal 	struct qm_sg_entry *sg;
1281c3e85bdcSAkhil Goyal 	uint32_t length = 0;
1282c4509373SSantosh Shukla 	rte_iova_t src_start_addr, dst_start_addr;
1283c3e85bdcSAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1284c3e85bdcSAkhil Goyal 			ses->iv.offset);
1285c3e85bdcSAkhil Goyal 
1286116ff44aSHemant Agrawal 	src_start_addr = sym->m_src->buf_iova + sym->m_src->data_off;
1287a389434eSAlok Makhariya 
1288a389434eSAlok Makhariya 	if (sym->m_dst)
1289116ff44aSHemant Agrawal 		dst_start_addr = sym->m_dst->buf_iova + sym->m_dst->data_off;
1290a389434eSAlok Makhariya 	else
1291a389434eSAlok Makhariya 		dst_start_addr = src_start_addr;
1292c3e85bdcSAkhil Goyal 
1293c3e85bdcSAkhil Goyal 	ctx = dpaa_sec_alloc_ctx(ses);
1294c3e85bdcSAkhil Goyal 	if (!ctx)
1295c3e85bdcSAkhil Goyal 		return NULL;
1296c3e85bdcSAkhil Goyal 
1297c3e85bdcSAkhil Goyal 	cf = &ctx->job;
1298c3e85bdcSAkhil Goyal 	ctx->op = op;
1299c3e85bdcSAkhil Goyal 
1300c3e85bdcSAkhil Goyal 	/* input */
1301c3e85bdcSAkhil Goyal 	rte_prefetch0(cf->sg);
1302c3e85bdcSAkhil Goyal 	sg = &cf->sg[2];
130395456e89SShreyansh Jain 	qm_sg_entry_set64(&cf->sg[1], dpaa_mem_vtop(sg));
1304c3e85bdcSAkhil Goyal 	if (is_encode(ses)) {
1305c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1306c3e85bdcSAkhil Goyal 		sg->length = ses->iv.length;
1307c3e85bdcSAkhil Goyal 		length += sg->length;
1308c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1309c3e85bdcSAkhil Goyal 
1310c3e85bdcSAkhil Goyal 		sg++;
1311c3e85bdcSAkhil Goyal 		if (ses->auth_only_len) {
1312c3e85bdcSAkhil Goyal 			qm_sg_entry_set64(sg,
1313c3e85bdcSAkhil Goyal 					  dpaa_mem_vtop(sym->aead.aad.data));
1314c3e85bdcSAkhil Goyal 			sg->length = ses->auth_only_len;
1315c3e85bdcSAkhil Goyal 			length += sg->length;
1316c3e85bdcSAkhil Goyal 			cpu_to_hw_sg(sg);
1317c3e85bdcSAkhil Goyal 			sg++;
1318c3e85bdcSAkhil Goyal 		}
1319a389434eSAlok Makhariya 		qm_sg_entry_set64(sg, src_start_addr + sym->aead.data.offset);
1320c3e85bdcSAkhil Goyal 		sg->length = sym->aead.data.length;
1321c3e85bdcSAkhil Goyal 		length += sg->length;
1322c3e85bdcSAkhil Goyal 		sg->final = 1;
1323c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1324c3e85bdcSAkhil Goyal 	} else {
1325c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1326c3e85bdcSAkhil Goyal 		sg->length = ses->iv.length;
1327c3e85bdcSAkhil Goyal 		length += sg->length;
1328c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1329c3e85bdcSAkhil Goyal 
1330c3e85bdcSAkhil Goyal 		sg++;
1331c3e85bdcSAkhil Goyal 		if (ses->auth_only_len) {
1332c3e85bdcSAkhil Goyal 			qm_sg_entry_set64(sg,
1333c3e85bdcSAkhil Goyal 					  dpaa_mem_vtop(sym->aead.aad.data));
1334c3e85bdcSAkhil Goyal 			sg->length = ses->auth_only_len;
1335c3e85bdcSAkhil Goyal 			length += sg->length;
1336c3e85bdcSAkhil Goyal 			cpu_to_hw_sg(sg);
1337c3e85bdcSAkhil Goyal 			sg++;
1338c3e85bdcSAkhil Goyal 		}
1339a389434eSAlok Makhariya 		qm_sg_entry_set64(sg, src_start_addr + sym->aead.data.offset);
1340c3e85bdcSAkhil Goyal 		sg->length = sym->aead.data.length;
1341c3e85bdcSAkhil Goyal 		length += sg->length;
1342c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1343c3e85bdcSAkhil Goyal 
1344c3e85bdcSAkhil Goyal 		memcpy(ctx->digest, sym->aead.digest.data,
1345c3e85bdcSAkhil Goyal 		       ses->digest_length);
1346c3e85bdcSAkhil Goyal 		sg++;
1347c3e85bdcSAkhil Goyal 
134895456e89SShreyansh Jain 		qm_sg_entry_set64(sg, dpaa_mem_vtop(ctx->digest));
1349c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
1350c3e85bdcSAkhil Goyal 		length += sg->length;
1351c3e85bdcSAkhil Goyal 		sg->final = 1;
1352c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1353c3e85bdcSAkhil Goyal 	}
1354c3e85bdcSAkhil Goyal 	/* input compound frame */
1355c3e85bdcSAkhil Goyal 	cf->sg[1].length = length;
1356c3e85bdcSAkhil Goyal 	cf->sg[1].extension = 1;
1357c3e85bdcSAkhil Goyal 	cf->sg[1].final = 1;
1358c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(&cf->sg[1]);
1359c3e85bdcSAkhil Goyal 
1360c3e85bdcSAkhil Goyal 	/* output */
1361c3e85bdcSAkhil Goyal 	sg++;
136295456e89SShreyansh Jain 	qm_sg_entry_set64(&cf->sg[0], dpaa_mem_vtop(sg));
1363c3e85bdcSAkhil Goyal 	qm_sg_entry_set64(sg,
1364a389434eSAlok Makhariya 		dst_start_addr + sym->aead.data.offset - ses->auth_only_len);
1365c3e85bdcSAkhil Goyal 	sg->length = sym->aead.data.length + ses->auth_only_len;
1366c3e85bdcSAkhil Goyal 	length = sg->length;
1367c3e85bdcSAkhil Goyal 	if (is_encode(ses)) {
1368c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1369c3e85bdcSAkhil Goyal 		/* set auth output */
1370c3e85bdcSAkhil Goyal 		sg++;
1371c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, sym->aead.digest.phys_addr);
1372c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
1373c3e85bdcSAkhil Goyal 		length += sg->length;
1374c3e85bdcSAkhil Goyal 	}
1375c3e85bdcSAkhil Goyal 	sg->final = 1;
1376c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1377c3e85bdcSAkhil Goyal 
1378c3e85bdcSAkhil Goyal 	/* output compound frame */
1379c3e85bdcSAkhil Goyal 	cf->sg[0].length = length;
1380c3e85bdcSAkhil Goyal 	cf->sg[0].extension = 1;
1381c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(&cf->sg[0]);
1382c3e85bdcSAkhil Goyal 
1383c3e85bdcSAkhil Goyal 	return cf;
1384c3e85bdcSAkhil Goyal }
1385c3e85bdcSAkhil Goyal 
1386c3e85bdcSAkhil Goyal static inline struct dpaa_sec_job *
1387a74af788SAkhil Goyal build_cipher_auth_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
1388a74af788SAkhil Goyal {
1389a74af788SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1390a74af788SAkhil Goyal 	struct dpaa_sec_job *cf;
1391a74af788SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1392a74af788SAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
1393a74af788SAkhil Goyal 	struct rte_mbuf *mbuf;
1394a74af788SAkhil Goyal 	uint8_t req_segs;
1395a74af788SAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1396a74af788SAkhil Goyal 			ses->iv.offset);
1397a74af788SAkhil Goyal 
1398a74af788SAkhil Goyal 	if (sym->m_dst) {
1399a74af788SAkhil Goyal 		mbuf = sym->m_dst;
1400a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 4;
1401a74af788SAkhil Goyal 	} else {
1402a74af788SAkhil Goyal 		mbuf = sym->m_src;
1403a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs * 2 + 4;
1404a74af788SAkhil Goyal 	}
1405a74af788SAkhil Goyal 
1406a74af788SAkhil Goyal 	if (req_segs > MAX_SG_ENTRIES) {
1407f163231eSHemant Agrawal 		DPAA_SEC_DP_ERR("Cipher-Auth: Max sec segs supported is %d",
1408a74af788SAkhil Goyal 				MAX_SG_ENTRIES);
1409a74af788SAkhil Goyal 		return NULL;
1410a74af788SAkhil Goyal 	}
1411a74af788SAkhil Goyal 
1412a74af788SAkhil Goyal 	ctx = dpaa_sec_alloc_ctx(ses);
1413a74af788SAkhil Goyal 	if (!ctx)
1414a74af788SAkhil Goyal 		return NULL;
1415a74af788SAkhil Goyal 
1416a74af788SAkhil Goyal 	cf = &ctx->job;
1417a74af788SAkhil Goyal 	ctx->op = op;
1418a74af788SAkhil Goyal 
1419a74af788SAkhil Goyal 	rte_prefetch0(cf->sg);
1420a74af788SAkhil Goyal 
1421a74af788SAkhil Goyal 	/* output */
1422a74af788SAkhil Goyal 	out_sg = &cf->sg[0];
1423a74af788SAkhil Goyal 	out_sg->extension = 1;
1424a74af788SAkhil Goyal 	if (is_encode(ses))
1425a74af788SAkhil Goyal 		out_sg->length = sym->auth.data.length + ses->digest_length;
1426a74af788SAkhil Goyal 	else
1427a74af788SAkhil Goyal 		out_sg->length = sym->auth.data.length;
1428a74af788SAkhil Goyal 
1429a74af788SAkhil Goyal 	/* output sg entries */
1430a74af788SAkhil Goyal 	sg = &cf->sg[2];
143195456e89SShreyansh Jain 	qm_sg_entry_set64(out_sg, dpaa_mem_vtop(sg));
1432a74af788SAkhil Goyal 	cpu_to_hw_sg(out_sg);
1433a74af788SAkhil Goyal 
1434a74af788SAkhil Goyal 	/* 1st seg */
1435a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1436a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->auth.data.offset;
1437a74af788SAkhil Goyal 	sg->offset = sym->auth.data.offset;
1438a74af788SAkhil Goyal 
1439a74af788SAkhil Goyal 	/* Successive segs */
1440a74af788SAkhil Goyal 	mbuf = mbuf->next;
1441a74af788SAkhil Goyal 	while (mbuf) {
1442a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1443a74af788SAkhil Goyal 		sg++;
1444a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1445a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1446a74af788SAkhil Goyal 		mbuf = mbuf->next;
1447a74af788SAkhil Goyal 	}
1448a74af788SAkhil Goyal 	sg->length -= ses->digest_length;
1449a74af788SAkhil Goyal 
1450a74af788SAkhil Goyal 	if (is_encode(ses)) {
1451a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1452a74af788SAkhil Goyal 		/* set auth output */
1453a74af788SAkhil Goyal 		sg++;
1454a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
1455a74af788SAkhil Goyal 		sg->length = ses->digest_length;
1456a74af788SAkhil Goyal 	}
1457a74af788SAkhil Goyal 	sg->final = 1;
1458a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1459a74af788SAkhil Goyal 
1460a74af788SAkhil Goyal 	/* input */
1461a74af788SAkhil Goyal 	mbuf = sym->m_src;
1462a74af788SAkhil Goyal 	in_sg = &cf->sg[1];
1463a74af788SAkhil Goyal 	in_sg->extension = 1;
1464a74af788SAkhil Goyal 	in_sg->final = 1;
1465a74af788SAkhil Goyal 	if (is_encode(ses))
1466a74af788SAkhil Goyal 		in_sg->length = ses->iv.length + sym->auth.data.length;
1467a74af788SAkhil Goyal 	else
1468a74af788SAkhil Goyal 		in_sg->length = ses->iv.length + sym->auth.data.length
1469a74af788SAkhil Goyal 						+ ses->digest_length;
1470a74af788SAkhil Goyal 
1471a74af788SAkhil Goyal 	/* input sg entries */
1472a74af788SAkhil Goyal 	sg++;
147395456e89SShreyansh Jain 	qm_sg_entry_set64(in_sg, dpaa_mem_vtop(sg));
1474a74af788SAkhil Goyal 	cpu_to_hw_sg(in_sg);
1475a74af788SAkhil Goyal 
1476a74af788SAkhil Goyal 	/* 1st seg IV */
1477a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1478a74af788SAkhil Goyal 	sg->length = ses->iv.length;
1479a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1480a74af788SAkhil Goyal 
1481a74af788SAkhil Goyal 	/* 2nd seg */
1482a74af788SAkhil Goyal 	sg++;
1483a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1484a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->auth.data.offset;
1485a74af788SAkhil Goyal 	sg->offset = sym->auth.data.offset;
1486a74af788SAkhil Goyal 
1487a74af788SAkhil Goyal 	/* Successive segs */
1488a74af788SAkhil Goyal 	mbuf = mbuf->next;
1489a74af788SAkhil Goyal 	while (mbuf) {
1490a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1491a74af788SAkhil Goyal 		sg++;
1492a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1493a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1494a74af788SAkhil Goyal 		mbuf = mbuf->next;
1495a74af788SAkhil Goyal 	}
1496a74af788SAkhil Goyal 
1497a74af788SAkhil Goyal 	sg->length -= ses->digest_length;
1498a74af788SAkhil Goyal 	if (is_decode(ses)) {
1499a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1500a74af788SAkhil Goyal 		sg++;
1501a74af788SAkhil Goyal 		memcpy(ctx->digest, sym->auth.digest.data,
1502a74af788SAkhil Goyal 			ses->digest_length);
150395456e89SShreyansh Jain 		qm_sg_entry_set64(sg, dpaa_mem_vtop(ctx->digest));
1504a74af788SAkhil Goyal 		sg->length = ses->digest_length;
1505a74af788SAkhil Goyal 	}
1506a74af788SAkhil Goyal 	sg->final = 1;
1507a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1508a74af788SAkhil Goyal 
1509a74af788SAkhil Goyal 	return cf;
1510a74af788SAkhil Goyal }
1511a74af788SAkhil Goyal 
1512a74af788SAkhil Goyal static inline struct dpaa_sec_job *
1513c3e85bdcSAkhil Goyal build_cipher_auth(struct rte_crypto_op *op, dpaa_sec_session *ses)
1514c3e85bdcSAkhil Goyal {
1515c3e85bdcSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1516c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
1517c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1518c3e85bdcSAkhil Goyal 	struct qm_sg_entry *sg;
1519c4509373SSantosh Shukla 	rte_iova_t src_start_addr, dst_start_addr;
1520c3e85bdcSAkhil Goyal 	uint32_t length = 0;
1521c3e85bdcSAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1522c3e85bdcSAkhil Goyal 			ses->iv.offset);
1523c3e85bdcSAkhil Goyal 
1524455da545SSantosh Shukla 	src_start_addr = sym->m_src->buf_iova + sym->m_src->data_off;
1525a389434eSAlok Makhariya 	if (sym->m_dst)
1526455da545SSantosh Shukla 		dst_start_addr = sym->m_dst->buf_iova + sym->m_dst->data_off;
1527a389434eSAlok Makhariya 	else
1528a389434eSAlok Makhariya 		dst_start_addr = src_start_addr;
1529c3e85bdcSAkhil Goyal 
1530c3e85bdcSAkhil Goyal 	ctx = dpaa_sec_alloc_ctx(ses);
1531c3e85bdcSAkhil Goyal 	if (!ctx)
1532c3e85bdcSAkhil Goyal 		return NULL;
1533c3e85bdcSAkhil Goyal 
1534c3e85bdcSAkhil Goyal 	cf = &ctx->job;
1535c3e85bdcSAkhil Goyal 	ctx->op = op;
1536c3e85bdcSAkhil Goyal 
1537c3e85bdcSAkhil Goyal 	/* input */
1538c3e85bdcSAkhil Goyal 	rte_prefetch0(cf->sg);
1539c3e85bdcSAkhil Goyal 	sg = &cf->sg[2];
154095456e89SShreyansh Jain 	qm_sg_entry_set64(&cf->sg[1], dpaa_mem_vtop(sg));
1541c3e85bdcSAkhil Goyal 	if (is_encode(ses)) {
1542c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1543c3e85bdcSAkhil Goyal 		sg->length = ses->iv.length;
1544c3e85bdcSAkhil Goyal 		length += sg->length;
1545c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1546c3e85bdcSAkhil Goyal 
1547c3e85bdcSAkhil Goyal 		sg++;
1548a389434eSAlok Makhariya 		qm_sg_entry_set64(sg, src_start_addr + sym->auth.data.offset);
1549c3e85bdcSAkhil Goyal 		sg->length = sym->auth.data.length;
1550c3e85bdcSAkhil Goyal 		length += sg->length;
1551c3e85bdcSAkhil Goyal 		sg->final = 1;
1552c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1553c3e85bdcSAkhil Goyal 	} else {
1554c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1555c3e85bdcSAkhil Goyal 		sg->length = ses->iv.length;
1556c3e85bdcSAkhil Goyal 		length += sg->length;
1557c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1558c3e85bdcSAkhil Goyal 
1559c3e85bdcSAkhil Goyal 		sg++;
1560c3e85bdcSAkhil Goyal 
1561a389434eSAlok Makhariya 		qm_sg_entry_set64(sg, src_start_addr + sym->auth.data.offset);
1562c3e85bdcSAkhil Goyal 		sg->length = sym->auth.data.length;
1563c3e85bdcSAkhil Goyal 		length += sg->length;
1564c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1565c3e85bdcSAkhil Goyal 
1566c3e85bdcSAkhil Goyal 		memcpy(ctx->digest, sym->auth.digest.data,
1567c3e85bdcSAkhil Goyal 		       ses->digest_length);
1568c3e85bdcSAkhil Goyal 		sg++;
1569c3e85bdcSAkhil Goyal 
157095456e89SShreyansh Jain 		qm_sg_entry_set64(sg, dpaa_mem_vtop(ctx->digest));
1571c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
1572c3e85bdcSAkhil Goyal 		length += sg->length;
1573c3e85bdcSAkhil Goyal 		sg->final = 1;
1574c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1575c3e85bdcSAkhil Goyal 	}
1576c3e85bdcSAkhil Goyal 	/* input compound frame */
1577c3e85bdcSAkhil Goyal 	cf->sg[1].length = length;
1578c3e85bdcSAkhil Goyal 	cf->sg[1].extension = 1;
1579c3e85bdcSAkhil Goyal 	cf->sg[1].final = 1;
1580c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(&cf->sg[1]);
1581c3e85bdcSAkhil Goyal 
1582c3e85bdcSAkhil Goyal 	/* output */
1583c3e85bdcSAkhil Goyal 	sg++;
158495456e89SShreyansh Jain 	qm_sg_entry_set64(&cf->sg[0], dpaa_mem_vtop(sg));
1585a389434eSAlok Makhariya 	qm_sg_entry_set64(sg, dst_start_addr + sym->cipher.data.offset);
1586c3e85bdcSAkhil Goyal 	sg->length = sym->cipher.data.length;
1587c3e85bdcSAkhil Goyal 	length = sg->length;
1588c3e85bdcSAkhil Goyal 	if (is_encode(ses)) {
1589c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1590c3e85bdcSAkhil Goyal 		/* set auth output */
1591c3e85bdcSAkhil Goyal 		sg++;
1592c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
1593c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
1594c3e85bdcSAkhil Goyal 		length += sg->length;
1595c3e85bdcSAkhil Goyal 	}
1596c3e85bdcSAkhil Goyal 	sg->final = 1;
1597c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1598c3e85bdcSAkhil Goyal 
1599c3e85bdcSAkhil Goyal 	/* output compound frame */
1600c3e85bdcSAkhil Goyal 	cf->sg[0].length = length;
1601c3e85bdcSAkhil Goyal 	cf->sg[0].extension = 1;
1602c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(&cf->sg[0]);
1603c3e85bdcSAkhil Goyal 
1604c3e85bdcSAkhil Goyal 	return cf;
1605c3e85bdcSAkhil Goyal }
1606c3e85bdcSAkhil Goyal 
16071f14d500SAkhil Goyal static inline struct dpaa_sec_job *
16081f14d500SAkhil Goyal build_proto(struct rte_crypto_op *op, dpaa_sec_session *ses)
16091f14d500SAkhil Goyal {
16101f14d500SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
16111f14d500SAkhil Goyal 	struct dpaa_sec_job *cf;
16121f14d500SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
16131f14d500SAkhil Goyal 	struct qm_sg_entry *sg;
16141f14d500SAkhil Goyal 	phys_addr_t src_start_addr, dst_start_addr;
16151f14d500SAkhil Goyal 
16161f14d500SAkhil Goyal 	ctx = dpaa_sec_alloc_ctx(ses);
16171f14d500SAkhil Goyal 	if (!ctx)
16181f14d500SAkhil Goyal 		return NULL;
16191f14d500SAkhil Goyal 	cf = &ctx->job;
16201f14d500SAkhil Goyal 	ctx->op = op;
16211f14d500SAkhil Goyal 
16221f14d500SAkhil Goyal 	src_start_addr = rte_pktmbuf_mtophys(sym->m_src);
16231f14d500SAkhil Goyal 
16241f14d500SAkhil Goyal 	if (sym->m_dst)
16251f14d500SAkhil Goyal 		dst_start_addr = rte_pktmbuf_mtophys(sym->m_dst);
16261f14d500SAkhil Goyal 	else
16271f14d500SAkhil Goyal 		dst_start_addr = src_start_addr;
16281f14d500SAkhil Goyal 
16291f14d500SAkhil Goyal 	/* input */
16301f14d500SAkhil Goyal 	sg = &cf->sg[1];
16311f14d500SAkhil Goyal 	qm_sg_entry_set64(sg, src_start_addr);
16321f14d500SAkhil Goyal 	sg->length = sym->m_src->pkt_len;
16331f14d500SAkhil Goyal 	sg->final = 1;
16341f14d500SAkhil Goyal 	cpu_to_hw_sg(sg);
16351f14d500SAkhil Goyal 
16361f14d500SAkhil Goyal 	sym->m_src->packet_type &= ~RTE_PTYPE_L4_MASK;
16371f14d500SAkhil Goyal 	/* output */
16381f14d500SAkhil Goyal 	sg = &cf->sg[0];
16391f14d500SAkhil Goyal 	qm_sg_entry_set64(sg, dst_start_addr);
16401f14d500SAkhil Goyal 	sg->length = sym->m_src->buf_len - sym->m_src->data_off;
16411f14d500SAkhil Goyal 	cpu_to_hw_sg(sg);
16421f14d500SAkhil Goyal 
16431f14d500SAkhil Goyal 	return cf;
16441f14d500SAkhil Goyal }
16451f14d500SAkhil Goyal 
16469a984458SAkhil Goyal static uint16_t
16479a984458SAkhil Goyal dpaa_sec_enqueue_burst(void *qp, struct rte_crypto_op **ops,
16489a984458SAkhil Goyal 		       uint16_t nb_ops)
1649c3e85bdcSAkhil Goyal {
16509a984458SAkhil Goyal 	/* Function to transmit the frames to given device and queuepair */
16519a984458SAkhil Goyal 	uint32_t loop;
16529a984458SAkhil Goyal 	struct dpaa_sec_qp *dpaa_qp = (struct dpaa_sec_qp *)qp;
16539a984458SAkhil Goyal 	uint16_t num_tx = 0;
16549a984458SAkhil Goyal 	struct qm_fd fds[DPAA_SEC_BURST], *fd;
16559a984458SAkhil Goyal 	uint32_t frames_to_send;
16569a984458SAkhil Goyal 	struct rte_crypto_op *op;
1657c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
1658c3e85bdcSAkhil Goyal 	dpaa_sec_session *ses;
16599a984458SAkhil Goyal 	uint32_t auth_only_len;
16609a984458SAkhil Goyal 	struct qman_fq *inq[DPAA_SEC_BURST];
1661c3e85bdcSAkhil Goyal 
16629a984458SAkhil Goyal 	while (nb_ops) {
16639a984458SAkhil Goyal 		frames_to_send = (nb_ops > DPAA_SEC_BURST) ?
16649a984458SAkhil Goyal 				DPAA_SEC_BURST : nb_ops;
16659a984458SAkhil Goyal 		for (loop = 0; loop < frames_to_send; loop++) {
16669a984458SAkhil Goyal 			op = *(ops++);
16679a984458SAkhil Goyal 			switch (op->sess_type) {
16689a984458SAkhil Goyal 			case RTE_CRYPTO_OP_WITH_SESSION:
16699a984458SAkhil Goyal 				ses = (dpaa_sec_session *)
1670012c5076SPablo de Lara 					get_sym_session_private_data(
16719a984458SAkhil Goyal 							op->sym->session,
16729a984458SAkhil Goyal 							cryptodev_driver_id);
16739a984458SAkhil Goyal 				break;
16749a984458SAkhil Goyal 			case RTE_CRYPTO_OP_SECURITY_SESSION:
16759a984458SAkhil Goyal 				ses = (dpaa_sec_session *)
16769a984458SAkhil Goyal 					get_sec_session_private_data(
16771f14d500SAkhil Goyal 							op->sym->sec_session);
16789a984458SAkhil Goyal 				break;
16799a984458SAkhil Goyal 			default:
1680f163231eSHemant Agrawal 				DPAA_SEC_DP_ERR(
16819a984458SAkhil Goyal 					"sessionless crypto op not supported");
16829a984458SAkhil Goyal 				frames_to_send = loop;
16839a984458SAkhil Goyal 				nb_ops = loop;
16849a984458SAkhil Goyal 				goto send_pkts;
16859a984458SAkhil Goyal 			}
16869198b2c2SAkhil Goyal 			if (unlikely(!ses->qp)) {
16879a984458SAkhil Goyal 				if (dpaa_sec_attach_sess_q(qp, ses)) {
16889a984458SAkhil Goyal 					frames_to_send = loop;
16899a984458SAkhil Goyal 					nb_ops = loop;
16909a984458SAkhil Goyal 					goto send_pkts;
16919a984458SAkhil Goyal 				}
16929198b2c2SAkhil Goyal 			} else if (unlikely(ses->qp != qp)) {
16939198b2c2SAkhil Goyal 				DPAA_SEC_DP_ERR("Old:sess->qp = %p"
16949198b2c2SAkhil Goyal 					" New qp = %p\n", ses->qp, qp);
16959198b2c2SAkhil Goyal 				frames_to_send = loop;
16969198b2c2SAkhil Goyal 				nb_ops = loop;
16979198b2c2SAkhil Goyal 				goto send_pkts;
1698c3e85bdcSAkhil Goyal 			}
1699c3e85bdcSAkhil Goyal 
17009a984458SAkhil Goyal 			auth_only_len = op->sym->auth.data.length -
17019a984458SAkhil Goyal 						op->sym->cipher.data.length;
1702a74af788SAkhil Goyal 			if (rte_pktmbuf_is_contiguous(op->sym->m_src)) {
170305b12700SHemant Agrawal 				if (is_proto_ipsec(ses)) {
170405b12700SHemant Agrawal 					cf = build_proto(op, ses);
1705*a1173d55SHemant Agrawal 				} else if (is_proto_pdcp(ses)) {
1706*a1173d55SHemant Agrawal 					cf = build_proto(op, ses);
170705b12700SHemant Agrawal 				} else if (is_auth_only(ses)) {
1708c3e85bdcSAkhil Goyal 					cf = build_auth_only(op, ses);
1709c3e85bdcSAkhil Goyal 				} else if (is_cipher_only(ses)) {
1710c3e85bdcSAkhil Goyal 					cf = build_cipher_only(op, ses);
1711c3e85bdcSAkhil Goyal 				} else if (is_aead(ses)) {
1712c3e85bdcSAkhil Goyal 					cf = build_cipher_auth_gcm(op, ses);
1713c3e85bdcSAkhil Goyal 					auth_only_len = ses->auth_only_len;
1714c3e85bdcSAkhil Goyal 				} else if (is_auth_cipher(ses)) {
1715c3e85bdcSAkhil Goyal 					cf = build_cipher_auth(op, ses);
1716c3e85bdcSAkhil Goyal 				} else {
1717f163231eSHemant Agrawal 					DPAA_SEC_DP_ERR("not supported ops");
17189a984458SAkhil Goyal 					frames_to_send = loop;
17199a984458SAkhil Goyal 					nb_ops = loop;
17209a984458SAkhil Goyal 					goto send_pkts;
1721c3e85bdcSAkhil Goyal 				}
1722a74af788SAkhil Goyal 			} else {
1723a74af788SAkhil Goyal 				if (is_auth_only(ses)) {
1724a74af788SAkhil Goyal 					cf = build_auth_only_sg(op, ses);
1725a74af788SAkhil Goyal 				} else if (is_cipher_only(ses)) {
1726a74af788SAkhil Goyal 					cf = build_cipher_only_sg(op, ses);
1727a74af788SAkhil Goyal 				} else if (is_aead(ses)) {
1728a74af788SAkhil Goyal 					cf = build_cipher_auth_gcm_sg(op, ses);
1729a74af788SAkhil Goyal 					auth_only_len = ses->auth_only_len;
1730a74af788SAkhil Goyal 				} else if (is_auth_cipher(ses)) {
1731a74af788SAkhil Goyal 					cf = build_cipher_auth_sg(op, ses);
1732a74af788SAkhil Goyal 				} else {
1733f163231eSHemant Agrawal 					DPAA_SEC_DP_ERR("not supported ops");
1734a74af788SAkhil Goyal 					frames_to_send = loop;
1735a74af788SAkhil Goyal 					nb_ops = loop;
1736a74af788SAkhil Goyal 					goto send_pkts;
1737a74af788SAkhil Goyal 				}
1738a74af788SAkhil Goyal 			}
17399a984458SAkhil Goyal 			if (unlikely(!cf)) {
17409a984458SAkhil Goyal 				frames_to_send = loop;
17419a984458SAkhil Goyal 				nb_ops = loop;
17429a984458SAkhil Goyal 				goto send_pkts;
17439a984458SAkhil Goyal 			}
1744c3e85bdcSAkhil Goyal 
17459a984458SAkhil Goyal 			fd = &fds[loop];
17469a984458SAkhil Goyal 			inq[loop] = ses->inq;
17479a984458SAkhil Goyal 			fd->opaque_addr = 0;
17489a984458SAkhil Goyal 			fd->cmd = 0;
174995456e89SShreyansh Jain 			qm_fd_addr_set64(fd, dpaa_mem_vtop(cf->sg));
17509a984458SAkhil Goyal 			fd->_format1 = qm_fd_compound;
17519a984458SAkhil Goyal 			fd->length29 = 2 * sizeof(struct qm_sg_entry);
17529a984458SAkhil Goyal 			/* Auth_only_len is set as 0 in descriptor and it is
17539a984458SAkhil Goyal 			 * overwritten here in the fd.cmd which will update
17549a984458SAkhil Goyal 			 * the DPOVRD reg.
1755c3e85bdcSAkhil Goyal 			 */
1756c3e85bdcSAkhil Goyal 			if (auth_only_len)
17579a984458SAkhil Goyal 				fd->cmd = 0x80000000 | auth_only_len;
1758c3e85bdcSAkhil Goyal 
17599a984458SAkhil Goyal 		}
17609a984458SAkhil Goyal send_pkts:
17619a984458SAkhil Goyal 		loop = 0;
17629a984458SAkhil Goyal 		while (loop < frames_to_send) {
17639a984458SAkhil Goyal 			loop += qman_enqueue_multi_fq(&inq[loop], &fds[loop],
17649a984458SAkhil Goyal 					frames_to_send - loop);
17659a984458SAkhil Goyal 		}
17669a984458SAkhil Goyal 		nb_ops -= frames_to_send;
17679a984458SAkhil Goyal 		num_tx += frames_to_send;
1768c3e85bdcSAkhil Goyal 	}
1769c3e85bdcSAkhil Goyal 
1770c3e85bdcSAkhil Goyal 	dpaa_qp->tx_pkts += num_tx;
1771c3e85bdcSAkhil Goyal 	dpaa_qp->tx_errs += nb_ops - num_tx;
1772c3e85bdcSAkhil Goyal 
1773c3e85bdcSAkhil Goyal 	return num_tx;
1774c3e85bdcSAkhil Goyal }
1775c3e85bdcSAkhil Goyal 
1776c3e85bdcSAkhil Goyal static uint16_t
1777c3e85bdcSAkhil Goyal dpaa_sec_dequeue_burst(void *qp, struct rte_crypto_op **ops,
1778c3e85bdcSAkhil Goyal 		       uint16_t nb_ops)
1779c3e85bdcSAkhil Goyal {
1780c3e85bdcSAkhil Goyal 	uint16_t num_rx;
1781c3e85bdcSAkhil Goyal 	struct dpaa_sec_qp *dpaa_qp = (struct dpaa_sec_qp *)qp;
1782c3e85bdcSAkhil Goyal 
1783c3e85bdcSAkhil Goyal 	num_rx = dpaa_sec_deq(dpaa_qp, ops, nb_ops);
1784c3e85bdcSAkhil Goyal 
1785c3e85bdcSAkhil Goyal 	dpaa_qp->rx_pkts += num_rx;
1786c3e85bdcSAkhil Goyal 	dpaa_qp->rx_errs += nb_ops - num_rx;
1787c3e85bdcSAkhil Goyal 
1788f163231eSHemant Agrawal 	DPAA_SEC_DP_DEBUG("SEC Received %d Packets\n", num_rx);
1789c3e85bdcSAkhil Goyal 
1790c3e85bdcSAkhil Goyal 	return num_rx;
1791c3e85bdcSAkhil Goyal }
1792c3e85bdcSAkhil Goyal 
1793c3e85bdcSAkhil Goyal /** Release queue pair */
1794c3e85bdcSAkhil Goyal static int
1795c3e85bdcSAkhil Goyal dpaa_sec_queue_pair_release(struct rte_cryptodev *dev,
1796c3e85bdcSAkhil Goyal 			    uint16_t qp_id)
1797c3e85bdcSAkhil Goyal {
1798c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals;
1799c3e85bdcSAkhil Goyal 	struct dpaa_sec_qp *qp = NULL;
1800c3e85bdcSAkhil Goyal 
1801c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
1802c3e85bdcSAkhil Goyal 
1803f163231eSHemant Agrawal 	DPAA_SEC_DEBUG("dev =%p, queue =%d", dev, qp_id);
1804c3e85bdcSAkhil Goyal 
1805c3e85bdcSAkhil Goyal 	internals = dev->data->dev_private;
1806c3e85bdcSAkhil Goyal 	if (qp_id >= internals->max_nb_queue_pairs) {
1807f163231eSHemant Agrawal 		DPAA_SEC_ERR("Max supported qpid %d",
1808c3e85bdcSAkhil Goyal 			     internals->max_nb_queue_pairs);
1809c3e85bdcSAkhil Goyal 		return -EINVAL;
1810c3e85bdcSAkhil Goyal 	}
1811c3e85bdcSAkhil Goyal 
1812c3e85bdcSAkhil Goyal 	qp = &internals->qps[qp_id];
1813c3e85bdcSAkhil Goyal 	qp->internals = NULL;
1814c3e85bdcSAkhil Goyal 	dev->data->queue_pairs[qp_id] = NULL;
1815c3e85bdcSAkhil Goyal 
1816c3e85bdcSAkhil Goyal 	return 0;
1817c3e85bdcSAkhil Goyal }
1818c3e85bdcSAkhil Goyal 
1819c3e85bdcSAkhil Goyal /** Setup a queue pair */
1820c3e85bdcSAkhil Goyal static int
1821c3e85bdcSAkhil Goyal dpaa_sec_queue_pair_setup(struct rte_cryptodev *dev, uint16_t qp_id,
1822c3e85bdcSAkhil Goyal 		__rte_unused const struct rte_cryptodev_qp_conf *qp_conf,
1823725d2a7fSFan Zhang 		__rte_unused int socket_id)
1824c3e85bdcSAkhil Goyal {
1825c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals;
1826c3e85bdcSAkhil Goyal 	struct dpaa_sec_qp *qp = NULL;
1827c3e85bdcSAkhil Goyal 
1828f163231eSHemant Agrawal 	DPAA_SEC_DEBUG("dev =%p, queue =%d, conf =%p", dev, qp_id, qp_conf);
1829c3e85bdcSAkhil Goyal 
1830c3e85bdcSAkhil Goyal 	internals = dev->data->dev_private;
1831c3e85bdcSAkhil Goyal 	if (qp_id >= internals->max_nb_queue_pairs) {
1832f163231eSHemant Agrawal 		DPAA_SEC_ERR("Max supported qpid %d",
1833c3e85bdcSAkhil Goyal 			     internals->max_nb_queue_pairs);
1834c3e85bdcSAkhil Goyal 		return -EINVAL;
1835c3e85bdcSAkhil Goyal 	}
1836c3e85bdcSAkhil Goyal 
1837c3e85bdcSAkhil Goyal 	qp = &internals->qps[qp_id];
1838c3e85bdcSAkhil Goyal 	qp->internals = internals;
1839c3e85bdcSAkhil Goyal 	dev->data->queue_pairs[qp_id] = qp;
1840c3e85bdcSAkhil Goyal 
1841c3e85bdcSAkhil Goyal 	return 0;
1842c3e85bdcSAkhil Goyal }
1843c3e85bdcSAkhil Goyal 
1844c3e85bdcSAkhil Goyal /** Return the number of allocated queue pairs */
1845c3e85bdcSAkhil Goyal static uint32_t
1846c3e85bdcSAkhil Goyal dpaa_sec_queue_pair_count(struct rte_cryptodev *dev)
1847c3e85bdcSAkhil Goyal {
1848c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
1849c3e85bdcSAkhil Goyal 
1850c3e85bdcSAkhil Goyal 	return dev->data->nb_queue_pairs;
1851c3e85bdcSAkhil Goyal }
1852c3e85bdcSAkhil Goyal 
1853c3e85bdcSAkhil Goyal /** Returns the size of session structure */
1854c3e85bdcSAkhil Goyal static unsigned int
1855012c5076SPablo de Lara dpaa_sec_sym_session_get_size(struct rte_cryptodev *dev __rte_unused)
1856c3e85bdcSAkhil Goyal {
1857c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
1858c3e85bdcSAkhil Goyal 
1859c3e85bdcSAkhil Goyal 	return sizeof(dpaa_sec_session);
1860c3e85bdcSAkhil Goyal }
1861c3e85bdcSAkhil Goyal 
1862c3e85bdcSAkhil Goyal static int
1863c3e85bdcSAkhil Goyal dpaa_sec_cipher_init(struct rte_cryptodev *dev __rte_unused,
1864c3e85bdcSAkhil Goyal 		     struct rte_crypto_sym_xform *xform,
1865c3e85bdcSAkhil Goyal 		     dpaa_sec_session *session)
1866c3e85bdcSAkhil Goyal {
1867c3e85bdcSAkhil Goyal 	session->cipher_alg = xform->cipher.algo;
1868c3e85bdcSAkhil Goyal 	session->iv.length = xform->cipher.iv.length;
1869c3e85bdcSAkhil Goyal 	session->iv.offset = xform->cipher.iv.offset;
1870c3e85bdcSAkhil Goyal 	session->cipher_key.data = rte_zmalloc(NULL, xform->cipher.key.length,
1871c3e85bdcSAkhil Goyal 					       RTE_CACHE_LINE_SIZE);
1872c3e85bdcSAkhil Goyal 	if (session->cipher_key.data == NULL && xform->cipher.key.length > 0) {
1873f163231eSHemant Agrawal 		DPAA_SEC_ERR("No Memory for cipher key");
1874c3e85bdcSAkhil Goyal 		return -ENOMEM;
1875c3e85bdcSAkhil Goyal 	}
1876c3e85bdcSAkhil Goyal 	session->cipher_key.length = xform->cipher.key.length;
1877c3e85bdcSAkhil Goyal 
1878c3e85bdcSAkhil Goyal 	memcpy(session->cipher_key.data, xform->cipher.key.data,
1879c3e85bdcSAkhil Goyal 	       xform->cipher.key.length);
1880c3e85bdcSAkhil Goyal 	session->dir = (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
1881c3e85bdcSAkhil Goyal 			DIR_ENC : DIR_DEC;
1882c3e85bdcSAkhil Goyal 
1883c3e85bdcSAkhil Goyal 	return 0;
1884c3e85bdcSAkhil Goyal }
1885c3e85bdcSAkhil Goyal 
1886c3e85bdcSAkhil Goyal static int
1887c3e85bdcSAkhil Goyal dpaa_sec_auth_init(struct rte_cryptodev *dev __rte_unused,
1888c3e85bdcSAkhil Goyal 		   struct rte_crypto_sym_xform *xform,
1889c3e85bdcSAkhil Goyal 		   dpaa_sec_session *session)
1890c3e85bdcSAkhil Goyal {
1891c3e85bdcSAkhil Goyal 	session->auth_alg = xform->auth.algo;
1892c3e85bdcSAkhil Goyal 	session->auth_key.data = rte_zmalloc(NULL, xform->auth.key.length,
1893c3e85bdcSAkhil Goyal 					     RTE_CACHE_LINE_SIZE);
1894c3e85bdcSAkhil Goyal 	if (session->auth_key.data == NULL && xform->auth.key.length > 0) {
1895f163231eSHemant Agrawal 		DPAA_SEC_ERR("No Memory for auth key");
1896c3e85bdcSAkhil Goyal 		return -ENOMEM;
1897c3e85bdcSAkhil Goyal 	}
1898c3e85bdcSAkhil Goyal 	session->auth_key.length = xform->auth.key.length;
1899c3e85bdcSAkhil Goyal 	session->digest_length = xform->auth.digest_length;
1900c3e85bdcSAkhil Goyal 
1901c3e85bdcSAkhil Goyal 	memcpy(session->auth_key.data, xform->auth.key.data,
1902c3e85bdcSAkhil Goyal 	       xform->auth.key.length);
1903c3e85bdcSAkhil Goyal 	session->dir = (xform->auth.op == RTE_CRYPTO_AUTH_OP_GENERATE) ?
1904c3e85bdcSAkhil Goyal 			DIR_ENC : DIR_DEC;
1905c3e85bdcSAkhil Goyal 
1906c3e85bdcSAkhil Goyal 	return 0;
1907c3e85bdcSAkhil Goyal }
1908c3e85bdcSAkhil Goyal 
1909c3e85bdcSAkhil Goyal static int
1910c3e85bdcSAkhil Goyal dpaa_sec_aead_init(struct rte_cryptodev *dev __rte_unused,
1911c3e85bdcSAkhil Goyal 		   struct rte_crypto_sym_xform *xform,
1912c3e85bdcSAkhil Goyal 		   dpaa_sec_session *session)
1913c3e85bdcSAkhil Goyal {
1914c3e85bdcSAkhil Goyal 	session->aead_alg = xform->aead.algo;
1915c3e85bdcSAkhil Goyal 	session->iv.length = xform->aead.iv.length;
1916c3e85bdcSAkhil Goyal 	session->iv.offset = xform->aead.iv.offset;
1917c3e85bdcSAkhil Goyal 	session->auth_only_len = xform->aead.aad_length;
1918c3e85bdcSAkhil Goyal 	session->aead_key.data = rte_zmalloc(NULL, xform->aead.key.length,
1919c3e85bdcSAkhil Goyal 					     RTE_CACHE_LINE_SIZE);
1920c3e85bdcSAkhil Goyal 	if (session->aead_key.data == NULL && xform->aead.key.length > 0) {
1921f163231eSHemant Agrawal 		DPAA_SEC_ERR("No Memory for aead key\n");
1922c3e85bdcSAkhil Goyal 		return -ENOMEM;
1923c3e85bdcSAkhil Goyal 	}
1924c3e85bdcSAkhil Goyal 	session->aead_key.length = xform->aead.key.length;
1925c3e85bdcSAkhil Goyal 	session->digest_length = xform->aead.digest_length;
1926c3e85bdcSAkhil Goyal 
1927c3e85bdcSAkhil Goyal 	memcpy(session->aead_key.data, xform->aead.key.data,
1928c3e85bdcSAkhil Goyal 	       xform->aead.key.length);
1929c3e85bdcSAkhil Goyal 	session->dir = (xform->aead.op == RTE_CRYPTO_AEAD_OP_ENCRYPT) ?
1930c3e85bdcSAkhil Goyal 			DIR_ENC : DIR_DEC;
1931c3e85bdcSAkhil Goyal 
1932c3e85bdcSAkhil Goyal 	return 0;
1933c3e85bdcSAkhil Goyal }
1934c3e85bdcSAkhil Goyal 
1935e79416d1SHemant Agrawal static struct qman_fq *
1936e79416d1SHemant Agrawal dpaa_sec_attach_rxq(struct dpaa_sec_dev_private *qi)
1937c3e85bdcSAkhil Goyal {
1938e79416d1SHemant Agrawal 	unsigned int i;
1939c3e85bdcSAkhil Goyal 
1940e79416d1SHemant Agrawal 	for (i = 0; i < qi->max_nb_sessions; i++) {
1941e79416d1SHemant Agrawal 		if (qi->inq_attach[i] == 0) {
1942e79416d1SHemant Agrawal 			qi->inq_attach[i] = 1;
1943e79416d1SHemant Agrawal 			return &qi->inq[i];
1944e79416d1SHemant Agrawal 		}
1945e79416d1SHemant Agrawal 	}
1946f163231eSHemant Agrawal 	DPAA_SEC_WARN("All ses session in use %x", qi->max_nb_sessions);
1947c3e85bdcSAkhil Goyal 
1948e79416d1SHemant Agrawal 	return NULL;
1949c3e85bdcSAkhil Goyal }
1950c3e85bdcSAkhil Goyal 
1951e79416d1SHemant Agrawal static int
1952e79416d1SHemant Agrawal dpaa_sec_detach_rxq(struct dpaa_sec_dev_private *qi, struct qman_fq *fq)
1953e79416d1SHemant Agrawal {
1954e79416d1SHemant Agrawal 	unsigned int i;
1955e79416d1SHemant Agrawal 
1956e79416d1SHemant Agrawal 	for (i = 0; i < qi->max_nb_sessions; i++) {
1957e79416d1SHemant Agrawal 		if (&qi->inq[i] == fq) {
1958b4053c4bSAlok Makhariya 			qman_retire_fq(fq, NULL);
1959b4053c4bSAlok Makhariya 			qman_oos_fq(fq);
1960e79416d1SHemant Agrawal 			qi->inq_attach[i] = 0;
1961e79416d1SHemant Agrawal 			return 0;
1962e79416d1SHemant Agrawal 		}
1963e79416d1SHemant Agrawal 	}
1964e79416d1SHemant Agrawal 	return -1;
1965e79416d1SHemant Agrawal }
1966e79416d1SHemant Agrawal 
1967e79416d1SHemant Agrawal static int
1968e79416d1SHemant Agrawal dpaa_sec_attach_sess_q(struct dpaa_sec_qp *qp, dpaa_sec_session *sess)
1969e79416d1SHemant Agrawal {
1970e79416d1SHemant Agrawal 	int ret;
1971e79416d1SHemant Agrawal 
1972c3e85bdcSAkhil Goyal 	sess->qp = qp;
1973e79416d1SHemant Agrawal 	ret = dpaa_sec_prep_cdb(sess);
1974e79416d1SHemant Agrawal 	if (ret) {
1975f163231eSHemant Agrawal 		DPAA_SEC_ERR("Unable to prepare sec cdb");
1976e79416d1SHemant Agrawal 		return -1;
1977e79416d1SHemant Agrawal 	}
19785b0f1bd3SAshish Jain 	if (unlikely(!RTE_PER_LCORE(dpaa_io))) {
19795b0f1bd3SAshish Jain 		ret = rte_dpaa_portal_init((void *)0);
19805b0f1bd3SAshish Jain 		if (ret) {
1981f163231eSHemant Agrawal 			DPAA_SEC_ERR("Failure in affining portal");
19825b0f1bd3SAshish Jain 			return ret;
19835b0f1bd3SAshish Jain 		}
19845b0f1bd3SAshish Jain 	}
1985e79416d1SHemant Agrawal 	ret = dpaa_sec_init_rx(sess->inq, dpaa_mem_vtop(&sess->cdb),
1986e79416d1SHemant Agrawal 			       qman_fq_fqid(&qp->outq));
1987e79416d1SHemant Agrawal 	if (ret)
1988f163231eSHemant Agrawal 		DPAA_SEC_ERR("Unable to init sec queue");
1989e79416d1SHemant Agrawal 
1990e79416d1SHemant Agrawal 	return ret;
1991c3e85bdcSAkhil Goyal }
1992c3e85bdcSAkhil Goyal 
1993c3e85bdcSAkhil Goyal static int
1994c3e85bdcSAkhil Goyal dpaa_sec_set_session_parameters(struct rte_cryptodev *dev,
1995c3e85bdcSAkhil Goyal 			    struct rte_crypto_sym_xform *xform,	void *sess)
1996c3e85bdcSAkhil Goyal {
1997c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals = dev->data->dev_private;
1998c3e85bdcSAkhil Goyal 	dpaa_sec_session *session = sess;
1999c3e85bdcSAkhil Goyal 
2000c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2001c3e85bdcSAkhil Goyal 
2002c3e85bdcSAkhil Goyal 	if (unlikely(sess == NULL)) {
2003f163231eSHemant Agrawal 		DPAA_SEC_ERR("invalid session struct");
2004c3e85bdcSAkhil Goyal 		return -EINVAL;
2005c3e85bdcSAkhil Goyal 	}
2006b0894102SAkhil Goyal 	memset(session, 0, sizeof(dpaa_sec_session));
2007c3e85bdcSAkhil Goyal 
2008c3e85bdcSAkhil Goyal 	/* Default IV length = 0 */
2009c3e85bdcSAkhil Goyal 	session->iv.length = 0;
2010c3e85bdcSAkhil Goyal 
2011c3e85bdcSAkhil Goyal 	/* Cipher Only */
2012c3e85bdcSAkhil Goyal 	if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER && xform->next == NULL) {
2013c3e85bdcSAkhil Goyal 		session->auth_alg = RTE_CRYPTO_AUTH_NULL;
2014c3e85bdcSAkhil Goyal 		dpaa_sec_cipher_init(dev, xform, session);
2015c3e85bdcSAkhil Goyal 
2016c3e85bdcSAkhil Goyal 	/* Authentication Only */
2017c3e85bdcSAkhil Goyal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
2018c3e85bdcSAkhil Goyal 		   xform->next == NULL) {
2019c3e85bdcSAkhil Goyal 		session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
2020c3e85bdcSAkhil Goyal 		dpaa_sec_auth_init(dev, xform, session);
2021c3e85bdcSAkhil Goyal 
2022c3e85bdcSAkhil Goyal 	/* Cipher then Authenticate */
2023c3e85bdcSAkhil Goyal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
2024c3e85bdcSAkhil Goyal 		   xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
2025c3e85bdcSAkhil Goyal 		if (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) {
2026c3e85bdcSAkhil Goyal 			dpaa_sec_cipher_init(dev, xform, session);
2027c3e85bdcSAkhil Goyal 			dpaa_sec_auth_init(dev, xform->next, session);
2028c3e85bdcSAkhil Goyal 		} else {
2029f163231eSHemant Agrawal 			DPAA_SEC_ERR("Not supported: Auth then Cipher");
2030c3e85bdcSAkhil Goyal 			return -EINVAL;
2031c3e85bdcSAkhil Goyal 		}
2032c3e85bdcSAkhil Goyal 
2033c3e85bdcSAkhil Goyal 	/* Authenticate then Cipher */
2034c3e85bdcSAkhil Goyal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
2035c3e85bdcSAkhil Goyal 		   xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
2036c3e85bdcSAkhil Goyal 		if (xform->next->cipher.op == RTE_CRYPTO_CIPHER_OP_DECRYPT) {
2037c3e85bdcSAkhil Goyal 			dpaa_sec_auth_init(dev, xform, session);
2038c3e85bdcSAkhil Goyal 			dpaa_sec_cipher_init(dev, xform->next, session);
2039c3e85bdcSAkhil Goyal 		} else {
2040f163231eSHemant Agrawal 			DPAA_SEC_ERR("Not supported: Auth then Cipher");
2041c3e85bdcSAkhil Goyal 			return -EINVAL;
2042c3e85bdcSAkhil Goyal 		}
2043c3e85bdcSAkhil Goyal 
2044c3e85bdcSAkhil Goyal 	/* AEAD operation for AES-GCM kind of Algorithms */
2045c3e85bdcSAkhil Goyal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD &&
2046c3e85bdcSAkhil Goyal 		   xform->next == NULL) {
2047c3e85bdcSAkhil Goyal 		dpaa_sec_aead_init(dev, xform, session);
2048c3e85bdcSAkhil Goyal 
2049c3e85bdcSAkhil Goyal 	} else {
2050f163231eSHemant Agrawal 		DPAA_SEC_ERR("Invalid crypto type");
2051c3e85bdcSAkhil Goyal 		return -EINVAL;
2052c3e85bdcSAkhil Goyal 	}
2053c3e85bdcSAkhil Goyal 	session->ctx_pool = internals->ctx_pool;
20543b617ee7SAkhil Goyal 	rte_spinlock_lock(&internals->lock);
2055e79416d1SHemant Agrawal 	session->inq = dpaa_sec_attach_rxq(internals);
20563b617ee7SAkhil Goyal 	rte_spinlock_unlock(&internals->lock);
2057e79416d1SHemant Agrawal 	if (session->inq == NULL) {
2058f163231eSHemant Agrawal 		DPAA_SEC_ERR("unable to attach sec queue");
2059e79416d1SHemant Agrawal 		goto err1;
2060e79416d1SHemant Agrawal 	}
2061c3e85bdcSAkhil Goyal 
2062c3e85bdcSAkhil Goyal 	return 0;
2063e79416d1SHemant Agrawal 
2064e79416d1SHemant Agrawal err1:
2065e79416d1SHemant Agrawal 	rte_free(session->cipher_key.data);
2066e79416d1SHemant Agrawal 	rte_free(session->auth_key.data);
2067e79416d1SHemant Agrawal 	memset(session, 0, sizeof(dpaa_sec_session));
2068e79416d1SHemant Agrawal 
2069e79416d1SHemant Agrawal 	return -EINVAL;
2070c3e85bdcSAkhil Goyal }
2071c3e85bdcSAkhil Goyal 
2072c3e85bdcSAkhil Goyal static int
2073012c5076SPablo de Lara dpaa_sec_sym_session_configure(struct rte_cryptodev *dev,
2074c3e85bdcSAkhil Goyal 		struct rte_crypto_sym_xform *xform,
2075c3e85bdcSAkhil Goyal 		struct rte_cryptodev_sym_session *sess,
2076c3e85bdcSAkhil Goyal 		struct rte_mempool *mempool)
2077c3e85bdcSAkhil Goyal {
2078c3e85bdcSAkhil Goyal 	void *sess_private_data;
2079c3e85bdcSAkhil Goyal 	int ret;
2080c3e85bdcSAkhil Goyal 
2081c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2082c3e85bdcSAkhil Goyal 
2083c3e85bdcSAkhil Goyal 	if (rte_mempool_get(mempool, &sess_private_data)) {
2084f163231eSHemant Agrawal 		DPAA_SEC_ERR("Couldn't get object from session mempool");
2085c3e85bdcSAkhil Goyal 		return -ENOMEM;
2086c3e85bdcSAkhil Goyal 	}
2087c3e85bdcSAkhil Goyal 
2088c3e85bdcSAkhil Goyal 	ret = dpaa_sec_set_session_parameters(dev, xform, sess_private_data);
2089c3e85bdcSAkhil Goyal 	if (ret != 0) {
2090f163231eSHemant Agrawal 		DPAA_SEC_ERR("failed to configure session parameters");
2091c3e85bdcSAkhil Goyal 
2092c3e85bdcSAkhil Goyal 		/* Return session to mempool */
2093c3e85bdcSAkhil Goyal 		rte_mempool_put(mempool, sess_private_data);
2094c3e85bdcSAkhil Goyal 		return ret;
2095c3e85bdcSAkhil Goyal 	}
2096c3e85bdcSAkhil Goyal 
2097012c5076SPablo de Lara 	set_sym_session_private_data(sess, dev->driver_id,
2098c3e85bdcSAkhil Goyal 			sess_private_data);
2099c3e85bdcSAkhil Goyal 
2100e79416d1SHemant Agrawal 
2101c3e85bdcSAkhil Goyal 	return 0;
2102c3e85bdcSAkhil Goyal }
2103c3e85bdcSAkhil Goyal 
2104c3e85bdcSAkhil Goyal /** Clear the memory of session so it doesn't leave key material behind */
2105c3e85bdcSAkhil Goyal static void
2106012c5076SPablo de Lara dpaa_sec_sym_session_clear(struct rte_cryptodev *dev,
2107c3e85bdcSAkhil Goyal 		struct rte_cryptodev_sym_session *sess)
2108c3e85bdcSAkhil Goyal {
2109e79416d1SHemant Agrawal 	struct dpaa_sec_dev_private *qi = dev->data->dev_private;
2110c3e85bdcSAkhil Goyal 	uint8_t index = dev->driver_id;
2111012c5076SPablo de Lara 	void *sess_priv = get_sym_session_private_data(sess, index);
2112e79416d1SHemant Agrawal 
2113e79416d1SHemant Agrawal 	PMD_INIT_FUNC_TRACE();
2114e79416d1SHemant Agrawal 
2115c3e85bdcSAkhil Goyal 	dpaa_sec_session *s = (dpaa_sec_session *)sess_priv;
2116c3e85bdcSAkhil Goyal 
2117c3e85bdcSAkhil Goyal 	if (sess_priv) {
2118e79416d1SHemant Agrawal 		struct rte_mempool *sess_mp = rte_mempool_from_obj(sess_priv);
2119e79416d1SHemant Agrawal 
2120e79416d1SHemant Agrawal 		if (s->inq)
2121e79416d1SHemant Agrawal 			dpaa_sec_detach_rxq(qi, s->inq);
2122c3e85bdcSAkhil Goyal 		rte_free(s->cipher_key.data);
2123c3e85bdcSAkhil Goyal 		rte_free(s->auth_key.data);
2124c3e85bdcSAkhil Goyal 		memset(s, 0, sizeof(dpaa_sec_session));
2125012c5076SPablo de Lara 		set_sym_session_private_data(sess, index, NULL);
2126c3e85bdcSAkhil Goyal 		rte_mempool_put(sess_mp, sess_priv);
2127c3e85bdcSAkhil Goyal 	}
2128c3e85bdcSAkhil Goyal }
2129c3e85bdcSAkhil Goyal 
2130c3e85bdcSAkhil Goyal static int
21311f14d500SAkhil Goyal dpaa_sec_set_ipsec_session(__rte_unused struct rte_cryptodev *dev,
21321f14d500SAkhil Goyal 			   struct rte_security_session_conf *conf,
21331f14d500SAkhil Goyal 			   void *sess)
21341f14d500SAkhil Goyal {
21351f14d500SAkhil Goyal 	struct dpaa_sec_dev_private *internals = dev->data->dev_private;
21361f14d500SAkhil Goyal 	struct rte_security_ipsec_xform *ipsec_xform = &conf->ipsec;
213705b12700SHemant Agrawal 	struct rte_crypto_auth_xform *auth_xform = NULL;
213805b12700SHemant Agrawal 	struct rte_crypto_cipher_xform *cipher_xform = NULL;
21391f14d500SAkhil Goyal 	dpaa_sec_session *session = (dpaa_sec_session *)sess;
21401f14d500SAkhil Goyal 
21411f14d500SAkhil Goyal 	PMD_INIT_FUNC_TRACE();
21421f14d500SAkhil Goyal 
2143b0894102SAkhil Goyal 	memset(session, 0, sizeof(dpaa_sec_session));
21441f14d500SAkhil Goyal 	if (ipsec_xform->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
21451f14d500SAkhil Goyal 		cipher_xform = &conf->crypto_xform->cipher;
214605b12700SHemant Agrawal 		if (conf->crypto_xform->next)
21471f14d500SAkhil Goyal 			auth_xform = &conf->crypto_xform->next->auth;
21481f14d500SAkhil Goyal 	} else {
21491f14d500SAkhil Goyal 		auth_xform = &conf->crypto_xform->auth;
215005b12700SHemant Agrawal 		if (conf->crypto_xform->next)
21511f14d500SAkhil Goyal 			cipher_xform = &conf->crypto_xform->next->cipher;
21521f14d500SAkhil Goyal 	}
21531f14d500SAkhil Goyal 	session->proto_alg = conf->protocol;
215405b12700SHemant Agrawal 
215505b12700SHemant Agrawal 	if (cipher_xform && cipher_xform->algo != RTE_CRYPTO_CIPHER_NULL) {
21561f14d500SAkhil Goyal 		session->cipher_key.data = rte_zmalloc(NULL,
21571f14d500SAkhil Goyal 						       cipher_xform->key.length,
21581f14d500SAkhil Goyal 						       RTE_CACHE_LINE_SIZE);
21591f14d500SAkhil Goyal 		if (session->cipher_key.data == NULL &&
21601f14d500SAkhil Goyal 				cipher_xform->key.length > 0) {
2161f163231eSHemant Agrawal 			DPAA_SEC_ERR("No Memory for cipher key");
21621f14d500SAkhil Goyal 			return -ENOMEM;
21631f14d500SAkhil Goyal 		}
216405b12700SHemant Agrawal 		memcpy(session->cipher_key.data, cipher_xform->key.data,
216505b12700SHemant Agrawal 				cipher_xform->key.length);
21661f14d500SAkhil Goyal 		session->cipher_key.length = cipher_xform->key.length;
216705b12700SHemant Agrawal 
216805b12700SHemant Agrawal 		switch (cipher_xform->algo) {
216905b12700SHemant Agrawal 		case RTE_CRYPTO_CIPHER_AES_CBC:
217005b12700SHemant Agrawal 		case RTE_CRYPTO_CIPHER_3DES_CBC:
217105b12700SHemant Agrawal 		case RTE_CRYPTO_CIPHER_AES_CTR:
217205b12700SHemant Agrawal 			break;
217305b12700SHemant Agrawal 		default:
217405b12700SHemant Agrawal 			DPAA_SEC_ERR("Crypto: Unsupported Cipher alg %u",
217505b12700SHemant Agrawal 				cipher_xform->algo);
217605b12700SHemant Agrawal 			goto out;
217705b12700SHemant Agrawal 		}
217805b12700SHemant Agrawal 		session->cipher_alg = cipher_xform->algo;
217905b12700SHemant Agrawal 	} else {
218005b12700SHemant Agrawal 		session->cipher_key.data = NULL;
218105b12700SHemant Agrawal 		session->cipher_key.length = 0;
218205b12700SHemant Agrawal 		session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
218305b12700SHemant Agrawal 	}
218405b12700SHemant Agrawal 
218505b12700SHemant Agrawal 	if (auth_xform && auth_xform->algo != RTE_CRYPTO_AUTH_NULL) {
21861f14d500SAkhil Goyal 		session->auth_key.data = rte_zmalloc(NULL,
21871f14d500SAkhil Goyal 						auth_xform->key.length,
21881f14d500SAkhil Goyal 						RTE_CACHE_LINE_SIZE);
21891f14d500SAkhil Goyal 		if (session->auth_key.data == NULL &&
21901f14d500SAkhil Goyal 				auth_xform->key.length > 0) {
2191f163231eSHemant Agrawal 			DPAA_SEC_ERR("No Memory for auth key");
21921f14d500SAkhil Goyal 			rte_free(session->cipher_key.data);
21931f14d500SAkhil Goyal 			return -ENOMEM;
21941f14d500SAkhil Goyal 		}
21951f14d500SAkhil Goyal 		memcpy(session->auth_key.data, auth_xform->key.data,
21961f14d500SAkhil Goyal 				auth_xform->key.length);
219705b12700SHemant Agrawal 		session->auth_key.length = auth_xform->key.length;
21981f14d500SAkhil Goyal 
21991f14d500SAkhil Goyal 		switch (auth_xform->algo) {
22001f14d500SAkhil Goyal 		case RTE_CRYPTO_AUTH_SHA1_HMAC:
22011f14d500SAkhil Goyal 		case RTE_CRYPTO_AUTH_MD5_HMAC:
22021f14d500SAkhil Goyal 		case RTE_CRYPTO_AUTH_SHA256_HMAC:
22031f14d500SAkhil Goyal 		case RTE_CRYPTO_AUTH_SHA384_HMAC:
22041f14d500SAkhil Goyal 		case RTE_CRYPTO_AUTH_SHA512_HMAC:
22051f14d500SAkhil Goyal 		case RTE_CRYPTO_AUTH_AES_CMAC:
22061f14d500SAkhil Goyal 			break;
220705b12700SHemant Agrawal 		default:
2208f163231eSHemant Agrawal 			DPAA_SEC_ERR("Crypto: Unsupported auth alg %u",
22091f14d500SAkhil Goyal 				auth_xform->algo);
22101f14d500SAkhil Goyal 			goto out;
22111f14d500SAkhil Goyal 		}
221205b12700SHemant Agrawal 		session->auth_alg = auth_xform->algo;
221305b12700SHemant Agrawal 	} else {
221405b12700SHemant Agrawal 		session->auth_key.data = NULL;
221505b12700SHemant Agrawal 		session->auth_key.length = 0;
221605b12700SHemant Agrawal 		session->auth_alg = RTE_CRYPTO_AUTH_NULL;
22171f14d500SAkhil Goyal 	}
22181f14d500SAkhil Goyal 
22191f14d500SAkhil Goyal 	if (ipsec_xform->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
22201f14d500SAkhil Goyal 		memset(&session->encap_pdb, 0, sizeof(struct ipsec_encap_pdb) +
22211f14d500SAkhil Goyal 				sizeof(session->ip4_hdr));
22221f14d500SAkhil Goyal 		session->ip4_hdr.ip_v = IPVERSION;
22231f14d500SAkhil Goyal 		session->ip4_hdr.ip_hl = 5;
22241f14d500SAkhil Goyal 		session->ip4_hdr.ip_len = rte_cpu_to_be_16(
22251f14d500SAkhil Goyal 						sizeof(session->ip4_hdr));
22261f14d500SAkhil Goyal 		session->ip4_hdr.ip_tos = ipsec_xform->tunnel.ipv4.dscp;
22271f14d500SAkhil Goyal 		session->ip4_hdr.ip_id = 0;
22281f14d500SAkhil Goyal 		session->ip4_hdr.ip_off = 0;
22291f14d500SAkhil Goyal 		session->ip4_hdr.ip_ttl = ipsec_xform->tunnel.ipv4.ttl;
22301f14d500SAkhil Goyal 		session->ip4_hdr.ip_p = (ipsec_xform->proto ==
22311f14d500SAkhil Goyal 				RTE_SECURITY_IPSEC_SA_PROTO_ESP) ? IPPROTO_ESP
22321f14d500SAkhil Goyal 				: IPPROTO_AH;
22331f14d500SAkhil Goyal 		session->ip4_hdr.ip_sum = 0;
22341f14d500SAkhil Goyal 		session->ip4_hdr.ip_src = ipsec_xform->tunnel.ipv4.src_ip;
22351f14d500SAkhil Goyal 		session->ip4_hdr.ip_dst = ipsec_xform->tunnel.ipv4.dst_ip;
22361f14d500SAkhil Goyal 		session->ip4_hdr.ip_sum = calc_chksum((uint16_t *)
22371f14d500SAkhil Goyal 						(void *)&session->ip4_hdr,
22381f14d500SAkhil Goyal 						sizeof(struct ip));
22391f14d500SAkhil Goyal 
22401f14d500SAkhil Goyal 		session->encap_pdb.options =
22411f14d500SAkhil Goyal 			(IPVERSION << PDBNH_ESP_ENCAP_SHIFT) |
22421f14d500SAkhil Goyal 			PDBOPTS_ESP_OIHI_PDB_INL |
22431f14d500SAkhil Goyal 			PDBOPTS_ESP_IVSRC |
224479fde9d0SAkhil Goyal 			PDBHMO_ESP_ENCAP_DTTL |
224579fde9d0SAkhil Goyal 			PDBHMO_ESP_SNR;
22461f14d500SAkhil Goyal 		session->encap_pdb.spi = ipsec_xform->spi;
22471f14d500SAkhil Goyal 		session->encap_pdb.ip_hdr_len = sizeof(struct ip);
22481f14d500SAkhil Goyal 
22491f14d500SAkhil Goyal 		session->dir = DIR_ENC;
22501f14d500SAkhil Goyal 	} else if (ipsec_xform->direction ==
22511f14d500SAkhil Goyal 			RTE_SECURITY_IPSEC_SA_DIR_INGRESS) {
22521f14d500SAkhil Goyal 		memset(&session->decap_pdb, 0, sizeof(struct ipsec_decap_pdb));
22531f14d500SAkhil Goyal 		session->decap_pdb.options = sizeof(struct ip) << 16;
22541f14d500SAkhil Goyal 		session->dir = DIR_DEC;
22551f14d500SAkhil Goyal 	} else
22561f14d500SAkhil Goyal 		goto out;
22571f14d500SAkhil Goyal 	session->ctx_pool = internals->ctx_pool;
22583b617ee7SAkhil Goyal 	rte_spinlock_lock(&internals->lock);
22591f14d500SAkhil Goyal 	session->inq = dpaa_sec_attach_rxq(internals);
22603b617ee7SAkhil Goyal 	rte_spinlock_unlock(&internals->lock);
22611f14d500SAkhil Goyal 	if (session->inq == NULL) {
2262f163231eSHemant Agrawal 		DPAA_SEC_ERR("unable to attach sec queue");
22631f14d500SAkhil Goyal 		goto out;
22641f14d500SAkhil Goyal 	}
22651f14d500SAkhil Goyal 
2266*a1173d55SHemant Agrawal 	return 0;
2267*a1173d55SHemant Agrawal out:
2268*a1173d55SHemant Agrawal 	rte_free(session->auth_key.data);
2269*a1173d55SHemant Agrawal 	rte_free(session->cipher_key.data);
2270*a1173d55SHemant Agrawal 	memset(session, 0, sizeof(dpaa_sec_session));
2271*a1173d55SHemant Agrawal 	return -1;
2272*a1173d55SHemant Agrawal }
22731f14d500SAkhil Goyal 
2274*a1173d55SHemant Agrawal static int
2275*a1173d55SHemant Agrawal dpaa_sec_set_pdcp_session(struct rte_cryptodev *dev,
2276*a1173d55SHemant Agrawal 			  struct rte_security_session_conf *conf,
2277*a1173d55SHemant Agrawal 			  void *sess)
2278*a1173d55SHemant Agrawal {
2279*a1173d55SHemant Agrawal 	struct rte_security_pdcp_xform *pdcp_xform = &conf->pdcp;
2280*a1173d55SHemant Agrawal 	struct rte_crypto_sym_xform *xform = conf->crypto_xform;
2281*a1173d55SHemant Agrawal 	struct rte_crypto_auth_xform *auth_xform = NULL;
2282*a1173d55SHemant Agrawal 	struct rte_crypto_cipher_xform *cipher_xform = NULL;
2283*a1173d55SHemant Agrawal 	dpaa_sec_session *session = (dpaa_sec_session *)sess;
2284*a1173d55SHemant Agrawal 	struct dpaa_sec_dev_private *dev_priv = dev->data->dev_private;
2285*a1173d55SHemant Agrawal 
2286*a1173d55SHemant Agrawal 	PMD_INIT_FUNC_TRACE();
2287*a1173d55SHemant Agrawal 
2288*a1173d55SHemant Agrawal 	memset(session, 0, sizeof(dpaa_sec_session));
2289*a1173d55SHemant Agrawal 
2290*a1173d55SHemant Agrawal 	/* find xfrm types */
2291*a1173d55SHemant Agrawal 	if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
2292*a1173d55SHemant Agrawal 		cipher_xform = &xform->cipher;
2293*a1173d55SHemant Agrawal 		if (xform->next != NULL)
2294*a1173d55SHemant Agrawal 			auth_xform = &xform->next->auth;
2295*a1173d55SHemant Agrawal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
2296*a1173d55SHemant Agrawal 		auth_xform = &xform->auth;
2297*a1173d55SHemant Agrawal 		if (xform->next != NULL)
2298*a1173d55SHemant Agrawal 			cipher_xform = &xform->next->cipher;
2299*a1173d55SHemant Agrawal 	} else {
2300*a1173d55SHemant Agrawal 		DPAA_SEC_ERR("Invalid crypto type");
2301*a1173d55SHemant Agrawal 		return -EINVAL;
2302*a1173d55SHemant Agrawal 	}
2303*a1173d55SHemant Agrawal 
2304*a1173d55SHemant Agrawal 	session->proto_alg = conf->protocol;
2305*a1173d55SHemant Agrawal 	if (cipher_xform) {
2306*a1173d55SHemant Agrawal 		session->cipher_key.data = rte_zmalloc(NULL,
2307*a1173d55SHemant Agrawal 					       cipher_xform->key.length,
2308*a1173d55SHemant Agrawal 					       RTE_CACHE_LINE_SIZE);
2309*a1173d55SHemant Agrawal 		if (session->cipher_key.data == NULL &&
2310*a1173d55SHemant Agrawal 				cipher_xform->key.length > 0) {
2311*a1173d55SHemant Agrawal 			DPAA_SEC_ERR("No Memory for cipher key");
2312*a1173d55SHemant Agrawal 			return -ENOMEM;
2313*a1173d55SHemant Agrawal 		}
2314*a1173d55SHemant Agrawal 		session->cipher_key.length = cipher_xform->key.length;
2315*a1173d55SHemant Agrawal 		memcpy(session->cipher_key.data, cipher_xform->key.data,
2316*a1173d55SHemant Agrawal 			cipher_xform->key.length);
2317*a1173d55SHemant Agrawal 		session->dir = (cipher_xform->op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
2318*a1173d55SHemant Agrawal 					DIR_ENC : DIR_DEC;
2319*a1173d55SHemant Agrawal 		session->cipher_alg = cipher_xform->algo;
2320*a1173d55SHemant Agrawal 	} else {
2321*a1173d55SHemant Agrawal 		session->cipher_key.data = NULL;
2322*a1173d55SHemant Agrawal 		session->cipher_key.length = 0;
2323*a1173d55SHemant Agrawal 		session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
2324*a1173d55SHemant Agrawal 		session->dir = DIR_ENC;
2325*a1173d55SHemant Agrawal 	}
2326*a1173d55SHemant Agrawal 
2327*a1173d55SHemant Agrawal 	/* Auth is only applicable for control mode operation. */
2328*a1173d55SHemant Agrawal 	if (pdcp_xform->domain == RTE_SECURITY_PDCP_MODE_CONTROL) {
2329*a1173d55SHemant Agrawal 		if (pdcp_xform->sn_size != RTE_SECURITY_PDCP_SN_SIZE_5) {
2330*a1173d55SHemant Agrawal 			DPAA_SEC_ERR(
2331*a1173d55SHemant Agrawal 				"PDCP Seq Num size should be 5 bits for cmode");
2332*a1173d55SHemant Agrawal 			goto out;
2333*a1173d55SHemant Agrawal 		}
2334*a1173d55SHemant Agrawal 		if (auth_xform) {
2335*a1173d55SHemant Agrawal 			session->auth_key.data = rte_zmalloc(NULL,
2336*a1173d55SHemant Agrawal 							auth_xform->key.length,
2337*a1173d55SHemant Agrawal 							RTE_CACHE_LINE_SIZE);
2338*a1173d55SHemant Agrawal 			if (session->auth_key.data == NULL &&
2339*a1173d55SHemant Agrawal 					auth_xform->key.length > 0) {
2340*a1173d55SHemant Agrawal 				DPAA_SEC_ERR("No Memory for auth key");
2341*a1173d55SHemant Agrawal 				rte_free(session->cipher_key.data);
2342*a1173d55SHemant Agrawal 				return -ENOMEM;
2343*a1173d55SHemant Agrawal 			}
2344*a1173d55SHemant Agrawal 			session->auth_key.length = auth_xform->key.length;
2345*a1173d55SHemant Agrawal 			memcpy(session->auth_key.data, auth_xform->key.data,
2346*a1173d55SHemant Agrawal 					auth_xform->key.length);
2347*a1173d55SHemant Agrawal 			session->auth_alg = auth_xform->algo;
2348*a1173d55SHemant Agrawal 		} else {
2349*a1173d55SHemant Agrawal 			session->auth_key.data = NULL;
2350*a1173d55SHemant Agrawal 			session->auth_key.length = 0;
2351*a1173d55SHemant Agrawal 			session->auth_alg = RTE_CRYPTO_AUTH_NULL;
2352*a1173d55SHemant Agrawal 		}
2353*a1173d55SHemant Agrawal 	}
2354*a1173d55SHemant Agrawal 	session->pdcp.domain = pdcp_xform->domain;
2355*a1173d55SHemant Agrawal 	session->pdcp.bearer = pdcp_xform->bearer;
2356*a1173d55SHemant Agrawal 	session->pdcp.pkt_dir = pdcp_xform->pkt_dir;
2357*a1173d55SHemant Agrawal 	session->pdcp.sn_size = pdcp_xform->sn_size;
2358*a1173d55SHemant Agrawal #ifdef ENABLE_HFN_OVERRIDE
2359*a1173d55SHemant Agrawal 	session->pdcp.hfn_ovd = pdcp_xform->hfn_ovd;
2360*a1173d55SHemant Agrawal #endif
2361*a1173d55SHemant Agrawal 	session->pdcp.hfn = pdcp_xform->hfn;
2362*a1173d55SHemant Agrawal 	session->pdcp.hfn_threshold = pdcp_xform->hfn_threshold;
2363*a1173d55SHemant Agrawal 
2364*a1173d55SHemant Agrawal 	session->ctx_pool = dev_priv->ctx_pool;
2365*a1173d55SHemant Agrawal 	rte_spinlock_lock(&dev_priv->lock);
2366*a1173d55SHemant Agrawal 	session->inq = dpaa_sec_attach_rxq(dev_priv);
2367*a1173d55SHemant Agrawal 	rte_spinlock_unlock(&dev_priv->lock);
2368*a1173d55SHemant Agrawal 	if (session->inq == NULL) {
2369*a1173d55SHemant Agrawal 		DPAA_SEC_ERR("unable to attach sec queue");
2370*a1173d55SHemant Agrawal 		goto out;
2371*a1173d55SHemant Agrawal 	}
23721f14d500SAkhil Goyal 	return 0;
23731f14d500SAkhil Goyal out:
23741f14d500SAkhil Goyal 	rte_free(session->auth_key.data);
23751f14d500SAkhil Goyal 	rte_free(session->cipher_key.data);
23761f14d500SAkhil Goyal 	memset(session, 0, sizeof(dpaa_sec_session));
23771f14d500SAkhil Goyal 	return -1;
23781f14d500SAkhil Goyal }
23791f14d500SAkhil Goyal 
23801f14d500SAkhil Goyal static int
23811f14d500SAkhil Goyal dpaa_sec_security_session_create(void *dev,
23821f14d500SAkhil Goyal 				 struct rte_security_session_conf *conf,
23831f14d500SAkhil Goyal 				 struct rte_security_session *sess,
23841f14d500SAkhil Goyal 				 struct rte_mempool *mempool)
23851f14d500SAkhil Goyal {
23861f14d500SAkhil Goyal 	void *sess_private_data;
23871f14d500SAkhil Goyal 	struct rte_cryptodev *cdev = (struct rte_cryptodev *)dev;
23881f14d500SAkhil Goyal 	int ret;
23891f14d500SAkhil Goyal 
23901f14d500SAkhil Goyal 	if (rte_mempool_get(mempool, &sess_private_data)) {
2391f163231eSHemant Agrawal 		DPAA_SEC_ERR("Couldn't get object from session mempool");
23921f14d500SAkhil Goyal 		return -ENOMEM;
23931f14d500SAkhil Goyal 	}
23941f14d500SAkhil Goyal 
23951f14d500SAkhil Goyal 	switch (conf->protocol) {
23961f14d500SAkhil Goyal 	case RTE_SECURITY_PROTOCOL_IPSEC:
23971f14d500SAkhil Goyal 		ret = dpaa_sec_set_ipsec_session(cdev, conf,
23981f14d500SAkhil Goyal 				sess_private_data);
23991f14d500SAkhil Goyal 		break;
2400*a1173d55SHemant Agrawal 	case RTE_SECURITY_PROTOCOL_PDCP:
2401*a1173d55SHemant Agrawal 		ret = dpaa_sec_set_pdcp_session(cdev, conf,
2402*a1173d55SHemant Agrawal 				sess_private_data);
2403*a1173d55SHemant Agrawal 		break;
24041f14d500SAkhil Goyal 	case RTE_SECURITY_PROTOCOL_MACSEC:
24051f14d500SAkhil Goyal 		return -ENOTSUP;
24061f14d500SAkhil Goyal 	default:
24071f14d500SAkhil Goyal 		return -EINVAL;
24081f14d500SAkhil Goyal 	}
24091f14d500SAkhil Goyal 	if (ret != 0) {
2410f163231eSHemant Agrawal 		DPAA_SEC_ERR("failed to configure session parameters");
24111f14d500SAkhil Goyal 		/* Return session to mempool */
24121f14d500SAkhil Goyal 		rte_mempool_put(mempool, sess_private_data);
24131f14d500SAkhil Goyal 		return ret;
24141f14d500SAkhil Goyal 	}
24151f14d500SAkhil Goyal 
24161f14d500SAkhil Goyal 	set_sec_session_private_data(sess, sess_private_data);
24171f14d500SAkhil Goyal 
24181f14d500SAkhil Goyal 	return ret;
24191f14d500SAkhil Goyal }
24201f14d500SAkhil Goyal 
24211f14d500SAkhil Goyal /** Clear the memory of session so it doesn't leave key material behind */
24221f14d500SAkhil Goyal static int
24231f14d500SAkhil Goyal dpaa_sec_security_session_destroy(void *dev __rte_unused,
24241f14d500SAkhil Goyal 		struct rte_security_session *sess)
24251f14d500SAkhil Goyal {
24261f14d500SAkhil Goyal 	PMD_INIT_FUNC_TRACE();
24271f14d500SAkhil Goyal 	void *sess_priv = get_sec_session_private_data(sess);
24281f14d500SAkhil Goyal 
24291f14d500SAkhil Goyal 	dpaa_sec_session *s = (dpaa_sec_session *)sess_priv;
24301f14d500SAkhil Goyal 
24311f14d500SAkhil Goyal 	if (sess_priv) {
24321f14d500SAkhil Goyal 		struct rte_mempool *sess_mp = rte_mempool_from_obj(sess_priv);
24331f14d500SAkhil Goyal 
24341f14d500SAkhil Goyal 		rte_free(s->cipher_key.data);
24351f14d500SAkhil Goyal 		rte_free(s->auth_key.data);
24361f14d500SAkhil Goyal 		memset(sess, 0, sizeof(dpaa_sec_session));
24371f14d500SAkhil Goyal 		set_sec_session_private_data(sess, NULL);
24381f14d500SAkhil Goyal 		rte_mempool_put(sess_mp, sess_priv);
24391f14d500SAkhil Goyal 	}
24401f14d500SAkhil Goyal 	return 0;
24411f14d500SAkhil Goyal }
24421f14d500SAkhil Goyal 
24431f14d500SAkhil Goyal 
24441f14d500SAkhil Goyal static int
24457e3e2954SAkhil Goyal dpaa_sec_dev_configure(struct rte_cryptodev *dev,
2446c3e85bdcSAkhil Goyal 		       struct rte_cryptodev_config *config __rte_unused)
2447c3e85bdcSAkhil Goyal {
24487e3e2954SAkhil Goyal 
24497e3e2954SAkhil Goyal 	char str[20];
24507e3e2954SAkhil Goyal 	struct dpaa_sec_dev_private *internals;
24517e3e2954SAkhil Goyal 
2452c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2453c3e85bdcSAkhil Goyal 
24547e3e2954SAkhil Goyal 	internals = dev->data->dev_private;
24557e3e2954SAkhil Goyal 	sprintf(str, "ctx_pool_%d", dev->data->dev_id);
24567e3e2954SAkhil Goyal 	if (!internals->ctx_pool) {
24577e3e2954SAkhil Goyal 		internals->ctx_pool = rte_mempool_create((const char *)str,
24587e3e2954SAkhil Goyal 							CTX_POOL_NUM_BUFS,
24597e3e2954SAkhil Goyal 							CTX_POOL_BUF_SIZE,
24607e3e2954SAkhil Goyal 							CTX_POOL_CACHE_SIZE, 0,
24617e3e2954SAkhil Goyal 							NULL, NULL, NULL, NULL,
24627e3e2954SAkhil Goyal 							SOCKET_ID_ANY, 0);
24637e3e2954SAkhil Goyal 		if (!internals->ctx_pool) {
2464f163231eSHemant Agrawal 			DPAA_SEC_ERR("%s create failed\n", str);
24657e3e2954SAkhil Goyal 			return -ENOMEM;
24667e3e2954SAkhil Goyal 		}
24677e3e2954SAkhil Goyal 	} else
2468f163231eSHemant Agrawal 		DPAA_SEC_INFO("mempool already created for dev_id : %d",
24697e3e2954SAkhil Goyal 				dev->data->dev_id);
24707e3e2954SAkhil Goyal 
2471c3e85bdcSAkhil Goyal 	return 0;
2472c3e85bdcSAkhil Goyal }
2473c3e85bdcSAkhil Goyal 
2474c3e85bdcSAkhil Goyal static int
2475c3e85bdcSAkhil Goyal dpaa_sec_dev_start(struct rte_cryptodev *dev __rte_unused)
2476c3e85bdcSAkhil Goyal {
2477c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2478c3e85bdcSAkhil Goyal 	return 0;
2479c3e85bdcSAkhil Goyal }
2480c3e85bdcSAkhil Goyal 
2481c3e85bdcSAkhil Goyal static void
2482c3e85bdcSAkhil Goyal dpaa_sec_dev_stop(struct rte_cryptodev *dev __rte_unused)
2483c3e85bdcSAkhil Goyal {
2484c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2485c3e85bdcSAkhil Goyal }
2486c3e85bdcSAkhil Goyal 
2487c3e85bdcSAkhil Goyal static int
24887e3e2954SAkhil Goyal dpaa_sec_dev_close(struct rte_cryptodev *dev)
2489c3e85bdcSAkhil Goyal {
24907e3e2954SAkhil Goyal 	struct dpaa_sec_dev_private *internals;
24917e3e2954SAkhil Goyal 
2492c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
24937e3e2954SAkhil Goyal 
24947e3e2954SAkhil Goyal 	if (dev == NULL)
24957e3e2954SAkhil Goyal 		return -ENOMEM;
24967e3e2954SAkhil Goyal 
24977e3e2954SAkhil Goyal 	internals = dev->data->dev_private;
24987e3e2954SAkhil Goyal 	rte_mempool_free(internals->ctx_pool);
24997e3e2954SAkhil Goyal 	internals->ctx_pool = NULL;
25007e3e2954SAkhil Goyal 
2501c3e85bdcSAkhil Goyal 	return 0;
2502c3e85bdcSAkhil Goyal }
2503c3e85bdcSAkhil Goyal 
2504c3e85bdcSAkhil Goyal static void
2505c3e85bdcSAkhil Goyal dpaa_sec_dev_infos_get(struct rte_cryptodev *dev,
2506c3e85bdcSAkhil Goyal 		       struct rte_cryptodev_info *info)
2507c3e85bdcSAkhil Goyal {
2508c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals = dev->data->dev_private;
2509c3e85bdcSAkhil Goyal 
2510c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2511c3e85bdcSAkhil Goyal 	if (info != NULL) {
2512c3e85bdcSAkhil Goyal 		info->max_nb_queue_pairs = internals->max_nb_queue_pairs;
2513c3e85bdcSAkhil Goyal 		info->feature_flags = dev->feature_flags;
2514c3e85bdcSAkhil Goyal 		info->capabilities = dpaa_sec_capabilities;
2515c3e85bdcSAkhil Goyal 		info->sym.max_nb_sessions = internals->max_nb_sessions;
2516c3e85bdcSAkhil Goyal 		info->driver_id = cryptodev_driver_id;
2517c3e85bdcSAkhil Goyal 	}
2518c3e85bdcSAkhil Goyal }
2519c3e85bdcSAkhil Goyal 
2520c3e85bdcSAkhil Goyal static struct rte_cryptodev_ops crypto_ops = {
2521c3e85bdcSAkhil Goyal 	.dev_configure	      = dpaa_sec_dev_configure,
2522c3e85bdcSAkhil Goyal 	.dev_start	      = dpaa_sec_dev_start,
2523c3e85bdcSAkhil Goyal 	.dev_stop	      = dpaa_sec_dev_stop,
2524c3e85bdcSAkhil Goyal 	.dev_close	      = dpaa_sec_dev_close,
2525c3e85bdcSAkhil Goyal 	.dev_infos_get        = dpaa_sec_dev_infos_get,
2526c3e85bdcSAkhil Goyal 	.queue_pair_setup     = dpaa_sec_queue_pair_setup,
2527c3e85bdcSAkhil Goyal 	.queue_pair_release   = dpaa_sec_queue_pair_release,
2528c3e85bdcSAkhil Goyal 	.queue_pair_count     = dpaa_sec_queue_pair_count,
2529012c5076SPablo de Lara 	.sym_session_get_size     = dpaa_sec_sym_session_get_size,
2530012c5076SPablo de Lara 	.sym_session_configure    = dpaa_sec_sym_session_configure,
2531012c5076SPablo de Lara 	.sym_session_clear        = dpaa_sec_sym_session_clear
2532c3e85bdcSAkhil Goyal };
2533c3e85bdcSAkhil Goyal 
25341f14d500SAkhil Goyal static const struct rte_security_capability *
25351f14d500SAkhil Goyal dpaa_sec_capabilities_get(void *device __rte_unused)
25361f14d500SAkhil Goyal {
25371f14d500SAkhil Goyal 	return dpaa_sec_security_cap;
25381f14d500SAkhil Goyal }
25391f14d500SAkhil Goyal 
2540b74fd6b8SFerruh Yigit static const struct rte_security_ops dpaa_sec_security_ops = {
25411f14d500SAkhil Goyal 	.session_create = dpaa_sec_security_session_create,
25421f14d500SAkhil Goyal 	.session_update = NULL,
25431f14d500SAkhil Goyal 	.session_stats_get = NULL,
25441f14d500SAkhil Goyal 	.session_destroy = dpaa_sec_security_session_destroy,
25451f14d500SAkhil Goyal 	.set_pkt_metadata = NULL,
25461f14d500SAkhil Goyal 	.capabilities_get = dpaa_sec_capabilities_get
25471f14d500SAkhil Goyal };
25481f14d500SAkhil Goyal 
2549c3e85bdcSAkhil Goyal static int
2550c3e85bdcSAkhil Goyal dpaa_sec_uninit(struct rte_cryptodev *dev)
2551c3e85bdcSAkhil Goyal {
2552debef417SShreyansh Jain 	struct dpaa_sec_dev_private *internals;
2553c3e85bdcSAkhil Goyal 
2554c3e85bdcSAkhil Goyal 	if (dev == NULL)
2555c3e85bdcSAkhil Goyal 		return -ENODEV;
2556c3e85bdcSAkhil Goyal 
2557debef417SShreyansh Jain 	internals = dev->data->dev_private;
25581f14d500SAkhil Goyal 	rte_free(dev->security_ctx);
25591f14d500SAkhil Goyal 
25607e3e2954SAkhil Goyal 	/* In case close has been called, internals->ctx_pool would be NULL */
2561c3e85bdcSAkhil Goyal 	rte_mempool_free(internals->ctx_pool);
2562c3e85bdcSAkhil Goyal 	rte_free(internals);
2563c3e85bdcSAkhil Goyal 
2564f163231eSHemant Agrawal 	DPAA_SEC_INFO("Closing DPAA_SEC device %s on numa socket %u",
2565c3e85bdcSAkhil Goyal 		      dev->data->name, rte_socket_id());
2566c3e85bdcSAkhil Goyal 
2567c3e85bdcSAkhil Goyal 	return 0;
2568c3e85bdcSAkhil Goyal }
2569c3e85bdcSAkhil Goyal 
2570c3e85bdcSAkhil Goyal static int
2571c3e85bdcSAkhil Goyal dpaa_sec_dev_init(struct rte_cryptodev *cryptodev)
2572c3e85bdcSAkhil Goyal {
2573c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals;
25741f14d500SAkhil Goyal 	struct rte_security_ctx *security_instance;
2575c3e85bdcSAkhil Goyal 	struct dpaa_sec_qp *qp;
2576e79416d1SHemant Agrawal 	uint32_t i, flags;
2577c3e85bdcSAkhil Goyal 	int ret;
2578c3e85bdcSAkhil Goyal 
2579c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2580c3e85bdcSAkhil Goyal 
2581c3e85bdcSAkhil Goyal 	cryptodev->driver_id = cryptodev_driver_id;
2582c3e85bdcSAkhil Goyal 	cryptodev->dev_ops = &crypto_ops;
2583c3e85bdcSAkhil Goyal 
2584c3e85bdcSAkhil Goyal 	cryptodev->enqueue_burst = dpaa_sec_enqueue_burst;
2585c3e85bdcSAkhil Goyal 	cryptodev->dequeue_burst = dpaa_sec_dequeue_burst;
2586c3e85bdcSAkhil Goyal 	cryptodev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
2587c3e85bdcSAkhil Goyal 			RTE_CRYPTODEV_FF_HW_ACCELERATED |
25881f14d500SAkhil Goyal 			RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
2589a74af788SAkhil Goyal 			RTE_CRYPTODEV_FF_SECURITY |
25902717246eSPablo de Lara 			RTE_CRYPTODEV_FF_IN_PLACE_SGL |
25912717246eSPablo de Lara 			RTE_CRYPTODEV_FF_OOP_SGL_IN_SGL_OUT |
25922717246eSPablo de Lara 			RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT |
25932717246eSPablo de Lara 			RTE_CRYPTODEV_FF_OOP_LB_IN_SGL_OUT |
25942717246eSPablo de Lara 			RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT;
2595c3e85bdcSAkhil Goyal 
2596c3e85bdcSAkhil Goyal 	internals = cryptodev->data->dev_private;
2597e79416d1SHemant Agrawal 	internals->max_nb_queue_pairs = RTE_DPAA_MAX_NB_SEC_QPS;
2598c3e85bdcSAkhil Goyal 	internals->max_nb_sessions = RTE_DPAA_SEC_PMD_MAX_NB_SESSIONS;
2599c3e85bdcSAkhil Goyal 
26001f14d500SAkhil Goyal 	/*
26011f14d500SAkhil Goyal 	 * For secondary processes, we don't initialise any further as primary
26021f14d500SAkhil Goyal 	 * has already done this work. Only check we don't need a different
26031f14d500SAkhil Goyal 	 * RX function
26041f14d500SAkhil Goyal 	 */
26051f14d500SAkhil Goyal 	if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
2606f163231eSHemant Agrawal 		DPAA_SEC_WARN("Device already init by primary process");
26071f14d500SAkhil Goyal 		return 0;
26081f14d500SAkhil Goyal 	}
26091f14d500SAkhil Goyal 
26101f14d500SAkhil Goyal 	/* Initialize security_ctx only for primary process*/
26111f14d500SAkhil Goyal 	security_instance = rte_malloc("rte_security_instances_ops",
26121f14d500SAkhil Goyal 				sizeof(struct rte_security_ctx), 0);
26131f14d500SAkhil Goyal 	if (security_instance == NULL)
26141f14d500SAkhil Goyal 		return -ENOMEM;
26151f14d500SAkhil Goyal 	security_instance->device = (void *)cryptodev;
26161f14d500SAkhil Goyal 	security_instance->ops = &dpaa_sec_security_ops;
26171f14d500SAkhil Goyal 	security_instance->sess_cnt = 0;
26181f14d500SAkhil Goyal 	cryptodev->security_ctx = security_instance;
26191f14d500SAkhil Goyal 
26203b617ee7SAkhil Goyal 	rte_spinlock_init(&internals->lock);
2621c3e85bdcSAkhil Goyal 	for (i = 0; i < internals->max_nb_queue_pairs; i++) {
2622c3e85bdcSAkhil Goyal 		/* init qman fq for queue pair */
2623c3e85bdcSAkhil Goyal 		qp = &internals->qps[i];
2624c3e85bdcSAkhil Goyal 		ret = dpaa_sec_init_tx(&qp->outq);
2625c3e85bdcSAkhil Goyal 		if (ret) {
2626f163231eSHemant Agrawal 			DPAA_SEC_ERR("config tx of queue pair  %d", i);
2627c3e85bdcSAkhil Goyal 			goto init_error;
2628c3e85bdcSAkhil Goyal 		}
2629e79416d1SHemant Agrawal 	}
2630e79416d1SHemant Agrawal 
2631e79416d1SHemant Agrawal 	flags = QMAN_FQ_FLAG_LOCKED | QMAN_FQ_FLAG_DYNAMIC_FQID |
2632e79416d1SHemant Agrawal 		QMAN_FQ_FLAG_TO_DCPORTAL;
2633e79416d1SHemant Agrawal 	for (i = 0; i < internals->max_nb_sessions; i++) {
2634e79416d1SHemant Agrawal 		/* create rx qman fq for sessions*/
2635e79416d1SHemant Agrawal 		ret = qman_create_fq(0, flags, &internals->inq[i]);
2636e79416d1SHemant Agrawal 		if (unlikely(ret != 0)) {
2637f163231eSHemant Agrawal 			DPAA_SEC_ERR("sec qman_create_fq failed");
2638c3e85bdcSAkhil Goyal 			goto init_error;
2639c3e85bdcSAkhil Goyal 		}
2640c3e85bdcSAkhil Goyal 	}
2641c3e85bdcSAkhil Goyal 
2642f163231eSHemant Agrawal 	RTE_LOG(INFO, PMD, "%s cryptodev init\n", cryptodev->data->name);
2643c3e85bdcSAkhil Goyal 	return 0;
2644c3e85bdcSAkhil Goyal 
2645c3e85bdcSAkhil Goyal init_error:
2646f163231eSHemant Agrawal 	DPAA_SEC_ERR("driver %s: create failed\n", cryptodev->data->name);
2647c3e85bdcSAkhil Goyal 
2648c3e85bdcSAkhil Goyal 	dpaa_sec_uninit(cryptodev);
2649c3e85bdcSAkhil Goyal 	return -EFAULT;
2650c3e85bdcSAkhil Goyal }
2651c3e85bdcSAkhil Goyal 
2652c3e85bdcSAkhil Goyal static int
2653eb5a9a76SShreyansh Jain cryptodev_dpaa_sec_probe(struct rte_dpaa_driver *dpaa_drv __rte_unused,
2654c3e85bdcSAkhil Goyal 				struct rte_dpaa_device *dpaa_dev)
2655c3e85bdcSAkhil Goyal {
2656c3e85bdcSAkhil Goyal 	struct rte_cryptodev *cryptodev;
2657c3e85bdcSAkhil Goyal 	char cryptodev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
2658c3e85bdcSAkhil Goyal 
2659c3e85bdcSAkhil Goyal 	int retval;
2660c3e85bdcSAkhil Goyal 
2661c3e85bdcSAkhil Goyal 	sprintf(cryptodev_name, "dpaa_sec-%d", dpaa_dev->id.dev_id);
2662c3e85bdcSAkhil Goyal 
2663c3e85bdcSAkhil Goyal 	cryptodev = rte_cryptodev_pmd_allocate(cryptodev_name, rte_socket_id());
2664c3e85bdcSAkhil Goyal 	if (cryptodev == NULL)
2665c3e85bdcSAkhil Goyal 		return -ENOMEM;
2666c3e85bdcSAkhil Goyal 
2667c3e85bdcSAkhil Goyal 	if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
2668c3e85bdcSAkhil Goyal 		cryptodev->data->dev_private = rte_zmalloc_socket(
2669c3e85bdcSAkhil Goyal 					"cryptodev private structure",
2670c3e85bdcSAkhil Goyal 					sizeof(struct dpaa_sec_dev_private),
2671c3e85bdcSAkhil Goyal 					RTE_CACHE_LINE_SIZE,
2672c3e85bdcSAkhil Goyal 					rte_socket_id());
2673c3e85bdcSAkhil Goyal 
2674c3e85bdcSAkhil Goyal 		if (cryptodev->data->dev_private == NULL)
2675c3e85bdcSAkhil Goyal 			rte_panic("Cannot allocate memzone for private "
2676c3e85bdcSAkhil Goyal 					"device data");
2677c3e85bdcSAkhil Goyal 	}
2678c3e85bdcSAkhil Goyal 
2679c3e85bdcSAkhil Goyal 	dpaa_dev->crypto_dev = cryptodev;
2680c3e85bdcSAkhil Goyal 	cryptodev->device = &dpaa_dev->device;
2681c3e85bdcSAkhil Goyal 
2682c3e85bdcSAkhil Goyal 	/* init user callbacks */
2683c3e85bdcSAkhil Goyal 	TAILQ_INIT(&(cryptodev->link_intr_cbs));
2684c3e85bdcSAkhil Goyal 
2685c3e85bdcSAkhil Goyal 	/* if sec device version is not configured */
2686c3e85bdcSAkhil Goyal 	if (!rta_get_sec_era()) {
2687c3e85bdcSAkhil Goyal 		const struct device_node *caam_node;
2688c3e85bdcSAkhil Goyal 
2689c3e85bdcSAkhil Goyal 		for_each_compatible_node(caam_node, NULL, "fsl,sec-v4.0") {
2690c3e85bdcSAkhil Goyal 			const uint32_t *prop = of_get_property(caam_node,
2691c3e85bdcSAkhil Goyal 					"fsl,sec-era",
2692c3e85bdcSAkhil Goyal 					NULL);
2693c3e85bdcSAkhil Goyal 			if (prop) {
2694c3e85bdcSAkhil Goyal 				rta_set_sec_era(
2695c3e85bdcSAkhil Goyal 					INTL_SEC_ERA(rte_cpu_to_be_32(*prop)));
2696c3e85bdcSAkhil Goyal 				break;
2697c3e85bdcSAkhil Goyal 			}
2698c3e85bdcSAkhil Goyal 		}
2699c3e85bdcSAkhil Goyal 	}
2700c3e85bdcSAkhil Goyal 
2701c3e85bdcSAkhil Goyal 	/* Invoke PMD device initialization function */
2702c3e85bdcSAkhil Goyal 	retval = dpaa_sec_dev_init(cryptodev);
2703c3e85bdcSAkhil Goyal 	if (retval == 0)
2704c3e85bdcSAkhil Goyal 		return 0;
2705c3e85bdcSAkhil Goyal 
2706c3e85bdcSAkhil Goyal 	/* In case of error, cleanup is done */
2707c3e85bdcSAkhil Goyal 	if (rte_eal_process_type() == RTE_PROC_PRIMARY)
2708c3e85bdcSAkhil Goyal 		rte_free(cryptodev->data->dev_private);
2709c3e85bdcSAkhil Goyal 
2710c3e85bdcSAkhil Goyal 	rte_cryptodev_pmd_release_device(cryptodev);
2711c3e85bdcSAkhil Goyal 
2712c3e85bdcSAkhil Goyal 	return -ENXIO;
2713c3e85bdcSAkhil Goyal }
2714c3e85bdcSAkhil Goyal 
2715c3e85bdcSAkhil Goyal static int
2716c3e85bdcSAkhil Goyal cryptodev_dpaa_sec_remove(struct rte_dpaa_device *dpaa_dev)
2717c3e85bdcSAkhil Goyal {
2718c3e85bdcSAkhil Goyal 	struct rte_cryptodev *cryptodev;
2719c3e85bdcSAkhil Goyal 	int ret;
2720c3e85bdcSAkhil Goyal 
2721c3e85bdcSAkhil Goyal 	cryptodev = dpaa_dev->crypto_dev;
2722c3e85bdcSAkhil Goyal 	if (cryptodev == NULL)
2723c3e85bdcSAkhil Goyal 		return -ENODEV;
2724c3e85bdcSAkhil Goyal 
2725c3e85bdcSAkhil Goyal 	ret = dpaa_sec_uninit(cryptodev);
2726c3e85bdcSAkhil Goyal 	if (ret)
2727c3e85bdcSAkhil Goyal 		return ret;
2728c3e85bdcSAkhil Goyal 
2729f2f020d2SDeclan Doherty 	return rte_cryptodev_pmd_destroy(cryptodev);
2730c3e85bdcSAkhil Goyal }
2731c3e85bdcSAkhil Goyal 
2732c3e85bdcSAkhil Goyal static struct rte_dpaa_driver rte_dpaa_sec_driver = {
2733c3e85bdcSAkhil Goyal 	.drv_type = FSL_DPAA_CRYPTO,
2734c3e85bdcSAkhil Goyal 	.driver = {
2735c3e85bdcSAkhil Goyal 		.name = "DPAA SEC PMD"
2736c3e85bdcSAkhil Goyal 	},
2737c3e85bdcSAkhil Goyal 	.probe = cryptodev_dpaa_sec_probe,
2738c3e85bdcSAkhil Goyal 	.remove = cryptodev_dpaa_sec_remove,
2739c3e85bdcSAkhil Goyal };
2740c3e85bdcSAkhil Goyal 
2741c3e85bdcSAkhil Goyal static struct cryptodev_driver dpaa_sec_crypto_drv;
2742c3e85bdcSAkhil Goyal 
2743c3e85bdcSAkhil Goyal RTE_PMD_REGISTER_DPAA(CRYPTODEV_NAME_DPAA_SEC_PMD, rte_dpaa_sec_driver);
2744f737f5ceSFiona Trahe RTE_PMD_REGISTER_CRYPTO_DRIVER(dpaa_sec_crypto_drv, rte_dpaa_sec_driver.driver,
2745c3e85bdcSAkhil Goyal 		cryptodev_driver_id);
2746f163231eSHemant Agrawal 
2747f8e99896SThomas Monjalon RTE_INIT(dpaa_sec_init_log)
2748f163231eSHemant Agrawal {
2749f163231eSHemant Agrawal 	dpaa_logtype_sec = rte_log_register("pmd.crypto.dpaa");
2750f163231eSHemant Agrawal 	if (dpaa_logtype_sec >= 0)
2751f163231eSHemant Agrawal 		rte_log_set_level(dpaa_logtype_sec, RTE_LOG_NOTICE);
2752f163231eSHemant Agrawal }
2753