1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright (C) 2019 Marvell International Ltd.
3  */
4 
5 #include <rte_bus_pci.h>
6 #include <rte_common.h>
7 #include <rte_crypto.h>
8 #include <rte_cryptodev.h>
9 #include <rte_cryptodev_pmd.h>
10 #include <rte_dev.h>
11 #include <rte_errno.h>
12 #include <rte_mempool.h>
13 #include <rte_pci.h>
14 
15 #include "otx2_common.h"
16 #include "otx2_cryptodev.h"
17 #include "otx2_cryptodev_capabilities.h"
18 #include "otx2_cryptodev_mbox.h"
19 #include "otx2_cryptodev_ops.h"
20 #include "otx2_cryptodev_sec.h"
21 #include "otx2_dev.h"
22 
23 /* CPT common headers */
24 #include "cpt_common.h"
25 #include "cpt_pmd_logs.h"
26 
27 uint8_t otx2_cryptodev_driver_id;
28 
29 static struct rte_pci_id pci_id_cpt_table[] = {
30 	{
31 		RTE_PCI_DEVICE(PCI_VENDOR_ID_CAVIUM,
32 			       PCI_DEVID_OCTEONTX2_RVU_CPT_VF)
33 	},
34 	/* sentinel */
35 	{
36 		.device_id = 0
37 	},
38 };
39 
40 static int
otx2_cpt_pci_probe(struct rte_pci_driver * pci_drv __rte_unused,struct rte_pci_device * pci_dev)41 otx2_cpt_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
42 		   struct rte_pci_device *pci_dev)
43 {
44 	struct rte_cryptodev_pmd_init_params init_params = {
45 		.name = "",
46 		.socket_id = rte_socket_id(),
47 		.private_data_size = sizeof(struct otx2_cpt_vf)
48 	};
49 	char name[RTE_CRYPTODEV_NAME_MAX_LEN];
50 	struct rte_cryptodev *dev;
51 	struct otx2_dev *otx2_dev;
52 	struct otx2_cpt_vf *vf;
53 	uint16_t nb_queues;
54 	int ret;
55 
56 	rte_pci_device_name(&pci_dev->addr, name, sizeof(name));
57 
58 	dev = rte_cryptodev_pmd_create(name, &pci_dev->device, &init_params);
59 	if (dev == NULL) {
60 		ret = -ENODEV;
61 		goto exit;
62 	}
63 
64 	dev->dev_ops = &otx2_cpt_ops;
65 
66 	dev->driver_id = otx2_cryptodev_driver_id;
67 
68 	/* Get private data space allocated */
69 	vf = dev->data->dev_private;
70 
71 	otx2_dev = &vf->otx2_dev;
72 
73 	if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
74 		/* Initialize the base otx2_dev object */
75 		ret = otx2_dev_init(pci_dev, otx2_dev);
76 		if (ret) {
77 			CPT_LOG_ERR("Could not initialize otx2_dev");
78 			goto pmd_destroy;
79 		}
80 
81 		/* Get number of queues available on the device */
82 		ret = otx2_cpt_available_queues_get(dev, &nb_queues);
83 		if (ret) {
84 			CPT_LOG_ERR("Could not determine the number of queues available");
85 			goto otx2_dev_fini;
86 		}
87 
88 		/* Don't exceed the limits set per VF */
89 		nb_queues = RTE_MIN(nb_queues, OTX2_CPT_MAX_QUEUES_PER_VF);
90 
91 		if (nb_queues == 0) {
92 			CPT_LOG_ERR("No free queues available on the device");
93 			goto otx2_dev_fini;
94 		}
95 
96 		vf->max_queues = nb_queues;
97 
98 		CPT_LOG_INFO("Max queues supported by device: %d",
99 				vf->max_queues);
100 
101 		ret = otx2_cpt_hardware_caps_get(dev, vf->hw_caps);
102 		if (ret) {
103 			CPT_LOG_ERR("Could not determine hardware capabilities");
104 			goto otx2_dev_fini;
105 		}
106 	}
107 
108 	otx2_crypto_capabilities_init(vf->hw_caps);
109 	otx2_crypto_sec_capabilities_init(vf->hw_caps);
110 
111 	/* Create security ctx */
112 	ret = otx2_crypto_sec_ctx_create(dev);
113 	if (ret)
114 		goto otx2_dev_fini;
115 
116 	dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
117 			     RTE_CRYPTODEV_FF_HW_ACCELERATED |
118 			     RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
119 			     RTE_CRYPTODEV_FF_IN_PLACE_SGL |
120 			     RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT |
121 			     RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT |
122 			     RTE_CRYPTODEV_FF_OOP_SGL_IN_SGL_OUT |
123 			     RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO |
124 			     RTE_CRYPTODEV_FF_RSA_PRIV_OP_KEY_QT |
125 			     RTE_CRYPTODEV_FF_SYM_SESSIONLESS |
126 			     RTE_CRYPTODEV_FF_SECURITY;
127 
128 	if (rte_eal_process_type() == RTE_PROC_SECONDARY)
129 		otx2_cpt_set_enqdeq_fns(dev);
130 
131 	return 0;
132 
133 otx2_dev_fini:
134 	if (rte_eal_process_type() == RTE_PROC_PRIMARY)
135 		otx2_dev_fini(pci_dev, otx2_dev);
136 pmd_destroy:
137 	rte_cryptodev_pmd_destroy(dev);
138 exit:
139 	CPT_LOG_ERR("Could not create device (vendor_id: 0x%x device_id: 0x%x)",
140 		    pci_dev->id.vendor_id, pci_dev->id.device_id);
141 	return ret;
142 }
143 
144 static int
otx2_cpt_pci_remove(struct rte_pci_device * pci_dev)145 otx2_cpt_pci_remove(struct rte_pci_device *pci_dev)
146 {
147 	char name[RTE_CRYPTODEV_NAME_MAX_LEN];
148 	struct rte_cryptodev *dev;
149 
150 	if (pci_dev == NULL)
151 		return -EINVAL;
152 
153 	rte_pci_device_name(&pci_dev->addr, name, sizeof(name));
154 
155 	dev = rte_cryptodev_pmd_get_named_dev(name);
156 	if (dev == NULL)
157 		return -ENODEV;
158 
159 	/* Destroy security ctx */
160 	otx2_crypto_sec_ctx_destroy(dev);
161 
162 	return rte_cryptodev_pmd_destroy(dev);
163 }
164 
165 static struct rte_pci_driver otx2_cryptodev_pmd = {
166 	.id_table = pci_id_cpt_table,
167 	.drv_flags = RTE_PCI_DRV_NEED_MAPPING,
168 	.probe = otx2_cpt_pci_probe,
169 	.remove = otx2_cpt_pci_remove,
170 };
171 
172 static struct cryptodev_driver otx2_cryptodev_drv;
173 
174 RTE_PMD_REGISTER_PCI(CRYPTODEV_NAME_OCTEONTX2_PMD, otx2_cryptodev_pmd);
175 RTE_PMD_REGISTER_PCI_TABLE(CRYPTODEV_NAME_OCTEONTX2_PMD, pci_id_cpt_table);
176 RTE_PMD_REGISTER_KMOD_DEP(CRYPTODEV_NAME_OCTEONTX2_PMD, "vfio-pci");
177 RTE_PMD_REGISTER_CRYPTO_DRIVER(otx2_cryptodev_drv, otx2_cryptodev_pmd.driver,
178 		otx2_cryptodev_driver_id);
179 RTE_LOG_REGISTER(otx2_cpt_logtype, pmd.crypto.octeontx2, NOTICE);
180