xref: /dpdk/drivers/crypto/octeontx/otx_cryptodev_ops.c (revision db06451baf6cbbdcbd80b3e682a790cabd351216)
1bfe2ae49SAnoob Joseph /* SPDX-License-Identifier: BSD-3-Clause
2bfe2ae49SAnoob Joseph  * Copyright(c) 2018 Cavium, Inc
3bfe2ae49SAnoob Joseph  */
4bfe2ae49SAnoob Joseph 
50dc1cffaSAnkur Dwivedi #include <rte_alarm.h>
60dc1cffaSAnkur Dwivedi #include <rte_bus_pci.h>
7bfe2ae49SAnoob Joseph #include <rte_cryptodev.h>
80906b99fSMurthy NSSR #include <rte_cryptodev_pmd.h>
944a2cebbSShijith Thotton #include <rte_eventdev.h>
1044a2cebbSShijith Thotton #include <rte_event_crypto_adapter.h>
11ec54bc9dSAnoob Joseph #include <rte_errno.h>
120dc1cffaSAnkur Dwivedi #include <rte_malloc.h>
13ec54bc9dSAnoob Joseph #include <rte_mempool.h>
140dc1cffaSAnkur Dwivedi 
15bfe2ae49SAnoob Joseph #include "otx_cryptodev.h"
160906b99fSMurthy NSSR #include "otx_cryptodev_capabilities.h"
170dc1cffaSAnkur Dwivedi #include "otx_cryptodev_hw_access.h"
1813d711f3SKanaka Durga Kotamarthy #include "otx_cryptodev_mbox.h"
19bfe2ae49SAnoob Joseph #include "otx_cryptodev_ops.h"
20bfe2ae49SAnoob Joseph 
2198c7b9c9SAnoob Joseph #include "cpt_pmd_logs.h"
22aa2cbd32SSunila Sahu #include "cpt_pmd_ops_helper.h"
2398c7b9c9SAnoob Joseph #include "cpt_ucode.h"
2433bcaae5SKanaka Durga Kotamarthy #include "cpt_ucode_asym.h"
2598c7b9c9SAnoob Joseph 
2644a2cebbSShijith Thotton #include "ssovf_worker.h"
2744a2cebbSShijith Thotton 
28aa2cbd32SSunila Sahu static uint64_t otx_fpm_iova[CPT_EC_ID_PMAX];
29aa2cbd32SSunila Sahu 
300961348fSMurthy NSSR /* Forward declarations */
310961348fSMurthy NSSR 
320961348fSMurthy NSSR static int
330961348fSMurthy NSSR otx_cpt_que_pair_release(struct rte_cryptodev *dev, uint16_t que_pair_id);
340961348fSMurthy NSSR 
350dc1cffaSAnkur Dwivedi /* Alarm routines */
360dc1cffaSAnkur Dwivedi 
370dc1cffaSAnkur Dwivedi static void
380dc1cffaSAnkur Dwivedi otx_cpt_alarm_cb(void *arg)
390dc1cffaSAnkur Dwivedi {
400dc1cffaSAnkur Dwivedi 	struct cpt_vf *cptvf = arg;
410dc1cffaSAnkur Dwivedi 	otx_cpt_poll_misc(cptvf);
420dc1cffaSAnkur Dwivedi 	rte_eal_alarm_set(CPT_INTR_POLL_INTERVAL_MS * 1000,
430dc1cffaSAnkur Dwivedi 			  otx_cpt_alarm_cb, cptvf);
440dc1cffaSAnkur Dwivedi }
450dc1cffaSAnkur Dwivedi 
460dc1cffaSAnkur Dwivedi static int
470dc1cffaSAnkur Dwivedi otx_cpt_periodic_alarm_start(void *arg)
480dc1cffaSAnkur Dwivedi {
490dc1cffaSAnkur Dwivedi 	return rte_eal_alarm_set(CPT_INTR_POLL_INTERVAL_MS * 1000,
500dc1cffaSAnkur Dwivedi 				 otx_cpt_alarm_cb, arg);
510dc1cffaSAnkur Dwivedi }
520dc1cffaSAnkur Dwivedi 
53273487f7SAnoob Joseph static int
54273487f7SAnoob Joseph otx_cpt_periodic_alarm_stop(void *arg)
55273487f7SAnoob Joseph {
56273487f7SAnoob Joseph 	return rte_eal_alarm_cancel(otx_cpt_alarm_cb, arg);
57273487f7SAnoob Joseph }
58273487f7SAnoob Joseph 
590906b99fSMurthy NSSR /* PMD ops */
600906b99fSMurthy NSSR 
610906b99fSMurthy NSSR static int
62aa2cbd32SSunila Sahu otx_cpt_dev_config(struct rte_cryptodev *dev,
630906b99fSMurthy NSSR 		   struct rte_cryptodev_config *config __rte_unused)
640906b99fSMurthy NSSR {
65aa2cbd32SSunila Sahu 	int ret = 0;
66aa2cbd32SSunila Sahu 
670906b99fSMurthy NSSR 	CPT_PMD_INIT_FUNC_TRACE();
68aa2cbd32SSunila Sahu 
69aa2cbd32SSunila Sahu 	if (dev->feature_flags & RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO)
70aa2cbd32SSunila Sahu 		/* Initialize shared FPM table */
71aa2cbd32SSunila Sahu 		ret = cpt_fpm_init(otx_fpm_iova);
72aa2cbd32SSunila Sahu 
73aa2cbd32SSunila Sahu 	return ret;
740906b99fSMurthy NSSR }
750906b99fSMurthy NSSR 
760906b99fSMurthy NSSR static int
770906b99fSMurthy NSSR otx_cpt_dev_start(struct rte_cryptodev *c_dev)
780906b99fSMurthy NSSR {
790906b99fSMurthy NSSR 	void *cptvf = c_dev->data->dev_private;
800906b99fSMurthy NSSR 
810906b99fSMurthy NSSR 	CPT_PMD_INIT_FUNC_TRACE();
820906b99fSMurthy NSSR 
830906b99fSMurthy NSSR 	return otx_cpt_start_device(cptvf);
840906b99fSMurthy NSSR }
850906b99fSMurthy NSSR 
860906b99fSMurthy NSSR static void
870906b99fSMurthy NSSR otx_cpt_dev_stop(struct rte_cryptodev *c_dev)
880906b99fSMurthy NSSR {
890906b99fSMurthy NSSR 	void *cptvf = c_dev->data->dev_private;
900906b99fSMurthy NSSR 
910906b99fSMurthy NSSR 	CPT_PMD_INIT_FUNC_TRACE();
920906b99fSMurthy NSSR 
93aa2cbd32SSunila Sahu 	if (c_dev->feature_flags & RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO)
94aa2cbd32SSunila Sahu 		cpt_fpm_clear();
95aa2cbd32SSunila Sahu 
960906b99fSMurthy NSSR 	otx_cpt_stop_device(cptvf);
970906b99fSMurthy NSSR }
980906b99fSMurthy NSSR 
990906b99fSMurthy NSSR static int
1000906b99fSMurthy NSSR otx_cpt_dev_close(struct rte_cryptodev *c_dev)
1010906b99fSMurthy NSSR {
1020906b99fSMurthy NSSR 	void *cptvf = c_dev->data->dev_private;
1030961348fSMurthy NSSR 	int i, ret;
1040906b99fSMurthy NSSR 
1050906b99fSMurthy NSSR 	CPT_PMD_INIT_FUNC_TRACE();
1060906b99fSMurthy NSSR 
1070961348fSMurthy NSSR 	for (i = 0; i < c_dev->data->nb_queue_pairs; i++) {
1080961348fSMurthy NSSR 		ret = otx_cpt_que_pair_release(c_dev, i);
1090961348fSMurthy NSSR 		if (ret)
1100961348fSMurthy NSSR 			return ret;
1110961348fSMurthy NSSR 	}
1120961348fSMurthy NSSR 
1130906b99fSMurthy NSSR 	otx_cpt_periodic_alarm_stop(cptvf);
1140906b99fSMurthy NSSR 	otx_cpt_deinit_device(cptvf);
1150906b99fSMurthy NSSR 
1160906b99fSMurthy NSSR 	return 0;
1170906b99fSMurthy NSSR }
1180906b99fSMurthy NSSR 
1190906b99fSMurthy NSSR static void
1200906b99fSMurthy NSSR otx_cpt_dev_info_get(struct rte_cryptodev *dev, struct rte_cryptodev_info *info)
1210906b99fSMurthy NSSR {
1220906b99fSMurthy NSSR 	CPT_PMD_INIT_FUNC_TRACE();
1230906b99fSMurthy NSSR 	if (info != NULL) {
1240906b99fSMurthy NSSR 		info->max_nb_queue_pairs = CPT_NUM_QS_PER_VF;
1250906b99fSMurthy NSSR 		info->feature_flags = dev->feature_flags;
12633bcaae5SKanaka Durga Kotamarthy 		info->capabilities = otx_get_capabilities(info->feature_flags);
1270906b99fSMurthy NSSR 		info->sym.max_nb_sessions = 0;
1280906b99fSMurthy NSSR 		info->driver_id = otx_cryptodev_driver_id;
1290906b99fSMurthy NSSR 		info->min_mbuf_headroom_req = OTX_CPT_MIN_HEADROOM_REQ;
1300906b99fSMurthy NSSR 		info->min_mbuf_tailroom_req = OTX_CPT_MIN_TAILROOM_REQ;
1310906b99fSMurthy NSSR 	}
1320906b99fSMurthy NSSR }
1330906b99fSMurthy NSSR 
1340961348fSMurthy NSSR static int
1350961348fSMurthy NSSR otx_cpt_que_pair_setup(struct rte_cryptodev *dev,
1360961348fSMurthy NSSR 		       uint16_t que_pair_id,
1370961348fSMurthy NSSR 		       const struct rte_cryptodev_qp_conf *qp_conf,
138725d2a7fSFan Zhang 		       int socket_id __rte_unused)
1390961348fSMurthy NSSR {
1400961348fSMurthy NSSR 	struct cpt_instance *instance = NULL;
1410961348fSMurthy NSSR 	struct rte_pci_device *pci_dev;
1420961348fSMurthy NSSR 	int ret = -1;
1430961348fSMurthy NSSR 
1440961348fSMurthy NSSR 	CPT_PMD_INIT_FUNC_TRACE();
1450961348fSMurthy NSSR 
1460961348fSMurthy NSSR 	if (dev->data->queue_pairs[que_pair_id] != NULL) {
1470961348fSMurthy NSSR 		ret = otx_cpt_que_pair_release(dev, que_pair_id);
1480961348fSMurthy NSSR 		if (ret)
1490961348fSMurthy NSSR 			return ret;
1500961348fSMurthy NSSR 	}
1510961348fSMurthy NSSR 
1520961348fSMurthy NSSR 	if (qp_conf->nb_descriptors > DEFAULT_CMD_QLEN) {
1530961348fSMurthy NSSR 		CPT_LOG_INFO("Number of descriptors too big %d, using default "
1540961348fSMurthy NSSR 			     "queue length of %d", qp_conf->nb_descriptors,
1550961348fSMurthy NSSR 			     DEFAULT_CMD_QLEN);
1560961348fSMurthy NSSR 	}
1570961348fSMurthy NSSR 
1580961348fSMurthy NSSR 	pci_dev = RTE_DEV_TO_PCI(dev->device);
1590961348fSMurthy NSSR 
1600961348fSMurthy NSSR 	if (pci_dev->mem_resource[0].addr == NULL) {
1610961348fSMurthy NSSR 		CPT_LOG_ERR("PCI mem address null");
1620961348fSMurthy NSSR 		return -EIO;
1630961348fSMurthy NSSR 	}
1640961348fSMurthy NSSR 
165ec54bc9dSAnoob Joseph 	ret = otx_cpt_get_resource(dev, 0, &instance, que_pair_id);
166accf8ca7SAnoob Joseph 	if (ret != 0 || instance == NULL) {
1670961348fSMurthy NSSR 		CPT_LOG_ERR("Error getting instance handle from device %s : "
1680961348fSMurthy NSSR 			    "ret = %d", dev->data->name, ret);
1690961348fSMurthy NSSR 		return ret;
1700961348fSMurthy NSSR 	}
1710961348fSMurthy NSSR 
1720961348fSMurthy NSSR 	instance->queue_id = que_pair_id;
173f194f198SAnoob Joseph 	instance->sess_mp = qp_conf->mp_session;
174f194f198SAnoob Joseph 	instance->sess_mp_priv = qp_conf->mp_session_private;
1750961348fSMurthy NSSR 	dev->data->queue_pairs[que_pair_id] = instance;
1760961348fSMurthy NSSR 
1770961348fSMurthy NSSR 	return 0;
1780961348fSMurthy NSSR }
1790961348fSMurthy NSSR 
1800961348fSMurthy NSSR static int
1810961348fSMurthy NSSR otx_cpt_que_pair_release(struct rte_cryptodev *dev, uint16_t que_pair_id)
1820961348fSMurthy NSSR {
1830961348fSMurthy NSSR 	struct cpt_instance *instance = dev->data->queue_pairs[que_pair_id];
1840961348fSMurthy NSSR 	int ret;
1850961348fSMurthy NSSR 
1860961348fSMurthy NSSR 	CPT_PMD_INIT_FUNC_TRACE();
1870961348fSMurthy NSSR 
1880961348fSMurthy NSSR 	ret = otx_cpt_put_resource(instance);
1890961348fSMurthy NSSR 	if (ret != 0) {
1900961348fSMurthy NSSR 		CPT_LOG_ERR("Error putting instance handle of device %s : "
1910961348fSMurthy NSSR 			    "ret = %d", dev->data->name, ret);
1920961348fSMurthy NSSR 		return ret;
1930961348fSMurthy NSSR 	}
1940961348fSMurthy NSSR 
1950961348fSMurthy NSSR 	dev->data->queue_pairs[que_pair_id] = NULL;
1960961348fSMurthy NSSR 
1970961348fSMurthy NSSR 	return 0;
1980961348fSMurthy NSSR }
1990961348fSMurthy NSSR 
20043d01767SNithin Dabilpuram static unsigned int
20143d01767SNithin Dabilpuram otx_cpt_get_session_size(struct rte_cryptodev *dev __rte_unused)
20243d01767SNithin Dabilpuram {
20343d01767SNithin Dabilpuram 	return cpt_get_session_size();
20443d01767SNithin Dabilpuram }
20543d01767SNithin Dabilpuram 
206caeba506SAnoob Joseph static int
207caeba506SAnoob Joseph sym_xform_verify(struct rte_crypto_sym_xform *xform)
20843d01767SNithin Dabilpuram {
209caeba506SAnoob Joseph 	if (xform->next) {
210caeba506SAnoob Joseph 		if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
211caeba506SAnoob Joseph 		    xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
212de5eb0a6STejasree Kondoj 		    xform->next->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT &&
213de5eb0a6STejasree Kondoj 		    (xform->auth.algo != RTE_CRYPTO_AUTH_SHA1_HMAC ||
214de5eb0a6STejasree Kondoj 		     xform->next->cipher.algo != RTE_CRYPTO_CIPHER_AES_CBC))
215caeba506SAnoob Joseph 			return -ENOTSUP;
21643d01767SNithin Dabilpuram 
217caeba506SAnoob Joseph 		if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
218caeba506SAnoob Joseph 		    xform->cipher.op == RTE_CRYPTO_CIPHER_OP_DECRYPT &&
219de5eb0a6STejasree Kondoj 		    xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
220de5eb0a6STejasree Kondoj 		    (xform->cipher.algo != RTE_CRYPTO_CIPHER_AES_CBC ||
221de5eb0a6STejasree Kondoj 		     xform->next->auth.algo != RTE_CRYPTO_AUTH_SHA1_HMAC))
222caeba506SAnoob Joseph 			return -ENOTSUP;
223caeba506SAnoob Joseph 
224caeba506SAnoob Joseph 		if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
225caeba506SAnoob Joseph 		    xform->cipher.algo == RTE_CRYPTO_CIPHER_3DES_CBC &&
226caeba506SAnoob Joseph 		    xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
227caeba506SAnoob Joseph 		    xform->next->auth.algo == RTE_CRYPTO_AUTH_SHA1)
228caeba506SAnoob Joseph 			return -ENOTSUP;
229caeba506SAnoob Joseph 
230caeba506SAnoob Joseph 		if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
231caeba506SAnoob Joseph 		    xform->auth.algo == RTE_CRYPTO_AUTH_SHA1 &&
232caeba506SAnoob Joseph 		    xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
233caeba506SAnoob Joseph 		    xform->next->cipher.algo == RTE_CRYPTO_CIPHER_3DES_CBC)
234caeba506SAnoob Joseph 			return -ENOTSUP;
235caeba506SAnoob Joseph 
236caeba506SAnoob Joseph 	} else {
237caeba506SAnoob Joseph 		if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
238caeba506SAnoob Joseph 		    xform->auth.algo == RTE_CRYPTO_AUTH_NULL &&
239caeba506SAnoob Joseph 		    xform->auth.op == RTE_CRYPTO_AUTH_OP_VERIFY)
240caeba506SAnoob Joseph 			return -ENOTSUP;
241caeba506SAnoob Joseph 	}
242caeba506SAnoob Joseph 	return 0;
243caeba506SAnoob Joseph }
244caeba506SAnoob Joseph 
245caeba506SAnoob Joseph static int
246caeba506SAnoob Joseph sym_session_configure(int driver_id, struct rte_crypto_sym_xform *xform,
247caeba506SAnoob Joseph 		      struct rte_cryptodev_sym_session *sess,
248caeba506SAnoob Joseph 		      struct rte_mempool *pool)
249caeba506SAnoob Joseph {
2507293bae1SArchana Muniganti 	struct rte_crypto_sym_xform *temp_xform = xform;
251caeba506SAnoob Joseph 	struct cpt_sess_misc *misc;
2526045c06aSArchana Muniganti 	vq_cmd_word3_t vq_cmd_w3;
253caeba506SAnoob Joseph 	void *priv;
254caeba506SAnoob Joseph 	int ret;
255caeba506SAnoob Joseph 
256caeba506SAnoob Joseph 	ret = sym_xform_verify(xform);
257caeba506SAnoob Joseph 	if (unlikely(ret))
258caeba506SAnoob Joseph 		return ret;
259caeba506SAnoob Joseph 
260caeba506SAnoob Joseph 	if (unlikely(rte_mempool_get(pool, &priv))) {
261caeba506SAnoob Joseph 		CPT_LOG_ERR("Could not allocate session private data");
262caeba506SAnoob Joseph 		return -ENOMEM;
263caeba506SAnoob Joseph 	}
264caeba506SAnoob Joseph 
2650b345f41SAnkur Dwivedi 	memset(priv, 0, sizeof(struct cpt_sess_misc) +
2666045c06aSArchana Muniganti 			offsetof(struct cpt_ctx, mc_ctx));
2670b345f41SAnkur Dwivedi 
268caeba506SAnoob Joseph 	misc = priv;
269caeba506SAnoob Joseph 
270caeba506SAnoob Joseph 	for ( ; xform != NULL; xform = xform->next) {
271caeba506SAnoob Joseph 		switch (xform->type) {
272caeba506SAnoob Joseph 		case RTE_CRYPTO_SYM_XFORM_AEAD:
273caeba506SAnoob Joseph 			ret = fill_sess_aead(xform, misc);
274caeba506SAnoob Joseph 			break;
275caeba506SAnoob Joseph 		case RTE_CRYPTO_SYM_XFORM_CIPHER:
276caeba506SAnoob Joseph 			ret = fill_sess_cipher(xform, misc);
277caeba506SAnoob Joseph 			break;
278caeba506SAnoob Joseph 		case RTE_CRYPTO_SYM_XFORM_AUTH:
279caeba506SAnoob Joseph 			if (xform->auth.algo == RTE_CRYPTO_AUTH_AES_GMAC)
280caeba506SAnoob Joseph 				ret = fill_sess_gmac(xform, misc);
281caeba506SAnoob Joseph 			else
282caeba506SAnoob Joseph 				ret = fill_sess_auth(xform, misc);
283caeba506SAnoob Joseph 			break;
284caeba506SAnoob Joseph 		default:
285caeba506SAnoob Joseph 			ret = -1;
286caeba506SAnoob Joseph 		}
287caeba506SAnoob Joseph 
288caeba506SAnoob Joseph 		if (ret)
289caeba506SAnoob Joseph 			goto priv_put;
290caeba506SAnoob Joseph 	}
291caeba506SAnoob Joseph 
2927293bae1SArchana Muniganti 	if ((GET_SESS_FC_TYPE(misc) == HASH_HMAC) &&
2937293bae1SArchana Muniganti 			cpt_mac_len_verify(&temp_xform->auth)) {
2947293bae1SArchana Muniganti 		CPT_LOG_ERR("MAC length is not supported");
295*db06451bSAnoob Joseph 		struct cpt_ctx *ctx = SESS_PRIV(misc);
296*db06451bSAnoob Joseph 		if (ctx->auth_key != NULL) {
297*db06451bSAnoob Joseph 			rte_free(ctx->auth_key);
298*db06451bSAnoob Joseph 			ctx->auth_key = NULL;
299*db06451bSAnoob Joseph 		}
3007293bae1SArchana Muniganti 		ret = -ENOTSUP;
3017293bae1SArchana Muniganti 		goto priv_put;
3027293bae1SArchana Muniganti 	}
3037293bae1SArchana Muniganti 
304caeba506SAnoob Joseph 	set_sym_session_private_data(sess, driver_id, priv);
305caeba506SAnoob Joseph 
306caeba506SAnoob Joseph 	misc->ctx_dma_addr = rte_mempool_virt2iova(misc) +
30743d01767SNithin Dabilpuram 			     sizeof(struct cpt_sess_misc);
308caeba506SAnoob Joseph 
3096045c06aSArchana Muniganti 	vq_cmd_w3.u64 = 0;
3106045c06aSArchana Muniganti 	vq_cmd_w3.s.grp = 0;
3116045c06aSArchana Muniganti 	vq_cmd_w3.s.cptr = misc->ctx_dma_addr + offsetof(struct cpt_ctx,
3126045c06aSArchana Muniganti 							 mc_ctx);
3136045c06aSArchana Muniganti 
3146045c06aSArchana Muniganti 	misc->cpt_inst_w7 = vq_cmd_w3.u64;
3156045c06aSArchana Muniganti 
316caeba506SAnoob Joseph 	return 0;
317caeba506SAnoob Joseph 
318caeba506SAnoob Joseph priv_put:
319caeba506SAnoob Joseph 	if (priv)
320caeba506SAnoob Joseph 		rte_mempool_put(pool, priv);
321caeba506SAnoob Joseph 	return -ENOTSUP;
322caeba506SAnoob Joseph }
323caeba506SAnoob Joseph 
324caeba506SAnoob Joseph static void
325caeba506SAnoob Joseph sym_session_clear(int driver_id, struct rte_cryptodev_sym_session *sess)
326caeba506SAnoob Joseph {
327caeba506SAnoob Joseph 	void *priv = get_sym_session_private_data(sess, driver_id);
328*db06451bSAnoob Joseph 	struct cpt_sess_misc *misc;
329caeba506SAnoob Joseph 	struct rte_mempool *pool;
330*db06451bSAnoob Joseph 	struct cpt_ctx *ctx;
331caeba506SAnoob Joseph 
332caeba506SAnoob Joseph 	if (priv == NULL)
333caeba506SAnoob Joseph 		return;
334caeba506SAnoob Joseph 
335*db06451bSAnoob Joseph 	misc = priv;
336*db06451bSAnoob Joseph 	ctx = SESS_PRIV(misc);
337*db06451bSAnoob Joseph 
338*db06451bSAnoob Joseph 	if (ctx->auth_key != NULL)
339*db06451bSAnoob Joseph 		rte_free(ctx->auth_key);
340*db06451bSAnoob Joseph 
341caeba506SAnoob Joseph 	memset(priv, 0, cpt_get_session_size());
342caeba506SAnoob Joseph 
343caeba506SAnoob Joseph 	pool = rte_mempool_from_obj(priv);
344caeba506SAnoob Joseph 
345caeba506SAnoob Joseph 	set_sym_session_private_data(sess, driver_id, NULL);
346caeba506SAnoob Joseph 
347caeba506SAnoob Joseph 	rte_mempool_put(pool, priv);
34843d01767SNithin Dabilpuram }
34943d01767SNithin Dabilpuram 
35043d01767SNithin Dabilpuram static int
35143d01767SNithin Dabilpuram otx_cpt_session_cfg(struct rte_cryptodev *dev,
35243d01767SNithin Dabilpuram 		    struct rte_crypto_sym_xform *xform,
35343d01767SNithin Dabilpuram 		    struct rte_cryptodev_sym_session *sess,
354caeba506SAnoob Joseph 		    struct rte_mempool *pool)
35543d01767SNithin Dabilpuram {
35643d01767SNithin Dabilpuram 	CPT_PMD_INIT_FUNC_TRACE();
35743d01767SNithin Dabilpuram 
358caeba506SAnoob Joseph 	return sym_session_configure(dev->driver_id, xform, sess, pool);
35943d01767SNithin Dabilpuram }
36043d01767SNithin Dabilpuram 
36143d01767SNithin Dabilpuram 
36243d01767SNithin Dabilpuram static void
36343d01767SNithin Dabilpuram otx_cpt_session_clear(struct rte_cryptodev *dev,
36443d01767SNithin Dabilpuram 		  struct rte_cryptodev_sym_session *sess)
36543d01767SNithin Dabilpuram {
36643d01767SNithin Dabilpuram 	CPT_PMD_INIT_FUNC_TRACE();
367caeba506SAnoob Joseph 
368caeba506SAnoob Joseph 	return sym_session_clear(dev->driver_id, sess);
36943d01767SNithin Dabilpuram }
37043d01767SNithin Dabilpuram 
37133bcaae5SKanaka Durga Kotamarthy static unsigned int
37233bcaae5SKanaka Durga Kotamarthy otx_cpt_asym_session_size_get(struct rte_cryptodev *dev __rte_unused)
37333bcaae5SKanaka Durga Kotamarthy {
37433bcaae5SKanaka Durga Kotamarthy 	return sizeof(struct cpt_asym_sess_misc);
37533bcaae5SKanaka Durga Kotamarthy }
37633bcaae5SKanaka Durga Kotamarthy 
37733bcaae5SKanaka Durga Kotamarthy static int
37833bcaae5SKanaka Durga Kotamarthy otx_cpt_asym_session_cfg(struct rte_cryptodev *dev,
37933bcaae5SKanaka Durga Kotamarthy 			 struct rte_crypto_asym_xform *xform __rte_unused,
38033bcaae5SKanaka Durga Kotamarthy 			 struct rte_cryptodev_asym_session *sess,
38133bcaae5SKanaka Durga Kotamarthy 			 struct rte_mempool *pool)
38233bcaae5SKanaka Durga Kotamarthy {
38333bcaae5SKanaka Durga Kotamarthy 	struct cpt_asym_sess_misc *priv;
38433bcaae5SKanaka Durga Kotamarthy 	int ret;
38533bcaae5SKanaka Durga Kotamarthy 
38633bcaae5SKanaka Durga Kotamarthy 	CPT_PMD_INIT_FUNC_TRACE();
38733bcaae5SKanaka Durga Kotamarthy 
38833bcaae5SKanaka Durga Kotamarthy 	if (rte_mempool_get(pool, (void **)&priv)) {
38933bcaae5SKanaka Durga Kotamarthy 		CPT_LOG_ERR("Could not allocate session private data");
39033bcaae5SKanaka Durga Kotamarthy 		return -ENOMEM;
39133bcaae5SKanaka Durga Kotamarthy 	}
39233bcaae5SKanaka Durga Kotamarthy 
39333bcaae5SKanaka Durga Kotamarthy 	memset(priv, 0, sizeof(struct cpt_asym_sess_misc));
39433bcaae5SKanaka Durga Kotamarthy 
39533bcaae5SKanaka Durga Kotamarthy 	ret = cpt_fill_asym_session_parameters(priv, xform);
39633bcaae5SKanaka Durga Kotamarthy 	if (ret) {
39733bcaae5SKanaka Durga Kotamarthy 		CPT_LOG_ERR("Could not configure session parameters");
39833bcaae5SKanaka Durga Kotamarthy 
39933bcaae5SKanaka Durga Kotamarthy 		/* Return session to mempool */
40033bcaae5SKanaka Durga Kotamarthy 		rte_mempool_put(pool, priv);
40133bcaae5SKanaka Durga Kotamarthy 		return ret;
40233bcaae5SKanaka Durga Kotamarthy 	}
40333bcaae5SKanaka Durga Kotamarthy 
4046045c06aSArchana Muniganti 	priv->cpt_inst_w7 = 0;
4056045c06aSArchana Muniganti 
40633bcaae5SKanaka Durga Kotamarthy 	set_asym_session_private_data(sess, dev->driver_id, priv);
40733bcaae5SKanaka Durga Kotamarthy 	return 0;
40833bcaae5SKanaka Durga Kotamarthy }
40933bcaae5SKanaka Durga Kotamarthy 
41033bcaae5SKanaka Durga Kotamarthy static void
41133bcaae5SKanaka Durga Kotamarthy otx_cpt_asym_session_clear(struct rte_cryptodev *dev,
41233bcaae5SKanaka Durga Kotamarthy 			   struct rte_cryptodev_asym_session *sess)
41333bcaae5SKanaka Durga Kotamarthy {
41433bcaae5SKanaka Durga Kotamarthy 	struct cpt_asym_sess_misc *priv;
41533bcaae5SKanaka Durga Kotamarthy 	struct rte_mempool *sess_mp;
41633bcaae5SKanaka Durga Kotamarthy 
41733bcaae5SKanaka Durga Kotamarthy 	CPT_PMD_INIT_FUNC_TRACE();
41833bcaae5SKanaka Durga Kotamarthy 
41933bcaae5SKanaka Durga Kotamarthy 	priv = get_asym_session_private_data(sess, dev->driver_id);
42033bcaae5SKanaka Durga Kotamarthy 
42133bcaae5SKanaka Durga Kotamarthy 	if (priv == NULL)
42233bcaae5SKanaka Durga Kotamarthy 		return;
42333bcaae5SKanaka Durga Kotamarthy 
42433bcaae5SKanaka Durga Kotamarthy 	/* Free resources allocated during session configure */
42533bcaae5SKanaka Durga Kotamarthy 	cpt_free_asym_session_parameters(priv);
42633bcaae5SKanaka Durga Kotamarthy 	memset(priv, 0, otx_cpt_asym_session_size_get(dev));
42733bcaae5SKanaka Durga Kotamarthy 	sess_mp = rte_mempool_from_obj(priv);
42833bcaae5SKanaka Durga Kotamarthy 	set_asym_session_private_data(sess, dev->driver_id, NULL);
42933bcaae5SKanaka Durga Kotamarthy 	rte_mempool_put(sess_mp, priv);
43033bcaae5SKanaka Durga Kotamarthy }
43133bcaae5SKanaka Durga Kotamarthy 
43244a2cebbSShijith Thotton static __rte_always_inline void * __rte_hot
433f194f198SAnoob Joseph otx_cpt_request_enqueue(struct cpt_instance *instance,
434f194f198SAnoob Joseph 			struct pending_queue *pqueue,
4356045c06aSArchana Muniganti 			void *req, uint64_t cpt_inst_w7)
436f194f198SAnoob Joseph {
437f194f198SAnoob Joseph 	struct cpt_request_info *user_req = (struct cpt_request_info *)req;
438f194f198SAnoob Joseph 
43944a2cebbSShijith Thotton 	if (unlikely(pqueue->pending_count >= DEFAULT_CMD_QLEN)) {
44044a2cebbSShijith Thotton 		rte_errno = EAGAIN;
44144a2cebbSShijith Thotton 		return NULL;
44244a2cebbSShijith Thotton 	}
443f194f198SAnoob Joseph 
4446045c06aSArchana Muniganti 	fill_cpt_inst(instance, req, cpt_inst_w7);
445f194f198SAnoob Joseph 
446f194f198SAnoob Joseph 	CPT_LOG_DP_DEBUG("req: %p op: %p ", req, user_req->op);
447f194f198SAnoob Joseph 
448f194f198SAnoob Joseph 	/* Fill time_out cycles */
449f194f198SAnoob Joseph 	user_req->time_out = rte_get_timer_cycles() +
450f194f198SAnoob Joseph 			DEFAULT_COMMAND_TIMEOUT * rte_get_timer_hz();
451f194f198SAnoob Joseph 	user_req->extra_time = 0;
452f194f198SAnoob Joseph 
453f194f198SAnoob Joseph 	/* Default mode of software queue */
454f194f198SAnoob Joseph 	mark_cpt_inst(instance);
455f194f198SAnoob Joseph 
456f194f198SAnoob Joseph 	CPT_LOG_DP_DEBUG("Submitted NB cmd with request: %p "
457f194f198SAnoob Joseph 			 "op: %p", user_req, user_req->op);
45844a2cebbSShijith Thotton 	return req;
459f194f198SAnoob Joseph }
460f194f198SAnoob Joseph 
46144a2cebbSShijith Thotton static __rte_always_inline void * __rte_hot
462e9a356e2SSunila Sahu otx_cpt_enq_single_asym(struct cpt_instance *instance,
463e9a356e2SSunila Sahu 			struct rte_crypto_op *op,
464e9a356e2SSunila Sahu 			struct pending_queue *pqueue)
465e9a356e2SSunila Sahu {
466e9a356e2SSunila Sahu 	struct cpt_qp_meta_info *minfo = &instance->meta_info;
467e9a356e2SSunila Sahu 	struct rte_crypto_asym_op *asym_op = op->asym;
468e9a356e2SSunila Sahu 	struct asym_op_params params = {0};
469e9a356e2SSunila Sahu 	struct cpt_asym_sess_misc *sess;
470e9a356e2SSunila Sahu 	uintptr_t *cop;
471e9a356e2SSunila Sahu 	void *mdata;
47244a2cebbSShijith Thotton 	void *req;
473e9a356e2SSunila Sahu 	int ret;
474e9a356e2SSunila Sahu 
475e9a356e2SSunila Sahu 	if (unlikely(rte_mempool_get(minfo->pool, &mdata) < 0)) {
476e9a356e2SSunila Sahu 		CPT_LOG_DP_ERR("Could not allocate meta buffer for request");
47744a2cebbSShijith Thotton 		rte_errno = ENOMEM;
47844a2cebbSShijith Thotton 		return NULL;
479e9a356e2SSunila Sahu 	}
480e9a356e2SSunila Sahu 
481e9a356e2SSunila Sahu 	sess = get_asym_session_private_data(asym_op->session,
482e9a356e2SSunila Sahu 					     otx_cryptodev_driver_id);
483e9a356e2SSunila Sahu 
484e9a356e2SSunila Sahu 	/* Store phys_addr of the mdata to meta_buf */
485e9a356e2SSunila Sahu 	params.meta_buf = rte_mempool_virt2iova(mdata);
486e9a356e2SSunila Sahu 
487e9a356e2SSunila Sahu 	cop = mdata;
488e9a356e2SSunila Sahu 	cop[0] = (uintptr_t)mdata;
489e9a356e2SSunila Sahu 	cop[1] = (uintptr_t)op;
490e9a356e2SSunila Sahu 	cop[2] = cop[3] = 0ULL;
491e9a356e2SSunila Sahu 
492e9a356e2SSunila Sahu 	params.req = RTE_PTR_ADD(cop, 4 * sizeof(uintptr_t));
493e9a356e2SSunila Sahu 	params.req->op = cop;
494e9a356e2SSunila Sahu 
495e9a356e2SSunila Sahu 	/* Adjust meta_buf by crypto_op data  and request_info struct */
496e9a356e2SSunila Sahu 	params.meta_buf += (4 * sizeof(uintptr_t)) +
497e9a356e2SSunila Sahu 			   sizeof(struct cpt_request_info);
498e9a356e2SSunila Sahu 
499e9a356e2SSunila Sahu 	switch (sess->xfrm_type) {
500e9a356e2SSunila Sahu 	case RTE_CRYPTO_ASYM_XFORM_MODEX:
501e9a356e2SSunila Sahu 		ret = cpt_modex_prep(&params, &sess->mod_ctx);
502e9a356e2SSunila Sahu 		if (unlikely(ret))
503e9a356e2SSunila Sahu 			goto req_fail;
504e9a356e2SSunila Sahu 		break;
505e9a356e2SSunila Sahu 	case RTE_CRYPTO_ASYM_XFORM_RSA:
506e9a356e2SSunila Sahu 		ret = cpt_enqueue_rsa_op(op, &params, sess);
507e9a356e2SSunila Sahu 		if (unlikely(ret))
508e9a356e2SSunila Sahu 			goto req_fail;
509e9a356e2SSunila Sahu 		break;
510aa2cbd32SSunila Sahu 	case RTE_CRYPTO_ASYM_XFORM_ECDSA:
511aa2cbd32SSunila Sahu 		ret = cpt_enqueue_ecdsa_op(op, &params, sess, otx_fpm_iova);
512aa2cbd32SSunila Sahu 		if (unlikely(ret))
513aa2cbd32SSunila Sahu 			goto req_fail;
514aa2cbd32SSunila Sahu 		break;
51599faef83SSunila Sahu 	case RTE_CRYPTO_ASYM_XFORM_ECPM:
51699faef83SSunila Sahu 		ret = cpt_ecpm_prep(&asym_op->ecpm, &params,
51799faef83SSunila Sahu 				    sess->ec_ctx.curveid);
51899faef83SSunila Sahu 		if (unlikely(ret))
51999faef83SSunila Sahu 			goto req_fail;
52099faef83SSunila Sahu 		break;
52199faef83SSunila Sahu 
522e9a356e2SSunila Sahu 	default:
523e9a356e2SSunila Sahu 		op->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
52444a2cebbSShijith Thotton 		rte_errno = EINVAL;
525e9a356e2SSunila Sahu 		goto req_fail;
526e9a356e2SSunila Sahu 	}
527e9a356e2SSunila Sahu 
52844a2cebbSShijith Thotton 	req = otx_cpt_request_enqueue(instance, pqueue, params.req,
5296045c06aSArchana Muniganti 				      sess->cpt_inst_w7);
53044a2cebbSShijith Thotton 	if (unlikely(req == NULL)) {
531e9a356e2SSunila Sahu 		CPT_LOG_DP_ERR("Could not enqueue crypto req");
532e9a356e2SSunila Sahu 		goto req_fail;
533e9a356e2SSunila Sahu 	}
534e9a356e2SSunila Sahu 
53544a2cebbSShijith Thotton 	return req;
536e9a356e2SSunila Sahu 
537e9a356e2SSunila Sahu req_fail:
538e9a356e2SSunila Sahu 	free_op_meta(mdata, minfo->pool);
539e9a356e2SSunila Sahu 
54044a2cebbSShijith Thotton 	return NULL;
541e9a356e2SSunila Sahu }
542e9a356e2SSunila Sahu 
54344a2cebbSShijith Thotton static __rte_always_inline void * __rte_hot
544f194f198SAnoob Joseph otx_cpt_enq_single_sym(struct cpt_instance *instance,
545f194f198SAnoob Joseph 		       struct rte_crypto_op *op,
546f194f198SAnoob Joseph 		       struct pending_queue *pqueue)
547f194f198SAnoob Joseph {
548f194f198SAnoob Joseph 	struct cpt_sess_misc *sess;
549f194f198SAnoob Joseph 	struct rte_crypto_sym_op *sym_op = op->sym;
5506045c06aSArchana Muniganti 	struct cpt_request_info *prep_req;
5516045c06aSArchana Muniganti 	void *mdata = NULL;
552f194f198SAnoob Joseph 	int ret = 0;
55344a2cebbSShijith Thotton 	void *req;
554f194f198SAnoob Joseph 	uint64_t cpt_op;
555f194f198SAnoob Joseph 
556f194f198SAnoob Joseph 	sess = (struct cpt_sess_misc *)
557f194f198SAnoob Joseph 			get_sym_session_private_data(sym_op->session,
558f194f198SAnoob Joseph 						     otx_cryptodev_driver_id);
559f194f198SAnoob Joseph 
560f194f198SAnoob Joseph 	cpt_op = sess->cpt_op;
561f194f198SAnoob Joseph 
562f194f198SAnoob Joseph 	if (likely(cpt_op & CPT_OP_CIPHER_MASK))
563ec54bc9dSAnoob Joseph 		ret = fill_fc_params(op, sess, &instance->meta_info, &mdata,
5646045c06aSArchana Muniganti 				     (void **)&prep_req);
565f194f198SAnoob Joseph 	else
566ec54bc9dSAnoob Joseph 		ret = fill_digest_params(op, sess, &instance->meta_info,
5676045c06aSArchana Muniganti 					 &mdata, (void **)&prep_req);
568f194f198SAnoob Joseph 
569f194f198SAnoob Joseph 	if (unlikely(ret)) {
570f194f198SAnoob Joseph 		CPT_LOG_DP_ERR("prep cryto req : op %p, cpt_op 0x%x "
571f194f198SAnoob Joseph 			       "ret 0x%x", op, (unsigned int)cpt_op, ret);
57244a2cebbSShijith Thotton 		return NULL;
573f194f198SAnoob Joseph 	}
574f194f198SAnoob Joseph 
575f194f198SAnoob Joseph 	/* Enqueue prepared instruction to h/w */
57644a2cebbSShijith Thotton 	req = otx_cpt_request_enqueue(instance, pqueue, prep_req,
5776045c06aSArchana Muniganti 				      sess->cpt_inst_w7);
57844a2cebbSShijith Thotton 	if (unlikely(req == NULL))
579f194f198SAnoob Joseph 		/* Buffer allocated for request preparation need to be freed */
580ec54bc9dSAnoob Joseph 		free_op_meta(mdata, instance->meta_info.pool);
58144a2cebbSShijith Thotton 
58244a2cebbSShijith Thotton 	return req;
583f194f198SAnoob Joseph }
584f194f198SAnoob Joseph 
58544a2cebbSShijith Thotton static __rte_always_inline void * __rte_hot
586f194f198SAnoob Joseph otx_cpt_enq_single_sym_sessless(struct cpt_instance *instance,
587f194f198SAnoob Joseph 				struct rte_crypto_op *op,
588caeba506SAnoob Joseph 				struct pending_queue *pend_q)
589f194f198SAnoob Joseph {
590caeba506SAnoob Joseph 	const int driver_id = otx_cryptodev_driver_id;
591f194f198SAnoob Joseph 	struct rte_crypto_sym_op *sym_op = op->sym;
592caeba506SAnoob Joseph 	struct rte_cryptodev_sym_session *sess;
59344a2cebbSShijith Thotton 	void *req;
594f194f198SAnoob Joseph 	int ret;
595f194f198SAnoob Joseph 
596caeba506SAnoob Joseph 	/* Create temporary session */
5978e177f14SAnkur Dwivedi 	sess = rte_cryptodev_sym_session_create(instance->sess_mp);
59844a2cebbSShijith Thotton 	if (sess == NULL) {
59944a2cebbSShijith Thotton 		rte_errno = ENOMEM;
60044a2cebbSShijith Thotton 		return NULL;
60144a2cebbSShijith Thotton 	}
602f194f198SAnoob Joseph 
603caeba506SAnoob Joseph 	ret = sym_session_configure(driver_id, sym_op->xform, sess,
604caeba506SAnoob Joseph 				    instance->sess_mp_priv);
605caeba506SAnoob Joseph 	if (ret)
606caeba506SAnoob Joseph 		goto sess_put;
607f194f198SAnoob Joseph 
608caeba506SAnoob Joseph 	sym_op->session = sess;
609f194f198SAnoob Joseph 
61044a2cebbSShijith Thotton 	req = otx_cpt_enq_single_sym(instance, op, pend_q);
611f194f198SAnoob Joseph 
61244a2cebbSShijith Thotton 	if (unlikely(req == NULL))
613caeba506SAnoob Joseph 		goto priv_put;
614f194f198SAnoob Joseph 
61544a2cebbSShijith Thotton 	return req;
616f194f198SAnoob Joseph 
617caeba506SAnoob Joseph priv_put:
618caeba506SAnoob Joseph 	sym_session_clear(driver_id, sess);
619caeba506SAnoob Joseph sess_put:
620caeba506SAnoob Joseph 	rte_mempool_put(instance->sess_mp, sess);
62144a2cebbSShijith Thotton 	return NULL;
622f194f198SAnoob Joseph }
623f194f198SAnoob Joseph 
624e9a356e2SSunila Sahu #define OP_TYPE_SYM		0
625e9a356e2SSunila Sahu #define OP_TYPE_ASYM		1
626e9a356e2SSunila Sahu 
62744a2cebbSShijith Thotton static __rte_always_inline void *__rte_hot
628f194f198SAnoob Joseph otx_cpt_enq_single(struct cpt_instance *inst,
629f194f198SAnoob Joseph 		   struct rte_crypto_op *op,
630e9a356e2SSunila Sahu 		   struct pending_queue *pqueue,
631e9a356e2SSunila Sahu 		   const uint8_t op_type)
632f194f198SAnoob Joseph {
633f194f198SAnoob Joseph 	/* Check for the type */
634f194f198SAnoob Joseph 
635e9a356e2SSunila Sahu 	if (op_type == OP_TYPE_SYM) {
636f194f198SAnoob Joseph 		if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION)
637f194f198SAnoob Joseph 			return otx_cpt_enq_single_sym(inst, op, pqueue);
638e9a356e2SSunila Sahu 		else
639e9a356e2SSunila Sahu 			return otx_cpt_enq_single_sym_sessless(inst, op,
640e9a356e2SSunila Sahu 							       pqueue);
641f194f198SAnoob Joseph 	}
642f194f198SAnoob Joseph 
643e9a356e2SSunila Sahu 	if (op_type == OP_TYPE_ASYM) {
644e9a356e2SSunila Sahu 		if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION)
645e9a356e2SSunila Sahu 			return otx_cpt_enq_single_asym(inst, op, pqueue);
646e9a356e2SSunila Sahu 	}
647e9a356e2SSunila Sahu 
648e9a356e2SSunila Sahu 	/* Should not reach here */
64944a2cebbSShijith Thotton 	rte_errno = ENOTSUP;
65044a2cebbSShijith Thotton 	return NULL;
651e9a356e2SSunila Sahu }
652e9a356e2SSunila Sahu 
653e3866e73SThomas Monjalon static  __rte_always_inline uint16_t __rte_hot
654e9a356e2SSunila Sahu otx_cpt_pkt_enqueue(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops,
655e9a356e2SSunila Sahu 		    const uint8_t op_type)
656ac4d88afSTejasree Kondoj {
657ac4d88afSTejasree Kondoj 	struct cpt_instance *instance = (struct cpt_instance *)qptr;
658f194f198SAnoob Joseph 	uint16_t count;
65944a2cebbSShijith Thotton 	void *req;
660ac4d88afSTejasree Kondoj 	struct cpt_vf *cptvf = (struct cpt_vf *)instance;
661ac4d88afSTejasree Kondoj 	struct pending_queue *pqueue = &cptvf->pqueue;
662ac4d88afSTejasree Kondoj 
663ac4d88afSTejasree Kondoj 	count = DEFAULT_CMD_QLEN - pqueue->pending_count;
664ac4d88afSTejasree Kondoj 	if (nb_ops > count)
665ac4d88afSTejasree Kondoj 		nb_ops = count;
666ac4d88afSTejasree Kondoj 
667ac4d88afSTejasree Kondoj 	count = 0;
668ac4d88afSTejasree Kondoj 	while (likely(count < nb_ops)) {
669f194f198SAnoob Joseph 
670f194f198SAnoob Joseph 		/* Enqueue single op */
67144a2cebbSShijith Thotton 		req = otx_cpt_enq_single(instance, ops[count], pqueue, op_type);
672f194f198SAnoob Joseph 
67344a2cebbSShijith Thotton 		if (unlikely(req == NULL))
674ac4d88afSTejasree Kondoj 			break;
67544a2cebbSShijith Thotton 
67644a2cebbSShijith Thotton 		pqueue->req_queue[pqueue->enq_tail] = (uintptr_t)req;
67744a2cebbSShijith Thotton 		MOD_INC(pqueue->enq_tail, DEFAULT_CMD_QLEN);
67844a2cebbSShijith Thotton 		pqueue->pending_count += 1;
679ac4d88afSTejasree Kondoj 		count++;
680ac4d88afSTejasree Kondoj 	}
681ac4d88afSTejasree Kondoj 	otx_cpt_ring_dbell(instance, count);
682ac4d88afSTejasree Kondoj 	return count;
683ac4d88afSTejasree Kondoj }
684ac4d88afSTejasree Kondoj 
685e9a356e2SSunila Sahu static uint16_t
686e9a356e2SSunila Sahu otx_cpt_enqueue_asym(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops)
687e9a356e2SSunila Sahu {
688e9a356e2SSunila Sahu 	return otx_cpt_pkt_enqueue(qptr, ops, nb_ops, OP_TYPE_ASYM);
689e9a356e2SSunila Sahu }
690e9a356e2SSunila Sahu 
691e9a356e2SSunila Sahu static uint16_t
692e9a356e2SSunila Sahu otx_cpt_enqueue_sym(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops)
693e9a356e2SSunila Sahu {
694e9a356e2SSunila Sahu 	return otx_cpt_pkt_enqueue(qptr, ops, nb_ops, OP_TYPE_SYM);
695e9a356e2SSunila Sahu }
696e9a356e2SSunila Sahu 
69744a2cebbSShijith Thotton static __rte_always_inline void
69844a2cebbSShijith Thotton submit_request_to_sso(struct ssows *ws, uintptr_t req,
69944a2cebbSShijith Thotton 		      struct rte_event *rsp_info)
70044a2cebbSShijith Thotton {
70144a2cebbSShijith Thotton 	uint64_t add_work;
70244a2cebbSShijith Thotton 
70344a2cebbSShijith Thotton 	add_work = rsp_info->flow_id | (RTE_EVENT_TYPE_CRYPTODEV << 28) |
70444a2cebbSShijith Thotton 		   ((uint64_t)(rsp_info->sched_type) << 32);
70544a2cebbSShijith Thotton 
70644a2cebbSShijith Thotton 	if (!rsp_info->sched_type)
70744a2cebbSShijith Thotton 		ssows_head_wait(ws);
70844a2cebbSShijith Thotton 
70944a2cebbSShijith Thotton 	rte_atomic_thread_fence(__ATOMIC_RELEASE);
71044a2cebbSShijith Thotton 	ssovf_store_pair(add_work, req, ws->grps[rsp_info->queue_id]);
71144a2cebbSShijith Thotton }
71244a2cebbSShijith Thotton 
71344a2cebbSShijith Thotton static inline union rte_event_crypto_metadata *
71444a2cebbSShijith Thotton get_event_crypto_mdata(struct rte_crypto_op *op)
71544a2cebbSShijith Thotton {
71644a2cebbSShijith Thotton 	union rte_event_crypto_metadata *ec_mdata;
71744a2cebbSShijith Thotton 
71844a2cebbSShijith Thotton 	if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION)
71944a2cebbSShijith Thotton 		ec_mdata = rte_cryptodev_sym_session_get_user_data(
72044a2cebbSShijith Thotton 							   op->sym->session);
72144a2cebbSShijith Thotton 	else if (op->sess_type == RTE_CRYPTO_OP_SESSIONLESS &&
72244a2cebbSShijith Thotton 		 op->private_data_offset)
72344a2cebbSShijith Thotton 		ec_mdata = (union rte_event_crypto_metadata *)
72444a2cebbSShijith Thotton 			((uint8_t *)op + op->private_data_offset);
72544a2cebbSShijith Thotton 	else
72644a2cebbSShijith Thotton 		return NULL;
72744a2cebbSShijith Thotton 
72844a2cebbSShijith Thotton 	return ec_mdata;
72944a2cebbSShijith Thotton }
73044a2cebbSShijith Thotton 
73144a2cebbSShijith Thotton uint16_t __rte_hot
73244a2cebbSShijith Thotton otx_crypto_adapter_enqueue(void *port, struct rte_crypto_op *op)
73344a2cebbSShijith Thotton {
73444a2cebbSShijith Thotton 	union rte_event_crypto_metadata *ec_mdata;
73544a2cebbSShijith Thotton 	struct cpt_instance *instance;
73644a2cebbSShijith Thotton 	struct cpt_request_info *req;
73744a2cebbSShijith Thotton 	struct rte_event *rsp_info;
73844a2cebbSShijith Thotton 	uint8_t op_type, cdev_id;
73944a2cebbSShijith Thotton 	uint16_t qp_id;
74044a2cebbSShijith Thotton 
74144a2cebbSShijith Thotton 	ec_mdata = get_event_crypto_mdata(op);
74244a2cebbSShijith Thotton 	if (unlikely(ec_mdata == NULL)) {
74344a2cebbSShijith Thotton 		rte_errno = EINVAL;
74444a2cebbSShijith Thotton 		return 0;
74544a2cebbSShijith Thotton 	}
74644a2cebbSShijith Thotton 
74744a2cebbSShijith Thotton 	cdev_id = ec_mdata->request_info.cdev_id;
74844a2cebbSShijith Thotton 	qp_id = ec_mdata->request_info.queue_pair_id;
74944a2cebbSShijith Thotton 	rsp_info = &ec_mdata->response_info;
75044a2cebbSShijith Thotton 	instance = rte_cryptodevs[cdev_id].data->queue_pairs[qp_id];
75144a2cebbSShijith Thotton 
75244a2cebbSShijith Thotton 	if (unlikely(!instance->ca_enabled)) {
75344a2cebbSShijith Thotton 		rte_errno = EINVAL;
75444a2cebbSShijith Thotton 		return 0;
75544a2cebbSShijith Thotton 	}
75644a2cebbSShijith Thotton 
75744a2cebbSShijith Thotton 	op_type = op->type == RTE_CRYPTO_OP_TYPE_SYMMETRIC ? OP_TYPE_SYM :
75844a2cebbSShijith Thotton 							     OP_TYPE_ASYM;
75944a2cebbSShijith Thotton 	req = otx_cpt_enq_single(instance, op,
76044a2cebbSShijith Thotton 				 &((struct cpt_vf *)instance)->pqueue, op_type);
76144a2cebbSShijith Thotton 	if (unlikely(req == NULL))
76244a2cebbSShijith Thotton 		return 0;
76344a2cebbSShijith Thotton 
76444a2cebbSShijith Thotton 	otx_cpt_ring_dbell(instance, 1);
76544a2cebbSShijith Thotton 	req->qp = instance;
76644a2cebbSShijith Thotton 	submit_request_to_sso(port, (uintptr_t)req, rsp_info);
76744a2cebbSShijith Thotton 
76844a2cebbSShijith Thotton 	return 1;
76944a2cebbSShijith Thotton }
77044a2cebbSShijith Thotton 
771e9a356e2SSunila Sahu static inline void
772e9a356e2SSunila Sahu otx_cpt_asym_rsa_op(struct rte_crypto_op *cop, struct cpt_request_info *req,
773e9a356e2SSunila Sahu 		    struct rte_crypto_rsa_xform *rsa_ctx)
774e9a356e2SSunila Sahu 
775e9a356e2SSunila Sahu {
776e9a356e2SSunila Sahu 	struct rte_crypto_rsa_op_param *rsa = &cop->asym->rsa;
777e9a356e2SSunila Sahu 
778e9a356e2SSunila Sahu 	switch (rsa->op_type) {
779e9a356e2SSunila Sahu 	case RTE_CRYPTO_ASYM_OP_ENCRYPT:
780e9a356e2SSunila Sahu 		rsa->cipher.length = rsa_ctx->n.length;
781e9a356e2SSunila Sahu 		memcpy(rsa->cipher.data, req->rptr, rsa->cipher.length);
782e9a356e2SSunila Sahu 		break;
783e9a356e2SSunila Sahu 	case RTE_CRYPTO_ASYM_OP_DECRYPT:
784e9a356e2SSunila Sahu 		if (rsa->pad == RTE_CRYPTO_RSA_PADDING_NONE)
785e9a356e2SSunila Sahu 			rsa->message.length = rsa_ctx->n.length;
786e9a356e2SSunila Sahu 		else {
787e9a356e2SSunila Sahu 			/* Get length of decrypted output */
788e9a356e2SSunila Sahu 			rsa->message.length = rte_cpu_to_be_16
789e9a356e2SSunila Sahu 					(*((uint16_t *)req->rptr));
790e9a356e2SSunila Sahu 
791e9a356e2SSunila Sahu 			/* Offset data pointer by length fields */
792e9a356e2SSunila Sahu 			req->rptr += 2;
793e9a356e2SSunila Sahu 		}
794e9a356e2SSunila Sahu 		memcpy(rsa->message.data, req->rptr, rsa->message.length);
795e9a356e2SSunila Sahu 		break;
796e9a356e2SSunila Sahu 	case RTE_CRYPTO_ASYM_OP_SIGN:
797e9a356e2SSunila Sahu 		rsa->sign.length = rsa_ctx->n.length;
798e9a356e2SSunila Sahu 		memcpy(rsa->sign.data, req->rptr, rsa->sign.length);
799e9a356e2SSunila Sahu 		break;
800e9a356e2SSunila Sahu 	case RTE_CRYPTO_ASYM_OP_VERIFY:
801e9a356e2SSunila Sahu 		if (rsa->pad == RTE_CRYPTO_RSA_PADDING_NONE)
802e9a356e2SSunila Sahu 			rsa->sign.length = rsa_ctx->n.length;
803e9a356e2SSunila Sahu 		else {
804e9a356e2SSunila Sahu 			/* Get length of decrypted output */
805e9a356e2SSunila Sahu 			rsa->sign.length = rte_cpu_to_be_16
806e9a356e2SSunila Sahu 					(*((uint16_t *)req->rptr));
807e9a356e2SSunila Sahu 
808e9a356e2SSunila Sahu 			/* Offset data pointer by length fields */
809e9a356e2SSunila Sahu 			req->rptr += 2;
810e9a356e2SSunila Sahu 		}
811e9a356e2SSunila Sahu 		memcpy(rsa->sign.data, req->rptr, rsa->sign.length);
812e9a356e2SSunila Sahu 
813e9a356e2SSunila Sahu 		if (memcmp(rsa->sign.data, rsa->message.data,
814e9a356e2SSunila Sahu 			   rsa->message.length)) {
815e9a356e2SSunila Sahu 			CPT_LOG_DP_ERR("RSA verification failed");
816e9a356e2SSunila Sahu 			cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
817e9a356e2SSunila Sahu 		}
818e9a356e2SSunila Sahu 		break;
819e9a356e2SSunila Sahu 	default:
820e9a356e2SSunila Sahu 		CPT_LOG_DP_DEBUG("Invalid RSA operation type");
821e9a356e2SSunila Sahu 		cop->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
822e9a356e2SSunila Sahu 		break;
823e9a356e2SSunila Sahu 	}
824e9a356e2SSunila Sahu }
825e9a356e2SSunila Sahu 
826aa2cbd32SSunila Sahu static __rte_always_inline void
827aa2cbd32SSunila Sahu otx_cpt_asym_dequeue_ecdsa_op(struct rte_crypto_ecdsa_op_param *ecdsa,
828aa2cbd32SSunila Sahu 			    struct cpt_request_info *req,
829aa2cbd32SSunila Sahu 			    struct cpt_asym_ec_ctx *ec)
830aa2cbd32SSunila Sahu 
831aa2cbd32SSunila Sahu {
832aa2cbd32SSunila Sahu 	int prime_len = ec_grp[ec->curveid].prime.length;
833aa2cbd32SSunila Sahu 
834aa2cbd32SSunila Sahu 	if (ecdsa->op_type == RTE_CRYPTO_ASYM_OP_VERIFY)
835aa2cbd32SSunila Sahu 		return;
836aa2cbd32SSunila Sahu 
837aa2cbd32SSunila Sahu 	/* Separate out sign r and s components */
838aa2cbd32SSunila Sahu 	memcpy(ecdsa->r.data, req->rptr, prime_len);
839ecd070acSArchana Muniganti 	memcpy(ecdsa->s.data, req->rptr + RTE_ALIGN_CEIL(prime_len, 8),
840ecd070acSArchana Muniganti 	       prime_len);
841aa2cbd32SSunila Sahu 	ecdsa->r.length = prime_len;
842aa2cbd32SSunila Sahu 	ecdsa->s.length = prime_len;
843aa2cbd32SSunila Sahu }
844aa2cbd32SSunila Sahu 
84599faef83SSunila Sahu static __rte_always_inline void
84699faef83SSunila Sahu otx_cpt_asym_dequeue_ecpm_op(struct rte_crypto_ecpm_op_param *ecpm,
84799faef83SSunila Sahu 			     struct cpt_request_info *req,
84899faef83SSunila Sahu 			     struct cpt_asym_ec_ctx *ec)
84999faef83SSunila Sahu {
85099faef83SSunila Sahu 	int prime_len = ec_grp[ec->curveid].prime.length;
85199faef83SSunila Sahu 
85299faef83SSunila Sahu 	memcpy(ecpm->r.x.data, req->rptr, prime_len);
853ecd070acSArchana Muniganti 	memcpy(ecpm->r.y.data, req->rptr + RTE_ALIGN_CEIL(prime_len, 8),
854ecd070acSArchana Muniganti 	       prime_len);
85599faef83SSunila Sahu 	ecpm->r.x.length = prime_len;
85699faef83SSunila Sahu 	ecpm->r.y.length = prime_len;
85799faef83SSunila Sahu }
85899faef83SSunila Sahu 
859e3866e73SThomas Monjalon static __rte_always_inline void __rte_hot
860e9a356e2SSunila Sahu otx_cpt_asym_post_process(struct rte_crypto_op *cop,
861e9a356e2SSunila Sahu 			  struct cpt_request_info *req)
862e9a356e2SSunila Sahu {
863e9a356e2SSunila Sahu 	struct rte_crypto_asym_op *op = cop->asym;
864e9a356e2SSunila Sahu 	struct cpt_asym_sess_misc *sess;
865e9a356e2SSunila Sahu 
866e9a356e2SSunila Sahu 	sess = get_asym_session_private_data(op->session,
867e9a356e2SSunila Sahu 					     otx_cryptodev_driver_id);
868e9a356e2SSunila Sahu 
869e9a356e2SSunila Sahu 	switch (sess->xfrm_type) {
870e9a356e2SSunila Sahu 	case RTE_CRYPTO_ASYM_XFORM_RSA:
871e9a356e2SSunila Sahu 		otx_cpt_asym_rsa_op(cop, req, &sess->rsa_ctx);
872e9a356e2SSunila Sahu 		break;
873e9a356e2SSunila Sahu 	case RTE_CRYPTO_ASYM_XFORM_MODEX:
874e9a356e2SSunila Sahu 		op->modex.result.length = sess->mod_ctx.modulus.length;
875e9a356e2SSunila Sahu 		memcpy(op->modex.result.data, req->rptr,
876e9a356e2SSunila Sahu 		       op->modex.result.length);
877e9a356e2SSunila Sahu 		break;
878aa2cbd32SSunila Sahu 	case RTE_CRYPTO_ASYM_XFORM_ECDSA:
879aa2cbd32SSunila Sahu 		otx_cpt_asym_dequeue_ecdsa_op(&op->ecdsa, req, &sess->ec_ctx);
880aa2cbd32SSunila Sahu 		break;
88199faef83SSunila Sahu 	case RTE_CRYPTO_ASYM_XFORM_ECPM:
88299faef83SSunila Sahu 		otx_cpt_asym_dequeue_ecpm_op(&op->ecpm, req, &sess->ec_ctx);
88399faef83SSunila Sahu 		break;
884e9a356e2SSunila Sahu 	default:
885e9a356e2SSunila Sahu 		CPT_LOG_DP_DEBUG("Invalid crypto xform type");
886e9a356e2SSunila Sahu 		cop->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
887e9a356e2SSunila Sahu 		break;
888e9a356e2SSunila Sahu 	}
889e9a356e2SSunila Sahu }
890e9a356e2SSunila Sahu 
891e3866e73SThomas Monjalon static __rte_always_inline void __rte_hot
892e9a356e2SSunila Sahu otx_cpt_dequeue_post_process(struct rte_crypto_op *cop, uintptr_t *rsp,
893e9a356e2SSunila Sahu 			     const uint8_t op_type)
894f194f198SAnoob Joseph {
895f194f198SAnoob Joseph 	/* H/w has returned success */
896f194f198SAnoob Joseph 	cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
897f194f198SAnoob Joseph 
898f194f198SAnoob Joseph 	/* Perform further post processing */
899f194f198SAnoob Joseph 
900e9a356e2SSunila Sahu 	if ((op_type == OP_TYPE_SYM) &&
901e9a356e2SSunila Sahu 	    (cop->type == RTE_CRYPTO_OP_TYPE_SYMMETRIC)) {
902f194f198SAnoob Joseph 		/* Check if auth verify need to be completed */
903f194f198SAnoob Joseph 		if (unlikely(rsp[2]))
904f194f198SAnoob Joseph 			compl_auth_verify(cop, (uint8_t *)rsp[2], rsp[3]);
905f194f198SAnoob Joseph 		return;
906f194f198SAnoob Joseph 	}
907e9a356e2SSunila Sahu 
908e9a356e2SSunila Sahu 	if ((op_type == OP_TYPE_ASYM) &&
909e9a356e2SSunila Sahu 	    (cop->type == RTE_CRYPTO_OP_TYPE_ASYMMETRIC)) {
910e9a356e2SSunila Sahu 		rsp = RTE_PTR_ADD(rsp, 4 * sizeof(uintptr_t));
911e9a356e2SSunila Sahu 		otx_cpt_asym_post_process(cop, (struct cpt_request_info *)rsp);
912f194f198SAnoob Joseph 	}
913f194f198SAnoob Joseph 
914e9a356e2SSunila Sahu 	return;
915e9a356e2SSunila Sahu }
916e9a356e2SSunila Sahu 
91744a2cebbSShijith Thotton static inline void
91844a2cebbSShijith Thotton free_sym_session_data(const struct cpt_instance *instance,
91944a2cebbSShijith Thotton 		      struct rte_crypto_op *cop)
92044a2cebbSShijith Thotton {
92144a2cebbSShijith Thotton 	void *sess_private_data_t = get_sym_session_private_data(
92244a2cebbSShijith Thotton 		cop->sym->session, otx_cryptodev_driver_id);
92344a2cebbSShijith Thotton 	memset(sess_private_data_t, 0, cpt_get_session_size());
92444a2cebbSShijith Thotton 	memset(cop->sym->session, 0,
92544a2cebbSShijith Thotton 	       rte_cryptodev_sym_get_existing_header_session_size(
92644a2cebbSShijith Thotton 		       cop->sym->session));
92744a2cebbSShijith Thotton 	rte_mempool_put(instance->sess_mp_priv, sess_private_data_t);
92844a2cebbSShijith Thotton 	rte_mempool_put(instance->sess_mp, cop->sym->session);
92944a2cebbSShijith Thotton 	cop->sym->session = NULL;
93044a2cebbSShijith Thotton }
93144a2cebbSShijith Thotton 
93244a2cebbSShijith Thotton static __rte_always_inline struct rte_crypto_op *
93344a2cebbSShijith Thotton otx_cpt_process_response(const struct cpt_instance *instance, uintptr_t *rsp,
93444a2cebbSShijith Thotton 			 uint8_t cc, const uint8_t op_type)
93544a2cebbSShijith Thotton {
93644a2cebbSShijith Thotton 	struct rte_crypto_op *cop;
93744a2cebbSShijith Thotton 	void *metabuf;
93844a2cebbSShijith Thotton 
93944a2cebbSShijith Thotton 	metabuf = (void *)rsp[0];
94044a2cebbSShijith Thotton 	cop = (void *)rsp[1];
94144a2cebbSShijith Thotton 
94244a2cebbSShijith Thotton 	/* Check completion code */
94344a2cebbSShijith Thotton 	if (likely(cc == 0)) {
94444a2cebbSShijith Thotton 		/* H/w success pkt. Post process */
94544a2cebbSShijith Thotton 		otx_cpt_dequeue_post_process(cop, rsp, op_type);
94644a2cebbSShijith Thotton 	} else if (cc == ERR_GC_ICV_MISCOMPARE) {
94744a2cebbSShijith Thotton 		/* auth data mismatch */
94844a2cebbSShijith Thotton 		cop->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
94944a2cebbSShijith Thotton 	} else {
95044a2cebbSShijith Thotton 		/* Error */
95144a2cebbSShijith Thotton 		cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
95244a2cebbSShijith Thotton 	}
95344a2cebbSShijith Thotton 
95444a2cebbSShijith Thotton 	if (unlikely(cop->sess_type == RTE_CRYPTO_OP_SESSIONLESS))
95544a2cebbSShijith Thotton 		free_sym_session_data(instance, cop);
95644a2cebbSShijith Thotton 	free_op_meta(metabuf, instance->meta_info.pool);
95744a2cebbSShijith Thotton 
95844a2cebbSShijith Thotton 	return cop;
95944a2cebbSShijith Thotton }
96044a2cebbSShijith Thotton 
961e3866e73SThomas Monjalon static __rte_always_inline uint16_t __rte_hot
962e9a356e2SSunila Sahu otx_cpt_pkt_dequeue(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops,
963e9a356e2SSunila Sahu 		    const uint8_t op_type)
96489f1a8d6STejasree Kondoj {
96589f1a8d6STejasree Kondoj 	struct cpt_instance *instance = (struct cpt_instance *)qptr;
966f194f198SAnoob Joseph 	struct cpt_request_info *user_req;
96789f1a8d6STejasree Kondoj 	struct cpt_vf *cptvf = (struct cpt_vf *)instance;
968f194f198SAnoob Joseph 	uint8_t cc[nb_ops];
969f194f198SAnoob Joseph 	int i, count, pcount;
970f194f198SAnoob Joseph 	uint8_t ret;
971f194f198SAnoob Joseph 	int nb_completed;
97289f1a8d6STejasree Kondoj 	struct pending_queue *pqueue = &cptvf->pqueue;
973f194f198SAnoob Joseph 
974f194f198SAnoob Joseph 	pcount = pqueue->pending_count;
975f194f198SAnoob Joseph 	count = (nb_ops > pcount) ? pcount : nb_ops;
976f194f198SAnoob Joseph 
977f194f198SAnoob Joseph 	for (i = 0; i < count; i++) {
97895dbafd4SArchana Muniganti 		user_req = (struct cpt_request_info *)
97995dbafd4SArchana Muniganti 				pqueue->req_queue[pqueue->deq_head];
980f194f198SAnoob Joseph 
98195dbafd4SArchana Muniganti 		if (likely((i+1) < count)) {
98295dbafd4SArchana Muniganti 			rte_prefetch_non_temporal(
98395dbafd4SArchana Muniganti 				(void *)pqueue->req_queue[i+1]);
98495dbafd4SArchana Muniganti 		}
985f194f198SAnoob Joseph 
986f194f198SAnoob Joseph 		ret = check_nb_command_id(user_req, instance);
987f194f198SAnoob Joseph 
988f194f198SAnoob Joseph 		if (unlikely(ret == ERR_REQ_PENDING)) {
989f194f198SAnoob Joseph 			/* Stop checking for completions */
990f194f198SAnoob Joseph 			break;
991f194f198SAnoob Joseph 		}
992f194f198SAnoob Joseph 
993f194f198SAnoob Joseph 		/* Return completion code and op handle */
994f194f198SAnoob Joseph 		cc[i] = ret;
995f194f198SAnoob Joseph 		ops[i] = user_req->op;
996f194f198SAnoob Joseph 
997f194f198SAnoob Joseph 		CPT_LOG_DP_DEBUG("Request %p Op %p completed with code %d",
998f194f198SAnoob Joseph 				 user_req, user_req->op, ret);
999f194f198SAnoob Joseph 
1000f194f198SAnoob Joseph 		MOD_INC(pqueue->deq_head, DEFAULT_CMD_QLEN);
1001f194f198SAnoob Joseph 		pqueue->pending_count -= 1;
1002f194f198SAnoob Joseph 	}
1003f194f198SAnoob Joseph 
1004f194f198SAnoob Joseph 	nb_completed = i;
1005f194f198SAnoob Joseph 
1006f194f198SAnoob Joseph 	for (i = 0; i < nb_completed; i++) {
100789f1a8d6STejasree Kondoj 		if (likely((i + 1) < nb_completed))
100889f1a8d6STejasree Kondoj 			rte_prefetch0(ops[i+1]);
1009f194f198SAnoob Joseph 
101044a2cebbSShijith Thotton 		ops[i] = otx_cpt_process_response(instance, (void *)ops[i],
101144a2cebbSShijith Thotton 						  cc[i], op_type);
1012f194f198SAnoob Joseph 	}
1013f194f198SAnoob Joseph 
101489f1a8d6STejasree Kondoj 	return nb_completed;
101589f1a8d6STejasree Kondoj }
101689f1a8d6STejasree Kondoj 
1017e9a356e2SSunila Sahu static uint16_t
1018e9a356e2SSunila Sahu otx_cpt_dequeue_asym(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops)
1019e9a356e2SSunila Sahu {
1020e9a356e2SSunila Sahu 	return otx_cpt_pkt_dequeue(qptr, ops, nb_ops, OP_TYPE_ASYM);
1021e9a356e2SSunila Sahu }
1022e9a356e2SSunila Sahu 
1023e9a356e2SSunila Sahu static uint16_t
1024e9a356e2SSunila Sahu otx_cpt_dequeue_sym(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops)
1025e9a356e2SSunila Sahu {
1026e9a356e2SSunila Sahu 	return otx_cpt_pkt_dequeue(qptr, ops, nb_ops, OP_TYPE_SYM);
1027e9a356e2SSunila Sahu }
1028e9a356e2SSunila Sahu 
102944a2cebbSShijith Thotton uintptr_t __rte_hot
103044a2cebbSShijith Thotton otx_crypto_adapter_dequeue(uintptr_t get_work1)
103144a2cebbSShijith Thotton {
103244a2cebbSShijith Thotton 	const struct cpt_instance *instance;
103344a2cebbSShijith Thotton 	struct cpt_request_info *req;
103444a2cebbSShijith Thotton 	struct rte_crypto_op *cop;
103544a2cebbSShijith Thotton 	uint8_t cc, op_type;
103644a2cebbSShijith Thotton 	uintptr_t *rsp;
103744a2cebbSShijith Thotton 
103844a2cebbSShijith Thotton 	req = (struct cpt_request_info *)get_work1;
103944a2cebbSShijith Thotton 	instance = req->qp;
104044a2cebbSShijith Thotton 	rsp = req->op;
104144a2cebbSShijith Thotton 	cop = (void *)rsp[1];
104244a2cebbSShijith Thotton 	op_type = cop->type == RTE_CRYPTO_OP_TYPE_SYMMETRIC ? OP_TYPE_SYM :
104344a2cebbSShijith Thotton 							      OP_TYPE_ASYM;
104444a2cebbSShijith Thotton 
104544a2cebbSShijith Thotton 	do {
104644a2cebbSShijith Thotton 		cc = check_nb_command_id(
104744a2cebbSShijith Thotton 			req, (struct cpt_instance *)(uintptr_t)instance);
104844a2cebbSShijith Thotton 	} while (cc == ERR_REQ_PENDING);
104944a2cebbSShijith Thotton 
105044a2cebbSShijith Thotton 	cop = otx_cpt_process_response(instance, (void *)req->op, cc, op_type);
105144a2cebbSShijith Thotton 
105244a2cebbSShijith Thotton 	return (uintptr_t)(cop);
105344a2cebbSShijith Thotton }
105444a2cebbSShijith Thotton 
10550906b99fSMurthy NSSR static struct rte_cryptodev_ops cptvf_ops = {
10560906b99fSMurthy NSSR 	/* Device related operations */
10570906b99fSMurthy NSSR 	.dev_configure = otx_cpt_dev_config,
10580906b99fSMurthy NSSR 	.dev_start = otx_cpt_dev_start,
10590906b99fSMurthy NSSR 	.dev_stop = otx_cpt_dev_stop,
10600906b99fSMurthy NSSR 	.dev_close = otx_cpt_dev_close,
10610906b99fSMurthy NSSR 	.dev_infos_get = otx_cpt_dev_info_get,
10620906b99fSMurthy NSSR 
1063966b43fdSAnkur Dwivedi 	.stats_get = NULL,
1064966b43fdSAnkur Dwivedi 	.stats_reset = NULL,
10650961348fSMurthy NSSR 	.queue_pair_setup = otx_cpt_que_pair_setup,
10660961348fSMurthy NSSR 	.queue_pair_release = otx_cpt_que_pair_release,
10670906b99fSMurthy NSSR 
10680906b99fSMurthy NSSR 	/* Crypto related operations */
106943d01767SNithin Dabilpuram 	.sym_session_get_size = otx_cpt_get_session_size,
107043d01767SNithin Dabilpuram 	.sym_session_configure = otx_cpt_session_cfg,
107133bcaae5SKanaka Durga Kotamarthy 	.sym_session_clear = otx_cpt_session_clear,
107233bcaae5SKanaka Durga Kotamarthy 
107333bcaae5SKanaka Durga Kotamarthy 	.asym_session_get_size = otx_cpt_asym_session_size_get,
107433bcaae5SKanaka Durga Kotamarthy 	.asym_session_configure = otx_cpt_asym_session_cfg,
107533bcaae5SKanaka Durga Kotamarthy 	.asym_session_clear = otx_cpt_asym_session_clear,
10760906b99fSMurthy NSSR };
10770906b99fSMurthy NSSR 
1078bfe2ae49SAnoob Joseph int
1079bfe2ae49SAnoob Joseph otx_cpt_dev_create(struct rte_cryptodev *c_dev)
1080bfe2ae49SAnoob Joseph {
10810dc1cffaSAnkur Dwivedi 	struct rte_pci_device *pdev = RTE_DEV_TO_PCI(c_dev->device);
10820dc1cffaSAnkur Dwivedi 	struct cpt_vf *cptvf = NULL;
10830dc1cffaSAnkur Dwivedi 	void *reg_base;
10840dc1cffaSAnkur Dwivedi 	char dev_name[32];
10850dc1cffaSAnkur Dwivedi 	int ret;
10860dc1cffaSAnkur Dwivedi 
10870dc1cffaSAnkur Dwivedi 	if (pdev->mem_resource[0].phys_addr == 0ULL)
10880dc1cffaSAnkur Dwivedi 		return -EIO;
10890dc1cffaSAnkur Dwivedi 
10900dc1cffaSAnkur Dwivedi 	/* for secondary processes, we don't initialise any further as primary
10910dc1cffaSAnkur Dwivedi 	 * has already done this work.
10920dc1cffaSAnkur Dwivedi 	 */
10930dc1cffaSAnkur Dwivedi 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1094bfe2ae49SAnoob Joseph 		return 0;
10950dc1cffaSAnkur Dwivedi 
10960dc1cffaSAnkur Dwivedi 	cptvf = rte_zmalloc_socket("otx_cryptodev_private_mem",
10970dc1cffaSAnkur Dwivedi 			sizeof(struct cpt_vf), RTE_CACHE_LINE_SIZE,
10980dc1cffaSAnkur Dwivedi 			rte_socket_id());
10990dc1cffaSAnkur Dwivedi 
11000dc1cffaSAnkur Dwivedi 	if (cptvf == NULL) {
11010dc1cffaSAnkur Dwivedi 		CPT_LOG_ERR("Cannot allocate memory for device private data");
11020dc1cffaSAnkur Dwivedi 		return -ENOMEM;
11030dc1cffaSAnkur Dwivedi 	}
11040dc1cffaSAnkur Dwivedi 
11050dc1cffaSAnkur Dwivedi 	snprintf(dev_name, 32, "%02x:%02x.%x",
11060dc1cffaSAnkur Dwivedi 			pdev->addr.bus, pdev->addr.devid, pdev->addr.function);
11070dc1cffaSAnkur Dwivedi 
11080dc1cffaSAnkur Dwivedi 	reg_base = pdev->mem_resource[0].addr;
11090dc1cffaSAnkur Dwivedi 	if (!reg_base) {
11100dc1cffaSAnkur Dwivedi 		CPT_LOG_ERR("Failed to map BAR0 of %s", dev_name);
11110dc1cffaSAnkur Dwivedi 		ret = -ENODEV;
11120dc1cffaSAnkur Dwivedi 		goto fail;
11130dc1cffaSAnkur Dwivedi 	}
11140dc1cffaSAnkur Dwivedi 
11150dc1cffaSAnkur Dwivedi 	ret = otx_cpt_hw_init(cptvf, pdev, reg_base, dev_name);
11160dc1cffaSAnkur Dwivedi 	if (ret) {
11170dc1cffaSAnkur Dwivedi 		CPT_LOG_ERR("Failed to init cptvf %s", dev_name);
11180dc1cffaSAnkur Dwivedi 		ret = -EIO;
11190dc1cffaSAnkur Dwivedi 		goto fail;
11200dc1cffaSAnkur Dwivedi 	}
11210dc1cffaSAnkur Dwivedi 
112213d711f3SKanaka Durga Kotamarthy 	switch (cptvf->vftype) {
112313d711f3SKanaka Durga Kotamarthy 	case OTX_CPT_VF_TYPE_AE:
112413d711f3SKanaka Durga Kotamarthy 		/* Set asymmetric cpt feature flags */
112513d711f3SKanaka Durga Kotamarthy 		c_dev->feature_flags = RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO |
112633bcaae5SKanaka Durga Kotamarthy 				RTE_CRYPTODEV_FF_HW_ACCELERATED |
112733bcaae5SKanaka Durga Kotamarthy 				RTE_CRYPTODEV_FF_RSA_PRIV_OP_KEY_QT;
112813d711f3SKanaka Durga Kotamarthy 		break;
112913d711f3SKanaka Durga Kotamarthy 	case OTX_CPT_VF_TYPE_SE:
113013d711f3SKanaka Durga Kotamarthy 		/* Set symmetric cpt feature flags */
113113d711f3SKanaka Durga Kotamarthy 		c_dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
113213d711f3SKanaka Durga Kotamarthy 				RTE_CRYPTODEV_FF_HW_ACCELERATED |
113313d711f3SKanaka Durga Kotamarthy 				RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
113413d711f3SKanaka Durga Kotamarthy 				RTE_CRYPTODEV_FF_IN_PLACE_SGL |
113516c01147SDidier Pallard 				RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT |
113613d711f3SKanaka Durga Kotamarthy 				RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT |
1137b3aaf24dSPablo de Lara 				RTE_CRYPTODEV_FF_OOP_SGL_IN_SGL_OUT |
1138de5eb0a6STejasree Kondoj 				RTE_CRYPTODEV_FF_SYM_SESSIONLESS |
1139de5eb0a6STejasree Kondoj 				RTE_CRYPTODEV_FF_DIGEST_ENCRYPTED;
114013d711f3SKanaka Durga Kotamarthy 		break;
114113d711f3SKanaka Durga Kotamarthy 	default:
114213d711f3SKanaka Durga Kotamarthy 		/* Feature not supported. Abort */
114313d711f3SKanaka Durga Kotamarthy 		CPT_LOG_ERR("VF type not supported by %s", dev_name);
114413d711f3SKanaka Durga Kotamarthy 		ret = -EIO;
114513d711f3SKanaka Durga Kotamarthy 		goto deinit_dev;
114613d711f3SKanaka Durga Kotamarthy 	}
114713d711f3SKanaka Durga Kotamarthy 
11480dc1cffaSAnkur Dwivedi 	/* Start off timer for mailbox interrupts */
11490dc1cffaSAnkur Dwivedi 	otx_cpt_periodic_alarm_start(cptvf);
11500dc1cffaSAnkur Dwivedi 
11510906b99fSMurthy NSSR 	c_dev->dev_ops = &cptvf_ops;
11520dc1cffaSAnkur Dwivedi 
1153e9a356e2SSunila Sahu 	if (c_dev->feature_flags & RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO) {
1154e9a356e2SSunila Sahu 		c_dev->enqueue_burst = otx_cpt_enqueue_sym;
1155e9a356e2SSunila Sahu 		c_dev->dequeue_burst = otx_cpt_dequeue_sym;
1156e9a356e2SSunila Sahu 	} else {
1157e9a356e2SSunila Sahu 		c_dev->enqueue_burst = otx_cpt_enqueue_asym;
1158e9a356e2SSunila Sahu 		c_dev->dequeue_burst = otx_cpt_dequeue_asym;
1159e9a356e2SSunila Sahu 	}
11600dc1cffaSAnkur Dwivedi 
11610dc1cffaSAnkur Dwivedi 	/* Save dev private data */
11620dc1cffaSAnkur Dwivedi 	c_dev->data->dev_private = cptvf;
11630dc1cffaSAnkur Dwivedi 
11640dc1cffaSAnkur Dwivedi 	return 0;
11650dc1cffaSAnkur Dwivedi 
116613d711f3SKanaka Durga Kotamarthy deinit_dev:
116713d711f3SKanaka Durga Kotamarthy 	otx_cpt_deinit_device(cptvf);
116813d711f3SKanaka Durga Kotamarthy 
11690dc1cffaSAnkur Dwivedi fail:
11700dc1cffaSAnkur Dwivedi 	if (cptvf) {
11710dc1cffaSAnkur Dwivedi 		/* Free private data allocated */
11720dc1cffaSAnkur Dwivedi 		rte_free(cptvf);
11730dc1cffaSAnkur Dwivedi 	}
11740dc1cffaSAnkur Dwivedi 
11750dc1cffaSAnkur Dwivedi 	return ret;
1176bfe2ae49SAnoob Joseph }
1177