xref: /dpdk/drivers/crypto/octeontx/otx_cryptodev_ops.c (revision 44a2cebbd4898e6d85eff6426342bef7c6bf23dd)
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>
9*44a2cebbSShijith Thotton #include <rte_eventdev.h>
10*44a2cebbSShijith 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 
26*44a2cebbSShijith Thotton #include "ssovf_worker.h"
27*44a2cebbSShijith 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");
2957293bae1SArchana Muniganti 		ret = -ENOTSUP;
2967293bae1SArchana Muniganti 		goto priv_put;
2977293bae1SArchana Muniganti 	}
2987293bae1SArchana Muniganti 
299caeba506SAnoob Joseph 	set_sym_session_private_data(sess, driver_id, priv);
300caeba506SAnoob Joseph 
301caeba506SAnoob Joseph 	misc->ctx_dma_addr = rte_mempool_virt2iova(misc) +
30243d01767SNithin Dabilpuram 			     sizeof(struct cpt_sess_misc);
303caeba506SAnoob Joseph 
3046045c06aSArchana Muniganti 	vq_cmd_w3.u64 = 0;
3056045c06aSArchana Muniganti 	vq_cmd_w3.s.grp = 0;
3066045c06aSArchana Muniganti 	vq_cmd_w3.s.cptr = misc->ctx_dma_addr + offsetof(struct cpt_ctx,
3076045c06aSArchana Muniganti 							 mc_ctx);
3086045c06aSArchana Muniganti 
3096045c06aSArchana Muniganti 	misc->cpt_inst_w7 = vq_cmd_w3.u64;
3106045c06aSArchana Muniganti 
311caeba506SAnoob Joseph 	return 0;
312caeba506SAnoob Joseph 
313caeba506SAnoob Joseph priv_put:
314caeba506SAnoob Joseph 	if (priv)
315caeba506SAnoob Joseph 		rte_mempool_put(pool, priv);
316caeba506SAnoob Joseph 	return -ENOTSUP;
317caeba506SAnoob Joseph }
318caeba506SAnoob Joseph 
319caeba506SAnoob Joseph static void
320caeba506SAnoob Joseph sym_session_clear(int driver_id, struct rte_cryptodev_sym_session *sess)
321caeba506SAnoob Joseph {
322caeba506SAnoob Joseph 	void *priv = get_sym_session_private_data(sess, driver_id);
323caeba506SAnoob Joseph 	struct rte_mempool *pool;
324caeba506SAnoob Joseph 
325caeba506SAnoob Joseph 	if (priv == NULL)
326caeba506SAnoob Joseph 		return;
327caeba506SAnoob Joseph 
328caeba506SAnoob Joseph 	memset(priv, 0, cpt_get_session_size());
329caeba506SAnoob Joseph 
330caeba506SAnoob Joseph 	pool = rte_mempool_from_obj(priv);
331caeba506SAnoob Joseph 
332caeba506SAnoob Joseph 	set_sym_session_private_data(sess, driver_id, NULL);
333caeba506SAnoob Joseph 
334caeba506SAnoob Joseph 	rte_mempool_put(pool, priv);
33543d01767SNithin Dabilpuram }
33643d01767SNithin Dabilpuram 
33743d01767SNithin Dabilpuram static int
33843d01767SNithin Dabilpuram otx_cpt_session_cfg(struct rte_cryptodev *dev,
33943d01767SNithin Dabilpuram 		    struct rte_crypto_sym_xform *xform,
34043d01767SNithin Dabilpuram 		    struct rte_cryptodev_sym_session *sess,
341caeba506SAnoob Joseph 		    struct rte_mempool *pool)
34243d01767SNithin Dabilpuram {
34343d01767SNithin Dabilpuram 	CPT_PMD_INIT_FUNC_TRACE();
34443d01767SNithin Dabilpuram 
345caeba506SAnoob Joseph 	return sym_session_configure(dev->driver_id, xform, sess, pool);
34643d01767SNithin Dabilpuram }
34743d01767SNithin Dabilpuram 
34843d01767SNithin Dabilpuram 
34943d01767SNithin Dabilpuram static void
35043d01767SNithin Dabilpuram otx_cpt_session_clear(struct rte_cryptodev *dev,
35143d01767SNithin Dabilpuram 		  struct rte_cryptodev_sym_session *sess)
35243d01767SNithin Dabilpuram {
35343d01767SNithin Dabilpuram 	CPT_PMD_INIT_FUNC_TRACE();
354caeba506SAnoob Joseph 
355caeba506SAnoob Joseph 	return sym_session_clear(dev->driver_id, sess);
35643d01767SNithin Dabilpuram }
35743d01767SNithin Dabilpuram 
35833bcaae5SKanaka Durga Kotamarthy static unsigned int
35933bcaae5SKanaka Durga Kotamarthy otx_cpt_asym_session_size_get(struct rte_cryptodev *dev __rte_unused)
36033bcaae5SKanaka Durga Kotamarthy {
36133bcaae5SKanaka Durga Kotamarthy 	return sizeof(struct cpt_asym_sess_misc);
36233bcaae5SKanaka Durga Kotamarthy }
36333bcaae5SKanaka Durga Kotamarthy 
36433bcaae5SKanaka Durga Kotamarthy static int
36533bcaae5SKanaka Durga Kotamarthy otx_cpt_asym_session_cfg(struct rte_cryptodev *dev,
36633bcaae5SKanaka Durga Kotamarthy 			 struct rte_crypto_asym_xform *xform __rte_unused,
36733bcaae5SKanaka Durga Kotamarthy 			 struct rte_cryptodev_asym_session *sess,
36833bcaae5SKanaka Durga Kotamarthy 			 struct rte_mempool *pool)
36933bcaae5SKanaka Durga Kotamarthy {
37033bcaae5SKanaka Durga Kotamarthy 	struct cpt_asym_sess_misc *priv;
37133bcaae5SKanaka Durga Kotamarthy 	int ret;
37233bcaae5SKanaka Durga Kotamarthy 
37333bcaae5SKanaka Durga Kotamarthy 	CPT_PMD_INIT_FUNC_TRACE();
37433bcaae5SKanaka Durga Kotamarthy 
37533bcaae5SKanaka Durga Kotamarthy 	if (rte_mempool_get(pool, (void **)&priv)) {
37633bcaae5SKanaka Durga Kotamarthy 		CPT_LOG_ERR("Could not allocate session private data");
37733bcaae5SKanaka Durga Kotamarthy 		return -ENOMEM;
37833bcaae5SKanaka Durga Kotamarthy 	}
37933bcaae5SKanaka Durga Kotamarthy 
38033bcaae5SKanaka Durga Kotamarthy 	memset(priv, 0, sizeof(struct cpt_asym_sess_misc));
38133bcaae5SKanaka Durga Kotamarthy 
38233bcaae5SKanaka Durga Kotamarthy 	ret = cpt_fill_asym_session_parameters(priv, xform);
38333bcaae5SKanaka Durga Kotamarthy 	if (ret) {
38433bcaae5SKanaka Durga Kotamarthy 		CPT_LOG_ERR("Could not configure session parameters");
38533bcaae5SKanaka Durga Kotamarthy 
38633bcaae5SKanaka Durga Kotamarthy 		/* Return session to mempool */
38733bcaae5SKanaka Durga Kotamarthy 		rte_mempool_put(pool, priv);
38833bcaae5SKanaka Durga Kotamarthy 		return ret;
38933bcaae5SKanaka Durga Kotamarthy 	}
39033bcaae5SKanaka Durga Kotamarthy 
3916045c06aSArchana Muniganti 	priv->cpt_inst_w7 = 0;
3926045c06aSArchana Muniganti 
39333bcaae5SKanaka Durga Kotamarthy 	set_asym_session_private_data(sess, dev->driver_id, priv);
39433bcaae5SKanaka Durga Kotamarthy 	return 0;
39533bcaae5SKanaka Durga Kotamarthy }
39633bcaae5SKanaka Durga Kotamarthy 
39733bcaae5SKanaka Durga Kotamarthy static void
39833bcaae5SKanaka Durga Kotamarthy otx_cpt_asym_session_clear(struct rte_cryptodev *dev,
39933bcaae5SKanaka Durga Kotamarthy 			   struct rte_cryptodev_asym_session *sess)
40033bcaae5SKanaka Durga Kotamarthy {
40133bcaae5SKanaka Durga Kotamarthy 	struct cpt_asym_sess_misc *priv;
40233bcaae5SKanaka Durga Kotamarthy 	struct rte_mempool *sess_mp;
40333bcaae5SKanaka Durga Kotamarthy 
40433bcaae5SKanaka Durga Kotamarthy 	CPT_PMD_INIT_FUNC_TRACE();
40533bcaae5SKanaka Durga Kotamarthy 
40633bcaae5SKanaka Durga Kotamarthy 	priv = get_asym_session_private_data(sess, dev->driver_id);
40733bcaae5SKanaka Durga Kotamarthy 
40833bcaae5SKanaka Durga Kotamarthy 	if (priv == NULL)
40933bcaae5SKanaka Durga Kotamarthy 		return;
41033bcaae5SKanaka Durga Kotamarthy 
41133bcaae5SKanaka Durga Kotamarthy 	/* Free resources allocated during session configure */
41233bcaae5SKanaka Durga Kotamarthy 	cpt_free_asym_session_parameters(priv);
41333bcaae5SKanaka Durga Kotamarthy 	memset(priv, 0, otx_cpt_asym_session_size_get(dev));
41433bcaae5SKanaka Durga Kotamarthy 	sess_mp = rte_mempool_from_obj(priv);
41533bcaae5SKanaka Durga Kotamarthy 	set_asym_session_private_data(sess, dev->driver_id, NULL);
41633bcaae5SKanaka Durga Kotamarthy 	rte_mempool_put(sess_mp, priv);
41733bcaae5SKanaka Durga Kotamarthy }
41833bcaae5SKanaka Durga Kotamarthy 
419*44a2cebbSShijith Thotton static __rte_always_inline void * __rte_hot
420f194f198SAnoob Joseph otx_cpt_request_enqueue(struct cpt_instance *instance,
421f194f198SAnoob Joseph 			struct pending_queue *pqueue,
4226045c06aSArchana Muniganti 			void *req, uint64_t cpt_inst_w7)
423f194f198SAnoob Joseph {
424f194f198SAnoob Joseph 	struct cpt_request_info *user_req = (struct cpt_request_info *)req;
425f194f198SAnoob Joseph 
426*44a2cebbSShijith Thotton 	if (unlikely(pqueue->pending_count >= DEFAULT_CMD_QLEN)) {
427*44a2cebbSShijith Thotton 		rte_errno = EAGAIN;
428*44a2cebbSShijith Thotton 		return NULL;
429*44a2cebbSShijith Thotton 	}
430f194f198SAnoob Joseph 
4316045c06aSArchana Muniganti 	fill_cpt_inst(instance, req, cpt_inst_w7);
432f194f198SAnoob Joseph 
433f194f198SAnoob Joseph 	CPT_LOG_DP_DEBUG("req: %p op: %p ", req, user_req->op);
434f194f198SAnoob Joseph 
435f194f198SAnoob Joseph 	/* Fill time_out cycles */
436f194f198SAnoob Joseph 	user_req->time_out = rte_get_timer_cycles() +
437f194f198SAnoob Joseph 			DEFAULT_COMMAND_TIMEOUT * rte_get_timer_hz();
438f194f198SAnoob Joseph 	user_req->extra_time = 0;
439f194f198SAnoob Joseph 
440f194f198SAnoob Joseph 	/* Default mode of software queue */
441f194f198SAnoob Joseph 	mark_cpt_inst(instance);
442f194f198SAnoob Joseph 
443f194f198SAnoob Joseph 	CPT_LOG_DP_DEBUG("Submitted NB cmd with request: %p "
444f194f198SAnoob Joseph 			 "op: %p", user_req, user_req->op);
445*44a2cebbSShijith Thotton 	return req;
446f194f198SAnoob Joseph }
447f194f198SAnoob Joseph 
448*44a2cebbSShijith Thotton static __rte_always_inline void * __rte_hot
449e9a356e2SSunila Sahu otx_cpt_enq_single_asym(struct cpt_instance *instance,
450e9a356e2SSunila Sahu 			struct rte_crypto_op *op,
451e9a356e2SSunila Sahu 			struct pending_queue *pqueue)
452e9a356e2SSunila Sahu {
453e9a356e2SSunila Sahu 	struct cpt_qp_meta_info *minfo = &instance->meta_info;
454e9a356e2SSunila Sahu 	struct rte_crypto_asym_op *asym_op = op->asym;
455e9a356e2SSunila Sahu 	struct asym_op_params params = {0};
456e9a356e2SSunila Sahu 	struct cpt_asym_sess_misc *sess;
457e9a356e2SSunila Sahu 	uintptr_t *cop;
458e9a356e2SSunila Sahu 	void *mdata;
459*44a2cebbSShijith Thotton 	void *req;
460e9a356e2SSunila Sahu 	int ret;
461e9a356e2SSunila Sahu 
462e9a356e2SSunila Sahu 	if (unlikely(rte_mempool_get(minfo->pool, &mdata) < 0)) {
463e9a356e2SSunila Sahu 		CPT_LOG_DP_ERR("Could not allocate meta buffer for request");
464*44a2cebbSShijith Thotton 		rte_errno = ENOMEM;
465*44a2cebbSShijith Thotton 		return NULL;
466e9a356e2SSunila Sahu 	}
467e9a356e2SSunila Sahu 
468e9a356e2SSunila Sahu 	sess = get_asym_session_private_data(asym_op->session,
469e9a356e2SSunila Sahu 					     otx_cryptodev_driver_id);
470e9a356e2SSunila Sahu 
471e9a356e2SSunila Sahu 	/* Store phys_addr of the mdata to meta_buf */
472e9a356e2SSunila Sahu 	params.meta_buf = rte_mempool_virt2iova(mdata);
473e9a356e2SSunila Sahu 
474e9a356e2SSunila Sahu 	cop = mdata;
475e9a356e2SSunila Sahu 	cop[0] = (uintptr_t)mdata;
476e9a356e2SSunila Sahu 	cop[1] = (uintptr_t)op;
477e9a356e2SSunila Sahu 	cop[2] = cop[3] = 0ULL;
478e9a356e2SSunila Sahu 
479e9a356e2SSunila Sahu 	params.req = RTE_PTR_ADD(cop, 4 * sizeof(uintptr_t));
480e9a356e2SSunila Sahu 	params.req->op = cop;
481e9a356e2SSunila Sahu 
482e9a356e2SSunila Sahu 	/* Adjust meta_buf by crypto_op data  and request_info struct */
483e9a356e2SSunila Sahu 	params.meta_buf += (4 * sizeof(uintptr_t)) +
484e9a356e2SSunila Sahu 			   sizeof(struct cpt_request_info);
485e9a356e2SSunila Sahu 
486e9a356e2SSunila Sahu 	switch (sess->xfrm_type) {
487e9a356e2SSunila Sahu 	case RTE_CRYPTO_ASYM_XFORM_MODEX:
488e9a356e2SSunila Sahu 		ret = cpt_modex_prep(&params, &sess->mod_ctx);
489e9a356e2SSunila Sahu 		if (unlikely(ret))
490e9a356e2SSunila Sahu 			goto req_fail;
491e9a356e2SSunila Sahu 		break;
492e9a356e2SSunila Sahu 	case RTE_CRYPTO_ASYM_XFORM_RSA:
493e9a356e2SSunila Sahu 		ret = cpt_enqueue_rsa_op(op, &params, sess);
494e9a356e2SSunila Sahu 		if (unlikely(ret))
495e9a356e2SSunila Sahu 			goto req_fail;
496e9a356e2SSunila Sahu 		break;
497aa2cbd32SSunila Sahu 	case RTE_CRYPTO_ASYM_XFORM_ECDSA:
498aa2cbd32SSunila Sahu 		ret = cpt_enqueue_ecdsa_op(op, &params, sess, otx_fpm_iova);
499aa2cbd32SSunila Sahu 		if (unlikely(ret))
500aa2cbd32SSunila Sahu 			goto req_fail;
501aa2cbd32SSunila Sahu 		break;
50299faef83SSunila Sahu 	case RTE_CRYPTO_ASYM_XFORM_ECPM:
50399faef83SSunila Sahu 		ret = cpt_ecpm_prep(&asym_op->ecpm, &params,
50499faef83SSunila Sahu 				    sess->ec_ctx.curveid);
50599faef83SSunila Sahu 		if (unlikely(ret))
50699faef83SSunila Sahu 			goto req_fail;
50799faef83SSunila Sahu 		break;
50899faef83SSunila Sahu 
509e9a356e2SSunila Sahu 	default:
510e9a356e2SSunila Sahu 		op->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
511*44a2cebbSShijith Thotton 		rte_errno = EINVAL;
512e9a356e2SSunila Sahu 		goto req_fail;
513e9a356e2SSunila Sahu 	}
514e9a356e2SSunila Sahu 
515*44a2cebbSShijith Thotton 	req = otx_cpt_request_enqueue(instance, pqueue, params.req,
5166045c06aSArchana Muniganti 				      sess->cpt_inst_w7);
517*44a2cebbSShijith Thotton 	if (unlikely(req == NULL)) {
518e9a356e2SSunila Sahu 		CPT_LOG_DP_ERR("Could not enqueue crypto req");
519e9a356e2SSunila Sahu 		goto req_fail;
520e9a356e2SSunila Sahu 	}
521e9a356e2SSunila Sahu 
522*44a2cebbSShijith Thotton 	return req;
523e9a356e2SSunila Sahu 
524e9a356e2SSunila Sahu req_fail:
525e9a356e2SSunila Sahu 	free_op_meta(mdata, minfo->pool);
526e9a356e2SSunila Sahu 
527*44a2cebbSShijith Thotton 	return NULL;
528e9a356e2SSunila Sahu }
529e9a356e2SSunila Sahu 
530*44a2cebbSShijith Thotton static __rte_always_inline void * __rte_hot
531f194f198SAnoob Joseph otx_cpt_enq_single_sym(struct cpt_instance *instance,
532f194f198SAnoob Joseph 		       struct rte_crypto_op *op,
533f194f198SAnoob Joseph 		       struct pending_queue *pqueue)
534f194f198SAnoob Joseph {
535f194f198SAnoob Joseph 	struct cpt_sess_misc *sess;
536f194f198SAnoob Joseph 	struct rte_crypto_sym_op *sym_op = op->sym;
5376045c06aSArchana Muniganti 	struct cpt_request_info *prep_req;
5386045c06aSArchana Muniganti 	void *mdata = NULL;
539f194f198SAnoob Joseph 	int ret = 0;
540*44a2cebbSShijith Thotton 	void *req;
541f194f198SAnoob Joseph 	uint64_t cpt_op;
542f194f198SAnoob Joseph 
543f194f198SAnoob Joseph 	sess = (struct cpt_sess_misc *)
544f194f198SAnoob Joseph 			get_sym_session_private_data(sym_op->session,
545f194f198SAnoob Joseph 						     otx_cryptodev_driver_id);
546f194f198SAnoob Joseph 
547f194f198SAnoob Joseph 	cpt_op = sess->cpt_op;
548f194f198SAnoob Joseph 
549f194f198SAnoob Joseph 	if (likely(cpt_op & CPT_OP_CIPHER_MASK))
550ec54bc9dSAnoob Joseph 		ret = fill_fc_params(op, sess, &instance->meta_info, &mdata,
5516045c06aSArchana Muniganti 				     (void **)&prep_req);
552f194f198SAnoob Joseph 	else
553ec54bc9dSAnoob Joseph 		ret = fill_digest_params(op, sess, &instance->meta_info,
5546045c06aSArchana Muniganti 					 &mdata, (void **)&prep_req);
555f194f198SAnoob Joseph 
556f194f198SAnoob Joseph 	if (unlikely(ret)) {
557f194f198SAnoob Joseph 		CPT_LOG_DP_ERR("prep cryto req : op %p, cpt_op 0x%x "
558f194f198SAnoob Joseph 			       "ret 0x%x", op, (unsigned int)cpt_op, ret);
559*44a2cebbSShijith Thotton 		return NULL;
560f194f198SAnoob Joseph 	}
561f194f198SAnoob Joseph 
562f194f198SAnoob Joseph 	/* Enqueue prepared instruction to h/w */
563*44a2cebbSShijith Thotton 	req = otx_cpt_request_enqueue(instance, pqueue, prep_req,
5646045c06aSArchana Muniganti 				      sess->cpt_inst_w7);
565*44a2cebbSShijith Thotton 	if (unlikely(req == NULL))
566f194f198SAnoob Joseph 		/* Buffer allocated for request preparation need to be freed */
567ec54bc9dSAnoob Joseph 		free_op_meta(mdata, instance->meta_info.pool);
568*44a2cebbSShijith Thotton 
569*44a2cebbSShijith Thotton 	return req;
570f194f198SAnoob Joseph }
571f194f198SAnoob Joseph 
572*44a2cebbSShijith Thotton static __rte_always_inline void * __rte_hot
573f194f198SAnoob Joseph otx_cpt_enq_single_sym_sessless(struct cpt_instance *instance,
574f194f198SAnoob Joseph 				struct rte_crypto_op *op,
575caeba506SAnoob Joseph 				struct pending_queue *pend_q)
576f194f198SAnoob Joseph {
577caeba506SAnoob Joseph 	const int driver_id = otx_cryptodev_driver_id;
578f194f198SAnoob Joseph 	struct rte_crypto_sym_op *sym_op = op->sym;
579caeba506SAnoob Joseph 	struct rte_cryptodev_sym_session *sess;
580*44a2cebbSShijith Thotton 	void *req;
581f194f198SAnoob Joseph 	int ret;
582f194f198SAnoob Joseph 
583caeba506SAnoob Joseph 	/* Create temporary session */
5848e177f14SAnkur Dwivedi 	sess = rte_cryptodev_sym_session_create(instance->sess_mp);
585*44a2cebbSShijith Thotton 	if (sess == NULL) {
586*44a2cebbSShijith Thotton 		rte_errno = ENOMEM;
587*44a2cebbSShijith Thotton 		return NULL;
588*44a2cebbSShijith Thotton 	}
589f194f198SAnoob Joseph 
590caeba506SAnoob Joseph 	ret = sym_session_configure(driver_id, sym_op->xform, sess,
591caeba506SAnoob Joseph 				    instance->sess_mp_priv);
592caeba506SAnoob Joseph 	if (ret)
593caeba506SAnoob Joseph 		goto sess_put;
594f194f198SAnoob Joseph 
595caeba506SAnoob Joseph 	sym_op->session = sess;
596f194f198SAnoob Joseph 
597*44a2cebbSShijith Thotton 	req = otx_cpt_enq_single_sym(instance, op, pend_q);
598f194f198SAnoob Joseph 
599*44a2cebbSShijith Thotton 	if (unlikely(req == NULL))
600caeba506SAnoob Joseph 		goto priv_put;
601f194f198SAnoob Joseph 
602*44a2cebbSShijith Thotton 	return req;
603f194f198SAnoob Joseph 
604caeba506SAnoob Joseph priv_put:
605caeba506SAnoob Joseph 	sym_session_clear(driver_id, sess);
606caeba506SAnoob Joseph sess_put:
607caeba506SAnoob Joseph 	rte_mempool_put(instance->sess_mp, sess);
608*44a2cebbSShijith Thotton 	return NULL;
609f194f198SAnoob Joseph }
610f194f198SAnoob Joseph 
611e9a356e2SSunila Sahu #define OP_TYPE_SYM		0
612e9a356e2SSunila Sahu #define OP_TYPE_ASYM		1
613e9a356e2SSunila Sahu 
614*44a2cebbSShijith Thotton static __rte_always_inline void *__rte_hot
615f194f198SAnoob Joseph otx_cpt_enq_single(struct cpt_instance *inst,
616f194f198SAnoob Joseph 		   struct rte_crypto_op *op,
617e9a356e2SSunila Sahu 		   struct pending_queue *pqueue,
618e9a356e2SSunila Sahu 		   const uint8_t op_type)
619f194f198SAnoob Joseph {
620f194f198SAnoob Joseph 	/* Check for the type */
621f194f198SAnoob Joseph 
622e9a356e2SSunila Sahu 	if (op_type == OP_TYPE_SYM) {
623f194f198SAnoob Joseph 		if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION)
624f194f198SAnoob Joseph 			return otx_cpt_enq_single_sym(inst, op, pqueue);
625e9a356e2SSunila Sahu 		else
626e9a356e2SSunila Sahu 			return otx_cpt_enq_single_sym_sessless(inst, op,
627e9a356e2SSunila Sahu 							       pqueue);
628f194f198SAnoob Joseph 	}
629f194f198SAnoob Joseph 
630e9a356e2SSunila Sahu 	if (op_type == OP_TYPE_ASYM) {
631e9a356e2SSunila Sahu 		if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION)
632e9a356e2SSunila Sahu 			return otx_cpt_enq_single_asym(inst, op, pqueue);
633e9a356e2SSunila Sahu 	}
634e9a356e2SSunila Sahu 
635e9a356e2SSunila Sahu 	/* Should not reach here */
636*44a2cebbSShijith Thotton 	rte_errno = ENOTSUP;
637*44a2cebbSShijith Thotton 	return NULL;
638e9a356e2SSunila Sahu }
639e9a356e2SSunila Sahu 
640e3866e73SThomas Monjalon static  __rte_always_inline uint16_t __rte_hot
641e9a356e2SSunila Sahu otx_cpt_pkt_enqueue(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops,
642e9a356e2SSunila Sahu 		    const uint8_t op_type)
643ac4d88afSTejasree Kondoj {
644ac4d88afSTejasree Kondoj 	struct cpt_instance *instance = (struct cpt_instance *)qptr;
645f194f198SAnoob Joseph 	uint16_t count;
646*44a2cebbSShijith Thotton 	void *req;
647ac4d88afSTejasree Kondoj 	struct cpt_vf *cptvf = (struct cpt_vf *)instance;
648ac4d88afSTejasree Kondoj 	struct pending_queue *pqueue = &cptvf->pqueue;
649ac4d88afSTejasree Kondoj 
650ac4d88afSTejasree Kondoj 	count = DEFAULT_CMD_QLEN - pqueue->pending_count;
651ac4d88afSTejasree Kondoj 	if (nb_ops > count)
652ac4d88afSTejasree Kondoj 		nb_ops = count;
653ac4d88afSTejasree Kondoj 
654ac4d88afSTejasree Kondoj 	count = 0;
655ac4d88afSTejasree Kondoj 	while (likely(count < nb_ops)) {
656f194f198SAnoob Joseph 
657f194f198SAnoob Joseph 		/* Enqueue single op */
658*44a2cebbSShijith Thotton 		req = otx_cpt_enq_single(instance, ops[count], pqueue, op_type);
659f194f198SAnoob Joseph 
660*44a2cebbSShijith Thotton 		if (unlikely(req == NULL))
661ac4d88afSTejasree Kondoj 			break;
662*44a2cebbSShijith Thotton 
663*44a2cebbSShijith Thotton 		pqueue->req_queue[pqueue->enq_tail] = (uintptr_t)req;
664*44a2cebbSShijith Thotton 		MOD_INC(pqueue->enq_tail, DEFAULT_CMD_QLEN);
665*44a2cebbSShijith Thotton 		pqueue->pending_count += 1;
666ac4d88afSTejasree Kondoj 		count++;
667ac4d88afSTejasree Kondoj 	}
668ac4d88afSTejasree Kondoj 	otx_cpt_ring_dbell(instance, count);
669ac4d88afSTejasree Kondoj 	return count;
670ac4d88afSTejasree Kondoj }
671ac4d88afSTejasree Kondoj 
672e9a356e2SSunila Sahu static uint16_t
673e9a356e2SSunila Sahu otx_cpt_enqueue_asym(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops)
674e9a356e2SSunila Sahu {
675e9a356e2SSunila Sahu 	return otx_cpt_pkt_enqueue(qptr, ops, nb_ops, OP_TYPE_ASYM);
676e9a356e2SSunila Sahu }
677e9a356e2SSunila Sahu 
678e9a356e2SSunila Sahu static uint16_t
679e9a356e2SSunila Sahu otx_cpt_enqueue_sym(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops)
680e9a356e2SSunila Sahu {
681e9a356e2SSunila Sahu 	return otx_cpt_pkt_enqueue(qptr, ops, nb_ops, OP_TYPE_SYM);
682e9a356e2SSunila Sahu }
683e9a356e2SSunila Sahu 
684*44a2cebbSShijith Thotton static __rte_always_inline void
685*44a2cebbSShijith Thotton submit_request_to_sso(struct ssows *ws, uintptr_t req,
686*44a2cebbSShijith Thotton 		      struct rte_event *rsp_info)
687*44a2cebbSShijith Thotton {
688*44a2cebbSShijith Thotton 	uint64_t add_work;
689*44a2cebbSShijith Thotton 
690*44a2cebbSShijith Thotton 	add_work = rsp_info->flow_id | (RTE_EVENT_TYPE_CRYPTODEV << 28) |
691*44a2cebbSShijith Thotton 		   ((uint64_t)(rsp_info->sched_type) << 32);
692*44a2cebbSShijith Thotton 
693*44a2cebbSShijith Thotton 	if (!rsp_info->sched_type)
694*44a2cebbSShijith Thotton 		ssows_head_wait(ws);
695*44a2cebbSShijith Thotton 
696*44a2cebbSShijith Thotton 	rte_atomic_thread_fence(__ATOMIC_RELEASE);
697*44a2cebbSShijith Thotton 	ssovf_store_pair(add_work, req, ws->grps[rsp_info->queue_id]);
698*44a2cebbSShijith Thotton }
699*44a2cebbSShijith Thotton 
700*44a2cebbSShijith Thotton static inline union rte_event_crypto_metadata *
701*44a2cebbSShijith Thotton get_event_crypto_mdata(struct rte_crypto_op *op)
702*44a2cebbSShijith Thotton {
703*44a2cebbSShijith Thotton 	union rte_event_crypto_metadata *ec_mdata;
704*44a2cebbSShijith Thotton 
705*44a2cebbSShijith Thotton 	if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION)
706*44a2cebbSShijith Thotton 		ec_mdata = rte_cryptodev_sym_session_get_user_data(
707*44a2cebbSShijith Thotton 							   op->sym->session);
708*44a2cebbSShijith Thotton 	else if (op->sess_type == RTE_CRYPTO_OP_SESSIONLESS &&
709*44a2cebbSShijith Thotton 		 op->private_data_offset)
710*44a2cebbSShijith Thotton 		ec_mdata = (union rte_event_crypto_metadata *)
711*44a2cebbSShijith Thotton 			((uint8_t *)op + op->private_data_offset);
712*44a2cebbSShijith Thotton 	else
713*44a2cebbSShijith Thotton 		return NULL;
714*44a2cebbSShijith Thotton 
715*44a2cebbSShijith Thotton 	return ec_mdata;
716*44a2cebbSShijith Thotton }
717*44a2cebbSShijith Thotton 
718*44a2cebbSShijith Thotton uint16_t __rte_hot
719*44a2cebbSShijith Thotton otx_crypto_adapter_enqueue(void *port, struct rte_crypto_op *op)
720*44a2cebbSShijith Thotton {
721*44a2cebbSShijith Thotton 	union rte_event_crypto_metadata *ec_mdata;
722*44a2cebbSShijith Thotton 	struct cpt_instance *instance;
723*44a2cebbSShijith Thotton 	struct cpt_request_info *req;
724*44a2cebbSShijith Thotton 	struct rte_event *rsp_info;
725*44a2cebbSShijith Thotton 	uint8_t op_type, cdev_id;
726*44a2cebbSShijith Thotton 	uint16_t qp_id;
727*44a2cebbSShijith Thotton 
728*44a2cebbSShijith Thotton 	ec_mdata = get_event_crypto_mdata(op);
729*44a2cebbSShijith Thotton 	if (unlikely(ec_mdata == NULL)) {
730*44a2cebbSShijith Thotton 		rte_errno = EINVAL;
731*44a2cebbSShijith Thotton 		return 0;
732*44a2cebbSShijith Thotton 	}
733*44a2cebbSShijith Thotton 
734*44a2cebbSShijith Thotton 	cdev_id = ec_mdata->request_info.cdev_id;
735*44a2cebbSShijith Thotton 	qp_id = ec_mdata->request_info.queue_pair_id;
736*44a2cebbSShijith Thotton 	rsp_info = &ec_mdata->response_info;
737*44a2cebbSShijith Thotton 	instance = rte_cryptodevs[cdev_id].data->queue_pairs[qp_id];
738*44a2cebbSShijith Thotton 
739*44a2cebbSShijith Thotton 	if (unlikely(!instance->ca_enabled)) {
740*44a2cebbSShijith Thotton 		rte_errno = EINVAL;
741*44a2cebbSShijith Thotton 		return 0;
742*44a2cebbSShijith Thotton 	}
743*44a2cebbSShijith Thotton 
744*44a2cebbSShijith Thotton 	op_type = op->type == RTE_CRYPTO_OP_TYPE_SYMMETRIC ? OP_TYPE_SYM :
745*44a2cebbSShijith Thotton 							     OP_TYPE_ASYM;
746*44a2cebbSShijith Thotton 	req = otx_cpt_enq_single(instance, op,
747*44a2cebbSShijith Thotton 				 &((struct cpt_vf *)instance)->pqueue, op_type);
748*44a2cebbSShijith Thotton 	if (unlikely(req == NULL))
749*44a2cebbSShijith Thotton 		return 0;
750*44a2cebbSShijith Thotton 
751*44a2cebbSShijith Thotton 	otx_cpt_ring_dbell(instance, 1);
752*44a2cebbSShijith Thotton 	req->qp = instance;
753*44a2cebbSShijith Thotton 	submit_request_to_sso(port, (uintptr_t)req, rsp_info);
754*44a2cebbSShijith Thotton 
755*44a2cebbSShijith Thotton 	return 1;
756*44a2cebbSShijith Thotton }
757*44a2cebbSShijith Thotton 
758e9a356e2SSunila Sahu static inline void
759e9a356e2SSunila Sahu otx_cpt_asym_rsa_op(struct rte_crypto_op *cop, struct cpt_request_info *req,
760e9a356e2SSunila Sahu 		    struct rte_crypto_rsa_xform *rsa_ctx)
761e9a356e2SSunila Sahu 
762e9a356e2SSunila Sahu {
763e9a356e2SSunila Sahu 	struct rte_crypto_rsa_op_param *rsa = &cop->asym->rsa;
764e9a356e2SSunila Sahu 
765e9a356e2SSunila Sahu 	switch (rsa->op_type) {
766e9a356e2SSunila Sahu 	case RTE_CRYPTO_ASYM_OP_ENCRYPT:
767e9a356e2SSunila Sahu 		rsa->cipher.length = rsa_ctx->n.length;
768e9a356e2SSunila Sahu 		memcpy(rsa->cipher.data, req->rptr, rsa->cipher.length);
769e9a356e2SSunila Sahu 		break;
770e9a356e2SSunila Sahu 	case RTE_CRYPTO_ASYM_OP_DECRYPT:
771e9a356e2SSunila Sahu 		if (rsa->pad == RTE_CRYPTO_RSA_PADDING_NONE)
772e9a356e2SSunila Sahu 			rsa->message.length = rsa_ctx->n.length;
773e9a356e2SSunila Sahu 		else {
774e9a356e2SSunila Sahu 			/* Get length of decrypted output */
775e9a356e2SSunila Sahu 			rsa->message.length = rte_cpu_to_be_16
776e9a356e2SSunila Sahu 					(*((uint16_t *)req->rptr));
777e9a356e2SSunila Sahu 
778e9a356e2SSunila Sahu 			/* Offset data pointer by length fields */
779e9a356e2SSunila Sahu 			req->rptr += 2;
780e9a356e2SSunila Sahu 		}
781e9a356e2SSunila Sahu 		memcpy(rsa->message.data, req->rptr, rsa->message.length);
782e9a356e2SSunila Sahu 		break;
783e9a356e2SSunila Sahu 	case RTE_CRYPTO_ASYM_OP_SIGN:
784e9a356e2SSunila Sahu 		rsa->sign.length = rsa_ctx->n.length;
785e9a356e2SSunila Sahu 		memcpy(rsa->sign.data, req->rptr, rsa->sign.length);
786e9a356e2SSunila Sahu 		break;
787e9a356e2SSunila Sahu 	case RTE_CRYPTO_ASYM_OP_VERIFY:
788e9a356e2SSunila Sahu 		if (rsa->pad == RTE_CRYPTO_RSA_PADDING_NONE)
789e9a356e2SSunila Sahu 			rsa->sign.length = rsa_ctx->n.length;
790e9a356e2SSunila Sahu 		else {
791e9a356e2SSunila Sahu 			/* Get length of decrypted output */
792e9a356e2SSunila Sahu 			rsa->sign.length = rte_cpu_to_be_16
793e9a356e2SSunila Sahu 					(*((uint16_t *)req->rptr));
794e9a356e2SSunila Sahu 
795e9a356e2SSunila Sahu 			/* Offset data pointer by length fields */
796e9a356e2SSunila Sahu 			req->rptr += 2;
797e9a356e2SSunila Sahu 		}
798e9a356e2SSunila Sahu 		memcpy(rsa->sign.data, req->rptr, rsa->sign.length);
799e9a356e2SSunila Sahu 
800e9a356e2SSunila Sahu 		if (memcmp(rsa->sign.data, rsa->message.data,
801e9a356e2SSunila Sahu 			   rsa->message.length)) {
802e9a356e2SSunila Sahu 			CPT_LOG_DP_ERR("RSA verification failed");
803e9a356e2SSunila Sahu 			cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
804e9a356e2SSunila Sahu 		}
805e9a356e2SSunila Sahu 		break;
806e9a356e2SSunila Sahu 	default:
807e9a356e2SSunila Sahu 		CPT_LOG_DP_DEBUG("Invalid RSA operation type");
808e9a356e2SSunila Sahu 		cop->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
809e9a356e2SSunila Sahu 		break;
810e9a356e2SSunila Sahu 	}
811e9a356e2SSunila Sahu }
812e9a356e2SSunila Sahu 
813aa2cbd32SSunila Sahu static __rte_always_inline void
814aa2cbd32SSunila Sahu otx_cpt_asym_dequeue_ecdsa_op(struct rte_crypto_ecdsa_op_param *ecdsa,
815aa2cbd32SSunila Sahu 			    struct cpt_request_info *req,
816aa2cbd32SSunila Sahu 			    struct cpt_asym_ec_ctx *ec)
817aa2cbd32SSunila Sahu 
818aa2cbd32SSunila Sahu {
819aa2cbd32SSunila Sahu 	int prime_len = ec_grp[ec->curveid].prime.length;
820aa2cbd32SSunila Sahu 
821aa2cbd32SSunila Sahu 	if (ecdsa->op_type == RTE_CRYPTO_ASYM_OP_VERIFY)
822aa2cbd32SSunila Sahu 		return;
823aa2cbd32SSunila Sahu 
824aa2cbd32SSunila Sahu 	/* Separate out sign r and s components */
825aa2cbd32SSunila Sahu 	memcpy(ecdsa->r.data, req->rptr, prime_len);
826ecd070acSArchana Muniganti 	memcpy(ecdsa->s.data, req->rptr + RTE_ALIGN_CEIL(prime_len, 8),
827ecd070acSArchana Muniganti 	       prime_len);
828aa2cbd32SSunila Sahu 	ecdsa->r.length = prime_len;
829aa2cbd32SSunila Sahu 	ecdsa->s.length = prime_len;
830aa2cbd32SSunila Sahu }
831aa2cbd32SSunila Sahu 
83299faef83SSunila Sahu static __rte_always_inline void
83399faef83SSunila Sahu otx_cpt_asym_dequeue_ecpm_op(struct rte_crypto_ecpm_op_param *ecpm,
83499faef83SSunila Sahu 			     struct cpt_request_info *req,
83599faef83SSunila Sahu 			     struct cpt_asym_ec_ctx *ec)
83699faef83SSunila Sahu {
83799faef83SSunila Sahu 	int prime_len = ec_grp[ec->curveid].prime.length;
83899faef83SSunila Sahu 
83999faef83SSunila Sahu 	memcpy(ecpm->r.x.data, req->rptr, prime_len);
840ecd070acSArchana Muniganti 	memcpy(ecpm->r.y.data, req->rptr + RTE_ALIGN_CEIL(prime_len, 8),
841ecd070acSArchana Muniganti 	       prime_len);
84299faef83SSunila Sahu 	ecpm->r.x.length = prime_len;
84399faef83SSunila Sahu 	ecpm->r.y.length = prime_len;
84499faef83SSunila Sahu }
84599faef83SSunila Sahu 
846e3866e73SThomas Monjalon static __rte_always_inline void __rte_hot
847e9a356e2SSunila Sahu otx_cpt_asym_post_process(struct rte_crypto_op *cop,
848e9a356e2SSunila Sahu 			  struct cpt_request_info *req)
849e9a356e2SSunila Sahu {
850e9a356e2SSunila Sahu 	struct rte_crypto_asym_op *op = cop->asym;
851e9a356e2SSunila Sahu 	struct cpt_asym_sess_misc *sess;
852e9a356e2SSunila Sahu 
853e9a356e2SSunila Sahu 	sess = get_asym_session_private_data(op->session,
854e9a356e2SSunila Sahu 					     otx_cryptodev_driver_id);
855e9a356e2SSunila Sahu 
856e9a356e2SSunila Sahu 	switch (sess->xfrm_type) {
857e9a356e2SSunila Sahu 	case RTE_CRYPTO_ASYM_XFORM_RSA:
858e9a356e2SSunila Sahu 		otx_cpt_asym_rsa_op(cop, req, &sess->rsa_ctx);
859e9a356e2SSunila Sahu 		break;
860e9a356e2SSunila Sahu 	case RTE_CRYPTO_ASYM_XFORM_MODEX:
861e9a356e2SSunila Sahu 		op->modex.result.length = sess->mod_ctx.modulus.length;
862e9a356e2SSunila Sahu 		memcpy(op->modex.result.data, req->rptr,
863e9a356e2SSunila Sahu 		       op->modex.result.length);
864e9a356e2SSunila Sahu 		break;
865aa2cbd32SSunila Sahu 	case RTE_CRYPTO_ASYM_XFORM_ECDSA:
866aa2cbd32SSunila Sahu 		otx_cpt_asym_dequeue_ecdsa_op(&op->ecdsa, req, &sess->ec_ctx);
867aa2cbd32SSunila Sahu 		break;
86899faef83SSunila Sahu 	case RTE_CRYPTO_ASYM_XFORM_ECPM:
86999faef83SSunila Sahu 		otx_cpt_asym_dequeue_ecpm_op(&op->ecpm, req, &sess->ec_ctx);
87099faef83SSunila Sahu 		break;
871e9a356e2SSunila Sahu 	default:
872e9a356e2SSunila Sahu 		CPT_LOG_DP_DEBUG("Invalid crypto xform type");
873e9a356e2SSunila Sahu 		cop->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
874e9a356e2SSunila Sahu 		break;
875e9a356e2SSunila Sahu 	}
876e9a356e2SSunila Sahu }
877e9a356e2SSunila Sahu 
878e3866e73SThomas Monjalon static __rte_always_inline void __rte_hot
879e9a356e2SSunila Sahu otx_cpt_dequeue_post_process(struct rte_crypto_op *cop, uintptr_t *rsp,
880e9a356e2SSunila Sahu 			     const uint8_t op_type)
881f194f198SAnoob Joseph {
882f194f198SAnoob Joseph 	/* H/w has returned success */
883f194f198SAnoob Joseph 	cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
884f194f198SAnoob Joseph 
885f194f198SAnoob Joseph 	/* Perform further post processing */
886f194f198SAnoob Joseph 
887e9a356e2SSunila Sahu 	if ((op_type == OP_TYPE_SYM) &&
888e9a356e2SSunila Sahu 	    (cop->type == RTE_CRYPTO_OP_TYPE_SYMMETRIC)) {
889f194f198SAnoob Joseph 		/* Check if auth verify need to be completed */
890f194f198SAnoob Joseph 		if (unlikely(rsp[2]))
891f194f198SAnoob Joseph 			compl_auth_verify(cop, (uint8_t *)rsp[2], rsp[3]);
892f194f198SAnoob Joseph 		return;
893f194f198SAnoob Joseph 	}
894e9a356e2SSunila Sahu 
895e9a356e2SSunila Sahu 	if ((op_type == OP_TYPE_ASYM) &&
896e9a356e2SSunila Sahu 	    (cop->type == RTE_CRYPTO_OP_TYPE_ASYMMETRIC)) {
897e9a356e2SSunila Sahu 		rsp = RTE_PTR_ADD(rsp, 4 * sizeof(uintptr_t));
898e9a356e2SSunila Sahu 		otx_cpt_asym_post_process(cop, (struct cpt_request_info *)rsp);
899f194f198SAnoob Joseph 	}
900f194f198SAnoob Joseph 
901e9a356e2SSunila Sahu 	return;
902e9a356e2SSunila Sahu }
903e9a356e2SSunila Sahu 
904*44a2cebbSShijith Thotton static inline void
905*44a2cebbSShijith Thotton free_sym_session_data(const struct cpt_instance *instance,
906*44a2cebbSShijith Thotton 		      struct rte_crypto_op *cop)
907*44a2cebbSShijith Thotton {
908*44a2cebbSShijith Thotton 	void *sess_private_data_t = get_sym_session_private_data(
909*44a2cebbSShijith Thotton 		cop->sym->session, otx_cryptodev_driver_id);
910*44a2cebbSShijith Thotton 	memset(sess_private_data_t, 0, cpt_get_session_size());
911*44a2cebbSShijith Thotton 	memset(cop->sym->session, 0,
912*44a2cebbSShijith Thotton 	       rte_cryptodev_sym_get_existing_header_session_size(
913*44a2cebbSShijith Thotton 		       cop->sym->session));
914*44a2cebbSShijith Thotton 	rte_mempool_put(instance->sess_mp_priv, sess_private_data_t);
915*44a2cebbSShijith Thotton 	rte_mempool_put(instance->sess_mp, cop->sym->session);
916*44a2cebbSShijith Thotton 	cop->sym->session = NULL;
917*44a2cebbSShijith Thotton }
918*44a2cebbSShijith Thotton 
919*44a2cebbSShijith Thotton static __rte_always_inline struct rte_crypto_op *
920*44a2cebbSShijith Thotton otx_cpt_process_response(const struct cpt_instance *instance, uintptr_t *rsp,
921*44a2cebbSShijith Thotton 			 uint8_t cc, const uint8_t op_type)
922*44a2cebbSShijith Thotton {
923*44a2cebbSShijith Thotton 	struct rte_crypto_op *cop;
924*44a2cebbSShijith Thotton 	void *metabuf;
925*44a2cebbSShijith Thotton 
926*44a2cebbSShijith Thotton 	metabuf = (void *)rsp[0];
927*44a2cebbSShijith Thotton 	cop = (void *)rsp[1];
928*44a2cebbSShijith Thotton 
929*44a2cebbSShijith Thotton 	/* Check completion code */
930*44a2cebbSShijith Thotton 	if (likely(cc == 0)) {
931*44a2cebbSShijith Thotton 		/* H/w success pkt. Post process */
932*44a2cebbSShijith Thotton 		otx_cpt_dequeue_post_process(cop, rsp, op_type);
933*44a2cebbSShijith Thotton 	} else if (cc == ERR_GC_ICV_MISCOMPARE) {
934*44a2cebbSShijith Thotton 		/* auth data mismatch */
935*44a2cebbSShijith Thotton 		cop->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
936*44a2cebbSShijith Thotton 	} else {
937*44a2cebbSShijith Thotton 		/* Error */
938*44a2cebbSShijith Thotton 		cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
939*44a2cebbSShijith Thotton 	}
940*44a2cebbSShijith Thotton 
941*44a2cebbSShijith Thotton 	if (unlikely(cop->sess_type == RTE_CRYPTO_OP_SESSIONLESS))
942*44a2cebbSShijith Thotton 		free_sym_session_data(instance, cop);
943*44a2cebbSShijith Thotton 	free_op_meta(metabuf, instance->meta_info.pool);
944*44a2cebbSShijith Thotton 
945*44a2cebbSShijith Thotton 	return cop;
946*44a2cebbSShijith Thotton }
947*44a2cebbSShijith Thotton 
948e3866e73SThomas Monjalon static __rte_always_inline uint16_t __rte_hot
949e9a356e2SSunila Sahu otx_cpt_pkt_dequeue(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops,
950e9a356e2SSunila Sahu 		    const uint8_t op_type)
95189f1a8d6STejasree Kondoj {
95289f1a8d6STejasree Kondoj 	struct cpt_instance *instance = (struct cpt_instance *)qptr;
953f194f198SAnoob Joseph 	struct cpt_request_info *user_req;
95489f1a8d6STejasree Kondoj 	struct cpt_vf *cptvf = (struct cpt_vf *)instance;
955f194f198SAnoob Joseph 	uint8_t cc[nb_ops];
956f194f198SAnoob Joseph 	int i, count, pcount;
957f194f198SAnoob Joseph 	uint8_t ret;
958f194f198SAnoob Joseph 	int nb_completed;
95989f1a8d6STejasree Kondoj 	struct pending_queue *pqueue = &cptvf->pqueue;
960f194f198SAnoob Joseph 
961f194f198SAnoob Joseph 	pcount = pqueue->pending_count;
962f194f198SAnoob Joseph 	count = (nb_ops > pcount) ? pcount : nb_ops;
963f194f198SAnoob Joseph 
964f194f198SAnoob Joseph 	for (i = 0; i < count; i++) {
96595dbafd4SArchana Muniganti 		user_req = (struct cpt_request_info *)
96695dbafd4SArchana Muniganti 				pqueue->req_queue[pqueue->deq_head];
967f194f198SAnoob Joseph 
96895dbafd4SArchana Muniganti 		if (likely((i+1) < count)) {
96995dbafd4SArchana Muniganti 			rte_prefetch_non_temporal(
97095dbafd4SArchana Muniganti 				(void *)pqueue->req_queue[i+1]);
97195dbafd4SArchana Muniganti 		}
972f194f198SAnoob Joseph 
973f194f198SAnoob Joseph 		ret = check_nb_command_id(user_req, instance);
974f194f198SAnoob Joseph 
975f194f198SAnoob Joseph 		if (unlikely(ret == ERR_REQ_PENDING)) {
976f194f198SAnoob Joseph 			/* Stop checking for completions */
977f194f198SAnoob Joseph 			break;
978f194f198SAnoob Joseph 		}
979f194f198SAnoob Joseph 
980f194f198SAnoob Joseph 		/* Return completion code and op handle */
981f194f198SAnoob Joseph 		cc[i] = ret;
982f194f198SAnoob Joseph 		ops[i] = user_req->op;
983f194f198SAnoob Joseph 
984f194f198SAnoob Joseph 		CPT_LOG_DP_DEBUG("Request %p Op %p completed with code %d",
985f194f198SAnoob Joseph 				 user_req, user_req->op, ret);
986f194f198SAnoob Joseph 
987f194f198SAnoob Joseph 		MOD_INC(pqueue->deq_head, DEFAULT_CMD_QLEN);
988f194f198SAnoob Joseph 		pqueue->pending_count -= 1;
989f194f198SAnoob Joseph 	}
990f194f198SAnoob Joseph 
991f194f198SAnoob Joseph 	nb_completed = i;
992f194f198SAnoob Joseph 
993f194f198SAnoob Joseph 	for (i = 0; i < nb_completed; i++) {
99489f1a8d6STejasree Kondoj 		if (likely((i + 1) < nb_completed))
99589f1a8d6STejasree Kondoj 			rte_prefetch0(ops[i+1]);
996f194f198SAnoob Joseph 
997*44a2cebbSShijith Thotton 		ops[i] = otx_cpt_process_response(instance, (void *)ops[i],
998*44a2cebbSShijith Thotton 						  cc[i], op_type);
999f194f198SAnoob Joseph 	}
1000f194f198SAnoob Joseph 
100189f1a8d6STejasree Kondoj 	return nb_completed;
100289f1a8d6STejasree Kondoj }
100389f1a8d6STejasree Kondoj 
1004e9a356e2SSunila Sahu static uint16_t
1005e9a356e2SSunila Sahu otx_cpt_dequeue_asym(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops)
1006e9a356e2SSunila Sahu {
1007e9a356e2SSunila Sahu 	return otx_cpt_pkt_dequeue(qptr, ops, nb_ops, OP_TYPE_ASYM);
1008e9a356e2SSunila Sahu }
1009e9a356e2SSunila Sahu 
1010e9a356e2SSunila Sahu static uint16_t
1011e9a356e2SSunila Sahu otx_cpt_dequeue_sym(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops)
1012e9a356e2SSunila Sahu {
1013e9a356e2SSunila Sahu 	return otx_cpt_pkt_dequeue(qptr, ops, nb_ops, OP_TYPE_SYM);
1014e9a356e2SSunila Sahu }
1015e9a356e2SSunila Sahu 
1016*44a2cebbSShijith Thotton uintptr_t __rte_hot
1017*44a2cebbSShijith Thotton otx_crypto_adapter_dequeue(uintptr_t get_work1)
1018*44a2cebbSShijith Thotton {
1019*44a2cebbSShijith Thotton 	const struct cpt_instance *instance;
1020*44a2cebbSShijith Thotton 	struct cpt_request_info *req;
1021*44a2cebbSShijith Thotton 	struct rte_crypto_op *cop;
1022*44a2cebbSShijith Thotton 	uint8_t cc, op_type;
1023*44a2cebbSShijith Thotton 	uintptr_t *rsp;
1024*44a2cebbSShijith Thotton 
1025*44a2cebbSShijith Thotton 	req = (struct cpt_request_info *)get_work1;
1026*44a2cebbSShijith Thotton 	instance = req->qp;
1027*44a2cebbSShijith Thotton 	rsp = req->op;
1028*44a2cebbSShijith Thotton 	cop = (void *)rsp[1];
1029*44a2cebbSShijith Thotton 	op_type = cop->type == RTE_CRYPTO_OP_TYPE_SYMMETRIC ? OP_TYPE_SYM :
1030*44a2cebbSShijith Thotton 							      OP_TYPE_ASYM;
1031*44a2cebbSShijith Thotton 
1032*44a2cebbSShijith Thotton 	do {
1033*44a2cebbSShijith Thotton 		cc = check_nb_command_id(
1034*44a2cebbSShijith Thotton 			req, (struct cpt_instance *)(uintptr_t)instance);
1035*44a2cebbSShijith Thotton 	} while (cc == ERR_REQ_PENDING);
1036*44a2cebbSShijith Thotton 
1037*44a2cebbSShijith Thotton 	cop = otx_cpt_process_response(instance, (void *)req->op, cc, op_type);
1038*44a2cebbSShijith Thotton 
1039*44a2cebbSShijith Thotton 	return (uintptr_t)(cop);
1040*44a2cebbSShijith Thotton }
1041*44a2cebbSShijith Thotton 
10420906b99fSMurthy NSSR static struct rte_cryptodev_ops cptvf_ops = {
10430906b99fSMurthy NSSR 	/* Device related operations */
10440906b99fSMurthy NSSR 	.dev_configure = otx_cpt_dev_config,
10450906b99fSMurthy NSSR 	.dev_start = otx_cpt_dev_start,
10460906b99fSMurthy NSSR 	.dev_stop = otx_cpt_dev_stop,
10470906b99fSMurthy NSSR 	.dev_close = otx_cpt_dev_close,
10480906b99fSMurthy NSSR 	.dev_infos_get = otx_cpt_dev_info_get,
10490906b99fSMurthy NSSR 
1050966b43fdSAnkur Dwivedi 	.stats_get = NULL,
1051966b43fdSAnkur Dwivedi 	.stats_reset = NULL,
10520961348fSMurthy NSSR 	.queue_pair_setup = otx_cpt_que_pair_setup,
10530961348fSMurthy NSSR 	.queue_pair_release = otx_cpt_que_pair_release,
10540906b99fSMurthy NSSR 
10550906b99fSMurthy NSSR 	/* Crypto related operations */
105643d01767SNithin Dabilpuram 	.sym_session_get_size = otx_cpt_get_session_size,
105743d01767SNithin Dabilpuram 	.sym_session_configure = otx_cpt_session_cfg,
105833bcaae5SKanaka Durga Kotamarthy 	.sym_session_clear = otx_cpt_session_clear,
105933bcaae5SKanaka Durga Kotamarthy 
106033bcaae5SKanaka Durga Kotamarthy 	.asym_session_get_size = otx_cpt_asym_session_size_get,
106133bcaae5SKanaka Durga Kotamarthy 	.asym_session_configure = otx_cpt_asym_session_cfg,
106233bcaae5SKanaka Durga Kotamarthy 	.asym_session_clear = otx_cpt_asym_session_clear,
10630906b99fSMurthy NSSR };
10640906b99fSMurthy NSSR 
1065bfe2ae49SAnoob Joseph int
1066bfe2ae49SAnoob Joseph otx_cpt_dev_create(struct rte_cryptodev *c_dev)
1067bfe2ae49SAnoob Joseph {
10680dc1cffaSAnkur Dwivedi 	struct rte_pci_device *pdev = RTE_DEV_TO_PCI(c_dev->device);
10690dc1cffaSAnkur Dwivedi 	struct cpt_vf *cptvf = NULL;
10700dc1cffaSAnkur Dwivedi 	void *reg_base;
10710dc1cffaSAnkur Dwivedi 	char dev_name[32];
10720dc1cffaSAnkur Dwivedi 	int ret;
10730dc1cffaSAnkur Dwivedi 
10740dc1cffaSAnkur Dwivedi 	if (pdev->mem_resource[0].phys_addr == 0ULL)
10750dc1cffaSAnkur Dwivedi 		return -EIO;
10760dc1cffaSAnkur Dwivedi 
10770dc1cffaSAnkur Dwivedi 	/* for secondary processes, we don't initialise any further as primary
10780dc1cffaSAnkur Dwivedi 	 * has already done this work.
10790dc1cffaSAnkur Dwivedi 	 */
10800dc1cffaSAnkur Dwivedi 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1081bfe2ae49SAnoob Joseph 		return 0;
10820dc1cffaSAnkur Dwivedi 
10830dc1cffaSAnkur Dwivedi 	cptvf = rte_zmalloc_socket("otx_cryptodev_private_mem",
10840dc1cffaSAnkur Dwivedi 			sizeof(struct cpt_vf), RTE_CACHE_LINE_SIZE,
10850dc1cffaSAnkur Dwivedi 			rte_socket_id());
10860dc1cffaSAnkur Dwivedi 
10870dc1cffaSAnkur Dwivedi 	if (cptvf == NULL) {
10880dc1cffaSAnkur Dwivedi 		CPT_LOG_ERR("Cannot allocate memory for device private data");
10890dc1cffaSAnkur Dwivedi 		return -ENOMEM;
10900dc1cffaSAnkur Dwivedi 	}
10910dc1cffaSAnkur Dwivedi 
10920dc1cffaSAnkur Dwivedi 	snprintf(dev_name, 32, "%02x:%02x.%x",
10930dc1cffaSAnkur Dwivedi 			pdev->addr.bus, pdev->addr.devid, pdev->addr.function);
10940dc1cffaSAnkur Dwivedi 
10950dc1cffaSAnkur Dwivedi 	reg_base = pdev->mem_resource[0].addr;
10960dc1cffaSAnkur Dwivedi 	if (!reg_base) {
10970dc1cffaSAnkur Dwivedi 		CPT_LOG_ERR("Failed to map BAR0 of %s", dev_name);
10980dc1cffaSAnkur Dwivedi 		ret = -ENODEV;
10990dc1cffaSAnkur Dwivedi 		goto fail;
11000dc1cffaSAnkur Dwivedi 	}
11010dc1cffaSAnkur Dwivedi 
11020dc1cffaSAnkur Dwivedi 	ret = otx_cpt_hw_init(cptvf, pdev, reg_base, dev_name);
11030dc1cffaSAnkur Dwivedi 	if (ret) {
11040dc1cffaSAnkur Dwivedi 		CPT_LOG_ERR("Failed to init cptvf %s", dev_name);
11050dc1cffaSAnkur Dwivedi 		ret = -EIO;
11060dc1cffaSAnkur Dwivedi 		goto fail;
11070dc1cffaSAnkur Dwivedi 	}
11080dc1cffaSAnkur Dwivedi 
110913d711f3SKanaka Durga Kotamarthy 	switch (cptvf->vftype) {
111013d711f3SKanaka Durga Kotamarthy 	case OTX_CPT_VF_TYPE_AE:
111113d711f3SKanaka Durga Kotamarthy 		/* Set asymmetric cpt feature flags */
111213d711f3SKanaka Durga Kotamarthy 		c_dev->feature_flags = RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO |
111333bcaae5SKanaka Durga Kotamarthy 				RTE_CRYPTODEV_FF_HW_ACCELERATED |
111433bcaae5SKanaka Durga Kotamarthy 				RTE_CRYPTODEV_FF_RSA_PRIV_OP_KEY_QT;
111513d711f3SKanaka Durga Kotamarthy 		break;
111613d711f3SKanaka Durga Kotamarthy 	case OTX_CPT_VF_TYPE_SE:
111713d711f3SKanaka Durga Kotamarthy 		/* Set symmetric cpt feature flags */
111813d711f3SKanaka Durga Kotamarthy 		c_dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
111913d711f3SKanaka Durga Kotamarthy 				RTE_CRYPTODEV_FF_HW_ACCELERATED |
112013d711f3SKanaka Durga Kotamarthy 				RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
112113d711f3SKanaka Durga Kotamarthy 				RTE_CRYPTODEV_FF_IN_PLACE_SGL |
112216c01147SDidier Pallard 				RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT |
112313d711f3SKanaka Durga Kotamarthy 				RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT |
1124b3aaf24dSPablo de Lara 				RTE_CRYPTODEV_FF_OOP_SGL_IN_SGL_OUT |
1125de5eb0a6STejasree Kondoj 				RTE_CRYPTODEV_FF_SYM_SESSIONLESS |
1126de5eb0a6STejasree Kondoj 				RTE_CRYPTODEV_FF_DIGEST_ENCRYPTED;
112713d711f3SKanaka Durga Kotamarthy 		break;
112813d711f3SKanaka Durga Kotamarthy 	default:
112913d711f3SKanaka Durga Kotamarthy 		/* Feature not supported. Abort */
113013d711f3SKanaka Durga Kotamarthy 		CPT_LOG_ERR("VF type not supported by %s", dev_name);
113113d711f3SKanaka Durga Kotamarthy 		ret = -EIO;
113213d711f3SKanaka Durga Kotamarthy 		goto deinit_dev;
113313d711f3SKanaka Durga Kotamarthy 	}
113413d711f3SKanaka Durga Kotamarthy 
11350dc1cffaSAnkur Dwivedi 	/* Start off timer for mailbox interrupts */
11360dc1cffaSAnkur Dwivedi 	otx_cpt_periodic_alarm_start(cptvf);
11370dc1cffaSAnkur Dwivedi 
11380906b99fSMurthy NSSR 	c_dev->dev_ops = &cptvf_ops;
11390dc1cffaSAnkur Dwivedi 
1140e9a356e2SSunila Sahu 	if (c_dev->feature_flags & RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO) {
1141e9a356e2SSunila Sahu 		c_dev->enqueue_burst = otx_cpt_enqueue_sym;
1142e9a356e2SSunila Sahu 		c_dev->dequeue_burst = otx_cpt_dequeue_sym;
1143e9a356e2SSunila Sahu 	} else {
1144e9a356e2SSunila Sahu 		c_dev->enqueue_burst = otx_cpt_enqueue_asym;
1145e9a356e2SSunila Sahu 		c_dev->dequeue_burst = otx_cpt_dequeue_asym;
1146e9a356e2SSunila Sahu 	}
11470dc1cffaSAnkur Dwivedi 
11480dc1cffaSAnkur Dwivedi 	/* Save dev private data */
11490dc1cffaSAnkur Dwivedi 	c_dev->data->dev_private = cptvf;
11500dc1cffaSAnkur Dwivedi 
11510dc1cffaSAnkur Dwivedi 	return 0;
11520dc1cffaSAnkur Dwivedi 
115313d711f3SKanaka Durga Kotamarthy deinit_dev:
115413d711f3SKanaka Durga Kotamarthy 	otx_cpt_deinit_device(cptvf);
115513d711f3SKanaka Durga Kotamarthy 
11560dc1cffaSAnkur Dwivedi fail:
11570dc1cffaSAnkur Dwivedi 	if (cptvf) {
11580dc1cffaSAnkur Dwivedi 		/* Free private data allocated */
11590dc1cffaSAnkur Dwivedi 		rte_free(cptvf);
11600dc1cffaSAnkur Dwivedi 	}
11610dc1cffaSAnkur Dwivedi 
11620dc1cffaSAnkur Dwivedi 	return ret;
1163bfe2ae49SAnoob Joseph }
1164