xref: /dpdk/drivers/crypto/dpaa_sec/dpaa_sec.c (revision f8e9989606e7a9548a25ac10e2daae4c8af230cb)
1d81734caSHemant Agrawal /* SPDX-License-Identifier: BSD-3-Clause
2c3e85bdcSAkhil Goyal  *
3c3e85bdcSAkhil Goyal  *   Copyright (c) 2016 Freescale Semiconductor, Inc. All rights reserved.
4f163231eSHemant Agrawal  *   Copyright 2017-2018 NXP
5c3e85bdcSAkhil Goyal  *
6c3e85bdcSAkhil Goyal  */
7c3e85bdcSAkhil Goyal 
8c3e85bdcSAkhil Goyal #include <fcntl.h>
9c3e85bdcSAkhil Goyal #include <unistd.h>
10c3e85bdcSAkhil Goyal #include <sched.h>
11c3e85bdcSAkhil Goyal #include <net/if.h>
12c3e85bdcSAkhil Goyal 
13c3e85bdcSAkhil Goyal #include <rte_byteorder.h>
14c3e85bdcSAkhil Goyal #include <rte_common.h>
15c3e85bdcSAkhil Goyal #include <rte_cryptodev_pmd.h>
16c3e85bdcSAkhil Goyal #include <rte_crypto.h>
17c3e85bdcSAkhil Goyal #include <rte_cryptodev.h>
181f14d500SAkhil Goyal #include <rte_security_driver.h>
19c3e85bdcSAkhil Goyal #include <rte_cycles.h>
20c3e85bdcSAkhil Goyal #include <rte_dev.h>
21c3e85bdcSAkhil Goyal #include <rte_kvargs.h>
22c3e85bdcSAkhil Goyal #include <rte_malloc.h>
23c3e85bdcSAkhil Goyal #include <rte_mbuf.h>
24c3e85bdcSAkhil Goyal #include <rte_memcpy.h>
25c3e85bdcSAkhil Goyal #include <rte_string_fns.h>
26c3e85bdcSAkhil Goyal 
27c3e85bdcSAkhil Goyal #include <fsl_usd.h>
28c3e85bdcSAkhil Goyal #include <fsl_qman.h>
29c3e85bdcSAkhil Goyal #include <of.h>
30c3e85bdcSAkhil Goyal 
31c3e85bdcSAkhil Goyal /* RTA header files */
32c3e85bdcSAkhil Goyal #include <hw/desc/common.h>
33c3e85bdcSAkhil Goyal #include <hw/desc/algo.h>
34c3e85bdcSAkhil Goyal #include <hw/desc/ipsec.h>
35c3e85bdcSAkhil Goyal 
36c3e85bdcSAkhil Goyal #include <rte_dpaa_bus.h>
37c3e85bdcSAkhil Goyal #include <dpaa_sec.h>
38c3e85bdcSAkhil Goyal #include <dpaa_sec_log.h>
39c3e85bdcSAkhil Goyal 
40c3e85bdcSAkhil Goyal enum rta_sec_era rta_sec_era;
41c3e85bdcSAkhil Goyal 
42f163231eSHemant Agrawal int dpaa_logtype_sec;
43f163231eSHemant Agrawal 
44c3e85bdcSAkhil Goyal static uint8_t cryptodev_driver_id;
45c3e85bdcSAkhil Goyal 
46c3e85bdcSAkhil Goyal static __thread struct rte_crypto_op **dpaa_sec_ops;
47c3e85bdcSAkhil Goyal static __thread int dpaa_sec_op_nb;
48c3e85bdcSAkhil Goyal 
49e79416d1SHemant Agrawal static int
50e79416d1SHemant Agrawal dpaa_sec_attach_sess_q(struct dpaa_sec_qp *qp, dpaa_sec_session *sess);
51e79416d1SHemant Agrawal 
52c3e85bdcSAkhil Goyal static inline void
53c3e85bdcSAkhil Goyal dpaa_sec_op_ending(struct dpaa_sec_op_ctx *ctx)
54c3e85bdcSAkhil Goyal {
55c3e85bdcSAkhil Goyal 	if (!ctx->fd_status) {
56c3e85bdcSAkhil Goyal 		ctx->op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
57c3e85bdcSAkhil Goyal 	} else {
58f163231eSHemant Agrawal 		DPAA_SEC_DP_WARN("SEC return err: 0x%x", ctx->fd_status);
59c3e85bdcSAkhil Goyal 		ctx->op->status = RTE_CRYPTO_OP_STATUS_ERROR;
60c3e85bdcSAkhil Goyal 	}
61c3e85bdcSAkhil Goyal 
62c3e85bdcSAkhil Goyal 	/* report op status to sym->op and then free the ctx memeory  */
63c3e85bdcSAkhil Goyal 	rte_mempool_put(ctx->ctx_pool, (void *)ctx);
64c3e85bdcSAkhil Goyal }
65c3e85bdcSAkhil Goyal 
66c3e85bdcSAkhil Goyal static inline struct dpaa_sec_op_ctx *
67c3e85bdcSAkhil Goyal dpaa_sec_alloc_ctx(dpaa_sec_session *ses)
68c3e85bdcSAkhil Goyal {
69c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
70c3e85bdcSAkhil Goyal 	int retval;
71c3e85bdcSAkhil Goyal 
72c3e85bdcSAkhil Goyal 	retval = rte_mempool_get(ses->ctx_pool, (void **)(&ctx));
73c3e85bdcSAkhil Goyal 	if (!ctx || retval) {
74f163231eSHemant Agrawal 		DPAA_SEC_DP_WARN("Alloc sec descriptor failed!");
75c3e85bdcSAkhil Goyal 		return NULL;
76c3e85bdcSAkhil Goyal 	}
77c3e85bdcSAkhil Goyal 	/*
78c3e85bdcSAkhil Goyal 	 * Clear SG memory. There are 16 SG entries of 16 Bytes each.
79c3e85bdcSAkhil Goyal 	 * one call to dcbz_64() clear 64 bytes, hence calling it 4 times
80c3e85bdcSAkhil Goyal 	 * to clear all the SG entries. dpaa_sec_alloc_ctx() is called for
81c3e85bdcSAkhil Goyal 	 * each packet, memset is costlier than dcbz_64().
82c3e85bdcSAkhil Goyal 	 */
83c3e85bdcSAkhil Goyal 	dcbz_64(&ctx->job.sg[SG_CACHELINE_0]);
84c3e85bdcSAkhil Goyal 	dcbz_64(&ctx->job.sg[SG_CACHELINE_1]);
85c3e85bdcSAkhil Goyal 	dcbz_64(&ctx->job.sg[SG_CACHELINE_2]);
86c3e85bdcSAkhil Goyal 	dcbz_64(&ctx->job.sg[SG_CACHELINE_3]);
87c3e85bdcSAkhil Goyal 
88c3e85bdcSAkhil Goyal 	ctx->ctx_pool = ses->ctx_pool;
890e5607e4SHemant Agrawal 	ctx->vtop_offset = (size_t) ctx
90fcf67029SHemant Agrawal 				- rte_mempool_virt2iova(ctx);
91c3e85bdcSAkhil Goyal 
92c3e85bdcSAkhil Goyal 	return ctx;
93c3e85bdcSAkhil Goyal }
94c3e85bdcSAkhil Goyal 
95c4509373SSantosh Shukla static inline rte_iova_t
96c3e85bdcSAkhil Goyal dpaa_mem_vtop(void *vaddr)
97c3e85bdcSAkhil Goyal {
9829f3c9e5SAnatoly Burakov 	const struct rte_memseg *ms;
99c3e85bdcSAkhil Goyal 
10066cc45e2SAnatoly Burakov 	ms = rte_mem_virt2memseg(vaddr, NULL);
10129f3c9e5SAnatoly Burakov 	if (ms)
10229f3c9e5SAnatoly Burakov 		return ms->iova + RTE_PTR_DIFF(vaddr, ms->addr);
1030e5607e4SHemant Agrawal 	return (size_t)NULL;
104c3e85bdcSAkhil Goyal }
105c3e85bdcSAkhil Goyal 
106c3e85bdcSAkhil Goyal static inline void *
107c4509373SSantosh Shukla dpaa_mem_ptov(rte_iova_t paddr)
108c3e85bdcSAkhil Goyal {
10911d2f002SAnatoly Burakov 	return rte_mem_iova2virt(paddr);
110c3e85bdcSAkhil Goyal }
111c3e85bdcSAkhil Goyal 
112c3e85bdcSAkhil Goyal static void
113c3e85bdcSAkhil Goyal ern_sec_fq_handler(struct qman_portal *qm __rte_unused,
114c3e85bdcSAkhil Goyal 		   struct qman_fq *fq,
115c3e85bdcSAkhil Goyal 		   const struct qm_mr_entry *msg)
116c3e85bdcSAkhil Goyal {
117f163231eSHemant Agrawal 	DPAA_SEC_DP_ERR("sec fq %d error, RC = %x, seqnum = %x\n",
118c3e85bdcSAkhil Goyal 			fq->fqid, msg->ern.rc, msg->ern.seqnum);
119c3e85bdcSAkhil Goyal }
120c3e85bdcSAkhil Goyal 
121c3e85bdcSAkhil Goyal /* initialize the queue with dest chan as caam chan so that
122c3e85bdcSAkhil Goyal  * all the packets in this queue could be dispatched into caam
123c3e85bdcSAkhil Goyal  */
124c3e85bdcSAkhil Goyal static int
125c4509373SSantosh Shukla dpaa_sec_init_rx(struct qman_fq *fq_in, rte_iova_t hwdesc,
126c3e85bdcSAkhil Goyal 		 uint32_t fqid_out)
127c3e85bdcSAkhil Goyal {
128c3e85bdcSAkhil Goyal 	struct qm_mcc_initfq fq_opts;
129c3e85bdcSAkhil Goyal 	uint32_t flags;
130c3e85bdcSAkhil Goyal 	int ret = -1;
131c3e85bdcSAkhil Goyal 
132c3e85bdcSAkhil Goyal 	/* Clear FQ options */
133c3e85bdcSAkhil Goyal 	memset(&fq_opts, 0x00, sizeof(struct qm_mcc_initfq));
134c3e85bdcSAkhil Goyal 
135c3e85bdcSAkhil Goyal 	flags = QMAN_INITFQ_FLAG_SCHED;
136c3e85bdcSAkhil Goyal 	fq_opts.we_mask = QM_INITFQ_WE_DESTWQ | QM_INITFQ_WE_CONTEXTA |
137c3e85bdcSAkhil Goyal 			  QM_INITFQ_WE_CONTEXTB;
138c3e85bdcSAkhil Goyal 
139c3e85bdcSAkhil Goyal 	qm_fqd_context_a_set64(&fq_opts.fqd, hwdesc);
140c3e85bdcSAkhil Goyal 	fq_opts.fqd.context_b = fqid_out;
141c3e85bdcSAkhil Goyal 	fq_opts.fqd.dest.channel = qm_channel_caam;
142c3e85bdcSAkhil Goyal 	fq_opts.fqd.dest.wq = 0;
143c3e85bdcSAkhil Goyal 
144c3e85bdcSAkhil Goyal 	fq_in->cb.ern  = ern_sec_fq_handler;
145c3e85bdcSAkhil Goyal 
146f163231eSHemant Agrawal 	DPAA_SEC_DEBUG("in-%x out-%x", fq_in->fqid, fqid_out);
147e79416d1SHemant Agrawal 
148c3e85bdcSAkhil Goyal 	ret = qman_init_fq(fq_in, flags, &fq_opts);
149c3e85bdcSAkhil Goyal 	if (unlikely(ret != 0))
150f163231eSHemant Agrawal 		DPAA_SEC_ERR("qman_init_fq failed %d", ret);
151c3e85bdcSAkhil Goyal 
152c3e85bdcSAkhil Goyal 	return ret;
153c3e85bdcSAkhil Goyal }
154c3e85bdcSAkhil Goyal 
155c3e85bdcSAkhil Goyal /* something is put into in_fq and caam put the crypto result into out_fq */
156c3e85bdcSAkhil Goyal static enum qman_cb_dqrr_result
157c3e85bdcSAkhil Goyal dqrr_out_fq_cb_rx(struct qman_portal *qm __always_unused,
158c3e85bdcSAkhil Goyal 		  struct qman_fq *fq __always_unused,
159c3e85bdcSAkhil Goyal 		  const struct qm_dqrr_entry *dqrr)
160c3e85bdcSAkhil Goyal {
161c3e85bdcSAkhil Goyal 	const struct qm_fd *fd;
162c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *job;
163c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
164c3e85bdcSAkhil Goyal 
165c3e85bdcSAkhil Goyal 	if (dpaa_sec_op_nb >= DPAA_SEC_BURST)
166c3e85bdcSAkhil Goyal 		return qman_cb_dqrr_defer;
167c3e85bdcSAkhil Goyal 
168c3e85bdcSAkhil Goyal 	if (!(dqrr->stat & QM_DQRR_STAT_FD_VALID))
169c3e85bdcSAkhil Goyal 		return qman_cb_dqrr_consume;
170c3e85bdcSAkhil Goyal 
171c3e85bdcSAkhil Goyal 	fd = &dqrr->fd;
172c3e85bdcSAkhil Goyal 	/* sg is embedded in an op ctx,
173c3e85bdcSAkhil Goyal 	 * sg[0] is for output
174c3e85bdcSAkhil Goyal 	 * sg[1] for input
175c3e85bdcSAkhil Goyal 	 */
176c3e85bdcSAkhil Goyal 	job = dpaa_mem_ptov(qm_fd_addr_get64(fd));
1771f14d500SAkhil Goyal 
178c3e85bdcSAkhil Goyal 	ctx = container_of(job, struct dpaa_sec_op_ctx, job);
179c3e85bdcSAkhil Goyal 	ctx->fd_status = fd->status;
1801f14d500SAkhil Goyal 	if (ctx->op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
1811f14d500SAkhil Goyal 		struct qm_sg_entry *sg_out;
1821f14d500SAkhil Goyal 		uint32_t len;
1831f14d500SAkhil Goyal 
1841f14d500SAkhil Goyal 		sg_out = &job->sg[0];
1851f14d500SAkhil Goyal 		hw_sg_to_cpu(sg_out);
1861f14d500SAkhil Goyal 		len = sg_out->length;
1871f14d500SAkhil Goyal 		ctx->op->sym->m_src->pkt_len = len;
1881f14d500SAkhil Goyal 		ctx->op->sym->m_src->data_len = len;
1891f14d500SAkhil Goyal 	}
190c3e85bdcSAkhil Goyal 	dpaa_sec_ops[dpaa_sec_op_nb++] = ctx->op;
191c3e85bdcSAkhil Goyal 	dpaa_sec_op_ending(ctx);
192c3e85bdcSAkhil Goyal 
193c3e85bdcSAkhil Goyal 	return qman_cb_dqrr_consume;
194c3e85bdcSAkhil Goyal }
195c3e85bdcSAkhil Goyal 
196c3e85bdcSAkhil Goyal /* caam result is put into this queue */
197c3e85bdcSAkhil Goyal static int
198c3e85bdcSAkhil Goyal dpaa_sec_init_tx(struct qman_fq *fq)
199c3e85bdcSAkhil Goyal {
200c3e85bdcSAkhil Goyal 	int ret;
201c3e85bdcSAkhil Goyal 	struct qm_mcc_initfq opts;
202c3e85bdcSAkhil Goyal 	uint32_t flags;
203c3e85bdcSAkhil Goyal 
204c3e85bdcSAkhil Goyal 	flags = QMAN_FQ_FLAG_NO_ENQUEUE | QMAN_FQ_FLAG_LOCKED |
205c3e85bdcSAkhil Goyal 		QMAN_FQ_FLAG_DYNAMIC_FQID;
206c3e85bdcSAkhil Goyal 
207c3e85bdcSAkhil Goyal 	ret = qman_create_fq(0, flags, fq);
208c3e85bdcSAkhil Goyal 	if (unlikely(ret)) {
209f163231eSHemant Agrawal 		DPAA_SEC_ERR("qman_create_fq failed");
210c3e85bdcSAkhil Goyal 		return ret;
211c3e85bdcSAkhil Goyal 	}
212c3e85bdcSAkhil Goyal 
213c3e85bdcSAkhil Goyal 	memset(&opts, 0, sizeof(opts));
214c3e85bdcSAkhil Goyal 	opts.we_mask = QM_INITFQ_WE_DESTWQ | QM_INITFQ_WE_FQCTRL |
215c3e85bdcSAkhil Goyal 		       QM_INITFQ_WE_CONTEXTA | QM_INITFQ_WE_CONTEXTB;
216c3e85bdcSAkhil Goyal 
217c3e85bdcSAkhil Goyal 	/* opts.fqd.dest.channel = dpaa_sec_pool_chan; */
218c3e85bdcSAkhil Goyal 
219c3e85bdcSAkhil Goyal 	fq->cb.dqrr = dqrr_out_fq_cb_rx;
220c3e85bdcSAkhil Goyal 	fq->cb.ern  = ern_sec_fq_handler;
221c3e85bdcSAkhil Goyal 
222c3e85bdcSAkhil Goyal 	ret = qman_init_fq(fq, 0, &opts);
223c3e85bdcSAkhil Goyal 	if (unlikely(ret)) {
224f163231eSHemant Agrawal 		DPAA_SEC_ERR("unable to init caam source fq!");
225c3e85bdcSAkhil Goyal 		return ret;
226c3e85bdcSAkhil Goyal 	}
227c3e85bdcSAkhil Goyal 
228c3e85bdcSAkhil Goyal 	return ret;
229c3e85bdcSAkhil Goyal }
230c3e85bdcSAkhil Goyal 
231c3e85bdcSAkhil Goyal static inline int is_cipher_only(dpaa_sec_session *ses)
232c3e85bdcSAkhil Goyal {
233c3e85bdcSAkhil Goyal 	return ((ses->cipher_alg != RTE_CRYPTO_CIPHER_NULL) &&
234c3e85bdcSAkhil Goyal 		(ses->auth_alg == RTE_CRYPTO_AUTH_NULL));
235c3e85bdcSAkhil Goyal }
236c3e85bdcSAkhil Goyal 
237c3e85bdcSAkhil Goyal static inline int is_auth_only(dpaa_sec_session *ses)
238c3e85bdcSAkhil Goyal {
239c3e85bdcSAkhil Goyal 	return ((ses->cipher_alg == RTE_CRYPTO_CIPHER_NULL) &&
240c3e85bdcSAkhil Goyal 		(ses->auth_alg != RTE_CRYPTO_AUTH_NULL));
241c3e85bdcSAkhil Goyal }
242c3e85bdcSAkhil Goyal 
243c3e85bdcSAkhil Goyal static inline int is_aead(dpaa_sec_session *ses)
244c3e85bdcSAkhil Goyal {
245c3e85bdcSAkhil Goyal 	return ((ses->cipher_alg == 0) &&
246c3e85bdcSAkhil Goyal 		(ses->auth_alg == 0) &&
247c3e85bdcSAkhil Goyal 		(ses->aead_alg != 0));
248c3e85bdcSAkhil Goyal }
249c3e85bdcSAkhil Goyal 
250c3e85bdcSAkhil Goyal static inline int is_auth_cipher(dpaa_sec_session *ses)
251c3e85bdcSAkhil Goyal {
252c3e85bdcSAkhil Goyal 	return ((ses->cipher_alg != RTE_CRYPTO_CIPHER_NULL) &&
2531f14d500SAkhil Goyal 		(ses->auth_alg != RTE_CRYPTO_AUTH_NULL) &&
2541f14d500SAkhil Goyal 		(ses->proto_alg != RTE_SECURITY_PROTOCOL_IPSEC));
2551f14d500SAkhil Goyal }
2561f14d500SAkhil Goyal 
2571f14d500SAkhil Goyal static inline int is_proto_ipsec(dpaa_sec_session *ses)
2581f14d500SAkhil Goyal {
2591f14d500SAkhil Goyal 	return (ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC);
260c3e85bdcSAkhil Goyal }
261c3e85bdcSAkhil Goyal 
262c3e85bdcSAkhil Goyal static inline int is_encode(dpaa_sec_session *ses)
263c3e85bdcSAkhil Goyal {
264c3e85bdcSAkhil Goyal 	return ses->dir == DIR_ENC;
265c3e85bdcSAkhil Goyal }
266c3e85bdcSAkhil Goyal 
267c3e85bdcSAkhil Goyal static inline int is_decode(dpaa_sec_session *ses)
268c3e85bdcSAkhil Goyal {
269c3e85bdcSAkhil Goyal 	return ses->dir == DIR_DEC;
270c3e85bdcSAkhil Goyal }
271c3e85bdcSAkhil Goyal 
272c3e85bdcSAkhil Goyal static inline void
273c3e85bdcSAkhil Goyal caam_auth_alg(dpaa_sec_session *ses, struct alginfo *alginfo_a)
274c3e85bdcSAkhil Goyal {
275c3e85bdcSAkhil Goyal 	switch (ses->auth_alg) {
276c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AUTH_NULL:
277c3e85bdcSAkhil Goyal 		ses->digest_length = 0;
278c3e85bdcSAkhil Goyal 		break;
279c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AUTH_MD5_HMAC:
2801f14d500SAkhil Goyal 		alginfo_a->algtype =
2811f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
2821f14d500SAkhil Goyal 			OP_PCL_IPSEC_HMAC_MD5_96 : OP_ALG_ALGSEL_MD5;
283c3e85bdcSAkhil Goyal 		alginfo_a->algmode = OP_ALG_AAI_HMAC;
284c3e85bdcSAkhil Goyal 		break;
285c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA1_HMAC:
2861f14d500SAkhil Goyal 		alginfo_a->algtype =
2871f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
2881f14d500SAkhil Goyal 			OP_PCL_IPSEC_HMAC_SHA1_96 : OP_ALG_ALGSEL_SHA1;
289c3e85bdcSAkhil Goyal 		alginfo_a->algmode = OP_ALG_AAI_HMAC;
290c3e85bdcSAkhil Goyal 		break;
291c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA224_HMAC:
2921f14d500SAkhil Goyal 		alginfo_a->algtype =
2931f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
2941f14d500SAkhil Goyal 			OP_PCL_IPSEC_HMAC_SHA1_160 : OP_ALG_ALGSEL_SHA224;
295c3e85bdcSAkhil Goyal 		alginfo_a->algmode = OP_ALG_AAI_HMAC;
296c3e85bdcSAkhil Goyal 		break;
297c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA256_HMAC:
2981f14d500SAkhil Goyal 		alginfo_a->algtype =
2991f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3001f14d500SAkhil Goyal 			OP_PCL_IPSEC_HMAC_SHA2_256_128 : OP_ALG_ALGSEL_SHA256;
301c3e85bdcSAkhil Goyal 		alginfo_a->algmode = OP_ALG_AAI_HMAC;
302c3e85bdcSAkhil Goyal 		break;
303c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA384_HMAC:
3041f14d500SAkhil Goyal 		alginfo_a->algtype =
3051f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3061f14d500SAkhil Goyal 			OP_PCL_IPSEC_HMAC_SHA2_384_192 : OP_ALG_ALGSEL_SHA384;
307c3e85bdcSAkhil Goyal 		alginfo_a->algmode = OP_ALG_AAI_HMAC;
308c3e85bdcSAkhil Goyal 		break;
309c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA512_HMAC:
3101f14d500SAkhil Goyal 		alginfo_a->algtype =
3111f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3121f14d500SAkhil Goyal 			OP_PCL_IPSEC_HMAC_SHA2_512_256 : OP_ALG_ALGSEL_SHA512;
313c3e85bdcSAkhil Goyal 		alginfo_a->algmode = OP_ALG_AAI_HMAC;
314c3e85bdcSAkhil Goyal 		break;
315c3e85bdcSAkhil Goyal 	default:
316f163231eSHemant Agrawal 		DPAA_SEC_ERR("unsupported auth alg %u", ses->auth_alg);
317c3e85bdcSAkhil Goyal 	}
318c3e85bdcSAkhil Goyal }
319c3e85bdcSAkhil Goyal 
320c3e85bdcSAkhil Goyal static inline void
321c3e85bdcSAkhil Goyal caam_cipher_alg(dpaa_sec_session *ses, struct alginfo *alginfo_c)
322c3e85bdcSAkhil Goyal {
323c3e85bdcSAkhil Goyal 	switch (ses->cipher_alg) {
324c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_CIPHER_NULL:
325c3e85bdcSAkhil Goyal 		break;
326c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_CIPHER_AES_CBC:
3271f14d500SAkhil Goyal 		alginfo_c->algtype =
3281f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3291f14d500SAkhil Goyal 			OP_PCL_IPSEC_AES_CBC : OP_ALG_ALGSEL_AES;
330c3e85bdcSAkhil Goyal 		alginfo_c->algmode = OP_ALG_AAI_CBC;
331c3e85bdcSAkhil Goyal 		break;
332c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_CIPHER_3DES_CBC:
3331f14d500SAkhil Goyal 		alginfo_c->algtype =
3341f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3351f14d500SAkhil Goyal 			OP_PCL_IPSEC_3DES : OP_ALG_ALGSEL_3DES;
336c3e85bdcSAkhil Goyal 		alginfo_c->algmode = OP_ALG_AAI_CBC;
337c3e85bdcSAkhil Goyal 		break;
338c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_CIPHER_AES_CTR:
3391f14d500SAkhil Goyal 		alginfo_c->algtype =
3401f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3411f14d500SAkhil Goyal 			OP_PCL_IPSEC_AES_CTR : OP_ALG_ALGSEL_AES;
342c3e85bdcSAkhil Goyal 		alginfo_c->algmode = OP_ALG_AAI_CTR;
343c3e85bdcSAkhil Goyal 		break;
344c3e85bdcSAkhil Goyal 	default:
345f163231eSHemant Agrawal 		DPAA_SEC_ERR("unsupported cipher alg %d", ses->cipher_alg);
346c3e85bdcSAkhil Goyal 	}
347c3e85bdcSAkhil Goyal }
348c3e85bdcSAkhil Goyal 
349c3e85bdcSAkhil Goyal static inline void
350c3e85bdcSAkhil Goyal caam_aead_alg(dpaa_sec_session *ses, struct alginfo *alginfo)
351c3e85bdcSAkhil Goyal {
352c3e85bdcSAkhil Goyal 	switch (ses->aead_alg) {
353c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AEAD_AES_GCM:
354c3e85bdcSAkhil Goyal 		alginfo->algtype = OP_ALG_ALGSEL_AES;
355c3e85bdcSAkhil Goyal 		alginfo->algmode = OP_ALG_AAI_GCM;
356c3e85bdcSAkhil Goyal 		break;
357c3e85bdcSAkhil Goyal 	default:
358f163231eSHemant Agrawal 		DPAA_SEC_ERR("unsupported AEAD alg %d", ses->aead_alg);
359c3e85bdcSAkhil Goyal 	}
360c3e85bdcSAkhil Goyal }
361c3e85bdcSAkhil Goyal 
362c3e85bdcSAkhil Goyal 
363c3e85bdcSAkhil Goyal /* prepare command block of the session */
364c3e85bdcSAkhil Goyal static int
365c3e85bdcSAkhil Goyal dpaa_sec_prep_cdb(dpaa_sec_session *ses)
366c3e85bdcSAkhil Goyal {
367c3e85bdcSAkhil Goyal 	struct alginfo alginfo_c = {0}, alginfo_a = {0}, alginfo = {0};
36822788c2cSSunil Kumar Kori 	int32_t shared_desc_len = 0;
369e79416d1SHemant Agrawal 	struct sec_cdb *cdb = &ses->cdb;
370c3e85bdcSAkhil Goyal 	int err;
371c3e85bdcSAkhil Goyal #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
372c3e85bdcSAkhil Goyal 	int swap = false;
373c3e85bdcSAkhil Goyal #else
374c3e85bdcSAkhil Goyal 	int swap = true;
375c3e85bdcSAkhil Goyal #endif
376c3e85bdcSAkhil Goyal 
377c3e85bdcSAkhil Goyal 	memset(cdb, 0, sizeof(struct sec_cdb));
378c3e85bdcSAkhil Goyal 
379c3e85bdcSAkhil Goyal 	if (is_cipher_only(ses)) {
380c3e85bdcSAkhil Goyal 		caam_cipher_alg(ses, &alginfo_c);
381c3e85bdcSAkhil Goyal 		if (alginfo_c.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
382f163231eSHemant Agrawal 			DPAA_SEC_ERR("not supported cipher alg");
383c3e85bdcSAkhil Goyal 			return -ENOTSUP;
384c3e85bdcSAkhil Goyal 		}
385c3e85bdcSAkhil Goyal 
3860e5607e4SHemant Agrawal 		alginfo_c.key = (size_t)ses->cipher_key.data;
387c3e85bdcSAkhil Goyal 		alginfo_c.keylen = ses->cipher_key.length;
388c3e85bdcSAkhil Goyal 		alginfo_c.key_enc_flags = 0;
389c3e85bdcSAkhil Goyal 		alginfo_c.key_type = RTA_DATA_IMM;
390c3e85bdcSAkhil Goyal 
391c3e85bdcSAkhil Goyal 		shared_desc_len = cnstr_shdsc_blkcipher(
392c3e85bdcSAkhil Goyal 						cdb->sh_desc, true,
393c3e85bdcSAkhil Goyal 						swap, &alginfo_c,
394c3e85bdcSAkhil Goyal 						NULL,
395c3e85bdcSAkhil Goyal 						ses->iv.length,
396c3e85bdcSAkhil Goyal 						ses->dir);
397c3e85bdcSAkhil Goyal 	} else if (is_auth_only(ses)) {
398c3e85bdcSAkhil Goyal 		caam_auth_alg(ses, &alginfo_a);
399c3e85bdcSAkhil Goyal 		if (alginfo_a.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
400f163231eSHemant Agrawal 			DPAA_SEC_ERR("not supported auth alg");
401c3e85bdcSAkhil Goyal 			return -ENOTSUP;
402c3e85bdcSAkhil Goyal 		}
403c3e85bdcSAkhil Goyal 
4040e5607e4SHemant Agrawal 		alginfo_a.key = (size_t)ses->auth_key.data;
405c3e85bdcSAkhil Goyal 		alginfo_a.keylen = ses->auth_key.length;
406c3e85bdcSAkhil Goyal 		alginfo_a.key_enc_flags = 0;
407c3e85bdcSAkhil Goyal 		alginfo_a.key_type = RTA_DATA_IMM;
408c3e85bdcSAkhil Goyal 
409c3e85bdcSAkhil Goyal 		shared_desc_len = cnstr_shdsc_hmac(cdb->sh_desc, true,
410c3e85bdcSAkhil Goyal 						   swap, &alginfo_a,
411c3e85bdcSAkhil Goyal 						   !ses->dir,
412c3e85bdcSAkhil Goyal 						   ses->digest_length);
413c3e85bdcSAkhil Goyal 	} else if (is_aead(ses)) {
414c3e85bdcSAkhil Goyal 		caam_aead_alg(ses, &alginfo);
415c3e85bdcSAkhil Goyal 		if (alginfo.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
416f163231eSHemant Agrawal 			DPAA_SEC_ERR("not supported aead alg");
417c3e85bdcSAkhil Goyal 			return -ENOTSUP;
418c3e85bdcSAkhil Goyal 		}
4190e5607e4SHemant Agrawal 		alginfo.key = (size_t)ses->aead_key.data;
420c3e85bdcSAkhil Goyal 		alginfo.keylen = ses->aead_key.length;
421c3e85bdcSAkhil Goyal 		alginfo.key_enc_flags = 0;
422c3e85bdcSAkhil Goyal 		alginfo.key_type = RTA_DATA_IMM;
423c3e85bdcSAkhil Goyal 
424c3e85bdcSAkhil Goyal 		if (ses->dir == DIR_ENC)
425c3e85bdcSAkhil Goyal 			shared_desc_len = cnstr_shdsc_gcm_encap(
426c3e85bdcSAkhil Goyal 					cdb->sh_desc, true, swap,
427c3e85bdcSAkhil Goyal 					&alginfo,
428c3e85bdcSAkhil Goyal 					ses->iv.length,
429c3e85bdcSAkhil Goyal 					ses->digest_length);
430c3e85bdcSAkhil Goyal 		else
431c3e85bdcSAkhil Goyal 			shared_desc_len = cnstr_shdsc_gcm_decap(
432c3e85bdcSAkhil Goyal 					cdb->sh_desc, true, swap,
433c3e85bdcSAkhil Goyal 					&alginfo,
434c3e85bdcSAkhil Goyal 					ses->iv.length,
435c3e85bdcSAkhil Goyal 					ses->digest_length);
436c3e85bdcSAkhil Goyal 	} else {
437c3e85bdcSAkhil Goyal 		caam_cipher_alg(ses, &alginfo_c);
438c3e85bdcSAkhil Goyal 		if (alginfo_c.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
439f163231eSHemant Agrawal 			DPAA_SEC_ERR("not supported cipher alg");
440c3e85bdcSAkhil Goyal 			return -ENOTSUP;
441c3e85bdcSAkhil Goyal 		}
442c3e85bdcSAkhil Goyal 
4430e5607e4SHemant Agrawal 		alginfo_c.key = (size_t)ses->cipher_key.data;
444c3e85bdcSAkhil Goyal 		alginfo_c.keylen = ses->cipher_key.length;
445c3e85bdcSAkhil Goyal 		alginfo_c.key_enc_flags = 0;
446c3e85bdcSAkhil Goyal 		alginfo_c.key_type = RTA_DATA_IMM;
447c3e85bdcSAkhil Goyal 
448c3e85bdcSAkhil Goyal 		caam_auth_alg(ses, &alginfo_a);
449c3e85bdcSAkhil Goyal 		if (alginfo_a.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
450f163231eSHemant Agrawal 			DPAA_SEC_ERR("not supported auth alg");
451c3e85bdcSAkhil Goyal 			return -ENOTSUP;
452c3e85bdcSAkhil Goyal 		}
453c3e85bdcSAkhil Goyal 
4540e5607e4SHemant Agrawal 		alginfo_a.key = (size_t)ses->auth_key.data;
455c3e85bdcSAkhil Goyal 		alginfo_a.keylen = ses->auth_key.length;
456c3e85bdcSAkhil Goyal 		alginfo_a.key_enc_flags = 0;
457c3e85bdcSAkhil Goyal 		alginfo_a.key_type = RTA_DATA_IMM;
458c3e85bdcSAkhil Goyal 
459c3e85bdcSAkhil Goyal 		cdb->sh_desc[0] = alginfo_c.keylen;
460c3e85bdcSAkhil Goyal 		cdb->sh_desc[1] = alginfo_a.keylen;
461c3e85bdcSAkhil Goyal 		err = rta_inline_query(IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN,
462c3e85bdcSAkhil Goyal 				       MIN_JOB_DESC_SIZE,
463c3e85bdcSAkhil Goyal 				       (unsigned int *)cdb->sh_desc,
464c3e85bdcSAkhil Goyal 				       &cdb->sh_desc[2], 2);
465c3e85bdcSAkhil Goyal 
466c3e85bdcSAkhil Goyal 		if (err < 0) {
467f163231eSHemant Agrawal 			DPAA_SEC_ERR("Crypto: Incorrect key lengths");
468c3e85bdcSAkhil Goyal 			return err;
469c3e85bdcSAkhil Goyal 		}
470c3e85bdcSAkhil Goyal 		if (cdb->sh_desc[2] & 1)
471c3e85bdcSAkhil Goyal 			alginfo_c.key_type = RTA_DATA_IMM;
472c3e85bdcSAkhil Goyal 		else {
4730e5607e4SHemant Agrawal 			alginfo_c.key = (size_t)dpaa_mem_vtop(
4740e5607e4SHemant Agrawal 						(void *)(size_t)alginfo_c.key);
475c3e85bdcSAkhil Goyal 			alginfo_c.key_type = RTA_DATA_PTR;
476c3e85bdcSAkhil Goyal 		}
477c3e85bdcSAkhil Goyal 		if (cdb->sh_desc[2] & (1<<1))
478c3e85bdcSAkhil Goyal 			alginfo_a.key_type = RTA_DATA_IMM;
479c3e85bdcSAkhil Goyal 		else {
4800e5607e4SHemant Agrawal 			alginfo_a.key = (size_t)dpaa_mem_vtop(
4810e5607e4SHemant Agrawal 						(void *)(size_t)alginfo_a.key);
482c3e85bdcSAkhil Goyal 			alginfo_a.key_type = RTA_DATA_PTR;
483c3e85bdcSAkhil Goyal 		}
484c3e85bdcSAkhil Goyal 		cdb->sh_desc[0] = 0;
485c3e85bdcSAkhil Goyal 		cdb->sh_desc[1] = 0;
486c3e85bdcSAkhil Goyal 		cdb->sh_desc[2] = 0;
4871f14d500SAkhil Goyal 		if (is_proto_ipsec(ses)) {
4881f14d500SAkhil Goyal 			if (ses->dir == DIR_ENC) {
4891f14d500SAkhil Goyal 				shared_desc_len = cnstr_shdsc_ipsec_new_encap(
4901f14d500SAkhil Goyal 						cdb->sh_desc,
4911f14d500SAkhil Goyal 						true, swap, &ses->encap_pdb,
4921f14d500SAkhil Goyal 						(uint8_t *)&ses->ip4_hdr,
4931f14d500SAkhil Goyal 						&alginfo_c, &alginfo_a);
4941f14d500SAkhil Goyal 			} else if (ses->dir == DIR_DEC) {
4951f14d500SAkhil Goyal 				shared_desc_len = cnstr_shdsc_ipsec_new_decap(
4961f14d500SAkhil Goyal 						cdb->sh_desc,
4971f14d500SAkhil Goyal 						true, swap, &ses->decap_pdb,
4981f14d500SAkhil Goyal 						&alginfo_c, &alginfo_a);
4991f14d500SAkhil Goyal 			}
5001f14d500SAkhil Goyal 		} else {
5011f14d500SAkhil Goyal 			/* Auth_only_len is set as 0 here and it will be
5021f14d500SAkhil Goyal 			 * overwritten in fd for each packet.
503c3e85bdcSAkhil Goyal 			 */
504c3e85bdcSAkhil Goyal 			shared_desc_len = cnstr_shdsc_authenc(cdb->sh_desc,
505c3e85bdcSAkhil Goyal 					true, swap, &alginfo_c, &alginfo_a,
506c3e85bdcSAkhil Goyal 					ses->iv.length, 0,
507c3e85bdcSAkhil Goyal 					ses->digest_length, ses->dir);
508c3e85bdcSAkhil Goyal 		}
5091f14d500SAkhil Goyal 	}
51022788c2cSSunil Kumar Kori 
51122788c2cSSunil Kumar Kori 	if (shared_desc_len < 0) {
512f163231eSHemant Agrawal 		DPAA_SEC_ERR("error in preparing command block");
51322788c2cSSunil Kumar Kori 		return shared_desc_len;
51422788c2cSSunil Kumar Kori 	}
51522788c2cSSunil Kumar Kori 
516c3e85bdcSAkhil Goyal 	cdb->sh_hdr.hi.field.idlen = shared_desc_len;
517c3e85bdcSAkhil Goyal 	cdb->sh_hdr.hi.word = rte_cpu_to_be_32(cdb->sh_hdr.hi.word);
518c3e85bdcSAkhil Goyal 	cdb->sh_hdr.lo.word = rte_cpu_to_be_32(cdb->sh_hdr.lo.word);
519c3e85bdcSAkhil Goyal 
520c3e85bdcSAkhil Goyal 	return 0;
521c3e85bdcSAkhil Goyal }
522c3e85bdcSAkhil Goyal 
523c3e85bdcSAkhil Goyal /* qp is lockless, should be accessed by only one thread */
524c3e85bdcSAkhil Goyal static int
525c3e85bdcSAkhil Goyal dpaa_sec_deq(struct dpaa_sec_qp *qp, struct rte_crypto_op **ops, int nb_ops)
526c3e85bdcSAkhil Goyal {
527c3e85bdcSAkhil Goyal 	struct qman_fq *fq;
5289a984458SAkhil Goyal 	unsigned int pkts = 0;
5299a984458SAkhil Goyal 	int ret;
5309a984458SAkhil Goyal 	struct qm_dqrr_entry *dq;
531c3e85bdcSAkhil Goyal 
532c3e85bdcSAkhil Goyal 	fq = &qp->outq;
5339a984458SAkhil Goyal 	ret = qman_set_vdq(fq, (nb_ops > DPAA_MAX_DEQUEUE_NUM_FRAMES) ?
5349a984458SAkhil Goyal 				DPAA_MAX_DEQUEUE_NUM_FRAMES : nb_ops);
5359a984458SAkhil Goyal 	if (ret)
5369a984458SAkhil Goyal 		return 0;
537c3e85bdcSAkhil Goyal 
5389a984458SAkhil Goyal 	do {
5399a984458SAkhil Goyal 		const struct qm_fd *fd;
5409a984458SAkhil Goyal 		struct dpaa_sec_job *job;
5419a984458SAkhil Goyal 		struct dpaa_sec_op_ctx *ctx;
5429a984458SAkhil Goyal 		struct rte_crypto_op *op;
543c3e85bdcSAkhil Goyal 
5449a984458SAkhil Goyal 		dq = qman_dequeue(fq);
5459a984458SAkhil Goyal 		if (!dq)
5469a984458SAkhil Goyal 			continue;
5479a984458SAkhil Goyal 
5489a984458SAkhil Goyal 		fd = &dq->fd;
5499a984458SAkhil Goyal 		/* sg is embedded in an op ctx,
5509a984458SAkhil Goyal 		 * sg[0] is for output
5519a984458SAkhil Goyal 		 * sg[1] for input
5529a984458SAkhil Goyal 		 */
5539a984458SAkhil Goyal 		job = dpaa_mem_ptov(qm_fd_addr_get64(fd));
5549a984458SAkhil Goyal 
5559a984458SAkhil Goyal 		ctx = container_of(job, struct dpaa_sec_op_ctx, job);
5569a984458SAkhil Goyal 		ctx->fd_status = fd->status;
5579a984458SAkhil Goyal 		op = ctx->op;
5589a984458SAkhil Goyal 		if (op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
5599a984458SAkhil Goyal 			struct qm_sg_entry *sg_out;
5609a984458SAkhil Goyal 			uint32_t len;
5619a984458SAkhil Goyal 
5629a984458SAkhil Goyal 			sg_out = &job->sg[0];
5639a984458SAkhil Goyal 			hw_sg_to_cpu(sg_out);
5649a984458SAkhil Goyal 			len = sg_out->length;
5659a984458SAkhil Goyal 			op->sym->m_src->pkt_len = len;
5669a984458SAkhil Goyal 			op->sym->m_src->data_len = len;
5679a984458SAkhil Goyal 		}
5689a984458SAkhil Goyal 		if (!ctx->fd_status) {
5699a984458SAkhil Goyal 			op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
5709a984458SAkhil Goyal 		} else {
571f163231eSHemant Agrawal 			DPAA_SEC_DP_WARN("SEC return err:0x%x", ctx->fd_status);
5729a984458SAkhil Goyal 			op->status = RTE_CRYPTO_OP_STATUS_ERROR;
5739a984458SAkhil Goyal 		}
5749a984458SAkhil Goyal 		ops[pkts++] = op;
5759a984458SAkhil Goyal 
5769a984458SAkhil Goyal 		/* report op status to sym->op and then free the ctx memeory */
5779a984458SAkhil Goyal 		rte_mempool_put(ctx->ctx_pool, (void *)ctx);
5789a984458SAkhil Goyal 
5799a984458SAkhil Goyal 		qman_dqrr_consume(fq, dq);
5809a984458SAkhil Goyal 	} while (fq->flags & QMAN_FQ_STATE_VDQCR);
5819a984458SAkhil Goyal 
5829a984458SAkhil Goyal 	return pkts;
583c3e85bdcSAkhil Goyal }
584c3e85bdcSAkhil Goyal 
585a74af788SAkhil Goyal static inline struct dpaa_sec_job *
586a74af788SAkhil Goyal build_auth_only_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
587a74af788SAkhil Goyal {
588a74af788SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
589a74af788SAkhil Goyal 	struct rte_mbuf *mbuf = sym->m_src;
590a74af788SAkhil Goyal 	struct dpaa_sec_job *cf;
591a74af788SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
592a74af788SAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
593a74af788SAkhil Goyal 	phys_addr_t start_addr;
594a74af788SAkhil Goyal 	uint8_t *old_digest, extra_segs;
595a74af788SAkhil Goyal 
596a74af788SAkhil Goyal 	if (is_decode(ses))
597a74af788SAkhil Goyal 		extra_segs = 3;
598a74af788SAkhil Goyal 	else
599a74af788SAkhil Goyal 		extra_segs = 2;
600a74af788SAkhil Goyal 
601a74af788SAkhil Goyal 	if ((mbuf->nb_segs + extra_segs) > MAX_SG_ENTRIES) {
602f163231eSHemant Agrawal 		DPAA_SEC_DP_ERR("Auth: Max sec segs supported is %d",
603a74af788SAkhil Goyal 				MAX_SG_ENTRIES);
604a74af788SAkhil Goyal 		return NULL;
605a74af788SAkhil Goyal 	}
606a74af788SAkhil Goyal 	ctx = dpaa_sec_alloc_ctx(ses);
607a74af788SAkhil Goyal 	if (!ctx)
608a74af788SAkhil Goyal 		return NULL;
609a74af788SAkhil Goyal 
610a74af788SAkhil Goyal 	cf = &ctx->job;
611a74af788SAkhil Goyal 	ctx->op = op;
612a74af788SAkhil Goyal 	old_digest = ctx->digest;
613a74af788SAkhil Goyal 
614a74af788SAkhil Goyal 	/* output */
615a74af788SAkhil Goyal 	out_sg = &cf->sg[0];
616a74af788SAkhil Goyal 	qm_sg_entry_set64(out_sg, sym->auth.digest.phys_addr);
617a74af788SAkhil Goyal 	out_sg->length = ses->digest_length;
618a74af788SAkhil Goyal 	cpu_to_hw_sg(out_sg);
619a74af788SAkhil Goyal 
620a74af788SAkhil Goyal 	/* input */
621a74af788SAkhil Goyal 	in_sg = &cf->sg[1];
622a74af788SAkhil Goyal 	/* need to extend the input to a compound frame */
623a74af788SAkhil Goyal 	in_sg->extension = 1;
624a74af788SAkhil Goyal 	in_sg->final = 1;
625a74af788SAkhil Goyal 	in_sg->length = sym->auth.data.length;
62695456e89SShreyansh Jain 	qm_sg_entry_set64(in_sg, dpaa_mem_vtop(&cf->sg[2]));
627a74af788SAkhil Goyal 
628a74af788SAkhil Goyal 	/* 1st seg */
629a74af788SAkhil Goyal 	sg = in_sg + 1;
630a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
631a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->auth.data.offset;
632a74af788SAkhil Goyal 	sg->offset = sym->auth.data.offset;
633a74af788SAkhil Goyal 
634a74af788SAkhil Goyal 	/* Successive segs */
635a74af788SAkhil Goyal 	mbuf = mbuf->next;
636a74af788SAkhil Goyal 	while (mbuf) {
637a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
638a74af788SAkhil Goyal 		sg++;
639a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
640a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
641a74af788SAkhil Goyal 		mbuf = mbuf->next;
642a74af788SAkhil Goyal 	}
643a74af788SAkhil Goyal 
644a74af788SAkhil Goyal 	if (is_decode(ses)) {
645a74af788SAkhil Goyal 		/* Digest verification case */
646a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
647a74af788SAkhil Goyal 		sg++;
648a74af788SAkhil Goyal 		rte_memcpy(old_digest, sym->auth.digest.data,
649a74af788SAkhil Goyal 				ses->digest_length);
65095456e89SShreyansh Jain 		start_addr = dpaa_mem_vtop(old_digest);
651a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, start_addr);
652a74af788SAkhil Goyal 		sg->length = ses->digest_length;
653a74af788SAkhil Goyal 		in_sg->length += ses->digest_length;
654a74af788SAkhil Goyal 	} else {
655a74af788SAkhil Goyal 		/* Digest calculation case */
656a74af788SAkhil Goyal 		sg->length -= ses->digest_length;
657a74af788SAkhil Goyal 	}
658a74af788SAkhil Goyal 	sg->final = 1;
659a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
660a74af788SAkhil Goyal 	cpu_to_hw_sg(in_sg);
661a74af788SAkhil Goyal 
662a74af788SAkhil Goyal 	return cf;
663a74af788SAkhil Goyal }
664a74af788SAkhil Goyal 
665c3e85bdcSAkhil Goyal /**
666c3e85bdcSAkhil Goyal  * packet looks like:
667c3e85bdcSAkhil Goyal  *		|<----data_len------->|
668c3e85bdcSAkhil Goyal  *    |ip_header|ah_header|icv|payload|
669c3e85bdcSAkhil Goyal  *              ^
670c3e85bdcSAkhil Goyal  *		|
671c3e85bdcSAkhil Goyal  *	   mbuf->pkt.data
672c3e85bdcSAkhil Goyal  */
673c3e85bdcSAkhil Goyal static inline struct dpaa_sec_job *
674c3e85bdcSAkhil Goyal build_auth_only(struct rte_crypto_op *op, dpaa_sec_session *ses)
675c3e85bdcSAkhil Goyal {
676c3e85bdcSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
677c3e85bdcSAkhil Goyal 	struct rte_mbuf *mbuf = sym->m_src;
678c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
679c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
680c3e85bdcSAkhil Goyal 	struct qm_sg_entry *sg;
681c4509373SSantosh Shukla 	rte_iova_t start_addr;
682c3e85bdcSAkhil Goyal 	uint8_t *old_digest;
683c3e85bdcSAkhil Goyal 
684c3e85bdcSAkhil Goyal 	ctx = dpaa_sec_alloc_ctx(ses);
685c3e85bdcSAkhil Goyal 	if (!ctx)
686c3e85bdcSAkhil Goyal 		return NULL;
687c3e85bdcSAkhil Goyal 
688c3e85bdcSAkhil Goyal 	cf = &ctx->job;
689c3e85bdcSAkhil Goyal 	ctx->op = op;
690c3e85bdcSAkhil Goyal 	old_digest = ctx->digest;
691c3e85bdcSAkhil Goyal 
692bfa9a8a4SThomas Monjalon 	start_addr = rte_pktmbuf_iova(mbuf);
693c3e85bdcSAkhil Goyal 	/* output */
694c3e85bdcSAkhil Goyal 	sg = &cf->sg[0];
695c3e85bdcSAkhil Goyal 	qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
696c3e85bdcSAkhil Goyal 	sg->length = ses->digest_length;
697c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
698c3e85bdcSAkhil Goyal 
699c3e85bdcSAkhil Goyal 	/* input */
700c3e85bdcSAkhil Goyal 	sg = &cf->sg[1];
701c3e85bdcSAkhil Goyal 	if (is_decode(ses)) {
702c3e85bdcSAkhil Goyal 		/* need to extend the input to a compound frame */
703c3e85bdcSAkhil Goyal 		sg->extension = 1;
70495456e89SShreyansh Jain 		qm_sg_entry_set64(sg, dpaa_mem_vtop(&cf->sg[2]));
705c3e85bdcSAkhil Goyal 		sg->length = sym->auth.data.length + ses->digest_length;
706c3e85bdcSAkhil Goyal 		sg->final = 1;
707c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
708c3e85bdcSAkhil Goyal 
709c3e85bdcSAkhil Goyal 		sg = &cf->sg[2];
710c3e85bdcSAkhil Goyal 		/* hash result or digest, save digest first */
711c3e85bdcSAkhil Goyal 		rte_memcpy(old_digest, sym->auth.digest.data,
712c3e85bdcSAkhil Goyal 			   ses->digest_length);
713c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, start_addr + sym->auth.data.offset);
714c3e85bdcSAkhil Goyal 		sg->length = sym->auth.data.length;
715c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
716c3e85bdcSAkhil Goyal 
717c3e85bdcSAkhil Goyal 		/* let's check digest by hw */
71895456e89SShreyansh Jain 		start_addr = dpaa_mem_vtop(old_digest);
719c3e85bdcSAkhil Goyal 		sg++;
720c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, start_addr);
721c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
722c3e85bdcSAkhil Goyal 		sg->final = 1;
723c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
724c3e85bdcSAkhil Goyal 	} else {
725c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, start_addr + sym->auth.data.offset);
726c3e85bdcSAkhil Goyal 		sg->length = sym->auth.data.length;
727c3e85bdcSAkhil Goyal 		sg->final = 1;
728c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
729c3e85bdcSAkhil Goyal 	}
730c3e85bdcSAkhil Goyal 
731c3e85bdcSAkhil Goyal 	return cf;
732c3e85bdcSAkhil Goyal }
733c3e85bdcSAkhil Goyal 
734c3e85bdcSAkhil Goyal static inline struct dpaa_sec_job *
735a74af788SAkhil Goyal build_cipher_only_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
736a74af788SAkhil Goyal {
737a74af788SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
738a74af788SAkhil Goyal 	struct dpaa_sec_job *cf;
739a74af788SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
740a74af788SAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
741a74af788SAkhil Goyal 	struct rte_mbuf *mbuf;
742a74af788SAkhil Goyal 	uint8_t req_segs;
743a74af788SAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
744a74af788SAkhil Goyal 			ses->iv.offset);
745a74af788SAkhil Goyal 
746a74af788SAkhil Goyal 	if (sym->m_dst) {
747a74af788SAkhil Goyal 		mbuf = sym->m_dst;
748a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 3;
749a74af788SAkhil Goyal 	} else {
750a74af788SAkhil Goyal 		mbuf = sym->m_src;
751a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs * 2 + 3;
752a74af788SAkhil Goyal 	}
753a74af788SAkhil Goyal 
754a74af788SAkhil Goyal 	if (req_segs > MAX_SG_ENTRIES) {
755f163231eSHemant Agrawal 		DPAA_SEC_DP_ERR("Cipher: Max sec segs supported is %d",
756a74af788SAkhil Goyal 				MAX_SG_ENTRIES);
757a74af788SAkhil Goyal 		return NULL;
758a74af788SAkhil Goyal 	}
759a74af788SAkhil Goyal 
760a74af788SAkhil Goyal 	ctx = dpaa_sec_alloc_ctx(ses);
761a74af788SAkhil Goyal 	if (!ctx)
762a74af788SAkhil Goyal 		return NULL;
763a74af788SAkhil Goyal 
764a74af788SAkhil Goyal 	cf = &ctx->job;
765a74af788SAkhil Goyal 	ctx->op = op;
766a74af788SAkhil Goyal 
767a74af788SAkhil Goyal 	/* output */
768a74af788SAkhil Goyal 	out_sg = &cf->sg[0];
769a74af788SAkhil Goyal 	out_sg->extension = 1;
770a74af788SAkhil Goyal 	out_sg->length = sym->cipher.data.length;
77195456e89SShreyansh Jain 	qm_sg_entry_set64(out_sg, dpaa_mem_vtop(&cf->sg[2]));
772a74af788SAkhil Goyal 	cpu_to_hw_sg(out_sg);
773a74af788SAkhil Goyal 
774a74af788SAkhil Goyal 	/* 1st seg */
775a74af788SAkhil Goyal 	sg = &cf->sg[2];
776a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
777a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->cipher.data.offset;
778a74af788SAkhil Goyal 	sg->offset = sym->cipher.data.offset;
779a74af788SAkhil Goyal 
780a74af788SAkhil Goyal 	/* Successive segs */
781a74af788SAkhil Goyal 	mbuf = mbuf->next;
782a74af788SAkhil Goyal 	while (mbuf) {
783a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
784a74af788SAkhil Goyal 		sg++;
785a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
786a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
787a74af788SAkhil Goyal 		mbuf = mbuf->next;
788a74af788SAkhil Goyal 	}
789a74af788SAkhil Goyal 	sg->final = 1;
790a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
791a74af788SAkhil Goyal 
792a74af788SAkhil Goyal 	/* input */
793a74af788SAkhil Goyal 	mbuf = sym->m_src;
794a74af788SAkhil Goyal 	in_sg = &cf->sg[1];
795a74af788SAkhil Goyal 	in_sg->extension = 1;
796a74af788SAkhil Goyal 	in_sg->final = 1;
797a74af788SAkhil Goyal 	in_sg->length = sym->cipher.data.length + ses->iv.length;
798a74af788SAkhil Goyal 
799a74af788SAkhil Goyal 	sg++;
80095456e89SShreyansh Jain 	qm_sg_entry_set64(in_sg, dpaa_mem_vtop(sg));
801a74af788SAkhil Goyal 	cpu_to_hw_sg(in_sg);
802a74af788SAkhil Goyal 
803a74af788SAkhil Goyal 	/* IV */
804a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
805a74af788SAkhil Goyal 	sg->length = ses->iv.length;
806a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
807a74af788SAkhil Goyal 
808a74af788SAkhil Goyal 	/* 1st seg */
809a74af788SAkhil Goyal 	sg++;
810a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
811a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->cipher.data.offset;
812a74af788SAkhil Goyal 	sg->offset = sym->cipher.data.offset;
813a74af788SAkhil Goyal 
814a74af788SAkhil Goyal 	/* Successive segs */
815a74af788SAkhil Goyal 	mbuf = mbuf->next;
816a74af788SAkhil Goyal 	while (mbuf) {
817a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
818a74af788SAkhil Goyal 		sg++;
819a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
820a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
821a74af788SAkhil Goyal 		mbuf = mbuf->next;
822a74af788SAkhil Goyal 	}
823a74af788SAkhil Goyal 	sg->final = 1;
824a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
825a74af788SAkhil Goyal 
826a74af788SAkhil Goyal 	return cf;
827a74af788SAkhil Goyal }
828a74af788SAkhil Goyal 
829a74af788SAkhil Goyal static inline struct dpaa_sec_job *
830c3e85bdcSAkhil Goyal build_cipher_only(struct rte_crypto_op *op, dpaa_sec_session *ses)
831c3e85bdcSAkhil Goyal {
832c3e85bdcSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
833c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
834c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
835c3e85bdcSAkhil Goyal 	struct qm_sg_entry *sg;
836c4509373SSantosh Shukla 	rte_iova_t src_start_addr, dst_start_addr;
837c3e85bdcSAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
838c3e85bdcSAkhil Goyal 			ses->iv.offset);
839c3e85bdcSAkhil Goyal 
840c3e85bdcSAkhil Goyal 	ctx = dpaa_sec_alloc_ctx(ses);
841c3e85bdcSAkhil Goyal 	if (!ctx)
842c3e85bdcSAkhil Goyal 		return NULL;
843c3e85bdcSAkhil Goyal 
844c3e85bdcSAkhil Goyal 	cf = &ctx->job;
845c3e85bdcSAkhil Goyal 	ctx->op = op;
846a389434eSAlok Makhariya 
847bfa9a8a4SThomas Monjalon 	src_start_addr = rte_pktmbuf_iova(sym->m_src);
848a389434eSAlok Makhariya 
849a389434eSAlok Makhariya 	if (sym->m_dst)
850bfa9a8a4SThomas Monjalon 		dst_start_addr = rte_pktmbuf_iova(sym->m_dst);
851a389434eSAlok Makhariya 	else
852a389434eSAlok Makhariya 		dst_start_addr = src_start_addr;
853c3e85bdcSAkhil Goyal 
854c3e85bdcSAkhil Goyal 	/* output */
855c3e85bdcSAkhil Goyal 	sg = &cf->sg[0];
856a389434eSAlok Makhariya 	qm_sg_entry_set64(sg, dst_start_addr + sym->cipher.data.offset);
857c3e85bdcSAkhil Goyal 	sg->length = sym->cipher.data.length + ses->iv.length;
858c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
859c3e85bdcSAkhil Goyal 
860c3e85bdcSAkhil Goyal 	/* input */
861c3e85bdcSAkhil Goyal 	sg = &cf->sg[1];
862c3e85bdcSAkhil Goyal 
863c3e85bdcSAkhil Goyal 	/* need to extend the input to a compound frame */
864c3e85bdcSAkhil Goyal 	sg->extension = 1;
865c3e85bdcSAkhil Goyal 	sg->final = 1;
866c3e85bdcSAkhil Goyal 	sg->length = sym->cipher.data.length + ses->iv.length;
86795456e89SShreyansh Jain 	qm_sg_entry_set64(sg, dpaa_mem_vtop(&cf->sg[2]));
868c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
869c3e85bdcSAkhil Goyal 
870c3e85bdcSAkhil Goyal 	sg = &cf->sg[2];
871c3e85bdcSAkhil Goyal 	qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
872c3e85bdcSAkhil Goyal 	sg->length = ses->iv.length;
873c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
874c3e85bdcSAkhil Goyal 
875c3e85bdcSAkhil Goyal 	sg++;
876a389434eSAlok Makhariya 	qm_sg_entry_set64(sg, src_start_addr + sym->cipher.data.offset);
877c3e85bdcSAkhil Goyal 	sg->length = sym->cipher.data.length;
878c3e85bdcSAkhil Goyal 	sg->final = 1;
879c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
880c3e85bdcSAkhil Goyal 
881c3e85bdcSAkhil Goyal 	return cf;
882c3e85bdcSAkhil Goyal }
883c3e85bdcSAkhil Goyal 
884c3e85bdcSAkhil Goyal static inline struct dpaa_sec_job *
885a74af788SAkhil Goyal build_cipher_auth_gcm_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
886a74af788SAkhil Goyal {
887a74af788SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
888a74af788SAkhil Goyal 	struct dpaa_sec_job *cf;
889a74af788SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
890a74af788SAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
891a74af788SAkhil Goyal 	struct rte_mbuf *mbuf;
892a74af788SAkhil Goyal 	uint8_t req_segs;
893a74af788SAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
894a74af788SAkhil Goyal 			ses->iv.offset);
895a74af788SAkhil Goyal 
896a74af788SAkhil Goyal 	if (sym->m_dst) {
897a74af788SAkhil Goyal 		mbuf = sym->m_dst;
898a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 4;
899a74af788SAkhil Goyal 	} else {
900a74af788SAkhil Goyal 		mbuf = sym->m_src;
901a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs * 2 + 4;
902a74af788SAkhil Goyal 	}
903a74af788SAkhil Goyal 
904a74af788SAkhil Goyal 	if (ses->auth_only_len)
905a74af788SAkhil Goyal 		req_segs++;
906a74af788SAkhil Goyal 
907a74af788SAkhil Goyal 	if (req_segs > MAX_SG_ENTRIES) {
908f163231eSHemant Agrawal 		DPAA_SEC_DP_ERR("AEAD: Max sec segs supported is %d",
909a74af788SAkhil Goyal 				MAX_SG_ENTRIES);
910a74af788SAkhil Goyal 		return NULL;
911a74af788SAkhil Goyal 	}
912a74af788SAkhil Goyal 
913a74af788SAkhil Goyal 	ctx = dpaa_sec_alloc_ctx(ses);
914a74af788SAkhil Goyal 	if (!ctx)
915a74af788SAkhil Goyal 		return NULL;
916a74af788SAkhil Goyal 
917a74af788SAkhil Goyal 	cf = &ctx->job;
918a74af788SAkhil Goyal 	ctx->op = op;
919a74af788SAkhil Goyal 
920a74af788SAkhil Goyal 	rte_prefetch0(cf->sg);
921a74af788SAkhil Goyal 
922a74af788SAkhil Goyal 	/* output */
923a74af788SAkhil Goyal 	out_sg = &cf->sg[0];
924a74af788SAkhil Goyal 	out_sg->extension = 1;
925a74af788SAkhil Goyal 	if (is_encode(ses))
926a74af788SAkhil Goyal 		out_sg->length = sym->aead.data.length + ses->auth_only_len
927a74af788SAkhil Goyal 						+ ses->digest_length;
928a74af788SAkhil Goyal 	else
929a74af788SAkhil Goyal 		out_sg->length = sym->aead.data.length + ses->auth_only_len;
930a74af788SAkhil Goyal 
931a74af788SAkhil Goyal 	/* output sg entries */
932a74af788SAkhil Goyal 	sg = &cf->sg[2];
93395456e89SShreyansh Jain 	qm_sg_entry_set64(out_sg, dpaa_mem_vtop(sg));
934a74af788SAkhil Goyal 	cpu_to_hw_sg(out_sg);
935a74af788SAkhil Goyal 
936a74af788SAkhil Goyal 	/* 1st seg */
937a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
938a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->aead.data.offset +
939a74af788SAkhil Goyal 					ses->auth_only_len;
940a74af788SAkhil Goyal 	sg->offset = sym->aead.data.offset - ses->auth_only_len;
941a74af788SAkhil Goyal 
942a74af788SAkhil Goyal 	/* Successive segs */
943a74af788SAkhil Goyal 	mbuf = mbuf->next;
944a74af788SAkhil Goyal 	while (mbuf) {
945a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
946a74af788SAkhil Goyal 		sg++;
947a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
948a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
949a74af788SAkhil Goyal 		mbuf = mbuf->next;
950a74af788SAkhil Goyal 	}
951a74af788SAkhil Goyal 	sg->length -= ses->digest_length;
952a74af788SAkhil Goyal 
953a74af788SAkhil Goyal 	if (is_encode(ses)) {
954a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
955a74af788SAkhil Goyal 		/* set auth output */
956a74af788SAkhil Goyal 		sg++;
957a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, sym->aead.digest.phys_addr);
958a74af788SAkhil Goyal 		sg->length = ses->digest_length;
959a74af788SAkhil Goyal 	}
960a74af788SAkhil Goyal 	sg->final = 1;
961a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
962a74af788SAkhil Goyal 
963a74af788SAkhil Goyal 	/* input */
964a74af788SAkhil Goyal 	mbuf = sym->m_src;
965a74af788SAkhil Goyal 	in_sg = &cf->sg[1];
966a74af788SAkhil Goyal 	in_sg->extension = 1;
967a74af788SAkhil Goyal 	in_sg->final = 1;
968a74af788SAkhil Goyal 	if (is_encode(ses))
969a74af788SAkhil Goyal 		in_sg->length = ses->iv.length + sym->aead.data.length
970a74af788SAkhil Goyal 							+ ses->auth_only_len;
971a74af788SAkhil Goyal 	else
972a74af788SAkhil Goyal 		in_sg->length = ses->iv.length + sym->aead.data.length
973a74af788SAkhil Goyal 				+ ses->auth_only_len + ses->digest_length;
974a74af788SAkhil Goyal 
975a74af788SAkhil Goyal 	/* input sg entries */
976a74af788SAkhil Goyal 	sg++;
97795456e89SShreyansh Jain 	qm_sg_entry_set64(in_sg, dpaa_mem_vtop(sg));
978a74af788SAkhil Goyal 	cpu_to_hw_sg(in_sg);
979a74af788SAkhil Goyal 
980a74af788SAkhil Goyal 	/* 1st seg IV */
981a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
982a74af788SAkhil Goyal 	sg->length = ses->iv.length;
983a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
984a74af788SAkhil Goyal 
985a74af788SAkhil Goyal 	/* 2nd seg auth only */
986a74af788SAkhil Goyal 	if (ses->auth_only_len) {
987a74af788SAkhil Goyal 		sg++;
988a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, dpaa_mem_vtop(sym->aead.aad.data));
989a74af788SAkhil Goyal 		sg->length = ses->auth_only_len;
990a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
991a74af788SAkhil Goyal 	}
992a74af788SAkhil Goyal 
993a74af788SAkhil Goyal 	/* 3rd seg */
994a74af788SAkhil Goyal 	sg++;
995a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
996a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->aead.data.offset;
997a74af788SAkhil Goyal 	sg->offset = sym->aead.data.offset;
998a74af788SAkhil Goyal 
999a74af788SAkhil Goyal 	/* Successive segs */
1000a74af788SAkhil Goyal 	mbuf = mbuf->next;
1001a74af788SAkhil Goyal 	while (mbuf) {
1002a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1003a74af788SAkhil Goyal 		sg++;
1004a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1005a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1006a74af788SAkhil Goyal 		mbuf = mbuf->next;
1007a74af788SAkhil Goyal 	}
1008a74af788SAkhil Goyal 
1009a74af788SAkhil Goyal 	if (is_decode(ses)) {
1010a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1011a74af788SAkhil Goyal 		sg++;
1012a74af788SAkhil Goyal 		memcpy(ctx->digest, sym->aead.digest.data,
1013a74af788SAkhil Goyal 			ses->digest_length);
101495456e89SShreyansh Jain 		qm_sg_entry_set64(sg, dpaa_mem_vtop(ctx->digest));
1015a74af788SAkhil Goyal 		sg->length = ses->digest_length;
1016a74af788SAkhil Goyal 	}
1017a74af788SAkhil Goyal 	sg->final = 1;
1018a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1019a74af788SAkhil Goyal 
1020a74af788SAkhil Goyal 	return cf;
1021a74af788SAkhil Goyal }
1022a74af788SAkhil Goyal 
1023a74af788SAkhil Goyal static inline struct dpaa_sec_job *
1024c3e85bdcSAkhil Goyal build_cipher_auth_gcm(struct rte_crypto_op *op, dpaa_sec_session *ses)
1025c3e85bdcSAkhil Goyal {
1026c3e85bdcSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1027c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
1028c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1029c3e85bdcSAkhil Goyal 	struct qm_sg_entry *sg;
1030c3e85bdcSAkhil Goyal 	uint32_t length = 0;
1031c4509373SSantosh Shukla 	rte_iova_t src_start_addr, dst_start_addr;
1032c3e85bdcSAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1033c3e85bdcSAkhil Goyal 			ses->iv.offset);
1034c3e85bdcSAkhil Goyal 
1035116ff44aSHemant Agrawal 	src_start_addr = sym->m_src->buf_iova + sym->m_src->data_off;
1036a389434eSAlok Makhariya 
1037a389434eSAlok Makhariya 	if (sym->m_dst)
1038116ff44aSHemant Agrawal 		dst_start_addr = sym->m_dst->buf_iova + sym->m_dst->data_off;
1039a389434eSAlok Makhariya 	else
1040a389434eSAlok Makhariya 		dst_start_addr = src_start_addr;
1041c3e85bdcSAkhil Goyal 
1042c3e85bdcSAkhil Goyal 	ctx = dpaa_sec_alloc_ctx(ses);
1043c3e85bdcSAkhil Goyal 	if (!ctx)
1044c3e85bdcSAkhil Goyal 		return NULL;
1045c3e85bdcSAkhil Goyal 
1046c3e85bdcSAkhil Goyal 	cf = &ctx->job;
1047c3e85bdcSAkhil Goyal 	ctx->op = op;
1048c3e85bdcSAkhil Goyal 
1049c3e85bdcSAkhil Goyal 	/* input */
1050c3e85bdcSAkhil Goyal 	rte_prefetch0(cf->sg);
1051c3e85bdcSAkhil Goyal 	sg = &cf->sg[2];
105295456e89SShreyansh Jain 	qm_sg_entry_set64(&cf->sg[1], dpaa_mem_vtop(sg));
1053c3e85bdcSAkhil Goyal 	if (is_encode(ses)) {
1054c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1055c3e85bdcSAkhil Goyal 		sg->length = ses->iv.length;
1056c3e85bdcSAkhil Goyal 		length += sg->length;
1057c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1058c3e85bdcSAkhil Goyal 
1059c3e85bdcSAkhil Goyal 		sg++;
1060c3e85bdcSAkhil Goyal 		if (ses->auth_only_len) {
1061c3e85bdcSAkhil Goyal 			qm_sg_entry_set64(sg,
1062c3e85bdcSAkhil Goyal 					  dpaa_mem_vtop(sym->aead.aad.data));
1063c3e85bdcSAkhil Goyal 			sg->length = ses->auth_only_len;
1064c3e85bdcSAkhil Goyal 			length += sg->length;
1065c3e85bdcSAkhil Goyal 			cpu_to_hw_sg(sg);
1066c3e85bdcSAkhil Goyal 			sg++;
1067c3e85bdcSAkhil Goyal 		}
1068a389434eSAlok Makhariya 		qm_sg_entry_set64(sg, src_start_addr + sym->aead.data.offset);
1069c3e85bdcSAkhil Goyal 		sg->length = sym->aead.data.length;
1070c3e85bdcSAkhil Goyal 		length += sg->length;
1071c3e85bdcSAkhil Goyal 		sg->final = 1;
1072c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1073c3e85bdcSAkhil Goyal 	} else {
1074c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1075c3e85bdcSAkhil Goyal 		sg->length = ses->iv.length;
1076c3e85bdcSAkhil Goyal 		length += sg->length;
1077c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1078c3e85bdcSAkhil Goyal 
1079c3e85bdcSAkhil Goyal 		sg++;
1080c3e85bdcSAkhil Goyal 		if (ses->auth_only_len) {
1081c3e85bdcSAkhil Goyal 			qm_sg_entry_set64(sg,
1082c3e85bdcSAkhil Goyal 					  dpaa_mem_vtop(sym->aead.aad.data));
1083c3e85bdcSAkhil Goyal 			sg->length = ses->auth_only_len;
1084c3e85bdcSAkhil Goyal 			length += sg->length;
1085c3e85bdcSAkhil Goyal 			cpu_to_hw_sg(sg);
1086c3e85bdcSAkhil Goyal 			sg++;
1087c3e85bdcSAkhil Goyal 		}
1088a389434eSAlok Makhariya 		qm_sg_entry_set64(sg, src_start_addr + sym->aead.data.offset);
1089c3e85bdcSAkhil Goyal 		sg->length = sym->aead.data.length;
1090c3e85bdcSAkhil Goyal 		length += sg->length;
1091c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1092c3e85bdcSAkhil Goyal 
1093c3e85bdcSAkhil Goyal 		memcpy(ctx->digest, sym->aead.digest.data,
1094c3e85bdcSAkhil Goyal 		       ses->digest_length);
1095c3e85bdcSAkhil Goyal 		sg++;
1096c3e85bdcSAkhil Goyal 
109795456e89SShreyansh Jain 		qm_sg_entry_set64(sg, dpaa_mem_vtop(ctx->digest));
1098c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
1099c3e85bdcSAkhil Goyal 		length += sg->length;
1100c3e85bdcSAkhil Goyal 		sg->final = 1;
1101c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1102c3e85bdcSAkhil Goyal 	}
1103c3e85bdcSAkhil Goyal 	/* input compound frame */
1104c3e85bdcSAkhil Goyal 	cf->sg[1].length = length;
1105c3e85bdcSAkhil Goyal 	cf->sg[1].extension = 1;
1106c3e85bdcSAkhil Goyal 	cf->sg[1].final = 1;
1107c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(&cf->sg[1]);
1108c3e85bdcSAkhil Goyal 
1109c3e85bdcSAkhil Goyal 	/* output */
1110c3e85bdcSAkhil Goyal 	sg++;
111195456e89SShreyansh Jain 	qm_sg_entry_set64(&cf->sg[0], dpaa_mem_vtop(sg));
1112c3e85bdcSAkhil Goyal 	qm_sg_entry_set64(sg,
1113a389434eSAlok Makhariya 		dst_start_addr + sym->aead.data.offset - ses->auth_only_len);
1114c3e85bdcSAkhil Goyal 	sg->length = sym->aead.data.length + ses->auth_only_len;
1115c3e85bdcSAkhil Goyal 	length = sg->length;
1116c3e85bdcSAkhil Goyal 	if (is_encode(ses)) {
1117c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1118c3e85bdcSAkhil Goyal 		/* set auth output */
1119c3e85bdcSAkhil Goyal 		sg++;
1120c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, sym->aead.digest.phys_addr);
1121c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
1122c3e85bdcSAkhil Goyal 		length += sg->length;
1123c3e85bdcSAkhil Goyal 	}
1124c3e85bdcSAkhil Goyal 	sg->final = 1;
1125c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1126c3e85bdcSAkhil Goyal 
1127c3e85bdcSAkhil Goyal 	/* output compound frame */
1128c3e85bdcSAkhil Goyal 	cf->sg[0].length = length;
1129c3e85bdcSAkhil Goyal 	cf->sg[0].extension = 1;
1130c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(&cf->sg[0]);
1131c3e85bdcSAkhil Goyal 
1132c3e85bdcSAkhil Goyal 	return cf;
1133c3e85bdcSAkhil Goyal }
1134c3e85bdcSAkhil Goyal 
1135c3e85bdcSAkhil Goyal static inline struct dpaa_sec_job *
1136a74af788SAkhil Goyal build_cipher_auth_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
1137a74af788SAkhil Goyal {
1138a74af788SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1139a74af788SAkhil Goyal 	struct dpaa_sec_job *cf;
1140a74af788SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1141a74af788SAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
1142a74af788SAkhil Goyal 	struct rte_mbuf *mbuf;
1143a74af788SAkhil Goyal 	uint8_t req_segs;
1144a74af788SAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1145a74af788SAkhil Goyal 			ses->iv.offset);
1146a74af788SAkhil Goyal 
1147a74af788SAkhil Goyal 	if (sym->m_dst) {
1148a74af788SAkhil Goyal 		mbuf = sym->m_dst;
1149a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 4;
1150a74af788SAkhil Goyal 	} else {
1151a74af788SAkhil Goyal 		mbuf = sym->m_src;
1152a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs * 2 + 4;
1153a74af788SAkhil Goyal 	}
1154a74af788SAkhil Goyal 
1155a74af788SAkhil Goyal 	if (req_segs > MAX_SG_ENTRIES) {
1156f163231eSHemant Agrawal 		DPAA_SEC_DP_ERR("Cipher-Auth: Max sec segs supported is %d",
1157a74af788SAkhil Goyal 				MAX_SG_ENTRIES);
1158a74af788SAkhil Goyal 		return NULL;
1159a74af788SAkhil Goyal 	}
1160a74af788SAkhil Goyal 
1161a74af788SAkhil Goyal 	ctx = dpaa_sec_alloc_ctx(ses);
1162a74af788SAkhil Goyal 	if (!ctx)
1163a74af788SAkhil Goyal 		return NULL;
1164a74af788SAkhil Goyal 
1165a74af788SAkhil Goyal 	cf = &ctx->job;
1166a74af788SAkhil Goyal 	ctx->op = op;
1167a74af788SAkhil Goyal 
1168a74af788SAkhil Goyal 	rte_prefetch0(cf->sg);
1169a74af788SAkhil Goyal 
1170a74af788SAkhil Goyal 	/* output */
1171a74af788SAkhil Goyal 	out_sg = &cf->sg[0];
1172a74af788SAkhil Goyal 	out_sg->extension = 1;
1173a74af788SAkhil Goyal 	if (is_encode(ses))
1174a74af788SAkhil Goyal 		out_sg->length = sym->auth.data.length + ses->digest_length;
1175a74af788SAkhil Goyal 	else
1176a74af788SAkhil Goyal 		out_sg->length = sym->auth.data.length;
1177a74af788SAkhil Goyal 
1178a74af788SAkhil Goyal 	/* output sg entries */
1179a74af788SAkhil Goyal 	sg = &cf->sg[2];
118095456e89SShreyansh Jain 	qm_sg_entry_set64(out_sg, dpaa_mem_vtop(sg));
1181a74af788SAkhil Goyal 	cpu_to_hw_sg(out_sg);
1182a74af788SAkhil Goyal 
1183a74af788SAkhil Goyal 	/* 1st seg */
1184a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1185a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->auth.data.offset;
1186a74af788SAkhil Goyal 	sg->offset = sym->auth.data.offset;
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->auth.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->auth.data.length;
1216a74af788SAkhil Goyal 	else
1217a74af788SAkhil Goyal 		in_sg->length = ses->iv.length + sym->auth.data.length
1218a74af788SAkhil Goyal 						+ ses->digest_length;
1219a74af788SAkhil Goyal 
1220a74af788SAkhil Goyal 	/* input sg entries */
1221a74af788SAkhil Goyal 	sg++;
122295456e89SShreyansh Jain 	qm_sg_entry_set64(in_sg, dpaa_mem_vtop(sg));
1223a74af788SAkhil Goyal 	cpu_to_hw_sg(in_sg);
1224a74af788SAkhil Goyal 
1225a74af788SAkhil Goyal 	/* 1st seg IV */
1226a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1227a74af788SAkhil Goyal 	sg->length = ses->iv.length;
1228a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1229a74af788SAkhil Goyal 
1230a74af788SAkhil Goyal 	/* 2nd seg */
1231a74af788SAkhil Goyal 	sg++;
1232a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1233a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->auth.data.offset;
1234a74af788SAkhil Goyal 	sg->offset = sym->auth.data.offset;
1235a74af788SAkhil Goyal 
1236a74af788SAkhil Goyal 	/* Successive segs */
1237a74af788SAkhil Goyal 	mbuf = mbuf->next;
1238a74af788SAkhil Goyal 	while (mbuf) {
1239a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1240a74af788SAkhil Goyal 		sg++;
1241a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1242a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1243a74af788SAkhil Goyal 		mbuf = mbuf->next;
1244a74af788SAkhil Goyal 	}
1245a74af788SAkhil Goyal 
1246a74af788SAkhil Goyal 	sg->length -= ses->digest_length;
1247a74af788SAkhil Goyal 	if (is_decode(ses)) {
1248a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1249a74af788SAkhil Goyal 		sg++;
1250a74af788SAkhil Goyal 		memcpy(ctx->digest, sym->auth.digest.data,
1251a74af788SAkhil Goyal 			ses->digest_length);
125295456e89SShreyansh Jain 		qm_sg_entry_set64(sg, dpaa_mem_vtop(ctx->digest));
1253a74af788SAkhil Goyal 		sg->length = ses->digest_length;
1254a74af788SAkhil Goyal 	}
1255a74af788SAkhil Goyal 	sg->final = 1;
1256a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1257a74af788SAkhil Goyal 
1258a74af788SAkhil Goyal 	return cf;
1259a74af788SAkhil Goyal }
1260a74af788SAkhil Goyal 
1261a74af788SAkhil Goyal static inline struct dpaa_sec_job *
1262c3e85bdcSAkhil Goyal build_cipher_auth(struct rte_crypto_op *op, dpaa_sec_session *ses)
1263c3e85bdcSAkhil Goyal {
1264c3e85bdcSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1265c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
1266c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1267c3e85bdcSAkhil Goyal 	struct qm_sg_entry *sg;
1268c4509373SSantosh Shukla 	rte_iova_t src_start_addr, dst_start_addr;
1269c3e85bdcSAkhil Goyal 	uint32_t length = 0;
1270c3e85bdcSAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1271c3e85bdcSAkhil Goyal 			ses->iv.offset);
1272c3e85bdcSAkhil Goyal 
1273455da545SSantosh Shukla 	src_start_addr = sym->m_src->buf_iova + sym->m_src->data_off;
1274a389434eSAlok Makhariya 	if (sym->m_dst)
1275455da545SSantosh Shukla 		dst_start_addr = sym->m_dst->buf_iova + sym->m_dst->data_off;
1276a389434eSAlok Makhariya 	else
1277a389434eSAlok Makhariya 		dst_start_addr = src_start_addr;
1278c3e85bdcSAkhil Goyal 
1279c3e85bdcSAkhil Goyal 	ctx = dpaa_sec_alloc_ctx(ses);
1280c3e85bdcSAkhil Goyal 	if (!ctx)
1281c3e85bdcSAkhil Goyal 		return NULL;
1282c3e85bdcSAkhil Goyal 
1283c3e85bdcSAkhil Goyal 	cf = &ctx->job;
1284c3e85bdcSAkhil Goyal 	ctx->op = op;
1285c3e85bdcSAkhil Goyal 
1286c3e85bdcSAkhil Goyal 	/* input */
1287c3e85bdcSAkhil Goyal 	rte_prefetch0(cf->sg);
1288c3e85bdcSAkhil Goyal 	sg = &cf->sg[2];
128995456e89SShreyansh Jain 	qm_sg_entry_set64(&cf->sg[1], dpaa_mem_vtop(sg));
1290c3e85bdcSAkhil Goyal 	if (is_encode(ses)) {
1291c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1292c3e85bdcSAkhil Goyal 		sg->length = ses->iv.length;
1293c3e85bdcSAkhil Goyal 		length += sg->length;
1294c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1295c3e85bdcSAkhil Goyal 
1296c3e85bdcSAkhil Goyal 		sg++;
1297a389434eSAlok Makhariya 		qm_sg_entry_set64(sg, src_start_addr + sym->auth.data.offset);
1298c3e85bdcSAkhil Goyal 		sg->length = sym->auth.data.length;
1299c3e85bdcSAkhil Goyal 		length += sg->length;
1300c3e85bdcSAkhil Goyal 		sg->final = 1;
1301c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1302c3e85bdcSAkhil Goyal 	} else {
1303c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1304c3e85bdcSAkhil Goyal 		sg->length = ses->iv.length;
1305c3e85bdcSAkhil Goyal 		length += sg->length;
1306c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1307c3e85bdcSAkhil Goyal 
1308c3e85bdcSAkhil Goyal 		sg++;
1309c3e85bdcSAkhil Goyal 
1310a389434eSAlok Makhariya 		qm_sg_entry_set64(sg, src_start_addr + sym->auth.data.offset);
1311c3e85bdcSAkhil Goyal 		sg->length = sym->auth.data.length;
1312c3e85bdcSAkhil Goyal 		length += sg->length;
1313c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1314c3e85bdcSAkhil Goyal 
1315c3e85bdcSAkhil Goyal 		memcpy(ctx->digest, sym->auth.digest.data,
1316c3e85bdcSAkhil Goyal 		       ses->digest_length);
1317c3e85bdcSAkhil Goyal 		sg++;
1318c3e85bdcSAkhil Goyal 
131995456e89SShreyansh Jain 		qm_sg_entry_set64(sg, dpaa_mem_vtop(ctx->digest));
1320c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
1321c3e85bdcSAkhil Goyal 		length += sg->length;
1322c3e85bdcSAkhil Goyal 		sg->final = 1;
1323c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1324c3e85bdcSAkhil Goyal 	}
1325c3e85bdcSAkhil Goyal 	/* input compound frame */
1326c3e85bdcSAkhil Goyal 	cf->sg[1].length = length;
1327c3e85bdcSAkhil Goyal 	cf->sg[1].extension = 1;
1328c3e85bdcSAkhil Goyal 	cf->sg[1].final = 1;
1329c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(&cf->sg[1]);
1330c3e85bdcSAkhil Goyal 
1331c3e85bdcSAkhil Goyal 	/* output */
1332c3e85bdcSAkhil Goyal 	sg++;
133395456e89SShreyansh Jain 	qm_sg_entry_set64(&cf->sg[0], dpaa_mem_vtop(sg));
1334a389434eSAlok Makhariya 	qm_sg_entry_set64(sg, dst_start_addr + sym->cipher.data.offset);
1335c3e85bdcSAkhil Goyal 	sg->length = sym->cipher.data.length;
1336c3e85bdcSAkhil Goyal 	length = sg->length;
1337c3e85bdcSAkhil Goyal 	if (is_encode(ses)) {
1338c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1339c3e85bdcSAkhil Goyal 		/* set auth output */
1340c3e85bdcSAkhil Goyal 		sg++;
1341c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
1342c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
1343c3e85bdcSAkhil Goyal 		length += sg->length;
1344c3e85bdcSAkhil Goyal 	}
1345c3e85bdcSAkhil Goyal 	sg->final = 1;
1346c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1347c3e85bdcSAkhil Goyal 
1348c3e85bdcSAkhil Goyal 	/* output compound frame */
1349c3e85bdcSAkhil Goyal 	cf->sg[0].length = length;
1350c3e85bdcSAkhil Goyal 	cf->sg[0].extension = 1;
1351c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(&cf->sg[0]);
1352c3e85bdcSAkhil Goyal 
1353c3e85bdcSAkhil Goyal 	return cf;
1354c3e85bdcSAkhil Goyal }
1355c3e85bdcSAkhil Goyal 
13561f14d500SAkhil Goyal static inline struct dpaa_sec_job *
13571f14d500SAkhil Goyal build_proto(struct rte_crypto_op *op, dpaa_sec_session *ses)
13581f14d500SAkhil Goyal {
13591f14d500SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
13601f14d500SAkhil Goyal 	struct dpaa_sec_job *cf;
13611f14d500SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
13621f14d500SAkhil Goyal 	struct qm_sg_entry *sg;
13631f14d500SAkhil Goyal 	phys_addr_t src_start_addr, dst_start_addr;
13641f14d500SAkhil Goyal 
13651f14d500SAkhil Goyal 	ctx = dpaa_sec_alloc_ctx(ses);
13661f14d500SAkhil Goyal 	if (!ctx)
13671f14d500SAkhil Goyal 		return NULL;
13681f14d500SAkhil Goyal 	cf = &ctx->job;
13691f14d500SAkhil Goyal 	ctx->op = op;
13701f14d500SAkhil Goyal 
13711f14d500SAkhil Goyal 	src_start_addr = rte_pktmbuf_mtophys(sym->m_src);
13721f14d500SAkhil Goyal 
13731f14d500SAkhil Goyal 	if (sym->m_dst)
13741f14d500SAkhil Goyal 		dst_start_addr = rte_pktmbuf_mtophys(sym->m_dst);
13751f14d500SAkhil Goyal 	else
13761f14d500SAkhil Goyal 		dst_start_addr = src_start_addr;
13771f14d500SAkhil Goyal 
13781f14d500SAkhil Goyal 	/* input */
13791f14d500SAkhil Goyal 	sg = &cf->sg[1];
13801f14d500SAkhil Goyal 	qm_sg_entry_set64(sg, src_start_addr);
13811f14d500SAkhil Goyal 	sg->length = sym->m_src->pkt_len;
13821f14d500SAkhil Goyal 	sg->final = 1;
13831f14d500SAkhil Goyal 	cpu_to_hw_sg(sg);
13841f14d500SAkhil Goyal 
13851f14d500SAkhil Goyal 	sym->m_src->packet_type &= ~RTE_PTYPE_L4_MASK;
13861f14d500SAkhil Goyal 	/* output */
13871f14d500SAkhil Goyal 	sg = &cf->sg[0];
13881f14d500SAkhil Goyal 	qm_sg_entry_set64(sg, dst_start_addr);
13891f14d500SAkhil Goyal 	sg->length = sym->m_src->buf_len - sym->m_src->data_off;
13901f14d500SAkhil Goyal 	cpu_to_hw_sg(sg);
13911f14d500SAkhil Goyal 
13921f14d500SAkhil Goyal 	return cf;
13931f14d500SAkhil Goyal }
13941f14d500SAkhil Goyal 
13959a984458SAkhil Goyal static uint16_t
13969a984458SAkhil Goyal dpaa_sec_enqueue_burst(void *qp, struct rte_crypto_op **ops,
13979a984458SAkhil Goyal 		       uint16_t nb_ops)
1398c3e85bdcSAkhil Goyal {
13999a984458SAkhil Goyal 	/* Function to transmit the frames to given device and queuepair */
14009a984458SAkhil Goyal 	uint32_t loop;
14019a984458SAkhil Goyal 	struct dpaa_sec_qp *dpaa_qp = (struct dpaa_sec_qp *)qp;
14029a984458SAkhil Goyal 	uint16_t num_tx = 0;
14039a984458SAkhil Goyal 	struct qm_fd fds[DPAA_SEC_BURST], *fd;
14049a984458SAkhil Goyal 	uint32_t frames_to_send;
14059a984458SAkhil Goyal 	struct rte_crypto_op *op;
1406c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
1407c3e85bdcSAkhil Goyal 	dpaa_sec_session *ses;
14089a984458SAkhil Goyal 	uint32_t auth_only_len;
14099a984458SAkhil Goyal 	struct qman_fq *inq[DPAA_SEC_BURST];
1410c3e85bdcSAkhil Goyal 
14119a984458SAkhil Goyal 	while (nb_ops) {
14129a984458SAkhil Goyal 		frames_to_send = (nb_ops > DPAA_SEC_BURST) ?
14139a984458SAkhil Goyal 				DPAA_SEC_BURST : nb_ops;
14149a984458SAkhil Goyal 		for (loop = 0; loop < frames_to_send; loop++) {
14159a984458SAkhil Goyal 			op = *(ops++);
14169a984458SAkhil Goyal 			switch (op->sess_type) {
14179a984458SAkhil Goyal 			case RTE_CRYPTO_OP_WITH_SESSION:
14189a984458SAkhil Goyal 				ses = (dpaa_sec_session *)
1419012c5076SPablo de Lara 					get_sym_session_private_data(
14209a984458SAkhil Goyal 							op->sym->session,
14219a984458SAkhil Goyal 							cryptodev_driver_id);
14229a984458SAkhil Goyal 				break;
14239a984458SAkhil Goyal 			case RTE_CRYPTO_OP_SECURITY_SESSION:
14249a984458SAkhil Goyal 				ses = (dpaa_sec_session *)
14259a984458SAkhil Goyal 					get_sec_session_private_data(
14261f14d500SAkhil Goyal 							op->sym->sec_session);
14279a984458SAkhil Goyal 				break;
14289a984458SAkhil Goyal 			default:
1429f163231eSHemant Agrawal 				DPAA_SEC_DP_ERR(
14309a984458SAkhil Goyal 					"sessionless crypto op not supported");
14319a984458SAkhil Goyal 				frames_to_send = loop;
14329a984458SAkhil Goyal 				nb_ops = loop;
14339a984458SAkhil Goyal 				goto send_pkts;
14349a984458SAkhil Goyal 			}
1435e79416d1SHemant Agrawal 			if (unlikely(!ses->qp || ses->qp != qp)) {
1436f163231eSHemant Agrawal 				DPAA_SEC_DP_ERR("sess->qp - %p qp %p",
14379a984458SAkhil Goyal 					     ses->qp, qp);
14389a984458SAkhil Goyal 				if (dpaa_sec_attach_sess_q(qp, ses)) {
14399a984458SAkhil Goyal 					frames_to_send = loop;
14409a984458SAkhil Goyal 					nb_ops = loop;
14419a984458SAkhil Goyal 					goto send_pkts;
14429a984458SAkhil Goyal 				}
1443c3e85bdcSAkhil Goyal 			}
1444c3e85bdcSAkhil Goyal 
14459a984458SAkhil Goyal 			auth_only_len = op->sym->auth.data.length -
14469a984458SAkhil Goyal 						op->sym->cipher.data.length;
1447a74af788SAkhil Goyal 			if (rte_pktmbuf_is_contiguous(op->sym->m_src)) {
1448c3e85bdcSAkhil Goyal 				if (is_auth_only(ses)) {
1449c3e85bdcSAkhil Goyal 					cf = build_auth_only(op, ses);
1450c3e85bdcSAkhil Goyal 				} else if (is_cipher_only(ses)) {
1451c3e85bdcSAkhil Goyal 					cf = build_cipher_only(op, ses);
1452c3e85bdcSAkhil Goyal 				} else if (is_aead(ses)) {
1453c3e85bdcSAkhil Goyal 					cf = build_cipher_auth_gcm(op, ses);
1454c3e85bdcSAkhil Goyal 					auth_only_len = ses->auth_only_len;
1455c3e85bdcSAkhil Goyal 				} else if (is_auth_cipher(ses)) {
1456c3e85bdcSAkhil Goyal 					cf = build_cipher_auth(op, ses);
14571f14d500SAkhil Goyal 				} else if (is_proto_ipsec(ses)) {
14581f14d500SAkhil Goyal 					cf = build_proto(op, ses);
1459c3e85bdcSAkhil Goyal 				} else {
1460f163231eSHemant Agrawal 					DPAA_SEC_DP_ERR("not supported ops");
14619a984458SAkhil Goyal 					frames_to_send = loop;
14629a984458SAkhil Goyal 					nb_ops = loop;
14639a984458SAkhil Goyal 					goto send_pkts;
1464c3e85bdcSAkhil Goyal 				}
1465a74af788SAkhil Goyal 			} else {
1466a74af788SAkhil Goyal 				if (is_auth_only(ses)) {
1467a74af788SAkhil Goyal 					cf = build_auth_only_sg(op, ses);
1468a74af788SAkhil Goyal 				} else if (is_cipher_only(ses)) {
1469a74af788SAkhil Goyal 					cf = build_cipher_only_sg(op, ses);
1470a74af788SAkhil Goyal 				} else if (is_aead(ses)) {
1471a74af788SAkhil Goyal 					cf = build_cipher_auth_gcm_sg(op, ses);
1472a74af788SAkhil Goyal 					auth_only_len = ses->auth_only_len;
1473a74af788SAkhil Goyal 				} else if (is_auth_cipher(ses)) {
1474a74af788SAkhil Goyal 					cf = build_cipher_auth_sg(op, ses);
1475a74af788SAkhil Goyal 				} else {
1476f163231eSHemant Agrawal 					DPAA_SEC_DP_ERR("not supported ops");
1477a74af788SAkhil Goyal 					frames_to_send = loop;
1478a74af788SAkhil Goyal 					nb_ops = loop;
1479a74af788SAkhil Goyal 					goto send_pkts;
1480a74af788SAkhil Goyal 				}
1481a74af788SAkhil Goyal 			}
14829a984458SAkhil Goyal 			if (unlikely(!cf)) {
14839a984458SAkhil Goyal 				frames_to_send = loop;
14849a984458SAkhil Goyal 				nb_ops = loop;
14859a984458SAkhil Goyal 				goto send_pkts;
14869a984458SAkhil Goyal 			}
1487c3e85bdcSAkhil Goyal 
14889a984458SAkhil Goyal 			fd = &fds[loop];
14899a984458SAkhil Goyal 			inq[loop] = ses->inq;
14909a984458SAkhil Goyal 			fd->opaque_addr = 0;
14919a984458SAkhil Goyal 			fd->cmd = 0;
149295456e89SShreyansh Jain 			qm_fd_addr_set64(fd, dpaa_mem_vtop(cf->sg));
14939a984458SAkhil Goyal 			fd->_format1 = qm_fd_compound;
14949a984458SAkhil Goyal 			fd->length29 = 2 * sizeof(struct qm_sg_entry);
14959a984458SAkhil Goyal 			/* Auth_only_len is set as 0 in descriptor and it is
14969a984458SAkhil Goyal 			 * overwritten here in the fd.cmd which will update
14979a984458SAkhil Goyal 			 * the DPOVRD reg.
1498c3e85bdcSAkhil Goyal 			 */
1499c3e85bdcSAkhil Goyal 			if (auth_only_len)
15009a984458SAkhil Goyal 				fd->cmd = 0x80000000 | auth_only_len;
1501c3e85bdcSAkhil Goyal 
15029a984458SAkhil Goyal 		}
15039a984458SAkhil Goyal send_pkts:
15049a984458SAkhil Goyal 		loop = 0;
15059a984458SAkhil Goyal 		while (loop < frames_to_send) {
15069a984458SAkhil Goyal 			loop += qman_enqueue_multi_fq(&inq[loop], &fds[loop],
15079a984458SAkhil Goyal 					frames_to_send - loop);
15089a984458SAkhil Goyal 		}
15099a984458SAkhil Goyal 		nb_ops -= frames_to_send;
15109a984458SAkhil Goyal 		num_tx += frames_to_send;
1511c3e85bdcSAkhil Goyal 	}
1512c3e85bdcSAkhil Goyal 
1513c3e85bdcSAkhil Goyal 	dpaa_qp->tx_pkts += num_tx;
1514c3e85bdcSAkhil Goyal 	dpaa_qp->tx_errs += nb_ops - num_tx;
1515c3e85bdcSAkhil Goyal 
1516c3e85bdcSAkhil Goyal 	return num_tx;
1517c3e85bdcSAkhil Goyal }
1518c3e85bdcSAkhil Goyal 
1519c3e85bdcSAkhil Goyal static uint16_t
1520c3e85bdcSAkhil Goyal dpaa_sec_dequeue_burst(void *qp, struct rte_crypto_op **ops,
1521c3e85bdcSAkhil Goyal 		       uint16_t nb_ops)
1522c3e85bdcSAkhil Goyal {
1523c3e85bdcSAkhil Goyal 	uint16_t num_rx;
1524c3e85bdcSAkhil Goyal 	struct dpaa_sec_qp *dpaa_qp = (struct dpaa_sec_qp *)qp;
1525c3e85bdcSAkhil Goyal 
1526c3e85bdcSAkhil Goyal 	num_rx = dpaa_sec_deq(dpaa_qp, ops, nb_ops);
1527c3e85bdcSAkhil Goyal 
1528c3e85bdcSAkhil Goyal 	dpaa_qp->rx_pkts += num_rx;
1529c3e85bdcSAkhil Goyal 	dpaa_qp->rx_errs += nb_ops - num_rx;
1530c3e85bdcSAkhil Goyal 
1531f163231eSHemant Agrawal 	DPAA_SEC_DP_DEBUG("SEC Received %d Packets\n", num_rx);
1532c3e85bdcSAkhil Goyal 
1533c3e85bdcSAkhil Goyal 	return num_rx;
1534c3e85bdcSAkhil Goyal }
1535c3e85bdcSAkhil Goyal 
1536c3e85bdcSAkhil Goyal /** Release queue pair */
1537c3e85bdcSAkhil Goyal static int
1538c3e85bdcSAkhil Goyal dpaa_sec_queue_pair_release(struct rte_cryptodev *dev,
1539c3e85bdcSAkhil Goyal 			    uint16_t qp_id)
1540c3e85bdcSAkhil Goyal {
1541c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals;
1542c3e85bdcSAkhil Goyal 	struct dpaa_sec_qp *qp = NULL;
1543c3e85bdcSAkhil Goyal 
1544c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
1545c3e85bdcSAkhil Goyal 
1546f163231eSHemant Agrawal 	DPAA_SEC_DEBUG("dev =%p, queue =%d", dev, qp_id);
1547c3e85bdcSAkhil Goyal 
1548c3e85bdcSAkhil Goyal 	internals = dev->data->dev_private;
1549c3e85bdcSAkhil Goyal 	if (qp_id >= internals->max_nb_queue_pairs) {
1550f163231eSHemant Agrawal 		DPAA_SEC_ERR("Max supported qpid %d",
1551c3e85bdcSAkhil Goyal 			     internals->max_nb_queue_pairs);
1552c3e85bdcSAkhil Goyal 		return -EINVAL;
1553c3e85bdcSAkhil Goyal 	}
1554c3e85bdcSAkhil Goyal 
1555c3e85bdcSAkhil Goyal 	qp = &internals->qps[qp_id];
1556c3e85bdcSAkhil Goyal 	qp->internals = NULL;
1557c3e85bdcSAkhil Goyal 	dev->data->queue_pairs[qp_id] = NULL;
1558c3e85bdcSAkhil Goyal 
1559c3e85bdcSAkhil Goyal 	return 0;
1560c3e85bdcSAkhil Goyal }
1561c3e85bdcSAkhil Goyal 
1562c3e85bdcSAkhil Goyal /** Setup a queue pair */
1563c3e85bdcSAkhil Goyal static int
1564c3e85bdcSAkhil Goyal dpaa_sec_queue_pair_setup(struct rte_cryptodev *dev, uint16_t qp_id,
1565c3e85bdcSAkhil Goyal 		__rte_unused const struct rte_cryptodev_qp_conf *qp_conf,
1566c3e85bdcSAkhil Goyal 		__rte_unused int socket_id,
1567c3e85bdcSAkhil Goyal 		__rte_unused struct rte_mempool *session_pool)
1568c3e85bdcSAkhil Goyal {
1569c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals;
1570c3e85bdcSAkhil Goyal 	struct dpaa_sec_qp *qp = NULL;
1571c3e85bdcSAkhil Goyal 
1572f163231eSHemant Agrawal 	DPAA_SEC_DEBUG("dev =%p, queue =%d, conf =%p", dev, qp_id, qp_conf);
1573c3e85bdcSAkhil Goyal 
1574c3e85bdcSAkhil Goyal 	internals = dev->data->dev_private;
1575c3e85bdcSAkhil Goyal 	if (qp_id >= internals->max_nb_queue_pairs) {
1576f163231eSHemant Agrawal 		DPAA_SEC_ERR("Max supported qpid %d",
1577c3e85bdcSAkhil Goyal 			     internals->max_nb_queue_pairs);
1578c3e85bdcSAkhil Goyal 		return -EINVAL;
1579c3e85bdcSAkhil Goyal 	}
1580c3e85bdcSAkhil Goyal 
1581c3e85bdcSAkhil Goyal 	qp = &internals->qps[qp_id];
1582c3e85bdcSAkhil Goyal 	qp->internals = internals;
1583c3e85bdcSAkhil Goyal 	dev->data->queue_pairs[qp_id] = qp;
1584c3e85bdcSAkhil Goyal 
1585c3e85bdcSAkhil Goyal 	return 0;
1586c3e85bdcSAkhil Goyal }
1587c3e85bdcSAkhil Goyal 
1588c3e85bdcSAkhil Goyal /** Return the number of allocated queue pairs */
1589c3e85bdcSAkhil Goyal static uint32_t
1590c3e85bdcSAkhil Goyal dpaa_sec_queue_pair_count(struct rte_cryptodev *dev)
1591c3e85bdcSAkhil Goyal {
1592c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
1593c3e85bdcSAkhil Goyal 
1594c3e85bdcSAkhil Goyal 	return dev->data->nb_queue_pairs;
1595c3e85bdcSAkhil Goyal }
1596c3e85bdcSAkhil Goyal 
1597c3e85bdcSAkhil Goyal /** Returns the size of session structure */
1598c3e85bdcSAkhil Goyal static unsigned int
1599012c5076SPablo de Lara dpaa_sec_sym_session_get_size(struct rte_cryptodev *dev __rte_unused)
1600c3e85bdcSAkhil Goyal {
1601c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
1602c3e85bdcSAkhil Goyal 
1603c3e85bdcSAkhil Goyal 	return sizeof(dpaa_sec_session);
1604c3e85bdcSAkhil Goyal }
1605c3e85bdcSAkhil Goyal 
1606c3e85bdcSAkhil Goyal static int
1607c3e85bdcSAkhil Goyal dpaa_sec_cipher_init(struct rte_cryptodev *dev __rte_unused,
1608c3e85bdcSAkhil Goyal 		     struct rte_crypto_sym_xform *xform,
1609c3e85bdcSAkhil Goyal 		     dpaa_sec_session *session)
1610c3e85bdcSAkhil Goyal {
1611c3e85bdcSAkhil Goyal 	session->cipher_alg = xform->cipher.algo;
1612c3e85bdcSAkhil Goyal 	session->iv.length = xform->cipher.iv.length;
1613c3e85bdcSAkhil Goyal 	session->iv.offset = xform->cipher.iv.offset;
1614c3e85bdcSAkhil Goyal 	session->cipher_key.data = rte_zmalloc(NULL, xform->cipher.key.length,
1615c3e85bdcSAkhil Goyal 					       RTE_CACHE_LINE_SIZE);
1616c3e85bdcSAkhil Goyal 	if (session->cipher_key.data == NULL && xform->cipher.key.length > 0) {
1617f163231eSHemant Agrawal 		DPAA_SEC_ERR("No Memory for cipher key");
1618c3e85bdcSAkhil Goyal 		return -ENOMEM;
1619c3e85bdcSAkhil Goyal 	}
1620c3e85bdcSAkhil Goyal 	session->cipher_key.length = xform->cipher.key.length;
1621c3e85bdcSAkhil Goyal 
1622c3e85bdcSAkhil Goyal 	memcpy(session->cipher_key.data, xform->cipher.key.data,
1623c3e85bdcSAkhil Goyal 	       xform->cipher.key.length);
1624c3e85bdcSAkhil Goyal 	session->dir = (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
1625c3e85bdcSAkhil Goyal 			DIR_ENC : DIR_DEC;
1626c3e85bdcSAkhil Goyal 
1627c3e85bdcSAkhil Goyal 	return 0;
1628c3e85bdcSAkhil Goyal }
1629c3e85bdcSAkhil Goyal 
1630c3e85bdcSAkhil Goyal static int
1631c3e85bdcSAkhil Goyal dpaa_sec_auth_init(struct rte_cryptodev *dev __rte_unused,
1632c3e85bdcSAkhil Goyal 		   struct rte_crypto_sym_xform *xform,
1633c3e85bdcSAkhil Goyal 		   dpaa_sec_session *session)
1634c3e85bdcSAkhil Goyal {
1635c3e85bdcSAkhil Goyal 	session->auth_alg = xform->auth.algo;
1636c3e85bdcSAkhil Goyal 	session->auth_key.data = rte_zmalloc(NULL, xform->auth.key.length,
1637c3e85bdcSAkhil Goyal 					     RTE_CACHE_LINE_SIZE);
1638c3e85bdcSAkhil Goyal 	if (session->auth_key.data == NULL && xform->auth.key.length > 0) {
1639f163231eSHemant Agrawal 		DPAA_SEC_ERR("No Memory for auth key");
1640c3e85bdcSAkhil Goyal 		return -ENOMEM;
1641c3e85bdcSAkhil Goyal 	}
1642c3e85bdcSAkhil Goyal 	session->auth_key.length = xform->auth.key.length;
1643c3e85bdcSAkhil Goyal 	session->digest_length = xform->auth.digest_length;
1644c3e85bdcSAkhil Goyal 
1645c3e85bdcSAkhil Goyal 	memcpy(session->auth_key.data, xform->auth.key.data,
1646c3e85bdcSAkhil Goyal 	       xform->auth.key.length);
1647c3e85bdcSAkhil Goyal 	session->dir = (xform->auth.op == RTE_CRYPTO_AUTH_OP_GENERATE) ?
1648c3e85bdcSAkhil Goyal 			DIR_ENC : DIR_DEC;
1649c3e85bdcSAkhil Goyal 
1650c3e85bdcSAkhil Goyal 	return 0;
1651c3e85bdcSAkhil Goyal }
1652c3e85bdcSAkhil Goyal 
1653c3e85bdcSAkhil Goyal static int
1654c3e85bdcSAkhil Goyal dpaa_sec_aead_init(struct rte_cryptodev *dev __rte_unused,
1655c3e85bdcSAkhil Goyal 		   struct rte_crypto_sym_xform *xform,
1656c3e85bdcSAkhil Goyal 		   dpaa_sec_session *session)
1657c3e85bdcSAkhil Goyal {
1658c3e85bdcSAkhil Goyal 	session->aead_alg = xform->aead.algo;
1659c3e85bdcSAkhil Goyal 	session->iv.length = xform->aead.iv.length;
1660c3e85bdcSAkhil Goyal 	session->iv.offset = xform->aead.iv.offset;
1661c3e85bdcSAkhil Goyal 	session->auth_only_len = xform->aead.aad_length;
1662c3e85bdcSAkhil Goyal 	session->aead_key.data = rte_zmalloc(NULL, xform->aead.key.length,
1663c3e85bdcSAkhil Goyal 					     RTE_CACHE_LINE_SIZE);
1664c3e85bdcSAkhil Goyal 	if (session->aead_key.data == NULL && xform->aead.key.length > 0) {
1665f163231eSHemant Agrawal 		DPAA_SEC_ERR("No Memory for aead key\n");
1666c3e85bdcSAkhil Goyal 		return -ENOMEM;
1667c3e85bdcSAkhil Goyal 	}
1668c3e85bdcSAkhil Goyal 	session->aead_key.length = xform->aead.key.length;
1669c3e85bdcSAkhil Goyal 	session->digest_length = xform->aead.digest_length;
1670c3e85bdcSAkhil Goyal 
1671c3e85bdcSAkhil Goyal 	memcpy(session->aead_key.data, xform->aead.key.data,
1672c3e85bdcSAkhil Goyal 	       xform->aead.key.length);
1673c3e85bdcSAkhil Goyal 	session->dir = (xform->aead.op == RTE_CRYPTO_AEAD_OP_ENCRYPT) ?
1674c3e85bdcSAkhil Goyal 			DIR_ENC : DIR_DEC;
1675c3e85bdcSAkhil Goyal 
1676c3e85bdcSAkhil Goyal 	return 0;
1677c3e85bdcSAkhil Goyal }
1678c3e85bdcSAkhil Goyal 
1679e79416d1SHemant Agrawal static struct qman_fq *
1680e79416d1SHemant Agrawal dpaa_sec_attach_rxq(struct dpaa_sec_dev_private *qi)
1681c3e85bdcSAkhil Goyal {
1682e79416d1SHemant Agrawal 	unsigned int i;
1683c3e85bdcSAkhil Goyal 
1684e79416d1SHemant Agrawal 	for (i = 0; i < qi->max_nb_sessions; i++) {
1685e79416d1SHemant Agrawal 		if (qi->inq_attach[i] == 0) {
1686e79416d1SHemant Agrawal 			qi->inq_attach[i] = 1;
1687e79416d1SHemant Agrawal 			return &qi->inq[i];
1688e79416d1SHemant Agrawal 		}
1689e79416d1SHemant Agrawal 	}
1690f163231eSHemant Agrawal 	DPAA_SEC_WARN("All ses session in use %x", qi->max_nb_sessions);
1691c3e85bdcSAkhil Goyal 
1692e79416d1SHemant Agrawal 	return NULL;
1693c3e85bdcSAkhil Goyal }
1694c3e85bdcSAkhil Goyal 
1695e79416d1SHemant Agrawal static int
1696e79416d1SHemant Agrawal dpaa_sec_detach_rxq(struct dpaa_sec_dev_private *qi, struct qman_fq *fq)
1697e79416d1SHemant Agrawal {
1698e79416d1SHemant Agrawal 	unsigned int i;
1699e79416d1SHemant Agrawal 
1700e79416d1SHemant Agrawal 	for (i = 0; i < qi->max_nb_sessions; i++) {
1701e79416d1SHemant Agrawal 		if (&qi->inq[i] == fq) {
1702b4053c4bSAlok Makhariya 			qman_retire_fq(fq, NULL);
1703b4053c4bSAlok Makhariya 			qman_oos_fq(fq);
1704e79416d1SHemant Agrawal 			qi->inq_attach[i] = 0;
1705e79416d1SHemant Agrawal 			return 0;
1706e79416d1SHemant Agrawal 		}
1707e79416d1SHemant Agrawal 	}
1708e79416d1SHemant Agrawal 	return -1;
1709e79416d1SHemant Agrawal }
1710e79416d1SHemant Agrawal 
1711e79416d1SHemant Agrawal static int
1712e79416d1SHemant Agrawal dpaa_sec_attach_sess_q(struct dpaa_sec_qp *qp, dpaa_sec_session *sess)
1713e79416d1SHemant Agrawal {
1714e79416d1SHemant Agrawal 	int ret;
1715e79416d1SHemant Agrawal 
1716c3e85bdcSAkhil Goyal 	sess->qp = qp;
1717e79416d1SHemant Agrawal 	ret = dpaa_sec_prep_cdb(sess);
1718e79416d1SHemant Agrawal 	if (ret) {
1719f163231eSHemant Agrawal 		DPAA_SEC_ERR("Unable to prepare sec cdb");
1720e79416d1SHemant Agrawal 		return -1;
1721e79416d1SHemant Agrawal 	}
17225b0f1bd3SAshish Jain 	if (unlikely(!RTE_PER_LCORE(dpaa_io))) {
17235b0f1bd3SAshish Jain 		ret = rte_dpaa_portal_init((void *)0);
17245b0f1bd3SAshish Jain 		if (ret) {
1725f163231eSHemant Agrawal 			DPAA_SEC_ERR("Failure in affining portal");
17265b0f1bd3SAshish Jain 			return ret;
17275b0f1bd3SAshish Jain 		}
17285b0f1bd3SAshish Jain 	}
1729e79416d1SHemant Agrawal 	ret = dpaa_sec_init_rx(sess->inq, dpaa_mem_vtop(&sess->cdb),
1730e79416d1SHemant Agrawal 			       qman_fq_fqid(&qp->outq));
1731e79416d1SHemant Agrawal 	if (ret)
1732f163231eSHemant Agrawal 		DPAA_SEC_ERR("Unable to init sec queue");
1733e79416d1SHemant Agrawal 
1734e79416d1SHemant Agrawal 	return ret;
1735c3e85bdcSAkhil Goyal }
1736c3e85bdcSAkhil Goyal 
1737c3e85bdcSAkhil Goyal static int
1738c3e85bdcSAkhil Goyal dpaa_sec_set_session_parameters(struct rte_cryptodev *dev,
1739c3e85bdcSAkhil Goyal 			    struct rte_crypto_sym_xform *xform,	void *sess)
1740c3e85bdcSAkhil Goyal {
1741c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals = dev->data->dev_private;
1742c3e85bdcSAkhil Goyal 	dpaa_sec_session *session = sess;
1743c3e85bdcSAkhil Goyal 
1744c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
1745c3e85bdcSAkhil Goyal 
1746c3e85bdcSAkhil Goyal 	if (unlikely(sess == NULL)) {
1747f163231eSHemant Agrawal 		DPAA_SEC_ERR("invalid session struct");
1748c3e85bdcSAkhil Goyal 		return -EINVAL;
1749c3e85bdcSAkhil Goyal 	}
1750c3e85bdcSAkhil Goyal 
1751c3e85bdcSAkhil Goyal 	/* Default IV length = 0 */
1752c3e85bdcSAkhil Goyal 	session->iv.length = 0;
1753c3e85bdcSAkhil Goyal 
1754c3e85bdcSAkhil Goyal 	/* Cipher Only */
1755c3e85bdcSAkhil Goyal 	if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER && xform->next == NULL) {
1756c3e85bdcSAkhil Goyal 		session->auth_alg = RTE_CRYPTO_AUTH_NULL;
1757c3e85bdcSAkhil Goyal 		dpaa_sec_cipher_init(dev, xform, session);
1758c3e85bdcSAkhil Goyal 
1759c3e85bdcSAkhil Goyal 	/* Authentication Only */
1760c3e85bdcSAkhil Goyal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
1761c3e85bdcSAkhil Goyal 		   xform->next == NULL) {
1762c3e85bdcSAkhil Goyal 		session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
1763c3e85bdcSAkhil Goyal 		dpaa_sec_auth_init(dev, xform, session);
1764c3e85bdcSAkhil Goyal 
1765c3e85bdcSAkhil Goyal 	/* Cipher then Authenticate */
1766c3e85bdcSAkhil Goyal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
1767c3e85bdcSAkhil Goyal 		   xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
1768c3e85bdcSAkhil Goyal 		if (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) {
1769c3e85bdcSAkhil Goyal 			dpaa_sec_cipher_init(dev, xform, session);
1770c3e85bdcSAkhil Goyal 			dpaa_sec_auth_init(dev, xform->next, session);
1771c3e85bdcSAkhil Goyal 		} else {
1772f163231eSHemant Agrawal 			DPAA_SEC_ERR("Not supported: Auth then Cipher");
1773c3e85bdcSAkhil Goyal 			return -EINVAL;
1774c3e85bdcSAkhil Goyal 		}
1775c3e85bdcSAkhil Goyal 
1776c3e85bdcSAkhil Goyal 	/* Authenticate then Cipher */
1777c3e85bdcSAkhil Goyal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
1778c3e85bdcSAkhil Goyal 		   xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
1779c3e85bdcSAkhil Goyal 		if (xform->next->cipher.op == RTE_CRYPTO_CIPHER_OP_DECRYPT) {
1780c3e85bdcSAkhil Goyal 			dpaa_sec_auth_init(dev, xform, session);
1781c3e85bdcSAkhil Goyal 			dpaa_sec_cipher_init(dev, xform->next, session);
1782c3e85bdcSAkhil Goyal 		} else {
1783f163231eSHemant Agrawal 			DPAA_SEC_ERR("Not supported: Auth then Cipher");
1784c3e85bdcSAkhil Goyal 			return -EINVAL;
1785c3e85bdcSAkhil Goyal 		}
1786c3e85bdcSAkhil Goyal 
1787c3e85bdcSAkhil Goyal 	/* AEAD operation for AES-GCM kind of Algorithms */
1788c3e85bdcSAkhil Goyal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD &&
1789c3e85bdcSAkhil Goyal 		   xform->next == NULL) {
1790c3e85bdcSAkhil Goyal 		dpaa_sec_aead_init(dev, xform, session);
1791c3e85bdcSAkhil Goyal 
1792c3e85bdcSAkhil Goyal 	} else {
1793f163231eSHemant Agrawal 		DPAA_SEC_ERR("Invalid crypto type");
1794c3e85bdcSAkhil Goyal 		return -EINVAL;
1795c3e85bdcSAkhil Goyal 	}
1796c3e85bdcSAkhil Goyal 	session->ctx_pool = internals->ctx_pool;
1797e79416d1SHemant Agrawal 	session->inq = dpaa_sec_attach_rxq(internals);
1798e79416d1SHemant Agrawal 	if (session->inq == NULL) {
1799f163231eSHemant Agrawal 		DPAA_SEC_ERR("unable to attach sec queue");
1800e79416d1SHemant Agrawal 		goto err1;
1801e79416d1SHemant Agrawal 	}
1802c3e85bdcSAkhil Goyal 
1803c3e85bdcSAkhil Goyal 	return 0;
1804e79416d1SHemant Agrawal 
1805e79416d1SHemant Agrawal err1:
1806e79416d1SHemant Agrawal 	rte_free(session->cipher_key.data);
1807e79416d1SHemant Agrawal 	rte_free(session->auth_key.data);
1808e79416d1SHemant Agrawal 	memset(session, 0, sizeof(dpaa_sec_session));
1809e79416d1SHemant Agrawal 
1810e79416d1SHemant Agrawal 	return -EINVAL;
1811c3e85bdcSAkhil Goyal }
1812c3e85bdcSAkhil Goyal 
1813c3e85bdcSAkhil Goyal static int
1814012c5076SPablo de Lara dpaa_sec_sym_session_configure(struct rte_cryptodev *dev,
1815c3e85bdcSAkhil Goyal 		struct rte_crypto_sym_xform *xform,
1816c3e85bdcSAkhil Goyal 		struct rte_cryptodev_sym_session *sess,
1817c3e85bdcSAkhil Goyal 		struct rte_mempool *mempool)
1818c3e85bdcSAkhil Goyal {
1819c3e85bdcSAkhil Goyal 	void *sess_private_data;
1820c3e85bdcSAkhil Goyal 	int ret;
1821c3e85bdcSAkhil Goyal 
1822c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
1823c3e85bdcSAkhil Goyal 
1824c3e85bdcSAkhil Goyal 	if (rte_mempool_get(mempool, &sess_private_data)) {
1825f163231eSHemant Agrawal 		DPAA_SEC_ERR("Couldn't get object from session mempool");
1826c3e85bdcSAkhil Goyal 		return -ENOMEM;
1827c3e85bdcSAkhil Goyal 	}
1828c3e85bdcSAkhil Goyal 
1829c3e85bdcSAkhil Goyal 	ret = dpaa_sec_set_session_parameters(dev, xform, sess_private_data);
1830c3e85bdcSAkhil Goyal 	if (ret != 0) {
1831f163231eSHemant Agrawal 		DPAA_SEC_ERR("failed to configure session parameters");
1832c3e85bdcSAkhil Goyal 
1833c3e85bdcSAkhil Goyal 		/* Return session to mempool */
1834c3e85bdcSAkhil Goyal 		rte_mempool_put(mempool, sess_private_data);
1835c3e85bdcSAkhil Goyal 		return ret;
1836c3e85bdcSAkhil Goyal 	}
1837c3e85bdcSAkhil Goyal 
1838012c5076SPablo de Lara 	set_sym_session_private_data(sess, dev->driver_id,
1839c3e85bdcSAkhil Goyal 			sess_private_data);
1840c3e85bdcSAkhil Goyal 
1841e79416d1SHemant Agrawal 
1842c3e85bdcSAkhil Goyal 	return 0;
1843c3e85bdcSAkhil Goyal }
1844c3e85bdcSAkhil Goyal 
1845c3e85bdcSAkhil Goyal /** Clear the memory of session so it doesn't leave key material behind */
1846c3e85bdcSAkhil Goyal static void
1847012c5076SPablo de Lara dpaa_sec_sym_session_clear(struct rte_cryptodev *dev,
1848c3e85bdcSAkhil Goyal 		struct rte_cryptodev_sym_session *sess)
1849c3e85bdcSAkhil Goyal {
1850e79416d1SHemant Agrawal 	struct dpaa_sec_dev_private *qi = dev->data->dev_private;
1851c3e85bdcSAkhil Goyal 	uint8_t index = dev->driver_id;
1852012c5076SPablo de Lara 	void *sess_priv = get_sym_session_private_data(sess, index);
1853e79416d1SHemant Agrawal 
1854e79416d1SHemant Agrawal 	PMD_INIT_FUNC_TRACE();
1855e79416d1SHemant Agrawal 
1856c3e85bdcSAkhil Goyal 	dpaa_sec_session *s = (dpaa_sec_session *)sess_priv;
1857c3e85bdcSAkhil Goyal 
1858c3e85bdcSAkhil Goyal 	if (sess_priv) {
1859e79416d1SHemant Agrawal 		struct rte_mempool *sess_mp = rte_mempool_from_obj(sess_priv);
1860e79416d1SHemant Agrawal 
1861e79416d1SHemant Agrawal 		if (s->inq)
1862e79416d1SHemant Agrawal 			dpaa_sec_detach_rxq(qi, s->inq);
1863c3e85bdcSAkhil Goyal 		rte_free(s->cipher_key.data);
1864c3e85bdcSAkhil Goyal 		rte_free(s->auth_key.data);
1865c3e85bdcSAkhil Goyal 		memset(s, 0, sizeof(dpaa_sec_session));
1866012c5076SPablo de Lara 		set_sym_session_private_data(sess, index, NULL);
1867c3e85bdcSAkhil Goyal 		rte_mempool_put(sess_mp, sess_priv);
1868c3e85bdcSAkhil Goyal 	}
1869c3e85bdcSAkhil Goyal }
1870c3e85bdcSAkhil Goyal 
1871c3e85bdcSAkhil Goyal static int
18721f14d500SAkhil Goyal dpaa_sec_set_ipsec_session(__rte_unused struct rte_cryptodev *dev,
18731f14d500SAkhil Goyal 			   struct rte_security_session_conf *conf,
18741f14d500SAkhil Goyal 			   void *sess)
18751f14d500SAkhil Goyal {
18761f14d500SAkhil Goyal 	struct dpaa_sec_dev_private *internals = dev->data->dev_private;
18771f14d500SAkhil Goyal 	struct rte_security_ipsec_xform *ipsec_xform = &conf->ipsec;
18781f14d500SAkhil Goyal 	struct rte_crypto_auth_xform *auth_xform;
18791f14d500SAkhil Goyal 	struct rte_crypto_cipher_xform *cipher_xform;
18801f14d500SAkhil Goyal 	dpaa_sec_session *session = (dpaa_sec_session *)sess;
18811f14d500SAkhil Goyal 
18821f14d500SAkhil Goyal 	PMD_INIT_FUNC_TRACE();
18831f14d500SAkhil Goyal 
18841f14d500SAkhil Goyal 	if (ipsec_xform->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
18851f14d500SAkhil Goyal 		cipher_xform = &conf->crypto_xform->cipher;
18861f14d500SAkhil Goyal 		auth_xform = &conf->crypto_xform->next->auth;
18871f14d500SAkhil Goyal 	} else {
18881f14d500SAkhil Goyal 		auth_xform = &conf->crypto_xform->auth;
18891f14d500SAkhil Goyal 		cipher_xform = &conf->crypto_xform->next->cipher;
18901f14d500SAkhil Goyal 	}
18911f14d500SAkhil Goyal 	session->proto_alg = conf->protocol;
18921f14d500SAkhil Goyal 	session->cipher_key.data = rte_zmalloc(NULL,
18931f14d500SAkhil Goyal 					       cipher_xform->key.length,
18941f14d500SAkhil Goyal 					       RTE_CACHE_LINE_SIZE);
18951f14d500SAkhil Goyal 	if (session->cipher_key.data == NULL &&
18961f14d500SAkhil Goyal 			cipher_xform->key.length > 0) {
1897f163231eSHemant Agrawal 		DPAA_SEC_ERR("No Memory for cipher key");
18981f14d500SAkhil Goyal 		return -ENOMEM;
18991f14d500SAkhil Goyal 	}
19001f14d500SAkhil Goyal 
19011f14d500SAkhil Goyal 	session->cipher_key.length = cipher_xform->key.length;
19021f14d500SAkhil Goyal 	session->auth_key.data = rte_zmalloc(NULL,
19031f14d500SAkhil Goyal 					auth_xform->key.length,
19041f14d500SAkhil Goyal 					RTE_CACHE_LINE_SIZE);
19051f14d500SAkhil Goyal 	if (session->auth_key.data == NULL &&
19061f14d500SAkhil Goyal 			auth_xform->key.length > 0) {
1907f163231eSHemant Agrawal 		DPAA_SEC_ERR("No Memory for auth key");
19081f14d500SAkhil Goyal 		rte_free(session->cipher_key.data);
19091f14d500SAkhil Goyal 		return -ENOMEM;
19101f14d500SAkhil Goyal 	}
19111f14d500SAkhil Goyal 	session->auth_key.length = auth_xform->key.length;
19121f14d500SAkhil Goyal 	memcpy(session->cipher_key.data, cipher_xform->key.data,
19131f14d500SAkhil Goyal 			cipher_xform->key.length);
19141f14d500SAkhil Goyal 	memcpy(session->auth_key.data, auth_xform->key.data,
19151f14d500SAkhil Goyal 			auth_xform->key.length);
19161f14d500SAkhil Goyal 
19171f14d500SAkhil Goyal 	switch (auth_xform->algo) {
19181f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA1_HMAC:
19191f14d500SAkhil Goyal 		session->auth_alg = RTE_CRYPTO_AUTH_SHA1_HMAC;
19201f14d500SAkhil Goyal 		break;
19211f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_MD5_HMAC:
19221f14d500SAkhil Goyal 		session->auth_alg = RTE_CRYPTO_AUTH_MD5_HMAC;
19231f14d500SAkhil Goyal 		break;
19241f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA256_HMAC:
19251f14d500SAkhil Goyal 		session->auth_alg = RTE_CRYPTO_AUTH_SHA256_HMAC;
19261f14d500SAkhil Goyal 		break;
19271f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA384_HMAC:
19281f14d500SAkhil Goyal 		session->auth_alg = RTE_CRYPTO_AUTH_SHA384_HMAC;
19291f14d500SAkhil Goyal 		break;
19301f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA512_HMAC:
19311f14d500SAkhil Goyal 		session->auth_alg = RTE_CRYPTO_AUTH_SHA512_HMAC;
19321f14d500SAkhil Goyal 		break;
19331f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_AES_CMAC:
19341f14d500SAkhil Goyal 		session->auth_alg = RTE_CRYPTO_AUTH_AES_CMAC;
19351f14d500SAkhil Goyal 		break;
19361f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_NULL:
19371f14d500SAkhil Goyal 		session->auth_alg = RTE_CRYPTO_AUTH_NULL;
19381f14d500SAkhil Goyal 		break;
19391f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA224_HMAC:
19401f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_AES_XCBC_MAC:
19411f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
19421f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA1:
19431f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA256:
19441f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA512:
19451f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA224:
19461f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA384:
19471f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_MD5:
19481f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_AES_GMAC:
19491f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_KASUMI_F9:
19501f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_AES_CBC_MAC:
19511f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_ZUC_EIA3:
1952f163231eSHemant Agrawal 		DPAA_SEC_ERR("Crypto: Unsupported auth alg %u",
19531f14d500SAkhil Goyal 			auth_xform->algo);
19541f14d500SAkhil Goyal 		goto out;
19551f14d500SAkhil Goyal 	default:
1956f163231eSHemant Agrawal 		DPAA_SEC_ERR("Crypto: Undefined Auth specified %u",
19571f14d500SAkhil Goyal 			auth_xform->algo);
19581f14d500SAkhil Goyal 		goto out;
19591f14d500SAkhil Goyal 	}
19601f14d500SAkhil Goyal 
19611f14d500SAkhil Goyal 	switch (cipher_xform->algo) {
19621f14d500SAkhil Goyal 	case RTE_CRYPTO_CIPHER_AES_CBC:
19631f14d500SAkhil Goyal 		session->cipher_alg = RTE_CRYPTO_CIPHER_AES_CBC;
19641f14d500SAkhil Goyal 		break;
19651f14d500SAkhil Goyal 	case RTE_CRYPTO_CIPHER_3DES_CBC:
19661f14d500SAkhil Goyal 		session->cipher_alg = RTE_CRYPTO_CIPHER_3DES_CBC;
19671f14d500SAkhil Goyal 		break;
19681f14d500SAkhil Goyal 	case RTE_CRYPTO_CIPHER_AES_CTR:
19691f14d500SAkhil Goyal 		session->cipher_alg = RTE_CRYPTO_CIPHER_AES_CTR;
19701f14d500SAkhil Goyal 		break;
19711f14d500SAkhil Goyal 	case RTE_CRYPTO_CIPHER_NULL:
19721f14d500SAkhil Goyal 	case RTE_CRYPTO_CIPHER_SNOW3G_UEA2:
19731f14d500SAkhil Goyal 	case RTE_CRYPTO_CIPHER_3DES_ECB:
19741f14d500SAkhil Goyal 	case RTE_CRYPTO_CIPHER_AES_ECB:
19751f14d500SAkhil Goyal 	case RTE_CRYPTO_CIPHER_KASUMI_F8:
1976f163231eSHemant Agrawal 		DPAA_SEC_ERR("Crypto: Unsupported Cipher alg %u",
19771f14d500SAkhil Goyal 			cipher_xform->algo);
19781f14d500SAkhil Goyal 		goto out;
19791f14d500SAkhil Goyal 	default:
1980f163231eSHemant Agrawal 		DPAA_SEC_ERR("Crypto: Undefined Cipher specified %u",
19811f14d500SAkhil Goyal 			cipher_xform->algo);
19821f14d500SAkhil Goyal 		goto out;
19831f14d500SAkhil Goyal 	}
19841f14d500SAkhil Goyal 
19851f14d500SAkhil Goyal 	if (ipsec_xform->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
19861f14d500SAkhil Goyal 		memset(&session->encap_pdb, 0, sizeof(struct ipsec_encap_pdb) +
19871f14d500SAkhil Goyal 				sizeof(session->ip4_hdr));
19881f14d500SAkhil Goyal 		session->ip4_hdr.ip_v = IPVERSION;
19891f14d500SAkhil Goyal 		session->ip4_hdr.ip_hl = 5;
19901f14d500SAkhil Goyal 		session->ip4_hdr.ip_len = rte_cpu_to_be_16(
19911f14d500SAkhil Goyal 						sizeof(session->ip4_hdr));
19921f14d500SAkhil Goyal 		session->ip4_hdr.ip_tos = ipsec_xform->tunnel.ipv4.dscp;
19931f14d500SAkhil Goyal 		session->ip4_hdr.ip_id = 0;
19941f14d500SAkhil Goyal 		session->ip4_hdr.ip_off = 0;
19951f14d500SAkhil Goyal 		session->ip4_hdr.ip_ttl = ipsec_xform->tunnel.ipv4.ttl;
19961f14d500SAkhil Goyal 		session->ip4_hdr.ip_p = (ipsec_xform->proto ==
19971f14d500SAkhil Goyal 				RTE_SECURITY_IPSEC_SA_PROTO_ESP) ? IPPROTO_ESP
19981f14d500SAkhil Goyal 				: IPPROTO_AH;
19991f14d500SAkhil Goyal 		session->ip4_hdr.ip_sum = 0;
20001f14d500SAkhil Goyal 		session->ip4_hdr.ip_src = ipsec_xform->tunnel.ipv4.src_ip;
20011f14d500SAkhil Goyal 		session->ip4_hdr.ip_dst = ipsec_xform->tunnel.ipv4.dst_ip;
20021f14d500SAkhil Goyal 		session->ip4_hdr.ip_sum = calc_chksum((uint16_t *)
20031f14d500SAkhil Goyal 						(void *)&session->ip4_hdr,
20041f14d500SAkhil Goyal 						sizeof(struct ip));
20051f14d500SAkhil Goyal 
20061f14d500SAkhil Goyal 		session->encap_pdb.options =
20071f14d500SAkhil Goyal 			(IPVERSION << PDBNH_ESP_ENCAP_SHIFT) |
20081f14d500SAkhil Goyal 			PDBOPTS_ESP_OIHI_PDB_INL |
20091f14d500SAkhil Goyal 			PDBOPTS_ESP_IVSRC |
20101f14d500SAkhil Goyal 			PDBHMO_ESP_ENCAP_DTTL;
20111f14d500SAkhil Goyal 		session->encap_pdb.spi = ipsec_xform->spi;
20121f14d500SAkhil Goyal 		session->encap_pdb.ip_hdr_len = sizeof(struct ip);
20131f14d500SAkhil Goyal 
20141f14d500SAkhil Goyal 		session->dir = DIR_ENC;
20151f14d500SAkhil Goyal 	} else if (ipsec_xform->direction ==
20161f14d500SAkhil Goyal 			RTE_SECURITY_IPSEC_SA_DIR_INGRESS) {
20171f14d500SAkhil Goyal 		memset(&session->decap_pdb, 0, sizeof(struct ipsec_decap_pdb));
20181f14d500SAkhil Goyal 		session->decap_pdb.options = sizeof(struct ip) << 16;
20191f14d500SAkhil Goyal 		session->dir = DIR_DEC;
20201f14d500SAkhil Goyal 	} else
20211f14d500SAkhil Goyal 		goto out;
20221f14d500SAkhil Goyal 	session->ctx_pool = internals->ctx_pool;
20231f14d500SAkhil Goyal 	session->inq = dpaa_sec_attach_rxq(internals);
20241f14d500SAkhil Goyal 	if (session->inq == NULL) {
2025f163231eSHemant Agrawal 		DPAA_SEC_ERR("unable to attach sec queue");
20261f14d500SAkhil Goyal 		goto out;
20271f14d500SAkhil Goyal 	}
20281f14d500SAkhil Goyal 
20291f14d500SAkhil Goyal 
20301f14d500SAkhil Goyal 	return 0;
20311f14d500SAkhil Goyal out:
20321f14d500SAkhil Goyal 	rte_free(session->auth_key.data);
20331f14d500SAkhil Goyal 	rte_free(session->cipher_key.data);
20341f14d500SAkhil Goyal 	memset(session, 0, sizeof(dpaa_sec_session));
20351f14d500SAkhil Goyal 	return -1;
20361f14d500SAkhil Goyal }
20371f14d500SAkhil Goyal 
20381f14d500SAkhil Goyal static int
20391f14d500SAkhil Goyal dpaa_sec_security_session_create(void *dev,
20401f14d500SAkhil Goyal 				 struct rte_security_session_conf *conf,
20411f14d500SAkhil Goyal 				 struct rte_security_session *sess,
20421f14d500SAkhil Goyal 				 struct rte_mempool *mempool)
20431f14d500SAkhil Goyal {
20441f14d500SAkhil Goyal 	void *sess_private_data;
20451f14d500SAkhil Goyal 	struct rte_cryptodev *cdev = (struct rte_cryptodev *)dev;
20461f14d500SAkhil Goyal 	int ret;
20471f14d500SAkhil Goyal 
20481f14d500SAkhil Goyal 	if (rte_mempool_get(mempool, &sess_private_data)) {
2049f163231eSHemant Agrawal 		DPAA_SEC_ERR("Couldn't get object from session mempool");
20501f14d500SAkhil Goyal 		return -ENOMEM;
20511f14d500SAkhil Goyal 	}
20521f14d500SAkhil Goyal 
20531f14d500SAkhil Goyal 	switch (conf->protocol) {
20541f14d500SAkhil Goyal 	case RTE_SECURITY_PROTOCOL_IPSEC:
20551f14d500SAkhil Goyal 		ret = dpaa_sec_set_ipsec_session(cdev, conf,
20561f14d500SAkhil Goyal 				sess_private_data);
20571f14d500SAkhil Goyal 		break;
20581f14d500SAkhil Goyal 	case RTE_SECURITY_PROTOCOL_MACSEC:
20591f14d500SAkhil Goyal 		return -ENOTSUP;
20601f14d500SAkhil Goyal 	default:
20611f14d500SAkhil Goyal 		return -EINVAL;
20621f14d500SAkhil Goyal 	}
20631f14d500SAkhil Goyal 	if (ret != 0) {
2064f163231eSHemant Agrawal 		DPAA_SEC_ERR("failed to configure session parameters");
20651f14d500SAkhil Goyal 		/* Return session to mempool */
20661f14d500SAkhil Goyal 		rte_mempool_put(mempool, sess_private_data);
20671f14d500SAkhil Goyal 		return ret;
20681f14d500SAkhil Goyal 	}
20691f14d500SAkhil Goyal 
20701f14d500SAkhil Goyal 	set_sec_session_private_data(sess, sess_private_data);
20711f14d500SAkhil Goyal 
20721f14d500SAkhil Goyal 	return ret;
20731f14d500SAkhil Goyal }
20741f14d500SAkhil Goyal 
20751f14d500SAkhil Goyal /** Clear the memory of session so it doesn't leave key material behind */
20761f14d500SAkhil Goyal static int
20771f14d500SAkhil Goyal dpaa_sec_security_session_destroy(void *dev __rte_unused,
20781f14d500SAkhil Goyal 		struct rte_security_session *sess)
20791f14d500SAkhil Goyal {
20801f14d500SAkhil Goyal 	PMD_INIT_FUNC_TRACE();
20811f14d500SAkhil Goyal 	void *sess_priv = get_sec_session_private_data(sess);
20821f14d500SAkhil Goyal 
20831f14d500SAkhil Goyal 	dpaa_sec_session *s = (dpaa_sec_session *)sess_priv;
20841f14d500SAkhil Goyal 
20851f14d500SAkhil Goyal 	if (sess_priv) {
20861f14d500SAkhil Goyal 		struct rte_mempool *sess_mp = rte_mempool_from_obj(sess_priv);
20871f14d500SAkhil Goyal 
20881f14d500SAkhil Goyal 		rte_free(s->cipher_key.data);
20891f14d500SAkhil Goyal 		rte_free(s->auth_key.data);
20901f14d500SAkhil Goyal 		memset(sess, 0, sizeof(dpaa_sec_session));
20911f14d500SAkhil Goyal 		set_sec_session_private_data(sess, NULL);
20921f14d500SAkhil Goyal 		rte_mempool_put(sess_mp, sess_priv);
20931f14d500SAkhil Goyal 	}
20941f14d500SAkhil Goyal 	return 0;
20951f14d500SAkhil Goyal }
20961f14d500SAkhil Goyal 
20971f14d500SAkhil Goyal 
20981f14d500SAkhil Goyal static int
20997e3e2954SAkhil Goyal dpaa_sec_dev_configure(struct rte_cryptodev *dev,
2100c3e85bdcSAkhil Goyal 		       struct rte_cryptodev_config *config __rte_unused)
2101c3e85bdcSAkhil Goyal {
21027e3e2954SAkhil Goyal 
21037e3e2954SAkhil Goyal 	char str[20];
21047e3e2954SAkhil Goyal 	struct dpaa_sec_dev_private *internals;
21057e3e2954SAkhil Goyal 
2106c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2107c3e85bdcSAkhil Goyal 
21087e3e2954SAkhil Goyal 	internals = dev->data->dev_private;
21097e3e2954SAkhil Goyal 	sprintf(str, "ctx_pool_%d", dev->data->dev_id);
21107e3e2954SAkhil Goyal 	if (!internals->ctx_pool) {
21117e3e2954SAkhil Goyal 		internals->ctx_pool = rte_mempool_create((const char *)str,
21127e3e2954SAkhil Goyal 							CTX_POOL_NUM_BUFS,
21137e3e2954SAkhil Goyal 							CTX_POOL_BUF_SIZE,
21147e3e2954SAkhil Goyal 							CTX_POOL_CACHE_SIZE, 0,
21157e3e2954SAkhil Goyal 							NULL, NULL, NULL, NULL,
21167e3e2954SAkhil Goyal 							SOCKET_ID_ANY, 0);
21177e3e2954SAkhil Goyal 		if (!internals->ctx_pool) {
2118f163231eSHemant Agrawal 			DPAA_SEC_ERR("%s create failed\n", str);
21197e3e2954SAkhil Goyal 			return -ENOMEM;
21207e3e2954SAkhil Goyal 		}
21217e3e2954SAkhil Goyal 	} else
2122f163231eSHemant Agrawal 		DPAA_SEC_INFO("mempool already created for dev_id : %d",
21237e3e2954SAkhil Goyal 				dev->data->dev_id);
21247e3e2954SAkhil Goyal 
2125c3e85bdcSAkhil Goyal 	return 0;
2126c3e85bdcSAkhil Goyal }
2127c3e85bdcSAkhil Goyal 
2128c3e85bdcSAkhil Goyal static int
2129c3e85bdcSAkhil Goyal dpaa_sec_dev_start(struct rte_cryptodev *dev __rte_unused)
2130c3e85bdcSAkhil Goyal {
2131c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2132c3e85bdcSAkhil Goyal 	return 0;
2133c3e85bdcSAkhil Goyal }
2134c3e85bdcSAkhil Goyal 
2135c3e85bdcSAkhil Goyal static void
2136c3e85bdcSAkhil Goyal dpaa_sec_dev_stop(struct rte_cryptodev *dev __rte_unused)
2137c3e85bdcSAkhil Goyal {
2138c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2139c3e85bdcSAkhil Goyal }
2140c3e85bdcSAkhil Goyal 
2141c3e85bdcSAkhil Goyal static int
21427e3e2954SAkhil Goyal dpaa_sec_dev_close(struct rte_cryptodev *dev)
2143c3e85bdcSAkhil Goyal {
21447e3e2954SAkhil Goyal 	struct dpaa_sec_dev_private *internals;
21457e3e2954SAkhil Goyal 
2146c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
21477e3e2954SAkhil Goyal 
21487e3e2954SAkhil Goyal 	if (dev == NULL)
21497e3e2954SAkhil Goyal 		return -ENOMEM;
21507e3e2954SAkhil Goyal 
21517e3e2954SAkhil Goyal 	internals = dev->data->dev_private;
21527e3e2954SAkhil Goyal 	rte_mempool_free(internals->ctx_pool);
21537e3e2954SAkhil Goyal 	internals->ctx_pool = NULL;
21547e3e2954SAkhil Goyal 
2155c3e85bdcSAkhil Goyal 	return 0;
2156c3e85bdcSAkhil Goyal }
2157c3e85bdcSAkhil Goyal 
2158c3e85bdcSAkhil Goyal static void
2159c3e85bdcSAkhil Goyal dpaa_sec_dev_infos_get(struct rte_cryptodev *dev,
2160c3e85bdcSAkhil Goyal 		       struct rte_cryptodev_info *info)
2161c3e85bdcSAkhil Goyal {
2162c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals = dev->data->dev_private;
2163c3e85bdcSAkhil Goyal 
2164c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2165c3e85bdcSAkhil Goyal 	if (info != NULL) {
2166c3e85bdcSAkhil Goyal 		info->max_nb_queue_pairs = internals->max_nb_queue_pairs;
2167c3e85bdcSAkhil Goyal 		info->feature_flags = dev->feature_flags;
2168c3e85bdcSAkhil Goyal 		info->capabilities = dpaa_sec_capabilities;
2169c3e85bdcSAkhil Goyal 		info->sym.max_nb_sessions = internals->max_nb_sessions;
2170c3e85bdcSAkhil Goyal 		info->driver_id = cryptodev_driver_id;
2171c3e85bdcSAkhil Goyal 	}
2172c3e85bdcSAkhil Goyal }
2173c3e85bdcSAkhil Goyal 
2174c3e85bdcSAkhil Goyal static struct rte_cryptodev_ops crypto_ops = {
2175c3e85bdcSAkhil Goyal 	.dev_configure	      = dpaa_sec_dev_configure,
2176c3e85bdcSAkhil Goyal 	.dev_start	      = dpaa_sec_dev_start,
2177c3e85bdcSAkhil Goyal 	.dev_stop	      = dpaa_sec_dev_stop,
2178c3e85bdcSAkhil Goyal 	.dev_close	      = dpaa_sec_dev_close,
2179c3e85bdcSAkhil Goyal 	.dev_infos_get        = dpaa_sec_dev_infos_get,
2180c3e85bdcSAkhil Goyal 	.queue_pair_setup     = dpaa_sec_queue_pair_setup,
2181c3e85bdcSAkhil Goyal 	.queue_pair_release   = dpaa_sec_queue_pair_release,
2182c3e85bdcSAkhil Goyal 	.queue_pair_count     = dpaa_sec_queue_pair_count,
2183012c5076SPablo de Lara 	.sym_session_get_size     = dpaa_sec_sym_session_get_size,
2184012c5076SPablo de Lara 	.sym_session_configure    = dpaa_sec_sym_session_configure,
2185012c5076SPablo de Lara 	.sym_session_clear        = dpaa_sec_sym_session_clear
2186c3e85bdcSAkhil Goyal };
2187c3e85bdcSAkhil Goyal 
21881f14d500SAkhil Goyal static const struct rte_security_capability *
21891f14d500SAkhil Goyal dpaa_sec_capabilities_get(void *device __rte_unused)
21901f14d500SAkhil Goyal {
21911f14d500SAkhil Goyal 	return dpaa_sec_security_cap;
21921f14d500SAkhil Goyal }
21931f14d500SAkhil Goyal 
21941f14d500SAkhil Goyal struct rte_security_ops dpaa_sec_security_ops = {
21951f14d500SAkhil Goyal 	.session_create = dpaa_sec_security_session_create,
21961f14d500SAkhil Goyal 	.session_update = NULL,
21971f14d500SAkhil Goyal 	.session_stats_get = NULL,
21981f14d500SAkhil Goyal 	.session_destroy = dpaa_sec_security_session_destroy,
21991f14d500SAkhil Goyal 	.set_pkt_metadata = NULL,
22001f14d500SAkhil Goyal 	.capabilities_get = dpaa_sec_capabilities_get
22011f14d500SAkhil Goyal };
22021f14d500SAkhil Goyal 
2203c3e85bdcSAkhil Goyal static int
2204c3e85bdcSAkhil Goyal dpaa_sec_uninit(struct rte_cryptodev *dev)
2205c3e85bdcSAkhil Goyal {
2206debef417SShreyansh Jain 	struct dpaa_sec_dev_private *internals;
2207c3e85bdcSAkhil Goyal 
2208c3e85bdcSAkhil Goyal 	if (dev == NULL)
2209c3e85bdcSAkhil Goyal 		return -ENODEV;
2210c3e85bdcSAkhil Goyal 
2211debef417SShreyansh Jain 	internals = dev->data->dev_private;
22121f14d500SAkhil Goyal 	rte_free(dev->security_ctx);
22131f14d500SAkhil Goyal 
22147e3e2954SAkhil Goyal 	/* In case close has been called, internals->ctx_pool would be NULL */
2215c3e85bdcSAkhil Goyal 	rte_mempool_free(internals->ctx_pool);
2216c3e85bdcSAkhil Goyal 	rte_free(internals);
2217c3e85bdcSAkhil Goyal 
2218f163231eSHemant Agrawal 	DPAA_SEC_INFO("Closing DPAA_SEC device %s on numa socket %u",
2219c3e85bdcSAkhil Goyal 		      dev->data->name, rte_socket_id());
2220c3e85bdcSAkhil Goyal 
2221c3e85bdcSAkhil Goyal 	return 0;
2222c3e85bdcSAkhil Goyal }
2223c3e85bdcSAkhil Goyal 
2224c3e85bdcSAkhil Goyal static int
2225c3e85bdcSAkhil Goyal dpaa_sec_dev_init(struct rte_cryptodev *cryptodev)
2226c3e85bdcSAkhil Goyal {
2227c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals;
22281f14d500SAkhil Goyal 	struct rte_security_ctx *security_instance;
2229c3e85bdcSAkhil Goyal 	struct dpaa_sec_qp *qp;
2230e79416d1SHemant Agrawal 	uint32_t i, flags;
2231c3e85bdcSAkhil Goyal 	int ret;
2232c3e85bdcSAkhil Goyal 
2233c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2234c3e85bdcSAkhil Goyal 
2235c3e85bdcSAkhil Goyal 	cryptodev->driver_id = cryptodev_driver_id;
2236c3e85bdcSAkhil Goyal 	cryptodev->dev_ops = &crypto_ops;
2237c3e85bdcSAkhil Goyal 
2238c3e85bdcSAkhil Goyal 	cryptodev->enqueue_burst = dpaa_sec_enqueue_burst;
2239c3e85bdcSAkhil Goyal 	cryptodev->dequeue_burst = dpaa_sec_dequeue_burst;
2240c3e85bdcSAkhil Goyal 	cryptodev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
2241c3e85bdcSAkhil Goyal 			RTE_CRYPTODEV_FF_HW_ACCELERATED |
22421f14d500SAkhil Goyal 			RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
2243a74af788SAkhil Goyal 			RTE_CRYPTODEV_FF_SECURITY |
22442717246eSPablo de Lara 			RTE_CRYPTODEV_FF_IN_PLACE_SGL |
22452717246eSPablo de Lara 			RTE_CRYPTODEV_FF_OOP_SGL_IN_SGL_OUT |
22462717246eSPablo de Lara 			RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT |
22472717246eSPablo de Lara 			RTE_CRYPTODEV_FF_OOP_LB_IN_SGL_OUT |
22482717246eSPablo de Lara 			RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT;
2249c3e85bdcSAkhil Goyal 
2250c3e85bdcSAkhil Goyal 	internals = cryptodev->data->dev_private;
2251e79416d1SHemant Agrawal 	internals->max_nb_queue_pairs = RTE_DPAA_MAX_NB_SEC_QPS;
2252c3e85bdcSAkhil Goyal 	internals->max_nb_sessions = RTE_DPAA_SEC_PMD_MAX_NB_SESSIONS;
2253c3e85bdcSAkhil Goyal 
22541f14d500SAkhil Goyal 	/*
22551f14d500SAkhil Goyal 	 * For secondary processes, we don't initialise any further as primary
22561f14d500SAkhil Goyal 	 * has already done this work. Only check we don't need a different
22571f14d500SAkhil Goyal 	 * RX function
22581f14d500SAkhil Goyal 	 */
22591f14d500SAkhil Goyal 	if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
2260f163231eSHemant Agrawal 		DPAA_SEC_WARN("Device already init by primary process");
22611f14d500SAkhil Goyal 		return 0;
22621f14d500SAkhil Goyal 	}
22631f14d500SAkhil Goyal 
22641f14d500SAkhil Goyal 	/* Initialize security_ctx only for primary process*/
22651f14d500SAkhil Goyal 	security_instance = rte_malloc("rte_security_instances_ops",
22661f14d500SAkhil Goyal 				sizeof(struct rte_security_ctx), 0);
22671f14d500SAkhil Goyal 	if (security_instance == NULL)
22681f14d500SAkhil Goyal 		return -ENOMEM;
22691f14d500SAkhil Goyal 	security_instance->device = (void *)cryptodev;
22701f14d500SAkhil Goyal 	security_instance->ops = &dpaa_sec_security_ops;
22711f14d500SAkhil Goyal 	security_instance->sess_cnt = 0;
22721f14d500SAkhil Goyal 	cryptodev->security_ctx = security_instance;
22731f14d500SAkhil Goyal 
2274c3e85bdcSAkhil Goyal 	for (i = 0; i < internals->max_nb_queue_pairs; i++) {
2275c3e85bdcSAkhil Goyal 		/* init qman fq for queue pair */
2276c3e85bdcSAkhil Goyal 		qp = &internals->qps[i];
2277c3e85bdcSAkhil Goyal 		ret = dpaa_sec_init_tx(&qp->outq);
2278c3e85bdcSAkhil Goyal 		if (ret) {
2279f163231eSHemant Agrawal 			DPAA_SEC_ERR("config tx of queue pair  %d", i);
2280c3e85bdcSAkhil Goyal 			goto init_error;
2281c3e85bdcSAkhil Goyal 		}
2282e79416d1SHemant Agrawal 	}
2283e79416d1SHemant Agrawal 
2284e79416d1SHemant Agrawal 	flags = QMAN_FQ_FLAG_LOCKED | QMAN_FQ_FLAG_DYNAMIC_FQID |
2285e79416d1SHemant Agrawal 		QMAN_FQ_FLAG_TO_DCPORTAL;
2286e79416d1SHemant Agrawal 	for (i = 0; i < internals->max_nb_sessions; i++) {
2287e79416d1SHemant Agrawal 		/* create rx qman fq for sessions*/
2288e79416d1SHemant Agrawal 		ret = qman_create_fq(0, flags, &internals->inq[i]);
2289e79416d1SHemant Agrawal 		if (unlikely(ret != 0)) {
2290f163231eSHemant Agrawal 			DPAA_SEC_ERR("sec qman_create_fq failed");
2291c3e85bdcSAkhil Goyal 			goto init_error;
2292c3e85bdcSAkhil Goyal 		}
2293c3e85bdcSAkhil Goyal 	}
2294c3e85bdcSAkhil Goyal 
2295f163231eSHemant Agrawal 	RTE_LOG(INFO, PMD, "%s cryptodev init\n", cryptodev->data->name);
2296c3e85bdcSAkhil Goyal 	return 0;
2297c3e85bdcSAkhil Goyal 
2298c3e85bdcSAkhil Goyal init_error:
2299f163231eSHemant Agrawal 	DPAA_SEC_ERR("driver %s: create failed\n", cryptodev->data->name);
2300c3e85bdcSAkhil Goyal 
2301c3e85bdcSAkhil Goyal 	dpaa_sec_uninit(cryptodev);
2302c3e85bdcSAkhil Goyal 	return -EFAULT;
2303c3e85bdcSAkhil Goyal }
2304c3e85bdcSAkhil Goyal 
2305c3e85bdcSAkhil Goyal static int
2306c3e85bdcSAkhil Goyal cryptodev_dpaa_sec_probe(struct rte_dpaa_driver *dpaa_drv,
2307c3e85bdcSAkhil Goyal 				struct rte_dpaa_device *dpaa_dev)
2308c3e85bdcSAkhil Goyal {
2309c3e85bdcSAkhil Goyal 	struct rte_cryptodev *cryptodev;
2310c3e85bdcSAkhil Goyal 	char cryptodev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
2311c3e85bdcSAkhil Goyal 
2312c3e85bdcSAkhil Goyal 	int retval;
2313c3e85bdcSAkhil Goyal 
2314c3e85bdcSAkhil Goyal 	sprintf(cryptodev_name, "dpaa_sec-%d", dpaa_dev->id.dev_id);
2315c3e85bdcSAkhil Goyal 
2316c3e85bdcSAkhil Goyal 	cryptodev = rte_cryptodev_pmd_allocate(cryptodev_name, rte_socket_id());
2317c3e85bdcSAkhil Goyal 	if (cryptodev == NULL)
2318c3e85bdcSAkhil Goyal 		return -ENOMEM;
2319c3e85bdcSAkhil Goyal 
2320c3e85bdcSAkhil Goyal 	if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
2321c3e85bdcSAkhil Goyal 		cryptodev->data->dev_private = rte_zmalloc_socket(
2322c3e85bdcSAkhil Goyal 					"cryptodev private structure",
2323c3e85bdcSAkhil Goyal 					sizeof(struct dpaa_sec_dev_private),
2324c3e85bdcSAkhil Goyal 					RTE_CACHE_LINE_SIZE,
2325c3e85bdcSAkhil Goyal 					rte_socket_id());
2326c3e85bdcSAkhil Goyal 
2327c3e85bdcSAkhil Goyal 		if (cryptodev->data->dev_private == NULL)
2328c3e85bdcSAkhil Goyal 			rte_panic("Cannot allocate memzone for private "
2329c3e85bdcSAkhil Goyal 					"device data");
2330c3e85bdcSAkhil Goyal 	}
2331c3e85bdcSAkhil Goyal 
2332c3e85bdcSAkhil Goyal 	dpaa_dev->crypto_dev = cryptodev;
2333c3e85bdcSAkhil Goyal 	cryptodev->device = &dpaa_dev->device;
2334c3e85bdcSAkhil Goyal 	cryptodev->device->driver = &dpaa_drv->driver;
2335c3e85bdcSAkhil Goyal 
2336c3e85bdcSAkhil Goyal 	/* init user callbacks */
2337c3e85bdcSAkhil Goyal 	TAILQ_INIT(&(cryptodev->link_intr_cbs));
2338c3e85bdcSAkhil Goyal 
2339c3e85bdcSAkhil Goyal 	/* if sec device version is not configured */
2340c3e85bdcSAkhil Goyal 	if (!rta_get_sec_era()) {
2341c3e85bdcSAkhil Goyal 		const struct device_node *caam_node;
2342c3e85bdcSAkhil Goyal 
2343c3e85bdcSAkhil Goyal 		for_each_compatible_node(caam_node, NULL, "fsl,sec-v4.0") {
2344c3e85bdcSAkhil Goyal 			const uint32_t *prop = of_get_property(caam_node,
2345c3e85bdcSAkhil Goyal 					"fsl,sec-era",
2346c3e85bdcSAkhil Goyal 					NULL);
2347c3e85bdcSAkhil Goyal 			if (prop) {
2348c3e85bdcSAkhil Goyal 				rta_set_sec_era(
2349c3e85bdcSAkhil Goyal 					INTL_SEC_ERA(rte_cpu_to_be_32(*prop)));
2350c3e85bdcSAkhil Goyal 				break;
2351c3e85bdcSAkhil Goyal 			}
2352c3e85bdcSAkhil Goyal 		}
2353c3e85bdcSAkhil Goyal 	}
2354c3e85bdcSAkhil Goyal 
2355c3e85bdcSAkhil Goyal 	/* Invoke PMD device initialization function */
2356c3e85bdcSAkhil Goyal 	retval = dpaa_sec_dev_init(cryptodev);
2357c3e85bdcSAkhil Goyal 	if (retval == 0)
2358c3e85bdcSAkhil Goyal 		return 0;
2359c3e85bdcSAkhil Goyal 
2360c3e85bdcSAkhil Goyal 	/* In case of error, cleanup is done */
2361c3e85bdcSAkhil Goyal 	if (rte_eal_process_type() == RTE_PROC_PRIMARY)
2362c3e85bdcSAkhil Goyal 		rte_free(cryptodev->data->dev_private);
2363c3e85bdcSAkhil Goyal 
2364c3e85bdcSAkhil Goyal 	rte_cryptodev_pmd_release_device(cryptodev);
2365c3e85bdcSAkhil Goyal 
2366c3e85bdcSAkhil Goyal 	return -ENXIO;
2367c3e85bdcSAkhil Goyal }
2368c3e85bdcSAkhil Goyal 
2369c3e85bdcSAkhil Goyal static int
2370c3e85bdcSAkhil Goyal cryptodev_dpaa_sec_remove(struct rte_dpaa_device *dpaa_dev)
2371c3e85bdcSAkhil Goyal {
2372c3e85bdcSAkhil Goyal 	struct rte_cryptodev *cryptodev;
2373c3e85bdcSAkhil Goyal 	int ret;
2374c3e85bdcSAkhil Goyal 
2375c3e85bdcSAkhil Goyal 	cryptodev = dpaa_dev->crypto_dev;
2376c3e85bdcSAkhil Goyal 	if (cryptodev == NULL)
2377c3e85bdcSAkhil Goyal 		return -ENODEV;
2378c3e85bdcSAkhil Goyal 
2379c3e85bdcSAkhil Goyal 	ret = dpaa_sec_uninit(cryptodev);
2380c3e85bdcSAkhil Goyal 	if (ret)
2381c3e85bdcSAkhil Goyal 		return ret;
2382c3e85bdcSAkhil Goyal 
2383f2f020d2SDeclan Doherty 	return rte_cryptodev_pmd_destroy(cryptodev);
2384c3e85bdcSAkhil Goyal }
2385c3e85bdcSAkhil Goyal 
2386c3e85bdcSAkhil Goyal static struct rte_dpaa_driver rte_dpaa_sec_driver = {
2387c3e85bdcSAkhil Goyal 	.drv_type = FSL_DPAA_CRYPTO,
2388c3e85bdcSAkhil Goyal 	.driver = {
2389c3e85bdcSAkhil Goyal 		.name = "DPAA SEC PMD"
2390c3e85bdcSAkhil Goyal 	},
2391c3e85bdcSAkhil Goyal 	.probe = cryptodev_dpaa_sec_probe,
2392c3e85bdcSAkhil Goyal 	.remove = cryptodev_dpaa_sec_remove,
2393c3e85bdcSAkhil Goyal };
2394c3e85bdcSAkhil Goyal 
2395c3e85bdcSAkhil Goyal static struct cryptodev_driver dpaa_sec_crypto_drv;
2396c3e85bdcSAkhil Goyal 
2397c3e85bdcSAkhil Goyal RTE_PMD_REGISTER_DPAA(CRYPTODEV_NAME_DPAA_SEC_PMD, rte_dpaa_sec_driver);
2398f737f5ceSFiona Trahe RTE_PMD_REGISTER_CRYPTO_DRIVER(dpaa_sec_crypto_drv, rte_dpaa_sec_driver.driver,
2399c3e85bdcSAkhil Goyal 		cryptodev_driver_id);
2400f163231eSHemant Agrawal 
2401*f8e99896SThomas Monjalon RTE_INIT(dpaa_sec_init_log)
2402f163231eSHemant Agrawal {
2403f163231eSHemant Agrawal 	dpaa_logtype_sec = rte_log_register("pmd.crypto.dpaa");
2404f163231eSHemant Agrawal 	if (dpaa_logtype_sec >= 0)
2405f163231eSHemant Agrawal 		rte_log_set_level(dpaa_logtype_sec, RTE_LOG_NOTICE);
2406f163231eSHemant Agrawal }
2407