xref: /dpdk/drivers/crypto/dpaa_sec/dpaa_sec.c (revision 0aa5986c280f34893b2a6bacf044cff31484fe0c)
1d81734caSHemant Agrawal /* SPDX-License-Identifier: BSD-3-Clause
2c3e85bdcSAkhil Goyal  *
3c3e85bdcSAkhil Goyal  *   Copyright (c) 2016 Freescale Semiconductor, Inc. All rights reserved.
4ebc27d1bSFranck Lenormand  *   Copyright 2017-2021 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>
15af668035SAkhil Goyal #include <cryptodev_pmd.h>
16c3e85bdcSAkhil Goyal #include <rte_crypto.h>
17c3e85bdcSAkhil Goyal #include <rte_cryptodev.h>
18a8d0d473SBruce Richardson #ifdef RTE_LIB_SECURITY
191f14d500SAkhil Goyal #include <rte_security_driver.h>
20314424b6SHemant Agrawal #endif
21c3e85bdcSAkhil Goyal #include <rte_cycles.h>
22c3e85bdcSAkhil Goyal #include <rte_dev.h>
23bd063651SFerruh Yigit #include <rte_ip.h>
24c3e85bdcSAkhil Goyal #include <rte_kvargs.h>
25c3e85bdcSAkhil Goyal #include <rte_malloc.h>
26c3e85bdcSAkhil Goyal #include <rte_mbuf.h>
27c3e85bdcSAkhil Goyal #include <rte_memcpy.h>
28c3e85bdcSAkhil Goyal #include <rte_string_fns.h>
293b617ee7SAkhil Goyal #include <rte_spinlock.h>
30c3e85bdcSAkhil Goyal 
31c3e85bdcSAkhil Goyal #include <fsl_usd.h>
32c3e85bdcSAkhil Goyal #include <fsl_qman.h>
338c83f28cSHemant Agrawal #include <dpaa_of.h>
34c3e85bdcSAkhil Goyal 
35c3e85bdcSAkhil Goyal /* RTA header files */
36c0ded849SHemant Agrawal #include <desc/common.h>
37c0ded849SHemant Agrawal #include <desc/algo.h>
38c0ded849SHemant Agrawal #include <desc/ipsec.h>
39c0ded849SHemant Agrawal #include <desc/pdcp.h>
405a4954bcSAkhil Goyal #include <desc/sdap.h>
41c3e85bdcSAkhil Goyal 
42c3e85bdcSAkhil Goyal #include <rte_dpaa_bus.h>
43c3e85bdcSAkhil Goyal #include <dpaa_sec.h>
44fe3688baSAkhil Goyal #include <dpaa_sec_event.h>
45c3e85bdcSAkhil Goyal #include <dpaa_sec_log.h>
4612e58429SThierry Herbelot #include <dpaax_iova_table.h>
47c3e85bdcSAkhil Goyal 
489d5f73c2SGagandeep Singh uint8_t dpaa_cryptodev_driver_id;
49e79416d1SHemant Agrawal 
50c3e85bdcSAkhil Goyal static inline void
51c3e85bdcSAkhil Goyal dpaa_sec_op_ending(struct dpaa_sec_op_ctx *ctx)
52c3e85bdcSAkhil Goyal {
53c3e85bdcSAkhil Goyal 	if (!ctx->fd_status) {
54c3e85bdcSAkhil Goyal 		ctx->op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
55c3e85bdcSAkhil Goyal 	} else {
56f163231eSHemant Agrawal 		DPAA_SEC_DP_WARN("SEC return err: 0x%x", ctx->fd_status);
57c3e85bdcSAkhil Goyal 		ctx->op->status = RTE_CRYPTO_OP_STATUS_ERROR;
58c3e85bdcSAkhil Goyal 	}
59c3e85bdcSAkhil Goyal }
60c3e85bdcSAkhil Goyal 
61c3e85bdcSAkhil Goyal static inline struct dpaa_sec_op_ctx *
62f7a5752eSHemant Agrawal dpaa_sec_alloc_ctx(dpaa_sec_session *ses, int sg_count)
63c3e85bdcSAkhil Goyal {
64c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
65f7a5752eSHemant Agrawal 	int i, retval;
66c3e85bdcSAkhil Goyal 
672ffb940eSAkhil Goyal 	retval = rte_mempool_get(
682ffb940eSAkhil Goyal 			ses->qp[rte_lcore_id() % MAX_DPAA_CORES]->ctx_pool,
692ffb940eSAkhil Goyal 			(void **)(&ctx));
70c3e85bdcSAkhil Goyal 	if (!ctx || retval) {
71f163231eSHemant Agrawal 		DPAA_SEC_DP_WARN("Alloc sec descriptor failed!");
72c3e85bdcSAkhil Goyal 		return NULL;
73c3e85bdcSAkhil Goyal 	}
74c3e85bdcSAkhil Goyal 	/*
75c3e85bdcSAkhil Goyal 	 * Clear SG memory. There are 16 SG entries of 16 Bytes each.
76c3e85bdcSAkhil Goyal 	 * one call to dcbz_64() clear 64 bytes, hence calling it 4 times
77c3e85bdcSAkhil Goyal 	 * to clear all the SG entries. dpaa_sec_alloc_ctx() is called for
78c3e85bdcSAkhil Goyal 	 * each packet, memset is costlier than dcbz_64().
79c3e85bdcSAkhil Goyal 	 */
80f7a5752eSHemant Agrawal 	for (i = 0; i < sg_count && i < MAX_JOB_SG_ENTRIES; i += 4)
81f7a5752eSHemant Agrawal 		dcbz_64(&ctx->job.sg[i]);
82c3e85bdcSAkhil Goyal 
832ffb940eSAkhil Goyal 	ctx->ctx_pool = ses->qp[rte_lcore_id() % MAX_DPAA_CORES]->ctx_pool;
84f7a5752eSHemant Agrawal 	ctx->vtop_offset = (size_t) ctx - rte_mempool_virt2iova(ctx);
85c3e85bdcSAkhil Goyal 
86c3e85bdcSAkhil Goyal 	return ctx;
87c3e85bdcSAkhil Goyal }
88c3e85bdcSAkhil Goyal 
89c3e85bdcSAkhil Goyal static void
90c3e85bdcSAkhil Goyal ern_sec_fq_handler(struct qman_portal *qm __rte_unused,
91c3e85bdcSAkhil Goyal 		   struct qman_fq *fq,
92c3e85bdcSAkhil Goyal 		   const struct qm_mr_entry *msg)
93c3e85bdcSAkhil Goyal {
94f163231eSHemant Agrawal 	DPAA_SEC_DP_ERR("sec fq %d error, RC = %x, seqnum = %x\n",
95c3e85bdcSAkhil Goyal 			fq->fqid, msg->ern.rc, msg->ern.seqnum);
96c3e85bdcSAkhil Goyal }
97c3e85bdcSAkhil Goyal 
98c3e85bdcSAkhil Goyal /* initialize the queue with dest chan as caam chan so that
99c3e85bdcSAkhil Goyal  * all the packets in this queue could be dispatched into caam
100c3e85bdcSAkhil Goyal  */
101c3e85bdcSAkhil Goyal static int
102c4509373SSantosh Shukla dpaa_sec_init_rx(struct qman_fq *fq_in, rte_iova_t hwdesc,
103c3e85bdcSAkhil Goyal 		 uint32_t fqid_out)
104c3e85bdcSAkhil Goyal {
105c3e85bdcSAkhil Goyal 	struct qm_mcc_initfq fq_opts;
106c3e85bdcSAkhil Goyal 	uint32_t flags;
107c3e85bdcSAkhil Goyal 	int ret = -1;
108c3e85bdcSAkhil Goyal 
109c3e85bdcSAkhil Goyal 	/* Clear FQ options */
110c3e85bdcSAkhil Goyal 	memset(&fq_opts, 0x00, sizeof(struct qm_mcc_initfq));
111c3e85bdcSAkhil Goyal 
112c3e85bdcSAkhil Goyal 	flags = QMAN_INITFQ_FLAG_SCHED;
113c3e85bdcSAkhil Goyal 	fq_opts.we_mask = QM_INITFQ_WE_DESTWQ | QM_INITFQ_WE_CONTEXTA |
114c3e85bdcSAkhil Goyal 			  QM_INITFQ_WE_CONTEXTB;
115c3e85bdcSAkhil Goyal 
116c3e85bdcSAkhil Goyal 	qm_fqd_context_a_set64(&fq_opts.fqd, hwdesc);
117c3e85bdcSAkhil Goyal 	fq_opts.fqd.context_b = fqid_out;
118a8ee206aSHemant Agrawal 	fq_opts.fqd.dest.channel = dpaa_get_qm_channel_caam();
119c3e85bdcSAkhil Goyal 	fq_opts.fqd.dest.wq = 0;
120c3e85bdcSAkhil Goyal 
121c3e85bdcSAkhil Goyal 	fq_in->cb.ern  = ern_sec_fq_handler;
122c3e85bdcSAkhil Goyal 
123f163231eSHemant Agrawal 	DPAA_SEC_DEBUG("in-%x out-%x", fq_in->fqid, fqid_out);
124e79416d1SHemant Agrawal 
125c3e85bdcSAkhil Goyal 	ret = qman_init_fq(fq_in, flags, &fq_opts);
126c3e85bdcSAkhil Goyal 	if (unlikely(ret != 0))
127f163231eSHemant Agrawal 		DPAA_SEC_ERR("qman_init_fq failed %d", ret);
128c3e85bdcSAkhil Goyal 
129c3e85bdcSAkhil Goyal 	return ret;
130c3e85bdcSAkhil Goyal }
131c3e85bdcSAkhil Goyal 
132c3e85bdcSAkhil Goyal /* something is put into in_fq and caam put the crypto result into out_fq */
133c3e85bdcSAkhil Goyal static enum qman_cb_dqrr_result
134c3e85bdcSAkhil Goyal dqrr_out_fq_cb_rx(struct qman_portal *qm __always_unused,
135c3e85bdcSAkhil Goyal 		  struct qman_fq *fq __always_unused,
136c3e85bdcSAkhil Goyal 		  const struct qm_dqrr_entry *dqrr)
137c3e85bdcSAkhil Goyal {
138c3e85bdcSAkhil Goyal 	const struct qm_fd *fd;
139c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *job;
140c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
141c3e85bdcSAkhil Goyal 
142e5872221SRohit Raj 	if (DPAA_PER_LCORE_DPAA_SEC_OP_NB >= DPAA_SEC_BURST)
143c3e85bdcSAkhil Goyal 		return qman_cb_dqrr_defer;
144c3e85bdcSAkhil Goyal 
145c3e85bdcSAkhil Goyal 	if (!(dqrr->stat & QM_DQRR_STAT_FD_VALID))
146c3e85bdcSAkhil Goyal 		return qman_cb_dqrr_consume;
147c3e85bdcSAkhil Goyal 
148c3e85bdcSAkhil Goyal 	fd = &dqrr->fd;
149c3e85bdcSAkhil Goyal 	/* sg is embedded in an op ctx,
150c3e85bdcSAkhil Goyal 	 * sg[0] is for output
151c3e85bdcSAkhil Goyal 	 * sg[1] for input
152c3e85bdcSAkhil Goyal 	 */
153ec861560SGagandeep Singh 	job = rte_dpaa_mem_ptov(qm_fd_addr_get64(fd));
1541f14d500SAkhil Goyal 
155c3e85bdcSAkhil Goyal 	ctx = container_of(job, struct dpaa_sec_op_ctx, job);
156c3e85bdcSAkhil Goyal 	ctx->fd_status = fd->status;
1571f14d500SAkhil Goyal 	if (ctx->op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
1581f14d500SAkhil Goyal 		struct qm_sg_entry *sg_out;
1591f14d500SAkhil Goyal 		uint32_t len;
160fb5c100aSAkhil Goyal 		struct rte_mbuf *mbuf = (ctx->op->sym->m_dst == NULL) ?
161fb5c100aSAkhil Goyal 				ctx->op->sym->m_src : ctx->op->sym->m_dst;
1621f14d500SAkhil Goyal 
1631f14d500SAkhil Goyal 		sg_out = &job->sg[0];
1641f14d500SAkhil Goyal 		hw_sg_to_cpu(sg_out);
1651f14d500SAkhil Goyal 		len = sg_out->length;
166fb5c100aSAkhil Goyal 		mbuf->pkt_len = len;
167fb5c100aSAkhil Goyal 		while (mbuf->next != NULL) {
168fb5c100aSAkhil Goyal 			len -= mbuf->data_len;
169fb5c100aSAkhil Goyal 			mbuf = mbuf->next;
170fb5c100aSAkhil Goyal 		}
171fb5c100aSAkhil Goyal 		mbuf->data_len = len;
1721f14d500SAkhil Goyal 	}
173e5872221SRohit Raj 	DPAA_PER_LCORE_RTE_CRYPTO_OP[DPAA_PER_LCORE_DPAA_SEC_OP_NB++] = ctx->op;
174c3e85bdcSAkhil Goyal 	dpaa_sec_op_ending(ctx);
175c3e85bdcSAkhil Goyal 
176c3e85bdcSAkhil Goyal 	return qman_cb_dqrr_consume;
177c3e85bdcSAkhil Goyal }
178c3e85bdcSAkhil Goyal 
179c3e85bdcSAkhil Goyal /* caam result is put into this queue */
180c3e85bdcSAkhil Goyal static int
181c3e85bdcSAkhil Goyal dpaa_sec_init_tx(struct qman_fq *fq)
182c3e85bdcSAkhil Goyal {
183c3e85bdcSAkhil Goyal 	int ret;
184c3e85bdcSAkhil Goyal 	struct qm_mcc_initfq opts;
185c3e85bdcSAkhil Goyal 	uint32_t flags;
186c3e85bdcSAkhil Goyal 
187c3e85bdcSAkhil Goyal 	flags = QMAN_FQ_FLAG_NO_ENQUEUE | QMAN_FQ_FLAG_LOCKED |
188c3e85bdcSAkhil Goyal 		QMAN_FQ_FLAG_DYNAMIC_FQID;
189c3e85bdcSAkhil Goyal 
190c3e85bdcSAkhil Goyal 	ret = qman_create_fq(0, flags, fq);
191c3e85bdcSAkhil Goyal 	if (unlikely(ret)) {
192f163231eSHemant Agrawal 		DPAA_SEC_ERR("qman_create_fq failed");
193c3e85bdcSAkhil Goyal 		return ret;
194c3e85bdcSAkhil Goyal 	}
195c3e85bdcSAkhil Goyal 
196c3e85bdcSAkhil Goyal 	memset(&opts, 0, sizeof(opts));
197c3e85bdcSAkhil Goyal 	opts.we_mask = QM_INITFQ_WE_DESTWQ | QM_INITFQ_WE_FQCTRL |
198c3e85bdcSAkhil Goyal 		       QM_INITFQ_WE_CONTEXTA | QM_INITFQ_WE_CONTEXTB;
199c3e85bdcSAkhil Goyal 
200c3e85bdcSAkhil Goyal 	/* opts.fqd.dest.channel = dpaa_sec_pool_chan; */
201c3e85bdcSAkhil Goyal 
202c3e85bdcSAkhil Goyal 	fq->cb.dqrr = dqrr_out_fq_cb_rx;
203c3e85bdcSAkhil Goyal 	fq->cb.ern  = ern_sec_fq_handler;
204c3e85bdcSAkhil Goyal 
205c3e85bdcSAkhil Goyal 	ret = qman_init_fq(fq, 0, &opts);
206c3e85bdcSAkhil Goyal 	if (unlikely(ret)) {
207f163231eSHemant Agrawal 		DPAA_SEC_ERR("unable to init caam source fq!");
208c3e85bdcSAkhil Goyal 		return ret;
209c3e85bdcSAkhil Goyal 	}
210c3e85bdcSAkhil Goyal 
211c3e85bdcSAkhil Goyal 	return ret;
212c3e85bdcSAkhil Goyal }
213c3e85bdcSAkhil Goyal 
2146290de2cSLukasz Wojciechowski static inline int is_aead(dpaa_sec_session *ses)
2156290de2cSLukasz Wojciechowski {
2166290de2cSLukasz Wojciechowski 	return ((ses->cipher_alg == 0) &&
2176290de2cSLukasz Wojciechowski 		(ses->auth_alg == 0) &&
2186290de2cSLukasz Wojciechowski 		(ses->aead_alg != 0));
2196290de2cSLukasz Wojciechowski }
2206290de2cSLukasz Wojciechowski 
221c3e85bdcSAkhil Goyal static inline int is_encode(dpaa_sec_session *ses)
222c3e85bdcSAkhil Goyal {
223c3e85bdcSAkhil Goyal 	return ses->dir == DIR_ENC;
224c3e85bdcSAkhil Goyal }
225c3e85bdcSAkhil Goyal 
226c3e85bdcSAkhil Goyal static inline int is_decode(dpaa_sec_session *ses)
227c3e85bdcSAkhil Goyal {
228c3e85bdcSAkhil Goyal 	return ses->dir == DIR_DEC;
229c3e85bdcSAkhil Goyal }
230c3e85bdcSAkhil Goyal 
231a8d0d473SBruce Richardson #ifdef RTE_LIB_SECURITY
232a1173d55SHemant Agrawal static int
233a1173d55SHemant Agrawal dpaa_sec_prep_pdcp_cdb(dpaa_sec_session *ses)
234a1173d55SHemant Agrawal {
235a1173d55SHemant Agrawal 	struct alginfo authdata = {0}, cipherdata = {0};
236a1173d55SHemant Agrawal 	struct sec_cdb *cdb = &ses->cdb;
2372e4cbdb4SVakul Garg 	struct alginfo *p_authdata = NULL;
238a1173d55SHemant Agrawal 	int32_t shared_desc_len = 0;
239a1173d55SHemant Agrawal #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
240a1173d55SHemant Agrawal 	int swap = false;
241a1173d55SHemant Agrawal #else
242a1173d55SHemant Agrawal 	int swap = true;
243a1173d55SHemant Agrawal #endif
244a1173d55SHemant Agrawal 
245a1173d55SHemant Agrawal 	cipherdata.key = (size_t)ses->cipher_key.data;
246a1173d55SHemant Agrawal 	cipherdata.keylen = ses->cipher_key.length;
247a1173d55SHemant Agrawal 	cipherdata.key_enc_flags = 0;
248a1173d55SHemant Agrawal 	cipherdata.key_type = RTA_DATA_IMM;
2498524b44eSHemant Agrawal 	cipherdata.algtype = ses->cipher_key.alg;
2508524b44eSHemant Agrawal 	cipherdata.algmode = ses->cipher_key.algmode;
251a1173d55SHemant Agrawal 
2522e4cbdb4SVakul Garg 	if (ses->auth_alg) {
253a1173d55SHemant Agrawal 		authdata.key = (size_t)ses->auth_key.data;
254a1173d55SHemant Agrawal 		authdata.keylen = ses->auth_key.length;
255a1173d55SHemant Agrawal 		authdata.key_enc_flags = 0;
256a1173d55SHemant Agrawal 		authdata.key_type = RTA_DATA_IMM;
2578524b44eSHemant Agrawal 		authdata.algtype = ses->auth_key.alg;
2588524b44eSHemant Agrawal 		authdata.algmode = ses->auth_key.algmode;
259a1173d55SHemant Agrawal 
2602e4cbdb4SVakul Garg 		p_authdata = &authdata;
2612e4cbdb4SVakul Garg 	}
2622e4cbdb4SVakul Garg 
263ebc27d1bSFranck Lenormand 	if (ses->pdcp.sdap_enabled) {
264ebc27d1bSFranck Lenormand 		int nb_keys_to_inline =
265ebc27d1bSFranck Lenormand 				rta_inline_pdcp_sdap_query(authdata.algtype,
266ebc27d1bSFranck Lenormand 					cipherdata.algtype,
267ebc27d1bSFranck Lenormand 					ses->pdcp.sn_size,
268ebc27d1bSFranck Lenormand 					ses->pdcp.hfn_ovd);
269ebc27d1bSFranck Lenormand 		if (nb_keys_to_inline >= 1) {
270ebc27d1bSFranck Lenormand 			cipherdata.key = (size_t)rte_dpaa_mem_vtop((void *)
271ebc27d1bSFranck Lenormand 						(size_t)cipherdata.key);
272ebc27d1bSFranck Lenormand 			cipherdata.key_type = RTA_DATA_PTR;
273ebc27d1bSFranck Lenormand 		}
274ebc27d1bSFranck Lenormand 		if (nb_keys_to_inline >= 2) {
275ebc27d1bSFranck Lenormand 			authdata.key = (size_t)rte_dpaa_mem_vtop((void *)
276ebc27d1bSFranck Lenormand 						(size_t)authdata.key);
277ebc27d1bSFranck Lenormand 			authdata.key_type = RTA_DATA_PTR;
278ebc27d1bSFranck Lenormand 		}
279ebc27d1bSFranck Lenormand 	} else {
280f6ab96f1SAkhil Goyal 		if (rta_inline_pdcp_query(authdata.algtype,
281f6ab96f1SAkhil Goyal 					cipherdata.algtype,
282f6ab96f1SAkhil Goyal 					ses->pdcp.sn_size,
283f6ab96f1SAkhil Goyal 					ses->pdcp.hfn_ovd)) {
284ebc27d1bSFranck Lenormand 			cipherdata.key = (size_t)rte_dpaa_mem_vtop((void *)
285f6ab96f1SAkhil Goyal 						(size_t)cipherdata.key);
286a1173d55SHemant Agrawal 			cipherdata.key_type = RTA_DATA_PTR;
287a1173d55SHemant Agrawal 		}
288ebc27d1bSFranck Lenormand 	}
289a1173d55SHemant Agrawal 
2902e4cbdb4SVakul Garg 	if (ses->pdcp.domain == RTE_SECURITY_PDCP_MODE_CONTROL) {
291a1173d55SHemant Agrawal 		if (ses->dir == DIR_ENC)
292a1173d55SHemant Agrawal 			shared_desc_len = cnstr_shdsc_pdcp_c_plane_encap(
293a1173d55SHemant Agrawal 					cdb->sh_desc, 1, swap,
294a1173d55SHemant Agrawal 					ses->pdcp.hfn,
295eac60082SVakul Garg 					ses->pdcp.sn_size,
296a1173d55SHemant Agrawal 					ses->pdcp.bearer,
297a1173d55SHemant Agrawal 					ses->pdcp.pkt_dir,
298a1173d55SHemant Agrawal 					ses->pdcp.hfn_threshold,
299a1173d55SHemant Agrawal 					&cipherdata, &authdata,
300a1173d55SHemant Agrawal 					0);
301a1173d55SHemant Agrawal 		else if (ses->dir == DIR_DEC)
302a1173d55SHemant Agrawal 			shared_desc_len = cnstr_shdsc_pdcp_c_plane_decap(
303a1173d55SHemant Agrawal 					cdb->sh_desc, 1, swap,
304a1173d55SHemant Agrawal 					ses->pdcp.hfn,
305eac60082SVakul Garg 					ses->pdcp.sn_size,
306a1173d55SHemant Agrawal 					ses->pdcp.bearer,
307a1173d55SHemant Agrawal 					ses->pdcp.pkt_dir,
308a1173d55SHemant Agrawal 					ses->pdcp.hfn_threshold,
309a1173d55SHemant Agrawal 					&cipherdata, &authdata,
310a1173d55SHemant Agrawal 					0);
31193e2661eSGagandeep Singh 	} else if (ses->pdcp.domain == RTE_SECURITY_PDCP_MODE_SHORT_MAC) {
31293e2661eSGagandeep Singh 		shared_desc_len = cnstr_shdsc_pdcp_short_mac(cdb->sh_desc,
31393e2661eSGagandeep Singh 						     1, swap, &authdata);
314a1173d55SHemant Agrawal 	} else {
3155a4954bcSAkhil Goyal 		if (ses->dir == DIR_ENC) {
3165a4954bcSAkhil Goyal 			if (ses->pdcp.sdap_enabled)
3175a4954bcSAkhil Goyal 				shared_desc_len =
3185a4954bcSAkhil Goyal 					cnstr_shdsc_pdcp_sdap_u_plane_encap(
319a1173d55SHemant Agrawal 						cdb->sh_desc, 1, swap,
320a1173d55SHemant Agrawal 						ses->pdcp.sn_size,
321a1173d55SHemant Agrawal 						ses->pdcp.hfn,
322a1173d55SHemant Agrawal 						ses->pdcp.bearer,
323a1173d55SHemant Agrawal 						ses->pdcp.pkt_dir,
324a1173d55SHemant Agrawal 						ses->pdcp.hfn_threshold,
3252e4cbdb4SVakul Garg 						&cipherdata, p_authdata, 0);
3265a4954bcSAkhil Goyal 			else
3275a4954bcSAkhil Goyal 				shared_desc_len =
3285a4954bcSAkhil Goyal 					cnstr_shdsc_pdcp_u_plane_encap(
329a1173d55SHemant Agrawal 						cdb->sh_desc, 1, swap,
330a1173d55SHemant Agrawal 						ses->pdcp.sn_size,
331a1173d55SHemant Agrawal 						ses->pdcp.hfn,
332a1173d55SHemant Agrawal 						ses->pdcp.bearer,
333a1173d55SHemant Agrawal 						ses->pdcp.pkt_dir,
334a1173d55SHemant Agrawal 						ses->pdcp.hfn_threshold,
3352e4cbdb4SVakul Garg 						&cipherdata, p_authdata, 0);
3365a4954bcSAkhil Goyal 		} else if (ses->dir == DIR_DEC) {
3375a4954bcSAkhil Goyal 			if (ses->pdcp.sdap_enabled)
3385a4954bcSAkhil Goyal 				shared_desc_len =
3395a4954bcSAkhil Goyal 					cnstr_shdsc_pdcp_sdap_u_plane_decap(
3405a4954bcSAkhil Goyal 						cdb->sh_desc, 1, swap,
3415a4954bcSAkhil Goyal 						ses->pdcp.sn_size,
3425a4954bcSAkhil Goyal 						ses->pdcp.hfn,
3435a4954bcSAkhil Goyal 						ses->pdcp.bearer,
3445a4954bcSAkhil Goyal 						ses->pdcp.pkt_dir,
3455a4954bcSAkhil Goyal 						ses->pdcp.hfn_threshold,
3465a4954bcSAkhil Goyal 						&cipherdata, p_authdata, 0);
3475a4954bcSAkhil Goyal 			else
3485a4954bcSAkhil Goyal 				shared_desc_len =
3495a4954bcSAkhil Goyal 					cnstr_shdsc_pdcp_u_plane_decap(
3505a4954bcSAkhil Goyal 						cdb->sh_desc, 1, swap,
3515a4954bcSAkhil Goyal 						ses->pdcp.sn_size,
3525a4954bcSAkhil Goyal 						ses->pdcp.hfn,
3535a4954bcSAkhil Goyal 						ses->pdcp.bearer,
3545a4954bcSAkhil Goyal 						ses->pdcp.pkt_dir,
3555a4954bcSAkhil Goyal 						ses->pdcp.hfn_threshold,
3565a4954bcSAkhil Goyal 						&cipherdata, p_authdata, 0);
3575a4954bcSAkhil Goyal 		}
358a1173d55SHemant Agrawal 	}
359a1173d55SHemant Agrawal 	return shared_desc_len;
360a1173d55SHemant Agrawal }
361a1173d55SHemant Agrawal 
36205b12700SHemant Agrawal /* prepare ipsec proto command block of the session */
36305b12700SHemant Agrawal static int
36405b12700SHemant Agrawal dpaa_sec_prep_ipsec_cdb(dpaa_sec_session *ses)
36505b12700SHemant Agrawal {
36605b12700SHemant Agrawal 	struct alginfo cipherdata = {0}, authdata = {0};
36705b12700SHemant Agrawal 	struct sec_cdb *cdb = &ses->cdb;
36805b12700SHemant Agrawal 	int32_t shared_desc_len = 0;
36905b12700SHemant Agrawal 	int err;
37005b12700SHemant Agrawal #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
37105b12700SHemant Agrawal 	int swap = false;
37205b12700SHemant Agrawal #else
37305b12700SHemant Agrawal 	int swap = true;
37405b12700SHemant Agrawal #endif
37505b12700SHemant Agrawal 
37605b12700SHemant Agrawal 	cipherdata.key = (size_t)ses->cipher_key.data;
37705b12700SHemant Agrawal 	cipherdata.keylen = ses->cipher_key.length;
37805b12700SHemant Agrawal 	cipherdata.key_enc_flags = 0;
37905b12700SHemant Agrawal 	cipherdata.key_type = RTA_DATA_IMM;
3808524b44eSHemant Agrawal 	cipherdata.algtype = ses->cipher_key.alg;
3818524b44eSHemant Agrawal 	cipherdata.algmode = ses->cipher_key.algmode;
38205b12700SHemant Agrawal 
3832c318722SHemant Agrawal 	if (ses->auth_key.length) {
38405b12700SHemant Agrawal 		authdata.key = (size_t)ses->auth_key.data;
38505b12700SHemant Agrawal 		authdata.keylen = ses->auth_key.length;
38605b12700SHemant Agrawal 		authdata.key_enc_flags = 0;
38705b12700SHemant Agrawal 		authdata.key_type = RTA_DATA_IMM;
3888524b44eSHemant Agrawal 		authdata.algtype = ses->auth_key.alg;
3898524b44eSHemant Agrawal 		authdata.algmode = ses->auth_key.algmode;
3902c318722SHemant Agrawal 	}
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,
395453b9593SAkhil Goyal 			       DESC_JOB_IO_LEN,
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 {
406ec861560SGagandeep Singh 		cipherdata.key = (size_t)rte_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 {
413ec861560SGagandeep Singh 		authdata.key = (size_t)rte_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) {
455a8d0d473SBruce Richardson #ifdef RTE_LIB_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:
4743e4fbc6cSGagandeep Singh 		case RTE_CRYPTO_CIPHER_DES_CBC:
475c5788a10SHemant Agrawal 		case RTE_CRYPTO_CIPHER_AES_CTR:
476c5788a10SHemant Agrawal 		case RTE_CRYPTO_CIPHER_3DES_CTR:
477c5788a10SHemant Agrawal 			shared_desc_len = cnstr_shdsc_blkcipher(
478c5788a10SHemant Agrawal 					cdb->sh_desc, true,
479c5788a10SHemant Agrawal 					swap, SHR_NEVER, &alginfo_c,
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) {
5094c42352cSGagandeep Singh 		case RTE_CRYPTO_AUTH_MD5:
5104c42352cSGagandeep Singh 		case RTE_CRYPTO_AUTH_SHA1:
5114c42352cSGagandeep Singh 		case RTE_CRYPTO_AUTH_SHA224:
5124c42352cSGagandeep Singh 		case RTE_CRYPTO_AUTH_SHA256:
5134c42352cSGagandeep Singh 		case RTE_CRYPTO_AUTH_SHA384:
5144c42352cSGagandeep Singh 		case RTE_CRYPTO_AUTH_SHA512:
5154c42352cSGagandeep Singh 			shared_desc_len = cnstr_shdsc_hash(
5164c42352cSGagandeep Singh 						cdb->sh_desc, true,
5174c42352cSGagandeep Singh 						swap, SHR_NEVER, &alginfo_a,
5184c42352cSGagandeep Singh 						!ses->dir,
5194c42352cSGagandeep Singh 						ses->digest_length);
5204c42352cSGagandeep Singh 			break;
521c5788a10SHemant Agrawal 		case RTE_CRYPTO_AUTH_MD5_HMAC:
522c5788a10SHemant Agrawal 		case RTE_CRYPTO_AUTH_SHA1_HMAC:
523c5788a10SHemant Agrawal 		case RTE_CRYPTO_AUTH_SHA224_HMAC:
524c5788a10SHemant Agrawal 		case RTE_CRYPTO_AUTH_SHA256_HMAC:
525c5788a10SHemant Agrawal 		case RTE_CRYPTO_AUTH_SHA384_HMAC:
526c5788a10SHemant Agrawal 		case RTE_CRYPTO_AUTH_SHA512_HMAC:
527c5788a10SHemant Agrawal 			shared_desc_len = cnstr_shdsc_hmac(
528c5788a10SHemant Agrawal 						cdb->sh_desc, true,
529c5788a10SHemant Agrawal 						swap, SHR_NEVER, &alginfo_a,
530c5788a10SHemant Agrawal 						!ses->dir,
531c5788a10SHemant Agrawal 						ses->digest_length);
532c5788a10SHemant Agrawal 			break;
533c5788a10SHemant Agrawal 		case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
534c5788a10SHemant Agrawal 			shared_desc_len = cnstr_shdsc_snow_f9(
535c5788a10SHemant Agrawal 						cdb->sh_desc, true, swap,
536c5788a10SHemant Agrawal 						&alginfo_a,
537c5788a10SHemant Agrawal 						!ses->dir,
538c5788a10SHemant Agrawal 						ses->digest_length);
539c5788a10SHemant Agrawal 			break;
540c5788a10SHemant Agrawal 		case RTE_CRYPTO_AUTH_ZUC_EIA3:
541c5788a10SHemant Agrawal 			shared_desc_len = cnstr_shdsc_zuca(
542c5788a10SHemant Agrawal 						cdb->sh_desc, true, swap,
543c5788a10SHemant Agrawal 						&alginfo_a,
544c5788a10SHemant Agrawal 						!ses->dir,
545c5788a10SHemant Agrawal 						ses->digest_length);
546c5788a10SHemant Agrawal 			break;
54766f95673SGagandeep Singh 		case RTE_CRYPTO_AUTH_AES_XCBC_MAC:
5482ed12d9bSGagandeep Singh 		case RTE_CRYPTO_AUTH_AES_CMAC:
54966f95673SGagandeep Singh 			shared_desc_len = cnstr_shdsc_aes_mac(
55066f95673SGagandeep Singh 						cdb->sh_desc,
55166f95673SGagandeep Singh 						true, swap, SHR_NEVER,
55266f95673SGagandeep Singh 						&alginfo_a,
55366f95673SGagandeep Singh 						!ses->dir,
55466f95673SGagandeep Singh 						ses->digest_length);
55566f95673SGagandeep Singh 			break;
556c5788a10SHemant Agrawal 		default:
557c5788a10SHemant Agrawal 			DPAA_SEC_ERR("unsupported auth alg %u", ses->auth_alg);
558c5788a10SHemant Agrawal 		}
5598524b44eSHemant Agrawal 		break;
5608524b44eSHemant Agrawal 	case DPAA_SEC_AEAD:
561c3e85bdcSAkhil Goyal 		if (alginfo.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
562f163231eSHemant Agrawal 			DPAA_SEC_ERR("not supported aead alg");
563c3e85bdcSAkhil Goyal 			return -ENOTSUP;
564c3e85bdcSAkhil Goyal 		}
5650e5607e4SHemant Agrawal 		alginfo.key = (size_t)ses->aead_key.data;
566c3e85bdcSAkhil Goyal 		alginfo.keylen = ses->aead_key.length;
567c3e85bdcSAkhil Goyal 		alginfo.key_enc_flags = 0;
568c3e85bdcSAkhil Goyal 		alginfo.key_type = RTA_DATA_IMM;
5698524b44eSHemant Agrawal 		alginfo.algtype = ses->aead_key.alg;
5708524b44eSHemant Agrawal 		alginfo.algmode = ses->aead_key.algmode;
571c3e85bdcSAkhil Goyal 
572c3e85bdcSAkhil Goyal 		if (ses->dir == DIR_ENC)
573c3e85bdcSAkhil Goyal 			shared_desc_len = cnstr_shdsc_gcm_encap(
5747449390bSAkhil Goyal 					cdb->sh_desc, true, swap, SHR_NEVER,
575c3e85bdcSAkhil Goyal 					&alginfo,
576c3e85bdcSAkhil Goyal 					ses->iv.length,
577c3e85bdcSAkhil Goyal 					ses->digest_length);
578c3e85bdcSAkhil Goyal 		else
579c3e85bdcSAkhil Goyal 			shared_desc_len = cnstr_shdsc_gcm_decap(
5807449390bSAkhil Goyal 					cdb->sh_desc, true, swap, SHR_NEVER,
581c3e85bdcSAkhil Goyal 					&alginfo,
582c3e85bdcSAkhil Goyal 					ses->iv.length,
583c3e85bdcSAkhil Goyal 					ses->digest_length);
5848524b44eSHemant Agrawal 		break;
5858524b44eSHemant Agrawal 	case DPAA_SEC_CIPHER_HASH:
5860e5607e4SHemant Agrawal 		alginfo_c.key = (size_t)ses->cipher_key.data;
587c3e85bdcSAkhil Goyal 		alginfo_c.keylen = ses->cipher_key.length;
588c3e85bdcSAkhil Goyal 		alginfo_c.key_enc_flags = 0;
589c3e85bdcSAkhil Goyal 		alginfo_c.key_type = RTA_DATA_IMM;
5908524b44eSHemant Agrawal 		alginfo_c.algtype = ses->cipher_key.alg;
5918524b44eSHemant Agrawal 		alginfo_c.algmode = ses->cipher_key.algmode;
592c3e85bdcSAkhil Goyal 
5930e5607e4SHemant Agrawal 		alginfo_a.key = (size_t)ses->auth_key.data;
594c3e85bdcSAkhil Goyal 		alginfo_a.keylen = ses->auth_key.length;
595c3e85bdcSAkhil Goyal 		alginfo_a.key_enc_flags = 0;
596c3e85bdcSAkhil Goyal 		alginfo_a.key_type = RTA_DATA_IMM;
5978524b44eSHemant Agrawal 		alginfo_a.algtype = ses->auth_key.alg;
5988524b44eSHemant Agrawal 		alginfo_a.algmode = ses->auth_key.algmode;
599c3e85bdcSAkhil Goyal 
600c3e85bdcSAkhil Goyal 		cdb->sh_desc[0] = alginfo_c.keylen;
601c3e85bdcSAkhil Goyal 		cdb->sh_desc[1] = alginfo_a.keylen;
602c3e85bdcSAkhil Goyal 		err = rta_inline_query(IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN,
603453b9593SAkhil Goyal 				       DESC_JOB_IO_LEN,
604c3e85bdcSAkhil Goyal 				       (unsigned int *)cdb->sh_desc,
605c3e85bdcSAkhil Goyal 				       &cdb->sh_desc[2], 2);
606c3e85bdcSAkhil Goyal 
607c3e85bdcSAkhil Goyal 		if (err < 0) {
608f163231eSHemant Agrawal 			DPAA_SEC_ERR("Crypto: Incorrect key lengths");
609c3e85bdcSAkhil Goyal 			return err;
610c3e85bdcSAkhil Goyal 		}
611c3e85bdcSAkhil Goyal 		if (cdb->sh_desc[2] & 1)
612c3e85bdcSAkhil Goyal 			alginfo_c.key_type = RTA_DATA_IMM;
613c3e85bdcSAkhil Goyal 		else {
614ec861560SGagandeep Singh 			alginfo_c.key = (size_t)rte_dpaa_mem_vtop(
6150e5607e4SHemant Agrawal 						(void *)(size_t)alginfo_c.key);
616c3e85bdcSAkhil Goyal 			alginfo_c.key_type = RTA_DATA_PTR;
617c3e85bdcSAkhil Goyal 		}
618c3e85bdcSAkhil Goyal 		if (cdb->sh_desc[2] & (1<<1))
619c3e85bdcSAkhil Goyal 			alginfo_a.key_type = RTA_DATA_IMM;
620c3e85bdcSAkhil Goyal 		else {
621ec861560SGagandeep Singh 			alginfo_a.key = (size_t)rte_dpaa_mem_vtop(
6220e5607e4SHemant Agrawal 						(void *)(size_t)alginfo_a.key);
623c3e85bdcSAkhil Goyal 			alginfo_a.key_type = RTA_DATA_PTR;
624c3e85bdcSAkhil Goyal 		}
625c3e85bdcSAkhil Goyal 		cdb->sh_desc[0] = 0;
626c3e85bdcSAkhil Goyal 		cdb->sh_desc[1] = 0;
627c3e85bdcSAkhil Goyal 		cdb->sh_desc[2] = 0;
6281f14d500SAkhil Goyal 		/* Auth_only_len is set as 0 here and it will be
6291f14d500SAkhil Goyal 		 * overwritten in fd for each packet.
630c3e85bdcSAkhil Goyal 		 */
631c3e85bdcSAkhil Goyal 		shared_desc_len = cnstr_shdsc_authenc(cdb->sh_desc,
6327449390bSAkhil Goyal 				true, swap, SHR_SERIAL, &alginfo_c, &alginfo_a,
6333394ed47SVakul Garg 				ses->iv.length,
634c3e85bdcSAkhil Goyal 				ses->digest_length, ses->dir);
6358524b44eSHemant Agrawal 		break;
6368524b44eSHemant Agrawal 	case DPAA_SEC_HASH_CIPHER:
6378524b44eSHemant Agrawal 	default:
6388524b44eSHemant Agrawal 		DPAA_SEC_ERR("error: Unsupported session");
6398524b44eSHemant Agrawal 		return -ENOTSUP;
640c3e85bdcSAkhil Goyal 	}
64122788c2cSSunil Kumar Kori 
64222788c2cSSunil Kumar Kori 	if (shared_desc_len < 0) {
643f163231eSHemant Agrawal 		DPAA_SEC_ERR("error in preparing command block");
64422788c2cSSunil Kumar Kori 		return shared_desc_len;
64522788c2cSSunil Kumar Kori 	}
64622788c2cSSunil Kumar Kori 
647c3e85bdcSAkhil Goyal 	cdb->sh_hdr.hi.field.idlen = shared_desc_len;
648c3e85bdcSAkhil Goyal 	cdb->sh_hdr.hi.word = rte_cpu_to_be_32(cdb->sh_hdr.hi.word);
649c3e85bdcSAkhil Goyal 	cdb->sh_hdr.lo.word = rte_cpu_to_be_32(cdb->sh_hdr.lo.word);
650c3e85bdcSAkhil Goyal 
651c3e85bdcSAkhil Goyal 	return 0;
652c3e85bdcSAkhil Goyal }
653c3e85bdcSAkhil Goyal 
654c3e85bdcSAkhil Goyal /* qp is lockless, should be accessed by only one thread */
655c3e85bdcSAkhil Goyal static int
656c3e85bdcSAkhil Goyal dpaa_sec_deq(struct dpaa_sec_qp *qp, struct rte_crypto_op **ops, int nb_ops)
657c3e85bdcSAkhil Goyal {
658c3e85bdcSAkhil Goyal 	struct qman_fq *fq;
6599a984458SAkhil Goyal 	unsigned int pkts = 0;
660f40d5a53SNipun Gupta 	int num_rx_bufs, ret;
6619a984458SAkhil Goyal 	struct qm_dqrr_entry *dq;
662f40d5a53SNipun Gupta 	uint32_t vdqcr_flags = 0;
663c3e85bdcSAkhil Goyal 
664c3e85bdcSAkhil Goyal 	fq = &qp->outq;
665f40d5a53SNipun Gupta 	/*
666f40d5a53SNipun Gupta 	 * Until request for four buffers, we provide exact number of buffers.
667f40d5a53SNipun Gupta 	 * Otherwise we do not set the QM_VDQCR_EXACT flag.
668f40d5a53SNipun Gupta 	 * Not setting QM_VDQCR_EXACT flag can provide two more buffers than
669f40d5a53SNipun Gupta 	 * requested, so we request two less in this case.
670f40d5a53SNipun Gupta 	 */
671f40d5a53SNipun Gupta 	if (nb_ops < 4) {
672f40d5a53SNipun Gupta 		vdqcr_flags = QM_VDQCR_EXACT;
673f40d5a53SNipun Gupta 		num_rx_bufs = nb_ops;
674f40d5a53SNipun Gupta 	} else {
675f40d5a53SNipun Gupta 		num_rx_bufs = nb_ops > DPAA_MAX_DEQUEUE_NUM_FRAMES ?
676f40d5a53SNipun Gupta 			(DPAA_MAX_DEQUEUE_NUM_FRAMES - 2) : (nb_ops - 2);
677f40d5a53SNipun Gupta 	}
678f40d5a53SNipun Gupta 	ret = qman_set_vdq(fq, num_rx_bufs, vdqcr_flags);
6799a984458SAkhil Goyal 	if (ret)
6809a984458SAkhil Goyal 		return 0;
681c3e85bdcSAkhil Goyal 
6829a984458SAkhil Goyal 	do {
6839a984458SAkhil Goyal 		const struct qm_fd *fd;
6849a984458SAkhil Goyal 		struct dpaa_sec_job *job;
6859a984458SAkhil Goyal 		struct dpaa_sec_op_ctx *ctx;
6869a984458SAkhil Goyal 		struct rte_crypto_op *op;
687c3e85bdcSAkhil Goyal 
6889a984458SAkhil Goyal 		dq = qman_dequeue(fq);
6899a984458SAkhil Goyal 		if (!dq)
6909a984458SAkhil Goyal 			continue;
6919a984458SAkhil Goyal 
6929a984458SAkhil Goyal 		fd = &dq->fd;
6939a984458SAkhil Goyal 		/* sg is embedded in an op ctx,
6949a984458SAkhil Goyal 		 * sg[0] is for output
6959a984458SAkhil Goyal 		 * sg[1] for input
6969a984458SAkhil Goyal 		 */
697ec861560SGagandeep Singh 		job = rte_dpaa_mem_ptov(qm_fd_addr_get64(fd));
6989a984458SAkhil Goyal 
6999a984458SAkhil Goyal 		ctx = container_of(job, struct dpaa_sec_op_ctx, job);
7009a984458SAkhil Goyal 		ctx->fd_status = fd->status;
7019a984458SAkhil Goyal 		op = ctx->op;
7029a984458SAkhil Goyal 		if (op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
7039a984458SAkhil Goyal 			struct qm_sg_entry *sg_out;
7049a984458SAkhil Goyal 			uint32_t len;
705fb5c100aSAkhil Goyal 			struct rte_mbuf *mbuf = (op->sym->m_dst == NULL) ?
706fb5c100aSAkhil Goyal 						op->sym->m_src : op->sym->m_dst;
7079a984458SAkhil Goyal 
7089a984458SAkhil Goyal 			sg_out = &job->sg[0];
7099a984458SAkhil Goyal 			hw_sg_to_cpu(sg_out);
7109a984458SAkhil Goyal 			len = sg_out->length;
711fb5c100aSAkhil Goyal 			mbuf->pkt_len = len;
712fb5c100aSAkhil Goyal 			while (mbuf->next != NULL) {
713fb5c100aSAkhil Goyal 				len -= mbuf->data_len;
714fb5c100aSAkhil Goyal 				mbuf = mbuf->next;
715fb5c100aSAkhil Goyal 			}
716fb5c100aSAkhil Goyal 			mbuf->data_len = len;
7179a984458SAkhil Goyal 		}
7189a984458SAkhil Goyal 		if (!ctx->fd_status) {
7199a984458SAkhil Goyal 			op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
7209a984458SAkhil Goyal 		} else {
721f163231eSHemant Agrawal 			DPAA_SEC_DP_WARN("SEC return err:0x%x", ctx->fd_status);
7229a984458SAkhil Goyal 			op->status = RTE_CRYPTO_OP_STATUS_ERROR;
7239a984458SAkhil Goyal 		}
7249a984458SAkhil Goyal 		ops[pkts++] = op;
7259a984458SAkhil Goyal 
7269a984458SAkhil Goyal 		/* report op status to sym->op and then free the ctx memeory */
7279a984458SAkhil Goyal 		rte_mempool_put(ctx->ctx_pool, (void *)ctx);
7289a984458SAkhil Goyal 
7299a984458SAkhil Goyal 		qman_dqrr_consume(fq, dq);
7309a984458SAkhil Goyal 	} while (fq->flags & QMAN_FQ_STATE_VDQCR);
7319a984458SAkhil Goyal 
7329a984458SAkhil Goyal 	return pkts;
733c3e85bdcSAkhil Goyal }
734c3e85bdcSAkhil Goyal 
735a74af788SAkhil Goyal static inline struct dpaa_sec_job *
736a74af788SAkhil Goyal build_auth_only_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
737a74af788SAkhil Goyal {
738a74af788SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
739a74af788SAkhil Goyal 	struct rte_mbuf *mbuf = sym->m_src;
740a74af788SAkhil Goyal 	struct dpaa_sec_job *cf;
741a74af788SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
742a74af788SAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
743a74af788SAkhil Goyal 	phys_addr_t start_addr;
744a74af788SAkhil Goyal 	uint8_t *old_digest, extra_segs;
745c5788a10SHemant Agrawal 	int data_len, data_offset;
746c5788a10SHemant Agrawal 
747c5788a10SHemant Agrawal 	data_len = sym->auth.data.length;
748c5788a10SHemant Agrawal 	data_offset = sym->auth.data.offset;
749c5788a10SHemant Agrawal 
750c5788a10SHemant Agrawal 	if (ses->auth_alg == RTE_CRYPTO_AUTH_SNOW3G_UIA2 ||
751c5788a10SHemant Agrawal 	    ses->auth_alg == RTE_CRYPTO_AUTH_ZUC_EIA3) {
752c5788a10SHemant Agrawal 		if ((data_len & 7) || (data_offset & 7)) {
753c5788a10SHemant Agrawal 			DPAA_SEC_ERR("AUTH: len/offset must be full bytes");
754c5788a10SHemant Agrawal 			return NULL;
755c5788a10SHemant Agrawal 		}
756c5788a10SHemant Agrawal 
757c5788a10SHemant Agrawal 		data_len = data_len >> 3;
758c5788a10SHemant Agrawal 		data_offset = data_offset >> 3;
759c5788a10SHemant Agrawal 	}
760a74af788SAkhil Goyal 
761a74af788SAkhil Goyal 	if (is_decode(ses))
762a74af788SAkhil Goyal 		extra_segs = 3;
763a74af788SAkhil Goyal 	else
764a74af788SAkhil Goyal 		extra_segs = 2;
765a74af788SAkhil Goyal 
766f7a5752eSHemant Agrawal 	if (mbuf->nb_segs > MAX_SG_ENTRIES) {
767f163231eSHemant Agrawal 		DPAA_SEC_DP_ERR("Auth: Max sec segs supported is %d",
768a74af788SAkhil Goyal 				MAX_SG_ENTRIES);
769a74af788SAkhil Goyal 		return NULL;
770a74af788SAkhil Goyal 	}
771f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, mbuf->nb_segs + extra_segs);
772a74af788SAkhil Goyal 	if (!ctx)
773a74af788SAkhil Goyal 		return NULL;
774a74af788SAkhil Goyal 
775a74af788SAkhil Goyal 	cf = &ctx->job;
776a74af788SAkhil Goyal 	ctx->op = op;
777a74af788SAkhil Goyal 	old_digest = ctx->digest;
778a74af788SAkhil Goyal 
779a74af788SAkhil Goyal 	/* output */
780a74af788SAkhil Goyal 	out_sg = &cf->sg[0];
781a74af788SAkhil Goyal 	qm_sg_entry_set64(out_sg, sym->auth.digest.phys_addr);
782a74af788SAkhil Goyal 	out_sg->length = ses->digest_length;
783a74af788SAkhil Goyal 	cpu_to_hw_sg(out_sg);
784a74af788SAkhil Goyal 
785a74af788SAkhil Goyal 	/* input */
786a74af788SAkhil Goyal 	in_sg = &cf->sg[1];
787a74af788SAkhil Goyal 	/* need to extend the input to a compound frame */
788a74af788SAkhil Goyal 	in_sg->extension = 1;
789a74af788SAkhil Goyal 	in_sg->final = 1;
790c5788a10SHemant Agrawal 	in_sg->length = data_len;
791ec861560SGagandeep Singh 	qm_sg_entry_set64(in_sg, rte_dpaa_mem_vtop(&cf->sg[2]));
792a74af788SAkhil Goyal 
793a74af788SAkhil Goyal 	/* 1st seg */
794a74af788SAkhil Goyal 	sg = in_sg + 1;
795a74af788SAkhil Goyal 
796c5788a10SHemant Agrawal 	if (ses->iv.length) {
797c5788a10SHemant Agrawal 		uint8_t *iv_ptr;
798c5788a10SHemant Agrawal 
799c5788a10SHemant Agrawal 		iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
800c5788a10SHemant Agrawal 						   ses->iv.offset);
801c5788a10SHemant Agrawal 
802c5788a10SHemant Agrawal 		if (ses->auth_alg == RTE_CRYPTO_AUTH_SNOW3G_UIA2) {
803c5788a10SHemant Agrawal 			iv_ptr = conv_to_snow_f9_iv(iv_ptr);
804c5788a10SHemant Agrawal 			sg->length = 12;
805c5788a10SHemant Agrawal 		} else if (ses->auth_alg == RTE_CRYPTO_AUTH_ZUC_EIA3) {
806c5788a10SHemant Agrawal 			iv_ptr = conv_to_zuc_eia_iv(iv_ptr);
807c5788a10SHemant Agrawal 			sg->length = 8;
808c5788a10SHemant Agrawal 		} else {
809c5788a10SHemant Agrawal 			sg->length = ses->iv.length;
810c5788a10SHemant Agrawal 		}
811ec861560SGagandeep Singh 		qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(iv_ptr));
812c5788a10SHemant Agrawal 		in_sg->length += sg->length;
813c5788a10SHemant Agrawal 		cpu_to_hw_sg(sg);
814c5788a10SHemant Agrawal 		sg++;
815c5788a10SHemant Agrawal 	}
816c5788a10SHemant Agrawal 
817ce627d63SThomas Monjalon 	qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
818c5788a10SHemant Agrawal 	sg->offset = data_offset;
819c5788a10SHemant Agrawal 
820c5788a10SHemant Agrawal 	if (data_len <= (mbuf->data_len - data_offset)) {
821c5788a10SHemant Agrawal 		sg->length = data_len;
822c5788a10SHemant Agrawal 	} else {
823c5788a10SHemant Agrawal 		sg->length = mbuf->data_len - data_offset;
824c5788a10SHemant Agrawal 
825c5788a10SHemant Agrawal 		/* remaining i/p segs */
826c5788a10SHemant Agrawal 		while ((data_len = data_len - sg->length) &&
827c5788a10SHemant Agrawal 		       (mbuf = mbuf->next)) {
828a74af788SAkhil Goyal 			cpu_to_hw_sg(sg);
829a74af788SAkhil Goyal 			sg++;
830ce627d63SThomas Monjalon 			qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
831c5788a10SHemant Agrawal 			if (data_len > mbuf->data_len)
832a74af788SAkhil Goyal 				sg->length = mbuf->data_len;
833c5788a10SHemant Agrawal 			else
834c5788a10SHemant Agrawal 				sg->length = data_len;
835c5788a10SHemant Agrawal 		}
836a74af788SAkhil Goyal 	}
837a74af788SAkhil Goyal 
838a74af788SAkhil Goyal 	if (is_decode(ses)) {
839a74af788SAkhil Goyal 		/* Digest verification case */
840a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
841a74af788SAkhil Goyal 		sg++;
842a74af788SAkhil Goyal 		rte_memcpy(old_digest, sym->auth.digest.data,
843a74af788SAkhil Goyal 				ses->digest_length);
844ec861560SGagandeep Singh 		start_addr = rte_dpaa_mem_vtop(old_digest);
845a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, start_addr);
846a74af788SAkhil Goyal 		sg->length = ses->digest_length;
847a74af788SAkhil Goyal 		in_sg->length += ses->digest_length;
848a74af788SAkhil Goyal 	}
849a74af788SAkhil Goyal 	sg->final = 1;
850a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
851a74af788SAkhil Goyal 	cpu_to_hw_sg(in_sg);
852a74af788SAkhil Goyal 
853a74af788SAkhil Goyal 	return cf;
854a74af788SAkhil Goyal }
855a74af788SAkhil Goyal 
856c3e85bdcSAkhil Goyal /**
857c3e85bdcSAkhil Goyal  * packet looks like:
858c3e85bdcSAkhil Goyal  *		|<----data_len------->|
859c3e85bdcSAkhil Goyal  *    |ip_header|ah_header|icv|payload|
860c3e85bdcSAkhil Goyal  *              ^
861c3e85bdcSAkhil Goyal  *		|
862c3e85bdcSAkhil Goyal  *	   mbuf->pkt.data
863c3e85bdcSAkhil Goyal  */
864c3e85bdcSAkhil Goyal static inline struct dpaa_sec_job *
865c3e85bdcSAkhil Goyal build_auth_only(struct rte_crypto_op *op, dpaa_sec_session *ses)
866c3e85bdcSAkhil Goyal {
867c3e85bdcSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
868c3e85bdcSAkhil Goyal 	struct rte_mbuf *mbuf = sym->m_src;
869c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
870c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
871c5788a10SHemant Agrawal 	struct qm_sg_entry *sg, *in_sg;
872c4509373SSantosh Shukla 	rte_iova_t start_addr;
873c3e85bdcSAkhil Goyal 	uint8_t *old_digest;
874c5788a10SHemant Agrawal 	int data_len, data_offset;
875c5788a10SHemant Agrawal 
876c5788a10SHemant Agrawal 	data_len = sym->auth.data.length;
877c5788a10SHemant Agrawal 	data_offset = sym->auth.data.offset;
878c5788a10SHemant Agrawal 
879c5788a10SHemant Agrawal 	if (ses->auth_alg == RTE_CRYPTO_AUTH_SNOW3G_UIA2 ||
880c5788a10SHemant Agrawal 	    ses->auth_alg == RTE_CRYPTO_AUTH_ZUC_EIA3) {
881c5788a10SHemant Agrawal 		if ((data_len & 7) || (data_offset & 7)) {
882c5788a10SHemant Agrawal 			DPAA_SEC_ERR("AUTH: len/offset must be full bytes");
883c5788a10SHemant Agrawal 			return NULL;
884c5788a10SHemant Agrawal 		}
885c5788a10SHemant Agrawal 
886c5788a10SHemant Agrawal 		data_len = data_len >> 3;
887c5788a10SHemant Agrawal 		data_offset = data_offset >> 3;
888c5788a10SHemant Agrawal 	}
889c3e85bdcSAkhil Goyal 
890f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, 4);
891c3e85bdcSAkhil Goyal 	if (!ctx)
892c3e85bdcSAkhil Goyal 		return NULL;
893c3e85bdcSAkhil Goyal 
894c3e85bdcSAkhil Goyal 	cf = &ctx->job;
895c3e85bdcSAkhil Goyal 	ctx->op = op;
896c3e85bdcSAkhil Goyal 	old_digest = ctx->digest;
897c3e85bdcSAkhil Goyal 
898bfa9a8a4SThomas Monjalon 	start_addr = rte_pktmbuf_iova(mbuf);
899c3e85bdcSAkhil Goyal 	/* output */
900c3e85bdcSAkhil Goyal 	sg = &cf->sg[0];
901c3e85bdcSAkhil Goyal 	qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
902c3e85bdcSAkhil Goyal 	sg->length = ses->digest_length;
903c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
904c3e85bdcSAkhil Goyal 
905c3e85bdcSAkhil Goyal 	/* input */
906c5788a10SHemant Agrawal 	in_sg = &cf->sg[1];
907c3e85bdcSAkhil Goyal 	/* need to extend the input to a compound frame */
908c5788a10SHemant Agrawal 	in_sg->extension = 1;
909c5788a10SHemant Agrawal 	in_sg->final = 1;
910c5788a10SHemant Agrawal 	in_sg->length = data_len;
911ec861560SGagandeep Singh 	qm_sg_entry_set64(in_sg, rte_dpaa_mem_vtop(&cf->sg[2]));
912c3e85bdcSAkhil Goyal 	sg = &cf->sg[2];
913c5788a10SHemant Agrawal 
914c5788a10SHemant Agrawal 	if (ses->iv.length) {
915c5788a10SHemant Agrawal 		uint8_t *iv_ptr;
916c5788a10SHemant Agrawal 
917c5788a10SHemant Agrawal 		iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
918c5788a10SHemant Agrawal 						   ses->iv.offset);
919c5788a10SHemant Agrawal 
920c5788a10SHemant Agrawal 		if (ses->auth_alg == RTE_CRYPTO_AUTH_SNOW3G_UIA2) {
921c5788a10SHemant Agrawal 			iv_ptr = conv_to_snow_f9_iv(iv_ptr);
922c5788a10SHemant Agrawal 			sg->length = 12;
923c5788a10SHemant Agrawal 		} else if (ses->auth_alg == RTE_CRYPTO_AUTH_ZUC_EIA3) {
924c5788a10SHemant Agrawal 			iv_ptr = conv_to_zuc_eia_iv(iv_ptr);
925c5788a10SHemant Agrawal 			sg->length = 8;
926c5788a10SHemant Agrawal 		} else {
927c5788a10SHemant Agrawal 			sg->length = ses->iv.length;
928c5788a10SHemant Agrawal 		}
929ec861560SGagandeep Singh 		qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(iv_ptr));
930c5788a10SHemant Agrawal 		in_sg->length += sg->length;
931c5788a10SHemant Agrawal 		cpu_to_hw_sg(sg);
932c5788a10SHemant Agrawal 		sg++;
933c5788a10SHemant Agrawal 	}
934c5788a10SHemant Agrawal 
935ce627d63SThomas Monjalon 	qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
936c5788a10SHemant Agrawal 	sg->offset = data_offset;
937c5788a10SHemant Agrawal 	sg->length = data_len;
938c5788a10SHemant Agrawal 
939c5788a10SHemant Agrawal 	if (is_decode(ses)) {
940c5788a10SHemant Agrawal 		/* Digest verification case */
941c5788a10SHemant Agrawal 		cpu_to_hw_sg(sg);
942c3e85bdcSAkhil Goyal 		/* hash result or digest, save digest first */
943c3e85bdcSAkhil Goyal 		rte_memcpy(old_digest, sym->auth.digest.data,
944c3e85bdcSAkhil Goyal 				ses->digest_length);
945c3e85bdcSAkhil Goyal 		/* let's check digest by hw */
946ec861560SGagandeep Singh 		start_addr = rte_dpaa_mem_vtop(old_digest);
947c3e85bdcSAkhil Goyal 		sg++;
948c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, start_addr);
949c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
950c5788a10SHemant Agrawal 		in_sg->length += ses->digest_length;
951c3e85bdcSAkhil Goyal 	}
952c5788a10SHemant Agrawal 	sg->final = 1;
953c5788a10SHemant Agrawal 	cpu_to_hw_sg(sg);
954c5788a10SHemant Agrawal 	cpu_to_hw_sg(in_sg);
955c3e85bdcSAkhil Goyal 
956c3e85bdcSAkhil Goyal 	return cf;
957c3e85bdcSAkhil Goyal }
958c3e85bdcSAkhil Goyal 
959c3e85bdcSAkhil Goyal static inline struct dpaa_sec_job *
960a74af788SAkhil Goyal build_cipher_only_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
961a74af788SAkhil Goyal {
962a74af788SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
963a74af788SAkhil Goyal 	struct dpaa_sec_job *cf;
964a74af788SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
965a74af788SAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
966a74af788SAkhil Goyal 	struct rte_mbuf *mbuf;
967a74af788SAkhil Goyal 	uint8_t req_segs;
968a74af788SAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
969a74af788SAkhil Goyal 			ses->iv.offset);
970c5788a10SHemant Agrawal 	int data_len, data_offset;
971c5788a10SHemant Agrawal 
972c5788a10SHemant Agrawal 	data_len = sym->cipher.data.length;
973c5788a10SHemant Agrawal 	data_offset = sym->cipher.data.offset;
974c5788a10SHemant Agrawal 
975c5788a10SHemant Agrawal 	if (ses->cipher_alg == RTE_CRYPTO_CIPHER_SNOW3G_UEA2 ||
976c5788a10SHemant Agrawal 		ses->cipher_alg == RTE_CRYPTO_CIPHER_ZUC_EEA3) {
977c5788a10SHemant Agrawal 		if ((data_len & 7) || (data_offset & 7)) {
978c5788a10SHemant Agrawal 			DPAA_SEC_ERR("CIPHER: len/offset must be full bytes");
979c5788a10SHemant Agrawal 			return NULL;
980c5788a10SHemant Agrawal 		}
981c5788a10SHemant Agrawal 
982c5788a10SHemant Agrawal 		data_len = data_len >> 3;
983c5788a10SHemant Agrawal 		data_offset = data_offset >> 3;
984c5788a10SHemant Agrawal 	}
985a74af788SAkhil Goyal 
986a74af788SAkhil Goyal 	if (sym->m_dst) {
987a74af788SAkhil Goyal 		mbuf = sym->m_dst;
988a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 3;
989a74af788SAkhil Goyal 	} else {
990a74af788SAkhil Goyal 		mbuf = sym->m_src;
991a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs * 2 + 3;
992a74af788SAkhil Goyal 	}
993f7a5752eSHemant Agrawal 	if (mbuf->nb_segs > MAX_SG_ENTRIES) {
994f163231eSHemant Agrawal 		DPAA_SEC_DP_ERR("Cipher: Max sec segs supported is %d",
995a74af788SAkhil Goyal 				MAX_SG_ENTRIES);
996a74af788SAkhil Goyal 		return NULL;
997a74af788SAkhil Goyal 	}
998a74af788SAkhil Goyal 
999f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, req_segs);
1000a74af788SAkhil Goyal 	if (!ctx)
1001a74af788SAkhil Goyal 		return NULL;
1002a74af788SAkhil Goyal 
1003a74af788SAkhil Goyal 	cf = &ctx->job;
1004a74af788SAkhil Goyal 	ctx->op = op;
1005a74af788SAkhil Goyal 
1006a74af788SAkhil Goyal 	/* output */
1007a74af788SAkhil Goyal 	out_sg = &cf->sg[0];
1008a74af788SAkhil Goyal 	out_sg->extension = 1;
1009c5788a10SHemant Agrawal 	out_sg->length = data_len;
1010ec861560SGagandeep Singh 	qm_sg_entry_set64(out_sg, rte_dpaa_mem_vtop(&cf->sg[2]));
1011a74af788SAkhil Goyal 	cpu_to_hw_sg(out_sg);
1012a74af788SAkhil Goyal 
1013a74af788SAkhil Goyal 	/* 1st seg */
1014a74af788SAkhil Goyal 	sg = &cf->sg[2];
1015ce627d63SThomas Monjalon 	qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1016c5788a10SHemant Agrawal 	sg->length = mbuf->data_len - data_offset;
1017c5788a10SHemant Agrawal 	sg->offset = data_offset;
1018a74af788SAkhil Goyal 
1019a74af788SAkhil Goyal 	/* Successive segs */
1020a74af788SAkhil Goyal 	mbuf = mbuf->next;
1021a74af788SAkhil Goyal 	while (mbuf) {
1022a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1023a74af788SAkhil Goyal 		sg++;
1024ce627d63SThomas Monjalon 		qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1025a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1026a74af788SAkhil Goyal 		mbuf = mbuf->next;
1027a74af788SAkhil Goyal 	}
1028a74af788SAkhil Goyal 	sg->final = 1;
1029a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1030a74af788SAkhil Goyal 
1031a74af788SAkhil Goyal 	/* input */
1032a74af788SAkhil Goyal 	mbuf = sym->m_src;
1033a74af788SAkhil Goyal 	in_sg = &cf->sg[1];
1034a74af788SAkhil Goyal 	in_sg->extension = 1;
1035a74af788SAkhil Goyal 	in_sg->final = 1;
1036c5788a10SHemant Agrawal 	in_sg->length = data_len + ses->iv.length;
1037a74af788SAkhil Goyal 
1038a74af788SAkhil Goyal 	sg++;
1039ec861560SGagandeep Singh 	qm_sg_entry_set64(in_sg, rte_dpaa_mem_vtop(sg));
1040a74af788SAkhil Goyal 	cpu_to_hw_sg(in_sg);
1041a74af788SAkhil Goyal 
1042a74af788SAkhil Goyal 	/* IV */
1043ec861560SGagandeep Singh 	qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(IV_ptr));
1044a74af788SAkhil Goyal 	sg->length = ses->iv.length;
1045a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1046a74af788SAkhil Goyal 
1047a74af788SAkhil Goyal 	/* 1st seg */
1048a74af788SAkhil Goyal 	sg++;
1049ce627d63SThomas Monjalon 	qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1050c5788a10SHemant Agrawal 	sg->length = mbuf->data_len - data_offset;
1051c5788a10SHemant Agrawal 	sg->offset = data_offset;
1052a74af788SAkhil Goyal 
1053a74af788SAkhil Goyal 	/* Successive segs */
1054a74af788SAkhil Goyal 	mbuf = mbuf->next;
1055a74af788SAkhil Goyal 	while (mbuf) {
1056a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1057a74af788SAkhil Goyal 		sg++;
1058ce627d63SThomas Monjalon 		qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1059a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1060a74af788SAkhil Goyal 		mbuf = mbuf->next;
1061a74af788SAkhil Goyal 	}
1062a74af788SAkhil Goyal 	sg->final = 1;
1063a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1064a74af788SAkhil Goyal 
1065a74af788SAkhil Goyal 	return cf;
1066a74af788SAkhil Goyal }
1067a74af788SAkhil Goyal 
1068a74af788SAkhil Goyal static inline struct dpaa_sec_job *
1069c3e85bdcSAkhil Goyal build_cipher_only(struct rte_crypto_op *op, dpaa_sec_session *ses)
1070c3e85bdcSAkhil Goyal {
1071c3e85bdcSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1072c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
1073c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1074c3e85bdcSAkhil Goyal 	struct qm_sg_entry *sg;
1075c4509373SSantosh Shukla 	rte_iova_t src_start_addr, dst_start_addr;
1076c3e85bdcSAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1077c3e85bdcSAkhil Goyal 			ses->iv.offset);
1078c5788a10SHemant Agrawal 	int data_len, data_offset;
1079c5788a10SHemant Agrawal 
1080c5788a10SHemant Agrawal 	data_len = sym->cipher.data.length;
1081c5788a10SHemant Agrawal 	data_offset = sym->cipher.data.offset;
1082c5788a10SHemant Agrawal 
1083c5788a10SHemant Agrawal 	if (ses->cipher_alg == RTE_CRYPTO_CIPHER_SNOW3G_UEA2 ||
1084c5788a10SHemant Agrawal 		ses->cipher_alg == RTE_CRYPTO_CIPHER_ZUC_EEA3) {
1085c5788a10SHemant Agrawal 		if ((data_len & 7) || (data_offset & 7)) {
1086c5788a10SHemant Agrawal 			DPAA_SEC_ERR("CIPHER: len/offset must be full bytes");
1087c5788a10SHemant Agrawal 			return NULL;
1088c5788a10SHemant Agrawal 		}
1089c5788a10SHemant Agrawal 
1090c5788a10SHemant Agrawal 		data_len = data_len >> 3;
1091c5788a10SHemant Agrawal 		data_offset = data_offset >> 3;
1092c5788a10SHemant Agrawal 	}
1093c3e85bdcSAkhil Goyal 
1094f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, 4);
1095c3e85bdcSAkhil Goyal 	if (!ctx)
1096c3e85bdcSAkhil Goyal 		return NULL;
1097c3e85bdcSAkhil Goyal 
1098c3e85bdcSAkhil Goyal 	cf = &ctx->job;
1099c3e85bdcSAkhil Goyal 	ctx->op = op;
1100a389434eSAlok Makhariya 
1101bfa9a8a4SThomas Monjalon 	src_start_addr = rte_pktmbuf_iova(sym->m_src);
1102a389434eSAlok Makhariya 
1103a389434eSAlok Makhariya 	if (sym->m_dst)
1104bfa9a8a4SThomas Monjalon 		dst_start_addr = rte_pktmbuf_iova(sym->m_dst);
1105a389434eSAlok Makhariya 	else
1106a389434eSAlok Makhariya 		dst_start_addr = src_start_addr;
1107c3e85bdcSAkhil Goyal 
1108c3e85bdcSAkhil Goyal 	/* output */
1109c3e85bdcSAkhil Goyal 	sg = &cf->sg[0];
1110c5788a10SHemant Agrawal 	qm_sg_entry_set64(sg, dst_start_addr + data_offset);
1111c5788a10SHemant Agrawal 	sg->length = data_len + ses->iv.length;
1112c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1113c3e85bdcSAkhil Goyal 
1114c3e85bdcSAkhil Goyal 	/* input */
1115c3e85bdcSAkhil Goyal 	sg = &cf->sg[1];
1116c3e85bdcSAkhil Goyal 
1117c3e85bdcSAkhil Goyal 	/* need to extend the input to a compound frame */
1118c3e85bdcSAkhil Goyal 	sg->extension = 1;
1119c3e85bdcSAkhil Goyal 	sg->final = 1;
1120c5788a10SHemant Agrawal 	sg->length = data_len + ses->iv.length;
1121ec861560SGagandeep Singh 	qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(&cf->sg[2]));
1122c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1123c3e85bdcSAkhil Goyal 
1124c3e85bdcSAkhil Goyal 	sg = &cf->sg[2];
1125ec861560SGagandeep Singh 	qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(IV_ptr));
1126c3e85bdcSAkhil Goyal 	sg->length = ses->iv.length;
1127c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1128c3e85bdcSAkhil Goyal 
1129c3e85bdcSAkhil Goyal 	sg++;
1130c5788a10SHemant Agrawal 	qm_sg_entry_set64(sg, src_start_addr + data_offset);
1131c5788a10SHemant Agrawal 	sg->length = data_len;
1132c3e85bdcSAkhil Goyal 	sg->final = 1;
1133c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1134c3e85bdcSAkhil Goyal 
1135c3e85bdcSAkhil Goyal 	return cf;
1136c3e85bdcSAkhil Goyal }
1137c3e85bdcSAkhil Goyal 
1138c3e85bdcSAkhil Goyal static inline struct dpaa_sec_job *
1139a74af788SAkhil Goyal build_cipher_auth_gcm_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
1140a74af788SAkhil Goyal {
1141a74af788SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1142a74af788SAkhil Goyal 	struct dpaa_sec_job *cf;
1143a74af788SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1144a74af788SAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
1145a74af788SAkhil Goyal 	struct rte_mbuf *mbuf;
1146a74af788SAkhil Goyal 	uint8_t req_segs;
1147a74af788SAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1148a74af788SAkhil Goyal 			ses->iv.offset);
1149a74af788SAkhil Goyal 
1150a74af788SAkhil Goyal 	if (sym->m_dst) {
1151a74af788SAkhil Goyal 		mbuf = sym->m_dst;
1152a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 4;
1153a74af788SAkhil Goyal 	} else {
1154a74af788SAkhil Goyal 		mbuf = sym->m_src;
1155a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs * 2 + 4;
1156a74af788SAkhil Goyal 	}
1157a74af788SAkhil Goyal 
1158a74af788SAkhil Goyal 	if (ses->auth_only_len)
1159a74af788SAkhil Goyal 		req_segs++;
1160a74af788SAkhil Goyal 
1161f7a5752eSHemant Agrawal 	if (mbuf->nb_segs > MAX_SG_ENTRIES) {
1162f163231eSHemant Agrawal 		DPAA_SEC_DP_ERR("AEAD: Max sec segs supported is %d",
1163a74af788SAkhil Goyal 				MAX_SG_ENTRIES);
1164a74af788SAkhil Goyal 		return NULL;
1165a74af788SAkhil Goyal 	}
1166a74af788SAkhil Goyal 
1167f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, req_segs);
1168a74af788SAkhil Goyal 	if (!ctx)
1169a74af788SAkhil Goyal 		return NULL;
1170a74af788SAkhil Goyal 
1171a74af788SAkhil Goyal 	cf = &ctx->job;
1172a74af788SAkhil Goyal 	ctx->op = op;
1173a74af788SAkhil Goyal 
1174a74af788SAkhil Goyal 	rte_prefetch0(cf->sg);
1175a74af788SAkhil Goyal 
1176a74af788SAkhil Goyal 	/* output */
1177a74af788SAkhil Goyal 	out_sg = &cf->sg[0];
1178a74af788SAkhil Goyal 	out_sg->extension = 1;
1179a74af788SAkhil Goyal 	if (is_encode(ses))
11807a4a6da4SVakul Garg 		out_sg->length = sym->aead.data.length + ses->digest_length;
1181a74af788SAkhil Goyal 	else
11827a4a6da4SVakul Garg 		out_sg->length = sym->aead.data.length;
1183a74af788SAkhil Goyal 
1184a74af788SAkhil Goyal 	/* output sg entries */
1185a74af788SAkhil Goyal 	sg = &cf->sg[2];
1186ec861560SGagandeep Singh 	qm_sg_entry_set64(out_sg, rte_dpaa_mem_vtop(sg));
1187a74af788SAkhil Goyal 	cpu_to_hw_sg(out_sg);
1188a74af788SAkhil Goyal 
1189a74af788SAkhil Goyal 	/* 1st seg */
1190ce627d63SThomas Monjalon 	qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
11917a4a6da4SVakul Garg 	sg->length = mbuf->data_len - sym->aead.data.offset;
11927a4a6da4SVakul Garg 	sg->offset = sym->aead.data.offset;
1193a74af788SAkhil Goyal 
1194a74af788SAkhil Goyal 	/* Successive segs */
1195a74af788SAkhil Goyal 	mbuf = mbuf->next;
1196a74af788SAkhil Goyal 	while (mbuf) {
1197a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1198a74af788SAkhil Goyal 		sg++;
1199ce627d63SThomas Monjalon 		qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1200a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1201a74af788SAkhil Goyal 		mbuf = mbuf->next;
1202a74af788SAkhil Goyal 	}
1203a74af788SAkhil Goyal 	sg->length -= ses->digest_length;
1204a74af788SAkhil Goyal 
1205a74af788SAkhil Goyal 	if (is_encode(ses)) {
1206a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1207a74af788SAkhil Goyal 		/* set auth output */
1208a74af788SAkhil Goyal 		sg++;
1209a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, sym->aead.digest.phys_addr);
1210a74af788SAkhil Goyal 		sg->length = ses->digest_length;
1211a74af788SAkhil Goyal 	}
1212a74af788SAkhil Goyal 	sg->final = 1;
1213a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1214a74af788SAkhil Goyal 
1215a74af788SAkhil Goyal 	/* input */
1216a74af788SAkhil Goyal 	mbuf = sym->m_src;
1217a74af788SAkhil Goyal 	in_sg = &cf->sg[1];
1218a74af788SAkhil Goyal 	in_sg->extension = 1;
1219a74af788SAkhil Goyal 	in_sg->final = 1;
1220a74af788SAkhil Goyal 	if (is_encode(ses))
1221a74af788SAkhil Goyal 		in_sg->length = ses->iv.length + sym->aead.data.length
1222a74af788SAkhil Goyal 							+ ses->auth_only_len;
1223a74af788SAkhil Goyal 	else
1224a74af788SAkhil Goyal 		in_sg->length = ses->iv.length + sym->aead.data.length
1225a74af788SAkhil Goyal 				+ ses->auth_only_len + ses->digest_length;
1226a74af788SAkhil Goyal 
1227a74af788SAkhil Goyal 	/* input sg entries */
1228a74af788SAkhil Goyal 	sg++;
1229ec861560SGagandeep Singh 	qm_sg_entry_set64(in_sg, rte_dpaa_mem_vtop(sg));
1230a74af788SAkhil Goyal 	cpu_to_hw_sg(in_sg);
1231a74af788SAkhil Goyal 
1232a74af788SAkhil Goyal 	/* 1st seg IV */
1233ec861560SGagandeep Singh 	qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(IV_ptr));
1234a74af788SAkhil Goyal 	sg->length = ses->iv.length;
1235a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1236a74af788SAkhil Goyal 
1237a74af788SAkhil Goyal 	/* 2nd seg auth only */
1238a74af788SAkhil Goyal 	if (ses->auth_only_len) {
1239a74af788SAkhil Goyal 		sg++;
1240ec861560SGagandeep Singh 		qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(sym->aead.aad.data));
1241a74af788SAkhil Goyal 		sg->length = ses->auth_only_len;
1242a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1243a74af788SAkhil Goyal 	}
1244a74af788SAkhil Goyal 
1245a74af788SAkhil Goyal 	/* 3rd seg */
1246a74af788SAkhil Goyal 	sg++;
1247ce627d63SThomas Monjalon 	qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1248a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->aead.data.offset;
1249a74af788SAkhil Goyal 	sg->offset = sym->aead.data.offset;
1250a74af788SAkhil Goyal 
1251a74af788SAkhil Goyal 	/* Successive segs */
1252a74af788SAkhil Goyal 	mbuf = mbuf->next;
1253a74af788SAkhil Goyal 	while (mbuf) {
1254a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1255a74af788SAkhil Goyal 		sg++;
1256ce627d63SThomas Monjalon 		qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1257a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1258a74af788SAkhil Goyal 		mbuf = mbuf->next;
1259a74af788SAkhil Goyal 	}
1260a74af788SAkhil Goyal 
1261a74af788SAkhil Goyal 	if (is_decode(ses)) {
1262a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1263a74af788SAkhil Goyal 		sg++;
1264a74af788SAkhil Goyal 		memcpy(ctx->digest, sym->aead.digest.data,
1265a74af788SAkhil Goyal 			ses->digest_length);
1266ec861560SGagandeep Singh 		qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(ctx->digest));
1267a74af788SAkhil Goyal 		sg->length = ses->digest_length;
1268a74af788SAkhil Goyal 	}
1269a74af788SAkhil Goyal 	sg->final = 1;
1270a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1271a74af788SAkhil Goyal 
1272a74af788SAkhil Goyal 	return cf;
1273a74af788SAkhil Goyal }
1274a74af788SAkhil Goyal 
1275a74af788SAkhil Goyal static inline struct dpaa_sec_job *
1276c3e85bdcSAkhil Goyal build_cipher_auth_gcm(struct rte_crypto_op *op, dpaa_sec_session *ses)
1277c3e85bdcSAkhil Goyal {
1278c3e85bdcSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1279c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
1280c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1281c3e85bdcSAkhil Goyal 	struct qm_sg_entry *sg;
1282c3e85bdcSAkhil Goyal 	uint32_t length = 0;
1283c4509373SSantosh Shukla 	rte_iova_t src_start_addr, dst_start_addr;
1284c3e85bdcSAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1285c3e85bdcSAkhil Goyal 			ses->iv.offset);
1286c3e85bdcSAkhil Goyal 
1287116ff44aSHemant Agrawal 	src_start_addr = sym->m_src->buf_iova + sym->m_src->data_off;
1288a389434eSAlok Makhariya 
1289a389434eSAlok Makhariya 	if (sym->m_dst)
1290116ff44aSHemant Agrawal 		dst_start_addr = sym->m_dst->buf_iova + sym->m_dst->data_off;
1291a389434eSAlok Makhariya 	else
1292a389434eSAlok Makhariya 		dst_start_addr = src_start_addr;
1293c3e85bdcSAkhil Goyal 
1294f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, 7);
1295c3e85bdcSAkhil Goyal 	if (!ctx)
1296c3e85bdcSAkhil Goyal 		return NULL;
1297c3e85bdcSAkhil Goyal 
1298c3e85bdcSAkhil Goyal 	cf = &ctx->job;
1299c3e85bdcSAkhil Goyal 	ctx->op = op;
1300c3e85bdcSAkhil Goyal 
1301c3e85bdcSAkhil Goyal 	/* input */
1302c3e85bdcSAkhil Goyal 	rte_prefetch0(cf->sg);
1303c3e85bdcSAkhil Goyal 	sg = &cf->sg[2];
1304ec861560SGagandeep Singh 	qm_sg_entry_set64(&cf->sg[1], rte_dpaa_mem_vtop(sg));
1305c3e85bdcSAkhil Goyal 	if (is_encode(ses)) {
1306ec861560SGagandeep Singh 		qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(IV_ptr));
1307c3e85bdcSAkhil Goyal 		sg->length = ses->iv.length;
1308c3e85bdcSAkhil Goyal 		length += sg->length;
1309c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1310c3e85bdcSAkhil Goyal 
1311c3e85bdcSAkhil Goyal 		sg++;
1312c3e85bdcSAkhil Goyal 		if (ses->auth_only_len) {
1313c3e85bdcSAkhil Goyal 			qm_sg_entry_set64(sg,
1314ec861560SGagandeep Singh 					  rte_dpaa_mem_vtop(sym->aead.aad.data));
1315c3e85bdcSAkhil Goyal 			sg->length = ses->auth_only_len;
1316c3e85bdcSAkhil Goyal 			length += sg->length;
1317c3e85bdcSAkhil Goyal 			cpu_to_hw_sg(sg);
1318c3e85bdcSAkhil Goyal 			sg++;
1319c3e85bdcSAkhil Goyal 		}
1320a389434eSAlok Makhariya 		qm_sg_entry_set64(sg, src_start_addr + sym->aead.data.offset);
1321c3e85bdcSAkhil Goyal 		sg->length = sym->aead.data.length;
1322c3e85bdcSAkhil Goyal 		length += sg->length;
1323c3e85bdcSAkhil Goyal 		sg->final = 1;
1324c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1325c3e85bdcSAkhil Goyal 	} else {
1326ec861560SGagandeep Singh 		qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(IV_ptr));
1327c3e85bdcSAkhil Goyal 		sg->length = ses->iv.length;
1328c3e85bdcSAkhil Goyal 		length += sg->length;
1329c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1330c3e85bdcSAkhil Goyal 
1331c3e85bdcSAkhil Goyal 		sg++;
1332c3e85bdcSAkhil Goyal 		if (ses->auth_only_len) {
1333c3e85bdcSAkhil Goyal 			qm_sg_entry_set64(sg,
1334ec861560SGagandeep Singh 					  rte_dpaa_mem_vtop(sym->aead.aad.data));
1335c3e85bdcSAkhil Goyal 			sg->length = ses->auth_only_len;
1336c3e85bdcSAkhil Goyal 			length += sg->length;
1337c3e85bdcSAkhil Goyal 			cpu_to_hw_sg(sg);
1338c3e85bdcSAkhil Goyal 			sg++;
1339c3e85bdcSAkhil Goyal 		}
1340a389434eSAlok Makhariya 		qm_sg_entry_set64(sg, src_start_addr + sym->aead.data.offset);
1341c3e85bdcSAkhil Goyal 		sg->length = sym->aead.data.length;
1342c3e85bdcSAkhil Goyal 		length += sg->length;
1343c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1344c3e85bdcSAkhil Goyal 
1345c3e85bdcSAkhil Goyal 		memcpy(ctx->digest, sym->aead.digest.data,
1346c3e85bdcSAkhil Goyal 		       ses->digest_length);
1347c3e85bdcSAkhil Goyal 		sg++;
1348c3e85bdcSAkhil Goyal 
1349ec861560SGagandeep Singh 		qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(ctx->digest));
1350c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
1351c3e85bdcSAkhil Goyal 		length += sg->length;
1352c3e85bdcSAkhil Goyal 		sg->final = 1;
1353c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1354c3e85bdcSAkhil Goyal 	}
1355c3e85bdcSAkhil Goyal 	/* input compound frame */
1356c3e85bdcSAkhil Goyal 	cf->sg[1].length = length;
1357c3e85bdcSAkhil Goyal 	cf->sg[1].extension = 1;
1358c3e85bdcSAkhil Goyal 	cf->sg[1].final = 1;
1359c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(&cf->sg[1]);
1360c3e85bdcSAkhil Goyal 
1361c3e85bdcSAkhil Goyal 	/* output */
1362c3e85bdcSAkhil Goyal 	sg++;
1363ec861560SGagandeep Singh 	qm_sg_entry_set64(&cf->sg[0], rte_dpaa_mem_vtop(sg));
1364c3e85bdcSAkhil Goyal 	qm_sg_entry_set64(sg,
13657a4a6da4SVakul Garg 		dst_start_addr + sym->aead.data.offset);
13667a4a6da4SVakul Garg 	sg->length = sym->aead.data.length;
1367c3e85bdcSAkhil Goyal 	length = sg->length;
1368c3e85bdcSAkhil Goyal 	if (is_encode(ses)) {
1369c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1370c3e85bdcSAkhil Goyal 		/* set auth output */
1371c3e85bdcSAkhil Goyal 		sg++;
1372c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, sym->aead.digest.phys_addr);
1373c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
1374c3e85bdcSAkhil Goyal 		length += sg->length;
1375c3e85bdcSAkhil Goyal 	}
1376c3e85bdcSAkhil Goyal 	sg->final = 1;
1377c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1378c3e85bdcSAkhil Goyal 
1379c3e85bdcSAkhil Goyal 	/* output compound frame */
1380c3e85bdcSAkhil Goyal 	cf->sg[0].length = length;
1381c3e85bdcSAkhil Goyal 	cf->sg[0].extension = 1;
1382c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(&cf->sg[0]);
1383c3e85bdcSAkhil Goyal 
1384c3e85bdcSAkhil Goyal 	return cf;
1385c3e85bdcSAkhil Goyal }
1386c3e85bdcSAkhil Goyal 
1387c3e85bdcSAkhil Goyal static inline struct dpaa_sec_job *
1388a74af788SAkhil Goyal build_cipher_auth_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
1389a74af788SAkhil Goyal {
1390a74af788SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1391a74af788SAkhil Goyal 	struct dpaa_sec_job *cf;
1392a74af788SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1393a74af788SAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
1394a74af788SAkhil Goyal 	struct rte_mbuf *mbuf;
1395a74af788SAkhil Goyal 	uint8_t req_segs;
1396a74af788SAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1397a74af788SAkhil Goyal 			ses->iv.offset);
1398a74af788SAkhil Goyal 
1399a74af788SAkhil Goyal 	if (sym->m_dst) {
1400a74af788SAkhil Goyal 		mbuf = sym->m_dst;
1401a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 4;
1402a74af788SAkhil Goyal 	} else {
1403a74af788SAkhil Goyal 		mbuf = sym->m_src;
1404a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs * 2 + 4;
1405a74af788SAkhil Goyal 	}
1406a74af788SAkhil Goyal 
1407f7a5752eSHemant Agrawal 	if (mbuf->nb_segs > MAX_SG_ENTRIES) {
1408f163231eSHemant Agrawal 		DPAA_SEC_DP_ERR("Cipher-Auth: Max sec segs supported is %d",
1409a74af788SAkhil Goyal 				MAX_SG_ENTRIES);
1410a74af788SAkhil Goyal 		return NULL;
1411a74af788SAkhil Goyal 	}
1412a74af788SAkhil Goyal 
1413f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, req_segs);
1414a74af788SAkhil Goyal 	if (!ctx)
1415a74af788SAkhil Goyal 		return NULL;
1416a74af788SAkhil Goyal 
1417a74af788SAkhil Goyal 	cf = &ctx->job;
1418a74af788SAkhil Goyal 	ctx->op = op;
1419a74af788SAkhil Goyal 
1420a74af788SAkhil Goyal 	rte_prefetch0(cf->sg);
1421a74af788SAkhil Goyal 
1422a74af788SAkhil Goyal 	/* output */
1423a74af788SAkhil Goyal 	out_sg = &cf->sg[0];
1424a74af788SAkhil Goyal 	out_sg->extension = 1;
1425a74af788SAkhil Goyal 	if (is_encode(ses))
1426a74af788SAkhil Goyal 		out_sg->length = sym->auth.data.length + ses->digest_length;
1427a74af788SAkhil Goyal 	else
1428a74af788SAkhil Goyal 		out_sg->length = sym->auth.data.length;
1429a74af788SAkhil Goyal 
1430a74af788SAkhil Goyal 	/* output sg entries */
1431a74af788SAkhil Goyal 	sg = &cf->sg[2];
1432ec861560SGagandeep Singh 	qm_sg_entry_set64(out_sg, rte_dpaa_mem_vtop(sg));
1433a74af788SAkhil Goyal 	cpu_to_hw_sg(out_sg);
1434a74af788SAkhil Goyal 
1435a74af788SAkhil Goyal 	/* 1st seg */
1436ce627d63SThomas Monjalon 	qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1437a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->auth.data.offset;
1438a74af788SAkhil Goyal 	sg->offset = sym->auth.data.offset;
1439a74af788SAkhil Goyal 
1440a74af788SAkhil Goyal 	/* Successive segs */
1441a74af788SAkhil Goyal 	mbuf = mbuf->next;
1442a74af788SAkhil Goyal 	while (mbuf) {
1443a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1444a74af788SAkhil Goyal 		sg++;
1445ce627d63SThomas Monjalon 		qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1446a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1447a74af788SAkhil Goyal 		mbuf = mbuf->next;
1448a74af788SAkhil Goyal 	}
1449a74af788SAkhil Goyal 	sg->length -= ses->digest_length;
1450a74af788SAkhil Goyal 
1451a74af788SAkhil Goyal 	if (is_encode(ses)) {
1452a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1453a74af788SAkhil Goyal 		/* set auth output */
1454a74af788SAkhil Goyal 		sg++;
1455a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
1456a74af788SAkhil Goyal 		sg->length = ses->digest_length;
1457a74af788SAkhil Goyal 	}
1458a74af788SAkhil Goyal 	sg->final = 1;
1459a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1460a74af788SAkhil Goyal 
1461a74af788SAkhil Goyal 	/* input */
1462a74af788SAkhil Goyal 	mbuf = sym->m_src;
1463a74af788SAkhil Goyal 	in_sg = &cf->sg[1];
1464a74af788SAkhil Goyal 	in_sg->extension = 1;
1465a74af788SAkhil Goyal 	in_sg->final = 1;
1466a74af788SAkhil Goyal 	if (is_encode(ses))
1467a74af788SAkhil Goyal 		in_sg->length = ses->iv.length + sym->auth.data.length;
1468a74af788SAkhil Goyal 	else
1469a74af788SAkhil Goyal 		in_sg->length = ses->iv.length + sym->auth.data.length
1470a74af788SAkhil Goyal 						+ ses->digest_length;
1471a74af788SAkhil Goyal 
1472a74af788SAkhil Goyal 	/* input sg entries */
1473a74af788SAkhil Goyal 	sg++;
1474ec861560SGagandeep Singh 	qm_sg_entry_set64(in_sg, rte_dpaa_mem_vtop(sg));
1475a74af788SAkhil Goyal 	cpu_to_hw_sg(in_sg);
1476a74af788SAkhil Goyal 
1477a74af788SAkhil Goyal 	/* 1st seg IV */
1478ec861560SGagandeep Singh 	qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(IV_ptr));
1479a74af788SAkhil Goyal 	sg->length = ses->iv.length;
1480a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1481a74af788SAkhil Goyal 
1482a74af788SAkhil Goyal 	/* 2nd seg */
1483a74af788SAkhil Goyal 	sg++;
1484ce627d63SThomas Monjalon 	qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1485a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->auth.data.offset;
1486a74af788SAkhil Goyal 	sg->offset = sym->auth.data.offset;
1487a74af788SAkhil Goyal 
1488a74af788SAkhil Goyal 	/* Successive segs */
1489a74af788SAkhil Goyal 	mbuf = mbuf->next;
1490a74af788SAkhil Goyal 	while (mbuf) {
1491a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1492a74af788SAkhil Goyal 		sg++;
1493ce627d63SThomas Monjalon 		qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1494a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1495a74af788SAkhil Goyal 		mbuf = mbuf->next;
1496a74af788SAkhil Goyal 	}
1497a74af788SAkhil Goyal 
1498a74af788SAkhil Goyal 	sg->length -= ses->digest_length;
1499a74af788SAkhil Goyal 	if (is_decode(ses)) {
1500a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1501a74af788SAkhil Goyal 		sg++;
1502a74af788SAkhil Goyal 		memcpy(ctx->digest, sym->auth.digest.data,
1503a74af788SAkhil Goyal 			ses->digest_length);
1504ec861560SGagandeep Singh 		qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(ctx->digest));
1505a74af788SAkhil Goyal 		sg->length = ses->digest_length;
1506a74af788SAkhil Goyal 	}
1507a74af788SAkhil Goyal 	sg->final = 1;
1508a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1509a74af788SAkhil Goyal 
1510a74af788SAkhil Goyal 	return cf;
1511a74af788SAkhil Goyal }
1512a74af788SAkhil Goyal 
1513a74af788SAkhil Goyal static inline struct dpaa_sec_job *
1514c3e85bdcSAkhil Goyal build_cipher_auth(struct rte_crypto_op *op, dpaa_sec_session *ses)
1515c3e85bdcSAkhil Goyal {
1516c3e85bdcSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1517c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
1518c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1519c3e85bdcSAkhil Goyal 	struct qm_sg_entry *sg;
1520c4509373SSantosh Shukla 	rte_iova_t src_start_addr, dst_start_addr;
1521c3e85bdcSAkhil Goyal 	uint32_t length = 0;
1522c3e85bdcSAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1523c3e85bdcSAkhil Goyal 			ses->iv.offset);
1524c3e85bdcSAkhil Goyal 
1525455da545SSantosh Shukla 	src_start_addr = sym->m_src->buf_iova + sym->m_src->data_off;
1526a389434eSAlok Makhariya 	if (sym->m_dst)
1527455da545SSantosh Shukla 		dst_start_addr = sym->m_dst->buf_iova + sym->m_dst->data_off;
1528a389434eSAlok Makhariya 	else
1529a389434eSAlok Makhariya 		dst_start_addr = src_start_addr;
1530c3e85bdcSAkhil Goyal 
1531f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, 7);
1532c3e85bdcSAkhil Goyal 	if (!ctx)
1533c3e85bdcSAkhil Goyal 		return NULL;
1534c3e85bdcSAkhil Goyal 
1535c3e85bdcSAkhil Goyal 	cf = &ctx->job;
1536c3e85bdcSAkhil Goyal 	ctx->op = op;
1537c3e85bdcSAkhil Goyal 
1538c3e85bdcSAkhil Goyal 	/* input */
1539c3e85bdcSAkhil Goyal 	rte_prefetch0(cf->sg);
1540c3e85bdcSAkhil Goyal 	sg = &cf->sg[2];
1541ec861560SGagandeep Singh 	qm_sg_entry_set64(&cf->sg[1], rte_dpaa_mem_vtop(sg));
1542c3e85bdcSAkhil Goyal 	if (is_encode(ses)) {
1543ec861560SGagandeep Singh 		qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(IV_ptr));
1544c3e85bdcSAkhil Goyal 		sg->length = ses->iv.length;
1545c3e85bdcSAkhil Goyal 		length += sg->length;
1546c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1547c3e85bdcSAkhil Goyal 
1548c3e85bdcSAkhil Goyal 		sg++;
1549a389434eSAlok Makhariya 		qm_sg_entry_set64(sg, src_start_addr + sym->auth.data.offset);
1550c3e85bdcSAkhil Goyal 		sg->length = sym->auth.data.length;
1551c3e85bdcSAkhil Goyal 		length += sg->length;
1552c3e85bdcSAkhil Goyal 		sg->final = 1;
1553c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1554c3e85bdcSAkhil Goyal 	} else {
1555ec861560SGagandeep Singh 		qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(IV_ptr));
1556c3e85bdcSAkhil Goyal 		sg->length = ses->iv.length;
1557c3e85bdcSAkhil Goyal 		length += sg->length;
1558c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1559c3e85bdcSAkhil Goyal 
1560c3e85bdcSAkhil Goyal 		sg++;
1561c3e85bdcSAkhil Goyal 
1562a389434eSAlok Makhariya 		qm_sg_entry_set64(sg, src_start_addr + sym->auth.data.offset);
1563c3e85bdcSAkhil Goyal 		sg->length = sym->auth.data.length;
1564c3e85bdcSAkhil Goyal 		length += sg->length;
1565c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1566c3e85bdcSAkhil Goyal 
1567c3e85bdcSAkhil Goyal 		memcpy(ctx->digest, sym->auth.digest.data,
1568c3e85bdcSAkhil Goyal 		       ses->digest_length);
1569c3e85bdcSAkhil Goyal 		sg++;
1570c3e85bdcSAkhil Goyal 
1571ec861560SGagandeep Singh 		qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(ctx->digest));
1572c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
1573c3e85bdcSAkhil Goyal 		length += sg->length;
1574c3e85bdcSAkhil Goyal 		sg->final = 1;
1575c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1576c3e85bdcSAkhil Goyal 	}
1577c3e85bdcSAkhil Goyal 	/* input compound frame */
1578c3e85bdcSAkhil Goyal 	cf->sg[1].length = length;
1579c3e85bdcSAkhil Goyal 	cf->sg[1].extension = 1;
1580c3e85bdcSAkhil Goyal 	cf->sg[1].final = 1;
1581c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(&cf->sg[1]);
1582c3e85bdcSAkhil Goyal 
1583c3e85bdcSAkhil Goyal 	/* output */
1584c3e85bdcSAkhil Goyal 	sg++;
1585ec861560SGagandeep Singh 	qm_sg_entry_set64(&cf->sg[0], rte_dpaa_mem_vtop(sg));
1586a389434eSAlok Makhariya 	qm_sg_entry_set64(sg, dst_start_addr + sym->cipher.data.offset);
1587c3e85bdcSAkhil Goyal 	sg->length = sym->cipher.data.length;
1588c3e85bdcSAkhil Goyal 	length = sg->length;
1589c3e85bdcSAkhil Goyal 	if (is_encode(ses)) {
1590c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1591c3e85bdcSAkhil Goyal 		/* set auth output */
1592c3e85bdcSAkhil Goyal 		sg++;
1593c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
1594c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
1595c3e85bdcSAkhil Goyal 		length += sg->length;
1596c3e85bdcSAkhil Goyal 	}
1597c3e85bdcSAkhil Goyal 	sg->final = 1;
1598c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1599c3e85bdcSAkhil Goyal 
1600c3e85bdcSAkhil Goyal 	/* output compound frame */
1601c3e85bdcSAkhil Goyal 	cf->sg[0].length = length;
1602c3e85bdcSAkhil Goyal 	cf->sg[0].extension = 1;
1603c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(&cf->sg[0]);
1604c3e85bdcSAkhil Goyal 
1605c3e85bdcSAkhil Goyal 	return cf;
1606c3e85bdcSAkhil Goyal }
1607c3e85bdcSAkhil Goyal 
1608a8d0d473SBruce Richardson #ifdef RTE_LIB_SECURITY
16091f14d500SAkhil Goyal static inline struct dpaa_sec_job *
16101f14d500SAkhil Goyal build_proto(struct rte_crypto_op *op, dpaa_sec_session *ses)
16111f14d500SAkhil Goyal {
16121f14d500SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
16131f14d500SAkhil Goyal 	struct dpaa_sec_job *cf;
16141f14d500SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
16151f14d500SAkhil Goyal 	struct qm_sg_entry *sg;
16161f14d500SAkhil Goyal 	phys_addr_t src_start_addr, dst_start_addr;
16171f14d500SAkhil Goyal 
1618f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, 2);
16191f14d500SAkhil Goyal 	if (!ctx)
16201f14d500SAkhil Goyal 		return NULL;
16211f14d500SAkhil Goyal 	cf = &ctx->job;
16221f14d500SAkhil Goyal 	ctx->op = op;
16231f14d500SAkhil Goyal 
1624ce627d63SThomas Monjalon 	src_start_addr = rte_pktmbuf_iova(sym->m_src);
16251f14d500SAkhil Goyal 
16261f14d500SAkhil Goyal 	if (sym->m_dst)
1627ce627d63SThomas Monjalon 		dst_start_addr = rte_pktmbuf_iova(sym->m_dst);
16281f14d500SAkhil Goyal 	else
16291f14d500SAkhil Goyal 		dst_start_addr = src_start_addr;
16301f14d500SAkhil Goyal 
16311f14d500SAkhil Goyal 	/* input */
16321f14d500SAkhil Goyal 	sg = &cf->sg[1];
16331f14d500SAkhil Goyal 	qm_sg_entry_set64(sg, src_start_addr);
16341f14d500SAkhil Goyal 	sg->length = sym->m_src->pkt_len;
16351f14d500SAkhil Goyal 	sg->final = 1;
16361f14d500SAkhil Goyal 	cpu_to_hw_sg(sg);
16371f14d500SAkhil Goyal 
16381f14d500SAkhil Goyal 	sym->m_src->packet_type &= ~RTE_PTYPE_L4_MASK;
16391f14d500SAkhil Goyal 	/* output */
16401f14d500SAkhil Goyal 	sg = &cf->sg[0];
16411f14d500SAkhil Goyal 	qm_sg_entry_set64(sg, dst_start_addr);
16421f14d500SAkhil Goyal 	sg->length = sym->m_src->buf_len - sym->m_src->data_off;
16431f14d500SAkhil Goyal 	cpu_to_hw_sg(sg);
16441f14d500SAkhil Goyal 
16451f14d500SAkhil Goyal 	return cf;
16461f14d500SAkhil Goyal }
16471f14d500SAkhil Goyal 
1648fb5c100aSAkhil Goyal static inline struct dpaa_sec_job *
1649fb5c100aSAkhil Goyal build_proto_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
1650fb5c100aSAkhil Goyal {
1651fb5c100aSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1652fb5c100aSAkhil Goyal 	struct dpaa_sec_job *cf;
1653fb5c100aSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1654fb5c100aSAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
1655fb5c100aSAkhil Goyal 	struct rte_mbuf *mbuf;
1656fb5c100aSAkhil Goyal 	uint8_t req_segs;
1657fb5c100aSAkhil Goyal 	uint32_t in_len = 0, out_len = 0;
1658fb5c100aSAkhil Goyal 
1659fb5c100aSAkhil Goyal 	if (sym->m_dst)
1660fb5c100aSAkhil Goyal 		mbuf = sym->m_dst;
1661fb5c100aSAkhil Goyal 	else
1662fb5c100aSAkhil Goyal 		mbuf = sym->m_src;
1663fb5c100aSAkhil Goyal 
1664fb5c100aSAkhil Goyal 	req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 2;
1665f7a5752eSHemant Agrawal 	if (mbuf->nb_segs > MAX_SG_ENTRIES) {
1666fb5c100aSAkhil Goyal 		DPAA_SEC_DP_ERR("Proto: Max sec segs supported is %d",
1667fb5c100aSAkhil Goyal 				MAX_SG_ENTRIES);
1668fb5c100aSAkhil Goyal 		return NULL;
1669fb5c100aSAkhil Goyal 	}
1670fb5c100aSAkhil Goyal 
1671f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, req_segs);
1672fb5c100aSAkhil Goyal 	if (!ctx)
1673fb5c100aSAkhil Goyal 		return NULL;
1674fb5c100aSAkhil Goyal 	cf = &ctx->job;
1675fb5c100aSAkhil Goyal 	ctx->op = op;
1676fb5c100aSAkhil Goyal 	/* output */
1677fb5c100aSAkhil Goyal 	out_sg = &cf->sg[0];
1678fb5c100aSAkhil Goyal 	out_sg->extension = 1;
1679ec861560SGagandeep Singh 	qm_sg_entry_set64(out_sg, rte_dpaa_mem_vtop(&cf->sg[2]));
1680fb5c100aSAkhil Goyal 
1681fb5c100aSAkhil Goyal 	/* 1st seg */
1682fb5c100aSAkhil Goyal 	sg = &cf->sg[2];
1683ce627d63SThomas Monjalon 	qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1684fb5c100aSAkhil Goyal 	sg->offset = 0;
1685fb5c100aSAkhil Goyal 
1686fb5c100aSAkhil Goyal 	/* Successive segs */
1687fb5c100aSAkhil Goyal 	while (mbuf->next) {
1688fb5c100aSAkhil Goyal 		sg->length = mbuf->data_len;
1689fb5c100aSAkhil Goyal 		out_len += sg->length;
1690fb5c100aSAkhil Goyal 		mbuf = mbuf->next;
1691fb5c100aSAkhil Goyal 		cpu_to_hw_sg(sg);
1692fb5c100aSAkhil Goyal 		sg++;
1693ce627d63SThomas Monjalon 		qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1694fb5c100aSAkhil Goyal 		sg->offset = 0;
1695fb5c100aSAkhil Goyal 	}
1696fb5c100aSAkhil Goyal 	sg->length = mbuf->buf_len - mbuf->data_off;
1697fb5c100aSAkhil Goyal 	out_len += sg->length;
1698fb5c100aSAkhil Goyal 	sg->final = 1;
1699fb5c100aSAkhil Goyal 	cpu_to_hw_sg(sg);
1700fb5c100aSAkhil Goyal 
1701fb5c100aSAkhil Goyal 	out_sg->length = out_len;
1702fb5c100aSAkhil Goyal 	cpu_to_hw_sg(out_sg);
1703fb5c100aSAkhil Goyal 
1704fb5c100aSAkhil Goyal 	/* input */
1705fb5c100aSAkhil Goyal 	mbuf = sym->m_src;
1706fb5c100aSAkhil Goyal 	in_sg = &cf->sg[1];
1707fb5c100aSAkhil Goyal 	in_sg->extension = 1;
1708fb5c100aSAkhil Goyal 	in_sg->final = 1;
1709fb5c100aSAkhil Goyal 	in_len = mbuf->data_len;
1710fb5c100aSAkhil Goyal 
1711fb5c100aSAkhil Goyal 	sg++;
1712ec861560SGagandeep Singh 	qm_sg_entry_set64(in_sg, rte_dpaa_mem_vtop(sg));
1713fb5c100aSAkhil Goyal 
1714fb5c100aSAkhil Goyal 	/* 1st seg */
1715ce627d63SThomas Monjalon 	qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1716fb5c100aSAkhil Goyal 	sg->length = mbuf->data_len;
1717fb5c100aSAkhil Goyal 	sg->offset = 0;
1718fb5c100aSAkhil Goyal 
1719fb5c100aSAkhil Goyal 	/* Successive segs */
1720fb5c100aSAkhil Goyal 	mbuf = mbuf->next;
1721fb5c100aSAkhil Goyal 	while (mbuf) {
1722fb5c100aSAkhil Goyal 		cpu_to_hw_sg(sg);
1723fb5c100aSAkhil Goyal 		sg++;
1724ce627d63SThomas Monjalon 		qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1725fb5c100aSAkhil Goyal 		sg->length = mbuf->data_len;
1726fb5c100aSAkhil Goyal 		sg->offset = 0;
1727fb5c100aSAkhil Goyal 		in_len += sg->length;
1728fb5c100aSAkhil Goyal 		mbuf = mbuf->next;
1729fb5c100aSAkhil Goyal 	}
1730fb5c100aSAkhil Goyal 	sg->final = 1;
1731fb5c100aSAkhil Goyal 	cpu_to_hw_sg(sg);
1732fb5c100aSAkhil Goyal 
1733fb5c100aSAkhil Goyal 	in_sg->length = in_len;
1734fb5c100aSAkhil Goyal 	cpu_to_hw_sg(in_sg);
1735fb5c100aSAkhil Goyal 
1736fb5c100aSAkhil Goyal 	sym->m_src->packet_type &= ~RTE_PTYPE_L4_MASK;
1737fb5c100aSAkhil Goyal 
1738fb5c100aSAkhil Goyal 	return cf;
1739fb5c100aSAkhil Goyal }
1740314424b6SHemant Agrawal #endif
1741fb5c100aSAkhil Goyal 
17429a984458SAkhil Goyal static uint16_t
17439a984458SAkhil Goyal dpaa_sec_enqueue_burst(void *qp, struct rte_crypto_op **ops,
17449a984458SAkhil Goyal 		       uint16_t nb_ops)
1745c3e85bdcSAkhil Goyal {
17469a984458SAkhil Goyal 	/* Function to transmit the frames to given device and queuepair */
17479a984458SAkhil Goyal 	uint32_t loop;
17489a984458SAkhil Goyal 	struct dpaa_sec_qp *dpaa_qp = (struct dpaa_sec_qp *)qp;
17499a984458SAkhil Goyal 	uint16_t num_tx = 0;
17509a984458SAkhil Goyal 	struct qm_fd fds[DPAA_SEC_BURST], *fd;
17519a984458SAkhil Goyal 	uint32_t frames_to_send;
17529a984458SAkhil Goyal 	struct rte_crypto_op *op;
1753c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
1754c3e85bdcSAkhil Goyal 	dpaa_sec_session *ses;
17553394ed47SVakul Garg 	uint16_t auth_hdr_len, auth_tail_len;
17563394ed47SVakul Garg 	uint32_t index, flags[DPAA_SEC_BURST] = {0};
17579a984458SAkhil Goyal 	struct qman_fq *inq[DPAA_SEC_BURST];
1758c3e85bdcSAkhil Goyal 
175922629f05SHemant Agrawal 	if (unlikely(!DPAA_PER_LCORE_PORTAL)) {
176022629f05SHemant Agrawal 		if (rte_dpaa_portal_init((void *)0)) {
176122629f05SHemant Agrawal 			DPAA_SEC_ERR("Failure in affining portal");
176222629f05SHemant Agrawal 			return 0;
176322629f05SHemant Agrawal 		}
176422629f05SHemant Agrawal 	}
176522629f05SHemant Agrawal 
17669a984458SAkhil Goyal 	while (nb_ops) {
17679a984458SAkhil Goyal 		frames_to_send = (nb_ops > DPAA_SEC_BURST) ?
17689a984458SAkhil Goyal 				DPAA_SEC_BURST : nb_ops;
17699a984458SAkhil Goyal 		for (loop = 0; loop < frames_to_send; loop++) {
17709a984458SAkhil Goyal 			op = *(ops++);
1771c9a1c2e5SDavid Marchand 			if (*dpaa_seqn(op->sym->m_src) != 0) {
1772c9a1c2e5SDavid Marchand 				index = *dpaa_seqn(op->sym->m_src) - 1;
1773fe3688baSAkhil Goyal 				if (DPAA_PER_LCORE_DQRR_HELD & (1 << index)) {
1774fe3688baSAkhil Goyal 					/* QM_EQCR_DCA_IDXMASK = 0x0f */
1775fe3688baSAkhil Goyal 					flags[loop] = ((index & 0x0f) << 8);
1776fe3688baSAkhil Goyal 					flags[loop] |= QMAN_ENQUEUE_FLAG_DCA;
1777fe3688baSAkhil Goyal 					DPAA_PER_LCORE_DQRR_SIZE--;
1778fe3688baSAkhil Goyal 					DPAA_PER_LCORE_DQRR_HELD &=
1779fe3688baSAkhil Goyal 								~(1 << index);
1780fe3688baSAkhil Goyal 				}
1781fe3688baSAkhil Goyal 			}
1782fe3688baSAkhil Goyal 
17839a984458SAkhil Goyal 			switch (op->sess_type) {
17849a984458SAkhil Goyal 			case RTE_CRYPTO_OP_WITH_SESSION:
17859a984458SAkhil Goyal 				ses = (dpaa_sec_session *)
1786012c5076SPablo de Lara 					get_sym_session_private_data(
17879a984458SAkhil Goyal 						op->sym->session,
17889d5f73c2SGagandeep Singh 						dpaa_cryptodev_driver_id);
17899a984458SAkhil Goyal 				break;
1790a8d0d473SBruce Richardson #ifdef RTE_LIB_SECURITY
17919a984458SAkhil Goyal 			case RTE_CRYPTO_OP_SECURITY_SESSION:
17929a984458SAkhil Goyal 				ses = (dpaa_sec_session *)
17939a984458SAkhil Goyal 					get_sec_session_private_data(
17941f14d500SAkhil Goyal 							op->sym->sec_session);
17959a984458SAkhil Goyal 				break;
1796314424b6SHemant Agrawal #endif
17979a984458SAkhil Goyal 			default:
1798f163231eSHemant Agrawal 				DPAA_SEC_DP_ERR(
17999a984458SAkhil Goyal 					"sessionless crypto op not supported");
18009a984458SAkhil Goyal 				frames_to_send = loop;
18019a984458SAkhil Goyal 				nb_ops = loop;
18029a984458SAkhil Goyal 				goto send_pkts;
18039a984458SAkhil Goyal 			}
1804e1e52232SHemant Agrawal 
1805e1e52232SHemant Agrawal 			if (!ses) {
1806e1e52232SHemant Agrawal 				DPAA_SEC_DP_ERR("session not available");
1807e1e52232SHemant Agrawal 				frames_to_send = loop;
1808e1e52232SHemant Agrawal 				nb_ops = loop;
1809e1e52232SHemant Agrawal 				goto send_pkts;
1810e1e52232SHemant Agrawal 			}
1811e1e52232SHemant Agrawal 
18124e694fe5SAkhil Goyal 			if (unlikely(!ses->qp[rte_lcore_id() % MAX_DPAA_CORES])) {
18139a984458SAkhil Goyal 				if (dpaa_sec_attach_sess_q(qp, ses)) {
18149a984458SAkhil Goyal 					frames_to_send = loop;
18159a984458SAkhil Goyal 					nb_ops = loop;
18169a984458SAkhil Goyal 					goto send_pkts;
18179a984458SAkhil Goyal 				}
18184e694fe5SAkhil Goyal 			} else if (unlikely(ses->qp[rte_lcore_id() %
18194e694fe5SAkhil Goyal 						MAX_DPAA_CORES] != qp)) {
18209198b2c2SAkhil Goyal 				DPAA_SEC_DP_ERR("Old:sess->qp = %p"
18214e694fe5SAkhil Goyal 					" New qp = %p\n",
18224e694fe5SAkhil Goyal 					ses->qp[rte_lcore_id() %
18234e694fe5SAkhil Goyal 					MAX_DPAA_CORES], qp);
18249198b2c2SAkhil Goyal 				frames_to_send = loop;
18259198b2c2SAkhil Goyal 				nb_ops = loop;
18269198b2c2SAkhil Goyal 				goto send_pkts;
1827c3e85bdcSAkhil Goyal 			}
1828c3e85bdcSAkhil Goyal 
18293394ed47SVakul Garg 			auth_hdr_len = op->sym->auth.data.length -
18309a984458SAkhil Goyal 						op->sym->cipher.data.length;
18313394ed47SVakul Garg 			auth_tail_len = 0;
18323394ed47SVakul Garg 
1833fb5c100aSAkhil Goyal 			if (rte_pktmbuf_is_contiguous(op->sym->m_src) &&
1834fb5c100aSAkhil Goyal 				  ((op->sym->m_dst == NULL) ||
1835fb5c100aSAkhil Goyal 				   rte_pktmbuf_is_contiguous(op->sym->m_dst))) {
18368524b44eSHemant Agrawal 				switch (ses->ctxt) {
1837a8d0d473SBruce Richardson #ifdef RTE_LIB_SECURITY
18388524b44eSHemant Agrawal 				case DPAA_SEC_PDCP:
18398524b44eSHemant Agrawal 				case DPAA_SEC_IPSEC:
184005b12700SHemant Agrawal 					cf = build_proto(op, ses);
18418524b44eSHemant Agrawal 					break;
1842314424b6SHemant Agrawal #endif
18438524b44eSHemant Agrawal 				case DPAA_SEC_AUTH:
1844c3e85bdcSAkhil Goyal 					cf = build_auth_only(op, ses);
18458524b44eSHemant Agrawal 					break;
18468524b44eSHemant Agrawal 				case DPAA_SEC_CIPHER:
1847c3e85bdcSAkhil Goyal 					cf = build_cipher_only(op, ses);
18488524b44eSHemant Agrawal 					break;
18498524b44eSHemant Agrawal 				case DPAA_SEC_AEAD:
1850c3e85bdcSAkhil Goyal 					cf = build_cipher_auth_gcm(op, ses);
18513394ed47SVakul Garg 					auth_hdr_len = ses->auth_only_len;
18528524b44eSHemant Agrawal 					break;
18538524b44eSHemant Agrawal 				case DPAA_SEC_CIPHER_HASH:
18543394ed47SVakul Garg 					auth_hdr_len =
18553394ed47SVakul Garg 						op->sym->cipher.data.offset
18563394ed47SVakul Garg 						- op->sym->auth.data.offset;
18573394ed47SVakul Garg 					auth_tail_len =
18583394ed47SVakul Garg 						op->sym->auth.data.length
18593394ed47SVakul Garg 						- op->sym->cipher.data.length
18603394ed47SVakul Garg 						- auth_hdr_len;
1861c3e85bdcSAkhil Goyal 					cf = build_cipher_auth(op, ses);
18628524b44eSHemant Agrawal 					break;
18638524b44eSHemant Agrawal 				default:
1864f163231eSHemant Agrawal 					DPAA_SEC_DP_ERR("not supported ops");
18659a984458SAkhil Goyal 					frames_to_send = loop;
18669a984458SAkhil Goyal 					nb_ops = loop;
18679a984458SAkhil Goyal 					goto send_pkts;
1868c3e85bdcSAkhil Goyal 				}
1869a74af788SAkhil Goyal 			} else {
18708524b44eSHemant Agrawal 				switch (ses->ctxt) {
1871a8d0d473SBruce Richardson #ifdef RTE_LIB_SECURITY
18728524b44eSHemant Agrawal 				case DPAA_SEC_PDCP:
18738524b44eSHemant Agrawal 				case DPAA_SEC_IPSEC:
1874fb5c100aSAkhil Goyal 					cf = build_proto_sg(op, ses);
18758524b44eSHemant Agrawal 					break;
1876314424b6SHemant Agrawal #endif
18778524b44eSHemant Agrawal 				case DPAA_SEC_AUTH:
1878a74af788SAkhil Goyal 					cf = build_auth_only_sg(op, ses);
18798524b44eSHemant Agrawal 					break;
18808524b44eSHemant Agrawal 				case DPAA_SEC_CIPHER:
1881a74af788SAkhil Goyal 					cf = build_cipher_only_sg(op, ses);
18828524b44eSHemant Agrawal 					break;
18838524b44eSHemant Agrawal 				case DPAA_SEC_AEAD:
1884a74af788SAkhil Goyal 					cf = build_cipher_auth_gcm_sg(op, ses);
18853394ed47SVakul Garg 					auth_hdr_len = ses->auth_only_len;
18868524b44eSHemant Agrawal 					break;
18878524b44eSHemant Agrawal 				case DPAA_SEC_CIPHER_HASH:
18883394ed47SVakul Garg 					auth_hdr_len =
18893394ed47SVakul Garg 						op->sym->cipher.data.offset
18903394ed47SVakul Garg 						- op->sym->auth.data.offset;
18913394ed47SVakul Garg 					auth_tail_len =
18923394ed47SVakul Garg 						op->sym->auth.data.length
18933394ed47SVakul Garg 						- op->sym->cipher.data.length
18943394ed47SVakul Garg 						- auth_hdr_len;
1895a74af788SAkhil Goyal 					cf = build_cipher_auth_sg(op, ses);
18968524b44eSHemant Agrawal 					break;
18978524b44eSHemant Agrawal 				default:
1898f163231eSHemant Agrawal 					DPAA_SEC_DP_ERR("not supported ops");
1899a74af788SAkhil Goyal 					frames_to_send = loop;
1900a74af788SAkhil Goyal 					nb_ops = loop;
1901a74af788SAkhil Goyal 					goto send_pkts;
1902a74af788SAkhil Goyal 				}
1903a74af788SAkhil Goyal 			}
19049a984458SAkhil Goyal 			if (unlikely(!cf)) {
19059a984458SAkhil Goyal 				frames_to_send = loop;
19069a984458SAkhil Goyal 				nb_ops = loop;
19079a984458SAkhil Goyal 				goto send_pkts;
19089a984458SAkhil Goyal 			}
1909c3e85bdcSAkhil Goyal 
19109a984458SAkhil Goyal 			fd = &fds[loop];
19114e694fe5SAkhil Goyal 			inq[loop] = ses->inq[rte_lcore_id() % MAX_DPAA_CORES];
19129a984458SAkhil Goyal 			fd->opaque_addr = 0;
19139a984458SAkhil Goyal 			fd->cmd = 0;
1914ec861560SGagandeep Singh 			qm_fd_addr_set64(fd, rte_dpaa_mem_vtop(cf->sg));
19159a984458SAkhil Goyal 			fd->_format1 = qm_fd_compound;
19169a984458SAkhil Goyal 			fd->length29 = 2 * sizeof(struct qm_sg_entry);
19173394ed47SVakul Garg 
19189a984458SAkhil Goyal 			/* Auth_only_len is set as 0 in descriptor and it is
19199a984458SAkhil Goyal 			 * overwritten here in the fd.cmd which will update
19209a984458SAkhil Goyal 			 * the DPOVRD reg.
1921c3e85bdcSAkhil Goyal 			 */
19223394ed47SVakul Garg 			if (auth_hdr_len || auth_tail_len) {
19233394ed47SVakul Garg 				fd->cmd = 0x80000000;
19243394ed47SVakul Garg 				fd->cmd |=
19253394ed47SVakul Garg 					((auth_tail_len << 16) | auth_hdr_len);
19263394ed47SVakul Garg 			}
1927c3e85bdcSAkhil Goyal 
1928a8d0d473SBruce Richardson #ifdef RTE_LIB_SECURITY
19296a0c9d36SAkhil Goyal 			/* In case of PDCP, per packet HFN is stored in
19306a0c9d36SAkhil Goyal 			 * mbuf priv after sym_op.
19316a0c9d36SAkhil Goyal 			 */
19328524b44eSHemant Agrawal 			if ((ses->ctxt == DPAA_SEC_PDCP) && ses->pdcp.hfn_ovd) {
19336a0c9d36SAkhil Goyal 				fd->cmd = 0x80000000 |
19346a0c9d36SAkhil Goyal 					*((uint32_t *)((uint8_t *)op +
19356a0c9d36SAkhil Goyal 					ses->pdcp.hfn_ovd_offset));
19368524b44eSHemant Agrawal 				DPAA_SEC_DP_DEBUG("Per packet HFN: %x, ovd:%u\n",
19376a0c9d36SAkhil Goyal 					*((uint32_t *)((uint8_t *)op +
19386a0c9d36SAkhil Goyal 					ses->pdcp.hfn_ovd_offset)),
19398524b44eSHemant Agrawal 					ses->pdcp.hfn_ovd);
19406a0c9d36SAkhil Goyal 			}
1941314424b6SHemant Agrawal #endif
19429a984458SAkhil Goyal 		}
19439a984458SAkhil Goyal send_pkts:
19449a984458SAkhil Goyal 		loop = 0;
19459a984458SAkhil Goyal 		while (loop < frames_to_send) {
19469a984458SAkhil Goyal 			loop += qman_enqueue_multi_fq(&inq[loop], &fds[loop],
1947fe3688baSAkhil Goyal 					&flags[loop], frames_to_send - loop);
19489a984458SAkhil Goyal 		}
19499a984458SAkhil Goyal 		nb_ops -= frames_to_send;
19509a984458SAkhil Goyal 		num_tx += frames_to_send;
1951c3e85bdcSAkhil Goyal 	}
1952c3e85bdcSAkhil Goyal 
1953c3e85bdcSAkhil Goyal 	dpaa_qp->tx_pkts += num_tx;
1954c3e85bdcSAkhil Goyal 	dpaa_qp->tx_errs += nb_ops - num_tx;
1955c3e85bdcSAkhil Goyal 
1956c3e85bdcSAkhil Goyal 	return num_tx;
1957c3e85bdcSAkhil Goyal }
1958c3e85bdcSAkhil Goyal 
1959c3e85bdcSAkhil Goyal static uint16_t
1960c3e85bdcSAkhil Goyal dpaa_sec_dequeue_burst(void *qp, struct rte_crypto_op **ops,
1961c3e85bdcSAkhil Goyal 		       uint16_t nb_ops)
1962c3e85bdcSAkhil Goyal {
1963c3e85bdcSAkhil Goyal 	uint16_t num_rx;
1964c3e85bdcSAkhil Goyal 	struct dpaa_sec_qp *dpaa_qp = (struct dpaa_sec_qp *)qp;
1965c3e85bdcSAkhil Goyal 
196622629f05SHemant Agrawal 	if (unlikely(!DPAA_PER_LCORE_PORTAL)) {
196722629f05SHemant Agrawal 		if (rte_dpaa_portal_init((void *)0)) {
196822629f05SHemant Agrawal 			DPAA_SEC_ERR("Failure in affining portal");
196922629f05SHemant Agrawal 			return 0;
197022629f05SHemant Agrawal 		}
197122629f05SHemant Agrawal 	}
197222629f05SHemant Agrawal 
1973c3e85bdcSAkhil Goyal 	num_rx = dpaa_sec_deq(dpaa_qp, ops, nb_ops);
1974c3e85bdcSAkhil Goyal 
1975c3e85bdcSAkhil Goyal 	dpaa_qp->rx_pkts += num_rx;
1976c3e85bdcSAkhil Goyal 	dpaa_qp->rx_errs += nb_ops - num_rx;
1977c3e85bdcSAkhil Goyal 
1978f163231eSHemant Agrawal 	DPAA_SEC_DP_DEBUG("SEC Received %d Packets\n", num_rx);
1979c3e85bdcSAkhil Goyal 
1980c3e85bdcSAkhil Goyal 	return num_rx;
1981c3e85bdcSAkhil Goyal }
1982c3e85bdcSAkhil Goyal 
1983c3e85bdcSAkhil Goyal /** Release queue pair */
1984c3e85bdcSAkhil Goyal static int
1985c3e85bdcSAkhil Goyal dpaa_sec_queue_pair_release(struct rte_cryptodev *dev,
1986c3e85bdcSAkhil Goyal 			    uint16_t qp_id)
1987c3e85bdcSAkhil Goyal {
1988c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals;
1989c3e85bdcSAkhil Goyal 	struct dpaa_sec_qp *qp = NULL;
1990c3e85bdcSAkhil Goyal 
1991c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
1992c3e85bdcSAkhil Goyal 
1993f163231eSHemant Agrawal 	DPAA_SEC_DEBUG("dev =%p, queue =%d", dev, qp_id);
1994c3e85bdcSAkhil Goyal 
1995c3e85bdcSAkhil Goyal 	internals = dev->data->dev_private;
1996c3e85bdcSAkhil Goyal 	if (qp_id >= internals->max_nb_queue_pairs) {
1997f163231eSHemant Agrawal 		DPAA_SEC_ERR("Max supported qpid %d",
1998c3e85bdcSAkhil Goyal 			     internals->max_nb_queue_pairs);
1999c3e85bdcSAkhil Goyal 		return -EINVAL;
2000c3e85bdcSAkhil Goyal 	}
2001c3e85bdcSAkhil Goyal 
2002c3e85bdcSAkhil Goyal 	qp = &internals->qps[qp_id];
20032ffb940eSAkhil Goyal 	rte_mempool_free(qp->ctx_pool);
2004c3e85bdcSAkhil Goyal 	qp->internals = NULL;
2005c3e85bdcSAkhil Goyal 	dev->data->queue_pairs[qp_id] = NULL;
2006c3e85bdcSAkhil Goyal 
2007c3e85bdcSAkhil Goyal 	return 0;
2008c3e85bdcSAkhil Goyal }
2009c3e85bdcSAkhil Goyal 
2010c3e85bdcSAkhil Goyal /** Setup a queue pair */
2011c3e85bdcSAkhil Goyal static int
2012c3e85bdcSAkhil Goyal dpaa_sec_queue_pair_setup(struct rte_cryptodev *dev, uint16_t qp_id,
2013c3e85bdcSAkhil Goyal 		__rte_unused const struct rte_cryptodev_qp_conf *qp_conf,
2014725d2a7fSFan Zhang 		__rte_unused int socket_id)
2015c3e85bdcSAkhil Goyal {
2016c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals;
2017c3e85bdcSAkhil Goyal 	struct dpaa_sec_qp *qp = NULL;
20182ffb940eSAkhil Goyal 	char str[20];
2019c3e85bdcSAkhil Goyal 
2020f163231eSHemant Agrawal 	DPAA_SEC_DEBUG("dev =%p, queue =%d, conf =%p", dev, qp_id, qp_conf);
2021c3e85bdcSAkhil Goyal 
2022c3e85bdcSAkhil Goyal 	internals = dev->data->dev_private;
2023c3e85bdcSAkhil Goyal 	if (qp_id >= internals->max_nb_queue_pairs) {
2024f163231eSHemant Agrawal 		DPAA_SEC_ERR("Max supported qpid %d",
2025c3e85bdcSAkhil Goyal 			     internals->max_nb_queue_pairs);
2026c3e85bdcSAkhil Goyal 		return -EINVAL;
2027c3e85bdcSAkhil Goyal 	}
2028c3e85bdcSAkhil Goyal 
2029c3e85bdcSAkhil Goyal 	qp = &internals->qps[qp_id];
2030c3e85bdcSAkhil Goyal 	qp->internals = internals;
20312ffb940eSAkhil Goyal 	snprintf(str, sizeof(str), "ctx_pool_d%d_qp%d",
20322ffb940eSAkhil Goyal 			dev->data->dev_id, qp_id);
20332ffb940eSAkhil Goyal 	if (!qp->ctx_pool) {
20342ffb940eSAkhil Goyal 		qp->ctx_pool = rte_mempool_create((const char *)str,
20352ffb940eSAkhil Goyal 							CTX_POOL_NUM_BUFS,
20362ffb940eSAkhil Goyal 							CTX_POOL_BUF_SIZE,
20372ffb940eSAkhil Goyal 							CTX_POOL_CACHE_SIZE, 0,
20382ffb940eSAkhil Goyal 							NULL, NULL, NULL, NULL,
20392ffb940eSAkhil Goyal 							SOCKET_ID_ANY, 0);
20402ffb940eSAkhil Goyal 		if (!qp->ctx_pool) {
20412ffb940eSAkhil Goyal 			DPAA_SEC_ERR("%s create failed\n", str);
20422ffb940eSAkhil Goyal 			return -ENOMEM;
20432ffb940eSAkhil Goyal 		}
20442ffb940eSAkhil Goyal 	} else
20452ffb940eSAkhil Goyal 		DPAA_SEC_INFO("mempool already created for dev_id : %d, qp: %d",
20462ffb940eSAkhil Goyal 				dev->data->dev_id, qp_id);
2047c3e85bdcSAkhil Goyal 	dev->data->queue_pairs[qp_id] = qp;
2048c3e85bdcSAkhil Goyal 
2049c3e85bdcSAkhil Goyal 	return 0;
2050c3e85bdcSAkhil Goyal }
2051c3e85bdcSAkhil Goyal 
2052c3e85bdcSAkhil Goyal /** Returns the size of session structure */
2053c3e85bdcSAkhil Goyal static unsigned int
2054012c5076SPablo de Lara dpaa_sec_sym_session_get_size(struct rte_cryptodev *dev __rte_unused)
2055c3e85bdcSAkhil Goyal {
2056c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2057c3e85bdcSAkhil Goyal 
2058c3e85bdcSAkhil Goyal 	return sizeof(dpaa_sec_session);
2059c3e85bdcSAkhil Goyal }
2060c3e85bdcSAkhil Goyal 
2061c3e85bdcSAkhil Goyal static int
2062c3e85bdcSAkhil Goyal dpaa_sec_cipher_init(struct rte_cryptodev *dev __rte_unused,
2063c3e85bdcSAkhil Goyal 		     struct rte_crypto_sym_xform *xform,
2064c3e85bdcSAkhil Goyal 		     dpaa_sec_session *session)
2065c3e85bdcSAkhil Goyal {
2066f73d6928SHemant Agrawal 	session->ctxt = DPAA_SEC_CIPHER;
2067c3e85bdcSAkhil Goyal 	session->cipher_alg = xform->cipher.algo;
2068c3e85bdcSAkhil Goyal 	session->iv.length = xform->cipher.iv.length;
2069c3e85bdcSAkhil Goyal 	session->iv.offset = xform->cipher.iv.offset;
2070c3e85bdcSAkhil Goyal 	session->cipher_key.data = rte_zmalloc(NULL, xform->cipher.key.length,
2071c3e85bdcSAkhil Goyal 					       RTE_CACHE_LINE_SIZE);
2072c3e85bdcSAkhil Goyal 	if (session->cipher_key.data == NULL && xform->cipher.key.length > 0) {
2073f163231eSHemant Agrawal 		DPAA_SEC_ERR("No Memory for cipher key");
2074c3e85bdcSAkhil Goyal 		return -ENOMEM;
2075c3e85bdcSAkhil Goyal 	}
2076c3e85bdcSAkhil Goyal 	session->cipher_key.length = xform->cipher.key.length;
2077c3e85bdcSAkhil Goyal 
2078c3e85bdcSAkhil Goyal 	memcpy(session->cipher_key.data, xform->cipher.key.data,
2079c3e85bdcSAkhil Goyal 	       xform->cipher.key.length);
20808524b44eSHemant Agrawal 	switch (xform->cipher.algo) {
20818524b44eSHemant Agrawal 	case RTE_CRYPTO_CIPHER_AES_CBC:
20828524b44eSHemant Agrawal 		session->cipher_key.alg = OP_ALG_ALGSEL_AES;
20838524b44eSHemant Agrawal 		session->cipher_key.algmode = OP_ALG_AAI_CBC;
20848524b44eSHemant Agrawal 		break;
20853e4fbc6cSGagandeep Singh 	case RTE_CRYPTO_CIPHER_DES_CBC:
20863e4fbc6cSGagandeep Singh 		session->cipher_key.alg = OP_ALG_ALGSEL_DES;
20873e4fbc6cSGagandeep Singh 		session->cipher_key.algmode = OP_ALG_AAI_CBC;
20883e4fbc6cSGagandeep Singh 		break;
20898524b44eSHemant Agrawal 	case RTE_CRYPTO_CIPHER_3DES_CBC:
20908524b44eSHemant Agrawal 		session->cipher_key.alg = OP_ALG_ALGSEL_3DES;
20918524b44eSHemant Agrawal 		session->cipher_key.algmode = OP_ALG_AAI_CBC;
20928524b44eSHemant Agrawal 		break;
20938524b44eSHemant Agrawal 	case RTE_CRYPTO_CIPHER_AES_CTR:
20948524b44eSHemant Agrawal 		session->cipher_key.alg = OP_ALG_ALGSEL_AES;
20958524b44eSHemant Agrawal 		session->cipher_key.algmode = OP_ALG_AAI_CTR;
20968524b44eSHemant Agrawal 		break;
20978524b44eSHemant Agrawal 	case RTE_CRYPTO_CIPHER_SNOW3G_UEA2:
20988524b44eSHemant Agrawal 		session->cipher_key.alg = OP_ALG_ALGSEL_SNOW_F8;
20998524b44eSHemant Agrawal 		break;
21008524b44eSHemant Agrawal 	case RTE_CRYPTO_CIPHER_ZUC_EEA3:
21018524b44eSHemant Agrawal 		session->cipher_key.alg = OP_ALG_ALGSEL_ZUCE;
21028524b44eSHemant Agrawal 		break;
21038524b44eSHemant Agrawal 	default:
21048524b44eSHemant Agrawal 		DPAA_SEC_ERR("Crypto: Undefined Cipher specified %u",
21058524b44eSHemant Agrawal 			      xform->cipher.algo);
2106c08ced9aSAkhil Goyal 		return -ENOTSUP;
21078524b44eSHemant Agrawal 	}
2108c3e85bdcSAkhil Goyal 	session->dir = (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
2109c3e85bdcSAkhil Goyal 			DIR_ENC : DIR_DEC;
2110c3e85bdcSAkhil Goyal 
2111c3e85bdcSAkhil Goyal 	return 0;
2112c3e85bdcSAkhil Goyal }
2113c3e85bdcSAkhil Goyal 
2114c3e85bdcSAkhil Goyal static int
2115c3e85bdcSAkhil Goyal dpaa_sec_auth_init(struct rte_cryptodev *dev __rte_unused,
2116c3e85bdcSAkhil Goyal 		   struct rte_crypto_sym_xform *xform,
2117c3e85bdcSAkhil Goyal 		   dpaa_sec_session *session)
2118c3e85bdcSAkhil Goyal {
2119f73d6928SHemant Agrawal 	session->ctxt = DPAA_SEC_AUTH;
2120c3e85bdcSAkhil Goyal 	session->auth_alg = xform->auth.algo;
21214c42352cSGagandeep Singh 	session->auth_key.length = xform->auth.key.length;
21224c42352cSGagandeep Singh 	if (xform->auth.key.length) {
21234c42352cSGagandeep Singh 		session->auth_key.data =
21244c42352cSGagandeep Singh 				rte_zmalloc(NULL, xform->auth.key.length,
2125c3e85bdcSAkhil Goyal 					     RTE_CACHE_LINE_SIZE);
21264c42352cSGagandeep Singh 		if (session->auth_key.data == NULL) {
2127f163231eSHemant Agrawal 			DPAA_SEC_ERR("No Memory for auth key");
2128c3e85bdcSAkhil Goyal 			return -ENOMEM;
2129c3e85bdcSAkhil Goyal 		}
21304c42352cSGagandeep Singh 		memcpy(session->auth_key.data, xform->auth.key.data,
21314c42352cSGagandeep Singh 				xform->auth.key.length);
21324c42352cSGagandeep Singh 
21334c42352cSGagandeep Singh 	}
2134c3e85bdcSAkhil Goyal 	session->digest_length = xform->auth.digest_length;
2135c5788a10SHemant Agrawal 	if (session->cipher_alg == RTE_CRYPTO_CIPHER_NULL) {
2136c5788a10SHemant Agrawal 		session->iv.offset = xform->auth.iv.offset;
2137c5788a10SHemant Agrawal 		session->iv.length = xform->auth.iv.length;
2138c5788a10SHemant Agrawal 	}
2139c3e85bdcSAkhil Goyal 
21408524b44eSHemant Agrawal 	switch (xform->auth.algo) {
21414c42352cSGagandeep Singh 	case RTE_CRYPTO_AUTH_SHA1:
21424c42352cSGagandeep Singh 		session->auth_key.alg = OP_ALG_ALGSEL_SHA1;
21434c42352cSGagandeep Singh 		session->auth_key.algmode = OP_ALG_AAI_HASH;
21444c42352cSGagandeep Singh 		break;
21458524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_SHA1_HMAC:
21468524b44eSHemant Agrawal 		session->auth_key.alg = OP_ALG_ALGSEL_SHA1;
21478524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
21488524b44eSHemant Agrawal 		break;
21494c42352cSGagandeep Singh 	case RTE_CRYPTO_AUTH_MD5:
21504c42352cSGagandeep Singh 		session->auth_key.alg = OP_ALG_ALGSEL_MD5;
21514c42352cSGagandeep Singh 		session->auth_key.algmode = OP_ALG_AAI_HASH;
21524c42352cSGagandeep Singh 		break;
21538524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_MD5_HMAC:
21548524b44eSHemant Agrawal 		session->auth_key.alg = OP_ALG_ALGSEL_MD5;
21558524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
21568524b44eSHemant Agrawal 		break;
21574c42352cSGagandeep Singh 	case RTE_CRYPTO_AUTH_SHA224:
21584c42352cSGagandeep Singh 		session->auth_key.alg = OP_ALG_ALGSEL_SHA224;
21594c42352cSGagandeep Singh 		session->auth_key.algmode = OP_ALG_AAI_HASH;
21604c42352cSGagandeep Singh 		break;
21618524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_SHA224_HMAC:
21628524b44eSHemant Agrawal 		session->auth_key.alg = OP_ALG_ALGSEL_SHA224;
21638524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
21648524b44eSHemant Agrawal 		break;
21654c42352cSGagandeep Singh 	case RTE_CRYPTO_AUTH_SHA256:
21664c42352cSGagandeep Singh 		session->auth_key.alg = OP_ALG_ALGSEL_SHA256;
21674c42352cSGagandeep Singh 		session->auth_key.algmode = OP_ALG_AAI_HASH;
21684c42352cSGagandeep Singh 		break;
21698524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_SHA256_HMAC:
21708524b44eSHemant Agrawal 		session->auth_key.alg = OP_ALG_ALGSEL_SHA256;
21718524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
21728524b44eSHemant Agrawal 		break;
21734c42352cSGagandeep Singh 	case RTE_CRYPTO_AUTH_SHA384:
21744c42352cSGagandeep Singh 		session->auth_key.alg = OP_ALG_ALGSEL_SHA384;
21754c42352cSGagandeep Singh 		session->auth_key.algmode = OP_ALG_AAI_HASH;
21764c42352cSGagandeep Singh 		break;
21778524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_SHA384_HMAC:
21788524b44eSHemant Agrawal 		session->auth_key.alg = OP_ALG_ALGSEL_SHA384;
21798524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
21808524b44eSHemant Agrawal 		break;
21814c42352cSGagandeep Singh 	case RTE_CRYPTO_AUTH_SHA512:
21824c42352cSGagandeep Singh 		session->auth_key.alg = OP_ALG_ALGSEL_SHA512;
21834c42352cSGagandeep Singh 		session->auth_key.algmode = OP_ALG_AAI_HASH;
21844c42352cSGagandeep Singh 		break;
21858524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_SHA512_HMAC:
21868524b44eSHemant Agrawal 		session->auth_key.alg = OP_ALG_ALGSEL_SHA512;
21878524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
21888524b44eSHemant Agrawal 		break;
21898524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
21908524b44eSHemant Agrawal 		session->auth_key.alg = OP_ALG_ALGSEL_SNOW_F9;
21918524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_F9;
21928524b44eSHemant Agrawal 		break;
21938524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_ZUC_EIA3:
21948524b44eSHemant Agrawal 		session->auth_key.alg = OP_ALG_ALGSEL_ZUCA;
21958524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_F9;
21968524b44eSHemant Agrawal 		break;
219766f95673SGagandeep Singh 	case RTE_CRYPTO_AUTH_AES_XCBC_MAC:
219866f95673SGagandeep Singh 		session->auth_key.alg = OP_ALG_ALGSEL_AES;
219966f95673SGagandeep Singh 		session->auth_key.algmode = OP_ALG_AAI_XCBC_MAC;
220066f95673SGagandeep Singh 		break;
22012ed12d9bSGagandeep Singh 	case RTE_CRYPTO_AUTH_AES_CMAC:
22022ed12d9bSGagandeep Singh 		session->auth_key.alg = OP_ALG_ALGSEL_AES;
22032ed12d9bSGagandeep Singh 		session->auth_key.algmode = OP_ALG_AAI_CMAC;
22042ed12d9bSGagandeep Singh 		break;
22058524b44eSHemant Agrawal 	default:
22068524b44eSHemant Agrawal 		DPAA_SEC_ERR("Crypto: Unsupported Auth specified %u",
22078524b44eSHemant Agrawal 			      xform->auth.algo);
2208c08ced9aSAkhil Goyal 		return -ENOTSUP;
22098524b44eSHemant Agrawal 	}
22108524b44eSHemant Agrawal 
2211c3e85bdcSAkhil Goyal 	session->dir = (xform->auth.op == RTE_CRYPTO_AUTH_OP_GENERATE) ?
2212c3e85bdcSAkhil Goyal 			DIR_ENC : DIR_DEC;
2213c3e85bdcSAkhil Goyal 
2214c3e85bdcSAkhil Goyal 	return 0;
2215c3e85bdcSAkhil Goyal }
2216c3e85bdcSAkhil Goyal 
2217c3e85bdcSAkhil Goyal static int
22188524b44eSHemant Agrawal dpaa_sec_chain_init(struct rte_cryptodev *dev __rte_unused,
22198524b44eSHemant Agrawal 		   struct rte_crypto_sym_xform *xform,
22208524b44eSHemant Agrawal 		   dpaa_sec_session *session)
22218524b44eSHemant Agrawal {
22228524b44eSHemant Agrawal 
22238524b44eSHemant Agrawal 	struct rte_crypto_cipher_xform *cipher_xform;
22248524b44eSHemant Agrawal 	struct rte_crypto_auth_xform *auth_xform;
22258524b44eSHemant Agrawal 
2226f73d6928SHemant Agrawal 	session->ctxt = DPAA_SEC_CIPHER_HASH;
22278524b44eSHemant Agrawal 	if (session->auth_cipher_text) {
22288524b44eSHemant Agrawal 		cipher_xform = &xform->cipher;
22298524b44eSHemant Agrawal 		auth_xform = &xform->next->auth;
22308524b44eSHemant Agrawal 	} else {
22318524b44eSHemant Agrawal 		cipher_xform = &xform->next->cipher;
22328524b44eSHemant Agrawal 		auth_xform = &xform->auth;
22338524b44eSHemant Agrawal 	}
22348524b44eSHemant Agrawal 
22358524b44eSHemant Agrawal 	/* Set IV parameters */
22368524b44eSHemant Agrawal 	session->iv.offset = cipher_xform->iv.offset;
22378524b44eSHemant Agrawal 	session->iv.length = cipher_xform->iv.length;
22388524b44eSHemant Agrawal 
22398524b44eSHemant Agrawal 	session->cipher_key.data = rte_zmalloc(NULL, cipher_xform->key.length,
22408524b44eSHemant Agrawal 					       RTE_CACHE_LINE_SIZE);
22418524b44eSHemant Agrawal 	if (session->cipher_key.data == NULL && cipher_xform->key.length > 0) {
22428524b44eSHemant Agrawal 		DPAA_SEC_ERR("No Memory for cipher key");
2243c08ced9aSAkhil Goyal 		return -ENOMEM;
22448524b44eSHemant Agrawal 	}
22458524b44eSHemant Agrawal 	session->cipher_key.length = cipher_xform->key.length;
22468524b44eSHemant Agrawal 	session->auth_key.data = rte_zmalloc(NULL, auth_xform->key.length,
22478524b44eSHemant Agrawal 					     RTE_CACHE_LINE_SIZE);
22488524b44eSHemant Agrawal 	if (session->auth_key.data == NULL && auth_xform->key.length > 0) {
22498524b44eSHemant Agrawal 		DPAA_SEC_ERR("No Memory for auth key");
22508524b44eSHemant Agrawal 		return -ENOMEM;
22518524b44eSHemant Agrawal 	}
22528524b44eSHemant Agrawal 	session->auth_key.length = auth_xform->key.length;
22538524b44eSHemant Agrawal 	memcpy(session->cipher_key.data, cipher_xform->key.data,
22548524b44eSHemant Agrawal 	       cipher_xform->key.length);
22558524b44eSHemant Agrawal 	memcpy(session->auth_key.data, auth_xform->key.data,
22568524b44eSHemant Agrawal 	       auth_xform->key.length);
22578524b44eSHemant Agrawal 
22588524b44eSHemant Agrawal 	session->digest_length = auth_xform->digest_length;
22598524b44eSHemant Agrawal 	session->auth_alg = auth_xform->algo;
22608524b44eSHemant Agrawal 
22618524b44eSHemant Agrawal 	switch (auth_xform->algo) {
22628524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_SHA1_HMAC:
22638524b44eSHemant Agrawal 		session->auth_key.alg = OP_ALG_ALGSEL_SHA1;
22648524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
22658524b44eSHemant Agrawal 		break;
22668524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_MD5_HMAC:
22678524b44eSHemant Agrawal 		session->auth_key.alg = OP_ALG_ALGSEL_MD5;
22688524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
22698524b44eSHemant Agrawal 		break;
22708524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_SHA224_HMAC:
22718524b44eSHemant Agrawal 		session->auth_key.alg = OP_ALG_ALGSEL_SHA224;
22728524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
22738524b44eSHemant Agrawal 		break;
22748524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_SHA256_HMAC:
22758524b44eSHemant Agrawal 		session->auth_key.alg = OP_ALG_ALGSEL_SHA256;
22768524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
22778524b44eSHemant Agrawal 		break;
22788524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_SHA384_HMAC:
22798524b44eSHemant Agrawal 		session->auth_key.alg = OP_ALG_ALGSEL_SHA384;
22808524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
22818524b44eSHemant Agrawal 		break;
22828524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_SHA512_HMAC:
22838524b44eSHemant Agrawal 		session->auth_key.alg = OP_ALG_ALGSEL_SHA512;
22848524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
22858524b44eSHemant Agrawal 		break;
228666f95673SGagandeep Singh 	case RTE_CRYPTO_AUTH_AES_XCBC_MAC:
228766f95673SGagandeep Singh 		session->auth_key.alg = OP_ALG_ALGSEL_AES;
228866f95673SGagandeep Singh 		session->auth_key.algmode = OP_ALG_AAI_XCBC_MAC;
228966f95673SGagandeep Singh 		break;
22902ed12d9bSGagandeep Singh 	case RTE_CRYPTO_AUTH_AES_CMAC:
22912ed12d9bSGagandeep Singh 		session->auth_key.alg = OP_ALG_ALGSEL_AES;
22922ed12d9bSGagandeep Singh 		session->auth_key.algmode = OP_ALG_AAI_CMAC;
22932ed12d9bSGagandeep Singh 		break;
22948524b44eSHemant Agrawal 	default:
22958524b44eSHemant Agrawal 		DPAA_SEC_ERR("Crypto: Unsupported Auth specified %u",
22968524b44eSHemant Agrawal 			      auth_xform->algo);
2297c08ced9aSAkhil Goyal 		return -ENOTSUP;
22988524b44eSHemant Agrawal 	}
22998524b44eSHemant Agrawal 
23008524b44eSHemant Agrawal 	session->cipher_alg = cipher_xform->algo;
23018524b44eSHemant Agrawal 
23028524b44eSHemant Agrawal 	switch (cipher_xform->algo) {
23038524b44eSHemant Agrawal 	case RTE_CRYPTO_CIPHER_AES_CBC:
23048524b44eSHemant Agrawal 		session->cipher_key.alg = OP_ALG_ALGSEL_AES;
23058524b44eSHemant Agrawal 		session->cipher_key.algmode = OP_ALG_AAI_CBC;
23068524b44eSHemant Agrawal 		break;
23073e4fbc6cSGagandeep Singh 	case RTE_CRYPTO_CIPHER_DES_CBC:
23083e4fbc6cSGagandeep Singh 		session->cipher_key.alg = OP_ALG_ALGSEL_DES;
23093e4fbc6cSGagandeep Singh 		session->cipher_key.algmode = OP_ALG_AAI_CBC;
23103e4fbc6cSGagandeep Singh 		break;
23118524b44eSHemant Agrawal 	case RTE_CRYPTO_CIPHER_3DES_CBC:
23128524b44eSHemant Agrawal 		session->cipher_key.alg = OP_ALG_ALGSEL_3DES;
23138524b44eSHemant Agrawal 		session->cipher_key.algmode = OP_ALG_AAI_CBC;
23148524b44eSHemant Agrawal 		break;
23158524b44eSHemant Agrawal 	case RTE_CRYPTO_CIPHER_AES_CTR:
23168524b44eSHemant Agrawal 		session->cipher_key.alg = OP_ALG_ALGSEL_AES;
23178524b44eSHemant Agrawal 		session->cipher_key.algmode = OP_ALG_AAI_CTR;
23188524b44eSHemant Agrawal 		break;
23198524b44eSHemant Agrawal 	default:
23208524b44eSHemant Agrawal 		DPAA_SEC_ERR("Crypto: Undefined Cipher specified %u",
23218524b44eSHemant Agrawal 			      cipher_xform->algo);
2322c08ced9aSAkhil Goyal 		return -ENOTSUP;
23238524b44eSHemant Agrawal 	}
23248524b44eSHemant Agrawal 	session->dir = (cipher_xform->op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
23258524b44eSHemant Agrawal 				DIR_ENC : DIR_DEC;
23268524b44eSHemant Agrawal 	return 0;
23278524b44eSHemant Agrawal }
23288524b44eSHemant Agrawal 
23298524b44eSHemant Agrawal static int
2330c3e85bdcSAkhil Goyal dpaa_sec_aead_init(struct rte_cryptodev *dev __rte_unused,
2331c3e85bdcSAkhil Goyal 		   struct rte_crypto_sym_xform *xform,
2332c3e85bdcSAkhil Goyal 		   dpaa_sec_session *session)
2333c3e85bdcSAkhil Goyal {
2334c3e85bdcSAkhil Goyal 	session->aead_alg = xform->aead.algo;
23358524b44eSHemant Agrawal 	session->ctxt = DPAA_SEC_AEAD;
2336c3e85bdcSAkhil Goyal 	session->iv.length = xform->aead.iv.length;
2337c3e85bdcSAkhil Goyal 	session->iv.offset = xform->aead.iv.offset;
2338c3e85bdcSAkhil Goyal 	session->auth_only_len = xform->aead.aad_length;
2339c3e85bdcSAkhil Goyal 	session->aead_key.data = rte_zmalloc(NULL, xform->aead.key.length,
2340c3e85bdcSAkhil Goyal 					     RTE_CACHE_LINE_SIZE);
2341c3e85bdcSAkhil Goyal 	if (session->aead_key.data == NULL && xform->aead.key.length > 0) {
2342f163231eSHemant Agrawal 		DPAA_SEC_ERR("No Memory for aead key\n");
2343c3e85bdcSAkhil Goyal 		return -ENOMEM;
2344c3e85bdcSAkhil Goyal 	}
2345c3e85bdcSAkhil Goyal 	session->aead_key.length = xform->aead.key.length;
2346c3e85bdcSAkhil Goyal 	session->digest_length = xform->aead.digest_length;
2347c3e85bdcSAkhil Goyal 
2348c3e85bdcSAkhil Goyal 	memcpy(session->aead_key.data, xform->aead.key.data,
2349c3e85bdcSAkhil Goyal 	       xform->aead.key.length);
23508524b44eSHemant Agrawal 
23518524b44eSHemant Agrawal 	switch (session->aead_alg) {
23528524b44eSHemant Agrawal 	case RTE_CRYPTO_AEAD_AES_GCM:
23538524b44eSHemant Agrawal 		session->aead_key.alg = OP_ALG_ALGSEL_AES;
23548524b44eSHemant Agrawal 		session->aead_key.algmode = OP_ALG_AAI_GCM;
23558524b44eSHemant Agrawal 		break;
23568524b44eSHemant Agrawal 	default:
23578524b44eSHemant Agrawal 		DPAA_SEC_ERR("unsupported AEAD alg %d", session->aead_alg);
2358c08ced9aSAkhil Goyal 		return -ENOTSUP;
23598524b44eSHemant Agrawal 	}
23608524b44eSHemant Agrawal 
2361c3e85bdcSAkhil Goyal 	session->dir = (xform->aead.op == RTE_CRYPTO_AEAD_OP_ENCRYPT) ?
2362c3e85bdcSAkhil Goyal 			DIR_ENC : DIR_DEC;
2363c3e85bdcSAkhil Goyal 
2364c3e85bdcSAkhil Goyal 	return 0;
2365c3e85bdcSAkhil Goyal }
2366c3e85bdcSAkhil Goyal 
2367e79416d1SHemant Agrawal static struct qman_fq *
2368e79416d1SHemant Agrawal dpaa_sec_attach_rxq(struct dpaa_sec_dev_private *qi)
2369c3e85bdcSAkhil Goyal {
2370e79416d1SHemant Agrawal 	unsigned int i;
2371c3e85bdcSAkhil Goyal 
2372fd900d38SGagandeep Singh 	for (i = 0; i < RTE_DPAA_MAX_RX_QUEUE; i++) {
2373e79416d1SHemant Agrawal 		if (qi->inq_attach[i] == 0) {
2374e79416d1SHemant Agrawal 			qi->inq_attach[i] = 1;
2375e79416d1SHemant Agrawal 			return &qi->inq[i];
2376e79416d1SHemant Agrawal 		}
2377e79416d1SHemant Agrawal 	}
2378e621d970SAkhil Goyal 	DPAA_SEC_WARN("All session in use %u", qi->max_nb_sessions);
2379c3e85bdcSAkhil Goyal 
2380e79416d1SHemant Agrawal 	return NULL;
2381c3e85bdcSAkhil Goyal }
2382c3e85bdcSAkhil Goyal 
2383e79416d1SHemant Agrawal static int
2384e79416d1SHemant Agrawal dpaa_sec_detach_rxq(struct dpaa_sec_dev_private *qi, struct qman_fq *fq)
2385e79416d1SHemant Agrawal {
2386e79416d1SHemant Agrawal 	unsigned int i;
2387e79416d1SHemant Agrawal 
2388fd900d38SGagandeep Singh 	for (i = 0; i < RTE_DPAA_MAX_RX_QUEUE; i++) {
2389e79416d1SHemant Agrawal 		if (&qi->inq[i] == fq) {
2390fd900d38SGagandeep Singh 			if (qman_retire_fq(fq, NULL) != 0)
2391626c7a58SHemant Agrawal 				DPAA_SEC_DEBUG("Queue is not retired\n");
2392b4053c4bSAlok Makhariya 			qman_oos_fq(fq);
2393e79416d1SHemant Agrawal 			qi->inq_attach[i] = 0;
2394e79416d1SHemant Agrawal 			return 0;
2395e79416d1SHemant Agrawal 		}
2396e79416d1SHemant Agrawal 	}
2397e79416d1SHemant Agrawal 	return -1;
2398e79416d1SHemant Agrawal }
2399e79416d1SHemant Agrawal 
24009d5f73c2SGagandeep Singh int
2401e79416d1SHemant Agrawal dpaa_sec_attach_sess_q(struct dpaa_sec_qp *qp, dpaa_sec_session *sess)
2402e79416d1SHemant Agrawal {
2403e79416d1SHemant Agrawal 	int ret;
2404e79416d1SHemant Agrawal 
24054e694fe5SAkhil Goyal 	sess->qp[rte_lcore_id() % MAX_DPAA_CORES] = qp;
2406e79416d1SHemant Agrawal 	ret = dpaa_sec_prep_cdb(sess);
2407e79416d1SHemant Agrawal 	if (ret) {
2408f163231eSHemant Agrawal 		DPAA_SEC_ERR("Unable to prepare sec cdb");
2409c08ced9aSAkhil Goyal 		return ret;
2410e79416d1SHemant Agrawal 	}
2411e5872221SRohit Raj 	if (unlikely(!DPAA_PER_LCORE_PORTAL)) {
24125b0f1bd3SAshish Jain 		ret = rte_dpaa_portal_init((void *)0);
24135b0f1bd3SAshish Jain 		if (ret) {
2414f163231eSHemant Agrawal 			DPAA_SEC_ERR("Failure in affining portal");
24155b0f1bd3SAshish Jain 			return ret;
24165b0f1bd3SAshish Jain 		}
24175b0f1bd3SAshish Jain 	}
24184e694fe5SAkhil Goyal 	ret = dpaa_sec_init_rx(sess->inq[rte_lcore_id() % MAX_DPAA_CORES],
2419ec861560SGagandeep Singh 			       rte_dpaa_mem_vtop(&sess->cdb),
2420e79416d1SHemant Agrawal 			       qman_fq_fqid(&qp->outq));
2421e79416d1SHemant Agrawal 	if (ret)
2422f163231eSHemant Agrawal 		DPAA_SEC_ERR("Unable to init sec queue");
2423e79416d1SHemant Agrawal 
2424e79416d1SHemant Agrawal 	return ret;
2425c3e85bdcSAkhil Goyal }
2426c3e85bdcSAkhil Goyal 
24276290de2cSLukasz Wojciechowski static inline void
24286290de2cSLukasz Wojciechowski free_session_data(dpaa_sec_session *s)
24296290de2cSLukasz Wojciechowski {
24306290de2cSLukasz Wojciechowski 	if (is_aead(s))
24316290de2cSLukasz Wojciechowski 		rte_free(s->aead_key.data);
24326290de2cSLukasz Wojciechowski 	else {
24336290de2cSLukasz Wojciechowski 		rte_free(s->auth_key.data);
24346290de2cSLukasz Wojciechowski 		rte_free(s->cipher_key.data);
24356290de2cSLukasz Wojciechowski 	}
24366290de2cSLukasz Wojciechowski 	memset(s, 0, sizeof(dpaa_sec_session));
24376290de2cSLukasz Wojciechowski }
24386290de2cSLukasz Wojciechowski 
2439c3e85bdcSAkhil Goyal static int
2440c3e85bdcSAkhil Goyal dpaa_sec_set_session_parameters(struct rte_cryptodev *dev,
2441c3e85bdcSAkhil Goyal 			    struct rte_crypto_sym_xform *xform,	void *sess)
2442c3e85bdcSAkhil Goyal {
2443c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals = dev->data->dev_private;
2444c3e85bdcSAkhil Goyal 	dpaa_sec_session *session = sess;
24454e694fe5SAkhil Goyal 	uint32_t i;
2446f73d6928SHemant Agrawal 	int ret;
2447c3e85bdcSAkhil Goyal 
2448c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2449c3e85bdcSAkhil Goyal 
2450c3e85bdcSAkhil Goyal 	if (unlikely(sess == NULL)) {
2451f163231eSHemant Agrawal 		DPAA_SEC_ERR("invalid session struct");
2452c3e85bdcSAkhil Goyal 		return -EINVAL;
2453c3e85bdcSAkhil Goyal 	}
2454b0894102SAkhil Goyal 	memset(session, 0, sizeof(dpaa_sec_session));
2455c3e85bdcSAkhil Goyal 
2456c3e85bdcSAkhil Goyal 	/* Default IV length = 0 */
2457c3e85bdcSAkhil Goyal 	session->iv.length = 0;
2458c3e85bdcSAkhil Goyal 
2459c3e85bdcSAkhil Goyal 	/* Cipher Only */
2460c3e85bdcSAkhil Goyal 	if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER && xform->next == NULL) {
2461c3e85bdcSAkhil Goyal 		session->auth_alg = RTE_CRYPTO_AUTH_NULL;
2462f73d6928SHemant Agrawal 		ret = dpaa_sec_cipher_init(dev, xform, session);
2463c3e85bdcSAkhil Goyal 
2464c3e85bdcSAkhil Goyal 	/* Authentication Only */
2465c3e85bdcSAkhil Goyal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
2466c3e85bdcSAkhil Goyal 		   xform->next == NULL) {
2467c3e85bdcSAkhil Goyal 		session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
24688524b44eSHemant Agrawal 		session->ctxt = DPAA_SEC_AUTH;
2469f73d6928SHemant Agrawal 		ret = dpaa_sec_auth_init(dev, xform, session);
2470c3e85bdcSAkhil Goyal 
2471c3e85bdcSAkhil Goyal 	/* Cipher then Authenticate */
2472c3e85bdcSAkhil Goyal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
2473c3e85bdcSAkhil Goyal 		   xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
2474c3e85bdcSAkhil Goyal 		if (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) {
24758524b44eSHemant Agrawal 			session->auth_cipher_text = 1;
2476f73d6928SHemant Agrawal 			if (xform->cipher.algo == RTE_CRYPTO_CIPHER_NULL)
2477f73d6928SHemant Agrawal 				ret = dpaa_sec_auth_init(dev, xform, session);
2478f73d6928SHemant Agrawal 			else if (xform->next->auth.algo == RTE_CRYPTO_AUTH_NULL)
2479f73d6928SHemant Agrawal 				ret = dpaa_sec_cipher_init(dev, xform, session);
2480f73d6928SHemant Agrawal 			else
2481f73d6928SHemant Agrawal 				ret = dpaa_sec_chain_init(dev, xform, session);
2482c3e85bdcSAkhil Goyal 		} else {
2483f163231eSHemant Agrawal 			DPAA_SEC_ERR("Not supported: Auth then Cipher");
2484c08ced9aSAkhil Goyal 			return -ENOTSUP;
2485c3e85bdcSAkhil Goyal 		}
2486c3e85bdcSAkhil Goyal 	/* Authenticate then Cipher */
2487c3e85bdcSAkhil Goyal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
2488c3e85bdcSAkhil Goyal 		   xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
2489c3e85bdcSAkhil Goyal 		if (xform->next->cipher.op == RTE_CRYPTO_CIPHER_OP_DECRYPT) {
24908524b44eSHemant Agrawal 			session->auth_cipher_text = 0;
2491f73d6928SHemant Agrawal 			if (xform->auth.algo == RTE_CRYPTO_AUTH_NULL)
2492f73d6928SHemant Agrawal 				ret = dpaa_sec_cipher_init(dev, xform, session);
2493f73d6928SHemant Agrawal 			else if (xform->next->cipher.algo
2494f73d6928SHemant Agrawal 					== RTE_CRYPTO_CIPHER_NULL)
2495f73d6928SHemant Agrawal 				ret = dpaa_sec_auth_init(dev, xform, session);
2496f73d6928SHemant Agrawal 			else
2497f73d6928SHemant Agrawal 				ret = dpaa_sec_chain_init(dev, xform, session);
2498c3e85bdcSAkhil Goyal 		} else {
2499f163231eSHemant Agrawal 			DPAA_SEC_ERR("Not supported: Auth then Cipher");
2500c08ced9aSAkhil Goyal 			return -ENOTSUP;
2501c3e85bdcSAkhil Goyal 		}
2502c3e85bdcSAkhil Goyal 
2503c3e85bdcSAkhil Goyal 	/* AEAD operation for AES-GCM kind of Algorithms */
2504c3e85bdcSAkhil Goyal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD &&
2505c3e85bdcSAkhil Goyal 		   xform->next == NULL) {
2506f73d6928SHemant Agrawal 		ret = dpaa_sec_aead_init(dev, xform, session);
2507c3e85bdcSAkhil Goyal 
2508c3e85bdcSAkhil Goyal 	} else {
2509f163231eSHemant Agrawal 		DPAA_SEC_ERR("Invalid crypto type");
2510c3e85bdcSAkhil Goyal 		return -EINVAL;
2511c3e85bdcSAkhil Goyal 	}
2512f73d6928SHemant Agrawal 	if (ret) {
2513f73d6928SHemant Agrawal 		DPAA_SEC_ERR("unable to init session");
2514f73d6928SHemant Agrawal 		goto err1;
2515f73d6928SHemant Agrawal 	}
2516f73d6928SHemant Agrawal 
25173b617ee7SAkhil Goyal 	rte_spinlock_lock(&internals->lock);
25184e694fe5SAkhil Goyal 	for (i = 0; i < MAX_DPAA_CORES; i++) {
25194e694fe5SAkhil Goyal 		session->inq[i] = dpaa_sec_attach_rxq(internals);
25204e694fe5SAkhil Goyal 		if (session->inq[i] == NULL) {
2521f163231eSHemant Agrawal 			DPAA_SEC_ERR("unable to attach sec queue");
25224e694fe5SAkhil Goyal 			rte_spinlock_unlock(&internals->lock);
2523c08ced9aSAkhil Goyal 			ret = -EBUSY;
2524e79416d1SHemant Agrawal 			goto err1;
2525e79416d1SHemant Agrawal 		}
25264e694fe5SAkhil Goyal 	}
25274e694fe5SAkhil Goyal 	rte_spinlock_unlock(&internals->lock);
2528c3e85bdcSAkhil Goyal 
2529c3e85bdcSAkhil Goyal 	return 0;
2530e79416d1SHemant Agrawal 
2531e79416d1SHemant Agrawal err1:
25326290de2cSLukasz Wojciechowski 	free_session_data(session);
2533c08ced9aSAkhil Goyal 	return ret;
2534c3e85bdcSAkhil Goyal }
2535c3e85bdcSAkhil Goyal 
2536c3e85bdcSAkhil Goyal static int
2537012c5076SPablo de Lara dpaa_sec_sym_session_configure(struct rte_cryptodev *dev,
2538c3e85bdcSAkhil Goyal 		struct rte_crypto_sym_xform *xform,
2539c3e85bdcSAkhil Goyal 		struct rte_cryptodev_sym_session *sess,
2540c3e85bdcSAkhil Goyal 		struct rte_mempool *mempool)
2541c3e85bdcSAkhil Goyal {
2542c3e85bdcSAkhil Goyal 	void *sess_private_data;
2543c3e85bdcSAkhil Goyal 	int ret;
2544c3e85bdcSAkhil Goyal 
2545c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2546c3e85bdcSAkhil Goyal 
2547c3e85bdcSAkhil Goyal 	if (rte_mempool_get(mempool, &sess_private_data)) {
2548f163231eSHemant Agrawal 		DPAA_SEC_ERR("Couldn't get object from session mempool");
2549c3e85bdcSAkhil Goyal 		return -ENOMEM;
2550c3e85bdcSAkhil Goyal 	}
2551c3e85bdcSAkhil Goyal 
2552c3e85bdcSAkhil Goyal 	ret = dpaa_sec_set_session_parameters(dev, xform, sess_private_data);
2553c3e85bdcSAkhil Goyal 	if (ret != 0) {
2554f163231eSHemant Agrawal 		DPAA_SEC_ERR("failed to configure session parameters");
2555c3e85bdcSAkhil Goyal 
2556c3e85bdcSAkhil Goyal 		/* Return session to mempool */
2557c3e85bdcSAkhil Goyal 		rte_mempool_put(mempool, sess_private_data);
2558c3e85bdcSAkhil Goyal 		return ret;
2559c3e85bdcSAkhil Goyal 	}
2560c3e85bdcSAkhil Goyal 
2561012c5076SPablo de Lara 	set_sym_session_private_data(sess, dev->driver_id,
2562c3e85bdcSAkhil Goyal 			sess_private_data);
2563c3e85bdcSAkhil Goyal 
2564e79416d1SHemant Agrawal 
2565c3e85bdcSAkhil Goyal 	return 0;
2566c3e85bdcSAkhil Goyal }
2567c3e85bdcSAkhil Goyal 
25683d0d5332SAkhil Goyal static inline void
25693d0d5332SAkhil Goyal free_session_memory(struct rte_cryptodev *dev, dpaa_sec_session *s)
2570c3e85bdcSAkhil Goyal {
2571e79416d1SHemant Agrawal 	struct dpaa_sec_dev_private *qi = dev->data->dev_private;
25723d0d5332SAkhil Goyal 	struct rte_mempool *sess_mp = rte_mempool_from_obj((void *)s);
25733d0d5332SAkhil Goyal 	uint8_t i;
2574e79416d1SHemant Agrawal 
2575e621d970SAkhil Goyal 	for (i = 0; i < MAX_DPAA_CORES; i++) {
2576e621d970SAkhil Goyal 		if (s->inq[i])
2577e621d970SAkhil Goyal 			dpaa_sec_detach_rxq(qi, s->inq[i]);
2578e621d970SAkhil Goyal 		s->inq[i] = NULL;
2579e621d970SAkhil Goyal 		s->qp[i] = NULL;
2580e621d970SAkhil Goyal 	}
25816290de2cSLukasz Wojciechowski 	free_session_data(s);
25823d0d5332SAkhil Goyal 	rte_mempool_put(sess_mp, (void *)s);
25833d0d5332SAkhil Goyal }
25843d0d5332SAkhil Goyal 
25853d0d5332SAkhil Goyal /** Clear the memory of session so it doesn't leave key material behind */
25863d0d5332SAkhil Goyal static void
25873d0d5332SAkhil Goyal dpaa_sec_sym_session_clear(struct rte_cryptodev *dev,
25883d0d5332SAkhil Goyal 		struct rte_cryptodev_sym_session *sess)
25893d0d5332SAkhil Goyal {
25903d0d5332SAkhil Goyal 	PMD_INIT_FUNC_TRACE();
25913d0d5332SAkhil Goyal 	uint8_t index = dev->driver_id;
25923d0d5332SAkhil Goyal 	void *sess_priv = get_sym_session_private_data(sess, index);
25933d0d5332SAkhil Goyal 	dpaa_sec_session *s = (dpaa_sec_session *)sess_priv;
25943d0d5332SAkhil Goyal 
25953d0d5332SAkhil Goyal 	if (sess_priv) {
25963d0d5332SAkhil Goyal 		free_session_memory(dev, s);
2597012c5076SPablo de Lara 		set_sym_session_private_data(sess, index, NULL);
2598c3e85bdcSAkhil Goyal 	}
2599c3e85bdcSAkhil Goyal }
2600c3e85bdcSAkhil Goyal 
2601a8d0d473SBruce Richardson #ifdef RTE_LIB_SECURITY
2602c3e85bdcSAkhil Goyal static int
26032c318722SHemant Agrawal dpaa_sec_ipsec_aead_init(struct rte_crypto_aead_xform *aead_xform,
26042c318722SHemant Agrawal 			struct rte_security_ipsec_xform *ipsec_xform,
26052c318722SHemant Agrawal 			dpaa_sec_session *session)
26061f14d500SAkhil Goyal {
26071f14d500SAkhil Goyal 	PMD_INIT_FUNC_TRACE();
26081f14d500SAkhil Goyal 
26092c318722SHemant Agrawal 	session->aead_key.data = rte_zmalloc(NULL, aead_xform->key.length,
26102c318722SHemant Agrawal 					       RTE_CACHE_LINE_SIZE);
26112c318722SHemant Agrawal 	if (session->aead_key.data == NULL && aead_xform->key.length > 0) {
26122c318722SHemant Agrawal 		DPAA_SEC_ERR("No Memory for aead key");
2613c08ced9aSAkhil Goyal 		return -ENOMEM;
26141f14d500SAkhil Goyal 	}
26152c318722SHemant Agrawal 	memcpy(session->aead_key.data, aead_xform->key.data,
26162c318722SHemant Agrawal 	       aead_xform->key.length);
261705b12700SHemant Agrawal 
26182c318722SHemant Agrawal 	session->digest_length = aead_xform->digest_length;
26192c318722SHemant Agrawal 	session->aead_key.length = aead_xform->key.length;
26202c318722SHemant Agrawal 
26212c318722SHemant Agrawal 	switch (aead_xform->algo) {
26222c318722SHemant Agrawal 	case RTE_CRYPTO_AEAD_AES_GCM:
26232c318722SHemant Agrawal 		switch (session->digest_length) {
26242c318722SHemant Agrawal 		case 8:
26252c318722SHemant Agrawal 			session->aead_key.alg = OP_PCL_IPSEC_AES_GCM8;
26262c318722SHemant Agrawal 			break;
26272c318722SHemant Agrawal 		case 12:
26282c318722SHemant Agrawal 			session->aead_key.alg = OP_PCL_IPSEC_AES_GCM12;
26292c318722SHemant Agrawal 			break;
26302c318722SHemant Agrawal 		case 16:
26312c318722SHemant Agrawal 			session->aead_key.alg = OP_PCL_IPSEC_AES_GCM16;
26322c318722SHemant Agrawal 			break;
26332c318722SHemant Agrawal 		default:
26342c318722SHemant Agrawal 			DPAA_SEC_ERR("Crypto: Undefined GCM digest %d",
26352c318722SHemant Agrawal 				     session->digest_length);
2636c08ced9aSAkhil Goyal 			return -EINVAL;
26372c318722SHemant Agrawal 		}
26382c318722SHemant Agrawal 		if (session->dir == DIR_ENC) {
26392c318722SHemant Agrawal 			memcpy(session->encap_pdb.gcm.salt,
26402c318722SHemant Agrawal 				(uint8_t *)&(ipsec_xform->salt), 4);
26412c318722SHemant Agrawal 		} else {
26422c318722SHemant Agrawal 			memcpy(session->decap_pdb.gcm.salt,
26432c318722SHemant Agrawal 				(uint8_t *)&(ipsec_xform->salt), 4);
26442c318722SHemant Agrawal 		}
26452c318722SHemant Agrawal 		session->aead_key.algmode = OP_ALG_AAI_GCM;
26462c318722SHemant Agrawal 		session->aead_alg = RTE_CRYPTO_AEAD_AES_GCM;
26472c318722SHemant Agrawal 		break;
26482c318722SHemant Agrawal 	default:
26492c318722SHemant Agrawal 		DPAA_SEC_ERR("Crypto: Undefined AEAD specified %u",
26502c318722SHemant Agrawal 			      aead_xform->algo);
2651c08ced9aSAkhil Goyal 		return -ENOTSUP;
26522c318722SHemant Agrawal 	}
26532c318722SHemant Agrawal 	return 0;
26542c318722SHemant Agrawal }
26552c318722SHemant Agrawal 
26562c318722SHemant Agrawal static int
26572c318722SHemant Agrawal dpaa_sec_ipsec_proto_init(struct rte_crypto_cipher_xform *cipher_xform,
26582c318722SHemant Agrawal 	struct rte_crypto_auth_xform *auth_xform,
26591cdfbb0bSVakul Garg 	struct rte_security_ipsec_xform *ipsec_xform,
26602c318722SHemant Agrawal 	dpaa_sec_session *session)
26612c318722SHemant Agrawal {
26622c318722SHemant Agrawal 	if (cipher_xform) {
26631f14d500SAkhil Goyal 		session->cipher_key.data = rte_zmalloc(NULL,
26641f14d500SAkhil Goyal 						       cipher_xform->key.length,
26651f14d500SAkhil Goyal 						       RTE_CACHE_LINE_SIZE);
26661f14d500SAkhil Goyal 		if (session->cipher_key.data == NULL &&
26671f14d500SAkhil Goyal 				cipher_xform->key.length > 0) {
2668f163231eSHemant Agrawal 			DPAA_SEC_ERR("No Memory for cipher key");
26691f14d500SAkhil Goyal 			return -ENOMEM;
26701f14d500SAkhil Goyal 		}
26712c318722SHemant Agrawal 
26722c318722SHemant Agrawal 		session->cipher_key.length = cipher_xform->key.length;
267305b12700SHemant Agrawal 		memcpy(session->cipher_key.data, cipher_xform->key.data,
267405b12700SHemant Agrawal 				cipher_xform->key.length);
267505b12700SHemant Agrawal 		session->cipher_alg = cipher_xform->algo;
267605b12700SHemant Agrawal 	} else {
267705b12700SHemant Agrawal 		session->cipher_key.data = NULL;
267805b12700SHemant Agrawal 		session->cipher_key.length = 0;
267905b12700SHemant Agrawal 		session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
268005b12700SHemant Agrawal 	}
268105b12700SHemant Agrawal 
26822c318722SHemant Agrawal 	if (auth_xform) {
26831f14d500SAkhil Goyal 		session->auth_key.data = rte_zmalloc(NULL,
26841f14d500SAkhil Goyal 						auth_xform->key.length,
26851f14d500SAkhil Goyal 						RTE_CACHE_LINE_SIZE);
26861f14d500SAkhil Goyal 		if (session->auth_key.data == NULL &&
26871f14d500SAkhil Goyal 				auth_xform->key.length > 0) {
2688f163231eSHemant Agrawal 			DPAA_SEC_ERR("No Memory for auth key");
26891f14d500SAkhil Goyal 			return -ENOMEM;
26901f14d500SAkhil Goyal 		}
26912c318722SHemant Agrawal 		session->auth_key.length = auth_xform->key.length;
26921f14d500SAkhil Goyal 		memcpy(session->auth_key.data, auth_xform->key.data,
26931f14d500SAkhil Goyal 				auth_xform->key.length);
26942c318722SHemant Agrawal 		session->auth_alg = auth_xform->algo;
2695247b6908SHemant Agrawal 		session->digest_length = auth_xform->digest_length;
26962c318722SHemant Agrawal 	} else {
26972c318722SHemant Agrawal 		session->auth_key.data = NULL;
26982c318722SHemant Agrawal 		session->auth_key.length = 0;
26992c318722SHemant Agrawal 		session->auth_alg = RTE_CRYPTO_AUTH_NULL;
27002c318722SHemant Agrawal 	}
27011f14d500SAkhil Goyal 
27022c318722SHemant Agrawal 	switch (session->auth_alg) {
27038524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_SHA1_HMAC:
27048524b44eSHemant Agrawal 		session->auth_key.alg = OP_PCL_IPSEC_HMAC_SHA1_96;
27058524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
27068524b44eSHemant Agrawal 		break;
27072c318722SHemant Agrawal 	case RTE_CRYPTO_AUTH_MD5_HMAC:
27082c318722SHemant Agrawal 		session->auth_key.alg = OP_PCL_IPSEC_HMAC_MD5_96;
27098524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
27108524b44eSHemant Agrawal 		break;
27111f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA256_HMAC:
27128524b44eSHemant Agrawal 		session->auth_key.alg = OP_PCL_IPSEC_HMAC_SHA2_256_128;
27138524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
2714247b6908SHemant Agrawal 		if (session->digest_length != 16)
2715247b6908SHemant Agrawal 			DPAA_SEC_WARN(
2716247b6908SHemant Agrawal 			"+++Using sha256-hmac truncated len is non-standard,"
2717247b6908SHemant Agrawal 			"it will not work with lookaside proto");
27188524b44eSHemant Agrawal 		break;
27191f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA384_HMAC:
27208524b44eSHemant Agrawal 		session->auth_key.alg = OP_PCL_IPSEC_HMAC_SHA2_384_192;
27218524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
27228524b44eSHemant Agrawal 		break;
27231f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA512_HMAC:
27248524b44eSHemant Agrawal 		session->auth_key.alg = OP_PCL_IPSEC_HMAC_SHA2_512_256;
27258524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
27261f14d500SAkhil Goyal 		break;
27272c318722SHemant Agrawal 	case RTE_CRYPTO_AUTH_AES_CMAC:
27282c318722SHemant Agrawal 		session->auth_key.alg = OP_PCL_IPSEC_AES_CMAC_96;
27292ed12d9bSGagandeep Singh 		session->auth_key.algmode = OP_ALG_AAI_CMAC;
27302c318722SHemant Agrawal 		break;
27312c318722SHemant Agrawal 	case RTE_CRYPTO_AUTH_NULL:
27322c318722SHemant Agrawal 		session->auth_key.alg = OP_PCL_IPSEC_HMAC_NULL;
27332c318722SHemant Agrawal 		break;
27342c318722SHemant Agrawal 	case RTE_CRYPTO_AUTH_AES_XCBC_MAC:
273566f95673SGagandeep Singh 		session->auth_key.alg = OP_PCL_IPSEC_AES_XCBC_MAC_96;
273666f95673SGagandeep Singh 		session->auth_key.algmode = OP_ALG_AAI_XCBC_MAC;
273766f95673SGagandeep Singh 		break;
273866f95673SGagandeep Singh 	case RTE_CRYPTO_AUTH_SHA224_HMAC:
27392c318722SHemant Agrawal 	case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
27402c318722SHemant Agrawal 	case RTE_CRYPTO_AUTH_SHA1:
27412c318722SHemant Agrawal 	case RTE_CRYPTO_AUTH_SHA256:
27422c318722SHemant Agrawal 	case RTE_CRYPTO_AUTH_SHA512:
27432c318722SHemant Agrawal 	case RTE_CRYPTO_AUTH_SHA224:
27442c318722SHemant Agrawal 	case RTE_CRYPTO_AUTH_SHA384:
27452c318722SHemant Agrawal 	case RTE_CRYPTO_AUTH_MD5:
27462c318722SHemant Agrawal 	case RTE_CRYPTO_AUTH_AES_GMAC:
27472c318722SHemant Agrawal 	case RTE_CRYPTO_AUTH_KASUMI_F9:
27482c318722SHemant Agrawal 	case RTE_CRYPTO_AUTH_AES_CBC_MAC:
27492c318722SHemant Agrawal 	case RTE_CRYPTO_AUTH_ZUC_EIA3:
2750f163231eSHemant Agrawal 		DPAA_SEC_ERR("Crypto: Unsupported auth alg %u",
27512c318722SHemant Agrawal 			      session->auth_alg);
2752c08ced9aSAkhil Goyal 		return -ENOTSUP;
27532c318722SHemant Agrawal 	default:
27542c318722SHemant Agrawal 		DPAA_SEC_ERR("Crypto: Undefined Auth specified %u",
27552c318722SHemant Agrawal 			      session->auth_alg);
2756c08ced9aSAkhil Goyal 		return -ENOTSUP;
27572c318722SHemant Agrawal 	}
27582c318722SHemant Agrawal 
27592c318722SHemant Agrawal 	switch (session->cipher_alg) {
27602c318722SHemant Agrawal 	case RTE_CRYPTO_CIPHER_AES_CBC:
27612c318722SHemant Agrawal 		session->cipher_key.alg = OP_PCL_IPSEC_AES_CBC;
27622c318722SHemant Agrawal 		session->cipher_key.algmode = OP_ALG_AAI_CBC;
27632c318722SHemant Agrawal 		break;
27643e4fbc6cSGagandeep Singh 	case RTE_CRYPTO_CIPHER_DES_CBC:
27653e4fbc6cSGagandeep Singh 		session->cipher_key.alg = OP_PCL_IPSEC_DES;
27663e4fbc6cSGagandeep Singh 		session->cipher_key.algmode = OP_ALG_AAI_CBC;
27673e4fbc6cSGagandeep Singh 		break;
27682c318722SHemant Agrawal 	case RTE_CRYPTO_CIPHER_3DES_CBC:
27692c318722SHemant Agrawal 		session->cipher_key.alg = OP_PCL_IPSEC_3DES;
27702c318722SHemant Agrawal 		session->cipher_key.algmode = OP_ALG_AAI_CBC;
27712c318722SHemant Agrawal 		break;
27722c318722SHemant Agrawal 	case RTE_CRYPTO_CIPHER_AES_CTR:
27732c318722SHemant Agrawal 		session->cipher_key.alg = OP_PCL_IPSEC_AES_CTR;
27742c318722SHemant Agrawal 		session->cipher_key.algmode = OP_ALG_AAI_CTR;
27751cdfbb0bSVakul Garg 		if (session->dir == DIR_ENC) {
27761cdfbb0bSVakul Garg 			session->encap_pdb.ctr.ctr_initial = 0x00000001;
27771cdfbb0bSVakul Garg 			session->encap_pdb.ctr.ctr_nonce = ipsec_xform->salt;
27781cdfbb0bSVakul Garg 		} else {
27791cdfbb0bSVakul Garg 			session->decap_pdb.ctr.ctr_initial = 0x00000001;
27801cdfbb0bSVakul Garg 			session->decap_pdb.ctr.ctr_nonce = ipsec_xform->salt;
27811cdfbb0bSVakul Garg 		}
27822c318722SHemant Agrawal 		break;
27832c318722SHemant Agrawal 	case RTE_CRYPTO_CIPHER_NULL:
27842c318722SHemant Agrawal 		session->cipher_key.alg = OP_PCL_IPSEC_NULL;
27852c318722SHemant Agrawal 		break;
27862c318722SHemant Agrawal 	case RTE_CRYPTO_CIPHER_SNOW3G_UEA2:
27872c318722SHemant Agrawal 	case RTE_CRYPTO_CIPHER_ZUC_EEA3:
27882c318722SHemant Agrawal 	case RTE_CRYPTO_CIPHER_3DES_ECB:
27892c318722SHemant Agrawal 	case RTE_CRYPTO_CIPHER_AES_ECB:
27902c318722SHemant Agrawal 	case RTE_CRYPTO_CIPHER_KASUMI_F8:
27912c318722SHemant Agrawal 		DPAA_SEC_ERR("Crypto: Unsupported Cipher alg %u",
27922c318722SHemant Agrawal 			      session->cipher_alg);
2793c08ced9aSAkhil Goyal 		return -ENOTSUP;
27942c318722SHemant Agrawal 	default:
27952c318722SHemant Agrawal 		DPAA_SEC_ERR("Crypto: Undefined Cipher specified %u",
27962c318722SHemant Agrawal 			      session->cipher_alg);
2797c08ced9aSAkhil Goyal 		return -ENOTSUP;
27982c318722SHemant Agrawal 	}
27992c318722SHemant Agrawal 
28002c318722SHemant Agrawal 	return 0;
28012c318722SHemant Agrawal }
28022c318722SHemant Agrawal 
28032c318722SHemant Agrawal static int
28042c318722SHemant Agrawal dpaa_sec_set_ipsec_session(__rte_unused struct rte_cryptodev *dev,
28052c318722SHemant Agrawal 			   struct rte_security_session_conf *conf,
28062c318722SHemant Agrawal 			   void *sess)
28072c318722SHemant Agrawal {
28082c318722SHemant Agrawal 	struct dpaa_sec_dev_private *internals = dev->data->dev_private;
28092c318722SHemant Agrawal 	struct rte_security_ipsec_xform *ipsec_xform = &conf->ipsec;
28102c318722SHemant Agrawal 	struct rte_crypto_auth_xform *auth_xform = NULL;
28112c318722SHemant Agrawal 	struct rte_crypto_cipher_xform *cipher_xform = NULL;
28122c318722SHemant Agrawal 	struct rte_crypto_aead_xform *aead_xform = NULL;
28132c318722SHemant Agrawal 	dpaa_sec_session *session = (dpaa_sec_session *)sess;
28142c318722SHemant Agrawal 	uint32_t i;
28152c318722SHemant Agrawal 	int ret;
28162c318722SHemant Agrawal 
28172c318722SHemant Agrawal 	PMD_INIT_FUNC_TRACE();
28182c318722SHemant Agrawal 
28192c318722SHemant Agrawal 	memset(session, 0, sizeof(dpaa_sec_session));
28202c318722SHemant Agrawal 	session->proto_alg = conf->protocol;
28212c318722SHemant Agrawal 	session->ctxt = DPAA_SEC_IPSEC;
28222c318722SHemant Agrawal 
28232c318722SHemant Agrawal 	if (ipsec_xform->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS)
28242c318722SHemant Agrawal 		session->dir = DIR_ENC;
28252c318722SHemant Agrawal 	else
28262c318722SHemant Agrawal 		session->dir = DIR_DEC;
28272c318722SHemant Agrawal 
28282c318722SHemant Agrawal 	if (conf->crypto_xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
28292c318722SHemant Agrawal 		cipher_xform = &conf->crypto_xform->cipher;
28302c318722SHemant Agrawal 		if (conf->crypto_xform->next)
28312c318722SHemant Agrawal 			auth_xform = &conf->crypto_xform->next->auth;
28322c318722SHemant Agrawal 		ret = dpaa_sec_ipsec_proto_init(cipher_xform, auth_xform,
28331cdfbb0bSVakul Garg 					ipsec_xform, session);
28342c318722SHemant Agrawal 	} else if (conf->crypto_xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
28352c318722SHemant Agrawal 		auth_xform = &conf->crypto_xform->auth;
28362c318722SHemant Agrawal 		if (conf->crypto_xform->next)
28372c318722SHemant Agrawal 			cipher_xform = &conf->crypto_xform->next->cipher;
28382c318722SHemant Agrawal 		ret = dpaa_sec_ipsec_proto_init(cipher_xform, auth_xform,
28391cdfbb0bSVakul Garg 					ipsec_xform, session);
28402c318722SHemant Agrawal 	} else if (conf->crypto_xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
28412c318722SHemant Agrawal 		aead_xform = &conf->crypto_xform->aead;
28422c318722SHemant Agrawal 		ret = dpaa_sec_ipsec_aead_init(aead_xform,
28432c318722SHemant Agrawal 					ipsec_xform, session);
28442c318722SHemant Agrawal 	} else {
28452c318722SHemant Agrawal 		DPAA_SEC_ERR("XFORM not specified");
28462c318722SHemant Agrawal 		ret = -EINVAL;
28471f14d500SAkhil Goyal 		goto out;
28481f14d500SAkhil Goyal 	}
28492c318722SHemant Agrawal 	if (ret) {
28502c318722SHemant Agrawal 		DPAA_SEC_ERR("Failed to process xform");
28512c318722SHemant Agrawal 		goto out;
28521f14d500SAkhil Goyal 	}
28531f14d500SAkhil Goyal 
28541f14d500SAkhil Goyal 	if (ipsec_xform->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
28555ab35d2eSAkhil Goyal 		if (ipsec_xform->tunnel.type ==
28565ab35d2eSAkhil Goyal 				RTE_SECURITY_IPSEC_TUNNEL_IPV4) {
28571f14d500SAkhil Goyal 			session->ip4_hdr.ip_v = IPVERSION;
28581f14d500SAkhil Goyal 			session->ip4_hdr.ip_hl = 5;
28591f14d500SAkhil Goyal 			session->ip4_hdr.ip_len = rte_cpu_to_be_16(
28601f14d500SAkhil Goyal 						sizeof(session->ip4_hdr));
28611f14d500SAkhil Goyal 			session->ip4_hdr.ip_tos = ipsec_xform->tunnel.ipv4.dscp;
28621f14d500SAkhil Goyal 			session->ip4_hdr.ip_id = 0;
28631f14d500SAkhil Goyal 			session->ip4_hdr.ip_off = 0;
28641f14d500SAkhil Goyal 			session->ip4_hdr.ip_ttl = ipsec_xform->tunnel.ipv4.ttl;
28651f14d500SAkhil Goyal 			session->ip4_hdr.ip_p = (ipsec_xform->proto ==
28665ab35d2eSAkhil Goyal 					RTE_SECURITY_IPSEC_SA_PROTO_ESP) ?
28675ab35d2eSAkhil Goyal 					IPPROTO_ESP : IPPROTO_AH;
28681f14d500SAkhil Goyal 			session->ip4_hdr.ip_sum = 0;
28695ab35d2eSAkhil Goyal 			session->ip4_hdr.ip_src =
28705ab35d2eSAkhil Goyal 					ipsec_xform->tunnel.ipv4.src_ip;
28715ab35d2eSAkhil Goyal 			session->ip4_hdr.ip_dst =
28725ab35d2eSAkhil Goyal 					ipsec_xform->tunnel.ipv4.dst_ip;
28731f14d500SAkhil Goyal 			session->ip4_hdr.ip_sum = calc_chksum((uint16_t *)
28741f14d500SAkhil Goyal 						(void *)&session->ip4_hdr,
28751f14d500SAkhil Goyal 						sizeof(struct ip));
28765ab35d2eSAkhil Goyal 			session->encap_pdb.ip_hdr_len = sizeof(struct ip);
28775ab35d2eSAkhil Goyal 		} else if (ipsec_xform->tunnel.type ==
28785ab35d2eSAkhil Goyal 				RTE_SECURITY_IPSEC_TUNNEL_IPV6) {
28795ab35d2eSAkhil Goyal 			session->ip6_hdr.vtc_flow = rte_cpu_to_be_32(
28805ab35d2eSAkhil Goyal 				DPAA_IPv6_DEFAULT_VTC_FLOW |
28815ab35d2eSAkhil Goyal 				((ipsec_xform->tunnel.ipv6.dscp <<
28825ab35d2eSAkhil Goyal 					RTE_IPV6_HDR_TC_SHIFT) &
28835ab35d2eSAkhil Goyal 					RTE_IPV6_HDR_TC_MASK) |
28845ab35d2eSAkhil Goyal 				((ipsec_xform->tunnel.ipv6.flabel <<
28855ab35d2eSAkhil Goyal 					RTE_IPV6_HDR_FL_SHIFT) &
28865ab35d2eSAkhil Goyal 					RTE_IPV6_HDR_FL_MASK));
28875ab35d2eSAkhil Goyal 			/* Payload length will be updated by HW */
28885ab35d2eSAkhil Goyal 			session->ip6_hdr.payload_len = 0;
28895ab35d2eSAkhil Goyal 			session->ip6_hdr.hop_limits =
28905ab35d2eSAkhil Goyal 					ipsec_xform->tunnel.ipv6.hlimit;
28915ab35d2eSAkhil Goyal 			session->ip6_hdr.proto = (ipsec_xform->proto ==
28925ab35d2eSAkhil Goyal 					RTE_SECURITY_IPSEC_SA_PROTO_ESP) ?
28935ab35d2eSAkhil Goyal 					IPPROTO_ESP : IPPROTO_AH;
28945ab35d2eSAkhil Goyal 			memcpy(&session->ip6_hdr.src_addr,
28955ab35d2eSAkhil Goyal 					&ipsec_xform->tunnel.ipv6.src_addr, 16);
28965ab35d2eSAkhil Goyal 			memcpy(&session->ip6_hdr.dst_addr,
28975ab35d2eSAkhil Goyal 					&ipsec_xform->tunnel.ipv6.dst_addr, 16);
28985ab35d2eSAkhil Goyal 			session->encap_pdb.ip_hdr_len =
28995ab35d2eSAkhil Goyal 						sizeof(struct rte_ipv6_hdr);
29005ab35d2eSAkhil Goyal 		}
2901*0aa5986cSGagandeep Singh 
29021f14d500SAkhil Goyal 		session->encap_pdb.options =
29031f14d500SAkhil Goyal 			(IPVERSION << PDBNH_ESP_ENCAP_SHIFT) |
29041f14d500SAkhil Goyal 			PDBOPTS_ESP_OIHI_PDB_INL |
29051f14d500SAkhil Goyal 			PDBOPTS_ESP_IVSRC |
290679fde9d0SAkhil Goyal 			PDBHMO_ESP_SNR;
2907*0aa5986cSGagandeep Singh 		if (ipsec_xform->options.dec_ttl)
2908*0aa5986cSGagandeep Singh 			session->encap_pdb.options |= PDBHMO_ESP_ENCAP_DTTL;
29090f318781SAkhil Goyal 		if (ipsec_xform->options.esn)
29100f318781SAkhil Goyal 			session->encap_pdb.options |= PDBOPTS_ESP_ESN;
29111f14d500SAkhil Goyal 		session->encap_pdb.spi = ipsec_xform->spi;
29122c318722SHemant Agrawal 
29131f14d500SAkhil Goyal 	} else if (ipsec_xform->direction ==
29141f14d500SAkhil Goyal 			RTE_SECURITY_IPSEC_SA_DIR_INGRESS) {
29155ab35d2eSAkhil Goyal 		if (ipsec_xform->tunnel.type == RTE_SECURITY_IPSEC_TUNNEL_IPV4)
29161f14d500SAkhil Goyal 			session->decap_pdb.options = sizeof(struct ip) << 16;
29175ab35d2eSAkhil Goyal 		else
29185ab35d2eSAkhil Goyal 			session->decap_pdb.options =
29195ab35d2eSAkhil Goyal 					sizeof(struct rte_ipv6_hdr) << 16;
29200f318781SAkhil Goyal 		if (ipsec_xform->options.esn)
29210f318781SAkhil Goyal 			session->decap_pdb.options |= PDBOPTS_ESP_ESN;
2922a37ce227SHemant Agrawal 		if (ipsec_xform->replay_win_sz) {
2923a37ce227SHemant Agrawal 			uint32_t win_sz;
2924a37ce227SHemant Agrawal 			win_sz = rte_align32pow2(ipsec_xform->replay_win_sz);
2925a37ce227SHemant Agrawal 
2926a37ce227SHemant Agrawal 			switch (win_sz) {
2927a37ce227SHemant Agrawal 			case 1:
2928a37ce227SHemant Agrawal 			case 2:
2929a37ce227SHemant Agrawal 			case 4:
2930a37ce227SHemant Agrawal 			case 8:
2931a37ce227SHemant Agrawal 			case 16:
2932a37ce227SHemant Agrawal 			case 32:
2933a37ce227SHemant Agrawal 				session->decap_pdb.options |= PDBOPTS_ESP_ARS32;
2934a37ce227SHemant Agrawal 				break;
2935a37ce227SHemant Agrawal 			case 64:
2936a37ce227SHemant Agrawal 				session->decap_pdb.options |= PDBOPTS_ESP_ARS64;
2937a37ce227SHemant Agrawal 				break;
2938a37ce227SHemant Agrawal 			default:
2939a37ce227SHemant Agrawal 				session->decap_pdb.options |=
2940a37ce227SHemant Agrawal 							PDBOPTS_ESP_ARS128;
2941a37ce227SHemant Agrawal 			}
2942a37ce227SHemant Agrawal 		}
29431f14d500SAkhil Goyal 	} else
29441f14d500SAkhil Goyal 		goto out;
29453b617ee7SAkhil Goyal 	rte_spinlock_lock(&internals->lock);
29464e694fe5SAkhil Goyal 	for (i = 0; i < MAX_DPAA_CORES; i++) {
29474e694fe5SAkhil Goyal 		session->inq[i] = dpaa_sec_attach_rxq(internals);
29484e694fe5SAkhil Goyal 		if (session->inq[i] == NULL) {
2949f163231eSHemant Agrawal 			DPAA_SEC_ERR("unable to attach sec queue");
29504e694fe5SAkhil Goyal 			rte_spinlock_unlock(&internals->lock);
29511f14d500SAkhil Goyal 			goto out;
29521f14d500SAkhil Goyal 		}
29534e694fe5SAkhil Goyal 	}
29544e694fe5SAkhil Goyal 	rte_spinlock_unlock(&internals->lock);
29551f14d500SAkhil Goyal 
2956a1173d55SHemant Agrawal 	return 0;
2957a1173d55SHemant Agrawal out:
29586290de2cSLukasz Wojciechowski 	free_session_data(session);
2959a1173d55SHemant Agrawal 	return -1;
2960a1173d55SHemant Agrawal }
29611f14d500SAkhil Goyal 
2962a1173d55SHemant Agrawal static int
2963a1173d55SHemant Agrawal dpaa_sec_set_pdcp_session(struct rte_cryptodev *dev,
2964a1173d55SHemant Agrawal 			  struct rte_security_session_conf *conf,
2965a1173d55SHemant Agrawal 			  void *sess)
2966a1173d55SHemant Agrawal {
2967a1173d55SHemant Agrawal 	struct rte_security_pdcp_xform *pdcp_xform = &conf->pdcp;
2968a1173d55SHemant Agrawal 	struct rte_crypto_sym_xform *xform = conf->crypto_xform;
2969a1173d55SHemant Agrawal 	struct rte_crypto_auth_xform *auth_xform = NULL;
2970a1173d55SHemant Agrawal 	struct rte_crypto_cipher_xform *cipher_xform = NULL;
2971a1173d55SHemant Agrawal 	dpaa_sec_session *session = (dpaa_sec_session *)sess;
2972a1173d55SHemant Agrawal 	struct dpaa_sec_dev_private *dev_priv = dev->data->dev_private;
29734e694fe5SAkhil Goyal 	uint32_t i;
2974c08ced9aSAkhil Goyal 	int ret;
2975a1173d55SHemant Agrawal 
2976a1173d55SHemant Agrawal 	PMD_INIT_FUNC_TRACE();
2977a1173d55SHemant Agrawal 
2978a1173d55SHemant Agrawal 	memset(session, 0, sizeof(dpaa_sec_session));
2979a1173d55SHemant Agrawal 
2980a1173d55SHemant Agrawal 	/* find xfrm types */
2981a1173d55SHemant Agrawal 	if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
2982a1173d55SHemant Agrawal 		cipher_xform = &xform->cipher;
2983a1173d55SHemant Agrawal 		if (xform->next != NULL)
2984a1173d55SHemant Agrawal 			auth_xform = &xform->next->auth;
2985a1173d55SHemant Agrawal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
2986a1173d55SHemant Agrawal 		auth_xform = &xform->auth;
2987a1173d55SHemant Agrawal 		if (xform->next != NULL)
2988a1173d55SHemant Agrawal 			cipher_xform = &xform->next->cipher;
2989a1173d55SHemant Agrawal 	} else {
2990a1173d55SHemant Agrawal 		DPAA_SEC_ERR("Invalid crypto type");
2991a1173d55SHemant Agrawal 		return -EINVAL;
2992a1173d55SHemant Agrawal 	}
2993a1173d55SHemant Agrawal 
2994a1173d55SHemant Agrawal 	session->proto_alg = conf->protocol;
29958524b44eSHemant Agrawal 	session->ctxt = DPAA_SEC_PDCP;
29968524b44eSHemant Agrawal 
2997a1173d55SHemant Agrawal 	if (cipher_xform) {
29988524b44eSHemant Agrawal 		switch (cipher_xform->algo) {
29998524b44eSHemant Agrawal 		case RTE_CRYPTO_CIPHER_SNOW3G_UEA2:
30008524b44eSHemant Agrawal 			session->cipher_key.alg = PDCP_CIPHER_TYPE_SNOW;
30018524b44eSHemant Agrawal 			break;
30028524b44eSHemant Agrawal 		case RTE_CRYPTO_CIPHER_ZUC_EEA3:
30038524b44eSHemant Agrawal 			session->cipher_key.alg = PDCP_CIPHER_TYPE_ZUC;
30048524b44eSHemant Agrawal 			break;
30058524b44eSHemant Agrawal 		case RTE_CRYPTO_CIPHER_AES_CTR:
30068524b44eSHemant Agrawal 			session->cipher_key.alg = PDCP_CIPHER_TYPE_AES;
30078524b44eSHemant Agrawal 			break;
30088524b44eSHemant Agrawal 		case RTE_CRYPTO_CIPHER_NULL:
30098524b44eSHemant Agrawal 			session->cipher_key.alg = PDCP_CIPHER_TYPE_NULL;
30108524b44eSHemant Agrawal 			break;
30118524b44eSHemant Agrawal 		default:
30128524b44eSHemant Agrawal 			DPAA_SEC_ERR("Crypto: Undefined Cipher specified %u",
30138524b44eSHemant Agrawal 				      session->cipher_alg);
3014c08ced9aSAkhil Goyal 			return -EINVAL;
30158524b44eSHemant Agrawal 		}
30168524b44eSHemant Agrawal 
3017a1173d55SHemant Agrawal 		session->cipher_key.data = rte_zmalloc(NULL,
3018a1173d55SHemant Agrawal 					       cipher_xform->key.length,
3019a1173d55SHemant Agrawal 					       RTE_CACHE_LINE_SIZE);
3020a1173d55SHemant Agrawal 		if (session->cipher_key.data == NULL &&
3021a1173d55SHemant Agrawal 				cipher_xform->key.length > 0) {
3022a1173d55SHemant Agrawal 			DPAA_SEC_ERR("No Memory for cipher key");
3023a1173d55SHemant Agrawal 			return -ENOMEM;
3024a1173d55SHemant Agrawal 		}
3025a1173d55SHemant Agrawal 		session->cipher_key.length = cipher_xform->key.length;
3026a1173d55SHemant Agrawal 		memcpy(session->cipher_key.data, cipher_xform->key.data,
3027a1173d55SHemant Agrawal 			cipher_xform->key.length);
3028a1173d55SHemant Agrawal 		session->dir = (cipher_xform->op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
3029a1173d55SHemant Agrawal 					DIR_ENC : DIR_DEC;
3030a1173d55SHemant Agrawal 		session->cipher_alg = cipher_xform->algo;
3031a1173d55SHemant Agrawal 	} else {
3032a1173d55SHemant Agrawal 		session->cipher_key.data = NULL;
3033a1173d55SHemant Agrawal 		session->cipher_key.length = 0;
3034a1173d55SHemant Agrawal 		session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
3035a1173d55SHemant Agrawal 		session->dir = DIR_ENC;
3036a1173d55SHemant Agrawal 	}
3037a1173d55SHemant Agrawal 
3038a1173d55SHemant Agrawal 	if (pdcp_xform->domain == RTE_SECURITY_PDCP_MODE_CONTROL) {
3039eac60082SVakul Garg 		if (pdcp_xform->sn_size != RTE_SECURITY_PDCP_SN_SIZE_5 &&
3040eac60082SVakul Garg 		    pdcp_xform->sn_size != RTE_SECURITY_PDCP_SN_SIZE_12) {
3041a1173d55SHemant Agrawal 			DPAA_SEC_ERR(
3042eac60082SVakul Garg 				"PDCP Seq Num size should be 5/12 bits for cmode");
3043c08ced9aSAkhil Goyal 			ret = -EINVAL;
3044a1173d55SHemant Agrawal 			goto out;
3045a1173d55SHemant Agrawal 		}
30462e4cbdb4SVakul Garg 	}
30472e4cbdb4SVakul Garg 
3048a1173d55SHemant Agrawal 	if (auth_xform) {
30498524b44eSHemant Agrawal 		switch (auth_xform->algo) {
30508524b44eSHemant Agrawal 		case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
30518524b44eSHemant Agrawal 			session->auth_key.alg = PDCP_AUTH_TYPE_SNOW;
30528524b44eSHemant Agrawal 			break;
30538524b44eSHemant Agrawal 		case RTE_CRYPTO_AUTH_ZUC_EIA3:
30548524b44eSHemant Agrawal 			session->auth_key.alg = PDCP_AUTH_TYPE_ZUC;
30558524b44eSHemant Agrawal 			break;
30568524b44eSHemant Agrawal 		case RTE_CRYPTO_AUTH_AES_CMAC:
30578524b44eSHemant Agrawal 			session->auth_key.alg = PDCP_AUTH_TYPE_AES;
30588524b44eSHemant Agrawal 			break;
30598524b44eSHemant Agrawal 		case RTE_CRYPTO_AUTH_NULL:
30608524b44eSHemant Agrawal 			session->auth_key.alg = PDCP_AUTH_TYPE_NULL;
30618524b44eSHemant Agrawal 			break;
30628524b44eSHemant Agrawal 		default:
30638524b44eSHemant Agrawal 			DPAA_SEC_ERR("Crypto: Unsupported auth alg %u",
30648524b44eSHemant Agrawal 				      session->auth_alg);
30658524b44eSHemant Agrawal 			rte_free(session->cipher_key.data);
3066c08ced9aSAkhil Goyal 			return -EINVAL;
30678524b44eSHemant Agrawal 		}
3068a1173d55SHemant Agrawal 		session->auth_key.data = rte_zmalloc(NULL,
3069a1173d55SHemant Agrawal 						     auth_xform->key.length,
3070a1173d55SHemant Agrawal 						     RTE_CACHE_LINE_SIZE);
30712e4cbdb4SVakul Garg 		if (!session->auth_key.data &&
3072a1173d55SHemant Agrawal 		    auth_xform->key.length > 0) {
3073a1173d55SHemant Agrawal 			DPAA_SEC_ERR("No Memory for auth key");
3074a1173d55SHemant Agrawal 			rte_free(session->cipher_key.data);
3075a1173d55SHemant Agrawal 			return -ENOMEM;
3076a1173d55SHemant Agrawal 		}
3077a1173d55SHemant Agrawal 		session->auth_key.length = auth_xform->key.length;
3078a1173d55SHemant Agrawal 		memcpy(session->auth_key.data, auth_xform->key.data,
3079a1173d55SHemant Agrawal 		       auth_xform->key.length);
3080a1173d55SHemant Agrawal 		session->auth_alg = auth_xform->algo;
3081a1173d55SHemant Agrawal 	} else {
3082a1173d55SHemant Agrawal 		session->auth_key.data = NULL;
3083a1173d55SHemant Agrawal 		session->auth_key.length = 0;
30842e4cbdb4SVakul Garg 		session->auth_alg = 0;
3085a1173d55SHemant Agrawal 	}
3086a1173d55SHemant Agrawal 	session->pdcp.domain = pdcp_xform->domain;
3087a1173d55SHemant Agrawal 	session->pdcp.bearer = pdcp_xform->bearer;
3088a1173d55SHemant Agrawal 	session->pdcp.pkt_dir = pdcp_xform->pkt_dir;
3089a1173d55SHemant Agrawal 	session->pdcp.sn_size = pdcp_xform->sn_size;
3090a1173d55SHemant Agrawal 	session->pdcp.hfn = pdcp_xform->hfn;
3091a1173d55SHemant Agrawal 	session->pdcp.hfn_threshold = pdcp_xform->hfn_threshold;
30926a0c9d36SAkhil Goyal 	session->pdcp.hfn_ovd = pdcp_xform->hfn_ovrd;
30935a4954bcSAkhil Goyal 	session->pdcp.sdap_enabled = pdcp_xform->sdap_enabled;
30948839c8a1SYunjian Wang 	if (cipher_xform)
30956a0c9d36SAkhil Goyal 		session->pdcp.hfn_ovd_offset = cipher_xform->iv.offset;
3096a1173d55SHemant Agrawal 
3097a1173d55SHemant Agrawal 	rte_spinlock_lock(&dev_priv->lock);
30984e694fe5SAkhil Goyal 	for (i = 0; i < MAX_DPAA_CORES; i++) {
30994e694fe5SAkhil Goyal 		session->inq[i] = dpaa_sec_attach_rxq(dev_priv);
31004e694fe5SAkhil Goyal 		if (session->inq[i] == NULL) {
3101a1173d55SHemant Agrawal 			DPAA_SEC_ERR("unable to attach sec queue");
31024e694fe5SAkhil Goyal 			rte_spinlock_unlock(&dev_priv->lock);
3103c08ced9aSAkhil Goyal 			ret = -EBUSY;
3104a1173d55SHemant Agrawal 			goto out;
3105a1173d55SHemant Agrawal 		}
31064e694fe5SAkhil Goyal 	}
31074e694fe5SAkhil Goyal 	rte_spinlock_unlock(&dev_priv->lock);
31081f14d500SAkhil Goyal 	return 0;
31091f14d500SAkhil Goyal out:
31101f14d500SAkhil Goyal 	rte_free(session->auth_key.data);
31111f14d500SAkhil Goyal 	rte_free(session->cipher_key.data);
31121f14d500SAkhil Goyal 	memset(session, 0, sizeof(dpaa_sec_session));
3113c08ced9aSAkhil Goyal 	return ret;
31141f14d500SAkhil Goyal }
31151f14d500SAkhil Goyal 
31161f14d500SAkhil Goyal static int
31171f14d500SAkhil Goyal dpaa_sec_security_session_create(void *dev,
31181f14d500SAkhil Goyal 				 struct rte_security_session_conf *conf,
31191f14d500SAkhil Goyal 				 struct rte_security_session *sess,
31201f14d500SAkhil Goyal 				 struct rte_mempool *mempool)
31211f14d500SAkhil Goyal {
31221f14d500SAkhil Goyal 	void *sess_private_data;
31231f14d500SAkhil Goyal 	struct rte_cryptodev *cdev = (struct rte_cryptodev *)dev;
31241f14d500SAkhil Goyal 	int ret;
31251f14d500SAkhil Goyal 
31261f14d500SAkhil Goyal 	if (rte_mempool_get(mempool, &sess_private_data)) {
3127f163231eSHemant Agrawal 		DPAA_SEC_ERR("Couldn't get object from session mempool");
31281f14d500SAkhil Goyal 		return -ENOMEM;
31291f14d500SAkhil Goyal 	}
31301f14d500SAkhil Goyal 
31311f14d500SAkhil Goyal 	switch (conf->protocol) {
31321f14d500SAkhil Goyal 	case RTE_SECURITY_PROTOCOL_IPSEC:
31331f14d500SAkhil Goyal 		ret = dpaa_sec_set_ipsec_session(cdev, conf,
31341f14d500SAkhil Goyal 				sess_private_data);
31351f14d500SAkhil Goyal 		break;
3136a1173d55SHemant Agrawal 	case RTE_SECURITY_PROTOCOL_PDCP:
3137a1173d55SHemant Agrawal 		ret = dpaa_sec_set_pdcp_session(cdev, conf,
3138a1173d55SHemant Agrawal 				sess_private_data);
3139a1173d55SHemant Agrawal 		break;
31401f14d500SAkhil Goyal 	case RTE_SECURITY_PROTOCOL_MACSEC:
31411f14d500SAkhil Goyal 		return -ENOTSUP;
31421f14d500SAkhil Goyal 	default:
31431f14d500SAkhil Goyal 		return -EINVAL;
31441f14d500SAkhil Goyal 	}
31451f14d500SAkhil Goyal 	if (ret != 0) {
3146f163231eSHemant Agrawal 		DPAA_SEC_ERR("failed to configure session parameters");
31471f14d500SAkhil Goyal 		/* Return session to mempool */
31481f14d500SAkhil Goyal 		rte_mempool_put(mempool, sess_private_data);
31491f14d500SAkhil Goyal 		return ret;
31501f14d500SAkhil Goyal 	}
31511f14d500SAkhil Goyal 
31521f14d500SAkhil Goyal 	set_sec_session_private_data(sess, sess_private_data);
31531f14d500SAkhil Goyal 
31541f14d500SAkhil Goyal 	return ret;
31551f14d500SAkhil Goyal }
31561f14d500SAkhil Goyal 
31571f14d500SAkhil Goyal /** Clear the memory of session so it doesn't leave key material behind */
31581f14d500SAkhil Goyal static int
31591f14d500SAkhil Goyal dpaa_sec_security_session_destroy(void *dev __rte_unused,
31601f14d500SAkhil Goyal 		struct rte_security_session *sess)
31611f14d500SAkhil Goyal {
31621f14d500SAkhil Goyal 	PMD_INIT_FUNC_TRACE();
31631f14d500SAkhil Goyal 	void *sess_priv = get_sec_session_private_data(sess);
31641f14d500SAkhil Goyal 	dpaa_sec_session *s = (dpaa_sec_session *)sess_priv;
31651f14d500SAkhil Goyal 
31661f14d500SAkhil Goyal 	if (sess_priv) {
31673d0d5332SAkhil Goyal 		free_session_memory((struct rte_cryptodev *)dev, s);
31681f14d500SAkhil Goyal 		set_sec_session_private_data(sess, NULL);
31691f14d500SAkhil Goyal 	}
31701f14d500SAkhil Goyal 	return 0;
31711f14d500SAkhil Goyal }
3172314424b6SHemant Agrawal #endif
31731f14d500SAkhil Goyal static int
31742ffb940eSAkhil Goyal dpaa_sec_dev_configure(struct rte_cryptodev *dev __rte_unused,
3175c3e85bdcSAkhil Goyal 		       struct rte_cryptodev_config *config __rte_unused)
3176c3e85bdcSAkhil Goyal {
3177c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
3178c3e85bdcSAkhil Goyal 
3179c3e85bdcSAkhil Goyal 	return 0;
3180c3e85bdcSAkhil Goyal }
3181c3e85bdcSAkhil Goyal 
3182c3e85bdcSAkhil Goyal static int
3183c3e85bdcSAkhil Goyal dpaa_sec_dev_start(struct rte_cryptodev *dev __rte_unused)
3184c3e85bdcSAkhil Goyal {
3185c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
3186c3e85bdcSAkhil Goyal 	return 0;
3187c3e85bdcSAkhil Goyal }
3188c3e85bdcSAkhil Goyal 
3189c3e85bdcSAkhil Goyal static void
3190c3e85bdcSAkhil Goyal dpaa_sec_dev_stop(struct rte_cryptodev *dev __rte_unused)
3191c3e85bdcSAkhil Goyal {
3192c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
3193c3e85bdcSAkhil Goyal }
3194c3e85bdcSAkhil Goyal 
3195c3e85bdcSAkhil Goyal static int
31967e3e2954SAkhil Goyal dpaa_sec_dev_close(struct rte_cryptodev *dev)
3197c3e85bdcSAkhil Goyal {
3198c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
31997e3e2954SAkhil Goyal 
32007e3e2954SAkhil Goyal 	if (dev == NULL)
32017e3e2954SAkhil Goyal 		return -ENOMEM;
32027e3e2954SAkhil Goyal 
3203c3e85bdcSAkhil Goyal 	return 0;
3204c3e85bdcSAkhil Goyal }
3205c3e85bdcSAkhil Goyal 
3206c3e85bdcSAkhil Goyal static void
3207c3e85bdcSAkhil Goyal dpaa_sec_dev_infos_get(struct rte_cryptodev *dev,
3208c3e85bdcSAkhil Goyal 		       struct rte_cryptodev_info *info)
3209c3e85bdcSAkhil Goyal {
3210c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals = dev->data->dev_private;
3211c3e85bdcSAkhil Goyal 
3212c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
3213c3e85bdcSAkhil Goyal 	if (info != NULL) {
3214c3e85bdcSAkhil Goyal 		info->max_nb_queue_pairs = internals->max_nb_queue_pairs;
3215c3e85bdcSAkhil Goyal 		info->feature_flags = dev->feature_flags;
3216c3e85bdcSAkhil Goyal 		info->capabilities = dpaa_sec_capabilities;
3217c3e85bdcSAkhil Goyal 		info->sym.max_nb_sessions = internals->max_nb_sessions;
32189d5f73c2SGagandeep Singh 		info->driver_id = dpaa_cryptodev_driver_id;
3219c3e85bdcSAkhil Goyal 	}
3220c3e85bdcSAkhil Goyal }
3221c3e85bdcSAkhil Goyal 
3222fe3688baSAkhil Goyal static enum qman_cb_dqrr_result
3223fe3688baSAkhil Goyal dpaa_sec_process_parallel_event(void *event,
3224fe3688baSAkhil Goyal 			struct qman_portal *qm __always_unused,
3225fe3688baSAkhil Goyal 			struct qman_fq *outq,
3226fe3688baSAkhil Goyal 			const struct qm_dqrr_entry *dqrr,
3227fe3688baSAkhil Goyal 			void **bufs)
3228fe3688baSAkhil Goyal {
3229fe3688baSAkhil Goyal 	const struct qm_fd *fd;
3230fe3688baSAkhil Goyal 	struct dpaa_sec_job *job;
3231fe3688baSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
3232fe3688baSAkhil Goyal 	struct rte_event *ev = (struct rte_event *)event;
3233fe3688baSAkhil Goyal 
3234fe3688baSAkhil Goyal 	fd = &dqrr->fd;
3235fe3688baSAkhil Goyal 
3236fe3688baSAkhil Goyal 	/* sg is embedded in an op ctx,
3237fe3688baSAkhil Goyal 	 * sg[0] is for output
3238fe3688baSAkhil Goyal 	 * sg[1] for input
3239fe3688baSAkhil Goyal 	 */
3240ec861560SGagandeep Singh 	job = rte_dpaa_mem_ptov(qm_fd_addr_get64(fd));
3241fe3688baSAkhil Goyal 
3242fe3688baSAkhil Goyal 	ctx = container_of(job, struct dpaa_sec_op_ctx, job);
3243fe3688baSAkhil Goyal 	ctx->fd_status = fd->status;
3244fe3688baSAkhil Goyal 	if (ctx->op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
3245fe3688baSAkhil Goyal 		struct qm_sg_entry *sg_out;
3246fe3688baSAkhil Goyal 		uint32_t len;
3247fe3688baSAkhil Goyal 
3248fe3688baSAkhil Goyal 		sg_out = &job->sg[0];
3249fe3688baSAkhil Goyal 		hw_sg_to_cpu(sg_out);
3250fe3688baSAkhil Goyal 		len = sg_out->length;
3251fe3688baSAkhil Goyal 		ctx->op->sym->m_src->pkt_len = len;
3252fe3688baSAkhil Goyal 		ctx->op->sym->m_src->data_len = len;
3253fe3688baSAkhil Goyal 	}
3254fe3688baSAkhil Goyal 	if (!ctx->fd_status) {
3255fe3688baSAkhil Goyal 		ctx->op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
3256fe3688baSAkhil Goyal 	} else {
3257fe3688baSAkhil Goyal 		DPAA_SEC_DP_WARN("SEC return err: 0x%x", ctx->fd_status);
3258fe3688baSAkhil Goyal 		ctx->op->status = RTE_CRYPTO_OP_STATUS_ERROR;
3259fe3688baSAkhil Goyal 	}
3260fe3688baSAkhil Goyal 	ev->event_ptr = (void *)ctx->op;
3261fe3688baSAkhil Goyal 
3262fe3688baSAkhil Goyal 	ev->flow_id = outq->ev.flow_id;
3263fe3688baSAkhil Goyal 	ev->sub_event_type = outq->ev.sub_event_type;
3264fe3688baSAkhil Goyal 	ev->event_type = RTE_EVENT_TYPE_CRYPTODEV;
3265fe3688baSAkhil Goyal 	ev->op = RTE_EVENT_OP_NEW;
3266fe3688baSAkhil Goyal 	ev->sched_type = outq->ev.sched_type;
3267fe3688baSAkhil Goyal 	ev->queue_id = outq->ev.queue_id;
3268fe3688baSAkhil Goyal 	ev->priority = outq->ev.priority;
3269fe3688baSAkhil Goyal 	*bufs = (void *)ctx->op;
3270fe3688baSAkhil Goyal 
3271fe3688baSAkhil Goyal 	rte_mempool_put(ctx->ctx_pool, (void *)ctx);
3272fe3688baSAkhil Goyal 
3273fe3688baSAkhil Goyal 	return qman_cb_dqrr_consume;
3274fe3688baSAkhil Goyal }
3275fe3688baSAkhil Goyal 
3276fe3688baSAkhil Goyal static enum qman_cb_dqrr_result
3277fe3688baSAkhil Goyal dpaa_sec_process_atomic_event(void *event,
3278fe3688baSAkhil Goyal 			struct qman_portal *qm __rte_unused,
3279fe3688baSAkhil Goyal 			struct qman_fq *outq,
3280fe3688baSAkhil Goyal 			const struct qm_dqrr_entry *dqrr,
3281fe3688baSAkhil Goyal 			void **bufs)
3282fe3688baSAkhil Goyal {
3283fe3688baSAkhil Goyal 	u8 index;
3284fe3688baSAkhil Goyal 	const struct qm_fd *fd;
3285fe3688baSAkhil Goyal 	struct dpaa_sec_job *job;
3286fe3688baSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
3287fe3688baSAkhil Goyal 	struct rte_event *ev = (struct rte_event *)event;
3288fe3688baSAkhil Goyal 
3289fe3688baSAkhil Goyal 	fd = &dqrr->fd;
3290fe3688baSAkhil Goyal 
3291fe3688baSAkhil Goyal 	/* sg is embedded in an op ctx,
3292fe3688baSAkhil Goyal 	 * sg[0] is for output
3293fe3688baSAkhil Goyal 	 * sg[1] for input
3294fe3688baSAkhil Goyal 	 */
3295ec861560SGagandeep Singh 	job = rte_dpaa_mem_ptov(qm_fd_addr_get64(fd));
3296fe3688baSAkhil Goyal 
3297fe3688baSAkhil Goyal 	ctx = container_of(job, struct dpaa_sec_op_ctx, job);
3298fe3688baSAkhil Goyal 	ctx->fd_status = fd->status;
3299fe3688baSAkhil Goyal 	if (ctx->op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
3300fe3688baSAkhil Goyal 		struct qm_sg_entry *sg_out;
3301fe3688baSAkhil Goyal 		uint32_t len;
3302fe3688baSAkhil Goyal 
3303fe3688baSAkhil Goyal 		sg_out = &job->sg[0];
3304fe3688baSAkhil Goyal 		hw_sg_to_cpu(sg_out);
3305fe3688baSAkhil Goyal 		len = sg_out->length;
3306fe3688baSAkhil Goyal 		ctx->op->sym->m_src->pkt_len = len;
3307fe3688baSAkhil Goyal 		ctx->op->sym->m_src->data_len = len;
3308fe3688baSAkhil Goyal 	}
3309fe3688baSAkhil Goyal 	if (!ctx->fd_status) {
3310fe3688baSAkhil Goyal 		ctx->op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
3311fe3688baSAkhil Goyal 	} else {
3312fe3688baSAkhil Goyal 		DPAA_SEC_DP_WARN("SEC return err: 0x%x", ctx->fd_status);
3313fe3688baSAkhil Goyal 		ctx->op->status = RTE_CRYPTO_OP_STATUS_ERROR;
3314fe3688baSAkhil Goyal 	}
3315fe3688baSAkhil Goyal 	ev->event_ptr = (void *)ctx->op;
3316fe3688baSAkhil Goyal 	ev->flow_id = outq->ev.flow_id;
3317fe3688baSAkhil Goyal 	ev->sub_event_type = outq->ev.sub_event_type;
3318fe3688baSAkhil Goyal 	ev->event_type = RTE_EVENT_TYPE_CRYPTODEV;
3319fe3688baSAkhil Goyal 	ev->op = RTE_EVENT_OP_NEW;
3320fe3688baSAkhil Goyal 	ev->sched_type = outq->ev.sched_type;
3321fe3688baSAkhil Goyal 	ev->queue_id = outq->ev.queue_id;
3322fe3688baSAkhil Goyal 	ev->priority = outq->ev.priority;
3323fe3688baSAkhil Goyal 
3324fe3688baSAkhil Goyal 	/* Save active dqrr entries */
3325fe3688baSAkhil Goyal 	index = ((uintptr_t)dqrr >> 6) & (16/*QM_DQRR_SIZE*/ - 1);
3326fe3688baSAkhil Goyal 	DPAA_PER_LCORE_DQRR_SIZE++;
3327fe3688baSAkhil Goyal 	DPAA_PER_LCORE_DQRR_HELD |= 1 << index;
3328fe3688baSAkhil Goyal 	DPAA_PER_LCORE_DQRR_MBUF(index) = ctx->op->sym->m_src;
3329fe3688baSAkhil Goyal 	ev->impl_opaque = index + 1;
3330c9a1c2e5SDavid Marchand 	*dpaa_seqn(ctx->op->sym->m_src) = (uint32_t)index + 1;
3331fe3688baSAkhil Goyal 	*bufs = (void *)ctx->op;
3332fe3688baSAkhil Goyal 
3333fe3688baSAkhil Goyal 	rte_mempool_put(ctx->ctx_pool, (void *)ctx);
3334fe3688baSAkhil Goyal 
3335fe3688baSAkhil Goyal 	return qman_cb_dqrr_defer;
3336fe3688baSAkhil Goyal }
3337fe3688baSAkhil Goyal 
3338fe3688baSAkhil Goyal int
3339fe3688baSAkhil Goyal dpaa_sec_eventq_attach(const struct rte_cryptodev *dev,
3340fe3688baSAkhil Goyal 		int qp_id,
3341fe3688baSAkhil Goyal 		uint16_t ch_id,
3342fe3688baSAkhil Goyal 		const struct rte_event *event)
3343fe3688baSAkhil Goyal {
3344fe3688baSAkhil Goyal 	struct dpaa_sec_qp *qp = dev->data->queue_pairs[qp_id];
3345fe3688baSAkhil Goyal 	struct qm_mcc_initfq opts = {0};
3346fe3688baSAkhil Goyal 
3347fe3688baSAkhil Goyal 	int ret;
3348fe3688baSAkhil Goyal 
3349fe3688baSAkhil Goyal 	opts.we_mask = QM_INITFQ_WE_DESTWQ | QM_INITFQ_WE_FQCTRL |
3350fe3688baSAkhil Goyal 		       QM_INITFQ_WE_CONTEXTA | QM_INITFQ_WE_CONTEXTB;
3351fe3688baSAkhil Goyal 	opts.fqd.dest.channel = ch_id;
3352fe3688baSAkhil Goyal 
3353fe3688baSAkhil Goyal 	switch (event->sched_type) {
3354fe3688baSAkhil Goyal 	case RTE_SCHED_TYPE_ATOMIC:
3355fe3688baSAkhil Goyal 		opts.fqd.fq_ctrl |= QM_FQCTRL_HOLDACTIVE;
3356fe3688baSAkhil Goyal 		/* Reset FQCTRL_AVOIDBLOCK bit as it is unnecessary
3357fe3688baSAkhil Goyal 		 * configuration with HOLD_ACTIVE setting
3358fe3688baSAkhil Goyal 		 */
3359fe3688baSAkhil Goyal 		opts.fqd.fq_ctrl &= (~QM_FQCTRL_AVOIDBLOCK);
3360fe3688baSAkhil Goyal 		qp->outq.cb.dqrr_dpdk_cb = dpaa_sec_process_atomic_event;
3361fe3688baSAkhil Goyal 		break;
3362fe3688baSAkhil Goyal 	case RTE_SCHED_TYPE_ORDERED:
3363fe3688baSAkhil Goyal 		DPAA_SEC_ERR("Ordered queue schedule type is not supported\n");
3364c08ced9aSAkhil Goyal 		return -ENOTSUP;
3365fe3688baSAkhil Goyal 	default:
3366fe3688baSAkhil Goyal 		opts.fqd.fq_ctrl |= QM_FQCTRL_AVOIDBLOCK;
3367fe3688baSAkhil Goyal 		qp->outq.cb.dqrr_dpdk_cb = dpaa_sec_process_parallel_event;
3368fe3688baSAkhil Goyal 		break;
3369fe3688baSAkhil Goyal 	}
3370fe3688baSAkhil Goyal 
3371fe3688baSAkhil Goyal 	ret = qman_init_fq(&qp->outq, QMAN_INITFQ_FLAG_SCHED, &opts);
3372fe3688baSAkhil Goyal 	if (unlikely(ret)) {
3373fe3688baSAkhil Goyal 		DPAA_SEC_ERR("unable to init caam source fq!");
3374fe3688baSAkhil Goyal 		return ret;
3375fe3688baSAkhil Goyal 	}
3376fe3688baSAkhil Goyal 
3377fe3688baSAkhil Goyal 	memcpy(&qp->outq.ev, event, sizeof(struct rte_event));
3378fe3688baSAkhil Goyal 
3379fe3688baSAkhil Goyal 	return 0;
3380fe3688baSAkhil Goyal }
3381fe3688baSAkhil Goyal 
3382fe3688baSAkhil Goyal int
3383fe3688baSAkhil Goyal dpaa_sec_eventq_detach(const struct rte_cryptodev *dev,
3384fe3688baSAkhil Goyal 			int qp_id)
3385fe3688baSAkhil Goyal {
3386fe3688baSAkhil Goyal 	struct qm_mcc_initfq opts = {0};
3387fe3688baSAkhil Goyal 	int ret;
3388fe3688baSAkhil Goyal 	struct dpaa_sec_qp *qp = dev->data->queue_pairs[qp_id];
3389fe3688baSAkhil Goyal 
3390fe3688baSAkhil Goyal 	opts.we_mask = QM_INITFQ_WE_DESTWQ | QM_INITFQ_WE_FQCTRL |
3391fe3688baSAkhil Goyal 		       QM_INITFQ_WE_CONTEXTA | QM_INITFQ_WE_CONTEXTB;
3392fe3688baSAkhil Goyal 	qp->outq.cb.dqrr = dqrr_out_fq_cb_rx;
3393fe3688baSAkhil Goyal 	qp->outq.cb.ern  = ern_sec_fq_handler;
3394fe3688baSAkhil Goyal 	qman_retire_fq(&qp->outq, NULL);
3395fe3688baSAkhil Goyal 	qman_oos_fq(&qp->outq);
3396fe3688baSAkhil Goyal 	ret = qman_init_fq(&qp->outq, 0, &opts);
3397fe3688baSAkhil Goyal 	if (ret)
3398fe3688baSAkhil Goyal 		RTE_LOG(ERR, PMD, "Error in qman_init_fq: ret: %d\n", ret);
3399fe3688baSAkhil Goyal 	qp->outq.cb.dqrr = NULL;
3400fe3688baSAkhil Goyal 
3401fe3688baSAkhil Goyal 	return ret;
3402fe3688baSAkhil Goyal }
3403fe3688baSAkhil Goyal 
3404c3e85bdcSAkhil Goyal static struct rte_cryptodev_ops crypto_ops = {
3405c3e85bdcSAkhil Goyal 	.dev_configure	      = dpaa_sec_dev_configure,
3406c3e85bdcSAkhil Goyal 	.dev_start	      = dpaa_sec_dev_start,
3407c3e85bdcSAkhil Goyal 	.dev_stop	      = dpaa_sec_dev_stop,
3408c3e85bdcSAkhil Goyal 	.dev_close	      = dpaa_sec_dev_close,
3409c3e85bdcSAkhil Goyal 	.dev_infos_get        = dpaa_sec_dev_infos_get,
3410c3e85bdcSAkhil Goyal 	.queue_pair_setup     = dpaa_sec_queue_pair_setup,
3411c3e85bdcSAkhil Goyal 	.queue_pair_release   = dpaa_sec_queue_pair_release,
3412012c5076SPablo de Lara 	.sym_session_get_size     = dpaa_sec_sym_session_get_size,
3413012c5076SPablo de Lara 	.sym_session_configure    = dpaa_sec_sym_session_configure,
34149d5f73c2SGagandeep Singh 	.sym_session_clear        = dpaa_sec_sym_session_clear,
34159d5f73c2SGagandeep Singh 	/* Raw data-path API related operations */
34169d5f73c2SGagandeep Singh 	.sym_get_raw_dp_ctx_size = dpaa_sec_get_dp_ctx_size,
34179d5f73c2SGagandeep Singh 	.sym_configure_raw_dp_ctx = dpaa_sec_configure_raw_dp_ctx,
3418c3e85bdcSAkhil Goyal };
3419c3e85bdcSAkhil Goyal 
3420a8d0d473SBruce Richardson #ifdef RTE_LIB_SECURITY
34211f14d500SAkhil Goyal static const struct rte_security_capability *
34221f14d500SAkhil Goyal dpaa_sec_capabilities_get(void *device __rte_unused)
34231f14d500SAkhil Goyal {
34241f14d500SAkhil Goyal 	return dpaa_sec_security_cap;
34251f14d500SAkhil Goyal }
34261f14d500SAkhil Goyal 
3427b74fd6b8SFerruh Yigit static const struct rte_security_ops dpaa_sec_security_ops = {
34281f14d500SAkhil Goyal 	.session_create = dpaa_sec_security_session_create,
34291f14d500SAkhil Goyal 	.session_update = NULL,
34301f14d500SAkhil Goyal 	.session_stats_get = NULL,
34311f14d500SAkhil Goyal 	.session_destroy = dpaa_sec_security_session_destroy,
34321f14d500SAkhil Goyal 	.set_pkt_metadata = NULL,
34331f14d500SAkhil Goyal 	.capabilities_get = dpaa_sec_capabilities_get
34341f14d500SAkhil Goyal };
3435314424b6SHemant Agrawal #endif
3436c3e85bdcSAkhil Goyal static int
3437c3e85bdcSAkhil Goyal dpaa_sec_uninit(struct rte_cryptodev *dev)
3438c3e85bdcSAkhil Goyal {
3439debef417SShreyansh Jain 	struct dpaa_sec_dev_private *internals;
3440c3e85bdcSAkhil Goyal 
3441c3e85bdcSAkhil Goyal 	if (dev == NULL)
3442c3e85bdcSAkhil Goyal 		return -ENODEV;
3443c3e85bdcSAkhil Goyal 
3444debef417SShreyansh Jain 	internals = dev->data->dev_private;
34451f14d500SAkhil Goyal 	rte_free(dev->security_ctx);
34461f14d500SAkhil Goyal 
3447c3e85bdcSAkhil Goyal 	rte_free(internals);
3448c3e85bdcSAkhil Goyal 
3449f163231eSHemant Agrawal 	DPAA_SEC_INFO("Closing DPAA_SEC device %s on numa socket %u",
3450c3e85bdcSAkhil Goyal 		      dev->data->name, rte_socket_id());
3451c3e85bdcSAkhil Goyal 
3452c3e85bdcSAkhil Goyal 	return 0;
3453c3e85bdcSAkhil Goyal }
3454c3e85bdcSAkhil Goyal 
3455c3e85bdcSAkhil Goyal static int
3456c3e85bdcSAkhil Goyal dpaa_sec_dev_init(struct rte_cryptodev *cryptodev)
3457c3e85bdcSAkhil Goyal {
3458c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals;
3459a8d0d473SBruce Richardson #ifdef RTE_LIB_SECURITY
34601f14d500SAkhil Goyal 	struct rte_security_ctx *security_instance;
3461314424b6SHemant Agrawal #endif
3462c3e85bdcSAkhil Goyal 	struct dpaa_sec_qp *qp;
3463e79416d1SHemant Agrawal 	uint32_t i, flags;
3464c3e85bdcSAkhil Goyal 	int ret;
3465c3e85bdcSAkhil Goyal 
3466c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
3467c3e85bdcSAkhil Goyal 
34689d5f73c2SGagandeep Singh 	cryptodev->driver_id = dpaa_cryptodev_driver_id;
3469c3e85bdcSAkhil Goyal 	cryptodev->dev_ops = &crypto_ops;
3470c3e85bdcSAkhil Goyal 
3471c3e85bdcSAkhil Goyal 	cryptodev->enqueue_burst = dpaa_sec_enqueue_burst;
3472c3e85bdcSAkhil Goyal 	cryptodev->dequeue_burst = dpaa_sec_dequeue_burst;
3473c3e85bdcSAkhil Goyal 	cryptodev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
3474c3e85bdcSAkhil Goyal 			RTE_CRYPTODEV_FF_HW_ACCELERATED |
34751f14d500SAkhil Goyal 			RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
3476a74af788SAkhil Goyal 			RTE_CRYPTODEV_FF_SECURITY |
34779d5f73c2SGagandeep Singh 			RTE_CRYPTODEV_FF_SYM_RAW_DP |
34782717246eSPablo de Lara 			RTE_CRYPTODEV_FF_IN_PLACE_SGL |
34792717246eSPablo de Lara 			RTE_CRYPTODEV_FF_OOP_SGL_IN_SGL_OUT |
34802717246eSPablo de Lara 			RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT |
34812717246eSPablo de Lara 			RTE_CRYPTODEV_FF_OOP_LB_IN_SGL_OUT |
34822717246eSPablo de Lara 			RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT;
3483c3e85bdcSAkhil Goyal 
3484c3e85bdcSAkhil Goyal 	internals = cryptodev->data->dev_private;
3485e79416d1SHemant Agrawal 	internals->max_nb_queue_pairs = RTE_DPAA_MAX_NB_SEC_QPS;
3486c3e85bdcSAkhil Goyal 	internals->max_nb_sessions = RTE_DPAA_SEC_PMD_MAX_NB_SESSIONS;
3487c3e85bdcSAkhil Goyal 
34881f14d500SAkhil Goyal 	/*
34891f14d500SAkhil Goyal 	 * For secondary processes, we don't initialise any further as primary
34901f14d500SAkhil Goyal 	 * has already done this work. Only check we don't need a different
34911f14d500SAkhil Goyal 	 * RX function
34921f14d500SAkhil Goyal 	 */
34931f14d500SAkhil Goyal 	if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
3494f163231eSHemant Agrawal 		DPAA_SEC_WARN("Device already init by primary process");
34951f14d500SAkhil Goyal 		return 0;
34961f14d500SAkhil Goyal 	}
3497a8d0d473SBruce Richardson #ifdef RTE_LIB_SECURITY
34981f14d500SAkhil Goyal 	/* Initialize security_ctx only for primary process*/
34991f14d500SAkhil Goyal 	security_instance = rte_malloc("rte_security_instances_ops",
35001f14d500SAkhil Goyal 				sizeof(struct rte_security_ctx), 0);
35011f14d500SAkhil Goyal 	if (security_instance == NULL)
35021f14d500SAkhil Goyal 		return -ENOMEM;
35031f14d500SAkhil Goyal 	security_instance->device = (void *)cryptodev;
35041f14d500SAkhil Goyal 	security_instance->ops = &dpaa_sec_security_ops;
35051f14d500SAkhil Goyal 	security_instance->sess_cnt = 0;
35061f14d500SAkhil Goyal 	cryptodev->security_ctx = security_instance;
3507314424b6SHemant Agrawal #endif
35083b617ee7SAkhil Goyal 	rte_spinlock_init(&internals->lock);
3509c3e85bdcSAkhil Goyal 	for (i = 0; i < internals->max_nb_queue_pairs; i++) {
3510c3e85bdcSAkhil Goyal 		/* init qman fq for queue pair */
3511c3e85bdcSAkhil Goyal 		qp = &internals->qps[i];
3512c3e85bdcSAkhil Goyal 		ret = dpaa_sec_init_tx(&qp->outq);
3513c3e85bdcSAkhil Goyal 		if (ret) {
3514f163231eSHemant Agrawal 			DPAA_SEC_ERR("config tx of queue pair  %d", i);
3515c3e85bdcSAkhil Goyal 			goto init_error;
3516c3e85bdcSAkhil Goyal 		}
3517e79416d1SHemant Agrawal 	}
3518e79416d1SHemant Agrawal 
3519e79416d1SHemant Agrawal 	flags = QMAN_FQ_FLAG_LOCKED | QMAN_FQ_FLAG_DYNAMIC_FQID |
3520e79416d1SHemant Agrawal 		QMAN_FQ_FLAG_TO_DCPORTAL;
3521fd900d38SGagandeep Singh 	for (i = 0; i < RTE_DPAA_MAX_RX_QUEUE; i++) {
3522e79416d1SHemant Agrawal 		/* create rx qman fq for sessions*/
3523e79416d1SHemant Agrawal 		ret = qman_create_fq(0, flags, &internals->inq[i]);
3524e79416d1SHemant Agrawal 		if (unlikely(ret != 0)) {
3525f163231eSHemant Agrawal 			DPAA_SEC_ERR("sec qman_create_fq failed");
3526c3e85bdcSAkhil Goyal 			goto init_error;
3527c3e85bdcSAkhil Goyal 		}
3528c3e85bdcSAkhil Goyal 	}
3529c3e85bdcSAkhil Goyal 
3530f163231eSHemant Agrawal 	RTE_LOG(INFO, PMD, "%s cryptodev init\n", cryptodev->data->name);
3531c3e85bdcSAkhil Goyal 	return 0;
3532c3e85bdcSAkhil Goyal 
3533c3e85bdcSAkhil Goyal init_error:
3534f163231eSHemant Agrawal 	DPAA_SEC_ERR("driver %s: create failed\n", cryptodev->data->name);
3535c3e85bdcSAkhil Goyal 
35362eaf352dSLukasz Wojciechowski 	rte_free(cryptodev->security_ctx);
3537c3e85bdcSAkhil Goyal 	return -EFAULT;
3538c3e85bdcSAkhil Goyal }
3539c3e85bdcSAkhil Goyal 
3540c3e85bdcSAkhil Goyal static int
3541eb5a9a76SShreyansh Jain cryptodev_dpaa_sec_probe(struct rte_dpaa_driver *dpaa_drv __rte_unused,
3542c3e85bdcSAkhil Goyal 				struct rte_dpaa_device *dpaa_dev)
3543c3e85bdcSAkhil Goyal {
3544c3e85bdcSAkhil Goyal 	struct rte_cryptodev *cryptodev;
3545c3e85bdcSAkhil Goyal 	char cryptodev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
3546c3e85bdcSAkhil Goyal 
3547c3e85bdcSAkhil Goyal 	int retval;
3548c3e85bdcSAkhil Goyal 
35490964a951SHemant Agrawal 	snprintf(cryptodev_name, sizeof(cryptodev_name), "%s", dpaa_dev->name);
3550c3e85bdcSAkhil Goyal 
3551c3e85bdcSAkhil Goyal 	cryptodev = rte_cryptodev_pmd_allocate(cryptodev_name, rte_socket_id());
3552c3e85bdcSAkhil Goyal 	if (cryptodev == NULL)
3553c3e85bdcSAkhil Goyal 		return -ENOMEM;
3554c3e85bdcSAkhil Goyal 
3555c3e85bdcSAkhil Goyal 	if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
3556c3e85bdcSAkhil Goyal 		cryptodev->data->dev_private = rte_zmalloc_socket(
3557c3e85bdcSAkhil Goyal 					"cryptodev private structure",
3558c3e85bdcSAkhil Goyal 					sizeof(struct dpaa_sec_dev_private),
3559c3e85bdcSAkhil Goyal 					RTE_CACHE_LINE_SIZE,
3560c3e85bdcSAkhil Goyal 					rte_socket_id());
3561c3e85bdcSAkhil Goyal 
3562c3e85bdcSAkhil Goyal 		if (cryptodev->data->dev_private == NULL)
3563c3e85bdcSAkhil Goyal 			rte_panic("Cannot allocate memzone for private "
3564c3e85bdcSAkhil Goyal 					"device data");
3565c3e85bdcSAkhil Goyal 	}
3566c3e85bdcSAkhil Goyal 
3567c3e85bdcSAkhil Goyal 	dpaa_dev->crypto_dev = cryptodev;
3568c3e85bdcSAkhil Goyal 	cryptodev->device = &dpaa_dev->device;
3569c3e85bdcSAkhil Goyal 
3570c3e85bdcSAkhil Goyal 	/* init user callbacks */
3571c3e85bdcSAkhil Goyal 	TAILQ_INIT(&(cryptodev->link_intr_cbs));
3572c3e85bdcSAkhil Goyal 
3573c3e85bdcSAkhil Goyal 	/* if sec device version is not configured */
3574c3e85bdcSAkhil Goyal 	if (!rta_get_sec_era()) {
3575c3e85bdcSAkhil Goyal 		const struct device_node *caam_node;
3576c3e85bdcSAkhil Goyal 
3577c3e85bdcSAkhil Goyal 		for_each_compatible_node(caam_node, NULL, "fsl,sec-v4.0") {
3578c3e85bdcSAkhil Goyal 			const uint32_t *prop = of_get_property(caam_node,
3579c3e85bdcSAkhil Goyal 					"fsl,sec-era",
3580c3e85bdcSAkhil Goyal 					NULL);
3581c3e85bdcSAkhil Goyal 			if (prop) {
3582c3e85bdcSAkhil Goyal 				rta_set_sec_era(
3583c3e85bdcSAkhil Goyal 					INTL_SEC_ERA(rte_cpu_to_be_32(*prop)));
3584c3e85bdcSAkhil Goyal 				break;
3585c3e85bdcSAkhil Goyal 			}
3586c3e85bdcSAkhil Goyal 		}
3587c3e85bdcSAkhil Goyal 	}
3588c3e85bdcSAkhil Goyal 
3589e5872221SRohit Raj 	if (unlikely(!DPAA_PER_LCORE_PORTAL)) {
3590408077f2SHemant Agrawal 		retval = rte_dpaa_portal_init((void *)1);
3591408077f2SHemant Agrawal 		if (retval) {
3592408077f2SHemant Agrawal 			DPAA_SEC_ERR("Unable to initialize portal");
35932eaf352dSLukasz Wojciechowski 			goto out;
3594408077f2SHemant Agrawal 		}
3595408077f2SHemant Agrawal 	}
3596408077f2SHemant Agrawal 
3597c3e85bdcSAkhil Goyal 	/* Invoke PMD device initialization function */
3598c3e85bdcSAkhil Goyal 	retval = dpaa_sec_dev_init(cryptodev);
3599d54c72ecSAkhil Goyal 	if (retval == 0) {
3600d54c72ecSAkhil Goyal 		rte_cryptodev_pmd_probing_finish(cryptodev);
3601c3e85bdcSAkhil Goyal 		return 0;
3602d54c72ecSAkhil Goyal 	}
3603c3e85bdcSAkhil Goyal 
36042eaf352dSLukasz Wojciechowski 	retval = -ENXIO;
36052eaf352dSLukasz Wojciechowski out:
3606c3e85bdcSAkhil Goyal 	/* In case of error, cleanup is done */
3607c3e85bdcSAkhil Goyal 	if (rte_eal_process_type() == RTE_PROC_PRIMARY)
3608c3e85bdcSAkhil Goyal 		rte_free(cryptodev->data->dev_private);
3609c3e85bdcSAkhil Goyal 
3610c3e85bdcSAkhil Goyal 	rte_cryptodev_pmd_release_device(cryptodev);
3611c3e85bdcSAkhil Goyal 
36122eaf352dSLukasz Wojciechowski 	return retval;
3613c3e85bdcSAkhil Goyal }
3614c3e85bdcSAkhil Goyal 
3615c3e85bdcSAkhil Goyal static int
3616c3e85bdcSAkhil Goyal cryptodev_dpaa_sec_remove(struct rte_dpaa_device *dpaa_dev)
3617c3e85bdcSAkhil Goyal {
3618c3e85bdcSAkhil Goyal 	struct rte_cryptodev *cryptodev;
3619c3e85bdcSAkhil Goyal 	int ret;
3620c3e85bdcSAkhil Goyal 
3621c3e85bdcSAkhil Goyal 	cryptodev = dpaa_dev->crypto_dev;
3622c3e85bdcSAkhil Goyal 	if (cryptodev == NULL)
3623c3e85bdcSAkhil Goyal 		return -ENODEV;
3624c3e85bdcSAkhil Goyal 
3625c3e85bdcSAkhil Goyal 	ret = dpaa_sec_uninit(cryptodev);
3626c3e85bdcSAkhil Goyal 	if (ret)
3627c3e85bdcSAkhil Goyal 		return ret;
3628c3e85bdcSAkhil Goyal 
3629f2f020d2SDeclan Doherty 	return rte_cryptodev_pmd_destroy(cryptodev);
3630c3e85bdcSAkhil Goyal }
3631c3e85bdcSAkhil Goyal 
3632c3e85bdcSAkhil Goyal static struct rte_dpaa_driver rte_dpaa_sec_driver = {
3633c3e85bdcSAkhil Goyal 	.drv_type = FSL_DPAA_CRYPTO,
3634c3e85bdcSAkhil Goyal 	.driver = {
3635c3e85bdcSAkhil Goyal 		.name = "DPAA SEC PMD"
3636c3e85bdcSAkhil Goyal 	},
3637c3e85bdcSAkhil Goyal 	.probe = cryptodev_dpaa_sec_probe,
3638c3e85bdcSAkhil Goyal 	.remove = cryptodev_dpaa_sec_remove,
3639c3e85bdcSAkhil Goyal };
3640c3e85bdcSAkhil Goyal 
3641c3e85bdcSAkhil Goyal static struct cryptodev_driver dpaa_sec_crypto_drv;
3642c3e85bdcSAkhil Goyal 
3643c3e85bdcSAkhil Goyal RTE_PMD_REGISTER_DPAA(CRYPTODEV_NAME_DPAA_SEC_PMD, rte_dpaa_sec_driver);
3644f737f5ceSFiona Trahe RTE_PMD_REGISTER_CRYPTO_DRIVER(dpaa_sec_crypto_drv, rte_dpaa_sec_driver.driver,
36459d5f73c2SGagandeep Singh 		dpaa_cryptodev_driver_id);
36469c99878aSJerin Jacob RTE_LOG_REGISTER(dpaa_logtype_sec, pmd.crypto.dpaa, NOTICE);
3647