1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2019 Intel Corporation
5 #include <rte_cryptodev_pmd.h>
10 #include "qat_asym_pmd.h"
11 #include "qat_sym_capabilities.h"
12 #include "qat_asym_capabilities.h"
14 uint8_t qat_asym_driver_id;
16 static const struct rte_cryptodev_capabilities qat_gen1_asym_capabilities[] = {
17 QAT_BASE_GEN1_ASYM_CAPABILITIES,
18 RTE_CRYPTODEV_END_OF_CAPABILITIES_LIST()
21 static int qat_asym_qp_release(struct rte_cryptodev *dev,
22 uint16_t queue_pair_id);
24 static int qat_asym_dev_config(__rte_unused struct rte_cryptodev *dev,
25 __rte_unused struct rte_cryptodev_config *config)
30 static int qat_asym_dev_start(__rte_unused struct rte_cryptodev *dev)
35 static void qat_asym_dev_stop(__rte_unused struct rte_cryptodev *dev)
40 static int qat_asym_dev_close(struct rte_cryptodev *dev)
44 for (i = 0; i < dev->data->nb_queue_pairs; i++) {
45 ret = qat_asym_qp_release(dev, i);
53 static void qat_asym_dev_info_get(struct rte_cryptodev *dev,
54 struct rte_cryptodev_info *info)
56 struct qat_asym_dev_private *internals = dev->data->dev_private;
57 const struct qat_qp_hw_data *asym_hw_qps =
58 qat_gen_config[internals->qat_dev->qat_dev_gen]
59 .qp_hw_data[QAT_SERVICE_ASYMMETRIC];
62 info->max_nb_queue_pairs = qat_qps_per_service(asym_hw_qps,
63 QAT_SERVICE_ASYMMETRIC);
64 info->feature_flags = dev->feature_flags;
65 info->capabilities = internals->qat_dev_capabilities;
66 info->driver_id = qat_asym_driver_id;
67 /* No limit of number of sessions */
68 info->sym.max_nb_sessions = 0;
72 static void qat_asym_stats_get(struct rte_cryptodev *dev,
73 struct rte_cryptodev_stats *stats)
75 struct qat_common_stats qat_stats = {0};
76 struct qat_asym_dev_private *qat_priv;
78 if (stats == NULL || dev == NULL) {
79 QAT_LOG(ERR, "invalid ptr: stats %p, dev %p", stats, dev);
82 qat_priv = dev->data->dev_private;
84 qat_stats_get(qat_priv->qat_dev, &qat_stats, QAT_SERVICE_ASYMMETRIC);
85 stats->enqueued_count = qat_stats.enqueued_count;
86 stats->dequeued_count = qat_stats.dequeued_count;
87 stats->enqueue_err_count = qat_stats.enqueue_err_count;
88 stats->dequeue_err_count = qat_stats.dequeue_err_count;
91 static void qat_asym_stats_reset(struct rte_cryptodev *dev)
93 struct qat_asym_dev_private *qat_priv;
96 QAT_LOG(ERR, "invalid asymmetric cryptodev ptr %p", dev);
99 qat_priv = dev->data->dev_private;
101 qat_stats_reset(qat_priv->qat_dev, QAT_SERVICE_ASYMMETRIC);
104 static int qat_asym_qp_release(struct rte_cryptodev *dev,
105 uint16_t queue_pair_id)
107 struct qat_asym_dev_private *qat_private = dev->data->dev_private;
109 QAT_LOG(DEBUG, "Release asym qp %u on device %d",
110 queue_pair_id, dev->data->dev_id);
112 qat_private->qat_dev->qps_in_use[QAT_SERVICE_ASYMMETRIC][queue_pair_id]
115 return qat_qp_release((struct qat_qp **)
116 &(dev->data->queue_pairs[queue_pair_id]));
119 static int qat_asym_qp_setup(struct rte_cryptodev *dev, uint16_t qp_id,
120 const struct rte_cryptodev_qp_conf *qp_conf,
123 struct qat_qp_config qat_qp_conf;
128 struct qat_qp **qp_addr =
129 (struct qat_qp **)&(dev->data->queue_pairs[qp_id]);
130 struct qat_asym_dev_private *qat_private = dev->data->dev_private;
131 const struct qat_qp_hw_data *asym_hw_qps =
132 qat_gen_config[qat_private->qat_dev->qat_dev_gen]
133 .qp_hw_data[QAT_SERVICE_ASYMMETRIC];
134 const struct qat_qp_hw_data *qp_hw_data = asym_hw_qps + qp_id;
136 /* If qp is already in use free ring memory and qp metadata. */
137 if (*qp_addr != NULL) {
138 ret = qat_asym_qp_release(dev, qp_id);
142 if (qp_id >= qat_qps_per_service(asym_hw_qps, QAT_SERVICE_ASYMMETRIC)) {
143 QAT_LOG(ERR, "qp_id %u invalid for this device", qp_id);
147 qat_qp_conf.hw = qp_hw_data;
148 qat_qp_conf.build_request = qat_asym_build_request;
149 qat_qp_conf.cookie_size = sizeof(struct qat_asym_op_cookie);
150 qat_qp_conf.nb_descriptors = qp_conf->nb_descriptors;
151 qat_qp_conf.socket_id = socket_id;
152 qat_qp_conf.service_str = "asym";
154 ret = qat_qp_setup(qat_private->qat_dev, qp_addr, qp_id, &qat_qp_conf);
158 /* store a link to the qp in the qat_pci_device */
159 qat_private->qat_dev->qps_in_use[QAT_SERVICE_ASYMMETRIC][qp_id]
162 qp = (struct qat_qp *)*qp_addr;
163 qp->min_enq_burst_threshold = qat_private->min_enq_burst_threshold;
165 for (i = 0; i < qp->nb_descriptors; i++) {
168 struct qat_asym_op_cookie __rte_unused *cookie =
170 cookie->input_addr = rte_mempool_virt2iova(cookie) +
171 offsetof(struct qat_asym_op_cookie,
174 cookie->output_addr = rte_mempool_virt2iova(cookie) +
175 offsetof(struct qat_asym_op_cookie,
178 for (j = 0; j < 8; j++) {
179 cookie->input_params_ptrs[j] =
180 rte_mempool_virt2iova(cookie) +
181 offsetof(struct qat_asym_op_cookie,
183 cookie->output_params_ptrs[j] =
184 rte_mempool_virt2iova(cookie) +
185 offsetof(struct qat_asym_op_cookie,
193 struct rte_cryptodev_ops crypto_qat_ops = {
195 /* Device related operations */
196 .dev_configure = qat_asym_dev_config,
197 .dev_start = qat_asym_dev_start,
198 .dev_stop = qat_asym_dev_stop,
199 .dev_close = qat_asym_dev_close,
200 .dev_infos_get = qat_asym_dev_info_get,
202 .stats_get = qat_asym_stats_get,
203 .stats_reset = qat_asym_stats_reset,
204 .queue_pair_setup = qat_asym_qp_setup,
205 .queue_pair_release = qat_asym_qp_release,
207 /* Crypto related operations */
208 .asym_session_get_size = qat_asym_session_get_private_size,
209 .asym_session_configure = qat_asym_session_configure,
210 .asym_session_clear = qat_asym_session_clear
213 uint16_t qat_asym_pmd_enqueue_op_burst(void *qp, struct rte_crypto_op **ops,
216 return qat_enqueue_op_burst(qp, (void **)ops, nb_ops);
219 uint16_t qat_asym_pmd_dequeue_op_burst(void *qp, struct rte_crypto_op **ops,
222 return qat_dequeue_op_burst(qp, (void **)ops, nb_ops);
225 /* An rte_driver is needed in the registration of both the device and the driver
227 * The actual qat pci's rte_driver can't be used as its name represents
228 * the whole pci device with all services. Think of this as a holder for a name
229 * for the crypto part of the pci device.
231 static const char qat_asym_drv_name[] = RTE_STR(CRYPTODEV_NAME_QAT_ASYM_PMD);
232 static const struct rte_driver cryptodev_qat_asym_driver = {
233 .name = qat_asym_drv_name,
234 .alias = qat_asym_drv_name
238 qat_asym_dev_create(struct qat_pci_device *qat_pci_dev,
239 struct qat_dev_cmd_param *qat_dev_cmd_param)
242 struct qat_device_info *qat_dev_instance =
243 &qat_pci_devs[qat_pci_dev->qat_dev_id];
244 struct rte_cryptodev_pmd_init_params init_params = {
247 qat_dev_instance->pci_dev->device.numa_node,
248 .private_data_size = sizeof(struct qat_asym_dev_private)
250 char name[RTE_CRYPTODEV_NAME_MAX_LEN];
251 char capa_memz_name[RTE_CRYPTODEV_NAME_MAX_LEN];
252 struct rte_cryptodev *cryptodev;
253 struct qat_asym_dev_private *internals;
255 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
256 qat_pci_dev->qat_asym_driver_id =
258 } else if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
259 if (qat_pci_dev->qat_asym_driver_id !=
260 qat_asym_driver_id) {
262 "Device %s have different driver id than corresponding device in primary process",
268 snprintf(name, RTE_CRYPTODEV_NAME_MAX_LEN, "%s_%s",
269 qat_pci_dev->name, "asym");
270 QAT_LOG(DEBUG, "Creating QAT ASYM device %s\n", name);
272 /* Populate subset device to use in cryptodev device creation */
273 qat_dev_instance->asym_rte_dev.driver = &cryptodev_qat_asym_driver;
274 qat_dev_instance->asym_rte_dev.numa_node =
275 qat_dev_instance->pci_dev->device.numa_node;
276 qat_dev_instance->asym_rte_dev.devargs = NULL;
278 cryptodev = rte_cryptodev_pmd_create(name,
279 &(qat_dev_instance->asym_rte_dev), &init_params);
281 if (cryptodev == NULL)
284 qat_dev_instance->asym_rte_dev.name = cryptodev->data->name;
285 cryptodev->driver_id = qat_asym_driver_id;
286 cryptodev->dev_ops = &crypto_qat_ops;
288 cryptodev->enqueue_burst = qat_asym_pmd_enqueue_op_burst;
289 cryptodev->dequeue_burst = qat_asym_pmd_dequeue_op_burst;
291 cryptodev->feature_flags = RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO |
292 RTE_CRYPTODEV_FF_HW_ACCELERATED |
293 RTE_CRYPTODEV_FF_ASYM_SESSIONLESS |
294 RTE_CRYPTODEV_FF_RSA_PRIV_OP_KEY_EXP |
295 RTE_CRYPTODEV_FF_RSA_PRIV_OP_KEY_QT;
297 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
300 snprintf(capa_memz_name, RTE_CRYPTODEV_NAME_MAX_LEN,
301 "QAT_ASYM_CAPA_GEN_%d",
302 qat_pci_dev->qat_dev_gen);
304 internals = cryptodev->data->dev_private;
305 internals->qat_dev = qat_pci_dev;
306 internals->asym_dev_id = cryptodev->data->dev_id;
307 internals->qat_dev_capabilities = qat_gen1_asym_capabilities;
309 internals->capa_mz = rte_memzone_lookup(capa_memz_name);
310 if (internals->capa_mz == NULL) {
311 internals->capa_mz = rte_memzone_reserve(capa_memz_name,
312 sizeof(qat_gen1_asym_capabilities),
315 if (internals->capa_mz == NULL) {
317 "Error allocating memzone for capabilities, destroying PMD for %s",
319 rte_cryptodev_pmd_destroy(cryptodev);
320 memset(&qat_dev_instance->asym_rte_dev, 0,
321 sizeof(qat_dev_instance->asym_rte_dev));
325 memcpy(internals->capa_mz->addr, qat_gen1_asym_capabilities,
326 sizeof(qat_gen1_asym_capabilities));
327 internals->qat_dev_capabilities = internals->capa_mz->addr;
330 if (qat_dev_cmd_param[i].name == NULL)
332 if (!strcmp(qat_dev_cmd_param[i].name, ASYM_ENQ_THRESHOLD_NAME))
333 internals->min_enq_burst_threshold =
334 qat_dev_cmd_param[i].val;
338 qat_pci_dev->asym_dev = internals;
339 QAT_LOG(DEBUG, "Created QAT ASYM device %s as cryptodev instance %d",
340 cryptodev->data->name, internals->asym_dev_id);
345 qat_asym_dev_destroy(struct qat_pci_device *qat_pci_dev)
347 struct rte_cryptodev *cryptodev;
349 if (qat_pci_dev == NULL)
351 if (qat_pci_dev->asym_dev == NULL)
353 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
354 rte_memzone_free(qat_pci_dev->asym_dev->capa_mz);
356 /* free crypto device */
357 cryptodev = rte_cryptodev_pmd_get_dev(
358 qat_pci_dev->asym_dev->asym_dev_id);
359 rte_cryptodev_pmd_destroy(cryptodev);
360 qat_pci_devs[qat_pci_dev->qat_dev_id].asym_rte_dev.name = NULL;
361 qat_pci_dev->asym_dev = NULL;
366 static struct cryptodev_driver qat_crypto_drv;
367 RTE_PMD_REGISTER_CRYPTO_DRIVER(qat_crypto_drv,
368 cryptodev_qat_asym_driver,