xref: /dpdk/drivers/crypto/dpaa_sec/dpaa_sec.c (revision a74af788c6324d4b2284d55b282b4aa3522e3135)
1d81734caSHemant Agrawal /* SPDX-License-Identifier: BSD-3-Clause
2c3e85bdcSAkhil Goyal  *
3c3e85bdcSAkhil Goyal  *   Copyright (c) 2016 Freescale Semiconductor, Inc. All rights reserved.
4d81734caSHemant Agrawal  *   Copyright 2017 NXP
5c3e85bdcSAkhil Goyal  *
6c3e85bdcSAkhil Goyal  */
7c3e85bdcSAkhil Goyal 
8c3e85bdcSAkhil Goyal #include <fcntl.h>
9c3e85bdcSAkhil Goyal #include <unistd.h>
10c3e85bdcSAkhil Goyal #include <sched.h>
11c3e85bdcSAkhil Goyal #include <net/if.h>
12c3e85bdcSAkhil Goyal 
13c3e85bdcSAkhil Goyal #include <rte_byteorder.h>
14c3e85bdcSAkhil Goyal #include <rte_common.h>
15c3e85bdcSAkhil Goyal #include <rte_cryptodev_pmd.h>
16c3e85bdcSAkhil Goyal #include <rte_crypto.h>
17c3e85bdcSAkhil Goyal #include <rte_cryptodev.h>
181f14d500SAkhil Goyal #include <rte_security_driver.h>
19c3e85bdcSAkhil Goyal #include <rte_cycles.h>
20c3e85bdcSAkhil Goyal #include <rte_dev.h>
21c3e85bdcSAkhil Goyal #include <rte_kvargs.h>
22c3e85bdcSAkhil Goyal #include <rte_malloc.h>
23c3e85bdcSAkhil Goyal #include <rte_mbuf.h>
24c3e85bdcSAkhil Goyal #include <rte_memcpy.h>
25c3e85bdcSAkhil Goyal #include <rte_string_fns.h>
26c3e85bdcSAkhil Goyal 
27c3e85bdcSAkhil Goyal #include <fsl_usd.h>
28c3e85bdcSAkhil Goyal #include <fsl_qman.h>
29c3e85bdcSAkhil Goyal #include <of.h>
30c3e85bdcSAkhil Goyal 
31c3e85bdcSAkhil Goyal /* RTA header files */
32c3e85bdcSAkhil Goyal #include <hw/desc/common.h>
33c3e85bdcSAkhil Goyal #include <hw/desc/algo.h>
34c3e85bdcSAkhil Goyal #include <hw/desc/ipsec.h>
35c3e85bdcSAkhil Goyal 
36c3e85bdcSAkhil Goyal #include <rte_dpaa_bus.h>
37c3e85bdcSAkhil Goyal #include <dpaa_sec.h>
38c3e85bdcSAkhil Goyal #include <dpaa_sec_log.h>
39c3e85bdcSAkhil Goyal 
40c3e85bdcSAkhil Goyal enum rta_sec_era rta_sec_era;
41c3e85bdcSAkhil Goyal 
42c3e85bdcSAkhil Goyal static uint8_t cryptodev_driver_id;
43c3e85bdcSAkhil Goyal 
44c3e85bdcSAkhil Goyal static __thread struct rte_crypto_op **dpaa_sec_ops;
45c3e85bdcSAkhil Goyal static __thread int dpaa_sec_op_nb;
46c3e85bdcSAkhil Goyal 
47e79416d1SHemant Agrawal static int
48e79416d1SHemant Agrawal dpaa_sec_attach_sess_q(struct dpaa_sec_qp *qp, dpaa_sec_session *sess);
49e79416d1SHemant Agrawal 
50c3e85bdcSAkhil Goyal static inline void
51c3e85bdcSAkhil Goyal dpaa_sec_op_ending(struct dpaa_sec_op_ctx *ctx)
52c3e85bdcSAkhil Goyal {
53c3e85bdcSAkhil Goyal 	if (!ctx->fd_status) {
54c3e85bdcSAkhil Goyal 		ctx->op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
55c3e85bdcSAkhil Goyal 	} else {
56c3e85bdcSAkhil Goyal 		PMD_RX_LOG(ERR, "SEC return err: 0x%x", ctx->fd_status);
57c3e85bdcSAkhil Goyal 		ctx->op->status = RTE_CRYPTO_OP_STATUS_ERROR;
58c3e85bdcSAkhil Goyal 	}
59c3e85bdcSAkhil Goyal 
60c3e85bdcSAkhil Goyal 	/* report op status to sym->op and then free the ctx memeory  */
61c3e85bdcSAkhil Goyal 	rte_mempool_put(ctx->ctx_pool, (void *)ctx);
62c3e85bdcSAkhil Goyal }
63c3e85bdcSAkhil Goyal 
64c3e85bdcSAkhil Goyal static inline struct dpaa_sec_op_ctx *
65c3e85bdcSAkhil Goyal dpaa_sec_alloc_ctx(dpaa_sec_session *ses)
66c3e85bdcSAkhil Goyal {
67c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
68c3e85bdcSAkhil Goyal 	int retval;
69c3e85bdcSAkhil Goyal 
70c3e85bdcSAkhil Goyal 	retval = rte_mempool_get(ses->ctx_pool, (void **)(&ctx));
71c3e85bdcSAkhil Goyal 	if (!ctx || retval) {
72c3e85bdcSAkhil Goyal 		PMD_TX_LOG(ERR, "Alloc sec descriptor failed!");
73c3e85bdcSAkhil Goyal 		return NULL;
74c3e85bdcSAkhil Goyal 	}
75c3e85bdcSAkhil Goyal 	/*
76c3e85bdcSAkhil Goyal 	 * Clear SG memory. There are 16 SG entries of 16 Bytes each.
77c3e85bdcSAkhil Goyal 	 * one call to dcbz_64() clear 64 bytes, hence calling it 4 times
78c3e85bdcSAkhil Goyal 	 * to clear all the SG entries. dpaa_sec_alloc_ctx() is called for
79c3e85bdcSAkhil Goyal 	 * each packet, memset is costlier than dcbz_64().
80c3e85bdcSAkhil Goyal 	 */
81c3e85bdcSAkhil Goyal 	dcbz_64(&ctx->job.sg[SG_CACHELINE_0]);
82c3e85bdcSAkhil Goyal 	dcbz_64(&ctx->job.sg[SG_CACHELINE_1]);
83c3e85bdcSAkhil Goyal 	dcbz_64(&ctx->job.sg[SG_CACHELINE_2]);
84c3e85bdcSAkhil Goyal 	dcbz_64(&ctx->job.sg[SG_CACHELINE_3]);
85c3e85bdcSAkhil Goyal 
86c3e85bdcSAkhil Goyal 	ctx->ctx_pool = ses->ctx_pool;
87fcf67029SHemant Agrawal 	ctx->vtop_offset = (uint64_t) ctx
88fcf67029SHemant Agrawal 				- rte_mempool_virt2iova(ctx);
89c3e85bdcSAkhil Goyal 
90c3e85bdcSAkhil Goyal 	return ctx;
91c3e85bdcSAkhil Goyal }
92c3e85bdcSAkhil Goyal 
93c4509373SSantosh Shukla static inline rte_iova_t
94c3e85bdcSAkhil Goyal dpaa_mem_vtop(void *vaddr)
95c3e85bdcSAkhil Goyal {
96c3e85bdcSAkhil Goyal 	const struct rte_memseg *memseg = rte_eal_get_physmem_layout();
97c3e85bdcSAkhil Goyal 	uint64_t vaddr_64, paddr;
98c3e85bdcSAkhil Goyal 	int i;
99c3e85bdcSAkhil Goyal 
100c3e85bdcSAkhil Goyal 	vaddr_64 = (uint64_t)vaddr;
101c3e85bdcSAkhil Goyal 	for (i = 0; i < RTE_MAX_MEMSEG && memseg[i].addr_64 != 0; i++) {
102c3e85bdcSAkhil Goyal 		if (vaddr_64 >= memseg[i].addr_64 &&
103c3e85bdcSAkhil Goyal 		    vaddr_64 < memseg[i].addr_64 + memseg[i].len) {
1042aab8337SHemant Agrawal 			paddr = memseg[i].iova +
105c3e85bdcSAkhil Goyal 				(vaddr_64 - memseg[i].addr_64);
106c3e85bdcSAkhil Goyal 
107c4509373SSantosh Shukla 			return (rte_iova_t)paddr;
108c3e85bdcSAkhil Goyal 		}
109c3e85bdcSAkhil Goyal 	}
110c4509373SSantosh Shukla 	return (rte_iova_t)(NULL);
111c3e85bdcSAkhil Goyal }
112c3e85bdcSAkhil Goyal 
113fcf67029SHemant Agrawal /* virtual address conversin when mempool support is available for ctx */
114fcf67029SHemant Agrawal static inline phys_addr_t
115fcf67029SHemant Agrawal dpaa_mem_vtop_ctx(struct dpaa_sec_op_ctx *ctx, void *vaddr)
116fcf67029SHemant Agrawal {
117fcf67029SHemant Agrawal 	return (uint64_t)vaddr - ctx->vtop_offset;
118fcf67029SHemant Agrawal }
119fcf67029SHemant Agrawal 
120c3e85bdcSAkhil Goyal static inline void *
121c4509373SSantosh Shukla dpaa_mem_ptov(rte_iova_t paddr)
122c3e85bdcSAkhil Goyal {
123c3e85bdcSAkhil Goyal 	const struct rte_memseg *memseg = rte_eal_get_physmem_layout();
124c3e85bdcSAkhil Goyal 	int i;
125c3e85bdcSAkhil Goyal 
126c3e85bdcSAkhil Goyal 	for (i = 0; i < RTE_MAX_MEMSEG && memseg[i].addr_64 != 0; i++) {
1272aab8337SHemant Agrawal 		if (paddr >= memseg[i].iova &&
1282aab8337SHemant Agrawal 		    (char *)paddr < (char *)memseg[i].iova + memseg[i].len)
129c3e85bdcSAkhil Goyal 			return (void *)(memseg[i].addr_64 +
1302aab8337SHemant Agrawal 					(paddr - memseg[i].iova));
131c3e85bdcSAkhil Goyal 	}
132c3e85bdcSAkhil Goyal 	return NULL;
133c3e85bdcSAkhil Goyal }
134c3e85bdcSAkhil Goyal 
135c3e85bdcSAkhil Goyal static void
136c3e85bdcSAkhil Goyal ern_sec_fq_handler(struct qman_portal *qm __rte_unused,
137c3e85bdcSAkhil Goyal 		   struct qman_fq *fq,
138c3e85bdcSAkhil Goyal 		   const struct qm_mr_entry *msg)
139c3e85bdcSAkhil Goyal {
140c3e85bdcSAkhil Goyal 	RTE_LOG_DP(ERR, PMD, "sec fq %d error, RC = %x, seqnum = %x\n",
141c3e85bdcSAkhil Goyal 		   fq->fqid, msg->ern.rc, msg->ern.seqnum);
142c3e85bdcSAkhil Goyal }
143c3e85bdcSAkhil Goyal 
144c3e85bdcSAkhil Goyal /* initialize the queue with dest chan as caam chan so that
145c3e85bdcSAkhil Goyal  * all the packets in this queue could be dispatched into caam
146c3e85bdcSAkhil Goyal  */
147c3e85bdcSAkhil Goyal static int
148c4509373SSantosh Shukla dpaa_sec_init_rx(struct qman_fq *fq_in, rte_iova_t hwdesc,
149c3e85bdcSAkhil Goyal 		 uint32_t fqid_out)
150c3e85bdcSAkhil Goyal {
151c3e85bdcSAkhil Goyal 	struct qm_mcc_initfq fq_opts;
152c3e85bdcSAkhil Goyal 	uint32_t flags;
153c3e85bdcSAkhil Goyal 	int ret = -1;
154c3e85bdcSAkhil Goyal 
155c3e85bdcSAkhil Goyal 	/* Clear FQ options */
156c3e85bdcSAkhil Goyal 	memset(&fq_opts, 0x00, sizeof(struct qm_mcc_initfq));
157c3e85bdcSAkhil Goyal 
158c3e85bdcSAkhil Goyal 	flags = QMAN_INITFQ_FLAG_SCHED;
159c3e85bdcSAkhil Goyal 	fq_opts.we_mask = QM_INITFQ_WE_DESTWQ | QM_INITFQ_WE_CONTEXTA |
160c3e85bdcSAkhil Goyal 			  QM_INITFQ_WE_CONTEXTB;
161c3e85bdcSAkhil Goyal 
162c3e85bdcSAkhil Goyal 	qm_fqd_context_a_set64(&fq_opts.fqd, hwdesc);
163c3e85bdcSAkhil Goyal 	fq_opts.fqd.context_b = fqid_out;
164c3e85bdcSAkhil Goyal 	fq_opts.fqd.dest.channel = qm_channel_caam;
165c3e85bdcSAkhil Goyal 	fq_opts.fqd.dest.wq = 0;
166c3e85bdcSAkhil Goyal 
167c3e85bdcSAkhil Goyal 	fq_in->cb.ern  = ern_sec_fq_handler;
168c3e85bdcSAkhil Goyal 
169e79416d1SHemant Agrawal 	PMD_INIT_LOG(DEBUG, "in-%x out-%x", fq_in->fqid, fqid_out);
170e79416d1SHemant Agrawal 
171c3e85bdcSAkhil Goyal 	ret = qman_init_fq(fq_in, flags, &fq_opts);
172c3e85bdcSAkhil Goyal 	if (unlikely(ret != 0))
173e79416d1SHemant Agrawal 		PMD_INIT_LOG(ERR, "qman_init_fq failed %d", ret);
174c3e85bdcSAkhil Goyal 
175c3e85bdcSAkhil Goyal 	return ret;
176c3e85bdcSAkhil Goyal }
177c3e85bdcSAkhil Goyal 
178c3e85bdcSAkhil Goyal /* something is put into in_fq and caam put the crypto result into out_fq */
179c3e85bdcSAkhil Goyal static enum qman_cb_dqrr_result
180c3e85bdcSAkhil Goyal dqrr_out_fq_cb_rx(struct qman_portal *qm __always_unused,
181c3e85bdcSAkhil Goyal 		  struct qman_fq *fq __always_unused,
182c3e85bdcSAkhil Goyal 		  const struct qm_dqrr_entry *dqrr)
183c3e85bdcSAkhil Goyal {
184c3e85bdcSAkhil Goyal 	const struct qm_fd *fd;
185c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *job;
186c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
187c3e85bdcSAkhil Goyal 
188c3e85bdcSAkhil Goyal 	if (dpaa_sec_op_nb >= DPAA_SEC_BURST)
189c3e85bdcSAkhil Goyal 		return qman_cb_dqrr_defer;
190c3e85bdcSAkhil Goyal 
191c3e85bdcSAkhil Goyal 	if (!(dqrr->stat & QM_DQRR_STAT_FD_VALID))
192c3e85bdcSAkhil Goyal 		return qman_cb_dqrr_consume;
193c3e85bdcSAkhil Goyal 
194c3e85bdcSAkhil Goyal 	fd = &dqrr->fd;
195c3e85bdcSAkhil Goyal 	/* sg is embedded in an op ctx,
196c3e85bdcSAkhil Goyal 	 * sg[0] is for output
197c3e85bdcSAkhil Goyal 	 * sg[1] for input
198c3e85bdcSAkhil Goyal 	 */
199c3e85bdcSAkhil Goyal 	job = dpaa_mem_ptov(qm_fd_addr_get64(fd));
2001f14d500SAkhil Goyal 
201c3e85bdcSAkhil Goyal 	ctx = container_of(job, struct dpaa_sec_op_ctx, job);
202c3e85bdcSAkhil Goyal 	ctx->fd_status = fd->status;
2031f14d500SAkhil Goyal 	if (ctx->op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
2041f14d500SAkhil Goyal 		struct qm_sg_entry *sg_out;
2051f14d500SAkhil Goyal 		uint32_t len;
2061f14d500SAkhil Goyal 
2071f14d500SAkhil Goyal 		sg_out = &job->sg[0];
2081f14d500SAkhil Goyal 		hw_sg_to_cpu(sg_out);
2091f14d500SAkhil Goyal 		len = sg_out->length;
2101f14d500SAkhil Goyal 		ctx->op->sym->m_src->pkt_len = len;
2111f14d500SAkhil Goyal 		ctx->op->sym->m_src->data_len = len;
2121f14d500SAkhil Goyal 	}
213c3e85bdcSAkhil Goyal 	dpaa_sec_ops[dpaa_sec_op_nb++] = ctx->op;
214c3e85bdcSAkhil Goyal 	dpaa_sec_op_ending(ctx);
215c3e85bdcSAkhil Goyal 
216c3e85bdcSAkhil Goyal 	return qman_cb_dqrr_consume;
217c3e85bdcSAkhil Goyal }
218c3e85bdcSAkhil Goyal 
219c3e85bdcSAkhil Goyal /* caam result is put into this queue */
220c3e85bdcSAkhil Goyal static int
221c3e85bdcSAkhil Goyal dpaa_sec_init_tx(struct qman_fq *fq)
222c3e85bdcSAkhil Goyal {
223c3e85bdcSAkhil Goyal 	int ret;
224c3e85bdcSAkhil Goyal 	struct qm_mcc_initfq opts;
225c3e85bdcSAkhil Goyal 	uint32_t flags;
226c3e85bdcSAkhil Goyal 
227c3e85bdcSAkhil Goyal 	flags = QMAN_FQ_FLAG_NO_ENQUEUE | QMAN_FQ_FLAG_LOCKED |
228c3e85bdcSAkhil Goyal 		QMAN_FQ_FLAG_DYNAMIC_FQID;
229c3e85bdcSAkhil Goyal 
230c3e85bdcSAkhil Goyal 	ret = qman_create_fq(0, flags, fq);
231c3e85bdcSAkhil Goyal 	if (unlikely(ret)) {
232c3e85bdcSAkhil Goyal 		PMD_INIT_LOG(ERR, "qman_create_fq failed");
233c3e85bdcSAkhil Goyal 		return ret;
234c3e85bdcSAkhil Goyal 	}
235c3e85bdcSAkhil Goyal 
236c3e85bdcSAkhil Goyal 	memset(&opts, 0, sizeof(opts));
237c3e85bdcSAkhil Goyal 	opts.we_mask = QM_INITFQ_WE_DESTWQ | QM_INITFQ_WE_FQCTRL |
238c3e85bdcSAkhil Goyal 		       QM_INITFQ_WE_CONTEXTA | QM_INITFQ_WE_CONTEXTB;
239c3e85bdcSAkhil Goyal 
240c3e85bdcSAkhil Goyal 	/* opts.fqd.dest.channel = dpaa_sec_pool_chan; */
241c3e85bdcSAkhil Goyal 
242c3e85bdcSAkhil Goyal 	fq->cb.dqrr = dqrr_out_fq_cb_rx;
243c3e85bdcSAkhil Goyal 	fq->cb.ern  = ern_sec_fq_handler;
244c3e85bdcSAkhil Goyal 
245c3e85bdcSAkhil Goyal 	ret = qman_init_fq(fq, 0, &opts);
246c3e85bdcSAkhil Goyal 	if (unlikely(ret)) {
247c3e85bdcSAkhil Goyal 		PMD_INIT_LOG(ERR, "unable to init caam source fq!");
248c3e85bdcSAkhil Goyal 		return ret;
249c3e85bdcSAkhil Goyal 	}
250c3e85bdcSAkhil Goyal 
251c3e85bdcSAkhil Goyal 	return ret;
252c3e85bdcSAkhil Goyal }
253c3e85bdcSAkhil Goyal 
254c3e85bdcSAkhil Goyal static inline int is_cipher_only(dpaa_sec_session *ses)
255c3e85bdcSAkhil Goyal {
256c3e85bdcSAkhil Goyal 	return ((ses->cipher_alg != RTE_CRYPTO_CIPHER_NULL) &&
257c3e85bdcSAkhil Goyal 		(ses->auth_alg == RTE_CRYPTO_AUTH_NULL));
258c3e85bdcSAkhil Goyal }
259c3e85bdcSAkhil Goyal 
260c3e85bdcSAkhil Goyal static inline int is_auth_only(dpaa_sec_session *ses)
261c3e85bdcSAkhil Goyal {
262c3e85bdcSAkhil Goyal 	return ((ses->cipher_alg == RTE_CRYPTO_CIPHER_NULL) &&
263c3e85bdcSAkhil Goyal 		(ses->auth_alg != RTE_CRYPTO_AUTH_NULL));
264c3e85bdcSAkhil Goyal }
265c3e85bdcSAkhil Goyal 
266c3e85bdcSAkhil Goyal static inline int is_aead(dpaa_sec_session *ses)
267c3e85bdcSAkhil Goyal {
268c3e85bdcSAkhil Goyal 	return ((ses->cipher_alg == 0) &&
269c3e85bdcSAkhil Goyal 		(ses->auth_alg == 0) &&
270c3e85bdcSAkhil Goyal 		(ses->aead_alg != 0));
271c3e85bdcSAkhil Goyal }
272c3e85bdcSAkhil Goyal 
273c3e85bdcSAkhil Goyal static inline int is_auth_cipher(dpaa_sec_session *ses)
274c3e85bdcSAkhil Goyal {
275c3e85bdcSAkhil Goyal 	return ((ses->cipher_alg != RTE_CRYPTO_CIPHER_NULL) &&
2761f14d500SAkhil Goyal 		(ses->auth_alg != RTE_CRYPTO_AUTH_NULL) &&
2771f14d500SAkhil Goyal 		(ses->proto_alg != RTE_SECURITY_PROTOCOL_IPSEC));
2781f14d500SAkhil Goyal }
2791f14d500SAkhil Goyal 
2801f14d500SAkhil Goyal static inline int is_proto_ipsec(dpaa_sec_session *ses)
2811f14d500SAkhil Goyal {
2821f14d500SAkhil Goyal 	return (ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC);
283c3e85bdcSAkhil Goyal }
284c3e85bdcSAkhil Goyal 
285c3e85bdcSAkhil Goyal static inline int is_encode(dpaa_sec_session *ses)
286c3e85bdcSAkhil Goyal {
287c3e85bdcSAkhil Goyal 	return ses->dir == DIR_ENC;
288c3e85bdcSAkhil Goyal }
289c3e85bdcSAkhil Goyal 
290c3e85bdcSAkhil Goyal static inline int is_decode(dpaa_sec_session *ses)
291c3e85bdcSAkhil Goyal {
292c3e85bdcSAkhil Goyal 	return ses->dir == DIR_DEC;
293c3e85bdcSAkhil Goyal }
294c3e85bdcSAkhil Goyal 
295c3e85bdcSAkhil Goyal static inline void
296c3e85bdcSAkhil Goyal caam_auth_alg(dpaa_sec_session *ses, struct alginfo *alginfo_a)
297c3e85bdcSAkhil Goyal {
298c3e85bdcSAkhil Goyal 	switch (ses->auth_alg) {
299c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AUTH_NULL:
300c3e85bdcSAkhil Goyal 		ses->digest_length = 0;
301c3e85bdcSAkhil Goyal 		break;
302c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AUTH_MD5_HMAC:
3031f14d500SAkhil Goyal 		alginfo_a->algtype =
3041f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3051f14d500SAkhil Goyal 			OP_PCL_IPSEC_HMAC_MD5_96 : OP_ALG_ALGSEL_MD5;
306c3e85bdcSAkhil Goyal 		alginfo_a->algmode = OP_ALG_AAI_HMAC;
307c3e85bdcSAkhil Goyal 		break;
308c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA1_HMAC:
3091f14d500SAkhil Goyal 		alginfo_a->algtype =
3101f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3111f14d500SAkhil Goyal 			OP_PCL_IPSEC_HMAC_SHA1_96 : OP_ALG_ALGSEL_SHA1;
312c3e85bdcSAkhil Goyal 		alginfo_a->algmode = OP_ALG_AAI_HMAC;
313c3e85bdcSAkhil Goyal 		break;
314c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA224_HMAC:
3151f14d500SAkhil Goyal 		alginfo_a->algtype =
3161f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3171f14d500SAkhil Goyal 			OP_PCL_IPSEC_HMAC_SHA1_160 : OP_ALG_ALGSEL_SHA224;
318c3e85bdcSAkhil Goyal 		alginfo_a->algmode = OP_ALG_AAI_HMAC;
319c3e85bdcSAkhil Goyal 		break;
320c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA256_HMAC:
3211f14d500SAkhil Goyal 		alginfo_a->algtype =
3221f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3231f14d500SAkhil Goyal 			OP_PCL_IPSEC_HMAC_SHA2_256_128 : OP_ALG_ALGSEL_SHA256;
324c3e85bdcSAkhil Goyal 		alginfo_a->algmode = OP_ALG_AAI_HMAC;
325c3e85bdcSAkhil Goyal 		break;
326c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA384_HMAC:
3271f14d500SAkhil Goyal 		alginfo_a->algtype =
3281f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3291f14d500SAkhil Goyal 			OP_PCL_IPSEC_HMAC_SHA2_384_192 : OP_ALG_ALGSEL_SHA384;
330c3e85bdcSAkhil Goyal 		alginfo_a->algmode = OP_ALG_AAI_HMAC;
331c3e85bdcSAkhil Goyal 		break;
332c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA512_HMAC:
3331f14d500SAkhil Goyal 		alginfo_a->algtype =
3341f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3351f14d500SAkhil Goyal 			OP_PCL_IPSEC_HMAC_SHA2_512_256 : OP_ALG_ALGSEL_SHA512;
336c3e85bdcSAkhil Goyal 		alginfo_a->algmode = OP_ALG_AAI_HMAC;
337c3e85bdcSAkhil Goyal 		break;
338c3e85bdcSAkhil Goyal 	default:
339c3e85bdcSAkhil Goyal 		PMD_INIT_LOG(ERR, "unsupported auth alg %u", ses->auth_alg);
340c3e85bdcSAkhil Goyal 	}
341c3e85bdcSAkhil Goyal }
342c3e85bdcSAkhil Goyal 
343c3e85bdcSAkhil Goyal static inline void
344c3e85bdcSAkhil Goyal caam_cipher_alg(dpaa_sec_session *ses, struct alginfo *alginfo_c)
345c3e85bdcSAkhil Goyal {
346c3e85bdcSAkhil Goyal 	switch (ses->cipher_alg) {
347c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_CIPHER_NULL:
348c3e85bdcSAkhil Goyal 		break;
349c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_CIPHER_AES_CBC:
3501f14d500SAkhil Goyal 		alginfo_c->algtype =
3511f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3521f14d500SAkhil Goyal 			OP_PCL_IPSEC_AES_CBC : OP_ALG_ALGSEL_AES;
353c3e85bdcSAkhil Goyal 		alginfo_c->algmode = OP_ALG_AAI_CBC;
354c3e85bdcSAkhil Goyal 		break;
355c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_CIPHER_3DES_CBC:
3561f14d500SAkhil Goyal 		alginfo_c->algtype =
3571f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3581f14d500SAkhil Goyal 			OP_PCL_IPSEC_3DES : OP_ALG_ALGSEL_3DES;
359c3e85bdcSAkhil Goyal 		alginfo_c->algmode = OP_ALG_AAI_CBC;
360c3e85bdcSAkhil Goyal 		break;
361c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_CIPHER_AES_CTR:
3621f14d500SAkhil Goyal 		alginfo_c->algtype =
3631f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3641f14d500SAkhil Goyal 			OP_PCL_IPSEC_AES_CTR : OP_ALG_ALGSEL_AES;
365c3e85bdcSAkhil Goyal 		alginfo_c->algmode = OP_ALG_AAI_CTR;
366c3e85bdcSAkhil Goyal 		break;
367c3e85bdcSAkhil Goyal 	default:
368c3e85bdcSAkhil Goyal 		PMD_INIT_LOG(ERR, "unsupported cipher alg %d", ses->cipher_alg);
369c3e85bdcSAkhil Goyal 	}
370c3e85bdcSAkhil Goyal }
371c3e85bdcSAkhil Goyal 
372c3e85bdcSAkhil Goyal static inline void
373c3e85bdcSAkhil Goyal caam_aead_alg(dpaa_sec_session *ses, struct alginfo *alginfo)
374c3e85bdcSAkhil Goyal {
375c3e85bdcSAkhil Goyal 	switch (ses->aead_alg) {
376c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AEAD_AES_GCM:
377c3e85bdcSAkhil Goyal 		alginfo->algtype = OP_ALG_ALGSEL_AES;
378c3e85bdcSAkhil Goyal 		alginfo->algmode = OP_ALG_AAI_GCM;
379c3e85bdcSAkhil Goyal 		break;
380c3e85bdcSAkhil Goyal 	default:
381c3e85bdcSAkhil Goyal 		PMD_INIT_LOG(ERR, "unsupported AEAD alg %d", ses->aead_alg);
382c3e85bdcSAkhil Goyal 	}
383c3e85bdcSAkhil Goyal }
384c3e85bdcSAkhil Goyal 
385c3e85bdcSAkhil Goyal 
386c3e85bdcSAkhil Goyal /* prepare command block of the session */
387c3e85bdcSAkhil Goyal static int
388c3e85bdcSAkhil Goyal dpaa_sec_prep_cdb(dpaa_sec_session *ses)
389c3e85bdcSAkhil Goyal {
390c3e85bdcSAkhil Goyal 	struct alginfo alginfo_c = {0}, alginfo_a = {0}, alginfo = {0};
391c3e85bdcSAkhil Goyal 	uint32_t shared_desc_len = 0;
392e79416d1SHemant Agrawal 	struct sec_cdb *cdb = &ses->cdb;
393c3e85bdcSAkhil Goyal 	int err;
394c3e85bdcSAkhil Goyal #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
395c3e85bdcSAkhil Goyal 	int swap = false;
396c3e85bdcSAkhil Goyal #else
397c3e85bdcSAkhil Goyal 	int swap = true;
398c3e85bdcSAkhil Goyal #endif
399c3e85bdcSAkhil Goyal 
400c3e85bdcSAkhil Goyal 	memset(cdb, 0, sizeof(struct sec_cdb));
401c3e85bdcSAkhil Goyal 
402c3e85bdcSAkhil Goyal 	if (is_cipher_only(ses)) {
403c3e85bdcSAkhil Goyal 		caam_cipher_alg(ses, &alginfo_c);
404c3e85bdcSAkhil Goyal 		if (alginfo_c.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
405c3e85bdcSAkhil Goyal 			PMD_TX_LOG(ERR, "not supported cipher alg\n");
406c3e85bdcSAkhil Goyal 			return -ENOTSUP;
407c3e85bdcSAkhil Goyal 		}
408c3e85bdcSAkhil Goyal 
409c3e85bdcSAkhil Goyal 		alginfo_c.key = (uint64_t)ses->cipher_key.data;
410c3e85bdcSAkhil Goyal 		alginfo_c.keylen = ses->cipher_key.length;
411c3e85bdcSAkhil Goyal 		alginfo_c.key_enc_flags = 0;
412c3e85bdcSAkhil Goyal 		alginfo_c.key_type = RTA_DATA_IMM;
413c3e85bdcSAkhil Goyal 
414c3e85bdcSAkhil Goyal 		shared_desc_len = cnstr_shdsc_blkcipher(
415c3e85bdcSAkhil Goyal 						cdb->sh_desc, true,
416c3e85bdcSAkhil Goyal 						swap, &alginfo_c,
417c3e85bdcSAkhil Goyal 						NULL,
418c3e85bdcSAkhil Goyal 						ses->iv.length,
419c3e85bdcSAkhil Goyal 						ses->dir);
420c3e85bdcSAkhil Goyal 	} else if (is_auth_only(ses)) {
421c3e85bdcSAkhil Goyal 		caam_auth_alg(ses, &alginfo_a);
422c3e85bdcSAkhil Goyal 		if (alginfo_a.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
423c3e85bdcSAkhil Goyal 			PMD_TX_LOG(ERR, "not supported auth alg\n");
424c3e85bdcSAkhil Goyal 			return -ENOTSUP;
425c3e85bdcSAkhil Goyal 		}
426c3e85bdcSAkhil Goyal 
427c3e85bdcSAkhil Goyal 		alginfo_a.key = (uint64_t)ses->auth_key.data;
428c3e85bdcSAkhil Goyal 		alginfo_a.keylen = ses->auth_key.length;
429c3e85bdcSAkhil Goyal 		alginfo_a.key_enc_flags = 0;
430c3e85bdcSAkhil Goyal 		alginfo_a.key_type = RTA_DATA_IMM;
431c3e85bdcSAkhil Goyal 
432c3e85bdcSAkhil Goyal 		shared_desc_len = cnstr_shdsc_hmac(cdb->sh_desc, true,
433c3e85bdcSAkhil Goyal 						   swap, &alginfo_a,
434c3e85bdcSAkhil Goyal 						   !ses->dir,
435c3e85bdcSAkhil Goyal 						   ses->digest_length);
436c3e85bdcSAkhil Goyal 	} else if (is_aead(ses)) {
437c3e85bdcSAkhil Goyal 		caam_aead_alg(ses, &alginfo);
438c3e85bdcSAkhil Goyal 		if (alginfo.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
439c3e85bdcSAkhil Goyal 			PMD_TX_LOG(ERR, "not supported aead alg\n");
440c3e85bdcSAkhil Goyal 			return -ENOTSUP;
441c3e85bdcSAkhil Goyal 		}
442c3e85bdcSAkhil Goyal 		alginfo.key = (uint64_t)ses->aead_key.data;
443c3e85bdcSAkhil Goyal 		alginfo.keylen = ses->aead_key.length;
444c3e85bdcSAkhil Goyal 		alginfo.key_enc_flags = 0;
445c3e85bdcSAkhil Goyal 		alginfo.key_type = RTA_DATA_IMM;
446c3e85bdcSAkhil Goyal 
447c3e85bdcSAkhil Goyal 		if (ses->dir == DIR_ENC)
448c3e85bdcSAkhil Goyal 			shared_desc_len = cnstr_shdsc_gcm_encap(
449c3e85bdcSAkhil Goyal 					cdb->sh_desc, true, swap,
450c3e85bdcSAkhil Goyal 					&alginfo,
451c3e85bdcSAkhil Goyal 					ses->iv.length,
452c3e85bdcSAkhil Goyal 					ses->digest_length);
453c3e85bdcSAkhil Goyal 		else
454c3e85bdcSAkhil Goyal 			shared_desc_len = cnstr_shdsc_gcm_decap(
455c3e85bdcSAkhil Goyal 					cdb->sh_desc, true, swap,
456c3e85bdcSAkhil Goyal 					&alginfo,
457c3e85bdcSAkhil Goyal 					ses->iv.length,
458c3e85bdcSAkhil Goyal 					ses->digest_length);
459c3e85bdcSAkhil Goyal 	} else {
460c3e85bdcSAkhil Goyal 		caam_cipher_alg(ses, &alginfo_c);
461c3e85bdcSAkhil Goyal 		if (alginfo_c.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
462c3e85bdcSAkhil Goyal 			PMD_TX_LOG(ERR, "not supported cipher alg\n");
463c3e85bdcSAkhil Goyal 			return -ENOTSUP;
464c3e85bdcSAkhil Goyal 		}
465c3e85bdcSAkhil Goyal 
466c3e85bdcSAkhil Goyal 		alginfo_c.key = (uint64_t)ses->cipher_key.data;
467c3e85bdcSAkhil Goyal 		alginfo_c.keylen = ses->cipher_key.length;
468c3e85bdcSAkhil Goyal 		alginfo_c.key_enc_flags = 0;
469c3e85bdcSAkhil Goyal 		alginfo_c.key_type = RTA_DATA_IMM;
470c3e85bdcSAkhil Goyal 
471c3e85bdcSAkhil Goyal 		caam_auth_alg(ses, &alginfo_a);
472c3e85bdcSAkhil Goyal 		if (alginfo_a.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
473c3e85bdcSAkhil Goyal 			PMD_TX_LOG(ERR, "not supported auth alg\n");
474c3e85bdcSAkhil Goyal 			return -ENOTSUP;
475c3e85bdcSAkhil Goyal 		}
476c3e85bdcSAkhil Goyal 
477c3e85bdcSAkhil Goyal 		alginfo_a.key = (uint64_t)ses->auth_key.data;
478c3e85bdcSAkhil Goyal 		alginfo_a.keylen = ses->auth_key.length;
479c3e85bdcSAkhil Goyal 		alginfo_a.key_enc_flags = 0;
480c3e85bdcSAkhil Goyal 		alginfo_a.key_type = RTA_DATA_IMM;
481c3e85bdcSAkhil Goyal 
482c3e85bdcSAkhil Goyal 		cdb->sh_desc[0] = alginfo_c.keylen;
483c3e85bdcSAkhil Goyal 		cdb->sh_desc[1] = alginfo_a.keylen;
484c3e85bdcSAkhil Goyal 		err = rta_inline_query(IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN,
485c3e85bdcSAkhil Goyal 				       MIN_JOB_DESC_SIZE,
486c3e85bdcSAkhil Goyal 				       (unsigned int *)cdb->sh_desc,
487c3e85bdcSAkhil Goyal 				       &cdb->sh_desc[2], 2);
488c3e85bdcSAkhil Goyal 
489c3e85bdcSAkhil Goyal 		if (err < 0) {
490c3e85bdcSAkhil Goyal 			PMD_TX_LOG(ERR, "Crypto: Incorrect key lengths");
491c3e85bdcSAkhil Goyal 			return err;
492c3e85bdcSAkhil Goyal 		}
493c3e85bdcSAkhil Goyal 		if (cdb->sh_desc[2] & 1)
494c3e85bdcSAkhil Goyal 			alginfo_c.key_type = RTA_DATA_IMM;
495c3e85bdcSAkhil Goyal 		else {
496c3e85bdcSAkhil Goyal 			alginfo_c.key = (uint64_t)dpaa_mem_vtop(
497c3e85bdcSAkhil Goyal 							(void *)alginfo_c.key);
498c3e85bdcSAkhil Goyal 			alginfo_c.key_type = RTA_DATA_PTR;
499c3e85bdcSAkhil Goyal 		}
500c3e85bdcSAkhil Goyal 		if (cdb->sh_desc[2] & (1<<1))
501c3e85bdcSAkhil Goyal 			alginfo_a.key_type = RTA_DATA_IMM;
502c3e85bdcSAkhil Goyal 		else {
503c3e85bdcSAkhil Goyal 			alginfo_a.key = (uint64_t)dpaa_mem_vtop(
504c3e85bdcSAkhil Goyal 							(void *)alginfo_a.key);
505c3e85bdcSAkhil Goyal 			alginfo_a.key_type = RTA_DATA_PTR;
506c3e85bdcSAkhil Goyal 		}
507c3e85bdcSAkhil Goyal 		cdb->sh_desc[0] = 0;
508c3e85bdcSAkhil Goyal 		cdb->sh_desc[1] = 0;
509c3e85bdcSAkhil Goyal 		cdb->sh_desc[2] = 0;
5101f14d500SAkhil Goyal 		if (is_proto_ipsec(ses)) {
5111f14d500SAkhil Goyal 			if (ses->dir == DIR_ENC) {
5121f14d500SAkhil Goyal 				shared_desc_len = cnstr_shdsc_ipsec_new_encap(
5131f14d500SAkhil Goyal 						cdb->sh_desc,
5141f14d500SAkhil Goyal 						true, swap, &ses->encap_pdb,
5151f14d500SAkhil Goyal 						(uint8_t *)&ses->ip4_hdr,
5161f14d500SAkhil Goyal 						&alginfo_c, &alginfo_a);
5171f14d500SAkhil Goyal 			} else if (ses->dir == DIR_DEC) {
5181f14d500SAkhil Goyal 				shared_desc_len = cnstr_shdsc_ipsec_new_decap(
5191f14d500SAkhil Goyal 						cdb->sh_desc,
5201f14d500SAkhil Goyal 						true, swap, &ses->decap_pdb,
5211f14d500SAkhil Goyal 						&alginfo_c, &alginfo_a);
5221f14d500SAkhil Goyal 			}
5231f14d500SAkhil Goyal 		} else {
5241f14d500SAkhil Goyal 			/* Auth_only_len is set as 0 here and it will be
5251f14d500SAkhil Goyal 			 * overwritten in fd for each packet.
526c3e85bdcSAkhil Goyal 			 */
527c3e85bdcSAkhil Goyal 			shared_desc_len = cnstr_shdsc_authenc(cdb->sh_desc,
528c3e85bdcSAkhil Goyal 					true, swap, &alginfo_c, &alginfo_a,
529c3e85bdcSAkhil Goyal 					ses->iv.length, 0,
530c3e85bdcSAkhil Goyal 					ses->digest_length, ses->dir);
531c3e85bdcSAkhil Goyal 		}
5321f14d500SAkhil Goyal 	}
533c3e85bdcSAkhil Goyal 	cdb->sh_hdr.hi.field.idlen = shared_desc_len;
534c3e85bdcSAkhil Goyal 	cdb->sh_hdr.hi.word = rte_cpu_to_be_32(cdb->sh_hdr.hi.word);
535c3e85bdcSAkhil Goyal 	cdb->sh_hdr.lo.word = rte_cpu_to_be_32(cdb->sh_hdr.lo.word);
536c3e85bdcSAkhil Goyal 
537c3e85bdcSAkhil Goyal 	return 0;
538c3e85bdcSAkhil Goyal }
539c3e85bdcSAkhil Goyal 
540c3e85bdcSAkhil Goyal /* qp is lockless, should be accessed by only one thread */
541c3e85bdcSAkhil Goyal static int
542c3e85bdcSAkhil Goyal dpaa_sec_deq(struct dpaa_sec_qp *qp, struct rte_crypto_op **ops, int nb_ops)
543c3e85bdcSAkhil Goyal {
544c3e85bdcSAkhil Goyal 	struct qman_fq *fq;
5459a984458SAkhil Goyal 	unsigned int pkts = 0;
5469a984458SAkhil Goyal 	int ret;
5479a984458SAkhil Goyal 	struct qm_dqrr_entry *dq;
548c3e85bdcSAkhil Goyal 
549c3e85bdcSAkhil Goyal 	fq = &qp->outq;
5509a984458SAkhil Goyal 	ret = qman_set_vdq(fq, (nb_ops > DPAA_MAX_DEQUEUE_NUM_FRAMES) ?
5519a984458SAkhil Goyal 				DPAA_MAX_DEQUEUE_NUM_FRAMES : nb_ops);
5529a984458SAkhil Goyal 	if (ret)
5539a984458SAkhil Goyal 		return 0;
554c3e85bdcSAkhil Goyal 
5559a984458SAkhil Goyal 	do {
5569a984458SAkhil Goyal 		const struct qm_fd *fd;
5579a984458SAkhil Goyal 		struct dpaa_sec_job *job;
5589a984458SAkhil Goyal 		struct dpaa_sec_op_ctx *ctx;
5599a984458SAkhil Goyal 		struct rte_crypto_op *op;
560c3e85bdcSAkhil Goyal 
5619a984458SAkhil Goyal 		dq = qman_dequeue(fq);
5629a984458SAkhil Goyal 		if (!dq)
5639a984458SAkhil Goyal 			continue;
5649a984458SAkhil Goyal 
5659a984458SAkhil Goyal 		fd = &dq->fd;
5669a984458SAkhil Goyal 		/* sg is embedded in an op ctx,
5679a984458SAkhil Goyal 		 * sg[0] is for output
5689a984458SAkhil Goyal 		 * sg[1] for input
5699a984458SAkhil Goyal 		 */
5709a984458SAkhil Goyal 		job = dpaa_mem_ptov(qm_fd_addr_get64(fd));
5719a984458SAkhil Goyal 
5729a984458SAkhil Goyal 		ctx = container_of(job, struct dpaa_sec_op_ctx, job);
5739a984458SAkhil Goyal 		ctx->fd_status = fd->status;
5749a984458SAkhil Goyal 		op = ctx->op;
5759a984458SAkhil Goyal 		if (op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
5769a984458SAkhil Goyal 			struct qm_sg_entry *sg_out;
5779a984458SAkhil Goyal 			uint32_t len;
5789a984458SAkhil Goyal 
5799a984458SAkhil Goyal 			sg_out = &job->sg[0];
5809a984458SAkhil Goyal 			hw_sg_to_cpu(sg_out);
5819a984458SAkhil Goyal 			len = sg_out->length;
5829a984458SAkhil Goyal 			op->sym->m_src->pkt_len = len;
5839a984458SAkhil Goyal 			op->sym->m_src->data_len = len;
5849a984458SAkhil Goyal 		}
5859a984458SAkhil Goyal 		if (!ctx->fd_status) {
5869a984458SAkhil Goyal 			op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
5879a984458SAkhil Goyal 		} else {
5889a984458SAkhil Goyal 			printf("\nSEC return err: 0x%x", ctx->fd_status);
5899a984458SAkhil Goyal 			op->status = RTE_CRYPTO_OP_STATUS_ERROR;
5909a984458SAkhil Goyal 		}
5919a984458SAkhil Goyal 		ops[pkts++] = op;
5929a984458SAkhil Goyal 
5939a984458SAkhil Goyal 		/* report op status to sym->op and then free the ctx memeory */
5949a984458SAkhil Goyal 		rte_mempool_put(ctx->ctx_pool, (void *)ctx);
5959a984458SAkhil Goyal 
5969a984458SAkhil Goyal 		qman_dqrr_consume(fq, dq);
5979a984458SAkhil Goyal 	} while (fq->flags & QMAN_FQ_STATE_VDQCR);
5989a984458SAkhil Goyal 
5999a984458SAkhil Goyal 	return pkts;
600c3e85bdcSAkhil Goyal }
601c3e85bdcSAkhil Goyal 
602*a74af788SAkhil Goyal static inline struct dpaa_sec_job *
603*a74af788SAkhil Goyal build_auth_only_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
604*a74af788SAkhil Goyal {
605*a74af788SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
606*a74af788SAkhil Goyal 	struct rte_mbuf *mbuf = sym->m_src;
607*a74af788SAkhil Goyal 	struct dpaa_sec_job *cf;
608*a74af788SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
609*a74af788SAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
610*a74af788SAkhil Goyal 	phys_addr_t start_addr;
611*a74af788SAkhil Goyal 	uint8_t *old_digest, extra_segs;
612*a74af788SAkhil Goyal 
613*a74af788SAkhil Goyal 	if (is_decode(ses))
614*a74af788SAkhil Goyal 		extra_segs = 3;
615*a74af788SAkhil Goyal 	else
616*a74af788SAkhil Goyal 		extra_segs = 2;
617*a74af788SAkhil Goyal 
618*a74af788SAkhil Goyal 	if ((mbuf->nb_segs + extra_segs) > MAX_SG_ENTRIES) {
619*a74af788SAkhil Goyal 		PMD_TX_LOG(ERR, "Auth: Max sec segs supported is %d\n",
620*a74af788SAkhil Goyal 								MAX_SG_ENTRIES);
621*a74af788SAkhil Goyal 		return NULL;
622*a74af788SAkhil Goyal 	}
623*a74af788SAkhil Goyal 	ctx = dpaa_sec_alloc_ctx(ses);
624*a74af788SAkhil Goyal 	if (!ctx)
625*a74af788SAkhil Goyal 		return NULL;
626*a74af788SAkhil Goyal 
627*a74af788SAkhil Goyal 	cf = &ctx->job;
628*a74af788SAkhil Goyal 	ctx->op = op;
629*a74af788SAkhil Goyal 	old_digest = ctx->digest;
630*a74af788SAkhil Goyal 
631*a74af788SAkhil Goyal 	/* output */
632*a74af788SAkhil Goyal 	out_sg = &cf->sg[0];
633*a74af788SAkhil Goyal 	qm_sg_entry_set64(out_sg, sym->auth.digest.phys_addr);
634*a74af788SAkhil Goyal 	out_sg->length = ses->digest_length;
635*a74af788SAkhil Goyal 	cpu_to_hw_sg(out_sg);
636*a74af788SAkhil Goyal 
637*a74af788SAkhil Goyal 	/* input */
638*a74af788SAkhil Goyal 	in_sg = &cf->sg[1];
639*a74af788SAkhil Goyal 	/* need to extend the input to a compound frame */
640*a74af788SAkhil Goyal 	in_sg->extension = 1;
641*a74af788SAkhil Goyal 	in_sg->final = 1;
642*a74af788SAkhil Goyal 	in_sg->length = sym->auth.data.length;
643*a74af788SAkhil Goyal 	qm_sg_entry_set64(in_sg, dpaa_mem_vtop_ctx(ctx, &cf->sg[2]));
644*a74af788SAkhil Goyal 
645*a74af788SAkhil Goyal 	/* 1st seg */
646*a74af788SAkhil Goyal 	sg = in_sg + 1;
647*a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
648*a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->auth.data.offset;
649*a74af788SAkhil Goyal 	sg->offset = sym->auth.data.offset;
650*a74af788SAkhil Goyal 
651*a74af788SAkhil Goyal 	/* Successive segs */
652*a74af788SAkhil Goyal 	mbuf = mbuf->next;
653*a74af788SAkhil Goyal 	while (mbuf) {
654*a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
655*a74af788SAkhil Goyal 		sg++;
656*a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
657*a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
658*a74af788SAkhil Goyal 		mbuf = mbuf->next;
659*a74af788SAkhil Goyal 	}
660*a74af788SAkhil Goyal 
661*a74af788SAkhil Goyal 	if (is_decode(ses)) {
662*a74af788SAkhil Goyal 		/* Digest verification case */
663*a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
664*a74af788SAkhil Goyal 		sg++;
665*a74af788SAkhil Goyal 		rte_memcpy(old_digest, sym->auth.digest.data,
666*a74af788SAkhil Goyal 				ses->digest_length);
667*a74af788SAkhil Goyal 		start_addr = dpaa_mem_vtop_ctx(ctx, old_digest);
668*a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, start_addr);
669*a74af788SAkhil Goyal 		sg->length = ses->digest_length;
670*a74af788SAkhil Goyal 		in_sg->length += ses->digest_length;
671*a74af788SAkhil Goyal 	} else {
672*a74af788SAkhil Goyal 		/* Digest calculation case */
673*a74af788SAkhil Goyal 		sg->length -= ses->digest_length;
674*a74af788SAkhil Goyal 	}
675*a74af788SAkhil Goyal 	sg->final = 1;
676*a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
677*a74af788SAkhil Goyal 	cpu_to_hw_sg(in_sg);
678*a74af788SAkhil Goyal 
679*a74af788SAkhil Goyal 	return cf;
680*a74af788SAkhil Goyal }
681*a74af788SAkhil Goyal 
682c3e85bdcSAkhil Goyal /**
683c3e85bdcSAkhil Goyal  * packet looks like:
684c3e85bdcSAkhil Goyal  *		|<----data_len------->|
685c3e85bdcSAkhil Goyal  *    |ip_header|ah_header|icv|payload|
686c3e85bdcSAkhil Goyal  *              ^
687c3e85bdcSAkhil Goyal  *		|
688c3e85bdcSAkhil Goyal  *	   mbuf->pkt.data
689c3e85bdcSAkhil Goyal  */
690c3e85bdcSAkhil Goyal static inline struct dpaa_sec_job *
691c3e85bdcSAkhil Goyal build_auth_only(struct rte_crypto_op *op, dpaa_sec_session *ses)
692c3e85bdcSAkhil Goyal {
693c3e85bdcSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
694c3e85bdcSAkhil Goyal 	struct rte_mbuf *mbuf = sym->m_src;
695c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
696c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
697c3e85bdcSAkhil Goyal 	struct qm_sg_entry *sg;
698c4509373SSantosh Shukla 	rte_iova_t start_addr;
699c3e85bdcSAkhil Goyal 	uint8_t *old_digest;
700c3e85bdcSAkhil Goyal 
701c3e85bdcSAkhil Goyal 	ctx = dpaa_sec_alloc_ctx(ses);
702c3e85bdcSAkhil Goyal 	if (!ctx)
703c3e85bdcSAkhil Goyal 		return NULL;
704c3e85bdcSAkhil Goyal 
705c3e85bdcSAkhil Goyal 	cf = &ctx->job;
706c3e85bdcSAkhil Goyal 	ctx->op = op;
707c3e85bdcSAkhil Goyal 	old_digest = ctx->digest;
708c3e85bdcSAkhil Goyal 
709bfa9a8a4SThomas Monjalon 	start_addr = rte_pktmbuf_iova(mbuf);
710c3e85bdcSAkhil Goyal 	/* output */
711c3e85bdcSAkhil Goyal 	sg = &cf->sg[0];
712c3e85bdcSAkhil Goyal 	qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
713c3e85bdcSAkhil Goyal 	sg->length = ses->digest_length;
714c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
715c3e85bdcSAkhil Goyal 
716c3e85bdcSAkhil Goyal 	/* input */
717c3e85bdcSAkhil Goyal 	sg = &cf->sg[1];
718c3e85bdcSAkhil Goyal 	if (is_decode(ses)) {
719c3e85bdcSAkhil Goyal 		/* need to extend the input to a compound frame */
720c3e85bdcSAkhil Goyal 		sg->extension = 1;
721fcf67029SHemant Agrawal 		qm_sg_entry_set64(sg, dpaa_mem_vtop_ctx(ctx, &cf->sg[2]));
722c3e85bdcSAkhil Goyal 		sg->length = sym->auth.data.length + ses->digest_length;
723c3e85bdcSAkhil Goyal 		sg->final = 1;
724c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
725c3e85bdcSAkhil Goyal 
726c3e85bdcSAkhil Goyal 		sg = &cf->sg[2];
727c3e85bdcSAkhil Goyal 		/* hash result or digest, save digest first */
728c3e85bdcSAkhil Goyal 		rte_memcpy(old_digest, sym->auth.digest.data,
729c3e85bdcSAkhil Goyal 			   ses->digest_length);
730c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, start_addr + sym->auth.data.offset);
731c3e85bdcSAkhil Goyal 		sg->length = sym->auth.data.length;
732c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
733c3e85bdcSAkhil Goyal 
734c3e85bdcSAkhil Goyal 		/* let's check digest by hw */
735fcf67029SHemant Agrawal 		start_addr = dpaa_mem_vtop_ctx(ctx, old_digest);
736c3e85bdcSAkhil Goyal 		sg++;
737c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, start_addr);
738c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
739c3e85bdcSAkhil Goyal 		sg->final = 1;
740c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
741c3e85bdcSAkhil Goyal 	} else {
742c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, start_addr + sym->auth.data.offset);
743c3e85bdcSAkhil Goyal 		sg->length = sym->auth.data.length;
744c3e85bdcSAkhil Goyal 		sg->final = 1;
745c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
746c3e85bdcSAkhil Goyal 	}
747c3e85bdcSAkhil Goyal 
748c3e85bdcSAkhil Goyal 	return cf;
749c3e85bdcSAkhil Goyal }
750c3e85bdcSAkhil Goyal 
751c3e85bdcSAkhil Goyal static inline struct dpaa_sec_job *
752*a74af788SAkhil Goyal build_cipher_only_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
753*a74af788SAkhil Goyal {
754*a74af788SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
755*a74af788SAkhil Goyal 	struct dpaa_sec_job *cf;
756*a74af788SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
757*a74af788SAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
758*a74af788SAkhil Goyal 	struct rte_mbuf *mbuf;
759*a74af788SAkhil Goyal 	uint8_t req_segs;
760*a74af788SAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
761*a74af788SAkhil Goyal 			ses->iv.offset);
762*a74af788SAkhil Goyal 
763*a74af788SAkhil Goyal 	if (sym->m_dst) {
764*a74af788SAkhil Goyal 		mbuf = sym->m_dst;
765*a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 3;
766*a74af788SAkhil Goyal 	} else {
767*a74af788SAkhil Goyal 		mbuf = sym->m_src;
768*a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs * 2 + 3;
769*a74af788SAkhil Goyal 	}
770*a74af788SAkhil Goyal 
771*a74af788SAkhil Goyal 	if (req_segs > MAX_SG_ENTRIES) {
772*a74af788SAkhil Goyal 		PMD_TX_LOG(ERR, "Cipher: Max sec segs supported is %d\n",
773*a74af788SAkhil Goyal 								MAX_SG_ENTRIES);
774*a74af788SAkhil Goyal 		return NULL;
775*a74af788SAkhil Goyal 	}
776*a74af788SAkhil Goyal 
777*a74af788SAkhil Goyal 	ctx = dpaa_sec_alloc_ctx(ses);
778*a74af788SAkhil Goyal 	if (!ctx)
779*a74af788SAkhil Goyal 		return NULL;
780*a74af788SAkhil Goyal 
781*a74af788SAkhil Goyal 	cf = &ctx->job;
782*a74af788SAkhil Goyal 	ctx->op = op;
783*a74af788SAkhil Goyal 
784*a74af788SAkhil Goyal 	/* output */
785*a74af788SAkhil Goyal 	out_sg = &cf->sg[0];
786*a74af788SAkhil Goyal 	out_sg->extension = 1;
787*a74af788SAkhil Goyal 	out_sg->length = sym->cipher.data.length;
788*a74af788SAkhil Goyal 	qm_sg_entry_set64(out_sg, dpaa_mem_vtop_ctx(ctx, &cf->sg[2]));
789*a74af788SAkhil Goyal 	cpu_to_hw_sg(out_sg);
790*a74af788SAkhil Goyal 
791*a74af788SAkhil Goyal 	/* 1st seg */
792*a74af788SAkhil Goyal 	sg = &cf->sg[2];
793*a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
794*a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->cipher.data.offset;
795*a74af788SAkhil Goyal 	sg->offset = sym->cipher.data.offset;
796*a74af788SAkhil Goyal 
797*a74af788SAkhil Goyal 	/* Successive segs */
798*a74af788SAkhil Goyal 	mbuf = mbuf->next;
799*a74af788SAkhil Goyal 	while (mbuf) {
800*a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
801*a74af788SAkhil Goyal 		sg++;
802*a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
803*a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
804*a74af788SAkhil Goyal 		mbuf = mbuf->next;
805*a74af788SAkhil Goyal 	}
806*a74af788SAkhil Goyal 	sg->final = 1;
807*a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
808*a74af788SAkhil Goyal 
809*a74af788SAkhil Goyal 	/* input */
810*a74af788SAkhil Goyal 	mbuf = sym->m_src;
811*a74af788SAkhil Goyal 	in_sg = &cf->sg[1];
812*a74af788SAkhil Goyal 	in_sg->extension = 1;
813*a74af788SAkhil Goyal 	in_sg->final = 1;
814*a74af788SAkhil Goyal 	in_sg->length = sym->cipher.data.length + ses->iv.length;
815*a74af788SAkhil Goyal 
816*a74af788SAkhil Goyal 	sg++;
817*a74af788SAkhil Goyal 	qm_sg_entry_set64(in_sg, dpaa_mem_vtop_ctx(ctx, sg));
818*a74af788SAkhil Goyal 	cpu_to_hw_sg(in_sg);
819*a74af788SAkhil Goyal 
820*a74af788SAkhil Goyal 	/* IV */
821*a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
822*a74af788SAkhil Goyal 	sg->length = ses->iv.length;
823*a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
824*a74af788SAkhil Goyal 
825*a74af788SAkhil Goyal 	/* 1st seg */
826*a74af788SAkhil Goyal 	sg++;
827*a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
828*a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->cipher.data.offset;
829*a74af788SAkhil Goyal 	sg->offset = sym->cipher.data.offset;
830*a74af788SAkhil Goyal 
831*a74af788SAkhil Goyal 	/* Successive segs */
832*a74af788SAkhil Goyal 	mbuf = mbuf->next;
833*a74af788SAkhil Goyal 	while (mbuf) {
834*a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
835*a74af788SAkhil Goyal 		sg++;
836*a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
837*a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
838*a74af788SAkhil Goyal 		mbuf = mbuf->next;
839*a74af788SAkhil Goyal 	}
840*a74af788SAkhil Goyal 	sg->final = 1;
841*a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
842*a74af788SAkhil Goyal 
843*a74af788SAkhil Goyal 	return cf;
844*a74af788SAkhil Goyal }
845*a74af788SAkhil Goyal 
846*a74af788SAkhil Goyal static inline struct dpaa_sec_job *
847c3e85bdcSAkhil Goyal build_cipher_only(struct rte_crypto_op *op, dpaa_sec_session *ses)
848c3e85bdcSAkhil Goyal {
849c3e85bdcSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
850c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
851c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
852c3e85bdcSAkhil Goyal 	struct qm_sg_entry *sg;
853c4509373SSantosh Shukla 	rte_iova_t src_start_addr, dst_start_addr;
854c3e85bdcSAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
855c3e85bdcSAkhil Goyal 			ses->iv.offset);
856c3e85bdcSAkhil Goyal 
857c3e85bdcSAkhil Goyal 	ctx = dpaa_sec_alloc_ctx(ses);
858c3e85bdcSAkhil Goyal 	if (!ctx)
859c3e85bdcSAkhil Goyal 		return NULL;
860c3e85bdcSAkhil Goyal 
861c3e85bdcSAkhil Goyal 	cf = &ctx->job;
862c3e85bdcSAkhil Goyal 	ctx->op = op;
863a389434eSAlok Makhariya 
864bfa9a8a4SThomas Monjalon 	src_start_addr = rte_pktmbuf_iova(sym->m_src);
865a389434eSAlok Makhariya 
866a389434eSAlok Makhariya 	if (sym->m_dst)
867bfa9a8a4SThomas Monjalon 		dst_start_addr = rte_pktmbuf_iova(sym->m_dst);
868a389434eSAlok Makhariya 	else
869a389434eSAlok Makhariya 		dst_start_addr = src_start_addr;
870c3e85bdcSAkhil Goyal 
871c3e85bdcSAkhil Goyal 	/* output */
872c3e85bdcSAkhil Goyal 	sg = &cf->sg[0];
873a389434eSAlok Makhariya 	qm_sg_entry_set64(sg, dst_start_addr + sym->cipher.data.offset);
874c3e85bdcSAkhil Goyal 	sg->length = sym->cipher.data.length + ses->iv.length;
875c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
876c3e85bdcSAkhil Goyal 
877c3e85bdcSAkhil Goyal 	/* input */
878c3e85bdcSAkhil Goyal 	sg = &cf->sg[1];
879c3e85bdcSAkhil Goyal 
880c3e85bdcSAkhil Goyal 	/* need to extend the input to a compound frame */
881c3e85bdcSAkhil Goyal 	sg->extension = 1;
882c3e85bdcSAkhil Goyal 	sg->final = 1;
883c3e85bdcSAkhil Goyal 	sg->length = sym->cipher.data.length + ses->iv.length;
884fcf67029SHemant Agrawal 	qm_sg_entry_set64(sg, dpaa_mem_vtop_ctx(ctx, &cf->sg[2]));
885c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
886c3e85bdcSAkhil Goyal 
887c3e85bdcSAkhil Goyal 	sg = &cf->sg[2];
888c3e85bdcSAkhil Goyal 	qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
889c3e85bdcSAkhil Goyal 	sg->length = ses->iv.length;
890c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
891c3e85bdcSAkhil Goyal 
892c3e85bdcSAkhil Goyal 	sg++;
893a389434eSAlok Makhariya 	qm_sg_entry_set64(sg, src_start_addr + sym->cipher.data.offset);
894c3e85bdcSAkhil Goyal 	sg->length = sym->cipher.data.length;
895c3e85bdcSAkhil Goyal 	sg->final = 1;
896c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
897c3e85bdcSAkhil Goyal 
898c3e85bdcSAkhil Goyal 	return cf;
899c3e85bdcSAkhil Goyal }
900c3e85bdcSAkhil Goyal 
901c3e85bdcSAkhil Goyal static inline struct dpaa_sec_job *
902*a74af788SAkhil Goyal build_cipher_auth_gcm_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
903*a74af788SAkhil Goyal {
904*a74af788SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
905*a74af788SAkhil Goyal 	struct dpaa_sec_job *cf;
906*a74af788SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
907*a74af788SAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
908*a74af788SAkhil Goyal 	struct rte_mbuf *mbuf;
909*a74af788SAkhil Goyal 	uint8_t req_segs;
910*a74af788SAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
911*a74af788SAkhil Goyal 			ses->iv.offset);
912*a74af788SAkhil Goyal 
913*a74af788SAkhil Goyal 	if (sym->m_dst) {
914*a74af788SAkhil Goyal 		mbuf = sym->m_dst;
915*a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 4;
916*a74af788SAkhil Goyal 	} else {
917*a74af788SAkhil Goyal 		mbuf = sym->m_src;
918*a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs * 2 + 4;
919*a74af788SAkhil Goyal 	}
920*a74af788SAkhil Goyal 
921*a74af788SAkhil Goyal 	if (ses->auth_only_len)
922*a74af788SAkhil Goyal 		req_segs++;
923*a74af788SAkhil Goyal 
924*a74af788SAkhil Goyal 	if (req_segs > MAX_SG_ENTRIES) {
925*a74af788SAkhil Goyal 		PMD_TX_LOG(ERR, "AEAD: Max sec segs supported is %d\n",
926*a74af788SAkhil Goyal 				MAX_SG_ENTRIES);
927*a74af788SAkhil Goyal 		return NULL;
928*a74af788SAkhil Goyal 	}
929*a74af788SAkhil Goyal 
930*a74af788SAkhil Goyal 	ctx = dpaa_sec_alloc_ctx(ses);
931*a74af788SAkhil Goyal 	if (!ctx)
932*a74af788SAkhil Goyal 		return NULL;
933*a74af788SAkhil Goyal 
934*a74af788SAkhil Goyal 	cf = &ctx->job;
935*a74af788SAkhil Goyal 	ctx->op = op;
936*a74af788SAkhil Goyal 
937*a74af788SAkhil Goyal 	rte_prefetch0(cf->sg);
938*a74af788SAkhil Goyal 
939*a74af788SAkhil Goyal 	/* output */
940*a74af788SAkhil Goyal 	out_sg = &cf->sg[0];
941*a74af788SAkhil Goyal 	out_sg->extension = 1;
942*a74af788SAkhil Goyal 	if (is_encode(ses))
943*a74af788SAkhil Goyal 		out_sg->length = sym->aead.data.length + ses->auth_only_len
944*a74af788SAkhil Goyal 						+ ses->digest_length;
945*a74af788SAkhil Goyal 	else
946*a74af788SAkhil Goyal 		out_sg->length = sym->aead.data.length + ses->auth_only_len;
947*a74af788SAkhil Goyal 
948*a74af788SAkhil Goyal 	/* output sg entries */
949*a74af788SAkhil Goyal 	sg = &cf->sg[2];
950*a74af788SAkhil Goyal 	qm_sg_entry_set64(out_sg, dpaa_mem_vtop_ctx(ctx, sg));
951*a74af788SAkhil Goyal 	cpu_to_hw_sg(out_sg);
952*a74af788SAkhil Goyal 
953*a74af788SAkhil Goyal 	/* 1st seg */
954*a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
955*a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->aead.data.offset +
956*a74af788SAkhil Goyal 					ses->auth_only_len;
957*a74af788SAkhil Goyal 	sg->offset = sym->aead.data.offset - ses->auth_only_len;
958*a74af788SAkhil Goyal 
959*a74af788SAkhil Goyal 	/* Successive segs */
960*a74af788SAkhil Goyal 	mbuf = mbuf->next;
961*a74af788SAkhil Goyal 	while (mbuf) {
962*a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
963*a74af788SAkhil Goyal 		sg++;
964*a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
965*a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
966*a74af788SAkhil Goyal 		mbuf = mbuf->next;
967*a74af788SAkhil Goyal 	}
968*a74af788SAkhil Goyal 	sg->length -= ses->digest_length;
969*a74af788SAkhil Goyal 
970*a74af788SAkhil Goyal 	if (is_encode(ses)) {
971*a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
972*a74af788SAkhil Goyal 		/* set auth output */
973*a74af788SAkhil Goyal 		sg++;
974*a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, sym->aead.digest.phys_addr);
975*a74af788SAkhil Goyal 		sg->length = ses->digest_length;
976*a74af788SAkhil Goyal 	}
977*a74af788SAkhil Goyal 	sg->final = 1;
978*a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
979*a74af788SAkhil Goyal 
980*a74af788SAkhil Goyal 	/* input */
981*a74af788SAkhil Goyal 	mbuf = sym->m_src;
982*a74af788SAkhil Goyal 	in_sg = &cf->sg[1];
983*a74af788SAkhil Goyal 	in_sg->extension = 1;
984*a74af788SAkhil Goyal 	in_sg->final = 1;
985*a74af788SAkhil Goyal 	if (is_encode(ses))
986*a74af788SAkhil Goyal 		in_sg->length = ses->iv.length + sym->aead.data.length
987*a74af788SAkhil Goyal 							+ ses->auth_only_len;
988*a74af788SAkhil Goyal 	else
989*a74af788SAkhil Goyal 		in_sg->length = ses->iv.length + sym->aead.data.length
990*a74af788SAkhil Goyal 				+ ses->auth_only_len + ses->digest_length;
991*a74af788SAkhil Goyal 
992*a74af788SAkhil Goyal 	/* input sg entries */
993*a74af788SAkhil Goyal 	sg++;
994*a74af788SAkhil Goyal 	qm_sg_entry_set64(in_sg, dpaa_mem_vtop_ctx(ctx, sg));
995*a74af788SAkhil Goyal 	cpu_to_hw_sg(in_sg);
996*a74af788SAkhil Goyal 
997*a74af788SAkhil Goyal 	/* 1st seg IV */
998*a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
999*a74af788SAkhil Goyal 	sg->length = ses->iv.length;
1000*a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1001*a74af788SAkhil Goyal 
1002*a74af788SAkhil Goyal 	/* 2nd seg auth only */
1003*a74af788SAkhil Goyal 	if (ses->auth_only_len) {
1004*a74af788SAkhil Goyal 		sg++;
1005*a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, dpaa_mem_vtop(sym->aead.aad.data));
1006*a74af788SAkhil Goyal 		sg->length = ses->auth_only_len;
1007*a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1008*a74af788SAkhil Goyal 	}
1009*a74af788SAkhil Goyal 
1010*a74af788SAkhil Goyal 	/* 3rd seg */
1011*a74af788SAkhil Goyal 	sg++;
1012*a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1013*a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->aead.data.offset;
1014*a74af788SAkhil Goyal 	sg->offset = sym->aead.data.offset;
1015*a74af788SAkhil Goyal 
1016*a74af788SAkhil Goyal 	/* Successive segs */
1017*a74af788SAkhil Goyal 	mbuf = mbuf->next;
1018*a74af788SAkhil Goyal 	while (mbuf) {
1019*a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1020*a74af788SAkhil Goyal 		sg++;
1021*a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1022*a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1023*a74af788SAkhil Goyal 		mbuf = mbuf->next;
1024*a74af788SAkhil Goyal 	}
1025*a74af788SAkhil Goyal 
1026*a74af788SAkhil Goyal 	if (is_decode(ses)) {
1027*a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1028*a74af788SAkhil Goyal 		sg++;
1029*a74af788SAkhil Goyal 		memcpy(ctx->digest, sym->aead.digest.data,
1030*a74af788SAkhil Goyal 			ses->digest_length);
1031*a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, dpaa_mem_vtop_ctx(ctx, ctx->digest));
1032*a74af788SAkhil Goyal 		sg->length = ses->digest_length;
1033*a74af788SAkhil Goyal 	}
1034*a74af788SAkhil Goyal 	sg->final = 1;
1035*a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1036*a74af788SAkhil Goyal 
1037*a74af788SAkhil Goyal 	return cf;
1038*a74af788SAkhil Goyal }
1039*a74af788SAkhil Goyal 
1040*a74af788SAkhil Goyal static inline struct dpaa_sec_job *
1041c3e85bdcSAkhil Goyal build_cipher_auth_gcm(struct rte_crypto_op *op, dpaa_sec_session *ses)
1042c3e85bdcSAkhil Goyal {
1043c3e85bdcSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1044c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
1045c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1046c3e85bdcSAkhil Goyal 	struct qm_sg_entry *sg;
1047c3e85bdcSAkhil Goyal 	uint32_t length = 0;
1048c4509373SSantosh Shukla 	rte_iova_t src_start_addr, dst_start_addr;
1049c3e85bdcSAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1050c3e85bdcSAkhil Goyal 			ses->iv.offset);
1051c3e85bdcSAkhil Goyal 
1052116ff44aSHemant Agrawal 	src_start_addr = sym->m_src->buf_iova + sym->m_src->data_off;
1053a389434eSAlok Makhariya 
1054a389434eSAlok Makhariya 	if (sym->m_dst)
1055116ff44aSHemant Agrawal 		dst_start_addr = sym->m_dst->buf_iova + sym->m_dst->data_off;
1056a389434eSAlok Makhariya 	else
1057a389434eSAlok Makhariya 		dst_start_addr = src_start_addr;
1058c3e85bdcSAkhil Goyal 
1059c3e85bdcSAkhil Goyal 	ctx = dpaa_sec_alloc_ctx(ses);
1060c3e85bdcSAkhil Goyal 	if (!ctx)
1061c3e85bdcSAkhil Goyal 		return NULL;
1062c3e85bdcSAkhil Goyal 
1063c3e85bdcSAkhil Goyal 	cf = &ctx->job;
1064c3e85bdcSAkhil Goyal 	ctx->op = op;
1065c3e85bdcSAkhil Goyal 
1066c3e85bdcSAkhil Goyal 	/* input */
1067c3e85bdcSAkhil Goyal 	rte_prefetch0(cf->sg);
1068c3e85bdcSAkhil Goyal 	sg = &cf->sg[2];
1069fcf67029SHemant Agrawal 	qm_sg_entry_set64(&cf->sg[1], dpaa_mem_vtop_ctx(ctx, sg));
1070c3e85bdcSAkhil Goyal 	if (is_encode(ses)) {
1071c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1072c3e85bdcSAkhil Goyal 		sg->length = ses->iv.length;
1073c3e85bdcSAkhil Goyal 		length += sg->length;
1074c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1075c3e85bdcSAkhil Goyal 
1076c3e85bdcSAkhil Goyal 		sg++;
1077c3e85bdcSAkhil Goyal 		if (ses->auth_only_len) {
1078c3e85bdcSAkhil Goyal 			qm_sg_entry_set64(sg,
1079c3e85bdcSAkhil Goyal 					  dpaa_mem_vtop(sym->aead.aad.data));
1080c3e85bdcSAkhil Goyal 			sg->length = ses->auth_only_len;
1081c3e85bdcSAkhil Goyal 			length += sg->length;
1082c3e85bdcSAkhil Goyal 			cpu_to_hw_sg(sg);
1083c3e85bdcSAkhil Goyal 			sg++;
1084c3e85bdcSAkhil Goyal 		}
1085a389434eSAlok Makhariya 		qm_sg_entry_set64(sg, src_start_addr + sym->aead.data.offset);
1086c3e85bdcSAkhil Goyal 		sg->length = sym->aead.data.length;
1087c3e85bdcSAkhil Goyal 		length += sg->length;
1088c3e85bdcSAkhil Goyal 		sg->final = 1;
1089c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1090c3e85bdcSAkhil Goyal 	} else {
1091c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1092c3e85bdcSAkhil Goyal 		sg->length = ses->iv.length;
1093c3e85bdcSAkhil Goyal 		length += sg->length;
1094c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1095c3e85bdcSAkhil Goyal 
1096c3e85bdcSAkhil Goyal 		sg++;
1097c3e85bdcSAkhil Goyal 		if (ses->auth_only_len) {
1098c3e85bdcSAkhil Goyal 			qm_sg_entry_set64(sg,
1099c3e85bdcSAkhil Goyal 					  dpaa_mem_vtop(sym->aead.aad.data));
1100c3e85bdcSAkhil Goyal 			sg->length = ses->auth_only_len;
1101c3e85bdcSAkhil Goyal 			length += sg->length;
1102c3e85bdcSAkhil Goyal 			cpu_to_hw_sg(sg);
1103c3e85bdcSAkhil Goyal 			sg++;
1104c3e85bdcSAkhil Goyal 		}
1105a389434eSAlok Makhariya 		qm_sg_entry_set64(sg, src_start_addr + sym->aead.data.offset);
1106c3e85bdcSAkhil Goyal 		sg->length = sym->aead.data.length;
1107c3e85bdcSAkhil Goyal 		length += sg->length;
1108c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1109c3e85bdcSAkhil Goyal 
1110c3e85bdcSAkhil Goyal 		memcpy(ctx->digest, sym->aead.digest.data,
1111c3e85bdcSAkhil Goyal 		       ses->digest_length);
1112c3e85bdcSAkhil Goyal 		sg++;
1113c3e85bdcSAkhil Goyal 
1114fcf67029SHemant Agrawal 		qm_sg_entry_set64(sg, dpaa_mem_vtop_ctx(ctx, ctx->digest));
1115c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
1116c3e85bdcSAkhil Goyal 		length += sg->length;
1117c3e85bdcSAkhil Goyal 		sg->final = 1;
1118c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1119c3e85bdcSAkhil Goyal 	}
1120c3e85bdcSAkhil Goyal 	/* input compound frame */
1121c3e85bdcSAkhil Goyal 	cf->sg[1].length = length;
1122c3e85bdcSAkhil Goyal 	cf->sg[1].extension = 1;
1123c3e85bdcSAkhil Goyal 	cf->sg[1].final = 1;
1124c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(&cf->sg[1]);
1125c3e85bdcSAkhil Goyal 
1126c3e85bdcSAkhil Goyal 	/* output */
1127c3e85bdcSAkhil Goyal 	sg++;
1128fcf67029SHemant Agrawal 	qm_sg_entry_set64(&cf->sg[0], dpaa_mem_vtop_ctx(ctx, sg));
1129c3e85bdcSAkhil Goyal 	qm_sg_entry_set64(sg,
1130a389434eSAlok Makhariya 		dst_start_addr + sym->aead.data.offset - ses->auth_only_len);
1131c3e85bdcSAkhil Goyal 	sg->length = sym->aead.data.length + ses->auth_only_len;
1132c3e85bdcSAkhil Goyal 	length = sg->length;
1133c3e85bdcSAkhil Goyal 	if (is_encode(ses)) {
1134c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1135c3e85bdcSAkhil Goyal 		/* set auth output */
1136c3e85bdcSAkhil Goyal 		sg++;
1137c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, sym->aead.digest.phys_addr);
1138c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
1139c3e85bdcSAkhil Goyal 		length += sg->length;
1140c3e85bdcSAkhil Goyal 	}
1141c3e85bdcSAkhil Goyal 	sg->final = 1;
1142c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1143c3e85bdcSAkhil Goyal 
1144c3e85bdcSAkhil Goyal 	/* output compound frame */
1145c3e85bdcSAkhil Goyal 	cf->sg[0].length = length;
1146c3e85bdcSAkhil Goyal 	cf->sg[0].extension = 1;
1147c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(&cf->sg[0]);
1148c3e85bdcSAkhil Goyal 
1149c3e85bdcSAkhil Goyal 	return cf;
1150c3e85bdcSAkhil Goyal }
1151c3e85bdcSAkhil Goyal 
1152c3e85bdcSAkhil Goyal static inline struct dpaa_sec_job *
1153*a74af788SAkhil Goyal build_cipher_auth_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
1154*a74af788SAkhil Goyal {
1155*a74af788SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1156*a74af788SAkhil Goyal 	struct dpaa_sec_job *cf;
1157*a74af788SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1158*a74af788SAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
1159*a74af788SAkhil Goyal 	struct rte_mbuf *mbuf;
1160*a74af788SAkhil Goyal 	uint8_t req_segs;
1161*a74af788SAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1162*a74af788SAkhil Goyal 			ses->iv.offset);
1163*a74af788SAkhil Goyal 
1164*a74af788SAkhil Goyal 	if (sym->m_dst) {
1165*a74af788SAkhil Goyal 		mbuf = sym->m_dst;
1166*a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 4;
1167*a74af788SAkhil Goyal 	} else {
1168*a74af788SAkhil Goyal 		mbuf = sym->m_src;
1169*a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs * 2 + 4;
1170*a74af788SAkhil Goyal 	}
1171*a74af788SAkhil Goyal 
1172*a74af788SAkhil Goyal 	if (req_segs > MAX_SG_ENTRIES) {
1173*a74af788SAkhil Goyal 		PMD_TX_LOG(ERR, "Cipher-Auth: Max sec segs supported is %d\n",
1174*a74af788SAkhil Goyal 				MAX_SG_ENTRIES);
1175*a74af788SAkhil Goyal 		return NULL;
1176*a74af788SAkhil Goyal 	}
1177*a74af788SAkhil Goyal 
1178*a74af788SAkhil Goyal 	ctx = dpaa_sec_alloc_ctx(ses);
1179*a74af788SAkhil Goyal 	if (!ctx)
1180*a74af788SAkhil Goyal 		return NULL;
1181*a74af788SAkhil Goyal 
1182*a74af788SAkhil Goyal 	cf = &ctx->job;
1183*a74af788SAkhil Goyal 	ctx->op = op;
1184*a74af788SAkhil Goyal 
1185*a74af788SAkhil Goyal 	rte_prefetch0(cf->sg);
1186*a74af788SAkhil Goyal 
1187*a74af788SAkhil Goyal 	/* output */
1188*a74af788SAkhil Goyal 	out_sg = &cf->sg[0];
1189*a74af788SAkhil Goyal 	out_sg->extension = 1;
1190*a74af788SAkhil Goyal 	if (is_encode(ses))
1191*a74af788SAkhil Goyal 		out_sg->length = sym->auth.data.length + ses->digest_length;
1192*a74af788SAkhil Goyal 	else
1193*a74af788SAkhil Goyal 		out_sg->length = sym->auth.data.length;
1194*a74af788SAkhil Goyal 
1195*a74af788SAkhil Goyal 	/* output sg entries */
1196*a74af788SAkhil Goyal 	sg = &cf->sg[2];
1197*a74af788SAkhil Goyal 	qm_sg_entry_set64(out_sg, dpaa_mem_vtop_ctx(ctx, sg));
1198*a74af788SAkhil Goyal 	cpu_to_hw_sg(out_sg);
1199*a74af788SAkhil Goyal 
1200*a74af788SAkhil Goyal 	/* 1st seg */
1201*a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1202*a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->auth.data.offset;
1203*a74af788SAkhil Goyal 	sg->offset = sym->auth.data.offset;
1204*a74af788SAkhil Goyal 
1205*a74af788SAkhil Goyal 	/* Successive segs */
1206*a74af788SAkhil Goyal 	mbuf = mbuf->next;
1207*a74af788SAkhil Goyal 	while (mbuf) {
1208*a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1209*a74af788SAkhil Goyal 		sg++;
1210*a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1211*a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1212*a74af788SAkhil Goyal 		mbuf = mbuf->next;
1213*a74af788SAkhil Goyal 	}
1214*a74af788SAkhil Goyal 	sg->length -= ses->digest_length;
1215*a74af788SAkhil Goyal 
1216*a74af788SAkhil Goyal 	if (is_encode(ses)) {
1217*a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1218*a74af788SAkhil Goyal 		/* set auth output */
1219*a74af788SAkhil Goyal 		sg++;
1220*a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
1221*a74af788SAkhil Goyal 		sg->length = ses->digest_length;
1222*a74af788SAkhil Goyal 	}
1223*a74af788SAkhil Goyal 	sg->final = 1;
1224*a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1225*a74af788SAkhil Goyal 
1226*a74af788SAkhil Goyal 	/* input */
1227*a74af788SAkhil Goyal 	mbuf = sym->m_src;
1228*a74af788SAkhil Goyal 	in_sg = &cf->sg[1];
1229*a74af788SAkhil Goyal 	in_sg->extension = 1;
1230*a74af788SAkhil Goyal 	in_sg->final = 1;
1231*a74af788SAkhil Goyal 	if (is_encode(ses))
1232*a74af788SAkhil Goyal 		in_sg->length = ses->iv.length + sym->auth.data.length;
1233*a74af788SAkhil Goyal 	else
1234*a74af788SAkhil Goyal 		in_sg->length = ses->iv.length + sym->auth.data.length
1235*a74af788SAkhil Goyal 						+ ses->digest_length;
1236*a74af788SAkhil Goyal 
1237*a74af788SAkhil Goyal 	/* input sg entries */
1238*a74af788SAkhil Goyal 	sg++;
1239*a74af788SAkhil Goyal 	qm_sg_entry_set64(in_sg, dpaa_mem_vtop_ctx(ctx, sg));
1240*a74af788SAkhil Goyal 	cpu_to_hw_sg(in_sg);
1241*a74af788SAkhil Goyal 
1242*a74af788SAkhil Goyal 	/* 1st seg IV */
1243*a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1244*a74af788SAkhil Goyal 	sg->length = ses->iv.length;
1245*a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1246*a74af788SAkhil Goyal 
1247*a74af788SAkhil Goyal 	/* 2nd seg */
1248*a74af788SAkhil Goyal 	sg++;
1249*a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1250*a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->auth.data.offset;
1251*a74af788SAkhil Goyal 	sg->offset = sym->auth.data.offset;
1252*a74af788SAkhil Goyal 
1253*a74af788SAkhil Goyal 	/* Successive segs */
1254*a74af788SAkhil Goyal 	mbuf = mbuf->next;
1255*a74af788SAkhil Goyal 	while (mbuf) {
1256*a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1257*a74af788SAkhil Goyal 		sg++;
1258*a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1259*a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1260*a74af788SAkhil Goyal 		mbuf = mbuf->next;
1261*a74af788SAkhil Goyal 	}
1262*a74af788SAkhil Goyal 
1263*a74af788SAkhil Goyal 	sg->length -= ses->digest_length;
1264*a74af788SAkhil Goyal 	if (is_decode(ses)) {
1265*a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1266*a74af788SAkhil Goyal 		sg++;
1267*a74af788SAkhil Goyal 		memcpy(ctx->digest, sym->auth.digest.data,
1268*a74af788SAkhil Goyal 			ses->digest_length);
1269*a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, dpaa_mem_vtop_ctx(ctx, ctx->digest));
1270*a74af788SAkhil Goyal 		sg->length = ses->digest_length;
1271*a74af788SAkhil Goyal 	}
1272*a74af788SAkhil Goyal 	sg->final = 1;
1273*a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1274*a74af788SAkhil Goyal 
1275*a74af788SAkhil Goyal 	return cf;
1276*a74af788SAkhil Goyal }
1277*a74af788SAkhil Goyal 
1278*a74af788SAkhil Goyal static inline struct dpaa_sec_job *
1279c3e85bdcSAkhil Goyal build_cipher_auth(struct rte_crypto_op *op, dpaa_sec_session *ses)
1280c3e85bdcSAkhil Goyal {
1281c3e85bdcSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1282c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
1283c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1284c3e85bdcSAkhil Goyal 	struct qm_sg_entry *sg;
1285c4509373SSantosh Shukla 	rte_iova_t src_start_addr, dst_start_addr;
1286c3e85bdcSAkhil Goyal 	uint32_t length = 0;
1287c3e85bdcSAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1288c3e85bdcSAkhil Goyal 			ses->iv.offset);
1289c3e85bdcSAkhil Goyal 
1290455da545SSantosh Shukla 	src_start_addr = sym->m_src->buf_iova + sym->m_src->data_off;
1291a389434eSAlok Makhariya 	if (sym->m_dst)
1292455da545SSantosh Shukla 		dst_start_addr = sym->m_dst->buf_iova + sym->m_dst->data_off;
1293a389434eSAlok Makhariya 	else
1294a389434eSAlok Makhariya 		dst_start_addr = src_start_addr;
1295c3e85bdcSAkhil Goyal 
1296c3e85bdcSAkhil Goyal 	ctx = dpaa_sec_alloc_ctx(ses);
1297c3e85bdcSAkhil Goyal 	if (!ctx)
1298c3e85bdcSAkhil Goyal 		return NULL;
1299c3e85bdcSAkhil Goyal 
1300c3e85bdcSAkhil Goyal 	cf = &ctx->job;
1301c3e85bdcSAkhil Goyal 	ctx->op = op;
1302c3e85bdcSAkhil Goyal 
1303c3e85bdcSAkhil Goyal 	/* input */
1304c3e85bdcSAkhil Goyal 	rte_prefetch0(cf->sg);
1305c3e85bdcSAkhil Goyal 	sg = &cf->sg[2];
1306fcf67029SHemant Agrawal 	qm_sg_entry_set64(&cf->sg[1], dpaa_mem_vtop_ctx(ctx, sg));
1307c3e85bdcSAkhil Goyal 	if (is_encode(ses)) {
1308c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1309c3e85bdcSAkhil Goyal 		sg->length = ses->iv.length;
1310c3e85bdcSAkhil Goyal 		length += sg->length;
1311c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1312c3e85bdcSAkhil Goyal 
1313c3e85bdcSAkhil Goyal 		sg++;
1314a389434eSAlok Makhariya 		qm_sg_entry_set64(sg, src_start_addr + sym->auth.data.offset);
1315c3e85bdcSAkhil Goyal 		sg->length = sym->auth.data.length;
1316c3e85bdcSAkhil Goyal 		length += sg->length;
1317c3e85bdcSAkhil Goyal 		sg->final = 1;
1318c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1319c3e85bdcSAkhil Goyal 	} else {
1320c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1321c3e85bdcSAkhil Goyal 		sg->length = ses->iv.length;
1322c3e85bdcSAkhil Goyal 		length += sg->length;
1323c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1324c3e85bdcSAkhil Goyal 
1325c3e85bdcSAkhil Goyal 		sg++;
1326c3e85bdcSAkhil Goyal 
1327a389434eSAlok Makhariya 		qm_sg_entry_set64(sg, src_start_addr + sym->auth.data.offset);
1328c3e85bdcSAkhil Goyal 		sg->length = sym->auth.data.length;
1329c3e85bdcSAkhil Goyal 		length += sg->length;
1330c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1331c3e85bdcSAkhil Goyal 
1332c3e85bdcSAkhil Goyal 		memcpy(ctx->digest, sym->auth.digest.data,
1333c3e85bdcSAkhil Goyal 		       ses->digest_length);
1334c3e85bdcSAkhil Goyal 		sg++;
1335c3e85bdcSAkhil Goyal 
1336fcf67029SHemant Agrawal 		qm_sg_entry_set64(sg, dpaa_mem_vtop_ctx(ctx, ctx->digest));
1337c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
1338c3e85bdcSAkhil Goyal 		length += sg->length;
1339c3e85bdcSAkhil Goyal 		sg->final = 1;
1340c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1341c3e85bdcSAkhil Goyal 	}
1342c3e85bdcSAkhil Goyal 	/* input compound frame */
1343c3e85bdcSAkhil Goyal 	cf->sg[1].length = length;
1344c3e85bdcSAkhil Goyal 	cf->sg[1].extension = 1;
1345c3e85bdcSAkhil Goyal 	cf->sg[1].final = 1;
1346c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(&cf->sg[1]);
1347c3e85bdcSAkhil Goyal 
1348c3e85bdcSAkhil Goyal 	/* output */
1349c3e85bdcSAkhil Goyal 	sg++;
1350fcf67029SHemant Agrawal 	qm_sg_entry_set64(&cf->sg[0], dpaa_mem_vtop_ctx(ctx, sg));
1351a389434eSAlok Makhariya 	qm_sg_entry_set64(sg, dst_start_addr + sym->cipher.data.offset);
1352c3e85bdcSAkhil Goyal 	sg->length = sym->cipher.data.length;
1353c3e85bdcSAkhil Goyal 	length = sg->length;
1354c3e85bdcSAkhil Goyal 	if (is_encode(ses)) {
1355c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1356c3e85bdcSAkhil Goyal 		/* set auth output */
1357c3e85bdcSAkhil Goyal 		sg++;
1358c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
1359c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
1360c3e85bdcSAkhil Goyal 		length += sg->length;
1361c3e85bdcSAkhil Goyal 	}
1362c3e85bdcSAkhil Goyal 	sg->final = 1;
1363c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1364c3e85bdcSAkhil Goyal 
1365c3e85bdcSAkhil Goyal 	/* output compound frame */
1366c3e85bdcSAkhil Goyal 	cf->sg[0].length = length;
1367c3e85bdcSAkhil Goyal 	cf->sg[0].extension = 1;
1368c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(&cf->sg[0]);
1369c3e85bdcSAkhil Goyal 
1370c3e85bdcSAkhil Goyal 	return cf;
1371c3e85bdcSAkhil Goyal }
1372c3e85bdcSAkhil Goyal 
13731f14d500SAkhil Goyal static inline struct dpaa_sec_job *
13741f14d500SAkhil Goyal build_proto(struct rte_crypto_op *op, dpaa_sec_session *ses)
13751f14d500SAkhil Goyal {
13761f14d500SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
13771f14d500SAkhil Goyal 	struct dpaa_sec_job *cf;
13781f14d500SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
13791f14d500SAkhil Goyal 	struct qm_sg_entry *sg;
13801f14d500SAkhil Goyal 	phys_addr_t src_start_addr, dst_start_addr;
13811f14d500SAkhil Goyal 
13821f14d500SAkhil Goyal 	ctx = dpaa_sec_alloc_ctx(ses);
13831f14d500SAkhil Goyal 	if (!ctx)
13841f14d500SAkhil Goyal 		return NULL;
13851f14d500SAkhil Goyal 	cf = &ctx->job;
13861f14d500SAkhil Goyal 	ctx->op = op;
13871f14d500SAkhil Goyal 
13881f14d500SAkhil Goyal 	src_start_addr = rte_pktmbuf_mtophys(sym->m_src);
13891f14d500SAkhil Goyal 
13901f14d500SAkhil Goyal 	if (sym->m_dst)
13911f14d500SAkhil Goyal 		dst_start_addr = rte_pktmbuf_mtophys(sym->m_dst);
13921f14d500SAkhil Goyal 	else
13931f14d500SAkhil Goyal 		dst_start_addr = src_start_addr;
13941f14d500SAkhil Goyal 
13951f14d500SAkhil Goyal 	/* input */
13961f14d500SAkhil Goyal 	sg = &cf->sg[1];
13971f14d500SAkhil Goyal 	qm_sg_entry_set64(sg, src_start_addr);
13981f14d500SAkhil Goyal 	sg->length = sym->m_src->pkt_len;
13991f14d500SAkhil Goyal 	sg->final = 1;
14001f14d500SAkhil Goyal 	cpu_to_hw_sg(sg);
14011f14d500SAkhil Goyal 
14021f14d500SAkhil Goyal 	sym->m_src->packet_type &= ~RTE_PTYPE_L4_MASK;
14031f14d500SAkhil Goyal 	/* output */
14041f14d500SAkhil Goyal 	sg = &cf->sg[0];
14051f14d500SAkhil Goyal 	qm_sg_entry_set64(sg, dst_start_addr);
14061f14d500SAkhil Goyal 	sg->length = sym->m_src->buf_len - sym->m_src->data_off;
14071f14d500SAkhil Goyal 	cpu_to_hw_sg(sg);
14081f14d500SAkhil Goyal 
14091f14d500SAkhil Goyal 	return cf;
14101f14d500SAkhil Goyal }
14111f14d500SAkhil Goyal 
14129a984458SAkhil Goyal static uint16_t
14139a984458SAkhil Goyal dpaa_sec_enqueue_burst(void *qp, struct rte_crypto_op **ops,
14149a984458SAkhil Goyal 		       uint16_t nb_ops)
1415c3e85bdcSAkhil Goyal {
14169a984458SAkhil Goyal 	/* Function to transmit the frames to given device and queuepair */
14179a984458SAkhil Goyal 	uint32_t loop;
14189a984458SAkhil Goyal 	struct dpaa_sec_qp *dpaa_qp = (struct dpaa_sec_qp *)qp;
14199a984458SAkhil Goyal 	uint16_t num_tx = 0;
14209a984458SAkhil Goyal 	struct qm_fd fds[DPAA_SEC_BURST], *fd;
14219a984458SAkhil Goyal 	uint32_t frames_to_send;
14229a984458SAkhil Goyal 	struct rte_crypto_op *op;
1423c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
1424c3e85bdcSAkhil Goyal 	dpaa_sec_session *ses;
14259a984458SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
14269a984458SAkhil Goyal 	uint32_t auth_only_len;
14279a984458SAkhil Goyal 	struct qman_fq *inq[DPAA_SEC_BURST];
1428c3e85bdcSAkhil Goyal 
14299a984458SAkhil Goyal 	while (nb_ops) {
14309a984458SAkhil Goyal 		frames_to_send = (nb_ops > DPAA_SEC_BURST) ?
14319a984458SAkhil Goyal 				DPAA_SEC_BURST : nb_ops;
14329a984458SAkhil Goyal 		for (loop = 0; loop < frames_to_send; loop++) {
14339a984458SAkhil Goyal 			op = *(ops++);
14349a984458SAkhil Goyal 			switch (op->sess_type) {
14359a984458SAkhil Goyal 			case RTE_CRYPTO_OP_WITH_SESSION:
14369a984458SAkhil Goyal 				ses = (dpaa_sec_session *)
14379a984458SAkhil Goyal 					get_session_private_data(
14389a984458SAkhil Goyal 							op->sym->session,
14399a984458SAkhil Goyal 							cryptodev_driver_id);
14409a984458SAkhil Goyal 				break;
14419a984458SAkhil Goyal 			case RTE_CRYPTO_OP_SECURITY_SESSION:
14429a984458SAkhil Goyal 				ses = (dpaa_sec_session *)
14439a984458SAkhil Goyal 					get_sec_session_private_data(
14441f14d500SAkhil Goyal 							op->sym->sec_session);
14459a984458SAkhil Goyal 				break;
14469a984458SAkhil Goyal 			default:
14479a984458SAkhil Goyal 				PMD_TX_LOG(ERR,
14489a984458SAkhil Goyal 					"sessionless crypto op not supported");
14499a984458SAkhil Goyal 				frames_to_send = loop;
14509a984458SAkhil Goyal 				nb_ops = loop;
14519a984458SAkhil Goyal 				goto send_pkts;
14529a984458SAkhil Goyal 			}
1453e79416d1SHemant Agrawal 			if (unlikely(!ses->qp || ses->qp != qp)) {
14549a984458SAkhil Goyal 				PMD_INIT_LOG(DEBUG, "sess->qp - %p qp %p",
14559a984458SAkhil Goyal 						ses->qp, qp);
14569a984458SAkhil Goyal 				if (dpaa_sec_attach_sess_q(qp, ses)) {
14579a984458SAkhil Goyal 					frames_to_send = loop;
14589a984458SAkhil Goyal 					nb_ops = loop;
14599a984458SAkhil Goyal 					goto send_pkts;
14609a984458SAkhil Goyal 				}
1461c3e85bdcSAkhil Goyal 			}
1462c3e85bdcSAkhil Goyal 
14639a984458SAkhil Goyal 			auth_only_len = op->sym->auth.data.length -
14649a984458SAkhil Goyal 						op->sym->cipher.data.length;
1465*a74af788SAkhil Goyal 			if (rte_pktmbuf_is_contiguous(op->sym->m_src)) {
1466c3e85bdcSAkhil Goyal 				if (is_auth_only(ses)) {
1467c3e85bdcSAkhil Goyal 					cf = build_auth_only(op, ses);
1468c3e85bdcSAkhil Goyal 				} else if (is_cipher_only(ses)) {
1469c3e85bdcSAkhil Goyal 					cf = build_cipher_only(op, ses);
1470c3e85bdcSAkhil Goyal 				} else if (is_aead(ses)) {
1471c3e85bdcSAkhil Goyal 					cf = build_cipher_auth_gcm(op, ses);
1472c3e85bdcSAkhil Goyal 					auth_only_len = ses->auth_only_len;
1473c3e85bdcSAkhil Goyal 				} else if (is_auth_cipher(ses)) {
1474c3e85bdcSAkhil Goyal 					cf = build_cipher_auth(op, ses);
14751f14d500SAkhil Goyal 				} else if (is_proto_ipsec(ses)) {
14761f14d500SAkhil Goyal 					cf = build_proto(op, ses);
1477c3e85bdcSAkhil Goyal 				} else {
1478c3e85bdcSAkhil Goyal 					PMD_TX_LOG(ERR, "not supported sec op");
14799a984458SAkhil Goyal 					frames_to_send = loop;
14809a984458SAkhil Goyal 					nb_ops = loop;
14819a984458SAkhil Goyal 					goto send_pkts;
1482c3e85bdcSAkhil Goyal 				}
1483*a74af788SAkhil Goyal 			} else {
1484*a74af788SAkhil Goyal 				if (is_auth_only(ses)) {
1485*a74af788SAkhil Goyal 					cf = build_auth_only_sg(op, ses);
1486*a74af788SAkhil Goyal 				} else if (is_cipher_only(ses)) {
1487*a74af788SAkhil Goyal 					cf = build_cipher_only_sg(op, ses);
1488*a74af788SAkhil Goyal 				} else if (is_aead(ses)) {
1489*a74af788SAkhil Goyal 					cf = build_cipher_auth_gcm_sg(op, ses);
1490*a74af788SAkhil Goyal 					auth_only_len = ses->auth_only_len;
1491*a74af788SAkhil Goyal 				} else if (is_auth_cipher(ses)) {
1492*a74af788SAkhil Goyal 					cf = build_cipher_auth_sg(op, ses);
1493*a74af788SAkhil Goyal 				} else {
1494*a74af788SAkhil Goyal 					PMD_TX_LOG(ERR, "not supported sec op");
1495*a74af788SAkhil Goyal 					frames_to_send = loop;
1496*a74af788SAkhil Goyal 					nb_ops = loop;
1497*a74af788SAkhil Goyal 					goto send_pkts;
1498*a74af788SAkhil Goyal 				}
1499*a74af788SAkhil Goyal 			}
15009a984458SAkhil Goyal 			if (unlikely(!cf)) {
15019a984458SAkhil Goyal 				frames_to_send = loop;
15029a984458SAkhil Goyal 				nb_ops = loop;
15039a984458SAkhil Goyal 				goto send_pkts;
15049a984458SAkhil Goyal 			}
1505c3e85bdcSAkhil Goyal 
15069a984458SAkhil Goyal 			fd = &fds[loop];
15079a984458SAkhil Goyal 			inq[loop] = ses->inq;
15089a984458SAkhil Goyal 			fd->opaque_addr = 0;
15099a984458SAkhil Goyal 			fd->cmd = 0;
15109a984458SAkhil Goyal 			ctx = container_of(cf, struct dpaa_sec_op_ctx, job);
15119a984458SAkhil Goyal 			qm_fd_addr_set64(fd, dpaa_mem_vtop_ctx(ctx, cf->sg));
15129a984458SAkhil Goyal 			fd->_format1 = qm_fd_compound;
15139a984458SAkhil Goyal 			fd->length29 = 2 * sizeof(struct qm_sg_entry);
15149a984458SAkhil Goyal 			/* Auth_only_len is set as 0 in descriptor and it is
15159a984458SAkhil Goyal 			 * overwritten here in the fd.cmd which will update
15169a984458SAkhil Goyal 			 * the DPOVRD reg.
1517c3e85bdcSAkhil Goyal 			 */
1518c3e85bdcSAkhil Goyal 			if (auth_only_len)
15199a984458SAkhil Goyal 				fd->cmd = 0x80000000 | auth_only_len;
1520c3e85bdcSAkhil Goyal 
15219a984458SAkhil Goyal 		}
15229a984458SAkhil Goyal send_pkts:
15239a984458SAkhil Goyal 		loop = 0;
15249a984458SAkhil Goyal 		while (loop < frames_to_send) {
15259a984458SAkhil Goyal 			loop += qman_enqueue_multi_fq(&inq[loop], &fds[loop],
15269a984458SAkhil Goyal 					frames_to_send - loop);
15279a984458SAkhil Goyal 		}
15289a984458SAkhil Goyal 		nb_ops -= frames_to_send;
15299a984458SAkhil Goyal 		num_tx += frames_to_send;
1530c3e85bdcSAkhil Goyal 	}
1531c3e85bdcSAkhil Goyal 
1532c3e85bdcSAkhil Goyal 	dpaa_qp->tx_pkts += num_tx;
1533c3e85bdcSAkhil Goyal 	dpaa_qp->tx_errs += nb_ops - num_tx;
1534c3e85bdcSAkhil Goyal 
1535c3e85bdcSAkhil Goyal 	return num_tx;
1536c3e85bdcSAkhil Goyal }
1537c3e85bdcSAkhil Goyal 
1538c3e85bdcSAkhil Goyal static uint16_t
1539c3e85bdcSAkhil Goyal dpaa_sec_dequeue_burst(void *qp, struct rte_crypto_op **ops,
1540c3e85bdcSAkhil Goyal 		       uint16_t nb_ops)
1541c3e85bdcSAkhil Goyal {
1542c3e85bdcSAkhil Goyal 	uint16_t num_rx;
1543c3e85bdcSAkhil Goyal 	struct dpaa_sec_qp *dpaa_qp = (struct dpaa_sec_qp *)qp;
1544c3e85bdcSAkhil Goyal 
1545c3e85bdcSAkhil Goyal 	num_rx = dpaa_sec_deq(dpaa_qp, ops, nb_ops);
1546c3e85bdcSAkhil Goyal 
1547c3e85bdcSAkhil Goyal 	dpaa_qp->rx_pkts += num_rx;
1548c3e85bdcSAkhil Goyal 	dpaa_qp->rx_errs += nb_ops - num_rx;
1549c3e85bdcSAkhil Goyal 
1550c3e85bdcSAkhil Goyal 	PMD_RX_LOG(DEBUG, "SEC Received %d Packets\n", num_rx);
1551c3e85bdcSAkhil Goyal 
1552c3e85bdcSAkhil Goyal 	return num_rx;
1553c3e85bdcSAkhil Goyal }
1554c3e85bdcSAkhil Goyal 
1555c3e85bdcSAkhil Goyal /** Release queue pair */
1556c3e85bdcSAkhil Goyal static int
1557c3e85bdcSAkhil Goyal dpaa_sec_queue_pair_release(struct rte_cryptodev *dev,
1558c3e85bdcSAkhil Goyal 			    uint16_t qp_id)
1559c3e85bdcSAkhil Goyal {
1560c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals;
1561c3e85bdcSAkhil Goyal 	struct dpaa_sec_qp *qp = NULL;
1562c3e85bdcSAkhil Goyal 
1563c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
1564c3e85bdcSAkhil Goyal 
1565c3e85bdcSAkhil Goyal 	PMD_INIT_LOG(DEBUG, "dev =%p, queue =%d", dev, qp_id);
1566c3e85bdcSAkhil Goyal 
1567c3e85bdcSAkhil Goyal 	internals = dev->data->dev_private;
1568c3e85bdcSAkhil Goyal 	if (qp_id >= internals->max_nb_queue_pairs) {
1569c3e85bdcSAkhil Goyal 		PMD_INIT_LOG(ERR, "Max supported qpid %d",
1570c3e85bdcSAkhil Goyal 			     internals->max_nb_queue_pairs);
1571c3e85bdcSAkhil Goyal 		return -EINVAL;
1572c3e85bdcSAkhil Goyal 	}
1573c3e85bdcSAkhil Goyal 
1574c3e85bdcSAkhil Goyal 	qp = &internals->qps[qp_id];
1575c3e85bdcSAkhil Goyal 	qp->internals = NULL;
1576c3e85bdcSAkhil Goyal 	dev->data->queue_pairs[qp_id] = NULL;
1577c3e85bdcSAkhil Goyal 
1578c3e85bdcSAkhil Goyal 	return 0;
1579c3e85bdcSAkhil Goyal }
1580c3e85bdcSAkhil Goyal 
1581c3e85bdcSAkhil Goyal /** Setup a queue pair */
1582c3e85bdcSAkhil Goyal static int
1583c3e85bdcSAkhil Goyal dpaa_sec_queue_pair_setup(struct rte_cryptodev *dev, uint16_t qp_id,
1584c3e85bdcSAkhil Goyal 		__rte_unused const struct rte_cryptodev_qp_conf *qp_conf,
1585c3e85bdcSAkhil Goyal 		__rte_unused int socket_id,
1586c3e85bdcSAkhil Goyal 		__rte_unused struct rte_mempool *session_pool)
1587c3e85bdcSAkhil Goyal {
1588c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals;
1589c3e85bdcSAkhil Goyal 	struct dpaa_sec_qp *qp = NULL;
1590c3e85bdcSAkhil Goyal 
1591c3e85bdcSAkhil Goyal 	PMD_INIT_LOG(DEBUG, "dev =%p, queue =%d, conf =%p",
1592c3e85bdcSAkhil Goyal 		     dev, qp_id, qp_conf);
1593c3e85bdcSAkhil Goyal 
1594c3e85bdcSAkhil Goyal 	internals = dev->data->dev_private;
1595c3e85bdcSAkhil Goyal 	if (qp_id >= internals->max_nb_queue_pairs) {
1596c3e85bdcSAkhil Goyal 		PMD_INIT_LOG(ERR, "Max supported qpid %d",
1597c3e85bdcSAkhil Goyal 			     internals->max_nb_queue_pairs);
1598c3e85bdcSAkhil Goyal 		return -EINVAL;
1599c3e85bdcSAkhil Goyal 	}
1600c3e85bdcSAkhil Goyal 
1601c3e85bdcSAkhil Goyal 	qp = &internals->qps[qp_id];
1602c3e85bdcSAkhil Goyal 	qp->internals = internals;
1603c3e85bdcSAkhil Goyal 	dev->data->queue_pairs[qp_id] = qp;
1604c3e85bdcSAkhil Goyal 
1605c3e85bdcSAkhil Goyal 	return 0;
1606c3e85bdcSAkhil Goyal }
1607c3e85bdcSAkhil Goyal 
1608c3e85bdcSAkhil Goyal /** Start queue pair */
1609c3e85bdcSAkhil Goyal static int
1610c3e85bdcSAkhil Goyal dpaa_sec_queue_pair_start(__rte_unused struct rte_cryptodev *dev,
1611c3e85bdcSAkhil Goyal 			  __rte_unused uint16_t queue_pair_id)
1612c3e85bdcSAkhil Goyal {
1613c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
1614c3e85bdcSAkhil Goyal 
1615c3e85bdcSAkhil Goyal 	return 0;
1616c3e85bdcSAkhil Goyal }
1617c3e85bdcSAkhil Goyal 
1618c3e85bdcSAkhil Goyal /** Stop queue pair */
1619c3e85bdcSAkhil Goyal static int
1620c3e85bdcSAkhil Goyal dpaa_sec_queue_pair_stop(__rte_unused struct rte_cryptodev *dev,
1621c3e85bdcSAkhil Goyal 			 __rte_unused uint16_t queue_pair_id)
1622c3e85bdcSAkhil Goyal {
1623c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
1624c3e85bdcSAkhil Goyal 
1625c3e85bdcSAkhil Goyal 	return 0;
1626c3e85bdcSAkhil Goyal }
1627c3e85bdcSAkhil Goyal 
1628c3e85bdcSAkhil Goyal /** Return the number of allocated queue pairs */
1629c3e85bdcSAkhil Goyal static uint32_t
1630c3e85bdcSAkhil Goyal dpaa_sec_queue_pair_count(struct rte_cryptodev *dev)
1631c3e85bdcSAkhil Goyal {
1632c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
1633c3e85bdcSAkhil Goyal 
1634c3e85bdcSAkhil Goyal 	return dev->data->nb_queue_pairs;
1635c3e85bdcSAkhil Goyal }
1636c3e85bdcSAkhil Goyal 
1637c3e85bdcSAkhil Goyal /** Returns the size of session structure */
1638c3e85bdcSAkhil Goyal static unsigned int
1639c3e85bdcSAkhil Goyal dpaa_sec_session_get_size(struct rte_cryptodev *dev __rte_unused)
1640c3e85bdcSAkhil Goyal {
1641c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
1642c3e85bdcSAkhil Goyal 
1643c3e85bdcSAkhil Goyal 	return sizeof(dpaa_sec_session);
1644c3e85bdcSAkhil Goyal }
1645c3e85bdcSAkhil Goyal 
1646c3e85bdcSAkhil Goyal static int
1647c3e85bdcSAkhil Goyal dpaa_sec_cipher_init(struct rte_cryptodev *dev __rte_unused,
1648c3e85bdcSAkhil Goyal 		     struct rte_crypto_sym_xform *xform,
1649c3e85bdcSAkhil Goyal 		     dpaa_sec_session *session)
1650c3e85bdcSAkhil Goyal {
1651c3e85bdcSAkhil Goyal 	session->cipher_alg = xform->cipher.algo;
1652c3e85bdcSAkhil Goyal 	session->iv.length = xform->cipher.iv.length;
1653c3e85bdcSAkhil Goyal 	session->iv.offset = xform->cipher.iv.offset;
1654c3e85bdcSAkhil Goyal 	session->cipher_key.data = rte_zmalloc(NULL, xform->cipher.key.length,
1655c3e85bdcSAkhil Goyal 					       RTE_CACHE_LINE_SIZE);
1656c3e85bdcSAkhil Goyal 	if (session->cipher_key.data == NULL && xform->cipher.key.length > 0) {
1657c3e85bdcSAkhil Goyal 		PMD_INIT_LOG(ERR, "No Memory for cipher key\n");
1658c3e85bdcSAkhil Goyal 		return -ENOMEM;
1659c3e85bdcSAkhil Goyal 	}
1660c3e85bdcSAkhil Goyal 	session->cipher_key.length = xform->cipher.key.length;
1661c3e85bdcSAkhil Goyal 
1662c3e85bdcSAkhil Goyal 	memcpy(session->cipher_key.data, xform->cipher.key.data,
1663c3e85bdcSAkhil Goyal 	       xform->cipher.key.length);
1664c3e85bdcSAkhil Goyal 	session->dir = (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
1665c3e85bdcSAkhil Goyal 			DIR_ENC : DIR_DEC;
1666c3e85bdcSAkhil Goyal 
1667c3e85bdcSAkhil Goyal 	return 0;
1668c3e85bdcSAkhil Goyal }
1669c3e85bdcSAkhil Goyal 
1670c3e85bdcSAkhil Goyal static int
1671c3e85bdcSAkhil Goyal dpaa_sec_auth_init(struct rte_cryptodev *dev __rte_unused,
1672c3e85bdcSAkhil Goyal 		   struct rte_crypto_sym_xform *xform,
1673c3e85bdcSAkhil Goyal 		   dpaa_sec_session *session)
1674c3e85bdcSAkhil Goyal {
1675c3e85bdcSAkhil Goyal 	session->auth_alg = xform->auth.algo;
1676c3e85bdcSAkhil Goyal 	session->auth_key.data = rte_zmalloc(NULL, xform->auth.key.length,
1677c3e85bdcSAkhil Goyal 					     RTE_CACHE_LINE_SIZE);
1678c3e85bdcSAkhil Goyal 	if (session->auth_key.data == NULL && xform->auth.key.length > 0) {
1679c3e85bdcSAkhil Goyal 		PMD_INIT_LOG(ERR, "No Memory for auth key\n");
1680c3e85bdcSAkhil Goyal 		return -ENOMEM;
1681c3e85bdcSAkhil Goyal 	}
1682c3e85bdcSAkhil Goyal 	session->auth_key.length = xform->auth.key.length;
1683c3e85bdcSAkhil Goyal 	session->digest_length = xform->auth.digest_length;
1684c3e85bdcSAkhil Goyal 
1685c3e85bdcSAkhil Goyal 	memcpy(session->auth_key.data, xform->auth.key.data,
1686c3e85bdcSAkhil Goyal 	       xform->auth.key.length);
1687c3e85bdcSAkhil Goyal 	session->dir = (xform->auth.op == RTE_CRYPTO_AUTH_OP_GENERATE) ?
1688c3e85bdcSAkhil Goyal 			DIR_ENC : DIR_DEC;
1689c3e85bdcSAkhil Goyal 
1690c3e85bdcSAkhil Goyal 	return 0;
1691c3e85bdcSAkhil Goyal }
1692c3e85bdcSAkhil Goyal 
1693c3e85bdcSAkhil Goyal static int
1694c3e85bdcSAkhil Goyal dpaa_sec_aead_init(struct rte_cryptodev *dev __rte_unused,
1695c3e85bdcSAkhil Goyal 		   struct rte_crypto_sym_xform *xform,
1696c3e85bdcSAkhil Goyal 		   dpaa_sec_session *session)
1697c3e85bdcSAkhil Goyal {
1698c3e85bdcSAkhil Goyal 	session->aead_alg = xform->aead.algo;
1699c3e85bdcSAkhil Goyal 	session->iv.length = xform->aead.iv.length;
1700c3e85bdcSAkhil Goyal 	session->iv.offset = xform->aead.iv.offset;
1701c3e85bdcSAkhil Goyal 	session->auth_only_len = xform->aead.aad_length;
1702c3e85bdcSAkhil Goyal 	session->aead_key.data = rte_zmalloc(NULL, xform->aead.key.length,
1703c3e85bdcSAkhil Goyal 					     RTE_CACHE_LINE_SIZE);
1704c3e85bdcSAkhil Goyal 	if (session->aead_key.data == NULL && xform->aead.key.length > 0) {
1705c3e85bdcSAkhil Goyal 		PMD_INIT_LOG(ERR, "No Memory for aead key\n");
1706c3e85bdcSAkhil Goyal 		return -ENOMEM;
1707c3e85bdcSAkhil Goyal 	}
1708c3e85bdcSAkhil Goyal 	session->aead_key.length = xform->aead.key.length;
1709c3e85bdcSAkhil Goyal 	session->digest_length = xform->aead.digest_length;
1710c3e85bdcSAkhil Goyal 
1711c3e85bdcSAkhil Goyal 	memcpy(session->aead_key.data, xform->aead.key.data,
1712c3e85bdcSAkhil Goyal 	       xform->aead.key.length);
1713c3e85bdcSAkhil Goyal 	session->dir = (xform->aead.op == RTE_CRYPTO_AEAD_OP_ENCRYPT) ?
1714c3e85bdcSAkhil Goyal 			DIR_ENC : DIR_DEC;
1715c3e85bdcSAkhil Goyal 
1716c3e85bdcSAkhil Goyal 	return 0;
1717c3e85bdcSAkhil Goyal }
1718c3e85bdcSAkhil Goyal 
1719e79416d1SHemant Agrawal static struct qman_fq *
1720e79416d1SHemant Agrawal dpaa_sec_attach_rxq(struct dpaa_sec_dev_private *qi)
1721c3e85bdcSAkhil Goyal {
1722e79416d1SHemant Agrawal 	unsigned int i;
1723c3e85bdcSAkhil Goyal 
1724e79416d1SHemant Agrawal 	for (i = 0; i < qi->max_nb_sessions; i++) {
1725e79416d1SHemant Agrawal 		if (qi->inq_attach[i] == 0) {
1726e79416d1SHemant Agrawal 			qi->inq_attach[i] = 1;
1727e79416d1SHemant Agrawal 			return &qi->inq[i];
1728e79416d1SHemant Agrawal 		}
1729e79416d1SHemant Agrawal 	}
1730e79416d1SHemant Agrawal 	PMD_DRV_LOG(ERR, "All ses session in use %x", qi->max_nb_sessions);
1731c3e85bdcSAkhil Goyal 
1732e79416d1SHemant Agrawal 	return NULL;
1733c3e85bdcSAkhil Goyal }
1734c3e85bdcSAkhil Goyal 
1735e79416d1SHemant Agrawal static int
1736e79416d1SHemant Agrawal dpaa_sec_detach_rxq(struct dpaa_sec_dev_private *qi, struct qman_fq *fq)
1737e79416d1SHemant Agrawal {
1738e79416d1SHemant Agrawal 	unsigned int i;
1739e79416d1SHemant Agrawal 
1740e79416d1SHemant Agrawal 	for (i = 0; i < qi->max_nb_sessions; i++) {
1741e79416d1SHemant Agrawal 		if (&qi->inq[i] == fq) {
1742b4053c4bSAlok Makhariya 			qman_retire_fq(fq, NULL);
1743b4053c4bSAlok Makhariya 			qman_oos_fq(fq);
1744e79416d1SHemant Agrawal 			qi->inq_attach[i] = 0;
1745e79416d1SHemant Agrawal 			return 0;
1746e79416d1SHemant Agrawal 		}
1747e79416d1SHemant Agrawal 	}
1748e79416d1SHemant Agrawal 	return -1;
1749e79416d1SHemant Agrawal }
1750e79416d1SHemant Agrawal 
1751e79416d1SHemant Agrawal static int
1752e79416d1SHemant Agrawal dpaa_sec_attach_sess_q(struct dpaa_sec_qp *qp, dpaa_sec_session *sess)
1753e79416d1SHemant Agrawal {
1754e79416d1SHemant Agrawal 	int ret;
1755e79416d1SHemant Agrawal 
1756c3e85bdcSAkhil Goyal 	sess->qp = qp;
1757e79416d1SHemant Agrawal 	ret = dpaa_sec_prep_cdb(sess);
1758e79416d1SHemant Agrawal 	if (ret) {
1759e79416d1SHemant Agrawal 		PMD_DRV_LOG(ERR, "Unable to prepare sec cdb");
1760e79416d1SHemant Agrawal 		return -1;
1761e79416d1SHemant Agrawal 	}
1762c3e85bdcSAkhil Goyal 
1763e79416d1SHemant Agrawal 	ret = dpaa_sec_init_rx(sess->inq, dpaa_mem_vtop(&sess->cdb),
1764e79416d1SHemant Agrawal 			       qman_fq_fqid(&qp->outq));
1765e79416d1SHemant Agrawal 	if (ret)
1766e79416d1SHemant Agrawal 		PMD_DRV_LOG(ERR, "Unable to init sec queue");
1767e79416d1SHemant Agrawal 
1768e79416d1SHemant Agrawal 	return ret;
1769c3e85bdcSAkhil Goyal }
1770c3e85bdcSAkhil Goyal 
1771c3e85bdcSAkhil Goyal static int
1772e79416d1SHemant Agrawal dpaa_sec_qp_attach_sess(struct rte_cryptodev *dev __rte_unused,
1773e79416d1SHemant Agrawal 			uint16_t qp_id __rte_unused,
1774e79416d1SHemant Agrawal 			void *ses __rte_unused)
1775c3e85bdcSAkhil Goyal {
1776c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
1777c3e85bdcSAkhil Goyal 	return 0;
1778c3e85bdcSAkhil Goyal }
1779c3e85bdcSAkhil Goyal 
1780e79416d1SHemant Agrawal static int
1781e79416d1SHemant Agrawal dpaa_sec_qp_detach_sess(struct rte_cryptodev *dev,
1782e79416d1SHemant Agrawal 			uint16_t qp_id  __rte_unused,
1783e79416d1SHemant Agrawal 			void *ses)
1784e79416d1SHemant Agrawal {
1785e79416d1SHemant Agrawal 	dpaa_sec_session *sess = ses;
1786e79416d1SHemant Agrawal 	struct dpaa_sec_dev_private *qi = dev->data->dev_private;
1787e79416d1SHemant Agrawal 
1788e79416d1SHemant Agrawal 	PMD_INIT_FUNC_TRACE();
1789e79416d1SHemant Agrawal 
1790e79416d1SHemant Agrawal 	if (sess->inq)
1791e79416d1SHemant Agrawal 		dpaa_sec_detach_rxq(qi, sess->inq);
1792e79416d1SHemant Agrawal 	sess->inq = NULL;
1793e79416d1SHemant Agrawal 
1794e79416d1SHemant Agrawal 	sess->qp = NULL;
1795e79416d1SHemant Agrawal 
1796e79416d1SHemant Agrawal 	return 0;
1797c3e85bdcSAkhil Goyal }
1798c3e85bdcSAkhil Goyal 
1799c3e85bdcSAkhil Goyal static int
1800c3e85bdcSAkhil Goyal dpaa_sec_set_session_parameters(struct rte_cryptodev *dev,
1801c3e85bdcSAkhil Goyal 			    struct rte_crypto_sym_xform *xform,	void *sess)
1802c3e85bdcSAkhil Goyal {
1803c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals = dev->data->dev_private;
1804c3e85bdcSAkhil Goyal 	dpaa_sec_session *session = sess;
1805c3e85bdcSAkhil Goyal 
1806c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
1807c3e85bdcSAkhil Goyal 
1808c3e85bdcSAkhil Goyal 	if (unlikely(sess == NULL)) {
1809c3e85bdcSAkhil Goyal 		RTE_LOG(ERR, PMD, "invalid session struct\n");
1810c3e85bdcSAkhil Goyal 		return -EINVAL;
1811c3e85bdcSAkhil Goyal 	}
1812c3e85bdcSAkhil Goyal 
1813c3e85bdcSAkhil Goyal 	/* Default IV length = 0 */
1814c3e85bdcSAkhil Goyal 	session->iv.length = 0;
1815c3e85bdcSAkhil Goyal 
1816c3e85bdcSAkhil Goyal 	/* Cipher Only */
1817c3e85bdcSAkhil Goyal 	if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER && xform->next == NULL) {
1818c3e85bdcSAkhil Goyal 		session->auth_alg = RTE_CRYPTO_AUTH_NULL;
1819c3e85bdcSAkhil Goyal 		dpaa_sec_cipher_init(dev, xform, session);
1820c3e85bdcSAkhil Goyal 
1821c3e85bdcSAkhil Goyal 	/* Authentication Only */
1822c3e85bdcSAkhil Goyal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
1823c3e85bdcSAkhil Goyal 		   xform->next == NULL) {
1824c3e85bdcSAkhil Goyal 		session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
1825c3e85bdcSAkhil Goyal 		dpaa_sec_auth_init(dev, xform, session);
1826c3e85bdcSAkhil Goyal 
1827c3e85bdcSAkhil Goyal 	/* Cipher then Authenticate */
1828c3e85bdcSAkhil Goyal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
1829c3e85bdcSAkhil Goyal 		   xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
1830c3e85bdcSAkhil Goyal 		if (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) {
1831c3e85bdcSAkhil Goyal 			dpaa_sec_cipher_init(dev, xform, session);
1832c3e85bdcSAkhil Goyal 			dpaa_sec_auth_init(dev, xform->next, session);
1833c3e85bdcSAkhil Goyal 		} else {
1834c3e85bdcSAkhil Goyal 			PMD_DRV_LOG(ERR, "Not supported: Auth then Cipher");
1835c3e85bdcSAkhil Goyal 			return -EINVAL;
1836c3e85bdcSAkhil Goyal 		}
1837c3e85bdcSAkhil Goyal 
1838c3e85bdcSAkhil Goyal 	/* Authenticate then Cipher */
1839c3e85bdcSAkhil Goyal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
1840c3e85bdcSAkhil Goyal 		   xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
1841c3e85bdcSAkhil Goyal 		if (xform->next->cipher.op == RTE_CRYPTO_CIPHER_OP_DECRYPT) {
1842c3e85bdcSAkhil Goyal 			dpaa_sec_auth_init(dev, xform, session);
1843c3e85bdcSAkhil Goyal 			dpaa_sec_cipher_init(dev, xform->next, session);
1844c3e85bdcSAkhil Goyal 		} else {
1845c3e85bdcSAkhil Goyal 			PMD_DRV_LOG(ERR, "Not supported: Auth then Cipher");
1846c3e85bdcSAkhil Goyal 			return -EINVAL;
1847c3e85bdcSAkhil Goyal 		}
1848c3e85bdcSAkhil Goyal 
1849c3e85bdcSAkhil Goyal 	/* AEAD operation for AES-GCM kind of Algorithms */
1850c3e85bdcSAkhil Goyal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD &&
1851c3e85bdcSAkhil Goyal 		   xform->next == NULL) {
1852c3e85bdcSAkhil Goyal 		dpaa_sec_aead_init(dev, xform, session);
1853c3e85bdcSAkhil Goyal 
1854c3e85bdcSAkhil Goyal 	} else {
1855c3e85bdcSAkhil Goyal 		PMD_DRV_LOG(ERR, "Invalid crypto type");
1856c3e85bdcSAkhil Goyal 		return -EINVAL;
1857c3e85bdcSAkhil Goyal 	}
1858c3e85bdcSAkhil Goyal 	session->ctx_pool = internals->ctx_pool;
1859e79416d1SHemant Agrawal 	session->inq = dpaa_sec_attach_rxq(internals);
1860e79416d1SHemant Agrawal 	if (session->inq == NULL) {
1861e79416d1SHemant Agrawal 		PMD_DRV_LOG(ERR, "unable to attach sec queue");
1862e79416d1SHemant Agrawal 		goto err1;
1863e79416d1SHemant Agrawal 	}
1864c3e85bdcSAkhil Goyal 
1865c3e85bdcSAkhil Goyal 	return 0;
1866e79416d1SHemant Agrawal 
1867e79416d1SHemant Agrawal err1:
1868e79416d1SHemant Agrawal 	rte_free(session->cipher_key.data);
1869e79416d1SHemant Agrawal 	rte_free(session->auth_key.data);
1870e79416d1SHemant Agrawal 	memset(session, 0, sizeof(dpaa_sec_session));
1871e79416d1SHemant Agrawal 
1872e79416d1SHemant Agrawal 	return -EINVAL;
1873c3e85bdcSAkhil Goyal }
1874c3e85bdcSAkhil Goyal 
1875c3e85bdcSAkhil Goyal static int
1876c3e85bdcSAkhil Goyal dpaa_sec_session_configure(struct rte_cryptodev *dev,
1877c3e85bdcSAkhil Goyal 		struct rte_crypto_sym_xform *xform,
1878c3e85bdcSAkhil Goyal 		struct rte_cryptodev_sym_session *sess,
1879c3e85bdcSAkhil Goyal 		struct rte_mempool *mempool)
1880c3e85bdcSAkhil Goyal {
1881c3e85bdcSAkhil Goyal 	void *sess_private_data;
1882c3e85bdcSAkhil Goyal 	int ret;
1883c3e85bdcSAkhil Goyal 
1884c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
1885c3e85bdcSAkhil Goyal 
1886c3e85bdcSAkhil Goyal 	if (rte_mempool_get(mempool, &sess_private_data)) {
1887c3e85bdcSAkhil Goyal 		CDEV_LOG_ERR(
1888c3e85bdcSAkhil Goyal 			"Couldn't get object from session mempool");
1889c3e85bdcSAkhil Goyal 		return -ENOMEM;
1890c3e85bdcSAkhil Goyal 	}
1891c3e85bdcSAkhil Goyal 
1892c3e85bdcSAkhil Goyal 	ret = dpaa_sec_set_session_parameters(dev, xform, sess_private_data);
1893c3e85bdcSAkhil Goyal 	if (ret != 0) {
1894c3e85bdcSAkhil Goyal 		PMD_DRV_LOG(ERR, "DPAA PMD: failed to configure "
1895c3e85bdcSAkhil Goyal 				"session parameters");
1896c3e85bdcSAkhil Goyal 
1897c3e85bdcSAkhil Goyal 		/* Return session to mempool */
1898c3e85bdcSAkhil Goyal 		rte_mempool_put(mempool, sess_private_data);
1899c3e85bdcSAkhil Goyal 		return ret;
1900c3e85bdcSAkhil Goyal 	}
1901c3e85bdcSAkhil Goyal 
1902c3e85bdcSAkhil Goyal 	set_session_private_data(sess, dev->driver_id,
1903c3e85bdcSAkhil Goyal 			sess_private_data);
1904c3e85bdcSAkhil Goyal 
1905e79416d1SHemant Agrawal 
1906c3e85bdcSAkhil Goyal 	return 0;
1907c3e85bdcSAkhil Goyal }
1908c3e85bdcSAkhil Goyal 
1909c3e85bdcSAkhil Goyal /** Clear the memory of session so it doesn't leave key material behind */
1910c3e85bdcSAkhil Goyal static void
1911c3e85bdcSAkhil Goyal dpaa_sec_session_clear(struct rte_cryptodev *dev,
1912c3e85bdcSAkhil Goyal 		struct rte_cryptodev_sym_session *sess)
1913c3e85bdcSAkhil Goyal {
1914e79416d1SHemant Agrawal 	struct dpaa_sec_dev_private *qi = dev->data->dev_private;
1915c3e85bdcSAkhil Goyal 	uint8_t index = dev->driver_id;
1916c3e85bdcSAkhil Goyal 	void *sess_priv = get_session_private_data(sess, index);
1917e79416d1SHemant Agrawal 
1918e79416d1SHemant Agrawal 	PMD_INIT_FUNC_TRACE();
1919e79416d1SHemant Agrawal 
1920c3e85bdcSAkhil Goyal 	dpaa_sec_session *s = (dpaa_sec_session *)sess_priv;
1921c3e85bdcSAkhil Goyal 
1922c3e85bdcSAkhil Goyal 	if (sess_priv) {
1923e79416d1SHemant Agrawal 		struct rte_mempool *sess_mp = rte_mempool_from_obj(sess_priv);
1924e79416d1SHemant Agrawal 
1925e79416d1SHemant Agrawal 		if (s->inq)
1926e79416d1SHemant Agrawal 			dpaa_sec_detach_rxq(qi, s->inq);
1927c3e85bdcSAkhil Goyal 		rte_free(s->cipher_key.data);
1928c3e85bdcSAkhil Goyal 		rte_free(s->auth_key.data);
1929c3e85bdcSAkhil Goyal 		memset(s, 0, sizeof(dpaa_sec_session));
1930c3e85bdcSAkhil Goyal 		set_session_private_data(sess, index, NULL);
1931c3e85bdcSAkhil Goyal 		rte_mempool_put(sess_mp, sess_priv);
1932c3e85bdcSAkhil Goyal 	}
1933c3e85bdcSAkhil Goyal }
1934c3e85bdcSAkhil Goyal 
1935c3e85bdcSAkhil Goyal static int
19361f14d500SAkhil Goyal dpaa_sec_set_ipsec_session(__rte_unused struct rte_cryptodev *dev,
19371f14d500SAkhil Goyal 			   struct rte_security_session_conf *conf,
19381f14d500SAkhil Goyal 			   void *sess)
19391f14d500SAkhil Goyal {
19401f14d500SAkhil Goyal 	struct dpaa_sec_dev_private *internals = dev->data->dev_private;
19411f14d500SAkhil Goyal 	struct rte_security_ipsec_xform *ipsec_xform = &conf->ipsec;
19421f14d500SAkhil Goyal 	struct rte_crypto_auth_xform *auth_xform;
19431f14d500SAkhil Goyal 	struct rte_crypto_cipher_xform *cipher_xform;
19441f14d500SAkhil Goyal 	dpaa_sec_session *session = (dpaa_sec_session *)sess;
19451f14d500SAkhil Goyal 
19461f14d500SAkhil Goyal 	PMD_INIT_FUNC_TRACE();
19471f14d500SAkhil Goyal 
19481f14d500SAkhil Goyal 	if (ipsec_xform->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
19491f14d500SAkhil Goyal 		cipher_xform = &conf->crypto_xform->cipher;
19501f14d500SAkhil Goyal 		auth_xform = &conf->crypto_xform->next->auth;
19511f14d500SAkhil Goyal 	} else {
19521f14d500SAkhil Goyal 		auth_xform = &conf->crypto_xform->auth;
19531f14d500SAkhil Goyal 		cipher_xform = &conf->crypto_xform->next->cipher;
19541f14d500SAkhil Goyal 	}
19551f14d500SAkhil Goyal 	session->proto_alg = conf->protocol;
19561f14d500SAkhil Goyal 	session->cipher_key.data = rte_zmalloc(NULL,
19571f14d500SAkhil Goyal 					       cipher_xform->key.length,
19581f14d500SAkhil Goyal 					       RTE_CACHE_LINE_SIZE);
19591f14d500SAkhil Goyal 	if (session->cipher_key.data == NULL &&
19601f14d500SAkhil Goyal 			cipher_xform->key.length > 0) {
19611f14d500SAkhil Goyal 		RTE_LOG(ERR, PMD, "No Memory for cipher key\n");
19621f14d500SAkhil Goyal 		return -ENOMEM;
19631f14d500SAkhil Goyal 	}
19641f14d500SAkhil Goyal 
19651f14d500SAkhil Goyal 	session->cipher_key.length = cipher_xform->key.length;
19661f14d500SAkhil Goyal 	session->auth_key.data = rte_zmalloc(NULL,
19671f14d500SAkhil Goyal 					auth_xform->key.length,
19681f14d500SAkhil Goyal 					RTE_CACHE_LINE_SIZE);
19691f14d500SAkhil Goyal 	if (session->auth_key.data == NULL &&
19701f14d500SAkhil Goyal 			auth_xform->key.length > 0) {
19711f14d500SAkhil Goyal 		RTE_LOG(ERR, PMD, "No Memory for auth key\n");
19721f14d500SAkhil Goyal 		rte_free(session->cipher_key.data);
19731f14d500SAkhil Goyal 		return -ENOMEM;
19741f14d500SAkhil Goyal 	}
19751f14d500SAkhil Goyal 	session->auth_key.length = auth_xform->key.length;
19761f14d500SAkhil Goyal 	memcpy(session->cipher_key.data, cipher_xform->key.data,
19771f14d500SAkhil Goyal 			cipher_xform->key.length);
19781f14d500SAkhil Goyal 	memcpy(session->auth_key.data, auth_xform->key.data,
19791f14d500SAkhil Goyal 			auth_xform->key.length);
19801f14d500SAkhil Goyal 
19811f14d500SAkhil Goyal 	switch (auth_xform->algo) {
19821f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA1_HMAC:
19831f14d500SAkhil Goyal 		session->auth_alg = RTE_CRYPTO_AUTH_SHA1_HMAC;
19841f14d500SAkhil Goyal 		break;
19851f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_MD5_HMAC:
19861f14d500SAkhil Goyal 		session->auth_alg = RTE_CRYPTO_AUTH_MD5_HMAC;
19871f14d500SAkhil Goyal 		break;
19881f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA256_HMAC:
19891f14d500SAkhil Goyal 		session->auth_alg = RTE_CRYPTO_AUTH_SHA256_HMAC;
19901f14d500SAkhil Goyal 		break;
19911f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA384_HMAC:
19921f14d500SAkhil Goyal 		session->auth_alg = RTE_CRYPTO_AUTH_SHA384_HMAC;
19931f14d500SAkhil Goyal 		break;
19941f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA512_HMAC:
19951f14d500SAkhil Goyal 		session->auth_alg = RTE_CRYPTO_AUTH_SHA512_HMAC;
19961f14d500SAkhil Goyal 		break;
19971f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_AES_CMAC:
19981f14d500SAkhil Goyal 		session->auth_alg = RTE_CRYPTO_AUTH_AES_CMAC;
19991f14d500SAkhil Goyal 		break;
20001f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_NULL:
20011f14d500SAkhil Goyal 		session->auth_alg = RTE_CRYPTO_AUTH_NULL;
20021f14d500SAkhil Goyal 		break;
20031f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA224_HMAC:
20041f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_AES_XCBC_MAC:
20051f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
20061f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA1:
20071f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA256:
20081f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA512:
20091f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA224:
20101f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA384:
20111f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_MD5:
20121f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_AES_GMAC:
20131f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_KASUMI_F9:
20141f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_AES_CBC_MAC:
20151f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_ZUC_EIA3:
20161f14d500SAkhil Goyal 		RTE_LOG(ERR, PMD, "Crypto: Unsupported auth alg %u\n",
20171f14d500SAkhil Goyal 			auth_xform->algo);
20181f14d500SAkhil Goyal 		goto out;
20191f14d500SAkhil Goyal 	default:
20201f14d500SAkhil Goyal 		RTE_LOG(ERR, PMD, "Crypto: Undefined Auth specified %u\n",
20211f14d500SAkhil Goyal 			auth_xform->algo);
20221f14d500SAkhil Goyal 		goto out;
20231f14d500SAkhil Goyal 	}
20241f14d500SAkhil Goyal 
20251f14d500SAkhil Goyal 	switch (cipher_xform->algo) {
20261f14d500SAkhil Goyal 	case RTE_CRYPTO_CIPHER_AES_CBC:
20271f14d500SAkhil Goyal 		session->cipher_alg = RTE_CRYPTO_CIPHER_AES_CBC;
20281f14d500SAkhil Goyal 		break;
20291f14d500SAkhil Goyal 	case RTE_CRYPTO_CIPHER_3DES_CBC:
20301f14d500SAkhil Goyal 		session->cipher_alg = RTE_CRYPTO_CIPHER_3DES_CBC;
20311f14d500SAkhil Goyal 		break;
20321f14d500SAkhil Goyal 	case RTE_CRYPTO_CIPHER_AES_CTR:
20331f14d500SAkhil Goyal 		session->cipher_alg = RTE_CRYPTO_CIPHER_AES_CTR;
20341f14d500SAkhil Goyal 		break;
20351f14d500SAkhil Goyal 	case RTE_CRYPTO_CIPHER_NULL:
20361f14d500SAkhil Goyal 	case RTE_CRYPTO_CIPHER_SNOW3G_UEA2:
20371f14d500SAkhil Goyal 	case RTE_CRYPTO_CIPHER_3DES_ECB:
20381f14d500SAkhil Goyal 	case RTE_CRYPTO_CIPHER_AES_ECB:
20391f14d500SAkhil Goyal 	case RTE_CRYPTO_CIPHER_KASUMI_F8:
20401f14d500SAkhil Goyal 		RTE_LOG(ERR, PMD, "Crypto: Unsupported Cipher alg %u\n",
20411f14d500SAkhil Goyal 			cipher_xform->algo);
20421f14d500SAkhil Goyal 		goto out;
20431f14d500SAkhil Goyal 	default:
20441f14d500SAkhil Goyal 		RTE_LOG(ERR, PMD, "Crypto: Undefined Cipher specified %u\n",
20451f14d500SAkhil Goyal 			cipher_xform->algo);
20461f14d500SAkhil Goyal 		goto out;
20471f14d500SAkhil Goyal 	}
20481f14d500SAkhil Goyal 
20491f14d500SAkhil Goyal 	if (ipsec_xform->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
20501f14d500SAkhil Goyal 		memset(&session->encap_pdb, 0, sizeof(struct ipsec_encap_pdb) +
20511f14d500SAkhil Goyal 				sizeof(session->ip4_hdr));
20521f14d500SAkhil Goyal 		session->ip4_hdr.ip_v = IPVERSION;
20531f14d500SAkhil Goyal 		session->ip4_hdr.ip_hl = 5;
20541f14d500SAkhil Goyal 		session->ip4_hdr.ip_len = rte_cpu_to_be_16(
20551f14d500SAkhil Goyal 						sizeof(session->ip4_hdr));
20561f14d500SAkhil Goyal 		session->ip4_hdr.ip_tos = ipsec_xform->tunnel.ipv4.dscp;
20571f14d500SAkhil Goyal 		session->ip4_hdr.ip_id = 0;
20581f14d500SAkhil Goyal 		session->ip4_hdr.ip_off = 0;
20591f14d500SAkhil Goyal 		session->ip4_hdr.ip_ttl = ipsec_xform->tunnel.ipv4.ttl;
20601f14d500SAkhil Goyal 		session->ip4_hdr.ip_p = (ipsec_xform->proto ==
20611f14d500SAkhil Goyal 				RTE_SECURITY_IPSEC_SA_PROTO_ESP) ? IPPROTO_ESP
20621f14d500SAkhil Goyal 				: IPPROTO_AH;
20631f14d500SAkhil Goyal 		session->ip4_hdr.ip_sum = 0;
20641f14d500SAkhil Goyal 		session->ip4_hdr.ip_src = ipsec_xform->tunnel.ipv4.src_ip;
20651f14d500SAkhil Goyal 		session->ip4_hdr.ip_dst = ipsec_xform->tunnel.ipv4.dst_ip;
20661f14d500SAkhil Goyal 		session->ip4_hdr.ip_sum = calc_chksum((uint16_t *)
20671f14d500SAkhil Goyal 						(void *)&session->ip4_hdr,
20681f14d500SAkhil Goyal 						sizeof(struct ip));
20691f14d500SAkhil Goyal 
20701f14d500SAkhil Goyal 		session->encap_pdb.options =
20711f14d500SAkhil Goyal 			(IPVERSION << PDBNH_ESP_ENCAP_SHIFT) |
20721f14d500SAkhil Goyal 			PDBOPTS_ESP_OIHI_PDB_INL |
20731f14d500SAkhil Goyal 			PDBOPTS_ESP_IVSRC |
20741f14d500SAkhil Goyal 			PDBHMO_ESP_ENCAP_DTTL;
20751f14d500SAkhil Goyal 		session->encap_pdb.spi = ipsec_xform->spi;
20761f14d500SAkhil Goyal 		session->encap_pdb.ip_hdr_len = sizeof(struct ip);
20771f14d500SAkhil Goyal 
20781f14d500SAkhil Goyal 		session->dir = DIR_ENC;
20791f14d500SAkhil Goyal 	} else if (ipsec_xform->direction ==
20801f14d500SAkhil Goyal 			RTE_SECURITY_IPSEC_SA_DIR_INGRESS) {
20811f14d500SAkhil Goyal 		memset(&session->decap_pdb, 0, sizeof(struct ipsec_decap_pdb));
20821f14d500SAkhil Goyal 		session->decap_pdb.options = sizeof(struct ip) << 16;
20831f14d500SAkhil Goyal 		session->dir = DIR_DEC;
20841f14d500SAkhil Goyal 	} else
20851f14d500SAkhil Goyal 		goto out;
20861f14d500SAkhil Goyal 	session->ctx_pool = internals->ctx_pool;
20871f14d500SAkhil Goyal 	session->inq = dpaa_sec_attach_rxq(internals);
20881f14d500SAkhil Goyal 	if (session->inq == NULL) {
20891f14d500SAkhil Goyal 		PMD_DRV_LOG(ERR, "unable to attach sec queue");
20901f14d500SAkhil Goyal 		goto out;
20911f14d500SAkhil Goyal 	}
20921f14d500SAkhil Goyal 
20931f14d500SAkhil Goyal 
20941f14d500SAkhil Goyal 	return 0;
20951f14d500SAkhil Goyal out:
20961f14d500SAkhil Goyal 	rte_free(session->auth_key.data);
20971f14d500SAkhil Goyal 	rte_free(session->cipher_key.data);
20981f14d500SAkhil Goyal 	memset(session, 0, sizeof(dpaa_sec_session));
20991f14d500SAkhil Goyal 	return -1;
21001f14d500SAkhil Goyal }
21011f14d500SAkhil Goyal 
21021f14d500SAkhil Goyal static int
21031f14d500SAkhil Goyal dpaa_sec_security_session_create(void *dev,
21041f14d500SAkhil Goyal 				 struct rte_security_session_conf *conf,
21051f14d500SAkhil Goyal 				 struct rte_security_session *sess,
21061f14d500SAkhil Goyal 				 struct rte_mempool *mempool)
21071f14d500SAkhil Goyal {
21081f14d500SAkhil Goyal 	void *sess_private_data;
21091f14d500SAkhil Goyal 	struct rte_cryptodev *cdev = (struct rte_cryptodev *)dev;
21101f14d500SAkhil Goyal 	int ret;
21111f14d500SAkhil Goyal 
21121f14d500SAkhil Goyal 	if (rte_mempool_get(mempool, &sess_private_data)) {
21131f14d500SAkhil Goyal 		CDEV_LOG_ERR(
21141f14d500SAkhil Goyal 			"Couldn't get object from session mempool");
21151f14d500SAkhil Goyal 		return -ENOMEM;
21161f14d500SAkhil Goyal 	}
21171f14d500SAkhil Goyal 
21181f14d500SAkhil Goyal 	switch (conf->protocol) {
21191f14d500SAkhil Goyal 	case RTE_SECURITY_PROTOCOL_IPSEC:
21201f14d500SAkhil Goyal 		ret = dpaa_sec_set_ipsec_session(cdev, conf,
21211f14d500SAkhil Goyal 				sess_private_data);
21221f14d500SAkhil Goyal 		break;
21231f14d500SAkhil Goyal 	case RTE_SECURITY_PROTOCOL_MACSEC:
21241f14d500SAkhil Goyal 		return -ENOTSUP;
21251f14d500SAkhil Goyal 	default:
21261f14d500SAkhil Goyal 		return -EINVAL;
21271f14d500SAkhil Goyal 	}
21281f14d500SAkhil Goyal 	if (ret != 0) {
21291f14d500SAkhil Goyal 		PMD_DRV_LOG(ERR,
21301f14d500SAkhil Goyal 			"DPAA2 PMD: failed to configure session parameters");
21311f14d500SAkhil Goyal 
21321f14d500SAkhil Goyal 		/* Return session to mempool */
21331f14d500SAkhil Goyal 		rte_mempool_put(mempool, sess_private_data);
21341f14d500SAkhil Goyal 		return ret;
21351f14d500SAkhil Goyal 	}
21361f14d500SAkhil Goyal 
21371f14d500SAkhil Goyal 	set_sec_session_private_data(sess, sess_private_data);
21381f14d500SAkhil Goyal 
21391f14d500SAkhil Goyal 	return ret;
21401f14d500SAkhil Goyal }
21411f14d500SAkhil Goyal 
21421f14d500SAkhil Goyal /** Clear the memory of session so it doesn't leave key material behind */
21431f14d500SAkhil Goyal static int
21441f14d500SAkhil Goyal dpaa_sec_security_session_destroy(void *dev __rte_unused,
21451f14d500SAkhil Goyal 		struct rte_security_session *sess)
21461f14d500SAkhil Goyal {
21471f14d500SAkhil Goyal 	PMD_INIT_FUNC_TRACE();
21481f14d500SAkhil Goyal 	void *sess_priv = get_sec_session_private_data(sess);
21491f14d500SAkhil Goyal 
21501f14d500SAkhil Goyal 	dpaa_sec_session *s = (dpaa_sec_session *)sess_priv;
21511f14d500SAkhil Goyal 
21521f14d500SAkhil Goyal 	if (sess_priv) {
21531f14d500SAkhil Goyal 		struct rte_mempool *sess_mp = rte_mempool_from_obj(sess_priv);
21541f14d500SAkhil Goyal 
21551f14d500SAkhil Goyal 		rte_free(s->cipher_key.data);
21561f14d500SAkhil Goyal 		rte_free(s->auth_key.data);
21571f14d500SAkhil Goyal 		memset(sess, 0, sizeof(dpaa_sec_session));
21581f14d500SAkhil Goyal 		set_sec_session_private_data(sess, NULL);
21591f14d500SAkhil Goyal 		rte_mempool_put(sess_mp, sess_priv);
21601f14d500SAkhil Goyal 	}
21611f14d500SAkhil Goyal 	return 0;
21621f14d500SAkhil Goyal }
21631f14d500SAkhil Goyal 
21641f14d500SAkhil Goyal 
21651f14d500SAkhil Goyal static int
2166c3e85bdcSAkhil Goyal dpaa_sec_dev_configure(struct rte_cryptodev *dev __rte_unused,
2167c3e85bdcSAkhil Goyal 		       struct rte_cryptodev_config *config __rte_unused)
2168c3e85bdcSAkhil Goyal {
2169c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2170c3e85bdcSAkhil Goyal 
2171c3e85bdcSAkhil Goyal 	return 0;
2172c3e85bdcSAkhil Goyal }
2173c3e85bdcSAkhil Goyal 
2174c3e85bdcSAkhil Goyal static int
2175c3e85bdcSAkhil Goyal dpaa_sec_dev_start(struct rte_cryptodev *dev __rte_unused)
2176c3e85bdcSAkhil Goyal {
2177c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2178c3e85bdcSAkhil Goyal 	return 0;
2179c3e85bdcSAkhil Goyal }
2180c3e85bdcSAkhil Goyal 
2181c3e85bdcSAkhil Goyal static void
2182c3e85bdcSAkhil Goyal dpaa_sec_dev_stop(struct rte_cryptodev *dev __rte_unused)
2183c3e85bdcSAkhil Goyal {
2184c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2185c3e85bdcSAkhil Goyal }
2186c3e85bdcSAkhil Goyal 
2187c3e85bdcSAkhil Goyal static int
2188c3e85bdcSAkhil Goyal dpaa_sec_dev_close(struct rte_cryptodev *dev __rte_unused)
2189c3e85bdcSAkhil Goyal {
2190c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2191c3e85bdcSAkhil Goyal 	return 0;
2192c3e85bdcSAkhil Goyal }
2193c3e85bdcSAkhil Goyal 
2194c3e85bdcSAkhil Goyal static void
2195c3e85bdcSAkhil Goyal dpaa_sec_dev_infos_get(struct rte_cryptodev *dev,
2196c3e85bdcSAkhil Goyal 		       struct rte_cryptodev_info *info)
2197c3e85bdcSAkhil Goyal {
2198c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals = dev->data->dev_private;
2199c3e85bdcSAkhil Goyal 
2200c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2201c3e85bdcSAkhil Goyal 	if (info != NULL) {
2202c3e85bdcSAkhil Goyal 		info->max_nb_queue_pairs = internals->max_nb_queue_pairs;
2203c3e85bdcSAkhil Goyal 		info->feature_flags = dev->feature_flags;
2204c3e85bdcSAkhil Goyal 		info->capabilities = dpaa_sec_capabilities;
2205c3e85bdcSAkhil Goyal 		info->sym.max_nb_sessions = internals->max_nb_sessions;
2206c3e85bdcSAkhil Goyal 		info->sym.max_nb_sessions_per_qp =
2207e79416d1SHemant Agrawal 			RTE_DPAA_SEC_PMD_MAX_NB_SESSIONS /
2208e79416d1SHemant Agrawal 			RTE_DPAA_MAX_NB_SEC_QPS;
2209c3e85bdcSAkhil Goyal 		info->driver_id = cryptodev_driver_id;
2210c3e85bdcSAkhil Goyal 	}
2211c3e85bdcSAkhil Goyal }
2212c3e85bdcSAkhil Goyal 
2213c3e85bdcSAkhil Goyal static struct rte_cryptodev_ops crypto_ops = {
2214c3e85bdcSAkhil Goyal 	.dev_configure	      = dpaa_sec_dev_configure,
2215c3e85bdcSAkhil Goyal 	.dev_start	      = dpaa_sec_dev_start,
2216c3e85bdcSAkhil Goyal 	.dev_stop	      = dpaa_sec_dev_stop,
2217c3e85bdcSAkhil Goyal 	.dev_close	      = dpaa_sec_dev_close,
2218c3e85bdcSAkhil Goyal 	.dev_infos_get        = dpaa_sec_dev_infos_get,
2219c3e85bdcSAkhil Goyal 	.queue_pair_setup     = dpaa_sec_queue_pair_setup,
2220c3e85bdcSAkhil Goyal 	.queue_pair_release   = dpaa_sec_queue_pair_release,
2221c3e85bdcSAkhil Goyal 	.queue_pair_start     = dpaa_sec_queue_pair_start,
2222c3e85bdcSAkhil Goyal 	.queue_pair_stop      = dpaa_sec_queue_pair_stop,
2223c3e85bdcSAkhil Goyal 	.queue_pair_count     = dpaa_sec_queue_pair_count,
2224c3e85bdcSAkhil Goyal 	.session_get_size     = dpaa_sec_session_get_size,
2225c3e85bdcSAkhil Goyal 	.session_configure    = dpaa_sec_session_configure,
2226c3e85bdcSAkhil Goyal 	.session_clear        = dpaa_sec_session_clear,
2227c3e85bdcSAkhil Goyal 	.qp_attach_session    = dpaa_sec_qp_attach_sess,
2228c3e85bdcSAkhil Goyal 	.qp_detach_session    = dpaa_sec_qp_detach_sess,
2229c3e85bdcSAkhil Goyal };
2230c3e85bdcSAkhil Goyal 
22311f14d500SAkhil Goyal static const struct rte_security_capability *
22321f14d500SAkhil Goyal dpaa_sec_capabilities_get(void *device __rte_unused)
22331f14d500SAkhil Goyal {
22341f14d500SAkhil Goyal 	return dpaa_sec_security_cap;
22351f14d500SAkhil Goyal }
22361f14d500SAkhil Goyal 
22371f14d500SAkhil Goyal struct rte_security_ops dpaa_sec_security_ops = {
22381f14d500SAkhil Goyal 	.session_create = dpaa_sec_security_session_create,
22391f14d500SAkhil Goyal 	.session_update = NULL,
22401f14d500SAkhil Goyal 	.session_stats_get = NULL,
22411f14d500SAkhil Goyal 	.session_destroy = dpaa_sec_security_session_destroy,
22421f14d500SAkhil Goyal 	.set_pkt_metadata = NULL,
22431f14d500SAkhil Goyal 	.capabilities_get = dpaa_sec_capabilities_get
22441f14d500SAkhil Goyal };
22451f14d500SAkhil Goyal 
2246c3e85bdcSAkhil Goyal static int
2247c3e85bdcSAkhil Goyal dpaa_sec_uninit(struct rte_cryptodev *dev)
2248c3e85bdcSAkhil Goyal {
2249c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals = dev->data->dev_private;
2250c3e85bdcSAkhil Goyal 
2251c3e85bdcSAkhil Goyal 	if (dev == NULL)
2252c3e85bdcSAkhil Goyal 		return -ENODEV;
2253c3e85bdcSAkhil Goyal 
22541f14d500SAkhil Goyal 	rte_free(dev->security_ctx);
22551f14d500SAkhil Goyal 
2256c3e85bdcSAkhil Goyal 	rte_mempool_free(internals->ctx_pool);
2257c3e85bdcSAkhil Goyal 	rte_free(internals);
2258c3e85bdcSAkhil Goyal 
2259c3e85bdcSAkhil Goyal 	PMD_INIT_LOG(INFO, "Closing DPAA_SEC device %s on numa socket %u\n",
2260c3e85bdcSAkhil Goyal 		     dev->data->name, rte_socket_id());
2261c3e85bdcSAkhil Goyal 
2262c3e85bdcSAkhil Goyal 	return 0;
2263c3e85bdcSAkhil Goyal }
2264c3e85bdcSAkhil Goyal 
2265c3e85bdcSAkhil Goyal static int
2266c3e85bdcSAkhil Goyal dpaa_sec_dev_init(struct rte_cryptodev *cryptodev)
2267c3e85bdcSAkhil Goyal {
2268c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals;
22691f14d500SAkhil Goyal 	struct rte_security_ctx *security_instance;
2270c3e85bdcSAkhil Goyal 	struct dpaa_sec_qp *qp;
2271e79416d1SHemant Agrawal 	uint32_t i, flags;
2272c3e85bdcSAkhil Goyal 	int ret;
2273c3e85bdcSAkhil Goyal 	char str[20];
2274c3e85bdcSAkhil Goyal 
2275c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2276c3e85bdcSAkhil Goyal 
2277c3e85bdcSAkhil Goyal 	cryptodev->driver_id = cryptodev_driver_id;
2278c3e85bdcSAkhil Goyal 	cryptodev->dev_ops = &crypto_ops;
2279c3e85bdcSAkhil Goyal 
2280c3e85bdcSAkhil Goyal 	cryptodev->enqueue_burst = dpaa_sec_enqueue_burst;
2281c3e85bdcSAkhil Goyal 	cryptodev->dequeue_burst = dpaa_sec_dequeue_burst;
2282c3e85bdcSAkhil Goyal 	cryptodev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
2283c3e85bdcSAkhil Goyal 			RTE_CRYPTODEV_FF_HW_ACCELERATED |
22841f14d500SAkhil Goyal 			RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
2285*a74af788SAkhil Goyal 			RTE_CRYPTODEV_FF_SECURITY |
2286*a74af788SAkhil Goyal 			RTE_CRYPTODEV_FF_MBUF_SCATTER_GATHER;
2287c3e85bdcSAkhil Goyal 
2288c3e85bdcSAkhil Goyal 	internals = cryptodev->data->dev_private;
2289e79416d1SHemant Agrawal 	internals->max_nb_queue_pairs = RTE_DPAA_MAX_NB_SEC_QPS;
2290c3e85bdcSAkhil Goyal 	internals->max_nb_sessions = RTE_DPAA_SEC_PMD_MAX_NB_SESSIONS;
2291c3e85bdcSAkhil Goyal 
22921f14d500SAkhil Goyal 	/*
22931f14d500SAkhil Goyal 	 * For secondary processes, we don't initialise any further as primary
22941f14d500SAkhil Goyal 	 * has already done this work. Only check we don't need a different
22951f14d500SAkhil Goyal 	 * RX function
22961f14d500SAkhil Goyal 	 */
22971f14d500SAkhil Goyal 	if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
22981f14d500SAkhil Goyal 		PMD_INIT_LOG(DEBUG, "Device already init by primary process");
22991f14d500SAkhil Goyal 		return 0;
23001f14d500SAkhil Goyal 	}
23011f14d500SAkhil Goyal 
23021f14d500SAkhil Goyal 	/* Initialize security_ctx only for primary process*/
23031f14d500SAkhil Goyal 	security_instance = rte_malloc("rte_security_instances_ops",
23041f14d500SAkhil Goyal 				sizeof(struct rte_security_ctx), 0);
23051f14d500SAkhil Goyal 	if (security_instance == NULL)
23061f14d500SAkhil Goyal 		return -ENOMEM;
23071f14d500SAkhil Goyal 	security_instance->device = (void *)cryptodev;
23081f14d500SAkhil Goyal 	security_instance->ops = &dpaa_sec_security_ops;
23091f14d500SAkhil Goyal 	security_instance->sess_cnt = 0;
23101f14d500SAkhil Goyal 	cryptodev->security_ctx = security_instance;
23111f14d500SAkhil Goyal 
2312c3e85bdcSAkhil Goyal 	for (i = 0; i < internals->max_nb_queue_pairs; i++) {
2313c3e85bdcSAkhil Goyal 		/* init qman fq for queue pair */
2314c3e85bdcSAkhil Goyal 		qp = &internals->qps[i];
2315c3e85bdcSAkhil Goyal 		ret = dpaa_sec_init_tx(&qp->outq);
2316c3e85bdcSAkhil Goyal 		if (ret) {
2317c3e85bdcSAkhil Goyal 			PMD_INIT_LOG(ERR, "config tx of queue pair  %d", i);
2318c3e85bdcSAkhil Goyal 			goto init_error;
2319c3e85bdcSAkhil Goyal 		}
2320e79416d1SHemant Agrawal 	}
2321e79416d1SHemant Agrawal 
2322e79416d1SHemant Agrawal 	flags = QMAN_FQ_FLAG_LOCKED | QMAN_FQ_FLAG_DYNAMIC_FQID |
2323e79416d1SHemant Agrawal 		QMAN_FQ_FLAG_TO_DCPORTAL;
2324e79416d1SHemant Agrawal 	for (i = 0; i < internals->max_nb_sessions; i++) {
2325e79416d1SHemant Agrawal 		/* create rx qman fq for sessions*/
2326e79416d1SHemant Agrawal 		ret = qman_create_fq(0, flags, &internals->inq[i]);
2327e79416d1SHemant Agrawal 		if (unlikely(ret != 0)) {
2328e79416d1SHemant Agrawal 			PMD_INIT_LOG(ERR, "sec qman_create_fq failed");
2329c3e85bdcSAkhil Goyal 			goto init_error;
2330c3e85bdcSAkhil Goyal 		}
2331c3e85bdcSAkhil Goyal 	}
2332c3e85bdcSAkhil Goyal 
2333c3e85bdcSAkhil Goyal 	sprintf(str, "ctx_pool_%d", cryptodev->data->dev_id);
2334c3e85bdcSAkhil Goyal 	internals->ctx_pool = rte_mempool_create((const char *)str,
2335c3e85bdcSAkhil Goyal 			CTX_POOL_NUM_BUFS,
2336c3e85bdcSAkhil Goyal 			CTX_POOL_BUF_SIZE,
2337c3e85bdcSAkhil Goyal 			CTX_POOL_CACHE_SIZE, 0,
2338c3e85bdcSAkhil Goyal 			NULL, NULL, NULL, NULL,
2339c3e85bdcSAkhil Goyal 			SOCKET_ID_ANY, 0);
2340c3e85bdcSAkhil Goyal 	if (!internals->ctx_pool) {
2341c3e85bdcSAkhil Goyal 		RTE_LOG(ERR, PMD, "%s create failed\n", str);
2342c3e85bdcSAkhil Goyal 		goto init_error;
2343c3e85bdcSAkhil Goyal 	}
2344c3e85bdcSAkhil Goyal 
2345c3e85bdcSAkhil Goyal 	PMD_INIT_LOG(DEBUG, "driver %s: created\n", cryptodev->data->name);
2346c3e85bdcSAkhil Goyal 	return 0;
2347c3e85bdcSAkhil Goyal 
2348c3e85bdcSAkhil Goyal init_error:
2349c3e85bdcSAkhil Goyal 	PMD_INIT_LOG(ERR, "driver %s: create failed\n", cryptodev->data->name);
2350c3e85bdcSAkhil Goyal 
2351c3e85bdcSAkhil Goyal 	dpaa_sec_uninit(cryptodev);
2352c3e85bdcSAkhil Goyal 	return -EFAULT;
2353c3e85bdcSAkhil Goyal }
2354c3e85bdcSAkhil Goyal 
2355c3e85bdcSAkhil Goyal static int
2356c3e85bdcSAkhil Goyal cryptodev_dpaa_sec_probe(struct rte_dpaa_driver *dpaa_drv,
2357c3e85bdcSAkhil Goyal 				struct rte_dpaa_device *dpaa_dev)
2358c3e85bdcSAkhil Goyal {
2359c3e85bdcSAkhil Goyal 	struct rte_cryptodev *cryptodev;
2360c3e85bdcSAkhil Goyal 	char cryptodev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
2361c3e85bdcSAkhil Goyal 
2362c3e85bdcSAkhil Goyal 	int retval;
2363c3e85bdcSAkhil Goyal 
2364c3e85bdcSAkhil Goyal 	sprintf(cryptodev_name, "dpaa_sec-%d", dpaa_dev->id.dev_id);
2365c3e85bdcSAkhil Goyal 
2366c3e85bdcSAkhil Goyal 	cryptodev = rte_cryptodev_pmd_allocate(cryptodev_name, rte_socket_id());
2367c3e85bdcSAkhil Goyal 	if (cryptodev == NULL)
2368c3e85bdcSAkhil Goyal 		return -ENOMEM;
2369c3e85bdcSAkhil Goyal 
2370c3e85bdcSAkhil Goyal 	if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
2371c3e85bdcSAkhil Goyal 		cryptodev->data->dev_private = rte_zmalloc_socket(
2372c3e85bdcSAkhil Goyal 					"cryptodev private structure",
2373c3e85bdcSAkhil Goyal 					sizeof(struct dpaa_sec_dev_private),
2374c3e85bdcSAkhil Goyal 					RTE_CACHE_LINE_SIZE,
2375c3e85bdcSAkhil Goyal 					rte_socket_id());
2376c3e85bdcSAkhil Goyal 
2377c3e85bdcSAkhil Goyal 		if (cryptodev->data->dev_private == NULL)
2378c3e85bdcSAkhil Goyal 			rte_panic("Cannot allocate memzone for private "
2379c3e85bdcSAkhil Goyal 					"device data");
2380c3e85bdcSAkhil Goyal 	}
2381c3e85bdcSAkhil Goyal 
2382c3e85bdcSAkhil Goyal 	dpaa_dev->crypto_dev = cryptodev;
2383c3e85bdcSAkhil Goyal 	cryptodev->device = &dpaa_dev->device;
2384c3e85bdcSAkhil Goyal 	cryptodev->device->driver = &dpaa_drv->driver;
2385c3e85bdcSAkhil Goyal 
2386c3e85bdcSAkhil Goyal 	/* init user callbacks */
2387c3e85bdcSAkhil Goyal 	TAILQ_INIT(&(cryptodev->link_intr_cbs));
2388c3e85bdcSAkhil Goyal 
2389c3e85bdcSAkhil Goyal 	/* if sec device version is not configured */
2390c3e85bdcSAkhil Goyal 	if (!rta_get_sec_era()) {
2391c3e85bdcSAkhil Goyal 		const struct device_node *caam_node;
2392c3e85bdcSAkhil Goyal 
2393c3e85bdcSAkhil Goyal 		for_each_compatible_node(caam_node, NULL, "fsl,sec-v4.0") {
2394c3e85bdcSAkhil Goyal 			const uint32_t *prop = of_get_property(caam_node,
2395c3e85bdcSAkhil Goyal 					"fsl,sec-era",
2396c3e85bdcSAkhil Goyal 					NULL);
2397c3e85bdcSAkhil Goyal 			if (prop) {
2398c3e85bdcSAkhil Goyal 				rta_set_sec_era(
2399c3e85bdcSAkhil Goyal 					INTL_SEC_ERA(rte_cpu_to_be_32(*prop)));
2400c3e85bdcSAkhil Goyal 				break;
2401c3e85bdcSAkhil Goyal 			}
2402c3e85bdcSAkhil Goyal 		}
2403c3e85bdcSAkhil Goyal 	}
2404c3e85bdcSAkhil Goyal 
2405c3e85bdcSAkhil Goyal 	/* Invoke PMD device initialization function */
2406c3e85bdcSAkhil Goyal 	retval = dpaa_sec_dev_init(cryptodev);
2407c3e85bdcSAkhil Goyal 	if (retval == 0)
2408c3e85bdcSAkhil Goyal 		return 0;
2409c3e85bdcSAkhil Goyal 
2410c3e85bdcSAkhil Goyal 	/* In case of error, cleanup is done */
2411c3e85bdcSAkhil Goyal 	if (rte_eal_process_type() == RTE_PROC_PRIMARY)
2412c3e85bdcSAkhil Goyal 		rte_free(cryptodev->data->dev_private);
2413c3e85bdcSAkhil Goyal 
2414c3e85bdcSAkhil Goyal 	rte_cryptodev_pmd_release_device(cryptodev);
2415c3e85bdcSAkhil Goyal 
2416c3e85bdcSAkhil Goyal 	return -ENXIO;
2417c3e85bdcSAkhil Goyal }
2418c3e85bdcSAkhil Goyal 
2419c3e85bdcSAkhil Goyal static int
2420c3e85bdcSAkhil Goyal cryptodev_dpaa_sec_remove(struct rte_dpaa_device *dpaa_dev)
2421c3e85bdcSAkhil Goyal {
2422c3e85bdcSAkhil Goyal 	struct rte_cryptodev *cryptodev;
2423c3e85bdcSAkhil Goyal 	int ret;
2424c3e85bdcSAkhil Goyal 
2425c3e85bdcSAkhil Goyal 	cryptodev = dpaa_dev->crypto_dev;
2426c3e85bdcSAkhil Goyal 	if (cryptodev == NULL)
2427c3e85bdcSAkhil Goyal 		return -ENODEV;
2428c3e85bdcSAkhil Goyal 
2429c3e85bdcSAkhil Goyal 	ret = dpaa_sec_uninit(cryptodev);
2430c3e85bdcSAkhil Goyal 	if (ret)
2431c3e85bdcSAkhil Goyal 		return ret;
2432c3e85bdcSAkhil Goyal 
2433f2f020d2SDeclan Doherty 	return rte_cryptodev_pmd_destroy(cryptodev);
2434c3e85bdcSAkhil Goyal }
2435c3e85bdcSAkhil Goyal 
2436c3e85bdcSAkhil Goyal static struct rte_dpaa_driver rte_dpaa_sec_driver = {
2437c3e85bdcSAkhil Goyal 	.drv_type = FSL_DPAA_CRYPTO,
2438c3e85bdcSAkhil Goyal 	.driver = {
2439c3e85bdcSAkhil Goyal 		.name = "DPAA SEC PMD"
2440c3e85bdcSAkhil Goyal 	},
2441c3e85bdcSAkhil Goyal 	.probe = cryptodev_dpaa_sec_probe,
2442c3e85bdcSAkhil Goyal 	.remove = cryptodev_dpaa_sec_remove,
2443c3e85bdcSAkhil Goyal };
2444c3e85bdcSAkhil Goyal 
2445c3e85bdcSAkhil Goyal static struct cryptodev_driver dpaa_sec_crypto_drv;
2446c3e85bdcSAkhil Goyal 
2447c3e85bdcSAkhil Goyal RTE_PMD_REGISTER_DPAA(CRYPTODEV_NAME_DPAA_SEC_PMD, rte_dpaa_sec_driver);
2448c3e85bdcSAkhil Goyal RTE_PMD_REGISTER_CRYPTO_DRIVER(dpaa_sec_crypto_drv, rte_dpaa_sec_driver,
2449c3e85bdcSAkhil Goyal 		cryptodev_driver_id);
2450