xref: /dpdk/drivers/crypto/dpaa_sec/dpaa_sec.c (revision 32d8bc55476dcfabf3b3650f75fc19c9b09050ba)
1d81734caSHemant Agrawal /* SPDX-License-Identifier: BSD-3-Clause
2c3e85bdcSAkhil Goyal  *
3c3e85bdcSAkhil Goyal  *   Copyright (c) 2016 Freescale Semiconductor, Inc. All rights reserved.
4c9fd1acdSGagandeep Singh  *   Copyright 2017-2024 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>
181f14d500SAkhil Goyal #include <rte_security_driver.h>
19c3e85bdcSAkhil Goyal #include <rte_cycles.h>
201acb7f54SDavid Marchand #include <dev_driver.h>
218a3167dbSGagandeep Singh #include <rte_io.h>
22bd063651SFerruh Yigit #include <rte_ip.h>
23*32d8bc55SBarry Cao #include <rte_udp.h>
24c3e85bdcSAkhil Goyal #include <rte_kvargs.h>
25c3e85bdcSAkhil Goyal #include <rte_malloc.h>
26c3e85bdcSAkhil Goyal #include <rte_mbuf.h>
27c3e85bdcSAkhil Goyal #include <rte_memcpy.h>
28c3e85bdcSAkhil Goyal #include <rte_string_fns.h>
293b617ee7SAkhil Goyal #include <rte_spinlock.h>
30b1bbf222SGagandeep Singh #include <rte_hexdump.h>
31c3e85bdcSAkhil Goyal 
32c3e85bdcSAkhil Goyal #include <fsl_usd.h>
33c3e85bdcSAkhil Goyal #include <fsl_qman.h>
348c83f28cSHemant Agrawal #include <dpaa_of.h>
35c3e85bdcSAkhil Goyal 
36c3e85bdcSAkhil Goyal /* RTA header files */
37c0ded849SHemant Agrawal #include <desc/common.h>
38c0ded849SHemant Agrawal #include <desc/algo.h>
39c0ded849SHemant Agrawal #include <desc/ipsec.h>
40c0ded849SHemant Agrawal #include <desc/pdcp.h>
415a4954bcSAkhil Goyal #include <desc/sdap.h>
42c3e85bdcSAkhil Goyal 
43a2f1da7dSDavid Marchand #include <bus_dpaa_driver.h>
44c3e85bdcSAkhil Goyal #include <dpaa_sec.h>
45fe3688baSAkhil Goyal #include <dpaa_sec_event.h>
46c3e85bdcSAkhil Goyal #include <dpaa_sec_log.h>
4712e58429SThierry Herbelot #include <dpaax_iova_table.h>
48c3e85bdcSAkhil Goyal 
49b1bbf222SGagandeep Singh #define DRIVER_DUMP_MODE "drv_dump_mode"
50*32d8bc55SBarry Cao #define DPAA_DEFAULT_NAT_T_PORT 4500
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 {
107f665790aSDavid Marchand 	DPAA_SEC_DP_ERR("sec fq %d error, RC = %x, seqnum = %x",
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 
240a1173d55SHemant Agrawal static int
241a1173d55SHemant Agrawal dpaa_sec_prep_pdcp_cdb(dpaa_sec_session *ses)
242a1173d55SHemant Agrawal {
243a1173d55SHemant Agrawal 	struct alginfo authdata = {0}, cipherdata = {0};
244a1173d55SHemant Agrawal 	struct sec_cdb *cdb = &ses->cdb;
2452e4cbdb4SVakul Garg 	struct alginfo *p_authdata = NULL;
246a1173d55SHemant Agrawal 	int32_t shared_desc_len = 0;
247a1173d55SHemant Agrawal #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
248a1173d55SHemant Agrawal 	int swap = false;
249a1173d55SHemant Agrawal #else
250a1173d55SHemant Agrawal 	int swap = true;
251a1173d55SHemant Agrawal #endif
252a1173d55SHemant Agrawal 
253a1173d55SHemant Agrawal 	cipherdata.key = (size_t)ses->cipher_key.data;
254a1173d55SHemant Agrawal 	cipherdata.keylen = ses->cipher_key.length;
255a1173d55SHemant Agrawal 	cipherdata.key_enc_flags = 0;
256a1173d55SHemant Agrawal 	cipherdata.key_type = RTA_DATA_IMM;
2578524b44eSHemant Agrawal 	cipherdata.algtype = ses->cipher_key.alg;
2588524b44eSHemant Agrawal 	cipherdata.algmode = ses->cipher_key.algmode;
259a1173d55SHemant Agrawal 
2602e4cbdb4SVakul Garg 	if (ses->auth_alg) {
261a1173d55SHemant Agrawal 		authdata.key = (size_t)ses->auth_key.data;
262a1173d55SHemant Agrawal 		authdata.keylen = ses->auth_key.length;
263a1173d55SHemant Agrawal 		authdata.key_enc_flags = 0;
264a1173d55SHemant Agrawal 		authdata.key_type = RTA_DATA_IMM;
2658524b44eSHemant Agrawal 		authdata.algtype = ses->auth_key.alg;
2668524b44eSHemant Agrawal 		authdata.algmode = ses->auth_key.algmode;
267a1173d55SHemant Agrawal 
2682e4cbdb4SVakul Garg 		p_authdata = &authdata;
2692e4cbdb4SVakul Garg 	}
2702e4cbdb4SVakul Garg 
271ebc27d1bSFranck Lenormand 	if (ses->pdcp.sdap_enabled) {
272ebc27d1bSFranck Lenormand 		int nb_keys_to_inline =
273ebc27d1bSFranck Lenormand 				rta_inline_pdcp_sdap_query(authdata.algtype,
274ebc27d1bSFranck Lenormand 					cipherdata.algtype,
275ebc27d1bSFranck Lenormand 					ses->pdcp.sn_size,
276ebc27d1bSFranck Lenormand 					ses->pdcp.hfn_ovd);
277ebc27d1bSFranck Lenormand 		if (nb_keys_to_inline >= 1) {
278ebc27d1bSFranck Lenormand 			cipherdata.key = (size_t)rte_dpaa_mem_vtop((void *)
279ebc27d1bSFranck Lenormand 						(size_t)cipherdata.key);
280ebc27d1bSFranck Lenormand 			cipherdata.key_type = RTA_DATA_PTR;
281ebc27d1bSFranck Lenormand 		}
282ebc27d1bSFranck Lenormand 		if (nb_keys_to_inline >= 2) {
283ebc27d1bSFranck Lenormand 			authdata.key = (size_t)rte_dpaa_mem_vtop((void *)
284ebc27d1bSFranck Lenormand 						(size_t)authdata.key);
285ebc27d1bSFranck Lenormand 			authdata.key_type = RTA_DATA_PTR;
286ebc27d1bSFranck Lenormand 		}
287ebc27d1bSFranck Lenormand 	} else {
288f6ab96f1SAkhil Goyal 		if (rta_inline_pdcp_query(authdata.algtype,
289f6ab96f1SAkhil Goyal 					cipherdata.algtype,
290f6ab96f1SAkhil Goyal 					ses->pdcp.sn_size,
291f6ab96f1SAkhil Goyal 					ses->pdcp.hfn_ovd)) {
292ebc27d1bSFranck Lenormand 			cipherdata.key = (size_t)rte_dpaa_mem_vtop((void *)
293f6ab96f1SAkhil Goyal 						(size_t)cipherdata.key);
294a1173d55SHemant Agrawal 			cipherdata.key_type = RTA_DATA_PTR;
295a1173d55SHemant Agrawal 		}
296ebc27d1bSFranck Lenormand 	}
297a1173d55SHemant Agrawal 
2982e4cbdb4SVakul Garg 	if (ses->pdcp.domain == RTE_SECURITY_PDCP_MODE_CONTROL) {
299a1173d55SHemant Agrawal 		if (ses->dir == DIR_ENC)
300a1173d55SHemant Agrawal 			shared_desc_len = cnstr_shdsc_pdcp_c_plane_encap(
301a1173d55SHemant Agrawal 					cdb->sh_desc, 1, swap,
302a1173d55SHemant Agrawal 					ses->pdcp.hfn,
303eac60082SVakul Garg 					ses->pdcp.sn_size,
304a1173d55SHemant Agrawal 					ses->pdcp.bearer,
305a1173d55SHemant Agrawal 					ses->pdcp.pkt_dir,
306a1173d55SHemant Agrawal 					ses->pdcp.hfn_threshold,
3076127fff8SFranck Lenormand 					&cipherdata, &authdata);
308a1173d55SHemant Agrawal 		else if (ses->dir == DIR_DEC)
309a1173d55SHemant Agrawal 			shared_desc_len = cnstr_shdsc_pdcp_c_plane_decap(
310a1173d55SHemant Agrawal 					cdb->sh_desc, 1, swap,
311a1173d55SHemant Agrawal 					ses->pdcp.hfn,
312eac60082SVakul Garg 					ses->pdcp.sn_size,
313a1173d55SHemant Agrawal 					ses->pdcp.bearer,
314a1173d55SHemant Agrawal 					ses->pdcp.pkt_dir,
315a1173d55SHemant Agrawal 					ses->pdcp.hfn_threshold,
3166127fff8SFranck Lenormand 					&cipherdata, &authdata);
31793e2661eSGagandeep Singh 	} else if (ses->pdcp.domain == RTE_SECURITY_PDCP_MODE_SHORT_MAC) {
31893e2661eSGagandeep Singh 		shared_desc_len = cnstr_shdsc_pdcp_short_mac(cdb->sh_desc,
31993e2661eSGagandeep Singh 						     1, swap, &authdata);
320a1173d55SHemant Agrawal 	} else {
3215a4954bcSAkhil Goyal 		if (ses->dir == DIR_ENC) {
3225a4954bcSAkhil Goyal 			if (ses->pdcp.sdap_enabled)
3235a4954bcSAkhil Goyal 				shared_desc_len =
3245a4954bcSAkhil Goyal 					cnstr_shdsc_pdcp_sdap_u_plane_encap(
325a1173d55SHemant Agrawal 						cdb->sh_desc, 1, swap,
326a1173d55SHemant Agrawal 						ses->pdcp.sn_size,
327a1173d55SHemant Agrawal 						ses->pdcp.hfn,
328a1173d55SHemant Agrawal 						ses->pdcp.bearer,
329a1173d55SHemant Agrawal 						ses->pdcp.pkt_dir,
330a1173d55SHemant Agrawal 						ses->pdcp.hfn_threshold,
3316127fff8SFranck Lenormand 						&cipherdata, p_authdata);
3325a4954bcSAkhil Goyal 			else
3335a4954bcSAkhil Goyal 				shared_desc_len =
3345a4954bcSAkhil Goyal 					cnstr_shdsc_pdcp_u_plane_encap(
335a1173d55SHemant Agrawal 						cdb->sh_desc, 1, swap,
336a1173d55SHemant Agrawal 						ses->pdcp.sn_size,
337a1173d55SHemant Agrawal 						ses->pdcp.hfn,
338a1173d55SHemant Agrawal 						ses->pdcp.bearer,
339a1173d55SHemant Agrawal 						ses->pdcp.pkt_dir,
340a1173d55SHemant Agrawal 						ses->pdcp.hfn_threshold,
3416127fff8SFranck Lenormand 						&cipherdata, p_authdata);
3425a4954bcSAkhil Goyal 		} else if (ses->dir == DIR_DEC) {
3435a4954bcSAkhil Goyal 			if (ses->pdcp.sdap_enabled)
3445a4954bcSAkhil Goyal 				shared_desc_len =
3455a4954bcSAkhil Goyal 					cnstr_shdsc_pdcp_sdap_u_plane_decap(
3465a4954bcSAkhil Goyal 						cdb->sh_desc, 1, swap,
3475a4954bcSAkhil Goyal 						ses->pdcp.sn_size,
3485a4954bcSAkhil Goyal 						ses->pdcp.hfn,
3495a4954bcSAkhil Goyal 						ses->pdcp.bearer,
3505a4954bcSAkhil Goyal 						ses->pdcp.pkt_dir,
3515a4954bcSAkhil Goyal 						ses->pdcp.hfn_threshold,
3526127fff8SFranck Lenormand 						&cipherdata, p_authdata);
3535a4954bcSAkhil Goyal 			else
3545a4954bcSAkhil Goyal 				shared_desc_len =
3555a4954bcSAkhil Goyal 					cnstr_shdsc_pdcp_u_plane_decap(
3565a4954bcSAkhil Goyal 						cdb->sh_desc, 1, swap,
3575a4954bcSAkhil Goyal 						ses->pdcp.sn_size,
3585a4954bcSAkhil Goyal 						ses->pdcp.hfn,
3595a4954bcSAkhil Goyal 						ses->pdcp.bearer,
3605a4954bcSAkhil Goyal 						ses->pdcp.pkt_dir,
3615a4954bcSAkhil Goyal 						ses->pdcp.hfn_threshold,
3626127fff8SFranck Lenormand 						&cipherdata, p_authdata);
3635a4954bcSAkhil Goyal 		}
364a1173d55SHemant Agrawal 	}
365a1173d55SHemant Agrawal 	return shared_desc_len;
366a1173d55SHemant Agrawal }
367a1173d55SHemant Agrawal 
36805b12700SHemant Agrawal /* prepare ipsec proto command block of the session */
36905b12700SHemant Agrawal static int
37005b12700SHemant Agrawal dpaa_sec_prep_ipsec_cdb(dpaa_sec_session *ses)
37105b12700SHemant Agrawal {
37205b12700SHemant Agrawal 	struct alginfo cipherdata = {0}, authdata = {0};
37305b12700SHemant Agrawal 	struct sec_cdb *cdb = &ses->cdb;
37405b12700SHemant Agrawal 	int32_t shared_desc_len = 0;
37505b12700SHemant Agrawal 	int err;
37605b12700SHemant Agrawal #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
37705b12700SHemant Agrawal 	int swap = false;
37805b12700SHemant Agrawal #else
37905b12700SHemant Agrawal 	int swap = true;
38005b12700SHemant Agrawal #endif
38105b12700SHemant Agrawal 
38205b12700SHemant Agrawal 	cipherdata.key = (size_t)ses->cipher_key.data;
38305b12700SHemant Agrawal 	cipherdata.keylen = ses->cipher_key.length;
38405b12700SHemant Agrawal 	cipherdata.key_enc_flags = 0;
38505b12700SHemant Agrawal 	cipherdata.key_type = RTA_DATA_IMM;
3868524b44eSHemant Agrawal 	cipherdata.algtype = ses->cipher_key.alg;
3878524b44eSHemant Agrawal 	cipherdata.algmode = ses->cipher_key.algmode;
38805b12700SHemant Agrawal 
3892c318722SHemant Agrawal 	if (ses->auth_key.length) {
39005b12700SHemant Agrawal 		authdata.key = (size_t)ses->auth_key.data;
39105b12700SHemant Agrawal 		authdata.keylen = ses->auth_key.length;
39205b12700SHemant Agrawal 		authdata.key_enc_flags = 0;
39305b12700SHemant Agrawal 		authdata.key_type = RTA_DATA_IMM;
3948524b44eSHemant Agrawal 		authdata.algtype = ses->auth_key.alg;
3958524b44eSHemant Agrawal 		authdata.algmode = ses->auth_key.algmode;
3962c318722SHemant Agrawal 	}
39705b12700SHemant Agrawal 
39805b12700SHemant Agrawal 	cdb->sh_desc[0] = cipherdata.keylen;
39905b12700SHemant Agrawal 	cdb->sh_desc[1] = authdata.keylen;
40004634157SGagandeep Singh 	err = rta_inline_ipsec_query(IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN,
401453b9593SAkhil Goyal 			       DESC_JOB_IO_LEN,
40205b12700SHemant Agrawal 			       (unsigned int *)cdb->sh_desc,
40304634157SGagandeep Singh 			       &cdb->sh_desc[2], 2, authdata.algtype, 1);
40405b12700SHemant Agrawal 
40505b12700SHemant Agrawal 	if (err < 0) {
40605b12700SHemant Agrawal 		DPAA_SEC_ERR("Crypto: Incorrect key lengths");
40705b12700SHemant Agrawal 		return err;
40805b12700SHemant Agrawal 	}
40905b12700SHemant Agrawal 	if (cdb->sh_desc[2] & 1)
41005b12700SHemant Agrawal 		cipherdata.key_type = RTA_DATA_IMM;
41105b12700SHemant Agrawal 	else {
412ec861560SGagandeep Singh 		cipherdata.key = (size_t)rte_dpaa_mem_vtop(
41305b12700SHemant Agrawal 					(void *)(size_t)cipherdata.key);
41405b12700SHemant Agrawal 		cipherdata.key_type = RTA_DATA_PTR;
41505b12700SHemant Agrawal 	}
41605b12700SHemant Agrawal 	if (cdb->sh_desc[2] & (1<<1))
41705b12700SHemant Agrawal 		authdata.key_type = RTA_DATA_IMM;
41805b12700SHemant Agrawal 	else {
419ec861560SGagandeep Singh 		authdata.key = (size_t)rte_dpaa_mem_vtop(
42005b12700SHemant Agrawal 					(void *)(size_t)authdata.key);
42105b12700SHemant Agrawal 		authdata.key_type = RTA_DATA_PTR;
42205b12700SHemant Agrawal 	}
42305b12700SHemant Agrawal 
42405b12700SHemant Agrawal 	cdb->sh_desc[0] = 0;
42505b12700SHemant Agrawal 	cdb->sh_desc[1] = 0;
42605b12700SHemant Agrawal 	cdb->sh_desc[2] = 0;
42705b12700SHemant Agrawal 	if (ses->dir == DIR_ENC) {
42805b12700SHemant Agrawal 		shared_desc_len = cnstr_shdsc_ipsec_new_encap(
42905b12700SHemant Agrawal 				cdb->sh_desc,
43005b12700SHemant Agrawal 				true, swap, SHR_SERIAL,
43105b12700SHemant Agrawal 				&ses->encap_pdb,
43205b12700SHemant Agrawal 				(uint8_t *)&ses->ip4_hdr,
43305b12700SHemant Agrawal 				&cipherdata, &authdata);
43405b12700SHemant Agrawal 	} else if (ses->dir == DIR_DEC) {
43505b12700SHemant Agrawal 		shared_desc_len = cnstr_shdsc_ipsec_new_decap(
43605b12700SHemant Agrawal 				cdb->sh_desc,
43705b12700SHemant Agrawal 				true, swap, SHR_SERIAL,
43805b12700SHemant Agrawal 				&ses->decap_pdb,
43905b12700SHemant Agrawal 				&cipherdata, &authdata);
44005b12700SHemant Agrawal 	}
44105b12700SHemant Agrawal 	return shared_desc_len;
44205b12700SHemant Agrawal }
44376c129ceSMaxime Coquelin 
444c3e85bdcSAkhil Goyal /* prepare command block of the session */
445c3e85bdcSAkhil Goyal static int
446c3e85bdcSAkhil Goyal dpaa_sec_prep_cdb(dpaa_sec_session *ses)
447c3e85bdcSAkhil Goyal {
448c3e85bdcSAkhil Goyal 	struct alginfo alginfo_c = {0}, alginfo_a = {0}, alginfo = {0};
44922788c2cSSunil Kumar Kori 	int32_t shared_desc_len = 0;
450e79416d1SHemant Agrawal 	struct sec_cdb *cdb = &ses->cdb;
451c3e85bdcSAkhil Goyal 	int err;
452c3e85bdcSAkhil Goyal #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
453c3e85bdcSAkhil Goyal 	int swap = false;
454c3e85bdcSAkhil Goyal #else
455c3e85bdcSAkhil Goyal 	int swap = true;
456c3e85bdcSAkhil Goyal #endif
457c3e85bdcSAkhil Goyal 
458c3e85bdcSAkhil Goyal 	memset(cdb, 0, sizeof(struct sec_cdb));
459c3e85bdcSAkhil Goyal 
4608524b44eSHemant Agrawal 	switch (ses->ctxt) {
4618524b44eSHemant Agrawal 	case DPAA_SEC_IPSEC:
46205b12700SHemant Agrawal 		shared_desc_len = dpaa_sec_prep_ipsec_cdb(ses);
4638524b44eSHemant Agrawal 		break;
4648524b44eSHemant Agrawal 	case DPAA_SEC_PDCP:
465a1173d55SHemant Agrawal 		shared_desc_len = dpaa_sec_prep_pdcp_cdb(ses);
4668524b44eSHemant Agrawal 		break;
4678524b44eSHemant Agrawal 	case DPAA_SEC_CIPHER:
4680e5607e4SHemant Agrawal 		alginfo_c.key = (size_t)ses->cipher_key.data;
469c3e85bdcSAkhil Goyal 		alginfo_c.keylen = ses->cipher_key.length;
470c3e85bdcSAkhil Goyal 		alginfo_c.key_enc_flags = 0;
471c3e85bdcSAkhil Goyal 		alginfo_c.key_type = RTA_DATA_IMM;
4728524b44eSHemant Agrawal 		alginfo_c.algtype = ses->cipher_key.alg;
4738524b44eSHemant Agrawal 		alginfo_c.algmode = ses->cipher_key.algmode;
4748524b44eSHemant Agrawal 
475c5788a10SHemant Agrawal 		switch (ses->cipher_alg) {
476c5788a10SHemant Agrawal 		case RTE_CRYPTO_CIPHER_AES_CBC:
477c5788a10SHemant Agrawal 		case RTE_CRYPTO_CIPHER_3DES_CBC:
4783e4fbc6cSGagandeep Singh 		case RTE_CRYPTO_CIPHER_DES_CBC:
479c5788a10SHemant Agrawal 		case RTE_CRYPTO_CIPHER_AES_CTR:
480c5788a10SHemant Agrawal 		case RTE_CRYPTO_CIPHER_3DES_CTR:
481c5788a10SHemant Agrawal 			shared_desc_len = cnstr_shdsc_blkcipher(
482c5788a10SHemant Agrawal 					cdb->sh_desc, true,
483c5788a10SHemant Agrawal 					swap, SHR_NEVER, &alginfo_c,
484c5788a10SHemant Agrawal 					ses->iv.length,
485c5788a10SHemant Agrawal 					ses->dir);
486c5788a10SHemant Agrawal 			break;
487c5788a10SHemant Agrawal 		case RTE_CRYPTO_CIPHER_SNOW3G_UEA2:
488c5788a10SHemant Agrawal 			shared_desc_len = cnstr_shdsc_snow_f8(
489c5788a10SHemant Agrawal 					cdb->sh_desc, true, swap,
490c5788a10SHemant Agrawal 					&alginfo_c,
491c5788a10SHemant Agrawal 					ses->dir);
492c5788a10SHemant Agrawal 			break;
493c5788a10SHemant Agrawal 		case RTE_CRYPTO_CIPHER_ZUC_EEA3:
494c5788a10SHemant Agrawal 			shared_desc_len = cnstr_shdsc_zuce(
495c5788a10SHemant Agrawal 					cdb->sh_desc, true, swap,
496c5788a10SHemant Agrawal 					&alginfo_c,
497c5788a10SHemant Agrawal 					ses->dir);
498c5788a10SHemant Agrawal 			break;
499c5788a10SHemant Agrawal 		default:
50077a9b5adSHemant Agrawal 			DPAA_SEC_ERR("unsupported cipher alg %s (%d)",
50177a9b5adSHemant Agrawal 					rte_cryptodev_get_cipher_algo_string(ses->cipher_alg),
502c5788a10SHemant Agrawal 				     ses->cipher_alg);
503c3e85bdcSAkhil Goyal 			return -ENOTSUP;
504c3e85bdcSAkhil Goyal 		}
5058524b44eSHemant Agrawal 		break;
5068524b44eSHemant Agrawal 	case DPAA_SEC_AUTH:
5070e5607e4SHemant Agrawal 		alginfo_a.key = (size_t)ses->auth_key.data;
508c3e85bdcSAkhil Goyal 		alginfo_a.keylen = ses->auth_key.length;
509c3e85bdcSAkhil Goyal 		alginfo_a.key_enc_flags = 0;
510c3e85bdcSAkhil Goyal 		alginfo_a.key_type = RTA_DATA_IMM;
5118524b44eSHemant Agrawal 		alginfo_a.algtype = ses->auth_key.alg;
5128524b44eSHemant Agrawal 		alginfo_a.algmode = ses->auth_key.algmode;
513c5788a10SHemant Agrawal 		switch (ses->auth_alg) {
5144c42352cSGagandeep Singh 		case RTE_CRYPTO_AUTH_MD5:
5154c42352cSGagandeep Singh 		case RTE_CRYPTO_AUTH_SHA1:
5164c42352cSGagandeep Singh 		case RTE_CRYPTO_AUTH_SHA224:
5174c42352cSGagandeep Singh 		case RTE_CRYPTO_AUTH_SHA256:
5184c42352cSGagandeep Singh 		case RTE_CRYPTO_AUTH_SHA384:
5194c42352cSGagandeep Singh 		case RTE_CRYPTO_AUTH_SHA512:
5204c42352cSGagandeep Singh 			shared_desc_len = cnstr_shdsc_hash(
5214c42352cSGagandeep Singh 						cdb->sh_desc, true,
5224c42352cSGagandeep Singh 						swap, SHR_NEVER, &alginfo_a,
5234c42352cSGagandeep Singh 						!ses->dir,
5244c42352cSGagandeep Singh 						ses->digest_length);
5254c42352cSGagandeep Singh 			break;
526c5788a10SHemant Agrawal 		case RTE_CRYPTO_AUTH_MD5_HMAC:
527c5788a10SHemant Agrawal 		case RTE_CRYPTO_AUTH_SHA1_HMAC:
528c5788a10SHemant Agrawal 		case RTE_CRYPTO_AUTH_SHA224_HMAC:
529c5788a10SHemant Agrawal 		case RTE_CRYPTO_AUTH_SHA256_HMAC:
530c5788a10SHemant Agrawal 		case RTE_CRYPTO_AUTH_SHA384_HMAC:
531c5788a10SHemant Agrawal 		case RTE_CRYPTO_AUTH_SHA512_HMAC:
532c5788a10SHemant Agrawal 			shared_desc_len = cnstr_shdsc_hmac(
533c5788a10SHemant Agrawal 						cdb->sh_desc, true,
534c5788a10SHemant Agrawal 						swap, SHR_NEVER, &alginfo_a,
535c5788a10SHemant Agrawal 						!ses->dir,
536c5788a10SHemant Agrawal 						ses->digest_length);
537c5788a10SHemant Agrawal 			break;
538c5788a10SHemant Agrawal 		case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
539c5788a10SHemant Agrawal 			shared_desc_len = cnstr_shdsc_snow_f9(
540c5788a10SHemant Agrawal 						cdb->sh_desc, true, swap,
541c5788a10SHemant Agrawal 						&alginfo_a,
542c5788a10SHemant Agrawal 						!ses->dir,
543c5788a10SHemant Agrawal 						ses->digest_length);
544c5788a10SHemant Agrawal 			break;
545c5788a10SHemant Agrawal 		case RTE_CRYPTO_AUTH_ZUC_EIA3:
546c5788a10SHemant Agrawal 			shared_desc_len = cnstr_shdsc_zuca(
547c5788a10SHemant Agrawal 						cdb->sh_desc, true, swap,
548c5788a10SHemant Agrawal 						&alginfo_a,
549c5788a10SHemant Agrawal 						!ses->dir,
550c5788a10SHemant Agrawal 						ses->digest_length);
551c5788a10SHemant Agrawal 			break;
55266f95673SGagandeep Singh 		case RTE_CRYPTO_AUTH_AES_XCBC_MAC:
5532ed12d9bSGagandeep Singh 		case RTE_CRYPTO_AUTH_AES_CMAC:
55466f95673SGagandeep Singh 			shared_desc_len = cnstr_shdsc_aes_mac(
55566f95673SGagandeep Singh 						cdb->sh_desc,
55666f95673SGagandeep Singh 						true, swap, SHR_NEVER,
55766f95673SGagandeep Singh 						&alginfo_a,
55866f95673SGagandeep Singh 						!ses->dir,
55966f95673SGagandeep Singh 						ses->digest_length);
56066f95673SGagandeep Singh 			break;
561c5788a10SHemant Agrawal 		default:
56277a9b5adSHemant Agrawal 			DPAA_SEC_ERR("unsupported auth alg %s (%u)",
56377a9b5adSHemant Agrawal 				rte_cryptodev_get_auth_algo_string(ses->auth_alg),
56477a9b5adSHemant Agrawal 				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 	default:
64477a9b5adSHemant Agrawal 		DPAA_SEC_ERR("error: Unsupported session %d", ses->ctxt);
6458524b44eSHemant Agrawal 		return -ENOTSUP;
646c3e85bdcSAkhil Goyal 	}
64722788c2cSSunil Kumar Kori 
64822788c2cSSunil Kumar Kori 	if (shared_desc_len < 0) {
649f163231eSHemant Agrawal 		DPAA_SEC_ERR("error in preparing command block");
65022788c2cSSunil Kumar Kori 		return shared_desc_len;
65122788c2cSSunil Kumar Kori 	}
65222788c2cSSunil Kumar Kori 
653c3e85bdcSAkhil Goyal 	cdb->sh_hdr.hi.field.idlen = shared_desc_len;
654c3e85bdcSAkhil Goyal 	cdb->sh_hdr.hi.word = rte_cpu_to_be_32(cdb->sh_hdr.hi.word);
655c3e85bdcSAkhil Goyal 	cdb->sh_hdr.lo.word = rte_cpu_to_be_32(cdb->sh_hdr.lo.word);
656c3e85bdcSAkhil Goyal 
657c3e85bdcSAkhil Goyal 	return 0;
658c3e85bdcSAkhil Goyal }
659c3e85bdcSAkhil Goyal 
660b1bbf222SGagandeep Singh static void
661a8794e39SHemant Agrawal dpaa_sec_dump(struct dpaa_sec_op_ctx *ctx, struct dpaa_sec_qp *qp, FILE *f)
662b1bbf222SGagandeep Singh {
663b1bbf222SGagandeep Singh 	struct dpaa_sec_job *job = &ctx->job;
664b1bbf222SGagandeep Singh 	struct rte_crypto_op *op = ctx->op;
665b1bbf222SGagandeep Singh 	dpaa_sec_session *sess = NULL;
666b1bbf222SGagandeep Singh 	struct sec_cdb c_cdb, *cdb;
667b1bbf222SGagandeep Singh 	uint8_t bufsize;
668b1bbf222SGagandeep Singh 	struct rte_crypto_sym_op *sym_op;
669b1bbf222SGagandeep Singh 	struct qm_sg_entry sg[2];
670b1bbf222SGagandeep Singh 
671b1bbf222SGagandeep Singh 	if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION)
6722a440d6aSAkhil Goyal 		sess = CRYPTODEV_GET_SYM_SESS_PRIV(op->sym->session);
6734331f814SDavid Marchand #ifdef RTE_LIB_SECURITY
674b1bbf222SGagandeep Singh 	else if (op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION)
6752973dbf9SAkhil Goyal 		sess = SECURITY_GET_SESS_PRIV(op->sym->session);
676b1bbf222SGagandeep Singh #endif
677b1bbf222SGagandeep Singh 	if (sess == NULL) {
678b1bbf222SGagandeep Singh 		printf("session is NULL\n");
679b1bbf222SGagandeep Singh 		goto mbuf_dump;
680b1bbf222SGagandeep Singh 	}
681b1bbf222SGagandeep Singh 
682b1bbf222SGagandeep Singh 	cdb = &sess->cdb;
683b1bbf222SGagandeep Singh 	rte_memcpy(&c_cdb, cdb, sizeof(struct sec_cdb));
6844331f814SDavid Marchand #ifdef RTE_LIB_SECURITY
685a8794e39SHemant Agrawal 	fprintf(f, "\nsession protocol type = %d\n", sess->proto_alg);
686b1bbf222SGagandeep Singh #endif
687a8794e39SHemant Agrawal 	fprintf(f, "\n****************************************\n"
688b1bbf222SGagandeep Singh 		"session params:\n\tContext type:\t%d\n\tDirection:\t%s\n"
689b1bbf222SGagandeep Singh 		"\tCipher alg:\t%d\n\tAuth alg:\t%d\n\tAead alg:\t%d\n"
690b1bbf222SGagandeep Singh 		"\tCipher key len:\t%"PRIu64"\n\tCipher alg:\t%d\n"
691b1bbf222SGagandeep Singh 		"\tCipher algmode:\t%d\n", sess->ctxt,
692b1bbf222SGagandeep Singh 		(sess->dir == DIR_ENC) ? "DIR_ENC" : "DIR_DEC",
693b1bbf222SGagandeep Singh 		sess->cipher_alg, sess->auth_alg, sess->aead_alg,
694b1bbf222SGagandeep Singh 		(uint64_t)sess->cipher_key.length, sess->cipher_key.alg,
695b1bbf222SGagandeep Singh 		sess->cipher_key.algmode);
696a8794e39SHemant Agrawal 		rte_hexdump(f, "cipher key", sess->cipher_key.data,
697b1bbf222SGagandeep Singh 				sess->cipher_key.length);
698a8794e39SHemant Agrawal 		rte_hexdump(f, "auth key", sess->auth_key.data,
699b1bbf222SGagandeep Singh 				sess->auth_key.length);
700a8794e39SHemant Agrawal 	fprintf(f, "\tAuth key len:\t%"PRIu64"\n\tAuth alg:\t%d\n"
701b1bbf222SGagandeep Singh 		"\tAuth algmode:\t%d\n\tIV len:\t\t%d\n\tIV offset:\t%d\n"
702b1bbf222SGagandeep Singh 		"\tdigest length:\t%d\n\tauth only len:\t\t%d\n"
703b1bbf222SGagandeep Singh 		"\taead cipher text:\t%d\n",
704b1bbf222SGagandeep Singh 		(uint64_t)sess->auth_key.length, sess->auth_key.alg,
705b1bbf222SGagandeep Singh 		sess->auth_key.algmode,
706b1bbf222SGagandeep Singh 		sess->iv.length, sess->iv.offset,
707b1bbf222SGagandeep Singh 		sess->digest_length, sess->auth_only_len,
708b1bbf222SGagandeep Singh 		sess->auth_cipher_text);
7094331f814SDavid Marchand #ifdef RTE_LIB_SECURITY
710a8794e39SHemant Agrawal 	fprintf(f, "PDCP session params:\n"
711b1bbf222SGagandeep Singh 		"\tDomain:\t\t%d\n\tBearer:\t\t%d\n\tpkt_dir:\t%d\n\thfn_ovd:"
712b1bbf222SGagandeep Singh 		"\t%d\n\tsn_size:\t%d\n\tsdap_enabled:\t%d\n\thfn_ovd_offset:"
713b1bbf222SGagandeep Singh 		"\t%d\n\thfn:\t\t%d\n"
714b1bbf222SGagandeep Singh 		"\thfn_threshold:\t0x%x\n", sess->pdcp.domain,
715b1bbf222SGagandeep Singh 		sess->pdcp.bearer, sess->pdcp.pkt_dir, sess->pdcp.hfn_ovd,
716b1bbf222SGagandeep Singh 		sess->pdcp.sn_size, sess->pdcp.sdap_enabled,
717b1bbf222SGagandeep Singh 		sess->pdcp.hfn_ovd_offset, sess->pdcp.hfn,
718b1bbf222SGagandeep Singh 		sess->pdcp.hfn_threshold);
719b1bbf222SGagandeep Singh #endif
720b1bbf222SGagandeep Singh 	c_cdb.sh_hdr.hi.word = rte_be_to_cpu_32(c_cdb.sh_hdr.hi.word);
721b1bbf222SGagandeep Singh 	c_cdb.sh_hdr.lo.word = rte_be_to_cpu_32(c_cdb.sh_hdr.lo.word);
722b1bbf222SGagandeep Singh 	bufsize = c_cdb.sh_hdr.hi.field.idlen;
723b1bbf222SGagandeep Singh 
724a8794e39SHemant Agrawal 	fprintf(f, "cdb = %p\n\n", cdb);
725a8794e39SHemant Agrawal 	fprintf(f, "Descriptor size = %d\n", bufsize);
726b1bbf222SGagandeep Singh 	int m;
727b1bbf222SGagandeep Singh 	for (m = 0; m < bufsize; m++)
728a8794e39SHemant Agrawal 		fprintf(f, "0x%x\n", rte_be_to_cpu_32(c_cdb.sh_desc[m]));
729b1bbf222SGagandeep Singh 
730a8794e39SHemant Agrawal 	fprintf(f, "\n");
731b1bbf222SGagandeep Singh mbuf_dump:
732b1bbf222SGagandeep Singh 	sym_op = op->sym;
733b1bbf222SGagandeep Singh 	if (sym_op->m_src) {
734a8794e39SHemant Agrawal 		fprintf(f, "Source mbuf:\n");
735a8794e39SHemant Agrawal 		rte_pktmbuf_dump(f, sym_op->m_src,
736b1bbf222SGagandeep Singh 				 sym_op->m_src->data_len);
737b1bbf222SGagandeep Singh 	}
738b1bbf222SGagandeep Singh 	if (sym_op->m_dst) {
739a8794e39SHemant Agrawal 		fprintf(f, "Destination mbuf:\n");
740a8794e39SHemant Agrawal 		rte_pktmbuf_dump(f, sym_op->m_dst,
741b1bbf222SGagandeep Singh 				 sym_op->m_dst->data_len);
742b1bbf222SGagandeep Singh 	}
743b1bbf222SGagandeep Singh 
744a8794e39SHemant Agrawal 	fprintf(f, "Session address = %p\ncipher offset: %d, length: %d\n"
745b1bbf222SGagandeep Singh 		"auth offset: %d, length:  %d\n aead offset: %d, length: %d\n",
746b1bbf222SGagandeep Singh 		sym_op->session, sym_op->cipher.data.offset,
747b1bbf222SGagandeep Singh 		sym_op->cipher.data.length,
748b1bbf222SGagandeep Singh 		sym_op->auth.data.offset, sym_op->auth.data.length,
749b1bbf222SGagandeep Singh 		sym_op->aead.data.offset, sym_op->aead.data.length);
750a8794e39SHemant Agrawal 	fprintf(f, "\n");
751b1bbf222SGagandeep Singh 
752a8794e39SHemant Agrawal 	fprintf(f, "******************************************************\n");
753a8794e39SHemant Agrawal 	fprintf(f, "ctx info:\n");
754a8794e39SHemant Agrawal 	fprintf(f, "job->sg[0] output info:\n");
755b1bbf222SGagandeep Singh 	memcpy(&sg[0], &job->sg[0], sizeof(sg[0]));
756a8794e39SHemant Agrawal 	fprintf(f, "\taddr = %"PRIx64",\n\tlen = %d,\n\tfinal = %d,\n\textension = %d"
757b1bbf222SGagandeep Singh 		"\n\tbpid = %d\n\toffset = %d\n",
758b1bbf222SGagandeep Singh 		(uint64_t)sg[0].addr, sg[0].length, sg[0].final,
759b1bbf222SGagandeep Singh 		sg[0].extension, sg[0].bpid, sg[0].offset);
760a8794e39SHemant Agrawal 	fprintf(f, "\njob->sg[1] input info:\n");
761b1bbf222SGagandeep Singh 	memcpy(&sg[1], &job->sg[1], sizeof(sg[1]));
762b1bbf222SGagandeep Singh 	hw_sg_to_cpu(&sg[1]);
763a8794e39SHemant Agrawal 	fprintf(f, "\taddr = %"PRIx64",\n\tlen = %d,\n\tfinal = %d,\n\textension = %d"
764b1bbf222SGagandeep Singh 		"\n\tbpid = %d\n\toffset = %d\n",
765b1bbf222SGagandeep Singh 		(uint64_t)sg[1].addr, sg[1].length, sg[1].final,
766b1bbf222SGagandeep Singh 		sg[1].extension, sg[1].bpid, sg[1].offset);
767b1bbf222SGagandeep Singh 
768a8794e39SHemant Agrawal 	fprintf(f, "\nctx pool addr = %p\n", ctx->ctx_pool);
769b1bbf222SGagandeep Singh 	if (ctx->ctx_pool)
770a8794e39SHemant Agrawal 		fprintf(f, "ctx pool available counts = %d\n",
771b1bbf222SGagandeep Singh 			rte_mempool_avail_count(ctx->ctx_pool));
772b1bbf222SGagandeep Singh 
773a8794e39SHemant Agrawal 	fprintf(f, "\nop pool addr = %p\n", op->mempool);
774b1bbf222SGagandeep Singh 	if (op->mempool)
775a8794e39SHemant Agrawal 		fprintf(f, "op pool available counts = %d\n",
776b1bbf222SGagandeep Singh 			rte_mempool_avail_count(op->mempool));
777b1bbf222SGagandeep Singh 
778a8794e39SHemant Agrawal 	fprintf(f, "********************************************************\n");
779a8794e39SHemant Agrawal 	fprintf(f, "Queue data:\n");
780a8794e39SHemant Agrawal 	fprintf(f, "\tFQID = 0x%x\n\tstate = %d\n\tnb_desc = %d\n"
781b1bbf222SGagandeep Singh 		"\tctx_pool = %p\n\trx_pkts = %d\n\ttx_pkts"
782b1bbf222SGagandeep Singh 	       "= %d\n\trx_errs = %d\n\ttx_errs = %d\n\n",
783b1bbf222SGagandeep Singh 		qp->outq.fqid, qp->outq.state, qp->outq.nb_desc,
784b1bbf222SGagandeep Singh 		qp->ctx_pool, qp->rx_pkts, qp->tx_pkts,
785b1bbf222SGagandeep Singh 		qp->rx_errs, qp->tx_errs);
786b1bbf222SGagandeep Singh }
787b1bbf222SGagandeep Singh 
788c3e85bdcSAkhil Goyal /* qp is lockless, should be accessed by only one thread */
789c3e85bdcSAkhil Goyal static int
790c3e85bdcSAkhil Goyal dpaa_sec_deq(struct dpaa_sec_qp *qp, struct rte_crypto_op **ops, int nb_ops)
791c3e85bdcSAkhil Goyal {
792c3e85bdcSAkhil Goyal 	struct qman_fq *fq;
7939a984458SAkhil Goyal 	unsigned int pkts = 0;
794f40d5a53SNipun Gupta 	int num_rx_bufs, ret;
7959a984458SAkhil Goyal 	struct qm_dqrr_entry *dq;
796f40d5a53SNipun Gupta 	uint32_t vdqcr_flags = 0;
797c3e85bdcSAkhil Goyal 
798c3e85bdcSAkhil Goyal 	fq = &qp->outq;
799f40d5a53SNipun Gupta 	/*
800f40d5a53SNipun Gupta 	 * Until request for four buffers, we provide exact number of buffers.
801f40d5a53SNipun Gupta 	 * Otherwise we do not set the QM_VDQCR_EXACT flag.
802f40d5a53SNipun Gupta 	 * Not setting QM_VDQCR_EXACT flag can provide two more buffers than
803f40d5a53SNipun Gupta 	 * requested, so we request two less in this case.
804f40d5a53SNipun Gupta 	 */
805f40d5a53SNipun Gupta 	if (nb_ops < 4) {
806f40d5a53SNipun Gupta 		vdqcr_flags = QM_VDQCR_EXACT;
807f40d5a53SNipun Gupta 		num_rx_bufs = nb_ops;
808f40d5a53SNipun Gupta 	} else {
809f40d5a53SNipun Gupta 		num_rx_bufs = nb_ops > DPAA_MAX_DEQUEUE_NUM_FRAMES ?
810f40d5a53SNipun Gupta 			(DPAA_MAX_DEQUEUE_NUM_FRAMES - 2) : (nb_ops - 2);
811f40d5a53SNipun Gupta 	}
812f40d5a53SNipun Gupta 	ret = qman_set_vdq(fq, num_rx_bufs, vdqcr_flags);
8139a984458SAkhil Goyal 	if (ret)
8149a984458SAkhil Goyal 		return 0;
815c3e85bdcSAkhil Goyal 
8169a984458SAkhil Goyal 	do {
8179a984458SAkhil Goyal 		const struct qm_fd *fd;
8189a984458SAkhil Goyal 		struct dpaa_sec_job *job;
8199a984458SAkhil Goyal 		struct dpaa_sec_op_ctx *ctx;
8209a984458SAkhil Goyal 		struct rte_crypto_op *op;
821c3e85bdcSAkhil Goyal 
8229a984458SAkhil Goyal 		dq = qman_dequeue(fq);
8239a984458SAkhil Goyal 		if (!dq)
8249a984458SAkhil Goyal 			continue;
8259a984458SAkhil Goyal 
8269a984458SAkhil Goyal 		fd = &dq->fd;
8279a984458SAkhil Goyal 		/* sg is embedded in an op ctx,
8289a984458SAkhil Goyal 		 * sg[0] is for output
8299a984458SAkhil Goyal 		 * sg[1] for input
8309a984458SAkhil Goyal 		 */
831ec861560SGagandeep Singh 		job = rte_dpaa_mem_ptov(qm_fd_addr_get64(fd));
8329a984458SAkhil Goyal 
8339a984458SAkhil Goyal 		ctx = container_of(job, struct dpaa_sec_op_ctx, job);
8349a984458SAkhil Goyal 		ctx->fd_status = fd->status;
8359a984458SAkhil Goyal 		op = ctx->op;
8369a984458SAkhil Goyal 		if (op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
8379a984458SAkhil Goyal 			struct qm_sg_entry *sg_out;
8389a984458SAkhil Goyal 			uint32_t len;
839fb5c100aSAkhil Goyal 			struct rte_mbuf *mbuf = (op->sym->m_dst == NULL) ?
840fb5c100aSAkhil Goyal 						op->sym->m_src : op->sym->m_dst;
8419a984458SAkhil Goyal 
8429a984458SAkhil Goyal 			sg_out = &job->sg[0];
8439a984458SAkhil Goyal 			hw_sg_to_cpu(sg_out);
8449a984458SAkhil Goyal 			len = sg_out->length;
845fb5c100aSAkhil Goyal 			mbuf->pkt_len = len;
846fb5c100aSAkhil Goyal 			while (mbuf->next != NULL) {
847fb5c100aSAkhil Goyal 				len -= mbuf->data_len;
848fb5c100aSAkhil Goyal 				mbuf = mbuf->next;
849fb5c100aSAkhil Goyal 			}
850fb5c100aSAkhil Goyal 			mbuf->data_len = len;
8519a984458SAkhil Goyal 		}
8529a984458SAkhil Goyal 		if (!ctx->fd_status) {
8539a984458SAkhil Goyal 			op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
8549a984458SAkhil Goyal 		} else {
855b1bbf222SGagandeep Singh 			if (dpaa_sec_dp_dump > DPAA_SEC_DP_NO_DUMP) {
856f665790aSDavid Marchand 				DPAA_SEC_DP_WARN("SEC return err:0x%x",
857b1bbf222SGagandeep Singh 						  ctx->fd_status);
858b1bbf222SGagandeep Singh 				if (dpaa_sec_dp_dump > DPAA_SEC_DP_ERR_DUMP)
859a8794e39SHemant Agrawal 					dpaa_sec_dump(ctx, qp, stdout);
860b1bbf222SGagandeep Singh 			}
8619a984458SAkhil Goyal 			op->status = RTE_CRYPTO_OP_STATUS_ERROR;
8629a984458SAkhil Goyal 		}
8639a984458SAkhil Goyal 		ops[pkts++] = op;
8649a984458SAkhil Goyal 
8657be78d02SJosh Soref 		/* report op status to sym->op and then free the ctx memory */
8669a984458SAkhil Goyal 		rte_mempool_put(ctx->ctx_pool, (void *)ctx);
8679a984458SAkhil Goyal 
8689a984458SAkhil Goyal 		qman_dqrr_consume(fq, dq);
8699a984458SAkhil Goyal 	} while (fq->flags & QMAN_FQ_STATE_VDQCR);
8709a984458SAkhil Goyal 
8719a984458SAkhil Goyal 	return pkts;
872c3e85bdcSAkhil Goyal }
873c3e85bdcSAkhil Goyal 
874a74af788SAkhil Goyal static inline struct dpaa_sec_job *
875a74af788SAkhil Goyal build_auth_only_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
876a74af788SAkhil Goyal {
877a74af788SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
878a74af788SAkhil Goyal 	struct rte_mbuf *mbuf = sym->m_src;
879a74af788SAkhil Goyal 	struct dpaa_sec_job *cf;
880a74af788SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
881a74af788SAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
882a74af788SAkhil Goyal 	phys_addr_t start_addr;
883a74af788SAkhil Goyal 	uint8_t *old_digest, extra_segs;
884c5788a10SHemant Agrawal 	int data_len, data_offset;
885c5788a10SHemant Agrawal 
886c5788a10SHemant Agrawal 	data_len = sym->auth.data.length;
887c5788a10SHemant Agrawal 	data_offset = sym->auth.data.offset;
888c5788a10SHemant Agrawal 
889c5788a10SHemant Agrawal 	if (ses->auth_alg == RTE_CRYPTO_AUTH_SNOW3G_UIA2 ||
890c5788a10SHemant Agrawal 	    ses->auth_alg == RTE_CRYPTO_AUTH_ZUC_EIA3) {
891c5788a10SHemant Agrawal 		if ((data_len & 7) || (data_offset & 7)) {
892c5788a10SHemant Agrawal 			DPAA_SEC_ERR("AUTH: len/offset must be full bytes");
893c5788a10SHemant Agrawal 			return NULL;
894c5788a10SHemant Agrawal 		}
895c5788a10SHemant Agrawal 
896c5788a10SHemant Agrawal 		data_len = data_len >> 3;
897c5788a10SHemant Agrawal 		data_offset = data_offset >> 3;
898c5788a10SHemant Agrawal 	}
899a74af788SAkhil Goyal 
900a74af788SAkhil Goyal 	if (is_decode(ses))
901a74af788SAkhil Goyal 		extra_segs = 3;
902a74af788SAkhil Goyal 	else
903a74af788SAkhil Goyal 		extra_segs = 2;
904a74af788SAkhil Goyal 
905f7a5752eSHemant Agrawal 	if (mbuf->nb_segs > MAX_SG_ENTRIES) {
906f163231eSHemant Agrawal 		DPAA_SEC_DP_ERR("Auth: Max sec segs supported is %d",
907a74af788SAkhil Goyal 				MAX_SG_ENTRIES);
908a74af788SAkhil Goyal 		return NULL;
909a74af788SAkhil Goyal 	}
910f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, mbuf->nb_segs + extra_segs);
911a74af788SAkhil Goyal 	if (!ctx)
912a74af788SAkhil Goyal 		return NULL;
913a74af788SAkhil Goyal 
914a74af788SAkhil Goyal 	cf = &ctx->job;
915a74af788SAkhil Goyal 	ctx->op = op;
916a74af788SAkhil Goyal 	old_digest = ctx->digest;
917a74af788SAkhil Goyal 
918a74af788SAkhil Goyal 	/* output */
919a74af788SAkhil Goyal 	out_sg = &cf->sg[0];
920a74af788SAkhil Goyal 	qm_sg_entry_set64(out_sg, sym->auth.digest.phys_addr);
921a74af788SAkhil Goyal 	out_sg->length = ses->digest_length;
922a74af788SAkhil Goyal 	cpu_to_hw_sg(out_sg);
923a74af788SAkhil Goyal 
924a74af788SAkhil Goyal 	/* input */
925a74af788SAkhil Goyal 	in_sg = &cf->sg[1];
926a74af788SAkhil Goyal 	/* need to extend the input to a compound frame */
927a74af788SAkhil Goyal 	in_sg->extension = 1;
928a74af788SAkhil Goyal 	in_sg->final = 1;
929c5788a10SHemant Agrawal 	in_sg->length = data_len;
930ec861560SGagandeep Singh 	qm_sg_entry_set64(in_sg, rte_dpaa_mem_vtop(&cf->sg[2]));
931a74af788SAkhil Goyal 
932a74af788SAkhil Goyal 	/* 1st seg */
933a74af788SAkhil Goyal 	sg = in_sg + 1;
934a74af788SAkhil Goyal 
935c5788a10SHemant Agrawal 	if (ses->iv.length) {
936c5788a10SHemant Agrawal 		uint8_t *iv_ptr;
937c5788a10SHemant Agrawal 
938c5788a10SHemant Agrawal 		iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
939c5788a10SHemant Agrawal 						   ses->iv.offset);
940c5788a10SHemant Agrawal 
941c5788a10SHemant Agrawal 		if (ses->auth_alg == RTE_CRYPTO_AUTH_SNOW3G_UIA2) {
942c5788a10SHemant Agrawal 			iv_ptr = conv_to_snow_f9_iv(iv_ptr);
943c5788a10SHemant Agrawal 			sg->length = 12;
944c5788a10SHemant Agrawal 		} else if (ses->auth_alg == RTE_CRYPTO_AUTH_ZUC_EIA3) {
945c5788a10SHemant Agrawal 			iv_ptr = conv_to_zuc_eia_iv(iv_ptr);
946c5788a10SHemant Agrawal 			sg->length = 8;
947c5788a10SHemant Agrawal 		} else {
948c5788a10SHemant Agrawal 			sg->length = ses->iv.length;
949c5788a10SHemant Agrawal 		}
950ec861560SGagandeep Singh 		qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(iv_ptr));
951c5788a10SHemant Agrawal 		in_sg->length += sg->length;
952c5788a10SHemant Agrawal 		cpu_to_hw_sg(sg);
953c5788a10SHemant Agrawal 		sg++;
954c5788a10SHemant Agrawal 	}
955c5788a10SHemant Agrawal 
956ce627d63SThomas Monjalon 	qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
957c5788a10SHemant Agrawal 	sg->offset = data_offset;
958c5788a10SHemant Agrawal 
959c5788a10SHemant Agrawal 	if (data_len <= (mbuf->data_len - data_offset)) {
960c5788a10SHemant Agrawal 		sg->length = data_len;
961c5788a10SHemant Agrawal 	} else {
962c5788a10SHemant Agrawal 		sg->length = mbuf->data_len - data_offset;
963c5788a10SHemant Agrawal 
964c5788a10SHemant Agrawal 		/* remaining i/p segs */
965c5788a10SHemant Agrawal 		while ((data_len = data_len - sg->length) &&
966c5788a10SHemant Agrawal 		       (mbuf = mbuf->next)) {
967a74af788SAkhil Goyal 			cpu_to_hw_sg(sg);
968a74af788SAkhil Goyal 			sg++;
969ce627d63SThomas Monjalon 			qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
970c5788a10SHemant Agrawal 			if (data_len > mbuf->data_len)
971a74af788SAkhil Goyal 				sg->length = mbuf->data_len;
972c5788a10SHemant Agrawal 			else
973c5788a10SHemant Agrawal 				sg->length = data_len;
974c5788a10SHemant Agrawal 		}
975a74af788SAkhil Goyal 	}
976a74af788SAkhil Goyal 
977a74af788SAkhil Goyal 	if (is_decode(ses)) {
978a74af788SAkhil Goyal 		/* Digest verification case */
979a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
980a74af788SAkhil Goyal 		sg++;
981a74af788SAkhil Goyal 		rte_memcpy(old_digest, sym->auth.digest.data,
982a74af788SAkhil Goyal 				ses->digest_length);
983ec861560SGagandeep Singh 		start_addr = rte_dpaa_mem_vtop(old_digest);
984a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, start_addr);
985a74af788SAkhil Goyal 		sg->length = ses->digest_length;
986a74af788SAkhil Goyal 		in_sg->length += ses->digest_length;
987a74af788SAkhil Goyal 	}
988a74af788SAkhil Goyal 	sg->final = 1;
989a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
990a74af788SAkhil Goyal 	cpu_to_hw_sg(in_sg);
991a74af788SAkhil Goyal 
992a74af788SAkhil Goyal 	return cf;
993a74af788SAkhil Goyal }
994a74af788SAkhil Goyal 
995c3e85bdcSAkhil Goyal /**
996c3e85bdcSAkhil Goyal  * packet looks like:
997c3e85bdcSAkhil Goyal  *		|<----data_len------->|
998c3e85bdcSAkhil Goyal  *    |ip_header|ah_header|icv|payload|
999c3e85bdcSAkhil Goyal  *              ^
1000c3e85bdcSAkhil Goyal  *		|
1001c3e85bdcSAkhil Goyal  *	   mbuf->pkt.data
1002c3e85bdcSAkhil Goyal  */
1003c3e85bdcSAkhil Goyal static inline struct dpaa_sec_job *
1004c3e85bdcSAkhil Goyal build_auth_only(struct rte_crypto_op *op, dpaa_sec_session *ses)
1005c3e85bdcSAkhil Goyal {
1006c3e85bdcSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1007c3e85bdcSAkhil Goyal 	struct rte_mbuf *mbuf = sym->m_src;
1008c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
1009c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1010c5788a10SHemant Agrawal 	struct qm_sg_entry *sg, *in_sg;
1011c4509373SSantosh Shukla 	rte_iova_t start_addr;
1012c3e85bdcSAkhil Goyal 	uint8_t *old_digest;
1013c5788a10SHemant Agrawal 	int data_len, data_offset;
1014c5788a10SHemant Agrawal 
1015c5788a10SHemant Agrawal 	data_len = sym->auth.data.length;
1016c5788a10SHemant Agrawal 	data_offset = sym->auth.data.offset;
1017c5788a10SHemant Agrawal 
1018c5788a10SHemant Agrawal 	if (ses->auth_alg == RTE_CRYPTO_AUTH_SNOW3G_UIA2 ||
1019c5788a10SHemant Agrawal 	    ses->auth_alg == RTE_CRYPTO_AUTH_ZUC_EIA3) {
1020c5788a10SHemant Agrawal 		if ((data_len & 7) || (data_offset & 7)) {
1021c5788a10SHemant Agrawal 			DPAA_SEC_ERR("AUTH: len/offset must be full bytes");
1022c5788a10SHemant Agrawal 			return NULL;
1023c5788a10SHemant Agrawal 		}
1024c5788a10SHemant Agrawal 
1025c5788a10SHemant Agrawal 		data_len = data_len >> 3;
1026c5788a10SHemant Agrawal 		data_offset = data_offset >> 3;
1027c5788a10SHemant Agrawal 	}
1028c3e85bdcSAkhil Goyal 
1029f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, 4);
1030c3e85bdcSAkhil Goyal 	if (!ctx)
1031c3e85bdcSAkhil Goyal 		return NULL;
1032c3e85bdcSAkhil Goyal 
1033c3e85bdcSAkhil Goyal 	cf = &ctx->job;
1034c3e85bdcSAkhil Goyal 	ctx->op = op;
1035c3e85bdcSAkhil Goyal 	old_digest = ctx->digest;
1036c3e85bdcSAkhil Goyal 
1037bfa9a8a4SThomas Monjalon 	start_addr = rte_pktmbuf_iova(mbuf);
1038c3e85bdcSAkhil Goyal 	/* output */
1039c3e85bdcSAkhil Goyal 	sg = &cf->sg[0];
1040c3e85bdcSAkhil Goyal 	qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
1041c3e85bdcSAkhil Goyal 	sg->length = ses->digest_length;
1042c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1043c3e85bdcSAkhil Goyal 
1044c3e85bdcSAkhil Goyal 	/* input */
1045c5788a10SHemant Agrawal 	in_sg = &cf->sg[1];
1046c3e85bdcSAkhil Goyal 	/* need to extend the input to a compound frame */
1047c5788a10SHemant Agrawal 	in_sg->extension = 1;
1048c5788a10SHemant Agrawal 	in_sg->final = 1;
1049c5788a10SHemant Agrawal 	in_sg->length = data_len;
1050ec861560SGagandeep Singh 	qm_sg_entry_set64(in_sg, rte_dpaa_mem_vtop(&cf->sg[2]));
1051c3e85bdcSAkhil Goyal 	sg = &cf->sg[2];
1052c5788a10SHemant Agrawal 
1053c5788a10SHemant Agrawal 	if (ses->iv.length) {
1054c5788a10SHemant Agrawal 		uint8_t *iv_ptr;
1055c5788a10SHemant Agrawal 
1056c5788a10SHemant Agrawal 		iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1057c5788a10SHemant Agrawal 						   ses->iv.offset);
1058c5788a10SHemant Agrawal 
1059c5788a10SHemant Agrawal 		if (ses->auth_alg == RTE_CRYPTO_AUTH_SNOW3G_UIA2) {
1060c5788a10SHemant Agrawal 			iv_ptr = conv_to_snow_f9_iv(iv_ptr);
1061c5788a10SHemant Agrawal 			sg->length = 12;
1062c5788a10SHemant Agrawal 		} else if (ses->auth_alg == RTE_CRYPTO_AUTH_ZUC_EIA3) {
1063c5788a10SHemant Agrawal 			iv_ptr = conv_to_zuc_eia_iv(iv_ptr);
1064c5788a10SHemant Agrawal 			sg->length = 8;
1065c5788a10SHemant Agrawal 		} else {
1066c5788a10SHemant Agrawal 			sg->length = ses->iv.length;
1067c5788a10SHemant Agrawal 		}
1068ec861560SGagandeep Singh 		qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(iv_ptr));
1069c5788a10SHemant Agrawal 		in_sg->length += sg->length;
1070c5788a10SHemant Agrawal 		cpu_to_hw_sg(sg);
1071c5788a10SHemant Agrawal 		sg++;
1072c5788a10SHemant Agrawal 	}
1073c5788a10SHemant Agrawal 
1074ce627d63SThomas Monjalon 	qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1075c5788a10SHemant Agrawal 	sg->offset = data_offset;
1076c5788a10SHemant Agrawal 	sg->length = data_len;
1077c5788a10SHemant Agrawal 
1078c5788a10SHemant Agrawal 	if (is_decode(ses)) {
1079c5788a10SHemant Agrawal 		/* Digest verification case */
1080c5788a10SHemant Agrawal 		cpu_to_hw_sg(sg);
1081c3e85bdcSAkhil Goyal 		/* hash result or digest, save digest first */
1082c3e85bdcSAkhil Goyal 		rte_memcpy(old_digest, sym->auth.digest.data,
1083c3e85bdcSAkhil Goyal 				ses->digest_length);
1084c3e85bdcSAkhil Goyal 		/* let's check digest by hw */
1085ec861560SGagandeep Singh 		start_addr = rte_dpaa_mem_vtop(old_digest);
1086c3e85bdcSAkhil Goyal 		sg++;
1087c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, start_addr);
1088c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
1089c5788a10SHemant Agrawal 		in_sg->length += ses->digest_length;
1090c3e85bdcSAkhil Goyal 	}
1091c5788a10SHemant Agrawal 	sg->final = 1;
1092c5788a10SHemant Agrawal 	cpu_to_hw_sg(sg);
1093c5788a10SHemant Agrawal 	cpu_to_hw_sg(in_sg);
1094c3e85bdcSAkhil Goyal 
1095c3e85bdcSAkhil Goyal 	return cf;
1096c3e85bdcSAkhil Goyal }
1097c3e85bdcSAkhil Goyal 
1098c3e85bdcSAkhil Goyal static inline struct dpaa_sec_job *
1099a74af788SAkhil Goyal build_cipher_only_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
1100a74af788SAkhil Goyal {
1101a74af788SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1102a74af788SAkhil Goyal 	struct dpaa_sec_job *cf;
1103a74af788SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1104a74af788SAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
1105a74af788SAkhil Goyal 	struct rte_mbuf *mbuf;
1106a74af788SAkhil Goyal 	uint8_t req_segs;
1107a74af788SAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1108a74af788SAkhil Goyal 			ses->iv.offset);
1109c5788a10SHemant Agrawal 	int data_len, data_offset;
1110c5788a10SHemant Agrawal 
1111c5788a10SHemant Agrawal 	data_len = sym->cipher.data.length;
1112c5788a10SHemant Agrawal 	data_offset = sym->cipher.data.offset;
1113c5788a10SHemant Agrawal 
1114c5788a10SHemant Agrawal 	if (ses->cipher_alg == RTE_CRYPTO_CIPHER_SNOW3G_UEA2 ||
1115c5788a10SHemant Agrawal 		ses->cipher_alg == RTE_CRYPTO_CIPHER_ZUC_EEA3) {
1116c5788a10SHemant Agrawal 		if ((data_len & 7) || (data_offset & 7)) {
1117c5788a10SHemant Agrawal 			DPAA_SEC_ERR("CIPHER: len/offset must be full bytes");
1118c5788a10SHemant Agrawal 			return NULL;
1119c5788a10SHemant Agrawal 		}
1120c5788a10SHemant Agrawal 
1121c5788a10SHemant Agrawal 		data_len = data_len >> 3;
1122c5788a10SHemant Agrawal 		data_offset = data_offset >> 3;
1123c5788a10SHemant Agrawal 	}
1124a74af788SAkhil Goyal 
1125a74af788SAkhil Goyal 	if (sym->m_dst) {
1126a74af788SAkhil Goyal 		mbuf = sym->m_dst;
1127a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 3;
1128a74af788SAkhil Goyal 	} else {
1129a74af788SAkhil Goyal 		mbuf = sym->m_src;
1130a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs * 2 + 3;
1131a74af788SAkhil Goyal 	}
1132f7a5752eSHemant Agrawal 	if (mbuf->nb_segs > MAX_SG_ENTRIES) {
1133f163231eSHemant Agrawal 		DPAA_SEC_DP_ERR("Cipher: Max sec segs supported is %d",
1134a74af788SAkhil Goyal 				MAX_SG_ENTRIES);
1135a74af788SAkhil Goyal 		return NULL;
1136a74af788SAkhil Goyal 	}
1137a74af788SAkhil Goyal 
1138f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, req_segs);
1139a74af788SAkhil Goyal 	if (!ctx)
1140a74af788SAkhil Goyal 		return NULL;
1141a74af788SAkhil Goyal 
1142a74af788SAkhil Goyal 	cf = &ctx->job;
1143a74af788SAkhil Goyal 	ctx->op = op;
1144a74af788SAkhil Goyal 
1145a74af788SAkhil Goyal 	/* output */
1146a74af788SAkhil Goyal 	out_sg = &cf->sg[0];
1147a74af788SAkhil Goyal 	out_sg->extension = 1;
1148c5788a10SHemant Agrawal 	out_sg->length = data_len;
1149ec861560SGagandeep Singh 	qm_sg_entry_set64(out_sg, rte_dpaa_mem_vtop(&cf->sg[2]));
1150a74af788SAkhil Goyal 	cpu_to_hw_sg(out_sg);
1151a74af788SAkhil Goyal 
1152a74af788SAkhil Goyal 	/* 1st seg */
1153a74af788SAkhil Goyal 	sg = &cf->sg[2];
1154ce627d63SThomas Monjalon 	qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1155c5788a10SHemant Agrawal 	sg->length = mbuf->data_len - data_offset;
1156c5788a10SHemant Agrawal 	sg->offset = data_offset;
1157a74af788SAkhil Goyal 
1158a74af788SAkhil Goyal 	/* Successive segs */
1159a74af788SAkhil Goyal 	mbuf = mbuf->next;
1160a74af788SAkhil Goyal 	while (mbuf) {
1161a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1162a74af788SAkhil Goyal 		sg++;
1163ce627d63SThomas Monjalon 		qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1164a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1165a74af788SAkhil Goyal 		mbuf = mbuf->next;
1166a74af788SAkhil Goyal 	}
1167a74af788SAkhil Goyal 	sg->final = 1;
1168a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1169a74af788SAkhil Goyal 
1170a74af788SAkhil Goyal 	/* input */
1171a74af788SAkhil Goyal 	mbuf = sym->m_src;
1172a74af788SAkhil Goyal 	in_sg = &cf->sg[1];
1173a74af788SAkhil Goyal 	in_sg->extension = 1;
1174a74af788SAkhil Goyal 	in_sg->final = 1;
1175c5788a10SHemant Agrawal 	in_sg->length = data_len + ses->iv.length;
1176a74af788SAkhil Goyal 
1177a74af788SAkhil Goyal 	sg++;
1178ec861560SGagandeep Singh 	qm_sg_entry_set64(in_sg, rte_dpaa_mem_vtop(sg));
1179a74af788SAkhil Goyal 	cpu_to_hw_sg(in_sg);
1180a74af788SAkhil Goyal 
1181a74af788SAkhil Goyal 	/* IV */
1182ec861560SGagandeep Singh 	qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(IV_ptr));
1183a74af788SAkhil Goyal 	sg->length = ses->iv.length;
1184a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1185a74af788SAkhil Goyal 
1186a74af788SAkhil Goyal 	/* 1st seg */
1187a74af788SAkhil Goyal 	sg++;
1188ce627d63SThomas Monjalon 	qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1189c5788a10SHemant Agrawal 	sg->length = mbuf->data_len - data_offset;
1190c5788a10SHemant Agrawal 	sg->offset = data_offset;
1191a74af788SAkhil Goyal 
1192a74af788SAkhil Goyal 	/* Successive segs */
1193a74af788SAkhil Goyal 	mbuf = mbuf->next;
1194a74af788SAkhil Goyal 	while (mbuf) {
1195a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1196a74af788SAkhil Goyal 		sg++;
1197ce627d63SThomas Monjalon 		qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1198a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1199a74af788SAkhil Goyal 		mbuf = mbuf->next;
1200a74af788SAkhil Goyal 	}
1201a74af788SAkhil Goyal 	sg->final = 1;
1202a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1203a74af788SAkhil Goyal 
1204a74af788SAkhil Goyal 	return cf;
1205a74af788SAkhil Goyal }
1206a74af788SAkhil Goyal 
1207a74af788SAkhil Goyal static inline struct dpaa_sec_job *
1208c3e85bdcSAkhil Goyal build_cipher_only(struct rte_crypto_op *op, dpaa_sec_session *ses)
1209c3e85bdcSAkhil Goyal {
1210c3e85bdcSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1211c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
1212c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1213c3e85bdcSAkhil Goyal 	struct qm_sg_entry *sg;
1214c4509373SSantosh Shukla 	rte_iova_t src_start_addr, dst_start_addr;
1215c3e85bdcSAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1216c3e85bdcSAkhil Goyal 			ses->iv.offset);
1217c5788a10SHemant Agrawal 	int data_len, data_offset;
1218c5788a10SHemant Agrawal 
1219c5788a10SHemant Agrawal 	data_len = sym->cipher.data.length;
1220c5788a10SHemant Agrawal 	data_offset = sym->cipher.data.offset;
1221c5788a10SHemant Agrawal 
1222c5788a10SHemant Agrawal 	if (ses->cipher_alg == RTE_CRYPTO_CIPHER_SNOW3G_UEA2 ||
1223c5788a10SHemant Agrawal 		ses->cipher_alg == RTE_CRYPTO_CIPHER_ZUC_EEA3) {
1224c5788a10SHemant Agrawal 		if ((data_len & 7) || (data_offset & 7)) {
1225c5788a10SHemant Agrawal 			DPAA_SEC_ERR("CIPHER: len/offset must be full bytes");
1226c5788a10SHemant Agrawal 			return NULL;
1227c5788a10SHemant Agrawal 		}
1228c5788a10SHemant Agrawal 
1229c5788a10SHemant Agrawal 		data_len = data_len >> 3;
1230c5788a10SHemant Agrawal 		data_offset = data_offset >> 3;
1231c5788a10SHemant Agrawal 	}
1232c3e85bdcSAkhil Goyal 
1233f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, 4);
1234c3e85bdcSAkhil Goyal 	if (!ctx)
1235c3e85bdcSAkhil Goyal 		return NULL;
1236c3e85bdcSAkhil Goyal 
1237c3e85bdcSAkhil Goyal 	cf = &ctx->job;
1238c3e85bdcSAkhil Goyal 	ctx->op = op;
1239a389434eSAlok Makhariya 
1240bfa9a8a4SThomas Monjalon 	src_start_addr = rte_pktmbuf_iova(sym->m_src);
1241a389434eSAlok Makhariya 
1242a389434eSAlok Makhariya 	if (sym->m_dst)
1243bfa9a8a4SThomas Monjalon 		dst_start_addr = rte_pktmbuf_iova(sym->m_dst);
1244a389434eSAlok Makhariya 	else
1245a389434eSAlok Makhariya 		dst_start_addr = src_start_addr;
1246c3e85bdcSAkhil Goyal 
1247c3e85bdcSAkhil Goyal 	/* output */
1248c3e85bdcSAkhil Goyal 	sg = &cf->sg[0];
1249c5788a10SHemant Agrawal 	qm_sg_entry_set64(sg, dst_start_addr + data_offset);
1250c5788a10SHemant Agrawal 	sg->length = data_len + ses->iv.length;
1251c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1252c3e85bdcSAkhil Goyal 
1253c3e85bdcSAkhil Goyal 	/* input */
1254c3e85bdcSAkhil Goyal 	sg = &cf->sg[1];
1255c3e85bdcSAkhil Goyal 
1256c3e85bdcSAkhil Goyal 	/* need to extend the input to a compound frame */
1257c3e85bdcSAkhil Goyal 	sg->extension = 1;
1258c3e85bdcSAkhil Goyal 	sg->final = 1;
1259c5788a10SHemant Agrawal 	sg->length = data_len + ses->iv.length;
1260ec861560SGagandeep Singh 	qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(&cf->sg[2]));
1261c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1262c3e85bdcSAkhil Goyal 
1263c3e85bdcSAkhil Goyal 	sg = &cf->sg[2];
1264ec861560SGagandeep Singh 	qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(IV_ptr));
1265c3e85bdcSAkhil Goyal 	sg->length = ses->iv.length;
1266c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1267c3e85bdcSAkhil Goyal 
1268c3e85bdcSAkhil Goyal 	sg++;
1269c5788a10SHemant Agrawal 	qm_sg_entry_set64(sg, src_start_addr + data_offset);
1270c5788a10SHemant Agrawal 	sg->length = data_len;
1271c3e85bdcSAkhil Goyal 	sg->final = 1;
1272c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1273c3e85bdcSAkhil Goyal 
1274c3e85bdcSAkhil Goyal 	return cf;
1275c3e85bdcSAkhil Goyal }
1276c3e85bdcSAkhil Goyal 
1277c3e85bdcSAkhil Goyal static inline struct dpaa_sec_job *
1278a74af788SAkhil Goyal build_cipher_auth_gcm_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
1279a74af788SAkhil Goyal {
1280a74af788SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1281a74af788SAkhil Goyal 	struct dpaa_sec_job *cf;
1282a74af788SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1283a74af788SAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
1284a74af788SAkhil Goyal 	struct rte_mbuf *mbuf;
1285a74af788SAkhil Goyal 	uint8_t req_segs;
1286a74af788SAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1287a74af788SAkhil Goyal 			ses->iv.offset);
1288a74af788SAkhil Goyal 
1289a74af788SAkhil Goyal 	if (sym->m_dst) {
1290a74af788SAkhil Goyal 		mbuf = sym->m_dst;
1291a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 4;
1292a74af788SAkhil Goyal 	} else {
1293a74af788SAkhil Goyal 		mbuf = sym->m_src;
1294a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs * 2 + 4;
1295a74af788SAkhil Goyal 	}
1296a74af788SAkhil Goyal 
1297a74af788SAkhil Goyal 	if (ses->auth_only_len)
1298a74af788SAkhil Goyal 		req_segs++;
1299a74af788SAkhil Goyal 
1300f7a5752eSHemant Agrawal 	if (mbuf->nb_segs > MAX_SG_ENTRIES) {
1301f163231eSHemant Agrawal 		DPAA_SEC_DP_ERR("AEAD: Max sec segs supported is %d",
1302a74af788SAkhil Goyal 				MAX_SG_ENTRIES);
1303a74af788SAkhil Goyal 		return NULL;
1304a74af788SAkhil Goyal 	}
1305a74af788SAkhil Goyal 
1306f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, req_segs);
1307a74af788SAkhil Goyal 	if (!ctx)
1308a74af788SAkhil Goyal 		return NULL;
1309a74af788SAkhil Goyal 
1310a74af788SAkhil Goyal 	cf = &ctx->job;
1311a74af788SAkhil Goyal 	ctx->op = op;
1312a74af788SAkhil Goyal 
1313a74af788SAkhil Goyal 	rte_prefetch0(cf->sg);
1314a74af788SAkhil Goyal 
1315a74af788SAkhil Goyal 	/* output */
1316a74af788SAkhil Goyal 	out_sg = &cf->sg[0];
1317a74af788SAkhil Goyal 	out_sg->extension = 1;
1318a74af788SAkhil Goyal 	if (is_encode(ses))
13197a4a6da4SVakul Garg 		out_sg->length = sym->aead.data.length + ses->digest_length;
1320a74af788SAkhil Goyal 	else
13217a4a6da4SVakul Garg 		out_sg->length = sym->aead.data.length;
1322a74af788SAkhil Goyal 
1323a74af788SAkhil Goyal 	/* output sg entries */
1324a74af788SAkhil Goyal 	sg = &cf->sg[2];
1325ec861560SGagandeep Singh 	qm_sg_entry_set64(out_sg, rte_dpaa_mem_vtop(sg));
1326a74af788SAkhil Goyal 	cpu_to_hw_sg(out_sg);
1327a74af788SAkhil Goyal 
1328a74af788SAkhil Goyal 	/* 1st seg */
1329ce627d63SThomas Monjalon 	qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
13307a4a6da4SVakul Garg 	sg->length = mbuf->data_len - sym->aead.data.offset;
13317a4a6da4SVakul Garg 	sg->offset = sym->aead.data.offset;
1332a74af788SAkhil Goyal 
1333a74af788SAkhil Goyal 	/* Successive segs */
1334a74af788SAkhil Goyal 	mbuf = mbuf->next;
1335a74af788SAkhil Goyal 	while (mbuf) {
1336a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1337a74af788SAkhil Goyal 		sg++;
1338ce627d63SThomas Monjalon 		qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1339a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1340a74af788SAkhil Goyal 		mbuf = mbuf->next;
1341a74af788SAkhil Goyal 	}
1342a74af788SAkhil Goyal 	sg->length -= ses->digest_length;
1343a74af788SAkhil Goyal 
1344a74af788SAkhil Goyal 	if (is_encode(ses)) {
1345a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1346a74af788SAkhil Goyal 		/* set auth output */
1347a74af788SAkhil Goyal 		sg++;
1348a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, sym->aead.digest.phys_addr);
1349a74af788SAkhil Goyal 		sg->length = ses->digest_length;
1350a74af788SAkhil Goyal 	}
1351a74af788SAkhil Goyal 	sg->final = 1;
1352a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1353a74af788SAkhil Goyal 
1354a74af788SAkhil Goyal 	/* input */
1355a74af788SAkhil Goyal 	mbuf = sym->m_src;
1356a74af788SAkhil Goyal 	in_sg = &cf->sg[1];
1357a74af788SAkhil Goyal 	in_sg->extension = 1;
1358a74af788SAkhil Goyal 	in_sg->final = 1;
1359a74af788SAkhil Goyal 	if (is_encode(ses))
1360a74af788SAkhil Goyal 		in_sg->length = ses->iv.length + sym->aead.data.length
1361a74af788SAkhil Goyal 							+ ses->auth_only_len;
1362a74af788SAkhil Goyal 	else
1363a74af788SAkhil Goyal 		in_sg->length = ses->iv.length + sym->aead.data.length
1364a74af788SAkhil Goyal 				+ ses->auth_only_len + ses->digest_length;
1365a74af788SAkhil Goyal 
1366a74af788SAkhil Goyal 	/* input sg entries */
1367a74af788SAkhil Goyal 	sg++;
1368ec861560SGagandeep Singh 	qm_sg_entry_set64(in_sg, rte_dpaa_mem_vtop(sg));
1369a74af788SAkhil Goyal 	cpu_to_hw_sg(in_sg);
1370a74af788SAkhil Goyal 
1371a74af788SAkhil Goyal 	/* 1st seg IV */
1372ec861560SGagandeep Singh 	qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(IV_ptr));
1373a74af788SAkhil Goyal 	sg->length = ses->iv.length;
1374a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1375a74af788SAkhil Goyal 
1376a74af788SAkhil Goyal 	/* 2nd seg auth only */
1377a74af788SAkhil Goyal 	if (ses->auth_only_len) {
1378a74af788SAkhil Goyal 		sg++;
1379ec861560SGagandeep Singh 		qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(sym->aead.aad.data));
1380a74af788SAkhil Goyal 		sg->length = ses->auth_only_len;
1381a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1382a74af788SAkhil Goyal 	}
1383a74af788SAkhil Goyal 
1384a74af788SAkhil Goyal 	/* 3rd seg */
1385a74af788SAkhil Goyal 	sg++;
1386ce627d63SThomas Monjalon 	qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1387a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->aead.data.offset;
1388a74af788SAkhil Goyal 	sg->offset = sym->aead.data.offset;
1389a74af788SAkhil Goyal 
1390a74af788SAkhil Goyal 	/* Successive segs */
1391a74af788SAkhil Goyal 	mbuf = mbuf->next;
1392a74af788SAkhil Goyal 	while (mbuf) {
1393a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1394a74af788SAkhil Goyal 		sg++;
1395ce627d63SThomas Monjalon 		qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1396a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1397a74af788SAkhil Goyal 		mbuf = mbuf->next;
1398a74af788SAkhil Goyal 	}
1399a74af788SAkhil Goyal 
1400a74af788SAkhil Goyal 	if (is_decode(ses)) {
1401a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1402a74af788SAkhil Goyal 		sg++;
1403a74af788SAkhil Goyal 		memcpy(ctx->digest, sym->aead.digest.data,
1404a74af788SAkhil Goyal 			ses->digest_length);
1405ec861560SGagandeep Singh 		qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(ctx->digest));
1406a74af788SAkhil Goyal 		sg->length = ses->digest_length;
1407a74af788SAkhil Goyal 	}
1408a74af788SAkhil Goyal 	sg->final = 1;
1409a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1410a74af788SAkhil Goyal 
1411a74af788SAkhil Goyal 	return cf;
1412a74af788SAkhil Goyal }
1413a74af788SAkhil Goyal 
1414a74af788SAkhil Goyal static inline struct dpaa_sec_job *
1415c3e85bdcSAkhil Goyal build_cipher_auth_gcm(struct rte_crypto_op *op, dpaa_sec_session *ses)
1416c3e85bdcSAkhil Goyal {
1417c3e85bdcSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1418c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
1419c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1420c3e85bdcSAkhil Goyal 	struct qm_sg_entry *sg;
1421c3e85bdcSAkhil Goyal 	uint32_t length = 0;
1422c4509373SSantosh Shukla 	rte_iova_t src_start_addr, dst_start_addr;
1423c3e85bdcSAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1424c3e85bdcSAkhil Goyal 			ses->iv.offset);
1425c3e85bdcSAkhil Goyal 
1426116ff44aSHemant Agrawal 	src_start_addr = sym->m_src->buf_iova + sym->m_src->data_off;
1427a389434eSAlok Makhariya 
1428a389434eSAlok Makhariya 	if (sym->m_dst)
1429116ff44aSHemant Agrawal 		dst_start_addr = sym->m_dst->buf_iova + sym->m_dst->data_off;
1430a389434eSAlok Makhariya 	else
1431a389434eSAlok Makhariya 		dst_start_addr = src_start_addr;
1432c3e85bdcSAkhil Goyal 
1433f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, 7);
1434c3e85bdcSAkhil Goyal 	if (!ctx)
1435c3e85bdcSAkhil Goyal 		return NULL;
1436c3e85bdcSAkhil Goyal 
1437c3e85bdcSAkhil Goyal 	cf = &ctx->job;
1438c3e85bdcSAkhil Goyal 	ctx->op = op;
1439c3e85bdcSAkhil Goyal 
1440c3e85bdcSAkhil Goyal 	/* input */
1441c3e85bdcSAkhil Goyal 	rte_prefetch0(cf->sg);
1442c3e85bdcSAkhil Goyal 	sg = &cf->sg[2];
1443ec861560SGagandeep Singh 	qm_sg_entry_set64(&cf->sg[1], rte_dpaa_mem_vtop(sg));
1444c3e85bdcSAkhil Goyal 	if (is_encode(ses)) {
1445ec861560SGagandeep Singh 		qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(IV_ptr));
1446c3e85bdcSAkhil Goyal 		sg->length = ses->iv.length;
1447c3e85bdcSAkhil Goyal 		length += sg->length;
1448c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1449c3e85bdcSAkhil Goyal 
1450c3e85bdcSAkhil Goyal 		sg++;
1451c3e85bdcSAkhil Goyal 		if (ses->auth_only_len) {
1452c3e85bdcSAkhil Goyal 			qm_sg_entry_set64(sg,
1453ec861560SGagandeep Singh 					  rte_dpaa_mem_vtop(sym->aead.aad.data));
1454c3e85bdcSAkhil Goyal 			sg->length = ses->auth_only_len;
1455c3e85bdcSAkhil Goyal 			length += sg->length;
1456c3e85bdcSAkhil Goyal 			cpu_to_hw_sg(sg);
1457c3e85bdcSAkhil Goyal 			sg++;
1458c3e85bdcSAkhil Goyal 		}
1459a389434eSAlok Makhariya 		qm_sg_entry_set64(sg, src_start_addr + sym->aead.data.offset);
1460c3e85bdcSAkhil Goyal 		sg->length = sym->aead.data.length;
1461c3e85bdcSAkhil Goyal 		length += sg->length;
1462c3e85bdcSAkhil Goyal 		sg->final = 1;
1463c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1464c3e85bdcSAkhil Goyal 	} else {
1465ec861560SGagandeep Singh 		qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(IV_ptr));
1466c3e85bdcSAkhil Goyal 		sg->length = ses->iv.length;
1467c3e85bdcSAkhil Goyal 		length += sg->length;
1468c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1469c3e85bdcSAkhil Goyal 
1470c3e85bdcSAkhil Goyal 		sg++;
1471c3e85bdcSAkhil Goyal 		if (ses->auth_only_len) {
1472c3e85bdcSAkhil Goyal 			qm_sg_entry_set64(sg,
1473ec861560SGagandeep Singh 					  rte_dpaa_mem_vtop(sym->aead.aad.data));
1474c3e85bdcSAkhil Goyal 			sg->length = ses->auth_only_len;
1475c3e85bdcSAkhil Goyal 			length += sg->length;
1476c3e85bdcSAkhil Goyal 			cpu_to_hw_sg(sg);
1477c3e85bdcSAkhil Goyal 			sg++;
1478c3e85bdcSAkhil Goyal 		}
1479a389434eSAlok Makhariya 		qm_sg_entry_set64(sg, src_start_addr + sym->aead.data.offset);
1480c3e85bdcSAkhil Goyal 		sg->length = sym->aead.data.length;
1481c3e85bdcSAkhil Goyal 		length += sg->length;
1482c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1483c3e85bdcSAkhil Goyal 
1484c3e85bdcSAkhil Goyal 		memcpy(ctx->digest, sym->aead.digest.data,
1485c3e85bdcSAkhil Goyal 		       ses->digest_length);
1486c3e85bdcSAkhil Goyal 		sg++;
1487c3e85bdcSAkhil Goyal 
1488ec861560SGagandeep Singh 		qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(ctx->digest));
1489c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
1490c3e85bdcSAkhil Goyal 		length += sg->length;
1491c3e85bdcSAkhil Goyal 		sg->final = 1;
1492c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1493c3e85bdcSAkhil Goyal 	}
1494c3e85bdcSAkhil Goyal 	/* input compound frame */
1495c3e85bdcSAkhil Goyal 	cf->sg[1].length = length;
1496c3e85bdcSAkhil Goyal 	cf->sg[1].extension = 1;
1497c3e85bdcSAkhil Goyal 	cf->sg[1].final = 1;
1498c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(&cf->sg[1]);
1499c3e85bdcSAkhil Goyal 
1500c3e85bdcSAkhil Goyal 	/* output */
1501c3e85bdcSAkhil Goyal 	sg++;
1502ec861560SGagandeep Singh 	qm_sg_entry_set64(&cf->sg[0], rte_dpaa_mem_vtop(sg));
1503c3e85bdcSAkhil Goyal 	qm_sg_entry_set64(sg,
15047a4a6da4SVakul Garg 		dst_start_addr + sym->aead.data.offset);
15057a4a6da4SVakul Garg 	sg->length = sym->aead.data.length;
1506c3e85bdcSAkhil Goyal 	length = sg->length;
1507c3e85bdcSAkhil Goyal 	if (is_encode(ses)) {
1508c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1509c3e85bdcSAkhil Goyal 		/* set auth output */
1510c3e85bdcSAkhil Goyal 		sg++;
1511c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, sym->aead.digest.phys_addr);
1512c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
1513c3e85bdcSAkhil Goyal 		length += sg->length;
1514c3e85bdcSAkhil Goyal 	}
1515c3e85bdcSAkhil Goyal 	sg->final = 1;
1516c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1517c3e85bdcSAkhil Goyal 
1518c3e85bdcSAkhil Goyal 	/* output compound frame */
1519c3e85bdcSAkhil Goyal 	cf->sg[0].length = length;
1520c3e85bdcSAkhil Goyal 	cf->sg[0].extension = 1;
1521c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(&cf->sg[0]);
1522c3e85bdcSAkhil Goyal 
1523c3e85bdcSAkhil Goyal 	return cf;
1524c3e85bdcSAkhil Goyal }
1525c3e85bdcSAkhil Goyal 
1526c3e85bdcSAkhil Goyal static inline struct dpaa_sec_job *
1527a74af788SAkhil Goyal build_cipher_auth_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
1528a74af788SAkhil Goyal {
1529a74af788SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1530a74af788SAkhil Goyal 	struct dpaa_sec_job *cf;
1531a74af788SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1532a74af788SAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
1533a74af788SAkhil Goyal 	struct rte_mbuf *mbuf;
1534a74af788SAkhil Goyal 	uint8_t req_segs;
1535a74af788SAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1536a74af788SAkhil Goyal 			ses->iv.offset);
1537a74af788SAkhil Goyal 
1538a74af788SAkhil Goyal 	if (sym->m_dst) {
1539a74af788SAkhil Goyal 		mbuf = sym->m_dst;
1540a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 4;
1541a74af788SAkhil Goyal 	} else {
1542a74af788SAkhil Goyal 		mbuf = sym->m_src;
1543a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs * 2 + 4;
1544a74af788SAkhil Goyal 	}
1545a74af788SAkhil Goyal 
1546f7a5752eSHemant Agrawal 	if (mbuf->nb_segs > MAX_SG_ENTRIES) {
1547f163231eSHemant Agrawal 		DPAA_SEC_DP_ERR("Cipher-Auth: Max sec segs supported is %d",
1548a74af788SAkhil Goyal 				MAX_SG_ENTRIES);
1549a74af788SAkhil Goyal 		return NULL;
1550a74af788SAkhil Goyal 	}
1551a74af788SAkhil Goyal 
1552f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, req_segs);
1553a74af788SAkhil Goyal 	if (!ctx)
1554a74af788SAkhil Goyal 		return NULL;
1555a74af788SAkhil Goyal 
1556a74af788SAkhil Goyal 	cf = &ctx->job;
1557a74af788SAkhil Goyal 	ctx->op = op;
1558a74af788SAkhil Goyal 
1559a74af788SAkhil Goyal 	rte_prefetch0(cf->sg);
1560a74af788SAkhil Goyal 
1561a74af788SAkhil Goyal 	/* output */
1562a74af788SAkhil Goyal 	out_sg = &cf->sg[0];
1563a74af788SAkhil Goyal 	out_sg->extension = 1;
1564a74af788SAkhil Goyal 	if (is_encode(ses))
1565a74af788SAkhil Goyal 		out_sg->length = sym->auth.data.length + ses->digest_length;
1566a74af788SAkhil Goyal 	else
1567a74af788SAkhil Goyal 		out_sg->length = sym->auth.data.length;
1568a74af788SAkhil Goyal 
1569a74af788SAkhil Goyal 	/* output sg entries */
1570a74af788SAkhil Goyal 	sg = &cf->sg[2];
1571ec861560SGagandeep Singh 	qm_sg_entry_set64(out_sg, rte_dpaa_mem_vtop(sg));
1572a74af788SAkhil Goyal 	cpu_to_hw_sg(out_sg);
1573a74af788SAkhil Goyal 
1574a74af788SAkhil Goyal 	/* 1st seg */
1575ce627d63SThomas Monjalon 	qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1576a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->auth.data.offset;
1577a74af788SAkhil Goyal 	sg->offset = sym->auth.data.offset;
1578a74af788SAkhil Goyal 
1579a74af788SAkhil Goyal 	/* Successive segs */
1580a74af788SAkhil Goyal 	mbuf = mbuf->next;
1581a74af788SAkhil Goyal 	while (mbuf) {
1582a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1583a74af788SAkhil Goyal 		sg++;
1584ce627d63SThomas Monjalon 		qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1585a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1586a74af788SAkhil Goyal 		mbuf = mbuf->next;
1587a74af788SAkhil Goyal 	}
1588a74af788SAkhil Goyal 	sg->length -= ses->digest_length;
1589a74af788SAkhil Goyal 
1590a74af788SAkhil Goyal 	if (is_encode(ses)) {
1591a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1592a74af788SAkhil Goyal 		/* set auth output */
1593a74af788SAkhil Goyal 		sg++;
1594a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
1595a74af788SAkhil Goyal 		sg->length = ses->digest_length;
1596a74af788SAkhil Goyal 	}
1597a74af788SAkhil Goyal 	sg->final = 1;
1598a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1599a74af788SAkhil Goyal 
1600a74af788SAkhil Goyal 	/* input */
1601a74af788SAkhil Goyal 	mbuf = sym->m_src;
1602a74af788SAkhil Goyal 	in_sg = &cf->sg[1];
1603a74af788SAkhil Goyal 	in_sg->extension = 1;
1604a74af788SAkhil Goyal 	in_sg->final = 1;
1605a74af788SAkhil Goyal 	if (is_encode(ses))
1606a74af788SAkhil Goyal 		in_sg->length = ses->iv.length + sym->auth.data.length;
1607a74af788SAkhil Goyal 	else
1608a74af788SAkhil Goyal 		in_sg->length = ses->iv.length + sym->auth.data.length
1609a74af788SAkhil Goyal 						+ ses->digest_length;
1610a74af788SAkhil Goyal 
1611a74af788SAkhil Goyal 	/* input sg entries */
1612a74af788SAkhil Goyal 	sg++;
1613ec861560SGagandeep Singh 	qm_sg_entry_set64(in_sg, rte_dpaa_mem_vtop(sg));
1614a74af788SAkhil Goyal 	cpu_to_hw_sg(in_sg);
1615a74af788SAkhil Goyal 
1616a74af788SAkhil Goyal 	/* 1st seg IV */
1617ec861560SGagandeep Singh 	qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(IV_ptr));
1618a74af788SAkhil Goyal 	sg->length = ses->iv.length;
1619a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1620a74af788SAkhil Goyal 
1621a74af788SAkhil Goyal 	/* 2nd seg */
1622a74af788SAkhil Goyal 	sg++;
1623ce627d63SThomas Monjalon 	qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1624a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->auth.data.offset;
1625a74af788SAkhil Goyal 	sg->offset = sym->auth.data.offset;
1626a74af788SAkhil Goyal 
1627a74af788SAkhil Goyal 	/* Successive segs */
1628a74af788SAkhil Goyal 	mbuf = mbuf->next;
1629a74af788SAkhil Goyal 	while (mbuf) {
1630a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1631a74af788SAkhil Goyal 		sg++;
1632ce627d63SThomas Monjalon 		qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1633a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1634a74af788SAkhil Goyal 		mbuf = mbuf->next;
1635a74af788SAkhil Goyal 	}
1636a74af788SAkhil Goyal 
1637a74af788SAkhil Goyal 	sg->length -= ses->digest_length;
1638a74af788SAkhil Goyal 	if (is_decode(ses)) {
1639a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1640a74af788SAkhil Goyal 		sg++;
1641a74af788SAkhil Goyal 		memcpy(ctx->digest, sym->auth.digest.data,
1642a74af788SAkhil Goyal 			ses->digest_length);
1643ec861560SGagandeep Singh 		qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(ctx->digest));
1644a74af788SAkhil Goyal 		sg->length = ses->digest_length;
1645a74af788SAkhil Goyal 	}
1646a74af788SAkhil Goyal 	sg->final = 1;
1647a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1648a74af788SAkhil Goyal 
1649a74af788SAkhil Goyal 	return cf;
1650a74af788SAkhil Goyal }
1651a74af788SAkhil Goyal 
1652a74af788SAkhil Goyal static inline struct dpaa_sec_job *
1653c3e85bdcSAkhil Goyal build_cipher_auth(struct rte_crypto_op *op, dpaa_sec_session *ses)
1654c3e85bdcSAkhil Goyal {
1655c3e85bdcSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1656c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
1657c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1658c3e85bdcSAkhil Goyal 	struct qm_sg_entry *sg;
1659c4509373SSantosh Shukla 	rte_iova_t src_start_addr, dst_start_addr;
1660c3e85bdcSAkhil Goyal 	uint32_t length = 0;
1661c3e85bdcSAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1662c3e85bdcSAkhil Goyal 			ses->iv.offset);
1663c3e85bdcSAkhil Goyal 
1664455da545SSantosh Shukla 	src_start_addr = sym->m_src->buf_iova + sym->m_src->data_off;
1665a389434eSAlok Makhariya 	if (sym->m_dst)
1666455da545SSantosh Shukla 		dst_start_addr = sym->m_dst->buf_iova + sym->m_dst->data_off;
1667a389434eSAlok Makhariya 	else
1668a389434eSAlok Makhariya 		dst_start_addr = src_start_addr;
1669c3e85bdcSAkhil Goyal 
1670f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, 7);
1671c3e85bdcSAkhil Goyal 	if (!ctx)
1672c3e85bdcSAkhil Goyal 		return NULL;
1673c3e85bdcSAkhil Goyal 
1674c3e85bdcSAkhil Goyal 	cf = &ctx->job;
1675c3e85bdcSAkhil Goyal 	ctx->op = op;
1676c3e85bdcSAkhil Goyal 
1677c3e85bdcSAkhil Goyal 	/* input */
1678c3e85bdcSAkhil Goyal 	rte_prefetch0(cf->sg);
1679c3e85bdcSAkhil Goyal 	sg = &cf->sg[2];
1680ec861560SGagandeep Singh 	qm_sg_entry_set64(&cf->sg[1], rte_dpaa_mem_vtop(sg));
1681c3e85bdcSAkhil Goyal 	if (is_encode(ses)) {
1682ec861560SGagandeep Singh 		qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(IV_ptr));
1683c3e85bdcSAkhil Goyal 		sg->length = ses->iv.length;
1684c3e85bdcSAkhil Goyal 		length += sg->length;
1685c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1686c3e85bdcSAkhil Goyal 
1687c3e85bdcSAkhil Goyal 		sg++;
1688a389434eSAlok Makhariya 		qm_sg_entry_set64(sg, src_start_addr + sym->auth.data.offset);
1689c3e85bdcSAkhil Goyal 		sg->length = sym->auth.data.length;
1690c3e85bdcSAkhil Goyal 		length += sg->length;
1691c3e85bdcSAkhil Goyal 		sg->final = 1;
1692c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1693c3e85bdcSAkhil Goyal 	} else {
1694ec861560SGagandeep Singh 		qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(IV_ptr));
1695c3e85bdcSAkhil Goyal 		sg->length = ses->iv.length;
1696c3e85bdcSAkhil Goyal 		length += sg->length;
1697c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1698c3e85bdcSAkhil Goyal 
1699c3e85bdcSAkhil Goyal 		sg++;
1700c3e85bdcSAkhil Goyal 
1701a389434eSAlok Makhariya 		qm_sg_entry_set64(sg, src_start_addr + sym->auth.data.offset);
1702c3e85bdcSAkhil Goyal 		sg->length = sym->auth.data.length;
1703c3e85bdcSAkhil Goyal 		length += sg->length;
1704c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1705c3e85bdcSAkhil Goyal 
1706c3e85bdcSAkhil Goyal 		memcpy(ctx->digest, sym->auth.digest.data,
1707c3e85bdcSAkhil Goyal 		       ses->digest_length);
1708c3e85bdcSAkhil Goyal 		sg++;
1709c3e85bdcSAkhil Goyal 
1710ec861560SGagandeep Singh 		qm_sg_entry_set64(sg, rte_dpaa_mem_vtop(ctx->digest));
1711c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
1712c3e85bdcSAkhil Goyal 		length += sg->length;
1713c3e85bdcSAkhil Goyal 		sg->final = 1;
1714c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1715c3e85bdcSAkhil Goyal 	}
1716c3e85bdcSAkhil Goyal 	/* input compound frame */
1717c3e85bdcSAkhil Goyal 	cf->sg[1].length = length;
1718c3e85bdcSAkhil Goyal 	cf->sg[1].extension = 1;
1719c3e85bdcSAkhil Goyal 	cf->sg[1].final = 1;
1720c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(&cf->sg[1]);
1721c3e85bdcSAkhil Goyal 
1722c3e85bdcSAkhil Goyal 	/* output */
1723c3e85bdcSAkhil Goyal 	sg++;
1724ec861560SGagandeep Singh 	qm_sg_entry_set64(&cf->sg[0], rte_dpaa_mem_vtop(sg));
1725a389434eSAlok Makhariya 	qm_sg_entry_set64(sg, dst_start_addr + sym->cipher.data.offset);
1726c3e85bdcSAkhil Goyal 	sg->length = sym->cipher.data.length;
1727c3e85bdcSAkhil Goyal 	length = sg->length;
1728c3e85bdcSAkhil Goyal 	if (is_encode(ses)) {
1729c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1730c3e85bdcSAkhil Goyal 		/* set auth output */
1731c3e85bdcSAkhil Goyal 		sg++;
1732c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
1733c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
1734c3e85bdcSAkhil Goyal 		length += sg->length;
1735c3e85bdcSAkhil Goyal 	}
1736c3e85bdcSAkhil Goyal 	sg->final = 1;
1737c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1738c3e85bdcSAkhil Goyal 
1739c3e85bdcSAkhil Goyal 	/* output compound frame */
1740c3e85bdcSAkhil Goyal 	cf->sg[0].length = length;
1741c3e85bdcSAkhil Goyal 	cf->sg[0].extension = 1;
1742c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(&cf->sg[0]);
1743c3e85bdcSAkhil Goyal 
1744c3e85bdcSAkhil Goyal 	return cf;
1745c3e85bdcSAkhil Goyal }
1746c3e85bdcSAkhil Goyal 
17471f14d500SAkhil Goyal static inline struct dpaa_sec_job *
17481f14d500SAkhil Goyal build_proto(struct rte_crypto_op *op, dpaa_sec_session *ses)
17491f14d500SAkhil Goyal {
17501f14d500SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
17511f14d500SAkhil Goyal 	struct dpaa_sec_job *cf;
17521f14d500SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
17531f14d500SAkhil Goyal 	struct qm_sg_entry *sg;
17541f14d500SAkhil Goyal 	phys_addr_t src_start_addr, dst_start_addr;
17551f14d500SAkhil Goyal 
1756f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, 2);
17571f14d500SAkhil Goyal 	if (!ctx)
17581f14d500SAkhil Goyal 		return NULL;
17591f14d500SAkhil Goyal 	cf = &ctx->job;
17601f14d500SAkhil Goyal 	ctx->op = op;
17611f14d500SAkhil Goyal 
1762ce627d63SThomas Monjalon 	src_start_addr = rte_pktmbuf_iova(sym->m_src);
17631f14d500SAkhil Goyal 
17641f14d500SAkhil Goyal 	if (sym->m_dst)
1765ce627d63SThomas Monjalon 		dst_start_addr = rte_pktmbuf_iova(sym->m_dst);
17661f14d500SAkhil Goyal 	else
17671f14d500SAkhil Goyal 		dst_start_addr = src_start_addr;
17681f14d500SAkhil Goyal 
17691f14d500SAkhil Goyal 	/* input */
17701f14d500SAkhil Goyal 	sg = &cf->sg[1];
17711f14d500SAkhil Goyal 	qm_sg_entry_set64(sg, src_start_addr);
17721f14d500SAkhil Goyal 	sg->length = sym->m_src->pkt_len;
17731f14d500SAkhil Goyal 	sg->final = 1;
17741f14d500SAkhil Goyal 	cpu_to_hw_sg(sg);
17751f14d500SAkhil Goyal 
17761f14d500SAkhil Goyal 	sym->m_src->packet_type &= ~RTE_PTYPE_L4_MASK;
17771f14d500SAkhil Goyal 	/* output */
17781f14d500SAkhil Goyal 	sg = &cf->sg[0];
17791f14d500SAkhil Goyal 	qm_sg_entry_set64(sg, dst_start_addr);
17801f14d500SAkhil Goyal 	sg->length = sym->m_src->buf_len - sym->m_src->data_off;
17811f14d500SAkhil Goyal 	cpu_to_hw_sg(sg);
17821f14d500SAkhil Goyal 
17831f14d500SAkhil Goyal 	return cf;
17841f14d500SAkhil Goyal }
17851f14d500SAkhil Goyal 
1786fb5c100aSAkhil Goyal static inline struct dpaa_sec_job *
1787fb5c100aSAkhil Goyal build_proto_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
1788fb5c100aSAkhil Goyal {
1789fb5c100aSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1790fb5c100aSAkhil Goyal 	struct dpaa_sec_job *cf;
1791fb5c100aSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1792fb5c100aSAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
1793fb5c100aSAkhil Goyal 	struct rte_mbuf *mbuf;
1794fb5c100aSAkhil Goyal 	uint8_t req_segs;
1795fb5c100aSAkhil Goyal 	uint32_t in_len = 0, out_len = 0;
1796fb5c100aSAkhil Goyal 
1797fb5c100aSAkhil Goyal 	if (sym->m_dst)
1798fb5c100aSAkhil Goyal 		mbuf = sym->m_dst;
1799fb5c100aSAkhil Goyal 	else
1800fb5c100aSAkhil Goyal 		mbuf = sym->m_src;
1801fb5c100aSAkhil Goyal 
1802fb5c100aSAkhil Goyal 	req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 2;
1803f7a5752eSHemant Agrawal 	if (mbuf->nb_segs > MAX_SG_ENTRIES) {
1804fb5c100aSAkhil Goyal 		DPAA_SEC_DP_ERR("Proto: Max sec segs supported is %d",
1805fb5c100aSAkhil Goyal 				MAX_SG_ENTRIES);
1806fb5c100aSAkhil Goyal 		return NULL;
1807fb5c100aSAkhil Goyal 	}
1808fb5c100aSAkhil Goyal 
1809f7a5752eSHemant Agrawal 	ctx = dpaa_sec_alloc_ctx(ses, req_segs);
1810fb5c100aSAkhil Goyal 	if (!ctx)
1811fb5c100aSAkhil Goyal 		return NULL;
1812fb5c100aSAkhil Goyal 	cf = &ctx->job;
1813fb5c100aSAkhil Goyal 	ctx->op = op;
1814fb5c100aSAkhil Goyal 	/* output */
1815fb5c100aSAkhil Goyal 	out_sg = &cf->sg[0];
1816fb5c100aSAkhil Goyal 	out_sg->extension = 1;
1817ec861560SGagandeep Singh 	qm_sg_entry_set64(out_sg, rte_dpaa_mem_vtop(&cf->sg[2]));
1818fb5c100aSAkhil Goyal 
1819fb5c100aSAkhil Goyal 	/* 1st seg */
1820fb5c100aSAkhil Goyal 	sg = &cf->sg[2];
1821ce627d63SThomas Monjalon 	qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1822fb5c100aSAkhil Goyal 	sg->offset = 0;
1823fb5c100aSAkhil Goyal 
1824fb5c100aSAkhil Goyal 	/* Successive segs */
1825fb5c100aSAkhil Goyal 	while (mbuf->next) {
1826fb5c100aSAkhil Goyal 		sg->length = mbuf->data_len;
1827fb5c100aSAkhil Goyal 		out_len += sg->length;
1828fb5c100aSAkhil Goyal 		mbuf = mbuf->next;
1829fb5c100aSAkhil Goyal 		cpu_to_hw_sg(sg);
1830fb5c100aSAkhil Goyal 		sg++;
1831ce627d63SThomas Monjalon 		qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1832fb5c100aSAkhil Goyal 		sg->offset = 0;
1833fb5c100aSAkhil Goyal 	}
1834fb5c100aSAkhil Goyal 	sg->length = mbuf->buf_len - mbuf->data_off;
1835fb5c100aSAkhil Goyal 	out_len += sg->length;
1836fb5c100aSAkhil Goyal 	sg->final = 1;
1837fb5c100aSAkhil Goyal 	cpu_to_hw_sg(sg);
1838fb5c100aSAkhil Goyal 
1839fb5c100aSAkhil Goyal 	out_sg->length = out_len;
1840fb5c100aSAkhil Goyal 	cpu_to_hw_sg(out_sg);
1841fb5c100aSAkhil Goyal 
1842fb5c100aSAkhil Goyal 	/* input */
1843fb5c100aSAkhil Goyal 	mbuf = sym->m_src;
1844fb5c100aSAkhil Goyal 	in_sg = &cf->sg[1];
1845fb5c100aSAkhil Goyal 	in_sg->extension = 1;
1846fb5c100aSAkhil Goyal 	in_sg->final = 1;
1847fb5c100aSAkhil Goyal 	in_len = mbuf->data_len;
1848fb5c100aSAkhil Goyal 
1849fb5c100aSAkhil Goyal 	sg++;
1850ec861560SGagandeep Singh 	qm_sg_entry_set64(in_sg, rte_dpaa_mem_vtop(sg));
1851fb5c100aSAkhil Goyal 
1852fb5c100aSAkhil Goyal 	/* 1st seg */
1853ce627d63SThomas Monjalon 	qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1854fb5c100aSAkhil Goyal 	sg->length = mbuf->data_len;
1855fb5c100aSAkhil Goyal 	sg->offset = 0;
1856fb5c100aSAkhil Goyal 
1857fb5c100aSAkhil Goyal 	/* Successive segs */
1858fb5c100aSAkhil Goyal 	mbuf = mbuf->next;
1859fb5c100aSAkhil Goyal 	while (mbuf) {
1860fb5c100aSAkhil Goyal 		cpu_to_hw_sg(sg);
1861fb5c100aSAkhil Goyal 		sg++;
1862ce627d63SThomas Monjalon 		qm_sg_entry_set64(sg, rte_pktmbuf_iova(mbuf));
1863fb5c100aSAkhil Goyal 		sg->length = mbuf->data_len;
1864fb5c100aSAkhil Goyal 		sg->offset = 0;
1865fb5c100aSAkhil Goyal 		in_len += sg->length;
1866fb5c100aSAkhil Goyal 		mbuf = mbuf->next;
1867fb5c100aSAkhil Goyal 	}
1868fb5c100aSAkhil Goyal 	sg->final = 1;
1869fb5c100aSAkhil Goyal 	cpu_to_hw_sg(sg);
1870fb5c100aSAkhil Goyal 
1871fb5c100aSAkhil Goyal 	in_sg->length = in_len;
1872fb5c100aSAkhil Goyal 	cpu_to_hw_sg(in_sg);
1873fb5c100aSAkhil Goyal 
1874fb5c100aSAkhil Goyal 	sym->m_src->packet_type &= ~RTE_PTYPE_L4_MASK;
1875fb5c100aSAkhil Goyal 
1876fb5c100aSAkhil Goyal 	return cf;
1877fb5c100aSAkhil Goyal }
1878fb5c100aSAkhil Goyal 
18799a984458SAkhil Goyal static uint16_t
18809a984458SAkhil Goyal dpaa_sec_enqueue_burst(void *qp, struct rte_crypto_op **ops,
18819a984458SAkhil Goyal 		       uint16_t nb_ops)
1882c3e85bdcSAkhil Goyal {
18839a984458SAkhil Goyal 	/* Function to transmit the frames to given device and queuepair */
18849a984458SAkhil Goyal 	uint32_t loop;
18859a984458SAkhil Goyal 	struct dpaa_sec_qp *dpaa_qp = (struct dpaa_sec_qp *)qp;
18869a984458SAkhil Goyal 	uint16_t num_tx = 0;
18879a984458SAkhil Goyal 	struct qm_fd fds[DPAA_SEC_BURST], *fd;
18889a984458SAkhil Goyal 	uint32_t frames_to_send;
18899a984458SAkhil Goyal 	struct rte_crypto_op *op;
1890c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
1891c3e85bdcSAkhil Goyal 	dpaa_sec_session *ses;
18923394ed47SVakul Garg 	uint16_t auth_hdr_len, auth_tail_len;
18933394ed47SVakul Garg 	uint32_t index, flags[DPAA_SEC_BURST] = {0};
18949a984458SAkhil Goyal 	struct qman_fq *inq[DPAA_SEC_BURST];
1895c3e85bdcSAkhil Goyal 
189622629f05SHemant Agrawal 	if (unlikely(!DPAA_PER_LCORE_PORTAL)) {
189722629f05SHemant Agrawal 		if (rte_dpaa_portal_init((void *)0)) {
189822629f05SHemant Agrawal 			DPAA_SEC_ERR("Failure in affining portal");
189922629f05SHemant Agrawal 			return 0;
190022629f05SHemant Agrawal 		}
190122629f05SHemant Agrawal 	}
190222629f05SHemant Agrawal 
19039a984458SAkhil Goyal 	while (nb_ops) {
19049a984458SAkhil Goyal 		frames_to_send = (nb_ops > DPAA_SEC_BURST) ?
19059a984458SAkhil Goyal 				DPAA_SEC_BURST : nb_ops;
19069a984458SAkhil Goyal 		for (loop = 0; loop < frames_to_send; loop++) {
19079a984458SAkhil Goyal 			op = *(ops++);
1908c9a1c2e5SDavid Marchand 			if (*dpaa_seqn(op->sym->m_src) != 0) {
1909c9a1c2e5SDavid Marchand 				index = *dpaa_seqn(op->sym->m_src) - 1;
1910fe3688baSAkhil Goyal 				if (DPAA_PER_LCORE_DQRR_HELD & (1 << index)) {
1911fe3688baSAkhil Goyal 					/* QM_EQCR_DCA_IDXMASK = 0x0f */
1912fe3688baSAkhil Goyal 					flags[loop] = ((index & 0x0f) << 8);
1913fe3688baSAkhil Goyal 					flags[loop] |= QMAN_ENQUEUE_FLAG_DCA;
1914fe3688baSAkhil Goyal 					DPAA_PER_LCORE_DQRR_SIZE--;
1915fe3688baSAkhil Goyal 					DPAA_PER_LCORE_DQRR_HELD &=
1916fe3688baSAkhil Goyal 								~(1 << index);
1917fe3688baSAkhil Goyal 				}
1918fe3688baSAkhil Goyal 			}
1919fe3688baSAkhil Goyal 
19209a984458SAkhil Goyal 			switch (op->sess_type) {
19219a984458SAkhil Goyal 			case RTE_CRYPTO_OP_WITH_SESSION:
19222a440d6aSAkhil Goyal 				ses = CRYPTODEV_GET_SYM_SESS_PRIV(op->sym->session);
19239a984458SAkhil Goyal 				break;
19249a984458SAkhil Goyal 			case RTE_CRYPTO_OP_SECURITY_SESSION:
19252973dbf9SAkhil Goyal 				ses = SECURITY_GET_SESS_PRIV(op->sym->session);
19269a984458SAkhil Goyal 				break;
19279a984458SAkhil Goyal 			default:
1928f163231eSHemant Agrawal 				DPAA_SEC_DP_ERR(
19299a984458SAkhil Goyal 					"sessionless crypto op not supported");
19309a984458SAkhil Goyal 				frames_to_send = loop;
19319a984458SAkhil Goyal 				nb_ops = loop;
19329a984458SAkhil Goyal 				goto send_pkts;
19339a984458SAkhil Goyal 			}
1934e1e52232SHemant Agrawal 
1935e1e52232SHemant Agrawal 			if (!ses) {
1936e1e52232SHemant Agrawal 				DPAA_SEC_DP_ERR("session not available");
1937e1e52232SHemant Agrawal 				frames_to_send = loop;
1938e1e52232SHemant Agrawal 				nb_ops = loop;
1939e1e52232SHemant Agrawal 				goto send_pkts;
1940e1e52232SHemant Agrawal 			}
1941e1e52232SHemant Agrawal 
19424e694fe5SAkhil Goyal 			if (unlikely(!ses->qp[rte_lcore_id() % MAX_DPAA_CORES])) {
19439a984458SAkhil Goyal 				if (dpaa_sec_attach_sess_q(qp, ses)) {
19449a984458SAkhil Goyal 					frames_to_send = loop;
19459a984458SAkhil Goyal 					nb_ops = loop;
19469a984458SAkhil Goyal 					goto send_pkts;
19479a984458SAkhil Goyal 				}
19484e694fe5SAkhil Goyal 			} else if (unlikely(ses->qp[rte_lcore_id() %
19494e694fe5SAkhil Goyal 						MAX_DPAA_CORES] != qp)) {
19509198b2c2SAkhil Goyal 				DPAA_SEC_DP_ERR("Old:sess->qp = %p"
1951f665790aSDavid Marchand 					" New qp = %p",
19524e694fe5SAkhil Goyal 					ses->qp[rte_lcore_id() %
19534e694fe5SAkhil Goyal 					MAX_DPAA_CORES], qp);
19549198b2c2SAkhil Goyal 				frames_to_send = loop;
19559198b2c2SAkhil Goyal 				nb_ops = loop;
19569198b2c2SAkhil Goyal 				goto send_pkts;
1957c3e85bdcSAkhil Goyal 			}
1958c3e85bdcSAkhil Goyal 
19593394ed47SVakul Garg 			auth_hdr_len = op->sym->auth.data.length -
19609a984458SAkhil Goyal 						op->sym->cipher.data.length;
19613394ed47SVakul Garg 			auth_tail_len = 0;
19623394ed47SVakul Garg 
1963fb5c100aSAkhil Goyal 			if (rte_pktmbuf_is_contiguous(op->sym->m_src) &&
1964fb5c100aSAkhil Goyal 				  ((op->sym->m_dst == NULL) ||
1965fb5c100aSAkhil Goyal 				   rte_pktmbuf_is_contiguous(op->sym->m_dst))) {
19668524b44eSHemant Agrawal 				switch (ses->ctxt) {
19678524b44eSHemant Agrawal 				case DPAA_SEC_PDCP:
19688524b44eSHemant Agrawal 				case DPAA_SEC_IPSEC:
196905b12700SHemant Agrawal 					cf = build_proto(op, ses);
19708524b44eSHemant Agrawal 					break;
19718524b44eSHemant Agrawal 				case DPAA_SEC_AUTH:
1972c3e85bdcSAkhil Goyal 					cf = build_auth_only(op, ses);
19738524b44eSHemant Agrawal 					break;
19748524b44eSHemant Agrawal 				case DPAA_SEC_CIPHER:
1975c3e85bdcSAkhil Goyal 					cf = build_cipher_only(op, ses);
19768524b44eSHemant Agrawal 					break;
19778524b44eSHemant Agrawal 				case DPAA_SEC_AEAD:
1978c3e85bdcSAkhil Goyal 					cf = build_cipher_auth_gcm(op, ses);
19793394ed47SVakul Garg 					auth_hdr_len = ses->auth_only_len;
19808524b44eSHemant Agrawal 					break;
19818524b44eSHemant Agrawal 				case DPAA_SEC_CIPHER_HASH:
19823394ed47SVakul Garg 					auth_hdr_len =
19833394ed47SVakul Garg 						op->sym->cipher.data.offset
19843394ed47SVakul Garg 						- op->sym->auth.data.offset;
19853394ed47SVakul Garg 					auth_tail_len =
19863394ed47SVakul Garg 						op->sym->auth.data.length
19873394ed47SVakul Garg 						- op->sym->cipher.data.length
19883394ed47SVakul Garg 						- auth_hdr_len;
1989c3e85bdcSAkhil Goyal 					cf = build_cipher_auth(op, ses);
19908524b44eSHemant Agrawal 					break;
19918524b44eSHemant Agrawal 				default:
1992f163231eSHemant Agrawal 					DPAA_SEC_DP_ERR("not supported ops");
19939a984458SAkhil Goyal 					frames_to_send = loop;
19949a984458SAkhil Goyal 					nb_ops = loop;
19959a984458SAkhil Goyal 					goto send_pkts;
1996c3e85bdcSAkhil Goyal 				}
1997a74af788SAkhil Goyal 			} else {
19988524b44eSHemant Agrawal 				switch (ses->ctxt) {
19998524b44eSHemant Agrawal 				case DPAA_SEC_PDCP:
20008524b44eSHemant Agrawal 				case DPAA_SEC_IPSEC:
2001fb5c100aSAkhil Goyal 					cf = build_proto_sg(op, ses);
20028524b44eSHemant Agrawal 					break;
20038524b44eSHemant Agrawal 				case DPAA_SEC_AUTH:
2004a74af788SAkhil Goyal 					cf = build_auth_only_sg(op, ses);
20058524b44eSHemant Agrawal 					break;
20068524b44eSHemant Agrawal 				case DPAA_SEC_CIPHER:
2007a74af788SAkhil Goyal 					cf = build_cipher_only_sg(op, ses);
20088524b44eSHemant Agrawal 					break;
20098524b44eSHemant Agrawal 				case DPAA_SEC_AEAD:
2010a74af788SAkhil Goyal 					cf = build_cipher_auth_gcm_sg(op, ses);
20113394ed47SVakul Garg 					auth_hdr_len = ses->auth_only_len;
20128524b44eSHemant Agrawal 					break;
20138524b44eSHemant Agrawal 				case DPAA_SEC_CIPHER_HASH:
20143394ed47SVakul Garg 					auth_hdr_len =
20153394ed47SVakul Garg 						op->sym->cipher.data.offset
20163394ed47SVakul Garg 						- op->sym->auth.data.offset;
20173394ed47SVakul Garg 					auth_tail_len =
20183394ed47SVakul Garg 						op->sym->auth.data.length
20193394ed47SVakul Garg 						- op->sym->cipher.data.length
20203394ed47SVakul Garg 						- auth_hdr_len;
2021a74af788SAkhil Goyal 					cf = build_cipher_auth_sg(op, ses);
20228524b44eSHemant Agrawal 					break;
20238524b44eSHemant Agrawal 				default:
2024f163231eSHemant Agrawal 					DPAA_SEC_DP_ERR("not supported ops");
2025a74af788SAkhil Goyal 					frames_to_send = loop;
2026a74af788SAkhil Goyal 					nb_ops = loop;
2027a74af788SAkhil Goyal 					goto send_pkts;
2028a74af788SAkhil Goyal 				}
2029a74af788SAkhil Goyal 			}
20309a984458SAkhil Goyal 			if (unlikely(!cf)) {
20319a984458SAkhil Goyal 				frames_to_send = loop;
20329a984458SAkhil Goyal 				nb_ops = loop;
20339a984458SAkhil Goyal 				goto send_pkts;
20349a984458SAkhil Goyal 			}
2035c3e85bdcSAkhil Goyal 
20369a984458SAkhil Goyal 			fd = &fds[loop];
20374e694fe5SAkhil Goyal 			inq[loop] = ses->inq[rte_lcore_id() % MAX_DPAA_CORES];
20389a984458SAkhil Goyal 			fd->opaque_addr = 0;
20399a984458SAkhil Goyal 			fd->cmd = 0;
2040ec861560SGagandeep Singh 			qm_fd_addr_set64(fd, rte_dpaa_mem_vtop(cf->sg));
20419a984458SAkhil Goyal 			fd->_format1 = qm_fd_compound;
20429a984458SAkhil Goyal 			fd->length29 = 2 * sizeof(struct qm_sg_entry);
20433394ed47SVakul Garg 
20449a984458SAkhil Goyal 			/* Auth_only_len is set as 0 in descriptor and it is
20459a984458SAkhil Goyal 			 * overwritten here in the fd.cmd which will update
20469a984458SAkhil Goyal 			 * the DPOVRD reg.
2047c3e85bdcSAkhil Goyal 			 */
20483394ed47SVakul Garg 			if (auth_hdr_len || auth_tail_len) {
20493394ed47SVakul Garg 				fd->cmd = 0x80000000;
20503394ed47SVakul Garg 				fd->cmd |=
20513394ed47SVakul Garg 					((auth_tail_len << 16) | auth_hdr_len);
20523394ed47SVakul Garg 			}
2053c3e85bdcSAkhil Goyal 
20546a0c9d36SAkhil Goyal 			/* In case of PDCP, per packet HFN is stored in
20556a0c9d36SAkhil Goyal 			 * mbuf priv after sym_op.
20566a0c9d36SAkhil Goyal 			 */
20578524b44eSHemant Agrawal 			if ((ses->ctxt == DPAA_SEC_PDCP) && ses->pdcp.hfn_ovd) {
20586a0c9d36SAkhil Goyal 				fd->cmd = 0x80000000 |
20596a0c9d36SAkhil Goyal 					*((uint32_t *)((uint8_t *)op +
20606a0c9d36SAkhil Goyal 					ses->pdcp.hfn_ovd_offset));
2061f665790aSDavid Marchand 				DPAA_SEC_DP_DEBUG("Per packet HFN: %x, ovd:%u",
20626a0c9d36SAkhil Goyal 					*((uint32_t *)((uint8_t *)op +
20636a0c9d36SAkhil Goyal 					ses->pdcp.hfn_ovd_offset)),
20648524b44eSHemant Agrawal 					ses->pdcp.hfn_ovd);
20656a0c9d36SAkhil Goyal 			}
20669a984458SAkhil Goyal 		}
20679a984458SAkhil Goyal send_pkts:
20689a984458SAkhil Goyal 		loop = 0;
20699a984458SAkhil Goyal 		while (loop < frames_to_send) {
20709a984458SAkhil Goyal 			loop += qman_enqueue_multi_fq(&inq[loop], &fds[loop],
2071fe3688baSAkhil Goyal 					&flags[loop], frames_to_send - loop);
20729a984458SAkhil Goyal 		}
20739a984458SAkhil Goyal 		nb_ops -= frames_to_send;
20749a984458SAkhil Goyal 		num_tx += frames_to_send;
2075c3e85bdcSAkhil Goyal 	}
2076c3e85bdcSAkhil Goyal 
2077c3e85bdcSAkhil Goyal 	dpaa_qp->tx_pkts += num_tx;
2078c3e85bdcSAkhil Goyal 	dpaa_qp->tx_errs += nb_ops - num_tx;
2079c3e85bdcSAkhil Goyal 
2080c3e85bdcSAkhil Goyal 	return num_tx;
2081c3e85bdcSAkhil Goyal }
2082c3e85bdcSAkhil Goyal 
2083c3e85bdcSAkhil Goyal static uint16_t
2084c3e85bdcSAkhil Goyal dpaa_sec_dequeue_burst(void *qp, struct rte_crypto_op **ops,
2085c3e85bdcSAkhil Goyal 		       uint16_t nb_ops)
2086c3e85bdcSAkhil Goyal {
2087c3e85bdcSAkhil Goyal 	uint16_t num_rx;
2088c3e85bdcSAkhil Goyal 	struct dpaa_sec_qp *dpaa_qp = (struct dpaa_sec_qp *)qp;
2089c3e85bdcSAkhil Goyal 
209022629f05SHemant Agrawal 	if (unlikely(!DPAA_PER_LCORE_PORTAL)) {
209122629f05SHemant Agrawal 		if (rte_dpaa_portal_init((void *)0)) {
209222629f05SHemant Agrawal 			DPAA_SEC_ERR("Failure in affining portal");
209322629f05SHemant Agrawal 			return 0;
209422629f05SHemant Agrawal 		}
209522629f05SHemant Agrawal 	}
209622629f05SHemant Agrawal 
2097c3e85bdcSAkhil Goyal 	num_rx = dpaa_sec_deq(dpaa_qp, ops, nb_ops);
2098c3e85bdcSAkhil Goyal 
2099c3e85bdcSAkhil Goyal 	dpaa_qp->rx_pkts += num_rx;
2100c3e85bdcSAkhil Goyal 	dpaa_qp->rx_errs += nb_ops - num_rx;
2101c3e85bdcSAkhil Goyal 
2102f665790aSDavid Marchand 	DPAA_SEC_DP_DEBUG("SEC Received %d Packets", num_rx);
2103c3e85bdcSAkhil Goyal 
2104c3e85bdcSAkhil Goyal 	return num_rx;
2105c3e85bdcSAkhil Goyal }
2106c3e85bdcSAkhil Goyal 
2107c3e85bdcSAkhil Goyal /** Release queue pair */
2108c3e85bdcSAkhil Goyal static int
2109c3e85bdcSAkhil Goyal dpaa_sec_queue_pair_release(struct rte_cryptodev *dev,
2110c3e85bdcSAkhil Goyal 			    uint16_t qp_id)
2111c3e85bdcSAkhil Goyal {
2112c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals;
2113c3e85bdcSAkhil Goyal 	struct dpaa_sec_qp *qp = NULL;
2114c3e85bdcSAkhil Goyal 
2115c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2116c3e85bdcSAkhil Goyal 
2117f163231eSHemant Agrawal 	DPAA_SEC_DEBUG("dev =%p, queue =%d", dev, qp_id);
2118c3e85bdcSAkhil Goyal 
2119c3e85bdcSAkhil Goyal 	internals = dev->data->dev_private;
2120c3e85bdcSAkhil Goyal 	if (qp_id >= internals->max_nb_queue_pairs) {
2121f163231eSHemant Agrawal 		DPAA_SEC_ERR("Max supported qpid %d",
2122c3e85bdcSAkhil Goyal 			     internals->max_nb_queue_pairs);
2123c3e85bdcSAkhil Goyal 		return -EINVAL;
2124c3e85bdcSAkhil Goyal 	}
2125c3e85bdcSAkhil Goyal 
2126c3e85bdcSAkhil Goyal 	qp = &internals->qps[qp_id];
21272ffb940eSAkhil Goyal 	rte_mempool_free(qp->ctx_pool);
2128c3e85bdcSAkhil Goyal 	qp->internals = NULL;
2129c3e85bdcSAkhil Goyal 	dev->data->queue_pairs[qp_id] = NULL;
2130c3e85bdcSAkhil Goyal 
2131c3e85bdcSAkhil Goyal 	return 0;
2132c3e85bdcSAkhil Goyal }
2133c3e85bdcSAkhil Goyal 
2134c3e85bdcSAkhil Goyal /** Setup a queue pair */
2135c3e85bdcSAkhil Goyal static int
2136c3e85bdcSAkhil Goyal dpaa_sec_queue_pair_setup(struct rte_cryptodev *dev, uint16_t qp_id,
2137c3e85bdcSAkhil Goyal 		__rte_unused const struct rte_cryptodev_qp_conf *qp_conf,
2138725d2a7fSFan Zhang 		__rte_unused int socket_id)
2139c3e85bdcSAkhil Goyal {
2140c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals;
2141c3e85bdcSAkhil Goyal 	struct dpaa_sec_qp *qp = NULL;
21422ffb940eSAkhil Goyal 	char str[20];
2143c3e85bdcSAkhil Goyal 
2144f163231eSHemant Agrawal 	DPAA_SEC_DEBUG("dev =%p, queue =%d, conf =%p", dev, qp_id, qp_conf);
2145c3e85bdcSAkhil Goyal 
2146c3e85bdcSAkhil Goyal 	internals = dev->data->dev_private;
2147c3e85bdcSAkhil Goyal 	if (qp_id >= internals->max_nb_queue_pairs) {
2148f163231eSHemant Agrawal 		DPAA_SEC_ERR("Max supported qpid %d",
2149c3e85bdcSAkhil Goyal 			     internals->max_nb_queue_pairs);
2150c3e85bdcSAkhil Goyal 		return -EINVAL;
2151c3e85bdcSAkhil Goyal 	}
2152c3e85bdcSAkhil Goyal 
2153c3e85bdcSAkhil Goyal 	qp = &internals->qps[qp_id];
2154c3e85bdcSAkhil Goyal 	qp->internals = internals;
21552ffb940eSAkhil Goyal 	snprintf(str, sizeof(str), "ctx_pool_d%d_qp%d",
21562ffb940eSAkhil Goyal 			dev->data->dev_id, qp_id);
21572ffb940eSAkhil Goyal 	if (!qp->ctx_pool) {
21582ffb940eSAkhil Goyal 		qp->ctx_pool = rte_mempool_create((const char *)str,
21592ffb940eSAkhil Goyal 							CTX_POOL_NUM_BUFS,
21602ffb940eSAkhil Goyal 							CTX_POOL_BUF_SIZE,
21612ffb940eSAkhil Goyal 							CTX_POOL_CACHE_SIZE, 0,
21622ffb940eSAkhil Goyal 							NULL, NULL, NULL, NULL,
21632ffb940eSAkhil Goyal 							SOCKET_ID_ANY, 0);
21642ffb940eSAkhil Goyal 		if (!qp->ctx_pool) {
2165f665790aSDavid Marchand 			DPAA_SEC_ERR("%s create failed", str);
21662ffb940eSAkhil Goyal 			return -ENOMEM;
21672ffb940eSAkhil Goyal 		}
21682ffb940eSAkhil Goyal 	} else
21692ffb940eSAkhil Goyal 		DPAA_SEC_INFO("mempool already created for dev_id : %d, qp: %d",
21702ffb940eSAkhil Goyal 				dev->data->dev_id, qp_id);
2171c3e85bdcSAkhil Goyal 	dev->data->queue_pairs[qp_id] = qp;
2172c3e85bdcSAkhil Goyal 
2173c3e85bdcSAkhil Goyal 	return 0;
2174c3e85bdcSAkhil Goyal }
2175c3e85bdcSAkhil Goyal 
2176c3e85bdcSAkhil Goyal /** Returns the size of session structure */
2177c3e85bdcSAkhil Goyal static unsigned int
2178012c5076SPablo de Lara dpaa_sec_sym_session_get_size(struct rte_cryptodev *dev __rte_unused)
2179c3e85bdcSAkhil Goyal {
2180c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2181c3e85bdcSAkhil Goyal 
2182c3e85bdcSAkhil Goyal 	return sizeof(dpaa_sec_session);
2183c3e85bdcSAkhil Goyal }
2184c3e85bdcSAkhil Goyal 
2185c3e85bdcSAkhil Goyal static int
2186c3e85bdcSAkhil Goyal dpaa_sec_cipher_init(struct rte_cryptodev *dev __rte_unused,
2187c3e85bdcSAkhil Goyal 		     struct rte_crypto_sym_xform *xform,
2188c3e85bdcSAkhil Goyal 		     dpaa_sec_session *session)
2189c3e85bdcSAkhil Goyal {
2190f73d6928SHemant Agrawal 	session->ctxt = DPAA_SEC_CIPHER;
2191c3e85bdcSAkhil Goyal 	session->cipher_alg = xform->cipher.algo;
2192c3e85bdcSAkhil Goyal 	session->iv.length = xform->cipher.iv.length;
2193c3e85bdcSAkhil Goyal 	session->iv.offset = xform->cipher.iv.offset;
2194c3e85bdcSAkhil Goyal 	session->cipher_key.data = rte_zmalloc(NULL, xform->cipher.key.length,
2195c3e85bdcSAkhil Goyal 					       RTE_CACHE_LINE_SIZE);
2196c3e85bdcSAkhil Goyal 	if (session->cipher_key.data == NULL && xform->cipher.key.length > 0) {
2197f163231eSHemant Agrawal 		DPAA_SEC_ERR("No Memory for cipher key");
2198c3e85bdcSAkhil Goyal 		return -ENOMEM;
2199c3e85bdcSAkhil Goyal 	}
2200c3e85bdcSAkhil Goyal 	session->cipher_key.length = xform->cipher.key.length;
2201c3e85bdcSAkhil Goyal 
2202c3e85bdcSAkhil Goyal 	memcpy(session->cipher_key.data, xform->cipher.key.data,
2203c3e85bdcSAkhil Goyal 	       xform->cipher.key.length);
22048524b44eSHemant Agrawal 	switch (xform->cipher.algo) {
22058524b44eSHemant Agrawal 	case RTE_CRYPTO_CIPHER_AES_CBC:
22068524b44eSHemant Agrawal 		session->cipher_key.alg = OP_ALG_ALGSEL_AES;
22078524b44eSHemant Agrawal 		session->cipher_key.algmode = OP_ALG_AAI_CBC;
22088524b44eSHemant Agrawal 		break;
22093e4fbc6cSGagandeep Singh 	case RTE_CRYPTO_CIPHER_DES_CBC:
22103e4fbc6cSGagandeep Singh 		session->cipher_key.alg = OP_ALG_ALGSEL_DES;
22113e4fbc6cSGagandeep Singh 		session->cipher_key.algmode = OP_ALG_AAI_CBC;
22123e4fbc6cSGagandeep Singh 		break;
22138524b44eSHemant Agrawal 	case RTE_CRYPTO_CIPHER_3DES_CBC:
22148524b44eSHemant Agrawal 		session->cipher_key.alg = OP_ALG_ALGSEL_3DES;
22158524b44eSHemant Agrawal 		session->cipher_key.algmode = OP_ALG_AAI_CBC;
22168524b44eSHemant Agrawal 		break;
22178524b44eSHemant Agrawal 	case RTE_CRYPTO_CIPHER_AES_CTR:
22188524b44eSHemant Agrawal 		session->cipher_key.alg = OP_ALG_ALGSEL_AES;
22198524b44eSHemant Agrawal 		session->cipher_key.algmode = OP_ALG_AAI_CTR;
22208524b44eSHemant Agrawal 		break;
22218524b44eSHemant Agrawal 	case RTE_CRYPTO_CIPHER_SNOW3G_UEA2:
22228524b44eSHemant Agrawal 		session->cipher_key.alg = OP_ALG_ALGSEL_SNOW_F8;
22238524b44eSHemant Agrawal 		break;
22248524b44eSHemant Agrawal 	case RTE_CRYPTO_CIPHER_ZUC_EEA3:
22258524b44eSHemant Agrawal 		session->cipher_key.alg = OP_ALG_ALGSEL_ZUCE;
22268524b44eSHemant Agrawal 		break;
22278524b44eSHemant Agrawal 	default:
222877a9b5adSHemant Agrawal 		DPAA_SEC_ERR("Crypto: Unsupported Cipher specified %s (%u)",
222977a9b5adSHemant Agrawal 			      rte_cryptodev_get_cipher_algo_string(xform->cipher.algo),
22308524b44eSHemant Agrawal 				  xform->cipher.algo);
2231c08ced9aSAkhil Goyal 		return -ENOTSUP;
22328524b44eSHemant Agrawal 	}
2233c3e85bdcSAkhil Goyal 	session->dir = (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
2234c3e85bdcSAkhil Goyal 			DIR_ENC : DIR_DEC;
2235c3e85bdcSAkhil Goyal 
2236c3e85bdcSAkhil Goyal 	return 0;
2237c3e85bdcSAkhil Goyal }
2238c3e85bdcSAkhil Goyal 
2239c3e85bdcSAkhil Goyal static int
2240c3e85bdcSAkhil Goyal dpaa_sec_auth_init(struct rte_cryptodev *dev __rte_unused,
2241c3e85bdcSAkhil Goyal 		   struct rte_crypto_sym_xform *xform,
2242c3e85bdcSAkhil Goyal 		   dpaa_sec_session *session)
2243c3e85bdcSAkhil Goyal {
2244f73d6928SHemant Agrawal 	session->ctxt = DPAA_SEC_AUTH;
2245c3e85bdcSAkhil Goyal 	session->auth_alg = xform->auth.algo;
22464c42352cSGagandeep Singh 	session->auth_key.length = xform->auth.key.length;
22474c42352cSGagandeep Singh 	if (xform->auth.key.length) {
22484c42352cSGagandeep Singh 		session->auth_key.data =
22494c42352cSGagandeep Singh 				rte_zmalloc(NULL, xform->auth.key.length,
2250c3e85bdcSAkhil Goyal 					     RTE_CACHE_LINE_SIZE);
22514c42352cSGagandeep Singh 		if (session->auth_key.data == NULL) {
2252f163231eSHemant Agrawal 			DPAA_SEC_ERR("No Memory for auth key");
2253c3e85bdcSAkhil Goyal 			return -ENOMEM;
2254c3e85bdcSAkhil Goyal 		}
22554c42352cSGagandeep Singh 		memcpy(session->auth_key.data, xform->auth.key.data,
22564c42352cSGagandeep Singh 				xform->auth.key.length);
22574c42352cSGagandeep Singh 
22584c42352cSGagandeep Singh 	}
2259c3e85bdcSAkhil Goyal 	session->digest_length = xform->auth.digest_length;
2260c5788a10SHemant Agrawal 	if (session->cipher_alg == RTE_CRYPTO_CIPHER_NULL) {
2261c5788a10SHemant Agrawal 		session->iv.offset = xform->auth.iv.offset;
2262c5788a10SHemant Agrawal 		session->iv.length = xform->auth.iv.length;
2263c5788a10SHemant Agrawal 	}
2264c3e85bdcSAkhil Goyal 
22658524b44eSHemant Agrawal 	switch (xform->auth.algo) {
22664c42352cSGagandeep Singh 	case RTE_CRYPTO_AUTH_SHA1:
22674c42352cSGagandeep Singh 		session->auth_key.alg = OP_ALG_ALGSEL_SHA1;
22684c42352cSGagandeep Singh 		session->auth_key.algmode = OP_ALG_AAI_HASH;
22694c42352cSGagandeep Singh 		break;
22708524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_SHA1_HMAC:
22718524b44eSHemant Agrawal 		session->auth_key.alg = OP_ALG_ALGSEL_SHA1;
22728524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
22738524b44eSHemant Agrawal 		break;
22744c42352cSGagandeep Singh 	case RTE_CRYPTO_AUTH_MD5:
22754c42352cSGagandeep Singh 		session->auth_key.alg = OP_ALG_ALGSEL_MD5;
22764c42352cSGagandeep Singh 		session->auth_key.algmode = OP_ALG_AAI_HASH;
22774c42352cSGagandeep Singh 		break;
22788524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_MD5_HMAC:
22798524b44eSHemant Agrawal 		session->auth_key.alg = OP_ALG_ALGSEL_MD5;
22808524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
22818524b44eSHemant Agrawal 		break;
22824c42352cSGagandeep Singh 	case RTE_CRYPTO_AUTH_SHA224:
22834c42352cSGagandeep Singh 		session->auth_key.alg = OP_ALG_ALGSEL_SHA224;
22844c42352cSGagandeep Singh 		session->auth_key.algmode = OP_ALG_AAI_HASH;
22854c42352cSGagandeep Singh 		break;
22868524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_SHA224_HMAC:
22878524b44eSHemant Agrawal 		session->auth_key.alg = OP_ALG_ALGSEL_SHA224;
22888524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
22898524b44eSHemant Agrawal 		break;
22904c42352cSGagandeep Singh 	case RTE_CRYPTO_AUTH_SHA256:
22914c42352cSGagandeep Singh 		session->auth_key.alg = OP_ALG_ALGSEL_SHA256;
22924c42352cSGagandeep Singh 		session->auth_key.algmode = OP_ALG_AAI_HASH;
22934c42352cSGagandeep Singh 		break;
22948524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_SHA256_HMAC:
22958524b44eSHemant Agrawal 		session->auth_key.alg = OP_ALG_ALGSEL_SHA256;
22968524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
22978524b44eSHemant Agrawal 		break;
22984c42352cSGagandeep Singh 	case RTE_CRYPTO_AUTH_SHA384:
22994c42352cSGagandeep Singh 		session->auth_key.alg = OP_ALG_ALGSEL_SHA384;
23004c42352cSGagandeep Singh 		session->auth_key.algmode = OP_ALG_AAI_HASH;
23014c42352cSGagandeep Singh 		break;
23028524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_SHA384_HMAC:
23038524b44eSHemant Agrawal 		session->auth_key.alg = OP_ALG_ALGSEL_SHA384;
23048524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
23058524b44eSHemant Agrawal 		break;
23064c42352cSGagandeep Singh 	case RTE_CRYPTO_AUTH_SHA512:
23074c42352cSGagandeep Singh 		session->auth_key.alg = OP_ALG_ALGSEL_SHA512;
23084c42352cSGagandeep Singh 		session->auth_key.algmode = OP_ALG_AAI_HASH;
23094c42352cSGagandeep Singh 		break;
23108524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_SHA512_HMAC:
23118524b44eSHemant Agrawal 		session->auth_key.alg = OP_ALG_ALGSEL_SHA512;
23128524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
23138524b44eSHemant Agrawal 		break;
23148524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
23158524b44eSHemant Agrawal 		session->auth_key.alg = OP_ALG_ALGSEL_SNOW_F9;
23168524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_F9;
23178524b44eSHemant Agrawal 		break;
23188524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_ZUC_EIA3:
23198524b44eSHemant Agrawal 		session->auth_key.alg = OP_ALG_ALGSEL_ZUCA;
23208524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_F9;
23218524b44eSHemant Agrawal 		break;
232266f95673SGagandeep Singh 	case RTE_CRYPTO_AUTH_AES_XCBC_MAC:
232366f95673SGagandeep Singh 		session->auth_key.alg = OP_ALG_ALGSEL_AES;
232466f95673SGagandeep Singh 		session->auth_key.algmode = OP_ALG_AAI_XCBC_MAC;
232566f95673SGagandeep Singh 		break;
23262ed12d9bSGagandeep Singh 	case RTE_CRYPTO_AUTH_AES_CMAC:
23272ed12d9bSGagandeep Singh 		session->auth_key.alg = OP_ALG_ALGSEL_AES;
23282ed12d9bSGagandeep Singh 		session->auth_key.algmode = OP_ALG_AAI_CMAC;
23292ed12d9bSGagandeep Singh 		break;
23308524b44eSHemant Agrawal 	default:
233177a9b5adSHemant Agrawal 		DPAA_SEC_ERR("Crypto: Unsupported Auth specified %s (%u)",
233277a9b5adSHemant Agrawal 			rte_cryptodev_get_auth_algo_string(xform->auth.algo),
23338524b44eSHemant Agrawal 			      xform->auth.algo);
2334c08ced9aSAkhil Goyal 		return -ENOTSUP;
23358524b44eSHemant Agrawal 	}
23368524b44eSHemant Agrawal 
2337c3e85bdcSAkhil Goyal 	session->dir = (xform->auth.op == RTE_CRYPTO_AUTH_OP_GENERATE) ?
2338c3e85bdcSAkhil Goyal 			DIR_ENC : DIR_DEC;
2339c3e85bdcSAkhil Goyal 
2340c3e85bdcSAkhil Goyal 	return 0;
2341c3e85bdcSAkhil Goyal }
2342c3e85bdcSAkhil Goyal 
2343c3e85bdcSAkhil Goyal static int
23448524b44eSHemant Agrawal dpaa_sec_chain_init(struct rte_cryptodev *dev __rte_unused,
23458524b44eSHemant Agrawal 		   struct rte_crypto_sym_xform *xform,
23468524b44eSHemant Agrawal 		   dpaa_sec_session *session)
23478524b44eSHemant Agrawal {
23488524b44eSHemant Agrawal 
23498524b44eSHemant Agrawal 	struct rte_crypto_cipher_xform *cipher_xform;
23508524b44eSHemant Agrawal 	struct rte_crypto_auth_xform *auth_xform;
23518524b44eSHemant Agrawal 
2352f73d6928SHemant Agrawal 	session->ctxt = DPAA_SEC_CIPHER_HASH;
23538524b44eSHemant Agrawal 	if (session->auth_cipher_text) {
23548524b44eSHemant Agrawal 		cipher_xform = &xform->cipher;
23558524b44eSHemant Agrawal 		auth_xform = &xform->next->auth;
23568524b44eSHemant Agrawal 	} else {
23578524b44eSHemant Agrawal 		cipher_xform = &xform->next->cipher;
23588524b44eSHemant Agrawal 		auth_xform = &xform->auth;
23598524b44eSHemant Agrawal 	}
23608524b44eSHemant Agrawal 
23618524b44eSHemant Agrawal 	/* Set IV parameters */
23628524b44eSHemant Agrawal 	session->iv.offset = cipher_xform->iv.offset;
23638524b44eSHemant Agrawal 	session->iv.length = cipher_xform->iv.length;
23648524b44eSHemant Agrawal 
23658524b44eSHemant Agrawal 	session->cipher_key.data = rte_zmalloc(NULL, cipher_xform->key.length,
23668524b44eSHemant Agrawal 					       RTE_CACHE_LINE_SIZE);
23678524b44eSHemant Agrawal 	if (session->cipher_key.data == NULL && cipher_xform->key.length > 0) {
23688524b44eSHemant Agrawal 		DPAA_SEC_ERR("No Memory for cipher key");
2369c08ced9aSAkhil Goyal 		return -ENOMEM;
23708524b44eSHemant Agrawal 	}
23718524b44eSHemant Agrawal 	session->cipher_key.length = cipher_xform->key.length;
23728524b44eSHemant Agrawal 	session->auth_key.data = rte_zmalloc(NULL, auth_xform->key.length,
23738524b44eSHemant Agrawal 					     RTE_CACHE_LINE_SIZE);
23748524b44eSHemant Agrawal 	if (session->auth_key.data == NULL && auth_xform->key.length > 0) {
23758524b44eSHemant Agrawal 		DPAA_SEC_ERR("No Memory for auth key");
23768524b44eSHemant Agrawal 		return -ENOMEM;
23778524b44eSHemant Agrawal 	}
23788524b44eSHemant Agrawal 	session->auth_key.length = auth_xform->key.length;
23798524b44eSHemant Agrawal 	memcpy(session->cipher_key.data, cipher_xform->key.data,
23808524b44eSHemant Agrawal 	       cipher_xform->key.length);
23818524b44eSHemant Agrawal 	memcpy(session->auth_key.data, auth_xform->key.data,
23828524b44eSHemant Agrawal 	       auth_xform->key.length);
23838524b44eSHemant Agrawal 
23848524b44eSHemant Agrawal 	session->digest_length = auth_xform->digest_length;
23858524b44eSHemant Agrawal 	session->auth_alg = auth_xform->algo;
23868524b44eSHemant Agrawal 
23878524b44eSHemant Agrawal 	switch (auth_xform->algo) {
23888524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_SHA1_HMAC:
23898524b44eSHemant Agrawal 		session->auth_key.alg = OP_ALG_ALGSEL_SHA1;
23908524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
23918524b44eSHemant Agrawal 		break;
23928524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_MD5_HMAC:
23938524b44eSHemant Agrawal 		session->auth_key.alg = OP_ALG_ALGSEL_MD5;
23948524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
23958524b44eSHemant Agrawal 		break;
23968524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_SHA224_HMAC:
23978524b44eSHemant Agrawal 		session->auth_key.alg = OP_ALG_ALGSEL_SHA224;
23988524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
23998524b44eSHemant Agrawal 		break;
24008524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_SHA256_HMAC:
24018524b44eSHemant Agrawal 		session->auth_key.alg = OP_ALG_ALGSEL_SHA256;
24028524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
24038524b44eSHemant Agrawal 		break;
24048524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_SHA384_HMAC:
24058524b44eSHemant Agrawal 		session->auth_key.alg = OP_ALG_ALGSEL_SHA384;
24068524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
24078524b44eSHemant Agrawal 		break;
24088524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_SHA512_HMAC:
24098524b44eSHemant Agrawal 		session->auth_key.alg = OP_ALG_ALGSEL_SHA512;
24108524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
24118524b44eSHemant Agrawal 		break;
241266f95673SGagandeep Singh 	case RTE_CRYPTO_AUTH_AES_XCBC_MAC:
241366f95673SGagandeep Singh 		session->auth_key.alg = OP_ALG_ALGSEL_AES;
241466f95673SGagandeep Singh 		session->auth_key.algmode = OP_ALG_AAI_XCBC_MAC;
241566f95673SGagandeep Singh 		break;
24162ed12d9bSGagandeep Singh 	case RTE_CRYPTO_AUTH_AES_CMAC:
24172ed12d9bSGagandeep Singh 		session->auth_key.alg = OP_ALG_ALGSEL_AES;
24182ed12d9bSGagandeep Singh 		session->auth_key.algmode = OP_ALG_AAI_CMAC;
24192ed12d9bSGagandeep Singh 		break;
24208524b44eSHemant Agrawal 	default:
242177a9b5adSHemant Agrawal 		DPAA_SEC_ERR("Crypto: Unsupported Auth specified %s (%u)",
242277a9b5adSHemant Agrawal 			rte_cryptodev_get_auth_algo_string(auth_xform->algo),
24238524b44eSHemant Agrawal 			      auth_xform->algo);
2424c08ced9aSAkhil Goyal 		return -ENOTSUP;
24258524b44eSHemant Agrawal 	}
24268524b44eSHemant Agrawal 
24278524b44eSHemant Agrawal 	session->cipher_alg = cipher_xform->algo;
24288524b44eSHemant Agrawal 
24298524b44eSHemant Agrawal 	switch (cipher_xform->algo) {
24308524b44eSHemant Agrawal 	case RTE_CRYPTO_CIPHER_AES_CBC:
24318524b44eSHemant Agrawal 		session->cipher_key.alg = OP_ALG_ALGSEL_AES;
24328524b44eSHemant Agrawal 		session->cipher_key.algmode = OP_ALG_AAI_CBC;
24338524b44eSHemant Agrawal 		break;
24343e4fbc6cSGagandeep Singh 	case RTE_CRYPTO_CIPHER_DES_CBC:
24353e4fbc6cSGagandeep Singh 		session->cipher_key.alg = OP_ALG_ALGSEL_DES;
24363e4fbc6cSGagandeep Singh 		session->cipher_key.algmode = OP_ALG_AAI_CBC;
24373e4fbc6cSGagandeep Singh 		break;
24388524b44eSHemant Agrawal 	case RTE_CRYPTO_CIPHER_3DES_CBC:
24398524b44eSHemant Agrawal 		session->cipher_key.alg = OP_ALG_ALGSEL_3DES;
24408524b44eSHemant Agrawal 		session->cipher_key.algmode = OP_ALG_AAI_CBC;
24418524b44eSHemant Agrawal 		break;
24428524b44eSHemant Agrawal 	case RTE_CRYPTO_CIPHER_AES_CTR:
24438524b44eSHemant Agrawal 		session->cipher_key.alg = OP_ALG_ALGSEL_AES;
24448524b44eSHemant Agrawal 		session->cipher_key.algmode = OP_ALG_AAI_CTR;
24458524b44eSHemant Agrawal 		break;
24468524b44eSHemant Agrawal 	default:
244777a9b5adSHemant Agrawal 
244877a9b5adSHemant Agrawal 		DPAA_SEC_ERR("Crypto: Undefined Cipher specified %s (%u)",
244977a9b5adSHemant Agrawal 			rte_cryptodev_get_cipher_algo_string(cipher_xform->algo),
24508524b44eSHemant Agrawal 			      cipher_xform->algo);
2451c08ced9aSAkhil Goyal 		return -ENOTSUP;
24528524b44eSHemant Agrawal 	}
24538524b44eSHemant Agrawal 	session->dir = (cipher_xform->op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
24548524b44eSHemant Agrawal 				DIR_ENC : DIR_DEC;
24558524b44eSHemant Agrawal 	return 0;
24568524b44eSHemant Agrawal }
24578524b44eSHemant Agrawal 
24588524b44eSHemant Agrawal static int
2459c3e85bdcSAkhil Goyal dpaa_sec_aead_init(struct rte_cryptodev *dev __rte_unused,
2460c3e85bdcSAkhil Goyal 		   struct rte_crypto_sym_xform *xform,
2461c3e85bdcSAkhil Goyal 		   dpaa_sec_session *session)
2462c3e85bdcSAkhil Goyal {
2463c3e85bdcSAkhil Goyal 	session->aead_alg = xform->aead.algo;
24648524b44eSHemant Agrawal 	session->ctxt = DPAA_SEC_AEAD;
2465c3e85bdcSAkhil Goyal 	session->iv.length = xform->aead.iv.length;
2466c3e85bdcSAkhil Goyal 	session->iv.offset = xform->aead.iv.offset;
2467c3e85bdcSAkhil Goyal 	session->auth_only_len = xform->aead.aad_length;
2468c3e85bdcSAkhil Goyal 	session->aead_key.data = rte_zmalloc(NULL, xform->aead.key.length,
2469c3e85bdcSAkhil Goyal 					     RTE_CACHE_LINE_SIZE);
2470c3e85bdcSAkhil Goyal 	if (session->aead_key.data == NULL && xform->aead.key.length > 0) {
2471f665790aSDavid Marchand 		DPAA_SEC_ERR("No Memory for aead key");
2472c3e85bdcSAkhil Goyal 		return -ENOMEM;
2473c3e85bdcSAkhil Goyal 	}
2474c3e85bdcSAkhil Goyal 	session->aead_key.length = xform->aead.key.length;
2475c3e85bdcSAkhil Goyal 	session->digest_length = xform->aead.digest_length;
2476c3e85bdcSAkhil Goyal 
2477c3e85bdcSAkhil Goyal 	memcpy(session->aead_key.data, xform->aead.key.data,
2478c3e85bdcSAkhil Goyal 	       xform->aead.key.length);
24798524b44eSHemant Agrawal 
24808524b44eSHemant Agrawal 	switch (session->aead_alg) {
24818524b44eSHemant Agrawal 	case RTE_CRYPTO_AEAD_AES_GCM:
24828524b44eSHemant Agrawal 		session->aead_key.alg = OP_ALG_ALGSEL_AES;
24838524b44eSHemant Agrawal 		session->aead_key.algmode = OP_ALG_AAI_GCM;
24848524b44eSHemant Agrawal 		break;
24858524b44eSHemant Agrawal 	default:
24868524b44eSHemant Agrawal 		DPAA_SEC_ERR("unsupported AEAD alg %d", session->aead_alg);
2487c08ced9aSAkhil Goyal 		return -ENOTSUP;
24888524b44eSHemant Agrawal 	}
24898524b44eSHemant Agrawal 
2490c3e85bdcSAkhil Goyal 	session->dir = (xform->aead.op == RTE_CRYPTO_AEAD_OP_ENCRYPT) ?
2491c3e85bdcSAkhil Goyal 			DIR_ENC : DIR_DEC;
2492c3e85bdcSAkhil Goyal 
2493c3e85bdcSAkhil Goyal 	return 0;
2494c3e85bdcSAkhil Goyal }
2495c3e85bdcSAkhil Goyal 
2496e79416d1SHemant Agrawal static struct qman_fq *
2497e79416d1SHemant Agrawal dpaa_sec_attach_rxq(struct dpaa_sec_dev_private *qi)
2498c3e85bdcSAkhil Goyal {
2499e79416d1SHemant Agrawal 	unsigned int i;
2500c3e85bdcSAkhil Goyal 
2501fd900d38SGagandeep Singh 	for (i = 0; i < RTE_DPAA_MAX_RX_QUEUE; i++) {
2502e79416d1SHemant Agrawal 		if (qi->inq_attach[i] == 0) {
2503e79416d1SHemant Agrawal 			qi->inq_attach[i] = 1;
2504e79416d1SHemant Agrawal 			return &qi->inq[i];
2505e79416d1SHemant Agrawal 		}
2506e79416d1SHemant Agrawal 	}
2507e621d970SAkhil Goyal 	DPAA_SEC_WARN("All session in use %u", qi->max_nb_sessions);
2508c3e85bdcSAkhil Goyal 
2509e79416d1SHemant Agrawal 	return NULL;
2510c3e85bdcSAkhil Goyal }
2511c3e85bdcSAkhil Goyal 
2512e79416d1SHemant Agrawal static int
2513e79416d1SHemant Agrawal dpaa_sec_detach_rxq(struct dpaa_sec_dev_private *qi, struct qman_fq *fq)
2514e79416d1SHemant Agrawal {
2515e79416d1SHemant Agrawal 	unsigned int i;
2516c9fd1acdSGagandeep Singh 	int ret;
2517e79416d1SHemant Agrawal 
2518fd900d38SGagandeep Singh 	for (i = 0; i < RTE_DPAA_MAX_RX_QUEUE; i++) {
2519e79416d1SHemant Agrawal 		if (&qi->inq[i] == fq) {
2520c9fd1acdSGagandeep Singh 			ret = qman_retire_fq(fq, NULL);
2521c9fd1acdSGagandeep Singh 			if (ret != 0)
2522f665790aSDavid Marchand 				DPAA_SEC_ERR("Queue %d is not retired err: %d",
2523f665790aSDavid Marchand 					     fq->fqid, ret);
2524b4053c4bSAlok Makhariya 			qman_oos_fq(fq);
2525e79416d1SHemant Agrawal 			qi->inq_attach[i] = 0;
2526e79416d1SHemant Agrawal 			return 0;
2527e79416d1SHemant Agrawal 		}
2528e79416d1SHemant Agrawal 	}
2529e79416d1SHemant Agrawal 	return -1;
2530e79416d1SHemant Agrawal }
2531e79416d1SHemant Agrawal 
25329d5f73c2SGagandeep Singh int
2533e79416d1SHemant Agrawal dpaa_sec_attach_sess_q(struct dpaa_sec_qp *qp, dpaa_sec_session *sess)
2534e79416d1SHemant Agrawal {
2535e79416d1SHemant Agrawal 	int ret;
2536e79416d1SHemant Agrawal 
25374e694fe5SAkhil Goyal 	sess->qp[rte_lcore_id() % MAX_DPAA_CORES] = qp;
2538e5872221SRohit Raj 	if (unlikely(!DPAA_PER_LCORE_PORTAL)) {
25395b0f1bd3SAshish Jain 		ret = rte_dpaa_portal_init((void *)0);
25405b0f1bd3SAshish Jain 		if (ret) {
2541f163231eSHemant Agrawal 			DPAA_SEC_ERR("Failure in affining portal");
25425b0f1bd3SAshish Jain 			return ret;
25435b0f1bd3SAshish Jain 		}
25445b0f1bd3SAshish Jain 	}
25454e694fe5SAkhil Goyal 	ret = dpaa_sec_init_rx(sess->inq[rte_lcore_id() % MAX_DPAA_CORES],
2546ec861560SGagandeep Singh 			       rte_dpaa_mem_vtop(&sess->cdb),
2547e79416d1SHemant Agrawal 			       qman_fq_fqid(&qp->outq));
2548e79416d1SHemant Agrawal 	if (ret)
2549f163231eSHemant Agrawal 		DPAA_SEC_ERR("Unable to init sec queue");
2550e79416d1SHemant Agrawal 
2551e79416d1SHemant Agrawal 	return ret;
2552c3e85bdcSAkhil Goyal }
2553c3e85bdcSAkhil Goyal 
25546290de2cSLukasz Wojciechowski static inline void
25556290de2cSLukasz Wojciechowski free_session_data(dpaa_sec_session *s)
25566290de2cSLukasz Wojciechowski {
25576290de2cSLukasz Wojciechowski 	if (is_aead(s))
25586290de2cSLukasz Wojciechowski 		rte_free(s->aead_key.data);
25596290de2cSLukasz Wojciechowski 	else {
25606290de2cSLukasz Wojciechowski 		rte_free(s->auth_key.data);
25616290de2cSLukasz Wojciechowski 		rte_free(s->cipher_key.data);
25626290de2cSLukasz Wojciechowski 	}
25636290de2cSLukasz Wojciechowski 	memset(s, 0, sizeof(dpaa_sec_session));
25646290de2cSLukasz Wojciechowski }
25656290de2cSLukasz Wojciechowski 
2566c3e85bdcSAkhil Goyal static int
2567c3e85bdcSAkhil Goyal dpaa_sec_set_session_parameters(struct rte_cryptodev *dev,
2568c3e85bdcSAkhil Goyal 			    struct rte_crypto_sym_xform *xform,	void *sess)
2569c3e85bdcSAkhil Goyal {
2570c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals = dev->data->dev_private;
2571c3e85bdcSAkhil Goyal 	dpaa_sec_session *session = sess;
25724e694fe5SAkhil Goyal 	uint32_t i;
2573f73d6928SHemant Agrawal 	int ret;
2574c3e85bdcSAkhil Goyal 
2575c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2576c3e85bdcSAkhil Goyal 
2577c3e85bdcSAkhil Goyal 	if (unlikely(sess == NULL)) {
2578f163231eSHemant Agrawal 		DPAA_SEC_ERR("invalid session struct");
2579c3e85bdcSAkhil Goyal 		return -EINVAL;
2580c3e85bdcSAkhil Goyal 	}
2581b0894102SAkhil Goyal 	memset(session, 0, sizeof(dpaa_sec_session));
2582c3e85bdcSAkhil Goyal 
2583c3e85bdcSAkhil Goyal 	/* Default IV length = 0 */
2584c3e85bdcSAkhil Goyal 	session->iv.length = 0;
2585c3e85bdcSAkhil Goyal 
2586c3e85bdcSAkhil Goyal 	/* Cipher Only */
2587c3e85bdcSAkhil Goyal 	if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER && xform->next == NULL) {
2588c3e85bdcSAkhil Goyal 		session->auth_alg = RTE_CRYPTO_AUTH_NULL;
2589f73d6928SHemant Agrawal 		ret = dpaa_sec_cipher_init(dev, xform, session);
2590c3e85bdcSAkhil Goyal 
2591c3e85bdcSAkhil Goyal 	/* Authentication Only */
2592c3e85bdcSAkhil Goyal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
2593c3e85bdcSAkhil Goyal 		   xform->next == NULL) {
2594c3e85bdcSAkhil Goyal 		session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
25958524b44eSHemant Agrawal 		session->ctxt = DPAA_SEC_AUTH;
2596f73d6928SHemant Agrawal 		ret = dpaa_sec_auth_init(dev, xform, session);
2597c3e85bdcSAkhil Goyal 
2598c3e85bdcSAkhil Goyal 	/* Cipher then Authenticate */
2599c3e85bdcSAkhil Goyal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
2600c3e85bdcSAkhil Goyal 		   xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
2601c3e85bdcSAkhil Goyal 		if (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) {
26028524b44eSHemant Agrawal 			session->auth_cipher_text = 1;
2603f73d6928SHemant Agrawal 			if (xform->cipher.algo == RTE_CRYPTO_CIPHER_NULL)
2604f73d6928SHemant Agrawal 				ret = dpaa_sec_auth_init(dev, xform, session);
2605f73d6928SHemant Agrawal 			else if (xform->next->auth.algo == RTE_CRYPTO_AUTH_NULL)
2606f73d6928SHemant Agrawal 				ret = dpaa_sec_cipher_init(dev, xform, session);
2607f73d6928SHemant Agrawal 			else
2608f73d6928SHemant Agrawal 				ret = dpaa_sec_chain_init(dev, xform, session);
2609c3e85bdcSAkhil Goyal 		} else {
2610f163231eSHemant Agrawal 			DPAA_SEC_ERR("Not supported: Auth then Cipher");
2611c08ced9aSAkhil Goyal 			return -ENOTSUP;
2612c3e85bdcSAkhil Goyal 		}
2613c3e85bdcSAkhil Goyal 	/* Authenticate then Cipher */
2614c3e85bdcSAkhil Goyal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
2615c3e85bdcSAkhil Goyal 		   xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
2616c3e85bdcSAkhil Goyal 		if (xform->next->cipher.op == RTE_CRYPTO_CIPHER_OP_DECRYPT) {
26178524b44eSHemant Agrawal 			session->auth_cipher_text = 0;
2618f73d6928SHemant Agrawal 			if (xform->auth.algo == RTE_CRYPTO_AUTH_NULL)
2619f73d6928SHemant Agrawal 				ret = dpaa_sec_cipher_init(dev, xform, session);
2620f73d6928SHemant Agrawal 			else if (xform->next->cipher.algo
2621f73d6928SHemant Agrawal 					== RTE_CRYPTO_CIPHER_NULL)
2622f73d6928SHemant Agrawal 				ret = dpaa_sec_auth_init(dev, xform, session);
2623f73d6928SHemant Agrawal 			else
2624f73d6928SHemant Agrawal 				ret = dpaa_sec_chain_init(dev, xform, session);
2625c3e85bdcSAkhil Goyal 		} else {
2626f163231eSHemant Agrawal 			DPAA_SEC_ERR("Not supported: Auth then Cipher");
2627c08ced9aSAkhil Goyal 			return -ENOTSUP;
2628c3e85bdcSAkhil Goyal 		}
2629c3e85bdcSAkhil Goyal 
2630c3e85bdcSAkhil Goyal 	/* AEAD operation for AES-GCM kind of Algorithms */
2631c3e85bdcSAkhil Goyal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD &&
2632c3e85bdcSAkhil Goyal 		   xform->next == NULL) {
2633f73d6928SHemant Agrawal 		ret = dpaa_sec_aead_init(dev, xform, session);
2634c3e85bdcSAkhil Goyal 
2635c3e85bdcSAkhil Goyal 	} else {
2636f163231eSHemant Agrawal 		DPAA_SEC_ERR("Invalid crypto type");
2637c3e85bdcSAkhil Goyal 		return -EINVAL;
2638c3e85bdcSAkhil Goyal 	}
2639f73d6928SHemant Agrawal 	if (ret) {
2640f73d6928SHemant Agrawal 		DPAA_SEC_ERR("unable to init session");
2641f73d6928SHemant Agrawal 		goto err1;
2642f73d6928SHemant Agrawal 	}
2643f73d6928SHemant Agrawal 
26443b617ee7SAkhil Goyal 	rte_spinlock_lock(&internals->lock);
26454e694fe5SAkhil Goyal 	for (i = 0; i < MAX_DPAA_CORES; i++) {
26464e694fe5SAkhil Goyal 		session->inq[i] = dpaa_sec_attach_rxq(internals);
26474e694fe5SAkhil Goyal 		if (session->inq[i] == NULL) {
2648f163231eSHemant Agrawal 			DPAA_SEC_ERR("unable to attach sec queue");
26494e694fe5SAkhil Goyal 			rte_spinlock_unlock(&internals->lock);
2650c08ced9aSAkhil Goyal 			ret = -EBUSY;
2651e79416d1SHemant Agrawal 			goto err1;
2652e79416d1SHemant Agrawal 		}
26534e694fe5SAkhil Goyal 	}
26544e694fe5SAkhil Goyal 	rte_spinlock_unlock(&internals->lock);
2655c3e85bdcSAkhil Goyal 
2656c3e85bdcSAkhil Goyal 	return 0;
2657e79416d1SHemant Agrawal 
2658e79416d1SHemant Agrawal err1:
26596290de2cSLukasz Wojciechowski 	free_session_data(session);
2660c08ced9aSAkhil Goyal 	return ret;
2661c3e85bdcSAkhil Goyal }
2662c3e85bdcSAkhil Goyal 
2663c3e85bdcSAkhil Goyal static int
2664012c5076SPablo de Lara dpaa_sec_sym_session_configure(struct rte_cryptodev *dev,
2665c3e85bdcSAkhil Goyal 		struct rte_crypto_sym_xform *xform,
2666bdce2564SAkhil Goyal 		struct rte_cryptodev_sym_session *sess)
2667c3e85bdcSAkhil Goyal {
26682a440d6aSAkhil Goyal 	void *sess_private_data = CRYPTODEV_GET_SYM_SESS_PRIV(sess);
2669c3e85bdcSAkhil Goyal 	int ret;
2670c3e85bdcSAkhil Goyal 
2671c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2672c3e85bdcSAkhil Goyal 
2673c3e85bdcSAkhil Goyal 	ret = dpaa_sec_set_session_parameters(dev, xform, sess_private_data);
2674c3e85bdcSAkhil Goyal 	if (ret != 0) {
2675f163231eSHemant Agrawal 		DPAA_SEC_ERR("failed to configure session parameters");
2676c3e85bdcSAkhil Goyal 		return ret;
2677c3e85bdcSAkhil Goyal 	}
2678c3e85bdcSAkhil Goyal 
267976da1b51SGagandeep Singh 	ret = dpaa_sec_prep_cdb(sess_private_data);
268076da1b51SGagandeep Singh 	if (ret) {
268176da1b51SGagandeep Singh 		DPAA_SEC_ERR("Unable to prepare sec cdb");
268276da1b51SGagandeep Singh 		return ret;
268376da1b51SGagandeep Singh 	}
2684e79416d1SHemant Agrawal 
2685c3e85bdcSAkhil Goyal 	return 0;
2686c3e85bdcSAkhil Goyal }
2687c3e85bdcSAkhil Goyal 
26883d0d5332SAkhil Goyal static inline void
26893d0d5332SAkhil Goyal free_session_memory(struct rte_cryptodev *dev, dpaa_sec_session *s)
2690c3e85bdcSAkhil Goyal {
2691e79416d1SHemant Agrawal 	struct dpaa_sec_dev_private *qi = dev->data->dev_private;
26923d0d5332SAkhil Goyal 	uint8_t i;
2693e79416d1SHemant Agrawal 
2694e621d970SAkhil Goyal 	for (i = 0; i < MAX_DPAA_CORES; i++) {
2695e621d970SAkhil Goyal 		if (s->inq[i])
2696e621d970SAkhil Goyal 			dpaa_sec_detach_rxq(qi, s->inq[i]);
2697e621d970SAkhil Goyal 		s->inq[i] = NULL;
2698e621d970SAkhil Goyal 		s->qp[i] = NULL;
2699e621d970SAkhil Goyal 	}
27006290de2cSLukasz Wojciechowski 	free_session_data(s);
27013d0d5332SAkhil Goyal }
27023d0d5332SAkhil Goyal 
27033d0d5332SAkhil Goyal /** Clear the memory of session so it doesn't leave key material behind */
27043d0d5332SAkhil Goyal static void
27053d0d5332SAkhil Goyal dpaa_sec_sym_session_clear(struct rte_cryptodev *dev,
27063d0d5332SAkhil Goyal 		struct rte_cryptodev_sym_session *sess)
27073d0d5332SAkhil Goyal {
27083d0d5332SAkhil Goyal 	PMD_INIT_FUNC_TRACE();
27092a440d6aSAkhil Goyal 	void *sess_priv = CRYPTODEV_GET_SYM_SESS_PRIV(sess);
27103d0d5332SAkhil Goyal 	dpaa_sec_session *s = (dpaa_sec_session *)sess_priv;
27113d0d5332SAkhil Goyal 
27123d0d5332SAkhil Goyal 	free_session_memory(dev, s);
2713c3e85bdcSAkhil Goyal }
2714c3e85bdcSAkhil Goyal 
2715c3e85bdcSAkhil Goyal static int
27162c318722SHemant Agrawal dpaa_sec_ipsec_aead_init(struct rte_crypto_aead_xform *aead_xform,
27172c318722SHemant Agrawal 			struct rte_security_ipsec_xform *ipsec_xform,
27182c318722SHemant Agrawal 			dpaa_sec_session *session)
27191f14d500SAkhil Goyal {
27201f14d500SAkhil Goyal 	PMD_INIT_FUNC_TRACE();
27211f14d500SAkhil Goyal 
27222c318722SHemant Agrawal 	session->aead_key.data = rte_zmalloc(NULL, aead_xform->key.length,
27232c318722SHemant Agrawal 					       RTE_CACHE_LINE_SIZE);
27242c318722SHemant Agrawal 	if (session->aead_key.data == NULL && aead_xform->key.length > 0) {
27252c318722SHemant Agrawal 		DPAA_SEC_ERR("No Memory for aead key");
2726c08ced9aSAkhil Goyal 		return -ENOMEM;
27271f14d500SAkhil Goyal 	}
27282c318722SHemant Agrawal 	memcpy(session->aead_key.data, aead_xform->key.data,
27292c318722SHemant Agrawal 	       aead_xform->key.length);
273005b12700SHemant Agrawal 
27312c318722SHemant Agrawal 	session->digest_length = aead_xform->digest_length;
27322c318722SHemant Agrawal 	session->aead_key.length = aead_xform->key.length;
27332c318722SHemant Agrawal 
27342c318722SHemant Agrawal 	switch (aead_xform->algo) {
27352c318722SHemant Agrawal 	case RTE_CRYPTO_AEAD_AES_GCM:
27362c318722SHemant Agrawal 		switch (session->digest_length) {
27372c318722SHemant Agrawal 		case 8:
27382c318722SHemant Agrawal 			session->aead_key.alg = OP_PCL_IPSEC_AES_GCM8;
27392c318722SHemant Agrawal 			break;
27402c318722SHemant Agrawal 		case 12:
27412c318722SHemant Agrawal 			session->aead_key.alg = OP_PCL_IPSEC_AES_GCM12;
27422c318722SHemant Agrawal 			break;
27432c318722SHemant Agrawal 		case 16:
27442c318722SHemant Agrawal 			session->aead_key.alg = OP_PCL_IPSEC_AES_GCM16;
27452c318722SHemant Agrawal 			break;
27462c318722SHemant Agrawal 		default:
27472c318722SHemant Agrawal 			DPAA_SEC_ERR("Crypto: Undefined GCM digest %d",
27482c318722SHemant Agrawal 				     session->digest_length);
2749c08ced9aSAkhil Goyal 			return -EINVAL;
27502c318722SHemant Agrawal 		}
27512c318722SHemant Agrawal 		if (session->dir == DIR_ENC) {
27522c318722SHemant Agrawal 			memcpy(session->encap_pdb.gcm.salt,
27532c318722SHemant Agrawal 				(uint8_t *)&(ipsec_xform->salt), 4);
27542c318722SHemant Agrawal 		} else {
27552c318722SHemant Agrawal 			memcpy(session->decap_pdb.gcm.salt,
27562c318722SHemant Agrawal 				(uint8_t *)&(ipsec_xform->salt), 4);
27572c318722SHemant Agrawal 		}
27582c318722SHemant Agrawal 		session->aead_key.algmode = OP_ALG_AAI_GCM;
27592c318722SHemant Agrawal 		session->aead_alg = RTE_CRYPTO_AEAD_AES_GCM;
27602c318722SHemant Agrawal 		break;
27612c318722SHemant Agrawal 	default:
27622c318722SHemant Agrawal 		DPAA_SEC_ERR("Crypto: Undefined AEAD specified %u",
27632c318722SHemant Agrawal 			      aead_xform->algo);
2764c08ced9aSAkhil Goyal 		return -ENOTSUP;
27652c318722SHemant Agrawal 	}
27662c318722SHemant Agrawal 	return 0;
27672c318722SHemant Agrawal }
27682c318722SHemant Agrawal 
27692c318722SHemant Agrawal static int
27702c318722SHemant Agrawal dpaa_sec_ipsec_proto_init(struct rte_crypto_cipher_xform *cipher_xform,
27712c318722SHemant Agrawal 	struct rte_crypto_auth_xform *auth_xform,
27721cdfbb0bSVakul Garg 	struct rte_security_ipsec_xform *ipsec_xform,
27732c318722SHemant Agrawal 	dpaa_sec_session *session)
27742c318722SHemant Agrawal {
27752c318722SHemant Agrawal 	if (cipher_xform) {
27761f14d500SAkhil Goyal 		session->cipher_key.data = rte_zmalloc(NULL,
27771f14d500SAkhil Goyal 						       cipher_xform->key.length,
27781f14d500SAkhil Goyal 						       RTE_CACHE_LINE_SIZE);
27791f14d500SAkhil Goyal 		if (session->cipher_key.data == NULL &&
27801f14d500SAkhil Goyal 				cipher_xform->key.length > 0) {
2781f163231eSHemant Agrawal 			DPAA_SEC_ERR("No Memory for cipher key");
27821f14d500SAkhil Goyal 			return -ENOMEM;
27831f14d500SAkhil Goyal 		}
27842c318722SHemant Agrawal 
27852c318722SHemant Agrawal 		session->cipher_key.length = cipher_xform->key.length;
278605b12700SHemant Agrawal 		memcpy(session->cipher_key.data, cipher_xform->key.data,
278705b12700SHemant Agrawal 				cipher_xform->key.length);
278805b12700SHemant Agrawal 		session->cipher_alg = cipher_xform->algo;
278905b12700SHemant Agrawal 	} else {
279005b12700SHemant Agrawal 		session->cipher_key.data = NULL;
279105b12700SHemant Agrawal 		session->cipher_key.length = 0;
279205b12700SHemant Agrawal 		session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
279305b12700SHemant Agrawal 	}
279405b12700SHemant Agrawal 
27952c318722SHemant Agrawal 	if (auth_xform) {
27961f14d500SAkhil Goyal 		session->auth_key.data = rte_zmalloc(NULL,
27971f14d500SAkhil Goyal 						auth_xform->key.length,
27981f14d500SAkhil Goyal 						RTE_CACHE_LINE_SIZE);
27991f14d500SAkhil Goyal 		if (session->auth_key.data == NULL &&
28001f14d500SAkhil Goyal 				auth_xform->key.length > 0) {
2801f163231eSHemant Agrawal 			DPAA_SEC_ERR("No Memory for auth key");
28021f14d500SAkhil Goyal 			return -ENOMEM;
28031f14d500SAkhil Goyal 		}
28042c318722SHemant Agrawal 		session->auth_key.length = auth_xform->key.length;
28051f14d500SAkhil Goyal 		memcpy(session->auth_key.data, auth_xform->key.data,
28061f14d500SAkhil Goyal 				auth_xform->key.length);
28072c318722SHemant Agrawal 		session->auth_alg = auth_xform->algo;
2808247b6908SHemant Agrawal 		session->digest_length = auth_xform->digest_length;
28092c318722SHemant Agrawal 	} else {
28102c318722SHemant Agrawal 		session->auth_key.data = NULL;
28112c318722SHemant Agrawal 		session->auth_key.length = 0;
28122c318722SHemant Agrawal 		session->auth_alg = RTE_CRYPTO_AUTH_NULL;
28132c318722SHemant Agrawal 	}
28141f14d500SAkhil Goyal 
28152c318722SHemant Agrawal 	switch (session->auth_alg) {
28168524b44eSHemant Agrawal 	case RTE_CRYPTO_AUTH_SHA1_HMAC:
28178524b44eSHemant Agrawal 		session->auth_key.alg = OP_PCL_IPSEC_HMAC_SHA1_96;
28188524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
28198524b44eSHemant Agrawal 		break;
28202c318722SHemant Agrawal 	case RTE_CRYPTO_AUTH_MD5_HMAC:
28212c318722SHemant Agrawal 		session->auth_key.alg = OP_PCL_IPSEC_HMAC_MD5_96;
28228524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
28238524b44eSHemant Agrawal 		break;
28241f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA256_HMAC:
28258524b44eSHemant Agrawal 		session->auth_key.alg = OP_PCL_IPSEC_HMAC_SHA2_256_128;
28268524b44eSHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
2827247b6908SHemant Agrawal 		if (session->digest_length != 16)
2828247b6908SHemant Agrawal 			DPAA_SEC_WARN(
2829247b6908SHemant Agrawal 			"+++Using sha256-hmac truncated len is non-standard,"
2830247b6908SHemant Agrawal 			"it will not work with lookaside proto");
28318524b44eSHemant Agrawal 		break;
2832c51ccb96SHemant Agrawal 	case RTE_CRYPTO_AUTH_SHA224_HMAC:
2833c51ccb96SHemant Agrawal 		session->auth_key.algmode = OP_ALG_AAI_HMAC;
2834c51ccb96SHemant Agrawal 		if (session->digest_length == 6)
2835c51ccb96SHemant Agrawal 			session->auth_key.alg = OP_PCL_IPSEC_HMAC_SHA2_224_96;
2836c51ccb96SHemant Agrawal 		else if (session->digest_length == 14)
2837c51ccb96SHemant Agrawal 			session->auth_key.alg = OP_PCL_IPSEC_HMAC_SHA2_224_224;
2838c51ccb96SHemant Agrawal 		else
2839c51ccb96SHemant Agrawal 			session->auth_key.alg = OP_PCL_IPSEC_HMAC_SHA2_224_112;
2840c51ccb96SHemant 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;
28602c318722SHemant Agrawal 	default:
286177a9b5adSHemant Agrawal 		DPAA_SEC_ERR("Crypto: Unsupported auth alg %s (%u)",
286277a9b5adSHemant Agrawal 			rte_cryptodev_get_auth_algo_string(session->auth_alg),
28632c318722SHemant Agrawal 			      session->auth_alg);
2864c08ced9aSAkhil Goyal 		return -ENOTSUP;
28652c318722SHemant Agrawal 	}
28662c318722SHemant Agrawal 
28672c318722SHemant Agrawal 	switch (session->cipher_alg) {
28682c318722SHemant Agrawal 	case RTE_CRYPTO_CIPHER_AES_CBC:
28692c318722SHemant Agrawal 		session->cipher_key.alg = OP_PCL_IPSEC_AES_CBC;
28702c318722SHemant Agrawal 		session->cipher_key.algmode = OP_ALG_AAI_CBC;
28712c318722SHemant Agrawal 		break;
28723e4fbc6cSGagandeep Singh 	case RTE_CRYPTO_CIPHER_DES_CBC:
28733e4fbc6cSGagandeep Singh 		session->cipher_key.alg = OP_PCL_IPSEC_DES;
28743e4fbc6cSGagandeep Singh 		session->cipher_key.algmode = OP_ALG_AAI_CBC;
28753e4fbc6cSGagandeep Singh 		break;
28762c318722SHemant Agrawal 	case RTE_CRYPTO_CIPHER_3DES_CBC:
28772c318722SHemant Agrawal 		session->cipher_key.alg = OP_PCL_IPSEC_3DES;
28782c318722SHemant Agrawal 		session->cipher_key.algmode = OP_ALG_AAI_CBC;
28792c318722SHemant Agrawal 		break;
28802c318722SHemant Agrawal 	case RTE_CRYPTO_CIPHER_AES_CTR:
28812c318722SHemant Agrawal 		session->cipher_key.alg = OP_PCL_IPSEC_AES_CTR;
28822c318722SHemant Agrawal 		session->cipher_key.algmode = OP_ALG_AAI_CTR;
28831cdfbb0bSVakul Garg 		if (session->dir == DIR_ENC) {
28841cdfbb0bSVakul Garg 			session->encap_pdb.ctr.ctr_initial = 0x00000001;
28851cdfbb0bSVakul Garg 			session->encap_pdb.ctr.ctr_nonce = ipsec_xform->salt;
28861cdfbb0bSVakul Garg 		} else {
28871cdfbb0bSVakul Garg 			session->decap_pdb.ctr.ctr_initial = 0x00000001;
28881cdfbb0bSVakul Garg 			session->decap_pdb.ctr.ctr_nonce = ipsec_xform->salt;
28891cdfbb0bSVakul Garg 		}
28902c318722SHemant Agrawal 		break;
28912c318722SHemant Agrawal 	case RTE_CRYPTO_CIPHER_NULL:
28922c318722SHemant Agrawal 		session->cipher_key.alg = OP_PCL_IPSEC_NULL;
28932c318722SHemant Agrawal 		break;
28942c318722SHemant Agrawal 	default:
289577a9b5adSHemant Agrawal 		DPAA_SEC_ERR("Crypto: Unsupported Cipher alg %s (%u)",
289677a9b5adSHemant Agrawal 			rte_cryptodev_get_cipher_algo_string(session->cipher_alg),
28972c318722SHemant Agrawal 			      session->cipher_alg);
2898c08ced9aSAkhil Goyal 		return -ENOTSUP;
28992c318722SHemant Agrawal 	}
29002c318722SHemant Agrawal 
29012c318722SHemant Agrawal 	return 0;
29022c318722SHemant Agrawal }
29032c318722SHemant Agrawal 
29042c318722SHemant Agrawal static int
29052c318722SHemant Agrawal dpaa_sec_set_ipsec_session(__rte_unused struct rte_cryptodev *dev,
29062c318722SHemant Agrawal 			   struct rte_security_session_conf *conf,
29072c318722SHemant Agrawal 			   void *sess)
29082c318722SHemant Agrawal {
29092c318722SHemant Agrawal 	struct dpaa_sec_dev_private *internals = dev->data->dev_private;
29102c318722SHemant Agrawal 	struct rte_security_ipsec_xform *ipsec_xform = &conf->ipsec;
29112c318722SHemant Agrawal 	struct rte_crypto_auth_xform *auth_xform = NULL;
29122c318722SHemant Agrawal 	struct rte_crypto_cipher_xform *cipher_xform = NULL;
29132c318722SHemant Agrawal 	struct rte_crypto_aead_xform *aead_xform = NULL;
29142c318722SHemant Agrawal 	dpaa_sec_session *session = (dpaa_sec_session *)sess;
29152c318722SHemant Agrawal 	uint32_t i;
29162c318722SHemant Agrawal 	int ret;
29172c318722SHemant Agrawal 
29182c318722SHemant Agrawal 	PMD_INIT_FUNC_TRACE();
29192c318722SHemant Agrawal 
29202c318722SHemant Agrawal 	memset(session, 0, sizeof(dpaa_sec_session));
29212c318722SHemant Agrawal 	session->proto_alg = conf->protocol;
29222c318722SHemant Agrawal 	session->ctxt = DPAA_SEC_IPSEC;
29232c318722SHemant Agrawal 
29248f4125c1SGagandeep Singh 	if (ipsec_xform->life.bytes_hard_limit != 0 ||
29258f4125c1SGagandeep Singh 	    ipsec_xform->life.bytes_soft_limit != 0 ||
29268f4125c1SGagandeep Singh 	    ipsec_xform->life.packets_hard_limit != 0 ||
29278f4125c1SGagandeep Singh 	    ipsec_xform->life.packets_soft_limit != 0)
29288f4125c1SGagandeep Singh 		return -ENOTSUP;
29298f4125c1SGagandeep Singh 
29302c318722SHemant Agrawal 	if (ipsec_xform->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS)
29312c318722SHemant Agrawal 		session->dir = DIR_ENC;
29322c318722SHemant Agrawal 	else
29332c318722SHemant Agrawal 		session->dir = DIR_DEC;
29342c318722SHemant Agrawal 
29352c318722SHemant Agrawal 	if (conf->crypto_xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
29362c318722SHemant Agrawal 		cipher_xform = &conf->crypto_xform->cipher;
29372c318722SHemant Agrawal 		if (conf->crypto_xform->next)
29382c318722SHemant Agrawal 			auth_xform = &conf->crypto_xform->next->auth;
29392c318722SHemant Agrawal 		ret = dpaa_sec_ipsec_proto_init(cipher_xform, auth_xform,
29401cdfbb0bSVakul Garg 					ipsec_xform, session);
29412c318722SHemant Agrawal 	} else if (conf->crypto_xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
29422c318722SHemant Agrawal 		auth_xform = &conf->crypto_xform->auth;
29432c318722SHemant Agrawal 		if (conf->crypto_xform->next)
29442c318722SHemant Agrawal 			cipher_xform = &conf->crypto_xform->next->cipher;
29452c318722SHemant Agrawal 		ret = dpaa_sec_ipsec_proto_init(cipher_xform, auth_xform,
29461cdfbb0bSVakul Garg 					ipsec_xform, session);
29472c318722SHemant Agrawal 	} else if (conf->crypto_xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
29482c318722SHemant Agrawal 		aead_xform = &conf->crypto_xform->aead;
29492c318722SHemant Agrawal 		ret = dpaa_sec_ipsec_aead_init(aead_xform,
29502c318722SHemant Agrawal 					ipsec_xform, session);
29512c318722SHemant Agrawal 	} else {
29522c318722SHemant Agrawal 		DPAA_SEC_ERR("XFORM not specified");
29532c318722SHemant Agrawal 		ret = -EINVAL;
29541f14d500SAkhil Goyal 		goto out;
29551f14d500SAkhil Goyal 	}
29562c318722SHemant Agrawal 	if (ret) {
29572c318722SHemant Agrawal 		DPAA_SEC_ERR("Failed to process xform");
29582c318722SHemant Agrawal 		goto out;
29591f14d500SAkhil Goyal 	}
29601f14d500SAkhil Goyal 
29611f14d500SAkhil Goyal 	if (ipsec_xform->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
29625ab35d2eSAkhil Goyal 		if (ipsec_xform->tunnel.type ==
29635ab35d2eSAkhil Goyal 				RTE_SECURITY_IPSEC_TUNNEL_IPV4) {
29641f14d500SAkhil Goyal 			session->ip4_hdr.ip_v = IPVERSION;
29651f14d500SAkhil Goyal 			session->ip4_hdr.ip_hl = 5;
2966*32d8bc55SBarry Cao 			if (ipsec_xform->options.udp_encap)
2967*32d8bc55SBarry Cao 				session->ip4_hdr.ip_len = rte_cpu_to_be_16(
2968*32d8bc55SBarry Cao 					sizeof(session->ip4_hdr) + sizeof(struct rte_udp_hdr));
2969*32d8bc55SBarry Cao 			else
29701f14d500SAkhil Goyal 				session->ip4_hdr.ip_len = rte_cpu_to_be_16(
29711f14d500SAkhil Goyal 					sizeof(session->ip4_hdr));
29721f14d500SAkhil Goyal 			session->ip4_hdr.ip_tos = ipsec_xform->tunnel.ipv4.dscp;
29731f14d500SAkhil Goyal 			session->ip4_hdr.ip_id = 0;
29741f14d500SAkhil Goyal 			session->ip4_hdr.ip_off = 0;
29751f14d500SAkhil Goyal 			session->ip4_hdr.ip_ttl = ipsec_xform->tunnel.ipv4.ttl;
2976*32d8bc55SBarry Cao 			if (ipsec_xform->options.udp_encap)
2977*32d8bc55SBarry Cao 				session->ip4_hdr.ip_p = IPPROTO_UDP;
2978*32d8bc55SBarry Cao 			else
29791f14d500SAkhil Goyal 				session->ip4_hdr.ip_p = (ipsec_xform->proto ==
29805ab35d2eSAkhil Goyal 						RTE_SECURITY_IPSEC_SA_PROTO_ESP) ?
29815ab35d2eSAkhil Goyal 						IPPROTO_ESP : IPPROTO_AH;
29821f14d500SAkhil Goyal 			session->ip4_hdr.ip_sum = 0;
29835ab35d2eSAkhil Goyal 			session->ip4_hdr.ip_src =
29845ab35d2eSAkhil Goyal 					ipsec_xform->tunnel.ipv4.src_ip;
29855ab35d2eSAkhil Goyal 			session->ip4_hdr.ip_dst =
29865ab35d2eSAkhil Goyal 					ipsec_xform->tunnel.ipv4.dst_ip;
29871f14d500SAkhil Goyal 			session->ip4_hdr.ip_sum = calc_chksum((uint16_t *)
29881f14d500SAkhil Goyal 						(void *)&session->ip4_hdr,
29891f14d500SAkhil Goyal 						sizeof(struct ip));
29905ab35d2eSAkhil Goyal 			session->encap_pdb.ip_hdr_len = sizeof(struct ip);
29915ab35d2eSAkhil Goyal 		} else if (ipsec_xform->tunnel.type ==
29925ab35d2eSAkhil Goyal 				RTE_SECURITY_IPSEC_TUNNEL_IPV6) {
29935ab35d2eSAkhil Goyal 			session->ip6_hdr.vtc_flow = rte_cpu_to_be_32(
29945ab35d2eSAkhil Goyal 				DPAA_IPv6_DEFAULT_VTC_FLOW |
29955ab35d2eSAkhil Goyal 				((ipsec_xform->tunnel.ipv6.dscp <<
29965ab35d2eSAkhil Goyal 					RTE_IPV6_HDR_TC_SHIFT) &
29975ab35d2eSAkhil Goyal 					RTE_IPV6_HDR_TC_MASK) |
29985ab35d2eSAkhil Goyal 				((ipsec_xform->tunnel.ipv6.flabel <<
29995ab35d2eSAkhil Goyal 					RTE_IPV6_HDR_FL_SHIFT) &
30005ab35d2eSAkhil Goyal 					RTE_IPV6_HDR_FL_MASK));
30015ab35d2eSAkhil Goyal 			/* Payload length will be updated by HW */
30025ab35d2eSAkhil Goyal 			session->ip6_hdr.payload_len = 0;
30035ab35d2eSAkhil Goyal 			session->ip6_hdr.hop_limits =
30045ab35d2eSAkhil Goyal 					ipsec_xform->tunnel.ipv6.hlimit;
3005*32d8bc55SBarry Cao 			if (ipsec_xform->options.udp_encap)
3006*32d8bc55SBarry Cao 				session->ip6_hdr.proto = IPPROTO_UDP;
3007*32d8bc55SBarry Cao 			else
30085ab35d2eSAkhil Goyal 				session->ip6_hdr.proto = (ipsec_xform->proto ==
30095ab35d2eSAkhil Goyal 						RTE_SECURITY_IPSEC_SA_PROTO_ESP) ?
30105ab35d2eSAkhil Goyal 						IPPROTO_ESP : IPPROTO_AH;
30115ab35d2eSAkhil Goyal 			memcpy(&session->ip6_hdr.src_addr,
30125ab35d2eSAkhil Goyal 					&ipsec_xform->tunnel.ipv6.src_addr, 16);
30135ab35d2eSAkhil Goyal 			memcpy(&session->ip6_hdr.dst_addr,
30145ab35d2eSAkhil Goyal 					&ipsec_xform->tunnel.ipv6.dst_addr, 16);
30155ab35d2eSAkhil Goyal 			session->encap_pdb.ip_hdr_len =
30165ab35d2eSAkhil Goyal 						sizeof(struct rte_ipv6_hdr);
30175ab35d2eSAkhil Goyal 		}
30180aa5986cSGagandeep Singh 
30191f14d500SAkhil Goyal 		session->encap_pdb.options =
30201f14d500SAkhil Goyal 			(IPVERSION << PDBNH_ESP_ENCAP_SHIFT) |
30211f14d500SAkhil Goyal 			PDBOPTS_ESP_OIHI_PDB_INL |
30221f14d500SAkhil Goyal 			PDBOPTS_ESP_IVSRC |
302379fde9d0SAkhil Goyal 			PDBHMO_ESP_SNR;
30240aa5986cSGagandeep Singh 		if (ipsec_xform->options.dec_ttl)
30250aa5986cSGagandeep Singh 			session->encap_pdb.options |= PDBHMO_ESP_ENCAP_DTTL;
30261f14d500SAkhil Goyal 		session->encap_pdb.spi = ipsec_xform->spi;
3027123098cdSGagandeep Singh 		/* Initializing the sequence number to 1, Security
3028123098cdSGagandeep Singh 		 * engine will choose this sequence number for first packet
3029123098cdSGagandeep Singh 		 * Refer: RFC4303 section: 3.3.3.Sequence Number Generation
3030123098cdSGagandeep Singh 		 */
3031123098cdSGagandeep Singh 		session->encap_pdb.seq_num = 1;
3032123098cdSGagandeep Singh 		if (ipsec_xform->options.esn) {
3033123098cdSGagandeep Singh 			session->encap_pdb.options |= PDBOPTS_ESP_ESN;
3034123098cdSGagandeep Singh 			session->encap_pdb.seq_num_ext_hi = conf->ipsec.esn.hi;
3035123098cdSGagandeep Singh 			session->encap_pdb.seq_num = conf->ipsec.esn.low;
3036123098cdSGagandeep Singh 		}
3037*32d8bc55SBarry Cao 		if (ipsec_xform->options.udp_encap) {
3038*32d8bc55SBarry Cao 			struct rte_udp_hdr *udp_hdr;
30392c318722SHemant Agrawal 
3040*32d8bc55SBarry Cao 			if (ipsec_xform->tunnel.type == RTE_SECURITY_IPSEC_TUNNEL_IPV4)
3041*32d8bc55SBarry Cao 				udp_hdr = (struct rte_udp_hdr *)(&session->udp4.udp_hdr);
3042*32d8bc55SBarry Cao 			else
3043*32d8bc55SBarry Cao 				udp_hdr = (struct rte_udp_hdr *)(&session->udp6.udp_hdr);
3044*32d8bc55SBarry Cao 
3045*32d8bc55SBarry Cao 			if (ipsec_xform->udp.sport)
3046*32d8bc55SBarry Cao 				udp_hdr->src_port = rte_cpu_to_be_16(ipsec_xform->udp.sport);
3047*32d8bc55SBarry Cao 			else
3048*32d8bc55SBarry Cao 				udp_hdr->src_port = rte_cpu_to_be_16(DPAA_DEFAULT_NAT_T_PORT);
3049*32d8bc55SBarry Cao 
3050*32d8bc55SBarry Cao 			if (ipsec_xform->udp.dport)
3051*32d8bc55SBarry Cao 				udp_hdr->dst_port = rte_cpu_to_be_16(ipsec_xform->udp.dport);
3052*32d8bc55SBarry Cao 			else
3053*32d8bc55SBarry Cao 				udp_hdr->dst_port = rte_cpu_to_be_16(DPAA_DEFAULT_NAT_T_PORT);
3054*32d8bc55SBarry Cao 			udp_hdr->dgram_len = 0;
3055*32d8bc55SBarry Cao 			udp_hdr->dgram_cksum = 0;
3056*32d8bc55SBarry Cao 
3057*32d8bc55SBarry Cao 			session->encap_pdb.ip_hdr_len += sizeof(struct rte_udp_hdr);
3058*32d8bc55SBarry Cao 			session->encap_pdb.options |= PDBOPTS_ESP_NAT | PDBOPTS_ESP_NUC;
3059*32d8bc55SBarry Cao 		}
3060c253236aSHemant Agrawal 		if (ipsec_xform->options.ecn)
3061c253236aSHemant Agrawal 			session->encap_pdb.options |= PDBOPTS_ESP_TECN;
30621f14d500SAkhil Goyal 	} else if (ipsec_xform->direction ==
30631f14d500SAkhil Goyal 			RTE_SECURITY_IPSEC_SA_DIR_INGRESS) {
3064c253236aSHemant Agrawal 		if (ipsec_xform->tunnel.type == RTE_SECURITY_IPSEC_TUNNEL_IPV4) {
3065*32d8bc55SBarry Cao 			if (ipsec_xform->options.udp_encap)
3066*32d8bc55SBarry Cao 				session->decap_pdb.options =
3067*32d8bc55SBarry Cao 					(sizeof(struct ip) + sizeof(struct rte_udp_hdr)) << 16;
3068*32d8bc55SBarry Cao 			else
30691f14d500SAkhil Goyal 				session->decap_pdb.options = sizeof(struct ip) << 16;
3070c253236aSHemant Agrawal 			if (ipsec_xform->options.copy_df)
3071c253236aSHemant Agrawal 				session->decap_pdb.options |= PDBHMO_ESP_DFV;
3072c253236aSHemant Agrawal 		} else {
3073*32d8bc55SBarry Cao 			if (ipsec_xform->options.udp_encap)
30745ab35d2eSAkhil Goyal 				session->decap_pdb.options =
3075*32d8bc55SBarry Cao 				(sizeof(struct rte_ipv6_hdr) + sizeof(struct rte_udp_hdr)) << 16;
3076*32d8bc55SBarry Cao 			else
3077*32d8bc55SBarry Cao 				session->decap_pdb.options = sizeof(struct rte_ipv6_hdr) << 16;
3078c253236aSHemant Agrawal 		}
3079123098cdSGagandeep Singh 		if (ipsec_xform->options.esn) {
30800f318781SAkhil Goyal 			session->decap_pdb.options |= PDBOPTS_ESP_ESN;
3081123098cdSGagandeep Singh 			session->decap_pdb.seq_num_ext_hi = conf->ipsec.esn.hi;
3082123098cdSGagandeep Singh 			session->decap_pdb.seq_num = conf->ipsec.esn.low;
3083123098cdSGagandeep Singh 		}
3084c253236aSHemant Agrawal 		if (ipsec_xform->options.copy_dscp)
3085c253236aSHemant Agrawal 			session->decap_pdb.options |= PDBHMO_ESP_DIFFSERV;
3086c253236aSHemant Agrawal 		if (ipsec_xform->options.ecn)
3087c253236aSHemant Agrawal 			session->decap_pdb.options |= PDBOPTS_ESP_TECN;
3088c253236aSHemant Agrawal 
3089a37ce227SHemant Agrawal 		if (ipsec_xform->replay_win_sz) {
3090a37ce227SHemant Agrawal 			uint32_t win_sz;
3091a37ce227SHemant Agrawal 			win_sz = rte_align32pow2(ipsec_xform->replay_win_sz);
3092a37ce227SHemant Agrawal 
3093a37ce227SHemant Agrawal 			switch (win_sz) {
3094a37ce227SHemant Agrawal 			case 1:
3095a37ce227SHemant Agrawal 			case 2:
3096a37ce227SHemant Agrawal 			case 4:
3097a37ce227SHemant Agrawal 			case 8:
3098a37ce227SHemant Agrawal 			case 16:
3099a37ce227SHemant Agrawal 			case 32:
3100a37ce227SHemant Agrawal 				session->decap_pdb.options |= PDBOPTS_ESP_ARS32;
3101a37ce227SHemant Agrawal 				break;
3102a37ce227SHemant Agrawal 			case 64:
3103a37ce227SHemant Agrawal 				session->decap_pdb.options |= PDBOPTS_ESP_ARS64;
3104a37ce227SHemant Agrawal 				break;
3105a37ce227SHemant Agrawal 			default:
3106a37ce227SHemant Agrawal 				session->decap_pdb.options |=
3107a37ce227SHemant Agrawal 							PDBOPTS_ESP_ARS128;
3108a37ce227SHemant Agrawal 			}
3109a37ce227SHemant Agrawal 		}
31101f14d500SAkhil Goyal 	} else
31111f14d500SAkhil Goyal 		goto out;
31123b617ee7SAkhil Goyal 	rte_spinlock_lock(&internals->lock);
31134e694fe5SAkhil Goyal 	for (i = 0; i < MAX_DPAA_CORES; i++) {
31144e694fe5SAkhil Goyal 		session->inq[i] = dpaa_sec_attach_rxq(internals);
31154e694fe5SAkhil Goyal 		if (session->inq[i] == NULL) {
3116f163231eSHemant Agrawal 			DPAA_SEC_ERR("unable to attach sec queue");
31174e694fe5SAkhil Goyal 			rte_spinlock_unlock(&internals->lock);
31181f14d500SAkhil Goyal 			goto out;
31191f14d500SAkhil Goyal 		}
31204e694fe5SAkhil Goyal 	}
31214e694fe5SAkhil Goyal 	rte_spinlock_unlock(&internals->lock);
31221f14d500SAkhil Goyal 
3123a1173d55SHemant Agrawal 	return 0;
3124a1173d55SHemant Agrawal out:
31256290de2cSLukasz Wojciechowski 	free_session_data(session);
3126a1173d55SHemant Agrawal 	return -1;
3127a1173d55SHemant Agrawal }
31281f14d500SAkhil Goyal 
3129a1173d55SHemant Agrawal static int
3130a1173d55SHemant Agrawal dpaa_sec_set_pdcp_session(struct rte_cryptodev *dev,
3131a1173d55SHemant Agrawal 			  struct rte_security_session_conf *conf,
3132a1173d55SHemant Agrawal 			  void *sess)
3133a1173d55SHemant Agrawal {
3134a1173d55SHemant Agrawal 	struct rte_security_pdcp_xform *pdcp_xform = &conf->pdcp;
3135a1173d55SHemant Agrawal 	struct rte_crypto_sym_xform *xform = conf->crypto_xform;
3136a1173d55SHemant Agrawal 	struct rte_crypto_auth_xform *auth_xform = NULL;
3137a1173d55SHemant Agrawal 	struct rte_crypto_cipher_xform *cipher_xform = NULL;
3138a1173d55SHemant Agrawal 	dpaa_sec_session *session = (dpaa_sec_session *)sess;
3139a1173d55SHemant Agrawal 	struct dpaa_sec_dev_private *dev_priv = dev->data->dev_private;
31404e694fe5SAkhil Goyal 	uint32_t i;
3141c08ced9aSAkhil Goyal 	int ret;
3142a1173d55SHemant Agrawal 
3143a1173d55SHemant Agrawal 	PMD_INIT_FUNC_TRACE();
3144a1173d55SHemant Agrawal 
3145a1173d55SHemant Agrawal 	memset(session, 0, sizeof(dpaa_sec_session));
3146a1173d55SHemant Agrawal 
3147a1173d55SHemant Agrawal 	/* find xfrm types */
3148a1173d55SHemant Agrawal 	if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
3149a1173d55SHemant Agrawal 		cipher_xform = &xform->cipher;
315099cc26f6SHemant Agrawal 		if (xform->next != NULL &&
315199cc26f6SHemant Agrawal 			xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH)
3152a1173d55SHemant Agrawal 			auth_xform = &xform->next->auth;
3153a1173d55SHemant Agrawal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
3154a1173d55SHemant Agrawal 		auth_xform = &xform->auth;
315599cc26f6SHemant Agrawal 		if (xform->next != NULL &&
315699cc26f6SHemant Agrawal 			xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER)
3157a1173d55SHemant Agrawal 			cipher_xform = &xform->next->cipher;
3158a1173d55SHemant Agrawal 	} else {
3159a1173d55SHemant Agrawal 		DPAA_SEC_ERR("Invalid crypto type");
3160a1173d55SHemant Agrawal 		return -EINVAL;
3161a1173d55SHemant Agrawal 	}
3162a1173d55SHemant Agrawal 
3163a1173d55SHemant Agrawal 	session->proto_alg = conf->protocol;
31648524b44eSHemant Agrawal 	session->ctxt = DPAA_SEC_PDCP;
31658524b44eSHemant Agrawal 
3166a1173d55SHemant Agrawal 	if (cipher_xform) {
31678524b44eSHemant Agrawal 		switch (cipher_xform->algo) {
31688524b44eSHemant Agrawal 		case RTE_CRYPTO_CIPHER_SNOW3G_UEA2:
31698524b44eSHemant Agrawal 			session->cipher_key.alg = PDCP_CIPHER_TYPE_SNOW;
31708524b44eSHemant Agrawal 			break;
31718524b44eSHemant Agrawal 		case RTE_CRYPTO_CIPHER_ZUC_EEA3:
31728524b44eSHemant Agrawal 			session->cipher_key.alg = PDCP_CIPHER_TYPE_ZUC;
31738524b44eSHemant Agrawal 			break;
31748524b44eSHemant Agrawal 		case RTE_CRYPTO_CIPHER_AES_CTR:
31758524b44eSHemant Agrawal 			session->cipher_key.alg = PDCP_CIPHER_TYPE_AES;
31768524b44eSHemant Agrawal 			break;
31778524b44eSHemant Agrawal 		case RTE_CRYPTO_CIPHER_NULL:
31788524b44eSHemant Agrawal 			session->cipher_key.alg = PDCP_CIPHER_TYPE_NULL;
31798524b44eSHemant Agrawal 			break;
31808524b44eSHemant Agrawal 		default:
31818524b44eSHemant Agrawal 			DPAA_SEC_ERR("Crypto: Undefined Cipher specified %u",
31828524b44eSHemant Agrawal 				      session->cipher_alg);
3183c08ced9aSAkhil Goyal 			return -EINVAL;
31848524b44eSHemant Agrawal 		}
31858524b44eSHemant Agrawal 
3186a1173d55SHemant Agrawal 		session->cipher_key.data = rte_zmalloc(NULL,
3187a1173d55SHemant Agrawal 					       cipher_xform->key.length,
3188a1173d55SHemant Agrawal 					       RTE_CACHE_LINE_SIZE);
3189a1173d55SHemant Agrawal 		if (session->cipher_key.data == NULL &&
3190a1173d55SHemant Agrawal 				cipher_xform->key.length > 0) {
3191a1173d55SHemant Agrawal 			DPAA_SEC_ERR("No Memory for cipher key");
3192a1173d55SHemant Agrawal 			return -ENOMEM;
3193a1173d55SHemant Agrawal 		}
3194a1173d55SHemant Agrawal 		session->cipher_key.length = cipher_xform->key.length;
3195a1173d55SHemant Agrawal 		memcpy(session->cipher_key.data, cipher_xform->key.data,
3196a1173d55SHemant Agrawal 			cipher_xform->key.length);
3197a1173d55SHemant Agrawal 		session->dir = (cipher_xform->op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
3198a1173d55SHemant Agrawal 					DIR_ENC : DIR_DEC;
3199a1173d55SHemant Agrawal 		session->cipher_alg = cipher_xform->algo;
3200a1173d55SHemant Agrawal 	} else {
3201a1173d55SHemant Agrawal 		session->cipher_key.data = NULL;
3202a1173d55SHemant Agrawal 		session->cipher_key.length = 0;
3203a1173d55SHemant Agrawal 		session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
3204a1173d55SHemant Agrawal 		session->dir = DIR_ENC;
3205a1173d55SHemant Agrawal 	}
3206a1173d55SHemant Agrawal 
3207a1173d55SHemant Agrawal 	if (pdcp_xform->domain == RTE_SECURITY_PDCP_MODE_CONTROL) {
3208eac60082SVakul Garg 		if (pdcp_xform->sn_size != RTE_SECURITY_PDCP_SN_SIZE_5 &&
3209eac60082SVakul Garg 		    pdcp_xform->sn_size != RTE_SECURITY_PDCP_SN_SIZE_12) {
3210a1173d55SHemant Agrawal 			DPAA_SEC_ERR(
3211eac60082SVakul Garg 				"PDCP Seq Num size should be 5/12 bits for cmode");
3212c08ced9aSAkhil Goyal 			ret = -EINVAL;
3213a1173d55SHemant Agrawal 			goto out;
3214a1173d55SHemant Agrawal 		}
32152e4cbdb4SVakul Garg 	}
32162e4cbdb4SVakul Garg 
3217a1173d55SHemant Agrawal 	if (auth_xform) {
32188524b44eSHemant Agrawal 		switch (auth_xform->algo) {
32198524b44eSHemant Agrawal 		case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
32208524b44eSHemant Agrawal 			session->auth_key.alg = PDCP_AUTH_TYPE_SNOW;
32218524b44eSHemant Agrawal 			break;
32228524b44eSHemant Agrawal 		case RTE_CRYPTO_AUTH_ZUC_EIA3:
32238524b44eSHemant Agrawal 			session->auth_key.alg = PDCP_AUTH_TYPE_ZUC;
32248524b44eSHemant Agrawal 			break;
32258524b44eSHemant Agrawal 		case RTE_CRYPTO_AUTH_AES_CMAC:
32268524b44eSHemant Agrawal 			session->auth_key.alg = PDCP_AUTH_TYPE_AES;
32278524b44eSHemant Agrawal 			break;
32288524b44eSHemant Agrawal 		case RTE_CRYPTO_AUTH_NULL:
32298524b44eSHemant Agrawal 			session->auth_key.alg = PDCP_AUTH_TYPE_NULL;
32308524b44eSHemant Agrawal 			break;
32318524b44eSHemant Agrawal 		default:
323277a9b5adSHemant Agrawal 			DPAA_SEC_ERR("Crypto: Unsupported auth alg %s (%u)",
323377a9b5adSHemant Agrawal 			rte_cryptodev_get_auth_algo_string(session->auth_alg),
32348524b44eSHemant Agrawal 				      session->auth_alg);
32358524b44eSHemant Agrawal 			rte_free(session->cipher_key.data);
3236c08ced9aSAkhil Goyal 			return -EINVAL;
32378524b44eSHemant Agrawal 		}
3238a1173d55SHemant Agrawal 		session->auth_key.data = rte_zmalloc(NULL,
3239a1173d55SHemant Agrawal 						     auth_xform->key.length,
3240a1173d55SHemant Agrawal 						     RTE_CACHE_LINE_SIZE);
32412e4cbdb4SVakul Garg 		if (!session->auth_key.data &&
3242a1173d55SHemant Agrawal 		    auth_xform->key.length > 0) {
3243a1173d55SHemant Agrawal 			DPAA_SEC_ERR("No Memory for auth key");
3244a1173d55SHemant Agrawal 			rte_free(session->cipher_key.data);
3245a1173d55SHemant Agrawal 			return -ENOMEM;
3246a1173d55SHemant Agrawal 		}
3247a1173d55SHemant Agrawal 		session->auth_key.length = auth_xform->key.length;
3248a1173d55SHemant Agrawal 		memcpy(session->auth_key.data, auth_xform->key.data,
3249a1173d55SHemant Agrawal 		       auth_xform->key.length);
3250a1173d55SHemant Agrawal 		session->auth_alg = auth_xform->algo;
3251a1173d55SHemant Agrawal 	} else {
32521182b364SGagandeep Singh 		if (pdcp_xform->domain == RTE_SECURITY_PDCP_MODE_CONTROL) {
32531182b364SGagandeep Singh 			DPAA_SEC_ERR("Crypto: Integrity must for c-plane");
32541182b364SGagandeep Singh 			ret = -EINVAL;
32551182b364SGagandeep Singh 			goto out;
32561182b364SGagandeep Singh 		}
3257a1173d55SHemant Agrawal 		session->auth_key.data = NULL;
3258a1173d55SHemant Agrawal 		session->auth_key.length = 0;
32592e4cbdb4SVakul Garg 		session->auth_alg = 0;
3260a1173d55SHemant Agrawal 	}
3261a1173d55SHemant Agrawal 	session->pdcp.domain = pdcp_xform->domain;
3262a1173d55SHemant Agrawal 	session->pdcp.bearer = pdcp_xform->bearer;
3263a1173d55SHemant Agrawal 	session->pdcp.pkt_dir = pdcp_xform->pkt_dir;
3264a1173d55SHemant Agrawal 	session->pdcp.sn_size = pdcp_xform->sn_size;
3265a1173d55SHemant Agrawal 	session->pdcp.hfn = pdcp_xform->hfn;
3266a1173d55SHemant Agrawal 	session->pdcp.hfn_threshold = pdcp_xform->hfn_threshold;
32676a0c9d36SAkhil Goyal 	session->pdcp.hfn_ovd = pdcp_xform->hfn_ovrd;
32685a4954bcSAkhil Goyal 	session->pdcp.sdap_enabled = pdcp_xform->sdap_enabled;
32698839c8a1SYunjian Wang 	if (cipher_xform)
32706a0c9d36SAkhil Goyal 		session->pdcp.hfn_ovd_offset = cipher_xform->iv.offset;
3271a1173d55SHemant Agrawal 
3272a1173d55SHemant Agrawal 	rte_spinlock_lock(&dev_priv->lock);
32734e694fe5SAkhil Goyal 	for (i = 0; i < MAX_DPAA_CORES; i++) {
32744e694fe5SAkhil Goyal 		session->inq[i] = dpaa_sec_attach_rxq(dev_priv);
32754e694fe5SAkhil Goyal 		if (session->inq[i] == NULL) {
3276a1173d55SHemant Agrawal 			DPAA_SEC_ERR("unable to attach sec queue");
32774e694fe5SAkhil Goyal 			rte_spinlock_unlock(&dev_priv->lock);
3278c08ced9aSAkhil Goyal 			ret = -EBUSY;
3279a1173d55SHemant Agrawal 			goto out;
3280a1173d55SHemant Agrawal 		}
32814e694fe5SAkhil Goyal 	}
32824e694fe5SAkhil Goyal 	rte_spinlock_unlock(&dev_priv->lock);
32831f14d500SAkhil Goyal 	return 0;
32841f14d500SAkhil Goyal out:
32851f14d500SAkhil Goyal 	rte_free(session->auth_key.data);
32861f14d500SAkhil Goyal 	rte_free(session->cipher_key.data);
32871f14d500SAkhil Goyal 	memset(session, 0, sizeof(dpaa_sec_session));
3288c08ced9aSAkhil Goyal 	return ret;
32891f14d500SAkhil Goyal }
32901f14d500SAkhil Goyal 
32911f14d500SAkhil Goyal static int
32921f14d500SAkhil Goyal dpaa_sec_security_session_create(void *dev,
32931f14d500SAkhil Goyal 				 struct rte_security_session_conf *conf,
32943f3fc330SAkhil Goyal 				 struct rte_security_session *sess)
32951f14d500SAkhil Goyal {
32963f3fc330SAkhil Goyal 	void *sess_private_data = SECURITY_GET_SESS_PRIV(sess);
32971f14d500SAkhil Goyal 	struct rte_cryptodev *cdev = (struct rte_cryptodev *)dev;
32981f14d500SAkhil Goyal 	int ret;
32991f14d500SAkhil Goyal 
33001f14d500SAkhil Goyal 	switch (conf->protocol) {
33011f14d500SAkhil Goyal 	case RTE_SECURITY_PROTOCOL_IPSEC:
33021f14d500SAkhil Goyal 		ret = dpaa_sec_set_ipsec_session(cdev, conf,
33031f14d500SAkhil Goyal 				sess_private_data);
33041f14d500SAkhil Goyal 		break;
3305a1173d55SHemant Agrawal 	case RTE_SECURITY_PROTOCOL_PDCP:
3306a1173d55SHemant Agrawal 		ret = dpaa_sec_set_pdcp_session(cdev, conf,
3307a1173d55SHemant Agrawal 				sess_private_data);
3308a1173d55SHemant Agrawal 		break;
33091f14d500SAkhil Goyal 	case RTE_SECURITY_PROTOCOL_MACSEC:
33101f14d500SAkhil Goyal 		return -ENOTSUP;
33111f14d500SAkhil Goyal 	default:
33121f14d500SAkhil Goyal 		return -EINVAL;
33131f14d500SAkhil Goyal 	}
33141f14d500SAkhil Goyal 	if (ret != 0) {
3315f163231eSHemant Agrawal 		DPAA_SEC_ERR("failed to configure session parameters");
33161f14d500SAkhil Goyal 		return ret;
33171f14d500SAkhil Goyal 	}
33181f14d500SAkhil Goyal 
331976da1b51SGagandeep Singh 	ret = dpaa_sec_prep_cdb(sess_private_data);
332076da1b51SGagandeep Singh 	if (ret) {
332176da1b51SGagandeep Singh 		DPAA_SEC_ERR("Unable to prepare sec cdb");
332276da1b51SGagandeep Singh 		return ret;
332376da1b51SGagandeep Singh 	}
332476da1b51SGagandeep Singh 
33251f14d500SAkhil Goyal 	return ret;
33261f14d500SAkhil Goyal }
33271f14d500SAkhil Goyal 
33281f14d500SAkhil Goyal /** Clear the memory of session so it doesn't leave key material behind */
33291f14d500SAkhil Goyal static int
33301f14d500SAkhil Goyal dpaa_sec_security_session_destroy(void *dev __rte_unused,
33311f14d500SAkhil Goyal 		struct rte_security_session *sess)
33321f14d500SAkhil Goyal {
33331f14d500SAkhil Goyal 	PMD_INIT_FUNC_TRACE();
33343f3fc330SAkhil Goyal 	void *sess_priv = SECURITY_GET_SESS_PRIV(sess);
33351f14d500SAkhil Goyal 	dpaa_sec_session *s = (dpaa_sec_session *)sess_priv;
33361f14d500SAkhil Goyal 
33371f14d500SAkhil Goyal 	if (sess_priv) {
33383d0d5332SAkhil Goyal 		free_session_memory((struct rte_cryptodev *)dev, s);
33391f14d500SAkhil Goyal 	}
33401f14d500SAkhil Goyal 	return 0;
33411f14d500SAkhil Goyal }
334266837861SAkhil Goyal 
334366837861SAkhil Goyal static unsigned int
334466837861SAkhil Goyal dpaa_sec_security_session_get_size(void *device __rte_unused)
334566837861SAkhil Goyal {
334666837861SAkhil Goyal 	return sizeof(dpaa_sec_session);
334766837861SAkhil Goyal }
334866837861SAkhil Goyal 
33491f14d500SAkhil Goyal static int
33502ffb940eSAkhil Goyal dpaa_sec_dev_configure(struct rte_cryptodev *dev __rte_unused,
3351c3e85bdcSAkhil Goyal 		       struct rte_cryptodev_config *config __rte_unused)
3352c3e85bdcSAkhil Goyal {
3353c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
3354c3e85bdcSAkhil Goyal 
3355c3e85bdcSAkhil Goyal 	return 0;
3356c3e85bdcSAkhil Goyal }
3357c3e85bdcSAkhil Goyal 
3358c3e85bdcSAkhil Goyal static int
3359c3e85bdcSAkhil Goyal dpaa_sec_dev_start(struct rte_cryptodev *dev __rte_unused)
3360c3e85bdcSAkhil Goyal {
3361c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
3362c3e85bdcSAkhil Goyal 	return 0;
3363c3e85bdcSAkhil Goyal }
3364c3e85bdcSAkhil Goyal 
3365c3e85bdcSAkhil Goyal static void
3366c3e85bdcSAkhil Goyal dpaa_sec_dev_stop(struct rte_cryptodev *dev __rte_unused)
3367c3e85bdcSAkhil Goyal {
3368c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
3369c3e85bdcSAkhil Goyal }
3370c3e85bdcSAkhil Goyal 
3371c3e85bdcSAkhil Goyal static int
33727e3e2954SAkhil Goyal dpaa_sec_dev_close(struct rte_cryptodev *dev)
3373c3e85bdcSAkhil Goyal {
3374c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
33757e3e2954SAkhil Goyal 
33767e3e2954SAkhil Goyal 	if (dev == NULL)
33777e3e2954SAkhil Goyal 		return -ENOMEM;
33787e3e2954SAkhil Goyal 
3379c3e85bdcSAkhil Goyal 	return 0;
3380c3e85bdcSAkhil Goyal }
3381c3e85bdcSAkhil Goyal 
3382c3e85bdcSAkhil Goyal static void
3383c3e85bdcSAkhil Goyal dpaa_sec_dev_infos_get(struct rte_cryptodev *dev,
3384c3e85bdcSAkhil Goyal 		       struct rte_cryptodev_info *info)
3385c3e85bdcSAkhil Goyal {
3386c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals = dev->data->dev_private;
3387c3e85bdcSAkhil Goyal 
3388c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
3389c3e85bdcSAkhil Goyal 	if (info != NULL) {
3390c3e85bdcSAkhil Goyal 		info->max_nb_queue_pairs = internals->max_nb_queue_pairs;
3391c3e85bdcSAkhil Goyal 		info->feature_flags = dev->feature_flags;
3392c3e85bdcSAkhil Goyal 		info->capabilities = dpaa_sec_capabilities;
3393c3e85bdcSAkhil Goyal 		info->sym.max_nb_sessions = internals->max_nb_sessions;
33949d5f73c2SGagandeep Singh 		info->driver_id = dpaa_cryptodev_driver_id;
3395c3e85bdcSAkhil Goyal 	}
3396c3e85bdcSAkhil Goyal }
3397c3e85bdcSAkhil Goyal 
3398fe3688baSAkhil Goyal static enum qman_cb_dqrr_result
3399fe3688baSAkhil Goyal dpaa_sec_process_parallel_event(void *event,
3400fe3688baSAkhil Goyal 			struct qman_portal *qm __always_unused,
3401fe3688baSAkhil Goyal 			struct qman_fq *outq,
3402fe3688baSAkhil Goyal 			const struct qm_dqrr_entry *dqrr,
3403fe3688baSAkhil Goyal 			void **bufs)
3404fe3688baSAkhil Goyal {
3405fe3688baSAkhil Goyal 	const struct qm_fd *fd;
3406fe3688baSAkhil Goyal 	struct dpaa_sec_job *job;
3407fe3688baSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
3408fe3688baSAkhil Goyal 	struct rte_event *ev = (struct rte_event *)event;
3409fe3688baSAkhil Goyal 
3410fe3688baSAkhil Goyal 	fd = &dqrr->fd;
3411fe3688baSAkhil Goyal 
3412fe3688baSAkhil Goyal 	/* sg is embedded in an op ctx,
3413fe3688baSAkhil Goyal 	 * sg[0] is for output
3414fe3688baSAkhil Goyal 	 * sg[1] for input
3415fe3688baSAkhil Goyal 	 */
3416ec861560SGagandeep Singh 	job = rte_dpaa_mem_ptov(qm_fd_addr_get64(fd));
3417fe3688baSAkhil Goyal 
3418fe3688baSAkhil Goyal 	ctx = container_of(job, struct dpaa_sec_op_ctx, job);
3419fe3688baSAkhil Goyal 	ctx->fd_status = fd->status;
3420fe3688baSAkhil Goyal 	if (ctx->op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
3421fe3688baSAkhil Goyal 		struct qm_sg_entry *sg_out;
3422fe3688baSAkhil Goyal 		uint32_t len;
3423fe3688baSAkhil Goyal 
3424fe3688baSAkhil Goyal 		sg_out = &job->sg[0];
3425fe3688baSAkhil Goyal 		hw_sg_to_cpu(sg_out);
3426fe3688baSAkhil Goyal 		len = sg_out->length;
3427fe3688baSAkhil Goyal 		ctx->op->sym->m_src->pkt_len = len;
3428fe3688baSAkhil Goyal 		ctx->op->sym->m_src->data_len = len;
3429fe3688baSAkhil Goyal 	}
3430fe3688baSAkhil Goyal 	if (!ctx->fd_status) {
3431fe3688baSAkhil Goyal 		ctx->op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
3432fe3688baSAkhil Goyal 	} else {
3433fe3688baSAkhil Goyal 		DPAA_SEC_DP_WARN("SEC return err: 0x%x", ctx->fd_status);
3434fe3688baSAkhil Goyal 		ctx->op->status = RTE_CRYPTO_OP_STATUS_ERROR;
3435fe3688baSAkhil Goyal 	}
3436fe3688baSAkhil Goyal 	ev->event_ptr = (void *)ctx->op;
3437fe3688baSAkhil Goyal 
3438fe3688baSAkhil Goyal 	ev->flow_id = outq->ev.flow_id;
3439fe3688baSAkhil Goyal 	ev->sub_event_type = outq->ev.sub_event_type;
3440fe3688baSAkhil Goyal 	ev->event_type = RTE_EVENT_TYPE_CRYPTODEV;
3441fe3688baSAkhil Goyal 	ev->op = RTE_EVENT_OP_NEW;
3442fe3688baSAkhil Goyal 	ev->sched_type = outq->ev.sched_type;
3443fe3688baSAkhil Goyal 	ev->queue_id = outq->ev.queue_id;
3444fe3688baSAkhil Goyal 	ev->priority = outq->ev.priority;
3445fe3688baSAkhil Goyal 	*bufs = (void *)ctx->op;
3446fe3688baSAkhil Goyal 
3447fe3688baSAkhil Goyal 	rte_mempool_put(ctx->ctx_pool, (void *)ctx);
3448fe3688baSAkhil Goyal 
3449fe3688baSAkhil Goyal 	return qman_cb_dqrr_consume;
3450fe3688baSAkhil Goyal }
3451fe3688baSAkhil Goyal 
3452fe3688baSAkhil Goyal static enum qman_cb_dqrr_result
3453fe3688baSAkhil Goyal dpaa_sec_process_atomic_event(void *event,
3454fe3688baSAkhil Goyal 			struct qman_portal *qm __rte_unused,
3455fe3688baSAkhil Goyal 			struct qman_fq *outq,
3456fe3688baSAkhil Goyal 			const struct qm_dqrr_entry *dqrr,
3457fe3688baSAkhil Goyal 			void **bufs)
3458fe3688baSAkhil Goyal {
3459fe3688baSAkhil Goyal 	u8 index;
3460fe3688baSAkhil Goyal 	const struct qm_fd *fd;
3461fe3688baSAkhil Goyal 	struct dpaa_sec_job *job;
3462fe3688baSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
3463fe3688baSAkhil Goyal 	struct rte_event *ev = (struct rte_event *)event;
3464fe3688baSAkhil Goyal 
3465fe3688baSAkhil Goyal 	fd = &dqrr->fd;
3466fe3688baSAkhil Goyal 
3467fe3688baSAkhil Goyal 	/* sg is embedded in an op ctx,
3468fe3688baSAkhil Goyal 	 * sg[0] is for output
3469fe3688baSAkhil Goyal 	 * sg[1] for input
3470fe3688baSAkhil Goyal 	 */
3471ec861560SGagandeep Singh 	job = rte_dpaa_mem_ptov(qm_fd_addr_get64(fd));
3472fe3688baSAkhil Goyal 
3473fe3688baSAkhil Goyal 	ctx = container_of(job, struct dpaa_sec_op_ctx, job);
3474fe3688baSAkhil Goyal 	ctx->fd_status = fd->status;
3475fe3688baSAkhil Goyal 	if (ctx->op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
3476fe3688baSAkhil Goyal 		struct qm_sg_entry *sg_out;
3477fe3688baSAkhil Goyal 		uint32_t len;
3478fe3688baSAkhil Goyal 
3479fe3688baSAkhil Goyal 		sg_out = &job->sg[0];
3480fe3688baSAkhil Goyal 		hw_sg_to_cpu(sg_out);
3481fe3688baSAkhil Goyal 		len = sg_out->length;
3482fe3688baSAkhil Goyal 		ctx->op->sym->m_src->pkt_len = len;
3483fe3688baSAkhil Goyal 		ctx->op->sym->m_src->data_len = len;
3484fe3688baSAkhil Goyal 	}
3485fe3688baSAkhil Goyal 	if (!ctx->fd_status) {
3486fe3688baSAkhil Goyal 		ctx->op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
3487fe3688baSAkhil Goyal 	} else {
3488fe3688baSAkhil Goyal 		DPAA_SEC_DP_WARN("SEC return err: 0x%x", ctx->fd_status);
3489fe3688baSAkhil Goyal 		ctx->op->status = RTE_CRYPTO_OP_STATUS_ERROR;
3490fe3688baSAkhil Goyal 	}
3491fe3688baSAkhil Goyal 	ev->event_ptr = (void *)ctx->op;
3492fe3688baSAkhil Goyal 	ev->flow_id = outq->ev.flow_id;
3493fe3688baSAkhil Goyal 	ev->sub_event_type = outq->ev.sub_event_type;
3494fe3688baSAkhil Goyal 	ev->event_type = RTE_EVENT_TYPE_CRYPTODEV;
3495fe3688baSAkhil Goyal 	ev->op = RTE_EVENT_OP_NEW;
3496fe3688baSAkhil Goyal 	ev->sched_type = outq->ev.sched_type;
3497fe3688baSAkhil Goyal 	ev->queue_id = outq->ev.queue_id;
3498fe3688baSAkhil Goyal 	ev->priority = outq->ev.priority;
3499fe3688baSAkhil Goyal 
3500fe3688baSAkhil Goyal 	/* Save active dqrr entries */
3501fe3688baSAkhil Goyal 	index = ((uintptr_t)dqrr >> 6) & (16/*QM_DQRR_SIZE*/ - 1);
3502fe3688baSAkhil Goyal 	DPAA_PER_LCORE_DQRR_SIZE++;
3503fe3688baSAkhil Goyal 	DPAA_PER_LCORE_DQRR_HELD |= 1 << index;
3504fe3688baSAkhil Goyal 	DPAA_PER_LCORE_DQRR_MBUF(index) = ctx->op->sym->m_src;
3505fe3688baSAkhil Goyal 	ev->impl_opaque = index + 1;
3506c9a1c2e5SDavid Marchand 	*dpaa_seqn(ctx->op->sym->m_src) = (uint32_t)index + 1;
3507fe3688baSAkhil Goyal 	*bufs = (void *)ctx->op;
3508fe3688baSAkhil Goyal 
3509fe3688baSAkhil Goyal 	rte_mempool_put(ctx->ctx_pool, (void *)ctx);
3510fe3688baSAkhil Goyal 
3511fe3688baSAkhil Goyal 	return qman_cb_dqrr_defer;
3512fe3688baSAkhil Goyal }
3513fe3688baSAkhil Goyal 
3514fe3688baSAkhil Goyal int
3515fe3688baSAkhil Goyal dpaa_sec_eventq_attach(const struct rte_cryptodev *dev,
3516fe3688baSAkhil Goyal 		int qp_id,
3517fe3688baSAkhil Goyal 		uint16_t ch_id,
3518fe3688baSAkhil Goyal 		const struct rte_event *event)
3519fe3688baSAkhil Goyal {
3520fe3688baSAkhil Goyal 	struct dpaa_sec_qp *qp = dev->data->queue_pairs[qp_id];
3521fe3688baSAkhil Goyal 	struct qm_mcc_initfq opts = {0};
3522fe3688baSAkhil Goyal 
3523fe3688baSAkhil Goyal 	int ret;
3524fe3688baSAkhil Goyal 
3525fe3688baSAkhil Goyal 	opts.we_mask = QM_INITFQ_WE_DESTWQ | QM_INITFQ_WE_FQCTRL |
3526fe3688baSAkhil Goyal 		       QM_INITFQ_WE_CONTEXTA | QM_INITFQ_WE_CONTEXTB;
3527fe3688baSAkhil Goyal 	opts.fqd.dest.channel = ch_id;
3528fe3688baSAkhil Goyal 
3529fe3688baSAkhil Goyal 	switch (event->sched_type) {
3530fe3688baSAkhil Goyal 	case RTE_SCHED_TYPE_ATOMIC:
3531fe3688baSAkhil Goyal 		opts.fqd.fq_ctrl |= QM_FQCTRL_HOLDACTIVE;
3532fe3688baSAkhil Goyal 		/* Reset FQCTRL_AVOIDBLOCK bit as it is unnecessary
3533fe3688baSAkhil Goyal 		 * configuration with HOLD_ACTIVE setting
3534fe3688baSAkhil Goyal 		 */
3535fe3688baSAkhil Goyal 		opts.fqd.fq_ctrl &= (~QM_FQCTRL_AVOIDBLOCK);
3536fe3688baSAkhil Goyal 		qp->outq.cb.dqrr_dpdk_cb = dpaa_sec_process_atomic_event;
3537fe3688baSAkhil Goyal 		break;
3538fe3688baSAkhil Goyal 	case RTE_SCHED_TYPE_ORDERED:
3539f665790aSDavid Marchand 		DPAA_SEC_ERR("Ordered queue schedule type is not supported");
3540c08ced9aSAkhil Goyal 		return -ENOTSUP;
3541fe3688baSAkhil Goyal 	default:
3542fe3688baSAkhil Goyal 		opts.fqd.fq_ctrl |= QM_FQCTRL_AVOIDBLOCK;
3543fe3688baSAkhil Goyal 		qp->outq.cb.dqrr_dpdk_cb = dpaa_sec_process_parallel_event;
3544fe3688baSAkhil Goyal 		break;
3545fe3688baSAkhil Goyal 	}
3546fe3688baSAkhil Goyal 
3547fe3688baSAkhil Goyal 	ret = qman_init_fq(&qp->outq, QMAN_INITFQ_FLAG_SCHED, &opts);
3548fe3688baSAkhil Goyal 	if (unlikely(ret)) {
3549fe3688baSAkhil Goyal 		DPAA_SEC_ERR("unable to init caam source fq!");
3550fe3688baSAkhil Goyal 		return ret;
3551fe3688baSAkhil Goyal 	}
3552fe3688baSAkhil Goyal 
3553fe3688baSAkhil Goyal 	memcpy(&qp->outq.ev, event, sizeof(struct rte_event));
3554fe3688baSAkhil Goyal 
3555fe3688baSAkhil Goyal 	return 0;
3556fe3688baSAkhil Goyal }
3557fe3688baSAkhil Goyal 
3558fe3688baSAkhil Goyal int
3559fe3688baSAkhil Goyal dpaa_sec_eventq_detach(const struct rte_cryptodev *dev,
3560fe3688baSAkhil Goyal 			int qp_id)
3561fe3688baSAkhil Goyal {
3562fe3688baSAkhil Goyal 	struct qm_mcc_initfq opts = {0};
3563fe3688baSAkhil Goyal 	int ret;
3564fe3688baSAkhil Goyal 	struct dpaa_sec_qp *qp = dev->data->queue_pairs[qp_id];
3565fe3688baSAkhil Goyal 
3566fe3688baSAkhil Goyal 	opts.we_mask = QM_INITFQ_WE_DESTWQ | QM_INITFQ_WE_FQCTRL |
3567fe3688baSAkhil Goyal 		       QM_INITFQ_WE_CONTEXTA | QM_INITFQ_WE_CONTEXTB;
3568fe3688baSAkhil Goyal 	qp->outq.cb.dqrr = dqrr_out_fq_cb_rx;
3569fe3688baSAkhil Goyal 	qp->outq.cb.ern  = ern_sec_fq_handler;
3570fe3688baSAkhil Goyal 	qman_retire_fq(&qp->outq, NULL);
3571fe3688baSAkhil Goyal 	qman_oos_fq(&qp->outq);
3572fe3688baSAkhil Goyal 	ret = qman_init_fq(&qp->outq, 0, &opts);
3573fe3688baSAkhil Goyal 	if (ret)
3574a247fcd9SStephen Hemminger 		DPAA_SEC_ERR("Error in qman_init_fq: ret: %d", ret);
3575fe3688baSAkhil Goyal 	qp->outq.cb.dqrr = NULL;
3576fe3688baSAkhil Goyal 
3577fe3688baSAkhil Goyal 	return ret;
3578fe3688baSAkhil Goyal }
3579fe3688baSAkhil Goyal 
3580c3e85bdcSAkhil Goyal static struct rte_cryptodev_ops crypto_ops = {
3581c3e85bdcSAkhil Goyal 	.dev_configure	      = dpaa_sec_dev_configure,
3582c3e85bdcSAkhil Goyal 	.dev_start	      = dpaa_sec_dev_start,
3583c3e85bdcSAkhil Goyal 	.dev_stop	      = dpaa_sec_dev_stop,
3584c3e85bdcSAkhil Goyal 	.dev_close	      = dpaa_sec_dev_close,
3585c3e85bdcSAkhil Goyal 	.dev_infos_get        = dpaa_sec_dev_infos_get,
3586c3e85bdcSAkhil Goyal 	.queue_pair_setup     = dpaa_sec_queue_pair_setup,
3587c3e85bdcSAkhil Goyal 	.queue_pair_release   = dpaa_sec_queue_pair_release,
3588012c5076SPablo de Lara 	.sym_session_get_size     = dpaa_sec_sym_session_get_size,
3589012c5076SPablo de Lara 	.sym_session_configure    = dpaa_sec_sym_session_configure,
35909d5f73c2SGagandeep Singh 	.sym_session_clear        = dpaa_sec_sym_session_clear,
35919d5f73c2SGagandeep Singh 	/* Raw data-path API related operations */
35929d5f73c2SGagandeep Singh 	.sym_get_raw_dp_ctx_size = dpaa_sec_get_dp_ctx_size,
35939d5f73c2SGagandeep Singh 	.sym_configure_raw_dp_ctx = dpaa_sec_configure_raw_dp_ctx,
3594c3e85bdcSAkhil Goyal };
3595c3e85bdcSAkhil Goyal 
35961f14d500SAkhil Goyal static const struct rte_security_capability *
35971f14d500SAkhil Goyal dpaa_sec_capabilities_get(void *device __rte_unused)
35981f14d500SAkhil Goyal {
35991f14d500SAkhil Goyal 	return dpaa_sec_security_cap;
36001f14d500SAkhil Goyal }
36011f14d500SAkhil Goyal 
3602b74fd6b8SFerruh Yigit static const struct rte_security_ops dpaa_sec_security_ops = {
36031f14d500SAkhil Goyal 	.session_create = dpaa_sec_security_session_create,
36041f14d500SAkhil Goyal 	.session_update = NULL,
360566837861SAkhil Goyal 	.session_get_size = dpaa_sec_security_session_get_size,
36061f14d500SAkhil Goyal 	.session_stats_get = NULL,
36071f14d500SAkhil Goyal 	.session_destroy = dpaa_sec_security_session_destroy,
36081f14d500SAkhil Goyal 	.set_pkt_metadata = NULL,
36091f14d500SAkhil Goyal 	.capabilities_get = dpaa_sec_capabilities_get
36101f14d500SAkhil Goyal };
361176c129ceSMaxime Coquelin 
3612c3e85bdcSAkhil Goyal static int
3613c3e85bdcSAkhil Goyal dpaa_sec_uninit(struct rte_cryptodev *dev)
3614c3e85bdcSAkhil Goyal {
3615debef417SShreyansh Jain 	struct dpaa_sec_dev_private *internals;
3616c3e85bdcSAkhil Goyal 
3617c3e85bdcSAkhil Goyal 	if (dev == NULL)
3618c3e85bdcSAkhil Goyal 		return -ENODEV;
3619c3e85bdcSAkhil Goyal 
3620debef417SShreyansh Jain 	internals = dev->data->dev_private;
36211f14d500SAkhil Goyal 	rte_free(dev->security_ctx);
36221f14d500SAkhil Goyal 
3623c3e85bdcSAkhil Goyal 	rte_free(internals);
3624c3e85bdcSAkhil Goyal 
3625f163231eSHemant Agrawal 	DPAA_SEC_INFO("Closing DPAA_SEC device %s on numa socket %u",
3626c3e85bdcSAkhil Goyal 		      dev->data->name, rte_socket_id());
3627c3e85bdcSAkhil Goyal 
3628c3e85bdcSAkhil Goyal 	return 0;
3629c3e85bdcSAkhil Goyal }
3630c3e85bdcSAkhil Goyal 
3631c3e85bdcSAkhil Goyal static int
3632b1bbf222SGagandeep Singh check_devargs_handler(__rte_unused const char *key, const char *value,
3633b1bbf222SGagandeep Singh 		      __rte_unused void *opaque)
3634b1bbf222SGagandeep Singh {
3635b1bbf222SGagandeep Singh 	dpaa_sec_dp_dump = atoi(value);
3636b1bbf222SGagandeep Singh 	if (dpaa_sec_dp_dump > DPAA_SEC_DP_FULL_DUMP) {
3637b1bbf222SGagandeep Singh 		DPAA_SEC_WARN("WARN: DPAA_SEC_DP_DUMP_LEVEL is not "
3638f665790aSDavid Marchand 			      "supported, changing to FULL error prints");
3639b1bbf222SGagandeep Singh 		dpaa_sec_dp_dump = DPAA_SEC_DP_FULL_DUMP;
3640b1bbf222SGagandeep Singh 	}
3641b1bbf222SGagandeep Singh 
3642b1bbf222SGagandeep Singh 	return 0;
3643b1bbf222SGagandeep Singh }
3644b1bbf222SGagandeep Singh 
3645b1bbf222SGagandeep Singh static void
3646b1bbf222SGagandeep Singh dpaa_sec_get_devargs(struct rte_devargs *devargs, const char *key)
3647b1bbf222SGagandeep Singh {
3648b1bbf222SGagandeep Singh 	struct rte_kvargs *kvlist;
3649b1bbf222SGagandeep Singh 
3650b1bbf222SGagandeep Singh 	if (!devargs)
3651b1bbf222SGagandeep Singh 		return;
3652b1bbf222SGagandeep Singh 
3653b1bbf222SGagandeep Singh 	kvlist = rte_kvargs_parse(devargs->args, NULL);
3654b1bbf222SGagandeep Singh 	if (!kvlist)
3655b1bbf222SGagandeep Singh 		return;
3656b1bbf222SGagandeep Singh 
3657b1bbf222SGagandeep Singh 	if (!rte_kvargs_count(kvlist, key)) {
3658b1bbf222SGagandeep Singh 		rte_kvargs_free(kvlist);
3659b1bbf222SGagandeep Singh 		return;
3660b1bbf222SGagandeep Singh 	}
3661b1bbf222SGagandeep Singh 
3662b1bbf222SGagandeep Singh 	rte_kvargs_process(kvlist, key,
3663b1bbf222SGagandeep Singh 				check_devargs_handler, NULL);
3664b1bbf222SGagandeep Singh 	rte_kvargs_free(kvlist);
3665b1bbf222SGagandeep Singh }
3666b1bbf222SGagandeep Singh 
3667b1bbf222SGagandeep Singh static int
3668c3e85bdcSAkhil Goyal dpaa_sec_dev_init(struct rte_cryptodev *cryptodev)
3669c3e85bdcSAkhil Goyal {
3670c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals;
36711f14d500SAkhil Goyal 	struct rte_security_ctx *security_instance;
3672c3e85bdcSAkhil Goyal 	struct dpaa_sec_qp *qp;
3673e79416d1SHemant Agrawal 	uint32_t i, flags;
3674c3e85bdcSAkhil Goyal 	int ret;
36758a3167dbSGagandeep Singh 	void *cmd_map;
36768a3167dbSGagandeep Singh 	int map_fd = -1;
3677c3e85bdcSAkhil Goyal 
3678c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
3679c3e85bdcSAkhil Goyal 
36808a3167dbSGagandeep Singh 	internals = cryptodev->data->dev_private;
36818a3167dbSGagandeep Singh 	map_fd = open("/dev/mem", O_RDWR);
36828a3167dbSGagandeep Singh 	if (unlikely(map_fd < 0)) {
36838a3167dbSGagandeep Singh 		DPAA_SEC_ERR("Unable to open (/dev/mem)");
36848a3167dbSGagandeep Singh 		return map_fd;
36858a3167dbSGagandeep Singh 	}
36868a3167dbSGagandeep Singh 	internals->sec_hw = mmap(NULL, MAP_SIZE, PROT_READ | PROT_WRITE,
36878a3167dbSGagandeep Singh 			    MAP_SHARED, map_fd, SEC_BASE_ADDR);
36888a3167dbSGagandeep Singh 	if (internals->sec_hw == MAP_FAILED) {
36898a3167dbSGagandeep Singh 		DPAA_SEC_ERR("Memory map failed");
36908a3167dbSGagandeep Singh 		close(map_fd);
36918a3167dbSGagandeep Singh 		return -EINVAL;
36928a3167dbSGagandeep Singh 	}
36938a3167dbSGagandeep Singh 	cmd_map = (uint8_t *)internals->sec_hw +
36948a3167dbSGagandeep Singh 		  (BLOCK_OFFSET * QI_BLOCK_NUMBER) + CMD_REG;
36958a3167dbSGagandeep Singh 	if (!(be32_to_cpu(rte_read32(cmd_map)) & QICTL_DQEN))
36968a3167dbSGagandeep Singh 		/* enable QI interface */
36978a3167dbSGagandeep Singh 		rte_write32(cpu_to_be32(QICTL_DQEN), cmd_map);
36988a3167dbSGagandeep Singh 
36998a3167dbSGagandeep Singh 	ret = munmap(internals->sec_hw, MAP_SIZE);
37008a3167dbSGagandeep Singh 	if (ret)
3701f665790aSDavid Marchand 		DPAA_SEC_WARN("munmap failed");
37028a3167dbSGagandeep Singh 
37038a3167dbSGagandeep Singh 	close(map_fd);
37049d5f73c2SGagandeep Singh 	cryptodev->driver_id = dpaa_cryptodev_driver_id;
3705c3e85bdcSAkhil Goyal 	cryptodev->dev_ops = &crypto_ops;
3706c3e85bdcSAkhil Goyal 
3707c3e85bdcSAkhil Goyal 	cryptodev->enqueue_burst = dpaa_sec_enqueue_burst;
3708c3e85bdcSAkhil Goyal 	cryptodev->dequeue_burst = dpaa_sec_dequeue_burst;
3709c3e85bdcSAkhil Goyal 	cryptodev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
3710c3e85bdcSAkhil Goyal 			RTE_CRYPTODEV_FF_HW_ACCELERATED |
37111f14d500SAkhil Goyal 			RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
3712a74af788SAkhil Goyal 			RTE_CRYPTODEV_FF_SECURITY |
37139d5f73c2SGagandeep Singh 			RTE_CRYPTODEV_FF_SYM_RAW_DP |
37142717246eSPablo de Lara 			RTE_CRYPTODEV_FF_IN_PLACE_SGL |
37152717246eSPablo de Lara 			RTE_CRYPTODEV_FF_OOP_SGL_IN_SGL_OUT |
37162717246eSPablo de Lara 			RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT |
37172717246eSPablo de Lara 			RTE_CRYPTODEV_FF_OOP_LB_IN_SGL_OUT |
37182717246eSPablo de Lara 			RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT;
3719c3e85bdcSAkhil Goyal 
3720e79416d1SHemant Agrawal 	internals->max_nb_queue_pairs = RTE_DPAA_MAX_NB_SEC_QPS;
3721c3e85bdcSAkhil Goyal 	internals->max_nb_sessions = RTE_DPAA_SEC_PMD_MAX_NB_SESSIONS;
3722c3e85bdcSAkhil Goyal 
37231f14d500SAkhil Goyal 	/*
37241f14d500SAkhil Goyal 	 * For secondary processes, we don't initialise any further as primary
37251f14d500SAkhil Goyal 	 * has already done this work. Only check we don't need a different
37261f14d500SAkhil Goyal 	 * RX function
37271f14d500SAkhil Goyal 	 */
37281f14d500SAkhil Goyal 	if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
3729f163231eSHemant Agrawal 		DPAA_SEC_WARN("Device already init by primary process");
37301f14d500SAkhil Goyal 		return 0;
37311f14d500SAkhil Goyal 	}
37321f14d500SAkhil Goyal 	/* Initialize security_ctx only for primary process*/
37331f14d500SAkhil Goyal 	security_instance = rte_malloc("rte_security_instances_ops",
37341f14d500SAkhil Goyal 				sizeof(struct rte_security_ctx), 0);
37351f14d500SAkhil Goyal 	if (security_instance == NULL)
37361f14d500SAkhil Goyal 		return -ENOMEM;
37371f14d500SAkhil Goyal 	security_instance->device = (void *)cryptodev;
37381f14d500SAkhil Goyal 	security_instance->ops = &dpaa_sec_security_ops;
37391f14d500SAkhil Goyal 	security_instance->sess_cnt = 0;
37401f14d500SAkhil Goyal 	cryptodev->security_ctx = security_instance;
37413b617ee7SAkhil Goyal 	rte_spinlock_init(&internals->lock);
3742c3e85bdcSAkhil Goyal 	for (i = 0; i < internals->max_nb_queue_pairs; i++) {
3743c3e85bdcSAkhil Goyal 		/* init qman fq for queue pair */
3744c3e85bdcSAkhil Goyal 		qp = &internals->qps[i];
3745c3e85bdcSAkhil Goyal 		ret = dpaa_sec_init_tx(&qp->outq);
3746c3e85bdcSAkhil Goyal 		if (ret) {
3747f163231eSHemant Agrawal 			DPAA_SEC_ERR("config tx of queue pair  %d", i);
3748c3e85bdcSAkhil Goyal 			goto init_error;
3749c3e85bdcSAkhil Goyal 		}
3750e79416d1SHemant Agrawal 	}
3751e79416d1SHemant Agrawal 
3752e79416d1SHemant Agrawal 	flags = QMAN_FQ_FLAG_LOCKED | QMAN_FQ_FLAG_DYNAMIC_FQID |
3753e79416d1SHemant Agrawal 		QMAN_FQ_FLAG_TO_DCPORTAL;
3754fd900d38SGagandeep Singh 	for (i = 0; i < RTE_DPAA_MAX_RX_QUEUE; i++) {
3755e79416d1SHemant Agrawal 		/* create rx qman fq for sessions*/
3756e79416d1SHemant Agrawal 		ret = qman_create_fq(0, flags, &internals->inq[i]);
3757e79416d1SHemant Agrawal 		if (unlikely(ret != 0)) {
3758f163231eSHemant Agrawal 			DPAA_SEC_ERR("sec qman_create_fq failed");
3759c3e85bdcSAkhil Goyal 			goto init_error;
3760c3e85bdcSAkhil Goyal 		}
3761c3e85bdcSAkhil Goyal 	}
3762c3e85bdcSAkhil Goyal 
3763b1bbf222SGagandeep Singh 	dpaa_sec_get_devargs(cryptodev->device->devargs, DRIVER_DUMP_MODE);
3764b1bbf222SGagandeep Singh 
3765a247fcd9SStephen Hemminger 	DPAA_SEC_INFO("%s cryptodev init", cryptodev->data->name);
3766c3e85bdcSAkhil Goyal 	return 0;
3767c3e85bdcSAkhil Goyal 
3768c3e85bdcSAkhil Goyal init_error:
3769f665790aSDavid Marchand 	DPAA_SEC_ERR("driver %s: create failed", cryptodev->data->name);
3770c3e85bdcSAkhil Goyal 
37712eaf352dSLukasz Wojciechowski 	rte_free(cryptodev->security_ctx);
3772c3e85bdcSAkhil Goyal 	return -EFAULT;
3773c3e85bdcSAkhil Goyal }
3774c3e85bdcSAkhil Goyal 
3775c3e85bdcSAkhil Goyal static int
3776eb5a9a76SShreyansh Jain cryptodev_dpaa_sec_probe(struct rte_dpaa_driver *dpaa_drv __rte_unused,
3777c3e85bdcSAkhil Goyal 				struct rte_dpaa_device *dpaa_dev)
3778c3e85bdcSAkhil Goyal {
3779c3e85bdcSAkhil Goyal 	struct rte_cryptodev *cryptodev;
3780c3e85bdcSAkhil Goyal 	char cryptodev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
3781c3e85bdcSAkhil Goyal 
3782c3e85bdcSAkhil Goyal 	int retval;
3783c3e85bdcSAkhil Goyal 
378496ec64f1SVanshika Shukla 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
378596ec64f1SVanshika Shukla 		return 0;
378696ec64f1SVanshika Shukla 
37870964a951SHemant Agrawal 	snprintf(cryptodev_name, sizeof(cryptodev_name), "%s", dpaa_dev->name);
3788c3e85bdcSAkhil Goyal 
3789c3e85bdcSAkhil Goyal 	cryptodev = rte_cryptodev_pmd_allocate(cryptodev_name, rte_socket_id());
3790c3e85bdcSAkhil Goyal 	if (cryptodev == NULL)
3791c3e85bdcSAkhil Goyal 		return -ENOMEM;
3792c3e85bdcSAkhil Goyal 
3793c3e85bdcSAkhil Goyal 	cryptodev->data->dev_private = rte_zmalloc_socket(
3794c3e85bdcSAkhil Goyal 				"cryptodev private structure",
3795c3e85bdcSAkhil Goyal 				sizeof(struct dpaa_sec_dev_private),
3796c3e85bdcSAkhil Goyal 				RTE_CACHE_LINE_SIZE,
3797c3e85bdcSAkhil Goyal 				rte_socket_id());
3798c3e85bdcSAkhil Goyal 
3799c3e85bdcSAkhil Goyal 	if (cryptodev->data->dev_private == NULL)
3800c3e85bdcSAkhil Goyal 		rte_panic("Cannot allocate memzone for private "
3801c3e85bdcSAkhil Goyal 				"device data");
3802c3e85bdcSAkhil Goyal 
3803c3e85bdcSAkhil Goyal 	dpaa_dev->crypto_dev = cryptodev;
3804c3e85bdcSAkhil Goyal 	cryptodev->device = &dpaa_dev->device;
3805c3e85bdcSAkhil Goyal 
3806c3e85bdcSAkhil Goyal 	/* init user callbacks */
3807c3e85bdcSAkhil Goyal 	TAILQ_INIT(&(cryptodev->link_intr_cbs));
3808c3e85bdcSAkhil Goyal 
3809c3e85bdcSAkhil Goyal 	/* if sec device version is not configured */
3810c3e85bdcSAkhil Goyal 	if (!rta_get_sec_era()) {
3811c3e85bdcSAkhil Goyal 		const struct device_node *caam_node;
3812c3e85bdcSAkhil Goyal 
3813c3e85bdcSAkhil Goyal 		for_each_compatible_node(caam_node, NULL, "fsl,sec-v4.0") {
3814c3e85bdcSAkhil Goyal 			const uint32_t *prop = of_get_property(caam_node,
3815c3e85bdcSAkhil Goyal 					"fsl,sec-era",
3816c3e85bdcSAkhil Goyal 					NULL);
3817c3e85bdcSAkhil Goyal 			if (prop) {
3818c3e85bdcSAkhil Goyal 				rta_set_sec_era(
3819c3e85bdcSAkhil Goyal 					INTL_SEC_ERA(rte_cpu_to_be_32(*prop)));
3820c3e85bdcSAkhil Goyal 				break;
3821c3e85bdcSAkhil Goyal 			}
3822c3e85bdcSAkhil Goyal 		}
3823c3e85bdcSAkhil Goyal 	}
3824c3e85bdcSAkhil Goyal 
3825e5872221SRohit Raj 	if (unlikely(!DPAA_PER_LCORE_PORTAL)) {
3826408077f2SHemant Agrawal 		retval = rte_dpaa_portal_init((void *)1);
3827408077f2SHemant Agrawal 		if (retval) {
3828408077f2SHemant Agrawal 			DPAA_SEC_ERR("Unable to initialize portal");
38292eaf352dSLukasz Wojciechowski 			goto out;
3830408077f2SHemant Agrawal 		}
3831408077f2SHemant Agrawal 	}
3832408077f2SHemant Agrawal 
3833c3e85bdcSAkhil Goyal 	/* Invoke PMD device initialization function */
3834c3e85bdcSAkhil Goyal 	retval = dpaa_sec_dev_init(cryptodev);
3835d54c72ecSAkhil Goyal 	if (retval == 0) {
3836d54c72ecSAkhil Goyal 		rte_cryptodev_pmd_probing_finish(cryptodev);
3837c3e85bdcSAkhil Goyal 		return 0;
3838d54c72ecSAkhil Goyal 	}
3839c3e85bdcSAkhil Goyal 
38402eaf352dSLukasz Wojciechowski 	retval = -ENXIO;
38412eaf352dSLukasz Wojciechowski out:
3842c3e85bdcSAkhil Goyal 	/* In case of error, cleanup is done */
3843c3e85bdcSAkhil Goyal 	rte_free(cryptodev->data->dev_private);
3844c3e85bdcSAkhil Goyal 
3845c3e85bdcSAkhil Goyal 	rte_cryptodev_pmd_release_device(cryptodev);
3846c3e85bdcSAkhil Goyal 
38472eaf352dSLukasz Wojciechowski 	return retval;
3848c3e85bdcSAkhil Goyal }
3849c3e85bdcSAkhil Goyal 
3850c3e85bdcSAkhil Goyal static int
3851c3e85bdcSAkhil Goyal cryptodev_dpaa_sec_remove(struct rte_dpaa_device *dpaa_dev)
3852c3e85bdcSAkhil Goyal {
3853c3e85bdcSAkhil Goyal 	struct rte_cryptodev *cryptodev;
3854c3e85bdcSAkhil Goyal 	int ret;
3855c3e85bdcSAkhil Goyal 
3856c3e85bdcSAkhil Goyal 	cryptodev = dpaa_dev->crypto_dev;
3857c3e85bdcSAkhil Goyal 	if (cryptodev == NULL)
3858c3e85bdcSAkhil Goyal 		return -ENODEV;
3859c3e85bdcSAkhil Goyal 
3860c3e85bdcSAkhil Goyal 	ret = dpaa_sec_uninit(cryptodev);
3861c3e85bdcSAkhil Goyal 	if (ret)
3862c3e85bdcSAkhil Goyal 		return ret;
3863c3e85bdcSAkhil Goyal 
3864f2f020d2SDeclan Doherty 	return rte_cryptodev_pmd_destroy(cryptodev);
3865c3e85bdcSAkhil Goyal }
3866c3e85bdcSAkhil Goyal 
3867c3e85bdcSAkhil Goyal static struct rte_dpaa_driver rte_dpaa_sec_driver = {
3868c3e85bdcSAkhil Goyal 	.drv_type = FSL_DPAA_CRYPTO,
3869c3e85bdcSAkhil Goyal 	.driver = {
3870c3e85bdcSAkhil Goyal 		.name = "DPAA SEC PMD"
3871c3e85bdcSAkhil Goyal 	},
3872c3e85bdcSAkhil Goyal 	.probe = cryptodev_dpaa_sec_probe,
3873c3e85bdcSAkhil Goyal 	.remove = cryptodev_dpaa_sec_remove,
3874c3e85bdcSAkhil Goyal };
3875c3e85bdcSAkhil Goyal 
3876c3e85bdcSAkhil Goyal static struct cryptodev_driver dpaa_sec_crypto_drv;
3877c3e85bdcSAkhil Goyal 
3878c3e85bdcSAkhil Goyal RTE_PMD_REGISTER_DPAA(CRYPTODEV_NAME_DPAA_SEC_PMD, rte_dpaa_sec_driver);
3879f737f5ceSFiona Trahe RTE_PMD_REGISTER_CRYPTO_DRIVER(dpaa_sec_crypto_drv, rte_dpaa_sec_driver.driver,
38809d5f73c2SGagandeep Singh 		dpaa_cryptodev_driver_id);
3881b1bbf222SGagandeep Singh RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_DPAA_SEC_PMD,
3882b1bbf222SGagandeep Singh 		DRIVER_DUMP_MODE "=<int>");
38839c99878aSJerin Jacob RTE_LOG_REGISTER(dpaa_logtype_sec, pmd.crypto.dpaa, NOTICE);
3884