xref: /dpdk/drivers/crypto/dpaa_sec/dpaa_sec.c (revision bdce2564dbf78e1fecc0db438b562ae19f0c057c)
1d81734caSHemant Agrawal /* SPDX-License-Identifier: BSD-3-Clause
2c3e85bdcSAkhil Goyal  *
3c3e85bdcSAkhil Goyal  *   Copyright (c) 2016 Freescale Semiconductor, Inc. All rights reserved.
48a3167dbSGagandeep Singh  *   Copyright 2017-2022 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>
221acb7f54SDavid Marchand #include <dev_driver.h>
238a3167dbSGagandeep Singh #include <rte_io.h>
24bd063651SFerruh Yigit #include <rte_ip.h>
25c3e85bdcSAkhil Goyal #include <rte_kvargs.h>
26c3e85bdcSAkhil Goyal #include <rte_malloc.h>
27c3e85bdcSAkhil Goyal #include <rte_mbuf.h>
28c3e85bdcSAkhil Goyal #include <rte_memcpy.h>
29c3e85bdcSAkhil Goyal #include <rte_string_fns.h>
303b617ee7SAkhil Goyal #include <rte_spinlock.h>
31b1bbf222SGagandeep Singh #include <rte_hexdump.h>
32c3e85bdcSAkhil Goyal 
33c3e85bdcSAkhil Goyal #include <fsl_usd.h>
34c3e85bdcSAkhil Goyal #include <fsl_qman.h>
358c83f28cSHemant Agrawal #include <dpaa_of.h>
36c3e85bdcSAkhil Goyal 
37c3e85bdcSAkhil Goyal /* RTA header files */
38c0ded849SHemant Agrawal #include <desc/common.h>
39c0ded849SHemant Agrawal #include <desc/algo.h>
40c0ded849SHemant Agrawal #include <desc/ipsec.h>
41c0ded849SHemant Agrawal #include <desc/pdcp.h>
425a4954bcSAkhil Goyal #include <desc/sdap.h>
43c3e85bdcSAkhil Goyal 
44a2f1da7dSDavid Marchand #include <bus_dpaa_driver.h>
45c3e85bdcSAkhil Goyal #include <dpaa_sec.h>
46fe3688baSAkhil Goyal #include <dpaa_sec_event.h>
47c3e85bdcSAkhil Goyal #include <dpaa_sec_log.h>
4812e58429SThierry Herbelot #include <dpaax_iova_table.h>
49c3e85bdcSAkhil Goyal 
50b1bbf222SGagandeep Singh #define DRIVER_DUMP_MODE "drv_dump_mode"
51b1bbf222SGagandeep Singh 
52b1bbf222SGagandeep Singh /* DPAA_SEC_DP_DUMP levels */
53b1bbf222SGagandeep Singh enum dpaa_sec_dump_levels {
54b1bbf222SGagandeep Singh 	DPAA_SEC_DP_NO_DUMP,
55b1bbf222SGagandeep Singh 	DPAA_SEC_DP_ERR_DUMP,
56b1bbf222SGagandeep Singh 	DPAA_SEC_DP_FULL_DUMP
57b1bbf222SGagandeep Singh };
58b1bbf222SGagandeep Singh 
59b1bbf222SGagandeep Singh uint8_t dpaa_sec_dp_dump = DPAA_SEC_DP_ERR_DUMP;
60b1bbf222SGagandeep Singh 
619d5f73c2SGagandeep Singh uint8_t dpaa_cryptodev_driver_id;
62e79416d1SHemant Agrawal 
63c3e85bdcSAkhil Goyal static inline void
64c3e85bdcSAkhil Goyal dpaa_sec_op_ending(struct dpaa_sec_op_ctx *ctx)
65c3e85bdcSAkhil Goyal {
66c3e85bdcSAkhil Goyal 	if (!ctx->fd_status) {
67c3e85bdcSAkhil Goyal 		ctx->op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
68c3e85bdcSAkhil Goyal 	} else {
69f163231eSHemant Agrawal 		DPAA_SEC_DP_WARN("SEC return err: 0x%x", ctx->fd_status);
70c3e85bdcSAkhil Goyal 		ctx->op->status = RTE_CRYPTO_OP_STATUS_ERROR;
71c3e85bdcSAkhil Goyal 	}
72c3e85bdcSAkhil Goyal }
73c3e85bdcSAkhil Goyal 
74c3e85bdcSAkhil Goyal static inline struct dpaa_sec_op_ctx *
75f7a5752eSHemant Agrawal dpaa_sec_alloc_ctx(dpaa_sec_session *ses, int sg_count)
76c3e85bdcSAkhil Goyal {
77c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
78f7a5752eSHemant Agrawal 	int i, retval;
79c3e85bdcSAkhil Goyal 
802ffb940eSAkhil Goyal 	retval = rte_mempool_get(
812ffb940eSAkhil Goyal 			ses->qp[rte_lcore_id() % MAX_DPAA_CORES]->ctx_pool,
822ffb940eSAkhil Goyal 			(void **)(&ctx));
83c3e85bdcSAkhil Goyal 	if (!ctx || retval) {
84f163231eSHemant Agrawal 		DPAA_SEC_DP_WARN("Alloc sec descriptor failed!");
85c3e85bdcSAkhil Goyal 		return NULL;
86c3e85bdcSAkhil Goyal 	}
87c3e85bdcSAkhil Goyal 	/*
88c3e85bdcSAkhil Goyal 	 * Clear SG memory. There are 16 SG entries of 16 Bytes each.
89c3e85bdcSAkhil Goyal 	 * one call to dcbz_64() clear 64 bytes, hence calling it 4 times
90c3e85bdcSAkhil Goyal 	 * to clear all the SG entries. dpaa_sec_alloc_ctx() is called for
91c3e85bdcSAkhil Goyal 	 * each packet, memset is costlier than dcbz_64().
92c3e85bdcSAkhil Goyal 	 */
93f7a5752eSHemant Agrawal 	for (i = 0; i < sg_count && i < MAX_JOB_SG_ENTRIES; i += 4)
94f7a5752eSHemant Agrawal 		dcbz_64(&ctx->job.sg[i]);
95c3e85bdcSAkhil Goyal 
962ffb940eSAkhil Goyal 	ctx->ctx_pool = ses->qp[rte_lcore_id() % MAX_DPAA_CORES]->ctx_pool;
97f7a5752eSHemant Agrawal 	ctx->vtop_offset = (size_t) ctx - rte_mempool_virt2iova(ctx);
98c3e85bdcSAkhil Goyal 
99c3e85bdcSAkhil Goyal 	return ctx;
100c3e85bdcSAkhil Goyal }
101c3e85bdcSAkhil Goyal 
102c3e85bdcSAkhil Goyal static void
103c3e85bdcSAkhil Goyal ern_sec_fq_handler(struct qman_portal *qm __rte_unused,
104c3e85bdcSAkhil Goyal 		   struct qman_fq *fq,
105c3e85bdcSAkhil Goyal 		   const struct qm_mr_entry *msg)
106c3e85bdcSAkhil Goyal {
107f163231eSHemant Agrawal 	DPAA_SEC_DP_ERR("sec fq %d error, RC = %x, seqnum = %x\n",
108c3e85bdcSAkhil Goyal 			fq->fqid, msg->ern.rc, msg->ern.seqnum);
109c3e85bdcSAkhil Goyal }
110c3e85bdcSAkhil Goyal 
111c3e85bdcSAkhil Goyal /* initialize the queue with dest chan as caam chan so that
112c3e85bdcSAkhil Goyal  * all the packets in this queue could be dispatched into caam
113c3e85bdcSAkhil Goyal  */
114c3e85bdcSAkhil Goyal static int
115c4509373SSantosh Shukla dpaa_sec_init_rx(struct qman_fq *fq_in, rte_iova_t hwdesc,
116c3e85bdcSAkhil Goyal 		 uint32_t fqid_out)
117c3e85bdcSAkhil Goyal {
118c3e85bdcSAkhil Goyal 	struct qm_mcc_initfq fq_opts;
119c3e85bdcSAkhil Goyal 	uint32_t flags;
120c3e85bdcSAkhil Goyal 	int ret = -1;
121c3e85bdcSAkhil Goyal 
122c3e85bdcSAkhil Goyal 	/* Clear FQ options */
123c3e85bdcSAkhil Goyal 	memset(&fq_opts, 0x00, sizeof(struct qm_mcc_initfq));
124c3e85bdcSAkhil Goyal 
125c3e85bdcSAkhil Goyal 	flags = QMAN_INITFQ_FLAG_SCHED;
126c3e85bdcSAkhil Goyal 	fq_opts.we_mask = QM_INITFQ_WE_DESTWQ | QM_INITFQ_WE_CONTEXTA |
127c3e85bdcSAkhil Goyal 			  QM_INITFQ_WE_CONTEXTB;
128c3e85bdcSAkhil Goyal 
129c3e85bdcSAkhil Goyal 	qm_fqd_context_a_set64(&fq_opts.fqd, hwdesc);
130c3e85bdcSAkhil Goyal 	fq_opts.fqd.context_b = fqid_out;
131a8ee206aSHemant Agrawal 	fq_opts.fqd.dest.channel = dpaa_get_qm_channel_caam();
132c3e85bdcSAkhil Goyal 	fq_opts.fqd.dest.wq = 0;
133c3e85bdcSAkhil Goyal 
134c3e85bdcSAkhil Goyal 	fq_in->cb.ern  = ern_sec_fq_handler;
135c3e85bdcSAkhil Goyal 
136f163231eSHemant Agrawal 	DPAA_SEC_DEBUG("in-%x out-%x", fq_in->fqid, fqid_out);
137e79416d1SHemant Agrawal 
138c3e85bdcSAkhil Goyal 	ret = qman_init_fq(fq_in, flags, &fq_opts);
139c3e85bdcSAkhil Goyal 	if (unlikely(ret != 0))
140f163231eSHemant Agrawal 		DPAA_SEC_ERR("qman_init_fq failed %d", ret);
141c3e85bdcSAkhil Goyal 
142c3e85bdcSAkhil Goyal 	return ret;
143c3e85bdcSAkhil Goyal }
144c3e85bdcSAkhil Goyal 
145c3e85bdcSAkhil Goyal /* something is put into in_fq and caam put the crypto result into out_fq */
146c3e85bdcSAkhil Goyal static enum qman_cb_dqrr_result
147c3e85bdcSAkhil Goyal dqrr_out_fq_cb_rx(struct qman_portal *qm __always_unused,
148c3e85bdcSAkhil Goyal 		  struct qman_fq *fq __always_unused,
149c3e85bdcSAkhil Goyal 		  const struct qm_dqrr_entry *dqrr)
150c3e85bdcSAkhil Goyal {
151c3e85bdcSAkhil Goyal 	const struct qm_fd *fd;
152c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *job;
153c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
154c3e85bdcSAkhil Goyal 
155c3e85bdcSAkhil Goyal 	if (!(dqrr->stat & QM_DQRR_STAT_FD_VALID))
156c3e85bdcSAkhil Goyal 		return qman_cb_dqrr_consume;
157c3e85bdcSAkhil Goyal 
158c3e85bdcSAkhil Goyal 	fd = &dqrr->fd;
159c3e85bdcSAkhil Goyal 	/* sg is embedded in an op ctx,
160c3e85bdcSAkhil Goyal 	 * sg[0] is for output
161c3e85bdcSAkhil Goyal 	 * sg[1] for input
162c3e85bdcSAkhil Goyal 	 */
163ec861560SGagandeep Singh 	job = rte_dpaa_mem_ptov(qm_fd_addr_get64(fd));
1641f14d500SAkhil Goyal 
165c3e85bdcSAkhil Goyal 	ctx = container_of(job, struct dpaa_sec_op_ctx, job);
166c3e85bdcSAkhil Goyal 	ctx->fd_status = fd->status;
1671f14d500SAkhil Goyal 	if (ctx->op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
1681f14d500SAkhil Goyal 		struct qm_sg_entry *sg_out;
1691f14d500SAkhil Goyal 		uint32_t len;
170fb5c100aSAkhil Goyal 		struct rte_mbuf *mbuf = (ctx->op->sym->m_dst == NULL) ?
171fb5c100aSAkhil Goyal 				ctx->op->sym->m_src : ctx->op->sym->m_dst;
1721f14d500SAkhil Goyal 
1731f14d500SAkhil Goyal 		sg_out = &job->sg[0];
1741f14d500SAkhil Goyal 		hw_sg_to_cpu(sg_out);
1751f14d500SAkhil Goyal 		len = sg_out->length;
176fb5c100aSAkhil Goyal 		mbuf->pkt_len = len;
177fb5c100aSAkhil Goyal 		while (mbuf->next != NULL) {
178fb5c100aSAkhil Goyal 			len -= mbuf->data_len;
179fb5c100aSAkhil Goyal 			mbuf = mbuf->next;
180fb5c100aSAkhil Goyal 		}
181fb5c100aSAkhil Goyal 		mbuf->data_len = len;
1821f14d500SAkhil Goyal 	}
183c3e85bdcSAkhil Goyal 	dpaa_sec_op_ending(ctx);
184c3e85bdcSAkhil Goyal 
185c3e85bdcSAkhil Goyal 	return qman_cb_dqrr_consume;
186c3e85bdcSAkhil Goyal }
187c3e85bdcSAkhil Goyal 
188c3e85bdcSAkhil Goyal /* caam result is put into this queue */
189c3e85bdcSAkhil Goyal static int
190c3e85bdcSAkhil Goyal dpaa_sec_init_tx(struct qman_fq *fq)
191c3e85bdcSAkhil Goyal {
192c3e85bdcSAkhil Goyal 	int ret;
193c3e85bdcSAkhil Goyal 	struct qm_mcc_initfq opts;
194c3e85bdcSAkhil Goyal 	uint32_t flags;
195c3e85bdcSAkhil Goyal 
196c3e85bdcSAkhil Goyal 	flags = QMAN_FQ_FLAG_NO_ENQUEUE | QMAN_FQ_FLAG_LOCKED |
197c3e85bdcSAkhil Goyal 		QMAN_FQ_FLAG_DYNAMIC_FQID;
198c3e85bdcSAkhil Goyal 
199c3e85bdcSAkhil Goyal 	ret = qman_create_fq(0, flags, fq);
200c3e85bdcSAkhil Goyal 	if (unlikely(ret)) {
201f163231eSHemant Agrawal 		DPAA_SEC_ERR("qman_create_fq failed");
202c3e85bdcSAkhil Goyal 		return ret;
203c3e85bdcSAkhil Goyal 	}
204c3e85bdcSAkhil Goyal 
205c3e85bdcSAkhil Goyal 	memset(&opts, 0, sizeof(opts));
206c3e85bdcSAkhil Goyal 	opts.we_mask = QM_INITFQ_WE_DESTWQ | QM_INITFQ_WE_FQCTRL |
207c3e85bdcSAkhil Goyal 		       QM_INITFQ_WE_CONTEXTA | QM_INITFQ_WE_CONTEXTB;
208c3e85bdcSAkhil Goyal 
209c3e85bdcSAkhil Goyal 	/* opts.fqd.dest.channel = dpaa_sec_pool_chan; */
210c3e85bdcSAkhil Goyal 
211c3e85bdcSAkhil Goyal 	fq->cb.dqrr = dqrr_out_fq_cb_rx;
212c3e85bdcSAkhil Goyal 	fq->cb.ern  = ern_sec_fq_handler;
213c3e85bdcSAkhil Goyal 
214c3e85bdcSAkhil Goyal 	ret = qman_init_fq(fq, 0, &opts);
215c3e85bdcSAkhil Goyal 	if (unlikely(ret)) {
216f163231eSHemant Agrawal 		DPAA_SEC_ERR("unable to init caam source fq!");
217c3e85bdcSAkhil Goyal 		return ret;
218c3e85bdcSAkhil Goyal 	}
219c3e85bdcSAkhil Goyal 
220c3e85bdcSAkhil Goyal 	return ret;
221c3e85bdcSAkhil Goyal }
222c3e85bdcSAkhil Goyal 
2236290de2cSLukasz Wojciechowski static inline int is_aead(dpaa_sec_session *ses)
2246290de2cSLukasz Wojciechowski {
2256290de2cSLukasz Wojciechowski 	return ((ses->cipher_alg == 0) &&
2266290de2cSLukasz Wojciechowski 		(ses->auth_alg == 0) &&
2276290de2cSLukasz Wojciechowski 		(ses->aead_alg != 0));
2286290de2cSLukasz Wojciechowski }
2296290de2cSLukasz Wojciechowski 
230c3e85bdcSAkhil Goyal static inline int is_encode(dpaa_sec_session *ses)
231c3e85bdcSAkhil Goyal {
232c3e85bdcSAkhil Goyal 	return ses->dir == DIR_ENC;
233c3e85bdcSAkhil Goyal }
234c3e85bdcSAkhil Goyal 
235c3e85bdcSAkhil Goyal static inline int is_decode(dpaa_sec_session *ses)
236c3e85bdcSAkhil Goyal {
237c3e85bdcSAkhil Goyal 	return ses->dir == DIR_DEC;
238c3e85bdcSAkhil Goyal }
239c3e85bdcSAkhil Goyal 
240a8d0d473SBruce Richardson #ifdef RTE_LIB_SECURITY
241a1173d55SHemant Agrawal static int
242a1173d55SHemant Agrawal dpaa_sec_prep_pdcp_cdb(dpaa_sec_session *ses)
243a1173d55SHemant Agrawal {
244a1173d55SHemant Agrawal 	struct alginfo authdata = {0}, cipherdata = {0};
245a1173d55SHemant Agrawal 	struct sec_cdb *cdb = &ses->cdb;
2462e4cbdb4SVakul Garg 	struct alginfo *p_authdata = NULL;
247a1173d55SHemant Agrawal 	int32_t shared_desc_len = 0;
248a1173d55SHemant Agrawal #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
249a1173d55SHemant Agrawal 	int swap = false;
250a1173d55SHemant Agrawal #else
251a1173d55SHemant Agrawal 	int swap = true;
252a1173d55SHemant Agrawal #endif
253a1173d55SHemant Agrawal 
254a1173d55SHemant Agrawal 	cipherdata.key = (size_t)ses->cipher_key.data;
255a1173d55SHemant Agrawal 	cipherdata.keylen = ses->cipher_key.length;
256a1173d55SHemant Agrawal 	cipherdata.key_enc_flags = 0;
257a1173d55SHemant Agrawal 	cipherdata.key_type = RTA_DATA_IMM;
2588524b44eSHemant Agrawal 	cipherdata.algtype = ses->cipher_key.alg;
2598524b44eSHemant Agrawal 	cipherdata.algmode = ses->cipher_key.algmode;
260a1173d55SHemant Agrawal 
2612e4cbdb4SVakul Garg 	if (ses->auth_alg) {
262a1173d55SHemant Agrawal 		authdata.key = (size_t)ses->auth_key.data;
263a1173d55SHemant Agrawal 		authdata.keylen = ses->auth_key.length;
264a1173d55SHemant Agrawal 		authdata.key_enc_flags = 0;
265a1173d55SHemant Agrawal 		authdata.key_type = RTA_DATA_IMM;
2668524b44eSHemant Agrawal 		authdata.algtype = ses->auth_key.alg;
2678524b44eSHemant Agrawal 		authdata.algmode = ses->auth_key.algmode;
268a1173d55SHemant Agrawal 
2692e4cbdb4SVakul Garg 		p_authdata = &authdata;
2702e4cbdb4SVakul Garg 	}
2712e4cbdb4SVakul Garg 
272ebc27d1bSFranck Lenormand 	if (ses->pdcp.sdap_enabled) {
273ebc27d1bSFranck Lenormand 		int nb_keys_to_inline =
274ebc27d1bSFranck Lenormand 				rta_inline_pdcp_sdap_query(authdata.algtype,
275ebc27d1bSFranck Lenormand 					cipherdata.algtype,
276ebc27d1bSFranck Lenormand 					ses->pdcp.sn_size,
277ebc27d1bSFranck Lenormand 					ses->pdcp.hfn_ovd);
278ebc27d1bSFranck Lenormand 		if (nb_keys_to_inline >= 1) {
279ebc27d1bSFranck Lenormand 			cipherdata.key = (size_t)rte_dpaa_mem_vtop((void *)
280ebc27d1bSFranck Lenormand 						(size_t)cipherdata.key);
281ebc27d1bSFranck Lenormand 			cipherdata.key_type = RTA_DATA_PTR;
282ebc27d1bSFranck Lenormand 		}
283ebc27d1bSFranck Lenormand 		if (nb_keys_to_inline >= 2) {
284ebc27d1bSFranck Lenormand 			authdata.key = (size_t)rte_dpaa_mem_vtop((void *)
285ebc27d1bSFranck Lenormand 						(size_t)authdata.key);
286ebc27d1bSFranck Lenormand 			authdata.key_type = RTA_DATA_PTR;
287ebc27d1bSFranck Lenormand 		}
288ebc27d1bSFranck Lenormand 	} else {
289f6ab96f1SAkhil Goyal 		if (rta_inline_pdcp_query(authdata.algtype,
290f6ab96f1SAkhil Goyal 					cipherdata.algtype,
291f6ab96f1SAkhil Goyal 					ses->pdcp.sn_size,
292f6ab96f1SAkhil Goyal 					ses->pdcp.hfn_ovd)) {
293ebc27d1bSFranck Lenormand 			cipherdata.key = (size_t)rte_dpaa_mem_vtop((void *)
294f6ab96f1SAkhil Goyal 						(size_t)cipherdata.key);
295a1173d55SHemant Agrawal 			cipherdata.key_type = RTA_DATA_PTR;
296a1173d55SHemant Agrawal 		}
297ebc27d1bSFranck Lenormand 	}
298a1173d55SHemant Agrawal 
2992e4cbdb4SVakul Garg 	if (ses->pdcp.domain == RTE_SECURITY_PDCP_MODE_CONTROL) {
300a1173d55SHemant Agrawal 		if (ses->dir == DIR_ENC)
301a1173d55SHemant Agrawal 			shared_desc_len = cnstr_shdsc_pdcp_c_plane_encap(
302a1173d55SHemant Agrawal 					cdb->sh_desc, 1, swap,
303a1173d55SHemant Agrawal 					ses->pdcp.hfn,
304eac60082SVakul Garg 					ses->pdcp.sn_size,
305a1173d55SHemant Agrawal 					ses->pdcp.bearer,
306a1173d55SHemant Agrawal 					ses->pdcp.pkt_dir,
307a1173d55SHemant Agrawal 					ses->pdcp.hfn_threshold,
3086127fff8SFranck Lenormand 					&cipherdata, &authdata);
309a1173d55SHemant Agrawal 		else if (ses->dir == DIR_DEC)
310a1173d55SHemant Agrawal 			shared_desc_len = cnstr_shdsc_pdcp_c_plane_decap(
311a1173d55SHemant Agrawal 					cdb->sh_desc, 1, swap,
312a1173d55SHemant Agrawal 					ses->pdcp.hfn,
313eac60082SVakul Garg 					ses->pdcp.sn_size,
314a1173d55SHemant Agrawal 					ses->pdcp.bearer,
315a1173d55SHemant Agrawal 					ses->pdcp.pkt_dir,
316a1173d55SHemant Agrawal 					ses->pdcp.hfn_threshold,
3176127fff8SFranck Lenormand 					&cipherdata, &authdata);
31893e2661eSGagandeep Singh 	} else if (ses->pdcp.domain == RTE_SECURITY_PDCP_MODE_SHORT_MAC) {
31993e2661eSGagandeep Singh 		shared_desc_len = cnstr_shdsc_pdcp_short_mac(cdb->sh_desc,
32093e2661eSGagandeep Singh 						     1, swap, &authdata);
321a1173d55SHemant Agrawal 	} else {
3225a4954bcSAkhil Goyal 		if (ses->dir == DIR_ENC) {
3235a4954bcSAkhil Goyal 			if (ses->pdcp.sdap_enabled)
3245a4954bcSAkhil Goyal 				shared_desc_len =
3255a4954bcSAkhil Goyal 					cnstr_shdsc_pdcp_sdap_u_plane_encap(
326a1173d55SHemant Agrawal 						cdb->sh_desc, 1, swap,
327a1173d55SHemant Agrawal 						ses->pdcp.sn_size,
328a1173d55SHemant Agrawal 						ses->pdcp.hfn,
329a1173d55SHemant Agrawal 						ses->pdcp.bearer,
330a1173d55SHemant Agrawal 						ses->pdcp.pkt_dir,
331a1173d55SHemant Agrawal 						ses->pdcp.hfn_threshold,
3326127fff8SFranck Lenormand 						&cipherdata, p_authdata);
3335a4954bcSAkhil Goyal 			else
3345a4954bcSAkhil Goyal 				shared_desc_len =
3355a4954bcSAkhil Goyal 					cnstr_shdsc_pdcp_u_plane_encap(
336a1173d55SHemant Agrawal 						cdb->sh_desc, 1, swap,
337a1173d55SHemant Agrawal 						ses->pdcp.sn_size,
338a1173d55SHemant Agrawal 						ses->pdcp.hfn,
339a1173d55SHemant Agrawal 						ses->pdcp.bearer,
340a1173d55SHemant Agrawal 						ses->pdcp.pkt_dir,
341a1173d55SHemant Agrawal 						ses->pdcp.hfn_threshold,
3426127fff8SFranck Lenormand 						&cipherdata, p_authdata);
3435a4954bcSAkhil Goyal 		} else if (ses->dir == DIR_DEC) {
3445a4954bcSAkhil Goyal 			if (ses->pdcp.sdap_enabled)
3455a4954bcSAkhil Goyal 				shared_desc_len =
3465a4954bcSAkhil Goyal 					cnstr_shdsc_pdcp_sdap_u_plane_decap(
3475a4954bcSAkhil Goyal 						cdb->sh_desc, 1, swap,
3485a4954bcSAkhil Goyal 						ses->pdcp.sn_size,
3495a4954bcSAkhil Goyal 						ses->pdcp.hfn,
3505a4954bcSAkhil Goyal 						ses->pdcp.bearer,
3515a4954bcSAkhil Goyal 						ses->pdcp.pkt_dir,
3525a4954bcSAkhil Goyal 						ses->pdcp.hfn_threshold,
3536127fff8SFranck Lenormand 						&cipherdata, p_authdata);
3545a4954bcSAkhil Goyal 			else
3555a4954bcSAkhil Goyal 				shared_desc_len =
3565a4954bcSAkhil Goyal 					cnstr_shdsc_pdcp_u_plane_decap(
3575a4954bcSAkhil Goyal 						cdb->sh_desc, 1, swap,
3585a4954bcSAkhil Goyal 						ses->pdcp.sn_size,
3595a4954bcSAkhil Goyal 						ses->pdcp.hfn,
3605a4954bcSAkhil Goyal 						ses->pdcp.bearer,
3615a4954bcSAkhil Goyal 						ses->pdcp.pkt_dir,
3625a4954bcSAkhil Goyal 						ses->pdcp.hfn_threshold,
3636127fff8SFranck Lenormand 						&cipherdata, p_authdata);
3645a4954bcSAkhil Goyal 		}
365a1173d55SHemant Agrawal 	}
366a1173d55SHemant Agrawal 	return shared_desc_len;
367a1173d55SHemant Agrawal }
368a1173d55SHemant Agrawal 
36905b12700SHemant Agrawal /* prepare ipsec proto command block of the session */
37005b12700SHemant Agrawal static int
37105b12700SHemant Agrawal dpaa_sec_prep_ipsec_cdb(dpaa_sec_session *ses)
37205b12700SHemant Agrawal {
37305b12700SHemant Agrawal 	struct alginfo cipherdata = {0}, authdata = {0};
37405b12700SHemant Agrawal 	struct sec_cdb *cdb = &ses->cdb;
37505b12700SHemant Agrawal 	int32_t shared_desc_len = 0;
37605b12700SHemant Agrawal 	int err;
37705b12700SHemant Agrawal #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
37805b12700SHemant Agrawal 	int swap = false;
37905b12700SHemant Agrawal #else
38005b12700SHemant Agrawal 	int swap = true;
38105b12700SHemant Agrawal #endif
38205b12700SHemant Agrawal 
38305b12700SHemant Agrawal 	cipherdata.key = (size_t)ses->cipher_key.data;
38405b12700SHemant Agrawal 	cipherdata.keylen = ses->cipher_key.length;
38505b12700SHemant Agrawal 	cipherdata.key_enc_flags = 0;
38605b12700SHemant Agrawal 	cipherdata.key_type = RTA_DATA_IMM;
3878524b44eSHemant Agrawal 	cipherdata.algtype = ses->cipher_key.alg;
3888524b44eSHemant Agrawal 	cipherdata.algmode = ses->cipher_key.algmode;
38905b12700SHemant Agrawal 
3902c318722SHemant Agrawal 	if (ses->auth_key.length) {
39105b12700SHemant Agrawal 		authdata.key = (size_t)ses->auth_key.data;
39205b12700SHemant Agrawal 		authdata.keylen = ses->auth_key.length;
39305b12700SHemant Agrawal 		authdata.key_enc_flags = 0;
39405b12700SHemant Agrawal 		authdata.key_type = RTA_DATA_IMM;
3958524b44eSHemant Agrawal 		authdata.algtype = ses->auth_key.alg;
3968524b44eSHemant Agrawal 		authdata.algmode = ses->auth_key.algmode;
3972c318722SHemant Agrawal 	}
39805b12700SHemant Agrawal 
39905b12700SHemant Agrawal 	cdb->sh_desc[0] = cipherdata.keylen;
40005b12700SHemant Agrawal 	cdb->sh_desc[1] = authdata.keylen;
40105b12700SHemant Agrawal 	err = rta_inline_query(IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN,
402453b9593SAkhil Goyal 			       DESC_JOB_IO_LEN,
40305b12700SHemant Agrawal 			       (unsigned int *)cdb->sh_desc,
40405b12700SHemant Agrawal 			       &cdb->sh_desc[2], 2);
40505b12700SHemant Agrawal 
40605b12700SHemant Agrawal 	if (err < 0) {
40705b12700SHemant Agrawal 		DPAA_SEC_ERR("Crypto: Incorrect key lengths");
40805b12700SHemant Agrawal 		return err;
40905b12700SHemant Agrawal 	}
41005b12700SHemant Agrawal 	if (cdb->sh_desc[2] & 1)
41105b12700SHemant Agrawal 		cipherdata.key_type = RTA_DATA_IMM;
41205b12700SHemant Agrawal 	else {
413ec861560SGagandeep Singh 		cipherdata.key = (size_t)rte_dpaa_mem_vtop(
41405b12700SHemant Agrawal 					(void *)(size_t)cipherdata.key);
41505b12700SHemant Agrawal 		cipherdata.key_type = RTA_DATA_PTR;
41605b12700SHemant Agrawal 	}
41705b12700SHemant Agrawal 	if (cdb->sh_desc[2] & (1<<1))
41805b12700SHemant Agrawal 		authdata.key_type = RTA_DATA_IMM;
41905b12700SHemant Agrawal 	else {
420ec861560SGagandeep Singh 		authdata.key = (size_t)rte_dpaa_mem_vtop(
42105b12700SHemant Agrawal 					(void *)(size_t)authdata.key);
42205b12700SHemant Agrawal 		authdata.key_type = RTA_DATA_PTR;
42305b12700SHemant Agrawal 	}
42405b12700SHemant Agrawal 
42505b12700SHemant Agrawal 	cdb->sh_desc[0] = 0;
42605b12700SHemant Agrawal 	cdb->sh_desc[1] = 0;
42705b12700SHemant Agrawal 	cdb->sh_desc[2] = 0;
42805b12700SHemant Agrawal 	if (ses->dir == DIR_ENC) {
42905b12700SHemant Agrawal 		shared_desc_len = cnstr_shdsc_ipsec_new_encap(
43005b12700SHemant Agrawal 				cdb->sh_desc,
43105b12700SHemant Agrawal 				true, swap, SHR_SERIAL,
43205b12700SHemant Agrawal 				&ses->encap_pdb,
43305b12700SHemant Agrawal 				(uint8_t *)&ses->ip4_hdr,
43405b12700SHemant Agrawal 				&cipherdata, &authdata);
43505b12700SHemant Agrawal 	} else if (ses->dir == DIR_DEC) {
43605b12700SHemant Agrawal 		shared_desc_len = cnstr_shdsc_ipsec_new_decap(
43705b12700SHemant Agrawal 				cdb->sh_desc,
43805b12700SHemant Agrawal 				true, swap, SHR_SERIAL,
43905b12700SHemant Agrawal 				&ses->decap_pdb,
44005b12700SHemant Agrawal 				&cipherdata, &authdata);
44105b12700SHemant Agrawal 	}
44205b12700SHemant Agrawal 	return shared_desc_len;
44305b12700SHemant Agrawal }
444314424b6SHemant Agrawal #endif
445c3e85bdcSAkhil Goyal /* prepare command block of the session */
446c3e85bdcSAkhil Goyal static int
447c3e85bdcSAkhil Goyal dpaa_sec_prep_cdb(dpaa_sec_session *ses)
448c3e85bdcSAkhil Goyal {
449c3e85bdcSAkhil Goyal 	struct alginfo alginfo_c = {0}, alginfo_a = {0}, alginfo = {0};
45022788c2cSSunil Kumar Kori 	int32_t shared_desc_len = 0;
451e79416d1SHemant Agrawal 	struct sec_cdb *cdb = &ses->cdb;
452c3e85bdcSAkhil Goyal 	int err;
453c3e85bdcSAkhil Goyal #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
454c3e85bdcSAkhil Goyal 	int swap = false;
455c3e85bdcSAkhil Goyal #else
456c3e85bdcSAkhil Goyal 	int swap = true;
457c3e85bdcSAkhil Goyal #endif
458c3e85bdcSAkhil Goyal 
459c3e85bdcSAkhil Goyal 	memset(cdb, 0, sizeof(struct sec_cdb));
460c3e85bdcSAkhil Goyal 
4618524b44eSHemant Agrawal 	switch (ses->ctxt) {
462a8d0d473SBruce Richardson #ifdef RTE_LIB_SECURITY
4638524b44eSHemant Agrawal 	case DPAA_SEC_IPSEC:
46405b12700SHemant Agrawal 		shared_desc_len = dpaa_sec_prep_ipsec_cdb(ses);
4658524b44eSHemant Agrawal 		break;
4668524b44eSHemant Agrawal 	case DPAA_SEC_PDCP:
467a1173d55SHemant Agrawal 		shared_desc_len = dpaa_sec_prep_pdcp_cdb(ses);
4688524b44eSHemant Agrawal 		break;
469314424b6SHemant Agrawal #endif
4708524b44eSHemant Agrawal 	case DPAA_SEC_CIPHER:
4710e5607e4SHemant Agrawal 		alginfo_c.key = (size_t)ses->cipher_key.data;
472c3e85bdcSAkhil Goyal 		alginfo_c.keylen = ses->cipher_key.length;
473c3e85bdcSAkhil Goyal 		alginfo_c.key_enc_flags = 0;
474c3e85bdcSAkhil Goyal 		alginfo_c.key_type = RTA_DATA_IMM;
4758524b44eSHemant Agrawal 		alginfo_c.algtype = ses->cipher_key.alg;
4768524b44eSHemant Agrawal 		alginfo_c.algmode = ses->cipher_key.algmode;
4778524b44eSHemant Agrawal 
478c5788a10SHemant Agrawal 		switch (ses->cipher_alg) {
479c5788a10SHemant Agrawal 		case RTE_CRYPTO_CIPHER_AES_CBC:
480c5788a10SHemant Agrawal 		case RTE_CRYPTO_CIPHER_3DES_CBC:
4813e4fbc6cSGagandeep Singh 		case RTE_CRYPTO_CIPHER_DES_CBC:
482c5788a10SHemant Agrawal 		case RTE_CRYPTO_CIPHER_AES_CTR:
483c5788a10SHemant Agrawal 		case RTE_CRYPTO_CIPHER_3DES_CTR:
484c5788a10SHemant Agrawal 			shared_desc_len = cnstr_shdsc_blkcipher(
485c5788a10SHemant Agrawal 					cdb->sh_desc, true,
486c5788a10SHemant Agrawal 					swap, SHR_NEVER, &alginfo_c,
487c5788a10SHemant Agrawal 					ses->iv.length,
488c5788a10SHemant Agrawal 					ses->dir);
489c5788a10SHemant Agrawal 			break;
490c5788a10SHemant Agrawal 		case RTE_CRYPTO_CIPHER_SNOW3G_UEA2:
491c5788a10SHemant Agrawal 			shared_desc_len = cnstr_shdsc_snow_f8(
492c5788a10SHemant Agrawal 					cdb->sh_desc, true, swap,
493c5788a10SHemant Agrawal 					&alginfo_c,
494c5788a10SHemant Agrawal 					ses->dir);
495c5788a10SHemant Agrawal 			break;
496c5788a10SHemant Agrawal 		case RTE_CRYPTO_CIPHER_ZUC_EEA3:
497c5788a10SHemant Agrawal 			shared_desc_len = cnstr_shdsc_zuce(
498c5788a10SHemant Agrawal 					cdb->sh_desc, true, swap,
499c5788a10SHemant Agrawal 					&alginfo_c,
500c5788a10SHemant Agrawal 					ses->dir);
501c5788a10SHemant Agrawal 			break;
502c5788a10SHemant Agrawal 		default:
503c5788a10SHemant Agrawal 			DPAA_SEC_ERR("unsupported cipher alg %d",
504c5788a10SHemant Agrawal 				     ses->cipher_alg);
505c3e85bdcSAkhil Goyal 			return -ENOTSUP;
506c3e85bdcSAkhil Goyal 		}
5078524b44eSHemant Agrawal 		break;
5088524b44eSHemant Agrawal 	case DPAA_SEC_AUTH:
5090e5607e4SHemant Agrawal 		alginfo_a.key = (size_t)ses->auth_key.data;
510c3e85bdcSAkhil Goyal 		alginfo_a.keylen = ses->auth_key.length;
511c3e85bdcSAkhil Goyal 		alginfo_a.key_enc_flags = 0;
512c3e85bdcSAkhil Goyal 		alginfo_a.key_type = RTA_DATA_IMM;
5138524b44eSHemant Agrawal 		alginfo_a.algtype = ses->auth_key.alg;
5148524b44eSHemant Agrawal 		alginfo_a.algmode = ses->auth_key.algmode;
515c5788a10SHemant Agrawal 		switch (ses->auth_alg) {
5164c42352cSGagandeep Singh 		case RTE_CRYPTO_AUTH_MD5:
5174c42352cSGagandeep Singh 		case RTE_CRYPTO_AUTH_SHA1:
5184c42352cSGagandeep Singh 		case RTE_CRYPTO_AUTH_SHA224:
5194c42352cSGagandeep Singh 		case RTE_CRYPTO_AUTH_SHA256:
5204c42352cSGagandeep Singh 		case RTE_CRYPTO_AUTH_SHA384:
5214c42352cSGagandeep Singh 		case RTE_CRYPTO_AUTH_SHA512:
5224c42352cSGagandeep Singh 			shared_desc_len = cnstr_shdsc_hash(
5234c42352cSGagandeep Singh 						cdb->sh_desc, true,
5244c42352cSGagandeep Singh 						swap, SHR_NEVER, &alginfo_a,
5254c42352cSGagandeep Singh 						!ses->dir,
5264c42352cSGagandeep Singh 						ses->digest_length);
5274c42352cSGagandeep Singh 			break;
528c5788a10SHemant Agrawal 		case RTE_CRYPTO_AUTH_MD5_HMAC:
529c5788a10SHemant Agrawal 		case RTE_CRYPTO_AUTH_SHA1_HMAC:
530c5788a10SHemant Agrawal 		case RTE_CRYPTO_AUTH_SHA224_HMAC:
531c5788a10SHemant Agrawal 		case RTE_CRYPTO_AUTH_SHA256_HMAC:
532c5788a10SHemant Agrawal 		case RTE_CRYPTO_AUTH_SHA384_HMAC:
533c5788a10SHemant Agrawal 		case RTE_CRYPTO_AUTH_SHA512_HMAC:
534c5788a10SHemant Agrawal 			shared_desc_len = cnstr_shdsc_hmac(
535c5788a10SHemant Agrawal 						cdb->sh_desc, true,
536c5788a10SHemant Agrawal 						swap, SHR_NEVER, &alginfo_a,
537c5788a10SHemant Agrawal 						!ses->dir,
538c5788a10SHemant Agrawal 						ses->digest_length);
539c5788a10SHemant Agrawal 			break;
540c5788a10SHemant Agrawal 		case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
541c5788a10SHemant Agrawal 			shared_desc_len = cnstr_shdsc_snow_f9(
542c5788a10SHemant Agrawal 						cdb->sh_desc, true, swap,
543c5788a10SHemant Agrawal 						&alginfo_a,
544c5788a10SHemant Agrawal 						!ses->dir,
545c5788a10SHemant Agrawal 						ses->digest_length);
546c5788a10SHemant Agrawal 			break;
547c5788a10SHemant Agrawal 		case RTE_CRYPTO_AUTH_ZUC_EIA3:
548c5788a10SHemant Agrawal 			shared_desc_len = cnstr_shdsc_zuca(
549c5788a10SHemant Agrawal 						cdb->sh_desc, true, swap,
550c5788a10SHemant Agrawal 						&alginfo_a,
551c5788a10SHemant Agrawal 						!ses->dir,
552c5788a10SHemant Agrawal 						ses->digest_length);
553c5788a10SHemant Agrawal 			break;
55466f95673SGagandeep Singh 		case RTE_CRYPTO_AUTH_AES_XCBC_MAC:
5552ed12d9bSGagandeep Singh 		case RTE_CRYPTO_AUTH_AES_CMAC:
55666f95673SGagandeep Singh 			shared_desc_len = cnstr_shdsc_aes_mac(
55766f95673SGagandeep Singh 						cdb->sh_desc,
55866f95673SGagandeep Singh 						true, swap, SHR_NEVER,
55966f95673SGagandeep Singh 						&alginfo_a,
56066f95673SGagandeep Singh 						!ses->dir,
56166f95673SGagandeep Singh 						ses->digest_length);
56266f95673SGagandeep Singh 			break;
563c5788a10SHemant Agrawal 		default:
564c5788a10SHemant Agrawal 			DPAA_SEC_ERR("unsupported auth alg %u", ses->auth_alg);
565c5788a10SHemant Agrawal 		}
5668524b44eSHemant Agrawal 		break;
5678524b44eSHemant Agrawal 	case DPAA_SEC_AEAD:
568c3e85bdcSAkhil Goyal 		if (alginfo.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
569f163231eSHemant Agrawal 			DPAA_SEC_ERR("not supported aead alg");
570c3e85bdcSAkhil Goyal 			return -ENOTSUP;
571c3e85bdcSAkhil Goyal 		}
5720e5607e4SHemant Agrawal 		alginfo.key = (size_t)ses->aead_key.data;
573c3e85bdcSAkhil Goyal 		alginfo.keylen = ses->aead_key.length;
574c3e85bdcSAkhil Goyal 		alginfo.key_enc_flags = 0;
575c3e85bdcSAkhil Goyal 		alginfo.key_type = RTA_DATA_IMM;
5768524b44eSHemant Agrawal 		alginfo.algtype = ses->aead_key.alg;
5778524b44eSHemant Agrawal 		alginfo.algmode = ses->aead_key.algmode;
578c3e85bdcSAkhil Goyal 
579c3e85bdcSAkhil Goyal 		if (ses->dir == DIR_ENC)
580c3e85bdcSAkhil Goyal 			shared_desc_len = cnstr_shdsc_gcm_encap(
5817449390bSAkhil Goyal 					cdb->sh_desc, true, swap, SHR_NEVER,
582c3e85bdcSAkhil Goyal 					&alginfo,
583c3e85bdcSAkhil Goyal 					ses->iv.length,
584c3e85bdcSAkhil Goyal 					ses->digest_length);
585c3e85bdcSAkhil Goyal 		else
586c3e85bdcSAkhil Goyal 			shared_desc_len = cnstr_shdsc_gcm_decap(
5877449390bSAkhil Goyal 					cdb->sh_desc, true, swap, SHR_NEVER,
588c3e85bdcSAkhil Goyal 					&alginfo,
589c3e85bdcSAkhil Goyal 					ses->iv.length,
590c3e85bdcSAkhil Goyal 					ses->digest_length);
5918524b44eSHemant Agrawal 		break;
5928524b44eSHemant Agrawal 	case DPAA_SEC_CIPHER_HASH:
5930e5607e4SHemant Agrawal 		alginfo_c.key = (size_t)ses->cipher_key.data;
594c3e85bdcSAkhil Goyal 		alginfo_c.keylen = ses->cipher_key.length;
595c3e85bdcSAkhil Goyal 		alginfo_c.key_enc_flags = 0;
596c3e85bdcSAkhil Goyal 		alginfo_c.key_type = RTA_DATA_IMM;
5978524b44eSHemant Agrawal 		alginfo_c.algtype = ses->cipher_key.alg;
5988524b44eSHemant Agrawal 		alginfo_c.algmode = ses->cipher_key.algmode;
599c3e85bdcSAkhil Goyal 
6000e5607e4SHemant Agrawal 		alginfo_a.key = (size_t)ses->auth_key.data;
601c3e85bdcSAkhil Goyal 		alginfo_a.keylen = ses->auth_key.length;
602c3e85bdcSAkhil Goyal 		alginfo_a.key_enc_flags = 0;
603c3e85bdcSAkhil Goyal 		alginfo_a.key_type = RTA_DATA_IMM;
6048524b44eSHemant Agrawal 		alginfo_a.algtype = ses->auth_key.alg;
6058524b44eSHemant Agrawal 		alginfo_a.algmode = ses->auth_key.algmode;
606c3e85bdcSAkhil Goyal 
607c3e85bdcSAkhil Goyal 		cdb->sh_desc[0] = alginfo_c.keylen;
608c3e85bdcSAkhil Goyal 		cdb->sh_desc[1] = alginfo_a.keylen;
609c3e85bdcSAkhil Goyal 		err = rta_inline_query(IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN,
610453b9593SAkhil Goyal 				       DESC_JOB_IO_LEN,
611c3e85bdcSAkhil Goyal 				       (unsigned int *)cdb->sh_desc,
612c3e85bdcSAkhil Goyal 				       &cdb->sh_desc[2], 2);
613c3e85bdcSAkhil Goyal 
614c3e85bdcSAkhil Goyal 		if (err < 0) {
615f163231eSHemant Agrawal 			DPAA_SEC_ERR("Crypto: Incorrect key lengths");
616c3e85bdcSAkhil Goyal 			return err;
617c3e85bdcSAkhil Goyal 		}
618c3e85bdcSAkhil Goyal 		if (cdb->sh_desc[2] & 1)
619c3e85bdcSAkhil Goyal 			alginfo_c.key_type = RTA_DATA_IMM;
620c3e85bdcSAkhil Goyal 		else {
621ec861560SGagandeep Singh 			alginfo_c.key = (size_t)rte_dpaa_mem_vtop(
6220e5607e4SHemant Agrawal 						(void *)(size_t)alginfo_c.key);
623c3e85bdcSAkhil Goyal 			alginfo_c.key_type = RTA_DATA_PTR;
624c3e85bdcSAkhil Goyal 		}
625c3e85bdcSAkhil Goyal 		if (cdb->sh_desc[2] & (1<<1))
626c3e85bdcSAkhil Goyal 			alginfo_a.key_type = RTA_DATA_IMM;
627c3e85bdcSAkhil Goyal 		else {
628ec861560SGagandeep Singh 			alginfo_a.key = (size_t)rte_dpaa_mem_vtop(
6290e5607e4SHemant Agrawal 						(void *)(size_t)alginfo_a.key);
630c3e85bdcSAkhil Goyal 			alginfo_a.key_type = RTA_DATA_PTR;
631c3e85bdcSAkhil Goyal 		}
632c3e85bdcSAkhil Goyal 		cdb->sh_desc[0] = 0;
633c3e85bdcSAkhil Goyal 		cdb->sh_desc[1] = 0;
634c3e85bdcSAkhil Goyal 		cdb->sh_desc[2] = 0;
6351f14d500SAkhil Goyal 		/* Auth_only_len is set as 0 here and it will be
6361f14d500SAkhil Goyal 		 * overwritten in fd for each packet.
637c3e85bdcSAkhil Goyal 		 */
638c3e85bdcSAkhil Goyal 		shared_desc_len = cnstr_shdsc_authenc(cdb->sh_desc,
6397449390bSAkhil Goyal 				true, swap, SHR_SERIAL, &alginfo_c, &alginfo_a,
6403394ed47SVakul Garg 				ses->iv.length,
641c3e85bdcSAkhil Goyal 				ses->digest_length, ses->dir);
6428524b44eSHemant Agrawal 		break;
6438524b44eSHemant Agrawal 	case DPAA_SEC_HASH_CIPHER:
6448524b44eSHemant Agrawal 	default:
6458524b44eSHemant Agrawal 		DPAA_SEC_ERR("error: Unsupported session");
6468524b44eSHemant Agrawal 		return -ENOTSUP;
647c3e85bdcSAkhil Goyal 	}
64822788c2cSSunil Kumar Kori 
64922788c2cSSunil Kumar Kori 	if (shared_desc_len < 0) {
650f163231eSHemant Agrawal 		DPAA_SEC_ERR("error in preparing command block");
65122788c2cSSunil Kumar Kori 		return shared_desc_len;
65222788c2cSSunil Kumar Kori 	}
65322788c2cSSunil Kumar Kori 
654c3e85bdcSAkhil Goyal 	cdb->sh_hdr.hi.field.idlen = shared_desc_len;
655c3e85bdcSAkhil Goyal 	cdb->sh_hdr.hi.word = rte_cpu_to_be_32(cdb->sh_hdr.hi.word);
656c3e85bdcSAkhil Goyal 	cdb->sh_hdr.lo.word = rte_cpu_to_be_32(cdb->sh_hdr.lo.word);
657c3e85bdcSAkhil Goyal 
658c3e85bdcSAkhil Goyal 	return 0;
659c3e85bdcSAkhil Goyal }
660c3e85bdcSAkhil Goyal 
661b1bbf222SGagandeep Singh static void
662b1bbf222SGagandeep Singh dpaa_sec_dump(struct dpaa_sec_op_ctx *ctx, struct dpaa_sec_qp *qp)
663b1bbf222SGagandeep Singh {
664b1bbf222SGagandeep Singh 	struct dpaa_sec_job *job = &ctx->job;
665b1bbf222SGagandeep Singh 	struct rte_crypto_op *op = ctx->op;
666b1bbf222SGagandeep Singh 	dpaa_sec_session *sess = NULL;
667b1bbf222SGagandeep Singh 	struct sec_cdb c_cdb, *cdb;
668b1bbf222SGagandeep Singh 	uint8_t bufsize;
669b1bbf222SGagandeep Singh 	struct rte_crypto_sym_op *sym_op;
670b1bbf222SGagandeep Singh 	struct qm_sg_entry sg[2];
671b1bbf222SGagandeep Singh 
672b1bbf222SGagandeep Singh 	if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION)
673*bdce2564SAkhil Goyal 		sess = (dpaa_sec_session *)op->sym->session->driver_priv_data;
674b1bbf222SGagandeep Singh #ifdef RTE_LIBRTE_SECURITY
675b1bbf222SGagandeep Singh 	else if (op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION)
676b1bbf222SGagandeep Singh 		sess = (dpaa_sec_session *)
677b1bbf222SGagandeep Singh 			get_sec_session_private_data(
678b1bbf222SGagandeep Singh 					op->sym->sec_session);
679b1bbf222SGagandeep Singh #endif
680b1bbf222SGagandeep Singh 	if (sess == NULL) {
681b1bbf222SGagandeep Singh 		printf("session is NULL\n");
682b1bbf222SGagandeep Singh 		goto mbuf_dump;
683b1bbf222SGagandeep Singh 	}
684b1bbf222SGagandeep Singh 
685b1bbf222SGagandeep Singh 	cdb = &sess->cdb;
686b1bbf222SGagandeep Singh 	rte_memcpy(&c_cdb, cdb, sizeof(struct sec_cdb));
687b1bbf222SGagandeep Singh #ifdef RTE_LIBRTE_SECURITY
688b1bbf222SGagandeep Singh 	printf("\nsession protocol type = %d\n", sess->proto_alg);
689b1bbf222SGagandeep Singh #endif
690b1bbf222SGagandeep Singh 	printf("\n****************************************\n"
691b1bbf222SGagandeep Singh 		"session params:\n\tContext type:\t%d\n\tDirection:\t%s\n"
692b1bbf222SGagandeep Singh 		"\tCipher alg:\t%d\n\tAuth alg:\t%d\n\tAead alg:\t%d\n"
693b1bbf222SGagandeep Singh 		"\tCipher key len:\t%"PRIu64"\n\tCipher alg:\t%d\n"
694b1bbf222SGagandeep Singh 		"\tCipher algmode:\t%d\n", sess->ctxt,
695b1bbf222SGagandeep Singh 		(sess->dir == DIR_ENC) ? "DIR_ENC" : "DIR_DEC",
696b1bbf222SGagandeep Singh 		sess->cipher_alg, sess->auth_alg, sess->aead_alg,
697b1bbf222SGagandeep Singh 		(uint64_t)sess->cipher_key.length, sess->cipher_key.alg,
698b1bbf222SGagandeep Singh 		sess->cipher_key.algmode);
699b1bbf222SGagandeep Singh 		rte_hexdump(stdout, "cipher key", sess->cipher_key.data,
700b1bbf222SGagandeep Singh 				sess->cipher_key.length);
701b1bbf222SGagandeep Singh 		rte_hexdump(stdout, "auth key", sess->auth_key.data,
702b1bbf222SGagandeep Singh 				sess->auth_key.length);
703b1bbf222SGagandeep Singh 	printf("\tAuth key len:\t%"PRIu64"\n\tAuth alg:\t%d\n"
704b1bbf222SGagandeep Singh 		"\tAuth algmode:\t%d\n\tIV len:\t\t%d\n\tIV offset:\t%d\n"
705b1bbf222SGagandeep Singh 		"\tdigest length:\t%d\n\tauth only len:\t\t%d\n"
706b1bbf222SGagandeep Singh 		"\taead cipher text:\t%d\n",
707b1bbf222SGagandeep Singh 		(uint64_t)sess->auth_key.length, sess->auth_key.alg,
708b1bbf222SGagandeep Singh 		sess->auth_key.algmode,
709b1bbf222SGagandeep Singh 		sess->iv.length, sess->iv.offset,
710b1bbf222SGagandeep Singh 		sess->digest_length, sess->auth_only_len,
711b1bbf222SGagandeep Singh 		sess->auth_cipher_text);
712b1bbf222SGagandeep Singh #ifdef RTE_LIBRTE_SECURITY
713b1bbf222SGagandeep Singh 	printf("PDCP session params:\n"
714b1bbf222SGagandeep Singh 		"\tDomain:\t\t%d\n\tBearer:\t\t%d\n\tpkt_dir:\t%d\n\thfn_ovd:"
715b1bbf222SGagandeep Singh 		"\t%d\n\tsn_size:\t%d\n\tsdap_enabled:\t%d\n\thfn_ovd_offset:"
716b1bbf222SGagandeep Singh 		"\t%d\n\thfn:\t\t%d\n"
717b1bbf222SGagandeep Singh 		"\thfn_threshold:\t0x%x\n", sess->pdcp.domain,
718b1bbf222SGagandeep Singh 		sess->pdcp.bearer, sess->pdcp.pkt_dir, sess->pdcp.hfn_ovd,
719b1bbf222SGagandeep Singh 		sess->pdcp.sn_size, sess->pdcp.sdap_enabled,
720b1bbf222SGagandeep Singh 		sess->pdcp.hfn_ovd_offset, sess->pdcp.hfn,
721b1bbf222SGagandeep Singh 		sess->pdcp.hfn_threshold);
722b1bbf222SGagandeep Singh #endif
723b1bbf222SGagandeep Singh 	c_cdb.sh_hdr.hi.word = rte_be_to_cpu_32(c_cdb.sh_hdr.hi.word);
724b1bbf222SGagandeep Singh 	c_cdb.sh_hdr.lo.word = rte_be_to_cpu_32(c_cdb.sh_hdr.lo.word);
725b1bbf222SGagandeep Singh 	bufsize = c_cdb.sh_hdr.hi.field.idlen;
726b1bbf222SGagandeep Singh 
727b1bbf222SGagandeep Singh 	printf("cdb = %p\n\n", cdb);
728b1bbf222SGagandeep Singh 	printf("Descriptor size = %d\n", bufsize);
729b1bbf222SGagandeep Singh 	int m;
730b1bbf222SGagandeep Singh 	for (m = 0; m < bufsize; m++)
731b1bbf222SGagandeep Singh 		printf("0x%x\n", rte_be_to_cpu_32(c_cdb.sh_desc[m]));
732b1bbf222SGagandeep Singh 
733b1bbf222SGagandeep Singh 	printf("\n");
734b1bbf222SGagandeep Singh mbuf_dump:
735b1bbf222SGagandeep Singh 	sym_op = op->sym;
736b1bbf222SGagandeep Singh 	if (sym_op->m_src) {
737b1bbf222SGagandeep Singh 		printf("Source mbuf:\n");
738b1bbf222SGagandeep Singh 		rte_pktmbuf_dump(stdout, sym_op->m_src,
739b1bbf222SGagandeep Singh 				 sym_op->m_src->data_len);
740b1bbf222SGagandeep Singh 	}
741b1bbf222SGagandeep Singh 	if (sym_op->m_dst) {
742b1bbf222SGagandeep Singh 		printf("Destination mbuf:\n");
743b1bbf222SGagandeep Singh 		rte_pktmbuf_dump(stdout, sym_op->m_dst,
744b1bbf222SGagandeep Singh 				 sym_op->m_dst->data_len);
745b1bbf222SGagandeep Singh 	}
746b1bbf222SGagandeep Singh 
747b1bbf222SGagandeep Singh 	printf("Session address = %p\ncipher offset: %d, length: %d\n"
748b1bbf222SGagandeep Singh 		"auth offset: %d, length:  %d\n aead offset: %d, length: %d\n",
749b1bbf222SGagandeep Singh 		sym_op->session, sym_op->cipher.data.offset,
750b1bbf222SGagandeep Singh 		sym_op->cipher.data.length,
751b1bbf222SGagandeep Singh 		sym_op->auth.data.offset, sym_op->auth.data.length,
752b1bbf222SGagandeep Singh 		sym_op->aead.data.offset, sym_op->aead.data.length);
753b1bbf222SGagandeep Singh 	printf("\n");
754b1bbf222SGagandeep Singh 
755b1bbf222SGagandeep Singh 	printf("******************************************************\n");
756b1bbf222SGagandeep Singh 	printf("ctx info:\n");
757b1bbf222SGagandeep Singh 	printf("job->sg[0] output info:\n");
758b1bbf222SGagandeep Singh 	memcpy(&sg[0], &job->sg[0], sizeof(sg[0]));
759b1bbf222SGagandeep Singh 	printf("\taddr = %"PRIx64",\n\tlen = %d,\n\tfinal = %d,\n\textention = %d"
760b1bbf222SGagandeep Singh 		"\n\tbpid = %d\n\toffset = %d\n",
761b1bbf222SGagandeep Singh 		(uint64_t)sg[0].addr, sg[0].length, sg[0].final,
762b1bbf222SGagandeep Singh 		sg[0].extension, sg[0].bpid, sg[0].offset);
763b1bbf222SGagandeep Singh 	printf("\njob->sg[1] input info:\n");
764b1bbf222SGagandeep Singh 	memcpy(&sg[1], &job->sg[1], sizeof(sg[1]));
765b1bbf222SGagandeep Singh 	hw_sg_to_cpu(&sg[1]);
766b1bbf222SGagandeep Singh 	printf("\taddr = %"PRIx64",\n\tlen = %d,\n\tfinal = %d,\n\textention = %d"
767b1bbf222SGagandeep Singh 		"\n\tbpid = %d\n\toffset = %d\n",
768b1bbf222SGagandeep Singh 		(uint64_t)sg[1].addr, sg[1].length, sg[1].final,
769b1bbf222SGagandeep Singh 		sg[1].extension, sg[1].bpid, sg[1].offset);
770b1bbf222SGagandeep Singh 
771b1bbf222SGagandeep Singh 	printf("\nctx pool addr = %p\n", ctx->ctx_pool);
772b1bbf222SGagandeep Singh 	if (ctx->ctx_pool)
773b1bbf222SGagandeep Singh 		printf("ctx pool available counts = %d\n",
774b1bbf222SGagandeep Singh 			rte_mempool_avail_count(ctx->ctx_pool));
775b1bbf222SGagandeep Singh 
776b1bbf222SGagandeep Singh 	printf("\nop pool addr = %p\n", op->mempool);
777b1bbf222SGagandeep Singh 	if (op->mempool)
778b1bbf222SGagandeep Singh 		printf("op pool available counts = %d\n",
779b1bbf222SGagandeep Singh 			rte_mempool_avail_count(op->mempool));
780b1bbf222SGagandeep Singh 
781b1bbf222SGagandeep Singh 	printf("********************************************************\n");
782b1bbf222SGagandeep Singh 	printf("Queue data:\n");
783b1bbf222SGagandeep Singh 	printf("\tFQID = 0x%x\n\tstate = %d\n\tnb_desc = %d\n"
784b1bbf222SGagandeep Singh 		"\tctx_pool = %p\n\trx_pkts = %d\n\ttx_pkts"
785b1bbf222SGagandeep Singh 	       "= %d\n\trx_errs = %d\n\ttx_errs = %d\n\n",
786b1bbf222SGagandeep Singh 		qp->outq.fqid, qp->outq.state, qp->outq.nb_desc,
787b1bbf222SGagandeep Singh 		qp->ctx_pool, qp->rx_pkts, qp->tx_pkts,
788b1bbf222SGagandeep Singh 		qp->rx_errs, qp->tx_errs);
789b1bbf222SGagandeep Singh }
790b1bbf222SGagandeep Singh 
791c3e85bdcSAkhil Goyal /* qp is lockless, should be accessed by only one thread */
792c3e85bdcSAkhil Goyal static int
793c3e85bdcSAkhil Goyal dpaa_sec_deq(struct dpaa_sec_qp *qp, struct rte_crypto_op **ops, int nb_ops)
794c3e85bdcSAkhil Goyal {
795c3e85bdcSAkhil Goyal 	struct qman_fq *fq;
7969a984458SAkhil Goyal 	unsigned int pkts = 0;
797f40d5a53SNipun Gupta 	int num_rx_bufs, ret;
7989a984458SAkhil Goyal 	struct qm_dqrr_entry *dq;
799f40d5a53SNipun Gupta 	uint32_t vdqcr_flags = 0;
800c3e85bdcSAkhil Goyal 
801c3e85bdcSAkhil Goyal 	fq = &qp->outq;
802f40d5a53SNipun Gupta 	/*
803f40d5a53SNipun Gupta 	 * Until request for four buffers, we provide exact number of buffers.
804f40d5a53SNipun Gupta 	 * Otherwise we do not set the QM_VDQCR_EXACT flag.
805f40d5a53SNipun Gupta 	 * Not setting QM_VDQCR_EXACT flag can provide two more buffers than
806f40d5a53SNipun Gupta 	 * requested, so we request two less in this case.
807f40d5a53SNipun Gupta 	 */
808f40d5a53SNipun Gupta 	if (nb_ops < 4) {
809f40d5a53SNipun Gupta 		vdqcr_flags = QM_VDQCR_EXACT;
810f40d5a53SNipun Gupta 		num_rx_bufs = nb_ops;
811f40d5a53SNipun Gupta 	} else {
812f40d5a53SNipun Gupta 		num_rx_bufs = nb_ops > DPAA_MAX_DEQUEUE_NUM_FRAMES ?
813f40d5a53SNipun Gupta 			(DPAA_MAX_DEQUEUE_NUM_FRAMES - 2) : (nb_ops - 2);
814f40d5a53SNipun Gupta 	}
815f40d5a53SNipun Gupta 	ret = qman_set_vdq(fq, num_rx_bufs, vdqcr_flags);
8169a984458SAkhil Goyal 	if (ret)
8179a984458SAkhil Goyal 		return 0;
818c3e85bdcSAkhil Goyal 
8199a984458SAkhil Goyal 	do {
8209a984458SAkhil Goyal 		const struct qm_fd *fd;
8219a984458SAkhil Goyal 		struct dpaa_sec_job *job;
8229a984458SAkhil Goyal 		struct dpaa_sec_op_ctx *ctx;
8239a984458SAkhil Goyal 		struct rte_crypto_op *op;
824c3e85bdcSAkhil Goyal 
8259a984458SAkhil Goyal 		dq = qman_dequeue(fq);
8269a984458SAkhil Goyal 		if (!dq)
8279a984458SAkhil Goyal 			continue;
8289a984458SAkhil Goyal 
8299a984458SAkhil Goyal 		fd = &dq->fd;
8309a984458SAkhil Goyal 		/* sg is embedded in an op ctx,
8319a984458SAkhil Goyal 		 * sg[0] is for output
8329a984458SAkhil Goyal 		 * sg[1] for input
8339a984458SAkhil Goyal 		 */
834ec861560SGagandeep Singh 		job = rte_dpaa_mem_ptov(qm_fd_addr_get64(fd));
8359a984458SAkhil Goyal 
8369a984458SAkhil Goyal 		ctx = container_of(job, struct dpaa_sec_op_ctx, job);
8379a984458SAkhil Goyal 		ctx->fd_status = fd->status;
8389a984458SAkhil Goyal 		op = ctx->op;
8399a984458SAkhil Goyal 		if (op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
8409a984458SAkhil Goyal 			struct qm_sg_entry *sg_out;
8419a984458SAkhil Goyal 			uint32_t len;
842fb5c100aSAkhil Goyal 			struct rte_mbuf *mbuf = (op->sym->m_dst == NULL) ?
843fb5c100aSAkhil Goyal 						op->sym->m_src : op->sym->m_dst;
8449a984458SAkhil Goyal 
8459a984458SAkhil Goyal 			sg_out = &job->sg[0];
8469a984458SAkhil Goyal 			hw_sg_to_cpu(sg_out);
8479a984458SAkhil Goyal 			len = sg_out->length;
848fb5c100aSAkhil Goyal 			mbuf->pkt_len = len;
849fb5c100aSAkhil Goyal 			while (mbuf->next != NULL) {
850fb5c100aSAkhil Goyal 				len -= mbuf->data_len;
851fb5c100aSAkhil Goyal 				mbuf = mbuf->next;
852fb5c100aSAkhil Goyal 			}
853fb5c100aSAkhil Goyal 			mbuf->data_len = len;
8549a984458SAkhil Goyal 		}
8559a984458SAkhil Goyal 		if (!ctx->fd_status) {
8569a984458SAkhil Goyal 			op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
8579a984458SAkhil Goyal 		} else {
858b1bbf222SGagandeep Singh 			if (dpaa_sec_dp_dump > DPAA_SEC_DP_NO_DUMP) {
859b1bbf222SGagandeep Singh 				DPAA_SEC_DP_WARN("SEC return err:0x%x\n",
860b1bbf222SGagandeep Singh 						  ctx->fd_status);
861b1bbf222SGagandeep Singh 				if (dpaa_sec_dp_dump > DPAA_SEC_DP_ERR_DUMP)
862b1bbf222SGagandeep Singh 					dpaa_sec_dump(ctx, qp);
863b1bbf222SGagandeep Singh 			}
8649a984458SAkhil Goyal 			op->status = RTE_CRYPTO_OP_STATUS_ERROR;
8659a984458SAkhil Goyal 		}
8669a984458SAkhil Goyal 		ops[pkts++] = op;
8679a984458SAkhil Goyal 
8687be78d02SJosh Soref 		/* report op status to sym->op and then free the ctx memory */
8699a984458SAkhil Goyal 		rte_mempool_put(ctx->ctx_pool, (void *)ctx);
8709a984458SAkhil Goyal 
8719a984458SAkhil Goyal 		qman_dqrr_consume(fq, dq);
8729a984458SAkhil Goyal 	} while (fq->flags & QMAN_FQ_STATE_VDQCR);
8739a984458SAkhil Goyal 
8749a984458SAkhil Goyal 	return pkts;
875c3e85bdcSAkhil Goyal }
876c3e85bdcSAkhil Goyal 
877a74af788SAkhil Goyal static inline struct dpaa_sec_job *
878a74af788SAkhil Goyal build_auth_only_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
879a74af788SAkhil Goyal {
880a74af788SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
881a74af788SAkhil Goyal 	struct rte_mbuf *mbuf = sym->m_src;
882a74af788SAkhil Goyal 	struct dpaa_sec_job *cf;
883a74af788SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
884a74af788SAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
885a74af788SAkhil Goyal 	phys_addr_t start_addr;
886a74af788SAkhil Goyal 	uint8_t *old_digest, extra_segs;
887c5788a10SHemant Agrawal 	int data_len, data_offset;
888c5788a10SHemant Agrawal 
889c5788a10SHemant Agrawal 	data_len = sym->auth.data.length;
890c5788a10SHemant Agrawal 	data_offset = sym->auth.data.offset;
891c5788a10SHemant Agrawal 
892c5788a10SHemant Agrawal 	if (ses->auth_alg == RTE_CRYPTO_AUTH_SNOW3G_UIA2 ||
893c5788a10SHemant Agrawal 	    ses->auth_alg == RTE_CRYPTO_AUTH_ZUC_EIA3) {
894c5788a10SHemant Agrawal 		if ((data_len & 7) || (data_offset & 7)) {
895c5788a10SHemant Agrawal 			DPAA_SEC_ERR("AUTH: len/offset must be full bytes");
896c5788a10SHemant Agrawal 			return NULL;
897c5788a10SHemant Agrawal 		}
898c5788a10SHemant Agrawal 
899c5788a10SHemant Agrawal 		data_len = data_len >> 3;
900c5788a10SHemant Agrawal 		data_offset = data_offset >> 3;
901c5788a10SHemant Agrawal 	}
902a74af788SAkhil Goyal 
903a74af788SAkhil Goyal 	if (is_decode(ses))
904a74af788SAkhil Goyal 		extra_segs = 3;
905a74af788SAkhil Goyal 	else
906a74af788SAkhil Goyal 		extra_segs = 2;
907a74af788SAkhil Goyal 
908f7a5752eSHemant Agrawal 	if (mbuf->nb_segs > MAX_SG_ENTRIES) {
909f163231eSHemant Agrawal 		DPAA_SEC_DP_ERR("Auth: Max sec segs supported is %d",
910a74af788SAkhil Goyal 				MAX_SG_ENTRIES);
911a74af788SAkhil Goyal 		return NULL;
912a74af788SAkhil Goyal 	}
913f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, mbuf->nb_segs + extra_segs);
914a74af788SAkhil Goyal 	if (!ctx)
915a74af788SAkhil Goyal 		return NULL;
916a74af788SAkhil Goyal 
917a74af788SAkhil Goyal 	cf = &ctx->job;
918a74af788SAkhil Goyal 	ctx->op = op;
919a74af788SAkhil Goyal 	old_digest = ctx->digest;
920a74af788SAkhil Goyal 
921a74af788SAkhil Goyal 	/* output */
922a74af788SAkhil Goyal 	out_sg = &cf->sg[0];
923a74af788SAkhil Goyal 	qm_sg_entry_set64(out_sg, sym->auth.digest.phys_addr);
924a74af788SAkhil Goyal 	out_sg->length = ses->digest_length;
925a74af788SAkhil Goyal 	cpu_to_hw_sg(out_sg);
926a74af788SAkhil Goyal 
927a74af788SAkhil Goyal 	/* input */
928a74af788SAkhil Goyal 	in_sg = &cf->sg[1];
929a74af788SAkhil Goyal 	/* need to extend the input to a compound frame */
930a74af788SAkhil Goyal 	in_sg->extension = 1;
931a74af788SAkhil Goyal 	in_sg->final = 1;
932c5788a10SHemant Agrawal 	in_sg->length = data_len;
933ec861560SGagandeep Singh 	qm_sg_entry_set64(in_sg, rte_dpaa_mem_vtop(&cf->sg[2]));
934a74af788SAkhil Goyal 
935a74af788SAkhil Goyal 	/* 1st seg */
936a74af788SAkhil Goyal 	sg = in_sg + 1;
937a74af788SAkhil Goyal 
938c5788a10SHemant Agrawal 	if (ses->iv.length) {
939c5788a10SHemant Agrawal 		uint8_t *iv_ptr;
940c5788a10SHemant Agrawal 
941c5788a10SHemant Agrawal 		iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
942c5788a10SHemant Agrawal 						   ses->iv.offset);
943c5788a10SHemant Agrawal 
944c5788a10SHemant Agrawal 		if (ses->auth_alg == RTE_CRYPTO_AUTH_SNOW3G_UIA2) {
945c5788a10SHemant Agrawal 			iv_ptr = conv_to_snow_f9_iv(iv_ptr);
946c5788a10SHemant Agrawal 			sg->length = 12;
947c5788a10SHemant Agrawal 		} else if (ses->auth_alg == RTE_CRYPTO_AUTH_ZUC_EIA3) {
948c5788a10SHemant Agrawal 			iv_ptr = conv_to_zuc_eia_iv(iv_ptr);
949c5788a10SHemant Agrawal 			sg->length = 8;
950c5788a10SHemant Agrawal 		} else {
951c5788a10SHemant Agrawal 			sg->length = ses->iv.length;
952c5788a10SHemant Agrawal 		}
953ec861560SGagandeep Singh 		qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(iv_ptr));
954c5788a10SHemant Agrawal 		in_sg->length += sg->length;
955c5788a10SHemant Agrawal 		cpu_to_hw_sg(sg);
956c5788a10SHemant Agrawal 		sg++;
957c5788a10SHemant Agrawal 	}
958c5788a10SHemant Agrawal 
959ce627d63SThomas Monjalon 	qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
960c5788a10SHemant Agrawal 	sg->offset = data_offset;
961c5788a10SHemant Agrawal 
962c5788a10SHemant Agrawal 	if (data_len <= (mbuf->data_len - data_offset)) {
963c5788a10SHemant Agrawal 		sg->length = data_len;
964c5788a10SHemant Agrawal 	} else {
965c5788a10SHemant Agrawal 		sg->length = mbuf->data_len - data_offset;
966c5788a10SHemant Agrawal 
967c5788a10SHemant Agrawal 		/* remaining i/p segs */
968c5788a10SHemant Agrawal 		while ((data_len = data_len - sg->length) &&
969c5788a10SHemant Agrawal 		       (mbuf = mbuf->next)) {
970a74af788SAkhil Goyal 			cpu_to_hw_sg(sg);
971a74af788SAkhil Goyal 			sg++;
972ce627d63SThomas Monjalon 			qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
973c5788a10SHemant Agrawal 			if (data_len > mbuf->data_len)
974a74af788SAkhil Goyal 				sg->length = mbuf->data_len;
975c5788a10SHemant Agrawal 			else
976c5788a10SHemant Agrawal 				sg->length = data_len;
977c5788a10SHemant Agrawal 		}
978a74af788SAkhil Goyal 	}
979a74af788SAkhil Goyal 
980a74af788SAkhil Goyal 	if (is_decode(ses)) {
981a74af788SAkhil Goyal 		/* Digest verification case */
982a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
983a74af788SAkhil Goyal 		sg++;
984a74af788SAkhil Goyal 		rte_memcpy(old_digest, sym->auth.digest.data,
985a74af788SAkhil Goyal 				ses->digest_length);
986ec861560SGagandeep Singh 		start_addr = rte_dpaa_mem_vtop(old_digest);
987a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, start_addr);
988a74af788SAkhil Goyal 		sg->length = ses->digest_length;
989a74af788SAkhil Goyal 		in_sg->length += ses->digest_length;
990a74af788SAkhil Goyal 	}
991a74af788SAkhil Goyal 	sg->final = 1;
992a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
993a74af788SAkhil Goyal 	cpu_to_hw_sg(in_sg);
994a74af788SAkhil Goyal 
995a74af788SAkhil Goyal 	return cf;
996a74af788SAkhil Goyal }
997a74af788SAkhil Goyal 
998c3e85bdcSAkhil Goyal /**
999c3e85bdcSAkhil Goyal  * packet looks like:
1000c3e85bdcSAkhil Goyal  *		|<----data_len------->|
1001c3e85bdcSAkhil Goyal  *    |ip_header|ah_header|icv|payload|
1002c3e85bdcSAkhil Goyal  *              ^
1003c3e85bdcSAkhil Goyal  *		|
1004c3e85bdcSAkhil Goyal  *	   mbuf->pkt.data
1005c3e85bdcSAkhil Goyal  */
1006c3e85bdcSAkhil Goyal static inline struct dpaa_sec_job *
1007c3e85bdcSAkhil Goyal build_auth_only(struct rte_crypto_op *op, dpaa_sec_session *ses)
1008c3e85bdcSAkhil Goyal {
1009c3e85bdcSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1010c3e85bdcSAkhil Goyal 	struct rte_mbuf *mbuf = sym->m_src;
1011c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
1012c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1013c5788a10SHemant Agrawal 	struct qm_sg_entry *sg, *in_sg;
1014c4509373SSantosh Shukla 	rte_iova_t start_addr;
1015c3e85bdcSAkhil Goyal 	uint8_t *old_digest;
1016c5788a10SHemant Agrawal 	int data_len, data_offset;
1017c5788a10SHemant Agrawal 
1018c5788a10SHemant Agrawal 	data_len = sym->auth.data.length;
1019c5788a10SHemant Agrawal 	data_offset = sym->auth.data.offset;
1020c5788a10SHemant Agrawal 
1021c5788a10SHemant Agrawal 	if (ses->auth_alg == RTE_CRYPTO_AUTH_SNOW3G_UIA2 ||
1022c5788a10SHemant Agrawal 	    ses->auth_alg == RTE_CRYPTO_AUTH_ZUC_EIA3) {
1023c5788a10SHemant Agrawal 		if ((data_len & 7) || (data_offset & 7)) {
1024c5788a10SHemant Agrawal 			DPAA_SEC_ERR("AUTH: len/offset must be full bytes");
1025c5788a10SHemant Agrawal 			return NULL;
1026c5788a10SHemant Agrawal 		}
1027c5788a10SHemant Agrawal 
1028c5788a10SHemant Agrawal 		data_len = data_len >> 3;
1029c5788a10SHemant Agrawal 		data_offset = data_offset >> 3;
1030c5788a10SHemant Agrawal 	}
1031c3e85bdcSAkhil Goyal 
1032f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, 4);
1033c3e85bdcSAkhil Goyal 	if (!ctx)
1034c3e85bdcSAkhil Goyal 		return NULL;
1035c3e85bdcSAkhil Goyal 
1036c3e85bdcSAkhil Goyal 	cf = &ctx->job;
1037c3e85bdcSAkhil Goyal 	ctx->op = op;
1038c3e85bdcSAkhil Goyal 	old_digest = ctx->digest;
1039c3e85bdcSAkhil Goyal 
1040bfa9a8a4SThomas Monjalon 	start_addr = rte_pktmbuf_iova(mbuf);
1041c3e85bdcSAkhil Goyal 	/* output */
1042c3e85bdcSAkhil Goyal 	sg = &cf->sg[0];
1043c3e85bdcSAkhil Goyal 	qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
1044c3e85bdcSAkhil Goyal 	sg->length = ses->digest_length;
1045c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1046c3e85bdcSAkhil Goyal 
1047c3e85bdcSAkhil Goyal 	/* input */
1048c5788a10SHemant Agrawal 	in_sg = &cf->sg[1];
1049c3e85bdcSAkhil Goyal 	/* need to extend the input to a compound frame */
1050c5788a10SHemant Agrawal 	in_sg->extension = 1;
1051c5788a10SHemant Agrawal 	in_sg->final = 1;
1052c5788a10SHemant Agrawal 	in_sg->length = data_len;
1053ec861560SGagandeep Singh 	qm_sg_entry_set64(in_sg, rte_dpaa_mem_vtop(&cf->sg[2]));
1054c3e85bdcSAkhil Goyal 	sg = &cf->sg[2];
1055c5788a10SHemant Agrawal 
1056c5788a10SHemant Agrawal 	if (ses->iv.length) {
1057c5788a10SHemant Agrawal 		uint8_t *iv_ptr;
1058c5788a10SHemant Agrawal 
1059c5788a10SHemant Agrawal 		iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1060c5788a10SHemant Agrawal 						   ses->iv.offset);
1061c5788a10SHemant Agrawal 
1062c5788a10SHemant Agrawal 		if (ses->auth_alg == RTE_CRYPTO_AUTH_SNOW3G_UIA2) {
1063c5788a10SHemant Agrawal 			iv_ptr = conv_to_snow_f9_iv(iv_ptr);
1064c5788a10SHemant Agrawal 			sg->length = 12;
1065c5788a10SHemant Agrawal 		} else if (ses->auth_alg == RTE_CRYPTO_AUTH_ZUC_EIA3) {
1066c5788a10SHemant Agrawal 			iv_ptr = conv_to_zuc_eia_iv(iv_ptr);
1067c5788a10SHemant Agrawal 			sg->length = 8;
1068c5788a10SHemant Agrawal 		} else {
1069c5788a10SHemant Agrawal 			sg->length = ses->iv.length;
1070c5788a10SHemant Agrawal 		}
1071ec861560SGagandeep Singh 		qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(iv_ptr));
1072c5788a10SHemant Agrawal 		in_sg->length += sg->length;
1073c5788a10SHemant Agrawal 		cpu_to_hw_sg(sg);
1074c5788a10SHemant Agrawal 		sg++;
1075c5788a10SHemant Agrawal 	}
1076c5788a10SHemant Agrawal 
1077ce627d63SThomas Monjalon 	qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1078c5788a10SHemant Agrawal 	sg->offset = data_offset;
1079c5788a10SHemant Agrawal 	sg->length = data_len;
1080c5788a10SHemant Agrawal 
1081c5788a10SHemant Agrawal 	if (is_decode(ses)) {
1082c5788a10SHemant Agrawal 		/* Digest verification case */
1083c5788a10SHemant Agrawal 		cpu_to_hw_sg(sg);
1084c3e85bdcSAkhil Goyal 		/* hash result or digest, save digest first */
1085c3e85bdcSAkhil Goyal 		rte_memcpy(old_digest, sym->auth.digest.data,
1086c3e85bdcSAkhil Goyal 				ses->digest_length);
1087c3e85bdcSAkhil Goyal 		/* let's check digest by hw */
1088ec861560SGagandeep Singh 		start_addr = rte_dpaa_mem_vtop(old_digest);
1089c3e85bdcSAkhil Goyal 		sg++;
1090c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, start_addr);
1091c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
1092c5788a10SHemant Agrawal 		in_sg->length += ses->digest_length;
1093c3e85bdcSAkhil Goyal 	}
1094c5788a10SHemant Agrawal 	sg->final = 1;
1095c5788a10SHemant Agrawal 	cpu_to_hw_sg(sg);
1096c5788a10SHemant Agrawal 	cpu_to_hw_sg(in_sg);
1097c3e85bdcSAkhil Goyal 
1098c3e85bdcSAkhil Goyal 	return cf;
1099c3e85bdcSAkhil Goyal }
1100c3e85bdcSAkhil Goyal 
1101c3e85bdcSAkhil Goyal static inline struct dpaa_sec_job *
1102a74af788SAkhil Goyal build_cipher_only_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
1103a74af788SAkhil Goyal {
1104a74af788SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1105a74af788SAkhil Goyal 	struct dpaa_sec_job *cf;
1106a74af788SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1107a74af788SAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
1108a74af788SAkhil Goyal 	struct rte_mbuf *mbuf;
1109a74af788SAkhil Goyal 	uint8_t req_segs;
1110a74af788SAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1111a74af788SAkhil Goyal 			ses->iv.offset);
1112c5788a10SHemant Agrawal 	int data_len, data_offset;
1113c5788a10SHemant Agrawal 
1114c5788a10SHemant Agrawal 	data_len = sym->cipher.data.length;
1115c5788a10SHemant Agrawal 	data_offset = sym->cipher.data.offset;
1116c5788a10SHemant Agrawal 
1117c5788a10SHemant Agrawal 	if (ses->cipher_alg == RTE_CRYPTO_CIPHER_SNOW3G_UEA2 ||
1118c5788a10SHemant Agrawal 		ses->cipher_alg == RTE_CRYPTO_CIPHER_ZUC_EEA3) {
1119c5788a10SHemant Agrawal 		if ((data_len & 7) || (data_offset & 7)) {
1120c5788a10SHemant Agrawal 			DPAA_SEC_ERR("CIPHER: len/offset must be full bytes");
1121c5788a10SHemant Agrawal 			return NULL;
1122c5788a10SHemant Agrawal 		}
1123c5788a10SHemant Agrawal 
1124c5788a10SHemant Agrawal 		data_len = data_len >> 3;
1125c5788a10SHemant Agrawal 		data_offset = data_offset >> 3;
1126c5788a10SHemant Agrawal 	}
1127a74af788SAkhil Goyal 
1128a74af788SAkhil Goyal 	if (sym->m_dst) {
1129a74af788SAkhil Goyal 		mbuf = sym->m_dst;
1130a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 3;
1131a74af788SAkhil Goyal 	} else {
1132a74af788SAkhil Goyal 		mbuf = sym->m_src;
1133a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs * 2 + 3;
1134a74af788SAkhil Goyal 	}
1135f7a5752eSHemant Agrawal 	if (mbuf->nb_segs > MAX_SG_ENTRIES) {
1136f163231eSHemant Agrawal 		DPAA_SEC_DP_ERR("Cipher: Max sec segs supported is %d",
1137a74af788SAkhil Goyal 				MAX_SG_ENTRIES);
1138a74af788SAkhil Goyal 		return NULL;
1139a74af788SAkhil Goyal 	}
1140a74af788SAkhil Goyal 
1141f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, req_segs);
1142a74af788SAkhil Goyal 	if (!ctx)
1143a74af788SAkhil Goyal 		return NULL;
1144a74af788SAkhil Goyal 
1145a74af788SAkhil Goyal 	cf = &ctx->job;
1146a74af788SAkhil Goyal 	ctx->op = op;
1147a74af788SAkhil Goyal 
1148a74af788SAkhil Goyal 	/* output */
1149a74af788SAkhil Goyal 	out_sg = &cf->sg[0];
1150a74af788SAkhil Goyal 	out_sg->extension = 1;
1151c5788a10SHemant Agrawal 	out_sg->length = data_len;
1152ec861560SGagandeep Singh 	qm_sg_entry_set64(out_sg, rte_dpaa_mem_vtop(&cf->sg[2]));
1153a74af788SAkhil Goyal 	cpu_to_hw_sg(out_sg);
1154a74af788SAkhil Goyal 
1155a74af788SAkhil Goyal 	/* 1st seg */
1156a74af788SAkhil Goyal 	sg = &cf->sg[2];
1157ce627d63SThomas Monjalon 	qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1158c5788a10SHemant Agrawal 	sg->length = mbuf->data_len - data_offset;
1159c5788a10SHemant Agrawal 	sg->offset = data_offset;
1160a74af788SAkhil Goyal 
1161a74af788SAkhil Goyal 	/* Successive segs */
1162a74af788SAkhil Goyal 	mbuf = mbuf->next;
1163a74af788SAkhil Goyal 	while (mbuf) {
1164a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1165a74af788SAkhil Goyal 		sg++;
1166ce627d63SThomas Monjalon 		qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1167a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1168a74af788SAkhil Goyal 		mbuf = mbuf->next;
1169a74af788SAkhil Goyal 	}
1170a74af788SAkhil Goyal 	sg->final = 1;
1171a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1172a74af788SAkhil Goyal 
1173a74af788SAkhil Goyal 	/* input */
1174a74af788SAkhil Goyal 	mbuf = sym->m_src;
1175a74af788SAkhil Goyal 	in_sg = &cf->sg[1];
1176a74af788SAkhil Goyal 	in_sg->extension = 1;
1177a74af788SAkhil Goyal 	in_sg->final = 1;
1178c5788a10SHemant Agrawal 	in_sg->length = data_len + ses->iv.length;
1179a74af788SAkhil Goyal 
1180a74af788SAkhil Goyal 	sg++;
1181ec861560SGagandeep Singh 	qm_sg_entry_set64(in_sg, rte_dpaa_mem_vtop(sg));
1182a74af788SAkhil Goyal 	cpu_to_hw_sg(in_sg);
1183a74af788SAkhil Goyal 
1184a74af788SAkhil Goyal 	/* IV */
1185ec861560SGagandeep Singh 	qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(IV_ptr));
1186a74af788SAkhil Goyal 	sg->length = ses->iv.length;
1187a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1188a74af788SAkhil Goyal 
1189a74af788SAkhil Goyal 	/* 1st seg */
1190a74af788SAkhil Goyal 	sg++;
1191ce627d63SThomas Monjalon 	qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1192c5788a10SHemant Agrawal 	sg->length = mbuf->data_len - data_offset;
1193c5788a10SHemant Agrawal 	sg->offset = data_offset;
1194a74af788SAkhil Goyal 
1195a74af788SAkhil Goyal 	/* Successive segs */
1196a74af788SAkhil Goyal 	mbuf = mbuf->next;
1197a74af788SAkhil Goyal 	while (mbuf) {
1198a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1199a74af788SAkhil Goyal 		sg++;
1200ce627d63SThomas Monjalon 		qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1201a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1202a74af788SAkhil Goyal 		mbuf = mbuf->next;
1203a74af788SAkhil Goyal 	}
1204a74af788SAkhil Goyal 	sg->final = 1;
1205a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1206a74af788SAkhil Goyal 
1207a74af788SAkhil Goyal 	return cf;
1208a74af788SAkhil Goyal }
1209a74af788SAkhil Goyal 
1210a74af788SAkhil Goyal static inline struct dpaa_sec_job *
1211c3e85bdcSAkhil Goyal build_cipher_only(struct rte_crypto_op *op, dpaa_sec_session *ses)
1212c3e85bdcSAkhil Goyal {
1213c3e85bdcSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1214c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
1215c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1216c3e85bdcSAkhil Goyal 	struct qm_sg_entry *sg;
1217c4509373SSantosh Shukla 	rte_iova_t src_start_addr, dst_start_addr;
1218c3e85bdcSAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1219c3e85bdcSAkhil Goyal 			ses->iv.offset);
1220c5788a10SHemant Agrawal 	int data_len, data_offset;
1221c5788a10SHemant Agrawal 
1222c5788a10SHemant Agrawal 	data_len = sym->cipher.data.length;
1223c5788a10SHemant Agrawal 	data_offset = sym->cipher.data.offset;
1224c5788a10SHemant Agrawal 
1225c5788a10SHemant Agrawal 	if (ses->cipher_alg == RTE_CRYPTO_CIPHER_SNOW3G_UEA2 ||
1226c5788a10SHemant Agrawal 		ses->cipher_alg == RTE_CRYPTO_CIPHER_ZUC_EEA3) {
1227c5788a10SHemant Agrawal 		if ((data_len & 7) || (data_offset & 7)) {
1228c5788a10SHemant Agrawal 			DPAA_SEC_ERR("CIPHER: len/offset must be full bytes");
1229c5788a10SHemant Agrawal 			return NULL;
1230c5788a10SHemant Agrawal 		}
1231c5788a10SHemant Agrawal 
1232c5788a10SHemant Agrawal 		data_len = data_len >> 3;
1233c5788a10SHemant Agrawal 		data_offset = data_offset >> 3;
1234c5788a10SHemant Agrawal 	}
1235c3e85bdcSAkhil Goyal 
1236f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, 4);
1237c3e85bdcSAkhil Goyal 	if (!ctx)
1238c3e85bdcSAkhil Goyal 		return NULL;
1239c3e85bdcSAkhil Goyal 
1240c3e85bdcSAkhil Goyal 	cf = &ctx->job;
1241c3e85bdcSAkhil Goyal 	ctx->op = op;
1242a389434eSAlok Makhariya 
1243bfa9a8a4SThomas Monjalon 	src_start_addr = rte_pktmbuf_iova(sym->m_src);
1244a389434eSAlok Makhariya 
1245a389434eSAlok Makhariya 	if (sym->m_dst)
1246bfa9a8a4SThomas Monjalon 		dst_start_addr = rte_pktmbuf_iova(sym->m_dst);
1247a389434eSAlok Makhariya 	else
1248a389434eSAlok Makhariya 		dst_start_addr = src_start_addr;
1249c3e85bdcSAkhil Goyal 
1250c3e85bdcSAkhil Goyal 	/* output */
1251c3e85bdcSAkhil Goyal 	sg = &cf->sg[0];
1252c5788a10SHemant Agrawal 	qm_sg_entry_set64(sg, dst_start_addr + data_offset);
1253c5788a10SHemant Agrawal 	sg->length = data_len + ses->iv.length;
1254c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1255c3e85bdcSAkhil Goyal 
1256c3e85bdcSAkhil Goyal 	/* input */
1257c3e85bdcSAkhil Goyal 	sg = &cf->sg[1];
1258c3e85bdcSAkhil Goyal 
1259c3e85bdcSAkhil Goyal 	/* need to extend the input to a compound frame */
1260c3e85bdcSAkhil Goyal 	sg->extension = 1;
1261c3e85bdcSAkhil Goyal 	sg->final = 1;
1262c5788a10SHemant Agrawal 	sg->length = data_len + ses->iv.length;
1263ec861560SGagandeep Singh 	qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(&cf->sg[2]));
1264c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1265c3e85bdcSAkhil Goyal 
1266c3e85bdcSAkhil Goyal 	sg = &cf->sg[2];
1267ec861560SGagandeep Singh 	qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(IV_ptr));
1268c3e85bdcSAkhil Goyal 	sg->length = ses->iv.length;
1269c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1270c3e85bdcSAkhil Goyal 
1271c3e85bdcSAkhil Goyal 	sg++;
1272c5788a10SHemant Agrawal 	qm_sg_entry_set64(sg, src_start_addr + data_offset);
1273c5788a10SHemant Agrawal 	sg->length = data_len;
1274c3e85bdcSAkhil Goyal 	sg->final = 1;
1275c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1276c3e85bdcSAkhil Goyal 
1277c3e85bdcSAkhil Goyal 	return cf;
1278c3e85bdcSAkhil Goyal }
1279c3e85bdcSAkhil Goyal 
1280c3e85bdcSAkhil Goyal static inline struct dpaa_sec_job *
1281a74af788SAkhil Goyal build_cipher_auth_gcm_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
1282a74af788SAkhil Goyal {
1283a74af788SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1284a74af788SAkhil Goyal 	struct dpaa_sec_job *cf;
1285a74af788SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1286a74af788SAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
1287a74af788SAkhil Goyal 	struct rte_mbuf *mbuf;
1288a74af788SAkhil Goyal 	uint8_t req_segs;
1289a74af788SAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1290a74af788SAkhil Goyal 			ses->iv.offset);
1291a74af788SAkhil Goyal 
1292a74af788SAkhil Goyal 	if (sym->m_dst) {
1293a74af788SAkhil Goyal 		mbuf = sym->m_dst;
1294a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 4;
1295a74af788SAkhil Goyal 	} else {
1296a74af788SAkhil Goyal 		mbuf = sym->m_src;
1297a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs * 2 + 4;
1298a74af788SAkhil Goyal 	}
1299a74af788SAkhil Goyal 
1300a74af788SAkhil Goyal 	if (ses->auth_only_len)
1301a74af788SAkhil Goyal 		req_segs++;
1302a74af788SAkhil Goyal 
1303f7a5752eSHemant Agrawal 	if (mbuf->nb_segs > MAX_SG_ENTRIES) {
1304f163231eSHemant Agrawal 		DPAA_SEC_DP_ERR("AEAD: Max sec segs supported is %d",
1305a74af788SAkhil Goyal 				MAX_SG_ENTRIES);
1306a74af788SAkhil Goyal 		return NULL;
1307a74af788SAkhil Goyal 	}
1308a74af788SAkhil Goyal 
1309f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, req_segs);
1310a74af788SAkhil Goyal 	if (!ctx)
1311a74af788SAkhil Goyal 		return NULL;
1312a74af788SAkhil Goyal 
1313a74af788SAkhil Goyal 	cf = &ctx->job;
1314a74af788SAkhil Goyal 	ctx->op = op;
1315a74af788SAkhil Goyal 
1316a74af788SAkhil Goyal 	rte_prefetch0(cf->sg);
1317a74af788SAkhil Goyal 
1318a74af788SAkhil Goyal 	/* output */
1319a74af788SAkhil Goyal 	out_sg = &cf->sg[0];
1320a74af788SAkhil Goyal 	out_sg->extension = 1;
1321a74af788SAkhil Goyal 	if (is_encode(ses))
13227a4a6da4SVakul Garg 		out_sg->length = sym->aead.data.length + ses->digest_length;
1323a74af788SAkhil Goyal 	else
13247a4a6da4SVakul Garg 		out_sg->length = sym->aead.data.length;
1325a74af788SAkhil Goyal 
1326a74af788SAkhil Goyal 	/* output sg entries */
1327a74af788SAkhil Goyal 	sg = &cf->sg[2];
1328ec861560SGagandeep Singh 	qm_sg_entry_set64(out_sg, rte_dpaa_mem_vtop(sg));
1329a74af788SAkhil Goyal 	cpu_to_hw_sg(out_sg);
1330a74af788SAkhil Goyal 
1331a74af788SAkhil Goyal 	/* 1st seg */
1332ce627d63SThomas Monjalon 	qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
13337a4a6da4SVakul Garg 	sg->length = mbuf->data_len - sym->aead.data.offset;
13347a4a6da4SVakul Garg 	sg->offset = sym->aead.data.offset;
1335a74af788SAkhil Goyal 
1336a74af788SAkhil Goyal 	/* Successive segs */
1337a74af788SAkhil Goyal 	mbuf = mbuf->next;
1338a74af788SAkhil Goyal 	while (mbuf) {
1339a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1340a74af788SAkhil Goyal 		sg++;
1341ce627d63SThomas Monjalon 		qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1342a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1343a74af788SAkhil Goyal 		mbuf = mbuf->next;
1344a74af788SAkhil Goyal 	}
1345a74af788SAkhil Goyal 	sg->length -= ses->digest_length;
1346a74af788SAkhil Goyal 
1347a74af788SAkhil Goyal 	if (is_encode(ses)) {
1348a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1349a74af788SAkhil Goyal 		/* set auth output */
1350a74af788SAkhil Goyal 		sg++;
1351a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, sym->aead.digest.phys_addr);
1352a74af788SAkhil Goyal 		sg->length = ses->digest_length;
1353a74af788SAkhil Goyal 	}
1354a74af788SAkhil Goyal 	sg->final = 1;
1355a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1356a74af788SAkhil Goyal 
1357a74af788SAkhil Goyal 	/* input */
1358a74af788SAkhil Goyal 	mbuf = sym->m_src;
1359a74af788SAkhil Goyal 	in_sg = &cf->sg[1];
1360a74af788SAkhil Goyal 	in_sg->extension = 1;
1361a74af788SAkhil Goyal 	in_sg->final = 1;
1362a74af788SAkhil Goyal 	if (is_encode(ses))
1363a74af788SAkhil Goyal 		in_sg->length = ses->iv.length + sym->aead.data.length
1364a74af788SAkhil Goyal 							+ ses->auth_only_len;
1365a74af788SAkhil Goyal 	else
1366a74af788SAkhil Goyal 		in_sg->length = ses->iv.length + sym->aead.data.length
1367a74af788SAkhil Goyal 				+ ses->auth_only_len + ses->digest_length;
1368a74af788SAkhil Goyal 
1369a74af788SAkhil Goyal 	/* input sg entries */
1370a74af788SAkhil Goyal 	sg++;
1371ec861560SGagandeep Singh 	qm_sg_entry_set64(in_sg, rte_dpaa_mem_vtop(sg));
1372a74af788SAkhil Goyal 	cpu_to_hw_sg(in_sg);
1373a74af788SAkhil Goyal 
1374a74af788SAkhil Goyal 	/* 1st seg IV */
1375ec861560SGagandeep Singh 	qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(IV_ptr));
1376a74af788SAkhil Goyal 	sg->length = ses->iv.length;
1377a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1378a74af788SAkhil Goyal 
1379a74af788SAkhil Goyal 	/* 2nd seg auth only */
1380a74af788SAkhil Goyal 	if (ses->auth_only_len) {
1381a74af788SAkhil Goyal 		sg++;
1382ec861560SGagandeep Singh 		qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(sym->aead.aad.data));
1383a74af788SAkhil Goyal 		sg->length = ses->auth_only_len;
1384a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1385a74af788SAkhil Goyal 	}
1386a74af788SAkhil Goyal 
1387a74af788SAkhil Goyal 	/* 3rd seg */
1388a74af788SAkhil Goyal 	sg++;
1389ce627d63SThomas Monjalon 	qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1390a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->aead.data.offset;
1391a74af788SAkhil Goyal 	sg->offset = sym->aead.data.offset;
1392a74af788SAkhil Goyal 
1393a74af788SAkhil Goyal 	/* Successive segs */
1394a74af788SAkhil Goyal 	mbuf = mbuf->next;
1395a74af788SAkhil Goyal 	while (mbuf) {
1396a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1397a74af788SAkhil Goyal 		sg++;
1398ce627d63SThomas Monjalon 		qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1399a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1400a74af788SAkhil Goyal 		mbuf = mbuf->next;
1401a74af788SAkhil Goyal 	}
1402a74af788SAkhil Goyal 
1403a74af788SAkhil Goyal 	if (is_decode(ses)) {
1404a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1405a74af788SAkhil Goyal 		sg++;
1406a74af788SAkhil Goyal 		memcpy(ctx->digest, sym->aead.digest.data,
1407a74af788SAkhil Goyal 			ses->digest_length);
1408ec861560SGagandeep Singh 		qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(ctx->digest));
1409a74af788SAkhil Goyal 		sg->length = ses->digest_length;
1410a74af788SAkhil Goyal 	}
1411a74af788SAkhil Goyal 	sg->final = 1;
1412a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1413a74af788SAkhil Goyal 
1414a74af788SAkhil Goyal 	return cf;
1415a74af788SAkhil Goyal }
1416a74af788SAkhil Goyal 
1417a74af788SAkhil Goyal static inline struct dpaa_sec_job *
1418c3e85bdcSAkhil Goyal build_cipher_auth_gcm(struct rte_crypto_op *op, dpaa_sec_session *ses)
1419c3e85bdcSAkhil Goyal {
1420c3e85bdcSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1421c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
1422c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1423c3e85bdcSAkhil Goyal 	struct qm_sg_entry *sg;
1424c3e85bdcSAkhil Goyal 	uint32_t length = 0;
1425c4509373SSantosh Shukla 	rte_iova_t src_start_addr, dst_start_addr;
1426c3e85bdcSAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1427c3e85bdcSAkhil Goyal 			ses->iv.offset);
1428c3e85bdcSAkhil Goyal 
1429116ff44aSHemant Agrawal 	src_start_addr = sym->m_src->buf_iova + sym->m_src->data_off;
1430a389434eSAlok Makhariya 
1431a389434eSAlok Makhariya 	if (sym->m_dst)
1432116ff44aSHemant Agrawal 		dst_start_addr = sym->m_dst->buf_iova + sym->m_dst->data_off;
1433a389434eSAlok Makhariya 	else
1434a389434eSAlok Makhariya 		dst_start_addr = src_start_addr;
1435c3e85bdcSAkhil Goyal 
1436f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, 7);
1437c3e85bdcSAkhil Goyal 	if (!ctx)
1438c3e85bdcSAkhil Goyal 		return NULL;
1439c3e85bdcSAkhil Goyal 
1440c3e85bdcSAkhil Goyal 	cf = &ctx->job;
1441c3e85bdcSAkhil Goyal 	ctx->op = op;
1442c3e85bdcSAkhil Goyal 
1443c3e85bdcSAkhil Goyal 	/* input */
1444c3e85bdcSAkhil Goyal 	rte_prefetch0(cf->sg);
1445c3e85bdcSAkhil Goyal 	sg = &cf->sg[2];
1446ec861560SGagandeep Singh 	qm_sg_entry_set64(&cf->sg[1], rte_dpaa_mem_vtop(sg));
1447c3e85bdcSAkhil Goyal 	if (is_encode(ses)) {
1448ec861560SGagandeep Singh 		qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(IV_ptr));
1449c3e85bdcSAkhil Goyal 		sg->length = ses->iv.length;
1450c3e85bdcSAkhil Goyal 		length += sg->length;
1451c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1452c3e85bdcSAkhil Goyal 
1453c3e85bdcSAkhil Goyal 		sg++;
1454c3e85bdcSAkhil Goyal 		if (ses->auth_only_len) {
1455c3e85bdcSAkhil Goyal 			qm_sg_entry_set64(sg,
1456ec861560SGagandeep Singh 					  rte_dpaa_mem_vtop(sym->aead.aad.data));
1457c3e85bdcSAkhil Goyal 			sg->length = ses->auth_only_len;
1458c3e85bdcSAkhil Goyal 			length += sg->length;
1459c3e85bdcSAkhil Goyal 			cpu_to_hw_sg(sg);
1460c3e85bdcSAkhil Goyal 			sg++;
1461c3e85bdcSAkhil Goyal 		}
1462a389434eSAlok Makhariya 		qm_sg_entry_set64(sg, src_start_addr + sym->aead.data.offset);
1463c3e85bdcSAkhil Goyal 		sg->length = sym->aead.data.length;
1464c3e85bdcSAkhil Goyal 		length += sg->length;
1465c3e85bdcSAkhil Goyal 		sg->final = 1;
1466c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1467c3e85bdcSAkhil Goyal 	} else {
1468ec861560SGagandeep Singh 		qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(IV_ptr));
1469c3e85bdcSAkhil Goyal 		sg->length = ses->iv.length;
1470c3e85bdcSAkhil Goyal 		length += sg->length;
1471c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1472c3e85bdcSAkhil Goyal 
1473c3e85bdcSAkhil Goyal 		sg++;
1474c3e85bdcSAkhil Goyal 		if (ses->auth_only_len) {
1475c3e85bdcSAkhil Goyal 			qm_sg_entry_set64(sg,
1476ec861560SGagandeep Singh 					  rte_dpaa_mem_vtop(sym->aead.aad.data));
1477c3e85bdcSAkhil Goyal 			sg->length = ses->auth_only_len;
1478c3e85bdcSAkhil Goyal 			length += sg->length;
1479c3e85bdcSAkhil Goyal 			cpu_to_hw_sg(sg);
1480c3e85bdcSAkhil Goyal 			sg++;
1481c3e85bdcSAkhil Goyal 		}
1482a389434eSAlok Makhariya 		qm_sg_entry_set64(sg, src_start_addr + sym->aead.data.offset);
1483c3e85bdcSAkhil Goyal 		sg->length = sym->aead.data.length;
1484c3e85bdcSAkhil Goyal 		length += sg->length;
1485c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1486c3e85bdcSAkhil Goyal 
1487c3e85bdcSAkhil Goyal 		memcpy(ctx->digest, sym->aead.digest.data,
1488c3e85bdcSAkhil Goyal 		       ses->digest_length);
1489c3e85bdcSAkhil Goyal 		sg++;
1490c3e85bdcSAkhil Goyal 
1491ec861560SGagandeep Singh 		qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(ctx->digest));
1492c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
1493c3e85bdcSAkhil Goyal 		length += sg->length;
1494c3e85bdcSAkhil Goyal 		sg->final = 1;
1495c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1496c3e85bdcSAkhil Goyal 	}
1497c3e85bdcSAkhil Goyal 	/* input compound frame */
1498c3e85bdcSAkhil Goyal 	cf->sg[1].length = length;
1499c3e85bdcSAkhil Goyal 	cf->sg[1].extension = 1;
1500c3e85bdcSAkhil Goyal 	cf->sg[1].final = 1;
1501c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(&cf->sg[1]);
1502c3e85bdcSAkhil Goyal 
1503c3e85bdcSAkhil Goyal 	/* output */
1504c3e85bdcSAkhil Goyal 	sg++;
1505ec861560SGagandeep Singh 	qm_sg_entry_set64(&cf->sg[0], rte_dpaa_mem_vtop(sg));
1506c3e85bdcSAkhil Goyal 	qm_sg_entry_set64(sg,
15077a4a6da4SVakul Garg 		dst_start_addr + sym->aead.data.offset);
15087a4a6da4SVakul Garg 	sg->length = sym->aead.data.length;
1509c3e85bdcSAkhil Goyal 	length = sg->length;
1510c3e85bdcSAkhil Goyal 	if (is_encode(ses)) {
1511c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1512c3e85bdcSAkhil Goyal 		/* set auth output */
1513c3e85bdcSAkhil Goyal 		sg++;
1514c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, sym->aead.digest.phys_addr);
1515c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
1516c3e85bdcSAkhil Goyal 		length += sg->length;
1517c3e85bdcSAkhil Goyal 	}
1518c3e85bdcSAkhil Goyal 	sg->final = 1;
1519c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1520c3e85bdcSAkhil Goyal 
1521c3e85bdcSAkhil Goyal 	/* output compound frame */
1522c3e85bdcSAkhil Goyal 	cf->sg[0].length = length;
1523c3e85bdcSAkhil Goyal 	cf->sg[0].extension = 1;
1524c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(&cf->sg[0]);
1525c3e85bdcSAkhil Goyal 
1526c3e85bdcSAkhil Goyal 	return cf;
1527c3e85bdcSAkhil Goyal }
1528c3e85bdcSAkhil Goyal 
1529c3e85bdcSAkhil Goyal static inline struct dpaa_sec_job *
1530a74af788SAkhil Goyal build_cipher_auth_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
1531a74af788SAkhil Goyal {
1532a74af788SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1533a74af788SAkhil Goyal 	struct dpaa_sec_job *cf;
1534a74af788SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1535a74af788SAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
1536a74af788SAkhil Goyal 	struct rte_mbuf *mbuf;
1537a74af788SAkhil Goyal 	uint8_t req_segs;
1538a74af788SAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1539a74af788SAkhil Goyal 			ses->iv.offset);
1540a74af788SAkhil Goyal 
1541a74af788SAkhil Goyal 	if (sym->m_dst) {
1542a74af788SAkhil Goyal 		mbuf = sym->m_dst;
1543a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 4;
1544a74af788SAkhil Goyal 	} else {
1545a74af788SAkhil Goyal 		mbuf = sym->m_src;
1546a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs * 2 + 4;
1547a74af788SAkhil Goyal 	}
1548a74af788SAkhil Goyal 
1549f7a5752eSHemant Agrawal 	if (mbuf->nb_segs > MAX_SG_ENTRIES) {
1550f163231eSHemant Agrawal 		DPAA_SEC_DP_ERR("Cipher-Auth: Max sec segs supported is %d",
1551a74af788SAkhil Goyal 				MAX_SG_ENTRIES);
1552a74af788SAkhil Goyal 		return NULL;
1553a74af788SAkhil Goyal 	}
1554a74af788SAkhil Goyal 
1555f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, req_segs);
1556a74af788SAkhil Goyal 	if (!ctx)
1557a74af788SAkhil Goyal 		return NULL;
1558a74af788SAkhil Goyal 
1559a74af788SAkhil Goyal 	cf = &ctx->job;
1560a74af788SAkhil Goyal 	ctx->op = op;
1561a74af788SAkhil Goyal 
1562a74af788SAkhil Goyal 	rte_prefetch0(cf->sg);
1563a74af788SAkhil Goyal 
1564a74af788SAkhil Goyal 	/* output */
1565a74af788SAkhil Goyal 	out_sg = &cf->sg[0];
1566a74af788SAkhil Goyal 	out_sg->extension = 1;
1567a74af788SAkhil Goyal 	if (is_encode(ses))
1568a74af788SAkhil Goyal 		out_sg->length = sym->auth.data.length + ses->digest_length;
1569a74af788SAkhil Goyal 	else
1570a74af788SAkhil Goyal 		out_sg->length = sym->auth.data.length;
1571a74af788SAkhil Goyal 
1572a74af788SAkhil Goyal 	/* output sg entries */
1573a74af788SAkhil Goyal 	sg = &cf->sg[2];
1574ec861560SGagandeep Singh 	qm_sg_entry_set64(out_sg, rte_dpaa_mem_vtop(sg));
1575a74af788SAkhil Goyal 	cpu_to_hw_sg(out_sg);
1576a74af788SAkhil Goyal 
1577a74af788SAkhil Goyal 	/* 1st seg */
1578ce627d63SThomas Monjalon 	qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1579a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->auth.data.offset;
1580a74af788SAkhil Goyal 	sg->offset = sym->auth.data.offset;
1581a74af788SAkhil Goyal 
1582a74af788SAkhil Goyal 	/* Successive segs */
1583a74af788SAkhil Goyal 	mbuf = mbuf->next;
1584a74af788SAkhil Goyal 	while (mbuf) {
1585a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1586a74af788SAkhil Goyal 		sg++;
1587ce627d63SThomas Monjalon 		qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1588a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1589a74af788SAkhil Goyal 		mbuf = mbuf->next;
1590a74af788SAkhil Goyal 	}
1591a74af788SAkhil Goyal 	sg->length -= ses->digest_length;
1592a74af788SAkhil Goyal 
1593a74af788SAkhil Goyal 	if (is_encode(ses)) {
1594a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1595a74af788SAkhil Goyal 		/* set auth output */
1596a74af788SAkhil Goyal 		sg++;
1597a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
1598a74af788SAkhil Goyal 		sg->length = ses->digest_length;
1599a74af788SAkhil Goyal 	}
1600a74af788SAkhil Goyal 	sg->final = 1;
1601a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1602a74af788SAkhil Goyal 
1603a74af788SAkhil Goyal 	/* input */
1604a74af788SAkhil Goyal 	mbuf = sym->m_src;
1605a74af788SAkhil Goyal 	in_sg = &cf->sg[1];
1606a74af788SAkhil Goyal 	in_sg->extension = 1;
1607a74af788SAkhil Goyal 	in_sg->final = 1;
1608a74af788SAkhil Goyal 	if (is_encode(ses))
1609a74af788SAkhil Goyal 		in_sg->length = ses->iv.length + sym->auth.data.length;
1610a74af788SAkhil Goyal 	else
1611a74af788SAkhil Goyal 		in_sg->length = ses->iv.length + sym->auth.data.length
1612a74af788SAkhil Goyal 						+ ses->digest_length;
1613a74af788SAkhil Goyal 
1614a74af788SAkhil Goyal 	/* input sg entries */
1615a74af788SAkhil Goyal 	sg++;
1616ec861560SGagandeep Singh 	qm_sg_entry_set64(in_sg, rte_dpaa_mem_vtop(sg));
1617a74af788SAkhil Goyal 	cpu_to_hw_sg(in_sg);
1618a74af788SAkhil Goyal 
1619a74af788SAkhil Goyal 	/* 1st seg IV */
1620ec861560SGagandeep Singh 	qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(IV_ptr));
1621a74af788SAkhil Goyal 	sg->length = ses->iv.length;
1622a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1623a74af788SAkhil Goyal 
1624a74af788SAkhil Goyal 	/* 2nd seg */
1625a74af788SAkhil Goyal 	sg++;
1626ce627d63SThomas Monjalon 	qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1627a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->auth.data.offset;
1628a74af788SAkhil Goyal 	sg->offset = sym->auth.data.offset;
1629a74af788SAkhil Goyal 
1630a74af788SAkhil Goyal 	/* Successive segs */
1631a74af788SAkhil Goyal 	mbuf = mbuf->next;
1632a74af788SAkhil Goyal 	while (mbuf) {
1633a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1634a74af788SAkhil Goyal 		sg++;
1635ce627d63SThomas Monjalon 		qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1636a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1637a74af788SAkhil Goyal 		mbuf = mbuf->next;
1638a74af788SAkhil Goyal 	}
1639a74af788SAkhil Goyal 
1640a74af788SAkhil Goyal 	sg->length -= ses->digest_length;
1641a74af788SAkhil Goyal 	if (is_decode(ses)) {
1642a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1643a74af788SAkhil Goyal 		sg++;
1644a74af788SAkhil Goyal 		memcpy(ctx->digest, sym->auth.digest.data,
1645a74af788SAkhil Goyal 			ses->digest_length);
1646ec861560SGagandeep Singh 		qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(ctx->digest));
1647a74af788SAkhil Goyal 		sg->length = ses->digest_length;
1648a74af788SAkhil Goyal 	}
1649a74af788SAkhil Goyal 	sg->final = 1;
1650a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1651a74af788SAkhil Goyal 
1652a74af788SAkhil Goyal 	return cf;
1653a74af788SAkhil Goyal }
1654a74af788SAkhil Goyal 
1655a74af788SAkhil Goyal static inline struct dpaa_sec_job *
1656c3e85bdcSAkhil Goyal build_cipher_auth(struct rte_crypto_op *op, dpaa_sec_session *ses)
1657c3e85bdcSAkhil Goyal {
1658c3e85bdcSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1659c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
1660c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1661c3e85bdcSAkhil Goyal 	struct qm_sg_entry *sg;
1662c4509373SSantosh Shukla 	rte_iova_t src_start_addr, dst_start_addr;
1663c3e85bdcSAkhil Goyal 	uint32_t length = 0;
1664c3e85bdcSAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1665c3e85bdcSAkhil Goyal 			ses->iv.offset);
1666c3e85bdcSAkhil Goyal 
1667455da545SSantosh Shukla 	src_start_addr = sym->m_src->buf_iova + sym->m_src->data_off;
1668a389434eSAlok Makhariya 	if (sym->m_dst)
1669455da545SSantosh Shukla 		dst_start_addr = sym->m_dst->buf_iova + sym->m_dst->data_off;
1670a389434eSAlok Makhariya 	else
1671a389434eSAlok Makhariya 		dst_start_addr = src_start_addr;
1672c3e85bdcSAkhil Goyal 
1673f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, 7);
1674c3e85bdcSAkhil Goyal 	if (!ctx)
1675c3e85bdcSAkhil Goyal 		return NULL;
1676c3e85bdcSAkhil Goyal 
1677c3e85bdcSAkhil Goyal 	cf = &ctx->job;
1678c3e85bdcSAkhil Goyal 	ctx->op = op;
1679c3e85bdcSAkhil Goyal 
1680c3e85bdcSAkhil Goyal 	/* input */
1681c3e85bdcSAkhil Goyal 	rte_prefetch0(cf->sg);
1682c3e85bdcSAkhil Goyal 	sg = &cf->sg[2];
1683ec861560SGagandeep Singh 	qm_sg_entry_set64(&cf->sg[1], rte_dpaa_mem_vtop(sg));
1684c3e85bdcSAkhil Goyal 	if (is_encode(ses)) {
1685ec861560SGagandeep Singh 		qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(IV_ptr));
1686c3e85bdcSAkhil Goyal 		sg->length = ses->iv.length;
1687c3e85bdcSAkhil Goyal 		length += sg->length;
1688c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1689c3e85bdcSAkhil Goyal 
1690c3e85bdcSAkhil Goyal 		sg++;
1691a389434eSAlok Makhariya 		qm_sg_entry_set64(sg, src_start_addr + sym->auth.data.offset);
1692c3e85bdcSAkhil Goyal 		sg->length = sym->auth.data.length;
1693c3e85bdcSAkhil Goyal 		length += sg->length;
1694c3e85bdcSAkhil Goyal 		sg->final = 1;
1695c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1696c3e85bdcSAkhil Goyal 	} else {
1697ec861560SGagandeep Singh 		qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(IV_ptr));
1698c3e85bdcSAkhil Goyal 		sg->length = ses->iv.length;
1699c3e85bdcSAkhil Goyal 		length += sg->length;
1700c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1701c3e85bdcSAkhil Goyal 
1702c3e85bdcSAkhil Goyal 		sg++;
1703c3e85bdcSAkhil Goyal 
1704a389434eSAlok Makhariya 		qm_sg_entry_set64(sg, src_start_addr + sym->auth.data.offset);
1705c3e85bdcSAkhil Goyal 		sg->length = sym->auth.data.length;
1706c3e85bdcSAkhil Goyal 		length += sg->length;
1707c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1708c3e85bdcSAkhil Goyal 
1709c3e85bdcSAkhil Goyal 		memcpy(ctx->digest, sym->auth.digest.data,
1710c3e85bdcSAkhil Goyal 		       ses->digest_length);
1711c3e85bdcSAkhil Goyal 		sg++;
1712c3e85bdcSAkhil Goyal 
1713ec861560SGagandeep Singh 		qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(ctx->digest));
1714c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
1715c3e85bdcSAkhil Goyal 		length += sg->length;
1716c3e85bdcSAkhil Goyal 		sg->final = 1;
1717c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1718c3e85bdcSAkhil Goyal 	}
1719c3e85bdcSAkhil Goyal 	/* input compound frame */
1720c3e85bdcSAkhil Goyal 	cf->sg[1].length = length;
1721c3e85bdcSAkhil Goyal 	cf->sg[1].extension = 1;
1722c3e85bdcSAkhil Goyal 	cf->sg[1].final = 1;
1723c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(&cf->sg[1]);
1724c3e85bdcSAkhil Goyal 
1725c3e85bdcSAkhil Goyal 	/* output */
1726c3e85bdcSAkhil Goyal 	sg++;
1727ec861560SGagandeep Singh 	qm_sg_entry_set64(&cf->sg[0], rte_dpaa_mem_vtop(sg));
1728a389434eSAlok Makhariya 	qm_sg_entry_set64(sg, dst_start_addr + sym->cipher.data.offset);
1729c3e85bdcSAkhil Goyal 	sg->length = sym->cipher.data.length;
1730c3e85bdcSAkhil Goyal 	length = sg->length;
1731c3e85bdcSAkhil Goyal 	if (is_encode(ses)) {
1732c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1733c3e85bdcSAkhil Goyal 		/* set auth output */
1734c3e85bdcSAkhil Goyal 		sg++;
1735c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
1736c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
1737c3e85bdcSAkhil Goyal 		length += sg->length;
1738c3e85bdcSAkhil Goyal 	}
1739c3e85bdcSAkhil Goyal 	sg->final = 1;
1740c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1741c3e85bdcSAkhil Goyal 
1742c3e85bdcSAkhil Goyal 	/* output compound frame */
1743c3e85bdcSAkhil Goyal 	cf->sg[0].length = length;
1744c3e85bdcSAkhil Goyal 	cf->sg[0].extension = 1;
1745c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(&cf->sg[0]);
1746c3e85bdcSAkhil Goyal 
1747c3e85bdcSAkhil Goyal 	return cf;
1748c3e85bdcSAkhil Goyal }
1749c3e85bdcSAkhil Goyal 
1750a8d0d473SBruce Richardson #ifdef RTE_LIB_SECURITY
17511f14d500SAkhil Goyal static inline struct dpaa_sec_job *
17521f14d500SAkhil Goyal build_proto(struct rte_crypto_op *op, dpaa_sec_session *ses)
17531f14d500SAkhil Goyal {
17541f14d500SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
17551f14d500SAkhil Goyal 	struct dpaa_sec_job *cf;
17561f14d500SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
17571f14d500SAkhil Goyal 	struct qm_sg_entry *sg;
17581f14d500SAkhil Goyal 	phys_addr_t src_start_addr, dst_start_addr;
17591f14d500SAkhil Goyal 
1760f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, 2);
17611f14d500SAkhil Goyal 	if (!ctx)
17621f14d500SAkhil Goyal 		return NULL;
17631f14d500SAkhil Goyal 	cf = &ctx->job;
17641f14d500SAkhil Goyal 	ctx->op = op;
17651f14d500SAkhil Goyal 
1766ce627d63SThomas Monjalon 	src_start_addr = rte_pktmbuf_iova(sym->m_src);
17671f14d500SAkhil Goyal 
17681f14d500SAkhil Goyal 	if (sym->m_dst)
1769ce627d63SThomas Monjalon 		dst_start_addr = rte_pktmbuf_iova(sym->m_dst);
17701f14d500SAkhil Goyal 	else
17711f14d500SAkhil Goyal 		dst_start_addr = src_start_addr;
17721f14d500SAkhil Goyal 
17731f14d500SAkhil Goyal 	/* input */
17741f14d500SAkhil Goyal 	sg = &cf->sg[1];
17751f14d500SAkhil Goyal 	qm_sg_entry_set64(sg, src_start_addr);
17761f14d500SAkhil Goyal 	sg->length = sym->m_src->pkt_len;
17771f14d500SAkhil Goyal 	sg->final = 1;
17781f14d500SAkhil Goyal 	cpu_to_hw_sg(sg);
17791f14d500SAkhil Goyal 
17801f14d500SAkhil Goyal 	sym->m_src->packet_type &= ~RTE_PTYPE_L4_MASK;
17811f14d500SAkhil Goyal 	/* output */
17821f14d500SAkhil Goyal 	sg = &cf->sg[0];
17831f14d500SAkhil Goyal 	qm_sg_entry_set64(sg, dst_start_addr);
17841f14d500SAkhil Goyal 	sg->length = sym->m_src->buf_len - sym->m_src->data_off;
17851f14d500SAkhil Goyal 	cpu_to_hw_sg(sg);
17861f14d500SAkhil Goyal 
17871f14d500SAkhil Goyal 	return cf;
17881f14d500SAkhil Goyal }
17891f14d500SAkhil Goyal 
1790fb5c100aSAkhil Goyal static inline struct dpaa_sec_job *
1791fb5c100aSAkhil Goyal build_proto_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
1792fb5c100aSAkhil Goyal {
1793fb5c100aSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1794fb5c100aSAkhil Goyal 	struct dpaa_sec_job *cf;
1795fb5c100aSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1796fb5c100aSAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
1797fb5c100aSAkhil Goyal 	struct rte_mbuf *mbuf;
1798fb5c100aSAkhil Goyal 	uint8_t req_segs;
1799fb5c100aSAkhil Goyal 	uint32_t in_len = 0, out_len = 0;
1800fb5c100aSAkhil Goyal 
1801fb5c100aSAkhil Goyal 	if (sym->m_dst)
1802fb5c100aSAkhil Goyal 		mbuf = sym->m_dst;
1803fb5c100aSAkhil Goyal 	else
1804fb5c100aSAkhil Goyal 		mbuf = sym->m_src;
1805fb5c100aSAkhil Goyal 
1806fb5c100aSAkhil Goyal 	req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 2;
1807f7a5752eSHemant Agrawal 	if (mbuf->nb_segs > MAX_SG_ENTRIES) {
1808fb5c100aSAkhil Goyal 		DPAA_SEC_DP_ERR("Proto: Max sec segs supported is %d",
1809fb5c100aSAkhil Goyal 				MAX_SG_ENTRIES);
1810fb5c100aSAkhil Goyal 		return NULL;
1811fb5c100aSAkhil Goyal 	}
1812fb5c100aSAkhil Goyal 
1813f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, req_segs);
1814fb5c100aSAkhil Goyal 	if (!ctx)
1815fb5c100aSAkhil Goyal 		return NULL;
1816fb5c100aSAkhil Goyal 	cf = &ctx->job;
1817fb5c100aSAkhil Goyal 	ctx->op = op;
1818fb5c100aSAkhil Goyal 	/* output */
1819fb5c100aSAkhil Goyal 	out_sg = &cf->sg[0];
1820fb5c100aSAkhil Goyal 	out_sg->extension = 1;
1821ec861560SGagandeep Singh 	qm_sg_entry_set64(out_sg, rte_dpaa_mem_vtop(&cf->sg[2]));
1822fb5c100aSAkhil Goyal 
1823fb5c100aSAkhil Goyal 	/* 1st seg */
1824fb5c100aSAkhil Goyal 	sg = &cf->sg[2];
1825ce627d63SThomas Monjalon 	qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1826fb5c100aSAkhil Goyal 	sg->offset = 0;
1827fb5c100aSAkhil Goyal 
1828fb5c100aSAkhil Goyal 	/* Successive segs */
1829fb5c100aSAkhil Goyal 	while (mbuf->next) {
1830fb5c100aSAkhil Goyal 		sg->length = mbuf->data_len;
1831fb5c100aSAkhil Goyal 		out_len += sg->length;
1832fb5c100aSAkhil Goyal 		mbuf = mbuf->next;
1833fb5c100aSAkhil Goyal 		cpu_to_hw_sg(sg);
1834fb5c100aSAkhil Goyal 		sg++;
1835ce627d63SThomas Monjalon 		qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1836fb5c100aSAkhil Goyal 		sg->offset = 0;
1837fb5c100aSAkhil Goyal 	}
1838fb5c100aSAkhil Goyal 	sg->length = mbuf->buf_len - mbuf->data_off;
1839fb5c100aSAkhil Goyal 	out_len += sg->length;
1840fb5c100aSAkhil Goyal 	sg->final = 1;
1841fb5c100aSAkhil Goyal 	cpu_to_hw_sg(sg);
1842fb5c100aSAkhil Goyal 
1843fb5c100aSAkhil Goyal 	out_sg->length = out_len;
1844fb5c100aSAkhil Goyal 	cpu_to_hw_sg(out_sg);
1845fb5c100aSAkhil Goyal 
1846fb5c100aSAkhil Goyal 	/* input */
1847fb5c100aSAkhil Goyal 	mbuf = sym->m_src;
1848fb5c100aSAkhil Goyal 	in_sg = &cf->sg[1];
1849fb5c100aSAkhil Goyal 	in_sg->extension = 1;
1850fb5c100aSAkhil Goyal 	in_sg->final = 1;
1851fb5c100aSAkhil Goyal 	in_len = mbuf->data_len;
1852fb5c100aSAkhil Goyal 
1853fb5c100aSAkhil Goyal 	sg++;
1854ec861560SGagandeep Singh 	qm_sg_entry_set64(in_sg, rte_dpaa_mem_vtop(sg));
1855fb5c100aSAkhil Goyal 
1856fb5c100aSAkhil Goyal 	/* 1st seg */
1857ce627d63SThomas Monjalon 	qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1858fb5c100aSAkhil Goyal 	sg->length = mbuf->data_len;
1859fb5c100aSAkhil Goyal 	sg->offset = 0;
1860fb5c100aSAkhil Goyal 
1861fb5c100aSAkhil Goyal 	/* Successive segs */
1862fb5c100aSAkhil Goyal 	mbuf = mbuf->next;
1863fb5c100aSAkhil Goyal 	while (mbuf) {
1864fb5c100aSAkhil Goyal 		cpu_to_hw_sg(sg);
1865fb5c100aSAkhil Goyal 		sg++;
1866ce627d63SThomas Monjalon 		qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1867fb5c100aSAkhil Goyal 		sg->length = mbuf->data_len;
1868fb5c100aSAkhil Goyal 		sg->offset = 0;
1869fb5c100aSAkhil Goyal 		in_len += sg->length;
1870fb5c100aSAkhil Goyal 		mbuf = mbuf->next;
1871fb5c100aSAkhil Goyal 	}
1872fb5c100aSAkhil Goyal 	sg->final = 1;
1873fb5c100aSAkhil Goyal 	cpu_to_hw_sg(sg);
1874fb5c100aSAkhil Goyal 
1875fb5c100aSAkhil Goyal 	in_sg->length = in_len;
1876fb5c100aSAkhil Goyal 	cpu_to_hw_sg(in_sg);
1877fb5c100aSAkhil Goyal 
1878fb5c100aSAkhil Goyal 	sym->m_src->packet_type &= ~RTE_PTYPE_L4_MASK;
1879fb5c100aSAkhil Goyal 
1880fb5c100aSAkhil Goyal 	return cf;
1881fb5c100aSAkhil Goyal }
1882314424b6SHemant Agrawal #endif
1883fb5c100aSAkhil Goyal 
18849a984458SAkhil Goyal static uint16_t
18859a984458SAkhil Goyal dpaa_sec_enqueue_burst(void *qp, struct rte_crypto_op **ops,
18869a984458SAkhil Goyal 		       uint16_t nb_ops)
1887c3e85bdcSAkhil Goyal {
18889a984458SAkhil Goyal 	/* Function to transmit the frames to given device and queuepair */
18899a984458SAkhil Goyal 	uint32_t loop;
18909a984458SAkhil Goyal 	struct dpaa_sec_qp *dpaa_qp = (struct dpaa_sec_qp *)qp;
18919a984458SAkhil Goyal 	uint16_t num_tx = 0;
18929a984458SAkhil Goyal 	struct qm_fd fds[DPAA_SEC_BURST], *fd;
18939a984458SAkhil Goyal 	uint32_t frames_to_send;
18949a984458SAkhil Goyal 	struct rte_crypto_op *op;
1895c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
1896c3e85bdcSAkhil Goyal 	dpaa_sec_session *ses;
18973394ed47SVakul Garg 	uint16_t auth_hdr_len, auth_tail_len;
18983394ed47SVakul Garg 	uint32_t index, flags[DPAA_SEC_BURST] = {0};
18999a984458SAkhil Goyal 	struct qman_fq *inq[DPAA_SEC_BURST];
1900c3e85bdcSAkhil Goyal 
190122629f05SHemant Agrawal 	if (unlikely(!DPAA_PER_LCORE_PORTAL)) {
190222629f05SHemant Agrawal 		if (rte_dpaa_portal_init((void *)0)) {
190322629f05SHemant Agrawal 			DPAA_SEC_ERR("Failure in affining portal");
190422629f05SHemant Agrawal 			return 0;
190522629f05SHemant Agrawal 		}
190622629f05SHemant Agrawal 	}
190722629f05SHemant Agrawal 
19089a984458SAkhil Goyal 	while (nb_ops) {
19099a984458SAkhil Goyal 		frames_to_send = (nb_ops > DPAA_SEC_BURST) ?
19109a984458SAkhil Goyal 				DPAA_SEC_BURST : nb_ops;
19119a984458SAkhil Goyal 		for (loop = 0; loop < frames_to_send; loop++) {
19129a984458SAkhil Goyal 			op = *(ops++);
1913c9a1c2e5SDavid Marchand 			if (*dpaa_seqn(op->sym->m_src) != 0) {
1914c9a1c2e5SDavid Marchand 				index = *dpaa_seqn(op->sym->m_src) - 1;
1915fe3688baSAkhil Goyal 				if (DPAA_PER_LCORE_DQRR_HELD & (1 << index)) {
1916fe3688baSAkhil Goyal 					/* QM_EQCR_DCA_IDXMASK = 0x0f */
1917fe3688baSAkhil Goyal 					flags[loop] = ((index & 0x0f) << 8);
1918fe3688baSAkhil Goyal 					flags[loop] |= QMAN_ENQUEUE_FLAG_DCA;
1919fe3688baSAkhil Goyal 					DPAA_PER_LCORE_DQRR_SIZE--;
1920fe3688baSAkhil Goyal 					DPAA_PER_LCORE_DQRR_HELD &=
1921fe3688baSAkhil Goyal 								~(1 << index);
1922fe3688baSAkhil Goyal 				}
1923fe3688baSAkhil Goyal 			}
1924fe3688baSAkhil Goyal 
19259a984458SAkhil Goyal 			switch (op->sess_type) {
19269a984458SAkhil Goyal 			case RTE_CRYPTO_OP_WITH_SESSION:
1927*bdce2564SAkhil Goyal 				ses = (void *)
1928*bdce2564SAkhil Goyal 					op->sym->session->driver_priv_data;
19299a984458SAkhil Goyal 				break;
1930a8d0d473SBruce Richardson #ifdef RTE_LIB_SECURITY
19319a984458SAkhil Goyal 			case RTE_CRYPTO_OP_SECURITY_SESSION:
19329a984458SAkhil Goyal 				ses = (dpaa_sec_session *)
19339a984458SAkhil Goyal 					get_sec_session_private_data(
19341f14d500SAkhil Goyal 							op->sym->sec_session);
19359a984458SAkhil Goyal 				break;
1936314424b6SHemant Agrawal #endif
19379a984458SAkhil Goyal 			default:
1938f163231eSHemant Agrawal 				DPAA_SEC_DP_ERR(
19399a984458SAkhil Goyal 					"sessionless crypto op not supported");
19409a984458SAkhil Goyal 				frames_to_send = loop;
19419a984458SAkhil Goyal 				nb_ops = loop;
19429a984458SAkhil Goyal 				goto send_pkts;
19439a984458SAkhil Goyal 			}
1944e1e52232SHemant Agrawal 
1945e1e52232SHemant Agrawal 			if (!ses) {
1946e1e52232SHemant Agrawal 				DPAA_SEC_DP_ERR("session not available");
1947e1e52232SHemant Agrawal 				frames_to_send = loop;
1948e1e52232SHemant Agrawal 				nb_ops = loop;
1949e1e52232SHemant Agrawal 				goto send_pkts;
1950e1e52232SHemant Agrawal 			}
1951e1e52232SHemant Agrawal 
19524e694fe5SAkhil Goyal 			if (unlikely(!ses->qp[rte_lcore_id() % MAX_DPAA_CORES])) {
19539a984458SAkhil Goyal 				if (dpaa_sec_attach_sess_q(qp, ses)) {
19549a984458SAkhil Goyal 					frames_to_send = loop;
19559a984458SAkhil Goyal 					nb_ops = loop;
19569a984458SAkhil Goyal 					goto send_pkts;
19579a984458SAkhil Goyal 				}
19584e694fe5SAkhil Goyal 			} else if (unlikely(ses->qp[rte_lcore_id() %
19594e694fe5SAkhil Goyal 						MAX_DPAA_CORES] != qp)) {
19609198b2c2SAkhil Goyal 				DPAA_SEC_DP_ERR("Old:sess->qp = %p"
19614e694fe5SAkhil Goyal 					" New qp = %p\n",
19624e694fe5SAkhil Goyal 					ses->qp[rte_lcore_id() %
19634e694fe5SAkhil Goyal 					MAX_DPAA_CORES], qp);
19649198b2c2SAkhil Goyal 				frames_to_send = loop;
19659198b2c2SAkhil Goyal 				nb_ops = loop;
19669198b2c2SAkhil Goyal 				goto send_pkts;
1967c3e85bdcSAkhil Goyal 			}
1968c3e85bdcSAkhil Goyal 
19693394ed47SVakul Garg 			auth_hdr_len = op->sym->auth.data.length -
19709a984458SAkhil Goyal 						op->sym->cipher.data.length;
19713394ed47SVakul Garg 			auth_tail_len = 0;
19723394ed47SVakul Garg 
1973fb5c100aSAkhil Goyal 			if (rte_pktmbuf_is_contiguous(op->sym->m_src) &&
1974fb5c100aSAkhil Goyal 				  ((op->sym->m_dst == NULL) ||
1975fb5c100aSAkhil Goyal 				   rte_pktmbuf_is_contiguous(op->sym->m_dst))) {
19768524b44eSHemant Agrawal 				switch (ses->ctxt) {
1977a8d0d473SBruce Richardson #ifdef RTE_LIB_SECURITY
19788524b44eSHemant Agrawal 				case DPAA_SEC_PDCP:
19798524b44eSHemant Agrawal 				case DPAA_SEC_IPSEC:
198005b12700SHemant Agrawal 					cf = build_proto(op, ses);
19818524b44eSHemant Agrawal 					break;
1982314424b6SHemant Agrawal #endif
19838524b44eSHemant Agrawal 				case DPAA_SEC_AUTH:
1984c3e85bdcSAkhil Goyal 					cf = build_auth_only(op, ses);
19858524b44eSHemant Agrawal 					break;
19868524b44eSHemant Agrawal 				case DPAA_SEC_CIPHER:
1987c3e85bdcSAkhil Goyal 					cf = build_cipher_only(op, ses);
19888524b44eSHemant Agrawal 					break;
19898524b44eSHemant Agrawal 				case DPAA_SEC_AEAD:
1990c3e85bdcSAkhil Goyal 					cf = build_cipher_auth_gcm(op, ses);
19913394ed47SVakul Garg 					auth_hdr_len = ses->auth_only_len;
19928524b44eSHemant Agrawal 					break;
19938524b44eSHemant Agrawal 				case DPAA_SEC_CIPHER_HASH:
19943394ed47SVakul Garg 					auth_hdr_len =
19953394ed47SVakul Garg 						op->sym->cipher.data.offset
19963394ed47SVakul Garg 						- op->sym->auth.data.offset;
19973394ed47SVakul Garg 					auth_tail_len =
19983394ed47SVakul Garg 						op->sym->auth.data.length
19993394ed47SVakul Garg 						- op->sym->cipher.data.length
20003394ed47SVakul Garg 						- auth_hdr_len;
2001c3e85bdcSAkhil Goyal 					cf = build_cipher_auth(op, ses);
20028524b44eSHemant Agrawal 					break;
20038524b44eSHemant Agrawal 				default:
2004f163231eSHemant Agrawal 					DPAA_SEC_DP_ERR("not supported ops");
20059a984458SAkhil Goyal 					frames_to_send = loop;
20069a984458SAkhil Goyal 					nb_ops = loop;
20079a984458SAkhil Goyal 					goto send_pkts;
2008c3e85bdcSAkhil Goyal 				}
2009a74af788SAkhil Goyal 			} else {
20108524b44eSHemant Agrawal 				switch (ses->ctxt) {
2011a8d0d473SBruce Richardson #ifdef RTE_LIB_SECURITY
20128524b44eSHemant Agrawal 				case DPAA_SEC_PDCP:
20138524b44eSHemant Agrawal 				case DPAA_SEC_IPSEC:
2014fb5c100aSAkhil Goyal 					cf = build_proto_sg(op, ses);
20158524b44eSHemant Agrawal 					break;
2016314424b6SHemant Agrawal #endif
20178524b44eSHemant Agrawal 				case DPAA_SEC_AUTH:
2018a74af788SAkhil Goyal 					cf = build_auth_only_sg(op, ses);
20198524b44eSHemant Agrawal 					break;
20208524b44eSHemant Agrawal 				case DPAA_SEC_CIPHER:
2021a74af788SAkhil Goyal 					cf = build_cipher_only_sg(op, ses);
20228524b44eSHemant Agrawal 					break;
20238524b44eSHemant Agrawal 				case DPAA_SEC_AEAD:
2024a74af788SAkhil Goyal 					cf = build_cipher_auth_gcm_sg(op, ses);
20253394ed47SVakul Garg 					auth_hdr_len = ses->auth_only_len;
20268524b44eSHemant Agrawal 					break;
20278524b44eSHemant Agrawal 				case DPAA_SEC_CIPHER_HASH:
20283394ed47SVakul Garg 					auth_hdr_len =
20293394ed47SVakul Garg 						op->sym->cipher.data.offset
20303394ed47SVakul Garg 						- op->sym->auth.data.offset;
20313394ed47SVakul Garg 					auth_tail_len =
20323394ed47SVakul Garg 						op->sym->auth.data.length
20333394ed47SVakul Garg 						- op->sym->cipher.data.length
20343394ed47SVakul Garg 						- auth_hdr_len;
2035a74af788SAkhil Goyal 					cf = build_cipher_auth_sg(op, ses);
20368524b44eSHemant Agrawal 					break;
20378524b44eSHemant Agrawal 				default:
2038f163231eSHemant Agrawal 					DPAA_SEC_DP_ERR("not supported ops");
2039a74af788SAkhil Goyal 					frames_to_send = loop;
2040a74af788SAkhil Goyal 					nb_ops = loop;
2041a74af788SAkhil Goyal 					goto send_pkts;
2042a74af788SAkhil Goyal 				}
2043a74af788SAkhil Goyal 			}
20449a984458SAkhil Goyal 			if (unlikely(!cf)) {
20459a984458SAkhil Goyal 				frames_to_send = loop;
20469a984458SAkhil Goyal 				nb_ops = loop;
20479a984458SAkhil Goyal 				goto send_pkts;
20489a984458SAkhil Goyal 			}
2049c3e85bdcSAkhil Goyal 
20509a984458SAkhil Goyal 			fd = &fds[loop];
20514e694fe5SAkhil Goyal 			inq[loop] = ses->inq[rte_lcore_id() % MAX_DPAA_CORES];
20529a984458SAkhil Goyal 			fd->opaque_addr = 0;
20539a984458SAkhil Goyal 			fd->cmd = 0;
2054ec861560SGagandeep Singh 			qm_fd_addr_set64(fd, rte_dpaa_mem_vtop(cf->sg));
20559a984458SAkhil Goyal 			fd->_format1 = qm_fd_compound;
20569a984458SAkhil Goyal 			fd->length29 = 2 * sizeof(struct qm_sg_entry);
20573394ed47SVakul Garg 
20589a984458SAkhil Goyal 			/* Auth_only_len is set as 0 in descriptor and it is
20599a984458SAkhil Goyal 			 * overwritten here in the fd.cmd which will update
20609a984458SAkhil Goyal 			 * the DPOVRD reg.
2061c3e85bdcSAkhil Goyal 			 */
20623394ed47SVakul Garg 			if (auth_hdr_len || auth_tail_len) {
20633394ed47SVakul Garg 				fd->cmd = 0x80000000;
20643394ed47SVakul Garg 				fd->cmd |=
20653394ed47SVakul Garg 					((auth_tail_len << 16) | auth_hdr_len);
20663394ed47SVakul Garg 			}
2067c3e85bdcSAkhil Goyal 
2068a8d0d473SBruce Richardson #ifdef RTE_LIB_SECURITY
20696a0c9d36SAkhil Goyal 			/* In case of PDCP, per packet HFN is stored in
20706a0c9d36SAkhil Goyal 			 * mbuf priv after sym_op.
20716a0c9d36SAkhil Goyal 			 */
20728524b44eSHemant Agrawal 			if ((ses->ctxt == DPAA_SEC_PDCP) && ses->pdcp.hfn_ovd) {
20736a0c9d36SAkhil Goyal 				fd->cmd = 0x80000000 |
20746a0c9d36SAkhil Goyal 					*((uint32_t *)((uint8_t *)op +
20756a0c9d36SAkhil Goyal 					ses->pdcp.hfn_ovd_offset));
20768524b44eSHemant Agrawal 				DPAA_SEC_DP_DEBUG("Per packet HFN: %x, ovd:%u\n",
20776a0c9d36SAkhil Goyal 					*((uint32_t *)((uint8_t *)op +
20786a0c9d36SAkhil Goyal 					ses->pdcp.hfn_ovd_offset)),
20798524b44eSHemant Agrawal 					ses->pdcp.hfn_ovd);
20806a0c9d36SAkhil Goyal 			}
2081314424b6SHemant Agrawal #endif
20829a984458SAkhil Goyal 		}
20839a984458SAkhil Goyal send_pkts:
20849a984458SAkhil Goyal 		loop = 0;
20859a984458SAkhil Goyal 		while (loop < frames_to_send) {
20869a984458SAkhil Goyal 			loop += qman_enqueue_multi_fq(&inq[loop], &fds[loop],
2087fe3688baSAkhil Goyal 					&flags[loop], frames_to_send - loop);
20889a984458SAkhil Goyal 		}
20899a984458SAkhil Goyal 		nb_ops -= frames_to_send;
20909a984458SAkhil Goyal 		num_tx += frames_to_send;
2091c3e85bdcSAkhil Goyal 	}
2092c3e85bdcSAkhil Goyal 
2093c3e85bdcSAkhil Goyal 	dpaa_qp->tx_pkts += num_tx;
2094c3e85bdcSAkhil Goyal 	dpaa_qp->tx_errs += nb_ops - num_tx;
2095c3e85bdcSAkhil Goyal 
2096c3e85bdcSAkhil Goyal 	return num_tx;
2097c3e85bdcSAkhil Goyal }
2098c3e85bdcSAkhil Goyal 
2099c3e85bdcSAkhil Goyal static uint16_t
2100c3e85bdcSAkhil Goyal dpaa_sec_dequeue_burst(void *qp, struct rte_crypto_op **ops,
2101c3e85bdcSAkhil Goyal 		       uint16_t nb_ops)
2102c3e85bdcSAkhil Goyal {
2103c3e85bdcSAkhil Goyal 	uint16_t num_rx;
2104c3e85bdcSAkhil Goyal 	struct dpaa_sec_qp *dpaa_qp = (struct dpaa_sec_qp *)qp;
2105c3e85bdcSAkhil Goyal 
210622629f05SHemant Agrawal 	if (unlikely(!DPAA_PER_LCORE_PORTAL)) {
210722629f05SHemant Agrawal 		if (rte_dpaa_portal_init((void *)0)) {
210822629f05SHemant Agrawal 			DPAA_SEC_ERR("Failure in affining portal");
210922629f05SHemant Agrawal 			return 0;
211022629f05SHemant Agrawal 		}
211122629f05SHemant Agrawal 	}
211222629f05SHemant Agrawal 
2113c3e85bdcSAkhil Goyal 	num_rx = dpaa_sec_deq(dpaa_qp, ops, nb_ops);
2114c3e85bdcSAkhil Goyal 
2115c3e85bdcSAkhil Goyal 	dpaa_qp->rx_pkts += num_rx;
2116c3e85bdcSAkhil Goyal 	dpaa_qp->rx_errs += nb_ops - num_rx;
2117c3e85bdcSAkhil Goyal 
2118f163231eSHemant Agrawal 	DPAA_SEC_DP_DEBUG("SEC Received %d Packets\n", num_rx);
2119c3e85bdcSAkhil Goyal 
2120c3e85bdcSAkhil Goyal 	return num_rx;
2121c3e85bdcSAkhil Goyal }
2122c3e85bdcSAkhil Goyal 
2123c3e85bdcSAkhil Goyal /** Release queue pair */
2124c3e85bdcSAkhil Goyal static int
2125c3e85bdcSAkhil Goyal dpaa_sec_queue_pair_release(struct rte_cryptodev *dev,
2126c3e85bdcSAkhil Goyal 			    uint16_t qp_id)
2127c3e85bdcSAkhil Goyal {
2128c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals;
2129c3e85bdcSAkhil Goyal 	struct dpaa_sec_qp *qp = NULL;
2130c3e85bdcSAkhil Goyal 
2131c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2132c3e85bdcSAkhil Goyal 
2133f163231eSHemant Agrawal 	DPAA_SEC_DEBUG("dev =%p, queue =%d", dev, qp_id);
2134c3e85bdcSAkhil Goyal 
2135c3e85bdcSAkhil Goyal 	internals = dev->data->dev_private;
2136c3e85bdcSAkhil Goyal 	if (qp_id >= internals->max_nb_queue_pairs) {
2137f163231eSHemant Agrawal 		DPAA_SEC_ERR("Max supported qpid %d",
2138c3e85bdcSAkhil Goyal 			     internals->max_nb_queue_pairs);
2139c3e85bdcSAkhil Goyal 		return -EINVAL;
2140c3e85bdcSAkhil Goyal 	}
2141c3e85bdcSAkhil Goyal 
2142c3e85bdcSAkhil Goyal 	qp = &internals->qps[qp_id];
21432ffb940eSAkhil Goyal 	rte_mempool_free(qp->ctx_pool);
2144c3e85bdcSAkhil Goyal 	qp->internals = NULL;
2145c3e85bdcSAkhil Goyal 	dev->data->queue_pairs[qp_id] = NULL;
2146c3e85bdcSAkhil Goyal 
2147c3e85bdcSAkhil Goyal 	return 0;
2148c3e85bdcSAkhil Goyal }
2149c3e85bdcSAkhil Goyal 
2150c3e85bdcSAkhil Goyal /** Setup a queue pair */
2151c3e85bdcSAkhil Goyal static int
2152c3e85bdcSAkhil Goyal dpaa_sec_queue_pair_setup(struct rte_cryptodev *dev, uint16_t qp_id,
2153c3e85bdcSAkhil Goyal 		__rte_unused const struct rte_cryptodev_qp_conf *qp_conf,
2154725d2a7fSFan Zhang 		__rte_unused int socket_id)
2155c3e85bdcSAkhil Goyal {
2156c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals;
2157c3e85bdcSAkhil Goyal 	struct dpaa_sec_qp *qp = NULL;
21582ffb940eSAkhil Goyal 	char str[20];
2159c3e85bdcSAkhil Goyal 
2160f163231eSHemant Agrawal 	DPAA_SEC_DEBUG("dev =%p, queue =%d, conf =%p", dev, qp_id, qp_conf);
2161c3e85bdcSAkhil Goyal 
2162c3e85bdcSAkhil Goyal 	internals = dev->data->dev_private;
2163c3e85bdcSAkhil Goyal 	if (qp_id >= internals->max_nb_queue_pairs) {
2164f163231eSHemant Agrawal 		DPAA_SEC_ERR("Max supported qpid %d",
2165c3e85bdcSAkhil Goyal 			     internals->max_nb_queue_pairs);
2166c3e85bdcSAkhil Goyal 		return -EINVAL;
2167c3e85bdcSAkhil Goyal 	}
2168c3e85bdcSAkhil Goyal 
2169c3e85bdcSAkhil Goyal 	qp = &internals->qps[qp_id];
2170c3e85bdcSAkhil Goyal 	qp->internals = internals;
21712ffb940eSAkhil Goyal 	snprintf(str, sizeof(str), "ctx_pool_d%d_qp%d",
21722ffb940eSAkhil Goyal 			dev->data->dev_id, qp_id);
21732ffb940eSAkhil Goyal 	if (!qp->ctx_pool) {
21742ffb940eSAkhil Goyal 		qp->ctx_pool = rte_mempool_create((const char *)str,
21752ffb940eSAkhil Goyal 							CTX_POOL_NUM_BUFS,
21762ffb940eSAkhil Goyal 							CTX_POOL_BUF_SIZE,
21772ffb940eSAkhil Goyal 							CTX_POOL_CACHE_SIZE, 0,
21782ffb940eSAkhil Goyal 							NULL, NULL, NULL, NULL,
21792ffb940eSAkhil Goyal 							SOCKET_ID_ANY, 0);
21802ffb940eSAkhil Goyal 		if (!qp->ctx_pool) {
21812ffb940eSAkhil Goyal 			DPAA_SEC_ERR("%s create failed\n", str);
21822ffb940eSAkhil Goyal 			return -ENOMEM;
21832ffb940eSAkhil Goyal 		}
21842ffb940eSAkhil Goyal 	} else
21852ffb940eSAkhil Goyal 		DPAA_SEC_INFO("mempool already created for dev_id : %d, qp: %d",
21862ffb940eSAkhil Goyal 				dev->data->dev_id, qp_id);
2187c3e85bdcSAkhil Goyal 	dev->data->queue_pairs[qp_id] = qp;
2188c3e85bdcSAkhil Goyal 
2189c3e85bdcSAkhil Goyal 	return 0;
2190c3e85bdcSAkhil Goyal }
2191c3e85bdcSAkhil Goyal 
2192c3e85bdcSAkhil Goyal /** Returns the size of session structure */
2193c3e85bdcSAkhil Goyal static unsigned int
2194012c5076SPablo de Lara dpaa_sec_sym_session_get_size(struct rte_cryptodev *dev __rte_unused)
2195c3e85bdcSAkhil Goyal {
2196c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2197c3e85bdcSAkhil Goyal 
2198c3e85bdcSAkhil Goyal 	return sizeof(dpaa_sec_session);
2199c3e85bdcSAkhil Goyal }
2200c3e85bdcSAkhil Goyal 
2201c3e85bdcSAkhil Goyal static int
2202c3e85bdcSAkhil Goyal dpaa_sec_cipher_init(struct rte_cryptodev *dev __rte_unused,
2203c3e85bdcSAkhil Goyal 		     struct rte_crypto_sym_xform *xform,
2204c3e85bdcSAkhil Goyal 		     dpaa_sec_session *session)
2205c3e85bdcSAkhil Goyal {
2206f73d6928SHemant Agrawal 	session->ctxt = DPAA_SEC_CIPHER;
2207c3e85bdcSAkhil Goyal 	session->cipher_alg = xform->cipher.algo;
2208c3e85bdcSAkhil Goyal 	session->iv.length = xform->cipher.iv.length;
2209c3e85bdcSAkhil Goyal 	session->iv.offset = xform->cipher.iv.offset;
2210c3e85bdcSAkhil Goyal 	session->cipher_key.data = rte_zmalloc(NULL, xform->cipher.key.length,
2211c3e85bdcSAkhil Goyal 					       RTE_CACHE_LINE_SIZE);
2212c3e85bdcSAkhil Goyal 	if (session->cipher_key.data == NULL && xform->cipher.key.length > 0) {
2213f163231eSHemant Agrawal 		DPAA_SEC_ERR("No Memory for cipher key");
2214c3e85bdcSAkhil Goyal 		return -ENOMEM;
2215c3e85bdcSAkhil Goyal 	}
2216c3e85bdcSAkhil Goyal 	session->cipher_key.length = xform->cipher.key.length;
2217c3e85bdcSAkhil Goyal 
2218c3e85bdcSAkhil Goyal 	memcpy(session->cipher_key.data, xform->cipher.key.data,
2219c3e85bdcSAkhil Goyal 	       xform->cipher.key.length);
22208524b44eSHemant Agrawal 	switch (xform->cipher.algo) {
22218524b44eSHemant Agrawal 	case RTE_CRYPTO_CIPHER_AES_CBC:
22228524b44eSHemant Agrawal 		session->cipher_key.alg = OP_ALG_ALGSEL_AES;
22238524b44eSHemant Agrawal 		session->cipher_key.algmode = OP_ALG_AAI_CBC;
22248524b44eSHemant Agrawal 		break;
22253e4fbc6cSGagandeep Singh 	case RTE_CRYPTO_CIPHER_DES_CBC:
22263e4fbc6cSGagandeep Singh 		session->cipher_key.alg = OP_ALG_ALGSEL_DES;
22273e4fbc6cSGagandeep Singh 		session->cipher_key.algmode = OP_ALG_AAI_CBC;
22283e4fbc6cSGagandeep Singh 		break;
22298524b44eSHemant Agrawal 	case RTE_CRYPTO_CIPHER_3DES_CBC:
22308524b44eSHemant Agrawal 		session->cipher_key.alg = OP_ALG_ALGSEL_3DES;
22318524b44eSHemant Agrawal 		session->cipher_key.algmode = OP_ALG_AAI_CBC;
22328524b44eSHemant Agrawal 		break;
22338524b44eSHemant Agrawal 	case RTE_CRYPTO_CIPHER_AES_CTR:
22348524b44eSHemant Agrawal 		session->cipher_key.alg = OP_ALG_ALGSEL_AES;
22358524b44eSHemant Agrawal 		session->cipher_key.algmode = OP_ALG_AAI_CTR;
22368524b44eSHemant Agrawal 		break;
22378524b44eSHemant Agrawal 	case RTE_CRYPTO_CIPHER_SNOW3G_UEA2:
22388524b44eSHemant Agrawal 		session->cipher_key.alg = OP_ALG_ALGSEL_SNOW_F8;
22398524b44eSHemant Agrawal 		break;
22408524b44eSHemant Agrawal 	case RTE_CRYPTO_CIPHER_ZUC_EEA3:
22418524b44eSHemant Agrawal 		session->cipher_key.alg = OP_ALG_ALGSEL_ZUCE;
22428524b44eSHemant Agrawal 		break;
22438524b44eSHemant Agrawal 	default:
22448524b44eSHemant Agrawal 		DPAA_SEC_ERR("Crypto: Undefined Cipher specified %u",
22458524b44eSHemant Agrawal 			      xform->cipher.algo);
2246c08ced9aSAkhil Goyal 		return -ENOTSUP;
22478524b44eSHemant Agrawal 	}
2248c3e85bdcSAkhil Goyal 	session->dir = (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
2249c3e85bdcSAkhil Goyal 			DIR_ENC : DIR_DEC;
2250c3e85bdcSAkhil Goyal 
2251c3e85bdcSAkhil Goyal 	return 0;
2252c3e85bdcSAkhil Goyal }
2253c3e85bdcSAkhil Goyal 
2254c3e85bdcSAkhil Goyal static int
2255c3e85bdcSAkhil Goyal dpaa_sec_auth_init(struct rte_cryptodev *dev __rte_unused,
2256c3e85bdcSAkhil Goyal 		   struct rte_crypto_sym_xform *xform,
2257c3e85bdcSAkhil Goyal 		   dpaa_sec_session *session)
2258c3e85bdcSAkhil Goyal {
2259f73d6928SHemant Agrawal 	session->ctxt = DPAA_SEC_AUTH;
2260c3e85bdcSAkhil Goyal 	session->auth_alg = xform->auth.algo;
22614c42352cSGagandeep Singh 	session->auth_key.length = xform->auth.key.length;
22624c42352cSGagandeep Singh 	if (xform->auth.key.length) {
22634c42352cSGagandeep Singh 		session->auth_key.data =
22644c42352cSGagandeep Singh 				rte_zmalloc(NULL, xform->auth.key.length,
2265c3e85bdcSAkhil Goyal 					     RTE_CACHE_LINE_SIZE);
22664c42352cSGagandeep Singh 		if (session->auth_key.data == NULL) {
2267f163231eSHemant Agrawal 			DPAA_SEC_ERR("No Memory for auth key");
2268c3e85bdcSAkhil Goyal 			return -ENOMEM;
2269c3e85bdcSAkhil Goyal 		}
22704c42352cSGagandeep Singh 		memcpy(session->auth_key.data, xform->auth.key.data,
22714c42352cSGagandeep Singh 				xform->auth.key.length);
22724c42352cSGagandeep Singh 
22734c42352cSGagandeep Singh 	}
2274c3e85bdcSAkhil Goyal 	session->digest_length = xform->auth.digest_length;
2275c5788a10SHemant Agrawal 	if (session->cipher_alg == RTE_CRYPTO_CIPHER_NULL) {
2276c5788a10SHemant Agrawal 		session->iv.offset = xform->auth.iv.offset;
2277c5788a10SHemant Agrawal 		session->iv.length = xform->auth.iv.length;
2278c5788a10SHemant Agrawal 	}
2279c3e85bdcSAkhil Goyal 
22808524b44eSHemant Agrawal 	switch (xform->auth.algo) {
22814c42352cSGagandeep Singh 	case RTE_CRYPTO_AUTH_SHA1:
22824c42352cSGagandeep Singh 		session->auth_key.alg = OP_ALG_ALGSEL_SHA1;
22834c42352cSGagandeep Singh 		session->auth_key.algmode = OP_ALG_AAI_HASH;
22844c42352cSGagandeep Singh 		break;
22858524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_SHA1_HMAC:
22868524b44eSHemant Agrawal 		session->auth_key.alg = OP_ALG_ALGSEL_SHA1;
22878524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
22888524b44eSHemant Agrawal 		break;
22894c42352cSGagandeep Singh 	case RTE_CRYPTO_AUTH_MD5:
22904c42352cSGagandeep Singh 		session->auth_key.alg = OP_ALG_ALGSEL_MD5;
22914c42352cSGagandeep Singh 		session->auth_key.algmode = OP_ALG_AAI_HASH;
22924c42352cSGagandeep Singh 		break;
22938524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_MD5_HMAC:
22948524b44eSHemant Agrawal 		session->auth_key.alg = OP_ALG_ALGSEL_MD5;
22958524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
22968524b44eSHemant Agrawal 		break;
22974c42352cSGagandeep Singh 	case RTE_CRYPTO_AUTH_SHA224:
22984c42352cSGagandeep Singh 		session->auth_key.alg = OP_ALG_ALGSEL_SHA224;
22994c42352cSGagandeep Singh 		session->auth_key.algmode = OP_ALG_AAI_HASH;
23004c42352cSGagandeep Singh 		break;
23018524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_SHA224_HMAC:
23028524b44eSHemant Agrawal 		session->auth_key.alg = OP_ALG_ALGSEL_SHA224;
23038524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
23048524b44eSHemant Agrawal 		break;
23054c42352cSGagandeep Singh 	case RTE_CRYPTO_AUTH_SHA256:
23064c42352cSGagandeep Singh 		session->auth_key.alg = OP_ALG_ALGSEL_SHA256;
23074c42352cSGagandeep Singh 		session->auth_key.algmode = OP_ALG_AAI_HASH;
23084c42352cSGagandeep Singh 		break;
23098524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_SHA256_HMAC:
23108524b44eSHemant Agrawal 		session->auth_key.alg = OP_ALG_ALGSEL_SHA256;
23118524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
23128524b44eSHemant Agrawal 		break;
23134c42352cSGagandeep Singh 	case RTE_CRYPTO_AUTH_SHA384:
23144c42352cSGagandeep Singh 		session->auth_key.alg = OP_ALG_ALGSEL_SHA384;
23154c42352cSGagandeep Singh 		session->auth_key.algmode = OP_ALG_AAI_HASH;
23164c42352cSGagandeep Singh 		break;
23178524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_SHA384_HMAC:
23188524b44eSHemant Agrawal 		session->auth_key.alg = OP_ALG_ALGSEL_SHA384;
23198524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
23208524b44eSHemant Agrawal 		break;
23214c42352cSGagandeep Singh 	case RTE_CRYPTO_AUTH_SHA512:
23224c42352cSGagandeep Singh 		session->auth_key.alg = OP_ALG_ALGSEL_SHA512;
23234c42352cSGagandeep Singh 		session->auth_key.algmode = OP_ALG_AAI_HASH;
23244c42352cSGagandeep Singh 		break;
23258524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_SHA512_HMAC:
23268524b44eSHemant Agrawal 		session->auth_key.alg = OP_ALG_ALGSEL_SHA512;
23278524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
23288524b44eSHemant Agrawal 		break;
23298524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
23308524b44eSHemant Agrawal 		session->auth_key.alg = OP_ALG_ALGSEL_SNOW_F9;
23318524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_F9;
23328524b44eSHemant Agrawal 		break;
23338524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_ZUC_EIA3:
23348524b44eSHemant Agrawal 		session->auth_key.alg = OP_ALG_ALGSEL_ZUCA;
23358524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_F9;
23368524b44eSHemant Agrawal 		break;
233766f95673SGagandeep Singh 	case RTE_CRYPTO_AUTH_AES_XCBC_MAC:
233866f95673SGagandeep Singh 		session->auth_key.alg = OP_ALG_ALGSEL_AES;
233966f95673SGagandeep Singh 		session->auth_key.algmode = OP_ALG_AAI_XCBC_MAC;
234066f95673SGagandeep Singh 		break;
23412ed12d9bSGagandeep Singh 	case RTE_CRYPTO_AUTH_AES_CMAC:
23422ed12d9bSGagandeep Singh 		session->auth_key.alg = OP_ALG_ALGSEL_AES;
23432ed12d9bSGagandeep Singh 		session->auth_key.algmode = OP_ALG_AAI_CMAC;
23442ed12d9bSGagandeep Singh 		break;
23458524b44eSHemant Agrawal 	default:
23468524b44eSHemant Agrawal 		DPAA_SEC_ERR("Crypto: Unsupported Auth specified %u",
23478524b44eSHemant Agrawal 			      xform->auth.algo);
2348c08ced9aSAkhil Goyal 		return -ENOTSUP;
23498524b44eSHemant Agrawal 	}
23508524b44eSHemant Agrawal 
2351c3e85bdcSAkhil Goyal 	session->dir = (xform->auth.op == RTE_CRYPTO_AUTH_OP_GENERATE) ?
2352c3e85bdcSAkhil Goyal 			DIR_ENC : DIR_DEC;
2353c3e85bdcSAkhil Goyal 
2354c3e85bdcSAkhil Goyal 	return 0;
2355c3e85bdcSAkhil Goyal }
2356c3e85bdcSAkhil Goyal 
2357c3e85bdcSAkhil Goyal static int
23588524b44eSHemant Agrawal dpaa_sec_chain_init(struct rte_cryptodev *dev __rte_unused,
23598524b44eSHemant Agrawal 		   struct rte_crypto_sym_xform *xform,
23608524b44eSHemant Agrawal 		   dpaa_sec_session *session)
23618524b44eSHemant Agrawal {
23628524b44eSHemant Agrawal 
23638524b44eSHemant Agrawal 	struct rte_crypto_cipher_xform *cipher_xform;
23648524b44eSHemant Agrawal 	struct rte_crypto_auth_xform *auth_xform;
23658524b44eSHemant Agrawal 
2366f73d6928SHemant Agrawal 	session->ctxt = DPAA_SEC_CIPHER_HASH;
23678524b44eSHemant Agrawal 	if (session->auth_cipher_text) {
23688524b44eSHemant Agrawal 		cipher_xform = &xform->cipher;
23698524b44eSHemant Agrawal 		auth_xform = &xform->next->auth;
23708524b44eSHemant Agrawal 	} else {
23718524b44eSHemant Agrawal 		cipher_xform = &xform->next->cipher;
23728524b44eSHemant Agrawal 		auth_xform = &xform->auth;
23738524b44eSHemant Agrawal 	}
23748524b44eSHemant Agrawal 
23758524b44eSHemant Agrawal 	/* Set IV parameters */
23768524b44eSHemant Agrawal 	session->iv.offset = cipher_xform->iv.offset;
23778524b44eSHemant Agrawal 	session->iv.length = cipher_xform->iv.length;
23788524b44eSHemant Agrawal 
23798524b44eSHemant Agrawal 	session->cipher_key.data = rte_zmalloc(NULL, cipher_xform->key.length,
23808524b44eSHemant Agrawal 					       RTE_CACHE_LINE_SIZE);
23818524b44eSHemant Agrawal 	if (session->cipher_key.data == NULL && cipher_xform->key.length > 0) {
23828524b44eSHemant Agrawal 		DPAA_SEC_ERR("No Memory for cipher key");
2383c08ced9aSAkhil Goyal 		return -ENOMEM;
23848524b44eSHemant Agrawal 	}
23858524b44eSHemant Agrawal 	session->cipher_key.length = cipher_xform->key.length;
23868524b44eSHemant Agrawal 	session->auth_key.data = rte_zmalloc(NULL, auth_xform->key.length,
23878524b44eSHemant Agrawal 					     RTE_CACHE_LINE_SIZE);
23888524b44eSHemant Agrawal 	if (session->auth_key.data == NULL && auth_xform->key.length > 0) {
23898524b44eSHemant Agrawal 		DPAA_SEC_ERR("No Memory for auth key");
23908524b44eSHemant Agrawal 		return -ENOMEM;
23918524b44eSHemant Agrawal 	}
23928524b44eSHemant Agrawal 	session->auth_key.length = auth_xform->key.length;
23938524b44eSHemant Agrawal 	memcpy(session->cipher_key.data, cipher_xform->key.data,
23948524b44eSHemant Agrawal 	       cipher_xform->key.length);
23958524b44eSHemant Agrawal 	memcpy(session->auth_key.data, auth_xform->key.data,
23968524b44eSHemant Agrawal 	       auth_xform->key.length);
23978524b44eSHemant Agrawal 
23988524b44eSHemant Agrawal 	session->digest_length = auth_xform->digest_length;
23998524b44eSHemant Agrawal 	session->auth_alg = auth_xform->algo;
24008524b44eSHemant Agrawal 
24018524b44eSHemant Agrawal 	switch (auth_xform->algo) {
24028524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_SHA1_HMAC:
24038524b44eSHemant Agrawal 		session->auth_key.alg = OP_ALG_ALGSEL_SHA1;
24048524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
24058524b44eSHemant Agrawal 		break;
24068524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_MD5_HMAC:
24078524b44eSHemant Agrawal 		session->auth_key.alg = OP_ALG_ALGSEL_MD5;
24088524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
24098524b44eSHemant Agrawal 		break;
24108524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_SHA224_HMAC:
24118524b44eSHemant Agrawal 		session->auth_key.alg = OP_ALG_ALGSEL_SHA224;
24128524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
24138524b44eSHemant Agrawal 		break;
24148524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_SHA256_HMAC:
24158524b44eSHemant Agrawal 		session->auth_key.alg = OP_ALG_ALGSEL_SHA256;
24168524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
24178524b44eSHemant Agrawal 		break;
24188524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_SHA384_HMAC:
24198524b44eSHemant Agrawal 		session->auth_key.alg = OP_ALG_ALGSEL_SHA384;
24208524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
24218524b44eSHemant Agrawal 		break;
24228524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_SHA512_HMAC:
24238524b44eSHemant Agrawal 		session->auth_key.alg = OP_ALG_ALGSEL_SHA512;
24248524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
24258524b44eSHemant Agrawal 		break;
242666f95673SGagandeep Singh 	case RTE_CRYPTO_AUTH_AES_XCBC_MAC:
242766f95673SGagandeep Singh 		session->auth_key.alg = OP_ALG_ALGSEL_AES;
242866f95673SGagandeep Singh 		session->auth_key.algmode = OP_ALG_AAI_XCBC_MAC;
242966f95673SGagandeep Singh 		break;
24302ed12d9bSGagandeep Singh 	case RTE_CRYPTO_AUTH_AES_CMAC:
24312ed12d9bSGagandeep Singh 		session->auth_key.alg = OP_ALG_ALGSEL_AES;
24322ed12d9bSGagandeep Singh 		session->auth_key.algmode = OP_ALG_AAI_CMAC;
24332ed12d9bSGagandeep Singh 		break;
24348524b44eSHemant Agrawal 	default:
24358524b44eSHemant Agrawal 		DPAA_SEC_ERR("Crypto: Unsupported Auth specified %u",
24368524b44eSHemant Agrawal 			      auth_xform->algo);
2437c08ced9aSAkhil Goyal 		return -ENOTSUP;
24388524b44eSHemant Agrawal 	}
24398524b44eSHemant Agrawal 
24408524b44eSHemant Agrawal 	session->cipher_alg = cipher_xform->algo;
24418524b44eSHemant Agrawal 
24428524b44eSHemant Agrawal 	switch (cipher_xform->algo) {
24438524b44eSHemant Agrawal 	case RTE_CRYPTO_CIPHER_AES_CBC:
24448524b44eSHemant Agrawal 		session->cipher_key.alg = OP_ALG_ALGSEL_AES;
24458524b44eSHemant Agrawal 		session->cipher_key.algmode = OP_ALG_AAI_CBC;
24468524b44eSHemant Agrawal 		break;
24473e4fbc6cSGagandeep Singh 	case RTE_CRYPTO_CIPHER_DES_CBC:
24483e4fbc6cSGagandeep Singh 		session->cipher_key.alg = OP_ALG_ALGSEL_DES;
24493e4fbc6cSGagandeep Singh 		session->cipher_key.algmode = OP_ALG_AAI_CBC;
24503e4fbc6cSGagandeep Singh 		break;
24518524b44eSHemant Agrawal 	case RTE_CRYPTO_CIPHER_3DES_CBC:
24528524b44eSHemant Agrawal 		session->cipher_key.alg = OP_ALG_ALGSEL_3DES;
24538524b44eSHemant Agrawal 		session->cipher_key.algmode = OP_ALG_AAI_CBC;
24548524b44eSHemant Agrawal 		break;
24558524b44eSHemant Agrawal 	case RTE_CRYPTO_CIPHER_AES_CTR:
24568524b44eSHemant Agrawal 		session->cipher_key.alg = OP_ALG_ALGSEL_AES;
24578524b44eSHemant Agrawal 		session->cipher_key.algmode = OP_ALG_AAI_CTR;
24588524b44eSHemant Agrawal 		break;
24598524b44eSHemant Agrawal 	default:
24608524b44eSHemant Agrawal 		DPAA_SEC_ERR("Crypto: Undefined Cipher specified %u",
24618524b44eSHemant Agrawal 			      cipher_xform->algo);
2462c08ced9aSAkhil Goyal 		return -ENOTSUP;
24638524b44eSHemant Agrawal 	}
24648524b44eSHemant Agrawal 	session->dir = (cipher_xform->op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
24658524b44eSHemant Agrawal 				DIR_ENC : DIR_DEC;
24668524b44eSHemant Agrawal 	return 0;
24678524b44eSHemant Agrawal }
24688524b44eSHemant Agrawal 
24698524b44eSHemant Agrawal static int
2470c3e85bdcSAkhil Goyal dpaa_sec_aead_init(struct rte_cryptodev *dev __rte_unused,
2471c3e85bdcSAkhil Goyal 		   struct rte_crypto_sym_xform *xform,
2472c3e85bdcSAkhil Goyal 		   dpaa_sec_session *session)
2473c3e85bdcSAkhil Goyal {
2474c3e85bdcSAkhil Goyal 	session->aead_alg = xform->aead.algo;
24758524b44eSHemant Agrawal 	session->ctxt = DPAA_SEC_AEAD;
2476c3e85bdcSAkhil Goyal 	session->iv.length = xform->aead.iv.length;
2477c3e85bdcSAkhil Goyal 	session->iv.offset = xform->aead.iv.offset;
2478c3e85bdcSAkhil Goyal 	session->auth_only_len = xform->aead.aad_length;
2479c3e85bdcSAkhil Goyal 	session->aead_key.data = rte_zmalloc(NULL, xform->aead.key.length,
2480c3e85bdcSAkhil Goyal 					     RTE_CACHE_LINE_SIZE);
2481c3e85bdcSAkhil Goyal 	if (session->aead_key.data == NULL && xform->aead.key.length > 0) {
2482f163231eSHemant Agrawal 		DPAA_SEC_ERR("No Memory for aead key\n");
2483c3e85bdcSAkhil Goyal 		return -ENOMEM;
2484c3e85bdcSAkhil Goyal 	}
2485c3e85bdcSAkhil Goyal 	session->aead_key.length = xform->aead.key.length;
2486c3e85bdcSAkhil Goyal 	session->digest_length = xform->aead.digest_length;
2487c3e85bdcSAkhil Goyal 
2488c3e85bdcSAkhil Goyal 	memcpy(session->aead_key.data, xform->aead.key.data,
2489c3e85bdcSAkhil Goyal 	       xform->aead.key.length);
24908524b44eSHemant Agrawal 
24918524b44eSHemant Agrawal 	switch (session->aead_alg) {
24928524b44eSHemant Agrawal 	case RTE_CRYPTO_AEAD_AES_GCM:
24938524b44eSHemant Agrawal 		session->aead_key.alg = OP_ALG_ALGSEL_AES;
24948524b44eSHemant Agrawal 		session->aead_key.algmode = OP_ALG_AAI_GCM;
24958524b44eSHemant Agrawal 		break;
24968524b44eSHemant Agrawal 	default:
24978524b44eSHemant Agrawal 		DPAA_SEC_ERR("unsupported AEAD alg %d", session->aead_alg);
2498c08ced9aSAkhil Goyal 		return -ENOTSUP;
24998524b44eSHemant Agrawal 	}
25008524b44eSHemant Agrawal 
2501c3e85bdcSAkhil Goyal 	session->dir = (xform->aead.op == RTE_CRYPTO_AEAD_OP_ENCRYPT) ?
2502c3e85bdcSAkhil Goyal 			DIR_ENC : DIR_DEC;
2503c3e85bdcSAkhil Goyal 
2504c3e85bdcSAkhil Goyal 	return 0;
2505c3e85bdcSAkhil Goyal }
2506c3e85bdcSAkhil Goyal 
2507e79416d1SHemant Agrawal static struct qman_fq *
2508e79416d1SHemant Agrawal dpaa_sec_attach_rxq(struct dpaa_sec_dev_private *qi)
2509c3e85bdcSAkhil Goyal {
2510e79416d1SHemant Agrawal 	unsigned int i;
2511c3e85bdcSAkhil Goyal 
2512fd900d38SGagandeep Singh 	for (i = 0; i < RTE_DPAA_MAX_RX_QUEUE; i++) {
2513e79416d1SHemant Agrawal 		if (qi->inq_attach[i] == 0) {
2514e79416d1SHemant Agrawal 			qi->inq_attach[i] = 1;
2515e79416d1SHemant Agrawal 			return &qi->inq[i];
2516e79416d1SHemant Agrawal 		}
2517e79416d1SHemant Agrawal 	}
2518e621d970SAkhil Goyal 	DPAA_SEC_WARN("All session in use %u", qi->max_nb_sessions);
2519c3e85bdcSAkhil Goyal 
2520e79416d1SHemant Agrawal 	return NULL;
2521c3e85bdcSAkhil Goyal }
2522c3e85bdcSAkhil Goyal 
2523e79416d1SHemant Agrawal static int
2524e79416d1SHemant Agrawal dpaa_sec_detach_rxq(struct dpaa_sec_dev_private *qi, struct qman_fq *fq)
2525e79416d1SHemant Agrawal {
2526e79416d1SHemant Agrawal 	unsigned int i;
2527e79416d1SHemant Agrawal 
2528fd900d38SGagandeep Singh 	for (i = 0; i < RTE_DPAA_MAX_RX_QUEUE; i++) {
2529e79416d1SHemant Agrawal 		if (&qi->inq[i] == fq) {
2530fd900d38SGagandeep Singh 			if (qman_retire_fq(fq, NULL) != 0)
2531626c7a58SHemant Agrawal 				DPAA_SEC_DEBUG("Queue is not retired\n");
2532b4053c4bSAlok Makhariya 			qman_oos_fq(fq);
2533e79416d1SHemant Agrawal 			qi->inq_attach[i] = 0;
2534e79416d1SHemant Agrawal 			return 0;
2535e79416d1SHemant Agrawal 		}
2536e79416d1SHemant Agrawal 	}
2537e79416d1SHemant Agrawal 	return -1;
2538e79416d1SHemant Agrawal }
2539e79416d1SHemant Agrawal 
25409d5f73c2SGagandeep Singh int
2541e79416d1SHemant Agrawal dpaa_sec_attach_sess_q(struct dpaa_sec_qp *qp, dpaa_sec_session *sess)
2542e79416d1SHemant Agrawal {
2543e79416d1SHemant Agrawal 	int ret;
2544e79416d1SHemant Agrawal 
25454e694fe5SAkhil Goyal 	sess->qp[rte_lcore_id() % MAX_DPAA_CORES] = qp;
2546e5872221SRohit Raj 	if (unlikely(!DPAA_PER_LCORE_PORTAL)) {
25475b0f1bd3SAshish Jain 		ret = rte_dpaa_portal_init((void *)0);
25485b0f1bd3SAshish Jain 		if (ret) {
2549f163231eSHemant Agrawal 			DPAA_SEC_ERR("Failure in affining portal");
25505b0f1bd3SAshish Jain 			return ret;
25515b0f1bd3SAshish Jain 		}
25525b0f1bd3SAshish Jain 	}
25534e694fe5SAkhil Goyal 	ret = dpaa_sec_init_rx(sess->inq[rte_lcore_id() % MAX_DPAA_CORES],
2554ec861560SGagandeep Singh 			       rte_dpaa_mem_vtop(&sess->cdb),
2555e79416d1SHemant Agrawal 			       qman_fq_fqid(&qp->outq));
2556e79416d1SHemant Agrawal 	if (ret)
2557f163231eSHemant Agrawal 		DPAA_SEC_ERR("Unable to init sec queue");
2558e79416d1SHemant Agrawal 
2559e79416d1SHemant Agrawal 	return ret;
2560c3e85bdcSAkhil Goyal }
2561c3e85bdcSAkhil Goyal 
25626290de2cSLukasz Wojciechowski static inline void
25636290de2cSLukasz Wojciechowski free_session_data(dpaa_sec_session *s)
25646290de2cSLukasz Wojciechowski {
25656290de2cSLukasz Wojciechowski 	if (is_aead(s))
25666290de2cSLukasz Wojciechowski 		rte_free(s->aead_key.data);
25676290de2cSLukasz Wojciechowski 	else {
25686290de2cSLukasz Wojciechowski 		rte_free(s->auth_key.data);
25696290de2cSLukasz Wojciechowski 		rte_free(s->cipher_key.data);
25706290de2cSLukasz Wojciechowski 	}
25716290de2cSLukasz Wojciechowski 	memset(s, 0, sizeof(dpaa_sec_session));
25726290de2cSLukasz Wojciechowski }
25736290de2cSLukasz Wojciechowski 
2574c3e85bdcSAkhil Goyal static int
2575c3e85bdcSAkhil Goyal dpaa_sec_set_session_parameters(struct rte_cryptodev *dev,
2576c3e85bdcSAkhil Goyal 			    struct rte_crypto_sym_xform *xform,	void *sess)
2577c3e85bdcSAkhil Goyal {
2578c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals = dev->data->dev_private;
2579c3e85bdcSAkhil Goyal 	dpaa_sec_session *session = sess;
25804e694fe5SAkhil Goyal 	uint32_t i;
2581f73d6928SHemant Agrawal 	int ret;
2582c3e85bdcSAkhil Goyal 
2583c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2584c3e85bdcSAkhil Goyal 
2585c3e85bdcSAkhil Goyal 	if (unlikely(sess == NULL)) {
2586f163231eSHemant Agrawal 		DPAA_SEC_ERR("invalid session struct");
2587c3e85bdcSAkhil Goyal 		return -EINVAL;
2588c3e85bdcSAkhil Goyal 	}
2589b0894102SAkhil Goyal 	memset(session, 0, sizeof(dpaa_sec_session));
2590c3e85bdcSAkhil Goyal 
2591c3e85bdcSAkhil Goyal 	/* Default IV length = 0 */
2592c3e85bdcSAkhil Goyal 	session->iv.length = 0;
2593c3e85bdcSAkhil Goyal 
2594c3e85bdcSAkhil Goyal 	/* Cipher Only */
2595c3e85bdcSAkhil Goyal 	if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER && xform->next == NULL) {
2596c3e85bdcSAkhil Goyal 		session->auth_alg = RTE_CRYPTO_AUTH_NULL;
2597f73d6928SHemant Agrawal 		ret = dpaa_sec_cipher_init(dev, xform, session);
2598c3e85bdcSAkhil Goyal 
2599c3e85bdcSAkhil Goyal 	/* Authentication Only */
2600c3e85bdcSAkhil Goyal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
2601c3e85bdcSAkhil Goyal 		   xform->next == NULL) {
2602c3e85bdcSAkhil Goyal 		session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
26038524b44eSHemant Agrawal 		session->ctxt = DPAA_SEC_AUTH;
2604f73d6928SHemant Agrawal 		ret = dpaa_sec_auth_init(dev, xform, session);
2605c3e85bdcSAkhil Goyal 
2606c3e85bdcSAkhil Goyal 	/* Cipher then Authenticate */
2607c3e85bdcSAkhil Goyal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
2608c3e85bdcSAkhil Goyal 		   xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
2609c3e85bdcSAkhil Goyal 		if (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) {
26108524b44eSHemant Agrawal 			session->auth_cipher_text = 1;
2611f73d6928SHemant Agrawal 			if (xform->cipher.algo == RTE_CRYPTO_CIPHER_NULL)
2612f73d6928SHemant Agrawal 				ret = dpaa_sec_auth_init(dev, xform, session);
2613f73d6928SHemant Agrawal 			else if (xform->next->auth.algo == RTE_CRYPTO_AUTH_NULL)
2614f73d6928SHemant Agrawal 				ret = dpaa_sec_cipher_init(dev, xform, session);
2615f73d6928SHemant Agrawal 			else
2616f73d6928SHemant Agrawal 				ret = dpaa_sec_chain_init(dev, xform, session);
2617c3e85bdcSAkhil Goyal 		} else {
2618f163231eSHemant Agrawal 			DPAA_SEC_ERR("Not supported: Auth then Cipher");
2619c08ced9aSAkhil Goyal 			return -ENOTSUP;
2620c3e85bdcSAkhil Goyal 		}
2621c3e85bdcSAkhil Goyal 	/* Authenticate then Cipher */
2622c3e85bdcSAkhil Goyal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
2623c3e85bdcSAkhil Goyal 		   xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
2624c3e85bdcSAkhil Goyal 		if (xform->next->cipher.op == RTE_CRYPTO_CIPHER_OP_DECRYPT) {
26258524b44eSHemant Agrawal 			session->auth_cipher_text = 0;
2626f73d6928SHemant Agrawal 			if (xform->auth.algo == RTE_CRYPTO_AUTH_NULL)
2627f73d6928SHemant Agrawal 				ret = dpaa_sec_cipher_init(dev, xform, session);
2628f73d6928SHemant Agrawal 			else if (xform->next->cipher.algo
2629f73d6928SHemant Agrawal 					== RTE_CRYPTO_CIPHER_NULL)
2630f73d6928SHemant Agrawal 				ret = dpaa_sec_auth_init(dev, xform, session);
2631f73d6928SHemant Agrawal 			else
2632f73d6928SHemant Agrawal 				ret = dpaa_sec_chain_init(dev, xform, session);
2633c3e85bdcSAkhil Goyal 		} else {
2634f163231eSHemant Agrawal 			DPAA_SEC_ERR("Not supported: Auth then Cipher");
2635c08ced9aSAkhil Goyal 			return -ENOTSUP;
2636c3e85bdcSAkhil Goyal 		}
2637c3e85bdcSAkhil Goyal 
2638c3e85bdcSAkhil Goyal 	/* AEAD operation for AES-GCM kind of Algorithms */
2639c3e85bdcSAkhil Goyal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD &&
2640c3e85bdcSAkhil Goyal 		   xform->next == NULL) {
2641f73d6928SHemant Agrawal 		ret = dpaa_sec_aead_init(dev, xform, session);
2642c3e85bdcSAkhil Goyal 
2643c3e85bdcSAkhil Goyal 	} else {
2644f163231eSHemant Agrawal 		DPAA_SEC_ERR("Invalid crypto type");
2645c3e85bdcSAkhil Goyal 		return -EINVAL;
2646c3e85bdcSAkhil Goyal 	}
2647f73d6928SHemant Agrawal 	if (ret) {
2648f73d6928SHemant Agrawal 		DPAA_SEC_ERR("unable to init session");
2649f73d6928SHemant Agrawal 		goto err1;
2650f73d6928SHemant Agrawal 	}
2651f73d6928SHemant Agrawal 
26523b617ee7SAkhil Goyal 	rte_spinlock_lock(&internals->lock);
26534e694fe5SAkhil Goyal 	for (i = 0; i < MAX_DPAA_CORES; i++) {
26544e694fe5SAkhil Goyal 		session->inq[i] = dpaa_sec_attach_rxq(internals);
26554e694fe5SAkhil Goyal 		if (session->inq[i] == NULL) {
2656f163231eSHemant Agrawal 			DPAA_SEC_ERR("unable to attach sec queue");
26574e694fe5SAkhil Goyal 			rte_spinlock_unlock(&internals->lock);
2658c08ced9aSAkhil Goyal 			ret = -EBUSY;
2659e79416d1SHemant Agrawal 			goto err1;
2660e79416d1SHemant Agrawal 		}
26614e694fe5SAkhil Goyal 	}
26624e694fe5SAkhil Goyal 	rte_spinlock_unlock(&internals->lock);
2663c3e85bdcSAkhil Goyal 
2664c3e85bdcSAkhil Goyal 	return 0;
2665e79416d1SHemant Agrawal 
2666e79416d1SHemant Agrawal err1:
26676290de2cSLukasz Wojciechowski 	free_session_data(session);
2668c08ced9aSAkhil Goyal 	return ret;
2669c3e85bdcSAkhil Goyal }
2670c3e85bdcSAkhil Goyal 
2671c3e85bdcSAkhil Goyal static int
2672012c5076SPablo de Lara dpaa_sec_sym_session_configure(struct rte_cryptodev *dev,
2673c3e85bdcSAkhil Goyal 		struct rte_crypto_sym_xform *xform,
2674*bdce2564SAkhil Goyal 		struct rte_cryptodev_sym_session *sess)
2675c3e85bdcSAkhil Goyal {
2676*bdce2564SAkhil Goyal 	void *sess_private_data = (void *)sess->driver_priv_data;
2677c3e85bdcSAkhil Goyal 	int ret;
2678c3e85bdcSAkhil Goyal 
2679c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2680c3e85bdcSAkhil Goyal 
2681c3e85bdcSAkhil Goyal 	ret = dpaa_sec_set_session_parameters(dev, xform, sess_private_data);
2682c3e85bdcSAkhil Goyal 	if (ret != 0) {
2683f163231eSHemant Agrawal 		DPAA_SEC_ERR("failed to configure session parameters");
2684c3e85bdcSAkhil Goyal 		return ret;
2685c3e85bdcSAkhil Goyal 	}
2686c3e85bdcSAkhil Goyal 
268776da1b51SGagandeep Singh 	ret = dpaa_sec_prep_cdb(sess_private_data);
268876da1b51SGagandeep Singh 	if (ret) {
268976da1b51SGagandeep Singh 		DPAA_SEC_ERR("Unable to prepare sec cdb");
269076da1b51SGagandeep Singh 		return ret;
269176da1b51SGagandeep Singh 	}
2692e79416d1SHemant Agrawal 
2693c3e85bdcSAkhil Goyal 	return 0;
2694c3e85bdcSAkhil Goyal }
2695c3e85bdcSAkhil Goyal 
26963d0d5332SAkhil Goyal static inline void
26973d0d5332SAkhil Goyal free_session_memory(struct rte_cryptodev *dev, dpaa_sec_session *s)
2698c3e85bdcSAkhil Goyal {
2699e79416d1SHemant Agrawal 	struct dpaa_sec_dev_private *qi = dev->data->dev_private;
27003d0d5332SAkhil Goyal 	uint8_t i;
2701e79416d1SHemant Agrawal 
2702e621d970SAkhil Goyal 	for (i = 0; i < MAX_DPAA_CORES; i++) {
2703e621d970SAkhil Goyal 		if (s->inq[i])
2704e621d970SAkhil Goyal 			dpaa_sec_detach_rxq(qi, s->inq[i]);
2705e621d970SAkhil Goyal 		s->inq[i] = NULL;
2706e621d970SAkhil Goyal 		s->qp[i] = NULL;
2707e621d970SAkhil Goyal 	}
27086290de2cSLukasz Wojciechowski 	free_session_data(s);
27093d0d5332SAkhil Goyal }
27103d0d5332SAkhil Goyal 
27113d0d5332SAkhil Goyal /** Clear the memory of session so it doesn't leave key material behind */
27123d0d5332SAkhil Goyal static void
27133d0d5332SAkhil Goyal dpaa_sec_sym_session_clear(struct rte_cryptodev *dev,
27143d0d5332SAkhil Goyal 		struct rte_cryptodev_sym_session *sess)
27153d0d5332SAkhil Goyal {
27163d0d5332SAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2717*bdce2564SAkhil Goyal 	void *sess_priv = (void *)sess->driver_priv_data;
27183d0d5332SAkhil Goyal 	dpaa_sec_session *s = (dpaa_sec_session *)sess_priv;
27193d0d5332SAkhil Goyal 
27203d0d5332SAkhil Goyal 	free_session_memory(dev, s);
2721c3e85bdcSAkhil Goyal }
2722c3e85bdcSAkhil Goyal 
2723a8d0d473SBruce Richardson #ifdef RTE_LIB_SECURITY
2724c3e85bdcSAkhil Goyal static int
27252c318722SHemant Agrawal dpaa_sec_ipsec_aead_init(struct rte_crypto_aead_xform *aead_xform,
27262c318722SHemant Agrawal 			struct rte_security_ipsec_xform *ipsec_xform,
27272c318722SHemant Agrawal 			dpaa_sec_session *session)
27281f14d500SAkhil Goyal {
27291f14d500SAkhil Goyal 	PMD_INIT_FUNC_TRACE();
27301f14d500SAkhil Goyal 
27312c318722SHemant Agrawal 	session->aead_key.data = rte_zmalloc(NULL, aead_xform->key.length,
27322c318722SHemant Agrawal 					       RTE_CACHE_LINE_SIZE);
27332c318722SHemant Agrawal 	if (session->aead_key.data == NULL && aead_xform->key.length > 0) {
27342c318722SHemant Agrawal 		DPAA_SEC_ERR("No Memory for aead key");
2735c08ced9aSAkhil Goyal 		return -ENOMEM;
27361f14d500SAkhil Goyal 	}
27372c318722SHemant Agrawal 	memcpy(session->aead_key.data, aead_xform->key.data,
27382c318722SHemant Agrawal 	       aead_xform->key.length);
273905b12700SHemant Agrawal 
27402c318722SHemant Agrawal 	session->digest_length = aead_xform->digest_length;
27412c318722SHemant Agrawal 	session->aead_key.length = aead_xform->key.length;
27422c318722SHemant Agrawal 
27432c318722SHemant Agrawal 	switch (aead_xform->algo) {
27442c318722SHemant Agrawal 	case RTE_CRYPTO_AEAD_AES_GCM:
27452c318722SHemant Agrawal 		switch (session->digest_length) {
27462c318722SHemant Agrawal 		case 8:
27472c318722SHemant Agrawal 			session->aead_key.alg = OP_PCL_IPSEC_AES_GCM8;
27482c318722SHemant Agrawal 			break;
27492c318722SHemant Agrawal 		case 12:
27502c318722SHemant Agrawal 			session->aead_key.alg = OP_PCL_IPSEC_AES_GCM12;
27512c318722SHemant Agrawal 			break;
27522c318722SHemant Agrawal 		case 16:
27532c318722SHemant Agrawal 			session->aead_key.alg = OP_PCL_IPSEC_AES_GCM16;
27542c318722SHemant Agrawal 			break;
27552c318722SHemant Agrawal 		default:
27562c318722SHemant Agrawal 			DPAA_SEC_ERR("Crypto: Undefined GCM digest %d",
27572c318722SHemant Agrawal 				     session->digest_length);
2758c08ced9aSAkhil Goyal 			return -EINVAL;
27592c318722SHemant Agrawal 		}
27602c318722SHemant Agrawal 		if (session->dir == DIR_ENC) {
27612c318722SHemant Agrawal 			memcpy(session->encap_pdb.gcm.salt,
27622c318722SHemant Agrawal 				(uint8_t *)&(ipsec_xform->salt), 4);
27632c318722SHemant Agrawal 		} else {
27642c318722SHemant Agrawal 			memcpy(session->decap_pdb.gcm.salt,
27652c318722SHemant Agrawal 				(uint8_t *)&(ipsec_xform->salt), 4);
27662c318722SHemant Agrawal 		}
27672c318722SHemant Agrawal 		session->aead_key.algmode = OP_ALG_AAI_GCM;
27682c318722SHemant Agrawal 		session->aead_alg = RTE_CRYPTO_AEAD_AES_GCM;
27692c318722SHemant Agrawal 		break;
27702c318722SHemant Agrawal 	default:
27712c318722SHemant Agrawal 		DPAA_SEC_ERR("Crypto: Undefined AEAD specified %u",
27722c318722SHemant Agrawal 			      aead_xform->algo);
2773c08ced9aSAkhil Goyal 		return -ENOTSUP;
27742c318722SHemant Agrawal 	}
27752c318722SHemant Agrawal 	return 0;
27762c318722SHemant Agrawal }
27772c318722SHemant Agrawal 
27782c318722SHemant Agrawal static int
27792c318722SHemant Agrawal dpaa_sec_ipsec_proto_init(struct rte_crypto_cipher_xform *cipher_xform,
27802c318722SHemant Agrawal 	struct rte_crypto_auth_xform *auth_xform,
27811cdfbb0bSVakul Garg 	struct rte_security_ipsec_xform *ipsec_xform,
27822c318722SHemant Agrawal 	dpaa_sec_session *session)
27832c318722SHemant Agrawal {
27842c318722SHemant Agrawal 	if (cipher_xform) {
27851f14d500SAkhil Goyal 		session->cipher_key.data = rte_zmalloc(NULL,
27861f14d500SAkhil Goyal 						       cipher_xform->key.length,
27871f14d500SAkhil Goyal 						       RTE_CACHE_LINE_SIZE);
27881f14d500SAkhil Goyal 		if (session->cipher_key.data == NULL &&
27891f14d500SAkhil Goyal 				cipher_xform->key.length > 0) {
2790f163231eSHemant Agrawal 			DPAA_SEC_ERR("No Memory for cipher key");
27911f14d500SAkhil Goyal 			return -ENOMEM;
27921f14d500SAkhil Goyal 		}
27932c318722SHemant Agrawal 
27942c318722SHemant Agrawal 		session->cipher_key.length = cipher_xform->key.length;
279505b12700SHemant Agrawal 		memcpy(session->cipher_key.data, cipher_xform->key.data,
279605b12700SHemant Agrawal 				cipher_xform->key.length);
279705b12700SHemant Agrawal 		session->cipher_alg = cipher_xform->algo;
279805b12700SHemant Agrawal 	} else {
279905b12700SHemant Agrawal 		session->cipher_key.data = NULL;
280005b12700SHemant Agrawal 		session->cipher_key.length = 0;
280105b12700SHemant Agrawal 		session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
280205b12700SHemant Agrawal 	}
280305b12700SHemant Agrawal 
28042c318722SHemant Agrawal 	if (auth_xform) {
28051f14d500SAkhil Goyal 		session->auth_key.data = rte_zmalloc(NULL,
28061f14d500SAkhil Goyal 						auth_xform->key.length,
28071f14d500SAkhil Goyal 						RTE_CACHE_LINE_SIZE);
28081f14d500SAkhil Goyal 		if (session->auth_key.data == NULL &&
28091f14d500SAkhil Goyal 				auth_xform->key.length > 0) {
2810f163231eSHemant Agrawal 			DPAA_SEC_ERR("No Memory for auth key");
28111f14d500SAkhil Goyal 			return -ENOMEM;
28121f14d500SAkhil Goyal 		}
28132c318722SHemant Agrawal 		session->auth_key.length = auth_xform->key.length;
28141f14d500SAkhil Goyal 		memcpy(session->auth_key.data, auth_xform->key.data,
28151f14d500SAkhil Goyal 				auth_xform->key.length);
28162c318722SHemant Agrawal 		session->auth_alg = auth_xform->algo;
2817247b6908SHemant Agrawal 		session->digest_length = auth_xform->digest_length;
28182c318722SHemant Agrawal 	} else {
28192c318722SHemant Agrawal 		session->auth_key.data = NULL;
28202c318722SHemant Agrawal 		session->auth_key.length = 0;
28212c318722SHemant Agrawal 		session->auth_alg = RTE_CRYPTO_AUTH_NULL;
28222c318722SHemant Agrawal 	}
28231f14d500SAkhil Goyal 
28242c318722SHemant Agrawal 	switch (session->auth_alg) {
28258524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_SHA1_HMAC:
28268524b44eSHemant Agrawal 		session->auth_key.alg = OP_PCL_IPSEC_HMAC_SHA1_96;
28278524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
28288524b44eSHemant Agrawal 		break;
28292c318722SHemant Agrawal 	case RTE_CRYPTO_AUTH_MD5_HMAC:
28302c318722SHemant Agrawal 		session->auth_key.alg = OP_PCL_IPSEC_HMAC_MD5_96;
28318524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
28328524b44eSHemant Agrawal 		break;
28331f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA256_HMAC:
28348524b44eSHemant Agrawal 		session->auth_key.alg = OP_PCL_IPSEC_HMAC_SHA2_256_128;
28358524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
2836247b6908SHemant Agrawal 		if (session->digest_length != 16)
2837247b6908SHemant Agrawal 			DPAA_SEC_WARN(
2838247b6908SHemant Agrawal 			"+++Using sha256-hmac truncated len is non-standard,"
2839247b6908SHemant Agrawal 			"it will not work with lookaside proto");
28408524b44eSHemant Agrawal 		break;
28411f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA384_HMAC:
28428524b44eSHemant Agrawal 		session->auth_key.alg = OP_PCL_IPSEC_HMAC_SHA2_384_192;
28438524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
28448524b44eSHemant Agrawal 		break;
28451f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA512_HMAC:
28468524b44eSHemant Agrawal 		session->auth_key.alg = OP_PCL_IPSEC_HMAC_SHA2_512_256;
28478524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
28481f14d500SAkhil Goyal 		break;
28492c318722SHemant Agrawal 	case RTE_CRYPTO_AUTH_AES_CMAC:
28502c318722SHemant Agrawal 		session->auth_key.alg = OP_PCL_IPSEC_AES_CMAC_96;
28512ed12d9bSGagandeep Singh 		session->auth_key.algmode = OP_ALG_AAI_CMAC;
28522c318722SHemant Agrawal 		break;
28532c318722SHemant Agrawal 	case RTE_CRYPTO_AUTH_NULL:
28542c318722SHemant Agrawal 		session->auth_key.alg = OP_PCL_IPSEC_HMAC_NULL;
28552c318722SHemant Agrawal 		break;
28562c318722SHemant Agrawal 	case RTE_CRYPTO_AUTH_AES_XCBC_MAC:
285766f95673SGagandeep Singh 		session->auth_key.alg = OP_PCL_IPSEC_AES_XCBC_MAC_96;
285866f95673SGagandeep Singh 		session->auth_key.algmode = OP_ALG_AAI_XCBC_MAC;
285966f95673SGagandeep Singh 		break;
286066f95673SGagandeep Singh 	case RTE_CRYPTO_AUTH_SHA224_HMAC:
28612c318722SHemant Agrawal 	case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
28622c318722SHemant Agrawal 	case RTE_CRYPTO_AUTH_SHA1:
28632c318722SHemant Agrawal 	case RTE_CRYPTO_AUTH_SHA256:
28642c318722SHemant Agrawal 	case RTE_CRYPTO_AUTH_SHA512:
28652c318722SHemant Agrawal 	case RTE_CRYPTO_AUTH_SHA224:
28662c318722SHemant Agrawal 	case RTE_CRYPTO_AUTH_SHA384:
28672c318722SHemant Agrawal 	case RTE_CRYPTO_AUTH_MD5:
28682c318722SHemant Agrawal 	case RTE_CRYPTO_AUTH_AES_GMAC:
28692c318722SHemant Agrawal 	case RTE_CRYPTO_AUTH_KASUMI_F9:
28702c318722SHemant Agrawal 	case RTE_CRYPTO_AUTH_AES_CBC_MAC:
28712c318722SHemant Agrawal 	case RTE_CRYPTO_AUTH_ZUC_EIA3:
2872f163231eSHemant Agrawal 		DPAA_SEC_ERR("Crypto: Unsupported auth alg %u",
28732c318722SHemant Agrawal 			      session->auth_alg);
2874c08ced9aSAkhil Goyal 		return -ENOTSUP;
28752c318722SHemant Agrawal 	default:
28762c318722SHemant Agrawal 		DPAA_SEC_ERR("Crypto: Undefined Auth specified %u",
28772c318722SHemant Agrawal 			      session->auth_alg);
2878c08ced9aSAkhil Goyal 		return -ENOTSUP;
28792c318722SHemant Agrawal 	}
28802c318722SHemant Agrawal 
28812c318722SHemant Agrawal 	switch (session->cipher_alg) {
28822c318722SHemant Agrawal 	case RTE_CRYPTO_CIPHER_AES_CBC:
28832c318722SHemant Agrawal 		session->cipher_key.alg = OP_PCL_IPSEC_AES_CBC;
28842c318722SHemant Agrawal 		session->cipher_key.algmode = OP_ALG_AAI_CBC;
28852c318722SHemant Agrawal 		break;
28863e4fbc6cSGagandeep Singh 	case RTE_CRYPTO_CIPHER_DES_CBC:
28873e4fbc6cSGagandeep Singh 		session->cipher_key.alg = OP_PCL_IPSEC_DES;
28883e4fbc6cSGagandeep Singh 		session->cipher_key.algmode = OP_ALG_AAI_CBC;
28893e4fbc6cSGagandeep Singh 		break;
28902c318722SHemant Agrawal 	case RTE_CRYPTO_CIPHER_3DES_CBC:
28912c318722SHemant Agrawal 		session->cipher_key.alg = OP_PCL_IPSEC_3DES;
28922c318722SHemant Agrawal 		session->cipher_key.algmode = OP_ALG_AAI_CBC;
28932c318722SHemant Agrawal 		break;
28942c318722SHemant Agrawal 	case RTE_CRYPTO_CIPHER_AES_CTR:
28952c318722SHemant Agrawal 		session->cipher_key.alg = OP_PCL_IPSEC_AES_CTR;
28962c318722SHemant Agrawal 		session->cipher_key.algmode = OP_ALG_AAI_CTR;
28971cdfbb0bSVakul Garg 		if (session->dir == DIR_ENC) {
28981cdfbb0bSVakul Garg 			session->encap_pdb.ctr.ctr_initial = 0x00000001;
28991cdfbb0bSVakul Garg 			session->encap_pdb.ctr.ctr_nonce = ipsec_xform->salt;
29001cdfbb0bSVakul Garg 		} else {
29011cdfbb0bSVakul Garg 			session->decap_pdb.ctr.ctr_initial = 0x00000001;
29021cdfbb0bSVakul Garg 			session->decap_pdb.ctr.ctr_nonce = ipsec_xform->salt;
29031cdfbb0bSVakul Garg 		}
29042c318722SHemant Agrawal 		break;
29052c318722SHemant Agrawal 	case RTE_CRYPTO_CIPHER_NULL:
29062c318722SHemant Agrawal 		session->cipher_key.alg = OP_PCL_IPSEC_NULL;
29072c318722SHemant Agrawal 		break;
29082c318722SHemant Agrawal 	case RTE_CRYPTO_CIPHER_SNOW3G_UEA2:
29092c318722SHemant Agrawal 	case RTE_CRYPTO_CIPHER_ZUC_EEA3:
29102c318722SHemant Agrawal 	case RTE_CRYPTO_CIPHER_3DES_ECB:
29112c318722SHemant Agrawal 	case RTE_CRYPTO_CIPHER_AES_ECB:
29122c318722SHemant Agrawal 	case RTE_CRYPTO_CIPHER_KASUMI_F8:
29132c318722SHemant Agrawal 		DPAA_SEC_ERR("Crypto: Unsupported Cipher alg %u",
29142c318722SHemant Agrawal 			      session->cipher_alg);
2915c08ced9aSAkhil Goyal 		return -ENOTSUP;
29162c318722SHemant Agrawal 	default:
29172c318722SHemant Agrawal 		DPAA_SEC_ERR("Crypto: Undefined Cipher specified %u",
29182c318722SHemant Agrawal 			      session->cipher_alg);
2919c08ced9aSAkhil Goyal 		return -ENOTSUP;
29202c318722SHemant Agrawal 	}
29212c318722SHemant Agrawal 
29222c318722SHemant Agrawal 	return 0;
29232c318722SHemant Agrawal }
29242c318722SHemant Agrawal 
29252c318722SHemant Agrawal static int
29262c318722SHemant Agrawal dpaa_sec_set_ipsec_session(__rte_unused struct rte_cryptodev *dev,
29272c318722SHemant Agrawal 			   struct rte_security_session_conf *conf,
29282c318722SHemant Agrawal 			   void *sess)
29292c318722SHemant Agrawal {
29302c318722SHemant Agrawal 	struct dpaa_sec_dev_private *internals = dev->data->dev_private;
29312c318722SHemant Agrawal 	struct rte_security_ipsec_xform *ipsec_xform = &conf->ipsec;
29322c318722SHemant Agrawal 	struct rte_crypto_auth_xform *auth_xform = NULL;
29332c318722SHemant Agrawal 	struct rte_crypto_cipher_xform *cipher_xform = NULL;
29342c318722SHemant Agrawal 	struct rte_crypto_aead_xform *aead_xform = NULL;
29352c318722SHemant Agrawal 	dpaa_sec_session *session = (dpaa_sec_session *)sess;
29362c318722SHemant Agrawal 	uint32_t i;
29372c318722SHemant Agrawal 	int ret;
29382c318722SHemant Agrawal 
29392c318722SHemant Agrawal 	PMD_INIT_FUNC_TRACE();
29402c318722SHemant Agrawal 
29412c318722SHemant Agrawal 	memset(session, 0, sizeof(dpaa_sec_session));
29422c318722SHemant Agrawal 	session->proto_alg = conf->protocol;
29432c318722SHemant Agrawal 	session->ctxt = DPAA_SEC_IPSEC;
29442c318722SHemant Agrawal 
29458f4125c1SGagandeep Singh 	if (ipsec_xform->life.bytes_hard_limit != 0 ||
29468f4125c1SGagandeep Singh 	    ipsec_xform->life.bytes_soft_limit != 0 ||
29478f4125c1SGagandeep Singh 	    ipsec_xform->life.packets_hard_limit != 0 ||
29488f4125c1SGagandeep Singh 	    ipsec_xform->life.packets_soft_limit != 0)
29498f4125c1SGagandeep Singh 		return -ENOTSUP;
29508f4125c1SGagandeep Singh 
29512c318722SHemant Agrawal 	if (ipsec_xform->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS)
29522c318722SHemant Agrawal 		session->dir = DIR_ENC;
29532c318722SHemant Agrawal 	else
29542c318722SHemant Agrawal 		session->dir = DIR_DEC;
29552c318722SHemant Agrawal 
29562c318722SHemant Agrawal 	if (conf->crypto_xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
29572c318722SHemant Agrawal 		cipher_xform = &conf->crypto_xform->cipher;
29582c318722SHemant Agrawal 		if (conf->crypto_xform->next)
29592c318722SHemant Agrawal 			auth_xform = &conf->crypto_xform->next->auth;
29602c318722SHemant Agrawal 		ret = dpaa_sec_ipsec_proto_init(cipher_xform, auth_xform,
29611cdfbb0bSVakul Garg 					ipsec_xform, session);
29622c318722SHemant Agrawal 	} else if (conf->crypto_xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
29632c318722SHemant Agrawal 		auth_xform = &conf->crypto_xform->auth;
29642c318722SHemant Agrawal 		if (conf->crypto_xform->next)
29652c318722SHemant Agrawal 			cipher_xform = &conf->crypto_xform->next->cipher;
29662c318722SHemant Agrawal 		ret = dpaa_sec_ipsec_proto_init(cipher_xform, auth_xform,
29671cdfbb0bSVakul Garg 					ipsec_xform, session);
29682c318722SHemant Agrawal 	} else if (conf->crypto_xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
29692c318722SHemant Agrawal 		aead_xform = &conf->crypto_xform->aead;
29702c318722SHemant Agrawal 		ret = dpaa_sec_ipsec_aead_init(aead_xform,
29712c318722SHemant Agrawal 					ipsec_xform, session);
29722c318722SHemant Agrawal 	} else {
29732c318722SHemant Agrawal 		DPAA_SEC_ERR("XFORM not specified");
29742c318722SHemant Agrawal 		ret = -EINVAL;
29751f14d500SAkhil Goyal 		goto out;
29761f14d500SAkhil Goyal 	}
29772c318722SHemant Agrawal 	if (ret) {
29782c318722SHemant Agrawal 		DPAA_SEC_ERR("Failed to process xform");
29792c318722SHemant Agrawal 		goto out;
29801f14d500SAkhil Goyal 	}
29811f14d500SAkhil Goyal 
29821f14d500SAkhil Goyal 	if (ipsec_xform->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
29835ab35d2eSAkhil Goyal 		if (ipsec_xform->tunnel.type ==
29845ab35d2eSAkhil Goyal 				RTE_SECURITY_IPSEC_TUNNEL_IPV4) {
29851f14d500SAkhil Goyal 			session->ip4_hdr.ip_v = IPVERSION;
29861f14d500SAkhil Goyal 			session->ip4_hdr.ip_hl = 5;
29871f14d500SAkhil Goyal 			session->ip4_hdr.ip_len = rte_cpu_to_be_16(
29881f14d500SAkhil Goyal 						sizeof(session->ip4_hdr));
29891f14d500SAkhil Goyal 			session->ip4_hdr.ip_tos = ipsec_xform->tunnel.ipv4.dscp;
29901f14d500SAkhil Goyal 			session->ip4_hdr.ip_id = 0;
29911f14d500SAkhil Goyal 			session->ip4_hdr.ip_off = 0;
29921f14d500SAkhil Goyal 			session->ip4_hdr.ip_ttl = ipsec_xform->tunnel.ipv4.ttl;
29931f14d500SAkhil Goyal 			session->ip4_hdr.ip_p = (ipsec_xform->proto ==
29945ab35d2eSAkhil Goyal 					RTE_SECURITY_IPSEC_SA_PROTO_ESP) ?
29955ab35d2eSAkhil Goyal 					IPPROTO_ESP : IPPROTO_AH;
29961f14d500SAkhil Goyal 			session->ip4_hdr.ip_sum = 0;
29975ab35d2eSAkhil Goyal 			session->ip4_hdr.ip_src =
29985ab35d2eSAkhil Goyal 					ipsec_xform->tunnel.ipv4.src_ip;
29995ab35d2eSAkhil Goyal 			session->ip4_hdr.ip_dst =
30005ab35d2eSAkhil Goyal 					ipsec_xform->tunnel.ipv4.dst_ip;
30011f14d500SAkhil Goyal 			session->ip4_hdr.ip_sum = calc_chksum((uint16_t *)
30021f14d500SAkhil Goyal 						(void *)&session->ip4_hdr,
30031f14d500SAkhil Goyal 						sizeof(struct ip));
30045ab35d2eSAkhil Goyal 			session->encap_pdb.ip_hdr_len = sizeof(struct ip);
30055ab35d2eSAkhil Goyal 		} else if (ipsec_xform->tunnel.type ==
30065ab35d2eSAkhil Goyal 				RTE_SECURITY_IPSEC_TUNNEL_IPV6) {
30075ab35d2eSAkhil Goyal 			session->ip6_hdr.vtc_flow = rte_cpu_to_be_32(
30085ab35d2eSAkhil Goyal 				DPAA_IPv6_DEFAULT_VTC_FLOW |
30095ab35d2eSAkhil Goyal 				((ipsec_xform->tunnel.ipv6.dscp <<
30105ab35d2eSAkhil Goyal 					RTE_IPV6_HDR_TC_SHIFT) &
30115ab35d2eSAkhil Goyal 					RTE_IPV6_HDR_TC_MASK) |
30125ab35d2eSAkhil Goyal 				((ipsec_xform->tunnel.ipv6.flabel <<
30135ab35d2eSAkhil Goyal 					RTE_IPV6_HDR_FL_SHIFT) &
30145ab35d2eSAkhil Goyal 					RTE_IPV6_HDR_FL_MASK));
30155ab35d2eSAkhil Goyal 			/* Payload length will be updated by HW */
30165ab35d2eSAkhil Goyal 			session->ip6_hdr.payload_len = 0;
30175ab35d2eSAkhil Goyal 			session->ip6_hdr.hop_limits =
30185ab35d2eSAkhil Goyal 					ipsec_xform->tunnel.ipv6.hlimit;
30195ab35d2eSAkhil Goyal 			session->ip6_hdr.proto = (ipsec_xform->proto ==
30205ab35d2eSAkhil Goyal 					RTE_SECURITY_IPSEC_SA_PROTO_ESP) ?
30215ab35d2eSAkhil Goyal 					IPPROTO_ESP : IPPROTO_AH;
30225ab35d2eSAkhil Goyal 			memcpy(&session->ip6_hdr.src_addr,
30235ab35d2eSAkhil Goyal 					&ipsec_xform->tunnel.ipv6.src_addr, 16);
30245ab35d2eSAkhil Goyal 			memcpy(&session->ip6_hdr.dst_addr,
30255ab35d2eSAkhil Goyal 					&ipsec_xform->tunnel.ipv6.dst_addr, 16);
30265ab35d2eSAkhil Goyal 			session->encap_pdb.ip_hdr_len =
30275ab35d2eSAkhil Goyal 						sizeof(struct rte_ipv6_hdr);
30285ab35d2eSAkhil Goyal 		}
30290aa5986cSGagandeep Singh 
30301f14d500SAkhil Goyal 		session->encap_pdb.options =
30311f14d500SAkhil Goyal 			(IPVERSION << PDBNH_ESP_ENCAP_SHIFT) |
30321f14d500SAkhil Goyal 			PDBOPTS_ESP_OIHI_PDB_INL |
30331f14d500SAkhil Goyal 			PDBOPTS_ESP_IVSRC |
303479fde9d0SAkhil Goyal 			PDBHMO_ESP_SNR;
30350aa5986cSGagandeep Singh 		if (ipsec_xform->options.dec_ttl)
30360aa5986cSGagandeep Singh 			session->encap_pdb.options |= PDBHMO_ESP_ENCAP_DTTL;
30370f318781SAkhil Goyal 		if (ipsec_xform->options.esn)
30380f318781SAkhil Goyal 			session->encap_pdb.options |= PDBOPTS_ESP_ESN;
30391f14d500SAkhil Goyal 		session->encap_pdb.spi = ipsec_xform->spi;
30402c318722SHemant Agrawal 
30411f14d500SAkhil Goyal 	} else if (ipsec_xform->direction ==
30421f14d500SAkhil Goyal 			RTE_SECURITY_IPSEC_SA_DIR_INGRESS) {
30435ab35d2eSAkhil Goyal 		if (ipsec_xform->tunnel.type == RTE_SECURITY_IPSEC_TUNNEL_IPV4)
30441f14d500SAkhil Goyal 			session->decap_pdb.options = sizeof(struct ip) << 16;
30455ab35d2eSAkhil Goyal 		else
30465ab35d2eSAkhil Goyal 			session->decap_pdb.options =
30475ab35d2eSAkhil Goyal 					sizeof(struct rte_ipv6_hdr) << 16;
30480f318781SAkhil Goyal 		if (ipsec_xform->options.esn)
30490f318781SAkhil Goyal 			session->decap_pdb.options |= PDBOPTS_ESP_ESN;
3050a37ce227SHemant Agrawal 		if (ipsec_xform->replay_win_sz) {
3051a37ce227SHemant Agrawal 			uint32_t win_sz;
3052a37ce227SHemant Agrawal 			win_sz = rte_align32pow2(ipsec_xform->replay_win_sz);
3053a37ce227SHemant Agrawal 
3054a37ce227SHemant Agrawal 			switch (win_sz) {
3055a37ce227SHemant Agrawal 			case 1:
3056a37ce227SHemant Agrawal 			case 2:
3057a37ce227SHemant Agrawal 			case 4:
3058a37ce227SHemant Agrawal 			case 8:
3059a37ce227SHemant Agrawal 			case 16:
3060a37ce227SHemant Agrawal 			case 32:
3061a37ce227SHemant Agrawal 				session->decap_pdb.options |= PDBOPTS_ESP_ARS32;
3062a37ce227SHemant Agrawal 				break;
3063a37ce227SHemant Agrawal 			case 64:
3064a37ce227SHemant Agrawal 				session->decap_pdb.options |= PDBOPTS_ESP_ARS64;
3065a37ce227SHemant Agrawal 				break;
3066a37ce227SHemant Agrawal 			default:
3067a37ce227SHemant Agrawal 				session->decap_pdb.options |=
3068a37ce227SHemant Agrawal 							PDBOPTS_ESP_ARS128;
3069a37ce227SHemant Agrawal 			}
3070a37ce227SHemant Agrawal 		}
30711f14d500SAkhil Goyal 	} else
30721f14d500SAkhil Goyal 		goto out;
30733b617ee7SAkhil Goyal 	rte_spinlock_lock(&internals->lock);
30744e694fe5SAkhil Goyal 	for (i = 0; i < MAX_DPAA_CORES; i++) {
30754e694fe5SAkhil Goyal 		session->inq[i] = dpaa_sec_attach_rxq(internals);
30764e694fe5SAkhil Goyal 		if (session->inq[i] == NULL) {
3077f163231eSHemant Agrawal 			DPAA_SEC_ERR("unable to attach sec queue");
30784e694fe5SAkhil Goyal 			rte_spinlock_unlock(&internals->lock);
30791f14d500SAkhil Goyal 			goto out;
30801f14d500SAkhil Goyal 		}
30814e694fe5SAkhil Goyal 	}
30824e694fe5SAkhil Goyal 	rte_spinlock_unlock(&internals->lock);
30831f14d500SAkhil Goyal 
3084a1173d55SHemant Agrawal 	return 0;
3085a1173d55SHemant Agrawal out:
30866290de2cSLukasz Wojciechowski 	free_session_data(session);
3087a1173d55SHemant Agrawal 	return -1;
3088a1173d55SHemant Agrawal }
30891f14d500SAkhil Goyal 
3090a1173d55SHemant Agrawal static int
3091a1173d55SHemant Agrawal dpaa_sec_set_pdcp_session(struct rte_cryptodev *dev,
3092a1173d55SHemant Agrawal 			  struct rte_security_session_conf *conf,
3093a1173d55SHemant Agrawal 			  void *sess)
3094a1173d55SHemant Agrawal {
3095a1173d55SHemant Agrawal 	struct rte_security_pdcp_xform *pdcp_xform = &conf->pdcp;
3096a1173d55SHemant Agrawal 	struct rte_crypto_sym_xform *xform = conf->crypto_xform;
3097a1173d55SHemant Agrawal 	struct rte_crypto_auth_xform *auth_xform = NULL;
3098a1173d55SHemant Agrawal 	struct rte_crypto_cipher_xform *cipher_xform = NULL;
3099a1173d55SHemant Agrawal 	dpaa_sec_session *session = (dpaa_sec_session *)sess;
3100a1173d55SHemant Agrawal 	struct dpaa_sec_dev_private *dev_priv = dev->data->dev_private;
31014e694fe5SAkhil Goyal 	uint32_t i;
3102c08ced9aSAkhil Goyal 	int ret;
3103a1173d55SHemant Agrawal 
3104a1173d55SHemant Agrawal 	PMD_INIT_FUNC_TRACE();
3105a1173d55SHemant Agrawal 
3106a1173d55SHemant Agrawal 	memset(session, 0, sizeof(dpaa_sec_session));
3107a1173d55SHemant Agrawal 
3108a1173d55SHemant Agrawal 	/* find xfrm types */
3109a1173d55SHemant Agrawal 	if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
3110a1173d55SHemant Agrawal 		cipher_xform = &xform->cipher;
311199cc26f6SHemant Agrawal 		if (xform->next != NULL &&
311299cc26f6SHemant Agrawal 			xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH)
3113a1173d55SHemant Agrawal 			auth_xform = &xform->next->auth;
3114a1173d55SHemant Agrawal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
3115a1173d55SHemant Agrawal 		auth_xform = &xform->auth;
311699cc26f6SHemant Agrawal 		if (xform->next != NULL &&
311799cc26f6SHemant Agrawal 			xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER)
3118a1173d55SHemant Agrawal 			cipher_xform = &xform->next->cipher;
3119a1173d55SHemant Agrawal 	} else {
3120a1173d55SHemant Agrawal 		DPAA_SEC_ERR("Invalid crypto type");
3121a1173d55SHemant Agrawal 		return -EINVAL;
3122a1173d55SHemant Agrawal 	}
3123a1173d55SHemant Agrawal 
3124a1173d55SHemant Agrawal 	session->proto_alg = conf->protocol;
31258524b44eSHemant Agrawal 	session->ctxt = DPAA_SEC_PDCP;
31268524b44eSHemant Agrawal 
3127a1173d55SHemant Agrawal 	if (cipher_xform) {
31288524b44eSHemant Agrawal 		switch (cipher_xform->algo) {
31298524b44eSHemant Agrawal 		case RTE_CRYPTO_CIPHER_SNOW3G_UEA2:
31308524b44eSHemant Agrawal 			session->cipher_key.alg = PDCP_CIPHER_TYPE_SNOW;
31318524b44eSHemant Agrawal 			break;
31328524b44eSHemant Agrawal 		case RTE_CRYPTO_CIPHER_ZUC_EEA3:
31338524b44eSHemant Agrawal 			session->cipher_key.alg = PDCP_CIPHER_TYPE_ZUC;
31348524b44eSHemant Agrawal 			break;
31358524b44eSHemant Agrawal 		case RTE_CRYPTO_CIPHER_AES_CTR:
31368524b44eSHemant Agrawal 			session->cipher_key.alg = PDCP_CIPHER_TYPE_AES;
31378524b44eSHemant Agrawal 			break;
31388524b44eSHemant Agrawal 		case RTE_CRYPTO_CIPHER_NULL:
31398524b44eSHemant Agrawal 			session->cipher_key.alg = PDCP_CIPHER_TYPE_NULL;
31408524b44eSHemant Agrawal 			break;
31418524b44eSHemant Agrawal 		default:
31428524b44eSHemant Agrawal 			DPAA_SEC_ERR("Crypto: Undefined Cipher specified %u",
31438524b44eSHemant Agrawal 				      session->cipher_alg);
3144c08ced9aSAkhil Goyal 			return -EINVAL;
31458524b44eSHemant Agrawal 		}
31468524b44eSHemant Agrawal 
3147a1173d55SHemant Agrawal 		session->cipher_key.data = rte_zmalloc(NULL,
3148a1173d55SHemant Agrawal 					       cipher_xform->key.length,
3149a1173d55SHemant Agrawal 					       RTE_CACHE_LINE_SIZE);
3150a1173d55SHemant Agrawal 		if (session->cipher_key.data == NULL &&
3151a1173d55SHemant Agrawal 				cipher_xform->key.length > 0) {
3152a1173d55SHemant Agrawal 			DPAA_SEC_ERR("No Memory for cipher key");
3153a1173d55SHemant Agrawal 			return -ENOMEM;
3154a1173d55SHemant Agrawal 		}
3155a1173d55SHemant Agrawal 		session->cipher_key.length = cipher_xform->key.length;
3156a1173d55SHemant Agrawal 		memcpy(session->cipher_key.data, cipher_xform->key.data,
3157a1173d55SHemant Agrawal 			cipher_xform->key.length);
3158a1173d55SHemant Agrawal 		session->dir = (cipher_xform->op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
3159a1173d55SHemant Agrawal 					DIR_ENC : DIR_DEC;
3160a1173d55SHemant Agrawal 		session->cipher_alg = cipher_xform->algo;
3161a1173d55SHemant Agrawal 	} else {
3162a1173d55SHemant Agrawal 		session->cipher_key.data = NULL;
3163a1173d55SHemant Agrawal 		session->cipher_key.length = 0;
3164a1173d55SHemant Agrawal 		session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
3165a1173d55SHemant Agrawal 		session->dir = DIR_ENC;
3166a1173d55SHemant Agrawal 	}
3167a1173d55SHemant Agrawal 
3168a1173d55SHemant Agrawal 	if (pdcp_xform->domain == RTE_SECURITY_PDCP_MODE_CONTROL) {
3169eac60082SVakul Garg 		if (pdcp_xform->sn_size != RTE_SECURITY_PDCP_SN_SIZE_5 &&
3170eac60082SVakul Garg 		    pdcp_xform->sn_size != RTE_SECURITY_PDCP_SN_SIZE_12) {
3171a1173d55SHemant Agrawal 			DPAA_SEC_ERR(
3172eac60082SVakul Garg 				"PDCP Seq Num size should be 5/12 bits for cmode");
3173c08ced9aSAkhil Goyal 			ret = -EINVAL;
3174a1173d55SHemant Agrawal 			goto out;
3175a1173d55SHemant Agrawal 		}
31762e4cbdb4SVakul Garg 	}
31772e4cbdb4SVakul Garg 
3178a1173d55SHemant Agrawal 	if (auth_xform) {
31798524b44eSHemant Agrawal 		switch (auth_xform->algo) {
31808524b44eSHemant Agrawal 		case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
31818524b44eSHemant Agrawal 			session->auth_key.alg = PDCP_AUTH_TYPE_SNOW;
31828524b44eSHemant Agrawal 			break;
31838524b44eSHemant Agrawal 		case RTE_CRYPTO_AUTH_ZUC_EIA3:
31848524b44eSHemant Agrawal 			session->auth_key.alg = PDCP_AUTH_TYPE_ZUC;
31858524b44eSHemant Agrawal 			break;
31868524b44eSHemant Agrawal 		case RTE_CRYPTO_AUTH_AES_CMAC:
31878524b44eSHemant Agrawal 			session->auth_key.alg = PDCP_AUTH_TYPE_AES;
31888524b44eSHemant Agrawal 			break;
31898524b44eSHemant Agrawal 		case RTE_CRYPTO_AUTH_NULL:
31908524b44eSHemant Agrawal 			session->auth_key.alg = PDCP_AUTH_TYPE_NULL;
31918524b44eSHemant Agrawal 			break;
31928524b44eSHemant Agrawal 		default:
31938524b44eSHemant Agrawal 			DPAA_SEC_ERR("Crypto: Unsupported auth alg %u",
31948524b44eSHemant Agrawal 				      session->auth_alg);
31958524b44eSHemant Agrawal 			rte_free(session->cipher_key.data);
3196c08ced9aSAkhil Goyal 			return -EINVAL;
31978524b44eSHemant Agrawal 		}
3198a1173d55SHemant Agrawal 		session->auth_key.data = rte_zmalloc(NULL,
3199a1173d55SHemant Agrawal 						     auth_xform->key.length,
3200a1173d55SHemant Agrawal 						     RTE_CACHE_LINE_SIZE);
32012e4cbdb4SVakul Garg 		if (!session->auth_key.data &&
3202a1173d55SHemant Agrawal 		    auth_xform->key.length > 0) {
3203a1173d55SHemant Agrawal 			DPAA_SEC_ERR("No Memory for auth key");
3204a1173d55SHemant Agrawal 			rte_free(session->cipher_key.data);
3205a1173d55SHemant Agrawal 			return -ENOMEM;
3206a1173d55SHemant Agrawal 		}
3207a1173d55SHemant Agrawal 		session->auth_key.length = auth_xform->key.length;
3208a1173d55SHemant Agrawal 		memcpy(session->auth_key.data, auth_xform->key.data,
3209a1173d55SHemant Agrawal 		       auth_xform->key.length);
3210a1173d55SHemant Agrawal 		session->auth_alg = auth_xform->algo;
3211a1173d55SHemant Agrawal 	} else {
3212a1173d55SHemant Agrawal 		session->auth_key.data = NULL;
3213a1173d55SHemant Agrawal 		session->auth_key.length = 0;
32142e4cbdb4SVakul Garg 		session->auth_alg = 0;
3215a1173d55SHemant Agrawal 	}
3216a1173d55SHemant Agrawal 	session->pdcp.domain = pdcp_xform->domain;
3217a1173d55SHemant Agrawal 	session->pdcp.bearer = pdcp_xform->bearer;
3218a1173d55SHemant Agrawal 	session->pdcp.pkt_dir = pdcp_xform->pkt_dir;
3219a1173d55SHemant Agrawal 	session->pdcp.sn_size = pdcp_xform->sn_size;
3220a1173d55SHemant Agrawal 	session->pdcp.hfn = pdcp_xform->hfn;
3221a1173d55SHemant Agrawal 	session->pdcp.hfn_threshold = pdcp_xform->hfn_threshold;
32226a0c9d36SAkhil Goyal 	session->pdcp.hfn_ovd = pdcp_xform->hfn_ovrd;
32235a4954bcSAkhil Goyal 	session->pdcp.sdap_enabled = pdcp_xform->sdap_enabled;
32248839c8a1SYunjian Wang 	if (cipher_xform)
32256a0c9d36SAkhil Goyal 		session->pdcp.hfn_ovd_offset = cipher_xform->iv.offset;
3226a1173d55SHemant Agrawal 
3227a1173d55SHemant Agrawal 	rte_spinlock_lock(&dev_priv->lock);
32284e694fe5SAkhil Goyal 	for (i = 0; i < MAX_DPAA_CORES; i++) {
32294e694fe5SAkhil Goyal 		session->inq[i] = dpaa_sec_attach_rxq(dev_priv);
32304e694fe5SAkhil Goyal 		if (session->inq[i] == NULL) {
3231a1173d55SHemant Agrawal 			DPAA_SEC_ERR("unable to attach sec queue");
32324e694fe5SAkhil Goyal 			rte_spinlock_unlock(&dev_priv->lock);
3233c08ced9aSAkhil Goyal 			ret = -EBUSY;
3234a1173d55SHemant Agrawal 			goto out;
3235a1173d55SHemant Agrawal 		}
32364e694fe5SAkhil Goyal 	}
32374e694fe5SAkhil Goyal 	rte_spinlock_unlock(&dev_priv->lock);
32381f14d500SAkhil Goyal 	return 0;
32391f14d500SAkhil Goyal out:
32401f14d500SAkhil Goyal 	rte_free(session->auth_key.data);
32411f14d500SAkhil Goyal 	rte_free(session->cipher_key.data);
32421f14d500SAkhil Goyal 	memset(session, 0, sizeof(dpaa_sec_session));
3243c08ced9aSAkhil Goyal 	return ret;
32441f14d500SAkhil Goyal }
32451f14d500SAkhil Goyal 
32461f14d500SAkhil Goyal static int
32471f14d500SAkhil Goyal dpaa_sec_security_session_create(void *dev,
32481f14d500SAkhil Goyal 				 struct rte_security_session_conf *conf,
32491f14d500SAkhil Goyal 				 struct rte_security_session *sess,
32501f14d500SAkhil Goyal 				 struct rte_mempool *mempool)
32511f14d500SAkhil Goyal {
32521f14d500SAkhil Goyal 	void *sess_private_data;
32531f14d500SAkhil Goyal 	struct rte_cryptodev *cdev = (struct rte_cryptodev *)dev;
32541f14d500SAkhil Goyal 	int ret;
32551f14d500SAkhil Goyal 
32561f14d500SAkhil Goyal 	if (rte_mempool_get(mempool, &sess_private_data)) {
3257f163231eSHemant Agrawal 		DPAA_SEC_ERR("Couldn't get object from session mempool");
32581f14d500SAkhil Goyal 		return -ENOMEM;
32591f14d500SAkhil Goyal 	}
32601f14d500SAkhil Goyal 
32611f14d500SAkhil Goyal 	switch (conf->protocol) {
32621f14d500SAkhil Goyal 	case RTE_SECURITY_PROTOCOL_IPSEC:
32631f14d500SAkhil Goyal 		ret = dpaa_sec_set_ipsec_session(cdev, conf,
32641f14d500SAkhil Goyal 				sess_private_data);
32651f14d500SAkhil Goyal 		break;
3266a1173d55SHemant Agrawal 	case RTE_SECURITY_PROTOCOL_PDCP:
3267a1173d55SHemant Agrawal 		ret = dpaa_sec_set_pdcp_session(cdev, conf,
3268a1173d55SHemant Agrawal 				sess_private_data);
3269a1173d55SHemant Agrawal 		break;
32701f14d500SAkhil Goyal 	case RTE_SECURITY_PROTOCOL_MACSEC:
32711f14d500SAkhil Goyal 		return -ENOTSUP;
32721f14d500SAkhil Goyal 	default:
32731f14d500SAkhil Goyal 		return -EINVAL;
32741f14d500SAkhil Goyal 	}
32751f14d500SAkhil Goyal 	if (ret != 0) {
3276f163231eSHemant Agrawal 		DPAA_SEC_ERR("failed to configure session parameters");
32771f14d500SAkhil Goyal 		/* Return session to mempool */
32781f14d500SAkhil Goyal 		rte_mempool_put(mempool, sess_private_data);
32791f14d500SAkhil Goyal 		return ret;
32801f14d500SAkhil Goyal 	}
32811f14d500SAkhil Goyal 
32821f14d500SAkhil Goyal 	set_sec_session_private_data(sess, sess_private_data);
32831f14d500SAkhil Goyal 
328476da1b51SGagandeep Singh 	ret = dpaa_sec_prep_cdb(sess_private_data);
328576da1b51SGagandeep Singh 	if (ret) {
328676da1b51SGagandeep Singh 		DPAA_SEC_ERR("Unable to prepare sec cdb");
328776da1b51SGagandeep Singh 		return ret;
328876da1b51SGagandeep Singh 	}
328976da1b51SGagandeep Singh 
32901f14d500SAkhil Goyal 	return ret;
32911f14d500SAkhil Goyal }
32921f14d500SAkhil Goyal 
32931f14d500SAkhil Goyal /** Clear the memory of session so it doesn't leave key material behind */
32941f14d500SAkhil Goyal static int
32951f14d500SAkhil Goyal dpaa_sec_security_session_destroy(void *dev __rte_unused,
32961f14d500SAkhil Goyal 		struct rte_security_session *sess)
32971f14d500SAkhil Goyal {
32981f14d500SAkhil Goyal 	PMD_INIT_FUNC_TRACE();
32991f14d500SAkhil Goyal 	void *sess_priv = get_sec_session_private_data(sess);
33001f14d500SAkhil Goyal 	dpaa_sec_session *s = (dpaa_sec_session *)sess_priv;
33011f14d500SAkhil Goyal 
33021f14d500SAkhil Goyal 	if (sess_priv) {
33033d0d5332SAkhil Goyal 		free_session_memory((struct rte_cryptodev *)dev, s);
33041f14d500SAkhil Goyal 		set_sec_session_private_data(sess, NULL);
33051f14d500SAkhil Goyal 	}
33061f14d500SAkhil Goyal 	return 0;
33071f14d500SAkhil Goyal }
3308314424b6SHemant Agrawal #endif
33091f14d500SAkhil Goyal static int
33102ffb940eSAkhil Goyal dpaa_sec_dev_configure(struct rte_cryptodev *dev __rte_unused,
3311c3e85bdcSAkhil Goyal 		       struct rte_cryptodev_config *config __rte_unused)
3312c3e85bdcSAkhil Goyal {
3313c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
3314c3e85bdcSAkhil Goyal 
3315c3e85bdcSAkhil Goyal 	return 0;
3316c3e85bdcSAkhil Goyal }
3317c3e85bdcSAkhil Goyal 
3318c3e85bdcSAkhil Goyal static int
3319c3e85bdcSAkhil Goyal dpaa_sec_dev_start(struct rte_cryptodev *dev __rte_unused)
3320c3e85bdcSAkhil Goyal {
3321c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
3322c3e85bdcSAkhil Goyal 	return 0;
3323c3e85bdcSAkhil Goyal }
3324c3e85bdcSAkhil Goyal 
3325c3e85bdcSAkhil Goyal static void
3326c3e85bdcSAkhil Goyal dpaa_sec_dev_stop(struct rte_cryptodev *dev __rte_unused)
3327c3e85bdcSAkhil Goyal {
3328c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
3329c3e85bdcSAkhil Goyal }
3330c3e85bdcSAkhil Goyal 
3331c3e85bdcSAkhil Goyal static int
33327e3e2954SAkhil Goyal dpaa_sec_dev_close(struct rte_cryptodev *dev)
3333c3e85bdcSAkhil Goyal {
3334c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
33357e3e2954SAkhil Goyal 
33367e3e2954SAkhil Goyal 	if (dev == NULL)
33377e3e2954SAkhil Goyal 		return -ENOMEM;
33387e3e2954SAkhil Goyal 
3339c3e85bdcSAkhil Goyal 	return 0;
3340c3e85bdcSAkhil Goyal }
3341c3e85bdcSAkhil Goyal 
3342c3e85bdcSAkhil Goyal static void
3343c3e85bdcSAkhil Goyal dpaa_sec_dev_infos_get(struct rte_cryptodev *dev,
3344c3e85bdcSAkhil Goyal 		       struct rte_cryptodev_info *info)
3345c3e85bdcSAkhil Goyal {
3346c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals = dev->data->dev_private;
3347c3e85bdcSAkhil Goyal 
3348c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
3349c3e85bdcSAkhil Goyal 	if (info != NULL) {
3350c3e85bdcSAkhil Goyal 		info->max_nb_queue_pairs = internals->max_nb_queue_pairs;
3351c3e85bdcSAkhil Goyal 		info->feature_flags = dev->feature_flags;
3352c3e85bdcSAkhil Goyal 		info->capabilities = dpaa_sec_capabilities;
3353c3e85bdcSAkhil Goyal 		info->sym.max_nb_sessions = internals->max_nb_sessions;
33549d5f73c2SGagandeep Singh 		info->driver_id = dpaa_cryptodev_driver_id;
3355c3e85bdcSAkhil Goyal 	}
3356c3e85bdcSAkhil Goyal }
3357c3e85bdcSAkhil Goyal 
3358fe3688baSAkhil Goyal static enum qman_cb_dqrr_result
3359fe3688baSAkhil Goyal dpaa_sec_process_parallel_event(void *event,
3360fe3688baSAkhil Goyal 			struct qman_portal *qm __always_unused,
3361fe3688baSAkhil Goyal 			struct qman_fq *outq,
3362fe3688baSAkhil Goyal 			const struct qm_dqrr_entry *dqrr,
3363fe3688baSAkhil Goyal 			void **bufs)
3364fe3688baSAkhil Goyal {
3365fe3688baSAkhil Goyal 	const struct qm_fd *fd;
3366fe3688baSAkhil Goyal 	struct dpaa_sec_job *job;
3367fe3688baSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
3368fe3688baSAkhil Goyal 	struct rte_event *ev = (struct rte_event *)event;
3369fe3688baSAkhil Goyal 
3370fe3688baSAkhil Goyal 	fd = &dqrr->fd;
3371fe3688baSAkhil Goyal 
3372fe3688baSAkhil Goyal 	/* sg is embedded in an op ctx,
3373fe3688baSAkhil Goyal 	 * sg[0] is for output
3374fe3688baSAkhil Goyal 	 * sg[1] for input
3375fe3688baSAkhil Goyal 	 */
3376ec861560SGagandeep Singh 	job = rte_dpaa_mem_ptov(qm_fd_addr_get64(fd));
3377fe3688baSAkhil Goyal 
3378fe3688baSAkhil Goyal 	ctx = container_of(job, struct dpaa_sec_op_ctx, job);
3379fe3688baSAkhil Goyal 	ctx->fd_status = fd->status;
3380fe3688baSAkhil Goyal 	if (ctx->op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
3381fe3688baSAkhil Goyal 		struct qm_sg_entry *sg_out;
3382fe3688baSAkhil Goyal 		uint32_t len;
3383fe3688baSAkhil Goyal 
3384fe3688baSAkhil Goyal 		sg_out = &job->sg[0];
3385fe3688baSAkhil Goyal 		hw_sg_to_cpu(sg_out);
3386fe3688baSAkhil Goyal 		len = sg_out->length;
3387fe3688baSAkhil Goyal 		ctx->op->sym->m_src->pkt_len = len;
3388fe3688baSAkhil Goyal 		ctx->op->sym->m_src->data_len = len;
3389fe3688baSAkhil Goyal 	}
3390fe3688baSAkhil Goyal 	if (!ctx->fd_status) {
3391fe3688baSAkhil Goyal 		ctx->op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
3392fe3688baSAkhil Goyal 	} else {
3393fe3688baSAkhil Goyal 		DPAA_SEC_DP_WARN("SEC return err: 0x%x", ctx->fd_status);
3394fe3688baSAkhil Goyal 		ctx->op->status = RTE_CRYPTO_OP_STATUS_ERROR;
3395fe3688baSAkhil Goyal 	}
3396fe3688baSAkhil Goyal 	ev->event_ptr = (void *)ctx->op;
3397fe3688baSAkhil Goyal 
3398fe3688baSAkhil Goyal 	ev->flow_id = outq->ev.flow_id;
3399fe3688baSAkhil Goyal 	ev->sub_event_type = outq->ev.sub_event_type;
3400fe3688baSAkhil Goyal 	ev->event_type = RTE_EVENT_TYPE_CRYPTODEV;
3401fe3688baSAkhil Goyal 	ev->op = RTE_EVENT_OP_NEW;
3402fe3688baSAkhil Goyal 	ev->sched_type = outq->ev.sched_type;
3403fe3688baSAkhil Goyal 	ev->queue_id = outq->ev.queue_id;
3404fe3688baSAkhil Goyal 	ev->priority = outq->ev.priority;
3405fe3688baSAkhil Goyal 	*bufs = (void *)ctx->op;
3406fe3688baSAkhil Goyal 
3407fe3688baSAkhil Goyal 	rte_mempool_put(ctx->ctx_pool, (void *)ctx);
3408fe3688baSAkhil Goyal 
3409fe3688baSAkhil Goyal 	return qman_cb_dqrr_consume;
3410fe3688baSAkhil Goyal }
3411fe3688baSAkhil Goyal 
3412fe3688baSAkhil Goyal static enum qman_cb_dqrr_result
3413fe3688baSAkhil Goyal dpaa_sec_process_atomic_event(void *event,
3414fe3688baSAkhil Goyal 			struct qman_portal *qm __rte_unused,
3415fe3688baSAkhil Goyal 			struct qman_fq *outq,
3416fe3688baSAkhil Goyal 			const struct qm_dqrr_entry *dqrr,
3417fe3688baSAkhil Goyal 			void **bufs)
3418fe3688baSAkhil Goyal {
3419fe3688baSAkhil Goyal 	u8 index;
3420fe3688baSAkhil Goyal 	const struct qm_fd *fd;
3421fe3688baSAkhil Goyal 	struct dpaa_sec_job *job;
3422fe3688baSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
3423fe3688baSAkhil Goyal 	struct rte_event *ev = (struct rte_event *)event;
3424fe3688baSAkhil Goyal 
3425fe3688baSAkhil Goyal 	fd = &dqrr->fd;
3426fe3688baSAkhil Goyal 
3427fe3688baSAkhil Goyal 	/* sg is embedded in an op ctx,
3428fe3688baSAkhil Goyal 	 * sg[0] is for output
3429fe3688baSAkhil Goyal 	 * sg[1] for input
3430fe3688baSAkhil Goyal 	 */
3431ec861560SGagandeep Singh 	job = rte_dpaa_mem_ptov(qm_fd_addr_get64(fd));
3432fe3688baSAkhil Goyal 
3433fe3688baSAkhil Goyal 	ctx = container_of(job, struct dpaa_sec_op_ctx, job);
3434fe3688baSAkhil Goyal 	ctx->fd_status = fd->status;
3435fe3688baSAkhil Goyal 	if (ctx->op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
3436fe3688baSAkhil Goyal 		struct qm_sg_entry *sg_out;
3437fe3688baSAkhil Goyal 		uint32_t len;
3438fe3688baSAkhil Goyal 
3439fe3688baSAkhil Goyal 		sg_out = &job->sg[0];
3440fe3688baSAkhil Goyal 		hw_sg_to_cpu(sg_out);
3441fe3688baSAkhil Goyal 		len = sg_out->length;
3442fe3688baSAkhil Goyal 		ctx->op->sym->m_src->pkt_len = len;
3443fe3688baSAkhil Goyal 		ctx->op->sym->m_src->data_len = len;
3444fe3688baSAkhil Goyal 	}
3445fe3688baSAkhil Goyal 	if (!ctx->fd_status) {
3446fe3688baSAkhil Goyal 		ctx->op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
3447fe3688baSAkhil Goyal 	} else {
3448fe3688baSAkhil Goyal 		DPAA_SEC_DP_WARN("SEC return err: 0x%x", ctx->fd_status);
3449fe3688baSAkhil Goyal 		ctx->op->status = RTE_CRYPTO_OP_STATUS_ERROR;
3450fe3688baSAkhil Goyal 	}
3451fe3688baSAkhil Goyal 	ev->event_ptr = (void *)ctx->op;
3452fe3688baSAkhil Goyal 	ev->flow_id = outq->ev.flow_id;
3453fe3688baSAkhil Goyal 	ev->sub_event_type = outq->ev.sub_event_type;
3454fe3688baSAkhil Goyal 	ev->event_type = RTE_EVENT_TYPE_CRYPTODEV;
3455fe3688baSAkhil Goyal 	ev->op = RTE_EVENT_OP_NEW;
3456fe3688baSAkhil Goyal 	ev->sched_type = outq->ev.sched_type;
3457fe3688baSAkhil Goyal 	ev->queue_id = outq->ev.queue_id;
3458fe3688baSAkhil Goyal 	ev->priority = outq->ev.priority;
3459fe3688baSAkhil Goyal 
3460fe3688baSAkhil Goyal 	/* Save active dqrr entries */
3461fe3688baSAkhil Goyal 	index = ((uintptr_t)dqrr >> 6) & (16/*QM_DQRR_SIZE*/ - 1);
3462fe3688baSAkhil Goyal 	DPAA_PER_LCORE_DQRR_SIZE++;
3463fe3688baSAkhil Goyal 	DPAA_PER_LCORE_DQRR_HELD |= 1 << index;
3464fe3688baSAkhil Goyal 	DPAA_PER_LCORE_DQRR_MBUF(index) = ctx->op->sym->m_src;
3465fe3688baSAkhil Goyal 	ev->impl_opaque = index + 1;
3466c9a1c2e5SDavid Marchand 	*dpaa_seqn(ctx->op->sym->m_src) = (uint32_t)index + 1;
3467fe3688baSAkhil Goyal 	*bufs = (void *)ctx->op;
3468fe3688baSAkhil Goyal 
3469fe3688baSAkhil Goyal 	rte_mempool_put(ctx->ctx_pool, (void *)ctx);
3470fe3688baSAkhil Goyal 
3471fe3688baSAkhil Goyal 	return qman_cb_dqrr_defer;
3472fe3688baSAkhil Goyal }
3473fe3688baSAkhil Goyal 
3474fe3688baSAkhil Goyal int
3475fe3688baSAkhil Goyal dpaa_sec_eventq_attach(const struct rte_cryptodev *dev,
3476fe3688baSAkhil Goyal 		int qp_id,
3477fe3688baSAkhil Goyal 		uint16_t ch_id,
3478fe3688baSAkhil Goyal 		const struct rte_event *event)
3479fe3688baSAkhil Goyal {
3480fe3688baSAkhil Goyal 	struct dpaa_sec_qp *qp = dev->data->queue_pairs[qp_id];
3481fe3688baSAkhil Goyal 	struct qm_mcc_initfq opts = {0};
3482fe3688baSAkhil Goyal 
3483fe3688baSAkhil Goyal 	int ret;
3484fe3688baSAkhil Goyal 
3485fe3688baSAkhil Goyal 	opts.we_mask = QM_INITFQ_WE_DESTWQ | QM_INITFQ_WE_FQCTRL |
3486fe3688baSAkhil Goyal 		       QM_INITFQ_WE_CONTEXTA | QM_INITFQ_WE_CONTEXTB;
3487fe3688baSAkhil Goyal 	opts.fqd.dest.channel = ch_id;
3488fe3688baSAkhil Goyal 
3489fe3688baSAkhil Goyal 	switch (event->sched_type) {
3490fe3688baSAkhil Goyal 	case RTE_SCHED_TYPE_ATOMIC:
3491fe3688baSAkhil Goyal 		opts.fqd.fq_ctrl |= QM_FQCTRL_HOLDACTIVE;
3492fe3688baSAkhil Goyal 		/* Reset FQCTRL_AVOIDBLOCK bit as it is unnecessary
3493fe3688baSAkhil Goyal 		 * configuration with HOLD_ACTIVE setting
3494fe3688baSAkhil Goyal 		 */
3495fe3688baSAkhil Goyal 		opts.fqd.fq_ctrl &= (~QM_FQCTRL_AVOIDBLOCK);
3496fe3688baSAkhil Goyal 		qp->outq.cb.dqrr_dpdk_cb = dpaa_sec_process_atomic_event;
3497fe3688baSAkhil Goyal 		break;
3498fe3688baSAkhil Goyal 	case RTE_SCHED_TYPE_ORDERED:
3499fe3688baSAkhil Goyal 		DPAA_SEC_ERR("Ordered queue schedule type is not supported\n");
3500c08ced9aSAkhil Goyal 		return -ENOTSUP;
3501fe3688baSAkhil Goyal 	default:
3502fe3688baSAkhil Goyal 		opts.fqd.fq_ctrl |= QM_FQCTRL_AVOIDBLOCK;
3503fe3688baSAkhil Goyal 		qp->outq.cb.dqrr_dpdk_cb = dpaa_sec_process_parallel_event;
3504fe3688baSAkhil Goyal 		break;
3505fe3688baSAkhil Goyal 	}
3506fe3688baSAkhil Goyal 
3507fe3688baSAkhil Goyal 	ret = qman_init_fq(&qp->outq, QMAN_INITFQ_FLAG_SCHED, &opts);
3508fe3688baSAkhil Goyal 	if (unlikely(ret)) {
3509fe3688baSAkhil Goyal 		DPAA_SEC_ERR("unable to init caam source fq!");
3510fe3688baSAkhil Goyal 		return ret;
3511fe3688baSAkhil Goyal 	}
3512fe3688baSAkhil Goyal 
3513fe3688baSAkhil Goyal 	memcpy(&qp->outq.ev, event, sizeof(struct rte_event));
3514fe3688baSAkhil Goyal 
3515fe3688baSAkhil Goyal 	return 0;
3516fe3688baSAkhil Goyal }
3517fe3688baSAkhil Goyal 
3518fe3688baSAkhil Goyal int
3519fe3688baSAkhil Goyal dpaa_sec_eventq_detach(const struct rte_cryptodev *dev,
3520fe3688baSAkhil Goyal 			int qp_id)
3521fe3688baSAkhil Goyal {
3522fe3688baSAkhil Goyal 	struct qm_mcc_initfq opts = {0};
3523fe3688baSAkhil Goyal 	int ret;
3524fe3688baSAkhil Goyal 	struct dpaa_sec_qp *qp = dev->data->queue_pairs[qp_id];
3525fe3688baSAkhil Goyal 
3526fe3688baSAkhil Goyal 	opts.we_mask = QM_INITFQ_WE_DESTWQ | QM_INITFQ_WE_FQCTRL |
3527fe3688baSAkhil Goyal 		       QM_INITFQ_WE_CONTEXTA | QM_INITFQ_WE_CONTEXTB;
3528fe3688baSAkhil Goyal 	qp->outq.cb.dqrr = dqrr_out_fq_cb_rx;
3529fe3688baSAkhil Goyal 	qp->outq.cb.ern  = ern_sec_fq_handler;
3530fe3688baSAkhil Goyal 	qman_retire_fq(&qp->outq, NULL);
3531fe3688baSAkhil Goyal 	qman_oos_fq(&qp->outq);
3532fe3688baSAkhil Goyal 	ret = qman_init_fq(&qp->outq, 0, &opts);
3533fe3688baSAkhil Goyal 	if (ret)
3534fe3688baSAkhil Goyal 		RTE_LOG(ERR, PMD, "Error in qman_init_fq: ret: %d\n", ret);
3535fe3688baSAkhil Goyal 	qp->outq.cb.dqrr = NULL;
3536fe3688baSAkhil Goyal 
3537fe3688baSAkhil Goyal 	return ret;
3538fe3688baSAkhil Goyal }
3539fe3688baSAkhil Goyal 
3540c3e85bdcSAkhil Goyal static struct rte_cryptodev_ops crypto_ops = {
3541c3e85bdcSAkhil Goyal 	.dev_configure	      = dpaa_sec_dev_configure,
3542c3e85bdcSAkhil Goyal 	.dev_start	      = dpaa_sec_dev_start,
3543c3e85bdcSAkhil Goyal 	.dev_stop	      = dpaa_sec_dev_stop,
3544c3e85bdcSAkhil Goyal 	.dev_close	      = dpaa_sec_dev_close,
3545c3e85bdcSAkhil Goyal 	.dev_infos_get        = dpaa_sec_dev_infos_get,
3546c3e85bdcSAkhil Goyal 	.queue_pair_setup     = dpaa_sec_queue_pair_setup,
3547c3e85bdcSAkhil Goyal 	.queue_pair_release   = dpaa_sec_queue_pair_release,
3548012c5076SPablo de Lara 	.sym_session_get_size     = dpaa_sec_sym_session_get_size,
3549012c5076SPablo de Lara 	.sym_session_configure    = dpaa_sec_sym_session_configure,
35509d5f73c2SGagandeep Singh 	.sym_session_clear        = dpaa_sec_sym_session_clear,
35519d5f73c2SGagandeep Singh 	/* Raw data-path API related operations */
35529d5f73c2SGagandeep Singh 	.sym_get_raw_dp_ctx_size = dpaa_sec_get_dp_ctx_size,
35539d5f73c2SGagandeep Singh 	.sym_configure_raw_dp_ctx = dpaa_sec_configure_raw_dp_ctx,
3554c3e85bdcSAkhil Goyal };
3555c3e85bdcSAkhil Goyal 
3556a8d0d473SBruce Richardson #ifdef RTE_LIB_SECURITY
35571f14d500SAkhil Goyal static const struct rte_security_capability *
35581f14d500SAkhil Goyal dpaa_sec_capabilities_get(void *device __rte_unused)
35591f14d500SAkhil Goyal {
35601f14d500SAkhil Goyal 	return dpaa_sec_security_cap;
35611f14d500SAkhil Goyal }
35621f14d500SAkhil Goyal 
3563b74fd6b8SFerruh Yigit static const struct rte_security_ops dpaa_sec_security_ops = {
35641f14d500SAkhil Goyal 	.session_create = dpaa_sec_security_session_create,
35651f14d500SAkhil Goyal 	.session_update = NULL,
35661f14d500SAkhil Goyal 	.session_stats_get = NULL,
35671f14d500SAkhil Goyal 	.session_destroy = dpaa_sec_security_session_destroy,
35681f14d500SAkhil Goyal 	.set_pkt_metadata = NULL,
35691f14d500SAkhil Goyal 	.capabilities_get = dpaa_sec_capabilities_get
35701f14d500SAkhil Goyal };
3571314424b6SHemant Agrawal #endif
3572c3e85bdcSAkhil Goyal static int
3573c3e85bdcSAkhil Goyal dpaa_sec_uninit(struct rte_cryptodev *dev)
3574c3e85bdcSAkhil Goyal {
3575debef417SShreyansh Jain 	struct dpaa_sec_dev_private *internals;
3576c3e85bdcSAkhil Goyal 
3577c3e85bdcSAkhil Goyal 	if (dev == NULL)
3578c3e85bdcSAkhil Goyal 		return -ENODEV;
3579c3e85bdcSAkhil Goyal 
3580debef417SShreyansh Jain 	internals = dev->data->dev_private;
35811f14d500SAkhil Goyal 	rte_free(dev->security_ctx);
35821f14d500SAkhil Goyal 
3583c3e85bdcSAkhil Goyal 	rte_free(internals);
3584c3e85bdcSAkhil Goyal 
3585f163231eSHemant Agrawal 	DPAA_SEC_INFO("Closing DPAA_SEC device %s on numa socket %u",
3586c3e85bdcSAkhil Goyal 		      dev->data->name, rte_socket_id());
3587c3e85bdcSAkhil Goyal 
3588c3e85bdcSAkhil Goyal 	return 0;
3589c3e85bdcSAkhil Goyal }
3590c3e85bdcSAkhil Goyal 
3591c3e85bdcSAkhil Goyal static int
3592b1bbf222SGagandeep Singh check_devargs_handler(__rte_unused const char *key, const char *value,
3593b1bbf222SGagandeep Singh 		      __rte_unused void *opaque)
3594b1bbf222SGagandeep Singh {
3595b1bbf222SGagandeep Singh 	dpaa_sec_dp_dump = atoi(value);
3596b1bbf222SGagandeep Singh 	if (dpaa_sec_dp_dump > DPAA_SEC_DP_FULL_DUMP) {
3597b1bbf222SGagandeep Singh 		DPAA_SEC_WARN("WARN: DPAA_SEC_DP_DUMP_LEVEL is not "
3598b1bbf222SGagandeep Singh 			      "supported, changing to FULL error prints\n");
3599b1bbf222SGagandeep Singh 		dpaa_sec_dp_dump = DPAA_SEC_DP_FULL_DUMP;
3600b1bbf222SGagandeep Singh 	}
3601b1bbf222SGagandeep Singh 
3602b1bbf222SGagandeep Singh 	return 0;
3603b1bbf222SGagandeep Singh }
3604b1bbf222SGagandeep Singh 
3605b1bbf222SGagandeep Singh static void
3606b1bbf222SGagandeep Singh dpaa_sec_get_devargs(struct rte_devargs *devargs, const char *key)
3607b1bbf222SGagandeep Singh {
3608b1bbf222SGagandeep Singh 	struct rte_kvargs *kvlist;
3609b1bbf222SGagandeep Singh 
3610b1bbf222SGagandeep Singh 	if (!devargs)
3611b1bbf222SGagandeep Singh 		return;
3612b1bbf222SGagandeep Singh 
3613b1bbf222SGagandeep Singh 	kvlist = rte_kvargs_parse(devargs->args, NULL);
3614b1bbf222SGagandeep Singh 	if (!kvlist)
3615b1bbf222SGagandeep Singh 		return;
3616b1bbf222SGagandeep Singh 
3617b1bbf222SGagandeep Singh 	if (!rte_kvargs_count(kvlist, key)) {
3618b1bbf222SGagandeep Singh 		rte_kvargs_free(kvlist);
3619b1bbf222SGagandeep Singh 		return;
3620b1bbf222SGagandeep Singh 	}
3621b1bbf222SGagandeep Singh 
3622b1bbf222SGagandeep Singh 	rte_kvargs_process(kvlist, key,
3623b1bbf222SGagandeep Singh 				check_devargs_handler, NULL);
3624b1bbf222SGagandeep Singh 	rte_kvargs_free(kvlist);
3625b1bbf222SGagandeep Singh }
3626b1bbf222SGagandeep Singh 
3627b1bbf222SGagandeep Singh static int
3628c3e85bdcSAkhil Goyal dpaa_sec_dev_init(struct rte_cryptodev *cryptodev)
3629c3e85bdcSAkhil Goyal {
3630c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals;
3631a8d0d473SBruce Richardson #ifdef RTE_LIB_SECURITY
36321f14d500SAkhil Goyal 	struct rte_security_ctx *security_instance;
3633314424b6SHemant Agrawal #endif
3634c3e85bdcSAkhil Goyal 	struct dpaa_sec_qp *qp;
3635e79416d1SHemant Agrawal 	uint32_t i, flags;
3636c3e85bdcSAkhil Goyal 	int ret;
36378a3167dbSGagandeep Singh 	void *cmd_map;
36388a3167dbSGagandeep Singh 	int map_fd = -1;
3639c3e85bdcSAkhil Goyal 
3640c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
3641c3e85bdcSAkhil Goyal 
36428a3167dbSGagandeep Singh 	internals = cryptodev->data->dev_private;
36438a3167dbSGagandeep Singh 	map_fd = open("/dev/mem", O_RDWR);
36448a3167dbSGagandeep Singh 	if (unlikely(map_fd < 0)) {
36458a3167dbSGagandeep Singh 		DPAA_SEC_ERR("Unable to open (/dev/mem)");
36468a3167dbSGagandeep Singh 		return map_fd;
36478a3167dbSGagandeep Singh 	}
36488a3167dbSGagandeep Singh 	internals->sec_hw = mmap(NULL, MAP_SIZE, PROT_READ | PROT_WRITE,
36498a3167dbSGagandeep Singh 			    MAP_SHARED, map_fd, SEC_BASE_ADDR);
36508a3167dbSGagandeep Singh 	if (internals->sec_hw == MAP_FAILED) {
36518a3167dbSGagandeep Singh 		DPAA_SEC_ERR("Memory map failed");
36528a3167dbSGagandeep Singh 		close(map_fd);
36538a3167dbSGagandeep Singh 		return -EINVAL;
36548a3167dbSGagandeep Singh 	}
36558a3167dbSGagandeep Singh 	cmd_map = (uint8_t *)internals->sec_hw +
36568a3167dbSGagandeep Singh 		  (BLOCK_OFFSET * QI_BLOCK_NUMBER) + CMD_REG;
36578a3167dbSGagandeep Singh 	if (!(be32_to_cpu(rte_read32(cmd_map)) & QICTL_DQEN))
36588a3167dbSGagandeep Singh 		/* enable QI interface */
36598a3167dbSGagandeep Singh 		rte_write32(cpu_to_be32(QICTL_DQEN), cmd_map);
36608a3167dbSGagandeep Singh 
36618a3167dbSGagandeep Singh 	ret = munmap(internals->sec_hw, MAP_SIZE);
36628a3167dbSGagandeep Singh 	if (ret)
36638a3167dbSGagandeep Singh 		DPAA_SEC_WARN("munmap failed\n");
36648a3167dbSGagandeep Singh 
36658a3167dbSGagandeep Singh 	close(map_fd);
36669d5f73c2SGagandeep Singh 	cryptodev->driver_id = dpaa_cryptodev_driver_id;
3667c3e85bdcSAkhil Goyal 	cryptodev->dev_ops = &crypto_ops;
3668c3e85bdcSAkhil Goyal 
3669c3e85bdcSAkhil Goyal 	cryptodev->enqueue_burst = dpaa_sec_enqueue_burst;
3670c3e85bdcSAkhil Goyal 	cryptodev->dequeue_burst = dpaa_sec_dequeue_burst;
3671c3e85bdcSAkhil Goyal 	cryptodev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
3672c3e85bdcSAkhil Goyal 			RTE_CRYPTODEV_FF_HW_ACCELERATED |
36731f14d500SAkhil Goyal 			RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
3674a74af788SAkhil Goyal 			RTE_CRYPTODEV_FF_SECURITY |
36759d5f73c2SGagandeep Singh 			RTE_CRYPTODEV_FF_SYM_RAW_DP |
36762717246eSPablo de Lara 			RTE_CRYPTODEV_FF_IN_PLACE_SGL |
36772717246eSPablo de Lara 			RTE_CRYPTODEV_FF_OOP_SGL_IN_SGL_OUT |
36782717246eSPablo de Lara 			RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT |
36792717246eSPablo de Lara 			RTE_CRYPTODEV_FF_OOP_LB_IN_SGL_OUT |
36802717246eSPablo de Lara 			RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT;
3681c3e85bdcSAkhil Goyal 
3682e79416d1SHemant Agrawal 	internals->max_nb_queue_pairs = RTE_DPAA_MAX_NB_SEC_QPS;
3683c3e85bdcSAkhil Goyal 	internals->max_nb_sessions = RTE_DPAA_SEC_PMD_MAX_NB_SESSIONS;
3684c3e85bdcSAkhil Goyal 
36851f14d500SAkhil Goyal 	/*
36861f14d500SAkhil Goyal 	 * For secondary processes, we don't initialise any further as primary
36871f14d500SAkhil Goyal 	 * has already done this work. Only check we don't need a different
36881f14d500SAkhil Goyal 	 * RX function
36891f14d500SAkhil Goyal 	 */
36901f14d500SAkhil Goyal 	if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
3691f163231eSHemant Agrawal 		DPAA_SEC_WARN("Device already init by primary process");
36921f14d500SAkhil Goyal 		return 0;
36931f14d500SAkhil Goyal 	}
3694a8d0d473SBruce Richardson #ifdef RTE_LIB_SECURITY
36951f14d500SAkhil Goyal 	/* Initialize security_ctx only for primary process*/
36961f14d500SAkhil Goyal 	security_instance = rte_malloc("rte_security_instances_ops",
36971f14d500SAkhil Goyal 				sizeof(struct rte_security_ctx), 0);
36981f14d500SAkhil Goyal 	if (security_instance == NULL)
36991f14d500SAkhil Goyal 		return -ENOMEM;
37001f14d500SAkhil Goyal 	security_instance->device = (void *)cryptodev;
37011f14d500SAkhil Goyal 	security_instance->ops = &dpaa_sec_security_ops;
37021f14d500SAkhil Goyal 	security_instance->sess_cnt = 0;
37031f14d500SAkhil Goyal 	cryptodev->security_ctx = security_instance;
3704314424b6SHemant Agrawal #endif
37053b617ee7SAkhil Goyal 	rte_spinlock_init(&internals->lock);
3706c3e85bdcSAkhil Goyal 	for (i = 0; i < internals->max_nb_queue_pairs; i++) {
3707c3e85bdcSAkhil Goyal 		/* init qman fq for queue pair */
3708c3e85bdcSAkhil Goyal 		qp = &internals->qps[i];
3709c3e85bdcSAkhil Goyal 		ret = dpaa_sec_init_tx(&qp->outq);
3710c3e85bdcSAkhil Goyal 		if (ret) {
3711f163231eSHemant Agrawal 			DPAA_SEC_ERR("config tx of queue pair  %d", i);
3712c3e85bdcSAkhil Goyal 			goto init_error;
3713c3e85bdcSAkhil Goyal 		}
3714e79416d1SHemant Agrawal 	}
3715e79416d1SHemant Agrawal 
3716e79416d1SHemant Agrawal 	flags = QMAN_FQ_FLAG_LOCKED | QMAN_FQ_FLAG_DYNAMIC_FQID |
3717e79416d1SHemant Agrawal 		QMAN_FQ_FLAG_TO_DCPORTAL;
3718fd900d38SGagandeep Singh 	for (i = 0; i < RTE_DPAA_MAX_RX_QUEUE; i++) {
3719e79416d1SHemant Agrawal 		/* create rx qman fq for sessions*/
3720e79416d1SHemant Agrawal 		ret = qman_create_fq(0, flags, &internals->inq[i]);
3721e79416d1SHemant Agrawal 		if (unlikely(ret != 0)) {
3722f163231eSHemant Agrawal 			DPAA_SEC_ERR("sec qman_create_fq failed");
3723c3e85bdcSAkhil Goyal 			goto init_error;
3724c3e85bdcSAkhil Goyal 		}
3725c3e85bdcSAkhil Goyal 	}
3726c3e85bdcSAkhil Goyal 
3727b1bbf222SGagandeep Singh 	dpaa_sec_get_devargs(cryptodev->device->devargs, DRIVER_DUMP_MODE);
3728b1bbf222SGagandeep Singh 
3729f163231eSHemant Agrawal 	RTE_LOG(INFO, PMD, "%s cryptodev init\n", cryptodev->data->name);
3730c3e85bdcSAkhil Goyal 	return 0;
3731c3e85bdcSAkhil Goyal 
3732c3e85bdcSAkhil Goyal init_error:
3733f163231eSHemant Agrawal 	DPAA_SEC_ERR("driver %s: create failed\n", cryptodev->data->name);
3734c3e85bdcSAkhil Goyal 
37352eaf352dSLukasz Wojciechowski 	rte_free(cryptodev->security_ctx);
3736c3e85bdcSAkhil Goyal 	return -EFAULT;
3737c3e85bdcSAkhil Goyal }
3738c3e85bdcSAkhil Goyal 
3739c3e85bdcSAkhil Goyal static int
3740eb5a9a76SShreyansh Jain cryptodev_dpaa_sec_probe(struct rte_dpaa_driver *dpaa_drv __rte_unused,
3741c3e85bdcSAkhil Goyal 				struct rte_dpaa_device *dpaa_dev)
3742c3e85bdcSAkhil Goyal {
3743c3e85bdcSAkhil Goyal 	struct rte_cryptodev *cryptodev;
3744c3e85bdcSAkhil Goyal 	char cryptodev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
3745c3e85bdcSAkhil Goyal 
3746c3e85bdcSAkhil Goyal 	int retval;
3747c3e85bdcSAkhil Goyal 
374896ec64f1SVanshika Shukla 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
374996ec64f1SVanshika Shukla 		return 0;
375096ec64f1SVanshika Shukla 
37510964a951SHemant Agrawal 	snprintf(cryptodev_name, sizeof(cryptodev_name), "%s", dpaa_dev->name);
3752c3e85bdcSAkhil Goyal 
3753c3e85bdcSAkhil Goyal 	cryptodev = rte_cryptodev_pmd_allocate(cryptodev_name, rte_socket_id());
3754c3e85bdcSAkhil Goyal 	if (cryptodev == NULL)
3755c3e85bdcSAkhil Goyal 		return -ENOMEM;
3756c3e85bdcSAkhil Goyal 
3757c3e85bdcSAkhil Goyal 	cryptodev->data->dev_private = rte_zmalloc_socket(
3758c3e85bdcSAkhil Goyal 				"cryptodev private structure",
3759c3e85bdcSAkhil Goyal 				sizeof(struct dpaa_sec_dev_private),
3760c3e85bdcSAkhil Goyal 				RTE_CACHE_LINE_SIZE,
3761c3e85bdcSAkhil Goyal 				rte_socket_id());
3762c3e85bdcSAkhil Goyal 
3763c3e85bdcSAkhil Goyal 	if (cryptodev->data->dev_private == NULL)
3764c3e85bdcSAkhil Goyal 		rte_panic("Cannot allocate memzone for private "
3765c3e85bdcSAkhil Goyal 				"device data");
3766c3e85bdcSAkhil Goyal 
3767c3e85bdcSAkhil Goyal 	dpaa_dev->crypto_dev = cryptodev;
3768c3e85bdcSAkhil Goyal 	cryptodev->device = &dpaa_dev->device;
3769c3e85bdcSAkhil Goyal 
3770c3e85bdcSAkhil Goyal 	/* init user callbacks */
3771c3e85bdcSAkhil Goyal 	TAILQ_INIT(&(cryptodev->link_intr_cbs));
3772c3e85bdcSAkhil Goyal 
3773c3e85bdcSAkhil Goyal 	/* if sec device version is not configured */
3774c3e85bdcSAkhil Goyal 	if (!rta_get_sec_era()) {
3775c3e85bdcSAkhil Goyal 		const struct device_node *caam_node;
3776c3e85bdcSAkhil Goyal 
3777c3e85bdcSAkhil Goyal 		for_each_compatible_node(caam_node, NULL, "fsl,sec-v4.0") {
3778c3e85bdcSAkhil Goyal 			const uint32_t *prop = of_get_property(caam_node,
3779c3e85bdcSAkhil Goyal 					"fsl,sec-era",
3780c3e85bdcSAkhil Goyal 					NULL);
3781c3e85bdcSAkhil Goyal 			if (prop) {
3782c3e85bdcSAkhil Goyal 				rta_set_sec_era(
3783c3e85bdcSAkhil Goyal 					INTL_SEC_ERA(rte_cpu_to_be_32(*prop)));
3784c3e85bdcSAkhil Goyal 				break;
3785c3e85bdcSAkhil Goyal 			}
3786c3e85bdcSAkhil Goyal 		}
3787c3e85bdcSAkhil Goyal 	}
3788c3e85bdcSAkhil Goyal 
3789e5872221SRohit Raj 	if (unlikely(!DPAA_PER_LCORE_PORTAL)) {
3790408077f2SHemant Agrawal 		retval = rte_dpaa_portal_init((void *)1);
3791408077f2SHemant Agrawal 		if (retval) {
3792408077f2SHemant Agrawal 			DPAA_SEC_ERR("Unable to initialize portal");
37932eaf352dSLukasz Wojciechowski 			goto out;
3794408077f2SHemant Agrawal 		}
3795408077f2SHemant Agrawal 	}
3796408077f2SHemant Agrawal 
3797c3e85bdcSAkhil Goyal 	/* Invoke PMD device initialization function */
3798c3e85bdcSAkhil Goyal 	retval = dpaa_sec_dev_init(cryptodev);
3799d54c72ecSAkhil Goyal 	if (retval == 0) {
3800d54c72ecSAkhil Goyal 		rte_cryptodev_pmd_probing_finish(cryptodev);
3801c3e85bdcSAkhil Goyal 		return 0;
3802d54c72ecSAkhil Goyal 	}
3803c3e85bdcSAkhil Goyal 
38042eaf352dSLukasz Wojciechowski 	retval = -ENXIO;
38052eaf352dSLukasz Wojciechowski out:
3806c3e85bdcSAkhil Goyal 	/* In case of error, cleanup is done */
3807c3e85bdcSAkhil Goyal 	rte_free(cryptodev->data->dev_private);
3808c3e85bdcSAkhil Goyal 
3809c3e85bdcSAkhil Goyal 	rte_cryptodev_pmd_release_device(cryptodev);
3810c3e85bdcSAkhil Goyal 
38112eaf352dSLukasz Wojciechowski 	return retval;
3812c3e85bdcSAkhil Goyal }
3813c3e85bdcSAkhil Goyal 
3814c3e85bdcSAkhil Goyal static int
3815c3e85bdcSAkhil Goyal cryptodev_dpaa_sec_remove(struct rte_dpaa_device *dpaa_dev)
3816c3e85bdcSAkhil Goyal {
3817c3e85bdcSAkhil Goyal 	struct rte_cryptodev *cryptodev;
3818c3e85bdcSAkhil Goyal 	int ret;
3819c3e85bdcSAkhil Goyal 
3820c3e85bdcSAkhil Goyal 	cryptodev = dpaa_dev->crypto_dev;
3821c3e85bdcSAkhil Goyal 	if (cryptodev == NULL)
3822c3e85bdcSAkhil Goyal 		return -ENODEV;
3823c3e85bdcSAkhil Goyal 
3824c3e85bdcSAkhil Goyal 	ret = dpaa_sec_uninit(cryptodev);
3825c3e85bdcSAkhil Goyal 	if (ret)
3826c3e85bdcSAkhil Goyal 		return ret;
3827c3e85bdcSAkhil Goyal 
3828f2f020d2SDeclan Doherty 	return rte_cryptodev_pmd_destroy(cryptodev);
3829c3e85bdcSAkhil Goyal }
3830c3e85bdcSAkhil Goyal 
3831c3e85bdcSAkhil Goyal static struct rte_dpaa_driver rte_dpaa_sec_driver = {
3832c3e85bdcSAkhil Goyal 	.drv_type = FSL_DPAA_CRYPTO,
3833c3e85bdcSAkhil Goyal 	.driver = {
3834c3e85bdcSAkhil Goyal 		.name = "DPAA SEC PMD"
3835c3e85bdcSAkhil Goyal 	},
3836c3e85bdcSAkhil Goyal 	.probe = cryptodev_dpaa_sec_probe,
3837c3e85bdcSAkhil Goyal 	.remove = cryptodev_dpaa_sec_remove,
3838c3e85bdcSAkhil Goyal };
3839c3e85bdcSAkhil Goyal 
3840c3e85bdcSAkhil Goyal static struct cryptodev_driver dpaa_sec_crypto_drv;
3841c3e85bdcSAkhil Goyal 
3842c3e85bdcSAkhil Goyal RTE_PMD_REGISTER_DPAA(CRYPTODEV_NAME_DPAA_SEC_PMD, rte_dpaa_sec_driver);
3843f737f5ceSFiona Trahe RTE_PMD_REGISTER_CRYPTO_DRIVER(dpaa_sec_crypto_drv, rte_dpaa_sec_driver.driver,
38449d5f73c2SGagandeep Singh 		dpaa_cryptodev_driver_id);
3845b1bbf222SGagandeep Singh RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_DPAA_SEC_PMD,
3846b1bbf222SGagandeep Singh 		DRIVER_DUMP_MODE "=<int>");
38479c99878aSJerin Jacob RTE_LOG_REGISTER(dpaa_logtype_sec, pmd.crypto.dpaa, NOTICE);
3848