xref: /dpdk/drivers/crypto/cnxk/cn9k_cryptodev.c (revision 3aecc6185177a47c8e23443d82f77afade20a5e7)
12457705eSAnkur Dwivedi /* SPDX-License-Identifier: BSD-3-Clause
22457705eSAnkur Dwivedi  * Copyright(C) 2021 Marvell.
32457705eSAnkur Dwivedi  */
42457705eSAnkur Dwivedi 
51f37cb2bSDavid Marchand #include <bus_pci_driver.h>
62457705eSAnkur Dwivedi #include <rte_common.h>
72457705eSAnkur Dwivedi #include <rte_crypto.h>
82457705eSAnkur Dwivedi #include <rte_cryptodev.h>
9af668035SAkhil Goyal #include <cryptodev_pmd.h>
101acb7f54SDavid Marchand #include <dev_driver.h>
112457705eSAnkur Dwivedi #include <rte_pci.h>
122457705eSAnkur Dwivedi 
132457705eSAnkur Dwivedi #include "cn9k_cryptodev.h"
14e0ab0865SAnkur Dwivedi #include "cn9k_cryptodev_ops.h"
1538f66464SArchana Muniganti #include "cn9k_ipsec.h"
16e0ab0865SAnkur Dwivedi #include "cnxk_cryptodev.h"
17bf662949SAnkur Dwivedi #include "cnxk_cryptodev_capabilities.h"
18*3aecc618SAkhil Goyal #include "cnxk_cryptodev_ops.h"
194cbef1e9SArchana Muniganti #include "cnxk_cryptodev_sec.h"
20bf662949SAnkur Dwivedi 
212457705eSAnkur Dwivedi #include "roc_api.h"
222457705eSAnkur Dwivedi 
232457705eSAnkur Dwivedi uint8_t cn9k_cryptodev_driver_id;
242457705eSAnkur Dwivedi 
252457705eSAnkur Dwivedi static struct rte_pci_id pci_id_cpt_table[] = {
262457705eSAnkur Dwivedi 	{
27b146c30dSAnoob Joseph 		RTE_PCI_DEVICE(PCI_VENDOR_ID_CAVIUM,
28b146c30dSAnoob Joseph 			       PCI_DEVID_CN9K_RVU_CPT_VF)
292457705eSAnkur Dwivedi 	},
302457705eSAnkur Dwivedi 	/* sentinel */
312457705eSAnkur Dwivedi 	{
322457705eSAnkur Dwivedi 		.device_id = 0
332457705eSAnkur Dwivedi 	},
342457705eSAnkur Dwivedi };
352457705eSAnkur Dwivedi 
36e0ab0865SAnkur Dwivedi static int
cn9k_cpt_pci_probe(struct rte_pci_driver * pci_drv __rte_unused,struct rte_pci_device * pci_dev)37e0ab0865SAnkur Dwivedi cn9k_cpt_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
38e0ab0865SAnkur Dwivedi 		   struct rte_pci_device *pci_dev)
39e0ab0865SAnkur Dwivedi {
40e0ab0865SAnkur Dwivedi 	struct rte_cryptodev_pmd_init_params init_params = {
41e0ab0865SAnkur Dwivedi 		.name = "",
42e0ab0865SAnkur Dwivedi 		.socket_id = rte_socket_id(),
43e0ab0865SAnkur Dwivedi 		.private_data_size = sizeof(struct cnxk_cpt_vf)
44e0ab0865SAnkur Dwivedi 	};
45e0ab0865SAnkur Dwivedi 	char name[RTE_CRYPTODEV_NAME_MAX_LEN];
46e0ab0865SAnkur Dwivedi 	struct rte_cryptodev *dev;
47e0ab0865SAnkur Dwivedi 	struct roc_cpt *roc_cpt;
48e0ab0865SAnkur Dwivedi 	struct cnxk_cpt_vf *vf;
49e0ab0865SAnkur Dwivedi 	int rc;
50e0ab0865SAnkur Dwivedi 
51e0ab0865SAnkur Dwivedi 	rc = roc_plt_init();
52e0ab0865SAnkur Dwivedi 	if (rc < 0) {
53e0ab0865SAnkur Dwivedi 		plt_err("Failed to initialize platform model");
54e0ab0865SAnkur Dwivedi 		return rc;
55e0ab0865SAnkur Dwivedi 	}
56e0ab0865SAnkur Dwivedi 
57e0ab0865SAnkur Dwivedi 	rte_pci_device_name(&pci_dev->addr, name, sizeof(name));
58e0ab0865SAnkur Dwivedi 
59e0ab0865SAnkur Dwivedi 	dev = rte_cryptodev_pmd_create(name, &pci_dev->device, &init_params);
60e0ab0865SAnkur Dwivedi 	if (dev == NULL) {
61e0ab0865SAnkur Dwivedi 		rc = -ENODEV;
62e0ab0865SAnkur Dwivedi 		goto exit;
63e0ab0865SAnkur Dwivedi 	}
64e0ab0865SAnkur Dwivedi 
65e0ab0865SAnkur Dwivedi 	/* Get private data space allocated */
66e0ab0865SAnkur Dwivedi 	vf = dev->data->dev_private;
67e0ab0865SAnkur Dwivedi 
68e0ab0865SAnkur Dwivedi 	roc_cpt = &vf->cpt;
69e0ab0865SAnkur Dwivedi 
70e0ab0865SAnkur Dwivedi 	if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
71e0ab0865SAnkur Dwivedi 		roc_cpt->pci_dev = pci_dev;
72135e0b71SAnkur Dwivedi 
73135e0b71SAnkur Dwivedi 		rc = cnxk_cpt_parse_devargs(dev->device->devargs, vf);
74135e0b71SAnkur Dwivedi 		if (rc) {
75135e0b71SAnkur Dwivedi 			plt_err("Failed to parse devargs rc=%d", rc);
76135e0b71SAnkur Dwivedi 			goto pmd_destroy;
77135e0b71SAnkur Dwivedi 		}
78135e0b71SAnkur Dwivedi 
79e0ab0865SAnkur Dwivedi 		rc = roc_cpt_dev_init(roc_cpt);
80e0ab0865SAnkur Dwivedi 		if (rc) {
81e0ab0865SAnkur Dwivedi 			plt_err("Failed to initialize roc cpt rc=%d", rc);
82e0ab0865SAnkur Dwivedi 			goto pmd_destroy;
83e0ab0865SAnkur Dwivedi 		}
84e0ab0865SAnkur Dwivedi 
85e0ab0865SAnkur Dwivedi 		rc = cnxk_cpt_eng_grp_add(roc_cpt);
86e0ab0865SAnkur Dwivedi 		if (rc) {
87e0ab0865SAnkur Dwivedi 			plt_err("Failed to add engine group rc=%d", rc);
88e0ab0865SAnkur Dwivedi 			goto dev_fini;
89e0ab0865SAnkur Dwivedi 		}
904cbef1e9SArchana Muniganti 
914cbef1e9SArchana Muniganti 		/* Create security context */
924cbef1e9SArchana Muniganti 		rc = cnxk_crypto_sec_ctx_create(dev);
934cbef1e9SArchana Muniganti 		if (rc)
944cbef1e9SArchana Muniganti 			goto dev_fini;
95e0ab0865SAnkur Dwivedi 	}
96e0ab0865SAnkur Dwivedi 
97e0ab0865SAnkur Dwivedi 	dev->dev_ops = &cn9k_cpt_ops;
98e0ab0865SAnkur Dwivedi 	dev->driver_id = cn9k_cryptodev_driver_id;
99760eedf3SAnoob Joseph 	dev->feature_flags = cnxk_cpt_default_ff_get();
100*3aecc618SAkhil Goyal 	dev->qp_depth_used = cnxk_cpt_qp_depth_used;
101e0ab0865SAnkur Dwivedi 
102bf662949SAnkur Dwivedi 	cnxk_cpt_caps_populate(vf);
103bf662949SAnkur Dwivedi 
10475ee5745SAnoob Joseph 	cn9k_cpt_set_enqdeq_fns(dev);
10538f66464SArchana Muniganti 	cn9k_sec_ops_override();
10675ee5745SAnoob Joseph 
107d54c72ecSAkhil Goyal 	rte_cryptodev_pmd_probing_finish(dev);
108d54c72ecSAkhil Goyal 
109e0ab0865SAnkur Dwivedi 	return 0;
110e0ab0865SAnkur Dwivedi 
111e0ab0865SAnkur Dwivedi dev_fini:
112e0ab0865SAnkur Dwivedi 	if (rte_eal_process_type() == RTE_PROC_PRIMARY)
113e0ab0865SAnkur Dwivedi 		roc_cpt_dev_fini(roc_cpt);
114e0ab0865SAnkur Dwivedi pmd_destroy:
115e0ab0865SAnkur Dwivedi 	rte_cryptodev_pmd_destroy(dev);
116e0ab0865SAnkur Dwivedi exit:
117e0ab0865SAnkur Dwivedi 	plt_err("Could not create device (vendor_id: 0x%x device_id: 0x%x)",
118e0ab0865SAnkur Dwivedi 		pci_dev->id.vendor_id, pci_dev->id.device_id);
119e0ab0865SAnkur Dwivedi 	return rc;
120e0ab0865SAnkur Dwivedi }
121e0ab0865SAnkur Dwivedi 
122e0ab0865SAnkur Dwivedi static int
cn9k_cpt_pci_remove(struct rte_pci_device * pci_dev)123e0ab0865SAnkur Dwivedi cn9k_cpt_pci_remove(struct rte_pci_device *pci_dev)
124e0ab0865SAnkur Dwivedi {
125e0ab0865SAnkur Dwivedi 	char name[RTE_CRYPTODEV_NAME_MAX_LEN];
126e0ab0865SAnkur Dwivedi 	struct rte_cryptodev *dev;
127e0ab0865SAnkur Dwivedi 	struct cnxk_cpt_vf *vf;
128e0ab0865SAnkur Dwivedi 	int ret;
129e0ab0865SAnkur Dwivedi 
130e0ab0865SAnkur Dwivedi 	if (pci_dev == NULL)
131e0ab0865SAnkur Dwivedi 		return -EINVAL;
132e0ab0865SAnkur Dwivedi 
133e0ab0865SAnkur Dwivedi 	rte_pci_device_name(&pci_dev->addr, name, sizeof(name));
134e0ab0865SAnkur Dwivedi 
135e0ab0865SAnkur Dwivedi 	dev = rte_cryptodev_pmd_get_named_dev(name);
136e0ab0865SAnkur Dwivedi 	if (dev == NULL)
137e0ab0865SAnkur Dwivedi 		return -ENODEV;
138e0ab0865SAnkur Dwivedi 
1394cbef1e9SArchana Muniganti 	/* Destroy security context */
1404cbef1e9SArchana Muniganti 	cnxk_crypto_sec_ctx_destroy(dev);
1414cbef1e9SArchana Muniganti 
142e0ab0865SAnkur Dwivedi 	if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
143f05e282fSTejasree Kondoj 		dev->dev_ops = NULL;
144e0ab0865SAnkur Dwivedi 		vf = dev->data->dev_private;
145e0ab0865SAnkur Dwivedi 		ret = roc_cpt_dev_fini(&vf->cpt);
146e0ab0865SAnkur Dwivedi 		if (ret)
147e0ab0865SAnkur Dwivedi 			return ret;
148e0ab0865SAnkur Dwivedi 	}
149e0ab0865SAnkur Dwivedi 
150e0ab0865SAnkur Dwivedi 	return rte_cryptodev_pmd_destroy(dev);
151e0ab0865SAnkur Dwivedi }
152e0ab0865SAnkur Dwivedi 
1532457705eSAnkur Dwivedi static struct rte_pci_driver cn9k_cryptodev_pmd = {
1542457705eSAnkur Dwivedi 	.id_table = pci_id_cpt_table,
1552457705eSAnkur Dwivedi 	.drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_NEED_IOVA_AS_VA,
156e0ab0865SAnkur Dwivedi 	.probe = cn9k_cpt_pci_probe,
157e0ab0865SAnkur Dwivedi 	.remove = cn9k_cpt_pci_remove,
1582457705eSAnkur Dwivedi };
1592457705eSAnkur Dwivedi 
1602457705eSAnkur Dwivedi static struct cryptodev_driver cn9k_cryptodev_drv;
1612457705eSAnkur Dwivedi 
1622457705eSAnkur Dwivedi RTE_PMD_REGISTER_PCI(CRYPTODEV_NAME_CN9K_PMD, cn9k_cryptodev_pmd);
1632457705eSAnkur Dwivedi RTE_PMD_REGISTER_PCI_TABLE(CRYPTODEV_NAME_CN9K_PMD, pci_id_cpt_table);
1642457705eSAnkur Dwivedi RTE_PMD_REGISTER_KMOD_DEP(CRYPTODEV_NAME_CN9K_PMD, "vfio-pci");
1652457705eSAnkur Dwivedi RTE_PMD_REGISTER_CRYPTO_DRIVER(cn9k_cryptodev_drv, cn9k_cryptodev_pmd.driver,
1662457705eSAnkur Dwivedi 			       cn9k_cryptodev_driver_id);
167