xref: /dpdk/drivers/crypto/dpaa_sec/dpaa_sec.c (revision 6a0c9d364afcb0b0ca445efb12a5f6fb65fd1ae2)
1d81734caSHemant Agrawal /* SPDX-License-Identifier: BSD-3-Clause
2c3e85bdcSAkhil Goyal  *
3c3e85bdcSAkhil Goyal  *   Copyright (c) 2016 Freescale Semiconductor, Inc. All rights reserved.
4e621d970SAkhil Goyal  *   Copyright 2017-2019 NXP
5c3e85bdcSAkhil Goyal  *
6c3e85bdcSAkhil Goyal  */
7c3e85bdcSAkhil Goyal 
8c3e85bdcSAkhil Goyal #include <fcntl.h>
9c3e85bdcSAkhil Goyal #include <unistd.h>
10c3e85bdcSAkhil Goyal #include <sched.h>
11c3e85bdcSAkhil Goyal #include <net/if.h>
12c3e85bdcSAkhil Goyal 
13c3e85bdcSAkhil Goyal #include <rte_byteorder.h>
14c3e85bdcSAkhil Goyal #include <rte_common.h>
15c3e85bdcSAkhil Goyal #include <rte_cryptodev_pmd.h>
16c3e85bdcSAkhil Goyal #include <rte_crypto.h>
17c3e85bdcSAkhil Goyal #include <rte_cryptodev.h>
181f14d500SAkhil Goyal #include <rte_security_driver.h>
19c3e85bdcSAkhil Goyal #include <rte_cycles.h>
20c3e85bdcSAkhil Goyal #include <rte_dev.h>
21c3e85bdcSAkhil Goyal #include <rte_kvargs.h>
22c3e85bdcSAkhil Goyal #include <rte_malloc.h>
23c3e85bdcSAkhil Goyal #include <rte_mbuf.h>
24c3e85bdcSAkhil Goyal #include <rte_memcpy.h>
25c3e85bdcSAkhil Goyal #include <rte_string_fns.h>
263b617ee7SAkhil Goyal #include <rte_spinlock.h>
27c3e85bdcSAkhil Goyal 
28c3e85bdcSAkhil Goyal #include <fsl_usd.h>
29c3e85bdcSAkhil Goyal #include <fsl_qman.h>
30c3e85bdcSAkhil Goyal #include <of.h>
31c3e85bdcSAkhil Goyal 
32c3e85bdcSAkhil Goyal /* RTA header files */
33c3e85bdcSAkhil Goyal #include <hw/desc/common.h>
34c3e85bdcSAkhil Goyal #include <hw/desc/algo.h>
35c3e85bdcSAkhil Goyal #include <hw/desc/ipsec.h>
36a1173d55SHemant Agrawal #include <hw/desc/pdcp.h>
37c3e85bdcSAkhil Goyal 
38c3e85bdcSAkhil Goyal #include <rte_dpaa_bus.h>
39c3e85bdcSAkhil Goyal #include <dpaa_sec.h>
40c3e85bdcSAkhil Goyal #include <dpaa_sec_log.h>
4112e58429SThierry Herbelot #include <dpaax_iova_table.h>
42c3e85bdcSAkhil Goyal 
43c3e85bdcSAkhil Goyal enum rta_sec_era rta_sec_era;
44c3e85bdcSAkhil Goyal 
45f163231eSHemant Agrawal int dpaa_logtype_sec;
46f163231eSHemant Agrawal 
47c3e85bdcSAkhil Goyal static uint8_t cryptodev_driver_id;
48c3e85bdcSAkhil Goyal 
49c3e85bdcSAkhil Goyal static __thread struct rte_crypto_op **dpaa_sec_ops;
50c3e85bdcSAkhil Goyal static __thread int dpaa_sec_op_nb;
51c3e85bdcSAkhil Goyal 
52e79416d1SHemant Agrawal static int
53e79416d1SHemant Agrawal dpaa_sec_attach_sess_q(struct dpaa_sec_qp *qp, dpaa_sec_session *sess);
54e79416d1SHemant Agrawal 
55c3e85bdcSAkhil Goyal static inline void
56c3e85bdcSAkhil Goyal dpaa_sec_op_ending(struct dpaa_sec_op_ctx *ctx)
57c3e85bdcSAkhil Goyal {
58c3e85bdcSAkhil Goyal 	if (!ctx->fd_status) {
59c3e85bdcSAkhil Goyal 		ctx->op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
60c3e85bdcSAkhil Goyal 	} else {
61f163231eSHemant Agrawal 		DPAA_SEC_DP_WARN("SEC return err: 0x%x", ctx->fd_status);
62c3e85bdcSAkhil Goyal 		ctx->op->status = RTE_CRYPTO_OP_STATUS_ERROR;
63c3e85bdcSAkhil Goyal 	}
64c3e85bdcSAkhil Goyal 
65a1f42a9dSThierry Herbelot 	/* report op status to sym->op and then free the ctx memory  */
66c3e85bdcSAkhil Goyal 	rte_mempool_put(ctx->ctx_pool, (void *)ctx);
67c3e85bdcSAkhil Goyal }
68c3e85bdcSAkhil Goyal 
69c3e85bdcSAkhil Goyal static inline struct dpaa_sec_op_ctx *
70c3e85bdcSAkhil Goyal dpaa_sec_alloc_ctx(dpaa_sec_session *ses)
71c3e85bdcSAkhil Goyal {
72c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
73c3e85bdcSAkhil Goyal 	int retval;
74c3e85bdcSAkhil Goyal 
75c3e85bdcSAkhil Goyal 	retval = rte_mempool_get(ses->ctx_pool, (void **)(&ctx));
76c3e85bdcSAkhil Goyal 	if (!ctx || retval) {
77f163231eSHemant Agrawal 		DPAA_SEC_DP_WARN("Alloc sec descriptor failed!");
78c3e85bdcSAkhil Goyal 		return NULL;
79c3e85bdcSAkhil Goyal 	}
80c3e85bdcSAkhil Goyal 	/*
81c3e85bdcSAkhil Goyal 	 * Clear SG memory. There are 16 SG entries of 16 Bytes each.
82c3e85bdcSAkhil Goyal 	 * one call to dcbz_64() clear 64 bytes, hence calling it 4 times
83c3e85bdcSAkhil Goyal 	 * to clear all the SG entries. dpaa_sec_alloc_ctx() is called for
84c3e85bdcSAkhil Goyal 	 * each packet, memset is costlier than dcbz_64().
85c3e85bdcSAkhil Goyal 	 */
86c3e85bdcSAkhil Goyal 	dcbz_64(&ctx->job.sg[SG_CACHELINE_0]);
87c3e85bdcSAkhil Goyal 	dcbz_64(&ctx->job.sg[SG_CACHELINE_1]);
88c3e85bdcSAkhil Goyal 	dcbz_64(&ctx->job.sg[SG_CACHELINE_2]);
89c3e85bdcSAkhil Goyal 	dcbz_64(&ctx->job.sg[SG_CACHELINE_3]);
90c3e85bdcSAkhil Goyal 
91c3e85bdcSAkhil Goyal 	ctx->ctx_pool = ses->ctx_pool;
920e5607e4SHemant Agrawal 	ctx->vtop_offset = (size_t) ctx
93fcf67029SHemant Agrawal 				- rte_mempool_virt2iova(ctx);
94c3e85bdcSAkhil Goyal 
95c3e85bdcSAkhil Goyal 	return ctx;
96c3e85bdcSAkhil Goyal }
97c3e85bdcSAkhil Goyal 
98c4509373SSantosh Shukla static inline rte_iova_t
99c3e85bdcSAkhil Goyal dpaa_mem_vtop(void *vaddr)
100c3e85bdcSAkhil Goyal {
10129f3c9e5SAnatoly Burakov 	const struct rte_memseg *ms;
102c3e85bdcSAkhil Goyal 
10366cc45e2SAnatoly Burakov 	ms = rte_mem_virt2memseg(vaddr, NULL);
10412e58429SThierry Herbelot 	if (ms) {
10512e58429SThierry Herbelot 		dpaax_iova_table_update(ms->iova, ms->addr, ms->len);
10629f3c9e5SAnatoly Burakov 		return ms->iova + RTE_PTR_DIFF(vaddr, ms->addr);
10712e58429SThierry Herbelot 	}
1080e5607e4SHemant Agrawal 	return (size_t)NULL;
109c3e85bdcSAkhil Goyal }
110c3e85bdcSAkhil Goyal 
111c3e85bdcSAkhil Goyal static inline void *
112c4509373SSantosh Shukla dpaa_mem_ptov(rte_iova_t paddr)
113c3e85bdcSAkhil Goyal {
1145a7dbb93SShreyansh Jain 	void *va;
1155a7dbb93SShreyansh Jain 
1165a7dbb93SShreyansh Jain 	va = (void *)dpaax_iova_table_get_va(paddr);
1175a7dbb93SShreyansh Jain 	if (likely(va))
1185a7dbb93SShreyansh Jain 		return va;
1195a7dbb93SShreyansh Jain 
12011d2f002SAnatoly Burakov 	return rte_mem_iova2virt(paddr);
121c3e85bdcSAkhil Goyal }
122c3e85bdcSAkhil Goyal 
123c3e85bdcSAkhil Goyal static void
124c3e85bdcSAkhil Goyal ern_sec_fq_handler(struct qman_portal *qm __rte_unused,
125c3e85bdcSAkhil Goyal 		   struct qman_fq *fq,
126c3e85bdcSAkhil Goyal 		   const struct qm_mr_entry *msg)
127c3e85bdcSAkhil Goyal {
128f163231eSHemant Agrawal 	DPAA_SEC_DP_ERR("sec fq %d error, RC = %x, seqnum = %x\n",
129c3e85bdcSAkhil Goyal 			fq->fqid, msg->ern.rc, msg->ern.seqnum);
130c3e85bdcSAkhil Goyal }
131c3e85bdcSAkhil Goyal 
132c3e85bdcSAkhil Goyal /* initialize the queue with dest chan as caam chan so that
133c3e85bdcSAkhil Goyal  * all the packets in this queue could be dispatched into caam
134c3e85bdcSAkhil Goyal  */
135c3e85bdcSAkhil Goyal static int
136c4509373SSantosh Shukla dpaa_sec_init_rx(struct qman_fq *fq_in, rte_iova_t hwdesc,
137c3e85bdcSAkhil Goyal 		 uint32_t fqid_out)
138c3e85bdcSAkhil Goyal {
139c3e85bdcSAkhil Goyal 	struct qm_mcc_initfq fq_opts;
140c3e85bdcSAkhil Goyal 	uint32_t flags;
141c3e85bdcSAkhil Goyal 	int ret = -1;
142c3e85bdcSAkhil Goyal 
143c3e85bdcSAkhil Goyal 	/* Clear FQ options */
144c3e85bdcSAkhil Goyal 	memset(&fq_opts, 0x00, sizeof(struct qm_mcc_initfq));
145c3e85bdcSAkhil Goyal 
146c3e85bdcSAkhil Goyal 	flags = QMAN_INITFQ_FLAG_SCHED;
147c3e85bdcSAkhil Goyal 	fq_opts.we_mask = QM_INITFQ_WE_DESTWQ | QM_INITFQ_WE_CONTEXTA |
148c3e85bdcSAkhil Goyal 			  QM_INITFQ_WE_CONTEXTB;
149c3e85bdcSAkhil Goyal 
150c3e85bdcSAkhil Goyal 	qm_fqd_context_a_set64(&fq_opts.fqd, hwdesc);
151c3e85bdcSAkhil Goyal 	fq_opts.fqd.context_b = fqid_out;
152c3e85bdcSAkhil Goyal 	fq_opts.fqd.dest.channel = qm_channel_caam;
153c3e85bdcSAkhil Goyal 	fq_opts.fqd.dest.wq = 0;
154c3e85bdcSAkhil Goyal 
155c3e85bdcSAkhil Goyal 	fq_in->cb.ern  = ern_sec_fq_handler;
156c3e85bdcSAkhil Goyal 
157f163231eSHemant Agrawal 	DPAA_SEC_DEBUG("in-%x out-%x", fq_in->fqid, fqid_out);
158e79416d1SHemant Agrawal 
159c3e85bdcSAkhil Goyal 	ret = qman_init_fq(fq_in, flags, &fq_opts);
160c3e85bdcSAkhil Goyal 	if (unlikely(ret != 0))
161f163231eSHemant Agrawal 		DPAA_SEC_ERR("qman_init_fq failed %d", ret);
162c3e85bdcSAkhil Goyal 
163c3e85bdcSAkhil Goyal 	return ret;
164c3e85bdcSAkhil Goyal }
165c3e85bdcSAkhil Goyal 
166c3e85bdcSAkhil Goyal /* something is put into in_fq and caam put the crypto result into out_fq */
167c3e85bdcSAkhil Goyal static enum qman_cb_dqrr_result
168c3e85bdcSAkhil Goyal dqrr_out_fq_cb_rx(struct qman_portal *qm __always_unused,
169c3e85bdcSAkhil Goyal 		  struct qman_fq *fq __always_unused,
170c3e85bdcSAkhil Goyal 		  const struct qm_dqrr_entry *dqrr)
171c3e85bdcSAkhil Goyal {
172c3e85bdcSAkhil Goyal 	const struct qm_fd *fd;
173c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *job;
174c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
175c3e85bdcSAkhil Goyal 
176c3e85bdcSAkhil Goyal 	if (dpaa_sec_op_nb >= DPAA_SEC_BURST)
177c3e85bdcSAkhil Goyal 		return qman_cb_dqrr_defer;
178c3e85bdcSAkhil Goyal 
179c3e85bdcSAkhil Goyal 	if (!(dqrr->stat & QM_DQRR_STAT_FD_VALID))
180c3e85bdcSAkhil Goyal 		return qman_cb_dqrr_consume;
181c3e85bdcSAkhil Goyal 
182c3e85bdcSAkhil Goyal 	fd = &dqrr->fd;
183c3e85bdcSAkhil Goyal 	/* sg is embedded in an op ctx,
184c3e85bdcSAkhil Goyal 	 * sg[0] is for output
185c3e85bdcSAkhil Goyal 	 * sg[1] for input
186c3e85bdcSAkhil Goyal 	 */
187c3e85bdcSAkhil Goyal 	job = dpaa_mem_ptov(qm_fd_addr_get64(fd));
1881f14d500SAkhil Goyal 
189c3e85bdcSAkhil Goyal 	ctx = container_of(job, struct dpaa_sec_op_ctx, job);
190c3e85bdcSAkhil Goyal 	ctx->fd_status = fd->status;
1911f14d500SAkhil Goyal 	if (ctx->op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
1921f14d500SAkhil Goyal 		struct qm_sg_entry *sg_out;
1931f14d500SAkhil Goyal 		uint32_t len;
1941f14d500SAkhil Goyal 
1951f14d500SAkhil Goyal 		sg_out = &job->sg[0];
1961f14d500SAkhil Goyal 		hw_sg_to_cpu(sg_out);
1971f14d500SAkhil Goyal 		len = sg_out->length;
1981f14d500SAkhil Goyal 		ctx->op->sym->m_src->pkt_len = len;
1991f14d500SAkhil Goyal 		ctx->op->sym->m_src->data_len = len;
2001f14d500SAkhil Goyal 	}
201c3e85bdcSAkhil Goyal 	dpaa_sec_ops[dpaa_sec_op_nb++] = ctx->op;
202c3e85bdcSAkhil Goyal 	dpaa_sec_op_ending(ctx);
203c3e85bdcSAkhil Goyal 
204c3e85bdcSAkhil Goyal 	return qman_cb_dqrr_consume;
205c3e85bdcSAkhil Goyal }
206c3e85bdcSAkhil Goyal 
207c3e85bdcSAkhil Goyal /* caam result is put into this queue */
208c3e85bdcSAkhil Goyal static int
209c3e85bdcSAkhil Goyal dpaa_sec_init_tx(struct qman_fq *fq)
210c3e85bdcSAkhil Goyal {
211c3e85bdcSAkhil Goyal 	int ret;
212c3e85bdcSAkhil Goyal 	struct qm_mcc_initfq opts;
213c3e85bdcSAkhil Goyal 	uint32_t flags;
214c3e85bdcSAkhil Goyal 
215c3e85bdcSAkhil Goyal 	flags = QMAN_FQ_FLAG_NO_ENQUEUE | QMAN_FQ_FLAG_LOCKED |
216c3e85bdcSAkhil Goyal 		QMAN_FQ_FLAG_DYNAMIC_FQID;
217c3e85bdcSAkhil Goyal 
218c3e85bdcSAkhil Goyal 	ret = qman_create_fq(0, flags, fq);
219c3e85bdcSAkhil Goyal 	if (unlikely(ret)) {
220f163231eSHemant Agrawal 		DPAA_SEC_ERR("qman_create_fq failed");
221c3e85bdcSAkhil Goyal 		return ret;
222c3e85bdcSAkhil Goyal 	}
223c3e85bdcSAkhil Goyal 
224c3e85bdcSAkhil Goyal 	memset(&opts, 0, sizeof(opts));
225c3e85bdcSAkhil Goyal 	opts.we_mask = QM_INITFQ_WE_DESTWQ | QM_INITFQ_WE_FQCTRL |
226c3e85bdcSAkhil Goyal 		       QM_INITFQ_WE_CONTEXTA | QM_INITFQ_WE_CONTEXTB;
227c3e85bdcSAkhil Goyal 
228c3e85bdcSAkhil Goyal 	/* opts.fqd.dest.channel = dpaa_sec_pool_chan; */
229c3e85bdcSAkhil Goyal 
230c3e85bdcSAkhil Goyal 	fq->cb.dqrr = dqrr_out_fq_cb_rx;
231c3e85bdcSAkhil Goyal 	fq->cb.ern  = ern_sec_fq_handler;
232c3e85bdcSAkhil Goyal 
233c3e85bdcSAkhil Goyal 	ret = qman_init_fq(fq, 0, &opts);
234c3e85bdcSAkhil Goyal 	if (unlikely(ret)) {
235f163231eSHemant Agrawal 		DPAA_SEC_ERR("unable to init caam source fq!");
236c3e85bdcSAkhil Goyal 		return ret;
237c3e85bdcSAkhil Goyal 	}
238c3e85bdcSAkhil Goyal 
239c3e85bdcSAkhil Goyal 	return ret;
240c3e85bdcSAkhil Goyal }
241c3e85bdcSAkhil Goyal 
242c3e85bdcSAkhil Goyal static inline int is_cipher_only(dpaa_sec_session *ses)
243c3e85bdcSAkhil Goyal {
244c3e85bdcSAkhil Goyal 	return ((ses->cipher_alg != RTE_CRYPTO_CIPHER_NULL) &&
245c3e85bdcSAkhil Goyal 		(ses->auth_alg == RTE_CRYPTO_AUTH_NULL));
246c3e85bdcSAkhil Goyal }
247c3e85bdcSAkhil Goyal 
248c3e85bdcSAkhil Goyal static inline int is_auth_only(dpaa_sec_session *ses)
249c3e85bdcSAkhil Goyal {
250c3e85bdcSAkhil Goyal 	return ((ses->cipher_alg == RTE_CRYPTO_CIPHER_NULL) &&
251c3e85bdcSAkhil Goyal 		(ses->auth_alg != RTE_CRYPTO_AUTH_NULL));
252c3e85bdcSAkhil Goyal }
253c3e85bdcSAkhil Goyal 
254c3e85bdcSAkhil Goyal static inline int is_aead(dpaa_sec_session *ses)
255c3e85bdcSAkhil Goyal {
256c3e85bdcSAkhil Goyal 	return ((ses->cipher_alg == 0) &&
257c3e85bdcSAkhil Goyal 		(ses->auth_alg == 0) &&
258c3e85bdcSAkhil Goyal 		(ses->aead_alg != 0));
259c3e85bdcSAkhil Goyal }
260c3e85bdcSAkhil Goyal 
261c3e85bdcSAkhil Goyal static inline int is_auth_cipher(dpaa_sec_session *ses)
262c3e85bdcSAkhil Goyal {
263c3e85bdcSAkhil Goyal 	return ((ses->cipher_alg != RTE_CRYPTO_CIPHER_NULL) &&
2641f14d500SAkhil Goyal 		(ses->auth_alg != RTE_CRYPTO_AUTH_NULL) &&
2651f14d500SAkhil Goyal 		(ses->proto_alg != RTE_SECURITY_PROTOCOL_IPSEC));
2661f14d500SAkhil Goyal }
2671f14d500SAkhil Goyal 
2681f14d500SAkhil Goyal static inline int is_proto_ipsec(dpaa_sec_session *ses)
2691f14d500SAkhil Goyal {
2701f14d500SAkhil Goyal 	return (ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC);
271c3e85bdcSAkhil Goyal }
272c3e85bdcSAkhil Goyal 
273a1173d55SHemant Agrawal static inline int is_proto_pdcp(dpaa_sec_session *ses)
274a1173d55SHemant Agrawal {
275a1173d55SHemant Agrawal 	return (ses->proto_alg == RTE_SECURITY_PROTOCOL_PDCP);
276a1173d55SHemant Agrawal }
277a1173d55SHemant Agrawal 
278c3e85bdcSAkhil Goyal static inline int is_encode(dpaa_sec_session *ses)
279c3e85bdcSAkhil Goyal {
280c3e85bdcSAkhil Goyal 	return ses->dir == DIR_ENC;
281c3e85bdcSAkhil Goyal }
282c3e85bdcSAkhil Goyal 
283c3e85bdcSAkhil Goyal static inline int is_decode(dpaa_sec_session *ses)
284c3e85bdcSAkhil Goyal {
285c3e85bdcSAkhil Goyal 	return ses->dir == DIR_DEC;
286c3e85bdcSAkhil Goyal }
287c3e85bdcSAkhil Goyal 
288c3e85bdcSAkhil Goyal static inline void
289c3e85bdcSAkhil Goyal caam_auth_alg(dpaa_sec_session *ses, struct alginfo *alginfo_a)
290c3e85bdcSAkhil Goyal {
291c3e85bdcSAkhil Goyal 	switch (ses->auth_alg) {
292c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AUTH_NULL:
29305b12700SHemant Agrawal 		alginfo_a->algtype =
29405b12700SHemant Agrawal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
29505b12700SHemant Agrawal 			OP_PCL_IPSEC_HMAC_NULL : 0;
296c3e85bdcSAkhil Goyal 		ses->digest_length = 0;
297c3e85bdcSAkhil Goyal 		break;
298c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AUTH_MD5_HMAC:
2991f14d500SAkhil Goyal 		alginfo_a->algtype =
3001f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3011f14d500SAkhil Goyal 			OP_PCL_IPSEC_HMAC_MD5_96 : OP_ALG_ALGSEL_MD5;
302c3e85bdcSAkhil Goyal 		alginfo_a->algmode = OP_ALG_AAI_HMAC;
303c3e85bdcSAkhil Goyal 		break;
304c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA1_HMAC:
3051f14d500SAkhil Goyal 		alginfo_a->algtype =
3061f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3071f14d500SAkhil Goyal 			OP_PCL_IPSEC_HMAC_SHA1_96 : OP_ALG_ALGSEL_SHA1;
308c3e85bdcSAkhil Goyal 		alginfo_a->algmode = OP_ALG_AAI_HMAC;
309c3e85bdcSAkhil Goyal 		break;
310c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA224_HMAC:
3111f14d500SAkhil Goyal 		alginfo_a->algtype =
3121f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3131f14d500SAkhil Goyal 			OP_PCL_IPSEC_HMAC_SHA1_160 : OP_ALG_ALGSEL_SHA224;
314c3e85bdcSAkhil Goyal 		alginfo_a->algmode = OP_ALG_AAI_HMAC;
315c3e85bdcSAkhil Goyal 		break;
316c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA256_HMAC:
3171f14d500SAkhil Goyal 		alginfo_a->algtype =
3181f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3191f14d500SAkhil Goyal 			OP_PCL_IPSEC_HMAC_SHA2_256_128 : OP_ALG_ALGSEL_SHA256;
320c3e85bdcSAkhil Goyal 		alginfo_a->algmode = OP_ALG_AAI_HMAC;
321c3e85bdcSAkhil Goyal 		break;
322c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA384_HMAC:
3231f14d500SAkhil Goyal 		alginfo_a->algtype =
3241f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3251f14d500SAkhil Goyal 			OP_PCL_IPSEC_HMAC_SHA2_384_192 : OP_ALG_ALGSEL_SHA384;
326c3e85bdcSAkhil Goyal 		alginfo_a->algmode = OP_ALG_AAI_HMAC;
327c3e85bdcSAkhil Goyal 		break;
328c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA512_HMAC:
3291f14d500SAkhil Goyal 		alginfo_a->algtype =
3301f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3311f14d500SAkhil Goyal 			OP_PCL_IPSEC_HMAC_SHA2_512_256 : OP_ALG_ALGSEL_SHA512;
332c3e85bdcSAkhil Goyal 		alginfo_a->algmode = OP_ALG_AAI_HMAC;
333c3e85bdcSAkhil Goyal 		break;
334c3e85bdcSAkhil Goyal 	default:
335f163231eSHemant Agrawal 		DPAA_SEC_ERR("unsupported auth alg %u", ses->auth_alg);
336c3e85bdcSAkhil Goyal 	}
337c3e85bdcSAkhil Goyal }
338c3e85bdcSAkhil Goyal 
339c3e85bdcSAkhil Goyal static inline void
340c3e85bdcSAkhil Goyal caam_cipher_alg(dpaa_sec_session *ses, struct alginfo *alginfo_c)
341c3e85bdcSAkhil Goyal {
342c3e85bdcSAkhil Goyal 	switch (ses->cipher_alg) {
343c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_CIPHER_NULL:
34405b12700SHemant Agrawal 		alginfo_c->algtype =
34505b12700SHemant Agrawal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
34605b12700SHemant Agrawal 			OP_PCL_IPSEC_NULL : 0;
347c3e85bdcSAkhil Goyal 		break;
348c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_CIPHER_AES_CBC:
3491f14d500SAkhil Goyal 		alginfo_c->algtype =
3501f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3511f14d500SAkhil Goyal 			OP_PCL_IPSEC_AES_CBC : OP_ALG_ALGSEL_AES;
352c3e85bdcSAkhil Goyal 		alginfo_c->algmode = OP_ALG_AAI_CBC;
353c3e85bdcSAkhil Goyal 		break;
354c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_CIPHER_3DES_CBC:
3551f14d500SAkhil Goyal 		alginfo_c->algtype =
3561f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3571f14d500SAkhil Goyal 			OP_PCL_IPSEC_3DES : OP_ALG_ALGSEL_3DES;
358c3e85bdcSAkhil Goyal 		alginfo_c->algmode = OP_ALG_AAI_CBC;
359c3e85bdcSAkhil Goyal 		break;
360c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_CIPHER_AES_CTR:
3611f14d500SAkhil Goyal 		alginfo_c->algtype =
3621f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3631f14d500SAkhil Goyal 			OP_PCL_IPSEC_AES_CTR : OP_ALG_ALGSEL_AES;
364c3e85bdcSAkhil Goyal 		alginfo_c->algmode = OP_ALG_AAI_CTR;
365c3e85bdcSAkhil Goyal 		break;
366c3e85bdcSAkhil Goyal 	default:
367f163231eSHemant Agrawal 		DPAA_SEC_ERR("unsupported cipher alg %d", ses->cipher_alg);
368c3e85bdcSAkhil Goyal 	}
369c3e85bdcSAkhil Goyal }
370c3e85bdcSAkhil Goyal 
371c3e85bdcSAkhil Goyal static inline void
372c3e85bdcSAkhil Goyal caam_aead_alg(dpaa_sec_session *ses, struct alginfo *alginfo)
373c3e85bdcSAkhil Goyal {
374c3e85bdcSAkhil Goyal 	switch (ses->aead_alg) {
375c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AEAD_AES_GCM:
376c3e85bdcSAkhil Goyal 		alginfo->algtype = OP_ALG_ALGSEL_AES;
377c3e85bdcSAkhil Goyal 		alginfo->algmode = OP_ALG_AAI_GCM;
378c3e85bdcSAkhil Goyal 		break;
379c3e85bdcSAkhil Goyal 	default:
380f163231eSHemant Agrawal 		DPAA_SEC_ERR("unsupported AEAD alg %d", ses->aead_alg);
381c3e85bdcSAkhil Goyal 	}
382c3e85bdcSAkhil Goyal }
383c3e85bdcSAkhil Goyal 
384a1173d55SHemant Agrawal static int
385a1173d55SHemant Agrawal dpaa_sec_prep_pdcp_cdb(dpaa_sec_session *ses)
386a1173d55SHemant Agrawal {
387a1173d55SHemant Agrawal 	struct alginfo authdata = {0}, cipherdata = {0};
388a1173d55SHemant Agrawal 	struct sec_cdb *cdb = &ses->cdb;
3892e4cbdb4SVakul Garg 	struct alginfo *p_authdata = NULL;
390a1173d55SHemant Agrawal 	int32_t shared_desc_len = 0;
391a1173d55SHemant Agrawal 	int err;
392a1173d55SHemant Agrawal #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
393a1173d55SHemant Agrawal 	int swap = false;
394a1173d55SHemant Agrawal #else
395a1173d55SHemant Agrawal 	int swap = true;
396a1173d55SHemant Agrawal #endif
397a1173d55SHemant Agrawal 
398a1173d55SHemant Agrawal 	switch (ses->cipher_alg) {
399a1173d55SHemant Agrawal 	case RTE_CRYPTO_CIPHER_SNOW3G_UEA2:
400a1173d55SHemant Agrawal 		cipherdata.algtype = PDCP_CIPHER_TYPE_SNOW;
401a1173d55SHemant Agrawal 		break;
402a1173d55SHemant Agrawal 	case RTE_CRYPTO_CIPHER_ZUC_EEA3:
403a1173d55SHemant Agrawal 		cipherdata.algtype = PDCP_CIPHER_TYPE_ZUC;
404a1173d55SHemant Agrawal 		break;
405a1173d55SHemant Agrawal 	case RTE_CRYPTO_CIPHER_AES_CTR:
406a1173d55SHemant Agrawal 		cipherdata.algtype = PDCP_CIPHER_TYPE_AES;
407a1173d55SHemant Agrawal 		break;
408a1173d55SHemant Agrawal 	case RTE_CRYPTO_CIPHER_NULL:
409a1173d55SHemant Agrawal 		cipherdata.algtype = PDCP_CIPHER_TYPE_NULL;
410a1173d55SHemant Agrawal 		break;
411a1173d55SHemant Agrawal 	default:
412a1173d55SHemant Agrawal 		DPAA_SEC_ERR("Crypto: Undefined Cipher specified %u",
413a1173d55SHemant Agrawal 			      ses->cipher_alg);
414a1173d55SHemant Agrawal 		return -1;
415a1173d55SHemant Agrawal 	}
416a1173d55SHemant Agrawal 
417a1173d55SHemant Agrawal 	cipherdata.key = (size_t)ses->cipher_key.data;
418a1173d55SHemant Agrawal 	cipherdata.keylen = ses->cipher_key.length;
419a1173d55SHemant Agrawal 	cipherdata.key_enc_flags = 0;
420a1173d55SHemant Agrawal 	cipherdata.key_type = RTA_DATA_IMM;
421a1173d55SHemant Agrawal 
4222e4cbdb4SVakul Garg 	cdb->sh_desc[0] = cipherdata.keylen;
4232e4cbdb4SVakul Garg 	cdb->sh_desc[1] = 0;
4242e4cbdb4SVakul Garg 	cdb->sh_desc[2] = 0;
4252e4cbdb4SVakul Garg 
4262e4cbdb4SVakul Garg 	if (ses->auth_alg) {
427a1173d55SHemant Agrawal 		switch (ses->auth_alg) {
428a1173d55SHemant Agrawal 		case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
429a1173d55SHemant Agrawal 			authdata.algtype = PDCP_AUTH_TYPE_SNOW;
430a1173d55SHemant Agrawal 			break;
431a1173d55SHemant Agrawal 		case RTE_CRYPTO_AUTH_ZUC_EIA3:
432a1173d55SHemant Agrawal 			authdata.algtype = PDCP_AUTH_TYPE_ZUC;
433a1173d55SHemant Agrawal 			break;
434a1173d55SHemant Agrawal 		case RTE_CRYPTO_AUTH_AES_CMAC:
435a1173d55SHemant Agrawal 			authdata.algtype = PDCP_AUTH_TYPE_AES;
436a1173d55SHemant Agrawal 			break;
437a1173d55SHemant Agrawal 		case RTE_CRYPTO_AUTH_NULL:
438a1173d55SHemant Agrawal 			authdata.algtype = PDCP_AUTH_TYPE_NULL;
439a1173d55SHemant Agrawal 			break;
440a1173d55SHemant Agrawal 		default:
441a1173d55SHemant Agrawal 			DPAA_SEC_ERR("Crypto: Unsupported auth alg %u",
442a1173d55SHemant Agrawal 				      ses->auth_alg);
443a1173d55SHemant Agrawal 			return -1;
444a1173d55SHemant Agrawal 		}
445a1173d55SHemant Agrawal 
446a1173d55SHemant Agrawal 		authdata.key = (size_t)ses->auth_key.data;
447a1173d55SHemant Agrawal 		authdata.keylen = ses->auth_key.length;
448a1173d55SHemant Agrawal 		authdata.key_enc_flags = 0;
449a1173d55SHemant Agrawal 		authdata.key_type = RTA_DATA_IMM;
450a1173d55SHemant Agrawal 
4512e4cbdb4SVakul Garg 		p_authdata = &authdata;
4522e4cbdb4SVakul Garg 
453a1173d55SHemant Agrawal 		cdb->sh_desc[1] = authdata.keylen;
4542e4cbdb4SVakul Garg 	}
4552e4cbdb4SVakul Garg 
456a1173d55SHemant Agrawal 	err = rta_inline_query(IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN,
457a1173d55SHemant Agrawal 			       MIN_JOB_DESC_SIZE,
458a1173d55SHemant Agrawal 			       (unsigned int *)cdb->sh_desc,
459a1173d55SHemant Agrawal 			       &cdb->sh_desc[2], 2);
460a1173d55SHemant Agrawal 	if (err < 0) {
461a1173d55SHemant Agrawal 		DPAA_SEC_ERR("Crypto: Incorrect key lengths");
462a1173d55SHemant Agrawal 		return err;
463a1173d55SHemant Agrawal 	}
4642e4cbdb4SVakul Garg 
465a1173d55SHemant Agrawal 	if (!(cdb->sh_desc[2] & 1) && cipherdata.keylen) {
4662e4cbdb4SVakul Garg 		cipherdata.key =
4672e4cbdb4SVakul Garg 			(size_t)dpaa_mem_vtop((void *)(size_t)cipherdata.key);
468a1173d55SHemant Agrawal 		cipherdata.key_type = RTA_DATA_PTR;
469a1173d55SHemant Agrawal 	}
470a1173d55SHemant Agrawal 	if (!(cdb->sh_desc[2] & (1 << 1)) &&  authdata.keylen) {
4712e4cbdb4SVakul Garg 		authdata.key =
4722e4cbdb4SVakul Garg 			(size_t)dpaa_mem_vtop((void *)(size_t)authdata.key);
473a1173d55SHemant Agrawal 		authdata.key_type = RTA_DATA_PTR;
474a1173d55SHemant Agrawal 	}
475a1173d55SHemant Agrawal 
476a1173d55SHemant Agrawal 	cdb->sh_desc[0] = 0;
477a1173d55SHemant Agrawal 	cdb->sh_desc[1] = 0;
478a1173d55SHemant Agrawal 	cdb->sh_desc[2] = 0;
479a1173d55SHemant Agrawal 
4802e4cbdb4SVakul Garg 	if (ses->pdcp.domain == RTE_SECURITY_PDCP_MODE_CONTROL) {
481a1173d55SHemant Agrawal 		if (ses->dir == DIR_ENC)
482a1173d55SHemant Agrawal 			shared_desc_len = cnstr_shdsc_pdcp_c_plane_encap(
483a1173d55SHemant Agrawal 					cdb->sh_desc, 1, swap,
484a1173d55SHemant Agrawal 					ses->pdcp.hfn,
485eac60082SVakul Garg 					ses->pdcp.sn_size,
486a1173d55SHemant Agrawal 					ses->pdcp.bearer,
487a1173d55SHemant Agrawal 					ses->pdcp.pkt_dir,
488a1173d55SHemant Agrawal 					ses->pdcp.hfn_threshold,
489a1173d55SHemant Agrawal 					&cipherdata, &authdata,
490a1173d55SHemant Agrawal 					0);
491a1173d55SHemant Agrawal 		else if (ses->dir == DIR_DEC)
492a1173d55SHemant Agrawal 			shared_desc_len = cnstr_shdsc_pdcp_c_plane_decap(
493a1173d55SHemant Agrawal 					cdb->sh_desc, 1, swap,
494a1173d55SHemant Agrawal 					ses->pdcp.hfn,
495eac60082SVakul Garg 					ses->pdcp.sn_size,
496a1173d55SHemant Agrawal 					ses->pdcp.bearer,
497a1173d55SHemant Agrawal 					ses->pdcp.pkt_dir,
498a1173d55SHemant Agrawal 					ses->pdcp.hfn_threshold,
499a1173d55SHemant Agrawal 					&cipherdata, &authdata,
500a1173d55SHemant Agrawal 					0);
501a1173d55SHemant Agrawal 	} else {
502a1173d55SHemant Agrawal 		if (ses->dir == DIR_ENC)
503a1173d55SHemant Agrawal 			shared_desc_len = cnstr_shdsc_pdcp_u_plane_encap(
504a1173d55SHemant Agrawal 					cdb->sh_desc, 1, swap,
505a1173d55SHemant Agrawal 					ses->pdcp.sn_size,
506a1173d55SHemant Agrawal 					ses->pdcp.hfn,
507a1173d55SHemant Agrawal 					ses->pdcp.bearer,
508a1173d55SHemant Agrawal 					ses->pdcp.pkt_dir,
509a1173d55SHemant Agrawal 					ses->pdcp.hfn_threshold,
5102e4cbdb4SVakul Garg 					&cipherdata, p_authdata, 0);
511a1173d55SHemant Agrawal 		else if (ses->dir == DIR_DEC)
512a1173d55SHemant Agrawal 			shared_desc_len = cnstr_shdsc_pdcp_u_plane_decap(
513a1173d55SHemant Agrawal 					cdb->sh_desc, 1, swap,
514a1173d55SHemant Agrawal 					ses->pdcp.sn_size,
515a1173d55SHemant Agrawal 					ses->pdcp.hfn,
516a1173d55SHemant Agrawal 					ses->pdcp.bearer,
517a1173d55SHemant Agrawal 					ses->pdcp.pkt_dir,
518a1173d55SHemant Agrawal 					ses->pdcp.hfn_threshold,
5192e4cbdb4SVakul Garg 					&cipherdata, p_authdata, 0);
520a1173d55SHemant Agrawal 	}
521a1173d55SHemant Agrawal 
522a1173d55SHemant Agrawal 	return shared_desc_len;
523a1173d55SHemant Agrawal }
524a1173d55SHemant Agrawal 
52505b12700SHemant Agrawal /* prepare ipsec proto command block of the session */
52605b12700SHemant Agrawal static int
52705b12700SHemant Agrawal dpaa_sec_prep_ipsec_cdb(dpaa_sec_session *ses)
52805b12700SHemant Agrawal {
52905b12700SHemant Agrawal 	struct alginfo cipherdata = {0}, authdata = {0};
53005b12700SHemant Agrawal 	struct sec_cdb *cdb = &ses->cdb;
53105b12700SHemant Agrawal 	int32_t shared_desc_len = 0;
53205b12700SHemant Agrawal 	int err;
53305b12700SHemant Agrawal #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
53405b12700SHemant Agrawal 	int swap = false;
53505b12700SHemant Agrawal #else
53605b12700SHemant Agrawal 	int swap = true;
53705b12700SHemant Agrawal #endif
53805b12700SHemant Agrawal 
53905b12700SHemant Agrawal 	caam_cipher_alg(ses, &cipherdata);
54005b12700SHemant Agrawal 	if (cipherdata.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
54105b12700SHemant Agrawal 		DPAA_SEC_ERR("not supported cipher alg");
54205b12700SHemant Agrawal 		return -ENOTSUP;
54305b12700SHemant Agrawal 	}
54405b12700SHemant Agrawal 
54505b12700SHemant Agrawal 	cipherdata.key = (size_t)ses->cipher_key.data;
54605b12700SHemant Agrawal 	cipherdata.keylen = ses->cipher_key.length;
54705b12700SHemant Agrawal 	cipherdata.key_enc_flags = 0;
54805b12700SHemant Agrawal 	cipherdata.key_type = RTA_DATA_IMM;
54905b12700SHemant Agrawal 
55005b12700SHemant Agrawal 	caam_auth_alg(ses, &authdata);
55105b12700SHemant Agrawal 	if (authdata.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
55205b12700SHemant Agrawal 		DPAA_SEC_ERR("not supported auth alg");
55305b12700SHemant Agrawal 		return -ENOTSUP;
55405b12700SHemant Agrawal 	}
55505b12700SHemant Agrawal 
55605b12700SHemant Agrawal 	authdata.key = (size_t)ses->auth_key.data;
55705b12700SHemant Agrawal 	authdata.keylen = ses->auth_key.length;
55805b12700SHemant Agrawal 	authdata.key_enc_flags = 0;
55905b12700SHemant Agrawal 	authdata.key_type = RTA_DATA_IMM;
56005b12700SHemant Agrawal 
56105b12700SHemant Agrawal 	cdb->sh_desc[0] = cipherdata.keylen;
56205b12700SHemant Agrawal 	cdb->sh_desc[1] = authdata.keylen;
56305b12700SHemant Agrawal 	err = rta_inline_query(IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN,
56405b12700SHemant Agrawal 			       MIN_JOB_DESC_SIZE,
56505b12700SHemant Agrawal 			       (unsigned int *)cdb->sh_desc,
56605b12700SHemant Agrawal 			       &cdb->sh_desc[2], 2);
56705b12700SHemant Agrawal 
56805b12700SHemant Agrawal 	if (err < 0) {
56905b12700SHemant Agrawal 		DPAA_SEC_ERR("Crypto: Incorrect key lengths");
57005b12700SHemant Agrawal 		return err;
57105b12700SHemant Agrawal 	}
57205b12700SHemant Agrawal 	if (cdb->sh_desc[2] & 1)
57305b12700SHemant Agrawal 		cipherdata.key_type = RTA_DATA_IMM;
57405b12700SHemant Agrawal 	else {
57505b12700SHemant Agrawal 		cipherdata.key = (size_t)dpaa_mem_vtop(
57605b12700SHemant Agrawal 					(void *)(size_t)cipherdata.key);
57705b12700SHemant Agrawal 		cipherdata.key_type = RTA_DATA_PTR;
57805b12700SHemant Agrawal 	}
57905b12700SHemant Agrawal 	if (cdb->sh_desc[2] & (1<<1))
58005b12700SHemant Agrawal 		authdata.key_type = RTA_DATA_IMM;
58105b12700SHemant Agrawal 	else {
58205b12700SHemant Agrawal 		authdata.key = (size_t)dpaa_mem_vtop(
58305b12700SHemant Agrawal 					(void *)(size_t)authdata.key);
58405b12700SHemant Agrawal 		authdata.key_type = RTA_DATA_PTR;
58505b12700SHemant Agrawal 	}
58605b12700SHemant Agrawal 
58705b12700SHemant Agrawal 	cdb->sh_desc[0] = 0;
58805b12700SHemant Agrawal 	cdb->sh_desc[1] = 0;
58905b12700SHemant Agrawal 	cdb->sh_desc[2] = 0;
59005b12700SHemant Agrawal 	if (ses->dir == DIR_ENC) {
59105b12700SHemant Agrawal 		shared_desc_len = cnstr_shdsc_ipsec_new_encap(
59205b12700SHemant Agrawal 				cdb->sh_desc,
59305b12700SHemant Agrawal 				true, swap, SHR_SERIAL,
59405b12700SHemant Agrawal 				&ses->encap_pdb,
59505b12700SHemant Agrawal 				(uint8_t *)&ses->ip4_hdr,
59605b12700SHemant Agrawal 				&cipherdata, &authdata);
59705b12700SHemant Agrawal 	} else if (ses->dir == DIR_DEC) {
59805b12700SHemant Agrawal 		shared_desc_len = cnstr_shdsc_ipsec_new_decap(
59905b12700SHemant Agrawal 				cdb->sh_desc,
60005b12700SHemant Agrawal 				true, swap, SHR_SERIAL,
60105b12700SHemant Agrawal 				&ses->decap_pdb,
60205b12700SHemant Agrawal 				&cipherdata, &authdata);
60305b12700SHemant Agrawal 	}
60405b12700SHemant Agrawal 	return shared_desc_len;
60505b12700SHemant Agrawal }
606c3e85bdcSAkhil Goyal 
607c3e85bdcSAkhil Goyal /* prepare command block of the session */
608c3e85bdcSAkhil Goyal static int
609c3e85bdcSAkhil Goyal dpaa_sec_prep_cdb(dpaa_sec_session *ses)
610c3e85bdcSAkhil Goyal {
611c3e85bdcSAkhil Goyal 	struct alginfo alginfo_c = {0}, alginfo_a = {0}, alginfo = {0};
61222788c2cSSunil Kumar Kori 	int32_t shared_desc_len = 0;
613e79416d1SHemant Agrawal 	struct sec_cdb *cdb = &ses->cdb;
614c3e85bdcSAkhil Goyal 	int err;
615c3e85bdcSAkhil Goyal #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
616c3e85bdcSAkhil Goyal 	int swap = false;
617c3e85bdcSAkhil Goyal #else
618c3e85bdcSAkhil Goyal 	int swap = true;
619c3e85bdcSAkhil Goyal #endif
620c3e85bdcSAkhil Goyal 
621c3e85bdcSAkhil Goyal 	memset(cdb, 0, sizeof(struct sec_cdb));
622c3e85bdcSAkhil Goyal 
62305b12700SHemant Agrawal 	if (is_proto_ipsec(ses)) {
62405b12700SHemant Agrawal 		shared_desc_len = dpaa_sec_prep_ipsec_cdb(ses);
625a1173d55SHemant Agrawal 	} else if (is_proto_pdcp(ses)) {
626a1173d55SHemant Agrawal 		shared_desc_len = dpaa_sec_prep_pdcp_cdb(ses);
62705b12700SHemant Agrawal 	} else if (is_cipher_only(ses)) {
628c3e85bdcSAkhil Goyal 		caam_cipher_alg(ses, &alginfo_c);
629c3e85bdcSAkhil Goyal 		if (alginfo_c.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
630f163231eSHemant Agrawal 			DPAA_SEC_ERR("not supported cipher alg");
631c3e85bdcSAkhil Goyal 			return -ENOTSUP;
632c3e85bdcSAkhil Goyal 		}
633c3e85bdcSAkhil Goyal 
6340e5607e4SHemant Agrawal 		alginfo_c.key = (size_t)ses->cipher_key.data;
635c3e85bdcSAkhil Goyal 		alginfo_c.keylen = ses->cipher_key.length;
636c3e85bdcSAkhil Goyal 		alginfo_c.key_enc_flags = 0;
637c3e85bdcSAkhil Goyal 		alginfo_c.key_type = RTA_DATA_IMM;
638c3e85bdcSAkhil Goyal 
639c3e85bdcSAkhil Goyal 		shared_desc_len = cnstr_shdsc_blkcipher(
640c3e85bdcSAkhil Goyal 						cdb->sh_desc, true,
6417449390bSAkhil Goyal 						swap, SHR_NEVER, &alginfo_c,
642c3e85bdcSAkhil Goyal 						NULL,
643c3e85bdcSAkhil Goyal 						ses->iv.length,
644c3e85bdcSAkhil Goyal 						ses->dir);
645c3e85bdcSAkhil Goyal 	} else if (is_auth_only(ses)) {
646c3e85bdcSAkhil Goyal 		caam_auth_alg(ses, &alginfo_a);
647c3e85bdcSAkhil Goyal 		if (alginfo_a.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
648f163231eSHemant Agrawal 			DPAA_SEC_ERR("not supported auth alg");
649c3e85bdcSAkhil Goyal 			return -ENOTSUP;
650c3e85bdcSAkhil Goyal 		}
651c3e85bdcSAkhil Goyal 
6520e5607e4SHemant Agrawal 		alginfo_a.key = (size_t)ses->auth_key.data;
653c3e85bdcSAkhil Goyal 		alginfo_a.keylen = ses->auth_key.length;
654c3e85bdcSAkhil Goyal 		alginfo_a.key_enc_flags = 0;
655c3e85bdcSAkhil Goyal 		alginfo_a.key_type = RTA_DATA_IMM;
656c3e85bdcSAkhil Goyal 
657c3e85bdcSAkhil Goyal 		shared_desc_len = cnstr_shdsc_hmac(cdb->sh_desc, true,
6587449390bSAkhil Goyal 						   swap, SHR_NEVER, &alginfo_a,
659c3e85bdcSAkhil Goyal 						   !ses->dir,
660c3e85bdcSAkhil Goyal 						   ses->digest_length);
661c3e85bdcSAkhil Goyal 	} else if (is_aead(ses)) {
662c3e85bdcSAkhil Goyal 		caam_aead_alg(ses, &alginfo);
663c3e85bdcSAkhil Goyal 		if (alginfo.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
664f163231eSHemant Agrawal 			DPAA_SEC_ERR("not supported aead alg");
665c3e85bdcSAkhil Goyal 			return -ENOTSUP;
666c3e85bdcSAkhil Goyal 		}
6670e5607e4SHemant Agrawal 		alginfo.key = (size_t)ses->aead_key.data;
668c3e85bdcSAkhil Goyal 		alginfo.keylen = ses->aead_key.length;
669c3e85bdcSAkhil Goyal 		alginfo.key_enc_flags = 0;
670c3e85bdcSAkhil Goyal 		alginfo.key_type = RTA_DATA_IMM;
671c3e85bdcSAkhil Goyal 
672c3e85bdcSAkhil Goyal 		if (ses->dir == DIR_ENC)
673c3e85bdcSAkhil Goyal 			shared_desc_len = cnstr_shdsc_gcm_encap(
6747449390bSAkhil Goyal 					cdb->sh_desc, true, swap, SHR_NEVER,
675c3e85bdcSAkhil Goyal 					&alginfo,
676c3e85bdcSAkhil Goyal 					ses->iv.length,
677c3e85bdcSAkhil Goyal 					ses->digest_length);
678c3e85bdcSAkhil Goyal 		else
679c3e85bdcSAkhil Goyal 			shared_desc_len = cnstr_shdsc_gcm_decap(
6807449390bSAkhil Goyal 					cdb->sh_desc, true, swap, SHR_NEVER,
681c3e85bdcSAkhil Goyal 					&alginfo,
682c3e85bdcSAkhil Goyal 					ses->iv.length,
683c3e85bdcSAkhil Goyal 					ses->digest_length);
684c3e85bdcSAkhil Goyal 	} else {
685c3e85bdcSAkhil Goyal 		caam_cipher_alg(ses, &alginfo_c);
686c3e85bdcSAkhil Goyal 		if (alginfo_c.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
687f163231eSHemant Agrawal 			DPAA_SEC_ERR("not supported cipher alg");
688c3e85bdcSAkhil Goyal 			return -ENOTSUP;
689c3e85bdcSAkhil Goyal 		}
690c3e85bdcSAkhil Goyal 
6910e5607e4SHemant Agrawal 		alginfo_c.key = (size_t)ses->cipher_key.data;
692c3e85bdcSAkhil Goyal 		alginfo_c.keylen = ses->cipher_key.length;
693c3e85bdcSAkhil Goyal 		alginfo_c.key_enc_flags = 0;
694c3e85bdcSAkhil Goyal 		alginfo_c.key_type = RTA_DATA_IMM;
695c3e85bdcSAkhil Goyal 
696c3e85bdcSAkhil Goyal 		caam_auth_alg(ses, &alginfo_a);
697c3e85bdcSAkhil Goyal 		if (alginfo_a.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
698f163231eSHemant Agrawal 			DPAA_SEC_ERR("not supported auth alg");
699c3e85bdcSAkhil Goyal 			return -ENOTSUP;
700c3e85bdcSAkhil Goyal 		}
701c3e85bdcSAkhil Goyal 
7020e5607e4SHemant Agrawal 		alginfo_a.key = (size_t)ses->auth_key.data;
703c3e85bdcSAkhil Goyal 		alginfo_a.keylen = ses->auth_key.length;
704c3e85bdcSAkhil Goyal 		alginfo_a.key_enc_flags = 0;
705c3e85bdcSAkhil Goyal 		alginfo_a.key_type = RTA_DATA_IMM;
706c3e85bdcSAkhil Goyal 
707c3e85bdcSAkhil Goyal 		cdb->sh_desc[0] = alginfo_c.keylen;
708c3e85bdcSAkhil Goyal 		cdb->sh_desc[1] = alginfo_a.keylen;
709c3e85bdcSAkhil Goyal 		err = rta_inline_query(IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN,
710c3e85bdcSAkhil Goyal 				       MIN_JOB_DESC_SIZE,
711c3e85bdcSAkhil Goyal 				       (unsigned int *)cdb->sh_desc,
712c3e85bdcSAkhil Goyal 				       &cdb->sh_desc[2], 2);
713c3e85bdcSAkhil Goyal 
714c3e85bdcSAkhil Goyal 		if (err < 0) {
715f163231eSHemant Agrawal 			DPAA_SEC_ERR("Crypto: Incorrect key lengths");
716c3e85bdcSAkhil Goyal 			return err;
717c3e85bdcSAkhil Goyal 		}
718c3e85bdcSAkhil Goyal 		if (cdb->sh_desc[2] & 1)
719c3e85bdcSAkhil Goyal 			alginfo_c.key_type = RTA_DATA_IMM;
720c3e85bdcSAkhil Goyal 		else {
7210e5607e4SHemant Agrawal 			alginfo_c.key = (size_t)dpaa_mem_vtop(
7220e5607e4SHemant Agrawal 						(void *)(size_t)alginfo_c.key);
723c3e85bdcSAkhil Goyal 			alginfo_c.key_type = RTA_DATA_PTR;
724c3e85bdcSAkhil Goyal 		}
725c3e85bdcSAkhil Goyal 		if (cdb->sh_desc[2] & (1<<1))
726c3e85bdcSAkhil Goyal 			alginfo_a.key_type = RTA_DATA_IMM;
727c3e85bdcSAkhil Goyal 		else {
7280e5607e4SHemant Agrawal 			alginfo_a.key = (size_t)dpaa_mem_vtop(
7290e5607e4SHemant Agrawal 						(void *)(size_t)alginfo_a.key);
730c3e85bdcSAkhil Goyal 			alginfo_a.key_type = RTA_DATA_PTR;
731c3e85bdcSAkhil Goyal 		}
732c3e85bdcSAkhil Goyal 		cdb->sh_desc[0] = 0;
733c3e85bdcSAkhil Goyal 		cdb->sh_desc[1] = 0;
734c3e85bdcSAkhil Goyal 		cdb->sh_desc[2] = 0;
7351f14d500SAkhil Goyal 		/* Auth_only_len is set as 0 here and it will be
7361f14d500SAkhil Goyal 		 * overwritten in fd for each packet.
737c3e85bdcSAkhil Goyal 		 */
738c3e85bdcSAkhil Goyal 		shared_desc_len = cnstr_shdsc_authenc(cdb->sh_desc,
7397449390bSAkhil Goyal 				true, swap, SHR_SERIAL, &alginfo_c, &alginfo_a,
740c3e85bdcSAkhil Goyal 				ses->iv.length, 0,
741c3e85bdcSAkhil Goyal 				ses->digest_length, ses->dir);
742c3e85bdcSAkhil Goyal 	}
74322788c2cSSunil Kumar Kori 
74422788c2cSSunil Kumar Kori 	if (shared_desc_len < 0) {
745f163231eSHemant Agrawal 		DPAA_SEC_ERR("error in preparing command block");
74622788c2cSSunil Kumar Kori 		return shared_desc_len;
74722788c2cSSunil Kumar Kori 	}
74822788c2cSSunil Kumar Kori 
749c3e85bdcSAkhil Goyal 	cdb->sh_hdr.hi.field.idlen = shared_desc_len;
750c3e85bdcSAkhil Goyal 	cdb->sh_hdr.hi.word = rte_cpu_to_be_32(cdb->sh_hdr.hi.word);
751c3e85bdcSAkhil Goyal 	cdb->sh_hdr.lo.word = rte_cpu_to_be_32(cdb->sh_hdr.lo.word);
752c3e85bdcSAkhil Goyal 
753c3e85bdcSAkhil Goyal 	return 0;
754c3e85bdcSAkhil Goyal }
755c3e85bdcSAkhil Goyal 
756c3e85bdcSAkhil Goyal /* qp is lockless, should be accessed by only one thread */
757c3e85bdcSAkhil Goyal static int
758c3e85bdcSAkhil Goyal dpaa_sec_deq(struct dpaa_sec_qp *qp, struct rte_crypto_op **ops, int nb_ops)
759c3e85bdcSAkhil Goyal {
760c3e85bdcSAkhil Goyal 	struct qman_fq *fq;
7619a984458SAkhil Goyal 	unsigned int pkts = 0;
762f40d5a53SNipun Gupta 	int num_rx_bufs, ret;
7639a984458SAkhil Goyal 	struct qm_dqrr_entry *dq;
764f40d5a53SNipun Gupta 	uint32_t vdqcr_flags = 0;
765c3e85bdcSAkhil Goyal 
766c3e85bdcSAkhil Goyal 	fq = &qp->outq;
767f40d5a53SNipun Gupta 	/*
768f40d5a53SNipun Gupta 	 * Until request for four buffers, we provide exact number of buffers.
769f40d5a53SNipun Gupta 	 * Otherwise we do not set the QM_VDQCR_EXACT flag.
770f40d5a53SNipun Gupta 	 * Not setting QM_VDQCR_EXACT flag can provide two more buffers than
771f40d5a53SNipun Gupta 	 * requested, so we request two less in this case.
772f40d5a53SNipun Gupta 	 */
773f40d5a53SNipun Gupta 	if (nb_ops < 4) {
774f40d5a53SNipun Gupta 		vdqcr_flags = QM_VDQCR_EXACT;
775f40d5a53SNipun Gupta 		num_rx_bufs = nb_ops;
776f40d5a53SNipun Gupta 	} else {
777f40d5a53SNipun Gupta 		num_rx_bufs = nb_ops > DPAA_MAX_DEQUEUE_NUM_FRAMES ?
778f40d5a53SNipun Gupta 			(DPAA_MAX_DEQUEUE_NUM_FRAMES - 2) : (nb_ops - 2);
779f40d5a53SNipun Gupta 	}
780f40d5a53SNipun Gupta 	ret = qman_set_vdq(fq, num_rx_bufs, vdqcr_flags);
7819a984458SAkhil Goyal 	if (ret)
7829a984458SAkhil Goyal 		return 0;
783c3e85bdcSAkhil Goyal 
7849a984458SAkhil Goyal 	do {
7859a984458SAkhil Goyal 		const struct qm_fd *fd;
7869a984458SAkhil Goyal 		struct dpaa_sec_job *job;
7879a984458SAkhil Goyal 		struct dpaa_sec_op_ctx *ctx;
7889a984458SAkhil Goyal 		struct rte_crypto_op *op;
789c3e85bdcSAkhil Goyal 
7909a984458SAkhil Goyal 		dq = qman_dequeue(fq);
7919a984458SAkhil Goyal 		if (!dq)
7929a984458SAkhil Goyal 			continue;
7939a984458SAkhil Goyal 
7949a984458SAkhil Goyal 		fd = &dq->fd;
7959a984458SAkhil Goyal 		/* sg is embedded in an op ctx,
7969a984458SAkhil Goyal 		 * sg[0] is for output
7979a984458SAkhil Goyal 		 * sg[1] for input
7989a984458SAkhil Goyal 		 */
7999a984458SAkhil Goyal 		job = dpaa_mem_ptov(qm_fd_addr_get64(fd));
8009a984458SAkhil Goyal 
8019a984458SAkhil Goyal 		ctx = container_of(job, struct dpaa_sec_op_ctx, job);
8029a984458SAkhil Goyal 		ctx->fd_status = fd->status;
8039a984458SAkhil Goyal 		op = ctx->op;
8049a984458SAkhil Goyal 		if (op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
8059a984458SAkhil Goyal 			struct qm_sg_entry *sg_out;
8069a984458SAkhil Goyal 			uint32_t len;
8079a984458SAkhil Goyal 
8089a984458SAkhil Goyal 			sg_out = &job->sg[0];
8099a984458SAkhil Goyal 			hw_sg_to_cpu(sg_out);
8109a984458SAkhil Goyal 			len = sg_out->length;
8119a984458SAkhil Goyal 			op->sym->m_src->pkt_len = len;
8129a984458SAkhil Goyal 			op->sym->m_src->data_len = len;
8139a984458SAkhil Goyal 		}
8149a984458SAkhil Goyal 		if (!ctx->fd_status) {
8159a984458SAkhil Goyal 			op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
8169a984458SAkhil Goyal 		} else {
817f163231eSHemant Agrawal 			DPAA_SEC_DP_WARN("SEC return err:0x%x", ctx->fd_status);
8189a984458SAkhil Goyal 			op->status = RTE_CRYPTO_OP_STATUS_ERROR;
8199a984458SAkhil Goyal 		}
8209a984458SAkhil Goyal 		ops[pkts++] = op;
8219a984458SAkhil Goyal 
8229a984458SAkhil Goyal 		/* report op status to sym->op and then free the ctx memeory */
8239a984458SAkhil Goyal 		rte_mempool_put(ctx->ctx_pool, (void *)ctx);
8249a984458SAkhil Goyal 
8259a984458SAkhil Goyal 		qman_dqrr_consume(fq, dq);
8269a984458SAkhil Goyal 	} while (fq->flags & QMAN_FQ_STATE_VDQCR);
8279a984458SAkhil Goyal 
8289a984458SAkhil Goyal 	return pkts;
829c3e85bdcSAkhil Goyal }
830c3e85bdcSAkhil Goyal 
831a74af788SAkhil Goyal static inline struct dpaa_sec_job *
832a74af788SAkhil Goyal build_auth_only_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
833a74af788SAkhil Goyal {
834a74af788SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
835a74af788SAkhil Goyal 	struct rte_mbuf *mbuf = sym->m_src;
836a74af788SAkhil Goyal 	struct dpaa_sec_job *cf;
837a74af788SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
838a74af788SAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
839a74af788SAkhil Goyal 	phys_addr_t start_addr;
840a74af788SAkhil Goyal 	uint8_t *old_digest, extra_segs;
841a74af788SAkhil Goyal 
842a74af788SAkhil Goyal 	if (is_decode(ses))
843a74af788SAkhil Goyal 		extra_segs = 3;
844a74af788SAkhil Goyal 	else
845a74af788SAkhil Goyal 		extra_segs = 2;
846a74af788SAkhil Goyal 
847a74af788SAkhil Goyal 	if ((mbuf->nb_segs + extra_segs) > MAX_SG_ENTRIES) {
848f163231eSHemant Agrawal 		DPAA_SEC_DP_ERR("Auth: Max sec segs supported is %d",
849a74af788SAkhil Goyal 				MAX_SG_ENTRIES);
850a74af788SAkhil Goyal 		return NULL;
851a74af788SAkhil Goyal 	}
852a74af788SAkhil Goyal 	ctx = dpaa_sec_alloc_ctx(ses);
853a74af788SAkhil Goyal 	if (!ctx)
854a74af788SAkhil Goyal 		return NULL;
855a74af788SAkhil Goyal 
856a74af788SAkhil Goyal 	cf = &ctx->job;
857a74af788SAkhil Goyal 	ctx->op = op;
858a74af788SAkhil Goyal 	old_digest = ctx->digest;
859a74af788SAkhil Goyal 
860a74af788SAkhil Goyal 	/* output */
861a74af788SAkhil Goyal 	out_sg = &cf->sg[0];
862a74af788SAkhil Goyal 	qm_sg_entry_set64(out_sg, sym->auth.digest.phys_addr);
863a74af788SAkhil Goyal 	out_sg->length = ses->digest_length;
864a74af788SAkhil Goyal 	cpu_to_hw_sg(out_sg);
865a74af788SAkhil Goyal 
866a74af788SAkhil Goyal 	/* input */
867a74af788SAkhil Goyal 	in_sg = &cf->sg[1];
868a74af788SAkhil Goyal 	/* need to extend the input to a compound frame */
869a74af788SAkhil Goyal 	in_sg->extension = 1;
870a74af788SAkhil Goyal 	in_sg->final = 1;
871a74af788SAkhil Goyal 	in_sg->length = sym->auth.data.length;
87295456e89SShreyansh Jain 	qm_sg_entry_set64(in_sg, dpaa_mem_vtop(&cf->sg[2]));
873a74af788SAkhil Goyal 
874a74af788SAkhil Goyal 	/* 1st seg */
875a74af788SAkhil Goyal 	sg = in_sg + 1;
876a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
877a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->auth.data.offset;
878a74af788SAkhil Goyal 	sg->offset = sym->auth.data.offset;
879a74af788SAkhil Goyal 
880a74af788SAkhil Goyal 	/* Successive segs */
881a74af788SAkhil Goyal 	mbuf = mbuf->next;
882a74af788SAkhil Goyal 	while (mbuf) {
883a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
884a74af788SAkhil Goyal 		sg++;
885a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
886a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
887a74af788SAkhil Goyal 		mbuf = mbuf->next;
888a74af788SAkhil Goyal 	}
889a74af788SAkhil Goyal 
890a74af788SAkhil Goyal 	if (is_decode(ses)) {
891a74af788SAkhil Goyal 		/* Digest verification case */
892a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
893a74af788SAkhil Goyal 		sg++;
894a74af788SAkhil Goyal 		rte_memcpy(old_digest, sym->auth.digest.data,
895a74af788SAkhil Goyal 				ses->digest_length);
89695456e89SShreyansh Jain 		start_addr = dpaa_mem_vtop(old_digest);
897a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, start_addr);
898a74af788SAkhil Goyal 		sg->length = ses->digest_length;
899a74af788SAkhil Goyal 		in_sg->length += ses->digest_length;
900a74af788SAkhil Goyal 	} else {
901a74af788SAkhil Goyal 		/* Digest calculation case */
902a74af788SAkhil Goyal 		sg->length -= ses->digest_length;
903a74af788SAkhil Goyal 	}
904a74af788SAkhil Goyal 	sg->final = 1;
905a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
906a74af788SAkhil Goyal 	cpu_to_hw_sg(in_sg);
907a74af788SAkhil Goyal 
908a74af788SAkhil Goyal 	return cf;
909a74af788SAkhil Goyal }
910a74af788SAkhil Goyal 
911c3e85bdcSAkhil Goyal /**
912c3e85bdcSAkhil Goyal  * packet looks like:
913c3e85bdcSAkhil Goyal  *		|<----data_len------->|
914c3e85bdcSAkhil Goyal  *    |ip_header|ah_header|icv|payload|
915c3e85bdcSAkhil Goyal  *              ^
916c3e85bdcSAkhil Goyal  *		|
917c3e85bdcSAkhil Goyal  *	   mbuf->pkt.data
918c3e85bdcSAkhil Goyal  */
919c3e85bdcSAkhil Goyal static inline struct dpaa_sec_job *
920c3e85bdcSAkhil Goyal build_auth_only(struct rte_crypto_op *op, dpaa_sec_session *ses)
921c3e85bdcSAkhil Goyal {
922c3e85bdcSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
923c3e85bdcSAkhil Goyal 	struct rte_mbuf *mbuf = sym->m_src;
924c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
925c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
926c3e85bdcSAkhil Goyal 	struct qm_sg_entry *sg;
927c4509373SSantosh Shukla 	rte_iova_t start_addr;
928c3e85bdcSAkhil Goyal 	uint8_t *old_digest;
929c3e85bdcSAkhil Goyal 
930c3e85bdcSAkhil Goyal 	ctx = dpaa_sec_alloc_ctx(ses);
931c3e85bdcSAkhil Goyal 	if (!ctx)
932c3e85bdcSAkhil Goyal 		return NULL;
933c3e85bdcSAkhil Goyal 
934c3e85bdcSAkhil Goyal 	cf = &ctx->job;
935c3e85bdcSAkhil Goyal 	ctx->op = op;
936c3e85bdcSAkhil Goyal 	old_digest = ctx->digest;
937c3e85bdcSAkhil Goyal 
938bfa9a8a4SThomas Monjalon 	start_addr = rte_pktmbuf_iova(mbuf);
939c3e85bdcSAkhil Goyal 	/* output */
940c3e85bdcSAkhil Goyal 	sg = &cf->sg[0];
941c3e85bdcSAkhil Goyal 	qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
942c3e85bdcSAkhil Goyal 	sg->length = ses->digest_length;
943c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
944c3e85bdcSAkhil Goyal 
945c3e85bdcSAkhil Goyal 	/* input */
946c3e85bdcSAkhil Goyal 	sg = &cf->sg[1];
947c3e85bdcSAkhil Goyal 	if (is_decode(ses)) {
948c3e85bdcSAkhil Goyal 		/* need to extend the input to a compound frame */
949c3e85bdcSAkhil Goyal 		sg->extension = 1;
95095456e89SShreyansh Jain 		qm_sg_entry_set64(sg, dpaa_mem_vtop(&cf->sg[2]));
951c3e85bdcSAkhil Goyal 		sg->length = sym->auth.data.length + ses->digest_length;
952c3e85bdcSAkhil Goyal 		sg->final = 1;
953c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
954c3e85bdcSAkhil Goyal 
955c3e85bdcSAkhil Goyal 		sg = &cf->sg[2];
956c3e85bdcSAkhil Goyal 		/* hash result or digest, save digest first */
957c3e85bdcSAkhil Goyal 		rte_memcpy(old_digest, sym->auth.digest.data,
958c3e85bdcSAkhil Goyal 			   ses->digest_length);
959c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, start_addr + sym->auth.data.offset);
960c3e85bdcSAkhil Goyal 		sg->length = sym->auth.data.length;
961c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
962c3e85bdcSAkhil Goyal 
963c3e85bdcSAkhil Goyal 		/* let's check digest by hw */
96495456e89SShreyansh Jain 		start_addr = dpaa_mem_vtop(old_digest);
965c3e85bdcSAkhil Goyal 		sg++;
966c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, start_addr);
967c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
968c3e85bdcSAkhil Goyal 		sg->final = 1;
969c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
970c3e85bdcSAkhil Goyal 	} else {
971c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, start_addr + sym->auth.data.offset);
972c3e85bdcSAkhil Goyal 		sg->length = sym->auth.data.length;
973c3e85bdcSAkhil Goyal 		sg->final = 1;
974c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
975c3e85bdcSAkhil Goyal 	}
976c3e85bdcSAkhil Goyal 
977c3e85bdcSAkhil Goyal 	return cf;
978c3e85bdcSAkhil Goyal }
979c3e85bdcSAkhil Goyal 
980c3e85bdcSAkhil Goyal static inline struct dpaa_sec_job *
981a74af788SAkhil Goyal build_cipher_only_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
982a74af788SAkhil Goyal {
983a74af788SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
984a74af788SAkhil Goyal 	struct dpaa_sec_job *cf;
985a74af788SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
986a74af788SAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
987a74af788SAkhil Goyal 	struct rte_mbuf *mbuf;
988a74af788SAkhil Goyal 	uint8_t req_segs;
989a74af788SAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
990a74af788SAkhil Goyal 			ses->iv.offset);
991a74af788SAkhil Goyal 
992a74af788SAkhil Goyal 	if (sym->m_dst) {
993a74af788SAkhil Goyal 		mbuf = sym->m_dst;
994a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 3;
995a74af788SAkhil Goyal 	} else {
996a74af788SAkhil Goyal 		mbuf = sym->m_src;
997a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs * 2 + 3;
998a74af788SAkhil Goyal 	}
999a74af788SAkhil Goyal 
1000a74af788SAkhil Goyal 	if (req_segs > MAX_SG_ENTRIES) {
1001f163231eSHemant Agrawal 		DPAA_SEC_DP_ERR("Cipher: Max sec segs supported is %d",
1002a74af788SAkhil Goyal 				MAX_SG_ENTRIES);
1003a74af788SAkhil Goyal 		return NULL;
1004a74af788SAkhil Goyal 	}
1005a74af788SAkhil Goyal 
1006a74af788SAkhil Goyal 	ctx = dpaa_sec_alloc_ctx(ses);
1007a74af788SAkhil Goyal 	if (!ctx)
1008a74af788SAkhil Goyal 		return NULL;
1009a74af788SAkhil Goyal 
1010a74af788SAkhil Goyal 	cf = &ctx->job;
1011a74af788SAkhil Goyal 	ctx->op = op;
1012a74af788SAkhil Goyal 
1013a74af788SAkhil Goyal 	/* output */
1014a74af788SAkhil Goyal 	out_sg = &cf->sg[0];
1015a74af788SAkhil Goyal 	out_sg->extension = 1;
1016a74af788SAkhil Goyal 	out_sg->length = sym->cipher.data.length;
101795456e89SShreyansh Jain 	qm_sg_entry_set64(out_sg, dpaa_mem_vtop(&cf->sg[2]));
1018a74af788SAkhil Goyal 	cpu_to_hw_sg(out_sg);
1019a74af788SAkhil Goyal 
1020a74af788SAkhil Goyal 	/* 1st seg */
1021a74af788SAkhil Goyal 	sg = &cf->sg[2];
1022a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1023a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->cipher.data.offset;
1024a74af788SAkhil Goyal 	sg->offset = sym->cipher.data.offset;
1025a74af788SAkhil Goyal 
1026a74af788SAkhil Goyal 	/* Successive segs */
1027a74af788SAkhil Goyal 	mbuf = mbuf->next;
1028a74af788SAkhil Goyal 	while (mbuf) {
1029a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1030a74af788SAkhil Goyal 		sg++;
1031a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1032a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1033a74af788SAkhil Goyal 		mbuf = mbuf->next;
1034a74af788SAkhil Goyal 	}
1035a74af788SAkhil Goyal 	sg->final = 1;
1036a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1037a74af788SAkhil Goyal 
1038a74af788SAkhil Goyal 	/* input */
1039a74af788SAkhil Goyal 	mbuf = sym->m_src;
1040a74af788SAkhil Goyal 	in_sg = &cf->sg[1];
1041a74af788SAkhil Goyal 	in_sg->extension = 1;
1042a74af788SAkhil Goyal 	in_sg->final = 1;
1043a74af788SAkhil Goyal 	in_sg->length = sym->cipher.data.length + ses->iv.length;
1044a74af788SAkhil Goyal 
1045a74af788SAkhil Goyal 	sg++;
104695456e89SShreyansh Jain 	qm_sg_entry_set64(in_sg, dpaa_mem_vtop(sg));
1047a74af788SAkhil Goyal 	cpu_to_hw_sg(in_sg);
1048a74af788SAkhil Goyal 
1049a74af788SAkhil Goyal 	/* IV */
1050a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1051a74af788SAkhil Goyal 	sg->length = ses->iv.length;
1052a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1053a74af788SAkhil Goyal 
1054a74af788SAkhil Goyal 	/* 1st seg */
1055a74af788SAkhil Goyal 	sg++;
1056a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1057a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->cipher.data.offset;
1058a74af788SAkhil Goyal 	sg->offset = sym->cipher.data.offset;
1059a74af788SAkhil Goyal 
1060a74af788SAkhil Goyal 	/* Successive segs */
1061a74af788SAkhil Goyal 	mbuf = mbuf->next;
1062a74af788SAkhil Goyal 	while (mbuf) {
1063a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1064a74af788SAkhil Goyal 		sg++;
1065a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1066a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1067a74af788SAkhil Goyal 		mbuf = mbuf->next;
1068a74af788SAkhil Goyal 	}
1069a74af788SAkhil Goyal 	sg->final = 1;
1070a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1071a74af788SAkhil Goyal 
1072a74af788SAkhil Goyal 	return cf;
1073a74af788SAkhil Goyal }
1074a74af788SAkhil Goyal 
1075a74af788SAkhil Goyal static inline struct dpaa_sec_job *
1076c3e85bdcSAkhil Goyal build_cipher_only(struct rte_crypto_op *op, dpaa_sec_session *ses)
1077c3e85bdcSAkhil Goyal {
1078c3e85bdcSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1079c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
1080c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1081c3e85bdcSAkhil Goyal 	struct qm_sg_entry *sg;
1082c4509373SSantosh Shukla 	rte_iova_t src_start_addr, dst_start_addr;
1083c3e85bdcSAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1084c3e85bdcSAkhil Goyal 			ses->iv.offset);
1085c3e85bdcSAkhil Goyal 
1086c3e85bdcSAkhil Goyal 	ctx = dpaa_sec_alloc_ctx(ses);
1087c3e85bdcSAkhil Goyal 	if (!ctx)
1088c3e85bdcSAkhil Goyal 		return NULL;
1089c3e85bdcSAkhil Goyal 
1090c3e85bdcSAkhil Goyal 	cf = &ctx->job;
1091c3e85bdcSAkhil Goyal 	ctx->op = op;
1092a389434eSAlok Makhariya 
1093bfa9a8a4SThomas Monjalon 	src_start_addr = rte_pktmbuf_iova(sym->m_src);
1094a389434eSAlok Makhariya 
1095a389434eSAlok Makhariya 	if (sym->m_dst)
1096bfa9a8a4SThomas Monjalon 		dst_start_addr = rte_pktmbuf_iova(sym->m_dst);
1097a389434eSAlok Makhariya 	else
1098a389434eSAlok Makhariya 		dst_start_addr = src_start_addr;
1099c3e85bdcSAkhil Goyal 
1100c3e85bdcSAkhil Goyal 	/* output */
1101c3e85bdcSAkhil Goyal 	sg = &cf->sg[0];
1102a389434eSAlok Makhariya 	qm_sg_entry_set64(sg, dst_start_addr + sym->cipher.data.offset);
1103c3e85bdcSAkhil Goyal 	sg->length = sym->cipher.data.length + ses->iv.length;
1104c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1105c3e85bdcSAkhil Goyal 
1106c3e85bdcSAkhil Goyal 	/* input */
1107c3e85bdcSAkhil Goyal 	sg = &cf->sg[1];
1108c3e85bdcSAkhil Goyal 
1109c3e85bdcSAkhil Goyal 	/* need to extend the input to a compound frame */
1110c3e85bdcSAkhil Goyal 	sg->extension = 1;
1111c3e85bdcSAkhil Goyal 	sg->final = 1;
1112c3e85bdcSAkhil Goyal 	sg->length = sym->cipher.data.length + ses->iv.length;
111395456e89SShreyansh Jain 	qm_sg_entry_set64(sg, dpaa_mem_vtop(&cf->sg[2]));
1114c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1115c3e85bdcSAkhil Goyal 
1116c3e85bdcSAkhil Goyal 	sg = &cf->sg[2];
1117c3e85bdcSAkhil Goyal 	qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1118c3e85bdcSAkhil Goyal 	sg->length = ses->iv.length;
1119c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1120c3e85bdcSAkhil Goyal 
1121c3e85bdcSAkhil Goyal 	sg++;
1122a389434eSAlok Makhariya 	qm_sg_entry_set64(sg, src_start_addr + sym->cipher.data.offset);
1123c3e85bdcSAkhil Goyal 	sg->length = sym->cipher.data.length;
1124c3e85bdcSAkhil Goyal 	sg->final = 1;
1125c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1126c3e85bdcSAkhil Goyal 
1127c3e85bdcSAkhil Goyal 	return cf;
1128c3e85bdcSAkhil Goyal }
1129c3e85bdcSAkhil Goyal 
1130c3e85bdcSAkhil Goyal static inline struct dpaa_sec_job *
1131a74af788SAkhil Goyal build_cipher_auth_gcm_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
1132a74af788SAkhil Goyal {
1133a74af788SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1134a74af788SAkhil Goyal 	struct dpaa_sec_job *cf;
1135a74af788SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1136a74af788SAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
1137a74af788SAkhil Goyal 	struct rte_mbuf *mbuf;
1138a74af788SAkhil Goyal 	uint8_t req_segs;
1139a74af788SAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1140a74af788SAkhil Goyal 			ses->iv.offset);
1141a74af788SAkhil Goyal 
1142a74af788SAkhil Goyal 	if (sym->m_dst) {
1143a74af788SAkhil Goyal 		mbuf = sym->m_dst;
1144a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 4;
1145a74af788SAkhil Goyal 	} else {
1146a74af788SAkhil Goyal 		mbuf = sym->m_src;
1147a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs * 2 + 4;
1148a74af788SAkhil Goyal 	}
1149a74af788SAkhil Goyal 
1150a74af788SAkhil Goyal 	if (ses->auth_only_len)
1151a74af788SAkhil Goyal 		req_segs++;
1152a74af788SAkhil Goyal 
1153a74af788SAkhil Goyal 	if (req_segs > MAX_SG_ENTRIES) {
1154f163231eSHemant Agrawal 		DPAA_SEC_DP_ERR("AEAD: Max sec segs supported is %d",
1155a74af788SAkhil Goyal 				MAX_SG_ENTRIES);
1156a74af788SAkhil Goyal 		return NULL;
1157a74af788SAkhil Goyal 	}
1158a74af788SAkhil Goyal 
1159a74af788SAkhil Goyal 	ctx = dpaa_sec_alloc_ctx(ses);
1160a74af788SAkhil Goyal 	if (!ctx)
1161a74af788SAkhil Goyal 		return NULL;
1162a74af788SAkhil Goyal 
1163a74af788SAkhil Goyal 	cf = &ctx->job;
1164a74af788SAkhil Goyal 	ctx->op = op;
1165a74af788SAkhil Goyal 
1166a74af788SAkhil Goyal 	rte_prefetch0(cf->sg);
1167a74af788SAkhil Goyal 
1168a74af788SAkhil Goyal 	/* output */
1169a74af788SAkhil Goyal 	out_sg = &cf->sg[0];
1170a74af788SAkhil Goyal 	out_sg->extension = 1;
1171a74af788SAkhil Goyal 	if (is_encode(ses))
1172a74af788SAkhil Goyal 		out_sg->length = sym->aead.data.length + ses->auth_only_len
1173a74af788SAkhil Goyal 						+ ses->digest_length;
1174a74af788SAkhil Goyal 	else
1175a74af788SAkhil Goyal 		out_sg->length = sym->aead.data.length + ses->auth_only_len;
1176a74af788SAkhil Goyal 
1177a74af788SAkhil Goyal 	/* output sg entries */
1178a74af788SAkhil Goyal 	sg = &cf->sg[2];
117995456e89SShreyansh Jain 	qm_sg_entry_set64(out_sg, dpaa_mem_vtop(sg));
1180a74af788SAkhil Goyal 	cpu_to_hw_sg(out_sg);
1181a74af788SAkhil Goyal 
1182a74af788SAkhil Goyal 	/* 1st seg */
1183a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1184a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->aead.data.offset +
1185a74af788SAkhil Goyal 					ses->auth_only_len;
1186a74af788SAkhil Goyal 	sg->offset = sym->aead.data.offset - ses->auth_only_len;
1187a74af788SAkhil Goyal 
1188a74af788SAkhil Goyal 	/* Successive segs */
1189a74af788SAkhil Goyal 	mbuf = mbuf->next;
1190a74af788SAkhil Goyal 	while (mbuf) {
1191a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1192a74af788SAkhil Goyal 		sg++;
1193a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1194a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1195a74af788SAkhil Goyal 		mbuf = mbuf->next;
1196a74af788SAkhil Goyal 	}
1197a74af788SAkhil Goyal 	sg->length -= ses->digest_length;
1198a74af788SAkhil Goyal 
1199a74af788SAkhil Goyal 	if (is_encode(ses)) {
1200a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1201a74af788SAkhil Goyal 		/* set auth output */
1202a74af788SAkhil Goyal 		sg++;
1203a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, sym->aead.digest.phys_addr);
1204a74af788SAkhil Goyal 		sg->length = ses->digest_length;
1205a74af788SAkhil Goyal 	}
1206a74af788SAkhil Goyal 	sg->final = 1;
1207a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1208a74af788SAkhil Goyal 
1209a74af788SAkhil Goyal 	/* input */
1210a74af788SAkhil Goyal 	mbuf = sym->m_src;
1211a74af788SAkhil Goyal 	in_sg = &cf->sg[1];
1212a74af788SAkhil Goyal 	in_sg->extension = 1;
1213a74af788SAkhil Goyal 	in_sg->final = 1;
1214a74af788SAkhil Goyal 	if (is_encode(ses))
1215a74af788SAkhil Goyal 		in_sg->length = ses->iv.length + sym->aead.data.length
1216a74af788SAkhil Goyal 							+ ses->auth_only_len;
1217a74af788SAkhil Goyal 	else
1218a74af788SAkhil Goyal 		in_sg->length = ses->iv.length + sym->aead.data.length
1219a74af788SAkhil Goyal 				+ ses->auth_only_len + ses->digest_length;
1220a74af788SAkhil Goyal 
1221a74af788SAkhil Goyal 	/* input sg entries */
1222a74af788SAkhil Goyal 	sg++;
122395456e89SShreyansh Jain 	qm_sg_entry_set64(in_sg, dpaa_mem_vtop(sg));
1224a74af788SAkhil Goyal 	cpu_to_hw_sg(in_sg);
1225a74af788SAkhil Goyal 
1226a74af788SAkhil Goyal 	/* 1st seg IV */
1227a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1228a74af788SAkhil Goyal 	sg->length = ses->iv.length;
1229a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1230a74af788SAkhil Goyal 
1231a74af788SAkhil Goyal 	/* 2nd seg auth only */
1232a74af788SAkhil Goyal 	if (ses->auth_only_len) {
1233a74af788SAkhil Goyal 		sg++;
1234a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, dpaa_mem_vtop(sym->aead.aad.data));
1235a74af788SAkhil Goyal 		sg->length = ses->auth_only_len;
1236a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1237a74af788SAkhil Goyal 	}
1238a74af788SAkhil Goyal 
1239a74af788SAkhil Goyal 	/* 3rd seg */
1240a74af788SAkhil Goyal 	sg++;
1241a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1242a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->aead.data.offset;
1243a74af788SAkhil Goyal 	sg->offset = sym->aead.data.offset;
1244a74af788SAkhil Goyal 
1245a74af788SAkhil Goyal 	/* Successive segs */
1246a74af788SAkhil Goyal 	mbuf = mbuf->next;
1247a74af788SAkhil Goyal 	while (mbuf) {
1248a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1249a74af788SAkhil Goyal 		sg++;
1250a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1251a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1252a74af788SAkhil Goyal 		mbuf = mbuf->next;
1253a74af788SAkhil Goyal 	}
1254a74af788SAkhil Goyal 
1255a74af788SAkhil Goyal 	if (is_decode(ses)) {
1256a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1257a74af788SAkhil Goyal 		sg++;
1258a74af788SAkhil Goyal 		memcpy(ctx->digest, sym->aead.digest.data,
1259a74af788SAkhil Goyal 			ses->digest_length);
126095456e89SShreyansh Jain 		qm_sg_entry_set64(sg, dpaa_mem_vtop(ctx->digest));
1261a74af788SAkhil Goyal 		sg->length = ses->digest_length;
1262a74af788SAkhil Goyal 	}
1263a74af788SAkhil Goyal 	sg->final = 1;
1264a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1265a74af788SAkhil Goyal 
1266a74af788SAkhil Goyal 	return cf;
1267a74af788SAkhil Goyal }
1268a74af788SAkhil Goyal 
1269a74af788SAkhil Goyal static inline struct dpaa_sec_job *
1270c3e85bdcSAkhil Goyal build_cipher_auth_gcm(struct rte_crypto_op *op, dpaa_sec_session *ses)
1271c3e85bdcSAkhil Goyal {
1272c3e85bdcSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1273c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
1274c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1275c3e85bdcSAkhil Goyal 	struct qm_sg_entry *sg;
1276c3e85bdcSAkhil Goyal 	uint32_t length = 0;
1277c4509373SSantosh Shukla 	rte_iova_t src_start_addr, dst_start_addr;
1278c3e85bdcSAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1279c3e85bdcSAkhil Goyal 			ses->iv.offset);
1280c3e85bdcSAkhil Goyal 
1281116ff44aSHemant Agrawal 	src_start_addr = sym->m_src->buf_iova + sym->m_src->data_off;
1282a389434eSAlok Makhariya 
1283a389434eSAlok Makhariya 	if (sym->m_dst)
1284116ff44aSHemant Agrawal 		dst_start_addr = sym->m_dst->buf_iova + sym->m_dst->data_off;
1285a389434eSAlok Makhariya 	else
1286a389434eSAlok Makhariya 		dst_start_addr = src_start_addr;
1287c3e85bdcSAkhil Goyal 
1288c3e85bdcSAkhil Goyal 	ctx = dpaa_sec_alloc_ctx(ses);
1289c3e85bdcSAkhil Goyal 	if (!ctx)
1290c3e85bdcSAkhil Goyal 		return NULL;
1291c3e85bdcSAkhil Goyal 
1292c3e85bdcSAkhil Goyal 	cf = &ctx->job;
1293c3e85bdcSAkhil Goyal 	ctx->op = op;
1294c3e85bdcSAkhil Goyal 
1295c3e85bdcSAkhil Goyal 	/* input */
1296c3e85bdcSAkhil Goyal 	rte_prefetch0(cf->sg);
1297c3e85bdcSAkhil Goyal 	sg = &cf->sg[2];
129895456e89SShreyansh Jain 	qm_sg_entry_set64(&cf->sg[1], dpaa_mem_vtop(sg));
1299c3e85bdcSAkhil Goyal 	if (is_encode(ses)) {
1300c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1301c3e85bdcSAkhil Goyal 		sg->length = ses->iv.length;
1302c3e85bdcSAkhil Goyal 		length += sg->length;
1303c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1304c3e85bdcSAkhil Goyal 
1305c3e85bdcSAkhil Goyal 		sg++;
1306c3e85bdcSAkhil Goyal 		if (ses->auth_only_len) {
1307c3e85bdcSAkhil Goyal 			qm_sg_entry_set64(sg,
1308c3e85bdcSAkhil Goyal 					  dpaa_mem_vtop(sym->aead.aad.data));
1309c3e85bdcSAkhil Goyal 			sg->length = ses->auth_only_len;
1310c3e85bdcSAkhil Goyal 			length += sg->length;
1311c3e85bdcSAkhil Goyal 			cpu_to_hw_sg(sg);
1312c3e85bdcSAkhil Goyal 			sg++;
1313c3e85bdcSAkhil Goyal 		}
1314a389434eSAlok Makhariya 		qm_sg_entry_set64(sg, src_start_addr + sym->aead.data.offset);
1315c3e85bdcSAkhil Goyal 		sg->length = sym->aead.data.length;
1316c3e85bdcSAkhil Goyal 		length += sg->length;
1317c3e85bdcSAkhil Goyal 		sg->final = 1;
1318c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1319c3e85bdcSAkhil Goyal 	} else {
1320c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1321c3e85bdcSAkhil Goyal 		sg->length = ses->iv.length;
1322c3e85bdcSAkhil Goyal 		length += sg->length;
1323c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1324c3e85bdcSAkhil Goyal 
1325c3e85bdcSAkhil Goyal 		sg++;
1326c3e85bdcSAkhil Goyal 		if (ses->auth_only_len) {
1327c3e85bdcSAkhil Goyal 			qm_sg_entry_set64(sg,
1328c3e85bdcSAkhil Goyal 					  dpaa_mem_vtop(sym->aead.aad.data));
1329c3e85bdcSAkhil Goyal 			sg->length = ses->auth_only_len;
1330c3e85bdcSAkhil Goyal 			length += sg->length;
1331c3e85bdcSAkhil Goyal 			cpu_to_hw_sg(sg);
1332c3e85bdcSAkhil Goyal 			sg++;
1333c3e85bdcSAkhil Goyal 		}
1334a389434eSAlok Makhariya 		qm_sg_entry_set64(sg, src_start_addr + sym->aead.data.offset);
1335c3e85bdcSAkhil Goyal 		sg->length = sym->aead.data.length;
1336c3e85bdcSAkhil Goyal 		length += sg->length;
1337c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1338c3e85bdcSAkhil Goyal 
1339c3e85bdcSAkhil Goyal 		memcpy(ctx->digest, sym->aead.digest.data,
1340c3e85bdcSAkhil Goyal 		       ses->digest_length);
1341c3e85bdcSAkhil Goyal 		sg++;
1342c3e85bdcSAkhil Goyal 
134395456e89SShreyansh Jain 		qm_sg_entry_set64(sg, dpaa_mem_vtop(ctx->digest));
1344c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
1345c3e85bdcSAkhil Goyal 		length += sg->length;
1346c3e85bdcSAkhil Goyal 		sg->final = 1;
1347c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1348c3e85bdcSAkhil Goyal 	}
1349c3e85bdcSAkhil Goyal 	/* input compound frame */
1350c3e85bdcSAkhil Goyal 	cf->sg[1].length = length;
1351c3e85bdcSAkhil Goyal 	cf->sg[1].extension = 1;
1352c3e85bdcSAkhil Goyal 	cf->sg[1].final = 1;
1353c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(&cf->sg[1]);
1354c3e85bdcSAkhil Goyal 
1355c3e85bdcSAkhil Goyal 	/* output */
1356c3e85bdcSAkhil Goyal 	sg++;
135795456e89SShreyansh Jain 	qm_sg_entry_set64(&cf->sg[0], dpaa_mem_vtop(sg));
1358c3e85bdcSAkhil Goyal 	qm_sg_entry_set64(sg,
1359a389434eSAlok Makhariya 		dst_start_addr + sym->aead.data.offset - ses->auth_only_len);
1360c3e85bdcSAkhil Goyal 	sg->length = sym->aead.data.length + ses->auth_only_len;
1361c3e85bdcSAkhil Goyal 	length = sg->length;
1362c3e85bdcSAkhil Goyal 	if (is_encode(ses)) {
1363c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1364c3e85bdcSAkhil Goyal 		/* set auth output */
1365c3e85bdcSAkhil Goyal 		sg++;
1366c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, sym->aead.digest.phys_addr);
1367c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
1368c3e85bdcSAkhil Goyal 		length += sg->length;
1369c3e85bdcSAkhil Goyal 	}
1370c3e85bdcSAkhil Goyal 	sg->final = 1;
1371c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1372c3e85bdcSAkhil Goyal 
1373c3e85bdcSAkhil Goyal 	/* output compound frame */
1374c3e85bdcSAkhil Goyal 	cf->sg[0].length = length;
1375c3e85bdcSAkhil Goyal 	cf->sg[0].extension = 1;
1376c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(&cf->sg[0]);
1377c3e85bdcSAkhil Goyal 
1378c3e85bdcSAkhil Goyal 	return cf;
1379c3e85bdcSAkhil Goyal }
1380c3e85bdcSAkhil Goyal 
1381c3e85bdcSAkhil Goyal static inline struct dpaa_sec_job *
1382a74af788SAkhil Goyal build_cipher_auth_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
1383a74af788SAkhil Goyal {
1384a74af788SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1385a74af788SAkhil Goyal 	struct dpaa_sec_job *cf;
1386a74af788SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1387a74af788SAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
1388a74af788SAkhil Goyal 	struct rte_mbuf *mbuf;
1389a74af788SAkhil Goyal 	uint8_t req_segs;
1390a74af788SAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1391a74af788SAkhil Goyal 			ses->iv.offset);
1392a74af788SAkhil Goyal 
1393a74af788SAkhil Goyal 	if (sym->m_dst) {
1394a74af788SAkhil Goyal 		mbuf = sym->m_dst;
1395a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 4;
1396a74af788SAkhil Goyal 	} else {
1397a74af788SAkhil Goyal 		mbuf = sym->m_src;
1398a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs * 2 + 4;
1399a74af788SAkhil Goyal 	}
1400a74af788SAkhil Goyal 
1401a74af788SAkhil Goyal 	if (req_segs > MAX_SG_ENTRIES) {
1402f163231eSHemant Agrawal 		DPAA_SEC_DP_ERR("Cipher-Auth: Max sec segs supported is %d",
1403a74af788SAkhil Goyal 				MAX_SG_ENTRIES);
1404a74af788SAkhil Goyal 		return NULL;
1405a74af788SAkhil Goyal 	}
1406a74af788SAkhil Goyal 
1407a74af788SAkhil Goyal 	ctx = dpaa_sec_alloc_ctx(ses);
1408a74af788SAkhil Goyal 	if (!ctx)
1409a74af788SAkhil Goyal 		return NULL;
1410a74af788SAkhil Goyal 
1411a74af788SAkhil Goyal 	cf = &ctx->job;
1412a74af788SAkhil Goyal 	ctx->op = op;
1413a74af788SAkhil Goyal 
1414a74af788SAkhil Goyal 	rte_prefetch0(cf->sg);
1415a74af788SAkhil Goyal 
1416a74af788SAkhil Goyal 	/* output */
1417a74af788SAkhil Goyal 	out_sg = &cf->sg[0];
1418a74af788SAkhil Goyal 	out_sg->extension = 1;
1419a74af788SAkhil Goyal 	if (is_encode(ses))
1420a74af788SAkhil Goyal 		out_sg->length = sym->auth.data.length + ses->digest_length;
1421a74af788SAkhil Goyal 	else
1422a74af788SAkhil Goyal 		out_sg->length = sym->auth.data.length;
1423a74af788SAkhil Goyal 
1424a74af788SAkhil Goyal 	/* output sg entries */
1425a74af788SAkhil Goyal 	sg = &cf->sg[2];
142695456e89SShreyansh Jain 	qm_sg_entry_set64(out_sg, dpaa_mem_vtop(sg));
1427a74af788SAkhil Goyal 	cpu_to_hw_sg(out_sg);
1428a74af788SAkhil Goyal 
1429a74af788SAkhil Goyal 	/* 1st seg */
1430a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1431a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->auth.data.offset;
1432a74af788SAkhil Goyal 	sg->offset = sym->auth.data.offset;
1433a74af788SAkhil Goyal 
1434a74af788SAkhil Goyal 	/* Successive segs */
1435a74af788SAkhil Goyal 	mbuf = mbuf->next;
1436a74af788SAkhil Goyal 	while (mbuf) {
1437a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1438a74af788SAkhil Goyal 		sg++;
1439a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1440a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1441a74af788SAkhil Goyal 		mbuf = mbuf->next;
1442a74af788SAkhil Goyal 	}
1443a74af788SAkhil Goyal 	sg->length -= ses->digest_length;
1444a74af788SAkhil Goyal 
1445a74af788SAkhil Goyal 	if (is_encode(ses)) {
1446a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1447a74af788SAkhil Goyal 		/* set auth output */
1448a74af788SAkhil Goyal 		sg++;
1449a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
1450a74af788SAkhil Goyal 		sg->length = ses->digest_length;
1451a74af788SAkhil Goyal 	}
1452a74af788SAkhil Goyal 	sg->final = 1;
1453a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1454a74af788SAkhil Goyal 
1455a74af788SAkhil Goyal 	/* input */
1456a74af788SAkhil Goyal 	mbuf = sym->m_src;
1457a74af788SAkhil Goyal 	in_sg = &cf->sg[1];
1458a74af788SAkhil Goyal 	in_sg->extension = 1;
1459a74af788SAkhil Goyal 	in_sg->final = 1;
1460a74af788SAkhil Goyal 	if (is_encode(ses))
1461a74af788SAkhil Goyal 		in_sg->length = ses->iv.length + sym->auth.data.length;
1462a74af788SAkhil Goyal 	else
1463a74af788SAkhil Goyal 		in_sg->length = ses->iv.length + sym->auth.data.length
1464a74af788SAkhil Goyal 						+ ses->digest_length;
1465a74af788SAkhil Goyal 
1466a74af788SAkhil Goyal 	/* input sg entries */
1467a74af788SAkhil Goyal 	sg++;
146895456e89SShreyansh Jain 	qm_sg_entry_set64(in_sg, dpaa_mem_vtop(sg));
1469a74af788SAkhil Goyal 	cpu_to_hw_sg(in_sg);
1470a74af788SAkhil Goyal 
1471a74af788SAkhil Goyal 	/* 1st seg IV */
1472a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1473a74af788SAkhil Goyal 	sg->length = ses->iv.length;
1474a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1475a74af788SAkhil Goyal 
1476a74af788SAkhil Goyal 	/* 2nd seg */
1477a74af788SAkhil Goyal 	sg++;
1478a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1479a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->auth.data.offset;
1480a74af788SAkhil Goyal 	sg->offset = sym->auth.data.offset;
1481a74af788SAkhil Goyal 
1482a74af788SAkhil Goyal 	/* Successive segs */
1483a74af788SAkhil Goyal 	mbuf = mbuf->next;
1484a74af788SAkhil Goyal 	while (mbuf) {
1485a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1486a74af788SAkhil Goyal 		sg++;
1487a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1488a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1489a74af788SAkhil Goyal 		mbuf = mbuf->next;
1490a74af788SAkhil Goyal 	}
1491a74af788SAkhil Goyal 
1492a74af788SAkhil Goyal 	sg->length -= ses->digest_length;
1493a74af788SAkhil Goyal 	if (is_decode(ses)) {
1494a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1495a74af788SAkhil Goyal 		sg++;
1496a74af788SAkhil Goyal 		memcpy(ctx->digest, sym->auth.digest.data,
1497a74af788SAkhil Goyal 			ses->digest_length);
149895456e89SShreyansh Jain 		qm_sg_entry_set64(sg, dpaa_mem_vtop(ctx->digest));
1499a74af788SAkhil Goyal 		sg->length = ses->digest_length;
1500a74af788SAkhil Goyal 	}
1501a74af788SAkhil Goyal 	sg->final = 1;
1502a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1503a74af788SAkhil Goyal 
1504a74af788SAkhil Goyal 	return cf;
1505a74af788SAkhil Goyal }
1506a74af788SAkhil Goyal 
1507a74af788SAkhil Goyal static inline struct dpaa_sec_job *
1508c3e85bdcSAkhil Goyal build_cipher_auth(struct rte_crypto_op *op, dpaa_sec_session *ses)
1509c3e85bdcSAkhil Goyal {
1510c3e85bdcSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1511c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
1512c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1513c3e85bdcSAkhil Goyal 	struct qm_sg_entry *sg;
1514c4509373SSantosh Shukla 	rte_iova_t src_start_addr, dst_start_addr;
1515c3e85bdcSAkhil Goyal 	uint32_t length = 0;
1516c3e85bdcSAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1517c3e85bdcSAkhil Goyal 			ses->iv.offset);
1518c3e85bdcSAkhil Goyal 
1519455da545SSantosh Shukla 	src_start_addr = sym->m_src->buf_iova + sym->m_src->data_off;
1520a389434eSAlok Makhariya 	if (sym->m_dst)
1521455da545SSantosh Shukla 		dst_start_addr = sym->m_dst->buf_iova + sym->m_dst->data_off;
1522a389434eSAlok Makhariya 	else
1523a389434eSAlok Makhariya 		dst_start_addr = src_start_addr;
1524c3e85bdcSAkhil Goyal 
1525c3e85bdcSAkhil Goyal 	ctx = dpaa_sec_alloc_ctx(ses);
1526c3e85bdcSAkhil Goyal 	if (!ctx)
1527c3e85bdcSAkhil Goyal 		return NULL;
1528c3e85bdcSAkhil Goyal 
1529c3e85bdcSAkhil Goyal 	cf = &ctx->job;
1530c3e85bdcSAkhil Goyal 	ctx->op = op;
1531c3e85bdcSAkhil Goyal 
1532c3e85bdcSAkhil Goyal 	/* input */
1533c3e85bdcSAkhil Goyal 	rte_prefetch0(cf->sg);
1534c3e85bdcSAkhil Goyal 	sg = &cf->sg[2];
153595456e89SShreyansh Jain 	qm_sg_entry_set64(&cf->sg[1], dpaa_mem_vtop(sg));
1536c3e85bdcSAkhil Goyal 	if (is_encode(ses)) {
1537c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1538c3e85bdcSAkhil Goyal 		sg->length = ses->iv.length;
1539c3e85bdcSAkhil Goyal 		length += sg->length;
1540c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1541c3e85bdcSAkhil Goyal 
1542c3e85bdcSAkhil Goyal 		sg++;
1543a389434eSAlok Makhariya 		qm_sg_entry_set64(sg, src_start_addr + sym->auth.data.offset);
1544c3e85bdcSAkhil Goyal 		sg->length = sym->auth.data.length;
1545c3e85bdcSAkhil Goyal 		length += sg->length;
1546c3e85bdcSAkhil Goyal 		sg->final = 1;
1547c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1548c3e85bdcSAkhil Goyal 	} else {
1549c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1550c3e85bdcSAkhil Goyal 		sg->length = ses->iv.length;
1551c3e85bdcSAkhil Goyal 		length += sg->length;
1552c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1553c3e85bdcSAkhil Goyal 
1554c3e85bdcSAkhil Goyal 		sg++;
1555c3e85bdcSAkhil Goyal 
1556a389434eSAlok Makhariya 		qm_sg_entry_set64(sg, src_start_addr + sym->auth.data.offset);
1557c3e85bdcSAkhil Goyal 		sg->length = sym->auth.data.length;
1558c3e85bdcSAkhil Goyal 		length += sg->length;
1559c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1560c3e85bdcSAkhil Goyal 
1561c3e85bdcSAkhil Goyal 		memcpy(ctx->digest, sym->auth.digest.data,
1562c3e85bdcSAkhil Goyal 		       ses->digest_length);
1563c3e85bdcSAkhil Goyal 		sg++;
1564c3e85bdcSAkhil Goyal 
156595456e89SShreyansh Jain 		qm_sg_entry_set64(sg, dpaa_mem_vtop(ctx->digest));
1566c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
1567c3e85bdcSAkhil Goyal 		length += sg->length;
1568c3e85bdcSAkhil Goyal 		sg->final = 1;
1569c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1570c3e85bdcSAkhil Goyal 	}
1571c3e85bdcSAkhil Goyal 	/* input compound frame */
1572c3e85bdcSAkhil Goyal 	cf->sg[1].length = length;
1573c3e85bdcSAkhil Goyal 	cf->sg[1].extension = 1;
1574c3e85bdcSAkhil Goyal 	cf->sg[1].final = 1;
1575c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(&cf->sg[1]);
1576c3e85bdcSAkhil Goyal 
1577c3e85bdcSAkhil Goyal 	/* output */
1578c3e85bdcSAkhil Goyal 	sg++;
157995456e89SShreyansh Jain 	qm_sg_entry_set64(&cf->sg[0], dpaa_mem_vtop(sg));
1580a389434eSAlok Makhariya 	qm_sg_entry_set64(sg, dst_start_addr + sym->cipher.data.offset);
1581c3e85bdcSAkhil Goyal 	sg->length = sym->cipher.data.length;
1582c3e85bdcSAkhil Goyal 	length = sg->length;
1583c3e85bdcSAkhil Goyal 	if (is_encode(ses)) {
1584c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1585c3e85bdcSAkhil Goyal 		/* set auth output */
1586c3e85bdcSAkhil Goyal 		sg++;
1587c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
1588c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
1589c3e85bdcSAkhil Goyal 		length += sg->length;
1590c3e85bdcSAkhil Goyal 	}
1591c3e85bdcSAkhil Goyal 	sg->final = 1;
1592c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1593c3e85bdcSAkhil Goyal 
1594c3e85bdcSAkhil Goyal 	/* output compound frame */
1595c3e85bdcSAkhil Goyal 	cf->sg[0].length = length;
1596c3e85bdcSAkhil Goyal 	cf->sg[0].extension = 1;
1597c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(&cf->sg[0]);
1598c3e85bdcSAkhil Goyal 
1599c3e85bdcSAkhil Goyal 	return cf;
1600c3e85bdcSAkhil Goyal }
1601c3e85bdcSAkhil Goyal 
16021f14d500SAkhil Goyal static inline struct dpaa_sec_job *
16031f14d500SAkhil Goyal build_proto(struct rte_crypto_op *op, dpaa_sec_session *ses)
16041f14d500SAkhil Goyal {
16051f14d500SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
16061f14d500SAkhil Goyal 	struct dpaa_sec_job *cf;
16071f14d500SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
16081f14d500SAkhil Goyal 	struct qm_sg_entry *sg;
16091f14d500SAkhil Goyal 	phys_addr_t src_start_addr, dst_start_addr;
16101f14d500SAkhil Goyal 
16111f14d500SAkhil Goyal 	ctx = dpaa_sec_alloc_ctx(ses);
16121f14d500SAkhil Goyal 	if (!ctx)
16131f14d500SAkhil Goyal 		return NULL;
16141f14d500SAkhil Goyal 	cf = &ctx->job;
16151f14d500SAkhil Goyal 	ctx->op = op;
16161f14d500SAkhil Goyal 
16171f14d500SAkhil Goyal 	src_start_addr = rte_pktmbuf_mtophys(sym->m_src);
16181f14d500SAkhil Goyal 
16191f14d500SAkhil Goyal 	if (sym->m_dst)
16201f14d500SAkhil Goyal 		dst_start_addr = rte_pktmbuf_mtophys(sym->m_dst);
16211f14d500SAkhil Goyal 	else
16221f14d500SAkhil Goyal 		dst_start_addr = src_start_addr;
16231f14d500SAkhil Goyal 
16241f14d500SAkhil Goyal 	/* input */
16251f14d500SAkhil Goyal 	sg = &cf->sg[1];
16261f14d500SAkhil Goyal 	qm_sg_entry_set64(sg, src_start_addr);
16271f14d500SAkhil Goyal 	sg->length = sym->m_src->pkt_len;
16281f14d500SAkhil Goyal 	sg->final = 1;
16291f14d500SAkhil Goyal 	cpu_to_hw_sg(sg);
16301f14d500SAkhil Goyal 
16311f14d500SAkhil Goyal 	sym->m_src->packet_type &= ~RTE_PTYPE_L4_MASK;
16321f14d500SAkhil Goyal 	/* output */
16331f14d500SAkhil Goyal 	sg = &cf->sg[0];
16341f14d500SAkhil Goyal 	qm_sg_entry_set64(sg, dst_start_addr);
16351f14d500SAkhil Goyal 	sg->length = sym->m_src->buf_len - sym->m_src->data_off;
16361f14d500SAkhil Goyal 	cpu_to_hw_sg(sg);
16371f14d500SAkhil Goyal 
16381f14d500SAkhil Goyal 	return cf;
16391f14d500SAkhil Goyal }
16401f14d500SAkhil Goyal 
16419a984458SAkhil Goyal static uint16_t
16429a984458SAkhil Goyal dpaa_sec_enqueue_burst(void *qp, struct rte_crypto_op **ops,
16439a984458SAkhil Goyal 		       uint16_t nb_ops)
1644c3e85bdcSAkhil Goyal {
16459a984458SAkhil Goyal 	/* Function to transmit the frames to given device and queuepair */
16469a984458SAkhil Goyal 	uint32_t loop;
16479a984458SAkhil Goyal 	struct dpaa_sec_qp *dpaa_qp = (struct dpaa_sec_qp *)qp;
16489a984458SAkhil Goyal 	uint16_t num_tx = 0;
16499a984458SAkhil Goyal 	struct qm_fd fds[DPAA_SEC_BURST], *fd;
16509a984458SAkhil Goyal 	uint32_t frames_to_send;
16519a984458SAkhil Goyal 	struct rte_crypto_op *op;
1652c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
1653c3e85bdcSAkhil Goyal 	dpaa_sec_session *ses;
16549a984458SAkhil Goyal 	uint32_t auth_only_len;
16559a984458SAkhil Goyal 	struct qman_fq *inq[DPAA_SEC_BURST];
1656c3e85bdcSAkhil Goyal 
16579a984458SAkhil Goyal 	while (nb_ops) {
16589a984458SAkhil Goyal 		frames_to_send = (nb_ops > DPAA_SEC_BURST) ?
16599a984458SAkhil Goyal 				DPAA_SEC_BURST : nb_ops;
16609a984458SAkhil Goyal 		for (loop = 0; loop < frames_to_send; loop++) {
16619a984458SAkhil Goyal 			op = *(ops++);
16629a984458SAkhil Goyal 			switch (op->sess_type) {
16639a984458SAkhil Goyal 			case RTE_CRYPTO_OP_WITH_SESSION:
16649a984458SAkhil Goyal 				ses = (dpaa_sec_session *)
1665012c5076SPablo de Lara 					get_sym_session_private_data(
16669a984458SAkhil Goyal 							op->sym->session,
16679a984458SAkhil Goyal 							cryptodev_driver_id);
16689a984458SAkhil Goyal 				break;
16699a984458SAkhil Goyal 			case RTE_CRYPTO_OP_SECURITY_SESSION:
16709a984458SAkhil Goyal 				ses = (dpaa_sec_session *)
16719a984458SAkhil Goyal 					get_sec_session_private_data(
16721f14d500SAkhil Goyal 							op->sym->sec_session);
16739a984458SAkhil Goyal 				break;
16749a984458SAkhil Goyal 			default:
1675f163231eSHemant Agrawal 				DPAA_SEC_DP_ERR(
16769a984458SAkhil Goyal 					"sessionless crypto op not supported");
16779a984458SAkhil Goyal 				frames_to_send = loop;
16789a984458SAkhil Goyal 				nb_ops = loop;
16799a984458SAkhil Goyal 				goto send_pkts;
16809a984458SAkhil Goyal 			}
16814e694fe5SAkhil Goyal 			if (unlikely(!ses->qp[rte_lcore_id() % MAX_DPAA_CORES])) {
16829a984458SAkhil Goyal 				if (dpaa_sec_attach_sess_q(qp, ses)) {
16839a984458SAkhil Goyal 					frames_to_send = loop;
16849a984458SAkhil Goyal 					nb_ops = loop;
16859a984458SAkhil Goyal 					goto send_pkts;
16869a984458SAkhil Goyal 				}
16874e694fe5SAkhil Goyal 			} else if (unlikely(ses->qp[rte_lcore_id() %
16884e694fe5SAkhil Goyal 						MAX_DPAA_CORES] != qp)) {
16899198b2c2SAkhil Goyal 				DPAA_SEC_DP_ERR("Old:sess->qp = %p"
16904e694fe5SAkhil Goyal 					" New qp = %p\n",
16914e694fe5SAkhil Goyal 					ses->qp[rte_lcore_id() %
16924e694fe5SAkhil Goyal 					MAX_DPAA_CORES], qp);
16939198b2c2SAkhil Goyal 				frames_to_send = loop;
16949198b2c2SAkhil Goyal 				nb_ops = loop;
16959198b2c2SAkhil Goyal 				goto send_pkts;
1696c3e85bdcSAkhil Goyal 			}
1697c3e85bdcSAkhil Goyal 
16989a984458SAkhil Goyal 			auth_only_len = op->sym->auth.data.length -
16999a984458SAkhil Goyal 						op->sym->cipher.data.length;
1700a74af788SAkhil Goyal 			if (rte_pktmbuf_is_contiguous(op->sym->m_src)) {
170105b12700SHemant Agrawal 				if (is_proto_ipsec(ses)) {
170205b12700SHemant Agrawal 					cf = build_proto(op, ses);
1703a1173d55SHemant Agrawal 				} else if (is_proto_pdcp(ses)) {
1704a1173d55SHemant Agrawal 					cf = build_proto(op, ses);
170505b12700SHemant Agrawal 				} else if (is_auth_only(ses)) {
1706c3e85bdcSAkhil Goyal 					cf = build_auth_only(op, ses);
1707c3e85bdcSAkhil Goyal 				} else if (is_cipher_only(ses)) {
1708c3e85bdcSAkhil Goyal 					cf = build_cipher_only(op, ses);
1709c3e85bdcSAkhil Goyal 				} else if (is_aead(ses)) {
1710c3e85bdcSAkhil Goyal 					cf = build_cipher_auth_gcm(op, ses);
1711c3e85bdcSAkhil Goyal 					auth_only_len = ses->auth_only_len;
1712c3e85bdcSAkhil Goyal 				} else if (is_auth_cipher(ses)) {
1713c3e85bdcSAkhil Goyal 					cf = build_cipher_auth(op, ses);
1714c3e85bdcSAkhil Goyal 				} else {
1715f163231eSHemant Agrawal 					DPAA_SEC_DP_ERR("not supported ops");
17169a984458SAkhil Goyal 					frames_to_send = loop;
17179a984458SAkhil Goyal 					nb_ops = loop;
17189a984458SAkhil Goyal 					goto send_pkts;
1719c3e85bdcSAkhil Goyal 				}
1720a74af788SAkhil Goyal 			} else {
1721a74af788SAkhil Goyal 				if (is_auth_only(ses)) {
1722a74af788SAkhil Goyal 					cf = build_auth_only_sg(op, ses);
1723a74af788SAkhil Goyal 				} else if (is_cipher_only(ses)) {
1724a74af788SAkhil Goyal 					cf = build_cipher_only_sg(op, ses);
1725a74af788SAkhil Goyal 				} else if (is_aead(ses)) {
1726a74af788SAkhil Goyal 					cf = build_cipher_auth_gcm_sg(op, ses);
1727a74af788SAkhil Goyal 					auth_only_len = ses->auth_only_len;
1728a74af788SAkhil Goyal 				} else if (is_auth_cipher(ses)) {
1729a74af788SAkhil Goyal 					cf = build_cipher_auth_sg(op, ses);
1730a74af788SAkhil Goyal 				} else {
1731f163231eSHemant Agrawal 					DPAA_SEC_DP_ERR("not supported ops");
1732a74af788SAkhil Goyal 					frames_to_send = loop;
1733a74af788SAkhil Goyal 					nb_ops = loop;
1734a74af788SAkhil Goyal 					goto send_pkts;
1735a74af788SAkhil Goyal 				}
1736a74af788SAkhil Goyal 			}
17379a984458SAkhil Goyal 			if (unlikely(!cf)) {
17389a984458SAkhil Goyal 				frames_to_send = loop;
17399a984458SAkhil Goyal 				nb_ops = loop;
17409a984458SAkhil Goyal 				goto send_pkts;
17419a984458SAkhil Goyal 			}
1742c3e85bdcSAkhil Goyal 
17439a984458SAkhil Goyal 			fd = &fds[loop];
17444e694fe5SAkhil Goyal 			inq[loop] = ses->inq[rte_lcore_id() % MAX_DPAA_CORES];
17459a984458SAkhil Goyal 			fd->opaque_addr = 0;
17469a984458SAkhil Goyal 			fd->cmd = 0;
174795456e89SShreyansh Jain 			qm_fd_addr_set64(fd, dpaa_mem_vtop(cf->sg));
17489a984458SAkhil Goyal 			fd->_format1 = qm_fd_compound;
17499a984458SAkhil Goyal 			fd->length29 = 2 * sizeof(struct qm_sg_entry);
17509a984458SAkhil Goyal 			/* Auth_only_len is set as 0 in descriptor and it is
17519a984458SAkhil Goyal 			 * overwritten here in the fd.cmd which will update
17529a984458SAkhil Goyal 			 * the DPOVRD reg.
1753c3e85bdcSAkhil Goyal 			 */
1754c3e85bdcSAkhil Goyal 			if (auth_only_len)
17559a984458SAkhil Goyal 				fd->cmd = 0x80000000 | auth_only_len;
1756c3e85bdcSAkhil Goyal 
1757*6a0c9d36SAkhil Goyal 			/* In case of PDCP, per packet HFN is stored in
1758*6a0c9d36SAkhil Goyal 			 * mbuf priv after sym_op.
1759*6a0c9d36SAkhil Goyal 			 */
1760*6a0c9d36SAkhil Goyal 			if (is_proto_pdcp(ses) && ses->pdcp.hfn_ovd) {
1761*6a0c9d36SAkhil Goyal 				fd->cmd = 0x80000000 |
1762*6a0c9d36SAkhil Goyal 					*((uint32_t *)((uint8_t *)op +
1763*6a0c9d36SAkhil Goyal 					ses->pdcp.hfn_ovd_offset));
1764*6a0c9d36SAkhil Goyal 				DPAA_SEC_DP_DEBUG("Per packet HFN: %x, ovd:%u,%u\n",
1765*6a0c9d36SAkhil Goyal 					*((uint32_t *)((uint8_t *)op +
1766*6a0c9d36SAkhil Goyal 					ses->pdcp.hfn_ovd_offset)),
1767*6a0c9d36SAkhil Goyal 					ses->pdcp.hfn_ovd,
1768*6a0c9d36SAkhil Goyal 					is_proto_pdcp(ses));
1769*6a0c9d36SAkhil Goyal 			}
1770*6a0c9d36SAkhil Goyal 
17719a984458SAkhil Goyal 		}
17729a984458SAkhil Goyal send_pkts:
17739a984458SAkhil Goyal 		loop = 0;
17749a984458SAkhil Goyal 		while (loop < frames_to_send) {
17759a984458SAkhil Goyal 			loop += qman_enqueue_multi_fq(&inq[loop], &fds[loop],
17769a984458SAkhil Goyal 					frames_to_send - loop);
17779a984458SAkhil Goyal 		}
17789a984458SAkhil Goyal 		nb_ops -= frames_to_send;
17799a984458SAkhil Goyal 		num_tx += frames_to_send;
1780c3e85bdcSAkhil Goyal 	}
1781c3e85bdcSAkhil Goyal 
1782c3e85bdcSAkhil Goyal 	dpaa_qp->tx_pkts += num_tx;
1783c3e85bdcSAkhil Goyal 	dpaa_qp->tx_errs += nb_ops - num_tx;
1784c3e85bdcSAkhil Goyal 
1785c3e85bdcSAkhil Goyal 	return num_tx;
1786c3e85bdcSAkhil Goyal }
1787c3e85bdcSAkhil Goyal 
1788c3e85bdcSAkhil Goyal static uint16_t
1789c3e85bdcSAkhil Goyal dpaa_sec_dequeue_burst(void *qp, struct rte_crypto_op **ops,
1790c3e85bdcSAkhil Goyal 		       uint16_t nb_ops)
1791c3e85bdcSAkhil Goyal {
1792c3e85bdcSAkhil Goyal 	uint16_t num_rx;
1793c3e85bdcSAkhil Goyal 	struct dpaa_sec_qp *dpaa_qp = (struct dpaa_sec_qp *)qp;
1794c3e85bdcSAkhil Goyal 
1795c3e85bdcSAkhil Goyal 	num_rx = dpaa_sec_deq(dpaa_qp, ops, nb_ops);
1796c3e85bdcSAkhil Goyal 
1797c3e85bdcSAkhil Goyal 	dpaa_qp->rx_pkts += num_rx;
1798c3e85bdcSAkhil Goyal 	dpaa_qp->rx_errs += nb_ops - num_rx;
1799c3e85bdcSAkhil Goyal 
1800f163231eSHemant Agrawal 	DPAA_SEC_DP_DEBUG("SEC Received %d Packets\n", num_rx);
1801c3e85bdcSAkhil Goyal 
1802c3e85bdcSAkhil Goyal 	return num_rx;
1803c3e85bdcSAkhil Goyal }
1804c3e85bdcSAkhil Goyal 
1805c3e85bdcSAkhil Goyal /** Release queue pair */
1806c3e85bdcSAkhil Goyal static int
1807c3e85bdcSAkhil Goyal dpaa_sec_queue_pair_release(struct rte_cryptodev *dev,
1808c3e85bdcSAkhil Goyal 			    uint16_t qp_id)
1809c3e85bdcSAkhil Goyal {
1810c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals;
1811c3e85bdcSAkhil Goyal 	struct dpaa_sec_qp *qp = NULL;
1812c3e85bdcSAkhil Goyal 
1813c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
1814c3e85bdcSAkhil Goyal 
1815f163231eSHemant Agrawal 	DPAA_SEC_DEBUG("dev =%p, queue =%d", dev, qp_id);
1816c3e85bdcSAkhil Goyal 
1817c3e85bdcSAkhil Goyal 	internals = dev->data->dev_private;
1818c3e85bdcSAkhil Goyal 	if (qp_id >= internals->max_nb_queue_pairs) {
1819f163231eSHemant Agrawal 		DPAA_SEC_ERR("Max supported qpid %d",
1820c3e85bdcSAkhil Goyal 			     internals->max_nb_queue_pairs);
1821c3e85bdcSAkhil Goyal 		return -EINVAL;
1822c3e85bdcSAkhil Goyal 	}
1823c3e85bdcSAkhil Goyal 
1824c3e85bdcSAkhil Goyal 	qp = &internals->qps[qp_id];
1825c3e85bdcSAkhil Goyal 	qp->internals = NULL;
1826c3e85bdcSAkhil Goyal 	dev->data->queue_pairs[qp_id] = NULL;
1827c3e85bdcSAkhil Goyal 
1828c3e85bdcSAkhil Goyal 	return 0;
1829c3e85bdcSAkhil Goyal }
1830c3e85bdcSAkhil Goyal 
1831c3e85bdcSAkhil Goyal /** Setup a queue pair */
1832c3e85bdcSAkhil Goyal static int
1833c3e85bdcSAkhil Goyal dpaa_sec_queue_pair_setup(struct rte_cryptodev *dev, uint16_t qp_id,
1834c3e85bdcSAkhil Goyal 		__rte_unused const struct rte_cryptodev_qp_conf *qp_conf,
1835725d2a7fSFan Zhang 		__rte_unused int socket_id)
1836c3e85bdcSAkhil Goyal {
1837c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals;
1838c3e85bdcSAkhil Goyal 	struct dpaa_sec_qp *qp = NULL;
1839c3e85bdcSAkhil Goyal 
1840f163231eSHemant Agrawal 	DPAA_SEC_DEBUG("dev =%p, queue =%d, conf =%p", dev, qp_id, qp_conf);
1841c3e85bdcSAkhil Goyal 
1842c3e85bdcSAkhil Goyal 	internals = dev->data->dev_private;
1843c3e85bdcSAkhil Goyal 	if (qp_id >= internals->max_nb_queue_pairs) {
1844f163231eSHemant Agrawal 		DPAA_SEC_ERR("Max supported qpid %d",
1845c3e85bdcSAkhil Goyal 			     internals->max_nb_queue_pairs);
1846c3e85bdcSAkhil Goyal 		return -EINVAL;
1847c3e85bdcSAkhil Goyal 	}
1848c3e85bdcSAkhil Goyal 
1849c3e85bdcSAkhil Goyal 	qp = &internals->qps[qp_id];
1850c3e85bdcSAkhil Goyal 	qp->internals = internals;
1851c3e85bdcSAkhil Goyal 	dev->data->queue_pairs[qp_id] = qp;
1852c3e85bdcSAkhil Goyal 
1853c3e85bdcSAkhil Goyal 	return 0;
1854c3e85bdcSAkhil Goyal }
1855c3e85bdcSAkhil Goyal 
1856c3e85bdcSAkhil Goyal /** Return the number of allocated queue pairs */
1857c3e85bdcSAkhil Goyal static uint32_t
1858c3e85bdcSAkhil Goyal dpaa_sec_queue_pair_count(struct rte_cryptodev *dev)
1859c3e85bdcSAkhil Goyal {
1860c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
1861c3e85bdcSAkhil Goyal 
1862c3e85bdcSAkhil Goyal 	return dev->data->nb_queue_pairs;
1863c3e85bdcSAkhil Goyal }
1864c3e85bdcSAkhil Goyal 
1865c3e85bdcSAkhil Goyal /** Returns the size of session structure */
1866c3e85bdcSAkhil Goyal static unsigned int
1867012c5076SPablo de Lara dpaa_sec_sym_session_get_size(struct rte_cryptodev *dev __rte_unused)
1868c3e85bdcSAkhil Goyal {
1869c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
1870c3e85bdcSAkhil Goyal 
1871c3e85bdcSAkhil Goyal 	return sizeof(dpaa_sec_session);
1872c3e85bdcSAkhil Goyal }
1873c3e85bdcSAkhil Goyal 
1874c3e85bdcSAkhil Goyal static int
1875c3e85bdcSAkhil Goyal dpaa_sec_cipher_init(struct rte_cryptodev *dev __rte_unused,
1876c3e85bdcSAkhil Goyal 		     struct rte_crypto_sym_xform *xform,
1877c3e85bdcSAkhil Goyal 		     dpaa_sec_session *session)
1878c3e85bdcSAkhil Goyal {
1879c3e85bdcSAkhil Goyal 	session->cipher_alg = xform->cipher.algo;
1880c3e85bdcSAkhil Goyal 	session->iv.length = xform->cipher.iv.length;
1881c3e85bdcSAkhil Goyal 	session->iv.offset = xform->cipher.iv.offset;
1882c3e85bdcSAkhil Goyal 	session->cipher_key.data = rte_zmalloc(NULL, xform->cipher.key.length,
1883c3e85bdcSAkhil Goyal 					       RTE_CACHE_LINE_SIZE);
1884c3e85bdcSAkhil Goyal 	if (session->cipher_key.data == NULL && xform->cipher.key.length > 0) {
1885f163231eSHemant Agrawal 		DPAA_SEC_ERR("No Memory for cipher key");
1886c3e85bdcSAkhil Goyal 		return -ENOMEM;
1887c3e85bdcSAkhil Goyal 	}
1888c3e85bdcSAkhil Goyal 	session->cipher_key.length = xform->cipher.key.length;
1889c3e85bdcSAkhil Goyal 
1890c3e85bdcSAkhil Goyal 	memcpy(session->cipher_key.data, xform->cipher.key.data,
1891c3e85bdcSAkhil Goyal 	       xform->cipher.key.length);
1892c3e85bdcSAkhil Goyal 	session->dir = (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
1893c3e85bdcSAkhil Goyal 			DIR_ENC : DIR_DEC;
1894c3e85bdcSAkhil Goyal 
1895c3e85bdcSAkhil Goyal 	return 0;
1896c3e85bdcSAkhil Goyal }
1897c3e85bdcSAkhil Goyal 
1898c3e85bdcSAkhil Goyal static int
1899c3e85bdcSAkhil Goyal dpaa_sec_auth_init(struct rte_cryptodev *dev __rte_unused,
1900c3e85bdcSAkhil Goyal 		   struct rte_crypto_sym_xform *xform,
1901c3e85bdcSAkhil Goyal 		   dpaa_sec_session *session)
1902c3e85bdcSAkhil Goyal {
1903c3e85bdcSAkhil Goyal 	session->auth_alg = xform->auth.algo;
1904c3e85bdcSAkhil Goyal 	session->auth_key.data = rte_zmalloc(NULL, xform->auth.key.length,
1905c3e85bdcSAkhil Goyal 					     RTE_CACHE_LINE_SIZE);
1906c3e85bdcSAkhil Goyal 	if (session->auth_key.data == NULL && xform->auth.key.length > 0) {
1907f163231eSHemant Agrawal 		DPAA_SEC_ERR("No Memory for auth key");
1908c3e85bdcSAkhil Goyal 		return -ENOMEM;
1909c3e85bdcSAkhil Goyal 	}
1910c3e85bdcSAkhil Goyal 	session->auth_key.length = xform->auth.key.length;
1911c3e85bdcSAkhil Goyal 	session->digest_length = xform->auth.digest_length;
1912c3e85bdcSAkhil Goyal 
1913c3e85bdcSAkhil Goyal 	memcpy(session->auth_key.data, xform->auth.key.data,
1914c3e85bdcSAkhil Goyal 	       xform->auth.key.length);
1915c3e85bdcSAkhil Goyal 	session->dir = (xform->auth.op == RTE_CRYPTO_AUTH_OP_GENERATE) ?
1916c3e85bdcSAkhil Goyal 			DIR_ENC : DIR_DEC;
1917c3e85bdcSAkhil Goyal 
1918c3e85bdcSAkhil Goyal 	return 0;
1919c3e85bdcSAkhil Goyal }
1920c3e85bdcSAkhil Goyal 
1921c3e85bdcSAkhil Goyal static int
1922c3e85bdcSAkhil Goyal dpaa_sec_aead_init(struct rte_cryptodev *dev __rte_unused,
1923c3e85bdcSAkhil Goyal 		   struct rte_crypto_sym_xform *xform,
1924c3e85bdcSAkhil Goyal 		   dpaa_sec_session *session)
1925c3e85bdcSAkhil Goyal {
1926c3e85bdcSAkhil Goyal 	session->aead_alg = xform->aead.algo;
1927c3e85bdcSAkhil Goyal 	session->iv.length = xform->aead.iv.length;
1928c3e85bdcSAkhil Goyal 	session->iv.offset = xform->aead.iv.offset;
1929c3e85bdcSAkhil Goyal 	session->auth_only_len = xform->aead.aad_length;
1930c3e85bdcSAkhil Goyal 	session->aead_key.data = rte_zmalloc(NULL, xform->aead.key.length,
1931c3e85bdcSAkhil Goyal 					     RTE_CACHE_LINE_SIZE);
1932c3e85bdcSAkhil Goyal 	if (session->aead_key.data == NULL && xform->aead.key.length > 0) {
1933f163231eSHemant Agrawal 		DPAA_SEC_ERR("No Memory for aead key\n");
1934c3e85bdcSAkhil Goyal 		return -ENOMEM;
1935c3e85bdcSAkhil Goyal 	}
1936c3e85bdcSAkhil Goyal 	session->aead_key.length = xform->aead.key.length;
1937c3e85bdcSAkhil Goyal 	session->digest_length = xform->aead.digest_length;
1938c3e85bdcSAkhil Goyal 
1939c3e85bdcSAkhil Goyal 	memcpy(session->aead_key.data, xform->aead.key.data,
1940c3e85bdcSAkhil Goyal 	       xform->aead.key.length);
1941c3e85bdcSAkhil Goyal 	session->dir = (xform->aead.op == RTE_CRYPTO_AEAD_OP_ENCRYPT) ?
1942c3e85bdcSAkhil Goyal 			DIR_ENC : DIR_DEC;
1943c3e85bdcSAkhil Goyal 
1944c3e85bdcSAkhil Goyal 	return 0;
1945c3e85bdcSAkhil Goyal }
1946c3e85bdcSAkhil Goyal 
1947e79416d1SHemant Agrawal static struct qman_fq *
1948e79416d1SHemant Agrawal dpaa_sec_attach_rxq(struct dpaa_sec_dev_private *qi)
1949c3e85bdcSAkhil Goyal {
1950e79416d1SHemant Agrawal 	unsigned int i;
1951c3e85bdcSAkhil Goyal 
1952e621d970SAkhil Goyal 	for (i = 0; i < qi->max_nb_sessions * MAX_DPAA_CORES; i++) {
1953e79416d1SHemant Agrawal 		if (qi->inq_attach[i] == 0) {
1954e79416d1SHemant Agrawal 			qi->inq_attach[i] = 1;
1955e79416d1SHemant Agrawal 			return &qi->inq[i];
1956e79416d1SHemant Agrawal 		}
1957e79416d1SHemant Agrawal 	}
1958e621d970SAkhil Goyal 	DPAA_SEC_WARN("All session in use %u", qi->max_nb_sessions);
1959c3e85bdcSAkhil Goyal 
1960e79416d1SHemant Agrawal 	return NULL;
1961c3e85bdcSAkhil Goyal }
1962c3e85bdcSAkhil Goyal 
1963e79416d1SHemant Agrawal static int
1964e79416d1SHemant Agrawal dpaa_sec_detach_rxq(struct dpaa_sec_dev_private *qi, struct qman_fq *fq)
1965e79416d1SHemant Agrawal {
1966e79416d1SHemant Agrawal 	unsigned int i;
1967e79416d1SHemant Agrawal 
1968e79416d1SHemant Agrawal 	for (i = 0; i < qi->max_nb_sessions; i++) {
1969e79416d1SHemant Agrawal 		if (&qi->inq[i] == fq) {
1970b4053c4bSAlok Makhariya 			qman_retire_fq(fq, NULL);
1971b4053c4bSAlok Makhariya 			qman_oos_fq(fq);
1972e79416d1SHemant Agrawal 			qi->inq_attach[i] = 0;
1973e79416d1SHemant Agrawal 			return 0;
1974e79416d1SHemant Agrawal 		}
1975e79416d1SHemant Agrawal 	}
1976e79416d1SHemant Agrawal 	return -1;
1977e79416d1SHemant Agrawal }
1978e79416d1SHemant Agrawal 
1979e79416d1SHemant Agrawal static int
1980e79416d1SHemant Agrawal dpaa_sec_attach_sess_q(struct dpaa_sec_qp *qp, dpaa_sec_session *sess)
1981e79416d1SHemant Agrawal {
1982e79416d1SHemant Agrawal 	int ret;
1983e79416d1SHemant Agrawal 
19844e694fe5SAkhil Goyal 	sess->qp[rte_lcore_id() % MAX_DPAA_CORES] = qp;
1985e79416d1SHemant Agrawal 	ret = dpaa_sec_prep_cdb(sess);
1986e79416d1SHemant Agrawal 	if (ret) {
1987f163231eSHemant Agrawal 		DPAA_SEC_ERR("Unable to prepare sec cdb");
1988e79416d1SHemant Agrawal 		return -1;
1989e79416d1SHemant Agrawal 	}
19905b0f1bd3SAshish Jain 	if (unlikely(!RTE_PER_LCORE(dpaa_io))) {
19915b0f1bd3SAshish Jain 		ret = rte_dpaa_portal_init((void *)0);
19925b0f1bd3SAshish Jain 		if (ret) {
1993f163231eSHemant Agrawal 			DPAA_SEC_ERR("Failure in affining portal");
19945b0f1bd3SAshish Jain 			return ret;
19955b0f1bd3SAshish Jain 		}
19965b0f1bd3SAshish Jain 	}
19974e694fe5SAkhil Goyal 	ret = dpaa_sec_init_rx(sess->inq[rte_lcore_id() % MAX_DPAA_CORES],
19984e694fe5SAkhil Goyal 			       dpaa_mem_vtop(&sess->cdb),
1999e79416d1SHemant Agrawal 			       qman_fq_fqid(&qp->outq));
2000e79416d1SHemant Agrawal 	if (ret)
2001f163231eSHemant Agrawal 		DPAA_SEC_ERR("Unable to init sec queue");
2002e79416d1SHemant Agrawal 
2003e79416d1SHemant Agrawal 	return ret;
2004c3e85bdcSAkhil Goyal }
2005c3e85bdcSAkhil Goyal 
2006c3e85bdcSAkhil Goyal static int
2007c3e85bdcSAkhil Goyal dpaa_sec_set_session_parameters(struct rte_cryptodev *dev,
2008c3e85bdcSAkhil Goyal 			    struct rte_crypto_sym_xform *xform,	void *sess)
2009c3e85bdcSAkhil Goyal {
2010c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals = dev->data->dev_private;
2011c3e85bdcSAkhil Goyal 	dpaa_sec_session *session = sess;
20124e694fe5SAkhil Goyal 	uint32_t i;
2013c3e85bdcSAkhil Goyal 
2014c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2015c3e85bdcSAkhil Goyal 
2016c3e85bdcSAkhil Goyal 	if (unlikely(sess == NULL)) {
2017f163231eSHemant Agrawal 		DPAA_SEC_ERR("invalid session struct");
2018c3e85bdcSAkhil Goyal 		return -EINVAL;
2019c3e85bdcSAkhil Goyal 	}
2020b0894102SAkhil Goyal 	memset(session, 0, sizeof(dpaa_sec_session));
2021c3e85bdcSAkhil Goyal 
2022c3e85bdcSAkhil Goyal 	/* Default IV length = 0 */
2023c3e85bdcSAkhil Goyal 	session->iv.length = 0;
2024c3e85bdcSAkhil Goyal 
2025c3e85bdcSAkhil Goyal 	/* Cipher Only */
2026c3e85bdcSAkhil Goyal 	if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER && xform->next == NULL) {
2027c3e85bdcSAkhil Goyal 		session->auth_alg = RTE_CRYPTO_AUTH_NULL;
2028c3e85bdcSAkhil Goyal 		dpaa_sec_cipher_init(dev, xform, session);
2029c3e85bdcSAkhil Goyal 
2030c3e85bdcSAkhil Goyal 	/* Authentication Only */
2031c3e85bdcSAkhil Goyal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
2032c3e85bdcSAkhil Goyal 		   xform->next == NULL) {
2033c3e85bdcSAkhil Goyal 		session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
2034c3e85bdcSAkhil Goyal 		dpaa_sec_auth_init(dev, xform, session);
2035c3e85bdcSAkhil Goyal 
2036c3e85bdcSAkhil Goyal 	/* Cipher then Authenticate */
2037c3e85bdcSAkhil Goyal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
2038c3e85bdcSAkhil Goyal 		   xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
2039c3e85bdcSAkhil Goyal 		if (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) {
2040c3e85bdcSAkhil Goyal 			dpaa_sec_cipher_init(dev, xform, session);
2041c3e85bdcSAkhil Goyal 			dpaa_sec_auth_init(dev, xform->next, session);
2042c3e85bdcSAkhil Goyal 		} else {
2043f163231eSHemant Agrawal 			DPAA_SEC_ERR("Not supported: Auth then Cipher");
2044c3e85bdcSAkhil Goyal 			return -EINVAL;
2045c3e85bdcSAkhil Goyal 		}
2046c3e85bdcSAkhil Goyal 
2047c3e85bdcSAkhil Goyal 	/* Authenticate then Cipher */
2048c3e85bdcSAkhil Goyal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
2049c3e85bdcSAkhil Goyal 		   xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
2050c3e85bdcSAkhil Goyal 		if (xform->next->cipher.op == RTE_CRYPTO_CIPHER_OP_DECRYPT) {
2051c3e85bdcSAkhil Goyal 			dpaa_sec_auth_init(dev, xform, session);
2052c3e85bdcSAkhil Goyal 			dpaa_sec_cipher_init(dev, xform->next, session);
2053c3e85bdcSAkhil Goyal 		} else {
2054f163231eSHemant Agrawal 			DPAA_SEC_ERR("Not supported: Auth then Cipher");
2055c3e85bdcSAkhil Goyal 			return -EINVAL;
2056c3e85bdcSAkhil Goyal 		}
2057c3e85bdcSAkhil Goyal 
2058c3e85bdcSAkhil Goyal 	/* AEAD operation for AES-GCM kind of Algorithms */
2059c3e85bdcSAkhil Goyal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD &&
2060c3e85bdcSAkhil Goyal 		   xform->next == NULL) {
2061c3e85bdcSAkhil Goyal 		dpaa_sec_aead_init(dev, xform, session);
2062c3e85bdcSAkhil Goyal 
2063c3e85bdcSAkhil Goyal 	} else {
2064f163231eSHemant Agrawal 		DPAA_SEC_ERR("Invalid crypto type");
2065c3e85bdcSAkhil Goyal 		return -EINVAL;
2066c3e85bdcSAkhil Goyal 	}
2067c3e85bdcSAkhil Goyal 	session->ctx_pool = internals->ctx_pool;
20683b617ee7SAkhil Goyal 	rte_spinlock_lock(&internals->lock);
20694e694fe5SAkhil Goyal 	for (i = 0; i < MAX_DPAA_CORES; i++) {
20704e694fe5SAkhil Goyal 		session->inq[i] = dpaa_sec_attach_rxq(internals);
20714e694fe5SAkhil Goyal 		if (session->inq[i] == NULL) {
2072f163231eSHemant Agrawal 			DPAA_SEC_ERR("unable to attach sec queue");
20734e694fe5SAkhil Goyal 			rte_spinlock_unlock(&internals->lock);
2074e79416d1SHemant Agrawal 			goto err1;
2075e79416d1SHemant Agrawal 		}
20764e694fe5SAkhil Goyal 	}
20774e694fe5SAkhil Goyal 	rte_spinlock_unlock(&internals->lock);
2078c3e85bdcSAkhil Goyal 
2079c3e85bdcSAkhil Goyal 	return 0;
2080e79416d1SHemant Agrawal 
2081e79416d1SHemant Agrawal err1:
2082e79416d1SHemant Agrawal 	rte_free(session->cipher_key.data);
2083e79416d1SHemant Agrawal 	rte_free(session->auth_key.data);
2084e79416d1SHemant Agrawal 	memset(session, 0, sizeof(dpaa_sec_session));
2085e79416d1SHemant Agrawal 
2086e79416d1SHemant Agrawal 	return -EINVAL;
2087c3e85bdcSAkhil Goyal }
2088c3e85bdcSAkhil Goyal 
2089c3e85bdcSAkhil Goyal static int
2090012c5076SPablo de Lara dpaa_sec_sym_session_configure(struct rte_cryptodev *dev,
2091c3e85bdcSAkhil Goyal 		struct rte_crypto_sym_xform *xform,
2092c3e85bdcSAkhil Goyal 		struct rte_cryptodev_sym_session *sess,
2093c3e85bdcSAkhil Goyal 		struct rte_mempool *mempool)
2094c3e85bdcSAkhil Goyal {
2095c3e85bdcSAkhil Goyal 	void *sess_private_data;
2096c3e85bdcSAkhil Goyal 	int ret;
2097c3e85bdcSAkhil Goyal 
2098c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2099c3e85bdcSAkhil Goyal 
2100c3e85bdcSAkhil Goyal 	if (rte_mempool_get(mempool, &sess_private_data)) {
2101f163231eSHemant Agrawal 		DPAA_SEC_ERR("Couldn't get object from session mempool");
2102c3e85bdcSAkhil Goyal 		return -ENOMEM;
2103c3e85bdcSAkhil Goyal 	}
2104c3e85bdcSAkhil Goyal 
2105c3e85bdcSAkhil Goyal 	ret = dpaa_sec_set_session_parameters(dev, xform, sess_private_data);
2106c3e85bdcSAkhil Goyal 	if (ret != 0) {
2107f163231eSHemant Agrawal 		DPAA_SEC_ERR("failed to configure session parameters");
2108c3e85bdcSAkhil Goyal 
2109c3e85bdcSAkhil Goyal 		/* Return session to mempool */
2110c3e85bdcSAkhil Goyal 		rte_mempool_put(mempool, sess_private_data);
2111c3e85bdcSAkhil Goyal 		return ret;
2112c3e85bdcSAkhil Goyal 	}
2113c3e85bdcSAkhil Goyal 
2114012c5076SPablo de Lara 	set_sym_session_private_data(sess, dev->driver_id,
2115c3e85bdcSAkhil Goyal 			sess_private_data);
2116c3e85bdcSAkhil Goyal 
2117e79416d1SHemant Agrawal 
2118c3e85bdcSAkhil Goyal 	return 0;
2119c3e85bdcSAkhil Goyal }
2120c3e85bdcSAkhil Goyal 
21213d0d5332SAkhil Goyal static inline void
21223d0d5332SAkhil Goyal free_session_memory(struct rte_cryptodev *dev, dpaa_sec_session *s)
2123c3e85bdcSAkhil Goyal {
2124e79416d1SHemant Agrawal 	struct dpaa_sec_dev_private *qi = dev->data->dev_private;
21253d0d5332SAkhil Goyal 	struct rte_mempool *sess_mp = rte_mempool_from_obj((void *)s);
21263d0d5332SAkhil Goyal 	uint8_t i;
2127e79416d1SHemant Agrawal 
2128e621d970SAkhil Goyal 	for (i = 0; i < MAX_DPAA_CORES; i++) {
2129e621d970SAkhil Goyal 		if (s->inq[i])
2130e621d970SAkhil Goyal 			dpaa_sec_detach_rxq(qi, s->inq[i]);
2131e621d970SAkhil Goyal 		s->inq[i] = NULL;
2132e621d970SAkhil Goyal 		s->qp[i] = NULL;
2133e621d970SAkhil Goyal 	}
2134c3e85bdcSAkhil Goyal 	rte_free(s->cipher_key.data);
2135c3e85bdcSAkhil Goyal 	rte_free(s->auth_key.data);
2136c3e85bdcSAkhil Goyal 	memset(s, 0, sizeof(dpaa_sec_session));
21373d0d5332SAkhil Goyal 	rte_mempool_put(sess_mp, (void *)s);
21383d0d5332SAkhil Goyal }
21393d0d5332SAkhil Goyal 
21403d0d5332SAkhil Goyal /** Clear the memory of session so it doesn't leave key material behind */
21413d0d5332SAkhil Goyal static void
21423d0d5332SAkhil Goyal dpaa_sec_sym_session_clear(struct rte_cryptodev *dev,
21433d0d5332SAkhil Goyal 		struct rte_cryptodev_sym_session *sess)
21443d0d5332SAkhil Goyal {
21453d0d5332SAkhil Goyal 	PMD_INIT_FUNC_TRACE();
21463d0d5332SAkhil Goyal 	uint8_t index = dev->driver_id;
21473d0d5332SAkhil Goyal 	void *sess_priv = get_sym_session_private_data(sess, index);
21483d0d5332SAkhil Goyal 	dpaa_sec_session *s = (dpaa_sec_session *)sess_priv;
21493d0d5332SAkhil Goyal 
21503d0d5332SAkhil Goyal 	if (sess_priv) {
21513d0d5332SAkhil Goyal 		free_session_memory(dev, s);
2152012c5076SPablo de Lara 		set_sym_session_private_data(sess, index, NULL);
2153c3e85bdcSAkhil Goyal 	}
2154c3e85bdcSAkhil Goyal }
2155c3e85bdcSAkhil Goyal 
2156c3e85bdcSAkhil Goyal static int
21571f14d500SAkhil Goyal dpaa_sec_set_ipsec_session(__rte_unused struct rte_cryptodev *dev,
21581f14d500SAkhil Goyal 			   struct rte_security_session_conf *conf,
21591f14d500SAkhil Goyal 			   void *sess)
21601f14d500SAkhil Goyal {
21611f14d500SAkhil Goyal 	struct dpaa_sec_dev_private *internals = dev->data->dev_private;
21621f14d500SAkhil Goyal 	struct rte_security_ipsec_xform *ipsec_xform = &conf->ipsec;
216305b12700SHemant Agrawal 	struct rte_crypto_auth_xform *auth_xform = NULL;
216405b12700SHemant Agrawal 	struct rte_crypto_cipher_xform *cipher_xform = NULL;
21651f14d500SAkhil Goyal 	dpaa_sec_session *session = (dpaa_sec_session *)sess;
21664e694fe5SAkhil Goyal 	uint32_t i;
21671f14d500SAkhil Goyal 
21681f14d500SAkhil Goyal 	PMD_INIT_FUNC_TRACE();
21691f14d500SAkhil Goyal 
2170b0894102SAkhil Goyal 	memset(session, 0, sizeof(dpaa_sec_session));
21711f14d500SAkhil Goyal 	if (ipsec_xform->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
21721f14d500SAkhil Goyal 		cipher_xform = &conf->crypto_xform->cipher;
217305b12700SHemant Agrawal 		if (conf->crypto_xform->next)
21741f14d500SAkhil Goyal 			auth_xform = &conf->crypto_xform->next->auth;
21751f14d500SAkhil Goyal 	} else {
21761f14d500SAkhil Goyal 		auth_xform = &conf->crypto_xform->auth;
217705b12700SHemant Agrawal 		if (conf->crypto_xform->next)
21781f14d500SAkhil Goyal 			cipher_xform = &conf->crypto_xform->next->cipher;
21791f14d500SAkhil Goyal 	}
21801f14d500SAkhil Goyal 	session->proto_alg = conf->protocol;
218105b12700SHemant Agrawal 
218205b12700SHemant Agrawal 	if (cipher_xform && cipher_xform->algo != RTE_CRYPTO_CIPHER_NULL) {
21831f14d500SAkhil Goyal 		session->cipher_key.data = rte_zmalloc(NULL,
21841f14d500SAkhil Goyal 						       cipher_xform->key.length,
21851f14d500SAkhil Goyal 						       RTE_CACHE_LINE_SIZE);
21861f14d500SAkhil Goyal 		if (session->cipher_key.data == NULL &&
21871f14d500SAkhil Goyal 				cipher_xform->key.length > 0) {
2188f163231eSHemant Agrawal 			DPAA_SEC_ERR("No Memory for cipher key");
21891f14d500SAkhil Goyal 			return -ENOMEM;
21901f14d500SAkhil Goyal 		}
219105b12700SHemant Agrawal 		memcpy(session->cipher_key.data, cipher_xform->key.data,
219205b12700SHemant Agrawal 				cipher_xform->key.length);
21931f14d500SAkhil Goyal 		session->cipher_key.length = cipher_xform->key.length;
219405b12700SHemant Agrawal 
219505b12700SHemant Agrawal 		switch (cipher_xform->algo) {
219605b12700SHemant Agrawal 		case RTE_CRYPTO_CIPHER_AES_CBC:
219705b12700SHemant Agrawal 		case RTE_CRYPTO_CIPHER_3DES_CBC:
219805b12700SHemant Agrawal 		case RTE_CRYPTO_CIPHER_AES_CTR:
219905b12700SHemant Agrawal 			break;
220005b12700SHemant Agrawal 		default:
220105b12700SHemant Agrawal 			DPAA_SEC_ERR("Crypto: Unsupported Cipher alg %u",
220205b12700SHemant Agrawal 				cipher_xform->algo);
220305b12700SHemant Agrawal 			goto out;
220405b12700SHemant Agrawal 		}
220505b12700SHemant Agrawal 		session->cipher_alg = cipher_xform->algo;
220605b12700SHemant Agrawal 	} else {
220705b12700SHemant Agrawal 		session->cipher_key.data = NULL;
220805b12700SHemant Agrawal 		session->cipher_key.length = 0;
220905b12700SHemant Agrawal 		session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
221005b12700SHemant Agrawal 	}
221105b12700SHemant Agrawal 
221205b12700SHemant Agrawal 	if (auth_xform && auth_xform->algo != RTE_CRYPTO_AUTH_NULL) {
22131f14d500SAkhil Goyal 		session->auth_key.data = rte_zmalloc(NULL,
22141f14d500SAkhil Goyal 						auth_xform->key.length,
22151f14d500SAkhil Goyal 						RTE_CACHE_LINE_SIZE);
22161f14d500SAkhil Goyal 		if (session->auth_key.data == NULL &&
22171f14d500SAkhil Goyal 				auth_xform->key.length > 0) {
2218f163231eSHemant Agrawal 			DPAA_SEC_ERR("No Memory for auth key");
22191f14d500SAkhil Goyal 			rte_free(session->cipher_key.data);
22201f14d500SAkhil Goyal 			return -ENOMEM;
22211f14d500SAkhil Goyal 		}
22221f14d500SAkhil Goyal 		memcpy(session->auth_key.data, auth_xform->key.data,
22231f14d500SAkhil Goyal 				auth_xform->key.length);
222405b12700SHemant Agrawal 		session->auth_key.length = auth_xform->key.length;
22251f14d500SAkhil Goyal 
22261f14d500SAkhil Goyal 		switch (auth_xform->algo) {
22271f14d500SAkhil Goyal 		case RTE_CRYPTO_AUTH_SHA1_HMAC:
22281f14d500SAkhil Goyal 		case RTE_CRYPTO_AUTH_MD5_HMAC:
22291f14d500SAkhil Goyal 		case RTE_CRYPTO_AUTH_SHA256_HMAC:
22301f14d500SAkhil Goyal 		case RTE_CRYPTO_AUTH_SHA384_HMAC:
22311f14d500SAkhil Goyal 		case RTE_CRYPTO_AUTH_SHA512_HMAC:
22321f14d500SAkhil Goyal 		case RTE_CRYPTO_AUTH_AES_CMAC:
22331f14d500SAkhil Goyal 			break;
223405b12700SHemant Agrawal 		default:
2235f163231eSHemant Agrawal 			DPAA_SEC_ERR("Crypto: Unsupported auth alg %u",
22361f14d500SAkhil Goyal 				auth_xform->algo);
22371f14d500SAkhil Goyal 			goto out;
22381f14d500SAkhil Goyal 		}
223905b12700SHemant Agrawal 		session->auth_alg = auth_xform->algo;
224005b12700SHemant Agrawal 	} else {
224105b12700SHemant Agrawal 		session->auth_key.data = NULL;
224205b12700SHemant Agrawal 		session->auth_key.length = 0;
224305b12700SHemant Agrawal 		session->auth_alg = RTE_CRYPTO_AUTH_NULL;
22441f14d500SAkhil Goyal 	}
22451f14d500SAkhil Goyal 
22461f14d500SAkhil Goyal 	if (ipsec_xform->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
22475ab35d2eSAkhil Goyal 		if (ipsec_xform->tunnel.type ==
22485ab35d2eSAkhil Goyal 				RTE_SECURITY_IPSEC_TUNNEL_IPV4) {
22495ab35d2eSAkhil Goyal 			memset(&session->encap_pdb, 0,
22505ab35d2eSAkhil Goyal 				sizeof(struct ipsec_encap_pdb) +
22511f14d500SAkhil Goyal 				sizeof(session->ip4_hdr));
22521f14d500SAkhil Goyal 			session->ip4_hdr.ip_v = IPVERSION;
22531f14d500SAkhil Goyal 			session->ip4_hdr.ip_hl = 5;
22541f14d500SAkhil Goyal 			session->ip4_hdr.ip_len = rte_cpu_to_be_16(
22551f14d500SAkhil Goyal 						sizeof(session->ip4_hdr));
22561f14d500SAkhil Goyal 			session->ip4_hdr.ip_tos = ipsec_xform->tunnel.ipv4.dscp;
22571f14d500SAkhil Goyal 			session->ip4_hdr.ip_id = 0;
22581f14d500SAkhil Goyal 			session->ip4_hdr.ip_off = 0;
22591f14d500SAkhil Goyal 			session->ip4_hdr.ip_ttl = ipsec_xform->tunnel.ipv4.ttl;
22601f14d500SAkhil Goyal 			session->ip4_hdr.ip_p = (ipsec_xform->proto ==
22615ab35d2eSAkhil Goyal 					RTE_SECURITY_IPSEC_SA_PROTO_ESP) ?
22625ab35d2eSAkhil Goyal 					IPPROTO_ESP : IPPROTO_AH;
22631f14d500SAkhil Goyal 			session->ip4_hdr.ip_sum = 0;
22645ab35d2eSAkhil Goyal 			session->ip4_hdr.ip_src =
22655ab35d2eSAkhil Goyal 					ipsec_xform->tunnel.ipv4.src_ip;
22665ab35d2eSAkhil Goyal 			session->ip4_hdr.ip_dst =
22675ab35d2eSAkhil Goyal 					ipsec_xform->tunnel.ipv4.dst_ip;
22681f14d500SAkhil Goyal 			session->ip4_hdr.ip_sum = calc_chksum((uint16_t *)
22691f14d500SAkhil Goyal 						(void *)&session->ip4_hdr,
22701f14d500SAkhil Goyal 						sizeof(struct ip));
22715ab35d2eSAkhil Goyal 			session->encap_pdb.ip_hdr_len = sizeof(struct ip);
22725ab35d2eSAkhil Goyal 		} else if (ipsec_xform->tunnel.type ==
22735ab35d2eSAkhil Goyal 				RTE_SECURITY_IPSEC_TUNNEL_IPV6) {
22745ab35d2eSAkhil Goyal 			memset(&session->encap_pdb, 0,
22755ab35d2eSAkhil Goyal 				sizeof(struct ipsec_encap_pdb) +
22765ab35d2eSAkhil Goyal 				sizeof(session->ip6_hdr));
22775ab35d2eSAkhil Goyal 			session->ip6_hdr.vtc_flow = rte_cpu_to_be_32(
22785ab35d2eSAkhil Goyal 				DPAA_IPv6_DEFAULT_VTC_FLOW |
22795ab35d2eSAkhil Goyal 				((ipsec_xform->tunnel.ipv6.dscp <<
22805ab35d2eSAkhil Goyal 					RTE_IPV6_HDR_TC_SHIFT) &
22815ab35d2eSAkhil Goyal 					RTE_IPV6_HDR_TC_MASK) |
22825ab35d2eSAkhil Goyal 				((ipsec_xform->tunnel.ipv6.flabel <<
22835ab35d2eSAkhil Goyal 					RTE_IPV6_HDR_FL_SHIFT) &
22845ab35d2eSAkhil Goyal 					RTE_IPV6_HDR_FL_MASK));
22855ab35d2eSAkhil Goyal 			/* Payload length will be updated by HW */
22865ab35d2eSAkhil Goyal 			session->ip6_hdr.payload_len = 0;
22875ab35d2eSAkhil Goyal 			session->ip6_hdr.hop_limits =
22885ab35d2eSAkhil Goyal 					ipsec_xform->tunnel.ipv6.hlimit;
22895ab35d2eSAkhil Goyal 			session->ip6_hdr.proto = (ipsec_xform->proto ==
22905ab35d2eSAkhil Goyal 					RTE_SECURITY_IPSEC_SA_PROTO_ESP) ?
22915ab35d2eSAkhil Goyal 					IPPROTO_ESP : IPPROTO_AH;
22925ab35d2eSAkhil Goyal 			memcpy(&session->ip6_hdr.src_addr,
22935ab35d2eSAkhil Goyal 					&ipsec_xform->tunnel.ipv6.src_addr, 16);
22945ab35d2eSAkhil Goyal 			memcpy(&session->ip6_hdr.dst_addr,
22955ab35d2eSAkhil Goyal 					&ipsec_xform->tunnel.ipv6.dst_addr, 16);
22965ab35d2eSAkhil Goyal 			session->encap_pdb.ip_hdr_len =
22975ab35d2eSAkhil Goyal 						sizeof(struct rte_ipv6_hdr);
22985ab35d2eSAkhil Goyal 		}
22991f14d500SAkhil Goyal 		session->encap_pdb.options =
23001f14d500SAkhil Goyal 			(IPVERSION << PDBNH_ESP_ENCAP_SHIFT) |
23011f14d500SAkhil Goyal 			PDBOPTS_ESP_OIHI_PDB_INL |
23021f14d500SAkhil Goyal 			PDBOPTS_ESP_IVSRC |
230379fde9d0SAkhil Goyal 			PDBHMO_ESP_ENCAP_DTTL |
230479fde9d0SAkhil Goyal 			PDBHMO_ESP_SNR;
23050f318781SAkhil Goyal 		if (ipsec_xform->options.esn)
23060f318781SAkhil Goyal 			session->encap_pdb.options |= PDBOPTS_ESP_ESN;
23071f14d500SAkhil Goyal 		session->encap_pdb.spi = ipsec_xform->spi;
23081f14d500SAkhil Goyal 		session->dir = DIR_ENC;
23091f14d500SAkhil Goyal 	} else if (ipsec_xform->direction ==
23101f14d500SAkhil Goyal 			RTE_SECURITY_IPSEC_SA_DIR_INGRESS) {
23111f14d500SAkhil Goyal 		memset(&session->decap_pdb, 0, sizeof(struct ipsec_decap_pdb));
23125ab35d2eSAkhil Goyal 		if (ipsec_xform->tunnel.type == RTE_SECURITY_IPSEC_TUNNEL_IPV4)
23131f14d500SAkhil Goyal 			session->decap_pdb.options = sizeof(struct ip) << 16;
23145ab35d2eSAkhil Goyal 		else
23155ab35d2eSAkhil Goyal 			session->decap_pdb.options =
23165ab35d2eSAkhil Goyal 					sizeof(struct rte_ipv6_hdr) << 16;
23170f318781SAkhil Goyal 		if (ipsec_xform->options.esn)
23180f318781SAkhil Goyal 			session->decap_pdb.options |= PDBOPTS_ESP_ESN;
23191f14d500SAkhil Goyal 		session->dir = DIR_DEC;
23201f14d500SAkhil Goyal 	} else
23211f14d500SAkhil Goyal 		goto out;
23221f14d500SAkhil Goyal 	session->ctx_pool = internals->ctx_pool;
23233b617ee7SAkhil Goyal 	rte_spinlock_lock(&internals->lock);
23244e694fe5SAkhil Goyal 	for (i = 0; i < MAX_DPAA_CORES; i++) {
23254e694fe5SAkhil Goyal 		session->inq[i] = dpaa_sec_attach_rxq(internals);
23264e694fe5SAkhil Goyal 		if (session->inq[i] == NULL) {
2327f163231eSHemant Agrawal 			DPAA_SEC_ERR("unable to attach sec queue");
23284e694fe5SAkhil Goyal 			rte_spinlock_unlock(&internals->lock);
23291f14d500SAkhil Goyal 			goto out;
23301f14d500SAkhil Goyal 		}
23314e694fe5SAkhil Goyal 	}
23324e694fe5SAkhil Goyal 	rte_spinlock_unlock(&internals->lock);
23331f14d500SAkhil Goyal 
2334a1173d55SHemant Agrawal 	return 0;
2335a1173d55SHemant Agrawal out:
2336a1173d55SHemant Agrawal 	rte_free(session->auth_key.data);
2337a1173d55SHemant Agrawal 	rte_free(session->cipher_key.data);
2338a1173d55SHemant Agrawal 	memset(session, 0, sizeof(dpaa_sec_session));
2339a1173d55SHemant Agrawal 	return -1;
2340a1173d55SHemant Agrawal }
23411f14d500SAkhil Goyal 
2342a1173d55SHemant Agrawal static int
2343a1173d55SHemant Agrawal dpaa_sec_set_pdcp_session(struct rte_cryptodev *dev,
2344a1173d55SHemant Agrawal 			  struct rte_security_session_conf *conf,
2345a1173d55SHemant Agrawal 			  void *sess)
2346a1173d55SHemant Agrawal {
2347a1173d55SHemant Agrawal 	struct rte_security_pdcp_xform *pdcp_xform = &conf->pdcp;
2348a1173d55SHemant Agrawal 	struct rte_crypto_sym_xform *xform = conf->crypto_xform;
2349a1173d55SHemant Agrawal 	struct rte_crypto_auth_xform *auth_xform = NULL;
2350a1173d55SHemant Agrawal 	struct rte_crypto_cipher_xform *cipher_xform = NULL;
2351a1173d55SHemant Agrawal 	dpaa_sec_session *session = (dpaa_sec_session *)sess;
2352a1173d55SHemant Agrawal 	struct dpaa_sec_dev_private *dev_priv = dev->data->dev_private;
23534e694fe5SAkhil Goyal 	uint32_t i;
2354a1173d55SHemant Agrawal 
2355a1173d55SHemant Agrawal 	PMD_INIT_FUNC_TRACE();
2356a1173d55SHemant Agrawal 
2357a1173d55SHemant Agrawal 	memset(session, 0, sizeof(dpaa_sec_session));
2358a1173d55SHemant Agrawal 
2359a1173d55SHemant Agrawal 	/* find xfrm types */
2360a1173d55SHemant Agrawal 	if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
2361a1173d55SHemant Agrawal 		cipher_xform = &xform->cipher;
2362a1173d55SHemant Agrawal 		if (xform->next != NULL)
2363a1173d55SHemant Agrawal 			auth_xform = &xform->next->auth;
2364a1173d55SHemant Agrawal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
2365a1173d55SHemant Agrawal 		auth_xform = &xform->auth;
2366a1173d55SHemant Agrawal 		if (xform->next != NULL)
2367a1173d55SHemant Agrawal 			cipher_xform = &xform->next->cipher;
2368a1173d55SHemant Agrawal 	} else {
2369a1173d55SHemant Agrawal 		DPAA_SEC_ERR("Invalid crypto type");
2370a1173d55SHemant Agrawal 		return -EINVAL;
2371a1173d55SHemant Agrawal 	}
2372a1173d55SHemant Agrawal 
2373a1173d55SHemant Agrawal 	session->proto_alg = conf->protocol;
2374a1173d55SHemant Agrawal 	if (cipher_xform) {
2375a1173d55SHemant Agrawal 		session->cipher_key.data = rte_zmalloc(NULL,
2376a1173d55SHemant Agrawal 					       cipher_xform->key.length,
2377a1173d55SHemant Agrawal 					       RTE_CACHE_LINE_SIZE);
2378a1173d55SHemant Agrawal 		if (session->cipher_key.data == NULL &&
2379a1173d55SHemant Agrawal 				cipher_xform->key.length > 0) {
2380a1173d55SHemant Agrawal 			DPAA_SEC_ERR("No Memory for cipher key");
2381a1173d55SHemant Agrawal 			return -ENOMEM;
2382a1173d55SHemant Agrawal 		}
2383a1173d55SHemant Agrawal 		session->cipher_key.length = cipher_xform->key.length;
2384a1173d55SHemant Agrawal 		memcpy(session->cipher_key.data, cipher_xform->key.data,
2385a1173d55SHemant Agrawal 			cipher_xform->key.length);
2386a1173d55SHemant Agrawal 		session->dir = (cipher_xform->op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
2387a1173d55SHemant Agrawal 					DIR_ENC : DIR_DEC;
2388a1173d55SHemant Agrawal 		session->cipher_alg = cipher_xform->algo;
2389a1173d55SHemant Agrawal 	} else {
2390a1173d55SHemant Agrawal 		session->cipher_key.data = NULL;
2391a1173d55SHemant Agrawal 		session->cipher_key.length = 0;
2392a1173d55SHemant Agrawal 		session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
2393a1173d55SHemant Agrawal 		session->dir = DIR_ENC;
2394a1173d55SHemant Agrawal 	}
2395a1173d55SHemant Agrawal 
2396a1173d55SHemant Agrawal 	if (pdcp_xform->domain == RTE_SECURITY_PDCP_MODE_CONTROL) {
2397eac60082SVakul Garg 		if (pdcp_xform->sn_size != RTE_SECURITY_PDCP_SN_SIZE_5 &&
2398eac60082SVakul Garg 		    pdcp_xform->sn_size != RTE_SECURITY_PDCP_SN_SIZE_12) {
2399a1173d55SHemant Agrawal 			DPAA_SEC_ERR(
2400eac60082SVakul Garg 				"PDCP Seq Num size should be 5/12 bits for cmode");
2401a1173d55SHemant Agrawal 			goto out;
2402a1173d55SHemant Agrawal 		}
24032e4cbdb4SVakul Garg 	}
24042e4cbdb4SVakul Garg 
2405a1173d55SHemant Agrawal 	if (auth_xform) {
2406a1173d55SHemant Agrawal 		session->auth_key.data = rte_zmalloc(NULL,
2407a1173d55SHemant Agrawal 						     auth_xform->key.length,
2408a1173d55SHemant Agrawal 						     RTE_CACHE_LINE_SIZE);
24092e4cbdb4SVakul Garg 		if (!session->auth_key.data &&
2410a1173d55SHemant Agrawal 		    auth_xform->key.length > 0) {
2411a1173d55SHemant Agrawal 			DPAA_SEC_ERR("No Memory for auth key");
2412a1173d55SHemant Agrawal 			rte_free(session->cipher_key.data);
2413a1173d55SHemant Agrawal 			return -ENOMEM;
2414a1173d55SHemant Agrawal 		}
2415a1173d55SHemant Agrawal 		session->auth_key.length = auth_xform->key.length;
2416a1173d55SHemant Agrawal 		memcpy(session->auth_key.data, auth_xform->key.data,
2417a1173d55SHemant Agrawal 		       auth_xform->key.length);
2418a1173d55SHemant Agrawal 		session->auth_alg = auth_xform->algo;
2419a1173d55SHemant Agrawal 	} else {
2420a1173d55SHemant Agrawal 		session->auth_key.data = NULL;
2421a1173d55SHemant Agrawal 		session->auth_key.length = 0;
24222e4cbdb4SVakul Garg 		session->auth_alg = 0;
2423a1173d55SHemant Agrawal 	}
2424a1173d55SHemant Agrawal 	session->pdcp.domain = pdcp_xform->domain;
2425a1173d55SHemant Agrawal 	session->pdcp.bearer = pdcp_xform->bearer;
2426a1173d55SHemant Agrawal 	session->pdcp.pkt_dir = pdcp_xform->pkt_dir;
2427a1173d55SHemant Agrawal 	session->pdcp.sn_size = pdcp_xform->sn_size;
2428a1173d55SHemant Agrawal 	session->pdcp.hfn = pdcp_xform->hfn;
2429a1173d55SHemant Agrawal 	session->pdcp.hfn_threshold = pdcp_xform->hfn_threshold;
2430*6a0c9d36SAkhil Goyal 	session->pdcp.hfn_ovd = pdcp_xform->hfn_ovrd;
2431*6a0c9d36SAkhil Goyal 	session->pdcp.hfn_ovd_offset = cipher_xform->iv.offset;
2432a1173d55SHemant Agrawal 
2433a1173d55SHemant Agrawal 	session->ctx_pool = dev_priv->ctx_pool;
2434a1173d55SHemant Agrawal 	rte_spinlock_lock(&dev_priv->lock);
24354e694fe5SAkhil Goyal 	for (i = 0; i < MAX_DPAA_CORES; i++) {
24364e694fe5SAkhil Goyal 		session->inq[i] = dpaa_sec_attach_rxq(dev_priv);
24374e694fe5SAkhil Goyal 		if (session->inq[i] == NULL) {
2438a1173d55SHemant Agrawal 			DPAA_SEC_ERR("unable to attach sec queue");
24394e694fe5SAkhil Goyal 			rte_spinlock_unlock(&dev_priv->lock);
2440a1173d55SHemant Agrawal 			goto out;
2441a1173d55SHemant Agrawal 		}
24424e694fe5SAkhil Goyal 	}
24434e694fe5SAkhil Goyal 	rte_spinlock_unlock(&dev_priv->lock);
24441f14d500SAkhil Goyal 	return 0;
24451f14d500SAkhil Goyal out:
24461f14d500SAkhil Goyal 	rte_free(session->auth_key.data);
24471f14d500SAkhil Goyal 	rte_free(session->cipher_key.data);
24481f14d500SAkhil Goyal 	memset(session, 0, sizeof(dpaa_sec_session));
24491f14d500SAkhil Goyal 	return -1;
24501f14d500SAkhil Goyal }
24511f14d500SAkhil Goyal 
24521f14d500SAkhil Goyal static int
24531f14d500SAkhil Goyal dpaa_sec_security_session_create(void *dev,
24541f14d500SAkhil Goyal 				 struct rte_security_session_conf *conf,
24551f14d500SAkhil Goyal 				 struct rte_security_session *sess,
24561f14d500SAkhil Goyal 				 struct rte_mempool *mempool)
24571f14d500SAkhil Goyal {
24581f14d500SAkhil Goyal 	void *sess_private_data;
24591f14d500SAkhil Goyal 	struct rte_cryptodev *cdev = (struct rte_cryptodev *)dev;
24601f14d500SAkhil Goyal 	int ret;
24611f14d500SAkhil Goyal 
24621f14d500SAkhil Goyal 	if (rte_mempool_get(mempool, &sess_private_data)) {
2463f163231eSHemant Agrawal 		DPAA_SEC_ERR("Couldn't get object from session mempool");
24641f14d500SAkhil Goyal 		return -ENOMEM;
24651f14d500SAkhil Goyal 	}
24661f14d500SAkhil Goyal 
24671f14d500SAkhil Goyal 	switch (conf->protocol) {
24681f14d500SAkhil Goyal 	case RTE_SECURITY_PROTOCOL_IPSEC:
24691f14d500SAkhil Goyal 		ret = dpaa_sec_set_ipsec_session(cdev, conf,
24701f14d500SAkhil Goyal 				sess_private_data);
24711f14d500SAkhil Goyal 		break;
2472a1173d55SHemant Agrawal 	case RTE_SECURITY_PROTOCOL_PDCP:
2473a1173d55SHemant Agrawal 		ret = dpaa_sec_set_pdcp_session(cdev, conf,
2474a1173d55SHemant Agrawal 				sess_private_data);
2475a1173d55SHemant Agrawal 		break;
24761f14d500SAkhil Goyal 	case RTE_SECURITY_PROTOCOL_MACSEC:
24771f14d500SAkhil Goyal 		return -ENOTSUP;
24781f14d500SAkhil Goyal 	default:
24791f14d500SAkhil Goyal 		return -EINVAL;
24801f14d500SAkhil Goyal 	}
24811f14d500SAkhil Goyal 	if (ret != 0) {
2482f163231eSHemant Agrawal 		DPAA_SEC_ERR("failed to configure session parameters");
24831f14d500SAkhil Goyal 		/* Return session to mempool */
24841f14d500SAkhil Goyal 		rte_mempool_put(mempool, sess_private_data);
24851f14d500SAkhil Goyal 		return ret;
24861f14d500SAkhil Goyal 	}
24871f14d500SAkhil Goyal 
24881f14d500SAkhil Goyal 	set_sec_session_private_data(sess, sess_private_data);
24891f14d500SAkhil Goyal 
24901f14d500SAkhil Goyal 	return ret;
24911f14d500SAkhil Goyal }
24921f14d500SAkhil Goyal 
24931f14d500SAkhil Goyal /** Clear the memory of session so it doesn't leave key material behind */
24941f14d500SAkhil Goyal static int
24951f14d500SAkhil Goyal dpaa_sec_security_session_destroy(void *dev __rte_unused,
24961f14d500SAkhil Goyal 		struct rte_security_session *sess)
24971f14d500SAkhil Goyal {
24981f14d500SAkhil Goyal 	PMD_INIT_FUNC_TRACE();
24991f14d500SAkhil Goyal 	void *sess_priv = get_sec_session_private_data(sess);
25001f14d500SAkhil Goyal 	dpaa_sec_session *s = (dpaa_sec_session *)sess_priv;
25011f14d500SAkhil Goyal 
25021f14d500SAkhil Goyal 	if (sess_priv) {
25033d0d5332SAkhil Goyal 		free_session_memory((struct rte_cryptodev *)dev, s);
25041f14d500SAkhil Goyal 		set_sec_session_private_data(sess, NULL);
25051f14d500SAkhil Goyal 	}
25061f14d500SAkhil Goyal 	return 0;
25071f14d500SAkhil Goyal }
25081f14d500SAkhil Goyal 
25091f14d500SAkhil Goyal static int
25107e3e2954SAkhil Goyal dpaa_sec_dev_configure(struct rte_cryptodev *dev,
2511c3e85bdcSAkhil Goyal 		       struct rte_cryptodev_config *config __rte_unused)
2512c3e85bdcSAkhil Goyal {
25137e3e2954SAkhil Goyal 
25147e3e2954SAkhil Goyal 	char str[20];
25157e3e2954SAkhil Goyal 	struct dpaa_sec_dev_private *internals;
25167e3e2954SAkhil Goyal 
2517c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2518c3e85bdcSAkhil Goyal 
25197e3e2954SAkhil Goyal 	internals = dev->data->dev_private;
2520a1e8241aSPallantla Poornima 	snprintf(str, sizeof(str), "ctx_pool_%d", dev->data->dev_id);
25217e3e2954SAkhil Goyal 	if (!internals->ctx_pool) {
25227e3e2954SAkhil Goyal 		internals->ctx_pool = rte_mempool_create((const char *)str,
25237e3e2954SAkhil Goyal 							CTX_POOL_NUM_BUFS,
25247e3e2954SAkhil Goyal 							CTX_POOL_BUF_SIZE,
25257e3e2954SAkhil Goyal 							CTX_POOL_CACHE_SIZE, 0,
25267e3e2954SAkhil Goyal 							NULL, NULL, NULL, NULL,
25277e3e2954SAkhil Goyal 							SOCKET_ID_ANY, 0);
25287e3e2954SAkhil Goyal 		if (!internals->ctx_pool) {
2529f163231eSHemant Agrawal 			DPAA_SEC_ERR("%s create failed\n", str);
25307e3e2954SAkhil Goyal 			return -ENOMEM;
25317e3e2954SAkhil Goyal 		}
25327e3e2954SAkhil Goyal 	} else
2533f163231eSHemant Agrawal 		DPAA_SEC_INFO("mempool already created for dev_id : %d",
25347e3e2954SAkhil Goyal 				dev->data->dev_id);
25357e3e2954SAkhil Goyal 
2536c3e85bdcSAkhil Goyal 	return 0;
2537c3e85bdcSAkhil Goyal }
2538c3e85bdcSAkhil Goyal 
2539c3e85bdcSAkhil Goyal static int
2540c3e85bdcSAkhil Goyal dpaa_sec_dev_start(struct rte_cryptodev *dev __rte_unused)
2541c3e85bdcSAkhil Goyal {
2542c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2543c3e85bdcSAkhil Goyal 	return 0;
2544c3e85bdcSAkhil Goyal }
2545c3e85bdcSAkhil Goyal 
2546c3e85bdcSAkhil Goyal static void
2547c3e85bdcSAkhil Goyal dpaa_sec_dev_stop(struct rte_cryptodev *dev __rte_unused)
2548c3e85bdcSAkhil Goyal {
2549c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2550c3e85bdcSAkhil Goyal }
2551c3e85bdcSAkhil Goyal 
2552c3e85bdcSAkhil Goyal static int
25537e3e2954SAkhil Goyal dpaa_sec_dev_close(struct rte_cryptodev *dev)
2554c3e85bdcSAkhil Goyal {
25557e3e2954SAkhil Goyal 	struct dpaa_sec_dev_private *internals;
25567e3e2954SAkhil Goyal 
2557c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
25587e3e2954SAkhil Goyal 
25597e3e2954SAkhil Goyal 	if (dev == NULL)
25607e3e2954SAkhil Goyal 		return -ENOMEM;
25617e3e2954SAkhil Goyal 
25627e3e2954SAkhil Goyal 	internals = dev->data->dev_private;
25637e3e2954SAkhil Goyal 	rte_mempool_free(internals->ctx_pool);
25647e3e2954SAkhil Goyal 	internals->ctx_pool = NULL;
25657e3e2954SAkhil Goyal 
2566c3e85bdcSAkhil Goyal 	return 0;
2567c3e85bdcSAkhil Goyal }
2568c3e85bdcSAkhil Goyal 
2569c3e85bdcSAkhil Goyal static void
2570c3e85bdcSAkhil Goyal dpaa_sec_dev_infos_get(struct rte_cryptodev *dev,
2571c3e85bdcSAkhil Goyal 		       struct rte_cryptodev_info *info)
2572c3e85bdcSAkhil Goyal {
2573c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals = dev->data->dev_private;
2574c3e85bdcSAkhil Goyal 
2575c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2576c3e85bdcSAkhil Goyal 	if (info != NULL) {
2577c3e85bdcSAkhil Goyal 		info->max_nb_queue_pairs = internals->max_nb_queue_pairs;
2578c3e85bdcSAkhil Goyal 		info->feature_flags = dev->feature_flags;
2579c3e85bdcSAkhil Goyal 		info->capabilities = dpaa_sec_capabilities;
2580c3e85bdcSAkhil Goyal 		info->sym.max_nb_sessions = internals->max_nb_sessions;
2581c3e85bdcSAkhil Goyal 		info->driver_id = cryptodev_driver_id;
2582c3e85bdcSAkhil Goyal 	}
2583c3e85bdcSAkhil Goyal }
2584c3e85bdcSAkhil Goyal 
2585c3e85bdcSAkhil Goyal static struct rte_cryptodev_ops crypto_ops = {
2586c3e85bdcSAkhil Goyal 	.dev_configure	      = dpaa_sec_dev_configure,
2587c3e85bdcSAkhil Goyal 	.dev_start	      = dpaa_sec_dev_start,
2588c3e85bdcSAkhil Goyal 	.dev_stop	      = dpaa_sec_dev_stop,
2589c3e85bdcSAkhil Goyal 	.dev_close	      = dpaa_sec_dev_close,
2590c3e85bdcSAkhil Goyal 	.dev_infos_get        = dpaa_sec_dev_infos_get,
2591c3e85bdcSAkhil Goyal 	.queue_pair_setup     = dpaa_sec_queue_pair_setup,
2592c3e85bdcSAkhil Goyal 	.queue_pair_release   = dpaa_sec_queue_pair_release,
2593c3e85bdcSAkhil Goyal 	.queue_pair_count     = dpaa_sec_queue_pair_count,
2594012c5076SPablo de Lara 	.sym_session_get_size     = dpaa_sec_sym_session_get_size,
2595012c5076SPablo de Lara 	.sym_session_configure    = dpaa_sec_sym_session_configure,
2596012c5076SPablo de Lara 	.sym_session_clear        = dpaa_sec_sym_session_clear
2597c3e85bdcSAkhil Goyal };
2598c3e85bdcSAkhil Goyal 
25991f14d500SAkhil Goyal static const struct rte_security_capability *
26001f14d500SAkhil Goyal dpaa_sec_capabilities_get(void *device __rte_unused)
26011f14d500SAkhil Goyal {
26021f14d500SAkhil Goyal 	return dpaa_sec_security_cap;
26031f14d500SAkhil Goyal }
26041f14d500SAkhil Goyal 
2605b74fd6b8SFerruh Yigit static const struct rte_security_ops dpaa_sec_security_ops = {
26061f14d500SAkhil Goyal 	.session_create = dpaa_sec_security_session_create,
26071f14d500SAkhil Goyal 	.session_update = NULL,
26081f14d500SAkhil Goyal 	.session_stats_get = NULL,
26091f14d500SAkhil Goyal 	.session_destroy = dpaa_sec_security_session_destroy,
26101f14d500SAkhil Goyal 	.set_pkt_metadata = NULL,
26111f14d500SAkhil Goyal 	.capabilities_get = dpaa_sec_capabilities_get
26121f14d500SAkhil Goyal };
26131f14d500SAkhil Goyal 
2614c3e85bdcSAkhil Goyal static int
2615c3e85bdcSAkhil Goyal dpaa_sec_uninit(struct rte_cryptodev *dev)
2616c3e85bdcSAkhil Goyal {
2617debef417SShreyansh Jain 	struct dpaa_sec_dev_private *internals;
2618c3e85bdcSAkhil Goyal 
2619c3e85bdcSAkhil Goyal 	if (dev == NULL)
2620c3e85bdcSAkhil Goyal 		return -ENODEV;
2621c3e85bdcSAkhil Goyal 
2622debef417SShreyansh Jain 	internals = dev->data->dev_private;
26231f14d500SAkhil Goyal 	rte_free(dev->security_ctx);
26241f14d500SAkhil Goyal 
26257e3e2954SAkhil Goyal 	/* In case close has been called, internals->ctx_pool would be NULL */
2626c3e85bdcSAkhil Goyal 	rte_mempool_free(internals->ctx_pool);
2627c3e85bdcSAkhil Goyal 	rte_free(internals);
2628c3e85bdcSAkhil Goyal 
2629f163231eSHemant Agrawal 	DPAA_SEC_INFO("Closing DPAA_SEC device %s on numa socket %u",
2630c3e85bdcSAkhil Goyal 		      dev->data->name, rte_socket_id());
2631c3e85bdcSAkhil Goyal 
2632c3e85bdcSAkhil Goyal 	return 0;
2633c3e85bdcSAkhil Goyal }
2634c3e85bdcSAkhil Goyal 
2635c3e85bdcSAkhil Goyal static int
2636c3e85bdcSAkhil Goyal dpaa_sec_dev_init(struct rte_cryptodev *cryptodev)
2637c3e85bdcSAkhil Goyal {
2638c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals;
26391f14d500SAkhil Goyal 	struct rte_security_ctx *security_instance;
2640c3e85bdcSAkhil Goyal 	struct dpaa_sec_qp *qp;
2641e79416d1SHemant Agrawal 	uint32_t i, flags;
2642c3e85bdcSAkhil Goyal 	int ret;
2643c3e85bdcSAkhil Goyal 
2644c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2645c3e85bdcSAkhil Goyal 
2646c3e85bdcSAkhil Goyal 	cryptodev->driver_id = cryptodev_driver_id;
2647c3e85bdcSAkhil Goyal 	cryptodev->dev_ops = &crypto_ops;
2648c3e85bdcSAkhil Goyal 
2649c3e85bdcSAkhil Goyal 	cryptodev->enqueue_burst = dpaa_sec_enqueue_burst;
2650c3e85bdcSAkhil Goyal 	cryptodev->dequeue_burst = dpaa_sec_dequeue_burst;
2651c3e85bdcSAkhil Goyal 	cryptodev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
2652c3e85bdcSAkhil Goyal 			RTE_CRYPTODEV_FF_HW_ACCELERATED |
26531f14d500SAkhil Goyal 			RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
2654a74af788SAkhil Goyal 			RTE_CRYPTODEV_FF_SECURITY |
26552717246eSPablo de Lara 			RTE_CRYPTODEV_FF_IN_PLACE_SGL |
26562717246eSPablo de Lara 			RTE_CRYPTODEV_FF_OOP_SGL_IN_SGL_OUT |
26572717246eSPablo de Lara 			RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT |
26582717246eSPablo de Lara 			RTE_CRYPTODEV_FF_OOP_LB_IN_SGL_OUT |
26592717246eSPablo de Lara 			RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT;
2660c3e85bdcSAkhil Goyal 
2661c3e85bdcSAkhil Goyal 	internals = cryptodev->data->dev_private;
2662e79416d1SHemant Agrawal 	internals->max_nb_queue_pairs = RTE_DPAA_MAX_NB_SEC_QPS;
2663c3e85bdcSAkhil Goyal 	internals->max_nb_sessions = RTE_DPAA_SEC_PMD_MAX_NB_SESSIONS;
2664c3e85bdcSAkhil Goyal 
26651f14d500SAkhil Goyal 	/*
26661f14d500SAkhil Goyal 	 * For secondary processes, we don't initialise any further as primary
26671f14d500SAkhil Goyal 	 * has already done this work. Only check we don't need a different
26681f14d500SAkhil Goyal 	 * RX function
26691f14d500SAkhil Goyal 	 */
26701f14d500SAkhil Goyal 	if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
2671f163231eSHemant Agrawal 		DPAA_SEC_WARN("Device already init by primary process");
26721f14d500SAkhil Goyal 		return 0;
26731f14d500SAkhil Goyal 	}
26741f14d500SAkhil Goyal 
26751f14d500SAkhil Goyal 	/* Initialize security_ctx only for primary process*/
26761f14d500SAkhil Goyal 	security_instance = rte_malloc("rte_security_instances_ops",
26771f14d500SAkhil Goyal 				sizeof(struct rte_security_ctx), 0);
26781f14d500SAkhil Goyal 	if (security_instance == NULL)
26791f14d500SAkhil Goyal 		return -ENOMEM;
26801f14d500SAkhil Goyal 	security_instance->device = (void *)cryptodev;
26811f14d500SAkhil Goyal 	security_instance->ops = &dpaa_sec_security_ops;
26821f14d500SAkhil Goyal 	security_instance->sess_cnt = 0;
26831f14d500SAkhil Goyal 	cryptodev->security_ctx = security_instance;
26841f14d500SAkhil Goyal 
26853b617ee7SAkhil Goyal 	rte_spinlock_init(&internals->lock);
2686c3e85bdcSAkhil Goyal 	for (i = 0; i < internals->max_nb_queue_pairs; i++) {
2687c3e85bdcSAkhil Goyal 		/* init qman fq for queue pair */
2688c3e85bdcSAkhil Goyal 		qp = &internals->qps[i];
2689c3e85bdcSAkhil Goyal 		ret = dpaa_sec_init_tx(&qp->outq);
2690c3e85bdcSAkhil Goyal 		if (ret) {
2691f163231eSHemant Agrawal 			DPAA_SEC_ERR("config tx of queue pair  %d", i);
2692c3e85bdcSAkhil Goyal 			goto init_error;
2693c3e85bdcSAkhil Goyal 		}
2694e79416d1SHemant Agrawal 	}
2695e79416d1SHemant Agrawal 
2696e79416d1SHemant Agrawal 	flags = QMAN_FQ_FLAG_LOCKED | QMAN_FQ_FLAG_DYNAMIC_FQID |
2697e79416d1SHemant Agrawal 		QMAN_FQ_FLAG_TO_DCPORTAL;
26984e694fe5SAkhil Goyal 	for (i = 0; i < MAX_DPAA_CORES * internals->max_nb_sessions; i++) {
2699e79416d1SHemant Agrawal 		/* create rx qman fq for sessions*/
2700e79416d1SHemant Agrawal 		ret = qman_create_fq(0, flags, &internals->inq[i]);
2701e79416d1SHemant Agrawal 		if (unlikely(ret != 0)) {
2702f163231eSHemant Agrawal 			DPAA_SEC_ERR("sec qman_create_fq failed");
2703c3e85bdcSAkhil Goyal 			goto init_error;
2704c3e85bdcSAkhil Goyal 		}
2705c3e85bdcSAkhil Goyal 	}
2706c3e85bdcSAkhil Goyal 
2707f163231eSHemant Agrawal 	RTE_LOG(INFO, PMD, "%s cryptodev init\n", cryptodev->data->name);
2708c3e85bdcSAkhil Goyal 	return 0;
2709c3e85bdcSAkhil Goyal 
2710c3e85bdcSAkhil Goyal init_error:
2711f163231eSHemant Agrawal 	DPAA_SEC_ERR("driver %s: create failed\n", cryptodev->data->name);
2712c3e85bdcSAkhil Goyal 
2713c3e85bdcSAkhil Goyal 	dpaa_sec_uninit(cryptodev);
2714c3e85bdcSAkhil Goyal 	return -EFAULT;
2715c3e85bdcSAkhil Goyal }
2716c3e85bdcSAkhil Goyal 
2717c3e85bdcSAkhil Goyal static int
2718eb5a9a76SShreyansh Jain cryptodev_dpaa_sec_probe(struct rte_dpaa_driver *dpaa_drv __rte_unused,
2719c3e85bdcSAkhil Goyal 				struct rte_dpaa_device *dpaa_dev)
2720c3e85bdcSAkhil Goyal {
2721c3e85bdcSAkhil Goyal 	struct rte_cryptodev *cryptodev;
2722c3e85bdcSAkhil Goyal 	char cryptodev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
2723c3e85bdcSAkhil Goyal 
2724c3e85bdcSAkhil Goyal 	int retval;
2725c3e85bdcSAkhil Goyal 
2726a1e8241aSPallantla Poornima 	snprintf(cryptodev_name, sizeof(cryptodev_name), "dpaa_sec-%d",
2727a1e8241aSPallantla Poornima 			dpaa_dev->id.dev_id);
2728c3e85bdcSAkhil Goyal 
2729c3e85bdcSAkhil Goyal 	cryptodev = rte_cryptodev_pmd_allocate(cryptodev_name, rte_socket_id());
2730c3e85bdcSAkhil Goyal 	if (cryptodev == NULL)
2731c3e85bdcSAkhil Goyal 		return -ENOMEM;
2732c3e85bdcSAkhil Goyal 
2733c3e85bdcSAkhil Goyal 	if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
2734c3e85bdcSAkhil Goyal 		cryptodev->data->dev_private = rte_zmalloc_socket(
2735c3e85bdcSAkhil Goyal 					"cryptodev private structure",
2736c3e85bdcSAkhil Goyal 					sizeof(struct dpaa_sec_dev_private),
2737c3e85bdcSAkhil Goyal 					RTE_CACHE_LINE_SIZE,
2738c3e85bdcSAkhil Goyal 					rte_socket_id());
2739c3e85bdcSAkhil Goyal 
2740c3e85bdcSAkhil Goyal 		if (cryptodev->data->dev_private == NULL)
2741c3e85bdcSAkhil Goyal 			rte_panic("Cannot allocate memzone for private "
2742c3e85bdcSAkhil Goyal 					"device data");
2743c3e85bdcSAkhil Goyal 	}
2744c3e85bdcSAkhil Goyal 
2745c3e85bdcSAkhil Goyal 	dpaa_dev->crypto_dev = cryptodev;
2746c3e85bdcSAkhil Goyal 	cryptodev->device = &dpaa_dev->device;
2747c3e85bdcSAkhil Goyal 
2748c3e85bdcSAkhil Goyal 	/* init user callbacks */
2749c3e85bdcSAkhil Goyal 	TAILQ_INIT(&(cryptodev->link_intr_cbs));
2750c3e85bdcSAkhil Goyal 
2751c3e85bdcSAkhil Goyal 	/* if sec device version is not configured */
2752c3e85bdcSAkhil Goyal 	if (!rta_get_sec_era()) {
2753c3e85bdcSAkhil Goyal 		const struct device_node *caam_node;
2754c3e85bdcSAkhil Goyal 
2755c3e85bdcSAkhil Goyal 		for_each_compatible_node(caam_node, NULL, "fsl,sec-v4.0") {
2756c3e85bdcSAkhil Goyal 			const uint32_t *prop = of_get_property(caam_node,
2757c3e85bdcSAkhil Goyal 					"fsl,sec-era",
2758c3e85bdcSAkhil Goyal 					NULL);
2759c3e85bdcSAkhil Goyal 			if (prop) {
2760c3e85bdcSAkhil Goyal 				rta_set_sec_era(
2761c3e85bdcSAkhil Goyal 					INTL_SEC_ERA(rte_cpu_to_be_32(*prop)));
2762c3e85bdcSAkhil Goyal 				break;
2763c3e85bdcSAkhil Goyal 			}
2764c3e85bdcSAkhil Goyal 		}
2765c3e85bdcSAkhil Goyal 	}
2766c3e85bdcSAkhil Goyal 
2767c3e85bdcSAkhil Goyal 	/* Invoke PMD device initialization function */
2768c3e85bdcSAkhil Goyal 	retval = dpaa_sec_dev_init(cryptodev);
2769c3e85bdcSAkhil Goyal 	if (retval == 0)
2770c3e85bdcSAkhil Goyal 		return 0;
2771c3e85bdcSAkhil Goyal 
2772c3e85bdcSAkhil Goyal 	/* In case of error, cleanup is done */
2773c3e85bdcSAkhil Goyal 	if (rte_eal_process_type() == RTE_PROC_PRIMARY)
2774c3e85bdcSAkhil Goyal 		rte_free(cryptodev->data->dev_private);
2775c3e85bdcSAkhil Goyal 
2776c3e85bdcSAkhil Goyal 	rte_cryptodev_pmd_release_device(cryptodev);
2777c3e85bdcSAkhil Goyal 
2778c3e85bdcSAkhil Goyal 	return -ENXIO;
2779c3e85bdcSAkhil Goyal }
2780c3e85bdcSAkhil Goyal 
2781c3e85bdcSAkhil Goyal static int
2782c3e85bdcSAkhil Goyal cryptodev_dpaa_sec_remove(struct rte_dpaa_device *dpaa_dev)
2783c3e85bdcSAkhil Goyal {
2784c3e85bdcSAkhil Goyal 	struct rte_cryptodev *cryptodev;
2785c3e85bdcSAkhil Goyal 	int ret;
2786c3e85bdcSAkhil Goyal 
2787c3e85bdcSAkhil Goyal 	cryptodev = dpaa_dev->crypto_dev;
2788c3e85bdcSAkhil Goyal 	if (cryptodev == NULL)
2789c3e85bdcSAkhil Goyal 		return -ENODEV;
2790c3e85bdcSAkhil Goyal 
2791c3e85bdcSAkhil Goyal 	ret = dpaa_sec_uninit(cryptodev);
2792c3e85bdcSAkhil Goyal 	if (ret)
2793c3e85bdcSAkhil Goyal 		return ret;
2794c3e85bdcSAkhil Goyal 
2795f2f020d2SDeclan Doherty 	return rte_cryptodev_pmd_destroy(cryptodev);
2796c3e85bdcSAkhil Goyal }
2797c3e85bdcSAkhil Goyal 
2798c3e85bdcSAkhil Goyal static struct rte_dpaa_driver rte_dpaa_sec_driver = {
2799c3e85bdcSAkhil Goyal 	.drv_type = FSL_DPAA_CRYPTO,
2800c3e85bdcSAkhil Goyal 	.driver = {
2801c3e85bdcSAkhil Goyal 		.name = "DPAA SEC PMD"
2802c3e85bdcSAkhil Goyal 	},
2803c3e85bdcSAkhil Goyal 	.probe = cryptodev_dpaa_sec_probe,
2804c3e85bdcSAkhil Goyal 	.remove = cryptodev_dpaa_sec_remove,
2805c3e85bdcSAkhil Goyal };
2806c3e85bdcSAkhil Goyal 
2807c3e85bdcSAkhil Goyal static struct cryptodev_driver dpaa_sec_crypto_drv;
2808c3e85bdcSAkhil Goyal 
2809c3e85bdcSAkhil Goyal RTE_PMD_REGISTER_DPAA(CRYPTODEV_NAME_DPAA_SEC_PMD, rte_dpaa_sec_driver);
2810f737f5ceSFiona Trahe RTE_PMD_REGISTER_CRYPTO_DRIVER(dpaa_sec_crypto_drv, rte_dpaa_sec_driver.driver,
2811c3e85bdcSAkhil Goyal 		cryptodev_driver_id);
2812f163231eSHemant Agrawal 
2813f8e99896SThomas Monjalon RTE_INIT(dpaa_sec_init_log)
2814f163231eSHemant Agrawal {
2815f163231eSHemant Agrawal 	dpaa_logtype_sec = rte_log_register("pmd.crypto.dpaa");
2816f163231eSHemant Agrawal 	if (dpaa_logtype_sec >= 0)
2817f163231eSHemant Agrawal 		rte_log_set_level(dpaa_logtype_sec, RTE_LOG_NOTICE);
2818f163231eSHemant Agrawal }
2819