xref: /dpdk/drivers/crypto/dpaa_sec/dpaa_sec.c (revision 11d2f002e0e6bb33b3ce73d355c40360936ab415)
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;
870e5607e4SHemant Agrawal 	ctx->vtop_offset = (size_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 
1000e5607e4SHemant Agrawal 	vaddr_64 = (size_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 	}
1100e5607e4SHemant Agrawal 	return (size_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 {
1170e5607e4SHemant Agrawal 	return (size_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 {
123*11d2f002SAnatoly Burakov 	return rte_mem_iova2virt(paddr);
124c3e85bdcSAkhil Goyal }
125c3e85bdcSAkhil Goyal 
126c3e85bdcSAkhil Goyal static void
127c3e85bdcSAkhil Goyal ern_sec_fq_handler(struct qman_portal *qm __rte_unused,
128c3e85bdcSAkhil Goyal 		   struct qman_fq *fq,
129c3e85bdcSAkhil Goyal 		   const struct qm_mr_entry *msg)
130c3e85bdcSAkhil Goyal {
131c3e85bdcSAkhil Goyal 	RTE_LOG_DP(ERR, PMD, "sec fq %d error, RC = %x, seqnum = %x\n",
132c3e85bdcSAkhil Goyal 		   fq->fqid, msg->ern.rc, msg->ern.seqnum);
133c3e85bdcSAkhil Goyal }
134c3e85bdcSAkhil Goyal 
135c3e85bdcSAkhil Goyal /* initialize the queue with dest chan as caam chan so that
136c3e85bdcSAkhil Goyal  * all the packets in this queue could be dispatched into caam
137c3e85bdcSAkhil Goyal  */
138c3e85bdcSAkhil Goyal static int
139c4509373SSantosh Shukla dpaa_sec_init_rx(struct qman_fq *fq_in, rte_iova_t hwdesc,
140c3e85bdcSAkhil Goyal 		 uint32_t fqid_out)
141c3e85bdcSAkhil Goyal {
142c3e85bdcSAkhil Goyal 	struct qm_mcc_initfq fq_opts;
143c3e85bdcSAkhil Goyal 	uint32_t flags;
144c3e85bdcSAkhil Goyal 	int ret = -1;
145c3e85bdcSAkhil Goyal 
146c3e85bdcSAkhil Goyal 	/* Clear FQ options */
147c3e85bdcSAkhil Goyal 	memset(&fq_opts, 0x00, sizeof(struct qm_mcc_initfq));
148c3e85bdcSAkhil Goyal 
149c3e85bdcSAkhil Goyal 	flags = QMAN_INITFQ_FLAG_SCHED;
150c3e85bdcSAkhil Goyal 	fq_opts.we_mask = QM_INITFQ_WE_DESTWQ | QM_INITFQ_WE_CONTEXTA |
151c3e85bdcSAkhil Goyal 			  QM_INITFQ_WE_CONTEXTB;
152c3e85bdcSAkhil Goyal 
153c3e85bdcSAkhil Goyal 	qm_fqd_context_a_set64(&fq_opts.fqd, hwdesc);
154c3e85bdcSAkhil Goyal 	fq_opts.fqd.context_b = fqid_out;
155c3e85bdcSAkhil Goyal 	fq_opts.fqd.dest.channel = qm_channel_caam;
156c3e85bdcSAkhil Goyal 	fq_opts.fqd.dest.wq = 0;
157c3e85bdcSAkhil Goyal 
158c3e85bdcSAkhil Goyal 	fq_in->cb.ern  = ern_sec_fq_handler;
159c3e85bdcSAkhil Goyal 
160e79416d1SHemant Agrawal 	PMD_INIT_LOG(DEBUG, "in-%x out-%x", fq_in->fqid, fqid_out);
161e79416d1SHemant Agrawal 
162c3e85bdcSAkhil Goyal 	ret = qman_init_fq(fq_in, flags, &fq_opts);
163c3e85bdcSAkhil Goyal 	if (unlikely(ret != 0))
164e79416d1SHemant Agrawal 		PMD_INIT_LOG(ERR, "qman_init_fq failed %d", ret);
165c3e85bdcSAkhil Goyal 
166c3e85bdcSAkhil Goyal 	return ret;
167c3e85bdcSAkhil Goyal }
168c3e85bdcSAkhil Goyal 
169c3e85bdcSAkhil Goyal /* something is put into in_fq and caam put the crypto result into out_fq */
170c3e85bdcSAkhil Goyal static enum qman_cb_dqrr_result
171c3e85bdcSAkhil Goyal dqrr_out_fq_cb_rx(struct qman_portal *qm __always_unused,
172c3e85bdcSAkhil Goyal 		  struct qman_fq *fq __always_unused,
173c3e85bdcSAkhil Goyal 		  const struct qm_dqrr_entry *dqrr)
174c3e85bdcSAkhil Goyal {
175c3e85bdcSAkhil Goyal 	const struct qm_fd *fd;
176c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *job;
177c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
178c3e85bdcSAkhil Goyal 
179c3e85bdcSAkhil Goyal 	if (dpaa_sec_op_nb >= DPAA_SEC_BURST)
180c3e85bdcSAkhil Goyal 		return qman_cb_dqrr_defer;
181c3e85bdcSAkhil Goyal 
182c3e85bdcSAkhil Goyal 	if (!(dqrr->stat & QM_DQRR_STAT_FD_VALID))
183c3e85bdcSAkhil Goyal 		return qman_cb_dqrr_consume;
184c3e85bdcSAkhil Goyal 
185c3e85bdcSAkhil Goyal 	fd = &dqrr->fd;
186c3e85bdcSAkhil Goyal 	/* sg is embedded in an op ctx,
187c3e85bdcSAkhil Goyal 	 * sg[0] is for output
188c3e85bdcSAkhil Goyal 	 * sg[1] for input
189c3e85bdcSAkhil Goyal 	 */
190c3e85bdcSAkhil Goyal 	job = dpaa_mem_ptov(qm_fd_addr_get64(fd));
1911f14d500SAkhil Goyal 
192c3e85bdcSAkhil Goyal 	ctx = container_of(job, struct dpaa_sec_op_ctx, job);
193c3e85bdcSAkhil Goyal 	ctx->fd_status = fd->status;
1941f14d500SAkhil Goyal 	if (ctx->op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
1951f14d500SAkhil Goyal 		struct qm_sg_entry *sg_out;
1961f14d500SAkhil Goyal 		uint32_t len;
1971f14d500SAkhil Goyal 
1981f14d500SAkhil Goyal 		sg_out = &job->sg[0];
1991f14d500SAkhil Goyal 		hw_sg_to_cpu(sg_out);
2001f14d500SAkhil Goyal 		len = sg_out->length;
2011f14d500SAkhil Goyal 		ctx->op->sym->m_src->pkt_len = len;
2021f14d500SAkhil Goyal 		ctx->op->sym->m_src->data_len = len;
2031f14d500SAkhil Goyal 	}
204c3e85bdcSAkhil Goyal 	dpaa_sec_ops[dpaa_sec_op_nb++] = ctx->op;
205c3e85bdcSAkhil Goyal 	dpaa_sec_op_ending(ctx);
206c3e85bdcSAkhil Goyal 
207c3e85bdcSAkhil Goyal 	return qman_cb_dqrr_consume;
208c3e85bdcSAkhil Goyal }
209c3e85bdcSAkhil Goyal 
210c3e85bdcSAkhil Goyal /* caam result is put into this queue */
211c3e85bdcSAkhil Goyal static int
212c3e85bdcSAkhil Goyal dpaa_sec_init_tx(struct qman_fq *fq)
213c3e85bdcSAkhil Goyal {
214c3e85bdcSAkhil Goyal 	int ret;
215c3e85bdcSAkhil Goyal 	struct qm_mcc_initfq opts;
216c3e85bdcSAkhil Goyal 	uint32_t flags;
217c3e85bdcSAkhil Goyal 
218c3e85bdcSAkhil Goyal 	flags = QMAN_FQ_FLAG_NO_ENQUEUE | QMAN_FQ_FLAG_LOCKED |
219c3e85bdcSAkhil Goyal 		QMAN_FQ_FLAG_DYNAMIC_FQID;
220c3e85bdcSAkhil Goyal 
221c3e85bdcSAkhil Goyal 	ret = qman_create_fq(0, flags, fq);
222c3e85bdcSAkhil Goyal 	if (unlikely(ret)) {
223c3e85bdcSAkhil Goyal 		PMD_INIT_LOG(ERR, "qman_create_fq failed");
224c3e85bdcSAkhil Goyal 		return ret;
225c3e85bdcSAkhil Goyal 	}
226c3e85bdcSAkhil Goyal 
227c3e85bdcSAkhil Goyal 	memset(&opts, 0, sizeof(opts));
228c3e85bdcSAkhil Goyal 	opts.we_mask = QM_INITFQ_WE_DESTWQ | QM_INITFQ_WE_FQCTRL |
229c3e85bdcSAkhil Goyal 		       QM_INITFQ_WE_CONTEXTA | QM_INITFQ_WE_CONTEXTB;
230c3e85bdcSAkhil Goyal 
231c3e85bdcSAkhil Goyal 	/* opts.fqd.dest.channel = dpaa_sec_pool_chan; */
232c3e85bdcSAkhil Goyal 
233c3e85bdcSAkhil Goyal 	fq->cb.dqrr = dqrr_out_fq_cb_rx;
234c3e85bdcSAkhil Goyal 	fq->cb.ern  = ern_sec_fq_handler;
235c3e85bdcSAkhil Goyal 
236c3e85bdcSAkhil Goyal 	ret = qman_init_fq(fq, 0, &opts);
237c3e85bdcSAkhil Goyal 	if (unlikely(ret)) {
238c3e85bdcSAkhil Goyal 		PMD_INIT_LOG(ERR, "unable to init caam source fq!");
239c3e85bdcSAkhil Goyal 		return ret;
240c3e85bdcSAkhil Goyal 	}
241c3e85bdcSAkhil Goyal 
242c3e85bdcSAkhil Goyal 	return ret;
243c3e85bdcSAkhil Goyal }
244c3e85bdcSAkhil Goyal 
245c3e85bdcSAkhil Goyal static inline int is_cipher_only(dpaa_sec_session *ses)
246c3e85bdcSAkhil Goyal {
247c3e85bdcSAkhil Goyal 	return ((ses->cipher_alg != RTE_CRYPTO_CIPHER_NULL) &&
248c3e85bdcSAkhil Goyal 		(ses->auth_alg == RTE_CRYPTO_AUTH_NULL));
249c3e85bdcSAkhil Goyal }
250c3e85bdcSAkhil Goyal 
251c3e85bdcSAkhil Goyal static inline int is_auth_only(dpaa_sec_session *ses)
252c3e85bdcSAkhil Goyal {
253c3e85bdcSAkhil Goyal 	return ((ses->cipher_alg == RTE_CRYPTO_CIPHER_NULL) &&
254c3e85bdcSAkhil Goyal 		(ses->auth_alg != RTE_CRYPTO_AUTH_NULL));
255c3e85bdcSAkhil Goyal }
256c3e85bdcSAkhil Goyal 
257c3e85bdcSAkhil Goyal static inline int is_aead(dpaa_sec_session *ses)
258c3e85bdcSAkhil Goyal {
259c3e85bdcSAkhil Goyal 	return ((ses->cipher_alg == 0) &&
260c3e85bdcSAkhil Goyal 		(ses->auth_alg == 0) &&
261c3e85bdcSAkhil Goyal 		(ses->aead_alg != 0));
262c3e85bdcSAkhil Goyal }
263c3e85bdcSAkhil Goyal 
264c3e85bdcSAkhil Goyal static inline int is_auth_cipher(dpaa_sec_session *ses)
265c3e85bdcSAkhil Goyal {
266c3e85bdcSAkhil Goyal 	return ((ses->cipher_alg != RTE_CRYPTO_CIPHER_NULL) &&
2671f14d500SAkhil Goyal 		(ses->auth_alg != RTE_CRYPTO_AUTH_NULL) &&
2681f14d500SAkhil Goyal 		(ses->proto_alg != RTE_SECURITY_PROTOCOL_IPSEC));
2691f14d500SAkhil Goyal }
2701f14d500SAkhil Goyal 
2711f14d500SAkhil Goyal static inline int is_proto_ipsec(dpaa_sec_session *ses)
2721f14d500SAkhil Goyal {
2731f14d500SAkhil Goyal 	return (ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC);
274c3e85bdcSAkhil Goyal }
275c3e85bdcSAkhil Goyal 
276c3e85bdcSAkhil Goyal static inline int is_encode(dpaa_sec_session *ses)
277c3e85bdcSAkhil Goyal {
278c3e85bdcSAkhil Goyal 	return ses->dir == DIR_ENC;
279c3e85bdcSAkhil Goyal }
280c3e85bdcSAkhil Goyal 
281c3e85bdcSAkhil Goyal static inline int is_decode(dpaa_sec_session *ses)
282c3e85bdcSAkhil Goyal {
283c3e85bdcSAkhil Goyal 	return ses->dir == DIR_DEC;
284c3e85bdcSAkhil Goyal }
285c3e85bdcSAkhil Goyal 
286c3e85bdcSAkhil Goyal static inline void
287c3e85bdcSAkhil Goyal caam_auth_alg(dpaa_sec_session *ses, struct alginfo *alginfo_a)
288c3e85bdcSAkhil Goyal {
289c3e85bdcSAkhil Goyal 	switch (ses->auth_alg) {
290c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AUTH_NULL:
291c3e85bdcSAkhil Goyal 		ses->digest_length = 0;
292c3e85bdcSAkhil Goyal 		break;
293c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AUTH_MD5_HMAC:
2941f14d500SAkhil Goyal 		alginfo_a->algtype =
2951f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
2961f14d500SAkhil Goyal 			OP_PCL_IPSEC_HMAC_MD5_96 : OP_ALG_ALGSEL_MD5;
297c3e85bdcSAkhil Goyal 		alginfo_a->algmode = OP_ALG_AAI_HMAC;
298c3e85bdcSAkhil Goyal 		break;
299c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA1_HMAC:
3001f14d500SAkhil Goyal 		alginfo_a->algtype =
3011f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3021f14d500SAkhil Goyal 			OP_PCL_IPSEC_HMAC_SHA1_96 : OP_ALG_ALGSEL_SHA1;
303c3e85bdcSAkhil Goyal 		alginfo_a->algmode = OP_ALG_AAI_HMAC;
304c3e85bdcSAkhil Goyal 		break;
305c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA224_HMAC:
3061f14d500SAkhil Goyal 		alginfo_a->algtype =
3071f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3081f14d500SAkhil Goyal 			OP_PCL_IPSEC_HMAC_SHA1_160 : OP_ALG_ALGSEL_SHA224;
309c3e85bdcSAkhil Goyal 		alginfo_a->algmode = OP_ALG_AAI_HMAC;
310c3e85bdcSAkhil Goyal 		break;
311c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA256_HMAC:
3121f14d500SAkhil Goyal 		alginfo_a->algtype =
3131f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3141f14d500SAkhil Goyal 			OP_PCL_IPSEC_HMAC_SHA2_256_128 : OP_ALG_ALGSEL_SHA256;
315c3e85bdcSAkhil Goyal 		alginfo_a->algmode = OP_ALG_AAI_HMAC;
316c3e85bdcSAkhil Goyal 		break;
317c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA384_HMAC:
3181f14d500SAkhil Goyal 		alginfo_a->algtype =
3191f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3201f14d500SAkhil Goyal 			OP_PCL_IPSEC_HMAC_SHA2_384_192 : OP_ALG_ALGSEL_SHA384;
321c3e85bdcSAkhil Goyal 		alginfo_a->algmode = OP_ALG_AAI_HMAC;
322c3e85bdcSAkhil Goyal 		break;
323c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA512_HMAC:
3241f14d500SAkhil Goyal 		alginfo_a->algtype =
3251f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3261f14d500SAkhil Goyal 			OP_PCL_IPSEC_HMAC_SHA2_512_256 : OP_ALG_ALGSEL_SHA512;
327c3e85bdcSAkhil Goyal 		alginfo_a->algmode = OP_ALG_AAI_HMAC;
328c3e85bdcSAkhil Goyal 		break;
329c3e85bdcSAkhil Goyal 	default:
330c3e85bdcSAkhil Goyal 		PMD_INIT_LOG(ERR, "unsupported auth alg %u", ses->auth_alg);
331c3e85bdcSAkhil Goyal 	}
332c3e85bdcSAkhil Goyal }
333c3e85bdcSAkhil Goyal 
334c3e85bdcSAkhil Goyal static inline void
335c3e85bdcSAkhil Goyal caam_cipher_alg(dpaa_sec_session *ses, struct alginfo *alginfo_c)
336c3e85bdcSAkhil Goyal {
337c3e85bdcSAkhil Goyal 	switch (ses->cipher_alg) {
338c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_CIPHER_NULL:
339c3e85bdcSAkhil Goyal 		break;
340c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_CIPHER_AES_CBC:
3411f14d500SAkhil Goyal 		alginfo_c->algtype =
3421f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3431f14d500SAkhil Goyal 			OP_PCL_IPSEC_AES_CBC : OP_ALG_ALGSEL_AES;
344c3e85bdcSAkhil Goyal 		alginfo_c->algmode = OP_ALG_AAI_CBC;
345c3e85bdcSAkhil Goyal 		break;
346c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_CIPHER_3DES_CBC:
3471f14d500SAkhil Goyal 		alginfo_c->algtype =
3481f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3491f14d500SAkhil Goyal 			OP_PCL_IPSEC_3DES : OP_ALG_ALGSEL_3DES;
350c3e85bdcSAkhil Goyal 		alginfo_c->algmode = OP_ALG_AAI_CBC;
351c3e85bdcSAkhil Goyal 		break;
352c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_CIPHER_AES_CTR:
3531f14d500SAkhil Goyal 		alginfo_c->algtype =
3541f14d500SAkhil Goyal 			(ses->proto_alg == RTE_SECURITY_PROTOCOL_IPSEC) ?
3551f14d500SAkhil Goyal 			OP_PCL_IPSEC_AES_CTR : OP_ALG_ALGSEL_AES;
356c3e85bdcSAkhil Goyal 		alginfo_c->algmode = OP_ALG_AAI_CTR;
357c3e85bdcSAkhil Goyal 		break;
358c3e85bdcSAkhil Goyal 	default:
359c3e85bdcSAkhil Goyal 		PMD_INIT_LOG(ERR, "unsupported cipher alg %d", ses->cipher_alg);
360c3e85bdcSAkhil Goyal 	}
361c3e85bdcSAkhil Goyal }
362c3e85bdcSAkhil Goyal 
363c3e85bdcSAkhil Goyal static inline void
364c3e85bdcSAkhil Goyal caam_aead_alg(dpaa_sec_session *ses, struct alginfo *alginfo)
365c3e85bdcSAkhil Goyal {
366c3e85bdcSAkhil Goyal 	switch (ses->aead_alg) {
367c3e85bdcSAkhil Goyal 	case RTE_CRYPTO_AEAD_AES_GCM:
368c3e85bdcSAkhil Goyal 		alginfo->algtype = OP_ALG_ALGSEL_AES;
369c3e85bdcSAkhil Goyal 		alginfo->algmode = OP_ALG_AAI_GCM;
370c3e85bdcSAkhil Goyal 		break;
371c3e85bdcSAkhil Goyal 	default:
372c3e85bdcSAkhil Goyal 		PMD_INIT_LOG(ERR, "unsupported AEAD alg %d", ses->aead_alg);
373c3e85bdcSAkhil Goyal 	}
374c3e85bdcSAkhil Goyal }
375c3e85bdcSAkhil Goyal 
376c3e85bdcSAkhil Goyal 
377c3e85bdcSAkhil Goyal /* prepare command block of the session */
378c3e85bdcSAkhil Goyal static int
379c3e85bdcSAkhil Goyal dpaa_sec_prep_cdb(dpaa_sec_session *ses)
380c3e85bdcSAkhil Goyal {
381c3e85bdcSAkhil Goyal 	struct alginfo alginfo_c = {0}, alginfo_a = {0}, alginfo = {0};
382c3e85bdcSAkhil Goyal 	uint32_t shared_desc_len = 0;
383e79416d1SHemant Agrawal 	struct sec_cdb *cdb = &ses->cdb;
384c3e85bdcSAkhil Goyal 	int err;
385c3e85bdcSAkhil Goyal #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
386c3e85bdcSAkhil Goyal 	int swap = false;
387c3e85bdcSAkhil Goyal #else
388c3e85bdcSAkhil Goyal 	int swap = true;
389c3e85bdcSAkhil Goyal #endif
390c3e85bdcSAkhil Goyal 
391c3e85bdcSAkhil Goyal 	memset(cdb, 0, sizeof(struct sec_cdb));
392c3e85bdcSAkhil Goyal 
393c3e85bdcSAkhil Goyal 	if (is_cipher_only(ses)) {
394c3e85bdcSAkhil Goyal 		caam_cipher_alg(ses, &alginfo_c);
395c3e85bdcSAkhil Goyal 		if (alginfo_c.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
396c3e85bdcSAkhil Goyal 			PMD_TX_LOG(ERR, "not supported cipher alg\n");
397c3e85bdcSAkhil Goyal 			return -ENOTSUP;
398c3e85bdcSAkhil Goyal 		}
399c3e85bdcSAkhil Goyal 
4000e5607e4SHemant Agrawal 		alginfo_c.key = (size_t)ses->cipher_key.data;
401c3e85bdcSAkhil Goyal 		alginfo_c.keylen = ses->cipher_key.length;
402c3e85bdcSAkhil Goyal 		alginfo_c.key_enc_flags = 0;
403c3e85bdcSAkhil Goyal 		alginfo_c.key_type = RTA_DATA_IMM;
404c3e85bdcSAkhil Goyal 
405c3e85bdcSAkhil Goyal 		shared_desc_len = cnstr_shdsc_blkcipher(
406c3e85bdcSAkhil Goyal 						cdb->sh_desc, true,
407c3e85bdcSAkhil Goyal 						swap, &alginfo_c,
408c3e85bdcSAkhil Goyal 						NULL,
409c3e85bdcSAkhil Goyal 						ses->iv.length,
410c3e85bdcSAkhil Goyal 						ses->dir);
411c3e85bdcSAkhil Goyal 	} else if (is_auth_only(ses)) {
412c3e85bdcSAkhil Goyal 		caam_auth_alg(ses, &alginfo_a);
413c3e85bdcSAkhil Goyal 		if (alginfo_a.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
414c3e85bdcSAkhil Goyal 			PMD_TX_LOG(ERR, "not supported auth alg\n");
415c3e85bdcSAkhil Goyal 			return -ENOTSUP;
416c3e85bdcSAkhil Goyal 		}
417c3e85bdcSAkhil Goyal 
4180e5607e4SHemant Agrawal 		alginfo_a.key = (size_t)ses->auth_key.data;
419c3e85bdcSAkhil Goyal 		alginfo_a.keylen = ses->auth_key.length;
420c3e85bdcSAkhil Goyal 		alginfo_a.key_enc_flags = 0;
421c3e85bdcSAkhil Goyal 		alginfo_a.key_type = RTA_DATA_IMM;
422c3e85bdcSAkhil Goyal 
423c3e85bdcSAkhil Goyal 		shared_desc_len = cnstr_shdsc_hmac(cdb->sh_desc, true,
424c3e85bdcSAkhil Goyal 						   swap, &alginfo_a,
425c3e85bdcSAkhil Goyal 						   !ses->dir,
426c3e85bdcSAkhil Goyal 						   ses->digest_length);
427c3e85bdcSAkhil Goyal 	} else if (is_aead(ses)) {
428c3e85bdcSAkhil Goyal 		caam_aead_alg(ses, &alginfo);
429c3e85bdcSAkhil Goyal 		if (alginfo.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
430c3e85bdcSAkhil Goyal 			PMD_TX_LOG(ERR, "not supported aead alg\n");
431c3e85bdcSAkhil Goyal 			return -ENOTSUP;
432c3e85bdcSAkhil Goyal 		}
4330e5607e4SHemant Agrawal 		alginfo.key = (size_t)ses->aead_key.data;
434c3e85bdcSAkhil Goyal 		alginfo.keylen = ses->aead_key.length;
435c3e85bdcSAkhil Goyal 		alginfo.key_enc_flags = 0;
436c3e85bdcSAkhil Goyal 		alginfo.key_type = RTA_DATA_IMM;
437c3e85bdcSAkhil Goyal 
438c3e85bdcSAkhil Goyal 		if (ses->dir == DIR_ENC)
439c3e85bdcSAkhil Goyal 			shared_desc_len = cnstr_shdsc_gcm_encap(
440c3e85bdcSAkhil Goyal 					cdb->sh_desc, true, swap,
441c3e85bdcSAkhil Goyal 					&alginfo,
442c3e85bdcSAkhil Goyal 					ses->iv.length,
443c3e85bdcSAkhil Goyal 					ses->digest_length);
444c3e85bdcSAkhil Goyal 		else
445c3e85bdcSAkhil Goyal 			shared_desc_len = cnstr_shdsc_gcm_decap(
446c3e85bdcSAkhil Goyal 					cdb->sh_desc, true, swap,
447c3e85bdcSAkhil Goyal 					&alginfo,
448c3e85bdcSAkhil Goyal 					ses->iv.length,
449c3e85bdcSAkhil Goyal 					ses->digest_length);
450c3e85bdcSAkhil Goyal 	} else {
451c3e85bdcSAkhil Goyal 		caam_cipher_alg(ses, &alginfo_c);
452c3e85bdcSAkhil Goyal 		if (alginfo_c.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
453c3e85bdcSAkhil Goyal 			PMD_TX_LOG(ERR, "not supported cipher alg\n");
454c3e85bdcSAkhil Goyal 			return -ENOTSUP;
455c3e85bdcSAkhil Goyal 		}
456c3e85bdcSAkhil Goyal 
4570e5607e4SHemant Agrawal 		alginfo_c.key = (size_t)ses->cipher_key.data;
458c3e85bdcSAkhil Goyal 		alginfo_c.keylen = ses->cipher_key.length;
459c3e85bdcSAkhil Goyal 		alginfo_c.key_enc_flags = 0;
460c3e85bdcSAkhil Goyal 		alginfo_c.key_type = RTA_DATA_IMM;
461c3e85bdcSAkhil Goyal 
462c3e85bdcSAkhil Goyal 		caam_auth_alg(ses, &alginfo_a);
463c3e85bdcSAkhil Goyal 		if (alginfo_a.algtype == (unsigned int)DPAA_SEC_ALG_UNSUPPORT) {
464c3e85bdcSAkhil Goyal 			PMD_TX_LOG(ERR, "not supported auth alg\n");
465c3e85bdcSAkhil Goyal 			return -ENOTSUP;
466c3e85bdcSAkhil Goyal 		}
467c3e85bdcSAkhil Goyal 
4680e5607e4SHemant Agrawal 		alginfo_a.key = (size_t)ses->auth_key.data;
469c3e85bdcSAkhil Goyal 		alginfo_a.keylen = ses->auth_key.length;
470c3e85bdcSAkhil Goyal 		alginfo_a.key_enc_flags = 0;
471c3e85bdcSAkhil Goyal 		alginfo_a.key_type = RTA_DATA_IMM;
472c3e85bdcSAkhil Goyal 
473c3e85bdcSAkhil Goyal 		cdb->sh_desc[0] = alginfo_c.keylen;
474c3e85bdcSAkhil Goyal 		cdb->sh_desc[1] = alginfo_a.keylen;
475c3e85bdcSAkhil Goyal 		err = rta_inline_query(IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN,
476c3e85bdcSAkhil Goyal 				       MIN_JOB_DESC_SIZE,
477c3e85bdcSAkhil Goyal 				       (unsigned int *)cdb->sh_desc,
478c3e85bdcSAkhil Goyal 				       &cdb->sh_desc[2], 2);
479c3e85bdcSAkhil Goyal 
480c3e85bdcSAkhil Goyal 		if (err < 0) {
481c3e85bdcSAkhil Goyal 			PMD_TX_LOG(ERR, "Crypto: Incorrect key lengths");
482c3e85bdcSAkhil Goyal 			return err;
483c3e85bdcSAkhil Goyal 		}
484c3e85bdcSAkhil Goyal 		if (cdb->sh_desc[2] & 1)
485c3e85bdcSAkhil Goyal 			alginfo_c.key_type = RTA_DATA_IMM;
486c3e85bdcSAkhil Goyal 		else {
4870e5607e4SHemant Agrawal 			alginfo_c.key = (size_t)dpaa_mem_vtop(
4880e5607e4SHemant Agrawal 						(void *)(size_t)alginfo_c.key);
489c3e85bdcSAkhil Goyal 			alginfo_c.key_type = RTA_DATA_PTR;
490c3e85bdcSAkhil Goyal 		}
491c3e85bdcSAkhil Goyal 		if (cdb->sh_desc[2] & (1<<1))
492c3e85bdcSAkhil Goyal 			alginfo_a.key_type = RTA_DATA_IMM;
493c3e85bdcSAkhil Goyal 		else {
4940e5607e4SHemant Agrawal 			alginfo_a.key = (size_t)dpaa_mem_vtop(
4950e5607e4SHemant Agrawal 						(void *)(size_t)alginfo_a.key);
496c3e85bdcSAkhil Goyal 			alginfo_a.key_type = RTA_DATA_PTR;
497c3e85bdcSAkhil Goyal 		}
498c3e85bdcSAkhil Goyal 		cdb->sh_desc[0] = 0;
499c3e85bdcSAkhil Goyal 		cdb->sh_desc[1] = 0;
500c3e85bdcSAkhil Goyal 		cdb->sh_desc[2] = 0;
5011f14d500SAkhil Goyal 		if (is_proto_ipsec(ses)) {
5021f14d500SAkhil Goyal 			if (ses->dir == DIR_ENC) {
5031f14d500SAkhil Goyal 				shared_desc_len = cnstr_shdsc_ipsec_new_encap(
5041f14d500SAkhil Goyal 						cdb->sh_desc,
5051f14d500SAkhil Goyal 						true, swap, &ses->encap_pdb,
5061f14d500SAkhil Goyal 						(uint8_t *)&ses->ip4_hdr,
5071f14d500SAkhil Goyal 						&alginfo_c, &alginfo_a);
5081f14d500SAkhil Goyal 			} else if (ses->dir == DIR_DEC) {
5091f14d500SAkhil Goyal 				shared_desc_len = cnstr_shdsc_ipsec_new_decap(
5101f14d500SAkhil Goyal 						cdb->sh_desc,
5111f14d500SAkhil Goyal 						true, swap, &ses->decap_pdb,
5121f14d500SAkhil Goyal 						&alginfo_c, &alginfo_a);
5131f14d500SAkhil Goyal 			}
5141f14d500SAkhil Goyal 		} else {
5151f14d500SAkhil Goyal 			/* Auth_only_len is set as 0 here and it will be
5161f14d500SAkhil Goyal 			 * overwritten in fd for each packet.
517c3e85bdcSAkhil Goyal 			 */
518c3e85bdcSAkhil Goyal 			shared_desc_len = cnstr_shdsc_authenc(cdb->sh_desc,
519c3e85bdcSAkhil Goyal 					true, swap, &alginfo_c, &alginfo_a,
520c3e85bdcSAkhil Goyal 					ses->iv.length, 0,
521c3e85bdcSAkhil Goyal 					ses->digest_length, ses->dir);
522c3e85bdcSAkhil Goyal 		}
5231f14d500SAkhil Goyal 	}
524c3e85bdcSAkhil Goyal 	cdb->sh_hdr.hi.field.idlen = shared_desc_len;
525c3e85bdcSAkhil Goyal 	cdb->sh_hdr.hi.word = rte_cpu_to_be_32(cdb->sh_hdr.hi.word);
526c3e85bdcSAkhil Goyal 	cdb->sh_hdr.lo.word = rte_cpu_to_be_32(cdb->sh_hdr.lo.word);
527c3e85bdcSAkhil Goyal 
528c3e85bdcSAkhil Goyal 	return 0;
529c3e85bdcSAkhil Goyal }
530c3e85bdcSAkhil Goyal 
531c3e85bdcSAkhil Goyal /* qp is lockless, should be accessed by only one thread */
532c3e85bdcSAkhil Goyal static int
533c3e85bdcSAkhil Goyal dpaa_sec_deq(struct dpaa_sec_qp *qp, struct rte_crypto_op **ops, int nb_ops)
534c3e85bdcSAkhil Goyal {
535c3e85bdcSAkhil Goyal 	struct qman_fq *fq;
5369a984458SAkhil Goyal 	unsigned int pkts = 0;
5379a984458SAkhil Goyal 	int ret;
5389a984458SAkhil Goyal 	struct qm_dqrr_entry *dq;
539c3e85bdcSAkhil Goyal 
540c3e85bdcSAkhil Goyal 	fq = &qp->outq;
5419a984458SAkhil Goyal 	ret = qman_set_vdq(fq, (nb_ops > DPAA_MAX_DEQUEUE_NUM_FRAMES) ?
5429a984458SAkhil Goyal 				DPAA_MAX_DEQUEUE_NUM_FRAMES : nb_ops);
5439a984458SAkhil Goyal 	if (ret)
5449a984458SAkhil Goyal 		return 0;
545c3e85bdcSAkhil Goyal 
5469a984458SAkhil Goyal 	do {
5479a984458SAkhil Goyal 		const struct qm_fd *fd;
5489a984458SAkhil Goyal 		struct dpaa_sec_job *job;
5499a984458SAkhil Goyal 		struct dpaa_sec_op_ctx *ctx;
5509a984458SAkhil Goyal 		struct rte_crypto_op *op;
551c3e85bdcSAkhil Goyal 
5529a984458SAkhil Goyal 		dq = qman_dequeue(fq);
5539a984458SAkhil Goyal 		if (!dq)
5549a984458SAkhil Goyal 			continue;
5559a984458SAkhil Goyal 
5569a984458SAkhil Goyal 		fd = &dq->fd;
5579a984458SAkhil Goyal 		/* sg is embedded in an op ctx,
5589a984458SAkhil Goyal 		 * sg[0] is for output
5599a984458SAkhil Goyal 		 * sg[1] for input
5609a984458SAkhil Goyal 		 */
5619a984458SAkhil Goyal 		job = dpaa_mem_ptov(qm_fd_addr_get64(fd));
5629a984458SAkhil Goyal 
5639a984458SAkhil Goyal 		ctx = container_of(job, struct dpaa_sec_op_ctx, job);
5649a984458SAkhil Goyal 		ctx->fd_status = fd->status;
5659a984458SAkhil Goyal 		op = ctx->op;
5669a984458SAkhil Goyal 		if (op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
5679a984458SAkhil Goyal 			struct qm_sg_entry *sg_out;
5689a984458SAkhil Goyal 			uint32_t len;
5699a984458SAkhil Goyal 
5709a984458SAkhil Goyal 			sg_out = &job->sg[0];
5719a984458SAkhil Goyal 			hw_sg_to_cpu(sg_out);
5729a984458SAkhil Goyal 			len = sg_out->length;
5739a984458SAkhil Goyal 			op->sym->m_src->pkt_len = len;
5749a984458SAkhil Goyal 			op->sym->m_src->data_len = len;
5759a984458SAkhil Goyal 		}
5769a984458SAkhil Goyal 		if (!ctx->fd_status) {
5779a984458SAkhil Goyal 			op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
5789a984458SAkhil Goyal 		} else {
5799a984458SAkhil Goyal 			printf("\nSEC return err: 0x%x", ctx->fd_status);
5809a984458SAkhil Goyal 			op->status = RTE_CRYPTO_OP_STATUS_ERROR;
5819a984458SAkhil Goyal 		}
5829a984458SAkhil Goyal 		ops[pkts++] = op;
5839a984458SAkhil Goyal 
5849a984458SAkhil Goyal 		/* report op status to sym->op and then free the ctx memeory */
5859a984458SAkhil Goyal 		rte_mempool_put(ctx->ctx_pool, (void *)ctx);
5869a984458SAkhil Goyal 
5879a984458SAkhil Goyal 		qman_dqrr_consume(fq, dq);
5889a984458SAkhil Goyal 	} while (fq->flags & QMAN_FQ_STATE_VDQCR);
5899a984458SAkhil Goyal 
5909a984458SAkhil Goyal 	return pkts;
591c3e85bdcSAkhil Goyal }
592c3e85bdcSAkhil Goyal 
593a74af788SAkhil Goyal static inline struct dpaa_sec_job *
594a74af788SAkhil Goyal build_auth_only_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
595a74af788SAkhil Goyal {
596a74af788SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
597a74af788SAkhil Goyal 	struct rte_mbuf *mbuf = sym->m_src;
598a74af788SAkhil Goyal 	struct dpaa_sec_job *cf;
599a74af788SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
600a74af788SAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
601a74af788SAkhil Goyal 	phys_addr_t start_addr;
602a74af788SAkhil Goyal 	uint8_t *old_digest, extra_segs;
603a74af788SAkhil Goyal 
604a74af788SAkhil Goyal 	if (is_decode(ses))
605a74af788SAkhil Goyal 		extra_segs = 3;
606a74af788SAkhil Goyal 	else
607a74af788SAkhil Goyal 		extra_segs = 2;
608a74af788SAkhil Goyal 
609a74af788SAkhil Goyal 	if ((mbuf->nb_segs + extra_segs) > MAX_SG_ENTRIES) {
610a74af788SAkhil Goyal 		PMD_TX_LOG(ERR, "Auth: Max sec segs supported is %d\n",
611a74af788SAkhil Goyal 								MAX_SG_ENTRIES);
612a74af788SAkhil Goyal 		return NULL;
613a74af788SAkhil Goyal 	}
614a74af788SAkhil Goyal 	ctx = dpaa_sec_alloc_ctx(ses);
615a74af788SAkhil Goyal 	if (!ctx)
616a74af788SAkhil Goyal 		return NULL;
617a74af788SAkhil Goyal 
618a74af788SAkhil Goyal 	cf = &ctx->job;
619a74af788SAkhil Goyal 	ctx->op = op;
620a74af788SAkhil Goyal 	old_digest = ctx->digest;
621a74af788SAkhil Goyal 
622a74af788SAkhil Goyal 	/* output */
623a74af788SAkhil Goyal 	out_sg = &cf->sg[0];
624a74af788SAkhil Goyal 	qm_sg_entry_set64(out_sg, sym->auth.digest.phys_addr);
625a74af788SAkhil Goyal 	out_sg->length = ses->digest_length;
626a74af788SAkhil Goyal 	cpu_to_hw_sg(out_sg);
627a74af788SAkhil Goyal 
628a74af788SAkhil Goyal 	/* input */
629a74af788SAkhil Goyal 	in_sg = &cf->sg[1];
630a74af788SAkhil Goyal 	/* need to extend the input to a compound frame */
631a74af788SAkhil Goyal 	in_sg->extension = 1;
632a74af788SAkhil Goyal 	in_sg->final = 1;
633a74af788SAkhil Goyal 	in_sg->length = sym->auth.data.length;
634a74af788SAkhil Goyal 	qm_sg_entry_set64(in_sg, dpaa_mem_vtop_ctx(ctx, &cf->sg[2]));
635a74af788SAkhil Goyal 
636a74af788SAkhil Goyal 	/* 1st seg */
637a74af788SAkhil Goyal 	sg = in_sg + 1;
638a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
639a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->auth.data.offset;
640a74af788SAkhil Goyal 	sg->offset = sym->auth.data.offset;
641a74af788SAkhil Goyal 
642a74af788SAkhil Goyal 	/* Successive segs */
643a74af788SAkhil Goyal 	mbuf = mbuf->next;
644a74af788SAkhil Goyal 	while (mbuf) {
645a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
646a74af788SAkhil Goyal 		sg++;
647a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
648a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
649a74af788SAkhil Goyal 		mbuf = mbuf->next;
650a74af788SAkhil Goyal 	}
651a74af788SAkhil Goyal 
652a74af788SAkhil Goyal 	if (is_decode(ses)) {
653a74af788SAkhil Goyal 		/* Digest verification case */
654a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
655a74af788SAkhil Goyal 		sg++;
656a74af788SAkhil Goyal 		rte_memcpy(old_digest, sym->auth.digest.data,
657a74af788SAkhil Goyal 				ses->digest_length);
658a74af788SAkhil Goyal 		start_addr = dpaa_mem_vtop_ctx(ctx, old_digest);
659a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, start_addr);
660a74af788SAkhil Goyal 		sg->length = ses->digest_length;
661a74af788SAkhil Goyal 		in_sg->length += ses->digest_length;
662a74af788SAkhil Goyal 	} else {
663a74af788SAkhil Goyal 		/* Digest calculation case */
664a74af788SAkhil Goyal 		sg->length -= ses->digest_length;
665a74af788SAkhil Goyal 	}
666a74af788SAkhil Goyal 	sg->final = 1;
667a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
668a74af788SAkhil Goyal 	cpu_to_hw_sg(in_sg);
669a74af788SAkhil Goyal 
670a74af788SAkhil Goyal 	return cf;
671a74af788SAkhil Goyal }
672a74af788SAkhil Goyal 
673c3e85bdcSAkhil Goyal /**
674c3e85bdcSAkhil Goyal  * packet looks like:
675c3e85bdcSAkhil Goyal  *		|<----data_len------->|
676c3e85bdcSAkhil Goyal  *    |ip_header|ah_header|icv|payload|
677c3e85bdcSAkhil Goyal  *              ^
678c3e85bdcSAkhil Goyal  *		|
679c3e85bdcSAkhil Goyal  *	   mbuf->pkt.data
680c3e85bdcSAkhil Goyal  */
681c3e85bdcSAkhil Goyal static inline struct dpaa_sec_job *
682c3e85bdcSAkhil Goyal build_auth_only(struct rte_crypto_op *op, dpaa_sec_session *ses)
683c3e85bdcSAkhil Goyal {
684c3e85bdcSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
685c3e85bdcSAkhil Goyal 	struct rte_mbuf *mbuf = sym->m_src;
686c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
687c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
688c3e85bdcSAkhil Goyal 	struct qm_sg_entry *sg;
689c4509373SSantosh Shukla 	rte_iova_t start_addr;
690c3e85bdcSAkhil Goyal 	uint8_t *old_digest;
691c3e85bdcSAkhil Goyal 
692c3e85bdcSAkhil Goyal 	ctx = dpaa_sec_alloc_ctx(ses);
693c3e85bdcSAkhil Goyal 	if (!ctx)
694c3e85bdcSAkhil Goyal 		return NULL;
695c3e85bdcSAkhil Goyal 
696c3e85bdcSAkhil Goyal 	cf = &ctx->job;
697c3e85bdcSAkhil Goyal 	ctx->op = op;
698c3e85bdcSAkhil Goyal 	old_digest = ctx->digest;
699c3e85bdcSAkhil Goyal 
700bfa9a8a4SThomas Monjalon 	start_addr = rte_pktmbuf_iova(mbuf);
701c3e85bdcSAkhil Goyal 	/* output */
702c3e85bdcSAkhil Goyal 	sg = &cf->sg[0];
703c3e85bdcSAkhil Goyal 	qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
704c3e85bdcSAkhil Goyal 	sg->length = ses->digest_length;
705c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
706c3e85bdcSAkhil Goyal 
707c3e85bdcSAkhil Goyal 	/* input */
708c3e85bdcSAkhil Goyal 	sg = &cf->sg[1];
709c3e85bdcSAkhil Goyal 	if (is_decode(ses)) {
710c3e85bdcSAkhil Goyal 		/* need to extend the input to a compound frame */
711c3e85bdcSAkhil Goyal 		sg->extension = 1;
712fcf67029SHemant Agrawal 		qm_sg_entry_set64(sg, dpaa_mem_vtop_ctx(ctx, &cf->sg[2]));
713c3e85bdcSAkhil Goyal 		sg->length = sym->auth.data.length + ses->digest_length;
714c3e85bdcSAkhil Goyal 		sg->final = 1;
715c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
716c3e85bdcSAkhil Goyal 
717c3e85bdcSAkhil Goyal 		sg = &cf->sg[2];
718c3e85bdcSAkhil Goyal 		/* hash result or digest, save digest first */
719c3e85bdcSAkhil Goyal 		rte_memcpy(old_digest, sym->auth.digest.data,
720c3e85bdcSAkhil Goyal 			   ses->digest_length);
721c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, start_addr + sym->auth.data.offset);
722c3e85bdcSAkhil Goyal 		sg->length = sym->auth.data.length;
723c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
724c3e85bdcSAkhil Goyal 
725c3e85bdcSAkhil Goyal 		/* let's check digest by hw */
726fcf67029SHemant Agrawal 		start_addr = dpaa_mem_vtop_ctx(ctx, old_digest);
727c3e85bdcSAkhil Goyal 		sg++;
728c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, start_addr);
729c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
730c3e85bdcSAkhil Goyal 		sg->final = 1;
731c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
732c3e85bdcSAkhil Goyal 	} else {
733c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, start_addr + sym->auth.data.offset);
734c3e85bdcSAkhil Goyal 		sg->length = sym->auth.data.length;
735c3e85bdcSAkhil Goyal 		sg->final = 1;
736c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
737c3e85bdcSAkhil Goyal 	}
738c3e85bdcSAkhil Goyal 
739c3e85bdcSAkhil Goyal 	return cf;
740c3e85bdcSAkhil Goyal }
741c3e85bdcSAkhil Goyal 
742c3e85bdcSAkhil Goyal static inline struct dpaa_sec_job *
743a74af788SAkhil Goyal build_cipher_only_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
744a74af788SAkhil Goyal {
745a74af788SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
746a74af788SAkhil Goyal 	struct dpaa_sec_job *cf;
747a74af788SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
748a74af788SAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
749a74af788SAkhil Goyal 	struct rte_mbuf *mbuf;
750a74af788SAkhil Goyal 	uint8_t req_segs;
751a74af788SAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
752a74af788SAkhil Goyal 			ses->iv.offset);
753a74af788SAkhil Goyal 
754a74af788SAkhil Goyal 	if (sym->m_dst) {
755a74af788SAkhil Goyal 		mbuf = sym->m_dst;
756a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 3;
757a74af788SAkhil Goyal 	} else {
758a74af788SAkhil Goyal 		mbuf = sym->m_src;
759a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs * 2 + 3;
760a74af788SAkhil Goyal 	}
761a74af788SAkhil Goyal 
762a74af788SAkhil Goyal 	if (req_segs > MAX_SG_ENTRIES) {
763a74af788SAkhil Goyal 		PMD_TX_LOG(ERR, "Cipher: Max sec segs supported is %d\n",
764a74af788SAkhil Goyal 								MAX_SG_ENTRIES);
765a74af788SAkhil Goyal 		return NULL;
766a74af788SAkhil Goyal 	}
767a74af788SAkhil Goyal 
768a74af788SAkhil Goyal 	ctx = dpaa_sec_alloc_ctx(ses);
769a74af788SAkhil Goyal 	if (!ctx)
770a74af788SAkhil Goyal 		return NULL;
771a74af788SAkhil Goyal 
772a74af788SAkhil Goyal 	cf = &ctx->job;
773a74af788SAkhil Goyal 	ctx->op = op;
774a74af788SAkhil Goyal 
775a74af788SAkhil Goyal 	/* output */
776a74af788SAkhil Goyal 	out_sg = &cf->sg[0];
777a74af788SAkhil Goyal 	out_sg->extension = 1;
778a74af788SAkhil Goyal 	out_sg->length = sym->cipher.data.length;
779a74af788SAkhil Goyal 	qm_sg_entry_set64(out_sg, dpaa_mem_vtop_ctx(ctx, &cf->sg[2]));
780a74af788SAkhil Goyal 	cpu_to_hw_sg(out_sg);
781a74af788SAkhil Goyal 
782a74af788SAkhil Goyal 	/* 1st seg */
783a74af788SAkhil Goyal 	sg = &cf->sg[2];
784a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
785a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->cipher.data.offset;
786a74af788SAkhil Goyal 	sg->offset = sym->cipher.data.offset;
787a74af788SAkhil Goyal 
788a74af788SAkhil Goyal 	/* Successive segs */
789a74af788SAkhil Goyal 	mbuf = mbuf->next;
790a74af788SAkhil Goyal 	while (mbuf) {
791a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
792a74af788SAkhil Goyal 		sg++;
793a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
794a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
795a74af788SAkhil Goyal 		mbuf = mbuf->next;
796a74af788SAkhil Goyal 	}
797a74af788SAkhil Goyal 	sg->final = 1;
798a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
799a74af788SAkhil Goyal 
800a74af788SAkhil Goyal 	/* input */
801a74af788SAkhil Goyal 	mbuf = sym->m_src;
802a74af788SAkhil Goyal 	in_sg = &cf->sg[1];
803a74af788SAkhil Goyal 	in_sg->extension = 1;
804a74af788SAkhil Goyal 	in_sg->final = 1;
805a74af788SAkhil Goyal 	in_sg->length = sym->cipher.data.length + ses->iv.length;
806a74af788SAkhil Goyal 
807a74af788SAkhil Goyal 	sg++;
808a74af788SAkhil Goyal 	qm_sg_entry_set64(in_sg, dpaa_mem_vtop_ctx(ctx, sg));
809a74af788SAkhil Goyal 	cpu_to_hw_sg(in_sg);
810a74af788SAkhil Goyal 
811a74af788SAkhil Goyal 	/* IV */
812a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
813a74af788SAkhil Goyal 	sg->length = ses->iv.length;
814a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
815a74af788SAkhil Goyal 
816a74af788SAkhil Goyal 	/* 1st seg */
817a74af788SAkhil Goyal 	sg++;
818a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
819a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->cipher.data.offset;
820a74af788SAkhil Goyal 	sg->offset = sym->cipher.data.offset;
821a74af788SAkhil Goyal 
822a74af788SAkhil Goyal 	/* Successive segs */
823a74af788SAkhil Goyal 	mbuf = mbuf->next;
824a74af788SAkhil Goyal 	while (mbuf) {
825a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
826a74af788SAkhil Goyal 		sg++;
827a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
828a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
829a74af788SAkhil Goyal 		mbuf = mbuf->next;
830a74af788SAkhil Goyal 	}
831a74af788SAkhil Goyal 	sg->final = 1;
832a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
833a74af788SAkhil Goyal 
834a74af788SAkhil Goyal 	return cf;
835a74af788SAkhil Goyal }
836a74af788SAkhil Goyal 
837a74af788SAkhil Goyal static inline struct dpaa_sec_job *
838c3e85bdcSAkhil Goyal build_cipher_only(struct rte_crypto_op *op, dpaa_sec_session *ses)
839c3e85bdcSAkhil Goyal {
840c3e85bdcSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
841c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
842c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
843c3e85bdcSAkhil Goyal 	struct qm_sg_entry *sg;
844c4509373SSantosh Shukla 	rte_iova_t src_start_addr, dst_start_addr;
845c3e85bdcSAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
846c3e85bdcSAkhil Goyal 			ses->iv.offset);
847c3e85bdcSAkhil Goyal 
848c3e85bdcSAkhil Goyal 	ctx = dpaa_sec_alloc_ctx(ses);
849c3e85bdcSAkhil Goyal 	if (!ctx)
850c3e85bdcSAkhil Goyal 		return NULL;
851c3e85bdcSAkhil Goyal 
852c3e85bdcSAkhil Goyal 	cf = &ctx->job;
853c3e85bdcSAkhil Goyal 	ctx->op = op;
854a389434eSAlok Makhariya 
855bfa9a8a4SThomas Monjalon 	src_start_addr = rte_pktmbuf_iova(sym->m_src);
856a389434eSAlok Makhariya 
857a389434eSAlok Makhariya 	if (sym->m_dst)
858bfa9a8a4SThomas Monjalon 		dst_start_addr = rte_pktmbuf_iova(sym->m_dst);
859a389434eSAlok Makhariya 	else
860a389434eSAlok Makhariya 		dst_start_addr = src_start_addr;
861c3e85bdcSAkhil Goyal 
862c3e85bdcSAkhil Goyal 	/* output */
863c3e85bdcSAkhil Goyal 	sg = &cf->sg[0];
864a389434eSAlok Makhariya 	qm_sg_entry_set64(sg, dst_start_addr + sym->cipher.data.offset);
865c3e85bdcSAkhil Goyal 	sg->length = sym->cipher.data.length + ses->iv.length;
866c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
867c3e85bdcSAkhil Goyal 
868c3e85bdcSAkhil Goyal 	/* input */
869c3e85bdcSAkhil Goyal 	sg = &cf->sg[1];
870c3e85bdcSAkhil Goyal 
871c3e85bdcSAkhil Goyal 	/* need to extend the input to a compound frame */
872c3e85bdcSAkhil Goyal 	sg->extension = 1;
873c3e85bdcSAkhil Goyal 	sg->final = 1;
874c3e85bdcSAkhil Goyal 	sg->length = sym->cipher.data.length + ses->iv.length;
875fcf67029SHemant Agrawal 	qm_sg_entry_set64(sg, dpaa_mem_vtop_ctx(ctx, &cf->sg[2]));
876c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
877c3e85bdcSAkhil Goyal 
878c3e85bdcSAkhil Goyal 	sg = &cf->sg[2];
879c3e85bdcSAkhil Goyal 	qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
880c3e85bdcSAkhil Goyal 	sg->length = ses->iv.length;
881c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
882c3e85bdcSAkhil Goyal 
883c3e85bdcSAkhil Goyal 	sg++;
884a389434eSAlok Makhariya 	qm_sg_entry_set64(sg, src_start_addr + sym->cipher.data.offset);
885c3e85bdcSAkhil Goyal 	sg->length = sym->cipher.data.length;
886c3e85bdcSAkhil Goyal 	sg->final = 1;
887c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
888c3e85bdcSAkhil Goyal 
889c3e85bdcSAkhil Goyal 	return cf;
890c3e85bdcSAkhil Goyal }
891c3e85bdcSAkhil Goyal 
892c3e85bdcSAkhil Goyal static inline struct dpaa_sec_job *
893a74af788SAkhil Goyal build_cipher_auth_gcm_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
894a74af788SAkhil Goyal {
895a74af788SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
896a74af788SAkhil Goyal 	struct dpaa_sec_job *cf;
897a74af788SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
898a74af788SAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
899a74af788SAkhil Goyal 	struct rte_mbuf *mbuf;
900a74af788SAkhil Goyal 	uint8_t req_segs;
901a74af788SAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
902a74af788SAkhil Goyal 			ses->iv.offset);
903a74af788SAkhil Goyal 
904a74af788SAkhil Goyal 	if (sym->m_dst) {
905a74af788SAkhil Goyal 		mbuf = sym->m_dst;
906a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 4;
907a74af788SAkhil Goyal 	} else {
908a74af788SAkhil Goyal 		mbuf = sym->m_src;
909a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs * 2 + 4;
910a74af788SAkhil Goyal 	}
911a74af788SAkhil Goyal 
912a74af788SAkhil Goyal 	if (ses->auth_only_len)
913a74af788SAkhil Goyal 		req_segs++;
914a74af788SAkhil Goyal 
915a74af788SAkhil Goyal 	if (req_segs > MAX_SG_ENTRIES) {
916a74af788SAkhil Goyal 		PMD_TX_LOG(ERR, "AEAD: Max sec segs supported is %d\n",
917a74af788SAkhil Goyal 				MAX_SG_ENTRIES);
918a74af788SAkhil Goyal 		return NULL;
919a74af788SAkhil Goyal 	}
920a74af788SAkhil Goyal 
921a74af788SAkhil Goyal 	ctx = dpaa_sec_alloc_ctx(ses);
922a74af788SAkhil Goyal 	if (!ctx)
923a74af788SAkhil Goyal 		return NULL;
924a74af788SAkhil Goyal 
925a74af788SAkhil Goyal 	cf = &ctx->job;
926a74af788SAkhil Goyal 	ctx->op = op;
927a74af788SAkhil Goyal 
928a74af788SAkhil Goyal 	rte_prefetch0(cf->sg);
929a74af788SAkhil Goyal 
930a74af788SAkhil Goyal 	/* output */
931a74af788SAkhil Goyal 	out_sg = &cf->sg[0];
932a74af788SAkhil Goyal 	out_sg->extension = 1;
933a74af788SAkhil Goyal 	if (is_encode(ses))
934a74af788SAkhil Goyal 		out_sg->length = sym->aead.data.length + ses->auth_only_len
935a74af788SAkhil Goyal 						+ ses->digest_length;
936a74af788SAkhil Goyal 	else
937a74af788SAkhil Goyal 		out_sg->length = sym->aead.data.length + ses->auth_only_len;
938a74af788SAkhil Goyal 
939a74af788SAkhil Goyal 	/* output sg entries */
940a74af788SAkhil Goyal 	sg = &cf->sg[2];
941a74af788SAkhil Goyal 	qm_sg_entry_set64(out_sg, dpaa_mem_vtop_ctx(ctx, sg));
942a74af788SAkhil Goyal 	cpu_to_hw_sg(out_sg);
943a74af788SAkhil Goyal 
944a74af788SAkhil Goyal 	/* 1st seg */
945a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
946a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->aead.data.offset +
947a74af788SAkhil Goyal 					ses->auth_only_len;
948a74af788SAkhil Goyal 	sg->offset = sym->aead.data.offset - ses->auth_only_len;
949a74af788SAkhil Goyal 
950a74af788SAkhil Goyal 	/* Successive segs */
951a74af788SAkhil Goyal 	mbuf = mbuf->next;
952a74af788SAkhil Goyal 	while (mbuf) {
953a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
954a74af788SAkhil Goyal 		sg++;
955a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
956a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
957a74af788SAkhil Goyal 		mbuf = mbuf->next;
958a74af788SAkhil Goyal 	}
959a74af788SAkhil Goyal 	sg->length -= ses->digest_length;
960a74af788SAkhil Goyal 
961a74af788SAkhil Goyal 	if (is_encode(ses)) {
962a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
963a74af788SAkhil Goyal 		/* set auth output */
964a74af788SAkhil Goyal 		sg++;
965a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, sym->aead.digest.phys_addr);
966a74af788SAkhil Goyal 		sg->length = ses->digest_length;
967a74af788SAkhil Goyal 	}
968a74af788SAkhil Goyal 	sg->final = 1;
969a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
970a74af788SAkhil Goyal 
971a74af788SAkhil Goyal 	/* input */
972a74af788SAkhil Goyal 	mbuf = sym->m_src;
973a74af788SAkhil Goyal 	in_sg = &cf->sg[1];
974a74af788SAkhil Goyal 	in_sg->extension = 1;
975a74af788SAkhil Goyal 	in_sg->final = 1;
976a74af788SAkhil Goyal 	if (is_encode(ses))
977a74af788SAkhil Goyal 		in_sg->length = ses->iv.length + sym->aead.data.length
978a74af788SAkhil Goyal 							+ ses->auth_only_len;
979a74af788SAkhil Goyal 	else
980a74af788SAkhil Goyal 		in_sg->length = ses->iv.length + sym->aead.data.length
981a74af788SAkhil Goyal 				+ ses->auth_only_len + ses->digest_length;
982a74af788SAkhil Goyal 
983a74af788SAkhil Goyal 	/* input sg entries */
984a74af788SAkhil Goyal 	sg++;
985a74af788SAkhil Goyal 	qm_sg_entry_set64(in_sg, dpaa_mem_vtop_ctx(ctx, sg));
986a74af788SAkhil Goyal 	cpu_to_hw_sg(in_sg);
987a74af788SAkhil Goyal 
988a74af788SAkhil Goyal 	/* 1st seg IV */
989a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
990a74af788SAkhil Goyal 	sg->length = ses->iv.length;
991a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
992a74af788SAkhil Goyal 
993a74af788SAkhil Goyal 	/* 2nd seg auth only */
994a74af788SAkhil Goyal 	if (ses->auth_only_len) {
995a74af788SAkhil Goyal 		sg++;
996a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, dpaa_mem_vtop(sym->aead.aad.data));
997a74af788SAkhil Goyal 		sg->length = ses->auth_only_len;
998a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
999a74af788SAkhil Goyal 	}
1000a74af788SAkhil Goyal 
1001a74af788SAkhil Goyal 	/* 3rd seg */
1002a74af788SAkhil Goyal 	sg++;
1003a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1004a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->aead.data.offset;
1005a74af788SAkhil Goyal 	sg->offset = sym->aead.data.offset;
1006a74af788SAkhil Goyal 
1007a74af788SAkhil Goyal 	/* Successive segs */
1008a74af788SAkhil Goyal 	mbuf = mbuf->next;
1009a74af788SAkhil Goyal 	while (mbuf) {
1010a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1011a74af788SAkhil Goyal 		sg++;
1012a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1013a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1014a74af788SAkhil Goyal 		mbuf = mbuf->next;
1015a74af788SAkhil Goyal 	}
1016a74af788SAkhil Goyal 
1017a74af788SAkhil Goyal 	if (is_decode(ses)) {
1018a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1019a74af788SAkhil Goyal 		sg++;
1020a74af788SAkhil Goyal 		memcpy(ctx->digest, sym->aead.digest.data,
1021a74af788SAkhil Goyal 			ses->digest_length);
1022a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, dpaa_mem_vtop_ctx(ctx, ctx->digest));
1023a74af788SAkhil Goyal 		sg->length = ses->digest_length;
1024a74af788SAkhil Goyal 	}
1025a74af788SAkhil Goyal 	sg->final = 1;
1026a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1027a74af788SAkhil Goyal 
1028a74af788SAkhil Goyal 	return cf;
1029a74af788SAkhil Goyal }
1030a74af788SAkhil Goyal 
1031a74af788SAkhil Goyal static inline struct dpaa_sec_job *
1032c3e85bdcSAkhil Goyal build_cipher_auth_gcm(struct rte_crypto_op *op, dpaa_sec_session *ses)
1033c3e85bdcSAkhil Goyal {
1034c3e85bdcSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1035c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
1036c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1037c3e85bdcSAkhil Goyal 	struct qm_sg_entry *sg;
1038c3e85bdcSAkhil Goyal 	uint32_t length = 0;
1039c4509373SSantosh Shukla 	rte_iova_t src_start_addr, dst_start_addr;
1040c3e85bdcSAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1041c3e85bdcSAkhil Goyal 			ses->iv.offset);
1042c3e85bdcSAkhil Goyal 
1043116ff44aSHemant Agrawal 	src_start_addr = sym->m_src->buf_iova + sym->m_src->data_off;
1044a389434eSAlok Makhariya 
1045a389434eSAlok Makhariya 	if (sym->m_dst)
1046116ff44aSHemant Agrawal 		dst_start_addr = sym->m_dst->buf_iova + sym->m_dst->data_off;
1047a389434eSAlok Makhariya 	else
1048a389434eSAlok Makhariya 		dst_start_addr = src_start_addr;
1049c3e85bdcSAkhil Goyal 
1050c3e85bdcSAkhil Goyal 	ctx = dpaa_sec_alloc_ctx(ses);
1051c3e85bdcSAkhil Goyal 	if (!ctx)
1052c3e85bdcSAkhil Goyal 		return NULL;
1053c3e85bdcSAkhil Goyal 
1054c3e85bdcSAkhil Goyal 	cf = &ctx->job;
1055c3e85bdcSAkhil Goyal 	ctx->op = op;
1056c3e85bdcSAkhil Goyal 
1057c3e85bdcSAkhil Goyal 	/* input */
1058c3e85bdcSAkhil Goyal 	rte_prefetch0(cf->sg);
1059c3e85bdcSAkhil Goyal 	sg = &cf->sg[2];
1060fcf67029SHemant Agrawal 	qm_sg_entry_set64(&cf->sg[1], dpaa_mem_vtop_ctx(ctx, sg));
1061c3e85bdcSAkhil Goyal 	if (is_encode(ses)) {
1062c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1063c3e85bdcSAkhil Goyal 		sg->length = ses->iv.length;
1064c3e85bdcSAkhil Goyal 		length += sg->length;
1065c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1066c3e85bdcSAkhil Goyal 
1067c3e85bdcSAkhil Goyal 		sg++;
1068c3e85bdcSAkhil Goyal 		if (ses->auth_only_len) {
1069c3e85bdcSAkhil Goyal 			qm_sg_entry_set64(sg,
1070c3e85bdcSAkhil Goyal 					  dpaa_mem_vtop(sym->aead.aad.data));
1071c3e85bdcSAkhil Goyal 			sg->length = ses->auth_only_len;
1072c3e85bdcSAkhil Goyal 			length += sg->length;
1073c3e85bdcSAkhil Goyal 			cpu_to_hw_sg(sg);
1074c3e85bdcSAkhil Goyal 			sg++;
1075c3e85bdcSAkhil Goyal 		}
1076a389434eSAlok Makhariya 		qm_sg_entry_set64(sg, src_start_addr + sym->aead.data.offset);
1077c3e85bdcSAkhil Goyal 		sg->length = sym->aead.data.length;
1078c3e85bdcSAkhil Goyal 		length += sg->length;
1079c3e85bdcSAkhil Goyal 		sg->final = 1;
1080c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1081c3e85bdcSAkhil Goyal 	} else {
1082c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1083c3e85bdcSAkhil Goyal 		sg->length = ses->iv.length;
1084c3e85bdcSAkhil Goyal 		length += sg->length;
1085c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1086c3e85bdcSAkhil Goyal 
1087c3e85bdcSAkhil Goyal 		sg++;
1088c3e85bdcSAkhil Goyal 		if (ses->auth_only_len) {
1089c3e85bdcSAkhil Goyal 			qm_sg_entry_set64(sg,
1090c3e85bdcSAkhil Goyal 					  dpaa_mem_vtop(sym->aead.aad.data));
1091c3e85bdcSAkhil Goyal 			sg->length = ses->auth_only_len;
1092c3e85bdcSAkhil Goyal 			length += sg->length;
1093c3e85bdcSAkhil Goyal 			cpu_to_hw_sg(sg);
1094c3e85bdcSAkhil Goyal 			sg++;
1095c3e85bdcSAkhil Goyal 		}
1096a389434eSAlok Makhariya 		qm_sg_entry_set64(sg, src_start_addr + sym->aead.data.offset);
1097c3e85bdcSAkhil Goyal 		sg->length = sym->aead.data.length;
1098c3e85bdcSAkhil Goyal 		length += sg->length;
1099c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1100c3e85bdcSAkhil Goyal 
1101c3e85bdcSAkhil Goyal 		memcpy(ctx->digest, sym->aead.digest.data,
1102c3e85bdcSAkhil Goyal 		       ses->digest_length);
1103c3e85bdcSAkhil Goyal 		sg++;
1104c3e85bdcSAkhil Goyal 
1105fcf67029SHemant Agrawal 		qm_sg_entry_set64(sg, dpaa_mem_vtop_ctx(ctx, ctx->digest));
1106c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
1107c3e85bdcSAkhil Goyal 		length += sg->length;
1108c3e85bdcSAkhil Goyal 		sg->final = 1;
1109c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1110c3e85bdcSAkhil Goyal 	}
1111c3e85bdcSAkhil Goyal 	/* input compound frame */
1112c3e85bdcSAkhil Goyal 	cf->sg[1].length = length;
1113c3e85bdcSAkhil Goyal 	cf->sg[1].extension = 1;
1114c3e85bdcSAkhil Goyal 	cf->sg[1].final = 1;
1115c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(&cf->sg[1]);
1116c3e85bdcSAkhil Goyal 
1117c3e85bdcSAkhil Goyal 	/* output */
1118c3e85bdcSAkhil Goyal 	sg++;
1119fcf67029SHemant Agrawal 	qm_sg_entry_set64(&cf->sg[0], dpaa_mem_vtop_ctx(ctx, sg));
1120c3e85bdcSAkhil Goyal 	qm_sg_entry_set64(sg,
1121a389434eSAlok Makhariya 		dst_start_addr + sym->aead.data.offset - ses->auth_only_len);
1122c3e85bdcSAkhil Goyal 	sg->length = sym->aead.data.length + ses->auth_only_len;
1123c3e85bdcSAkhil Goyal 	length = sg->length;
1124c3e85bdcSAkhil Goyal 	if (is_encode(ses)) {
1125c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1126c3e85bdcSAkhil Goyal 		/* set auth output */
1127c3e85bdcSAkhil Goyal 		sg++;
1128c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, sym->aead.digest.phys_addr);
1129c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
1130c3e85bdcSAkhil Goyal 		length += sg->length;
1131c3e85bdcSAkhil Goyal 	}
1132c3e85bdcSAkhil Goyal 	sg->final = 1;
1133c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1134c3e85bdcSAkhil Goyal 
1135c3e85bdcSAkhil Goyal 	/* output compound frame */
1136c3e85bdcSAkhil Goyal 	cf->sg[0].length = length;
1137c3e85bdcSAkhil Goyal 	cf->sg[0].extension = 1;
1138c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(&cf->sg[0]);
1139c3e85bdcSAkhil Goyal 
1140c3e85bdcSAkhil Goyal 	return cf;
1141c3e85bdcSAkhil Goyal }
1142c3e85bdcSAkhil Goyal 
1143c3e85bdcSAkhil Goyal static inline struct dpaa_sec_job *
1144a74af788SAkhil Goyal build_cipher_auth_sg(struct rte_crypto_op *op, dpaa_sec_session *ses)
1145a74af788SAkhil Goyal {
1146a74af788SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1147a74af788SAkhil Goyal 	struct dpaa_sec_job *cf;
1148a74af788SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1149a74af788SAkhil Goyal 	struct qm_sg_entry *sg, *out_sg, *in_sg;
1150a74af788SAkhil Goyal 	struct rte_mbuf *mbuf;
1151a74af788SAkhil Goyal 	uint8_t req_segs;
1152a74af788SAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1153a74af788SAkhil Goyal 			ses->iv.offset);
1154a74af788SAkhil Goyal 
1155a74af788SAkhil Goyal 	if (sym->m_dst) {
1156a74af788SAkhil Goyal 		mbuf = sym->m_dst;
1157a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs + sym->m_src->nb_segs + 4;
1158a74af788SAkhil Goyal 	} else {
1159a74af788SAkhil Goyal 		mbuf = sym->m_src;
1160a74af788SAkhil Goyal 		req_segs = mbuf->nb_segs * 2 + 4;
1161a74af788SAkhil Goyal 	}
1162a74af788SAkhil Goyal 
1163a74af788SAkhil Goyal 	if (req_segs > MAX_SG_ENTRIES) {
1164a74af788SAkhil Goyal 		PMD_TX_LOG(ERR, "Cipher-Auth: Max sec segs supported is %d\n",
1165a74af788SAkhil Goyal 				MAX_SG_ENTRIES);
1166a74af788SAkhil Goyal 		return NULL;
1167a74af788SAkhil Goyal 	}
1168a74af788SAkhil Goyal 
1169a74af788SAkhil Goyal 	ctx = dpaa_sec_alloc_ctx(ses);
1170a74af788SAkhil Goyal 	if (!ctx)
1171a74af788SAkhil Goyal 		return NULL;
1172a74af788SAkhil Goyal 
1173a74af788SAkhil Goyal 	cf = &ctx->job;
1174a74af788SAkhil Goyal 	ctx->op = op;
1175a74af788SAkhil Goyal 
1176a74af788SAkhil Goyal 	rte_prefetch0(cf->sg);
1177a74af788SAkhil Goyal 
1178a74af788SAkhil Goyal 	/* output */
1179a74af788SAkhil Goyal 	out_sg = &cf->sg[0];
1180a74af788SAkhil Goyal 	out_sg->extension = 1;
1181a74af788SAkhil Goyal 	if (is_encode(ses))
1182a74af788SAkhil Goyal 		out_sg->length = sym->auth.data.length + ses->digest_length;
1183a74af788SAkhil Goyal 	else
1184a74af788SAkhil Goyal 		out_sg->length = sym->auth.data.length;
1185a74af788SAkhil Goyal 
1186a74af788SAkhil Goyal 	/* output sg entries */
1187a74af788SAkhil Goyal 	sg = &cf->sg[2];
1188a74af788SAkhil Goyal 	qm_sg_entry_set64(out_sg, dpaa_mem_vtop_ctx(ctx, sg));
1189a74af788SAkhil Goyal 	cpu_to_hw_sg(out_sg);
1190a74af788SAkhil Goyal 
1191a74af788SAkhil Goyal 	/* 1st seg */
1192a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1193a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->auth.data.offset;
1194a74af788SAkhil Goyal 	sg->offset = sym->auth.data.offset;
1195a74af788SAkhil Goyal 
1196a74af788SAkhil Goyal 	/* Successive segs */
1197a74af788SAkhil Goyal 	mbuf = mbuf->next;
1198a74af788SAkhil Goyal 	while (mbuf) {
1199a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1200a74af788SAkhil Goyal 		sg++;
1201a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1202a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1203a74af788SAkhil Goyal 		mbuf = mbuf->next;
1204a74af788SAkhil Goyal 	}
1205a74af788SAkhil Goyal 	sg->length -= ses->digest_length;
1206a74af788SAkhil Goyal 
1207a74af788SAkhil Goyal 	if (is_encode(ses)) {
1208a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1209a74af788SAkhil Goyal 		/* set auth output */
1210a74af788SAkhil Goyal 		sg++;
1211a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
1212a74af788SAkhil Goyal 		sg->length = ses->digest_length;
1213a74af788SAkhil Goyal 	}
1214a74af788SAkhil Goyal 	sg->final = 1;
1215a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1216a74af788SAkhil Goyal 
1217a74af788SAkhil Goyal 	/* input */
1218a74af788SAkhil Goyal 	mbuf = sym->m_src;
1219a74af788SAkhil Goyal 	in_sg = &cf->sg[1];
1220a74af788SAkhil Goyal 	in_sg->extension = 1;
1221a74af788SAkhil Goyal 	in_sg->final = 1;
1222a74af788SAkhil Goyal 	if (is_encode(ses))
1223a74af788SAkhil Goyal 		in_sg->length = ses->iv.length + sym->auth.data.length;
1224a74af788SAkhil Goyal 	else
1225a74af788SAkhil Goyal 		in_sg->length = ses->iv.length + sym->auth.data.length
1226a74af788SAkhil Goyal 						+ ses->digest_length;
1227a74af788SAkhil Goyal 
1228a74af788SAkhil Goyal 	/* input sg entries */
1229a74af788SAkhil Goyal 	sg++;
1230a74af788SAkhil Goyal 	qm_sg_entry_set64(in_sg, dpaa_mem_vtop_ctx(ctx, sg));
1231a74af788SAkhil Goyal 	cpu_to_hw_sg(in_sg);
1232a74af788SAkhil Goyal 
1233a74af788SAkhil Goyal 	/* 1st seg IV */
1234a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1235a74af788SAkhil Goyal 	sg->length = ses->iv.length;
1236a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1237a74af788SAkhil Goyal 
1238a74af788SAkhil Goyal 	/* 2nd seg */
1239a74af788SAkhil Goyal 	sg++;
1240a74af788SAkhil Goyal 	qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1241a74af788SAkhil Goyal 	sg->length = mbuf->data_len - sym->auth.data.offset;
1242a74af788SAkhil Goyal 	sg->offset = sym->auth.data.offset;
1243a74af788SAkhil Goyal 
1244a74af788SAkhil Goyal 	/* Successive segs */
1245a74af788SAkhil Goyal 	mbuf = mbuf->next;
1246a74af788SAkhil Goyal 	while (mbuf) {
1247a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1248a74af788SAkhil Goyal 		sg++;
1249a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, rte_pktmbuf_mtophys(mbuf));
1250a74af788SAkhil Goyal 		sg->length = mbuf->data_len;
1251a74af788SAkhil Goyal 		mbuf = mbuf->next;
1252a74af788SAkhil Goyal 	}
1253a74af788SAkhil Goyal 
1254a74af788SAkhil Goyal 	sg->length -= ses->digest_length;
1255a74af788SAkhil Goyal 	if (is_decode(ses)) {
1256a74af788SAkhil Goyal 		cpu_to_hw_sg(sg);
1257a74af788SAkhil Goyal 		sg++;
1258a74af788SAkhil Goyal 		memcpy(ctx->digest, sym->auth.digest.data,
1259a74af788SAkhil Goyal 			ses->digest_length);
1260a74af788SAkhil Goyal 		qm_sg_entry_set64(sg, dpaa_mem_vtop_ctx(ctx, ctx->digest));
1261a74af788SAkhil Goyal 		sg->length = ses->digest_length;
1262a74af788SAkhil Goyal 	}
1263a74af788SAkhil Goyal 	sg->final = 1;
1264a74af788SAkhil Goyal 	cpu_to_hw_sg(sg);
1265a74af788SAkhil Goyal 
1266a74af788SAkhil Goyal 	return cf;
1267a74af788SAkhil Goyal }
1268a74af788SAkhil Goyal 
1269a74af788SAkhil Goyal static inline struct dpaa_sec_job *
1270c3e85bdcSAkhil Goyal build_cipher_auth(struct rte_crypto_op *op, dpaa_sec_session *ses)
1271c3e85bdcSAkhil Goyal {
1272c3e85bdcSAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
1273c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
1274c3e85bdcSAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
1275c3e85bdcSAkhil Goyal 	struct qm_sg_entry *sg;
1276c4509373SSantosh Shukla 	rte_iova_t src_start_addr, dst_start_addr;
1277c3e85bdcSAkhil Goyal 	uint32_t length = 0;
1278c3e85bdcSAkhil Goyal 	uint8_t *IV_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
1279c3e85bdcSAkhil Goyal 			ses->iv.offset);
1280c3e85bdcSAkhil Goyal 
1281455da545SSantosh Shukla 	src_start_addr = sym->m_src->buf_iova + sym->m_src->data_off;
1282a389434eSAlok Makhariya 	if (sym->m_dst)
1283455da545SSantosh Shukla 		dst_start_addr = sym->m_dst->buf_iova + sym->m_dst->data_off;
1284a389434eSAlok Makhariya 	else
1285a389434eSAlok Makhariya 		dst_start_addr = src_start_addr;
1286c3e85bdcSAkhil Goyal 
1287c3e85bdcSAkhil Goyal 	ctx = dpaa_sec_alloc_ctx(ses);
1288c3e85bdcSAkhil Goyal 	if (!ctx)
1289c3e85bdcSAkhil Goyal 		return NULL;
1290c3e85bdcSAkhil Goyal 
1291c3e85bdcSAkhil Goyal 	cf = &ctx->job;
1292c3e85bdcSAkhil Goyal 	ctx->op = op;
1293c3e85bdcSAkhil Goyal 
1294c3e85bdcSAkhil Goyal 	/* input */
1295c3e85bdcSAkhil Goyal 	rte_prefetch0(cf->sg);
1296c3e85bdcSAkhil Goyal 	sg = &cf->sg[2];
1297fcf67029SHemant Agrawal 	qm_sg_entry_set64(&cf->sg[1], dpaa_mem_vtop_ctx(ctx, sg));
1298c3e85bdcSAkhil Goyal 	if (is_encode(ses)) {
1299c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1300c3e85bdcSAkhil Goyal 		sg->length = ses->iv.length;
1301c3e85bdcSAkhil Goyal 		length += sg->length;
1302c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1303c3e85bdcSAkhil Goyal 
1304c3e85bdcSAkhil Goyal 		sg++;
1305a389434eSAlok Makhariya 		qm_sg_entry_set64(sg, src_start_addr + sym->auth.data.offset);
1306c3e85bdcSAkhil Goyal 		sg->length = sym->auth.data.length;
1307c3e85bdcSAkhil Goyal 		length += sg->length;
1308c3e85bdcSAkhil Goyal 		sg->final = 1;
1309c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1310c3e85bdcSAkhil Goyal 	} else {
1311c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, dpaa_mem_vtop(IV_ptr));
1312c3e85bdcSAkhil Goyal 		sg->length = ses->iv.length;
1313c3e85bdcSAkhil Goyal 		length += sg->length;
1314c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1315c3e85bdcSAkhil Goyal 
1316c3e85bdcSAkhil Goyal 		sg++;
1317c3e85bdcSAkhil Goyal 
1318a389434eSAlok Makhariya 		qm_sg_entry_set64(sg, src_start_addr + sym->auth.data.offset);
1319c3e85bdcSAkhil Goyal 		sg->length = sym->auth.data.length;
1320c3e85bdcSAkhil Goyal 		length += sg->length;
1321c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1322c3e85bdcSAkhil Goyal 
1323c3e85bdcSAkhil Goyal 		memcpy(ctx->digest, sym->auth.digest.data,
1324c3e85bdcSAkhil Goyal 		       ses->digest_length);
1325c3e85bdcSAkhil Goyal 		sg++;
1326c3e85bdcSAkhil Goyal 
1327fcf67029SHemant Agrawal 		qm_sg_entry_set64(sg, dpaa_mem_vtop_ctx(ctx, ctx->digest));
1328c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
1329c3e85bdcSAkhil Goyal 		length += sg->length;
1330c3e85bdcSAkhil Goyal 		sg->final = 1;
1331c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1332c3e85bdcSAkhil Goyal 	}
1333c3e85bdcSAkhil Goyal 	/* input compound frame */
1334c3e85bdcSAkhil Goyal 	cf->sg[1].length = length;
1335c3e85bdcSAkhil Goyal 	cf->sg[1].extension = 1;
1336c3e85bdcSAkhil Goyal 	cf->sg[1].final = 1;
1337c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(&cf->sg[1]);
1338c3e85bdcSAkhil Goyal 
1339c3e85bdcSAkhil Goyal 	/* output */
1340c3e85bdcSAkhil Goyal 	sg++;
1341fcf67029SHemant Agrawal 	qm_sg_entry_set64(&cf->sg[0], dpaa_mem_vtop_ctx(ctx, sg));
1342a389434eSAlok Makhariya 	qm_sg_entry_set64(sg, dst_start_addr + sym->cipher.data.offset);
1343c3e85bdcSAkhil Goyal 	sg->length = sym->cipher.data.length;
1344c3e85bdcSAkhil Goyal 	length = sg->length;
1345c3e85bdcSAkhil Goyal 	if (is_encode(ses)) {
1346c3e85bdcSAkhil Goyal 		cpu_to_hw_sg(sg);
1347c3e85bdcSAkhil Goyal 		/* set auth output */
1348c3e85bdcSAkhil Goyal 		sg++;
1349c3e85bdcSAkhil Goyal 		qm_sg_entry_set64(sg, sym->auth.digest.phys_addr);
1350c3e85bdcSAkhil Goyal 		sg->length = ses->digest_length;
1351c3e85bdcSAkhil Goyal 		length += sg->length;
1352c3e85bdcSAkhil Goyal 	}
1353c3e85bdcSAkhil Goyal 	sg->final = 1;
1354c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(sg);
1355c3e85bdcSAkhil Goyal 
1356c3e85bdcSAkhil Goyal 	/* output compound frame */
1357c3e85bdcSAkhil Goyal 	cf->sg[0].length = length;
1358c3e85bdcSAkhil Goyal 	cf->sg[0].extension = 1;
1359c3e85bdcSAkhil Goyal 	cpu_to_hw_sg(&cf->sg[0]);
1360c3e85bdcSAkhil Goyal 
1361c3e85bdcSAkhil Goyal 	return cf;
1362c3e85bdcSAkhil Goyal }
1363c3e85bdcSAkhil Goyal 
13641f14d500SAkhil Goyal static inline struct dpaa_sec_job *
13651f14d500SAkhil Goyal build_proto(struct rte_crypto_op *op, dpaa_sec_session *ses)
13661f14d500SAkhil Goyal {
13671f14d500SAkhil Goyal 	struct rte_crypto_sym_op *sym = op->sym;
13681f14d500SAkhil Goyal 	struct dpaa_sec_job *cf;
13691f14d500SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
13701f14d500SAkhil Goyal 	struct qm_sg_entry *sg;
13711f14d500SAkhil Goyal 	phys_addr_t src_start_addr, dst_start_addr;
13721f14d500SAkhil Goyal 
13731f14d500SAkhil Goyal 	ctx = dpaa_sec_alloc_ctx(ses);
13741f14d500SAkhil Goyal 	if (!ctx)
13751f14d500SAkhil Goyal 		return NULL;
13761f14d500SAkhil Goyal 	cf = &ctx->job;
13771f14d500SAkhil Goyal 	ctx->op = op;
13781f14d500SAkhil Goyal 
13791f14d500SAkhil Goyal 	src_start_addr = rte_pktmbuf_mtophys(sym->m_src);
13801f14d500SAkhil Goyal 
13811f14d500SAkhil Goyal 	if (sym->m_dst)
13821f14d500SAkhil Goyal 		dst_start_addr = rte_pktmbuf_mtophys(sym->m_dst);
13831f14d500SAkhil Goyal 	else
13841f14d500SAkhil Goyal 		dst_start_addr = src_start_addr;
13851f14d500SAkhil Goyal 
13861f14d500SAkhil Goyal 	/* input */
13871f14d500SAkhil Goyal 	sg = &cf->sg[1];
13881f14d500SAkhil Goyal 	qm_sg_entry_set64(sg, src_start_addr);
13891f14d500SAkhil Goyal 	sg->length = sym->m_src->pkt_len;
13901f14d500SAkhil Goyal 	sg->final = 1;
13911f14d500SAkhil Goyal 	cpu_to_hw_sg(sg);
13921f14d500SAkhil Goyal 
13931f14d500SAkhil Goyal 	sym->m_src->packet_type &= ~RTE_PTYPE_L4_MASK;
13941f14d500SAkhil Goyal 	/* output */
13951f14d500SAkhil Goyal 	sg = &cf->sg[0];
13961f14d500SAkhil Goyal 	qm_sg_entry_set64(sg, dst_start_addr);
13971f14d500SAkhil Goyal 	sg->length = sym->m_src->buf_len - sym->m_src->data_off;
13981f14d500SAkhil Goyal 	cpu_to_hw_sg(sg);
13991f14d500SAkhil Goyal 
14001f14d500SAkhil Goyal 	return cf;
14011f14d500SAkhil Goyal }
14021f14d500SAkhil Goyal 
14039a984458SAkhil Goyal static uint16_t
14049a984458SAkhil Goyal dpaa_sec_enqueue_burst(void *qp, struct rte_crypto_op **ops,
14059a984458SAkhil Goyal 		       uint16_t nb_ops)
1406c3e85bdcSAkhil Goyal {
14079a984458SAkhil Goyal 	/* Function to transmit the frames to given device and queuepair */
14089a984458SAkhil Goyal 	uint32_t loop;
14099a984458SAkhil Goyal 	struct dpaa_sec_qp *dpaa_qp = (struct dpaa_sec_qp *)qp;
14109a984458SAkhil Goyal 	uint16_t num_tx = 0;
14119a984458SAkhil Goyal 	struct qm_fd fds[DPAA_SEC_BURST], *fd;
14129a984458SAkhil Goyal 	uint32_t frames_to_send;
14139a984458SAkhil Goyal 	struct rte_crypto_op *op;
1414c3e85bdcSAkhil Goyal 	struct dpaa_sec_job *cf;
1415c3e85bdcSAkhil Goyal 	dpaa_sec_session *ses;
14169a984458SAkhil Goyal 	struct dpaa_sec_op_ctx *ctx;
14179a984458SAkhil Goyal 	uint32_t auth_only_len;
14189a984458SAkhil Goyal 	struct qman_fq *inq[DPAA_SEC_BURST];
1419c3e85bdcSAkhil Goyal 
14209a984458SAkhil Goyal 	while (nb_ops) {
14219a984458SAkhil Goyal 		frames_to_send = (nb_ops > DPAA_SEC_BURST) ?
14229a984458SAkhil Goyal 				DPAA_SEC_BURST : nb_ops;
14239a984458SAkhil Goyal 		for (loop = 0; loop < frames_to_send; loop++) {
14249a984458SAkhil Goyal 			op = *(ops++);
14259a984458SAkhil Goyal 			switch (op->sess_type) {
14269a984458SAkhil Goyal 			case RTE_CRYPTO_OP_WITH_SESSION:
14279a984458SAkhil Goyal 				ses = (dpaa_sec_session *)
14289a984458SAkhil Goyal 					get_session_private_data(
14299a984458SAkhil Goyal 							op->sym->session,
14309a984458SAkhil Goyal 							cryptodev_driver_id);
14319a984458SAkhil Goyal 				break;
14329a984458SAkhil Goyal 			case RTE_CRYPTO_OP_SECURITY_SESSION:
14339a984458SAkhil Goyal 				ses = (dpaa_sec_session *)
14349a984458SAkhil Goyal 					get_sec_session_private_data(
14351f14d500SAkhil Goyal 							op->sym->sec_session);
14369a984458SAkhil Goyal 				break;
14379a984458SAkhil Goyal 			default:
14389a984458SAkhil Goyal 				PMD_TX_LOG(ERR,
14399a984458SAkhil Goyal 					"sessionless crypto op not supported");
14409a984458SAkhil Goyal 				frames_to_send = loop;
14419a984458SAkhil Goyal 				nb_ops = loop;
14429a984458SAkhil Goyal 				goto send_pkts;
14439a984458SAkhil Goyal 			}
1444e79416d1SHemant Agrawal 			if (unlikely(!ses->qp || ses->qp != qp)) {
14459a984458SAkhil Goyal 				PMD_INIT_LOG(DEBUG, "sess->qp - %p qp %p",
14469a984458SAkhil Goyal 						ses->qp, qp);
14479a984458SAkhil Goyal 				if (dpaa_sec_attach_sess_q(qp, ses)) {
14489a984458SAkhil Goyal 					frames_to_send = loop;
14499a984458SAkhil Goyal 					nb_ops = loop;
14509a984458SAkhil Goyal 					goto send_pkts;
14519a984458SAkhil Goyal 				}
1452c3e85bdcSAkhil Goyal 			}
1453c3e85bdcSAkhil Goyal 
14549a984458SAkhil Goyal 			auth_only_len = op->sym->auth.data.length -
14559a984458SAkhil Goyal 						op->sym->cipher.data.length;
1456a74af788SAkhil Goyal 			if (rte_pktmbuf_is_contiguous(op->sym->m_src)) {
1457c3e85bdcSAkhil Goyal 				if (is_auth_only(ses)) {
1458c3e85bdcSAkhil Goyal 					cf = build_auth_only(op, ses);
1459c3e85bdcSAkhil Goyal 				} else if (is_cipher_only(ses)) {
1460c3e85bdcSAkhil Goyal 					cf = build_cipher_only(op, ses);
1461c3e85bdcSAkhil Goyal 				} else if (is_aead(ses)) {
1462c3e85bdcSAkhil Goyal 					cf = build_cipher_auth_gcm(op, ses);
1463c3e85bdcSAkhil Goyal 					auth_only_len = ses->auth_only_len;
1464c3e85bdcSAkhil Goyal 				} else if (is_auth_cipher(ses)) {
1465c3e85bdcSAkhil Goyal 					cf = build_cipher_auth(op, ses);
14661f14d500SAkhil Goyal 				} else if (is_proto_ipsec(ses)) {
14671f14d500SAkhil Goyal 					cf = build_proto(op, ses);
1468c3e85bdcSAkhil Goyal 				} else {
1469c3e85bdcSAkhil Goyal 					PMD_TX_LOG(ERR, "not supported sec op");
14709a984458SAkhil Goyal 					frames_to_send = loop;
14719a984458SAkhil Goyal 					nb_ops = loop;
14729a984458SAkhil Goyal 					goto send_pkts;
1473c3e85bdcSAkhil Goyal 				}
1474a74af788SAkhil Goyal 			} else {
1475a74af788SAkhil Goyal 				if (is_auth_only(ses)) {
1476a74af788SAkhil Goyal 					cf = build_auth_only_sg(op, ses);
1477a74af788SAkhil Goyal 				} else if (is_cipher_only(ses)) {
1478a74af788SAkhil Goyal 					cf = build_cipher_only_sg(op, ses);
1479a74af788SAkhil Goyal 				} else if (is_aead(ses)) {
1480a74af788SAkhil Goyal 					cf = build_cipher_auth_gcm_sg(op, ses);
1481a74af788SAkhil Goyal 					auth_only_len = ses->auth_only_len;
1482a74af788SAkhil Goyal 				} else if (is_auth_cipher(ses)) {
1483a74af788SAkhil Goyal 					cf = build_cipher_auth_sg(op, ses);
1484a74af788SAkhil Goyal 				} else {
1485a74af788SAkhil Goyal 					PMD_TX_LOG(ERR, "not supported sec op");
1486a74af788SAkhil Goyal 					frames_to_send = loop;
1487a74af788SAkhil Goyal 					nb_ops = loop;
1488a74af788SAkhil Goyal 					goto send_pkts;
1489a74af788SAkhil Goyal 				}
1490a74af788SAkhil Goyal 			}
14919a984458SAkhil Goyal 			if (unlikely(!cf)) {
14929a984458SAkhil Goyal 				frames_to_send = loop;
14939a984458SAkhil Goyal 				nb_ops = loop;
14949a984458SAkhil Goyal 				goto send_pkts;
14959a984458SAkhil Goyal 			}
1496c3e85bdcSAkhil Goyal 
14979a984458SAkhil Goyal 			fd = &fds[loop];
14989a984458SAkhil Goyal 			inq[loop] = ses->inq;
14999a984458SAkhil Goyal 			fd->opaque_addr = 0;
15009a984458SAkhil Goyal 			fd->cmd = 0;
15019a984458SAkhil Goyal 			ctx = container_of(cf, struct dpaa_sec_op_ctx, job);
15029a984458SAkhil Goyal 			qm_fd_addr_set64(fd, dpaa_mem_vtop_ctx(ctx, cf->sg));
15039a984458SAkhil Goyal 			fd->_format1 = qm_fd_compound;
15049a984458SAkhil Goyal 			fd->length29 = 2 * sizeof(struct qm_sg_entry);
15059a984458SAkhil Goyal 			/* Auth_only_len is set as 0 in descriptor and it is
15069a984458SAkhil Goyal 			 * overwritten here in the fd.cmd which will update
15079a984458SAkhil Goyal 			 * the DPOVRD reg.
1508c3e85bdcSAkhil Goyal 			 */
1509c3e85bdcSAkhil Goyal 			if (auth_only_len)
15109a984458SAkhil Goyal 				fd->cmd = 0x80000000 | auth_only_len;
1511c3e85bdcSAkhil Goyal 
15129a984458SAkhil Goyal 		}
15139a984458SAkhil Goyal send_pkts:
15149a984458SAkhil Goyal 		loop = 0;
15159a984458SAkhil Goyal 		while (loop < frames_to_send) {
15169a984458SAkhil Goyal 			loop += qman_enqueue_multi_fq(&inq[loop], &fds[loop],
15179a984458SAkhil Goyal 					frames_to_send - loop);
15189a984458SAkhil Goyal 		}
15199a984458SAkhil Goyal 		nb_ops -= frames_to_send;
15209a984458SAkhil Goyal 		num_tx += frames_to_send;
1521c3e85bdcSAkhil Goyal 	}
1522c3e85bdcSAkhil Goyal 
1523c3e85bdcSAkhil Goyal 	dpaa_qp->tx_pkts += num_tx;
1524c3e85bdcSAkhil Goyal 	dpaa_qp->tx_errs += nb_ops - num_tx;
1525c3e85bdcSAkhil Goyal 
1526c3e85bdcSAkhil Goyal 	return num_tx;
1527c3e85bdcSAkhil Goyal }
1528c3e85bdcSAkhil Goyal 
1529c3e85bdcSAkhil Goyal static uint16_t
1530c3e85bdcSAkhil Goyal dpaa_sec_dequeue_burst(void *qp, struct rte_crypto_op **ops,
1531c3e85bdcSAkhil Goyal 		       uint16_t nb_ops)
1532c3e85bdcSAkhil Goyal {
1533c3e85bdcSAkhil Goyal 	uint16_t num_rx;
1534c3e85bdcSAkhil Goyal 	struct dpaa_sec_qp *dpaa_qp = (struct dpaa_sec_qp *)qp;
1535c3e85bdcSAkhil Goyal 
1536c3e85bdcSAkhil Goyal 	num_rx = dpaa_sec_deq(dpaa_qp, ops, nb_ops);
1537c3e85bdcSAkhil Goyal 
1538c3e85bdcSAkhil Goyal 	dpaa_qp->rx_pkts += num_rx;
1539c3e85bdcSAkhil Goyal 	dpaa_qp->rx_errs += nb_ops - num_rx;
1540c3e85bdcSAkhil Goyal 
1541c3e85bdcSAkhil Goyal 	PMD_RX_LOG(DEBUG, "SEC Received %d Packets\n", num_rx);
1542c3e85bdcSAkhil Goyal 
1543c3e85bdcSAkhil Goyal 	return num_rx;
1544c3e85bdcSAkhil Goyal }
1545c3e85bdcSAkhil Goyal 
1546c3e85bdcSAkhil Goyal /** Release queue pair */
1547c3e85bdcSAkhil Goyal static int
1548c3e85bdcSAkhil Goyal dpaa_sec_queue_pair_release(struct rte_cryptodev *dev,
1549c3e85bdcSAkhil Goyal 			    uint16_t qp_id)
1550c3e85bdcSAkhil Goyal {
1551c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals;
1552c3e85bdcSAkhil Goyal 	struct dpaa_sec_qp *qp = NULL;
1553c3e85bdcSAkhil Goyal 
1554c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
1555c3e85bdcSAkhil Goyal 
1556c3e85bdcSAkhil Goyal 	PMD_INIT_LOG(DEBUG, "dev =%p, queue =%d", dev, qp_id);
1557c3e85bdcSAkhil Goyal 
1558c3e85bdcSAkhil Goyal 	internals = dev->data->dev_private;
1559c3e85bdcSAkhil Goyal 	if (qp_id >= internals->max_nb_queue_pairs) {
1560c3e85bdcSAkhil Goyal 		PMD_INIT_LOG(ERR, "Max supported qpid %d",
1561c3e85bdcSAkhil Goyal 			     internals->max_nb_queue_pairs);
1562c3e85bdcSAkhil Goyal 		return -EINVAL;
1563c3e85bdcSAkhil Goyal 	}
1564c3e85bdcSAkhil Goyal 
1565c3e85bdcSAkhil Goyal 	qp = &internals->qps[qp_id];
1566c3e85bdcSAkhil Goyal 	qp->internals = NULL;
1567c3e85bdcSAkhil Goyal 	dev->data->queue_pairs[qp_id] = NULL;
1568c3e85bdcSAkhil Goyal 
1569c3e85bdcSAkhil Goyal 	return 0;
1570c3e85bdcSAkhil Goyal }
1571c3e85bdcSAkhil Goyal 
1572c3e85bdcSAkhil Goyal /** Setup a queue pair */
1573c3e85bdcSAkhil Goyal static int
1574c3e85bdcSAkhil Goyal dpaa_sec_queue_pair_setup(struct rte_cryptodev *dev, uint16_t qp_id,
1575c3e85bdcSAkhil Goyal 		__rte_unused const struct rte_cryptodev_qp_conf *qp_conf,
1576c3e85bdcSAkhil Goyal 		__rte_unused int socket_id,
1577c3e85bdcSAkhil Goyal 		__rte_unused struct rte_mempool *session_pool)
1578c3e85bdcSAkhil Goyal {
1579c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals;
1580c3e85bdcSAkhil Goyal 	struct dpaa_sec_qp *qp = NULL;
1581c3e85bdcSAkhil Goyal 
1582c3e85bdcSAkhil Goyal 	PMD_INIT_LOG(DEBUG, "dev =%p, queue =%d, conf =%p",
1583c3e85bdcSAkhil Goyal 		     dev, qp_id, qp_conf);
1584c3e85bdcSAkhil Goyal 
1585c3e85bdcSAkhil Goyal 	internals = dev->data->dev_private;
1586c3e85bdcSAkhil Goyal 	if (qp_id >= internals->max_nb_queue_pairs) {
1587c3e85bdcSAkhil Goyal 		PMD_INIT_LOG(ERR, "Max supported qpid %d",
1588c3e85bdcSAkhil Goyal 			     internals->max_nb_queue_pairs);
1589c3e85bdcSAkhil Goyal 		return -EINVAL;
1590c3e85bdcSAkhil Goyal 	}
1591c3e85bdcSAkhil Goyal 
1592c3e85bdcSAkhil Goyal 	qp = &internals->qps[qp_id];
1593c3e85bdcSAkhil Goyal 	qp->internals = internals;
1594c3e85bdcSAkhil Goyal 	dev->data->queue_pairs[qp_id] = qp;
1595c3e85bdcSAkhil Goyal 
1596c3e85bdcSAkhil Goyal 	return 0;
1597c3e85bdcSAkhil Goyal }
1598c3e85bdcSAkhil Goyal 
1599c3e85bdcSAkhil Goyal /** Start queue pair */
1600c3e85bdcSAkhil Goyal static int
1601c3e85bdcSAkhil Goyal dpaa_sec_queue_pair_start(__rte_unused struct rte_cryptodev *dev,
1602c3e85bdcSAkhil Goyal 			  __rte_unused uint16_t queue_pair_id)
1603c3e85bdcSAkhil Goyal {
1604c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
1605c3e85bdcSAkhil Goyal 
1606c3e85bdcSAkhil Goyal 	return 0;
1607c3e85bdcSAkhil Goyal }
1608c3e85bdcSAkhil Goyal 
1609c3e85bdcSAkhil Goyal /** Stop queue pair */
1610c3e85bdcSAkhil Goyal static int
1611c3e85bdcSAkhil Goyal dpaa_sec_queue_pair_stop(__rte_unused struct rte_cryptodev *dev,
1612c3e85bdcSAkhil Goyal 			 __rte_unused uint16_t queue_pair_id)
1613c3e85bdcSAkhil Goyal {
1614c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
1615c3e85bdcSAkhil Goyal 
1616c3e85bdcSAkhil Goyal 	return 0;
1617c3e85bdcSAkhil Goyal }
1618c3e85bdcSAkhil Goyal 
1619c3e85bdcSAkhil Goyal /** Return the number of allocated queue pairs */
1620c3e85bdcSAkhil Goyal static uint32_t
1621c3e85bdcSAkhil Goyal dpaa_sec_queue_pair_count(struct rte_cryptodev *dev)
1622c3e85bdcSAkhil Goyal {
1623c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
1624c3e85bdcSAkhil Goyal 
1625c3e85bdcSAkhil Goyal 	return dev->data->nb_queue_pairs;
1626c3e85bdcSAkhil Goyal }
1627c3e85bdcSAkhil Goyal 
1628c3e85bdcSAkhil Goyal /** Returns the size of session structure */
1629c3e85bdcSAkhil Goyal static unsigned int
1630c3e85bdcSAkhil Goyal dpaa_sec_session_get_size(struct rte_cryptodev *dev __rte_unused)
1631c3e85bdcSAkhil Goyal {
1632c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
1633c3e85bdcSAkhil Goyal 
1634c3e85bdcSAkhil Goyal 	return sizeof(dpaa_sec_session);
1635c3e85bdcSAkhil Goyal }
1636c3e85bdcSAkhil Goyal 
1637c3e85bdcSAkhil Goyal static int
1638c3e85bdcSAkhil Goyal dpaa_sec_cipher_init(struct rte_cryptodev *dev __rte_unused,
1639c3e85bdcSAkhil Goyal 		     struct rte_crypto_sym_xform *xform,
1640c3e85bdcSAkhil Goyal 		     dpaa_sec_session *session)
1641c3e85bdcSAkhil Goyal {
1642c3e85bdcSAkhil Goyal 	session->cipher_alg = xform->cipher.algo;
1643c3e85bdcSAkhil Goyal 	session->iv.length = xform->cipher.iv.length;
1644c3e85bdcSAkhil Goyal 	session->iv.offset = xform->cipher.iv.offset;
1645c3e85bdcSAkhil Goyal 	session->cipher_key.data = rte_zmalloc(NULL, xform->cipher.key.length,
1646c3e85bdcSAkhil Goyal 					       RTE_CACHE_LINE_SIZE);
1647c3e85bdcSAkhil Goyal 	if (session->cipher_key.data == NULL && xform->cipher.key.length > 0) {
1648c3e85bdcSAkhil Goyal 		PMD_INIT_LOG(ERR, "No Memory for cipher key\n");
1649c3e85bdcSAkhil Goyal 		return -ENOMEM;
1650c3e85bdcSAkhil Goyal 	}
1651c3e85bdcSAkhil Goyal 	session->cipher_key.length = xform->cipher.key.length;
1652c3e85bdcSAkhil Goyal 
1653c3e85bdcSAkhil Goyal 	memcpy(session->cipher_key.data, xform->cipher.key.data,
1654c3e85bdcSAkhil Goyal 	       xform->cipher.key.length);
1655c3e85bdcSAkhil Goyal 	session->dir = (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
1656c3e85bdcSAkhil Goyal 			DIR_ENC : DIR_DEC;
1657c3e85bdcSAkhil Goyal 
1658c3e85bdcSAkhil Goyal 	return 0;
1659c3e85bdcSAkhil Goyal }
1660c3e85bdcSAkhil Goyal 
1661c3e85bdcSAkhil Goyal static int
1662c3e85bdcSAkhil Goyal dpaa_sec_auth_init(struct rte_cryptodev *dev __rte_unused,
1663c3e85bdcSAkhil Goyal 		   struct rte_crypto_sym_xform *xform,
1664c3e85bdcSAkhil Goyal 		   dpaa_sec_session *session)
1665c3e85bdcSAkhil Goyal {
1666c3e85bdcSAkhil Goyal 	session->auth_alg = xform->auth.algo;
1667c3e85bdcSAkhil Goyal 	session->auth_key.data = rte_zmalloc(NULL, xform->auth.key.length,
1668c3e85bdcSAkhil Goyal 					     RTE_CACHE_LINE_SIZE);
1669c3e85bdcSAkhil Goyal 	if (session->auth_key.data == NULL && xform->auth.key.length > 0) {
1670c3e85bdcSAkhil Goyal 		PMD_INIT_LOG(ERR, "No Memory for auth key\n");
1671c3e85bdcSAkhil Goyal 		return -ENOMEM;
1672c3e85bdcSAkhil Goyal 	}
1673c3e85bdcSAkhil Goyal 	session->auth_key.length = xform->auth.key.length;
1674c3e85bdcSAkhil Goyal 	session->digest_length = xform->auth.digest_length;
1675c3e85bdcSAkhil Goyal 
1676c3e85bdcSAkhil Goyal 	memcpy(session->auth_key.data, xform->auth.key.data,
1677c3e85bdcSAkhil Goyal 	       xform->auth.key.length);
1678c3e85bdcSAkhil Goyal 	session->dir = (xform->auth.op == RTE_CRYPTO_AUTH_OP_GENERATE) ?
1679c3e85bdcSAkhil Goyal 			DIR_ENC : DIR_DEC;
1680c3e85bdcSAkhil Goyal 
1681c3e85bdcSAkhil Goyal 	return 0;
1682c3e85bdcSAkhil Goyal }
1683c3e85bdcSAkhil Goyal 
1684c3e85bdcSAkhil Goyal static int
1685c3e85bdcSAkhil Goyal dpaa_sec_aead_init(struct rte_cryptodev *dev __rte_unused,
1686c3e85bdcSAkhil Goyal 		   struct rte_crypto_sym_xform *xform,
1687c3e85bdcSAkhil Goyal 		   dpaa_sec_session *session)
1688c3e85bdcSAkhil Goyal {
1689c3e85bdcSAkhil Goyal 	session->aead_alg = xform->aead.algo;
1690c3e85bdcSAkhil Goyal 	session->iv.length = xform->aead.iv.length;
1691c3e85bdcSAkhil Goyal 	session->iv.offset = xform->aead.iv.offset;
1692c3e85bdcSAkhil Goyal 	session->auth_only_len = xform->aead.aad_length;
1693c3e85bdcSAkhil Goyal 	session->aead_key.data = rte_zmalloc(NULL, xform->aead.key.length,
1694c3e85bdcSAkhil Goyal 					     RTE_CACHE_LINE_SIZE);
1695c3e85bdcSAkhil Goyal 	if (session->aead_key.data == NULL && xform->aead.key.length > 0) {
1696c3e85bdcSAkhil Goyal 		PMD_INIT_LOG(ERR, "No Memory for aead key\n");
1697c3e85bdcSAkhil Goyal 		return -ENOMEM;
1698c3e85bdcSAkhil Goyal 	}
1699c3e85bdcSAkhil Goyal 	session->aead_key.length = xform->aead.key.length;
1700c3e85bdcSAkhil Goyal 	session->digest_length = xform->aead.digest_length;
1701c3e85bdcSAkhil Goyal 
1702c3e85bdcSAkhil Goyal 	memcpy(session->aead_key.data, xform->aead.key.data,
1703c3e85bdcSAkhil Goyal 	       xform->aead.key.length);
1704c3e85bdcSAkhil Goyal 	session->dir = (xform->aead.op == RTE_CRYPTO_AEAD_OP_ENCRYPT) ?
1705c3e85bdcSAkhil Goyal 			DIR_ENC : DIR_DEC;
1706c3e85bdcSAkhil Goyal 
1707c3e85bdcSAkhil Goyal 	return 0;
1708c3e85bdcSAkhil Goyal }
1709c3e85bdcSAkhil Goyal 
1710e79416d1SHemant Agrawal static struct qman_fq *
1711e79416d1SHemant Agrawal dpaa_sec_attach_rxq(struct dpaa_sec_dev_private *qi)
1712c3e85bdcSAkhil Goyal {
1713e79416d1SHemant Agrawal 	unsigned int i;
1714c3e85bdcSAkhil Goyal 
1715e79416d1SHemant Agrawal 	for (i = 0; i < qi->max_nb_sessions; i++) {
1716e79416d1SHemant Agrawal 		if (qi->inq_attach[i] == 0) {
1717e79416d1SHemant Agrawal 			qi->inq_attach[i] = 1;
1718e79416d1SHemant Agrawal 			return &qi->inq[i];
1719e79416d1SHemant Agrawal 		}
1720e79416d1SHemant Agrawal 	}
1721e79416d1SHemant Agrawal 	PMD_DRV_LOG(ERR, "All ses session in use %x", qi->max_nb_sessions);
1722c3e85bdcSAkhil Goyal 
1723e79416d1SHemant Agrawal 	return NULL;
1724c3e85bdcSAkhil Goyal }
1725c3e85bdcSAkhil Goyal 
1726e79416d1SHemant Agrawal static int
1727e79416d1SHemant Agrawal dpaa_sec_detach_rxq(struct dpaa_sec_dev_private *qi, struct qman_fq *fq)
1728e79416d1SHemant Agrawal {
1729e79416d1SHemant Agrawal 	unsigned int i;
1730e79416d1SHemant Agrawal 
1731e79416d1SHemant Agrawal 	for (i = 0; i < qi->max_nb_sessions; i++) {
1732e79416d1SHemant Agrawal 		if (&qi->inq[i] == fq) {
1733b4053c4bSAlok Makhariya 			qman_retire_fq(fq, NULL);
1734b4053c4bSAlok Makhariya 			qman_oos_fq(fq);
1735e79416d1SHemant Agrawal 			qi->inq_attach[i] = 0;
1736e79416d1SHemant Agrawal 			return 0;
1737e79416d1SHemant Agrawal 		}
1738e79416d1SHemant Agrawal 	}
1739e79416d1SHemant Agrawal 	return -1;
1740e79416d1SHemant Agrawal }
1741e79416d1SHemant Agrawal 
1742e79416d1SHemant Agrawal static int
1743e79416d1SHemant Agrawal dpaa_sec_attach_sess_q(struct dpaa_sec_qp *qp, dpaa_sec_session *sess)
1744e79416d1SHemant Agrawal {
1745e79416d1SHemant Agrawal 	int ret;
1746e79416d1SHemant Agrawal 
1747c3e85bdcSAkhil Goyal 	sess->qp = qp;
1748e79416d1SHemant Agrawal 	ret = dpaa_sec_prep_cdb(sess);
1749e79416d1SHemant Agrawal 	if (ret) {
1750e79416d1SHemant Agrawal 		PMD_DRV_LOG(ERR, "Unable to prepare sec cdb");
1751e79416d1SHemant Agrawal 		return -1;
1752e79416d1SHemant Agrawal 	}
1753c3e85bdcSAkhil Goyal 
1754e79416d1SHemant Agrawal 	ret = dpaa_sec_init_rx(sess->inq, dpaa_mem_vtop(&sess->cdb),
1755e79416d1SHemant Agrawal 			       qman_fq_fqid(&qp->outq));
1756e79416d1SHemant Agrawal 	if (ret)
1757e79416d1SHemant Agrawal 		PMD_DRV_LOG(ERR, "Unable to init sec queue");
1758e79416d1SHemant Agrawal 
1759e79416d1SHemant Agrawal 	return ret;
1760c3e85bdcSAkhil Goyal }
1761c3e85bdcSAkhil Goyal 
1762c3e85bdcSAkhil Goyal static int
1763e79416d1SHemant Agrawal dpaa_sec_qp_attach_sess(struct rte_cryptodev *dev __rte_unused,
1764e79416d1SHemant Agrawal 			uint16_t qp_id __rte_unused,
1765e79416d1SHemant Agrawal 			void *ses __rte_unused)
1766c3e85bdcSAkhil Goyal {
1767c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
1768c3e85bdcSAkhil Goyal 	return 0;
1769c3e85bdcSAkhil Goyal }
1770c3e85bdcSAkhil Goyal 
1771e79416d1SHemant Agrawal static int
1772e79416d1SHemant Agrawal dpaa_sec_qp_detach_sess(struct rte_cryptodev *dev,
1773e79416d1SHemant Agrawal 			uint16_t qp_id  __rte_unused,
1774e79416d1SHemant Agrawal 			void *ses)
1775e79416d1SHemant Agrawal {
1776e79416d1SHemant Agrawal 	dpaa_sec_session *sess = ses;
1777e79416d1SHemant Agrawal 	struct dpaa_sec_dev_private *qi = dev->data->dev_private;
1778e79416d1SHemant Agrawal 
1779e79416d1SHemant Agrawal 	PMD_INIT_FUNC_TRACE();
1780e79416d1SHemant Agrawal 
1781e79416d1SHemant Agrawal 	if (sess->inq)
1782e79416d1SHemant Agrawal 		dpaa_sec_detach_rxq(qi, sess->inq);
1783e79416d1SHemant Agrawal 	sess->inq = NULL;
1784e79416d1SHemant Agrawal 
1785e79416d1SHemant Agrawal 	sess->qp = NULL;
1786e79416d1SHemant Agrawal 
1787e79416d1SHemant Agrawal 	return 0;
1788c3e85bdcSAkhil Goyal }
1789c3e85bdcSAkhil Goyal 
1790c3e85bdcSAkhil Goyal static int
1791c3e85bdcSAkhil Goyal dpaa_sec_set_session_parameters(struct rte_cryptodev *dev,
1792c3e85bdcSAkhil Goyal 			    struct rte_crypto_sym_xform *xform,	void *sess)
1793c3e85bdcSAkhil Goyal {
1794c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals = dev->data->dev_private;
1795c3e85bdcSAkhil Goyal 	dpaa_sec_session *session = sess;
1796c3e85bdcSAkhil Goyal 
1797c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
1798c3e85bdcSAkhil Goyal 
1799c3e85bdcSAkhil Goyal 	if (unlikely(sess == NULL)) {
1800c3e85bdcSAkhil Goyal 		RTE_LOG(ERR, PMD, "invalid session struct\n");
1801c3e85bdcSAkhil Goyal 		return -EINVAL;
1802c3e85bdcSAkhil Goyal 	}
1803c3e85bdcSAkhil Goyal 
1804c3e85bdcSAkhil Goyal 	/* Default IV length = 0 */
1805c3e85bdcSAkhil Goyal 	session->iv.length = 0;
1806c3e85bdcSAkhil Goyal 
1807c3e85bdcSAkhil Goyal 	/* Cipher Only */
1808c3e85bdcSAkhil Goyal 	if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER && xform->next == NULL) {
1809c3e85bdcSAkhil Goyal 		session->auth_alg = RTE_CRYPTO_AUTH_NULL;
1810c3e85bdcSAkhil Goyal 		dpaa_sec_cipher_init(dev, xform, session);
1811c3e85bdcSAkhil Goyal 
1812c3e85bdcSAkhil Goyal 	/* Authentication Only */
1813c3e85bdcSAkhil Goyal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
1814c3e85bdcSAkhil Goyal 		   xform->next == NULL) {
1815c3e85bdcSAkhil Goyal 		session->cipher_alg = RTE_CRYPTO_CIPHER_NULL;
1816c3e85bdcSAkhil Goyal 		dpaa_sec_auth_init(dev, xform, session);
1817c3e85bdcSAkhil Goyal 
1818c3e85bdcSAkhil Goyal 	/* Cipher then Authenticate */
1819c3e85bdcSAkhil Goyal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
1820c3e85bdcSAkhil Goyal 		   xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
1821c3e85bdcSAkhil Goyal 		if (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) {
1822c3e85bdcSAkhil Goyal 			dpaa_sec_cipher_init(dev, xform, session);
1823c3e85bdcSAkhil Goyal 			dpaa_sec_auth_init(dev, xform->next, session);
1824c3e85bdcSAkhil Goyal 		} else {
1825c3e85bdcSAkhil Goyal 			PMD_DRV_LOG(ERR, "Not supported: Auth then Cipher");
1826c3e85bdcSAkhil Goyal 			return -EINVAL;
1827c3e85bdcSAkhil Goyal 		}
1828c3e85bdcSAkhil Goyal 
1829c3e85bdcSAkhil Goyal 	/* Authenticate then Cipher */
1830c3e85bdcSAkhil Goyal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
1831c3e85bdcSAkhil Goyal 		   xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
1832c3e85bdcSAkhil Goyal 		if (xform->next->cipher.op == RTE_CRYPTO_CIPHER_OP_DECRYPT) {
1833c3e85bdcSAkhil Goyal 			dpaa_sec_auth_init(dev, xform, session);
1834c3e85bdcSAkhil Goyal 			dpaa_sec_cipher_init(dev, xform->next, session);
1835c3e85bdcSAkhil Goyal 		} else {
1836c3e85bdcSAkhil Goyal 			PMD_DRV_LOG(ERR, "Not supported: Auth then Cipher");
1837c3e85bdcSAkhil Goyal 			return -EINVAL;
1838c3e85bdcSAkhil Goyal 		}
1839c3e85bdcSAkhil Goyal 
1840c3e85bdcSAkhil Goyal 	/* AEAD operation for AES-GCM kind of Algorithms */
1841c3e85bdcSAkhil Goyal 	} else if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD &&
1842c3e85bdcSAkhil Goyal 		   xform->next == NULL) {
1843c3e85bdcSAkhil Goyal 		dpaa_sec_aead_init(dev, xform, session);
1844c3e85bdcSAkhil Goyal 
1845c3e85bdcSAkhil Goyal 	} else {
1846c3e85bdcSAkhil Goyal 		PMD_DRV_LOG(ERR, "Invalid crypto type");
1847c3e85bdcSAkhil Goyal 		return -EINVAL;
1848c3e85bdcSAkhil Goyal 	}
1849c3e85bdcSAkhil Goyal 	session->ctx_pool = internals->ctx_pool;
1850e79416d1SHemant Agrawal 	session->inq = dpaa_sec_attach_rxq(internals);
1851e79416d1SHemant Agrawal 	if (session->inq == NULL) {
1852e79416d1SHemant Agrawal 		PMD_DRV_LOG(ERR, "unable to attach sec queue");
1853e79416d1SHemant Agrawal 		goto err1;
1854e79416d1SHemant Agrawal 	}
1855c3e85bdcSAkhil Goyal 
1856c3e85bdcSAkhil Goyal 	return 0;
1857e79416d1SHemant Agrawal 
1858e79416d1SHemant Agrawal err1:
1859e79416d1SHemant Agrawal 	rte_free(session->cipher_key.data);
1860e79416d1SHemant Agrawal 	rte_free(session->auth_key.data);
1861e79416d1SHemant Agrawal 	memset(session, 0, sizeof(dpaa_sec_session));
1862e79416d1SHemant Agrawal 
1863e79416d1SHemant Agrawal 	return -EINVAL;
1864c3e85bdcSAkhil Goyal }
1865c3e85bdcSAkhil Goyal 
1866c3e85bdcSAkhil Goyal static int
1867c3e85bdcSAkhil Goyal dpaa_sec_session_configure(struct rte_cryptodev *dev,
1868c3e85bdcSAkhil Goyal 		struct rte_crypto_sym_xform *xform,
1869c3e85bdcSAkhil Goyal 		struct rte_cryptodev_sym_session *sess,
1870c3e85bdcSAkhil Goyal 		struct rte_mempool *mempool)
1871c3e85bdcSAkhil Goyal {
1872c3e85bdcSAkhil Goyal 	void *sess_private_data;
1873c3e85bdcSAkhil Goyal 	int ret;
1874c3e85bdcSAkhil Goyal 
1875c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
1876c3e85bdcSAkhil Goyal 
1877c3e85bdcSAkhil Goyal 	if (rte_mempool_get(mempool, &sess_private_data)) {
1878c3e85bdcSAkhil Goyal 		CDEV_LOG_ERR(
1879c3e85bdcSAkhil Goyal 			"Couldn't get object from session mempool");
1880c3e85bdcSAkhil Goyal 		return -ENOMEM;
1881c3e85bdcSAkhil Goyal 	}
1882c3e85bdcSAkhil Goyal 
1883c3e85bdcSAkhil Goyal 	ret = dpaa_sec_set_session_parameters(dev, xform, sess_private_data);
1884c3e85bdcSAkhil Goyal 	if (ret != 0) {
1885c3e85bdcSAkhil Goyal 		PMD_DRV_LOG(ERR, "DPAA PMD: failed to configure "
1886c3e85bdcSAkhil Goyal 				"session parameters");
1887c3e85bdcSAkhil Goyal 
1888c3e85bdcSAkhil Goyal 		/* Return session to mempool */
1889c3e85bdcSAkhil Goyal 		rte_mempool_put(mempool, sess_private_data);
1890c3e85bdcSAkhil Goyal 		return ret;
1891c3e85bdcSAkhil Goyal 	}
1892c3e85bdcSAkhil Goyal 
1893c3e85bdcSAkhil Goyal 	set_session_private_data(sess, dev->driver_id,
1894c3e85bdcSAkhil Goyal 			sess_private_data);
1895c3e85bdcSAkhil Goyal 
1896e79416d1SHemant Agrawal 
1897c3e85bdcSAkhil Goyal 	return 0;
1898c3e85bdcSAkhil Goyal }
1899c3e85bdcSAkhil Goyal 
1900c3e85bdcSAkhil Goyal /** Clear the memory of session so it doesn't leave key material behind */
1901c3e85bdcSAkhil Goyal static void
1902c3e85bdcSAkhil Goyal dpaa_sec_session_clear(struct rte_cryptodev *dev,
1903c3e85bdcSAkhil Goyal 		struct rte_cryptodev_sym_session *sess)
1904c3e85bdcSAkhil Goyal {
1905e79416d1SHemant Agrawal 	struct dpaa_sec_dev_private *qi = dev->data->dev_private;
1906c3e85bdcSAkhil Goyal 	uint8_t index = dev->driver_id;
1907c3e85bdcSAkhil Goyal 	void *sess_priv = get_session_private_data(sess, index);
1908e79416d1SHemant Agrawal 
1909e79416d1SHemant Agrawal 	PMD_INIT_FUNC_TRACE();
1910e79416d1SHemant Agrawal 
1911c3e85bdcSAkhil Goyal 	dpaa_sec_session *s = (dpaa_sec_session *)sess_priv;
1912c3e85bdcSAkhil Goyal 
1913c3e85bdcSAkhil Goyal 	if (sess_priv) {
1914e79416d1SHemant Agrawal 		struct rte_mempool *sess_mp = rte_mempool_from_obj(sess_priv);
1915e79416d1SHemant Agrawal 
1916e79416d1SHemant Agrawal 		if (s->inq)
1917e79416d1SHemant Agrawal 			dpaa_sec_detach_rxq(qi, s->inq);
1918c3e85bdcSAkhil Goyal 		rte_free(s->cipher_key.data);
1919c3e85bdcSAkhil Goyal 		rte_free(s->auth_key.data);
1920c3e85bdcSAkhil Goyal 		memset(s, 0, sizeof(dpaa_sec_session));
1921c3e85bdcSAkhil Goyal 		set_session_private_data(sess, index, NULL);
1922c3e85bdcSAkhil Goyal 		rte_mempool_put(sess_mp, sess_priv);
1923c3e85bdcSAkhil Goyal 	}
1924c3e85bdcSAkhil Goyal }
1925c3e85bdcSAkhil Goyal 
1926c3e85bdcSAkhil Goyal static int
19271f14d500SAkhil Goyal dpaa_sec_set_ipsec_session(__rte_unused struct rte_cryptodev *dev,
19281f14d500SAkhil Goyal 			   struct rte_security_session_conf *conf,
19291f14d500SAkhil Goyal 			   void *sess)
19301f14d500SAkhil Goyal {
19311f14d500SAkhil Goyal 	struct dpaa_sec_dev_private *internals = dev->data->dev_private;
19321f14d500SAkhil Goyal 	struct rte_security_ipsec_xform *ipsec_xform = &conf->ipsec;
19331f14d500SAkhil Goyal 	struct rte_crypto_auth_xform *auth_xform;
19341f14d500SAkhil Goyal 	struct rte_crypto_cipher_xform *cipher_xform;
19351f14d500SAkhil Goyal 	dpaa_sec_session *session = (dpaa_sec_session *)sess;
19361f14d500SAkhil Goyal 
19371f14d500SAkhil Goyal 	PMD_INIT_FUNC_TRACE();
19381f14d500SAkhil Goyal 
19391f14d500SAkhil Goyal 	if (ipsec_xform->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
19401f14d500SAkhil Goyal 		cipher_xform = &conf->crypto_xform->cipher;
19411f14d500SAkhil Goyal 		auth_xform = &conf->crypto_xform->next->auth;
19421f14d500SAkhil Goyal 	} else {
19431f14d500SAkhil Goyal 		auth_xform = &conf->crypto_xform->auth;
19441f14d500SAkhil Goyal 		cipher_xform = &conf->crypto_xform->next->cipher;
19451f14d500SAkhil Goyal 	}
19461f14d500SAkhil Goyal 	session->proto_alg = conf->protocol;
19471f14d500SAkhil Goyal 	session->cipher_key.data = rte_zmalloc(NULL,
19481f14d500SAkhil Goyal 					       cipher_xform->key.length,
19491f14d500SAkhil Goyal 					       RTE_CACHE_LINE_SIZE);
19501f14d500SAkhil Goyal 	if (session->cipher_key.data == NULL &&
19511f14d500SAkhil Goyal 			cipher_xform->key.length > 0) {
19521f14d500SAkhil Goyal 		RTE_LOG(ERR, PMD, "No Memory for cipher key\n");
19531f14d500SAkhil Goyal 		return -ENOMEM;
19541f14d500SAkhil Goyal 	}
19551f14d500SAkhil Goyal 
19561f14d500SAkhil Goyal 	session->cipher_key.length = cipher_xform->key.length;
19571f14d500SAkhil Goyal 	session->auth_key.data = rte_zmalloc(NULL,
19581f14d500SAkhil Goyal 					auth_xform->key.length,
19591f14d500SAkhil Goyal 					RTE_CACHE_LINE_SIZE);
19601f14d500SAkhil Goyal 	if (session->auth_key.data == NULL &&
19611f14d500SAkhil Goyal 			auth_xform->key.length > 0) {
19621f14d500SAkhil Goyal 		RTE_LOG(ERR, PMD, "No Memory for auth key\n");
19631f14d500SAkhil Goyal 		rte_free(session->cipher_key.data);
19641f14d500SAkhil Goyal 		return -ENOMEM;
19651f14d500SAkhil Goyal 	}
19661f14d500SAkhil Goyal 	session->auth_key.length = auth_xform->key.length;
19671f14d500SAkhil Goyal 	memcpy(session->cipher_key.data, cipher_xform->key.data,
19681f14d500SAkhil Goyal 			cipher_xform->key.length);
19691f14d500SAkhil Goyal 	memcpy(session->auth_key.data, auth_xform->key.data,
19701f14d500SAkhil Goyal 			auth_xform->key.length);
19711f14d500SAkhil Goyal 
19721f14d500SAkhil Goyal 	switch (auth_xform->algo) {
19731f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA1_HMAC:
19741f14d500SAkhil Goyal 		session->auth_alg = RTE_CRYPTO_AUTH_SHA1_HMAC;
19751f14d500SAkhil Goyal 		break;
19761f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_MD5_HMAC:
19771f14d500SAkhil Goyal 		session->auth_alg = RTE_CRYPTO_AUTH_MD5_HMAC;
19781f14d500SAkhil Goyal 		break;
19791f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA256_HMAC:
19801f14d500SAkhil Goyal 		session->auth_alg = RTE_CRYPTO_AUTH_SHA256_HMAC;
19811f14d500SAkhil Goyal 		break;
19821f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA384_HMAC:
19831f14d500SAkhil Goyal 		session->auth_alg = RTE_CRYPTO_AUTH_SHA384_HMAC;
19841f14d500SAkhil Goyal 		break;
19851f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA512_HMAC:
19861f14d500SAkhil Goyal 		session->auth_alg = RTE_CRYPTO_AUTH_SHA512_HMAC;
19871f14d500SAkhil Goyal 		break;
19881f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_AES_CMAC:
19891f14d500SAkhil Goyal 		session->auth_alg = RTE_CRYPTO_AUTH_AES_CMAC;
19901f14d500SAkhil Goyal 		break;
19911f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_NULL:
19921f14d500SAkhil Goyal 		session->auth_alg = RTE_CRYPTO_AUTH_NULL;
19931f14d500SAkhil Goyal 		break;
19941f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA224_HMAC:
19951f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_AES_XCBC_MAC:
19961f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
19971f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA1:
19981f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA256:
19991f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA512:
20001f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA224:
20011f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_SHA384:
20021f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_MD5:
20031f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_AES_GMAC:
20041f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_KASUMI_F9:
20051f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_AES_CBC_MAC:
20061f14d500SAkhil Goyal 	case RTE_CRYPTO_AUTH_ZUC_EIA3:
20071f14d500SAkhil Goyal 		RTE_LOG(ERR, PMD, "Crypto: Unsupported auth alg %u\n",
20081f14d500SAkhil Goyal 			auth_xform->algo);
20091f14d500SAkhil Goyal 		goto out;
20101f14d500SAkhil Goyal 	default:
20111f14d500SAkhil Goyal 		RTE_LOG(ERR, PMD, "Crypto: Undefined Auth specified %u\n",
20121f14d500SAkhil Goyal 			auth_xform->algo);
20131f14d500SAkhil Goyal 		goto out;
20141f14d500SAkhil Goyal 	}
20151f14d500SAkhil Goyal 
20161f14d500SAkhil Goyal 	switch (cipher_xform->algo) {
20171f14d500SAkhil Goyal 	case RTE_CRYPTO_CIPHER_AES_CBC:
20181f14d500SAkhil Goyal 		session->cipher_alg = RTE_CRYPTO_CIPHER_AES_CBC;
20191f14d500SAkhil Goyal 		break;
20201f14d500SAkhil Goyal 	case RTE_CRYPTO_CIPHER_3DES_CBC:
20211f14d500SAkhil Goyal 		session->cipher_alg = RTE_CRYPTO_CIPHER_3DES_CBC;
20221f14d500SAkhil Goyal 		break;
20231f14d500SAkhil Goyal 	case RTE_CRYPTO_CIPHER_AES_CTR:
20241f14d500SAkhil Goyal 		session->cipher_alg = RTE_CRYPTO_CIPHER_AES_CTR;
20251f14d500SAkhil Goyal 		break;
20261f14d500SAkhil Goyal 	case RTE_CRYPTO_CIPHER_NULL:
20271f14d500SAkhil Goyal 	case RTE_CRYPTO_CIPHER_SNOW3G_UEA2:
20281f14d500SAkhil Goyal 	case RTE_CRYPTO_CIPHER_3DES_ECB:
20291f14d500SAkhil Goyal 	case RTE_CRYPTO_CIPHER_AES_ECB:
20301f14d500SAkhil Goyal 	case RTE_CRYPTO_CIPHER_KASUMI_F8:
20311f14d500SAkhil Goyal 		RTE_LOG(ERR, PMD, "Crypto: Unsupported Cipher alg %u\n",
20321f14d500SAkhil Goyal 			cipher_xform->algo);
20331f14d500SAkhil Goyal 		goto out;
20341f14d500SAkhil Goyal 	default:
20351f14d500SAkhil Goyal 		RTE_LOG(ERR, PMD, "Crypto: Undefined Cipher specified %u\n",
20361f14d500SAkhil Goyal 			cipher_xform->algo);
20371f14d500SAkhil Goyal 		goto out;
20381f14d500SAkhil Goyal 	}
20391f14d500SAkhil Goyal 
20401f14d500SAkhil Goyal 	if (ipsec_xform->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
20411f14d500SAkhil Goyal 		memset(&session->encap_pdb, 0, sizeof(struct ipsec_encap_pdb) +
20421f14d500SAkhil Goyal 				sizeof(session->ip4_hdr));
20431f14d500SAkhil Goyal 		session->ip4_hdr.ip_v = IPVERSION;
20441f14d500SAkhil Goyal 		session->ip4_hdr.ip_hl = 5;
20451f14d500SAkhil Goyal 		session->ip4_hdr.ip_len = rte_cpu_to_be_16(
20461f14d500SAkhil Goyal 						sizeof(session->ip4_hdr));
20471f14d500SAkhil Goyal 		session->ip4_hdr.ip_tos = ipsec_xform->tunnel.ipv4.dscp;
20481f14d500SAkhil Goyal 		session->ip4_hdr.ip_id = 0;
20491f14d500SAkhil Goyal 		session->ip4_hdr.ip_off = 0;
20501f14d500SAkhil Goyal 		session->ip4_hdr.ip_ttl = ipsec_xform->tunnel.ipv4.ttl;
20511f14d500SAkhil Goyal 		session->ip4_hdr.ip_p = (ipsec_xform->proto ==
20521f14d500SAkhil Goyal 				RTE_SECURITY_IPSEC_SA_PROTO_ESP) ? IPPROTO_ESP
20531f14d500SAkhil Goyal 				: IPPROTO_AH;
20541f14d500SAkhil Goyal 		session->ip4_hdr.ip_sum = 0;
20551f14d500SAkhil Goyal 		session->ip4_hdr.ip_src = ipsec_xform->tunnel.ipv4.src_ip;
20561f14d500SAkhil Goyal 		session->ip4_hdr.ip_dst = ipsec_xform->tunnel.ipv4.dst_ip;
20571f14d500SAkhil Goyal 		session->ip4_hdr.ip_sum = calc_chksum((uint16_t *)
20581f14d500SAkhil Goyal 						(void *)&session->ip4_hdr,
20591f14d500SAkhil Goyal 						sizeof(struct ip));
20601f14d500SAkhil Goyal 
20611f14d500SAkhil Goyal 		session->encap_pdb.options =
20621f14d500SAkhil Goyal 			(IPVERSION << PDBNH_ESP_ENCAP_SHIFT) |
20631f14d500SAkhil Goyal 			PDBOPTS_ESP_OIHI_PDB_INL |
20641f14d500SAkhil Goyal 			PDBOPTS_ESP_IVSRC |
20651f14d500SAkhil Goyal 			PDBHMO_ESP_ENCAP_DTTL;
20661f14d500SAkhil Goyal 		session->encap_pdb.spi = ipsec_xform->spi;
20671f14d500SAkhil Goyal 		session->encap_pdb.ip_hdr_len = sizeof(struct ip);
20681f14d500SAkhil Goyal 
20691f14d500SAkhil Goyal 		session->dir = DIR_ENC;
20701f14d500SAkhil Goyal 	} else if (ipsec_xform->direction ==
20711f14d500SAkhil Goyal 			RTE_SECURITY_IPSEC_SA_DIR_INGRESS) {
20721f14d500SAkhil Goyal 		memset(&session->decap_pdb, 0, sizeof(struct ipsec_decap_pdb));
20731f14d500SAkhil Goyal 		session->decap_pdb.options = sizeof(struct ip) << 16;
20741f14d500SAkhil Goyal 		session->dir = DIR_DEC;
20751f14d500SAkhil Goyal 	} else
20761f14d500SAkhil Goyal 		goto out;
20771f14d500SAkhil Goyal 	session->ctx_pool = internals->ctx_pool;
20781f14d500SAkhil Goyal 	session->inq = dpaa_sec_attach_rxq(internals);
20791f14d500SAkhil Goyal 	if (session->inq == NULL) {
20801f14d500SAkhil Goyal 		PMD_DRV_LOG(ERR, "unable to attach sec queue");
20811f14d500SAkhil Goyal 		goto out;
20821f14d500SAkhil Goyal 	}
20831f14d500SAkhil Goyal 
20841f14d500SAkhil Goyal 
20851f14d500SAkhil Goyal 	return 0;
20861f14d500SAkhil Goyal out:
20871f14d500SAkhil Goyal 	rte_free(session->auth_key.data);
20881f14d500SAkhil Goyal 	rte_free(session->cipher_key.data);
20891f14d500SAkhil Goyal 	memset(session, 0, sizeof(dpaa_sec_session));
20901f14d500SAkhil Goyal 	return -1;
20911f14d500SAkhil Goyal }
20921f14d500SAkhil Goyal 
20931f14d500SAkhil Goyal static int
20941f14d500SAkhil Goyal dpaa_sec_security_session_create(void *dev,
20951f14d500SAkhil Goyal 				 struct rte_security_session_conf *conf,
20961f14d500SAkhil Goyal 				 struct rte_security_session *sess,
20971f14d500SAkhil Goyal 				 struct rte_mempool *mempool)
20981f14d500SAkhil Goyal {
20991f14d500SAkhil Goyal 	void *sess_private_data;
21001f14d500SAkhil Goyal 	struct rte_cryptodev *cdev = (struct rte_cryptodev *)dev;
21011f14d500SAkhil Goyal 	int ret;
21021f14d500SAkhil Goyal 
21031f14d500SAkhil Goyal 	if (rte_mempool_get(mempool, &sess_private_data)) {
21041f14d500SAkhil Goyal 		CDEV_LOG_ERR(
21051f14d500SAkhil Goyal 			"Couldn't get object from session mempool");
21061f14d500SAkhil Goyal 		return -ENOMEM;
21071f14d500SAkhil Goyal 	}
21081f14d500SAkhil Goyal 
21091f14d500SAkhil Goyal 	switch (conf->protocol) {
21101f14d500SAkhil Goyal 	case RTE_SECURITY_PROTOCOL_IPSEC:
21111f14d500SAkhil Goyal 		ret = dpaa_sec_set_ipsec_session(cdev, conf,
21121f14d500SAkhil Goyal 				sess_private_data);
21131f14d500SAkhil Goyal 		break;
21141f14d500SAkhil Goyal 	case RTE_SECURITY_PROTOCOL_MACSEC:
21151f14d500SAkhil Goyal 		return -ENOTSUP;
21161f14d500SAkhil Goyal 	default:
21171f14d500SAkhil Goyal 		return -EINVAL;
21181f14d500SAkhil Goyal 	}
21191f14d500SAkhil Goyal 	if (ret != 0) {
21201f14d500SAkhil Goyal 		PMD_DRV_LOG(ERR,
21211f14d500SAkhil Goyal 			"DPAA2 PMD: failed to configure session parameters");
21221f14d500SAkhil Goyal 
21231f14d500SAkhil Goyal 		/* Return session to mempool */
21241f14d500SAkhil Goyal 		rte_mempool_put(mempool, sess_private_data);
21251f14d500SAkhil Goyal 		return ret;
21261f14d500SAkhil Goyal 	}
21271f14d500SAkhil Goyal 
21281f14d500SAkhil Goyal 	set_sec_session_private_data(sess, sess_private_data);
21291f14d500SAkhil Goyal 
21301f14d500SAkhil Goyal 	return ret;
21311f14d500SAkhil Goyal }
21321f14d500SAkhil Goyal 
21331f14d500SAkhil Goyal /** Clear the memory of session so it doesn't leave key material behind */
21341f14d500SAkhil Goyal static int
21351f14d500SAkhil Goyal dpaa_sec_security_session_destroy(void *dev __rte_unused,
21361f14d500SAkhil Goyal 		struct rte_security_session *sess)
21371f14d500SAkhil Goyal {
21381f14d500SAkhil Goyal 	PMD_INIT_FUNC_TRACE();
21391f14d500SAkhil Goyal 	void *sess_priv = get_sec_session_private_data(sess);
21401f14d500SAkhil Goyal 
21411f14d500SAkhil Goyal 	dpaa_sec_session *s = (dpaa_sec_session *)sess_priv;
21421f14d500SAkhil Goyal 
21431f14d500SAkhil Goyal 	if (sess_priv) {
21441f14d500SAkhil Goyal 		struct rte_mempool *sess_mp = rte_mempool_from_obj(sess_priv);
21451f14d500SAkhil Goyal 
21461f14d500SAkhil Goyal 		rte_free(s->cipher_key.data);
21471f14d500SAkhil Goyal 		rte_free(s->auth_key.data);
21481f14d500SAkhil Goyal 		memset(sess, 0, sizeof(dpaa_sec_session));
21491f14d500SAkhil Goyal 		set_sec_session_private_data(sess, NULL);
21501f14d500SAkhil Goyal 		rte_mempool_put(sess_mp, sess_priv);
21511f14d500SAkhil Goyal 	}
21521f14d500SAkhil Goyal 	return 0;
21531f14d500SAkhil Goyal }
21541f14d500SAkhil Goyal 
21551f14d500SAkhil Goyal 
21561f14d500SAkhil Goyal static int
2157c3e85bdcSAkhil Goyal dpaa_sec_dev_configure(struct rte_cryptodev *dev __rte_unused,
2158c3e85bdcSAkhil Goyal 		       struct rte_cryptodev_config *config __rte_unused)
2159c3e85bdcSAkhil Goyal {
2160c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2161c3e85bdcSAkhil Goyal 
2162c3e85bdcSAkhil Goyal 	return 0;
2163c3e85bdcSAkhil Goyal }
2164c3e85bdcSAkhil Goyal 
2165c3e85bdcSAkhil Goyal static int
2166c3e85bdcSAkhil Goyal dpaa_sec_dev_start(struct rte_cryptodev *dev __rte_unused)
2167c3e85bdcSAkhil Goyal {
2168c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2169c3e85bdcSAkhil Goyal 	return 0;
2170c3e85bdcSAkhil Goyal }
2171c3e85bdcSAkhil Goyal 
2172c3e85bdcSAkhil Goyal static void
2173c3e85bdcSAkhil Goyal dpaa_sec_dev_stop(struct rte_cryptodev *dev __rte_unused)
2174c3e85bdcSAkhil Goyal {
2175c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2176c3e85bdcSAkhil Goyal }
2177c3e85bdcSAkhil Goyal 
2178c3e85bdcSAkhil Goyal static int
2179c3e85bdcSAkhil Goyal dpaa_sec_dev_close(struct rte_cryptodev *dev __rte_unused)
2180c3e85bdcSAkhil Goyal {
2181c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2182c3e85bdcSAkhil Goyal 	return 0;
2183c3e85bdcSAkhil Goyal }
2184c3e85bdcSAkhil Goyal 
2185c3e85bdcSAkhil Goyal static void
2186c3e85bdcSAkhil Goyal dpaa_sec_dev_infos_get(struct rte_cryptodev *dev,
2187c3e85bdcSAkhil Goyal 		       struct rte_cryptodev_info *info)
2188c3e85bdcSAkhil Goyal {
2189c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals = dev->data->dev_private;
2190c3e85bdcSAkhil Goyal 
2191c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2192c3e85bdcSAkhil Goyal 	if (info != NULL) {
2193c3e85bdcSAkhil Goyal 		info->max_nb_queue_pairs = internals->max_nb_queue_pairs;
2194c3e85bdcSAkhil Goyal 		info->feature_flags = dev->feature_flags;
2195c3e85bdcSAkhil Goyal 		info->capabilities = dpaa_sec_capabilities;
2196c3e85bdcSAkhil Goyal 		info->sym.max_nb_sessions = internals->max_nb_sessions;
2197c3e85bdcSAkhil Goyal 		info->sym.max_nb_sessions_per_qp =
2198e79416d1SHemant Agrawal 			RTE_DPAA_SEC_PMD_MAX_NB_SESSIONS /
2199e79416d1SHemant Agrawal 			RTE_DPAA_MAX_NB_SEC_QPS;
2200c3e85bdcSAkhil Goyal 		info->driver_id = cryptodev_driver_id;
2201c3e85bdcSAkhil Goyal 	}
2202c3e85bdcSAkhil Goyal }
2203c3e85bdcSAkhil Goyal 
2204c3e85bdcSAkhil Goyal static struct rte_cryptodev_ops crypto_ops = {
2205c3e85bdcSAkhil Goyal 	.dev_configure	      = dpaa_sec_dev_configure,
2206c3e85bdcSAkhil Goyal 	.dev_start	      = dpaa_sec_dev_start,
2207c3e85bdcSAkhil Goyal 	.dev_stop	      = dpaa_sec_dev_stop,
2208c3e85bdcSAkhil Goyal 	.dev_close	      = dpaa_sec_dev_close,
2209c3e85bdcSAkhil Goyal 	.dev_infos_get        = dpaa_sec_dev_infos_get,
2210c3e85bdcSAkhil Goyal 	.queue_pair_setup     = dpaa_sec_queue_pair_setup,
2211c3e85bdcSAkhil Goyal 	.queue_pair_release   = dpaa_sec_queue_pair_release,
2212c3e85bdcSAkhil Goyal 	.queue_pair_start     = dpaa_sec_queue_pair_start,
2213c3e85bdcSAkhil Goyal 	.queue_pair_stop      = dpaa_sec_queue_pair_stop,
2214c3e85bdcSAkhil Goyal 	.queue_pair_count     = dpaa_sec_queue_pair_count,
2215c3e85bdcSAkhil Goyal 	.session_get_size     = dpaa_sec_session_get_size,
2216c3e85bdcSAkhil Goyal 	.session_configure    = dpaa_sec_session_configure,
2217c3e85bdcSAkhil Goyal 	.session_clear        = dpaa_sec_session_clear,
2218c3e85bdcSAkhil Goyal 	.qp_attach_session    = dpaa_sec_qp_attach_sess,
2219c3e85bdcSAkhil Goyal 	.qp_detach_session    = dpaa_sec_qp_detach_sess,
2220c3e85bdcSAkhil Goyal };
2221c3e85bdcSAkhil Goyal 
22221f14d500SAkhil Goyal static const struct rte_security_capability *
22231f14d500SAkhil Goyal dpaa_sec_capabilities_get(void *device __rte_unused)
22241f14d500SAkhil Goyal {
22251f14d500SAkhil Goyal 	return dpaa_sec_security_cap;
22261f14d500SAkhil Goyal }
22271f14d500SAkhil Goyal 
22281f14d500SAkhil Goyal struct rte_security_ops dpaa_sec_security_ops = {
22291f14d500SAkhil Goyal 	.session_create = dpaa_sec_security_session_create,
22301f14d500SAkhil Goyal 	.session_update = NULL,
22311f14d500SAkhil Goyal 	.session_stats_get = NULL,
22321f14d500SAkhil Goyal 	.session_destroy = dpaa_sec_security_session_destroy,
22331f14d500SAkhil Goyal 	.set_pkt_metadata = NULL,
22341f14d500SAkhil Goyal 	.capabilities_get = dpaa_sec_capabilities_get
22351f14d500SAkhil Goyal };
22361f14d500SAkhil Goyal 
2237c3e85bdcSAkhil Goyal static int
2238c3e85bdcSAkhil Goyal dpaa_sec_uninit(struct rte_cryptodev *dev)
2239c3e85bdcSAkhil Goyal {
2240c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals = dev->data->dev_private;
2241c3e85bdcSAkhil Goyal 
2242c3e85bdcSAkhil Goyal 	if (dev == NULL)
2243c3e85bdcSAkhil Goyal 		return -ENODEV;
2244c3e85bdcSAkhil Goyal 
22451f14d500SAkhil Goyal 	rte_free(dev->security_ctx);
22461f14d500SAkhil Goyal 
2247c3e85bdcSAkhil Goyal 	rte_mempool_free(internals->ctx_pool);
2248c3e85bdcSAkhil Goyal 	rte_free(internals);
2249c3e85bdcSAkhil Goyal 
2250c3e85bdcSAkhil Goyal 	PMD_INIT_LOG(INFO, "Closing DPAA_SEC device %s on numa socket %u\n",
2251c3e85bdcSAkhil Goyal 		     dev->data->name, rte_socket_id());
2252c3e85bdcSAkhil Goyal 
2253c3e85bdcSAkhil Goyal 	return 0;
2254c3e85bdcSAkhil Goyal }
2255c3e85bdcSAkhil Goyal 
2256c3e85bdcSAkhil Goyal static int
2257c3e85bdcSAkhil Goyal dpaa_sec_dev_init(struct rte_cryptodev *cryptodev)
2258c3e85bdcSAkhil Goyal {
2259c3e85bdcSAkhil Goyal 	struct dpaa_sec_dev_private *internals;
22601f14d500SAkhil Goyal 	struct rte_security_ctx *security_instance;
2261c3e85bdcSAkhil Goyal 	struct dpaa_sec_qp *qp;
2262e79416d1SHemant Agrawal 	uint32_t i, flags;
2263c3e85bdcSAkhil Goyal 	int ret;
2264c3e85bdcSAkhil Goyal 	char str[20];
2265c3e85bdcSAkhil Goyal 
2266c3e85bdcSAkhil Goyal 	PMD_INIT_FUNC_TRACE();
2267c3e85bdcSAkhil Goyal 
2268c3e85bdcSAkhil Goyal 	cryptodev->driver_id = cryptodev_driver_id;
2269c3e85bdcSAkhil Goyal 	cryptodev->dev_ops = &crypto_ops;
2270c3e85bdcSAkhil Goyal 
2271c3e85bdcSAkhil Goyal 	cryptodev->enqueue_burst = dpaa_sec_enqueue_burst;
2272c3e85bdcSAkhil Goyal 	cryptodev->dequeue_burst = dpaa_sec_dequeue_burst;
2273c3e85bdcSAkhil Goyal 	cryptodev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
2274c3e85bdcSAkhil Goyal 			RTE_CRYPTODEV_FF_HW_ACCELERATED |
22751f14d500SAkhil Goyal 			RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
2276a74af788SAkhil Goyal 			RTE_CRYPTODEV_FF_SECURITY |
2277a74af788SAkhil Goyal 			RTE_CRYPTODEV_FF_MBUF_SCATTER_GATHER;
2278c3e85bdcSAkhil Goyal 
2279c3e85bdcSAkhil Goyal 	internals = cryptodev->data->dev_private;
2280e79416d1SHemant Agrawal 	internals->max_nb_queue_pairs = RTE_DPAA_MAX_NB_SEC_QPS;
2281c3e85bdcSAkhil Goyal 	internals->max_nb_sessions = RTE_DPAA_SEC_PMD_MAX_NB_SESSIONS;
2282c3e85bdcSAkhil Goyal 
22831f14d500SAkhil Goyal 	/*
22841f14d500SAkhil Goyal 	 * For secondary processes, we don't initialise any further as primary
22851f14d500SAkhil Goyal 	 * has already done this work. Only check we don't need a different
22861f14d500SAkhil Goyal 	 * RX function
22871f14d500SAkhil Goyal 	 */
22881f14d500SAkhil Goyal 	if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
22891f14d500SAkhil Goyal 		PMD_INIT_LOG(DEBUG, "Device already init by primary process");
22901f14d500SAkhil Goyal 		return 0;
22911f14d500SAkhil Goyal 	}
22921f14d500SAkhil Goyal 
22931f14d500SAkhil Goyal 	/* Initialize security_ctx only for primary process*/
22941f14d500SAkhil Goyal 	security_instance = rte_malloc("rte_security_instances_ops",
22951f14d500SAkhil Goyal 				sizeof(struct rte_security_ctx), 0);
22961f14d500SAkhil Goyal 	if (security_instance == NULL)
22971f14d500SAkhil Goyal 		return -ENOMEM;
22981f14d500SAkhil Goyal 	security_instance->device = (void *)cryptodev;
22991f14d500SAkhil Goyal 	security_instance->ops = &dpaa_sec_security_ops;
23001f14d500SAkhil Goyal 	security_instance->sess_cnt = 0;
23011f14d500SAkhil Goyal 	cryptodev->security_ctx = security_instance;
23021f14d500SAkhil Goyal 
2303c3e85bdcSAkhil Goyal 	for (i = 0; i < internals->max_nb_queue_pairs; i++) {
2304c3e85bdcSAkhil Goyal 		/* init qman fq for queue pair */
2305c3e85bdcSAkhil Goyal 		qp = &internals->qps[i];
2306c3e85bdcSAkhil Goyal 		ret = dpaa_sec_init_tx(&qp->outq);
2307c3e85bdcSAkhil Goyal 		if (ret) {
2308c3e85bdcSAkhil Goyal 			PMD_INIT_LOG(ERR, "config tx of queue pair  %d", i);
2309c3e85bdcSAkhil Goyal 			goto init_error;
2310c3e85bdcSAkhil Goyal 		}
2311e79416d1SHemant Agrawal 	}
2312e79416d1SHemant Agrawal 
2313e79416d1SHemant Agrawal 	flags = QMAN_FQ_FLAG_LOCKED | QMAN_FQ_FLAG_DYNAMIC_FQID |
2314e79416d1SHemant Agrawal 		QMAN_FQ_FLAG_TO_DCPORTAL;
2315e79416d1SHemant Agrawal 	for (i = 0; i < internals->max_nb_sessions; i++) {
2316e79416d1SHemant Agrawal 		/* create rx qman fq for sessions*/
2317e79416d1SHemant Agrawal 		ret = qman_create_fq(0, flags, &internals->inq[i]);
2318e79416d1SHemant Agrawal 		if (unlikely(ret != 0)) {
2319e79416d1SHemant Agrawal 			PMD_INIT_LOG(ERR, "sec qman_create_fq failed");
2320c3e85bdcSAkhil Goyal 			goto init_error;
2321c3e85bdcSAkhil Goyal 		}
2322c3e85bdcSAkhil Goyal 	}
2323c3e85bdcSAkhil Goyal 
2324c3e85bdcSAkhil Goyal 	sprintf(str, "ctx_pool_%d", cryptodev->data->dev_id);
2325c3e85bdcSAkhil Goyal 	internals->ctx_pool = rte_mempool_create((const char *)str,
2326c3e85bdcSAkhil Goyal 			CTX_POOL_NUM_BUFS,
2327c3e85bdcSAkhil Goyal 			CTX_POOL_BUF_SIZE,
2328c3e85bdcSAkhil Goyal 			CTX_POOL_CACHE_SIZE, 0,
2329c3e85bdcSAkhil Goyal 			NULL, NULL, NULL, NULL,
2330c3e85bdcSAkhil Goyal 			SOCKET_ID_ANY, 0);
2331c3e85bdcSAkhil Goyal 	if (!internals->ctx_pool) {
2332c3e85bdcSAkhil Goyal 		RTE_LOG(ERR, PMD, "%s create failed\n", str);
2333c3e85bdcSAkhil Goyal 		goto init_error;
2334c3e85bdcSAkhil Goyal 	}
2335c3e85bdcSAkhil Goyal 
2336c3e85bdcSAkhil Goyal 	PMD_INIT_LOG(DEBUG, "driver %s: created\n", cryptodev->data->name);
2337c3e85bdcSAkhil Goyal 	return 0;
2338c3e85bdcSAkhil Goyal 
2339c3e85bdcSAkhil Goyal init_error:
2340c3e85bdcSAkhil Goyal 	PMD_INIT_LOG(ERR, "driver %s: create failed\n", cryptodev->data->name);
2341c3e85bdcSAkhil Goyal 
2342c3e85bdcSAkhil Goyal 	dpaa_sec_uninit(cryptodev);
2343c3e85bdcSAkhil Goyal 	return -EFAULT;
2344c3e85bdcSAkhil Goyal }
2345c3e85bdcSAkhil Goyal 
2346c3e85bdcSAkhil Goyal static int
2347c3e85bdcSAkhil Goyal cryptodev_dpaa_sec_probe(struct rte_dpaa_driver *dpaa_drv,
2348c3e85bdcSAkhil Goyal 				struct rte_dpaa_device *dpaa_dev)
2349c3e85bdcSAkhil Goyal {
2350c3e85bdcSAkhil Goyal 	struct rte_cryptodev *cryptodev;
2351c3e85bdcSAkhil Goyal 	char cryptodev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
2352c3e85bdcSAkhil Goyal 
2353c3e85bdcSAkhil Goyal 	int retval;
2354c3e85bdcSAkhil Goyal 
2355c3e85bdcSAkhil Goyal 	sprintf(cryptodev_name, "dpaa_sec-%d", dpaa_dev->id.dev_id);
2356c3e85bdcSAkhil Goyal 
2357c3e85bdcSAkhil Goyal 	cryptodev = rte_cryptodev_pmd_allocate(cryptodev_name, rte_socket_id());
2358c3e85bdcSAkhil Goyal 	if (cryptodev == NULL)
2359c3e85bdcSAkhil Goyal 		return -ENOMEM;
2360c3e85bdcSAkhil Goyal 
2361c3e85bdcSAkhil Goyal 	if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
2362c3e85bdcSAkhil Goyal 		cryptodev->data->dev_private = rte_zmalloc_socket(
2363c3e85bdcSAkhil Goyal 					"cryptodev private structure",
2364c3e85bdcSAkhil Goyal 					sizeof(struct dpaa_sec_dev_private),
2365c3e85bdcSAkhil Goyal 					RTE_CACHE_LINE_SIZE,
2366c3e85bdcSAkhil Goyal 					rte_socket_id());
2367c3e85bdcSAkhil Goyal 
2368c3e85bdcSAkhil Goyal 		if (cryptodev->data->dev_private == NULL)
2369c3e85bdcSAkhil Goyal 			rte_panic("Cannot allocate memzone for private "
2370c3e85bdcSAkhil Goyal 					"device data");
2371c3e85bdcSAkhil Goyal 	}
2372c3e85bdcSAkhil Goyal 
2373c3e85bdcSAkhil Goyal 	dpaa_dev->crypto_dev = cryptodev;
2374c3e85bdcSAkhil Goyal 	cryptodev->device = &dpaa_dev->device;
2375c3e85bdcSAkhil Goyal 	cryptodev->device->driver = &dpaa_drv->driver;
2376c3e85bdcSAkhil Goyal 
2377c3e85bdcSAkhil Goyal 	/* init user callbacks */
2378c3e85bdcSAkhil Goyal 	TAILQ_INIT(&(cryptodev->link_intr_cbs));
2379c3e85bdcSAkhil Goyal 
2380c3e85bdcSAkhil Goyal 	/* if sec device version is not configured */
2381c3e85bdcSAkhil Goyal 	if (!rta_get_sec_era()) {
2382c3e85bdcSAkhil Goyal 		const struct device_node *caam_node;
2383c3e85bdcSAkhil Goyal 
2384c3e85bdcSAkhil Goyal 		for_each_compatible_node(caam_node, NULL, "fsl,sec-v4.0") {
2385c3e85bdcSAkhil Goyal 			const uint32_t *prop = of_get_property(caam_node,
2386c3e85bdcSAkhil Goyal 					"fsl,sec-era",
2387c3e85bdcSAkhil Goyal 					NULL);
2388c3e85bdcSAkhil Goyal 			if (prop) {
2389c3e85bdcSAkhil Goyal 				rta_set_sec_era(
2390c3e85bdcSAkhil Goyal 					INTL_SEC_ERA(rte_cpu_to_be_32(*prop)));
2391c3e85bdcSAkhil Goyal 				break;
2392c3e85bdcSAkhil Goyal 			}
2393c3e85bdcSAkhil Goyal 		}
2394c3e85bdcSAkhil Goyal 	}
2395c3e85bdcSAkhil Goyal 
2396c3e85bdcSAkhil Goyal 	/* Invoke PMD device initialization function */
2397c3e85bdcSAkhil Goyal 	retval = dpaa_sec_dev_init(cryptodev);
2398c3e85bdcSAkhil Goyal 	if (retval == 0)
2399c3e85bdcSAkhil Goyal 		return 0;
2400c3e85bdcSAkhil Goyal 
2401c3e85bdcSAkhil Goyal 	/* In case of error, cleanup is done */
2402c3e85bdcSAkhil Goyal 	if (rte_eal_process_type() == RTE_PROC_PRIMARY)
2403c3e85bdcSAkhil Goyal 		rte_free(cryptodev->data->dev_private);
2404c3e85bdcSAkhil Goyal 
2405c3e85bdcSAkhil Goyal 	rte_cryptodev_pmd_release_device(cryptodev);
2406c3e85bdcSAkhil Goyal 
2407c3e85bdcSAkhil Goyal 	return -ENXIO;
2408c3e85bdcSAkhil Goyal }
2409c3e85bdcSAkhil Goyal 
2410c3e85bdcSAkhil Goyal static int
2411c3e85bdcSAkhil Goyal cryptodev_dpaa_sec_remove(struct rte_dpaa_device *dpaa_dev)
2412c3e85bdcSAkhil Goyal {
2413c3e85bdcSAkhil Goyal 	struct rte_cryptodev *cryptodev;
2414c3e85bdcSAkhil Goyal 	int ret;
2415c3e85bdcSAkhil Goyal 
2416c3e85bdcSAkhil Goyal 	cryptodev = dpaa_dev->crypto_dev;
2417c3e85bdcSAkhil Goyal 	if (cryptodev == NULL)
2418c3e85bdcSAkhil Goyal 		return -ENODEV;
2419c3e85bdcSAkhil Goyal 
2420c3e85bdcSAkhil Goyal 	ret = dpaa_sec_uninit(cryptodev);
2421c3e85bdcSAkhil Goyal 	if (ret)
2422c3e85bdcSAkhil Goyal 		return ret;
2423c3e85bdcSAkhil Goyal 
2424f2f020d2SDeclan Doherty 	return rte_cryptodev_pmd_destroy(cryptodev);
2425c3e85bdcSAkhil Goyal }
2426c3e85bdcSAkhil Goyal 
2427c3e85bdcSAkhil Goyal static struct rte_dpaa_driver rte_dpaa_sec_driver = {
2428c3e85bdcSAkhil Goyal 	.drv_type = FSL_DPAA_CRYPTO,
2429c3e85bdcSAkhil Goyal 	.driver = {
2430c3e85bdcSAkhil Goyal 		.name = "DPAA SEC PMD"
2431c3e85bdcSAkhil Goyal 	},
2432c3e85bdcSAkhil Goyal 	.probe = cryptodev_dpaa_sec_probe,
2433c3e85bdcSAkhil Goyal 	.remove = cryptodev_dpaa_sec_remove,
2434c3e85bdcSAkhil Goyal };
2435c3e85bdcSAkhil Goyal 
2436c3e85bdcSAkhil Goyal static struct cryptodev_driver dpaa_sec_crypto_drv;
2437c3e85bdcSAkhil Goyal 
2438c3e85bdcSAkhil Goyal RTE_PMD_REGISTER_DPAA(CRYPTODEV_NAME_DPAA_SEC_PMD, rte_dpaa_sec_driver);
2439c3e85bdcSAkhil Goyal RTE_PMD_REGISTER_CRYPTO_DRIVER(dpaa_sec_crypto_drv, rte_dpaa_sec_driver,
2440c3e85bdcSAkhil Goyal 		cryptodev_driver_id);
2441