xref: /dpdk/drivers/crypto/octeontx/otx_cryptodev_ops.c (revision 8a97564b1c1e035daaa0cdda553edd46178889e2)
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>
61f37cb2bSDavid Marchand #include <bus_pci_driver.h>
7bfe2ae49SAnoob Joseph #include <rte_cryptodev.h>
8af668035SAkhil Goyal #include <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;
1740961348fSMurthy NSSR 	dev->data->queue_pairs[que_pair_id] = instance;
1750961348fSMurthy NSSR 
1760961348fSMurthy NSSR 	return 0;
1770961348fSMurthy NSSR }
1780961348fSMurthy NSSR 
1790961348fSMurthy NSSR static int
1800961348fSMurthy NSSR otx_cpt_que_pair_release(struct rte_cryptodev *dev, uint16_t que_pair_id)
1810961348fSMurthy NSSR {
1820961348fSMurthy NSSR 	struct cpt_instance *instance = dev->data->queue_pairs[que_pair_id];
1830961348fSMurthy NSSR 	int ret;
1840961348fSMurthy NSSR 
1850961348fSMurthy NSSR 	CPT_PMD_INIT_FUNC_TRACE();
1860961348fSMurthy NSSR 
1870961348fSMurthy NSSR 	ret = otx_cpt_put_resource(instance);
1880961348fSMurthy NSSR 	if (ret != 0) {
1890961348fSMurthy NSSR 		CPT_LOG_ERR("Error putting instance handle of device %s : "
1900961348fSMurthy NSSR 			    "ret = %d", dev->data->name, ret);
1910961348fSMurthy NSSR 		return ret;
1920961348fSMurthy NSSR 	}
1930961348fSMurthy NSSR 
1940961348fSMurthy NSSR 	dev->data->queue_pairs[que_pair_id] = NULL;
1950961348fSMurthy NSSR 
1960961348fSMurthy NSSR 	return 0;
1970961348fSMurthy NSSR }
1980961348fSMurthy NSSR 
19943d01767SNithin Dabilpuram static unsigned int
20043d01767SNithin Dabilpuram otx_cpt_get_session_size(struct rte_cryptodev *dev __rte_unused)
20143d01767SNithin Dabilpuram {
20243d01767SNithin Dabilpuram 	return cpt_get_session_size();
20343d01767SNithin Dabilpuram }
20443d01767SNithin Dabilpuram 
205caeba506SAnoob Joseph static int
206caeba506SAnoob Joseph sym_xform_verify(struct rte_crypto_sym_xform *xform)
20743d01767SNithin Dabilpuram {
208caeba506SAnoob Joseph 	if (xform->next) {
209caeba506SAnoob Joseph 		if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
210caeba506SAnoob Joseph 		    xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
211de5eb0a6STejasree Kondoj 		    xform->next->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT &&
212de5eb0a6STejasree Kondoj 		    (xform->auth.algo != RTE_CRYPTO_AUTH_SHA1_HMAC ||
213de5eb0a6STejasree Kondoj 		     xform->next->cipher.algo != RTE_CRYPTO_CIPHER_AES_CBC))
214caeba506SAnoob Joseph 			return -ENOTSUP;
21543d01767SNithin Dabilpuram 
216caeba506SAnoob Joseph 		if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
217caeba506SAnoob Joseph 		    xform->cipher.op == RTE_CRYPTO_CIPHER_OP_DECRYPT &&
218de5eb0a6STejasree Kondoj 		    xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
219de5eb0a6STejasree Kondoj 		    (xform->cipher.algo != RTE_CRYPTO_CIPHER_AES_CBC ||
220de5eb0a6STejasree Kondoj 		     xform->next->auth.algo != RTE_CRYPTO_AUTH_SHA1_HMAC))
221caeba506SAnoob Joseph 			return -ENOTSUP;
222caeba506SAnoob Joseph 
223caeba506SAnoob Joseph 		if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
224caeba506SAnoob Joseph 		    xform->cipher.algo == RTE_CRYPTO_CIPHER_3DES_CBC &&
225caeba506SAnoob Joseph 		    xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
226caeba506SAnoob Joseph 		    xform->next->auth.algo == RTE_CRYPTO_AUTH_SHA1)
227caeba506SAnoob Joseph 			return -ENOTSUP;
228caeba506SAnoob Joseph 
229caeba506SAnoob Joseph 		if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
230caeba506SAnoob Joseph 		    xform->auth.algo == RTE_CRYPTO_AUTH_SHA1 &&
231caeba506SAnoob Joseph 		    xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
232caeba506SAnoob Joseph 		    xform->next->cipher.algo == RTE_CRYPTO_CIPHER_3DES_CBC)
233caeba506SAnoob Joseph 			return -ENOTSUP;
234caeba506SAnoob Joseph 
235caeba506SAnoob Joseph 	} else {
236caeba506SAnoob Joseph 		if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
237caeba506SAnoob Joseph 		    xform->auth.algo == RTE_CRYPTO_AUTH_NULL &&
238caeba506SAnoob Joseph 		    xform->auth.op == RTE_CRYPTO_AUTH_OP_VERIFY)
239caeba506SAnoob Joseph 			return -ENOTSUP;
240caeba506SAnoob Joseph 	}
241caeba506SAnoob Joseph 	return 0;
242caeba506SAnoob Joseph }
243caeba506SAnoob Joseph 
244caeba506SAnoob Joseph static int
245bdce2564SAkhil Goyal sym_session_configure(struct rte_crypto_sym_xform *xform,
246bdce2564SAkhil Goyal 		      struct rte_cryptodev_sym_session *sess)
247caeba506SAnoob Joseph {
2487293bae1SArchana Muniganti 	struct rte_crypto_sym_xform *temp_xform = xform;
249caeba506SAnoob Joseph 	struct cpt_sess_misc *misc;
2506045c06aSArchana Muniganti 	vq_cmd_word3_t vq_cmd_w3;
2512a440d6aSAkhil Goyal 	void *priv = CRYPTODEV_GET_SYM_SESS_PRIV(sess);
252caeba506SAnoob Joseph 	int ret;
253caeba506SAnoob Joseph 
254caeba506SAnoob Joseph 	ret = sym_xform_verify(xform);
255caeba506SAnoob Joseph 	if (unlikely(ret))
256caeba506SAnoob Joseph 		return ret;
257caeba506SAnoob Joseph 
2580b345f41SAnkur Dwivedi 	memset(priv, 0, sizeof(struct cpt_sess_misc) +
2596045c06aSArchana Muniganti 			offsetof(struct cpt_ctx, mc_ctx));
2600b345f41SAnkur Dwivedi 
261caeba506SAnoob Joseph 	misc = priv;
262caeba506SAnoob Joseph 
263caeba506SAnoob Joseph 	for ( ; xform != NULL; xform = xform->next) {
264caeba506SAnoob Joseph 		switch (xform->type) {
265caeba506SAnoob Joseph 		case RTE_CRYPTO_SYM_XFORM_AEAD:
266caeba506SAnoob Joseph 			ret = fill_sess_aead(xform, misc);
267caeba506SAnoob Joseph 			break;
268caeba506SAnoob Joseph 		case RTE_CRYPTO_SYM_XFORM_CIPHER:
269caeba506SAnoob Joseph 			ret = fill_sess_cipher(xform, misc);
270caeba506SAnoob Joseph 			break;
271caeba506SAnoob Joseph 		case RTE_CRYPTO_SYM_XFORM_AUTH:
272caeba506SAnoob Joseph 			if (xform->auth.algo == RTE_CRYPTO_AUTH_AES_GMAC)
273caeba506SAnoob Joseph 				ret = fill_sess_gmac(xform, misc);
274caeba506SAnoob Joseph 			else
275caeba506SAnoob Joseph 				ret = fill_sess_auth(xform, misc);
276caeba506SAnoob Joseph 			break;
277caeba506SAnoob Joseph 		default:
278caeba506SAnoob Joseph 			ret = -1;
279caeba506SAnoob Joseph 		}
280caeba506SAnoob Joseph 
281caeba506SAnoob Joseph 		if (ret)
282caeba506SAnoob Joseph 			goto priv_put;
283caeba506SAnoob Joseph 	}
284caeba506SAnoob Joseph 
2857293bae1SArchana Muniganti 	if ((GET_SESS_FC_TYPE(misc) == HASH_HMAC) &&
2867293bae1SArchana Muniganti 			cpt_mac_len_verify(&temp_xform->auth)) {
2877293bae1SArchana Muniganti 		CPT_LOG_ERR("MAC length is not supported");
288db06451bSAnoob Joseph 		struct cpt_ctx *ctx = SESS_PRIV(misc);
289db06451bSAnoob Joseph 		if (ctx->auth_key != NULL) {
290db06451bSAnoob Joseph 			rte_free(ctx->auth_key);
291db06451bSAnoob Joseph 			ctx->auth_key = NULL;
292db06451bSAnoob Joseph 		}
2937293bae1SArchana Muniganti 		ret = -ENOTSUP;
2947293bae1SArchana Muniganti 		goto priv_put;
2957293bae1SArchana Muniganti 	}
2967293bae1SArchana Muniganti 
2972a440d6aSAkhil Goyal 	misc->ctx_dma_addr = CRYPTODEV_GET_SYM_SESS_PRIV_IOVA(sess) +
29843d01767SNithin Dabilpuram 			     sizeof(struct cpt_sess_misc);
299caeba506SAnoob Joseph 
3006045c06aSArchana Muniganti 	vq_cmd_w3.u64 = 0;
3016045c06aSArchana Muniganti 	vq_cmd_w3.s.grp = 0;
3026045c06aSArchana Muniganti 	vq_cmd_w3.s.cptr = misc->ctx_dma_addr + offsetof(struct cpt_ctx,
3036045c06aSArchana Muniganti 							 mc_ctx);
3046045c06aSArchana Muniganti 
3056045c06aSArchana Muniganti 	misc->cpt_inst_w7 = vq_cmd_w3.u64;
3066045c06aSArchana Muniganti 
307caeba506SAnoob Joseph 	return 0;
308caeba506SAnoob Joseph 
309caeba506SAnoob Joseph priv_put:
310caeba506SAnoob Joseph 	return -ENOTSUP;
311caeba506SAnoob Joseph }
312caeba506SAnoob Joseph 
313caeba506SAnoob Joseph static void
314bdce2564SAkhil Goyal sym_session_clear(struct rte_cryptodev_sym_session *sess)
315caeba506SAnoob Joseph {
3162a440d6aSAkhil Goyal 	void *priv = CRYPTODEV_GET_SYM_SESS_PRIV(sess);
317db06451bSAnoob Joseph 	struct cpt_sess_misc *misc;
318db06451bSAnoob Joseph 	struct cpt_ctx *ctx;
319caeba506SAnoob Joseph 
320caeba506SAnoob Joseph 	if (priv == NULL)
321caeba506SAnoob Joseph 		return;
322caeba506SAnoob Joseph 
323db06451bSAnoob Joseph 	misc = priv;
324db06451bSAnoob Joseph 	ctx = SESS_PRIV(misc);
325db06451bSAnoob Joseph 
326db06451bSAnoob Joseph 	rte_free(ctx->auth_key);
32743d01767SNithin Dabilpuram }
32843d01767SNithin Dabilpuram 
32943d01767SNithin Dabilpuram static int
330bdce2564SAkhil Goyal otx_cpt_session_cfg(struct rte_cryptodev *dev __rte_unused,
33143d01767SNithin Dabilpuram 		    struct rte_crypto_sym_xform *xform,
33243d01767SNithin Dabilpuram 		    struct rte_cryptodev_sym_session *sess)
33343d01767SNithin Dabilpuram {
33443d01767SNithin Dabilpuram 	CPT_PMD_INIT_FUNC_TRACE();
335caeba506SAnoob Joseph 
336bdce2564SAkhil Goyal 	return sym_session_configure(xform, sess);
337bdce2564SAkhil Goyal }
338bdce2564SAkhil Goyal 
339bdce2564SAkhil Goyal 
340bdce2564SAkhil Goyal static void
341bdce2564SAkhil Goyal otx_cpt_session_clear(struct rte_cryptodev *dev __rte_unused,
342bdce2564SAkhil Goyal 		  struct rte_cryptodev_sym_session *sess)
343bdce2564SAkhil Goyal {
344bdce2564SAkhil Goyal 	CPT_PMD_INIT_FUNC_TRACE();
345bdce2564SAkhil Goyal 
346bdce2564SAkhil Goyal 	return sym_session_clear(sess);
34743d01767SNithin Dabilpuram }
34843d01767SNithin Dabilpuram 
34933bcaae5SKanaka Durga Kotamarthy static unsigned int
35033bcaae5SKanaka Durga Kotamarthy otx_cpt_asym_session_size_get(struct rte_cryptodev *dev __rte_unused)
35133bcaae5SKanaka Durga Kotamarthy {
35233bcaae5SKanaka Durga Kotamarthy 	return sizeof(struct cpt_asym_sess_misc);
35333bcaae5SKanaka Durga Kotamarthy }
35433bcaae5SKanaka Durga Kotamarthy 
35533bcaae5SKanaka Durga Kotamarthy static int
3561f1e4b7cSCiara Power otx_cpt_asym_session_cfg(struct rte_cryptodev *dev __rte_unused,
35733bcaae5SKanaka Durga Kotamarthy 			 struct rte_crypto_asym_xform *xform __rte_unused,
3581f1e4b7cSCiara Power 			 struct rte_cryptodev_asym_session *sess)
35933bcaae5SKanaka Durga Kotamarthy {
3601f1e4b7cSCiara Power 	struct cpt_asym_sess_misc *priv = (struct cpt_asym_sess_misc *)
3611f1e4b7cSCiara Power 			sess->sess_private_data;
36233bcaae5SKanaka Durga Kotamarthy 	int ret;
36333bcaae5SKanaka Durga Kotamarthy 
36433bcaae5SKanaka Durga Kotamarthy 	CPT_PMD_INIT_FUNC_TRACE();
36533bcaae5SKanaka Durga Kotamarthy 
36633bcaae5SKanaka Durga Kotamarthy 	ret = cpt_fill_asym_session_parameters(priv, xform);
36733bcaae5SKanaka Durga Kotamarthy 	if (ret) {
36833bcaae5SKanaka Durga Kotamarthy 		CPT_LOG_ERR("Could not configure session parameters");
36933bcaae5SKanaka Durga Kotamarthy 		return ret;
37033bcaae5SKanaka Durga Kotamarthy 	}
37133bcaae5SKanaka Durga Kotamarthy 
3726045c06aSArchana Muniganti 	priv->cpt_inst_w7 = 0;
3736045c06aSArchana Muniganti 
37433bcaae5SKanaka Durga Kotamarthy 	return 0;
37533bcaae5SKanaka Durga Kotamarthy }
37633bcaae5SKanaka Durga Kotamarthy 
37733bcaae5SKanaka Durga Kotamarthy static void
37833bcaae5SKanaka Durga Kotamarthy otx_cpt_asym_session_clear(struct rte_cryptodev *dev,
37933bcaae5SKanaka Durga Kotamarthy 			   struct rte_cryptodev_asym_session *sess)
38033bcaae5SKanaka Durga Kotamarthy {
38133bcaae5SKanaka Durga Kotamarthy 	struct cpt_asym_sess_misc *priv;
38233bcaae5SKanaka Durga Kotamarthy 
38333bcaae5SKanaka Durga Kotamarthy 	CPT_PMD_INIT_FUNC_TRACE();
38433bcaae5SKanaka Durga Kotamarthy 
3851f1e4b7cSCiara Power 	priv = (struct cpt_asym_sess_misc *) sess->sess_private_data;
38633bcaae5SKanaka Durga Kotamarthy 
38733bcaae5SKanaka Durga Kotamarthy 	if (priv == NULL)
38833bcaae5SKanaka Durga Kotamarthy 		return;
38933bcaae5SKanaka Durga Kotamarthy 
39033bcaae5SKanaka Durga Kotamarthy 	/* Free resources allocated during session configure */
39133bcaae5SKanaka Durga Kotamarthy 	cpt_free_asym_session_parameters(priv);
39233bcaae5SKanaka Durga Kotamarthy 	memset(priv, 0, otx_cpt_asym_session_size_get(dev));
39333bcaae5SKanaka Durga Kotamarthy }
39433bcaae5SKanaka Durga Kotamarthy 
39544a2cebbSShijith Thotton static __rte_always_inline void * __rte_hot
396f194f198SAnoob Joseph otx_cpt_request_enqueue(struct cpt_instance *instance,
3976045c06aSArchana Muniganti 			void *req, uint64_t cpt_inst_w7)
398f194f198SAnoob Joseph {
399f194f198SAnoob Joseph 	struct cpt_request_info *user_req = (struct cpt_request_info *)req;
400f194f198SAnoob Joseph 
4016045c06aSArchana Muniganti 	fill_cpt_inst(instance, req, cpt_inst_w7);
402f194f198SAnoob Joseph 
403f194f198SAnoob Joseph 	CPT_LOG_DP_DEBUG("req: %p op: %p ", req, user_req->op);
404f194f198SAnoob Joseph 
405f194f198SAnoob Joseph 	/* Fill time_out cycles */
406f194f198SAnoob Joseph 	user_req->time_out = rte_get_timer_cycles() +
407f194f198SAnoob Joseph 			DEFAULT_COMMAND_TIMEOUT * rte_get_timer_hz();
408f194f198SAnoob Joseph 	user_req->extra_time = 0;
409f194f198SAnoob Joseph 
410f194f198SAnoob Joseph 	/* Default mode of software queue */
411f194f198SAnoob Joseph 	mark_cpt_inst(instance);
412f194f198SAnoob Joseph 
413f194f198SAnoob Joseph 	CPT_LOG_DP_DEBUG("Submitted NB cmd with request: %p "
414f194f198SAnoob Joseph 			 "op: %p", user_req, user_req->op);
41544a2cebbSShijith Thotton 	return req;
416f194f198SAnoob Joseph }
417f194f198SAnoob Joseph 
41844a2cebbSShijith Thotton static __rte_always_inline void * __rte_hot
419e9a356e2SSunila Sahu otx_cpt_enq_single_asym(struct cpt_instance *instance,
420c9902a15SDavid George 			struct rte_crypto_op *op)
421e9a356e2SSunila Sahu {
422e9a356e2SSunila Sahu 	struct cpt_qp_meta_info *minfo = &instance->meta_info;
423e9a356e2SSunila Sahu 	struct rte_crypto_asym_op *asym_op = op->asym;
424e9a356e2SSunila Sahu 	struct asym_op_params params = {0};
425e9a356e2SSunila Sahu 	struct cpt_asym_sess_misc *sess;
426e9a356e2SSunila Sahu 	uintptr_t *cop;
427e9a356e2SSunila Sahu 	void *mdata;
42844a2cebbSShijith Thotton 	void *req;
429e9a356e2SSunila Sahu 	int ret;
430e9a356e2SSunila Sahu 
431e9a356e2SSunila Sahu 	if (unlikely(rte_mempool_get(minfo->pool, &mdata) < 0)) {
432e9a356e2SSunila Sahu 		CPT_LOG_DP_ERR("Could not allocate meta buffer for request");
43344a2cebbSShijith Thotton 		rte_errno = ENOMEM;
43444a2cebbSShijith Thotton 		return NULL;
435e9a356e2SSunila Sahu 	}
436e9a356e2SSunila Sahu 
4371f1e4b7cSCiara Power 	sess = (struct cpt_asym_sess_misc *)
4381f1e4b7cSCiara Power 			asym_op->session->sess_private_data;
439e9a356e2SSunila Sahu 
440e9a356e2SSunila Sahu 	/* Store phys_addr of the mdata to meta_buf */
441e9a356e2SSunila Sahu 	params.meta_buf = rte_mempool_virt2iova(mdata);
442e9a356e2SSunila Sahu 
443e9a356e2SSunila Sahu 	cop = mdata;
444e9a356e2SSunila Sahu 	cop[0] = (uintptr_t)mdata;
445e9a356e2SSunila Sahu 	cop[1] = (uintptr_t)op;
446e9a356e2SSunila Sahu 	cop[2] = cop[3] = 0ULL;
447e9a356e2SSunila Sahu 
448e9a356e2SSunila Sahu 	params.req = RTE_PTR_ADD(cop, 4 * sizeof(uintptr_t));
449e9a356e2SSunila Sahu 	params.req->op = cop;
450e9a356e2SSunila Sahu 
451e9a356e2SSunila Sahu 	/* Adjust meta_buf by crypto_op data  and request_info struct */
452e9a356e2SSunila Sahu 	params.meta_buf += (4 * sizeof(uintptr_t)) +
453e9a356e2SSunila Sahu 			   sizeof(struct cpt_request_info);
454e9a356e2SSunila Sahu 
455e9a356e2SSunila Sahu 	switch (sess->xfrm_type) {
456e9a356e2SSunila Sahu 	case RTE_CRYPTO_ASYM_XFORM_MODEX:
457e9a356e2SSunila Sahu 		ret = cpt_modex_prep(&params, &sess->mod_ctx);
458e9a356e2SSunila Sahu 		if (unlikely(ret))
459e9a356e2SSunila Sahu 			goto req_fail;
460e9a356e2SSunila Sahu 		break;
461e9a356e2SSunila Sahu 	case RTE_CRYPTO_ASYM_XFORM_RSA:
462e9a356e2SSunila Sahu 		ret = cpt_enqueue_rsa_op(op, &params, sess);
463e9a356e2SSunila Sahu 		if (unlikely(ret))
464e9a356e2SSunila Sahu 			goto req_fail;
465e9a356e2SSunila Sahu 		break;
466aa2cbd32SSunila Sahu 	case RTE_CRYPTO_ASYM_XFORM_ECDSA:
467aa2cbd32SSunila Sahu 		ret = cpt_enqueue_ecdsa_op(op, &params, sess, otx_fpm_iova);
468aa2cbd32SSunila Sahu 		if (unlikely(ret))
469aa2cbd32SSunila Sahu 			goto req_fail;
470aa2cbd32SSunila Sahu 		break;
47199faef83SSunila Sahu 	case RTE_CRYPTO_ASYM_XFORM_ECPM:
47299faef83SSunila Sahu 		ret = cpt_ecpm_prep(&asym_op->ecpm, &params,
47399faef83SSunila Sahu 				    sess->ec_ctx.curveid);
47499faef83SSunila Sahu 		if (unlikely(ret))
47599faef83SSunila Sahu 			goto req_fail;
47699faef83SSunila Sahu 		break;
47799faef83SSunila Sahu 
478e9a356e2SSunila Sahu 	default:
479e9a356e2SSunila Sahu 		op->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
48044a2cebbSShijith Thotton 		rte_errno = EINVAL;
481e9a356e2SSunila Sahu 		goto req_fail;
482e9a356e2SSunila Sahu 	}
483e9a356e2SSunila Sahu 
484c9902a15SDavid George 	req = otx_cpt_request_enqueue(instance, params.req, sess->cpt_inst_w7);
48544a2cebbSShijith Thotton 	if (unlikely(req == NULL)) {
486e9a356e2SSunila Sahu 		CPT_LOG_DP_ERR("Could not enqueue crypto req");
487e9a356e2SSunila Sahu 		goto req_fail;
488e9a356e2SSunila Sahu 	}
489e9a356e2SSunila Sahu 
49044a2cebbSShijith Thotton 	return req;
491e9a356e2SSunila Sahu 
492e9a356e2SSunila Sahu req_fail:
493e9a356e2SSunila Sahu 	free_op_meta(mdata, minfo->pool);
494e9a356e2SSunila Sahu 
49544a2cebbSShijith Thotton 	return NULL;
496e9a356e2SSunila Sahu }
497e9a356e2SSunila Sahu 
49844a2cebbSShijith Thotton static __rte_always_inline void * __rte_hot
499f194f198SAnoob Joseph otx_cpt_enq_single_sym(struct cpt_instance *instance,
500c9902a15SDavid George 		       struct rte_crypto_op *op)
501f194f198SAnoob Joseph {
502f194f198SAnoob Joseph 	struct cpt_sess_misc *sess;
503f194f198SAnoob Joseph 	struct rte_crypto_sym_op *sym_op = op->sym;
5046045c06aSArchana Muniganti 	struct cpt_request_info *prep_req;
5056045c06aSArchana Muniganti 	void *mdata = NULL;
506f194f198SAnoob Joseph 	int ret = 0;
50744a2cebbSShijith Thotton 	void *req;
508f194f198SAnoob Joseph 	uint64_t cpt_op;
509f194f198SAnoob Joseph 
5102a440d6aSAkhil Goyal 	sess = CRYPTODEV_GET_SYM_SESS_PRIV(sym_op->session);
511f194f198SAnoob Joseph 	cpt_op = sess->cpt_op;
512f194f198SAnoob Joseph 
513f194f198SAnoob Joseph 	if (likely(cpt_op & CPT_OP_CIPHER_MASK))
514ec54bc9dSAnoob Joseph 		ret = fill_fc_params(op, sess, &instance->meta_info, &mdata,
5156045c06aSArchana Muniganti 				     (void **)&prep_req);
516f194f198SAnoob Joseph 	else
517ec54bc9dSAnoob Joseph 		ret = fill_digest_params(op, sess, &instance->meta_info,
5186045c06aSArchana Muniganti 					 &mdata, (void **)&prep_req);
519f194f198SAnoob Joseph 
520f194f198SAnoob Joseph 	if (unlikely(ret)) {
5217be78d02SJosh Soref 		CPT_LOG_DP_ERR("prep crypto req : op %p, cpt_op 0x%x "
522f194f198SAnoob Joseph 			       "ret 0x%x", op, (unsigned int)cpt_op, ret);
52344a2cebbSShijith Thotton 		return NULL;
524f194f198SAnoob Joseph 	}
525f194f198SAnoob Joseph 
526f194f198SAnoob Joseph 	/* Enqueue prepared instruction to h/w */
527c9902a15SDavid George 	req = otx_cpt_request_enqueue(instance, prep_req, sess->cpt_inst_w7);
52844a2cebbSShijith Thotton 	if (unlikely(req == NULL))
529f194f198SAnoob Joseph 		/* Buffer allocated for request preparation need to be freed */
530ec54bc9dSAnoob Joseph 		free_op_meta(mdata, instance->meta_info.pool);
53144a2cebbSShijith Thotton 
53244a2cebbSShijith Thotton 	return req;
533f194f198SAnoob Joseph }
534f194f198SAnoob Joseph 
53544a2cebbSShijith Thotton static __rte_always_inline void * __rte_hot
536f194f198SAnoob Joseph otx_cpt_enq_single_sym_sessless(struct cpt_instance *instance,
537c9902a15SDavid George 				struct rte_crypto_op *op)
538f194f198SAnoob Joseph {
539f194f198SAnoob Joseph 	struct rte_crypto_sym_op *sym_op = op->sym;
540caeba506SAnoob Joseph 	struct rte_cryptodev_sym_session *sess;
54144a2cebbSShijith Thotton 	void *req;
542f194f198SAnoob Joseph 	int ret;
543f194f198SAnoob Joseph 
544caeba506SAnoob Joseph 	/* Create temporary session */
545bdce2564SAkhil Goyal 	if (rte_mempool_get(instance->sess_mp, (void **)&sess) < 0) {
54644a2cebbSShijith Thotton 		rte_errno = ENOMEM;
54744a2cebbSShijith Thotton 		return NULL;
54844a2cebbSShijith Thotton 	}
549f194f198SAnoob Joseph 
550bdce2564SAkhil Goyal 	ret = sym_session_configure(sym_op->xform, sess);
551caeba506SAnoob Joseph 	if (ret)
552caeba506SAnoob Joseph 		goto sess_put;
553f194f198SAnoob Joseph 
554caeba506SAnoob Joseph 	sym_op->session = sess;
555f194f198SAnoob Joseph 
556c9902a15SDavid George 	/* Enqueue op with the tmp session set */
557c9902a15SDavid George 	req = otx_cpt_enq_single_sym(instance, op);
55844a2cebbSShijith Thotton 	if (unlikely(req == NULL))
559bdce2564SAkhil Goyal 		goto sess_put;
560f194f198SAnoob Joseph 
56144a2cebbSShijith Thotton 	return req;
562f194f198SAnoob Joseph 
563caeba506SAnoob Joseph sess_put:
564caeba506SAnoob Joseph 	rte_mempool_put(instance->sess_mp, sess);
56544a2cebbSShijith Thotton 	return NULL;
566f194f198SAnoob Joseph }
567f194f198SAnoob Joseph 
568e9a356e2SSunila Sahu #define OP_TYPE_SYM		0
569e9a356e2SSunila Sahu #define OP_TYPE_ASYM		1
570e9a356e2SSunila Sahu 
57144a2cebbSShijith Thotton static __rte_always_inline void *__rte_hot
572f194f198SAnoob Joseph otx_cpt_enq_single(struct cpt_instance *inst,
573f194f198SAnoob Joseph 		   struct rte_crypto_op *op,
574e9a356e2SSunila Sahu 		   const uint8_t op_type)
575f194f198SAnoob Joseph {
576f194f198SAnoob Joseph 	/* Check for the type */
577f194f198SAnoob Joseph 
578e9a356e2SSunila Sahu 	if (op_type == OP_TYPE_SYM) {
579f194f198SAnoob Joseph 		if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION)
580c9902a15SDavid George 			return otx_cpt_enq_single_sym(inst, op);
581e9a356e2SSunila Sahu 		else
582c9902a15SDavid George 			return otx_cpt_enq_single_sym_sessless(inst, op);
583f194f198SAnoob Joseph 	}
584f194f198SAnoob Joseph 
585e9a356e2SSunila Sahu 	if (op_type == OP_TYPE_ASYM) {
586e9a356e2SSunila Sahu 		if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION)
587c9902a15SDavid George 			return otx_cpt_enq_single_asym(inst, op);
588e9a356e2SSunila Sahu 	}
589e9a356e2SSunila Sahu 
590e9a356e2SSunila Sahu 	/* Should not reach here */
59144a2cebbSShijith Thotton 	rte_errno = ENOTSUP;
59244a2cebbSShijith Thotton 	return NULL;
593e9a356e2SSunila Sahu }
594e9a356e2SSunila Sahu 
595e3866e73SThomas Monjalon static  __rte_always_inline uint16_t __rte_hot
596e9a356e2SSunila Sahu otx_cpt_pkt_enqueue(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops,
597e9a356e2SSunila Sahu 		    const uint8_t op_type)
598ac4d88afSTejasree Kondoj {
599ac4d88afSTejasree Kondoj 	struct cpt_instance *instance = (struct cpt_instance *)qptr;
600c9902a15SDavid George 	uint16_t count, free_slots;
60144a2cebbSShijith Thotton 	void *req;
602ac4d88afSTejasree Kondoj 	struct cpt_vf *cptvf = (struct cpt_vf *)instance;
603ac4d88afSTejasree Kondoj 	struct pending_queue *pqueue = &cptvf->pqueue;
604ac4d88afSTejasree Kondoj 
605c9902a15SDavid George 	free_slots = pending_queue_free_slots(pqueue, DEFAULT_CMD_QLEN,
606c9902a15SDavid George 				DEFAULT_CMD_QRSVD_SLOTS);
607c9902a15SDavid George 	if (nb_ops > free_slots)
608c9902a15SDavid George 		nb_ops = free_slots;
609ac4d88afSTejasree Kondoj 
610ac4d88afSTejasree Kondoj 	count = 0;
611ac4d88afSTejasree Kondoj 	while (likely(count < nb_ops)) {
612f194f198SAnoob Joseph 
613f194f198SAnoob Joseph 		/* Enqueue single op */
614c9902a15SDavid George 		req = otx_cpt_enq_single(instance, ops[count], op_type);
615f194f198SAnoob Joseph 
61644a2cebbSShijith Thotton 		if (unlikely(req == NULL))
617ac4d88afSTejasree Kondoj 			break;
61844a2cebbSShijith Thotton 
619c9902a15SDavid George 		pending_queue_push(pqueue, req, count, DEFAULT_CMD_QLEN);
620ac4d88afSTejasree Kondoj 		count++;
621ac4d88afSTejasree Kondoj 	}
622c9902a15SDavid George 
623c9902a15SDavid George 	if (likely(count)) {
624c9902a15SDavid George 		pending_queue_commit(pqueue, count, DEFAULT_CMD_QLEN);
625ac4d88afSTejasree Kondoj 		otx_cpt_ring_dbell(instance, count);
626c9902a15SDavid George 	}
627ac4d88afSTejasree Kondoj 	return count;
628ac4d88afSTejasree Kondoj }
629ac4d88afSTejasree Kondoj 
630e9a356e2SSunila Sahu static uint16_t
631e9a356e2SSunila Sahu otx_cpt_enqueue_asym(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops)
632e9a356e2SSunila Sahu {
633e9a356e2SSunila Sahu 	return otx_cpt_pkt_enqueue(qptr, ops, nb_ops, OP_TYPE_ASYM);
634e9a356e2SSunila Sahu }
635e9a356e2SSunila Sahu 
636e9a356e2SSunila Sahu static uint16_t
637e9a356e2SSunila Sahu otx_cpt_enqueue_sym(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops)
638e9a356e2SSunila Sahu {
639e9a356e2SSunila Sahu 	return otx_cpt_pkt_enqueue(qptr, ops, nb_ops, OP_TYPE_SYM);
640e9a356e2SSunila Sahu }
641e9a356e2SSunila Sahu 
64244a2cebbSShijith Thotton static __rte_always_inline void
64344a2cebbSShijith Thotton submit_request_to_sso(struct ssows *ws, uintptr_t req,
64444a2cebbSShijith Thotton 		      struct rte_event *rsp_info)
64544a2cebbSShijith Thotton {
64644a2cebbSShijith Thotton 	uint64_t add_work;
64744a2cebbSShijith Thotton 
64844a2cebbSShijith Thotton 	add_work = rsp_info->flow_id | (RTE_EVENT_TYPE_CRYPTODEV << 28) |
649a662baa7SAnoob Joseph 		   (rsp_info->sub_event_type << 20) |
65044a2cebbSShijith Thotton 		   ((uint64_t)(rsp_info->sched_type) << 32);
65144a2cebbSShijith Thotton 
65244a2cebbSShijith Thotton 	if (!rsp_info->sched_type)
65344a2cebbSShijith Thotton 		ssows_head_wait(ws);
65444a2cebbSShijith Thotton 
655e12a0166STyler Retzlaff 	rte_atomic_thread_fence(rte_memory_order_release);
65644a2cebbSShijith Thotton 	ssovf_store_pair(add_work, req, ws->grps[rsp_info->queue_id]);
65744a2cebbSShijith Thotton }
65844a2cebbSShijith Thotton 
65944a2cebbSShijith Thotton uint16_t __rte_hot
66044a2cebbSShijith Thotton otx_crypto_adapter_enqueue(void *port, struct rte_crypto_op *op)
66144a2cebbSShijith Thotton {
66244a2cebbSShijith Thotton 	union rte_event_crypto_metadata *ec_mdata;
66344a2cebbSShijith Thotton 	struct cpt_instance *instance;
66444a2cebbSShijith Thotton 	struct cpt_request_info *req;
66544a2cebbSShijith Thotton 	struct rte_event *rsp_info;
66644a2cebbSShijith Thotton 	uint8_t op_type, cdev_id;
66744a2cebbSShijith Thotton 	uint16_t qp_id;
66844a2cebbSShijith Thotton 
669a974f7cbSAkhil Goyal 	ec_mdata = rte_cryptodev_session_event_mdata_get(op);
67044a2cebbSShijith Thotton 	if (unlikely(ec_mdata == NULL)) {
67144a2cebbSShijith Thotton 		rte_errno = EINVAL;
67244a2cebbSShijith Thotton 		return 0;
67344a2cebbSShijith Thotton 	}
67444a2cebbSShijith Thotton 
67544a2cebbSShijith Thotton 	cdev_id = ec_mdata->request_info.cdev_id;
67644a2cebbSShijith Thotton 	qp_id = ec_mdata->request_info.queue_pair_id;
67744a2cebbSShijith Thotton 	rsp_info = &ec_mdata->response_info;
67844a2cebbSShijith Thotton 	instance = rte_cryptodevs[cdev_id].data->queue_pairs[qp_id];
67944a2cebbSShijith Thotton 
68044a2cebbSShijith Thotton 	if (unlikely(!instance->ca_enabled)) {
68144a2cebbSShijith Thotton 		rte_errno = EINVAL;
68244a2cebbSShijith Thotton 		return 0;
68344a2cebbSShijith Thotton 	}
68444a2cebbSShijith Thotton 
68544a2cebbSShijith Thotton 	op_type = op->type == RTE_CRYPTO_OP_TYPE_SYMMETRIC ? OP_TYPE_SYM :
68644a2cebbSShijith Thotton 							     OP_TYPE_ASYM;
687c9902a15SDavid George 	req = otx_cpt_enq_single(instance, op, op_type);
68844a2cebbSShijith Thotton 	if (unlikely(req == NULL))
68944a2cebbSShijith Thotton 		return 0;
69044a2cebbSShijith Thotton 
69144a2cebbSShijith Thotton 	otx_cpt_ring_dbell(instance, 1);
69244a2cebbSShijith Thotton 	req->qp = instance;
69344a2cebbSShijith Thotton 	submit_request_to_sso(port, (uintptr_t)req, rsp_info);
69444a2cebbSShijith Thotton 
69544a2cebbSShijith Thotton 	return 1;
69644a2cebbSShijith Thotton }
69744a2cebbSShijith Thotton 
698e9a356e2SSunila Sahu static inline void
699e9a356e2SSunila Sahu otx_cpt_asym_rsa_op(struct rte_crypto_op *cop, struct cpt_request_info *req,
700e9a356e2SSunila Sahu 		    struct rte_crypto_rsa_xform *rsa_ctx)
701e9a356e2SSunila Sahu 
702e9a356e2SSunila Sahu {
703e9a356e2SSunila Sahu 	struct rte_crypto_rsa_op_param *rsa = &cop->asym->rsa;
704e9a356e2SSunila Sahu 
705e9a356e2SSunila Sahu 	switch (rsa->op_type) {
706e9a356e2SSunila Sahu 	case RTE_CRYPTO_ASYM_OP_ENCRYPT:
707e9a356e2SSunila Sahu 		rsa->cipher.length = rsa_ctx->n.length;
708e9a356e2SSunila Sahu 		memcpy(rsa->cipher.data, req->rptr, rsa->cipher.length);
709e9a356e2SSunila Sahu 		break;
710e9a356e2SSunila Sahu 	case RTE_CRYPTO_ASYM_OP_DECRYPT:
711*8a97564bSGowrishankar Muthukrishnan 		if (rsa_ctx->padding.type == RTE_CRYPTO_RSA_PADDING_NONE)
712e9a356e2SSunila Sahu 			rsa->message.length = rsa_ctx->n.length;
713e9a356e2SSunila Sahu 		else {
714e9a356e2SSunila Sahu 			/* Get length of decrypted output */
715e9a356e2SSunila Sahu 			rsa->message.length = rte_cpu_to_be_16
716e9a356e2SSunila Sahu 					(*((uint16_t *)req->rptr));
717e9a356e2SSunila Sahu 
718e9a356e2SSunila Sahu 			/* Offset data pointer by length fields */
719e9a356e2SSunila Sahu 			req->rptr += 2;
720e9a356e2SSunila Sahu 		}
721e9a356e2SSunila Sahu 		memcpy(rsa->message.data, req->rptr, rsa->message.length);
722e9a356e2SSunila Sahu 		break;
723e9a356e2SSunila Sahu 	case RTE_CRYPTO_ASYM_OP_SIGN:
724e9a356e2SSunila Sahu 		rsa->sign.length = rsa_ctx->n.length;
725e9a356e2SSunila Sahu 		memcpy(rsa->sign.data, req->rptr, rsa->sign.length);
726e9a356e2SSunila Sahu 		break;
727e9a356e2SSunila Sahu 	case RTE_CRYPTO_ASYM_OP_VERIFY:
728*8a97564bSGowrishankar Muthukrishnan 		if (rsa_ctx->padding.type == RTE_CRYPTO_RSA_PADDING_NONE)
729e9a356e2SSunila Sahu 			rsa->sign.length = rsa_ctx->n.length;
730e9a356e2SSunila Sahu 		else {
731e9a356e2SSunila Sahu 			/* Get length of decrypted output */
732e9a356e2SSunila Sahu 			rsa->sign.length = rte_cpu_to_be_16
733e9a356e2SSunila Sahu 					(*((uint16_t *)req->rptr));
734e9a356e2SSunila Sahu 
735e9a356e2SSunila Sahu 			/* Offset data pointer by length fields */
736e9a356e2SSunila Sahu 			req->rptr += 2;
737e9a356e2SSunila Sahu 		}
738e9a356e2SSunila Sahu 		memcpy(rsa->sign.data, req->rptr, rsa->sign.length);
739e9a356e2SSunila Sahu 
740e9a356e2SSunila Sahu 		if (memcmp(rsa->sign.data, rsa->message.data,
741e9a356e2SSunila Sahu 			   rsa->message.length)) {
742e9a356e2SSunila Sahu 			CPT_LOG_DP_ERR("RSA verification failed");
743e9a356e2SSunila Sahu 			cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
744e9a356e2SSunila Sahu 		}
745e9a356e2SSunila Sahu 		break;
746e9a356e2SSunila Sahu 	default:
747e9a356e2SSunila Sahu 		CPT_LOG_DP_DEBUG("Invalid RSA operation type");
748e9a356e2SSunila Sahu 		cop->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
749e9a356e2SSunila Sahu 		break;
750e9a356e2SSunila Sahu 	}
751e9a356e2SSunila Sahu }
752e9a356e2SSunila Sahu 
753aa2cbd32SSunila Sahu static __rte_always_inline void
754aa2cbd32SSunila Sahu otx_cpt_asym_dequeue_ecdsa_op(struct rte_crypto_ecdsa_op_param *ecdsa,
755aa2cbd32SSunila Sahu 			    struct cpt_request_info *req,
756aa2cbd32SSunila Sahu 			    struct cpt_asym_ec_ctx *ec)
757aa2cbd32SSunila Sahu 
758aa2cbd32SSunila Sahu {
759aa2cbd32SSunila Sahu 	int prime_len = ec_grp[ec->curveid].prime.length;
760aa2cbd32SSunila Sahu 
761aa2cbd32SSunila Sahu 	if (ecdsa->op_type == RTE_CRYPTO_ASYM_OP_VERIFY)
762aa2cbd32SSunila Sahu 		return;
763aa2cbd32SSunila Sahu 
764aa2cbd32SSunila Sahu 	/* Separate out sign r and s components */
765aa2cbd32SSunila Sahu 	memcpy(ecdsa->r.data, req->rptr, prime_len);
766ecd070acSArchana Muniganti 	memcpy(ecdsa->s.data, req->rptr + RTE_ALIGN_CEIL(prime_len, 8),
767ecd070acSArchana Muniganti 	       prime_len);
768aa2cbd32SSunila Sahu 	ecdsa->r.length = prime_len;
769aa2cbd32SSunila Sahu 	ecdsa->s.length = prime_len;
770aa2cbd32SSunila Sahu }
771aa2cbd32SSunila Sahu 
77299faef83SSunila Sahu static __rte_always_inline void
77399faef83SSunila Sahu otx_cpt_asym_dequeue_ecpm_op(struct rte_crypto_ecpm_op_param *ecpm,
77499faef83SSunila Sahu 			     struct cpt_request_info *req,
77599faef83SSunila Sahu 			     struct cpt_asym_ec_ctx *ec)
77699faef83SSunila Sahu {
77799faef83SSunila Sahu 	int prime_len = ec_grp[ec->curveid].prime.length;
77899faef83SSunila Sahu 
77999faef83SSunila Sahu 	memcpy(ecpm->r.x.data, req->rptr, prime_len);
780ecd070acSArchana Muniganti 	memcpy(ecpm->r.y.data, req->rptr + RTE_ALIGN_CEIL(prime_len, 8),
781ecd070acSArchana Muniganti 	       prime_len);
78299faef83SSunila Sahu 	ecpm->r.x.length = prime_len;
78399faef83SSunila Sahu 	ecpm->r.y.length = prime_len;
78499faef83SSunila Sahu }
78599faef83SSunila Sahu 
786e3866e73SThomas Monjalon static __rte_always_inline void __rte_hot
787e9a356e2SSunila Sahu otx_cpt_asym_post_process(struct rte_crypto_op *cop,
788e9a356e2SSunila Sahu 			  struct cpt_request_info *req)
789e9a356e2SSunila Sahu {
790e9a356e2SSunila Sahu 	struct rte_crypto_asym_op *op = cop->asym;
791e9a356e2SSunila Sahu 	struct cpt_asym_sess_misc *sess;
792e9a356e2SSunila Sahu 
7931f1e4b7cSCiara Power 	sess = (struct cpt_asym_sess_misc *) op->session->sess_private_data;
794e9a356e2SSunila Sahu 
795e9a356e2SSunila Sahu 	switch (sess->xfrm_type) {
796e9a356e2SSunila Sahu 	case RTE_CRYPTO_ASYM_XFORM_RSA:
797e9a356e2SSunila Sahu 		otx_cpt_asym_rsa_op(cop, req, &sess->rsa_ctx);
798e9a356e2SSunila Sahu 		break;
799e9a356e2SSunila Sahu 	case RTE_CRYPTO_ASYM_XFORM_MODEX:
800e9a356e2SSunila Sahu 		op->modex.result.length = sess->mod_ctx.modulus.length;
801e9a356e2SSunila Sahu 		memcpy(op->modex.result.data, req->rptr,
802e9a356e2SSunila Sahu 		       op->modex.result.length);
803e9a356e2SSunila Sahu 		break;
804aa2cbd32SSunila Sahu 	case RTE_CRYPTO_ASYM_XFORM_ECDSA:
805aa2cbd32SSunila Sahu 		otx_cpt_asym_dequeue_ecdsa_op(&op->ecdsa, req, &sess->ec_ctx);
806aa2cbd32SSunila Sahu 		break;
80799faef83SSunila Sahu 	case RTE_CRYPTO_ASYM_XFORM_ECPM:
80899faef83SSunila Sahu 		otx_cpt_asym_dequeue_ecpm_op(&op->ecpm, req, &sess->ec_ctx);
80999faef83SSunila Sahu 		break;
810e9a356e2SSunila Sahu 	default:
811e9a356e2SSunila Sahu 		CPT_LOG_DP_DEBUG("Invalid crypto xform type");
812e9a356e2SSunila Sahu 		cop->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
813e9a356e2SSunila Sahu 		break;
814e9a356e2SSunila Sahu 	}
815e9a356e2SSunila Sahu }
816e9a356e2SSunila Sahu 
817e3866e73SThomas Monjalon static __rte_always_inline void __rte_hot
818e9a356e2SSunila Sahu otx_cpt_dequeue_post_process(struct rte_crypto_op *cop, uintptr_t *rsp,
819e9a356e2SSunila Sahu 			     const uint8_t op_type)
820f194f198SAnoob Joseph {
821f194f198SAnoob Joseph 	/* H/w has returned success */
822f194f198SAnoob Joseph 	cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
823f194f198SAnoob Joseph 
824f194f198SAnoob Joseph 	/* Perform further post processing */
825f194f198SAnoob Joseph 
826e9a356e2SSunila Sahu 	if ((op_type == OP_TYPE_SYM) &&
827e9a356e2SSunila Sahu 	    (cop->type == RTE_CRYPTO_OP_TYPE_SYMMETRIC)) {
828f194f198SAnoob Joseph 		/* Check if auth verify need to be completed */
829f194f198SAnoob Joseph 		if (unlikely(rsp[2]))
830f194f198SAnoob Joseph 			compl_auth_verify(cop, (uint8_t *)rsp[2], rsp[3]);
831f194f198SAnoob Joseph 		return;
832f194f198SAnoob Joseph 	}
833e9a356e2SSunila Sahu 
834e9a356e2SSunila Sahu 	if ((op_type == OP_TYPE_ASYM) &&
835e9a356e2SSunila Sahu 	    (cop->type == RTE_CRYPTO_OP_TYPE_ASYMMETRIC)) {
836e9a356e2SSunila Sahu 		rsp = RTE_PTR_ADD(rsp, 4 * sizeof(uintptr_t));
837e9a356e2SSunila Sahu 		otx_cpt_asym_post_process(cop, (struct cpt_request_info *)rsp);
838f194f198SAnoob Joseph 	}
839f194f198SAnoob Joseph 
840e9a356e2SSunila Sahu 	return;
841e9a356e2SSunila Sahu }
842e9a356e2SSunila Sahu 
84344a2cebbSShijith Thotton static inline void
84444a2cebbSShijith Thotton free_sym_session_data(const struct cpt_instance *instance,
84544a2cebbSShijith Thotton 		      struct rte_crypto_op *cop)
84644a2cebbSShijith Thotton {
8472a440d6aSAkhil Goyal 	void *sess_private_data_t = CRYPTODEV_GET_SYM_SESS_PRIV(cop->sym->session);
848bdce2564SAkhil Goyal 
84944a2cebbSShijith Thotton 	memset(sess_private_data_t, 0, cpt_get_session_size());
85044a2cebbSShijith Thotton 	rte_mempool_put(instance->sess_mp, cop->sym->session);
85144a2cebbSShijith Thotton 	cop->sym->session = NULL;
85244a2cebbSShijith Thotton }
85344a2cebbSShijith Thotton 
85444a2cebbSShijith Thotton static __rte_always_inline struct rte_crypto_op *
85544a2cebbSShijith Thotton otx_cpt_process_response(const struct cpt_instance *instance, uintptr_t *rsp,
85644a2cebbSShijith Thotton 			 uint8_t cc, const uint8_t op_type)
85744a2cebbSShijith Thotton {
85844a2cebbSShijith Thotton 	struct rte_crypto_op *cop;
85944a2cebbSShijith Thotton 	void *metabuf;
86044a2cebbSShijith Thotton 
86144a2cebbSShijith Thotton 	metabuf = (void *)rsp[0];
86244a2cebbSShijith Thotton 	cop = (void *)rsp[1];
86344a2cebbSShijith Thotton 
86444a2cebbSShijith Thotton 	/* Check completion code */
86544a2cebbSShijith Thotton 	if (likely(cc == 0)) {
86644a2cebbSShijith Thotton 		/* H/w success pkt. Post process */
86744a2cebbSShijith Thotton 		otx_cpt_dequeue_post_process(cop, rsp, op_type);
86844a2cebbSShijith Thotton 	} else if (cc == ERR_GC_ICV_MISCOMPARE) {
86944a2cebbSShijith Thotton 		/* auth data mismatch */
87044a2cebbSShijith Thotton 		cop->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
87144a2cebbSShijith Thotton 	} else {
87244a2cebbSShijith Thotton 		/* Error */
87344a2cebbSShijith Thotton 		cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
87444a2cebbSShijith Thotton 	}
87544a2cebbSShijith Thotton 
87644a2cebbSShijith Thotton 	if (unlikely(cop->sess_type == RTE_CRYPTO_OP_SESSIONLESS))
87744a2cebbSShijith Thotton 		free_sym_session_data(instance, cop);
87844a2cebbSShijith Thotton 	free_op_meta(metabuf, instance->meta_info.pool);
87944a2cebbSShijith Thotton 
88044a2cebbSShijith Thotton 	return cop;
88144a2cebbSShijith Thotton }
88244a2cebbSShijith Thotton 
883e3866e73SThomas Monjalon static __rte_always_inline uint16_t __rte_hot
884e9a356e2SSunila Sahu otx_cpt_pkt_dequeue(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops,
885e9a356e2SSunila Sahu 		    const uint8_t op_type)
88689f1a8d6STejasree Kondoj {
88789f1a8d6STejasree Kondoj 	struct cpt_instance *instance = (struct cpt_instance *)qptr;
888f194f198SAnoob Joseph 	struct cpt_request_info *user_req;
88989f1a8d6STejasree Kondoj 	struct cpt_vf *cptvf = (struct cpt_vf *)instance;
890f194f198SAnoob Joseph 	uint8_t cc[nb_ops];
891f194f198SAnoob Joseph 	int i, count, pcount;
892f194f198SAnoob Joseph 	uint8_t ret;
893f194f198SAnoob Joseph 	int nb_completed;
89489f1a8d6STejasree Kondoj 	struct pending_queue *pqueue = &cptvf->pqueue;
895f194f198SAnoob Joseph 
896c9902a15SDavid George 	pcount = pending_queue_level(pqueue, DEFAULT_CMD_QLEN);
897c9902a15SDavid George 
898c9902a15SDavid George 	/* Ensure pcount isn't read before data lands */
899e12a0166STyler Retzlaff 	rte_atomic_thread_fence(rte_memory_order_acquire);
900c9902a15SDavid George 
901f194f198SAnoob Joseph 	count = (nb_ops > pcount) ? pcount : nb_ops;
902f194f198SAnoob Joseph 
903f194f198SAnoob Joseph 	for (i = 0; i < count; i++) {
904c9902a15SDavid George 		pending_queue_peek(pqueue, (void **) &user_req,
905c9902a15SDavid George 			DEFAULT_CMD_QLEN, i + 1 < count);
906f194f198SAnoob Joseph 
907f194f198SAnoob Joseph 		ret = check_nb_command_id(user_req, instance);
908f194f198SAnoob Joseph 
909f194f198SAnoob Joseph 		if (unlikely(ret == ERR_REQ_PENDING)) {
910f194f198SAnoob Joseph 			/* Stop checking for completions */
911f194f198SAnoob Joseph 			break;
912f194f198SAnoob Joseph 		}
913f194f198SAnoob Joseph 
914f194f198SAnoob Joseph 		/* Return completion code and op handle */
915f194f198SAnoob Joseph 		cc[i] = ret;
916f194f198SAnoob Joseph 		ops[i] = user_req->op;
917f194f198SAnoob Joseph 
918f194f198SAnoob Joseph 		CPT_LOG_DP_DEBUG("Request %p Op %p completed with code %d",
919f194f198SAnoob Joseph 				 user_req, user_req->op, ret);
920f194f198SAnoob Joseph 
921c9902a15SDavid George 		pending_queue_pop(pqueue, DEFAULT_CMD_QLEN);
922f194f198SAnoob Joseph 	}
923f194f198SAnoob Joseph 
924f194f198SAnoob Joseph 	nb_completed = i;
925f194f198SAnoob Joseph 
926f194f198SAnoob Joseph 	for (i = 0; i < nb_completed; i++) {
92789f1a8d6STejasree Kondoj 		if (likely((i + 1) < nb_completed))
92889f1a8d6STejasree Kondoj 			rte_prefetch0(ops[i+1]);
929f194f198SAnoob Joseph 
93044a2cebbSShijith Thotton 		ops[i] = otx_cpt_process_response(instance, (void *)ops[i],
93144a2cebbSShijith Thotton 						  cc[i], op_type);
932f194f198SAnoob Joseph 	}
933f194f198SAnoob Joseph 
93489f1a8d6STejasree Kondoj 	return nb_completed;
93589f1a8d6STejasree Kondoj }
93689f1a8d6STejasree Kondoj 
937e9a356e2SSunila Sahu static uint16_t
938e9a356e2SSunila Sahu otx_cpt_dequeue_asym(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops)
939e9a356e2SSunila Sahu {
940e9a356e2SSunila Sahu 	return otx_cpt_pkt_dequeue(qptr, ops, nb_ops, OP_TYPE_ASYM);
941e9a356e2SSunila Sahu }
942e9a356e2SSunila Sahu 
943e9a356e2SSunila Sahu static uint16_t
944e9a356e2SSunila Sahu otx_cpt_dequeue_sym(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops)
945e9a356e2SSunila Sahu {
946e9a356e2SSunila Sahu 	return otx_cpt_pkt_dequeue(qptr, ops, nb_ops, OP_TYPE_SYM);
947e9a356e2SSunila Sahu }
948e9a356e2SSunila Sahu 
94944a2cebbSShijith Thotton uintptr_t __rte_hot
95044a2cebbSShijith Thotton otx_crypto_adapter_dequeue(uintptr_t get_work1)
95144a2cebbSShijith Thotton {
95244a2cebbSShijith Thotton 	const struct cpt_instance *instance;
95344a2cebbSShijith Thotton 	struct cpt_request_info *req;
95444a2cebbSShijith Thotton 	struct rte_crypto_op *cop;
95544a2cebbSShijith Thotton 	uint8_t cc, op_type;
95644a2cebbSShijith Thotton 	uintptr_t *rsp;
95744a2cebbSShijith Thotton 
95844a2cebbSShijith Thotton 	req = (struct cpt_request_info *)get_work1;
95944a2cebbSShijith Thotton 	instance = req->qp;
96044a2cebbSShijith Thotton 	rsp = req->op;
96144a2cebbSShijith Thotton 	cop = (void *)rsp[1];
96244a2cebbSShijith Thotton 	op_type = cop->type == RTE_CRYPTO_OP_TYPE_SYMMETRIC ? OP_TYPE_SYM :
96344a2cebbSShijith Thotton 							      OP_TYPE_ASYM;
96444a2cebbSShijith Thotton 
96544a2cebbSShijith Thotton 	do {
96644a2cebbSShijith Thotton 		cc = check_nb_command_id(
96744a2cebbSShijith Thotton 			req, (struct cpt_instance *)(uintptr_t)instance);
96844a2cebbSShijith Thotton 	} while (cc == ERR_REQ_PENDING);
96944a2cebbSShijith Thotton 
97044a2cebbSShijith Thotton 	cop = otx_cpt_process_response(instance, (void *)req->op, cc, op_type);
97144a2cebbSShijith Thotton 
97244a2cebbSShijith Thotton 	return (uintptr_t)(cop);
97344a2cebbSShijith Thotton }
97444a2cebbSShijith Thotton 
9750906b99fSMurthy NSSR static struct rte_cryptodev_ops cptvf_ops = {
9760906b99fSMurthy NSSR 	/* Device related operations */
9770906b99fSMurthy NSSR 	.dev_configure = otx_cpt_dev_config,
9780906b99fSMurthy NSSR 	.dev_start = otx_cpt_dev_start,
9790906b99fSMurthy NSSR 	.dev_stop = otx_cpt_dev_stop,
9800906b99fSMurthy NSSR 	.dev_close = otx_cpt_dev_close,
9810906b99fSMurthy NSSR 	.dev_infos_get = otx_cpt_dev_info_get,
9820906b99fSMurthy NSSR 
983966b43fdSAnkur Dwivedi 	.stats_get = NULL,
984966b43fdSAnkur Dwivedi 	.stats_reset = NULL,
9850961348fSMurthy NSSR 	.queue_pair_setup = otx_cpt_que_pair_setup,
9860961348fSMurthy NSSR 	.queue_pair_release = otx_cpt_que_pair_release,
9870906b99fSMurthy NSSR 
9880906b99fSMurthy NSSR 	/* Crypto related operations */
98943d01767SNithin Dabilpuram 	.sym_session_get_size = otx_cpt_get_session_size,
99043d01767SNithin Dabilpuram 	.sym_session_configure = otx_cpt_session_cfg,
99133bcaae5SKanaka Durga Kotamarthy 	.sym_session_clear = otx_cpt_session_clear,
99233bcaae5SKanaka Durga Kotamarthy 
99333bcaae5SKanaka Durga Kotamarthy 	.asym_session_get_size = otx_cpt_asym_session_size_get,
99433bcaae5SKanaka Durga Kotamarthy 	.asym_session_configure = otx_cpt_asym_session_cfg,
99533bcaae5SKanaka Durga Kotamarthy 	.asym_session_clear = otx_cpt_asym_session_clear,
9960906b99fSMurthy NSSR };
9970906b99fSMurthy NSSR 
998bfe2ae49SAnoob Joseph int
999bfe2ae49SAnoob Joseph otx_cpt_dev_create(struct rte_cryptodev *c_dev)
1000bfe2ae49SAnoob Joseph {
10010dc1cffaSAnkur Dwivedi 	struct rte_pci_device *pdev = RTE_DEV_TO_PCI(c_dev->device);
10020dc1cffaSAnkur Dwivedi 	struct cpt_vf *cptvf = NULL;
10030dc1cffaSAnkur Dwivedi 	void *reg_base;
10040dc1cffaSAnkur Dwivedi 	char dev_name[32];
10050dc1cffaSAnkur Dwivedi 	int ret;
10060dc1cffaSAnkur Dwivedi 
10070dc1cffaSAnkur Dwivedi 	if (pdev->mem_resource[0].phys_addr == 0ULL)
10080dc1cffaSAnkur Dwivedi 		return -EIO;
10090dc1cffaSAnkur Dwivedi 
10100dc1cffaSAnkur Dwivedi 	/* for secondary processes, we don't initialise any further as primary
10110dc1cffaSAnkur Dwivedi 	 * has already done this work.
10120dc1cffaSAnkur Dwivedi 	 */
10130dc1cffaSAnkur Dwivedi 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1014bfe2ae49SAnoob Joseph 		return 0;
10150dc1cffaSAnkur Dwivedi 
10160dc1cffaSAnkur Dwivedi 	cptvf = rte_zmalloc_socket("otx_cryptodev_private_mem",
10170dc1cffaSAnkur Dwivedi 			sizeof(struct cpt_vf), RTE_CACHE_LINE_SIZE,
10180dc1cffaSAnkur Dwivedi 			rte_socket_id());
10190dc1cffaSAnkur Dwivedi 
10200dc1cffaSAnkur Dwivedi 	if (cptvf == NULL) {
10210dc1cffaSAnkur Dwivedi 		CPT_LOG_ERR("Cannot allocate memory for device private data");
10220dc1cffaSAnkur Dwivedi 		return -ENOMEM;
10230dc1cffaSAnkur Dwivedi 	}
10240dc1cffaSAnkur Dwivedi 
10250dc1cffaSAnkur Dwivedi 	snprintf(dev_name, 32, "%02x:%02x.%x",
10260dc1cffaSAnkur Dwivedi 			pdev->addr.bus, pdev->addr.devid, pdev->addr.function);
10270dc1cffaSAnkur Dwivedi 
10280dc1cffaSAnkur Dwivedi 	reg_base = pdev->mem_resource[0].addr;
10290dc1cffaSAnkur Dwivedi 	if (!reg_base) {
10300dc1cffaSAnkur Dwivedi 		CPT_LOG_ERR("Failed to map BAR0 of %s", dev_name);
10310dc1cffaSAnkur Dwivedi 		ret = -ENODEV;
10320dc1cffaSAnkur Dwivedi 		goto fail;
10330dc1cffaSAnkur Dwivedi 	}
10340dc1cffaSAnkur Dwivedi 
10350dc1cffaSAnkur Dwivedi 	ret = otx_cpt_hw_init(cptvf, pdev, reg_base, dev_name);
10360dc1cffaSAnkur Dwivedi 	if (ret) {
10370dc1cffaSAnkur Dwivedi 		CPT_LOG_ERR("Failed to init cptvf %s", dev_name);
10380dc1cffaSAnkur Dwivedi 		ret = -EIO;
10390dc1cffaSAnkur Dwivedi 		goto fail;
10400dc1cffaSAnkur Dwivedi 	}
10410dc1cffaSAnkur Dwivedi 
104213d711f3SKanaka Durga Kotamarthy 	switch (cptvf->vftype) {
104313d711f3SKanaka Durga Kotamarthy 	case OTX_CPT_VF_TYPE_AE:
104413d711f3SKanaka Durga Kotamarthy 		/* Set asymmetric cpt feature flags */
104513d711f3SKanaka Durga Kotamarthy 		c_dev->feature_flags = RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO |
104633bcaae5SKanaka Durga Kotamarthy 				RTE_CRYPTODEV_FF_HW_ACCELERATED |
104733bcaae5SKanaka Durga Kotamarthy 				RTE_CRYPTODEV_FF_RSA_PRIV_OP_KEY_QT;
104813d711f3SKanaka Durga Kotamarthy 		break;
104913d711f3SKanaka Durga Kotamarthy 	case OTX_CPT_VF_TYPE_SE:
105013d711f3SKanaka Durga Kotamarthy 		/* Set symmetric cpt feature flags */
105113d711f3SKanaka Durga Kotamarthy 		c_dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
105213d711f3SKanaka Durga Kotamarthy 				RTE_CRYPTODEV_FF_HW_ACCELERATED |
105313d711f3SKanaka Durga Kotamarthy 				RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
105413d711f3SKanaka Durga Kotamarthy 				RTE_CRYPTODEV_FF_IN_PLACE_SGL |
105516c01147SDidier Pallard 				RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT |
105613d711f3SKanaka Durga Kotamarthy 				RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT |
1057b3aaf24dSPablo de Lara 				RTE_CRYPTODEV_FF_OOP_SGL_IN_SGL_OUT |
1058de5eb0a6STejasree Kondoj 				RTE_CRYPTODEV_FF_SYM_SESSIONLESS |
1059de5eb0a6STejasree Kondoj 				RTE_CRYPTODEV_FF_DIGEST_ENCRYPTED;
106013d711f3SKanaka Durga Kotamarthy 		break;
106113d711f3SKanaka Durga Kotamarthy 	default:
106213d711f3SKanaka Durga Kotamarthy 		/* Feature not supported. Abort */
106313d711f3SKanaka Durga Kotamarthy 		CPT_LOG_ERR("VF type not supported by %s", dev_name);
106413d711f3SKanaka Durga Kotamarthy 		ret = -EIO;
106513d711f3SKanaka Durga Kotamarthy 		goto deinit_dev;
106613d711f3SKanaka Durga Kotamarthy 	}
106713d711f3SKanaka Durga Kotamarthy 
10680dc1cffaSAnkur Dwivedi 	/* Start off timer for mailbox interrupts */
10690dc1cffaSAnkur Dwivedi 	otx_cpt_periodic_alarm_start(cptvf);
10700dc1cffaSAnkur Dwivedi 
10710906b99fSMurthy NSSR 	c_dev->dev_ops = &cptvf_ops;
10720dc1cffaSAnkur Dwivedi 
1073e9a356e2SSunila Sahu 	if (c_dev->feature_flags & RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO) {
1074e9a356e2SSunila Sahu 		c_dev->enqueue_burst = otx_cpt_enqueue_sym;
1075e9a356e2SSunila Sahu 		c_dev->dequeue_burst = otx_cpt_dequeue_sym;
1076e9a356e2SSunila Sahu 	} else {
1077e9a356e2SSunila Sahu 		c_dev->enqueue_burst = otx_cpt_enqueue_asym;
1078e9a356e2SSunila Sahu 		c_dev->dequeue_burst = otx_cpt_dequeue_asym;
1079e9a356e2SSunila Sahu 	}
10800dc1cffaSAnkur Dwivedi 
10810dc1cffaSAnkur Dwivedi 	/* Save dev private data */
10820dc1cffaSAnkur Dwivedi 	c_dev->data->dev_private = cptvf;
10830dc1cffaSAnkur Dwivedi 
10840dc1cffaSAnkur Dwivedi 	return 0;
10850dc1cffaSAnkur Dwivedi 
108613d711f3SKanaka Durga Kotamarthy deinit_dev:
108713d711f3SKanaka Durga Kotamarthy 	otx_cpt_deinit_device(cptvf);
108813d711f3SKanaka Durga Kotamarthy 
10890dc1cffaSAnkur Dwivedi fail:
10900dc1cffaSAnkur Dwivedi 	if (cptvf) {
10910dc1cffaSAnkur Dwivedi 		/* Free private data allocated */
10920dc1cffaSAnkur Dwivedi 		rte_free(cptvf);
10930dc1cffaSAnkur Dwivedi 	}
10940dc1cffaSAnkur Dwivedi 
10950dc1cffaSAnkur Dwivedi 	return ret;
1096bfe2ae49SAnoob Joseph }
1097