xref: /dpdk/drivers/crypto/dpaa_sec/dpaa_sec.c (revision f73d6928e01145d56dbaa53640d9ecb13fc72fa5)
1d81734caSHemant Agrawal /* SPDX-License-Identifier: BSD-3-Clause
2c3e85bdcSAkhil Goyal  *
3c3e85bdcSAkhil Goyal  *   Copyright (c) 2016 Freescale Semiconductor, Inc. All rights reserved.
4e621d970SAkhil Goyal  *   Copyright 2017-2019 NXP
5c3e85bdcSAkhil Goyal  *
6c3e85bdcSAkhil Goyal  */
7c3e85bdcSAkhil Goyal 
8c3e85bdcSAkhil Goyal #include <fcntl.h>
9c3e85bdcSAkhil Goyal #include <unistd.h>
10c3e85bdcSAkhil Goyal #include <sched.h>
11c3e85bdcSAkhil Goyal #include <net/if.h>
12c3e85bdcSAkhil Goyal 
13c3e85bdcSAkhil Goyal #include <rte_byteorder.h>
14c3e85bdcSAkhil Goyal #include <rte_common.h>
15c3e85bdcSAkhil Goyal #include <rte_cryptodev_pmd.h>
16c3e85bdcSAkhil Goyal #include <rte_crypto.h>
17c3e85bdcSAkhil Goyal #include <rte_cryptodev.h>
18314424b6SHemant Agrawal #ifdef RTE_LIBRTE_SECURITY
191f14d500SAkhil Goyal #include <rte_security_driver.h>
20314424b6SHemant Agrawal #endif
21c3e85bdcSAkhil Goyal #include <rte_cycles.h>
22c3e85bdcSAkhil Goyal #include <rte_dev.h>
23c3e85bdcSAkhil Goyal #include <rte_kvargs.h>
24c3e85bdcSAkhil Goyal #include <rte_malloc.h>
25c3e85bdcSAkhil Goyal #include <rte_mbuf.h>
26c3e85bdcSAkhil Goyal #include <rte_memcpy.h>
27c3e85bdcSAkhil Goyal #include <rte_string_fns.h>
283b617ee7SAkhil Goyal #include <rte_spinlock.h>
29c3e85bdcSAkhil Goyal 
30c3e85bdcSAkhil Goyal #include <fsl_usd.h>
31c3e85bdcSAkhil Goyal #include <fsl_qman.h>
328c83f28cSHemant Agrawal #include <dpaa_of.h>
33c3e85bdcSAkhil Goyal 
34c3e85bdcSAkhil Goyal /* RTA header files */
35c0ded849SHemant Agrawal #include <desc/common.h>
36c0ded849SHemant Agrawal #include <desc/algo.h>
37c0ded849SHemant Agrawal #include <desc/ipsec.h>
38c0ded849SHemant Agrawal #include <desc/pdcp.h>
39c3e85bdcSAkhil Goyal 
40c3e85bdcSAkhil Goyal #include <rte_dpaa_bus.h>
41c3e85bdcSAkhil Goyal #include <dpaa_sec.h>
42fe3688baSAkhil Goyal #include <dpaa_sec_event.h>
43c3e85bdcSAkhil Goyal #include <dpaa_sec_log.h>
4412e58429SThierry Herbelot #include <dpaax_iova_table.h>
45c3e85bdcSAkhil Goyal 
46c3e85bdcSAkhil Goyal enum rta_sec_era rta_sec_era;
47c3e85bdcSAkhil Goyal 
48f163231eSHemant Agrawal int dpaa_logtype_sec;
49f163231eSHemant Agrawal 
50c3e85bdcSAkhil Goyal static uint8_t cryptodev_driver_id;
51c3e85bdcSAkhil Goyal 
52c3e85bdcSAkhil Goyal static __thread struct rte_crypto_op **dpaa_sec_ops;
53c3e85bdcSAkhil Goyal static __thread int dpaa_sec_op_nb;
54c3e85bdcSAkhil Goyal 
55e79416d1SHemant Agrawal static int
56e79416d1SHemant Agrawal dpaa_sec_attach_sess_q(struct dpaa_sec_qp *qp, dpaa_sec_session *sess);
57e79416d1SHemant Agrawal 
58c3e85bdcSAkhil Goyal static inline void
59c3e85bdcSAkhil Goyal dpaa_sec_op_ending(struct dpaa_sec_op_ctx *ctx)
60c3e85bdcSAkhil Goyal {
61c3e85bdcSAkhil Goyal 	if (!ctx->fd_status) {
62c3e85bdcSAkhil Goyal 		ctx->op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
63c3e85bdcSAkhil Goyal 	} else {
64f163231eSHemant Agrawal 		DPAA_SEC_DP_WARN("SEC return err: 0x%x", ctx->fd_status);
65c3e85bdcSAkhil Goyal 		ctx->op->status = RTE_CRYPTO_OP_STATUS_ERROR;
66c3e85bdcSAkhil Goyal 	}
67c3e85bdcSAkhil Goyal }
68c3e85bdcSAkhil Goyal 
69c3e85bdcSAkhil Goyal static inline struct dpaa_sec_op_ctx *
70f7a5752eSHemant Agrawal dpaa_sec_alloc_ctx(dpaa_sec_session *ses, int sg_count)
71c3e85bdcSAkhil Goyal {
72c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
73f7a5752eSHemant Agrawal 	int i, retval;
74c3e85bdcSAkhil Goyal 
752ffb940eSAkhil Goyal 	retval = rte_mempool_get(
762ffb940eSAkhil Goyal 			ses->qp[rte_lcore_id() % MAX_DPAA_CORES]->ctx_pool,
772ffb940eSAkhil Goyal 			(void **)(&ctx));
78c3e85bdcSAkhil Goyal 	if (!ctx || retval) {
79f163231eSHemant Agrawal 		DPAA_SEC_DP_WARN("Alloc sec descriptor failed!");
80c3e85bdcSAkhil Goyal 		return NULL;
81c3e85bdcSAkhil Goyal 	}
82c3e85bdcSAkhil Goyal 	/*
83c3e85bdcSAkhil Goyal 	 * Clear SG memory. There are 16 SG entries of 16 Bytes each.
84c3e85bdcSAkhil Goyal 	 * one call to dcbz_64() clear 64 bytes, hence calling it 4 times
85c3e85bdcSAkhil Goyal 	 * to clear all the SG entries. dpaa_sec_alloc_ctx() is called for
86c3e85bdcSAkhil Goyal 	 * each packet, memset is costlier than dcbz_64().
87c3e85bdcSAkhil Goyal 	 */
88f7a5752eSHemant Agrawal 	for (i = 0; i < sg_count && i < MAX_JOB_SG_ENTRIES; i += 4)
89f7a5752eSHemant Agrawal 		dcbz_64(&ctx->job.sg[i]);
90c3e85bdcSAkhil Goyal 
912ffb940eSAkhil Goyal 	ctx->ctx_pool = ses->qp[rte_lcore_id() % MAX_DPAA_CORES]->ctx_pool;
92f7a5752eSHemant Agrawal 	ctx->vtop_offset = (size_t) ctx - rte_mempool_virt2iova(ctx);
93c3e85bdcSAkhil Goyal 
94c3e85bdcSAkhil Goyal 	return ctx;
95c3e85bdcSAkhil Goyal }
96c3e85bdcSAkhil Goyal 
97c4509373SSantosh Shukla static inline rte_iova_t
98c3e85bdcSAkhil Goyal dpaa_mem_vtop(void *vaddr)
99c3e85bdcSAkhil Goyal {
10029f3c9e5SAnatoly Burakov 	const struct rte_memseg *ms;
101c3e85bdcSAkhil Goyal 
10266cc45e2SAnatoly Burakov 	ms = rte_mem_virt2memseg(vaddr, NULL);
10312e58429SThierry Herbelot 	if (ms) {
10412e58429SThierry Herbelot 		dpaax_iova_table_update(ms->iova, ms->addr, ms->len);
10529f3c9e5SAnatoly Burakov 		return ms->iova + RTE_PTR_DIFF(vaddr, ms->addr);
10612e58429SThierry Herbelot 	}
1070e5607e4SHemant Agrawal 	return (size_t)NULL;
108c3e85bdcSAkhil Goyal }
109c3e85bdcSAkhil Goyal 
110c3e85bdcSAkhil Goyal static inline void *
111c4509373SSantosh Shukla dpaa_mem_ptov(rte_iova_t paddr)
112c3e85bdcSAkhil Goyal {
1135a7dbb93SShreyansh Jain 	void *va;
1145a7dbb93SShreyansh Jain 
1155a7dbb93SShreyansh Jain 	va = (void *)dpaax_iova_table_get_va(paddr);
1165a7dbb93SShreyansh Jain 	if (likely(va))
1175a7dbb93SShreyansh Jain 		return va;
1185a7dbb93SShreyansh Jain 
11911d2f002SAnatoly Burakov 	return rte_mem_iova2virt(paddr);
120c3e85bdcSAkhil Goyal }
121c3e85bdcSAkhil Goyal 
122c3e85bdcSAkhil Goyal static void
123c3e85bdcSAkhil Goyal ern_sec_fq_handler(struct qman_portal *qm __rte_unused,
124c3e85bdcSAkhil Goyal 		   struct qman_fq *fq,
125c3e85bdcSAkhil Goyal 		   const struct qm_mr_entry *msg)
126c3e85bdcSAkhil Goyal {
127f163231eSHemant Agrawal 	DPAA_SEC_DP_ERR("sec fq %d error, RC = %x, seqnum = %x\n",
128c3e85bdcSAkhil Goyal 			fq->fqid, msg->ern.rc, msg->ern.seqnum);
129c3e85bdcSAkhil Goyal }
130c3e85bdcSAkhil Goyal 
131c3e85bdcSAkhil Goyal /* initialize the queue with dest chan as caam chan so that
132c3e85bdcSAkhil Goyal  * all the packets in this queue could be dispatched into caam
133c3e85bdcSAkhil Goyal  */
134c3e85bdcSAkhil Goyal static int
135c4509373SSantosh Shukla dpaa_sec_init_rx(struct qman_fq *fq_in, rte_iova_t hwdesc,
136c3e85bdcSAkhil Goyal 		 uint32_t fqid_out)
137c3e85bdcSAkhil Goyal {
138c3e85bdcSAkhil Goyal 	struct qm_mcc_initfq fq_opts;
139c3e85bdcSAkhil Goyal 	uint32_t flags;
140c3e85bdcSAkhil Goyal 	int ret = -1;
141c3e85bdcSAkhil Goyal 
142c3e85bdcSAkhil Goyal 	/* Clear FQ options */
143c3e85bdcSAkhil Goyal 	memset(&fq_opts, 0x00, sizeof(struct qm_mcc_initfq));
144c3e85bdcSAkhil Goyal 
145c3e85bdcSAkhil Goyal 	flags = QMAN_INITFQ_FLAG_SCHED;
146c3e85bdcSAkhil Goyal 	fq_opts.we_mask = QM_INITFQ_WE_DESTWQ | QM_INITFQ_WE_CONTEXTA |
147c3e85bdcSAkhil Goyal 			  QM_INITFQ_WE_CONTEXTB;
148c3e85bdcSAkhil Goyal 
149c3e85bdcSAkhil Goyal 	qm_fqd_context_a_set64(&fq_opts.fqd, hwdesc);
150c3e85bdcSAkhil Goyal 	fq_opts.fqd.context_b = fqid_out;
151c3e85bdcSAkhil Goyal 	fq_opts.fqd.dest.channel = qm_channel_caam;
152c3e85bdcSAkhil Goyal 	fq_opts.fqd.dest.wq = 0;
153c3e85bdcSAkhil Goyal 
154c3e85bdcSAkhil Goyal 	fq_in->cb.ern  = ern_sec_fq_handler;
155c3e85bdcSAkhil Goyal 
156f163231eSHemant Agrawal 	DPAA_SEC_DEBUG("in-%x out-%x", fq_in->fqid, fqid_out);
157e79416d1SHemant Agrawal 
158c3e85bdcSAkhil Goyal 	ret = qman_init_fq(fq_in, flags, &fq_opts);
159c3e85bdcSAkhil Goyal 	if (unlikely(ret != 0))
160f163231eSHemant Agrawal 		DPAA_SEC_ERR("qman_init_fq failed %d", ret);
161c3e85bdcSAkhil Goyal 
162c3e85bdcSAkhil Goyal 	return ret;
163c3e85bdcSAkhil Goyal }
164c3e85bdcSAkhil Goyal 
165c3e85bdcSAkhil Goyal /* something is put into in_fq and caam put the crypto result into out_fq */
166c3e85bdcSAkhil Goyal static enum qman_cb_dqrr_result
167c3e85bdcSAkhil Goyal dqrr_out_fq_cb_rx(struct qman_portal *qm __always_unused,
168c3e85bdcSAkhil Goyal 		  struct qman_fq *fq __always_unused,
169c3e85bdcSAkhil Goyal 		  const struct qm_dqrr_entry *dqrr)
170c3e85bdcSAkhil Goyal {
171c3e85bdcSAkhil Goyal 	const struct qm_fd *fd;
172c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *job;
173c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
174c3e85bdcSAkhil Goyal 
175c3e85bdcSAkhil Goyal 	if (dpaa_sec_op_nb >= DPAA_SEC_BURST)
176c3e85bdcSAkhil Goyal 		return qman_cb_dqrr_defer;
177c3e85bdcSAkhil Goyal 
178c3e85bdcSAkhil Goyal 	if (!(dqrr->stat & QM_DQRR_STAT_FD_VALID))
179c3e85bdcSAkhil Goyal 		return qman_cb_dqrr_consume;
180c3e85bdcSAkhil Goyal 
181c3e85bdcSAkhil Goyal 	fd = &dqrr->fd;
182c3e85bdcSAkhil Goyal 	/* sg is embedded in an op ctx,
183c3e85bdcSAkhil Goyal 	 * sg[0] is for output
184c3e85bdcSAkhil Goyal 	 * sg[1] for input
185c3e85bdcSAkhil Goyal 	 */
186c3e85bdcSAkhil Goyal 	job = dpaa_mem_ptov(qm_fd_addr_get64(fd));
1871f14d500SAkhil Goyal 
188c3e85bdcSAkhil Goyal 	ctx = container_of(job, struct dpaa_sec_op_ctx, job);
189c3e85bdcSAkhil Goyal 	ctx->fd_status = fd->status;
1901f14d500SAkhil Goyal 	if (ctx->op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
1911f14d500SAkhil Goyal 		struct qm_sg_entry *sg_out;
1921f14d500SAkhil Goyal 		uint32_t len;
193fb5c100aSAkhil Goyal 		struct rte_mbuf *mbuf = (ctx->op->sym->m_dst == NULL) ?
194fb5c100aSAkhil Goyal 				ctx->op->sym->m_src : ctx->op->sym->m_dst;
1951f14d500SAkhil Goyal 
1961f14d500SAkhil Goyal 		sg_out = &job->sg[0];
1971f14d500SAkhil Goyal 		hw_sg_to_cpu(sg_out);
1981f14d500SAkhil Goyal 		len = sg_out->length;
199fb5c100aSAkhil Goyal 		mbuf->pkt_len = len;
200fb5c100aSAkhil Goyal 		while (mbuf->next != NULL) {
201fb5c100aSAkhil Goyal 			len -= mbuf->data_len;
202fb5c100aSAkhil Goyal 			mbuf = mbuf->next;
203fb5c100aSAkhil Goyal 		}
204fb5c100aSAkhil Goyal 		mbuf->data_len = len;
2051f14d500SAkhil Goyal 	}
206c3e85bdcSAkhil Goyal 	dpaa_sec_ops[dpaa_sec_op_nb++] = ctx->op;
207c3e85bdcSAkhil Goyal 	dpaa_sec_op_ending(ctx);
208c3e85bdcSAkhil Goyal 
209c3e85bdcSAkhil Goyal 	return qman_cb_dqrr_consume;
210c3e85bdcSAkhil Goyal }
211c3e85bdcSAkhil Goyal 
212c3e85bdcSAkhil Goyal /* caam result is put into this queue */
213c3e85bdcSAkhil Goyal static int
214c3e85bdcSAkhil Goyal dpaa_sec_init_tx(struct qman_fq *fq)
215c3e85bdcSAkhil Goyal {
216c3e85bdcSAkhil Goyal 	int ret;
217c3e85bdcSAkhil Goyal 	struct qm_mcc_initfq opts;
218c3e85bdcSAkhil Goyal 	uint32_t flags;
219c3e85bdcSAkhil Goyal 
220c3e85bdcSAkhil Goyal 	flags = QMAN_FQ_FLAG_NO_ENQUEUE | QMAN_FQ_FLAG_LOCKED |
221c3e85bdcSAkhil Goyal 		QMAN_FQ_FLAG_DYNAMIC_FQID;
222c3e85bdcSAkhil Goyal 
223c3e85bdcSAkhil Goyal 	ret = qman_create_fq(0, flags, fq);
224c3e85bdcSAkhil Goyal 	if (unlikely(ret)) {
225f163231eSHemant Agrawal 		DPAA_SEC_ERR("qman_create_fq failed");
226c3e85bdcSAkhil Goyal 		return ret;
227c3e85bdcSAkhil Goyal 	}
228c3e85bdcSAkhil Goyal 
229c3e85bdcSAkhil Goyal 	memset(&opts, 0, sizeof(opts));
230c3e85bdcSAkhil Goyal 	opts.we_mask = QM_INITFQ_WE_DESTWQ | QM_INITFQ_WE_FQCTRL |
231c3e85bdcSAkhil Goyal 		       QM_INITFQ_WE_CONTEXTA | QM_INITFQ_WE_CONTEXTB;
232c3e85bdcSAkhil Goyal 
233c3e85bdcSAkhil Goyal 	/* opts.fqd.dest.channel = dpaa_sec_pool_chan; */
234c3e85bdcSAkhil Goyal 
235c3e85bdcSAkhil Goyal 	fq->cb.dqrr = dqrr_out_fq_cb_rx;
236c3e85bdcSAkhil Goyal 	fq->cb.ern  = ern_sec_fq_handler;
237c3e85bdcSAkhil Goyal 
238c3e85bdcSAkhil Goyal 	ret = qman_init_fq(fq, 0, &opts);
239c3e85bdcSAkhil Goyal 	if (unlikely(ret)) {
240f163231eSHemant Agrawal 		DPAA_SEC_ERR("unable to init caam source fq!");
241c3e85bdcSAkhil Goyal 		return ret;
242c3e85bdcSAkhil Goyal 	}
243c3e85bdcSAkhil Goyal 
244c3e85bdcSAkhil Goyal 	return ret;
245c3e85bdcSAkhil Goyal }
246c3e85bdcSAkhil Goyal 
247c3e85bdcSAkhil Goyal static inline int is_encode(dpaa_sec_session *ses)
248c3e85bdcSAkhil Goyal {
249c3e85bdcSAkhil Goyal 	return ses->dir == DIR_ENC;
250c3e85bdcSAkhil Goyal }
251c3e85bdcSAkhil Goyal 
252c3e85bdcSAkhil Goyal static inline int is_decode(dpaa_sec_session *ses)
253c3e85bdcSAkhil Goyal {
254c3e85bdcSAkhil Goyal 	return ses->dir == DIR_DEC;
255c3e85bdcSAkhil Goyal }
256c3e85bdcSAkhil Goyal 
257314424b6SHemant Agrawal #ifdef RTE_LIBRTE_SECURITY
258a1173d55SHemant Agrawal static int
259a1173d55SHemant Agrawal dpaa_sec_prep_pdcp_cdb(dpaa_sec_session *ses)
260a1173d55SHemant Agrawal {
261a1173d55SHemant Agrawal 	struct alginfo authdata = {0}, cipherdata = {0};
262a1173d55SHemant Agrawal 	struct sec_cdb *cdb = &ses->cdb;
2632e4cbdb4SVakul Garg 	struct alginfo *p_authdata = NULL;
264a1173d55SHemant Agrawal 	int32_t shared_desc_len = 0;
265a1173d55SHemant Agrawal 	int err;
266a1173d55SHemant Agrawal #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
267a1173d55SHemant Agrawal 	int swap = false;
268a1173d55SHemant Agrawal #else
269a1173d55SHemant Agrawal 	int swap = true;
270a1173d55SHemant Agrawal #endif
271a1173d55SHemant Agrawal 
272a1173d55SHemant Agrawal 	cipherdata.key = (size_t)ses->cipher_key.data;
273a1173d55SHemant Agrawal 	cipherdata.keylen = ses->cipher_key.length;
274a1173d55SHemant Agrawal 	cipherdata.key_enc_flags = 0;
275a1173d55SHemant Agrawal 	cipherdata.key_type = RTA_DATA_IMM;
2768524b44eSHemant Agrawal 	cipherdata.algtype = ses->cipher_key.alg;
2778524b44eSHemant Agrawal 	cipherdata.algmode = ses->cipher_key.algmode;
278a1173d55SHemant Agrawal 
2792e4cbdb4SVakul Garg 	cdb->sh_desc[0] = cipherdata.keylen;
2802e4cbdb4SVakul Garg 	cdb->sh_desc[1] = 0;
2812e4cbdb4SVakul Garg 	cdb->sh_desc[2] = 0;
2822e4cbdb4SVakul Garg 
2832e4cbdb4SVakul Garg 	if (ses->auth_alg) {
284a1173d55SHemant Agrawal 		authdata.key = (size_t)ses->auth_key.data;
285a1173d55SHemant Agrawal 		authdata.keylen = ses->auth_key.length;
286a1173d55SHemant Agrawal 		authdata.key_enc_flags = 0;
287a1173d55SHemant Agrawal 		authdata.key_type = RTA_DATA_IMM;
2888524b44eSHemant Agrawal 		authdata.algtype = ses->auth_key.alg;
2898524b44eSHemant Agrawal 		authdata.algmode = ses->auth_key.algmode;
290a1173d55SHemant Agrawal 
2912e4cbdb4SVakul Garg 		p_authdata = &authdata;
2922e4cbdb4SVakul Garg 
293a1173d55SHemant Agrawal 		cdb->sh_desc[1] = authdata.keylen;
2942e4cbdb4SVakul Garg 	}
2952e4cbdb4SVakul Garg 
296a1173d55SHemant Agrawal 	err = rta_inline_query(IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN,
297a1173d55SHemant Agrawal 			       MIN_JOB_DESC_SIZE,
298a1173d55SHemant Agrawal 			       (unsigned int *)cdb->sh_desc,
299a1173d55SHemant Agrawal 			       &cdb->sh_desc[2], 2);
300a1173d55SHemant Agrawal 	if (err < 0) {
301a1173d55SHemant Agrawal 		DPAA_SEC_ERR("Crypto: Incorrect key lengths");
302a1173d55SHemant Agrawal 		return err;
303a1173d55SHemant Agrawal 	}
3042e4cbdb4SVakul Garg 
305a1173d55SHemant Agrawal 	if (!(cdb->sh_desc[2] & 1) && cipherdata.keylen) {
3062e4cbdb4SVakul Garg 		cipherdata.key =
3072e4cbdb4SVakul Garg 			(size_t)dpaa_mem_vtop((void *)(size_t)cipherdata.key);
308a1173d55SHemant Agrawal 		cipherdata.key_type = RTA_DATA_PTR;
309a1173d55SHemant Agrawal 	}
310a1173d55SHemant Agrawal 	if (!(cdb->sh_desc[2] & (1 << 1)) &&  authdata.keylen) {
3112e4cbdb4SVakul Garg 		authdata.key =
3122e4cbdb4SVakul Garg 			(size_t)dpaa_mem_vtop((void *)(size_t)authdata.key);
313a1173d55SHemant Agrawal 		authdata.key_type = RTA_DATA_PTR;
314a1173d55SHemant Agrawal 	}
315a1173d55SHemant Agrawal 
316a1173d55SHemant Agrawal 	cdb->sh_desc[0] = 0;
317a1173d55SHemant Agrawal 	cdb->sh_desc[1] = 0;
318a1173d55SHemant Agrawal 	cdb->sh_desc[2] = 0;
319a1173d55SHemant Agrawal 
3202e4cbdb4SVakul Garg 	if (ses->pdcp.domain == RTE_SECURITY_PDCP_MODE_CONTROL) {
321a1173d55SHemant Agrawal 		if (ses->dir == DIR_ENC)
322a1173d55SHemant Agrawal 			shared_desc_len = cnstr_shdsc_pdcp_c_plane_encap(
323a1173d55SHemant Agrawal 					cdb->sh_desc, 1, swap,
324a1173d55SHemant Agrawal 					ses->pdcp.hfn,
325eac60082SVakul Garg 					ses->pdcp.sn_size,
326a1173d55SHemant Agrawal 					ses->pdcp.bearer,
327a1173d55SHemant Agrawal 					ses->pdcp.pkt_dir,
328a1173d55SHemant Agrawal 					ses->pdcp.hfn_threshold,
329a1173d55SHemant Agrawal 					&cipherdata, &authdata,
330a1173d55SHemant Agrawal 					0);
331a1173d55SHemant Agrawal 		else if (ses->dir == DIR_DEC)
332a1173d55SHemant Agrawal 			shared_desc_len = cnstr_shdsc_pdcp_c_plane_decap(
333a1173d55SHemant Agrawal 					cdb->sh_desc, 1, swap,
334a1173d55SHemant Agrawal 					ses->pdcp.hfn,
335eac60082SVakul Garg 					ses->pdcp.sn_size,
336a1173d55SHemant Agrawal 					ses->pdcp.bearer,
337a1173d55SHemant Agrawal 					ses->pdcp.pkt_dir,
338a1173d55SHemant Agrawal 					ses->pdcp.hfn_threshold,
339a1173d55SHemant Agrawal 					&cipherdata, &authdata,
340a1173d55SHemant Agrawal 					0);
341a1173d55SHemant Agrawal 	} else {
342a1173d55SHemant Agrawal 		if (ses->dir == DIR_ENC)
343a1173d55SHemant Agrawal 			shared_desc_len = cnstr_shdsc_pdcp_u_plane_encap(
344a1173d55SHemant Agrawal 					cdb->sh_desc, 1, swap,
345a1173d55SHemant Agrawal 					ses->pdcp.sn_size,
346a1173d55SHemant Agrawal 					ses->pdcp.hfn,
347a1173d55SHemant Agrawal 					ses->pdcp.bearer,
348a1173d55SHemant Agrawal 					ses->pdcp.pkt_dir,
349a1173d55SHemant Agrawal 					ses->pdcp.hfn_threshold,
3502e4cbdb4SVakul Garg 					&cipherdata, p_authdata, 0);
351a1173d55SHemant Agrawal 		else if (ses->dir == DIR_DEC)
352a1173d55SHemant Agrawal 			shared_desc_len = cnstr_shdsc_pdcp_u_plane_decap(
353a1173d55SHemant Agrawal 					cdb->sh_desc, 1, swap,
354a1173d55SHemant Agrawal 					ses->pdcp.sn_size,
355a1173d55SHemant Agrawal 					ses->pdcp.hfn,
356a1173d55SHemant Agrawal 					ses->pdcp.bearer,
357a1173d55SHemant Agrawal 					ses->pdcp.pkt_dir,
358a1173d55SHemant Agrawal 					ses->pdcp.hfn_threshold,
3592e4cbdb4SVakul Garg 					&cipherdata, p_authdata, 0);
360a1173d55SHemant Agrawal 	}
361a1173d55SHemant Agrawal 	return shared_desc_len;
362a1173d55SHemant Agrawal }
363a1173d55SHemant Agrawal 
36405b12700SHemant Agrawal /* prepare ipsec proto command block of the session */
36505b12700SHemant Agrawal static int
36605b12700SHemant Agrawal dpaa_sec_prep_ipsec_cdb(dpaa_sec_session *ses)
36705b12700SHemant Agrawal {
36805b12700SHemant Agrawal 	struct alginfo cipherdata = {0}, authdata = {0};
36905b12700SHemant Agrawal 	struct sec_cdb *cdb = &ses->cdb;
37005b12700SHemant Agrawal 	int32_t shared_desc_len = 0;
37105b12700SHemant Agrawal 	int err;
37205b12700SHemant Agrawal #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
37305b12700SHemant Agrawal 	int swap = false;
37405b12700SHemant Agrawal #else
37505b12700SHemant Agrawal 	int swap = true;
37605b12700SHemant Agrawal #endif
37705b12700SHemant Agrawal 
37805b12700SHemant Agrawal 	cipherdata.key = (size_t)ses->cipher_key.data;
37905b12700SHemant Agrawal 	cipherdata.keylen = ses->cipher_key.length;
38005b12700SHemant Agrawal 	cipherdata.key_enc_flags = 0;
38105b12700SHemant Agrawal 	cipherdata.key_type = RTA_DATA_IMM;
3828524b44eSHemant Agrawal 	cipherdata.algtype = ses->cipher_key.alg;
3838524b44eSHemant Agrawal 	cipherdata.algmode = ses->cipher_key.algmode;
38405b12700SHemant Agrawal 
38505b12700SHemant Agrawal 	authdata.key = (size_t)ses->auth_key.data;
38605b12700SHemant Agrawal 	authdata.keylen = ses->auth_key.length;
38705b12700SHemant Agrawal 	authdata.key_enc_flags = 0;
38805b12700SHemant Agrawal 	authdata.key_type = RTA_DATA_IMM;
3898524b44eSHemant Agrawal 	authdata.algtype = ses->auth_key.alg;
3908524b44eSHemant Agrawal 	authdata.algmode = ses->auth_key.algmode;
39105b12700SHemant Agrawal 
39205b12700SHemant Agrawal 	cdb->sh_desc[0] = cipherdata.keylen;
39305b12700SHemant Agrawal 	cdb->sh_desc[1] = authdata.keylen;
39405b12700SHemant Agrawal 	err = rta_inline_query(IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN,
39505b12700SHemant Agrawal 			       MIN_JOB_DESC_SIZE,
39605b12700SHemant Agrawal 			       (unsigned int *)cdb->sh_desc,
39705b12700SHemant Agrawal 			       &cdb->sh_desc[2], 2);
39805b12700SHemant Agrawal 
39905b12700SHemant Agrawal 	if (err < 0) {
40005b12700SHemant Agrawal 		DPAA_SEC_ERR("Crypto: Incorrect key lengths");
40105b12700SHemant Agrawal 		return err;
40205b12700SHemant Agrawal 	}
40305b12700SHemant Agrawal 	if (cdb->sh_desc[2] & 1)
40405b12700SHemant Agrawal 		cipherdata.key_type = RTA_DATA_IMM;
40505b12700SHemant Agrawal 	else {
40605b12700SHemant Agrawal 		cipherdata.key = (size_t)dpaa_mem_vtop(
40705b12700SHemant Agrawal 					(void *)(size_t)cipherdata.key);
40805b12700SHemant Agrawal 		cipherdata.key_type = RTA_DATA_PTR;
40905b12700SHemant Agrawal 	}
41005b12700SHemant Agrawal 	if (cdb->sh_desc[2] & (1<<1))
41105b12700SHemant Agrawal 		authdata.key_type = RTA_DATA_IMM;
41205b12700SHemant Agrawal 	else {
41305b12700SHemant Agrawal 		authdata.key = (size_t)dpaa_mem_vtop(
41405b12700SHemant Agrawal 					(void *)(size_t)authdata.key);
41505b12700SHemant Agrawal 		authdata.key_type = RTA_DATA_PTR;
41605b12700SHemant Agrawal 	}
41705b12700SHemant Agrawal 
41805b12700SHemant Agrawal 	cdb->sh_desc[0] = 0;
41905b12700SHemant Agrawal 	cdb->sh_desc[1] = 0;
42005b12700SHemant Agrawal 	cdb->sh_desc[2] = 0;
42105b12700SHemant Agrawal 	if (ses->dir == DIR_ENC) {
42205b12700SHemant Agrawal 		shared_desc_len = cnstr_shdsc_ipsec_new_encap(
42305b12700SHemant Agrawal 				cdb->sh_desc,
42405b12700SHemant Agrawal 				true, swap, SHR_SERIAL,
42505b12700SHemant Agrawal 				&ses->encap_pdb,
42605b12700SHemant Agrawal 				(uint8_t *)&ses->ip4_hdr,
42705b12700SHemant Agrawal 				&cipherdata, &authdata);
42805b12700SHemant Agrawal 	} else if (ses->dir == DIR_DEC) {
42905b12700SHemant Agrawal 		shared_desc_len = cnstr_shdsc_ipsec_new_decap(
43005b12700SHemant Agrawal 				cdb->sh_desc,
43105b12700SHemant Agrawal 				true, swap, SHR_SERIAL,
43205b12700SHemant Agrawal 				&ses->decap_pdb,
43305b12700SHemant Agrawal 				&cipherdata, &authdata);
43405b12700SHemant Agrawal 	}
43505b12700SHemant Agrawal 	return shared_desc_len;
43605b12700SHemant Agrawal }
437314424b6SHemant Agrawal #endif
438c3e85bdcSAkhil Goyal /* prepare command block of the session */
439c3e85bdcSAkhil Goyal static int
440c3e85bdcSAkhil Goyal dpaa_sec_prep_cdb(dpaa_sec_session *ses)
441c3e85bdcSAkhil Goyal {
442c3e85bdcSAkhil Goyal 	struct alginfo alginfo_c = {0}, alginfo_a = {0}, alginfo = {0};
44322788c2cSSunil Kumar Kori 	int32_t shared_desc_len = 0;
444e79416d1SHemant Agrawal 	struct sec_cdb *cdb = &ses->cdb;
445c3e85bdcSAkhil Goyal 	int err;
446c3e85bdcSAkhil Goyal #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
447c3e85bdcSAkhil Goyal 	int swap = false;
448c3e85bdcSAkhil Goyal #else
449c3e85bdcSAkhil Goyal 	int swap = true;
450c3e85bdcSAkhil Goyal #endif
451c3e85bdcSAkhil Goyal 
452c3e85bdcSAkhil Goyal 	memset(cdb, 0, sizeof(struct sec_cdb));
453c3e85bdcSAkhil Goyal 
4548524b44eSHemant Agrawal 	switch (ses->ctxt) {
455314424b6SHemant Agrawal #ifdef RTE_LIBRTE_SECURITY
4568524b44eSHemant Agrawal 	case DPAA_SEC_IPSEC:
45705b12700SHemant Agrawal 		shared_desc_len = dpaa_sec_prep_ipsec_cdb(ses);
4588524b44eSHemant Agrawal 		break;
4598524b44eSHemant Agrawal 	case DPAA_SEC_PDCP:
460a1173d55SHemant Agrawal 		shared_desc_len = dpaa_sec_prep_pdcp_cdb(ses);
4618524b44eSHemant Agrawal 		break;
462314424b6SHemant Agrawal #endif
4638524b44eSHemant Agrawal 	case DPAA_SEC_CIPHER:
4640e5607e4SHemant Agrawal 		alginfo_c.key = (size_t)ses->cipher_key.data;
465c3e85bdcSAkhil Goyal 		alginfo_c.keylen = ses->cipher_key.length;
466c3e85bdcSAkhil Goyal 		alginfo_c.key_enc_flags = 0;
467c3e85bdcSAkhil Goyal 		alginfo_c.key_type = RTA_DATA_IMM;
4688524b44eSHemant Agrawal 		alginfo_c.algtype = ses->cipher_key.alg;
4698524b44eSHemant Agrawal 		alginfo_c.algmode = ses->cipher_key.algmode;
4708524b44eSHemant Agrawal 
471c5788a10SHemant Agrawal 		switch (ses->cipher_alg) {
472c5788a10SHemant Agrawal 		case RTE_CRYPTO_CIPHER_AES_CBC:
473c5788a10SHemant Agrawal 		case RTE_CRYPTO_CIPHER_3DES_CBC:
474c5788a10SHemant Agrawal 		case RTE_CRYPTO_CIPHER_AES_CTR:
475c5788a10SHemant Agrawal 		case RTE_CRYPTO_CIPHER_3DES_CTR:
476c5788a10SHemant Agrawal 			shared_desc_len = cnstr_shdsc_blkcipher(
477c5788a10SHemant Agrawal 					cdb->sh_desc, true,
478c5788a10SHemant Agrawal 					swap, SHR_NEVER, &alginfo_c,
479c5788a10SHemant Agrawal 					NULL,
480c5788a10SHemant Agrawal 					ses->iv.length,
481c5788a10SHemant Agrawal 					ses->dir);
482c5788a10SHemant Agrawal 			break;
483c5788a10SHemant Agrawal 		case RTE_CRYPTO_CIPHER_SNOW3G_UEA2:
484c5788a10SHemant Agrawal 			shared_desc_len = cnstr_shdsc_snow_f8(
485c5788a10SHemant Agrawal 					cdb->sh_desc, true, swap,
486c5788a10SHemant Agrawal 					&alginfo_c,
487c5788a10SHemant Agrawal 					ses->dir);
488c5788a10SHemant Agrawal 			break;
489c5788a10SHemant Agrawal 		case RTE_CRYPTO_CIPHER_ZUC_EEA3:
490c5788a10SHemant Agrawal 			shared_desc_len = cnstr_shdsc_zuce(
491c5788a10SHemant Agrawal 					cdb->sh_desc, true, swap,
492c5788a10SHemant Agrawal 					&alginfo_c,
493c5788a10SHemant Agrawal 					ses->dir);
494c5788a10SHemant Agrawal 			break;
495c5788a10SHemant Agrawal 		default:
496c5788a10SHemant Agrawal 			DPAA_SEC_ERR("unsupported cipher alg %d",
497c5788a10SHemant Agrawal 				     ses->cipher_alg);
498c3e85bdcSAkhil Goyal 			return -ENOTSUP;
499c3e85bdcSAkhil Goyal 		}
5008524b44eSHemant Agrawal 		break;
5018524b44eSHemant Agrawal 	case DPAA_SEC_AUTH:
5020e5607e4SHemant Agrawal 		alginfo_a.key = (size_t)ses->auth_key.data;
503c3e85bdcSAkhil Goyal 		alginfo_a.keylen = ses->auth_key.length;
504c3e85bdcSAkhil Goyal 		alginfo_a.key_enc_flags = 0;
505c3e85bdcSAkhil Goyal 		alginfo_a.key_type = RTA_DATA_IMM;
5068524b44eSHemant Agrawal 		alginfo_a.algtype = ses->auth_key.alg;
5078524b44eSHemant Agrawal 		alginfo_a.algmode = ses->auth_key.algmode;
508c5788a10SHemant Agrawal 		switch (ses->auth_alg) {
509c5788a10SHemant Agrawal 		case RTE_CRYPTO_AUTH_MD5_HMAC:
510c5788a10SHemant Agrawal 		case RTE_CRYPTO_AUTH_SHA1_HMAC:
511c5788a10SHemant Agrawal 		case RTE_CRYPTO_AUTH_SHA224_HMAC:
512c5788a10SHemant Agrawal 		case RTE_CRYPTO_AUTH_SHA256_HMAC:
513c5788a10SHemant Agrawal 		case RTE_CRYPTO_AUTH_SHA384_HMAC:
514c5788a10SHemant Agrawal 		case RTE_CRYPTO_AUTH_SHA512_HMAC:
515c5788a10SHemant Agrawal 			shared_desc_len = cnstr_shdsc_hmac(
516c5788a10SHemant Agrawal 						cdb->sh_desc, true,
517c5788a10SHemant Agrawal 						swap, SHR_NEVER, &alginfo_a,
518c5788a10SHemant Agrawal 						!ses->dir,
519c5788a10SHemant Agrawal 						ses->digest_length);
520c5788a10SHemant Agrawal 			break;
521c5788a10SHemant Agrawal 		case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
522c5788a10SHemant Agrawal 			shared_desc_len = cnstr_shdsc_snow_f9(
523c5788a10SHemant Agrawal 						cdb->sh_desc, true, swap,
524c5788a10SHemant Agrawal 						&alginfo_a,
525c5788a10SHemant Agrawal 						!ses->dir,
526c5788a10SHemant Agrawal 						ses->digest_length);
527c5788a10SHemant Agrawal 			break;
528c5788a10SHemant Agrawal 		case RTE_CRYPTO_AUTH_ZUC_EIA3:
529c5788a10SHemant Agrawal 			shared_desc_len = cnstr_shdsc_zuca(
530c5788a10SHemant Agrawal 						cdb->sh_desc, true, swap,
531c5788a10SHemant Agrawal 						&alginfo_a,
532c5788a10SHemant Agrawal 						!ses->dir,
533c5788a10SHemant Agrawal 						ses->digest_length);
534c5788a10SHemant Agrawal 			break;
535c5788a10SHemant Agrawal 		default:
536c5788a10SHemant Agrawal 			DPAA_SEC_ERR("unsupported auth alg %u", ses->auth_alg);
537c5788a10SHemant Agrawal 		}
5388524b44eSHemant Agrawal 		break;
5398524b44eSHemant Agrawal 	case DPAA_SEC_AEAD:
540c3e85bdcSAkhil Goyal 		if (alginfo.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
541f163231eSHemant Agrawal 			DPAA_SEC_ERR("not supported aead alg");
542c3e85bdcSAkhil Goyal 			return -ENOTSUP;
543c3e85bdcSAkhil Goyal 		}
5440e5607e4SHemant Agrawal 		alginfo.key = (size_t)ses->aead_key.data;
545c3e85bdcSAkhil Goyal 		alginfo.keylen = ses->aead_key.length;
546c3e85bdcSAkhil Goyal 		alginfo.key_enc_flags = 0;
547c3e85bdcSAkhil Goyal 		alginfo.key_type = RTA_DATA_IMM;
5488524b44eSHemant Agrawal 		alginfo.algtype = ses->aead_key.alg;
5498524b44eSHemant Agrawal 		alginfo.algmode = ses->aead_key.algmode;
550c3e85bdcSAkhil Goyal 
551c3e85bdcSAkhil Goyal 		if (ses->dir == DIR_ENC)
552c3e85bdcSAkhil Goyal 			shared_desc_len = cnstr_shdsc_gcm_encap(
5537449390bSAkhil Goyal 					cdb->sh_desc, true, swap, SHR_NEVER,
554c3e85bdcSAkhil Goyal 					&alginfo,
555c3e85bdcSAkhil Goyal 					ses->iv.length,
556c3e85bdcSAkhil Goyal 					ses->digest_length);
557c3e85bdcSAkhil Goyal 		else
558c3e85bdcSAkhil Goyal 			shared_desc_len = cnstr_shdsc_gcm_decap(
5597449390bSAkhil Goyal 					cdb->sh_desc, true, swap, SHR_NEVER,
560c3e85bdcSAkhil Goyal 					&alginfo,
561c3e85bdcSAkhil Goyal 					ses->iv.length,
562c3e85bdcSAkhil Goyal 					ses->digest_length);
5638524b44eSHemant Agrawal 		break;
5648524b44eSHemant Agrawal 	case DPAA_SEC_CIPHER_HASH:
5650e5607e4SHemant Agrawal 		alginfo_c.key = (size_t)ses->cipher_key.data;
566c3e85bdcSAkhil Goyal 		alginfo_c.keylen = ses->cipher_key.length;
567c3e85bdcSAkhil Goyal 		alginfo_c.key_enc_flags = 0;
568c3e85bdcSAkhil Goyal 		alginfo_c.key_type = RTA_DATA_IMM;
5698524b44eSHemant Agrawal 		alginfo_c.algtype = ses->cipher_key.alg;
5708524b44eSHemant Agrawal 		alginfo_c.algmode = ses->cipher_key.algmode;
571c3e85bdcSAkhil Goyal 
5720e5607e4SHemant Agrawal 		alginfo_a.key = (size_t)ses->auth_key.data;
573c3e85bdcSAkhil Goyal 		alginfo_a.keylen = ses->auth_key.length;
574c3e85bdcSAkhil Goyal 		alginfo_a.key_enc_flags = 0;
575c3e85bdcSAkhil Goyal 		alginfo_a.key_type = RTA_DATA_IMM;
5768524b44eSHemant Agrawal 		alginfo_a.algtype = ses->auth_key.alg;
5778524b44eSHemant Agrawal 		alginfo_a.algmode = ses->auth_key.algmode;
578c3e85bdcSAkhil Goyal 
579c3e85bdcSAkhil Goyal 		cdb->sh_desc[0] = alginfo_c.keylen;
580c3e85bdcSAkhil Goyal 		cdb->sh_desc[1] = alginfo_a.keylen;
581c3e85bdcSAkhil Goyal 		err = rta_inline_query(IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN,
582c3e85bdcSAkhil Goyal 				       MIN_JOB_DESC_SIZE,
583c3e85bdcSAkhil Goyal 				       (unsigned int *)cdb->sh_desc,
584c3e85bdcSAkhil Goyal 				       &cdb->sh_desc[2], 2);
585c3e85bdcSAkhil Goyal 
586c3e85bdcSAkhil Goyal 		if (err < 0) {
587f163231eSHemant Agrawal 			DPAA_SEC_ERR("Crypto: Incorrect key lengths");
588c3e85bdcSAkhil Goyal 			return err;
589c3e85bdcSAkhil Goyal 		}
590c3e85bdcSAkhil Goyal 		if (cdb->sh_desc[2] & 1)
591c3e85bdcSAkhil Goyal 			alginfo_c.key_type = RTA_DATA_IMM;
592c3e85bdcSAkhil Goyal 		else {
5930e5607e4SHemant Agrawal 			alginfo_c.key = (size_t)dpaa_mem_vtop(
5940e5607e4SHemant Agrawal 						(void *)(size_t)alginfo_c.key);
595c3e85bdcSAkhil Goyal 			alginfo_c.key_type = RTA_DATA_PTR;
596c3e85bdcSAkhil Goyal 		}
597c3e85bdcSAkhil Goyal 		if (cdb->sh_desc[2] & (1<<1))
598c3e85bdcSAkhil Goyal 			alginfo_a.key_type = RTA_DATA_IMM;
599c3e85bdcSAkhil Goyal 		else {
6000e5607e4SHemant Agrawal 			alginfo_a.key = (size_t)dpaa_mem_vtop(
6010e5607e4SHemant Agrawal 						(void *)(size_t)alginfo_a.key);
602c3e85bdcSAkhil Goyal 			alginfo_a.key_type = RTA_DATA_PTR;
603c3e85bdcSAkhil Goyal 		}
604c3e85bdcSAkhil Goyal 		cdb->sh_desc[0] = 0;
605c3e85bdcSAkhil Goyal 		cdb->sh_desc[1] = 0;
606c3e85bdcSAkhil Goyal 		cdb->sh_desc[2] = 0;
6071f14d500SAkhil Goyal 		/* Auth_only_len is set as 0 here and it will be
6081f14d500SAkhil Goyal 		 * overwritten in fd for each packet.
609c3e85bdcSAkhil Goyal 		 */
610c3e85bdcSAkhil Goyal 		shared_desc_len = cnstr_shdsc_authenc(cdb->sh_desc,
6117449390bSAkhil Goyal 				true, swap, SHR_SERIAL, &alginfo_c, &alginfo_a,
6123394ed47SVakul Garg 				ses->iv.length,
613c3e85bdcSAkhil Goyal 				ses->digest_length, ses->dir);
6148524b44eSHemant Agrawal 		break;
6158524b44eSHemant Agrawal 	case DPAA_SEC_HASH_CIPHER:
6168524b44eSHemant Agrawal 	default:
6178524b44eSHemant Agrawal 		DPAA_SEC_ERR("error: Unsupported session");
6188524b44eSHemant Agrawal 		return -ENOTSUP;
619c3e85bdcSAkhil Goyal 	}
62022788c2cSSunil Kumar Kori 
62122788c2cSSunil Kumar Kori 	if (shared_desc_len < 0) {
622f163231eSHemant Agrawal 		DPAA_SEC_ERR("error in preparing command block");
62322788c2cSSunil Kumar Kori 		return shared_desc_len;
62422788c2cSSunil Kumar Kori 	}
62522788c2cSSunil Kumar Kori 
626c3e85bdcSAkhil Goyal 	cdb->sh_hdr.hi.field.idlen = shared_desc_len;
627c3e85bdcSAkhil Goyal 	cdb->sh_hdr.hi.word = rte_cpu_to_be_32(cdb->sh_hdr.hi.word);
628c3e85bdcSAkhil Goyal 	cdb->sh_hdr.lo.word = rte_cpu_to_be_32(cdb->sh_hdr.lo.word);
629c3e85bdcSAkhil Goyal 
630c3e85bdcSAkhil Goyal 	return 0;
631c3e85bdcSAkhil Goyal }
632c3e85bdcSAkhil Goyal 
633c3e85bdcSAkhil Goyal /* qp is lockless, should be accessed by only one thread */
634c3e85bdcSAkhil Goyal static int
635c3e85bdcSAkhil Goyal dpaa_sec_deq(struct dpaa_sec_qp *qp, struct rte_crypto_op **ops, int nb_ops)
636c3e85bdcSAkhil Goyal {
637c3e85bdcSAkhil Goyal 	struct qman_fq *fq;
6389a984458SAkhil Goyal 	unsigned int pkts = 0;
639f40d5a53SNipun Gupta 	int num_rx_bufs, ret;
6409a984458SAkhil Goyal 	struct qm_dqrr_entry *dq;
641f40d5a53SNipun Gupta 	uint32_t vdqcr_flags = 0;
642c3e85bdcSAkhil Goyal 
643c3e85bdcSAkhil Goyal 	fq = &qp->outq;
644f40d5a53SNipun Gupta 	/*
645f40d5a53SNipun Gupta 	 * Until request for four buffers, we provide exact number of buffers.
646f40d5a53SNipun Gupta 	 * Otherwise we do not set the QM_VDQCR_EXACT flag.
647f40d5a53SNipun Gupta 	 * Not setting QM_VDQCR_EXACT flag can provide two more buffers than
648f40d5a53SNipun Gupta 	 * requested, so we request two less in this case.
649f40d5a53SNipun Gupta 	 */
650f40d5a53SNipun Gupta 	if (nb_ops < 4) {
651f40d5a53SNipun Gupta 		vdqcr_flags = QM_VDQCR_EXACT;
652f40d5a53SNipun Gupta 		num_rx_bufs = nb_ops;
653f40d5a53SNipun Gupta 	} else {
654f40d5a53SNipun Gupta 		num_rx_bufs = nb_ops > DPAA_MAX_DEQUEUE_NUM_FRAMES ?
655f40d5a53SNipun Gupta 			(DPAA_MAX_DEQUEUE_NUM_FRAMES - 2) : (nb_ops - 2);
656f40d5a53SNipun Gupta 	}
657f40d5a53SNipun Gupta 	ret = qman_set_vdq(fq, num_rx_bufs, vdqcr_flags);
6589a984458SAkhil Goyal 	if (ret)
6599a984458SAkhil Goyal 		return 0;
660c3e85bdcSAkhil Goyal 
6619a984458SAkhil Goyal 	do {
6629a984458SAkhil Goyal 		const struct qm_fd *fd;
6639a984458SAkhil Goyal 		struct dpaa_sec_job *job;
6649a984458SAkhil Goyal 		struct dpaa_sec_op_ctx *ctx;
6659a984458SAkhil Goyal 		struct rte_crypto_op *op;
666c3e85bdcSAkhil Goyal 
6679a984458SAkhil Goyal 		dq = qman_dequeue(fq);
6689a984458SAkhil Goyal 		if (!dq)
6699a984458SAkhil Goyal 			continue;
6709a984458SAkhil Goyal 
6719a984458SAkhil Goyal 		fd = &dq->fd;
6729a984458SAkhil Goyal 		/* sg is embedded in an op ctx,
6739a984458SAkhil Goyal 		 * sg[0] is for output
6749a984458SAkhil Goyal 		 * sg[1] for input
6759a984458SAkhil Goyal 		 */
6769a984458SAkhil Goyal 		job = dpaa_mem_ptov(qm_fd_addr_get64(fd));
6779a984458SAkhil Goyal 
6789a984458SAkhil Goyal 		ctx = container_of(job, struct dpaa_sec_op_ctx, job);
6799a984458SAkhil Goyal 		ctx->fd_status = fd->status;
6809a984458SAkhil Goyal 		op = ctx->op;
6819a984458SAkhil Goyal 		if (op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
6829a984458SAkhil Goyal 			struct qm_sg_entry *sg_out;
6839a984458SAkhil Goyal 			uint32_t len;
684fb5c100aSAkhil Goyal 			struct rte_mbuf *mbuf = (op->sym->m_dst == NULL) ?
685fb5c100aSAkhil Goyal 						op->sym->m_src : op->sym->m_dst;
6869a984458SAkhil Goyal 
6879a984458SAkhil Goyal 			sg_out = &job->sg[0];
6889a984458SAkhil Goyal 			hw_sg_to_cpu(sg_out);
6899a984458SAkhil Goyal 			len = sg_out->length;
690fb5c100aSAkhil Goyal 			mbuf->pkt_len = len;
691fb5c100aSAkhil Goyal 			while (mbuf->next != NULL) {
692fb5c100aSAkhil Goyal 				len -= mbuf->data_len;
693fb5c100aSAkhil Goyal 				mbuf = mbuf->next;
694fb5c100aSAkhil Goyal 			}
695fb5c100aSAkhil Goyal 			mbuf->data_len = len;
6969a984458SAkhil Goyal 		}
6979a984458SAkhil Goyal 		if (!ctx->fd_status) {
6989a984458SAkhil Goyal 			op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
6999a984458SAkhil Goyal 		} else {
700f163231eSHemant Agrawal 			DPAA_SEC_DP_WARN("SEC return err:0x%x", ctx->fd_status);
7019a984458SAkhil Goyal 			op->status = RTE_CRYPTO_OP_STATUS_ERROR;
7029a984458SAkhil Goyal 		}
7039a984458SAkhil Goyal 		ops[pkts++] = op;
7049a984458SAkhil Goyal 
7059a984458SAkhil Goyal 		/* report op status to sym->op and then free the ctx memeory */
7069a984458SAkhil Goyal 		rte_mempool_put(ctx->ctx_pool, (void *)ctx);
7079a984458SAkhil Goyal 
7089a984458SAkhil Goyal 		qman_dqrr_consume(fq, dq);
7099a984458SAkhil Goyal 	} while (fq->flags & QMAN_FQ_STATE_VDQCR);
7109a984458SAkhil Goyal 
7119a984458SAkhil Goyal 	return pkts;
712c3e85bdcSAkhil Goyal }
713c3e85bdcSAkhil Goyal 
714a74af788SAkhil Goyal static inline struct dpaa_sec_job *
715a74af788SAkhil Goyal build_auth_only_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
716a74af788SAkhil Goyal {
717a74af788SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
718a74af788SAkhil Goyal 	struct rte_mbuf *mbuf = sym->m_src;
719a74af788SAkhil Goyal 	struct dpaa_sec_job *cf;
720a74af788SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
721a74af788SAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
722a74af788SAkhil Goyal 	phys_addr_t start_addr;
723a74af788SAkhil Goyal 	uint8_t *old_digest, extra_segs;
724c5788a10SHemant Agrawal 	int data_len, data_offset;
725c5788a10SHemant Agrawal 
726c5788a10SHemant Agrawal 	data_len = sym->auth.data.length;
727c5788a10SHemant Agrawal 	data_offset = sym->auth.data.offset;
728c5788a10SHemant Agrawal 
729c5788a10SHemant Agrawal 	if (ses->auth_alg == RTE_CRYPTO_AUTH_SNOW3G_UIA2 ||
730c5788a10SHemant Agrawal 	    ses->auth_alg == RTE_CRYPTO_AUTH_ZUC_EIA3) {
731c5788a10SHemant Agrawal 		if ((data_len & 7) || (data_offset & 7)) {
732c5788a10SHemant Agrawal 			DPAA_SEC_ERR("AUTH: len/offset must be full bytes");
733c5788a10SHemant Agrawal 			return NULL;
734c5788a10SHemant Agrawal 		}
735c5788a10SHemant Agrawal 
736c5788a10SHemant Agrawal 		data_len = data_len >> 3;
737c5788a10SHemant Agrawal 		data_offset = data_offset >> 3;
738c5788a10SHemant Agrawal 	}
739a74af788SAkhil Goyal 
740a74af788SAkhil Goyal 	if (is_decode(ses))
741a74af788SAkhil Goyal 		extra_segs = 3;
742a74af788SAkhil Goyal 	else
743a74af788SAkhil Goyal 		extra_segs = 2;
744a74af788SAkhil Goyal 
745f7a5752eSHemant Agrawal 	if (mbuf->nb_segs > MAX_SG_ENTRIES) {
746f163231eSHemant Agrawal 		DPAA_SEC_DP_ERR("Auth: Max sec segs supported is %d",
747a74af788SAkhil Goyal 				MAX_SG_ENTRIES);
748a74af788SAkhil Goyal 		return NULL;
749a74af788SAkhil Goyal 	}
750f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, mbuf->nb_segs + extra_segs);
751a74af788SAkhil Goyal 	if (!ctx)
752a74af788SAkhil Goyal 		return NULL;
753a74af788SAkhil Goyal 
754a74af788SAkhil Goyal 	cf = &ctx->job;
755a74af788SAkhil Goyal 	ctx->op = op;
756a74af788SAkhil Goyal 	old_digest = ctx->digest;
757a74af788SAkhil Goyal 
758a74af788SAkhil Goyal 	/* output */
759a74af788SAkhil Goyal 	out_sg = &cf->sg[0];
760a74af788SAkhil Goyal 	qm_sg_entry_set64(out_sg, sym->auth.digest.phys_addr);
761a74af788SAkhil Goyal 	out_sg->length = ses->digest_length;
762a74af788SAkhil Goyal 	cpu_to_hw_sg(out_sg);
763a74af788SAkhil Goyal 
764a74af788SAkhil Goyal 	/* input */
765a74af788SAkhil Goyal 	in_sg = &cf->sg[1];
766a74af788SAkhil Goyal 	/* need to extend the input to a compound frame */
767a74af788SAkhil Goyal 	in_sg->extension = 1;
768a74af788SAkhil Goyal 	in_sg->final = 1;
769c5788a10SHemant Agrawal 	in_sg->length = data_len;
77095456e89SShreyansh Jain 	qm_sg_entry_set64(in_sg, dpaa_mem_vtop(&cf->sg[2]));
771a74af788SAkhil Goyal 
772a74af788SAkhil Goyal 	/* 1st seg */
773a74af788SAkhil Goyal 	sg = in_sg + 1;
774a74af788SAkhil Goyal 
775c5788a10SHemant Agrawal 	if (ses->iv.length) {
776c5788a10SHemant Agrawal 		uint8_t *iv_ptr;
777c5788a10SHemant Agrawal 
778c5788a10SHemant Agrawal 		iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
779c5788a10SHemant Agrawal 						   ses->iv.offset);
780c5788a10SHemant Agrawal 
781c5788a10SHemant Agrawal 		if (ses->auth_alg == RTE_CRYPTO_AUTH_SNOW3G_UIA2) {
782c5788a10SHemant Agrawal 			iv_ptr = conv_to_snow_f9_iv(iv_ptr);
783c5788a10SHemant Agrawal 			sg->length = 12;
784c5788a10SHemant Agrawal 		} else if (ses->auth_alg == RTE_CRYPTO_AUTH_ZUC_EIA3) {
785c5788a10SHemant Agrawal 			iv_ptr = conv_to_zuc_eia_iv(iv_ptr);
786c5788a10SHemant Agrawal 			sg->length = 8;
787c5788a10SHemant Agrawal 		} else {
788c5788a10SHemant Agrawal 			sg->length = ses->iv.length;
789c5788a10SHemant Agrawal 		}
790c5788a10SHemant Agrawal 		qm_sg_entry_set64(sg, dpaa_mem_vtop(iv_ptr));
791c5788a10SHemant Agrawal 		in_sg->length += sg->length;
792c5788a10SHemant Agrawal 		cpu_to_hw_sg(sg);
793c5788a10SHemant Agrawal 		sg++;
794c5788a10SHemant Agrawal 	}
795c5788a10SHemant Agrawal 
796c5788a10SHemant Agrawal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
797c5788a10SHemant Agrawal 	sg->offset = data_offset;
798c5788a10SHemant Agrawal 
799c5788a10SHemant Agrawal 	if (data_len <= (mbuf->data_len - data_offset)) {
800c5788a10SHemant Agrawal 		sg->length = data_len;
801c5788a10SHemant Agrawal 	} else {
802c5788a10SHemant Agrawal 		sg->length = mbuf->data_len - data_offset;
803c5788a10SHemant Agrawal 
804c5788a10SHemant Agrawal 		/* remaining i/p segs */
805c5788a10SHemant Agrawal 		while ((data_len = data_len - sg->length) &&
806c5788a10SHemant Agrawal 		       (mbuf = mbuf->next)) {
807a74af788SAkhil Goyal 			cpu_to_hw_sg(sg);
808a74af788SAkhil Goyal 			sg++;
809a74af788SAkhil Goyal 			qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
810c5788a10SHemant Agrawal 			if (data_len > mbuf->data_len)
811a74af788SAkhil Goyal 				sg->length = mbuf->data_len;
812c5788a10SHemant Agrawal 			else
813c5788a10SHemant Agrawal 				sg->length = data_len;
814c5788a10SHemant Agrawal 		}
815a74af788SAkhil Goyal 	}
816a74af788SAkhil Goyal 
817a74af788SAkhil Goyal 	if (is_decode(ses)) {
818a74af788SAkhil Goyal 		/* Digest verification case */
819a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
820a74af788SAkhil Goyal 		sg++;
821a74af788SAkhil Goyal 		rte_memcpy(old_digest, sym->auth.digest.data,
822a74af788SAkhil Goyal 				ses->digest_length);
82395456e89SShreyansh Jain 		start_addr = dpaa_mem_vtop(old_digest);
824a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, start_addr);
825a74af788SAkhil Goyal 		sg->length = ses->digest_length;
826a74af788SAkhil Goyal 		in_sg->length += ses->digest_length;
827a74af788SAkhil Goyal 	}
828a74af788SAkhil Goyal 	sg->final = 1;
829a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
830a74af788SAkhil Goyal 	cpu_to_hw_sg(in_sg);
831a74af788SAkhil Goyal 
832a74af788SAkhil Goyal 	return cf;
833a74af788SAkhil Goyal }
834a74af788SAkhil Goyal 
835c3e85bdcSAkhil Goyal /**
836c3e85bdcSAkhil Goyal  * packet looks like:
837c3e85bdcSAkhil Goyal  *		|<----data_len------->|
838c3e85bdcSAkhil Goyal  *    |ip_header|ah_header|icv|payload|
839c3e85bdcSAkhil Goyal  *              ^
840c3e85bdcSAkhil Goyal  *		|
841c3e85bdcSAkhil Goyal  *	   mbuf->pkt.data
842c3e85bdcSAkhil Goyal  */
843c3e85bdcSAkhil Goyal static inline struct dpaa_sec_job *
844c3e85bdcSAkhil Goyal build_auth_only(struct rte_crypto_op *op, dpaa_sec_session *ses)
845c3e85bdcSAkhil Goyal {
846c3e85bdcSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
847c3e85bdcSAkhil Goyal 	struct rte_mbuf *mbuf = sym->m_src;
848c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
849c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
850c5788a10SHemant Agrawal 	struct qm_sg_entry *sg, *in_sg;
851c4509373SSantosh Shukla 	rte_iova_t start_addr;
852c3e85bdcSAkhil Goyal 	uint8_t *old_digest;
853c5788a10SHemant Agrawal 	int data_len, data_offset;
854c5788a10SHemant Agrawal 
855c5788a10SHemant Agrawal 	data_len = sym->auth.data.length;
856c5788a10SHemant Agrawal 	data_offset = sym->auth.data.offset;
857c5788a10SHemant Agrawal 
858c5788a10SHemant Agrawal 	if (ses->auth_alg == RTE_CRYPTO_AUTH_SNOW3G_UIA2 ||
859c5788a10SHemant Agrawal 	    ses->auth_alg == RTE_CRYPTO_AUTH_ZUC_EIA3) {
860c5788a10SHemant Agrawal 		if ((data_len & 7) || (data_offset & 7)) {
861c5788a10SHemant Agrawal 			DPAA_SEC_ERR("AUTH: len/offset must be full bytes");
862c5788a10SHemant Agrawal 			return NULL;
863c5788a10SHemant Agrawal 		}
864c5788a10SHemant Agrawal 
865c5788a10SHemant Agrawal 		data_len = data_len >> 3;
866c5788a10SHemant Agrawal 		data_offset = data_offset >> 3;
867c5788a10SHemant Agrawal 	}
868c3e85bdcSAkhil Goyal 
869f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, 4);
870c3e85bdcSAkhil Goyal 	if (!ctx)
871c3e85bdcSAkhil Goyal 		return NULL;
872c3e85bdcSAkhil Goyal 
873c3e85bdcSAkhil Goyal 	cf = &ctx->job;
874c3e85bdcSAkhil Goyal 	ctx->op = op;
875c3e85bdcSAkhil Goyal 	old_digest = ctx->digest;
876c3e85bdcSAkhil Goyal 
877bfa9a8a4SThomas Monjalon 	start_addr = rte_pktmbuf_iova(mbuf);
878c3e85bdcSAkhil Goyal 	/* output */
879c3e85bdcSAkhil Goyal 	sg = &cf->sg[0];
880c3e85bdcSAkhil Goyal 	qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
881c3e85bdcSAkhil Goyal 	sg->length = ses->digest_length;
882c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
883c3e85bdcSAkhil Goyal 
884c3e85bdcSAkhil Goyal 	/* input */
885c5788a10SHemant Agrawal 	in_sg = &cf->sg[1];
886c3e85bdcSAkhil Goyal 	/* need to extend the input to a compound frame */
887c5788a10SHemant Agrawal 	in_sg->extension = 1;
888c5788a10SHemant Agrawal 	in_sg->final = 1;
889c5788a10SHemant Agrawal 	in_sg->length = data_len;
890c5788a10SHemant Agrawal 	qm_sg_entry_set64(in_sg, dpaa_mem_vtop(&cf->sg[2]));
891c3e85bdcSAkhil Goyal 	sg = &cf->sg[2];
892c5788a10SHemant Agrawal 
893c5788a10SHemant Agrawal 	if (ses->iv.length) {
894c5788a10SHemant Agrawal 		uint8_t *iv_ptr;
895c5788a10SHemant Agrawal 
896c5788a10SHemant Agrawal 		iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
897c5788a10SHemant Agrawal 						   ses->iv.offset);
898c5788a10SHemant Agrawal 
899c5788a10SHemant Agrawal 		if (ses->auth_alg == RTE_CRYPTO_AUTH_SNOW3G_UIA2) {
900c5788a10SHemant Agrawal 			iv_ptr = conv_to_snow_f9_iv(iv_ptr);
901c5788a10SHemant Agrawal 			sg->length = 12;
902c5788a10SHemant Agrawal 		} else if (ses->auth_alg == RTE_CRYPTO_AUTH_ZUC_EIA3) {
903c5788a10SHemant Agrawal 			iv_ptr = conv_to_zuc_eia_iv(iv_ptr);
904c5788a10SHemant Agrawal 			sg->length = 8;
905c5788a10SHemant Agrawal 		} else {
906c5788a10SHemant Agrawal 			sg->length = ses->iv.length;
907c5788a10SHemant Agrawal 		}
908c5788a10SHemant Agrawal 		qm_sg_entry_set64(sg, dpaa_mem_vtop(iv_ptr));
909c5788a10SHemant Agrawal 		in_sg->length += sg->length;
910c5788a10SHemant Agrawal 		cpu_to_hw_sg(sg);
911c5788a10SHemant Agrawal 		sg++;
912c5788a10SHemant Agrawal 	}
913c5788a10SHemant Agrawal 
914c5788a10SHemant Agrawal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
915c5788a10SHemant Agrawal 	sg->offset = data_offset;
916c5788a10SHemant Agrawal 	sg->length = data_len;
917c5788a10SHemant Agrawal 
918c5788a10SHemant Agrawal 	if (is_decode(ses)) {
919c5788a10SHemant Agrawal 		/* Digest verification case */
920c5788a10SHemant Agrawal 		cpu_to_hw_sg(sg);
921c3e85bdcSAkhil Goyal 		/* hash result or digest, save digest first */
922c3e85bdcSAkhil Goyal 		rte_memcpy(old_digest, sym->auth.digest.data,
923c3e85bdcSAkhil Goyal 				ses->digest_length);
924c3e85bdcSAkhil Goyal 		/* let's check digest by hw */
92595456e89SShreyansh Jain 		start_addr = dpaa_mem_vtop(old_digest);
926c3e85bdcSAkhil Goyal 		sg++;
927c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, start_addr);
928c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
929c5788a10SHemant Agrawal 		in_sg->length += ses->digest_length;
930c3e85bdcSAkhil Goyal 	}
931c5788a10SHemant Agrawal 	sg->final = 1;
932c5788a10SHemant Agrawal 	cpu_to_hw_sg(sg);
933c5788a10SHemant Agrawal 	cpu_to_hw_sg(in_sg);
934c3e85bdcSAkhil Goyal 
935c3e85bdcSAkhil Goyal 	return cf;
936c3e85bdcSAkhil Goyal }
937c3e85bdcSAkhil Goyal 
938c3e85bdcSAkhil Goyal static inline struct dpaa_sec_job *
939a74af788SAkhil Goyal build_cipher_only_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
940a74af788SAkhil Goyal {
941a74af788SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
942a74af788SAkhil Goyal 	struct dpaa_sec_job *cf;
943a74af788SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
944a74af788SAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
945a74af788SAkhil Goyal 	struct rte_mbuf *mbuf;
946a74af788SAkhil Goyal 	uint8_t req_segs;
947a74af788SAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
948a74af788SAkhil Goyal 			ses->iv.offset);
949c5788a10SHemant Agrawal 	int data_len, data_offset;
950c5788a10SHemant Agrawal 
951c5788a10SHemant Agrawal 	data_len = sym->cipher.data.length;
952c5788a10SHemant Agrawal 	data_offset = sym->cipher.data.offset;
953c5788a10SHemant Agrawal 
954c5788a10SHemant Agrawal 	if (ses->cipher_alg == RTE_CRYPTO_CIPHER_SNOW3G_UEA2 ||
955c5788a10SHemant Agrawal 		ses->cipher_alg == RTE_CRYPTO_CIPHER_ZUC_EEA3) {
956c5788a10SHemant Agrawal 		if ((data_len & 7) || (data_offset & 7)) {
957c5788a10SHemant Agrawal 			DPAA_SEC_ERR("CIPHER: len/offset must be full bytes");
958c5788a10SHemant Agrawal 			return NULL;
959c5788a10SHemant Agrawal 		}
960c5788a10SHemant Agrawal 
961c5788a10SHemant Agrawal 		data_len = data_len >> 3;
962c5788a10SHemant Agrawal 		data_offset = data_offset >> 3;
963c5788a10SHemant Agrawal 	}
964a74af788SAkhil Goyal 
965a74af788SAkhil Goyal 	if (sym->m_dst) {
966a74af788SAkhil Goyal 		mbuf = sym->m_dst;
967a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 3;
968a74af788SAkhil Goyal 	} else {
969a74af788SAkhil Goyal 		mbuf = sym->m_src;
970a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs * 2 + 3;
971a74af788SAkhil Goyal 	}
972f7a5752eSHemant Agrawal 	if (mbuf->nb_segs > MAX_SG_ENTRIES) {
973f163231eSHemant Agrawal 		DPAA_SEC_DP_ERR("Cipher: Max sec segs supported is %d",
974a74af788SAkhil Goyal 				MAX_SG_ENTRIES);
975a74af788SAkhil Goyal 		return NULL;
976a74af788SAkhil Goyal 	}
977a74af788SAkhil Goyal 
978f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, req_segs);
979a74af788SAkhil Goyal 	if (!ctx)
980a74af788SAkhil Goyal 		return NULL;
981a74af788SAkhil Goyal 
982a74af788SAkhil Goyal 	cf = &ctx->job;
983a74af788SAkhil Goyal 	ctx->op = op;
984a74af788SAkhil Goyal 
985a74af788SAkhil Goyal 	/* output */
986a74af788SAkhil Goyal 	out_sg = &cf->sg[0];
987a74af788SAkhil Goyal 	out_sg->extension = 1;
988c5788a10SHemant Agrawal 	out_sg->length = data_len;
98995456e89SShreyansh Jain 	qm_sg_entry_set64(out_sg, dpaa_mem_vtop(&cf->sg[2]));
990a74af788SAkhil Goyal 	cpu_to_hw_sg(out_sg);
991a74af788SAkhil Goyal 
992a74af788SAkhil Goyal 	/* 1st seg */
993a74af788SAkhil Goyal 	sg = &cf->sg[2];
994a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
995c5788a10SHemant Agrawal 	sg->length = mbuf->data_len - data_offset;
996c5788a10SHemant Agrawal 	sg->offset = data_offset;
997a74af788SAkhil Goyal 
998a74af788SAkhil Goyal 	/* Successive segs */
999a74af788SAkhil Goyal 	mbuf = mbuf->next;
1000a74af788SAkhil Goyal 	while (mbuf) {
1001a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1002a74af788SAkhil Goyal 		sg++;
1003a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1004a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1005a74af788SAkhil Goyal 		mbuf = mbuf->next;
1006a74af788SAkhil Goyal 	}
1007a74af788SAkhil Goyal 	sg->final = 1;
1008a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1009a74af788SAkhil Goyal 
1010a74af788SAkhil Goyal 	/* input */
1011a74af788SAkhil Goyal 	mbuf = sym->m_src;
1012a74af788SAkhil Goyal 	in_sg = &cf->sg[1];
1013a74af788SAkhil Goyal 	in_sg->extension = 1;
1014a74af788SAkhil Goyal 	in_sg->final = 1;
1015c5788a10SHemant Agrawal 	in_sg->length = data_len + ses->iv.length;
1016a74af788SAkhil Goyal 
1017a74af788SAkhil Goyal 	sg++;
101895456e89SShreyansh Jain 	qm_sg_entry_set64(in_sg, dpaa_mem_vtop(sg));
1019a74af788SAkhil Goyal 	cpu_to_hw_sg(in_sg);
1020a74af788SAkhil Goyal 
1021a74af788SAkhil Goyal 	/* IV */
1022a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1023a74af788SAkhil Goyal 	sg->length = ses->iv.length;
1024a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1025a74af788SAkhil Goyal 
1026a74af788SAkhil Goyal 	/* 1st seg */
1027a74af788SAkhil Goyal 	sg++;
1028a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1029c5788a10SHemant Agrawal 	sg->length = mbuf->data_len - data_offset;
1030c5788a10SHemant Agrawal 	sg->offset = data_offset;
1031a74af788SAkhil Goyal 
1032a74af788SAkhil Goyal 	/* Successive segs */
1033a74af788SAkhil Goyal 	mbuf = mbuf->next;
1034a74af788SAkhil Goyal 	while (mbuf) {
1035a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1036a74af788SAkhil Goyal 		sg++;
1037a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1038a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1039a74af788SAkhil Goyal 		mbuf = mbuf->next;
1040a74af788SAkhil Goyal 	}
1041a74af788SAkhil Goyal 	sg->final = 1;
1042a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1043a74af788SAkhil Goyal 
1044a74af788SAkhil Goyal 	return cf;
1045a74af788SAkhil Goyal }
1046a74af788SAkhil Goyal 
1047a74af788SAkhil Goyal static inline struct dpaa_sec_job *
1048c3e85bdcSAkhil Goyal build_cipher_only(struct rte_crypto_op *op, dpaa_sec_session *ses)
1049c3e85bdcSAkhil Goyal {
1050c3e85bdcSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1051c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
1052c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1053c3e85bdcSAkhil Goyal 	struct qm_sg_entry *sg;
1054c4509373SSantosh Shukla 	rte_iova_t src_start_addr, dst_start_addr;
1055c3e85bdcSAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1056c3e85bdcSAkhil Goyal 			ses->iv.offset);
1057c5788a10SHemant Agrawal 	int data_len, data_offset;
1058c5788a10SHemant Agrawal 
1059c5788a10SHemant Agrawal 	data_len = sym->cipher.data.length;
1060c5788a10SHemant Agrawal 	data_offset = sym->cipher.data.offset;
1061c5788a10SHemant Agrawal 
1062c5788a10SHemant Agrawal 	if (ses->cipher_alg == RTE_CRYPTO_CIPHER_SNOW3G_UEA2 ||
1063c5788a10SHemant Agrawal 		ses->cipher_alg == RTE_CRYPTO_CIPHER_ZUC_EEA3) {
1064c5788a10SHemant Agrawal 		if ((data_len & 7) || (data_offset & 7)) {
1065c5788a10SHemant Agrawal 			DPAA_SEC_ERR("CIPHER: len/offset must be full bytes");
1066c5788a10SHemant Agrawal 			return NULL;
1067c5788a10SHemant Agrawal 		}
1068c5788a10SHemant Agrawal 
1069c5788a10SHemant Agrawal 		data_len = data_len >> 3;
1070c5788a10SHemant Agrawal 		data_offset = data_offset >> 3;
1071c5788a10SHemant Agrawal 	}
1072c3e85bdcSAkhil Goyal 
1073f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, 4);
1074c3e85bdcSAkhil Goyal 	if (!ctx)
1075c3e85bdcSAkhil Goyal 		return NULL;
1076c3e85bdcSAkhil Goyal 
1077c3e85bdcSAkhil Goyal 	cf = &ctx->job;
1078c3e85bdcSAkhil Goyal 	ctx->op = op;
1079a389434eSAlok Makhariya 
1080bfa9a8a4SThomas Monjalon 	src_start_addr = rte_pktmbuf_iova(sym->m_src);
1081a389434eSAlok Makhariya 
1082a389434eSAlok Makhariya 	if (sym->m_dst)
1083bfa9a8a4SThomas Monjalon 		dst_start_addr = rte_pktmbuf_iova(sym->m_dst);
1084a389434eSAlok Makhariya 	else
1085a389434eSAlok Makhariya 		dst_start_addr = src_start_addr;
1086c3e85bdcSAkhil Goyal 
1087c3e85bdcSAkhil Goyal 	/* output */
1088c3e85bdcSAkhil Goyal 	sg = &cf->sg[0];
1089c5788a10SHemant Agrawal 	qm_sg_entry_set64(sg, dst_start_addr + data_offset);
1090c5788a10SHemant Agrawal 	sg->length = data_len + ses->iv.length;
1091c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1092c3e85bdcSAkhil Goyal 
1093c3e85bdcSAkhil Goyal 	/* input */
1094c3e85bdcSAkhil Goyal 	sg = &cf->sg[1];
1095c3e85bdcSAkhil Goyal 
1096c3e85bdcSAkhil Goyal 	/* need to extend the input to a compound frame */
1097c3e85bdcSAkhil Goyal 	sg->extension = 1;
1098c3e85bdcSAkhil Goyal 	sg->final = 1;
1099c5788a10SHemant Agrawal 	sg->length = data_len + ses->iv.length;
110095456e89SShreyansh Jain 	qm_sg_entry_set64(sg, dpaa_mem_vtop(&cf->sg[2]));
1101c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1102c3e85bdcSAkhil Goyal 
1103c3e85bdcSAkhil Goyal 	sg = &cf->sg[2];
1104c3e85bdcSAkhil Goyal 	qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1105c3e85bdcSAkhil Goyal 	sg->length = ses->iv.length;
1106c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1107c3e85bdcSAkhil Goyal 
1108c3e85bdcSAkhil Goyal 	sg++;
1109c5788a10SHemant Agrawal 	qm_sg_entry_set64(sg, src_start_addr + data_offset);
1110c5788a10SHemant Agrawal 	sg->length = data_len;
1111c3e85bdcSAkhil Goyal 	sg->final = 1;
1112c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1113c3e85bdcSAkhil Goyal 
1114c3e85bdcSAkhil Goyal 	return cf;
1115c3e85bdcSAkhil Goyal }
1116c3e85bdcSAkhil Goyal 
1117c3e85bdcSAkhil Goyal static inline struct dpaa_sec_job *
1118a74af788SAkhil Goyal build_cipher_auth_gcm_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
1119a74af788SAkhil Goyal {
1120a74af788SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1121a74af788SAkhil Goyal 	struct dpaa_sec_job *cf;
1122a74af788SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1123a74af788SAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
1124a74af788SAkhil Goyal 	struct rte_mbuf *mbuf;
1125a74af788SAkhil Goyal 	uint8_t req_segs;
1126a74af788SAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1127a74af788SAkhil Goyal 			ses->iv.offset);
1128a74af788SAkhil Goyal 
1129a74af788SAkhil Goyal 	if (sym->m_dst) {
1130a74af788SAkhil Goyal 		mbuf = sym->m_dst;
1131a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 4;
1132a74af788SAkhil Goyal 	} else {
1133a74af788SAkhil Goyal 		mbuf = sym->m_src;
1134a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs * 2 + 4;
1135a74af788SAkhil Goyal 	}
1136a74af788SAkhil Goyal 
1137a74af788SAkhil Goyal 	if (ses->auth_only_len)
1138a74af788SAkhil Goyal 		req_segs++;
1139a74af788SAkhil Goyal 
1140f7a5752eSHemant Agrawal 	if (mbuf->nb_segs > MAX_SG_ENTRIES) {
1141f163231eSHemant Agrawal 		DPAA_SEC_DP_ERR("AEAD: Max sec segs supported is %d",
1142a74af788SAkhil Goyal 				MAX_SG_ENTRIES);
1143a74af788SAkhil Goyal 		return NULL;
1144a74af788SAkhil Goyal 	}
1145a74af788SAkhil Goyal 
1146f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, req_segs);
1147a74af788SAkhil Goyal 	if (!ctx)
1148a74af788SAkhil Goyal 		return NULL;
1149a74af788SAkhil Goyal 
1150a74af788SAkhil Goyal 	cf = &ctx->job;
1151a74af788SAkhil Goyal 	ctx->op = op;
1152a74af788SAkhil Goyal 
1153a74af788SAkhil Goyal 	rte_prefetch0(cf->sg);
1154a74af788SAkhil Goyal 
1155a74af788SAkhil Goyal 	/* output */
1156a74af788SAkhil Goyal 	out_sg = &cf->sg[0];
1157a74af788SAkhil Goyal 	out_sg->extension = 1;
1158a74af788SAkhil Goyal 	if (is_encode(ses))
11597a4a6da4SVakul Garg 		out_sg->length = sym->aead.data.length + ses->digest_length;
1160a74af788SAkhil Goyal 	else
11617a4a6da4SVakul Garg 		out_sg->length = sym->aead.data.length;
1162a74af788SAkhil Goyal 
1163a74af788SAkhil Goyal 	/* output sg entries */
1164a74af788SAkhil Goyal 	sg = &cf->sg[2];
116595456e89SShreyansh Jain 	qm_sg_entry_set64(out_sg, dpaa_mem_vtop(sg));
1166a74af788SAkhil Goyal 	cpu_to_hw_sg(out_sg);
1167a74af788SAkhil Goyal 
1168a74af788SAkhil Goyal 	/* 1st seg */
1169a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
11707a4a6da4SVakul Garg 	sg->length = mbuf->data_len - sym->aead.data.offset;
11717a4a6da4SVakul Garg 	sg->offset = sym->aead.data.offset;
1172a74af788SAkhil Goyal 
1173a74af788SAkhil Goyal 	/* Successive segs */
1174a74af788SAkhil Goyal 	mbuf = mbuf->next;
1175a74af788SAkhil Goyal 	while (mbuf) {
1176a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1177a74af788SAkhil Goyal 		sg++;
1178a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1179a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1180a74af788SAkhil Goyal 		mbuf = mbuf->next;
1181a74af788SAkhil Goyal 	}
1182a74af788SAkhil Goyal 	sg->length -= ses->digest_length;
1183a74af788SAkhil Goyal 
1184a74af788SAkhil Goyal 	if (is_encode(ses)) {
1185a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1186a74af788SAkhil Goyal 		/* set auth output */
1187a74af788SAkhil Goyal 		sg++;
1188a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, sym->aead.digest.phys_addr);
1189a74af788SAkhil Goyal 		sg->length = ses->digest_length;
1190a74af788SAkhil Goyal 	}
1191a74af788SAkhil Goyal 	sg->final = 1;
1192a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1193a74af788SAkhil Goyal 
1194a74af788SAkhil Goyal 	/* input */
1195a74af788SAkhil Goyal 	mbuf = sym->m_src;
1196a74af788SAkhil Goyal 	in_sg = &cf->sg[1];
1197a74af788SAkhil Goyal 	in_sg->extension = 1;
1198a74af788SAkhil Goyal 	in_sg->final = 1;
1199a74af788SAkhil Goyal 	if (is_encode(ses))
1200a74af788SAkhil Goyal 		in_sg->length = ses->iv.length + sym->aead.data.length
1201a74af788SAkhil Goyal 							+ ses->auth_only_len;
1202a74af788SAkhil Goyal 	else
1203a74af788SAkhil Goyal 		in_sg->length = ses->iv.length + sym->aead.data.length
1204a74af788SAkhil Goyal 				+ ses->auth_only_len + ses->digest_length;
1205a74af788SAkhil Goyal 
1206a74af788SAkhil Goyal 	/* input sg entries */
1207a74af788SAkhil Goyal 	sg++;
120895456e89SShreyansh Jain 	qm_sg_entry_set64(in_sg, dpaa_mem_vtop(sg));
1209a74af788SAkhil Goyal 	cpu_to_hw_sg(in_sg);
1210a74af788SAkhil Goyal 
1211a74af788SAkhil Goyal 	/* 1st seg IV */
1212a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1213a74af788SAkhil Goyal 	sg->length = ses->iv.length;
1214a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1215a74af788SAkhil Goyal 
1216a74af788SAkhil Goyal 	/* 2nd seg auth only */
1217a74af788SAkhil Goyal 	if (ses->auth_only_len) {
1218a74af788SAkhil Goyal 		sg++;
1219a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, dpaa_mem_vtop(sym->aead.aad.data));
1220a74af788SAkhil Goyal 		sg->length = ses->auth_only_len;
1221a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1222a74af788SAkhil Goyal 	}
1223a74af788SAkhil Goyal 
1224a74af788SAkhil Goyal 	/* 3rd seg */
1225a74af788SAkhil Goyal 	sg++;
1226a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1227a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->aead.data.offset;
1228a74af788SAkhil Goyal 	sg->offset = sym->aead.data.offset;
1229a74af788SAkhil Goyal 
1230a74af788SAkhil Goyal 	/* Successive segs */
1231a74af788SAkhil Goyal 	mbuf = mbuf->next;
1232a74af788SAkhil Goyal 	while (mbuf) {
1233a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1234a74af788SAkhil Goyal 		sg++;
1235a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1236a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1237a74af788SAkhil Goyal 		mbuf = mbuf->next;
1238a74af788SAkhil Goyal 	}
1239a74af788SAkhil Goyal 
1240a74af788SAkhil Goyal 	if (is_decode(ses)) {
1241a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1242a74af788SAkhil Goyal 		sg++;
1243a74af788SAkhil Goyal 		memcpy(ctx->digest, sym->aead.digest.data,
1244a74af788SAkhil Goyal 			ses->digest_length);
124595456e89SShreyansh Jain 		qm_sg_entry_set64(sg, dpaa_mem_vtop(ctx->digest));
1246a74af788SAkhil Goyal 		sg->length = ses->digest_length;
1247a74af788SAkhil Goyal 	}
1248a74af788SAkhil Goyal 	sg->final = 1;
1249a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1250a74af788SAkhil Goyal 
1251a74af788SAkhil Goyal 	return cf;
1252a74af788SAkhil Goyal }
1253a74af788SAkhil Goyal 
1254a74af788SAkhil Goyal static inline struct dpaa_sec_job *
1255c3e85bdcSAkhil Goyal build_cipher_auth_gcm(struct rte_crypto_op *op, dpaa_sec_session *ses)
1256c3e85bdcSAkhil Goyal {
1257c3e85bdcSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1258c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
1259c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1260c3e85bdcSAkhil Goyal 	struct qm_sg_entry *sg;
1261c3e85bdcSAkhil Goyal 	uint32_t length = 0;
1262c4509373SSantosh Shukla 	rte_iova_t src_start_addr, dst_start_addr;
1263c3e85bdcSAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1264c3e85bdcSAkhil Goyal 			ses->iv.offset);
1265c3e85bdcSAkhil Goyal 
1266116ff44aSHemant Agrawal 	src_start_addr = sym->m_src->buf_iova + sym->m_src->data_off;
1267a389434eSAlok Makhariya 
1268a389434eSAlok Makhariya 	if (sym->m_dst)
1269116ff44aSHemant Agrawal 		dst_start_addr = sym->m_dst->buf_iova + sym->m_dst->data_off;
1270a389434eSAlok Makhariya 	else
1271a389434eSAlok Makhariya 		dst_start_addr = src_start_addr;
1272c3e85bdcSAkhil Goyal 
1273f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, 7);
1274c3e85bdcSAkhil Goyal 	if (!ctx)
1275c3e85bdcSAkhil Goyal 		return NULL;
1276c3e85bdcSAkhil Goyal 
1277c3e85bdcSAkhil Goyal 	cf = &ctx->job;
1278c3e85bdcSAkhil Goyal 	ctx->op = op;
1279c3e85bdcSAkhil Goyal 
1280c3e85bdcSAkhil Goyal 	/* input */
1281c3e85bdcSAkhil Goyal 	rte_prefetch0(cf->sg);
1282c3e85bdcSAkhil Goyal 	sg = &cf->sg[2];
128395456e89SShreyansh Jain 	qm_sg_entry_set64(&cf->sg[1], dpaa_mem_vtop(sg));
1284c3e85bdcSAkhil Goyal 	if (is_encode(ses)) {
1285c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1286c3e85bdcSAkhil Goyal 		sg->length = ses->iv.length;
1287c3e85bdcSAkhil Goyal 		length += sg->length;
1288c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1289c3e85bdcSAkhil Goyal 
1290c3e85bdcSAkhil Goyal 		sg++;
1291c3e85bdcSAkhil Goyal 		if (ses->auth_only_len) {
1292c3e85bdcSAkhil Goyal 			qm_sg_entry_set64(sg,
1293c3e85bdcSAkhil Goyal 					  dpaa_mem_vtop(sym->aead.aad.data));
1294c3e85bdcSAkhil Goyal 			sg->length = ses->auth_only_len;
1295c3e85bdcSAkhil Goyal 			length += sg->length;
1296c3e85bdcSAkhil Goyal 			cpu_to_hw_sg(sg);
1297c3e85bdcSAkhil Goyal 			sg++;
1298c3e85bdcSAkhil Goyal 		}
1299a389434eSAlok Makhariya 		qm_sg_entry_set64(sg, src_start_addr + sym->aead.data.offset);
1300c3e85bdcSAkhil Goyal 		sg->length = sym->aead.data.length;
1301c3e85bdcSAkhil Goyal 		length += sg->length;
1302c3e85bdcSAkhil Goyal 		sg->final = 1;
1303c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1304c3e85bdcSAkhil Goyal 	} else {
1305c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1306c3e85bdcSAkhil Goyal 		sg->length = ses->iv.length;
1307c3e85bdcSAkhil Goyal 		length += sg->length;
1308c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1309c3e85bdcSAkhil Goyal 
1310c3e85bdcSAkhil Goyal 		sg++;
1311c3e85bdcSAkhil Goyal 		if (ses->auth_only_len) {
1312c3e85bdcSAkhil Goyal 			qm_sg_entry_set64(sg,
1313c3e85bdcSAkhil Goyal 					  dpaa_mem_vtop(sym->aead.aad.data));
1314c3e85bdcSAkhil Goyal 			sg->length = ses->auth_only_len;
1315c3e85bdcSAkhil Goyal 			length += sg->length;
1316c3e85bdcSAkhil Goyal 			cpu_to_hw_sg(sg);
1317c3e85bdcSAkhil Goyal 			sg++;
1318c3e85bdcSAkhil Goyal 		}
1319a389434eSAlok Makhariya 		qm_sg_entry_set64(sg, src_start_addr + sym->aead.data.offset);
1320c3e85bdcSAkhil Goyal 		sg->length = sym->aead.data.length;
1321c3e85bdcSAkhil Goyal 		length += sg->length;
1322c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1323c3e85bdcSAkhil Goyal 
1324c3e85bdcSAkhil Goyal 		memcpy(ctx->digest, sym->aead.digest.data,
1325c3e85bdcSAkhil Goyal 		       ses->digest_length);
1326c3e85bdcSAkhil Goyal 		sg++;
1327c3e85bdcSAkhil Goyal 
132895456e89SShreyansh Jain 		qm_sg_entry_set64(sg, dpaa_mem_vtop(ctx->digest));
1329c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
1330c3e85bdcSAkhil Goyal 		length += sg->length;
1331c3e85bdcSAkhil Goyal 		sg->final = 1;
1332c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1333c3e85bdcSAkhil Goyal 	}
1334c3e85bdcSAkhil Goyal 	/* input compound frame */
1335c3e85bdcSAkhil Goyal 	cf->sg[1].length = length;
1336c3e85bdcSAkhil Goyal 	cf->sg[1].extension = 1;
1337c3e85bdcSAkhil Goyal 	cf->sg[1].final = 1;
1338c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(&cf->sg[1]);
1339c3e85bdcSAkhil Goyal 
1340c3e85bdcSAkhil Goyal 	/* output */
1341c3e85bdcSAkhil Goyal 	sg++;
134295456e89SShreyansh Jain 	qm_sg_entry_set64(&cf->sg[0], dpaa_mem_vtop(sg));
1343c3e85bdcSAkhil Goyal 	qm_sg_entry_set64(sg,
13447a4a6da4SVakul Garg 		dst_start_addr + sym->aead.data.offset);
13457a4a6da4SVakul Garg 	sg->length = sym->aead.data.length;
1346c3e85bdcSAkhil Goyal 	length = sg->length;
1347c3e85bdcSAkhil Goyal 	if (is_encode(ses)) {
1348c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1349c3e85bdcSAkhil Goyal 		/* set auth output */
1350c3e85bdcSAkhil Goyal 		sg++;
1351c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, sym->aead.digest.phys_addr);
1352c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
1353c3e85bdcSAkhil Goyal 		length += sg->length;
1354c3e85bdcSAkhil Goyal 	}
1355c3e85bdcSAkhil Goyal 	sg->final = 1;
1356c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1357c3e85bdcSAkhil Goyal 
1358c3e85bdcSAkhil Goyal 	/* output compound frame */
1359c3e85bdcSAkhil Goyal 	cf->sg[0].length = length;
1360c3e85bdcSAkhil Goyal 	cf->sg[0].extension = 1;
1361c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(&cf->sg[0]);
1362c3e85bdcSAkhil Goyal 
1363c3e85bdcSAkhil Goyal 	return cf;
1364c3e85bdcSAkhil Goyal }
1365c3e85bdcSAkhil Goyal 
1366c3e85bdcSAkhil Goyal static inline struct dpaa_sec_job *
1367a74af788SAkhil Goyal build_cipher_auth_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
1368a74af788SAkhil Goyal {
1369a74af788SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1370a74af788SAkhil Goyal 	struct dpaa_sec_job *cf;
1371a74af788SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1372a74af788SAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
1373a74af788SAkhil Goyal 	struct rte_mbuf *mbuf;
1374a74af788SAkhil Goyal 	uint8_t req_segs;
1375a74af788SAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1376a74af788SAkhil Goyal 			ses->iv.offset);
1377a74af788SAkhil Goyal 
1378a74af788SAkhil Goyal 	if (sym->m_dst) {
1379a74af788SAkhil Goyal 		mbuf = sym->m_dst;
1380a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 4;
1381a74af788SAkhil Goyal 	} else {
1382a74af788SAkhil Goyal 		mbuf = sym->m_src;
1383a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs * 2 + 4;
1384a74af788SAkhil Goyal 	}
1385a74af788SAkhil Goyal 
1386f7a5752eSHemant Agrawal 	if (mbuf->nb_segs > MAX_SG_ENTRIES) {
1387f163231eSHemant Agrawal 		DPAA_SEC_DP_ERR("Cipher-Auth: Max sec segs supported is %d",
1388a74af788SAkhil Goyal 				MAX_SG_ENTRIES);
1389a74af788SAkhil Goyal 		return NULL;
1390a74af788SAkhil Goyal 	}
1391a74af788SAkhil Goyal 
1392f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, req_segs);
1393a74af788SAkhil Goyal 	if (!ctx)
1394a74af788SAkhil Goyal 		return NULL;
1395a74af788SAkhil Goyal 
1396a74af788SAkhil Goyal 	cf = &ctx->job;
1397a74af788SAkhil Goyal 	ctx->op = op;
1398a74af788SAkhil Goyal 
1399a74af788SAkhil Goyal 	rte_prefetch0(cf->sg);
1400a74af788SAkhil Goyal 
1401a74af788SAkhil Goyal 	/* output */
1402a74af788SAkhil Goyal 	out_sg = &cf->sg[0];
1403a74af788SAkhil Goyal 	out_sg->extension = 1;
1404a74af788SAkhil Goyal 	if (is_encode(ses))
1405a74af788SAkhil Goyal 		out_sg->length = sym->auth.data.length + ses->digest_length;
1406a74af788SAkhil Goyal 	else
1407a74af788SAkhil Goyal 		out_sg->length = sym->auth.data.length;
1408a74af788SAkhil Goyal 
1409a74af788SAkhil Goyal 	/* output sg entries */
1410a74af788SAkhil Goyal 	sg = &cf->sg[2];
141195456e89SShreyansh Jain 	qm_sg_entry_set64(out_sg, dpaa_mem_vtop(sg));
1412a74af788SAkhil Goyal 	cpu_to_hw_sg(out_sg);
1413a74af788SAkhil Goyal 
1414a74af788SAkhil Goyal 	/* 1st seg */
1415a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1416a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->auth.data.offset;
1417a74af788SAkhil Goyal 	sg->offset = sym->auth.data.offset;
1418a74af788SAkhil Goyal 
1419a74af788SAkhil Goyal 	/* Successive segs */
1420a74af788SAkhil Goyal 	mbuf = mbuf->next;
1421a74af788SAkhil Goyal 	while (mbuf) {
1422a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1423a74af788SAkhil Goyal 		sg++;
1424a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1425a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1426a74af788SAkhil Goyal 		mbuf = mbuf->next;
1427a74af788SAkhil Goyal 	}
1428a74af788SAkhil Goyal 	sg->length -= ses->digest_length;
1429a74af788SAkhil Goyal 
1430a74af788SAkhil Goyal 	if (is_encode(ses)) {
1431a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1432a74af788SAkhil Goyal 		/* set auth output */
1433a74af788SAkhil Goyal 		sg++;
1434a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
1435a74af788SAkhil Goyal 		sg->length = ses->digest_length;
1436a74af788SAkhil Goyal 	}
1437a74af788SAkhil Goyal 	sg->final = 1;
1438a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1439a74af788SAkhil Goyal 
1440a74af788SAkhil Goyal 	/* input */
1441a74af788SAkhil Goyal 	mbuf = sym->m_src;
1442a74af788SAkhil Goyal 	in_sg = &cf->sg[1];
1443a74af788SAkhil Goyal 	in_sg->extension = 1;
1444a74af788SAkhil Goyal 	in_sg->final = 1;
1445a74af788SAkhil Goyal 	if (is_encode(ses))
1446a74af788SAkhil Goyal 		in_sg->length = ses->iv.length + sym->auth.data.length;
1447a74af788SAkhil Goyal 	else
1448a74af788SAkhil Goyal 		in_sg->length = ses->iv.length + sym->auth.data.length
1449a74af788SAkhil Goyal 						+ ses->digest_length;
1450a74af788SAkhil Goyal 
1451a74af788SAkhil Goyal 	/* input sg entries */
1452a74af788SAkhil Goyal 	sg++;
145395456e89SShreyansh Jain 	qm_sg_entry_set64(in_sg, dpaa_mem_vtop(sg));
1454a74af788SAkhil Goyal 	cpu_to_hw_sg(in_sg);
1455a74af788SAkhil Goyal 
1456a74af788SAkhil Goyal 	/* 1st seg IV */
1457a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1458a74af788SAkhil Goyal 	sg->length = ses->iv.length;
1459a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1460a74af788SAkhil Goyal 
1461a74af788SAkhil Goyal 	/* 2nd seg */
1462a74af788SAkhil Goyal 	sg++;
1463a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1464a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->auth.data.offset;
1465a74af788SAkhil Goyal 	sg->offset = sym->auth.data.offset;
1466a74af788SAkhil Goyal 
1467a74af788SAkhil Goyal 	/* Successive segs */
1468a74af788SAkhil Goyal 	mbuf = mbuf->next;
1469a74af788SAkhil Goyal 	while (mbuf) {
1470a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1471a74af788SAkhil Goyal 		sg++;
1472a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1473a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1474a74af788SAkhil Goyal 		mbuf = mbuf->next;
1475a74af788SAkhil Goyal 	}
1476a74af788SAkhil Goyal 
1477a74af788SAkhil Goyal 	sg->length -= ses->digest_length;
1478a74af788SAkhil Goyal 	if (is_decode(ses)) {
1479a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1480a74af788SAkhil Goyal 		sg++;
1481a74af788SAkhil Goyal 		memcpy(ctx->digest, sym->auth.digest.data,
1482a74af788SAkhil Goyal 			ses->digest_length);
148395456e89SShreyansh Jain 		qm_sg_entry_set64(sg, dpaa_mem_vtop(ctx->digest));
1484a74af788SAkhil Goyal 		sg->length = ses->digest_length;
1485a74af788SAkhil Goyal 	}
1486a74af788SAkhil Goyal 	sg->final = 1;
1487a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1488a74af788SAkhil Goyal 
1489a74af788SAkhil Goyal 	return cf;
1490a74af788SAkhil Goyal }
1491a74af788SAkhil Goyal 
1492a74af788SAkhil Goyal static inline struct dpaa_sec_job *
1493c3e85bdcSAkhil Goyal build_cipher_auth(struct rte_crypto_op *op, dpaa_sec_session *ses)
1494c3e85bdcSAkhil Goyal {
1495c3e85bdcSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1496c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
1497c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1498c3e85bdcSAkhil Goyal 	struct qm_sg_entry *sg;
1499c4509373SSantosh Shukla 	rte_iova_t src_start_addr, dst_start_addr;
1500c3e85bdcSAkhil Goyal 	uint32_t length = 0;
1501c3e85bdcSAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1502c3e85bdcSAkhil Goyal 			ses->iv.offset);
1503c3e85bdcSAkhil Goyal 
1504455da545SSantosh Shukla 	src_start_addr = sym->m_src->buf_iova + sym->m_src->data_off;
1505a389434eSAlok Makhariya 	if (sym->m_dst)
1506455da545SSantosh Shukla 		dst_start_addr = sym->m_dst->buf_iova + sym->m_dst->data_off;
1507a389434eSAlok Makhariya 	else
1508a389434eSAlok Makhariya 		dst_start_addr = src_start_addr;
1509c3e85bdcSAkhil Goyal 
1510f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, 7);
1511c3e85bdcSAkhil Goyal 	if (!ctx)
1512c3e85bdcSAkhil Goyal 		return NULL;
1513c3e85bdcSAkhil Goyal 
1514c3e85bdcSAkhil Goyal 	cf = &ctx->job;
1515c3e85bdcSAkhil Goyal 	ctx->op = op;
1516c3e85bdcSAkhil Goyal 
1517c3e85bdcSAkhil Goyal 	/* input */
1518c3e85bdcSAkhil Goyal 	rte_prefetch0(cf->sg);
1519c3e85bdcSAkhil Goyal 	sg = &cf->sg[2];
152095456e89SShreyansh Jain 	qm_sg_entry_set64(&cf->sg[1], dpaa_mem_vtop(sg));
1521c3e85bdcSAkhil Goyal 	if (is_encode(ses)) {
1522c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1523c3e85bdcSAkhil Goyal 		sg->length = ses->iv.length;
1524c3e85bdcSAkhil Goyal 		length += sg->length;
1525c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1526c3e85bdcSAkhil Goyal 
1527c3e85bdcSAkhil Goyal 		sg++;
1528a389434eSAlok Makhariya 		qm_sg_entry_set64(sg, src_start_addr + sym->auth.data.offset);
1529c3e85bdcSAkhil Goyal 		sg->length = sym->auth.data.length;
1530c3e85bdcSAkhil Goyal 		length += sg->length;
1531c3e85bdcSAkhil Goyal 		sg->final = 1;
1532c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1533c3e85bdcSAkhil Goyal 	} else {
1534c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1535c3e85bdcSAkhil Goyal 		sg->length = ses->iv.length;
1536c3e85bdcSAkhil Goyal 		length += sg->length;
1537c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1538c3e85bdcSAkhil Goyal 
1539c3e85bdcSAkhil Goyal 		sg++;
1540c3e85bdcSAkhil Goyal 
1541a389434eSAlok Makhariya 		qm_sg_entry_set64(sg, src_start_addr + sym->auth.data.offset);
1542c3e85bdcSAkhil Goyal 		sg->length = sym->auth.data.length;
1543c3e85bdcSAkhil Goyal 		length += sg->length;
1544c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1545c3e85bdcSAkhil Goyal 
1546c3e85bdcSAkhil Goyal 		memcpy(ctx->digest, sym->auth.digest.data,
1547c3e85bdcSAkhil Goyal 		       ses->digest_length);
1548c3e85bdcSAkhil Goyal 		sg++;
1549c3e85bdcSAkhil Goyal 
155095456e89SShreyansh Jain 		qm_sg_entry_set64(sg, dpaa_mem_vtop(ctx->digest));
1551c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
1552c3e85bdcSAkhil Goyal 		length += sg->length;
1553c3e85bdcSAkhil Goyal 		sg->final = 1;
1554c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1555c3e85bdcSAkhil Goyal 	}
1556c3e85bdcSAkhil Goyal 	/* input compound frame */
1557c3e85bdcSAkhil Goyal 	cf->sg[1].length = length;
1558c3e85bdcSAkhil Goyal 	cf->sg[1].extension = 1;
1559c3e85bdcSAkhil Goyal 	cf->sg[1].final = 1;
1560c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(&cf->sg[1]);
1561c3e85bdcSAkhil Goyal 
1562c3e85bdcSAkhil Goyal 	/* output */
1563c3e85bdcSAkhil Goyal 	sg++;
156495456e89SShreyansh Jain 	qm_sg_entry_set64(&cf->sg[0], dpaa_mem_vtop(sg));
1565a389434eSAlok Makhariya 	qm_sg_entry_set64(sg, dst_start_addr + sym->cipher.data.offset);
1566c3e85bdcSAkhil Goyal 	sg->length = sym->cipher.data.length;
1567c3e85bdcSAkhil Goyal 	length = sg->length;
1568c3e85bdcSAkhil Goyal 	if (is_encode(ses)) {
1569c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1570c3e85bdcSAkhil Goyal 		/* set auth output */
1571c3e85bdcSAkhil Goyal 		sg++;
1572c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
1573c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
1574c3e85bdcSAkhil Goyal 		length += sg->length;
1575c3e85bdcSAkhil Goyal 	}
1576c3e85bdcSAkhil Goyal 	sg->final = 1;
1577c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1578c3e85bdcSAkhil Goyal 
1579c3e85bdcSAkhil Goyal 	/* output compound frame */
1580c3e85bdcSAkhil Goyal 	cf->sg[0].length = length;
1581c3e85bdcSAkhil Goyal 	cf->sg[0].extension = 1;
1582c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(&cf->sg[0]);
1583c3e85bdcSAkhil Goyal 
1584c3e85bdcSAkhil Goyal 	return cf;
1585c3e85bdcSAkhil Goyal }
1586c3e85bdcSAkhil Goyal 
1587314424b6SHemant Agrawal #ifdef RTE_LIBRTE_SECURITY
15881f14d500SAkhil Goyal static inline struct dpaa_sec_job *
15891f14d500SAkhil Goyal build_proto(struct rte_crypto_op *op, dpaa_sec_session *ses)
15901f14d500SAkhil Goyal {
15911f14d500SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
15921f14d500SAkhil Goyal 	struct dpaa_sec_job *cf;
15931f14d500SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
15941f14d500SAkhil Goyal 	struct qm_sg_entry *sg;
15951f14d500SAkhil Goyal 	phys_addr_t src_start_addr, dst_start_addr;
15961f14d500SAkhil Goyal 
1597f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, 2);
15981f14d500SAkhil Goyal 	if (!ctx)
15991f14d500SAkhil Goyal 		return NULL;
16001f14d500SAkhil Goyal 	cf = &ctx->job;
16011f14d500SAkhil Goyal 	ctx->op = op;
16021f14d500SAkhil Goyal 
16031f14d500SAkhil Goyal 	src_start_addr = rte_pktmbuf_mtophys(sym->m_src);
16041f14d500SAkhil Goyal 
16051f14d500SAkhil Goyal 	if (sym->m_dst)
16061f14d500SAkhil Goyal 		dst_start_addr = rte_pktmbuf_mtophys(sym->m_dst);
16071f14d500SAkhil Goyal 	else
16081f14d500SAkhil Goyal 		dst_start_addr = src_start_addr;
16091f14d500SAkhil Goyal 
16101f14d500SAkhil Goyal 	/* input */
16111f14d500SAkhil Goyal 	sg = &cf->sg[1];
16121f14d500SAkhil Goyal 	qm_sg_entry_set64(sg, src_start_addr);
16131f14d500SAkhil Goyal 	sg->length = sym->m_src->pkt_len;
16141f14d500SAkhil Goyal 	sg->final = 1;
16151f14d500SAkhil Goyal 	cpu_to_hw_sg(sg);
16161f14d500SAkhil Goyal 
16171f14d500SAkhil Goyal 	sym->m_src->packet_type &= ~RTE_PTYPE_L4_MASK;
16181f14d500SAkhil Goyal 	/* output */
16191f14d500SAkhil Goyal 	sg = &cf->sg[0];
16201f14d500SAkhil Goyal 	qm_sg_entry_set64(sg, dst_start_addr);
16211f14d500SAkhil Goyal 	sg->length = sym->m_src->buf_len - sym->m_src->data_off;
16221f14d500SAkhil Goyal 	cpu_to_hw_sg(sg);
16231f14d500SAkhil Goyal 
16241f14d500SAkhil Goyal 	return cf;
16251f14d500SAkhil Goyal }
16261f14d500SAkhil Goyal 
1627fb5c100aSAkhil Goyal static inline struct dpaa_sec_job *
1628fb5c100aSAkhil Goyal build_proto_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
1629fb5c100aSAkhil Goyal {
1630fb5c100aSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1631fb5c100aSAkhil Goyal 	struct dpaa_sec_job *cf;
1632fb5c100aSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1633fb5c100aSAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
1634fb5c100aSAkhil Goyal 	struct rte_mbuf *mbuf;
1635fb5c100aSAkhil Goyal 	uint8_t req_segs;
1636fb5c100aSAkhil Goyal 	uint32_t in_len = 0, out_len = 0;
1637fb5c100aSAkhil Goyal 
1638fb5c100aSAkhil Goyal 	if (sym->m_dst)
1639fb5c100aSAkhil Goyal 		mbuf = sym->m_dst;
1640fb5c100aSAkhil Goyal 	else
1641fb5c100aSAkhil Goyal 		mbuf = sym->m_src;
1642fb5c100aSAkhil Goyal 
1643fb5c100aSAkhil Goyal 	req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 2;
1644f7a5752eSHemant Agrawal 	if (mbuf->nb_segs > MAX_SG_ENTRIES) {
1645fb5c100aSAkhil Goyal 		DPAA_SEC_DP_ERR("Proto: Max sec segs supported is %d",
1646fb5c100aSAkhil Goyal 				MAX_SG_ENTRIES);
1647fb5c100aSAkhil Goyal 		return NULL;
1648fb5c100aSAkhil Goyal 	}
1649fb5c100aSAkhil Goyal 
1650f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, req_segs);
1651fb5c100aSAkhil Goyal 	if (!ctx)
1652fb5c100aSAkhil Goyal 		return NULL;
1653fb5c100aSAkhil Goyal 	cf = &ctx->job;
1654fb5c100aSAkhil Goyal 	ctx->op = op;
1655fb5c100aSAkhil Goyal 	/* output */
1656fb5c100aSAkhil Goyal 	out_sg = &cf->sg[0];
1657fb5c100aSAkhil Goyal 	out_sg->extension = 1;
1658fb5c100aSAkhil Goyal 	qm_sg_entry_set64(out_sg, dpaa_mem_vtop(&cf->sg[2]));
1659fb5c100aSAkhil Goyal 
1660fb5c100aSAkhil Goyal 	/* 1st seg */
1661fb5c100aSAkhil Goyal 	sg = &cf->sg[2];
1662fb5c100aSAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1663fb5c100aSAkhil Goyal 	sg->offset = 0;
1664fb5c100aSAkhil Goyal 
1665fb5c100aSAkhil Goyal 	/* Successive segs */
1666fb5c100aSAkhil Goyal 	while (mbuf->next) {
1667fb5c100aSAkhil Goyal 		sg->length = mbuf->data_len;
1668fb5c100aSAkhil Goyal 		out_len += sg->length;
1669fb5c100aSAkhil Goyal 		mbuf = mbuf->next;
1670fb5c100aSAkhil Goyal 		cpu_to_hw_sg(sg);
1671fb5c100aSAkhil Goyal 		sg++;
1672fb5c100aSAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1673fb5c100aSAkhil Goyal 		sg->offset = 0;
1674fb5c100aSAkhil Goyal 	}
1675fb5c100aSAkhil Goyal 	sg->length = mbuf->buf_len - mbuf->data_off;
1676fb5c100aSAkhil Goyal 	out_len += sg->length;
1677fb5c100aSAkhil Goyal 	sg->final = 1;
1678fb5c100aSAkhil Goyal 	cpu_to_hw_sg(sg);
1679fb5c100aSAkhil Goyal 
1680fb5c100aSAkhil Goyal 	out_sg->length = out_len;
1681fb5c100aSAkhil Goyal 	cpu_to_hw_sg(out_sg);
1682fb5c100aSAkhil Goyal 
1683fb5c100aSAkhil Goyal 	/* input */
1684fb5c100aSAkhil Goyal 	mbuf = sym->m_src;
1685fb5c100aSAkhil Goyal 	in_sg = &cf->sg[1];
1686fb5c100aSAkhil Goyal 	in_sg->extension = 1;
1687fb5c100aSAkhil Goyal 	in_sg->final = 1;
1688fb5c100aSAkhil Goyal 	in_len = mbuf->data_len;
1689fb5c100aSAkhil Goyal 
1690fb5c100aSAkhil Goyal 	sg++;
1691fb5c100aSAkhil Goyal 	qm_sg_entry_set64(in_sg, dpaa_mem_vtop(sg));
1692fb5c100aSAkhil Goyal 
1693fb5c100aSAkhil Goyal 	/* 1st seg */
1694fb5c100aSAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1695fb5c100aSAkhil Goyal 	sg->length = mbuf->data_len;
1696fb5c100aSAkhil Goyal 	sg->offset = 0;
1697fb5c100aSAkhil Goyal 
1698fb5c100aSAkhil Goyal 	/* Successive segs */
1699fb5c100aSAkhil Goyal 	mbuf = mbuf->next;
1700fb5c100aSAkhil Goyal 	while (mbuf) {
1701fb5c100aSAkhil Goyal 		cpu_to_hw_sg(sg);
1702fb5c100aSAkhil Goyal 		sg++;
1703fb5c100aSAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1704fb5c100aSAkhil Goyal 		sg->length = mbuf->data_len;
1705fb5c100aSAkhil Goyal 		sg->offset = 0;
1706fb5c100aSAkhil Goyal 		in_len += sg->length;
1707fb5c100aSAkhil Goyal 		mbuf = mbuf->next;
1708fb5c100aSAkhil Goyal 	}
1709fb5c100aSAkhil Goyal 	sg->final = 1;
1710fb5c100aSAkhil Goyal 	cpu_to_hw_sg(sg);
1711fb5c100aSAkhil Goyal 
1712fb5c100aSAkhil Goyal 	in_sg->length = in_len;
1713fb5c100aSAkhil Goyal 	cpu_to_hw_sg(in_sg);
1714fb5c100aSAkhil Goyal 
1715fb5c100aSAkhil Goyal 	sym->m_src->packet_type &= ~RTE_PTYPE_L4_MASK;
1716fb5c100aSAkhil Goyal 
1717fb5c100aSAkhil Goyal 	return cf;
1718fb5c100aSAkhil Goyal }
1719314424b6SHemant Agrawal #endif
1720fb5c100aSAkhil Goyal 
17219a984458SAkhil Goyal static uint16_t
17229a984458SAkhil Goyal dpaa_sec_enqueue_burst(void *qp, struct rte_crypto_op **ops,
17239a984458SAkhil Goyal 		       uint16_t nb_ops)
1724c3e85bdcSAkhil Goyal {
17259a984458SAkhil Goyal 	/* Function to transmit the frames to given device and queuepair */
17269a984458SAkhil Goyal 	uint32_t loop;
17279a984458SAkhil Goyal 	struct dpaa_sec_qp *dpaa_qp = (struct dpaa_sec_qp *)qp;
17289a984458SAkhil Goyal 	uint16_t num_tx = 0;
17299a984458SAkhil Goyal 	struct qm_fd fds[DPAA_SEC_BURST], *fd;
17309a984458SAkhil Goyal 	uint32_t frames_to_send;
17319a984458SAkhil Goyal 	struct rte_crypto_op *op;
1732c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
1733c3e85bdcSAkhil Goyal 	dpaa_sec_session *ses;
17343394ed47SVakul Garg 	uint16_t auth_hdr_len, auth_tail_len;
17353394ed47SVakul Garg 	uint32_t index, flags[DPAA_SEC_BURST] = {0};
17369a984458SAkhil Goyal 	struct qman_fq *inq[DPAA_SEC_BURST];
1737c3e85bdcSAkhil Goyal 
17389a984458SAkhil Goyal 	while (nb_ops) {
17399a984458SAkhil Goyal 		frames_to_send = (nb_ops > DPAA_SEC_BURST) ?
17409a984458SAkhil Goyal 				DPAA_SEC_BURST : nb_ops;
17419a984458SAkhil Goyal 		for (loop = 0; loop < frames_to_send; loop++) {
17429a984458SAkhil Goyal 			op = *(ops++);
1743fe3688baSAkhil Goyal 			if (op->sym->m_src->seqn != 0) {
1744fe3688baSAkhil Goyal 				index = op->sym->m_src->seqn - 1;
1745fe3688baSAkhil Goyal 				if (DPAA_PER_LCORE_DQRR_HELD & (1 << index)) {
1746fe3688baSAkhil Goyal 					/* QM_EQCR_DCA_IDXMASK = 0x0f */
1747fe3688baSAkhil Goyal 					flags[loop] = ((index & 0x0f) << 8);
1748fe3688baSAkhil Goyal 					flags[loop] |= QMAN_ENQUEUE_FLAG_DCA;
1749fe3688baSAkhil Goyal 					DPAA_PER_LCORE_DQRR_SIZE--;
1750fe3688baSAkhil Goyal 					DPAA_PER_LCORE_DQRR_HELD &=
1751fe3688baSAkhil Goyal 								~(1 << index);
1752fe3688baSAkhil Goyal 				}
1753fe3688baSAkhil Goyal 			}
1754fe3688baSAkhil Goyal 
17559a984458SAkhil Goyal 			switch (op->sess_type) {
17569a984458SAkhil Goyal 			case RTE_CRYPTO_OP_WITH_SESSION:
17579a984458SAkhil Goyal 				ses = (dpaa_sec_session *)
1758012c5076SPablo de Lara 					get_sym_session_private_data(
17599a984458SAkhil Goyal 							op->sym->session,
17609a984458SAkhil Goyal 							cryptodev_driver_id);
17619a984458SAkhil Goyal 				break;
1762314424b6SHemant Agrawal #ifdef RTE_LIBRTE_SECURITY
17639a984458SAkhil Goyal 			case RTE_CRYPTO_OP_SECURITY_SESSION:
17649a984458SAkhil Goyal 				ses = (dpaa_sec_session *)
17659a984458SAkhil Goyal 					get_sec_session_private_data(
17661f14d500SAkhil Goyal 							op->sym->sec_session);
17679a984458SAkhil Goyal 				break;
1768314424b6SHemant Agrawal #endif
17699a984458SAkhil Goyal 			default:
1770f163231eSHemant Agrawal 				DPAA_SEC_DP_ERR(
17719a984458SAkhil Goyal 					"sessionless crypto op not supported");
17729a984458SAkhil Goyal 				frames_to_send = loop;
17739a984458SAkhil Goyal 				nb_ops = loop;
17749a984458SAkhil Goyal 				goto send_pkts;
17759a984458SAkhil Goyal 			}
1776e1e52232SHemant Agrawal 
1777e1e52232SHemant Agrawal 			if (!ses) {
1778e1e52232SHemant Agrawal 				DPAA_SEC_DP_ERR("session not available");
1779e1e52232SHemant Agrawal 				frames_to_send = loop;
1780e1e52232SHemant Agrawal 				nb_ops = loop;
1781e1e52232SHemant Agrawal 				goto send_pkts;
1782e1e52232SHemant Agrawal 			}
1783e1e52232SHemant Agrawal 
17844e694fe5SAkhil Goyal 			if (unlikely(!ses->qp[rte_lcore_id() % MAX_DPAA_CORES])) {
17859a984458SAkhil Goyal 				if (dpaa_sec_attach_sess_q(qp, ses)) {
17869a984458SAkhil Goyal 					frames_to_send = loop;
17879a984458SAkhil Goyal 					nb_ops = loop;
17889a984458SAkhil Goyal 					goto send_pkts;
17899a984458SAkhil Goyal 				}
17904e694fe5SAkhil Goyal 			} else if (unlikely(ses->qp[rte_lcore_id() %
17914e694fe5SAkhil Goyal 						MAX_DPAA_CORES] != qp)) {
17929198b2c2SAkhil Goyal 				DPAA_SEC_DP_ERR("Old:sess->qp = %p"
17934e694fe5SAkhil Goyal 					" New qp = %p\n",
17944e694fe5SAkhil Goyal 					ses->qp[rte_lcore_id() %
17954e694fe5SAkhil Goyal 					MAX_DPAA_CORES], qp);
17969198b2c2SAkhil Goyal 				frames_to_send = loop;
17979198b2c2SAkhil Goyal 				nb_ops = loop;
17989198b2c2SAkhil Goyal 				goto send_pkts;
1799c3e85bdcSAkhil Goyal 			}
1800c3e85bdcSAkhil Goyal 
18013394ed47SVakul Garg 			auth_hdr_len = op->sym->auth.data.length -
18029a984458SAkhil Goyal 						op->sym->cipher.data.length;
18033394ed47SVakul Garg 			auth_tail_len = 0;
18043394ed47SVakul Garg 
1805fb5c100aSAkhil Goyal 			if (rte_pktmbuf_is_contiguous(op->sym->m_src) &&
1806fb5c100aSAkhil Goyal 				  ((op->sym->m_dst == NULL) ||
1807fb5c100aSAkhil Goyal 				   rte_pktmbuf_is_contiguous(op->sym->m_dst))) {
18088524b44eSHemant Agrawal 				switch (ses->ctxt) {
1809314424b6SHemant Agrawal #ifdef RTE_LIBRTE_SECURITY
18108524b44eSHemant Agrawal 				case DPAA_SEC_PDCP:
18118524b44eSHemant Agrawal 				case DPAA_SEC_IPSEC:
181205b12700SHemant Agrawal 					cf = build_proto(op, ses);
18138524b44eSHemant Agrawal 					break;
1814314424b6SHemant Agrawal #endif
18158524b44eSHemant Agrawal 				case DPAA_SEC_AUTH:
1816c3e85bdcSAkhil Goyal 					cf = build_auth_only(op, ses);
18178524b44eSHemant Agrawal 					break;
18188524b44eSHemant Agrawal 				case DPAA_SEC_CIPHER:
1819c3e85bdcSAkhil Goyal 					cf = build_cipher_only(op, ses);
18208524b44eSHemant Agrawal 					break;
18218524b44eSHemant Agrawal 				case DPAA_SEC_AEAD:
1822c3e85bdcSAkhil Goyal 					cf = build_cipher_auth_gcm(op, ses);
18233394ed47SVakul Garg 					auth_hdr_len = ses->auth_only_len;
18248524b44eSHemant Agrawal 					break;
18258524b44eSHemant Agrawal 				case DPAA_SEC_CIPHER_HASH:
18263394ed47SVakul Garg 					auth_hdr_len =
18273394ed47SVakul Garg 						op->sym->cipher.data.offset
18283394ed47SVakul Garg 						- op->sym->auth.data.offset;
18293394ed47SVakul Garg 					auth_tail_len =
18303394ed47SVakul Garg 						op->sym->auth.data.length
18313394ed47SVakul Garg 						- op->sym->cipher.data.length
18323394ed47SVakul Garg 						- auth_hdr_len;
1833c3e85bdcSAkhil Goyal 					cf = build_cipher_auth(op, ses);
18348524b44eSHemant Agrawal 					break;
18358524b44eSHemant Agrawal 				default:
1836f163231eSHemant Agrawal 					DPAA_SEC_DP_ERR("not supported ops");
18379a984458SAkhil Goyal 					frames_to_send = loop;
18389a984458SAkhil Goyal 					nb_ops = loop;
18399a984458SAkhil Goyal 					goto send_pkts;
1840c3e85bdcSAkhil Goyal 				}
1841a74af788SAkhil Goyal 			} else {
18428524b44eSHemant Agrawal 				switch (ses->ctxt) {
1843314424b6SHemant Agrawal #ifdef RTE_LIBRTE_SECURITY
18448524b44eSHemant Agrawal 				case DPAA_SEC_PDCP:
18458524b44eSHemant Agrawal 				case DPAA_SEC_IPSEC:
1846fb5c100aSAkhil Goyal 					cf = build_proto_sg(op, ses);
18478524b44eSHemant Agrawal 					break;
1848314424b6SHemant Agrawal #endif
18498524b44eSHemant Agrawal 				case DPAA_SEC_AUTH:
1850a74af788SAkhil Goyal 					cf = build_auth_only_sg(op, ses);
18518524b44eSHemant Agrawal 					break;
18528524b44eSHemant Agrawal 				case DPAA_SEC_CIPHER:
1853a74af788SAkhil Goyal 					cf = build_cipher_only_sg(op, ses);
18548524b44eSHemant Agrawal 					break;
18558524b44eSHemant Agrawal 				case DPAA_SEC_AEAD:
1856a74af788SAkhil Goyal 					cf = build_cipher_auth_gcm_sg(op, ses);
18573394ed47SVakul Garg 					auth_hdr_len = ses->auth_only_len;
18588524b44eSHemant Agrawal 					break;
18598524b44eSHemant Agrawal 				case DPAA_SEC_CIPHER_HASH:
18603394ed47SVakul Garg 					auth_hdr_len =
18613394ed47SVakul Garg 						op->sym->cipher.data.offset
18623394ed47SVakul Garg 						- op->sym->auth.data.offset;
18633394ed47SVakul Garg 					auth_tail_len =
18643394ed47SVakul Garg 						op->sym->auth.data.length
18653394ed47SVakul Garg 						- op->sym->cipher.data.length
18663394ed47SVakul Garg 						- auth_hdr_len;
1867a74af788SAkhil Goyal 					cf = build_cipher_auth_sg(op, ses);
18688524b44eSHemant Agrawal 					break;
18698524b44eSHemant Agrawal 				default:
1870f163231eSHemant Agrawal 					DPAA_SEC_DP_ERR("not supported ops");
1871a74af788SAkhil Goyal 					frames_to_send = loop;
1872a74af788SAkhil Goyal 					nb_ops = loop;
1873a74af788SAkhil Goyal 					goto send_pkts;
1874a74af788SAkhil Goyal 				}
1875a74af788SAkhil Goyal 			}
18769a984458SAkhil Goyal 			if (unlikely(!cf)) {
18779a984458SAkhil Goyal 				frames_to_send = loop;
18789a984458SAkhil Goyal 				nb_ops = loop;
18799a984458SAkhil Goyal 				goto send_pkts;
18809a984458SAkhil Goyal 			}
1881c3e85bdcSAkhil Goyal 
18829a984458SAkhil Goyal 			fd = &fds[loop];
18834e694fe5SAkhil Goyal 			inq[loop] = ses->inq[rte_lcore_id() % MAX_DPAA_CORES];
18849a984458SAkhil Goyal 			fd->opaque_addr = 0;
18859a984458SAkhil Goyal 			fd->cmd = 0;
188695456e89SShreyansh Jain 			qm_fd_addr_set64(fd, dpaa_mem_vtop(cf->sg));
18879a984458SAkhil Goyal 			fd->_format1 = qm_fd_compound;
18889a984458SAkhil Goyal 			fd->length29 = 2 * sizeof(struct qm_sg_entry);
18893394ed47SVakul Garg 
18909a984458SAkhil Goyal 			/* Auth_only_len is set as 0 in descriptor and it is
18919a984458SAkhil Goyal 			 * overwritten here in the fd.cmd which will update
18929a984458SAkhil Goyal 			 * the DPOVRD reg.
1893c3e85bdcSAkhil Goyal 			 */
18943394ed47SVakul Garg 			if (auth_hdr_len || auth_tail_len) {
18953394ed47SVakul Garg 				fd->cmd = 0x80000000;
18963394ed47SVakul Garg 				fd->cmd |=
18973394ed47SVakul Garg 					((auth_tail_len << 16) | auth_hdr_len);
18983394ed47SVakul Garg 			}
1899c3e85bdcSAkhil Goyal 
1900314424b6SHemant Agrawal #ifdef RTE_LIBRTE_SECURITY
19016a0c9d36SAkhil Goyal 			/* In case of PDCP, per packet HFN is stored in
19026a0c9d36SAkhil Goyal 			 * mbuf priv after sym_op.
19036a0c9d36SAkhil Goyal 			 */
19048524b44eSHemant Agrawal 			if ((ses->ctxt == DPAA_SEC_PDCP) && ses->pdcp.hfn_ovd) {
19056a0c9d36SAkhil Goyal 				fd->cmd = 0x80000000 |
19066a0c9d36SAkhil Goyal 					*((uint32_t *)((uint8_t *)op +
19076a0c9d36SAkhil Goyal 					ses->pdcp.hfn_ovd_offset));
19088524b44eSHemant Agrawal 				DPAA_SEC_DP_DEBUG("Per packet HFN: %x, ovd:%u\n",
19096a0c9d36SAkhil Goyal 					*((uint32_t *)((uint8_t *)op +
19106a0c9d36SAkhil Goyal 					ses->pdcp.hfn_ovd_offset)),
19118524b44eSHemant Agrawal 					ses->pdcp.hfn_ovd);
19126a0c9d36SAkhil Goyal 			}
1913314424b6SHemant Agrawal #endif
19149a984458SAkhil Goyal 		}
19159a984458SAkhil Goyal send_pkts:
19169a984458SAkhil Goyal 		loop = 0;
19179a984458SAkhil Goyal 		while (loop < frames_to_send) {
19189a984458SAkhil Goyal 			loop += qman_enqueue_multi_fq(&inq[loop], &fds[loop],
1919fe3688baSAkhil Goyal 					&flags[loop], frames_to_send - loop);
19209a984458SAkhil Goyal 		}
19219a984458SAkhil Goyal 		nb_ops -= frames_to_send;
19229a984458SAkhil Goyal 		num_tx += frames_to_send;
1923c3e85bdcSAkhil Goyal 	}
1924c3e85bdcSAkhil Goyal 
1925c3e85bdcSAkhil Goyal 	dpaa_qp->tx_pkts += num_tx;
1926c3e85bdcSAkhil Goyal 	dpaa_qp->tx_errs += nb_ops - num_tx;
1927c3e85bdcSAkhil Goyal 
1928c3e85bdcSAkhil Goyal 	return num_tx;
1929c3e85bdcSAkhil Goyal }
1930c3e85bdcSAkhil Goyal 
1931c3e85bdcSAkhil Goyal static uint16_t
1932c3e85bdcSAkhil Goyal dpaa_sec_dequeue_burst(void *qp, struct rte_crypto_op **ops,
1933c3e85bdcSAkhil Goyal 		       uint16_t nb_ops)
1934c3e85bdcSAkhil Goyal {
1935c3e85bdcSAkhil Goyal 	uint16_t num_rx;
1936c3e85bdcSAkhil Goyal 	struct dpaa_sec_qp *dpaa_qp = (struct dpaa_sec_qp *)qp;
1937c3e85bdcSAkhil Goyal 
1938c3e85bdcSAkhil Goyal 	num_rx = dpaa_sec_deq(dpaa_qp, ops, nb_ops);
1939c3e85bdcSAkhil Goyal 
1940c3e85bdcSAkhil Goyal 	dpaa_qp->rx_pkts += num_rx;
1941c3e85bdcSAkhil Goyal 	dpaa_qp->rx_errs += nb_ops - num_rx;
1942c3e85bdcSAkhil Goyal 
1943f163231eSHemant Agrawal 	DPAA_SEC_DP_DEBUG("SEC Received %d Packets\n", num_rx);
1944c3e85bdcSAkhil Goyal 
1945c3e85bdcSAkhil Goyal 	return num_rx;
1946c3e85bdcSAkhil Goyal }
1947c3e85bdcSAkhil Goyal 
1948c3e85bdcSAkhil Goyal /** Release queue pair */
1949c3e85bdcSAkhil Goyal static int
1950c3e85bdcSAkhil Goyal dpaa_sec_queue_pair_release(struct rte_cryptodev *dev,
1951c3e85bdcSAkhil Goyal 			    uint16_t qp_id)
1952c3e85bdcSAkhil Goyal {
1953c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals;
1954c3e85bdcSAkhil Goyal 	struct dpaa_sec_qp *qp = NULL;
1955c3e85bdcSAkhil Goyal 
1956c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
1957c3e85bdcSAkhil Goyal 
1958f163231eSHemant Agrawal 	DPAA_SEC_DEBUG("dev =%p, queue =%d", dev, qp_id);
1959c3e85bdcSAkhil Goyal 
1960c3e85bdcSAkhil Goyal 	internals = dev->data->dev_private;
1961c3e85bdcSAkhil Goyal 	if (qp_id >= internals->max_nb_queue_pairs) {
1962f163231eSHemant Agrawal 		DPAA_SEC_ERR("Max supported qpid %d",
1963c3e85bdcSAkhil Goyal 			     internals->max_nb_queue_pairs);
1964c3e85bdcSAkhil Goyal 		return -EINVAL;
1965c3e85bdcSAkhil Goyal 	}
1966c3e85bdcSAkhil Goyal 
1967c3e85bdcSAkhil Goyal 	qp = &internals->qps[qp_id];
19682ffb940eSAkhil Goyal 	rte_mempool_free(qp->ctx_pool);
1969c3e85bdcSAkhil Goyal 	qp->internals = NULL;
1970c3e85bdcSAkhil Goyal 	dev->data->queue_pairs[qp_id] = NULL;
1971c3e85bdcSAkhil Goyal 
1972c3e85bdcSAkhil Goyal 	return 0;
1973c3e85bdcSAkhil Goyal }
1974c3e85bdcSAkhil Goyal 
1975c3e85bdcSAkhil Goyal /** Setup a queue pair */
1976c3e85bdcSAkhil Goyal static int
1977c3e85bdcSAkhil Goyal dpaa_sec_queue_pair_setup(struct rte_cryptodev *dev, uint16_t qp_id,
1978c3e85bdcSAkhil Goyal 		__rte_unused const struct rte_cryptodev_qp_conf *qp_conf,
1979725d2a7fSFan Zhang 		__rte_unused int socket_id)
1980c3e85bdcSAkhil Goyal {
1981c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals;
1982c3e85bdcSAkhil Goyal 	struct dpaa_sec_qp *qp = NULL;
19832ffb940eSAkhil Goyal 	char str[20];
1984c3e85bdcSAkhil Goyal 
1985f163231eSHemant Agrawal 	DPAA_SEC_DEBUG("dev =%p, queue =%d, conf =%p", dev, qp_id, qp_conf);
1986c3e85bdcSAkhil Goyal 
1987c3e85bdcSAkhil Goyal 	internals = dev->data->dev_private;
1988c3e85bdcSAkhil Goyal 	if (qp_id >= internals->max_nb_queue_pairs) {
1989f163231eSHemant Agrawal 		DPAA_SEC_ERR("Max supported qpid %d",
1990c3e85bdcSAkhil Goyal 			     internals->max_nb_queue_pairs);
1991c3e85bdcSAkhil Goyal 		return -EINVAL;
1992c3e85bdcSAkhil Goyal 	}
1993c3e85bdcSAkhil Goyal 
1994c3e85bdcSAkhil Goyal 	qp = &internals->qps[qp_id];
1995c3e85bdcSAkhil Goyal 	qp->internals = internals;
19962ffb940eSAkhil Goyal 	snprintf(str, sizeof(str), "ctx_pool_d%d_qp%d",
19972ffb940eSAkhil Goyal 			dev->data->dev_id, qp_id);
19982ffb940eSAkhil Goyal 	if (!qp->ctx_pool) {
19992ffb940eSAkhil Goyal 		qp->ctx_pool = rte_mempool_create((const char *)str,
20002ffb940eSAkhil Goyal 							CTX_POOL_NUM_BUFS,
20012ffb940eSAkhil Goyal 							CTX_POOL_BUF_SIZE,
20022ffb940eSAkhil Goyal 							CTX_POOL_CACHE_SIZE, 0,
20032ffb940eSAkhil Goyal 							NULL, NULL, NULL, NULL,
20042ffb940eSAkhil Goyal 							SOCKET_ID_ANY, 0);
20052ffb940eSAkhil Goyal 		if (!qp->ctx_pool) {
20062ffb940eSAkhil Goyal 			DPAA_SEC_ERR("%s create failed\n", str);
20072ffb940eSAkhil Goyal 			return -ENOMEM;
20082ffb940eSAkhil Goyal 		}
20092ffb940eSAkhil Goyal 	} else
20102ffb940eSAkhil Goyal 		DPAA_SEC_INFO("mempool already created for dev_id : %d, qp: %d",
20112ffb940eSAkhil Goyal 				dev->data->dev_id, qp_id);
2012c3e85bdcSAkhil Goyal 	dev->data->queue_pairs[qp_id] = qp;
2013c3e85bdcSAkhil Goyal 
2014c3e85bdcSAkhil Goyal 	return 0;
2015c3e85bdcSAkhil Goyal }
2016c3e85bdcSAkhil Goyal 
2017c3e85bdcSAkhil Goyal /** Return the number of allocated queue pairs */
2018c3e85bdcSAkhil Goyal static uint32_t
2019c3e85bdcSAkhil Goyal dpaa_sec_queue_pair_count(struct rte_cryptodev *dev)
2020c3e85bdcSAkhil Goyal {
2021c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2022c3e85bdcSAkhil Goyal 
2023c3e85bdcSAkhil Goyal 	return dev->data->nb_queue_pairs;
2024c3e85bdcSAkhil Goyal }
2025c3e85bdcSAkhil Goyal 
2026c3e85bdcSAkhil Goyal /** Returns the size of session structure */
2027c3e85bdcSAkhil Goyal static unsigned int
2028012c5076SPablo de Lara dpaa_sec_sym_session_get_size(struct rte_cryptodev *dev __rte_unused)
2029c3e85bdcSAkhil Goyal {
2030c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2031c3e85bdcSAkhil Goyal 
2032c3e85bdcSAkhil Goyal 	return sizeof(dpaa_sec_session);
2033c3e85bdcSAkhil Goyal }
2034c3e85bdcSAkhil Goyal 
2035c3e85bdcSAkhil Goyal static int
2036c3e85bdcSAkhil Goyal dpaa_sec_cipher_init(struct rte_cryptodev *dev __rte_unused,
2037c3e85bdcSAkhil Goyal 		     struct rte_crypto_sym_xform *xform,
2038c3e85bdcSAkhil Goyal 		     dpaa_sec_session *session)
2039c3e85bdcSAkhil Goyal {
2040*f73d6928SHemant Agrawal 	session->ctxt = DPAA_SEC_CIPHER;
2041c3e85bdcSAkhil Goyal 	session->cipher_alg = xform->cipher.algo;
2042c3e85bdcSAkhil Goyal 	session->iv.length = xform->cipher.iv.length;
2043c3e85bdcSAkhil Goyal 	session->iv.offset = xform->cipher.iv.offset;
2044c3e85bdcSAkhil Goyal 	session->cipher_key.data = rte_zmalloc(NULL, xform->cipher.key.length,
2045c3e85bdcSAkhil Goyal 					       RTE_CACHE_LINE_SIZE);
2046c3e85bdcSAkhil Goyal 	if (session->cipher_key.data == NULL && xform->cipher.key.length > 0) {
2047f163231eSHemant Agrawal 		DPAA_SEC_ERR("No Memory for cipher key");
2048c3e85bdcSAkhil Goyal 		return -ENOMEM;
2049c3e85bdcSAkhil Goyal 	}
2050c3e85bdcSAkhil Goyal 	session->cipher_key.length = xform->cipher.key.length;
2051c3e85bdcSAkhil Goyal 
2052c3e85bdcSAkhil Goyal 	memcpy(session->cipher_key.data, xform->cipher.key.data,
2053c3e85bdcSAkhil Goyal 	       xform->cipher.key.length);
20548524b44eSHemant Agrawal 	switch (xform->cipher.algo) {
20558524b44eSHemant Agrawal 	case RTE_CRYPTO_CIPHER_AES_CBC:
20568524b44eSHemant Agrawal 		session->cipher_key.alg = OP_ALG_ALGSEL_AES;
20578524b44eSHemant Agrawal 		session->cipher_key.algmode = OP_ALG_AAI_CBC;
20588524b44eSHemant Agrawal 		break;
20598524b44eSHemant Agrawal 	case RTE_CRYPTO_CIPHER_3DES_CBC:
20608524b44eSHemant Agrawal 		session->cipher_key.alg = OP_ALG_ALGSEL_3DES;
20618524b44eSHemant Agrawal 		session->cipher_key.algmode = OP_ALG_AAI_CBC;
20628524b44eSHemant Agrawal 		break;
20638524b44eSHemant Agrawal 	case RTE_CRYPTO_CIPHER_AES_CTR:
20648524b44eSHemant Agrawal 		session->cipher_key.alg = OP_ALG_ALGSEL_AES;
20658524b44eSHemant Agrawal 		session->cipher_key.algmode = OP_ALG_AAI_CTR;
20668524b44eSHemant Agrawal 		break;
20678524b44eSHemant Agrawal 	case RTE_CRYPTO_CIPHER_SNOW3G_UEA2:
20688524b44eSHemant Agrawal 		session->cipher_key.alg = OP_ALG_ALGSEL_SNOW_F8;
20698524b44eSHemant Agrawal 		break;
20708524b44eSHemant Agrawal 	case RTE_CRYPTO_CIPHER_ZUC_EEA3:
20718524b44eSHemant Agrawal 		session->cipher_key.alg = OP_ALG_ALGSEL_ZUCE;
20728524b44eSHemant Agrawal 		break;
20738524b44eSHemant Agrawal 	default:
20748524b44eSHemant Agrawal 		DPAA_SEC_ERR("Crypto: Undefined Cipher specified %u",
20758524b44eSHemant Agrawal 			      xform->cipher.algo);
20768524b44eSHemant Agrawal 		rte_free(session->cipher_key.data);
20778524b44eSHemant Agrawal 		return -1;
20788524b44eSHemant Agrawal 	}
2079c3e85bdcSAkhil Goyal 	session->dir = (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
2080c3e85bdcSAkhil Goyal 			DIR_ENC : DIR_DEC;
2081c3e85bdcSAkhil Goyal 
2082c3e85bdcSAkhil Goyal 	return 0;
2083c3e85bdcSAkhil Goyal }
2084c3e85bdcSAkhil Goyal 
2085c3e85bdcSAkhil Goyal static int
2086c3e85bdcSAkhil Goyal dpaa_sec_auth_init(struct rte_cryptodev *dev __rte_unused,
2087c3e85bdcSAkhil Goyal 		   struct rte_crypto_sym_xform *xform,
2088c3e85bdcSAkhil Goyal 		   dpaa_sec_session *session)
2089c3e85bdcSAkhil Goyal {
2090*f73d6928SHemant Agrawal 	session->ctxt = DPAA_SEC_AUTH;
2091c3e85bdcSAkhil Goyal 	session->auth_alg = xform->auth.algo;
2092c3e85bdcSAkhil Goyal 	session->auth_key.data = rte_zmalloc(NULL, xform->auth.key.length,
2093c3e85bdcSAkhil Goyal 					     RTE_CACHE_LINE_SIZE);
2094c3e85bdcSAkhil Goyal 	if (session->auth_key.data == NULL && xform->auth.key.length > 0) {
2095f163231eSHemant Agrawal 		DPAA_SEC_ERR("No Memory for auth key");
2096c3e85bdcSAkhil Goyal 		return -ENOMEM;
2097c3e85bdcSAkhil Goyal 	}
2098c3e85bdcSAkhil Goyal 	session->auth_key.length = xform->auth.key.length;
2099c3e85bdcSAkhil Goyal 	session->digest_length = xform->auth.digest_length;
2100c5788a10SHemant Agrawal 	if (session->cipher_alg == RTE_CRYPTO_CIPHER_NULL) {
2101c5788a10SHemant Agrawal 		session->iv.offset = xform->auth.iv.offset;
2102c5788a10SHemant Agrawal 		session->iv.length = xform->auth.iv.length;
2103c5788a10SHemant Agrawal 	}
2104c3e85bdcSAkhil Goyal 
2105c3e85bdcSAkhil Goyal 	memcpy(session->auth_key.data, xform->auth.key.data,
2106c3e85bdcSAkhil Goyal 	       xform->auth.key.length);
21078524b44eSHemant Agrawal 
21088524b44eSHemant Agrawal 	switch (xform->auth.algo) {
21098524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_SHA1_HMAC:
21108524b44eSHemant Agrawal 		session->auth_key.alg = OP_ALG_ALGSEL_SHA1;
21118524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
21128524b44eSHemant Agrawal 		break;
21138524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_MD5_HMAC:
21148524b44eSHemant Agrawal 		session->auth_key.alg = OP_ALG_ALGSEL_MD5;
21158524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
21168524b44eSHemant Agrawal 		break;
21178524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_SHA224_HMAC:
21188524b44eSHemant Agrawal 		session->auth_key.alg = OP_ALG_ALGSEL_SHA224;
21198524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
21208524b44eSHemant Agrawal 		break;
21218524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_SHA256_HMAC:
21228524b44eSHemant Agrawal 		session->auth_key.alg = OP_ALG_ALGSEL_SHA256;
21238524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
21248524b44eSHemant Agrawal 		break;
21258524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_SHA384_HMAC:
21268524b44eSHemant Agrawal 		session->auth_key.alg = OP_ALG_ALGSEL_SHA384;
21278524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
21288524b44eSHemant Agrawal 		break;
21298524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_SHA512_HMAC:
21308524b44eSHemant Agrawal 		session->auth_key.alg = OP_ALG_ALGSEL_SHA512;
21318524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
21328524b44eSHemant Agrawal 		break;
21338524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
21348524b44eSHemant Agrawal 		session->auth_key.alg = OP_ALG_ALGSEL_SNOW_F9;
21358524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_F9;
21368524b44eSHemant Agrawal 		break;
21378524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_ZUC_EIA3:
21388524b44eSHemant Agrawal 		session->auth_key.alg = OP_ALG_ALGSEL_ZUCA;
21398524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_F9;
21408524b44eSHemant Agrawal 		break;
21418524b44eSHemant Agrawal 	default:
21428524b44eSHemant Agrawal 		DPAA_SEC_ERR("Crypto: Unsupported Auth specified %u",
21438524b44eSHemant Agrawal 			      xform->auth.algo);
21448524b44eSHemant Agrawal 		rte_free(session->auth_key.data);
21458524b44eSHemant Agrawal 		return -1;
21468524b44eSHemant Agrawal 	}
21478524b44eSHemant Agrawal 
2148c3e85bdcSAkhil Goyal 	session->dir = (xform->auth.op == RTE_CRYPTO_AUTH_OP_GENERATE) ?
2149c3e85bdcSAkhil Goyal 			DIR_ENC : DIR_DEC;
2150c3e85bdcSAkhil Goyal 
2151c3e85bdcSAkhil Goyal 	return 0;
2152c3e85bdcSAkhil Goyal }
2153c3e85bdcSAkhil Goyal 
2154c3e85bdcSAkhil Goyal static int
21558524b44eSHemant Agrawal dpaa_sec_chain_init(struct rte_cryptodev *dev __rte_unused,
21568524b44eSHemant Agrawal 		   struct rte_crypto_sym_xform *xform,
21578524b44eSHemant Agrawal 		   dpaa_sec_session *session)
21588524b44eSHemant Agrawal {
21598524b44eSHemant Agrawal 
21608524b44eSHemant Agrawal 	struct rte_crypto_cipher_xform *cipher_xform;
21618524b44eSHemant Agrawal 	struct rte_crypto_auth_xform *auth_xform;
21628524b44eSHemant Agrawal 
2163*f73d6928SHemant Agrawal 	session->ctxt = DPAA_SEC_CIPHER_HASH;
21648524b44eSHemant Agrawal 	if (session->auth_cipher_text) {
21658524b44eSHemant Agrawal 		cipher_xform = &xform->cipher;
21668524b44eSHemant Agrawal 		auth_xform = &xform->next->auth;
21678524b44eSHemant Agrawal 	} else {
21688524b44eSHemant Agrawal 		cipher_xform = &xform->next->cipher;
21698524b44eSHemant Agrawal 		auth_xform = &xform->auth;
21708524b44eSHemant Agrawal 	}
21718524b44eSHemant Agrawal 
21728524b44eSHemant Agrawal 	/* Set IV parameters */
21738524b44eSHemant Agrawal 	session->iv.offset = cipher_xform->iv.offset;
21748524b44eSHemant Agrawal 	session->iv.length = cipher_xform->iv.length;
21758524b44eSHemant Agrawal 
21768524b44eSHemant Agrawal 	session->cipher_key.data = rte_zmalloc(NULL, cipher_xform->key.length,
21778524b44eSHemant Agrawal 					       RTE_CACHE_LINE_SIZE);
21788524b44eSHemant Agrawal 	if (session->cipher_key.data == NULL && cipher_xform->key.length > 0) {
21798524b44eSHemant Agrawal 		DPAA_SEC_ERR("No Memory for cipher key");
21808524b44eSHemant Agrawal 		return -1;
21818524b44eSHemant Agrawal 	}
21828524b44eSHemant Agrawal 	session->cipher_key.length = cipher_xform->key.length;
21838524b44eSHemant Agrawal 	session->auth_key.data = rte_zmalloc(NULL, auth_xform->key.length,
21848524b44eSHemant Agrawal 					     RTE_CACHE_LINE_SIZE);
21858524b44eSHemant Agrawal 	if (session->auth_key.data == NULL && auth_xform->key.length > 0) {
21868524b44eSHemant Agrawal 		DPAA_SEC_ERR("No Memory for auth key");
21878524b44eSHemant Agrawal 		rte_free(session->cipher_key.data);
21888524b44eSHemant Agrawal 		return -ENOMEM;
21898524b44eSHemant Agrawal 	}
21908524b44eSHemant Agrawal 	session->auth_key.length = auth_xform->key.length;
21918524b44eSHemant Agrawal 	memcpy(session->cipher_key.data, cipher_xform->key.data,
21928524b44eSHemant Agrawal 	       cipher_xform->key.length);
21938524b44eSHemant Agrawal 	memcpy(session->auth_key.data, auth_xform->key.data,
21948524b44eSHemant Agrawal 	       auth_xform->key.length);
21958524b44eSHemant Agrawal 
21968524b44eSHemant Agrawal 	session->digest_length = auth_xform->digest_length;
21978524b44eSHemant Agrawal 	session->auth_alg = auth_xform->algo;
21988524b44eSHemant Agrawal 
21998524b44eSHemant Agrawal 	switch (auth_xform->algo) {
22008524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_SHA1_HMAC:
22018524b44eSHemant Agrawal 		session->auth_key.alg = OP_ALG_ALGSEL_SHA1;
22028524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
22038524b44eSHemant Agrawal 		break;
22048524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_MD5_HMAC:
22058524b44eSHemant Agrawal 		session->auth_key.alg = OP_ALG_ALGSEL_MD5;
22068524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
22078524b44eSHemant Agrawal 		break;
22088524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_SHA224_HMAC:
22098524b44eSHemant Agrawal 		session->auth_key.alg = OP_ALG_ALGSEL_SHA224;
22108524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
22118524b44eSHemant Agrawal 		break;
22128524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_SHA256_HMAC:
22138524b44eSHemant Agrawal 		session->auth_key.alg = OP_ALG_ALGSEL_SHA256;
22148524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
22158524b44eSHemant Agrawal 		break;
22168524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_SHA384_HMAC:
22178524b44eSHemant Agrawal 		session->auth_key.alg = OP_ALG_ALGSEL_SHA384;
22188524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
22198524b44eSHemant Agrawal 		break;
22208524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_SHA512_HMAC:
22218524b44eSHemant Agrawal 		session->auth_key.alg = OP_ALG_ALGSEL_SHA512;
22228524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
22238524b44eSHemant Agrawal 		break;
22248524b44eSHemant Agrawal 	default:
22258524b44eSHemant Agrawal 		DPAA_SEC_ERR("Crypto: Unsupported Auth specified %u",
22268524b44eSHemant Agrawal 			      auth_xform->algo);
22278524b44eSHemant Agrawal 		goto error_out;
22288524b44eSHemant Agrawal 	}
22298524b44eSHemant Agrawal 
22308524b44eSHemant Agrawal 	session->cipher_alg = cipher_xform->algo;
22318524b44eSHemant Agrawal 
22328524b44eSHemant Agrawal 	switch (cipher_xform->algo) {
22338524b44eSHemant Agrawal 	case RTE_CRYPTO_CIPHER_AES_CBC:
22348524b44eSHemant Agrawal 		session->cipher_key.alg = OP_ALG_ALGSEL_AES;
22358524b44eSHemant Agrawal 		session->cipher_key.algmode = OP_ALG_AAI_CBC;
22368524b44eSHemant Agrawal 		break;
22378524b44eSHemant Agrawal 	case RTE_CRYPTO_CIPHER_3DES_CBC:
22388524b44eSHemant Agrawal 		session->cipher_key.alg = OP_ALG_ALGSEL_3DES;
22398524b44eSHemant Agrawal 		session->cipher_key.algmode = OP_ALG_AAI_CBC;
22408524b44eSHemant Agrawal 		break;
22418524b44eSHemant Agrawal 	case RTE_CRYPTO_CIPHER_AES_CTR:
22428524b44eSHemant Agrawal 		session->cipher_key.alg = OP_ALG_ALGSEL_AES;
22438524b44eSHemant Agrawal 		session->cipher_key.algmode = OP_ALG_AAI_CTR;
22448524b44eSHemant Agrawal 		break;
22458524b44eSHemant Agrawal 	default:
22468524b44eSHemant Agrawal 		DPAA_SEC_ERR("Crypto: Undefined Cipher specified %u",
22478524b44eSHemant Agrawal 			      cipher_xform->algo);
22488524b44eSHemant Agrawal 		goto error_out;
22498524b44eSHemant Agrawal 	}
22508524b44eSHemant Agrawal 	session->dir = (cipher_xform->op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
22518524b44eSHemant Agrawal 				DIR_ENC : DIR_DEC;
22528524b44eSHemant Agrawal 	return 0;
22538524b44eSHemant Agrawal 
22548524b44eSHemant Agrawal error_out:
22558524b44eSHemant Agrawal 	rte_free(session->cipher_key.data);
22568524b44eSHemant Agrawal 	rte_free(session->auth_key.data);
22578524b44eSHemant Agrawal 	return -1;
22588524b44eSHemant Agrawal }
22598524b44eSHemant Agrawal 
22608524b44eSHemant Agrawal static int
2261c3e85bdcSAkhil Goyal dpaa_sec_aead_init(struct rte_cryptodev *dev __rte_unused,
2262c3e85bdcSAkhil Goyal 		   struct rte_crypto_sym_xform *xform,
2263c3e85bdcSAkhil Goyal 		   dpaa_sec_session *session)
2264c3e85bdcSAkhil Goyal {
2265c3e85bdcSAkhil Goyal 	session->aead_alg = xform->aead.algo;
22668524b44eSHemant Agrawal 	session->ctxt = DPAA_SEC_AEAD;
2267c3e85bdcSAkhil Goyal 	session->iv.length = xform->aead.iv.length;
2268c3e85bdcSAkhil Goyal 	session->iv.offset = xform->aead.iv.offset;
2269c3e85bdcSAkhil Goyal 	session->auth_only_len = xform->aead.aad_length;
2270c3e85bdcSAkhil Goyal 	session->aead_key.data = rte_zmalloc(NULL, xform->aead.key.length,
2271c3e85bdcSAkhil Goyal 					     RTE_CACHE_LINE_SIZE);
2272c3e85bdcSAkhil Goyal 	if (session->aead_key.data == NULL && xform->aead.key.length > 0) {
2273f163231eSHemant Agrawal 		DPAA_SEC_ERR("No Memory for aead key\n");
2274c3e85bdcSAkhil Goyal 		return -ENOMEM;
2275c3e85bdcSAkhil Goyal 	}
2276c3e85bdcSAkhil Goyal 	session->aead_key.length = xform->aead.key.length;
2277c3e85bdcSAkhil Goyal 	session->digest_length = xform->aead.digest_length;
2278c3e85bdcSAkhil Goyal 
2279c3e85bdcSAkhil Goyal 	memcpy(session->aead_key.data, xform->aead.key.data,
2280c3e85bdcSAkhil Goyal 	       xform->aead.key.length);
22818524b44eSHemant Agrawal 
22828524b44eSHemant Agrawal 	switch (session->aead_alg) {
22838524b44eSHemant Agrawal 	case RTE_CRYPTO_AEAD_AES_GCM:
22848524b44eSHemant Agrawal 		session->aead_key.alg = OP_ALG_ALGSEL_AES;
22858524b44eSHemant Agrawal 		session->aead_key.algmode = OP_ALG_AAI_GCM;
22868524b44eSHemant Agrawal 		break;
22878524b44eSHemant Agrawal 	default:
22888524b44eSHemant Agrawal 		DPAA_SEC_ERR("unsupported AEAD alg %d", session->aead_alg);
22898524b44eSHemant Agrawal 		rte_free(session->aead_key.data);
22908524b44eSHemant Agrawal 		return -ENOMEM;
22918524b44eSHemant Agrawal 	}
22928524b44eSHemant Agrawal 
2293c3e85bdcSAkhil Goyal 	session->dir = (xform->aead.op == RTE_CRYPTO_AEAD_OP_ENCRYPT) ?
2294c3e85bdcSAkhil Goyal 			DIR_ENC : DIR_DEC;
2295c3e85bdcSAkhil Goyal 
2296c3e85bdcSAkhil Goyal 	return 0;
2297c3e85bdcSAkhil Goyal }
2298c3e85bdcSAkhil Goyal 
2299e79416d1SHemant Agrawal static struct qman_fq *
2300e79416d1SHemant Agrawal dpaa_sec_attach_rxq(struct dpaa_sec_dev_private *qi)
2301c3e85bdcSAkhil Goyal {
2302e79416d1SHemant Agrawal 	unsigned int i;
2303c3e85bdcSAkhil Goyal 
2304e621d970SAkhil Goyal 	for (i = 0; i < qi->max_nb_sessions * MAX_DPAA_CORES; i++) {
2305e79416d1SHemant Agrawal 		if (qi->inq_attach[i] == 0) {
2306e79416d1SHemant Agrawal 			qi->inq_attach[i] = 1;
2307e79416d1SHemant Agrawal 			return &qi->inq[i];
2308e79416d1SHemant Agrawal 		}
2309e79416d1SHemant Agrawal 	}
2310e621d970SAkhil Goyal 	DPAA_SEC_WARN("All session in use %u", qi->max_nb_sessions);
2311c3e85bdcSAkhil Goyal 
2312e79416d1SHemant Agrawal 	return NULL;
2313c3e85bdcSAkhil Goyal }
2314c3e85bdcSAkhil Goyal 
2315e79416d1SHemant Agrawal static int
2316e79416d1SHemant Agrawal dpaa_sec_detach_rxq(struct dpaa_sec_dev_private *qi, struct qman_fq *fq)
2317e79416d1SHemant Agrawal {
2318e79416d1SHemant Agrawal 	unsigned int i;
2319e79416d1SHemant Agrawal 
2320e79416d1SHemant Agrawal 	for (i = 0; i < qi->max_nb_sessions; i++) {
2321e79416d1SHemant Agrawal 		if (&qi->inq[i] == fq) {
2322b4053c4bSAlok Makhariya 			qman_retire_fq(fq, NULL);
2323b4053c4bSAlok Makhariya 			qman_oos_fq(fq);
2324e79416d1SHemant Agrawal 			qi->inq_attach[i] = 0;
2325e79416d1SHemant Agrawal 			return 0;
2326e79416d1SHemant Agrawal 		}
2327e79416d1SHemant Agrawal 	}
2328e79416d1SHemant Agrawal 	return -1;
2329e79416d1SHemant Agrawal }
2330e79416d1SHemant Agrawal 
2331e79416d1SHemant Agrawal static int
2332e79416d1SHemant Agrawal dpaa_sec_attach_sess_q(struct dpaa_sec_qp *qp, dpaa_sec_session *sess)
2333e79416d1SHemant Agrawal {
2334e79416d1SHemant Agrawal 	int ret;
2335e79416d1SHemant Agrawal 
23364e694fe5SAkhil Goyal 	sess->qp[rte_lcore_id() % MAX_DPAA_CORES] = qp;
2337e79416d1SHemant Agrawal 	ret = dpaa_sec_prep_cdb(sess);
2338e79416d1SHemant Agrawal 	if (ret) {
2339f163231eSHemant Agrawal 		DPAA_SEC_ERR("Unable to prepare sec cdb");
2340e79416d1SHemant Agrawal 		return -1;
2341e79416d1SHemant Agrawal 	}
23425b0f1bd3SAshish Jain 	if (unlikely(!RTE_PER_LCORE(dpaa_io))) {
23435b0f1bd3SAshish Jain 		ret = rte_dpaa_portal_init((void *)0);
23445b0f1bd3SAshish Jain 		if (ret) {
2345f163231eSHemant Agrawal 			DPAA_SEC_ERR("Failure in affining portal");
23465b0f1bd3SAshish Jain 			return ret;
23475b0f1bd3SAshish Jain 		}
23485b0f1bd3SAshish Jain 	}
23494e694fe5SAkhil Goyal 	ret = dpaa_sec_init_rx(sess->inq[rte_lcore_id() % MAX_DPAA_CORES],
23504e694fe5SAkhil Goyal 			       dpaa_mem_vtop(&sess->cdb),
2351e79416d1SHemant Agrawal 			       qman_fq_fqid(&qp->outq));
2352e79416d1SHemant Agrawal 	if (ret)
2353f163231eSHemant Agrawal 		DPAA_SEC_ERR("Unable to init sec queue");
2354e79416d1SHemant Agrawal 
2355e79416d1SHemant Agrawal 	return ret;
2356c3e85bdcSAkhil Goyal }
2357c3e85bdcSAkhil Goyal 
2358c3e85bdcSAkhil Goyal static int
2359c3e85bdcSAkhil Goyal dpaa_sec_set_session_parameters(struct rte_cryptodev *dev,
2360c3e85bdcSAkhil Goyal 			    struct rte_crypto_sym_xform *xform,	void *sess)
2361c3e85bdcSAkhil Goyal {
2362c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals = dev->data->dev_private;
2363c3e85bdcSAkhil Goyal 	dpaa_sec_session *session = sess;
23644e694fe5SAkhil Goyal 	uint32_t i;
2365*f73d6928SHemant Agrawal 	int ret;
2366c3e85bdcSAkhil Goyal 
2367c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2368c3e85bdcSAkhil Goyal 
2369c3e85bdcSAkhil Goyal 	if (unlikely(sess == NULL)) {
2370f163231eSHemant Agrawal 		DPAA_SEC_ERR("invalid session struct");
2371c3e85bdcSAkhil Goyal 		return -EINVAL;
2372c3e85bdcSAkhil Goyal 	}
2373b0894102SAkhil Goyal 	memset(session, 0, sizeof(dpaa_sec_session));
2374c3e85bdcSAkhil Goyal 
2375c3e85bdcSAkhil Goyal 	/* Default IV length = 0 */
2376c3e85bdcSAkhil Goyal 	session->iv.length = 0;
2377c3e85bdcSAkhil Goyal 
2378c3e85bdcSAkhil Goyal 	/* Cipher Only */
2379c3e85bdcSAkhil Goyal 	if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER && xform->next == NULL) {
2380c3e85bdcSAkhil Goyal 		session->auth_alg = RTE_CRYPTO_AUTH_NULL;
2381*f73d6928SHemant Agrawal 		ret = dpaa_sec_cipher_init(dev, xform, session);
2382c3e85bdcSAkhil Goyal 
2383c3e85bdcSAkhil Goyal 	/* Authentication Only */
2384c3e85bdcSAkhil Goyal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
2385c3e85bdcSAkhil Goyal 		   xform->next == NULL) {
2386c3e85bdcSAkhil Goyal 		session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
23878524b44eSHemant Agrawal 		session->ctxt = DPAA_SEC_AUTH;
2388*f73d6928SHemant Agrawal 		ret = dpaa_sec_auth_init(dev, xform, session);
2389c3e85bdcSAkhil Goyal 
2390c3e85bdcSAkhil Goyal 	/* Cipher then Authenticate */
2391c3e85bdcSAkhil Goyal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
2392c3e85bdcSAkhil Goyal 		   xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
2393c3e85bdcSAkhil Goyal 		if (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) {
23948524b44eSHemant Agrawal 			session->auth_cipher_text = 1;
2395*f73d6928SHemant Agrawal 			if (xform->cipher.algo == RTE_CRYPTO_CIPHER_NULL)
2396*f73d6928SHemant Agrawal 				ret = dpaa_sec_auth_init(dev, xform, session);
2397*f73d6928SHemant Agrawal 			else if (xform->next->auth.algo == RTE_CRYPTO_AUTH_NULL)
2398*f73d6928SHemant Agrawal 				ret = dpaa_sec_cipher_init(dev, xform, session);
2399*f73d6928SHemant Agrawal 			else
2400*f73d6928SHemant Agrawal 				ret = dpaa_sec_chain_init(dev, xform, session);
2401c3e85bdcSAkhil Goyal 		} else {
2402f163231eSHemant Agrawal 			DPAA_SEC_ERR("Not supported: Auth then Cipher");
2403c3e85bdcSAkhil Goyal 			return -EINVAL;
2404c3e85bdcSAkhil Goyal 		}
2405c3e85bdcSAkhil Goyal 	/* Authenticate then Cipher */
2406c3e85bdcSAkhil Goyal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
2407c3e85bdcSAkhil Goyal 		   xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
2408c3e85bdcSAkhil Goyal 		if (xform->next->cipher.op == RTE_CRYPTO_CIPHER_OP_DECRYPT) {
24098524b44eSHemant Agrawal 			session->auth_cipher_text = 0;
2410*f73d6928SHemant Agrawal 			if (xform->auth.algo == RTE_CRYPTO_AUTH_NULL)
2411*f73d6928SHemant Agrawal 				ret = dpaa_sec_cipher_init(dev, xform, session);
2412*f73d6928SHemant Agrawal 			else if (xform->next->cipher.algo
2413*f73d6928SHemant Agrawal 					== RTE_CRYPTO_CIPHER_NULL)
2414*f73d6928SHemant Agrawal 				ret = dpaa_sec_auth_init(dev, xform, session);
2415*f73d6928SHemant Agrawal 			else
2416*f73d6928SHemant Agrawal 				ret = dpaa_sec_chain_init(dev, xform, session);
2417c3e85bdcSAkhil Goyal 		} else {
2418f163231eSHemant Agrawal 			DPAA_SEC_ERR("Not supported: Auth then Cipher");
2419c3e85bdcSAkhil Goyal 			return -EINVAL;
2420c3e85bdcSAkhil Goyal 		}
2421c3e85bdcSAkhil Goyal 
2422c3e85bdcSAkhil Goyal 	/* AEAD operation for AES-GCM kind of Algorithms */
2423c3e85bdcSAkhil Goyal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD &&
2424c3e85bdcSAkhil Goyal 		   xform->next == NULL) {
2425*f73d6928SHemant Agrawal 		ret = dpaa_sec_aead_init(dev, xform, session);
2426c3e85bdcSAkhil Goyal 
2427c3e85bdcSAkhil Goyal 	} else {
2428f163231eSHemant Agrawal 		DPAA_SEC_ERR("Invalid crypto type");
2429c3e85bdcSAkhil Goyal 		return -EINVAL;
2430c3e85bdcSAkhil Goyal 	}
2431*f73d6928SHemant Agrawal 	if (ret) {
2432*f73d6928SHemant Agrawal 		DPAA_SEC_ERR("unable to init session");
2433*f73d6928SHemant Agrawal 		goto err1;
2434*f73d6928SHemant Agrawal 	}
2435*f73d6928SHemant Agrawal 
24363b617ee7SAkhil Goyal 	rte_spinlock_lock(&internals->lock);
24374e694fe5SAkhil Goyal 	for (i = 0; i < MAX_DPAA_CORES; i++) {
24384e694fe5SAkhil Goyal 		session->inq[i] = dpaa_sec_attach_rxq(internals);
24394e694fe5SAkhil Goyal 		if (session->inq[i] == NULL) {
2440f163231eSHemant Agrawal 			DPAA_SEC_ERR("unable to attach sec queue");
24414e694fe5SAkhil Goyal 			rte_spinlock_unlock(&internals->lock);
2442e79416d1SHemant Agrawal 			goto err1;
2443e79416d1SHemant Agrawal 		}
24444e694fe5SAkhil Goyal 	}
24454e694fe5SAkhil Goyal 	rte_spinlock_unlock(&internals->lock);
2446c3e85bdcSAkhil Goyal 
2447c3e85bdcSAkhil Goyal 	return 0;
2448e79416d1SHemant Agrawal 
2449e79416d1SHemant Agrawal err1:
2450e79416d1SHemant Agrawal 	rte_free(session->cipher_key.data);
2451e79416d1SHemant Agrawal 	rte_free(session->auth_key.data);
2452e79416d1SHemant Agrawal 	memset(session, 0, sizeof(dpaa_sec_session));
2453e79416d1SHemant Agrawal 
2454e79416d1SHemant Agrawal 	return -EINVAL;
2455c3e85bdcSAkhil Goyal }
2456c3e85bdcSAkhil Goyal 
2457c3e85bdcSAkhil Goyal static int
2458012c5076SPablo de Lara dpaa_sec_sym_session_configure(struct rte_cryptodev *dev,
2459c3e85bdcSAkhil Goyal 		struct rte_crypto_sym_xform *xform,
2460c3e85bdcSAkhil Goyal 		struct rte_cryptodev_sym_session *sess,
2461c3e85bdcSAkhil Goyal 		struct rte_mempool *mempool)
2462c3e85bdcSAkhil Goyal {
2463c3e85bdcSAkhil Goyal 	void *sess_private_data;
2464c3e85bdcSAkhil Goyal 	int ret;
2465c3e85bdcSAkhil Goyal 
2466c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2467c3e85bdcSAkhil Goyal 
2468c3e85bdcSAkhil Goyal 	if (rte_mempool_get(mempool, &sess_private_data)) {
2469f163231eSHemant Agrawal 		DPAA_SEC_ERR("Couldn't get object from session mempool");
2470c3e85bdcSAkhil Goyal 		return -ENOMEM;
2471c3e85bdcSAkhil Goyal 	}
2472c3e85bdcSAkhil Goyal 
2473c3e85bdcSAkhil Goyal 	ret = dpaa_sec_set_session_parameters(dev, xform, sess_private_data);
2474c3e85bdcSAkhil Goyal 	if (ret != 0) {
2475f163231eSHemant Agrawal 		DPAA_SEC_ERR("failed to configure session parameters");
2476c3e85bdcSAkhil Goyal 
2477c3e85bdcSAkhil Goyal 		/* Return session to mempool */
2478c3e85bdcSAkhil Goyal 		rte_mempool_put(mempool, sess_private_data);
2479c3e85bdcSAkhil Goyal 		return ret;
2480c3e85bdcSAkhil Goyal 	}
2481c3e85bdcSAkhil Goyal 
2482012c5076SPablo de Lara 	set_sym_session_private_data(sess, dev->driver_id,
2483c3e85bdcSAkhil Goyal 			sess_private_data);
2484c3e85bdcSAkhil Goyal 
2485e79416d1SHemant Agrawal 
2486c3e85bdcSAkhil Goyal 	return 0;
2487c3e85bdcSAkhil Goyal }
2488c3e85bdcSAkhil Goyal 
24893d0d5332SAkhil Goyal static inline void
24903d0d5332SAkhil Goyal free_session_memory(struct rte_cryptodev *dev, dpaa_sec_session *s)
2491c3e85bdcSAkhil Goyal {
2492e79416d1SHemant Agrawal 	struct dpaa_sec_dev_private *qi = dev->data->dev_private;
24933d0d5332SAkhil Goyal 	struct rte_mempool *sess_mp = rte_mempool_from_obj((void *)s);
24943d0d5332SAkhil Goyal 	uint8_t i;
2495e79416d1SHemant Agrawal 
2496e621d970SAkhil Goyal 	for (i = 0; i < MAX_DPAA_CORES; i++) {
2497e621d970SAkhil Goyal 		if (s->inq[i])
2498e621d970SAkhil Goyal 			dpaa_sec_detach_rxq(qi, s->inq[i]);
2499e621d970SAkhil Goyal 		s->inq[i] = NULL;
2500e621d970SAkhil Goyal 		s->qp[i] = NULL;
2501e621d970SAkhil Goyal 	}
2502c3e85bdcSAkhil Goyal 	rte_free(s->cipher_key.data);
2503c3e85bdcSAkhil Goyal 	rte_free(s->auth_key.data);
2504c3e85bdcSAkhil Goyal 	memset(s, 0, sizeof(dpaa_sec_session));
25053d0d5332SAkhil Goyal 	rte_mempool_put(sess_mp, (void *)s);
25063d0d5332SAkhil Goyal }
25073d0d5332SAkhil Goyal 
25083d0d5332SAkhil Goyal /** Clear the memory of session so it doesn't leave key material behind */
25093d0d5332SAkhil Goyal static void
25103d0d5332SAkhil Goyal dpaa_sec_sym_session_clear(struct rte_cryptodev *dev,
25113d0d5332SAkhil Goyal 		struct rte_cryptodev_sym_session *sess)
25123d0d5332SAkhil Goyal {
25133d0d5332SAkhil Goyal 	PMD_INIT_FUNC_TRACE();
25143d0d5332SAkhil Goyal 	uint8_t index = dev->driver_id;
25153d0d5332SAkhil Goyal 	void *sess_priv = get_sym_session_private_data(sess, index);
25163d0d5332SAkhil Goyal 	dpaa_sec_session *s = (dpaa_sec_session *)sess_priv;
25173d0d5332SAkhil Goyal 
25183d0d5332SAkhil Goyal 	if (sess_priv) {
25193d0d5332SAkhil Goyal 		free_session_memory(dev, s);
2520012c5076SPablo de Lara 		set_sym_session_private_data(sess, index, NULL);
2521c3e85bdcSAkhil Goyal 	}
2522c3e85bdcSAkhil Goyal }
2523c3e85bdcSAkhil Goyal 
2524314424b6SHemant Agrawal #ifdef RTE_LIBRTE_SECURITY
2525c3e85bdcSAkhil Goyal static int
25261f14d500SAkhil Goyal dpaa_sec_set_ipsec_session(__rte_unused struct rte_cryptodev *dev,
25271f14d500SAkhil Goyal 			   struct rte_security_session_conf *conf,
25281f14d500SAkhil Goyal 			   void *sess)
25291f14d500SAkhil Goyal {
25301f14d500SAkhil Goyal 	struct dpaa_sec_dev_private *internals = dev->data->dev_private;
25311f14d500SAkhil Goyal 	struct rte_security_ipsec_xform *ipsec_xform = &conf->ipsec;
253205b12700SHemant Agrawal 	struct rte_crypto_auth_xform *auth_xform = NULL;
253305b12700SHemant Agrawal 	struct rte_crypto_cipher_xform *cipher_xform = NULL;
25341f14d500SAkhil Goyal 	dpaa_sec_session *session = (dpaa_sec_session *)sess;
25354e694fe5SAkhil Goyal 	uint32_t i;
25361f14d500SAkhil Goyal 
25371f14d500SAkhil Goyal 	PMD_INIT_FUNC_TRACE();
25381f14d500SAkhil Goyal 
2539b0894102SAkhil Goyal 	memset(session, 0, sizeof(dpaa_sec_session));
25401f14d500SAkhil Goyal 	if (ipsec_xform->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
25411f14d500SAkhil Goyal 		cipher_xform = &conf->crypto_xform->cipher;
254205b12700SHemant Agrawal 		if (conf->crypto_xform->next)
25431f14d500SAkhil Goyal 			auth_xform = &conf->crypto_xform->next->auth;
25441f14d500SAkhil Goyal 	} else {
25451f14d500SAkhil Goyal 		auth_xform = &conf->crypto_xform->auth;
254605b12700SHemant Agrawal 		if (conf->crypto_xform->next)
25471f14d500SAkhil Goyal 			cipher_xform = &conf->crypto_xform->next->cipher;
25481f14d500SAkhil Goyal 	}
25491f14d500SAkhil Goyal 	session->proto_alg = conf->protocol;
25508524b44eSHemant Agrawal 	session->ctxt = DPAA_SEC_IPSEC;
255105b12700SHemant Agrawal 
255205b12700SHemant Agrawal 	if (cipher_xform && cipher_xform->algo != RTE_CRYPTO_CIPHER_NULL) {
25531f14d500SAkhil Goyal 		session->cipher_key.data = rte_zmalloc(NULL,
25541f14d500SAkhil Goyal 						       cipher_xform->key.length,
25551f14d500SAkhil Goyal 						       RTE_CACHE_LINE_SIZE);
25561f14d500SAkhil Goyal 		if (session->cipher_key.data == NULL &&
25571f14d500SAkhil Goyal 				cipher_xform->key.length > 0) {
2558f163231eSHemant Agrawal 			DPAA_SEC_ERR("No Memory for cipher key");
25591f14d500SAkhil Goyal 			return -ENOMEM;
25601f14d500SAkhil Goyal 		}
256105b12700SHemant Agrawal 		memcpy(session->cipher_key.data, cipher_xform->key.data,
256205b12700SHemant Agrawal 				cipher_xform->key.length);
25631f14d500SAkhil Goyal 		session->cipher_key.length = cipher_xform->key.length;
256405b12700SHemant Agrawal 
256505b12700SHemant Agrawal 		switch (cipher_xform->algo) {
25668524b44eSHemant Agrawal 		case RTE_CRYPTO_CIPHER_NULL:
25678524b44eSHemant Agrawal 			session->cipher_key.alg = OP_PCL_IPSEC_NULL;
25688524b44eSHemant Agrawal 			break;
256905b12700SHemant Agrawal 		case RTE_CRYPTO_CIPHER_AES_CBC:
25708524b44eSHemant Agrawal 			session->cipher_key.alg = OP_PCL_IPSEC_AES_CBC;
25718524b44eSHemant Agrawal 			session->cipher_key.algmode = OP_ALG_AAI_CBC;
25728524b44eSHemant Agrawal 			break;
257305b12700SHemant Agrawal 		case RTE_CRYPTO_CIPHER_3DES_CBC:
25748524b44eSHemant Agrawal 			session->cipher_key.alg = OP_PCL_IPSEC_3DES;
25758524b44eSHemant Agrawal 			session->cipher_key.algmode = OP_ALG_AAI_CBC;
25768524b44eSHemant Agrawal 			break;
257705b12700SHemant Agrawal 		case RTE_CRYPTO_CIPHER_AES_CTR:
25788524b44eSHemant Agrawal 			session->cipher_key.alg = OP_PCL_IPSEC_AES_CTR;
25798524b44eSHemant Agrawal 			session->cipher_key.algmode = OP_ALG_AAI_CTR;
258005b12700SHemant Agrawal 			break;
258105b12700SHemant Agrawal 		default:
258205b12700SHemant Agrawal 			DPAA_SEC_ERR("Crypto: Unsupported Cipher alg %u",
258305b12700SHemant Agrawal 				cipher_xform->algo);
258405b12700SHemant Agrawal 			goto out;
258505b12700SHemant Agrawal 		}
258605b12700SHemant Agrawal 		session->cipher_alg = cipher_xform->algo;
258705b12700SHemant Agrawal 	} else {
258805b12700SHemant Agrawal 		session->cipher_key.data = NULL;
258905b12700SHemant Agrawal 		session->cipher_key.length = 0;
259005b12700SHemant Agrawal 		session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
259105b12700SHemant Agrawal 	}
259205b12700SHemant Agrawal 
259305b12700SHemant Agrawal 	if (auth_xform && auth_xform->algo != RTE_CRYPTO_AUTH_NULL) {
25941f14d500SAkhil Goyal 		session->auth_key.data = rte_zmalloc(NULL,
25951f14d500SAkhil Goyal 						auth_xform->key.length,
25961f14d500SAkhil Goyal 						RTE_CACHE_LINE_SIZE);
25971f14d500SAkhil Goyal 		if (session->auth_key.data == NULL &&
25981f14d500SAkhil Goyal 				auth_xform->key.length > 0) {
2599f163231eSHemant Agrawal 			DPAA_SEC_ERR("No Memory for auth key");
26001f14d500SAkhil Goyal 			rte_free(session->cipher_key.data);
26011f14d500SAkhil Goyal 			return -ENOMEM;
26021f14d500SAkhil Goyal 		}
26031f14d500SAkhil Goyal 		memcpy(session->auth_key.data, auth_xform->key.data,
26041f14d500SAkhil Goyal 				auth_xform->key.length);
260505b12700SHemant Agrawal 		session->auth_key.length = auth_xform->key.length;
26061f14d500SAkhil Goyal 
26071f14d500SAkhil Goyal 		switch (auth_xform->algo) {
26088524b44eSHemant Agrawal 		case RTE_CRYPTO_AUTH_NULL:
26098524b44eSHemant Agrawal 			session->auth_key.alg = OP_PCL_IPSEC_HMAC_NULL;
26108524b44eSHemant Agrawal 			session->digest_length = 0;
26118524b44eSHemant Agrawal 			break;
26121f14d500SAkhil Goyal 		case RTE_CRYPTO_AUTH_MD5_HMAC:
26138524b44eSHemant Agrawal 			session->auth_key.alg = OP_PCL_IPSEC_HMAC_MD5_96;
26148524b44eSHemant Agrawal 			session->auth_key.algmode = OP_ALG_AAI_HMAC;
26158524b44eSHemant Agrawal 			break;
26168524b44eSHemant Agrawal 		case RTE_CRYPTO_AUTH_SHA1_HMAC:
26178524b44eSHemant Agrawal 			session->auth_key.alg = OP_PCL_IPSEC_HMAC_SHA1_96;
26188524b44eSHemant Agrawal 			session->auth_key.algmode = OP_ALG_AAI_HMAC;
26198524b44eSHemant Agrawal 			break;
26208524b44eSHemant Agrawal 		case RTE_CRYPTO_AUTH_SHA224_HMAC:
26218524b44eSHemant Agrawal 			session->auth_key.alg = OP_PCL_IPSEC_HMAC_SHA1_160;
26228524b44eSHemant Agrawal 			session->auth_key.algmode = OP_ALG_AAI_HMAC;
26238524b44eSHemant Agrawal 			break;
26241f14d500SAkhil Goyal 		case RTE_CRYPTO_AUTH_SHA256_HMAC:
26258524b44eSHemant Agrawal 			session->auth_key.alg = OP_PCL_IPSEC_HMAC_SHA2_256_128;
26268524b44eSHemant Agrawal 			session->auth_key.algmode = OP_ALG_AAI_HMAC;
26278524b44eSHemant Agrawal 			break;
26281f14d500SAkhil Goyal 		case RTE_CRYPTO_AUTH_SHA384_HMAC:
26298524b44eSHemant Agrawal 			session->auth_key.alg = OP_PCL_IPSEC_HMAC_SHA2_384_192;
26308524b44eSHemant Agrawal 			session->auth_key.algmode = OP_ALG_AAI_HMAC;
26318524b44eSHemant Agrawal 			break;
26321f14d500SAkhil Goyal 		case RTE_CRYPTO_AUTH_SHA512_HMAC:
26338524b44eSHemant Agrawal 			session->auth_key.alg = OP_PCL_IPSEC_HMAC_SHA2_512_256;
26348524b44eSHemant Agrawal 			session->auth_key.algmode = OP_ALG_AAI_HMAC;
26351f14d500SAkhil Goyal 			break;
263605b12700SHemant Agrawal 		default:
2637f163231eSHemant Agrawal 			DPAA_SEC_ERR("Crypto: Unsupported auth alg %u",
26381f14d500SAkhil Goyal 				auth_xform->algo);
26391f14d500SAkhil Goyal 			goto out;
26401f14d500SAkhil Goyal 		}
264105b12700SHemant Agrawal 		session->auth_alg = auth_xform->algo;
264205b12700SHemant Agrawal 	} else {
264305b12700SHemant Agrawal 		session->auth_key.data = NULL;
264405b12700SHemant Agrawal 		session->auth_key.length = 0;
264505b12700SHemant Agrawal 		session->auth_alg = RTE_CRYPTO_AUTH_NULL;
26461f14d500SAkhil Goyal 	}
26471f14d500SAkhil Goyal 
26481f14d500SAkhil Goyal 	if (ipsec_xform->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
26495ab35d2eSAkhil Goyal 		if (ipsec_xform->tunnel.type ==
26505ab35d2eSAkhil Goyal 				RTE_SECURITY_IPSEC_TUNNEL_IPV4) {
26515ab35d2eSAkhil Goyal 			memset(&session->encap_pdb, 0,
26525ab35d2eSAkhil Goyal 				sizeof(struct ipsec_encap_pdb) +
26531f14d500SAkhil Goyal 				sizeof(session->ip4_hdr));
26541f14d500SAkhil Goyal 			session->ip4_hdr.ip_v = IPVERSION;
26551f14d500SAkhil Goyal 			session->ip4_hdr.ip_hl = 5;
26561f14d500SAkhil Goyal 			session->ip4_hdr.ip_len = rte_cpu_to_be_16(
26571f14d500SAkhil Goyal 						sizeof(session->ip4_hdr));
26581f14d500SAkhil Goyal 			session->ip4_hdr.ip_tos = ipsec_xform->tunnel.ipv4.dscp;
26591f14d500SAkhil Goyal 			session->ip4_hdr.ip_id = 0;
26601f14d500SAkhil Goyal 			session->ip4_hdr.ip_off = 0;
26611f14d500SAkhil Goyal 			session->ip4_hdr.ip_ttl = ipsec_xform->tunnel.ipv4.ttl;
26621f14d500SAkhil Goyal 			session->ip4_hdr.ip_p = (ipsec_xform->proto ==
26635ab35d2eSAkhil Goyal 					RTE_SECURITY_IPSEC_SA_PROTO_ESP) ?
26645ab35d2eSAkhil Goyal 					IPPROTO_ESP : IPPROTO_AH;
26651f14d500SAkhil Goyal 			session->ip4_hdr.ip_sum = 0;
26665ab35d2eSAkhil Goyal 			session->ip4_hdr.ip_src =
26675ab35d2eSAkhil Goyal 					ipsec_xform->tunnel.ipv4.src_ip;
26685ab35d2eSAkhil Goyal 			session->ip4_hdr.ip_dst =
26695ab35d2eSAkhil Goyal 					ipsec_xform->tunnel.ipv4.dst_ip;
26701f14d500SAkhil Goyal 			session->ip4_hdr.ip_sum = calc_chksum((uint16_t *)
26711f14d500SAkhil Goyal 						(void *)&session->ip4_hdr,
26721f14d500SAkhil Goyal 						sizeof(struct ip));
26735ab35d2eSAkhil Goyal 			session->encap_pdb.ip_hdr_len = sizeof(struct ip);
26745ab35d2eSAkhil Goyal 		} else if (ipsec_xform->tunnel.type ==
26755ab35d2eSAkhil Goyal 				RTE_SECURITY_IPSEC_TUNNEL_IPV6) {
26765ab35d2eSAkhil Goyal 			memset(&session->encap_pdb, 0,
26775ab35d2eSAkhil Goyal 				sizeof(struct ipsec_encap_pdb) +
26785ab35d2eSAkhil Goyal 				sizeof(session->ip6_hdr));
26795ab35d2eSAkhil Goyal 			session->ip6_hdr.vtc_flow = rte_cpu_to_be_32(
26805ab35d2eSAkhil Goyal 				DPAA_IPv6_DEFAULT_VTC_FLOW |
26815ab35d2eSAkhil Goyal 				((ipsec_xform->tunnel.ipv6.dscp <<
26825ab35d2eSAkhil Goyal 					RTE_IPV6_HDR_TC_SHIFT) &
26835ab35d2eSAkhil Goyal 					RTE_IPV6_HDR_TC_MASK) |
26845ab35d2eSAkhil Goyal 				((ipsec_xform->tunnel.ipv6.flabel <<
26855ab35d2eSAkhil Goyal 					RTE_IPV6_HDR_FL_SHIFT) &
26865ab35d2eSAkhil Goyal 					RTE_IPV6_HDR_FL_MASK));
26875ab35d2eSAkhil Goyal 			/* Payload length will be updated by HW */
26885ab35d2eSAkhil Goyal 			session->ip6_hdr.payload_len = 0;
26895ab35d2eSAkhil Goyal 			session->ip6_hdr.hop_limits =
26905ab35d2eSAkhil Goyal 					ipsec_xform->tunnel.ipv6.hlimit;
26915ab35d2eSAkhil Goyal 			session->ip6_hdr.proto = (ipsec_xform->proto ==
26925ab35d2eSAkhil Goyal 					RTE_SECURITY_IPSEC_SA_PROTO_ESP) ?
26935ab35d2eSAkhil Goyal 					IPPROTO_ESP : IPPROTO_AH;
26945ab35d2eSAkhil Goyal 			memcpy(&session->ip6_hdr.src_addr,
26955ab35d2eSAkhil Goyal 					&ipsec_xform->tunnel.ipv6.src_addr, 16);
26965ab35d2eSAkhil Goyal 			memcpy(&session->ip6_hdr.dst_addr,
26975ab35d2eSAkhil Goyal 					&ipsec_xform->tunnel.ipv6.dst_addr, 16);
26985ab35d2eSAkhil Goyal 			session->encap_pdb.ip_hdr_len =
26995ab35d2eSAkhil Goyal 						sizeof(struct rte_ipv6_hdr);
27005ab35d2eSAkhil Goyal 		}
27011f14d500SAkhil Goyal 		session->encap_pdb.options =
27021f14d500SAkhil Goyal 			(IPVERSION << PDBNH_ESP_ENCAP_SHIFT) |
27031f14d500SAkhil Goyal 			PDBOPTS_ESP_OIHI_PDB_INL |
27041f14d500SAkhil Goyal 			PDBOPTS_ESP_IVSRC |
270579fde9d0SAkhil Goyal 			PDBHMO_ESP_ENCAP_DTTL |
270679fde9d0SAkhil Goyal 			PDBHMO_ESP_SNR;
27070f318781SAkhil Goyal 		if (ipsec_xform->options.esn)
27080f318781SAkhil Goyal 			session->encap_pdb.options |= PDBOPTS_ESP_ESN;
27091f14d500SAkhil Goyal 		session->encap_pdb.spi = ipsec_xform->spi;
27101f14d500SAkhil Goyal 		session->dir = DIR_ENC;
27111f14d500SAkhil Goyal 	} else if (ipsec_xform->direction ==
27121f14d500SAkhil Goyal 			RTE_SECURITY_IPSEC_SA_DIR_INGRESS) {
27131f14d500SAkhil Goyal 		memset(&session->decap_pdb, 0, sizeof(struct ipsec_decap_pdb));
27145ab35d2eSAkhil Goyal 		if (ipsec_xform->tunnel.type == RTE_SECURITY_IPSEC_TUNNEL_IPV4)
27151f14d500SAkhil Goyal 			session->decap_pdb.options = sizeof(struct ip) << 16;
27165ab35d2eSAkhil Goyal 		else
27175ab35d2eSAkhil Goyal 			session->decap_pdb.options =
27185ab35d2eSAkhil Goyal 					sizeof(struct rte_ipv6_hdr) << 16;
27190f318781SAkhil Goyal 		if (ipsec_xform->options.esn)
27200f318781SAkhil Goyal 			session->decap_pdb.options |= PDBOPTS_ESP_ESN;
27211f14d500SAkhil Goyal 		session->dir = DIR_DEC;
27221f14d500SAkhil Goyal 	} else
27231f14d500SAkhil Goyal 		goto out;
27243b617ee7SAkhil Goyal 	rte_spinlock_lock(&internals->lock);
27254e694fe5SAkhil Goyal 	for (i = 0; i < MAX_DPAA_CORES; i++) {
27264e694fe5SAkhil Goyal 		session->inq[i] = dpaa_sec_attach_rxq(internals);
27274e694fe5SAkhil Goyal 		if (session->inq[i] == NULL) {
2728f163231eSHemant Agrawal 			DPAA_SEC_ERR("unable to attach sec queue");
27294e694fe5SAkhil Goyal 			rte_spinlock_unlock(&internals->lock);
27301f14d500SAkhil Goyal 			goto out;
27311f14d500SAkhil Goyal 		}
27324e694fe5SAkhil Goyal 	}
27334e694fe5SAkhil Goyal 	rte_spinlock_unlock(&internals->lock);
27341f14d500SAkhil Goyal 
2735a1173d55SHemant Agrawal 	return 0;
2736a1173d55SHemant Agrawal out:
2737a1173d55SHemant Agrawal 	rte_free(session->auth_key.data);
2738a1173d55SHemant Agrawal 	rte_free(session->cipher_key.data);
2739a1173d55SHemant Agrawal 	memset(session, 0, sizeof(dpaa_sec_session));
2740a1173d55SHemant Agrawal 	return -1;
2741a1173d55SHemant Agrawal }
27421f14d500SAkhil Goyal 
2743a1173d55SHemant Agrawal static int
2744a1173d55SHemant Agrawal dpaa_sec_set_pdcp_session(struct rte_cryptodev *dev,
2745a1173d55SHemant Agrawal 			  struct rte_security_session_conf *conf,
2746a1173d55SHemant Agrawal 			  void *sess)
2747a1173d55SHemant Agrawal {
2748a1173d55SHemant Agrawal 	struct rte_security_pdcp_xform *pdcp_xform = &conf->pdcp;
2749a1173d55SHemant Agrawal 	struct rte_crypto_sym_xform *xform = conf->crypto_xform;
2750a1173d55SHemant Agrawal 	struct rte_crypto_auth_xform *auth_xform = NULL;
2751a1173d55SHemant Agrawal 	struct rte_crypto_cipher_xform *cipher_xform = NULL;
2752a1173d55SHemant Agrawal 	dpaa_sec_session *session = (dpaa_sec_session *)sess;
2753a1173d55SHemant Agrawal 	struct dpaa_sec_dev_private *dev_priv = dev->data->dev_private;
27544e694fe5SAkhil Goyal 	uint32_t i;
2755a1173d55SHemant Agrawal 
2756a1173d55SHemant Agrawal 	PMD_INIT_FUNC_TRACE();
2757a1173d55SHemant Agrawal 
2758a1173d55SHemant Agrawal 	memset(session, 0, sizeof(dpaa_sec_session));
2759a1173d55SHemant Agrawal 
2760a1173d55SHemant Agrawal 	/* find xfrm types */
2761a1173d55SHemant Agrawal 	if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
2762a1173d55SHemant Agrawal 		cipher_xform = &xform->cipher;
2763a1173d55SHemant Agrawal 		if (xform->next != NULL)
2764a1173d55SHemant Agrawal 			auth_xform = &xform->next->auth;
2765a1173d55SHemant Agrawal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
2766a1173d55SHemant Agrawal 		auth_xform = &xform->auth;
2767a1173d55SHemant Agrawal 		if (xform->next != NULL)
2768a1173d55SHemant Agrawal 			cipher_xform = &xform->next->cipher;
2769a1173d55SHemant Agrawal 	} else {
2770a1173d55SHemant Agrawal 		DPAA_SEC_ERR("Invalid crypto type");
2771a1173d55SHemant Agrawal 		return -EINVAL;
2772a1173d55SHemant Agrawal 	}
2773a1173d55SHemant Agrawal 
2774a1173d55SHemant Agrawal 	session->proto_alg = conf->protocol;
27758524b44eSHemant Agrawal 	session->ctxt = DPAA_SEC_PDCP;
27768524b44eSHemant Agrawal 
2777a1173d55SHemant Agrawal 	if (cipher_xform) {
27788524b44eSHemant Agrawal 		switch (cipher_xform->algo) {
27798524b44eSHemant Agrawal 		case RTE_CRYPTO_CIPHER_SNOW3G_UEA2:
27808524b44eSHemant Agrawal 			session->cipher_key.alg = PDCP_CIPHER_TYPE_SNOW;
27818524b44eSHemant Agrawal 			break;
27828524b44eSHemant Agrawal 		case RTE_CRYPTO_CIPHER_ZUC_EEA3:
27838524b44eSHemant Agrawal 			session->cipher_key.alg = PDCP_CIPHER_TYPE_ZUC;
27848524b44eSHemant Agrawal 			break;
27858524b44eSHemant Agrawal 		case RTE_CRYPTO_CIPHER_AES_CTR:
27868524b44eSHemant Agrawal 			session->cipher_key.alg = PDCP_CIPHER_TYPE_AES;
27878524b44eSHemant Agrawal 			break;
27888524b44eSHemant Agrawal 		case RTE_CRYPTO_CIPHER_NULL:
27898524b44eSHemant Agrawal 			session->cipher_key.alg = PDCP_CIPHER_TYPE_NULL;
27908524b44eSHemant Agrawal 			break;
27918524b44eSHemant Agrawal 		default:
27928524b44eSHemant Agrawal 			DPAA_SEC_ERR("Crypto: Undefined Cipher specified %u",
27938524b44eSHemant Agrawal 				      session->cipher_alg);
27948524b44eSHemant Agrawal 			return -1;
27958524b44eSHemant Agrawal 		}
27968524b44eSHemant Agrawal 
2797a1173d55SHemant Agrawal 		session->cipher_key.data = rte_zmalloc(NULL,
2798a1173d55SHemant Agrawal 					       cipher_xform->key.length,
2799a1173d55SHemant Agrawal 					       RTE_CACHE_LINE_SIZE);
2800a1173d55SHemant Agrawal 		if (session->cipher_key.data == NULL &&
2801a1173d55SHemant Agrawal 				cipher_xform->key.length > 0) {
2802a1173d55SHemant Agrawal 			DPAA_SEC_ERR("No Memory for cipher key");
2803a1173d55SHemant Agrawal 			return -ENOMEM;
2804a1173d55SHemant Agrawal 		}
2805a1173d55SHemant Agrawal 		session->cipher_key.length = cipher_xform->key.length;
2806a1173d55SHemant Agrawal 		memcpy(session->cipher_key.data, cipher_xform->key.data,
2807a1173d55SHemant Agrawal 			cipher_xform->key.length);
2808a1173d55SHemant Agrawal 		session->dir = (cipher_xform->op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
2809a1173d55SHemant Agrawal 					DIR_ENC : DIR_DEC;
2810a1173d55SHemant Agrawal 		session->cipher_alg = cipher_xform->algo;
2811a1173d55SHemant Agrawal 	} else {
2812a1173d55SHemant Agrawal 		session->cipher_key.data = NULL;
2813a1173d55SHemant Agrawal 		session->cipher_key.length = 0;
2814a1173d55SHemant Agrawal 		session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
2815a1173d55SHemant Agrawal 		session->dir = DIR_ENC;
2816a1173d55SHemant Agrawal 	}
2817a1173d55SHemant Agrawal 
2818a1173d55SHemant Agrawal 	if (pdcp_xform->domain == RTE_SECURITY_PDCP_MODE_CONTROL) {
2819eac60082SVakul Garg 		if (pdcp_xform->sn_size != RTE_SECURITY_PDCP_SN_SIZE_5 &&
2820eac60082SVakul Garg 		    pdcp_xform->sn_size != RTE_SECURITY_PDCP_SN_SIZE_12) {
2821a1173d55SHemant Agrawal 			DPAA_SEC_ERR(
2822eac60082SVakul Garg 				"PDCP Seq Num size should be 5/12 bits for cmode");
2823a1173d55SHemant Agrawal 			goto out;
2824a1173d55SHemant Agrawal 		}
28252e4cbdb4SVakul Garg 	}
28262e4cbdb4SVakul Garg 
2827a1173d55SHemant Agrawal 	if (auth_xform) {
28288524b44eSHemant Agrawal 		switch (auth_xform->algo) {
28298524b44eSHemant Agrawal 		case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
28308524b44eSHemant Agrawal 			session->auth_key.alg = PDCP_AUTH_TYPE_SNOW;
28318524b44eSHemant Agrawal 			break;
28328524b44eSHemant Agrawal 		case RTE_CRYPTO_AUTH_ZUC_EIA3:
28338524b44eSHemant Agrawal 			session->auth_key.alg = PDCP_AUTH_TYPE_ZUC;
28348524b44eSHemant Agrawal 			break;
28358524b44eSHemant Agrawal 		case RTE_CRYPTO_AUTH_AES_CMAC:
28368524b44eSHemant Agrawal 			session->auth_key.alg = PDCP_AUTH_TYPE_AES;
28378524b44eSHemant Agrawal 			break;
28388524b44eSHemant Agrawal 		case RTE_CRYPTO_AUTH_NULL:
28398524b44eSHemant Agrawal 			session->auth_key.alg = PDCP_AUTH_TYPE_NULL;
28408524b44eSHemant Agrawal 			break;
28418524b44eSHemant Agrawal 		default:
28428524b44eSHemant Agrawal 			DPAA_SEC_ERR("Crypto: Unsupported auth alg %u",
28438524b44eSHemant Agrawal 				      session->auth_alg);
28448524b44eSHemant Agrawal 			rte_free(session->cipher_key.data);
28458524b44eSHemant Agrawal 			return -1;
28468524b44eSHemant Agrawal 		}
2847a1173d55SHemant Agrawal 		session->auth_key.data = rte_zmalloc(NULL,
2848a1173d55SHemant Agrawal 						     auth_xform->key.length,
2849a1173d55SHemant Agrawal 						     RTE_CACHE_LINE_SIZE);
28502e4cbdb4SVakul Garg 		if (!session->auth_key.data &&
2851a1173d55SHemant Agrawal 		    auth_xform->key.length > 0) {
2852a1173d55SHemant Agrawal 			DPAA_SEC_ERR("No Memory for auth key");
2853a1173d55SHemant Agrawal 			rte_free(session->cipher_key.data);
2854a1173d55SHemant Agrawal 			return -ENOMEM;
2855a1173d55SHemant Agrawal 		}
2856a1173d55SHemant Agrawal 		session->auth_key.length = auth_xform->key.length;
2857a1173d55SHemant Agrawal 		memcpy(session->auth_key.data, auth_xform->key.data,
2858a1173d55SHemant Agrawal 		       auth_xform->key.length);
2859a1173d55SHemant Agrawal 		session->auth_alg = auth_xform->algo;
2860a1173d55SHemant Agrawal 	} else {
2861a1173d55SHemant Agrawal 		session->auth_key.data = NULL;
2862a1173d55SHemant Agrawal 		session->auth_key.length = 0;
28632e4cbdb4SVakul Garg 		session->auth_alg = 0;
2864a1173d55SHemant Agrawal 	}
2865a1173d55SHemant Agrawal 	session->pdcp.domain = pdcp_xform->domain;
2866a1173d55SHemant Agrawal 	session->pdcp.bearer = pdcp_xform->bearer;
2867a1173d55SHemant Agrawal 	session->pdcp.pkt_dir = pdcp_xform->pkt_dir;
2868a1173d55SHemant Agrawal 	session->pdcp.sn_size = pdcp_xform->sn_size;
2869a1173d55SHemant Agrawal 	session->pdcp.hfn = pdcp_xform->hfn;
2870a1173d55SHemant Agrawal 	session->pdcp.hfn_threshold = pdcp_xform->hfn_threshold;
28716a0c9d36SAkhil Goyal 	session->pdcp.hfn_ovd = pdcp_xform->hfn_ovrd;
28726a0c9d36SAkhil Goyal 	session->pdcp.hfn_ovd_offset = cipher_xform->iv.offset;
2873a1173d55SHemant Agrawal 
2874a1173d55SHemant Agrawal 	rte_spinlock_lock(&dev_priv->lock);
28754e694fe5SAkhil Goyal 	for (i = 0; i < MAX_DPAA_CORES; i++) {
28764e694fe5SAkhil Goyal 		session->inq[i] = dpaa_sec_attach_rxq(dev_priv);
28774e694fe5SAkhil Goyal 		if (session->inq[i] == NULL) {
2878a1173d55SHemant Agrawal 			DPAA_SEC_ERR("unable to attach sec queue");
28794e694fe5SAkhil Goyal 			rte_spinlock_unlock(&dev_priv->lock);
2880a1173d55SHemant Agrawal 			goto out;
2881a1173d55SHemant Agrawal 		}
28824e694fe5SAkhil Goyal 	}
28834e694fe5SAkhil Goyal 	rte_spinlock_unlock(&dev_priv->lock);
28841f14d500SAkhil Goyal 	return 0;
28851f14d500SAkhil Goyal out:
28861f14d500SAkhil Goyal 	rte_free(session->auth_key.data);
28871f14d500SAkhil Goyal 	rte_free(session->cipher_key.data);
28881f14d500SAkhil Goyal 	memset(session, 0, sizeof(dpaa_sec_session));
28891f14d500SAkhil Goyal 	return -1;
28901f14d500SAkhil Goyal }
28911f14d500SAkhil Goyal 
28921f14d500SAkhil Goyal static int
28931f14d500SAkhil Goyal dpaa_sec_security_session_create(void *dev,
28941f14d500SAkhil Goyal 				 struct rte_security_session_conf *conf,
28951f14d500SAkhil Goyal 				 struct rte_security_session *sess,
28961f14d500SAkhil Goyal 				 struct rte_mempool *mempool)
28971f14d500SAkhil Goyal {
28981f14d500SAkhil Goyal 	void *sess_private_data;
28991f14d500SAkhil Goyal 	struct rte_cryptodev *cdev = (struct rte_cryptodev *)dev;
29001f14d500SAkhil Goyal 	int ret;
29011f14d500SAkhil Goyal 
29021f14d500SAkhil Goyal 	if (rte_mempool_get(mempool, &sess_private_data)) {
2903f163231eSHemant Agrawal 		DPAA_SEC_ERR("Couldn't get object from session mempool");
29041f14d500SAkhil Goyal 		return -ENOMEM;
29051f14d500SAkhil Goyal 	}
29061f14d500SAkhil Goyal 
29071f14d500SAkhil Goyal 	switch (conf->protocol) {
29081f14d500SAkhil Goyal 	case RTE_SECURITY_PROTOCOL_IPSEC:
29091f14d500SAkhil Goyal 		ret = dpaa_sec_set_ipsec_session(cdev, conf,
29101f14d500SAkhil Goyal 				sess_private_data);
29111f14d500SAkhil Goyal 		break;
2912a1173d55SHemant Agrawal 	case RTE_SECURITY_PROTOCOL_PDCP:
2913a1173d55SHemant Agrawal 		ret = dpaa_sec_set_pdcp_session(cdev, conf,
2914a1173d55SHemant Agrawal 				sess_private_data);
2915a1173d55SHemant Agrawal 		break;
29161f14d500SAkhil Goyal 	case RTE_SECURITY_PROTOCOL_MACSEC:
29171f14d500SAkhil Goyal 		return -ENOTSUP;
29181f14d500SAkhil Goyal 	default:
29191f14d500SAkhil Goyal 		return -EINVAL;
29201f14d500SAkhil Goyal 	}
29211f14d500SAkhil Goyal 	if (ret != 0) {
2922f163231eSHemant Agrawal 		DPAA_SEC_ERR("failed to configure session parameters");
29231f14d500SAkhil Goyal 		/* Return session to mempool */
29241f14d500SAkhil Goyal 		rte_mempool_put(mempool, sess_private_data);
29251f14d500SAkhil Goyal 		return ret;
29261f14d500SAkhil Goyal 	}
29271f14d500SAkhil Goyal 
29281f14d500SAkhil Goyal 	set_sec_session_private_data(sess, sess_private_data);
29291f14d500SAkhil Goyal 
29301f14d500SAkhil Goyal 	return ret;
29311f14d500SAkhil Goyal }
29321f14d500SAkhil Goyal 
29331f14d500SAkhil Goyal /** Clear the memory of session so it doesn't leave key material behind */
29341f14d500SAkhil Goyal static int
29351f14d500SAkhil Goyal dpaa_sec_security_session_destroy(void *dev __rte_unused,
29361f14d500SAkhil Goyal 		struct rte_security_session *sess)
29371f14d500SAkhil Goyal {
29381f14d500SAkhil Goyal 	PMD_INIT_FUNC_TRACE();
29391f14d500SAkhil Goyal 	void *sess_priv = get_sec_session_private_data(sess);
29401f14d500SAkhil Goyal 	dpaa_sec_session *s = (dpaa_sec_session *)sess_priv;
29411f14d500SAkhil Goyal 
29421f14d500SAkhil Goyal 	if (sess_priv) {
29433d0d5332SAkhil Goyal 		free_session_memory((struct rte_cryptodev *)dev, s);
29441f14d500SAkhil Goyal 		set_sec_session_private_data(sess, NULL);
29451f14d500SAkhil Goyal 	}
29461f14d500SAkhil Goyal 	return 0;
29471f14d500SAkhil Goyal }
2948314424b6SHemant Agrawal #endif
29491f14d500SAkhil Goyal static int
29502ffb940eSAkhil Goyal dpaa_sec_dev_configure(struct rte_cryptodev *dev __rte_unused,
2951c3e85bdcSAkhil Goyal 		       struct rte_cryptodev_config *config __rte_unused)
2952c3e85bdcSAkhil Goyal {
2953c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2954c3e85bdcSAkhil Goyal 
2955c3e85bdcSAkhil Goyal 	return 0;
2956c3e85bdcSAkhil Goyal }
2957c3e85bdcSAkhil Goyal 
2958c3e85bdcSAkhil Goyal static int
2959c3e85bdcSAkhil Goyal dpaa_sec_dev_start(struct rte_cryptodev *dev __rte_unused)
2960c3e85bdcSAkhil Goyal {
2961c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2962c3e85bdcSAkhil Goyal 	return 0;
2963c3e85bdcSAkhil Goyal }
2964c3e85bdcSAkhil Goyal 
2965c3e85bdcSAkhil Goyal static void
2966c3e85bdcSAkhil Goyal dpaa_sec_dev_stop(struct rte_cryptodev *dev __rte_unused)
2967c3e85bdcSAkhil Goyal {
2968c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2969c3e85bdcSAkhil Goyal }
2970c3e85bdcSAkhil Goyal 
2971c3e85bdcSAkhil Goyal static int
29727e3e2954SAkhil Goyal dpaa_sec_dev_close(struct rte_cryptodev *dev)
2973c3e85bdcSAkhil Goyal {
2974c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
29757e3e2954SAkhil Goyal 
29767e3e2954SAkhil Goyal 	if (dev == NULL)
29777e3e2954SAkhil Goyal 		return -ENOMEM;
29787e3e2954SAkhil Goyal 
2979c3e85bdcSAkhil Goyal 	return 0;
2980c3e85bdcSAkhil Goyal }
2981c3e85bdcSAkhil Goyal 
2982c3e85bdcSAkhil Goyal static void
2983c3e85bdcSAkhil Goyal dpaa_sec_dev_infos_get(struct rte_cryptodev *dev,
2984c3e85bdcSAkhil Goyal 		       struct rte_cryptodev_info *info)
2985c3e85bdcSAkhil Goyal {
2986c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals = dev->data->dev_private;
2987c3e85bdcSAkhil Goyal 
2988c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2989c3e85bdcSAkhil Goyal 	if (info != NULL) {
2990c3e85bdcSAkhil Goyal 		info->max_nb_queue_pairs = internals->max_nb_queue_pairs;
2991c3e85bdcSAkhil Goyal 		info->feature_flags = dev->feature_flags;
2992c3e85bdcSAkhil Goyal 		info->capabilities = dpaa_sec_capabilities;
2993c3e85bdcSAkhil Goyal 		info->sym.max_nb_sessions = internals->max_nb_sessions;
2994c3e85bdcSAkhil Goyal 		info->driver_id = cryptodev_driver_id;
2995c3e85bdcSAkhil Goyal 	}
2996c3e85bdcSAkhil Goyal }
2997c3e85bdcSAkhil Goyal 
2998fe3688baSAkhil Goyal static enum qman_cb_dqrr_result
2999fe3688baSAkhil Goyal dpaa_sec_process_parallel_event(void *event,
3000fe3688baSAkhil Goyal 			struct qman_portal *qm __always_unused,
3001fe3688baSAkhil Goyal 			struct qman_fq *outq,
3002fe3688baSAkhil Goyal 			const struct qm_dqrr_entry *dqrr,
3003fe3688baSAkhil Goyal 			void **bufs)
3004fe3688baSAkhil Goyal {
3005fe3688baSAkhil Goyal 	const struct qm_fd *fd;
3006fe3688baSAkhil Goyal 	struct dpaa_sec_job *job;
3007fe3688baSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
3008fe3688baSAkhil Goyal 	struct rte_event *ev = (struct rte_event *)event;
3009fe3688baSAkhil Goyal 
3010fe3688baSAkhil Goyal 	fd = &dqrr->fd;
3011fe3688baSAkhil Goyal 
3012fe3688baSAkhil Goyal 	/* sg is embedded in an op ctx,
3013fe3688baSAkhil Goyal 	 * sg[0] is for output
3014fe3688baSAkhil Goyal 	 * sg[1] for input
3015fe3688baSAkhil Goyal 	 */
3016fe3688baSAkhil Goyal 	job = dpaa_mem_ptov(qm_fd_addr_get64(fd));
3017fe3688baSAkhil Goyal 
3018fe3688baSAkhil Goyal 	ctx = container_of(job, struct dpaa_sec_op_ctx, job);
3019fe3688baSAkhil Goyal 	ctx->fd_status = fd->status;
3020fe3688baSAkhil Goyal 	if (ctx->op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
3021fe3688baSAkhil Goyal 		struct qm_sg_entry *sg_out;
3022fe3688baSAkhil Goyal 		uint32_t len;
3023fe3688baSAkhil Goyal 
3024fe3688baSAkhil Goyal 		sg_out = &job->sg[0];
3025fe3688baSAkhil Goyal 		hw_sg_to_cpu(sg_out);
3026fe3688baSAkhil Goyal 		len = sg_out->length;
3027fe3688baSAkhil Goyal 		ctx->op->sym->m_src->pkt_len = len;
3028fe3688baSAkhil Goyal 		ctx->op->sym->m_src->data_len = len;
3029fe3688baSAkhil Goyal 	}
3030fe3688baSAkhil Goyal 	if (!ctx->fd_status) {
3031fe3688baSAkhil Goyal 		ctx->op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
3032fe3688baSAkhil Goyal 	} else {
3033fe3688baSAkhil Goyal 		DPAA_SEC_DP_WARN("SEC return err: 0x%x", ctx->fd_status);
3034fe3688baSAkhil Goyal 		ctx->op->status = RTE_CRYPTO_OP_STATUS_ERROR;
3035fe3688baSAkhil Goyal 	}
3036fe3688baSAkhil Goyal 	ev->event_ptr = (void *)ctx->op;
3037fe3688baSAkhil Goyal 
3038fe3688baSAkhil Goyal 	ev->flow_id = outq->ev.flow_id;
3039fe3688baSAkhil Goyal 	ev->sub_event_type = outq->ev.sub_event_type;
3040fe3688baSAkhil Goyal 	ev->event_type = RTE_EVENT_TYPE_CRYPTODEV;
3041fe3688baSAkhil Goyal 	ev->op = RTE_EVENT_OP_NEW;
3042fe3688baSAkhil Goyal 	ev->sched_type = outq->ev.sched_type;
3043fe3688baSAkhil Goyal 	ev->queue_id = outq->ev.queue_id;
3044fe3688baSAkhil Goyal 	ev->priority = outq->ev.priority;
3045fe3688baSAkhil Goyal 	*bufs = (void *)ctx->op;
3046fe3688baSAkhil Goyal 
3047fe3688baSAkhil Goyal 	rte_mempool_put(ctx->ctx_pool, (void *)ctx);
3048fe3688baSAkhil Goyal 
3049fe3688baSAkhil Goyal 	return qman_cb_dqrr_consume;
3050fe3688baSAkhil Goyal }
3051fe3688baSAkhil Goyal 
3052fe3688baSAkhil Goyal static enum qman_cb_dqrr_result
3053fe3688baSAkhil Goyal dpaa_sec_process_atomic_event(void *event,
3054fe3688baSAkhil Goyal 			struct qman_portal *qm __rte_unused,
3055fe3688baSAkhil Goyal 			struct qman_fq *outq,
3056fe3688baSAkhil Goyal 			const struct qm_dqrr_entry *dqrr,
3057fe3688baSAkhil Goyal 			void **bufs)
3058fe3688baSAkhil Goyal {
3059fe3688baSAkhil Goyal 	u8 index;
3060fe3688baSAkhil Goyal 	const struct qm_fd *fd;
3061fe3688baSAkhil Goyal 	struct dpaa_sec_job *job;
3062fe3688baSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
3063fe3688baSAkhil Goyal 	struct rte_event *ev = (struct rte_event *)event;
3064fe3688baSAkhil Goyal 
3065fe3688baSAkhil Goyal 	fd = &dqrr->fd;
3066fe3688baSAkhil Goyal 
3067fe3688baSAkhil Goyal 	/* sg is embedded in an op ctx,
3068fe3688baSAkhil Goyal 	 * sg[0] is for output
3069fe3688baSAkhil Goyal 	 * sg[1] for input
3070fe3688baSAkhil Goyal 	 */
3071fe3688baSAkhil Goyal 	job = dpaa_mem_ptov(qm_fd_addr_get64(fd));
3072fe3688baSAkhil Goyal 
3073fe3688baSAkhil Goyal 	ctx = container_of(job, struct dpaa_sec_op_ctx, job);
3074fe3688baSAkhil Goyal 	ctx->fd_status = fd->status;
3075fe3688baSAkhil Goyal 	if (ctx->op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
3076fe3688baSAkhil Goyal 		struct qm_sg_entry *sg_out;
3077fe3688baSAkhil Goyal 		uint32_t len;
3078fe3688baSAkhil Goyal 
3079fe3688baSAkhil Goyal 		sg_out = &job->sg[0];
3080fe3688baSAkhil Goyal 		hw_sg_to_cpu(sg_out);
3081fe3688baSAkhil Goyal 		len = sg_out->length;
3082fe3688baSAkhil Goyal 		ctx->op->sym->m_src->pkt_len = len;
3083fe3688baSAkhil Goyal 		ctx->op->sym->m_src->data_len = len;
3084fe3688baSAkhil Goyal 	}
3085fe3688baSAkhil Goyal 	if (!ctx->fd_status) {
3086fe3688baSAkhil Goyal 		ctx->op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
3087fe3688baSAkhil Goyal 	} else {
3088fe3688baSAkhil Goyal 		DPAA_SEC_DP_WARN("SEC return err: 0x%x", ctx->fd_status);
3089fe3688baSAkhil Goyal 		ctx->op->status = RTE_CRYPTO_OP_STATUS_ERROR;
3090fe3688baSAkhil Goyal 	}
3091fe3688baSAkhil Goyal 	ev->event_ptr = (void *)ctx->op;
3092fe3688baSAkhil Goyal 	ev->flow_id = outq->ev.flow_id;
3093fe3688baSAkhil Goyal 	ev->sub_event_type = outq->ev.sub_event_type;
3094fe3688baSAkhil Goyal 	ev->event_type = RTE_EVENT_TYPE_CRYPTODEV;
3095fe3688baSAkhil Goyal 	ev->op = RTE_EVENT_OP_NEW;
3096fe3688baSAkhil Goyal 	ev->sched_type = outq->ev.sched_type;
3097fe3688baSAkhil Goyal 	ev->queue_id = outq->ev.queue_id;
3098fe3688baSAkhil Goyal 	ev->priority = outq->ev.priority;
3099fe3688baSAkhil Goyal 
3100fe3688baSAkhil Goyal 	/* Save active dqrr entries */
3101fe3688baSAkhil Goyal 	index = ((uintptr_t)dqrr >> 6) & (16/*QM_DQRR_SIZE*/ - 1);
3102fe3688baSAkhil Goyal 	DPAA_PER_LCORE_DQRR_SIZE++;
3103fe3688baSAkhil Goyal 	DPAA_PER_LCORE_DQRR_HELD |= 1 << index;
3104fe3688baSAkhil Goyal 	DPAA_PER_LCORE_DQRR_MBUF(index) = ctx->op->sym->m_src;
3105fe3688baSAkhil Goyal 	ev->impl_opaque = index + 1;
3106fe3688baSAkhil Goyal 	ctx->op->sym->m_src->seqn = (uint32_t)index + 1;
3107fe3688baSAkhil Goyal 	*bufs = (void *)ctx->op;
3108fe3688baSAkhil Goyal 
3109fe3688baSAkhil Goyal 	rte_mempool_put(ctx->ctx_pool, (void *)ctx);
3110fe3688baSAkhil Goyal 
3111fe3688baSAkhil Goyal 	return qman_cb_dqrr_defer;
3112fe3688baSAkhil Goyal }
3113fe3688baSAkhil Goyal 
3114fe3688baSAkhil Goyal int
3115fe3688baSAkhil Goyal dpaa_sec_eventq_attach(const struct rte_cryptodev *dev,
3116fe3688baSAkhil Goyal 		int qp_id,
3117fe3688baSAkhil Goyal 		uint16_t ch_id,
3118fe3688baSAkhil Goyal 		const struct rte_event *event)
3119fe3688baSAkhil Goyal {
3120fe3688baSAkhil Goyal 	struct dpaa_sec_qp *qp = dev->data->queue_pairs[qp_id];
3121fe3688baSAkhil Goyal 	struct qm_mcc_initfq opts = {0};
3122fe3688baSAkhil Goyal 
3123fe3688baSAkhil Goyal 	int ret;
3124fe3688baSAkhil Goyal 
3125fe3688baSAkhil Goyal 	opts.we_mask = QM_INITFQ_WE_DESTWQ | QM_INITFQ_WE_FQCTRL |
3126fe3688baSAkhil Goyal 		       QM_INITFQ_WE_CONTEXTA | QM_INITFQ_WE_CONTEXTB;
3127fe3688baSAkhil Goyal 	opts.fqd.dest.channel = ch_id;
3128fe3688baSAkhil Goyal 
3129fe3688baSAkhil Goyal 	switch (event->sched_type) {
3130fe3688baSAkhil Goyal 	case RTE_SCHED_TYPE_ATOMIC:
3131fe3688baSAkhil Goyal 		opts.fqd.fq_ctrl |= QM_FQCTRL_HOLDACTIVE;
3132fe3688baSAkhil Goyal 		/* Reset FQCTRL_AVOIDBLOCK bit as it is unnecessary
3133fe3688baSAkhil Goyal 		 * configuration with HOLD_ACTIVE setting
3134fe3688baSAkhil Goyal 		 */
3135fe3688baSAkhil Goyal 		opts.fqd.fq_ctrl &= (~QM_FQCTRL_AVOIDBLOCK);
3136fe3688baSAkhil Goyal 		qp->outq.cb.dqrr_dpdk_cb = dpaa_sec_process_atomic_event;
3137fe3688baSAkhil Goyal 		break;
3138fe3688baSAkhil Goyal 	case RTE_SCHED_TYPE_ORDERED:
3139fe3688baSAkhil Goyal 		DPAA_SEC_ERR("Ordered queue schedule type is not supported\n");
3140fe3688baSAkhil Goyal 		return -1;
3141fe3688baSAkhil Goyal 	default:
3142fe3688baSAkhil Goyal 		opts.fqd.fq_ctrl |= QM_FQCTRL_AVOIDBLOCK;
3143fe3688baSAkhil Goyal 		qp->outq.cb.dqrr_dpdk_cb = dpaa_sec_process_parallel_event;
3144fe3688baSAkhil Goyal 		break;
3145fe3688baSAkhil Goyal 	}
3146fe3688baSAkhil Goyal 
3147fe3688baSAkhil Goyal 	ret = qman_init_fq(&qp->outq, QMAN_INITFQ_FLAG_SCHED, &opts);
3148fe3688baSAkhil Goyal 	if (unlikely(ret)) {
3149fe3688baSAkhil Goyal 		DPAA_SEC_ERR("unable to init caam source fq!");
3150fe3688baSAkhil Goyal 		return ret;
3151fe3688baSAkhil Goyal 	}
3152fe3688baSAkhil Goyal 
3153fe3688baSAkhil Goyal 	memcpy(&qp->outq.ev, event, sizeof(struct rte_event));
3154fe3688baSAkhil Goyal 
3155fe3688baSAkhil Goyal 	return 0;
3156fe3688baSAkhil Goyal }
3157fe3688baSAkhil Goyal 
3158fe3688baSAkhil Goyal int
3159fe3688baSAkhil Goyal dpaa_sec_eventq_detach(const struct rte_cryptodev *dev,
3160fe3688baSAkhil Goyal 			int qp_id)
3161fe3688baSAkhil Goyal {
3162fe3688baSAkhil Goyal 	struct qm_mcc_initfq opts = {0};
3163fe3688baSAkhil Goyal 	int ret;
3164fe3688baSAkhil Goyal 	struct dpaa_sec_qp *qp = dev->data->queue_pairs[qp_id];
3165fe3688baSAkhil Goyal 
3166fe3688baSAkhil Goyal 	opts.we_mask = QM_INITFQ_WE_DESTWQ | QM_INITFQ_WE_FQCTRL |
3167fe3688baSAkhil Goyal 		       QM_INITFQ_WE_CONTEXTA | QM_INITFQ_WE_CONTEXTB;
3168fe3688baSAkhil Goyal 	qp->outq.cb.dqrr = dqrr_out_fq_cb_rx;
3169fe3688baSAkhil Goyal 	qp->outq.cb.ern  = ern_sec_fq_handler;
3170fe3688baSAkhil Goyal 	qman_retire_fq(&qp->outq, NULL);
3171fe3688baSAkhil Goyal 	qman_oos_fq(&qp->outq);
3172fe3688baSAkhil Goyal 	ret = qman_init_fq(&qp->outq, 0, &opts);
3173fe3688baSAkhil Goyal 	if (ret)
3174fe3688baSAkhil Goyal 		RTE_LOG(ERR, PMD, "Error in qman_init_fq: ret: %d\n", ret);
3175fe3688baSAkhil Goyal 	qp->outq.cb.dqrr = NULL;
3176fe3688baSAkhil Goyal 
3177fe3688baSAkhil Goyal 	return ret;
3178fe3688baSAkhil Goyal }
3179fe3688baSAkhil Goyal 
3180c3e85bdcSAkhil Goyal static struct rte_cryptodev_ops crypto_ops = {
3181c3e85bdcSAkhil Goyal 	.dev_configure	      = dpaa_sec_dev_configure,
3182c3e85bdcSAkhil Goyal 	.dev_start	      = dpaa_sec_dev_start,
3183c3e85bdcSAkhil Goyal 	.dev_stop	      = dpaa_sec_dev_stop,
3184c3e85bdcSAkhil Goyal 	.dev_close	      = dpaa_sec_dev_close,
3185c3e85bdcSAkhil Goyal 	.dev_infos_get        = dpaa_sec_dev_infos_get,
3186c3e85bdcSAkhil Goyal 	.queue_pair_setup     = dpaa_sec_queue_pair_setup,
3187c3e85bdcSAkhil Goyal 	.queue_pair_release   = dpaa_sec_queue_pair_release,
3188c3e85bdcSAkhil Goyal 	.queue_pair_count     = dpaa_sec_queue_pair_count,
3189012c5076SPablo de Lara 	.sym_session_get_size     = dpaa_sec_sym_session_get_size,
3190012c5076SPablo de Lara 	.sym_session_configure    = dpaa_sec_sym_session_configure,
3191012c5076SPablo de Lara 	.sym_session_clear        = dpaa_sec_sym_session_clear
3192c3e85bdcSAkhil Goyal };
3193c3e85bdcSAkhil Goyal 
3194314424b6SHemant Agrawal #ifdef RTE_LIBRTE_SECURITY
31951f14d500SAkhil Goyal static const struct rte_security_capability *
31961f14d500SAkhil Goyal dpaa_sec_capabilities_get(void *device __rte_unused)
31971f14d500SAkhil Goyal {
31981f14d500SAkhil Goyal 	return dpaa_sec_security_cap;
31991f14d500SAkhil Goyal }
32001f14d500SAkhil Goyal 
3201b74fd6b8SFerruh Yigit static const struct rte_security_ops dpaa_sec_security_ops = {
32021f14d500SAkhil Goyal 	.session_create = dpaa_sec_security_session_create,
32031f14d500SAkhil Goyal 	.session_update = NULL,
32041f14d500SAkhil Goyal 	.session_stats_get = NULL,
32051f14d500SAkhil Goyal 	.session_destroy = dpaa_sec_security_session_destroy,
32061f14d500SAkhil Goyal 	.set_pkt_metadata = NULL,
32071f14d500SAkhil Goyal 	.capabilities_get = dpaa_sec_capabilities_get
32081f14d500SAkhil Goyal };
3209314424b6SHemant Agrawal #endif
3210c3e85bdcSAkhil Goyal static int
3211c3e85bdcSAkhil Goyal dpaa_sec_uninit(struct rte_cryptodev *dev)
3212c3e85bdcSAkhil Goyal {
3213debef417SShreyansh Jain 	struct dpaa_sec_dev_private *internals;
3214c3e85bdcSAkhil Goyal 
3215c3e85bdcSAkhil Goyal 	if (dev == NULL)
3216c3e85bdcSAkhil Goyal 		return -ENODEV;
3217c3e85bdcSAkhil Goyal 
3218debef417SShreyansh Jain 	internals = dev->data->dev_private;
32191f14d500SAkhil Goyal 	rte_free(dev->security_ctx);
32201f14d500SAkhil Goyal 
3221c3e85bdcSAkhil Goyal 	rte_free(internals);
3222c3e85bdcSAkhil Goyal 
3223f163231eSHemant Agrawal 	DPAA_SEC_INFO("Closing DPAA_SEC device %s on numa socket %u",
3224c3e85bdcSAkhil Goyal 		      dev->data->name, rte_socket_id());
3225c3e85bdcSAkhil Goyal 
3226c3e85bdcSAkhil Goyal 	return 0;
3227c3e85bdcSAkhil Goyal }
3228c3e85bdcSAkhil Goyal 
3229c3e85bdcSAkhil Goyal static int
3230c3e85bdcSAkhil Goyal dpaa_sec_dev_init(struct rte_cryptodev *cryptodev)
3231c3e85bdcSAkhil Goyal {
3232c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals;
3233314424b6SHemant Agrawal #ifdef RTE_LIBRTE_SECURITY
32341f14d500SAkhil Goyal 	struct rte_security_ctx *security_instance;
3235314424b6SHemant Agrawal #endif
3236c3e85bdcSAkhil Goyal 	struct dpaa_sec_qp *qp;
3237e79416d1SHemant Agrawal 	uint32_t i, flags;
3238c3e85bdcSAkhil Goyal 	int ret;
3239c3e85bdcSAkhil Goyal 
3240c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
3241c3e85bdcSAkhil Goyal 
3242c3e85bdcSAkhil Goyal 	cryptodev->driver_id = cryptodev_driver_id;
3243c3e85bdcSAkhil Goyal 	cryptodev->dev_ops = &crypto_ops;
3244c3e85bdcSAkhil Goyal 
3245c3e85bdcSAkhil Goyal 	cryptodev->enqueue_burst = dpaa_sec_enqueue_burst;
3246c3e85bdcSAkhil Goyal 	cryptodev->dequeue_burst = dpaa_sec_dequeue_burst;
3247c3e85bdcSAkhil Goyal 	cryptodev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
3248c3e85bdcSAkhil Goyal 			RTE_CRYPTODEV_FF_HW_ACCELERATED |
32491f14d500SAkhil Goyal 			RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
3250a74af788SAkhil Goyal 			RTE_CRYPTODEV_FF_SECURITY |
32512717246eSPablo de Lara 			RTE_CRYPTODEV_FF_IN_PLACE_SGL |
32522717246eSPablo de Lara 			RTE_CRYPTODEV_FF_OOP_SGL_IN_SGL_OUT |
32532717246eSPablo de Lara 			RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT |
32542717246eSPablo de Lara 			RTE_CRYPTODEV_FF_OOP_LB_IN_SGL_OUT |
32552717246eSPablo de Lara 			RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT;
3256c3e85bdcSAkhil Goyal 
3257c3e85bdcSAkhil Goyal 	internals = cryptodev->data->dev_private;
3258e79416d1SHemant Agrawal 	internals->max_nb_queue_pairs = RTE_DPAA_MAX_NB_SEC_QPS;
3259c3e85bdcSAkhil Goyal 	internals->max_nb_sessions = RTE_DPAA_SEC_PMD_MAX_NB_SESSIONS;
3260c3e85bdcSAkhil Goyal 
32611f14d500SAkhil Goyal 	/*
32621f14d500SAkhil Goyal 	 * For secondary processes, we don't initialise any further as primary
32631f14d500SAkhil Goyal 	 * has already done this work. Only check we don't need a different
32641f14d500SAkhil Goyal 	 * RX function
32651f14d500SAkhil Goyal 	 */
32661f14d500SAkhil Goyal 	if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
3267f163231eSHemant Agrawal 		DPAA_SEC_WARN("Device already init by primary process");
32681f14d500SAkhil Goyal 		return 0;
32691f14d500SAkhil Goyal 	}
3270314424b6SHemant Agrawal #ifdef RTE_LIBRTE_SECURITY
32711f14d500SAkhil Goyal 	/* Initialize security_ctx only for primary process*/
32721f14d500SAkhil Goyal 	security_instance = rte_malloc("rte_security_instances_ops",
32731f14d500SAkhil Goyal 				sizeof(struct rte_security_ctx), 0);
32741f14d500SAkhil Goyal 	if (security_instance == NULL)
32751f14d500SAkhil Goyal 		return -ENOMEM;
32761f14d500SAkhil Goyal 	security_instance->device = (void *)cryptodev;
32771f14d500SAkhil Goyal 	security_instance->ops = &dpaa_sec_security_ops;
32781f14d500SAkhil Goyal 	security_instance->sess_cnt = 0;
32791f14d500SAkhil Goyal 	cryptodev->security_ctx = security_instance;
3280314424b6SHemant Agrawal #endif
32813b617ee7SAkhil Goyal 	rte_spinlock_init(&internals->lock);
3282c3e85bdcSAkhil Goyal 	for (i = 0; i < internals->max_nb_queue_pairs; i++) {
3283c3e85bdcSAkhil Goyal 		/* init qman fq for queue pair */
3284c3e85bdcSAkhil Goyal 		qp = &internals->qps[i];
3285c3e85bdcSAkhil Goyal 		ret = dpaa_sec_init_tx(&qp->outq);
3286c3e85bdcSAkhil Goyal 		if (ret) {
3287f163231eSHemant Agrawal 			DPAA_SEC_ERR("config tx of queue pair  %d", i);
3288c3e85bdcSAkhil Goyal 			goto init_error;
3289c3e85bdcSAkhil Goyal 		}
3290e79416d1SHemant Agrawal 	}
3291e79416d1SHemant Agrawal 
3292e79416d1SHemant Agrawal 	flags = QMAN_FQ_FLAG_LOCKED | QMAN_FQ_FLAG_DYNAMIC_FQID |
3293e79416d1SHemant Agrawal 		QMAN_FQ_FLAG_TO_DCPORTAL;
32944e694fe5SAkhil Goyal 	for (i = 0; i < MAX_DPAA_CORES * internals->max_nb_sessions; i++) {
3295e79416d1SHemant Agrawal 		/* create rx qman fq for sessions*/
3296e79416d1SHemant Agrawal 		ret = qman_create_fq(0, flags, &internals->inq[i]);
3297e79416d1SHemant Agrawal 		if (unlikely(ret != 0)) {
3298f163231eSHemant Agrawal 			DPAA_SEC_ERR("sec qman_create_fq failed");
3299c3e85bdcSAkhil Goyal 			goto init_error;
3300c3e85bdcSAkhil Goyal 		}
3301c3e85bdcSAkhil Goyal 	}
3302c3e85bdcSAkhil Goyal 
3303f163231eSHemant Agrawal 	RTE_LOG(INFO, PMD, "%s cryptodev init\n", cryptodev->data->name);
3304c3e85bdcSAkhil Goyal 	return 0;
3305c3e85bdcSAkhil Goyal 
3306c3e85bdcSAkhil Goyal init_error:
3307f163231eSHemant Agrawal 	DPAA_SEC_ERR("driver %s: create failed\n", cryptodev->data->name);
3308c3e85bdcSAkhil Goyal 
3309c3e85bdcSAkhil Goyal 	dpaa_sec_uninit(cryptodev);
3310c3e85bdcSAkhil Goyal 	return -EFAULT;
3311c3e85bdcSAkhil Goyal }
3312c3e85bdcSAkhil Goyal 
3313c3e85bdcSAkhil Goyal static int
3314eb5a9a76SShreyansh Jain cryptodev_dpaa_sec_probe(struct rte_dpaa_driver *dpaa_drv __rte_unused,
3315c3e85bdcSAkhil Goyal 				struct rte_dpaa_device *dpaa_dev)
3316c3e85bdcSAkhil Goyal {
3317c3e85bdcSAkhil Goyal 	struct rte_cryptodev *cryptodev;
3318c3e85bdcSAkhil Goyal 	char cryptodev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
3319c3e85bdcSAkhil Goyal 
3320c3e85bdcSAkhil Goyal 	int retval;
3321c3e85bdcSAkhil Goyal 
33220964a951SHemant Agrawal 	snprintf(cryptodev_name, sizeof(cryptodev_name), "%s", dpaa_dev->name);
3323c3e85bdcSAkhil Goyal 
3324c3e85bdcSAkhil Goyal 	cryptodev = rte_cryptodev_pmd_allocate(cryptodev_name, rte_socket_id());
3325c3e85bdcSAkhil Goyal 	if (cryptodev == NULL)
3326c3e85bdcSAkhil Goyal 		return -ENOMEM;
3327c3e85bdcSAkhil Goyal 
3328c3e85bdcSAkhil Goyal 	if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
3329c3e85bdcSAkhil Goyal 		cryptodev->data->dev_private = rte_zmalloc_socket(
3330c3e85bdcSAkhil Goyal 					"cryptodev private structure",
3331c3e85bdcSAkhil Goyal 					sizeof(struct dpaa_sec_dev_private),
3332c3e85bdcSAkhil Goyal 					RTE_CACHE_LINE_SIZE,
3333c3e85bdcSAkhil Goyal 					rte_socket_id());
3334c3e85bdcSAkhil Goyal 
3335c3e85bdcSAkhil Goyal 		if (cryptodev->data->dev_private == NULL)
3336c3e85bdcSAkhil Goyal 			rte_panic("Cannot allocate memzone for private "
3337c3e85bdcSAkhil Goyal 					"device data");
3338c3e85bdcSAkhil Goyal 	}
3339c3e85bdcSAkhil Goyal 
3340c3e85bdcSAkhil Goyal 	dpaa_dev->crypto_dev = cryptodev;
3341c3e85bdcSAkhil Goyal 	cryptodev->device = &dpaa_dev->device;
3342c3e85bdcSAkhil Goyal 
3343c3e85bdcSAkhil Goyal 	/* init user callbacks */
3344c3e85bdcSAkhil Goyal 	TAILQ_INIT(&(cryptodev->link_intr_cbs));
3345c3e85bdcSAkhil Goyal 
3346c3e85bdcSAkhil Goyal 	/* if sec device version is not configured */
3347c3e85bdcSAkhil Goyal 	if (!rta_get_sec_era()) {
3348c3e85bdcSAkhil Goyal 		const struct device_node *caam_node;
3349c3e85bdcSAkhil Goyal 
3350c3e85bdcSAkhil Goyal 		for_each_compatible_node(caam_node, NULL, "fsl,sec-v4.0") {
3351c3e85bdcSAkhil Goyal 			const uint32_t *prop = of_get_property(caam_node,
3352c3e85bdcSAkhil Goyal 					"fsl,sec-era",
3353c3e85bdcSAkhil Goyal 					NULL);
3354c3e85bdcSAkhil Goyal 			if (prop) {
3355c3e85bdcSAkhil Goyal 				rta_set_sec_era(
3356c3e85bdcSAkhil Goyal 					INTL_SEC_ERA(rte_cpu_to_be_32(*prop)));
3357c3e85bdcSAkhil Goyal 				break;
3358c3e85bdcSAkhil Goyal 			}
3359c3e85bdcSAkhil Goyal 		}
3360c3e85bdcSAkhil Goyal 	}
3361c3e85bdcSAkhil Goyal 
3362408077f2SHemant Agrawal 	if (unlikely(!RTE_PER_LCORE(dpaa_io))) {
3363408077f2SHemant Agrawal 		retval = rte_dpaa_portal_init((void *)1);
3364408077f2SHemant Agrawal 		if (retval) {
3365408077f2SHemant Agrawal 			DPAA_SEC_ERR("Unable to initialize portal");
3366408077f2SHemant Agrawal 			return retval;
3367408077f2SHemant Agrawal 		}
3368408077f2SHemant Agrawal 	}
3369408077f2SHemant Agrawal 
3370c3e85bdcSAkhil Goyal 	/* Invoke PMD device initialization function */
3371c3e85bdcSAkhil Goyal 	retval = dpaa_sec_dev_init(cryptodev);
3372c3e85bdcSAkhil Goyal 	if (retval == 0)
3373c3e85bdcSAkhil Goyal 		return 0;
3374c3e85bdcSAkhil Goyal 
3375c3e85bdcSAkhil Goyal 	/* In case of error, cleanup is done */
3376c3e85bdcSAkhil Goyal 	if (rte_eal_process_type() == RTE_PROC_PRIMARY)
3377c3e85bdcSAkhil Goyal 		rte_free(cryptodev->data->dev_private);
3378c3e85bdcSAkhil Goyal 
3379c3e85bdcSAkhil Goyal 	rte_cryptodev_pmd_release_device(cryptodev);
3380c3e85bdcSAkhil Goyal 
3381c3e85bdcSAkhil Goyal 	return -ENXIO;
3382c3e85bdcSAkhil Goyal }
3383c3e85bdcSAkhil Goyal 
3384c3e85bdcSAkhil Goyal static int
3385c3e85bdcSAkhil Goyal cryptodev_dpaa_sec_remove(struct rte_dpaa_device *dpaa_dev)
3386c3e85bdcSAkhil Goyal {
3387c3e85bdcSAkhil Goyal 	struct rte_cryptodev *cryptodev;
3388c3e85bdcSAkhil Goyal 	int ret;
3389c3e85bdcSAkhil Goyal 
3390c3e85bdcSAkhil Goyal 	cryptodev = dpaa_dev->crypto_dev;
3391c3e85bdcSAkhil Goyal 	if (cryptodev == NULL)
3392c3e85bdcSAkhil Goyal 		return -ENODEV;
3393c3e85bdcSAkhil Goyal 
3394c3e85bdcSAkhil Goyal 	ret = dpaa_sec_uninit(cryptodev);
3395c3e85bdcSAkhil Goyal 	if (ret)
3396c3e85bdcSAkhil Goyal 		return ret;
3397c3e85bdcSAkhil Goyal 
3398f2f020d2SDeclan Doherty 	return rte_cryptodev_pmd_destroy(cryptodev);
3399c3e85bdcSAkhil Goyal }
3400c3e85bdcSAkhil Goyal 
3401c3e85bdcSAkhil Goyal static struct rte_dpaa_driver rte_dpaa_sec_driver = {
3402c3e85bdcSAkhil Goyal 	.drv_type = FSL_DPAA_CRYPTO,
3403c3e85bdcSAkhil Goyal 	.driver = {
3404c3e85bdcSAkhil Goyal 		.name = "DPAA SEC PMD"
3405c3e85bdcSAkhil Goyal 	},
3406c3e85bdcSAkhil Goyal 	.probe = cryptodev_dpaa_sec_probe,
3407c3e85bdcSAkhil Goyal 	.remove = cryptodev_dpaa_sec_remove,
3408c3e85bdcSAkhil Goyal };
3409c3e85bdcSAkhil Goyal 
3410c3e85bdcSAkhil Goyal static struct cryptodev_driver dpaa_sec_crypto_drv;
3411c3e85bdcSAkhil Goyal 
3412c3e85bdcSAkhil Goyal RTE_PMD_REGISTER_DPAA(CRYPTODEV_NAME_DPAA_SEC_PMD, rte_dpaa_sec_driver);
3413f737f5ceSFiona Trahe RTE_PMD_REGISTER_CRYPTO_DRIVER(dpaa_sec_crypto_drv, rte_dpaa_sec_driver.driver,
3414c3e85bdcSAkhil Goyal 		cryptodev_driver_id);
3415f163231eSHemant Agrawal 
3416f8e99896SThomas Monjalon RTE_INIT(dpaa_sec_init_log)
3417f163231eSHemant Agrawal {
3418f163231eSHemant Agrawal 	dpaa_logtype_sec = rte_log_register("pmd.crypto.dpaa");
3419f163231eSHemant Agrawal 	if (dpaa_logtype_sec >= 0)
3420f163231eSHemant Agrawal 		rte_log_set_level(dpaa_logtype_sec, RTE_LOG_NOTICE);
3421f163231eSHemant Agrawal }
3422