1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2017-2018 Intel Corporation
10 #include <rte_string_fns.h>
11 #include <rte_malloc.h>
13 #include <rte_memzone.h>
15 #include "rte_compressdev.h"
16 #include "rte_compressdev_internal.h"
17 #include "rte_compressdev_pmd.h"
19 #define RTE_COMPRESSDEV_DETACHED (0)
20 #define RTE_COMPRESSDEV_ATTACHED (1)
22 static struct rte_compressdev rte_comp_devices[RTE_COMPRESS_MAX_DEVS];
24 static struct rte_compressdev_global compressdev_globals = {
25 .devs = rte_comp_devices,
28 .max_devs = RTE_COMPRESS_MAX_DEVS
31 const struct rte_compressdev_capabilities *
32 rte_compressdev_capability_get(uint8_t dev_id,
33 enum rte_comp_algorithm algo)
35 const struct rte_compressdev_capabilities *capability;
36 struct rte_compressdev_info dev_info;
39 if (dev_id >= compressdev_globals.nb_devs) {
40 COMPRESSDEV_LOG(ERR, "Invalid dev_id=%d", dev_id);
43 rte_compressdev_info_get(dev_id, &dev_info);
45 while ((capability = &dev_info.capabilities[i++])->algo !=
46 RTE_COMP_ALGO_UNSPECIFIED){
47 if (capability->algo == algo)
55 rte_compressdev_get_feature_name(uint64_t flag)
58 case RTE_COMPDEV_FF_HW_ACCELERATED:
59 return "HW_ACCELERATED";
60 case RTE_COMPDEV_FF_CPU_SSE:
62 case RTE_COMPDEV_FF_CPU_AVX:
64 case RTE_COMPDEV_FF_CPU_AVX2:
66 case RTE_COMPDEV_FF_CPU_AVX512:
68 case RTE_COMPDEV_FF_CPU_NEON:
70 case RTE_COMPDEV_FF_OP_DONE_IN_DEQUEUE:
71 return "OP_DONE_IN_DEQ";
77 static struct rte_compressdev *
78 rte_compressdev_get_dev(uint8_t dev_id)
80 return &compressdev_globals.devs[dev_id];
83 struct rte_compressdev *
84 rte_compressdev_pmd_get_named_dev(const char *name)
86 struct rte_compressdev *dev;
92 for (i = 0; i < compressdev_globals.max_devs; i++) {
93 dev = &compressdev_globals.devs[i];
95 if ((dev->attached == RTE_COMPRESSDEV_ATTACHED) &&
96 (strcmp(dev->data->name, name) == 0))
104 rte_compressdev_is_valid_dev(uint8_t dev_id)
106 struct rte_compressdev *dev = NULL;
108 if (dev_id >= compressdev_globals.nb_devs)
111 dev = rte_compressdev_get_dev(dev_id);
112 if (dev->attached != RTE_COMPRESSDEV_ATTACHED)
120 rte_compressdev_get_dev_id(const char *name)
127 for (i = 0; i < compressdev_globals.nb_devs; i++)
128 if ((strcmp(compressdev_globals.devs[i].data->name, name)
130 (compressdev_globals.devs[i].attached ==
131 RTE_COMPRESSDEV_ATTACHED))
138 rte_compressdev_count(void)
140 return compressdev_globals.nb_devs;
144 rte_compressdev_devices_get(const char *driver_name, uint8_t *devices,
147 uint8_t i, count = 0;
148 struct rte_compressdev *devs = compressdev_globals.devs;
149 uint8_t max_devs = compressdev_globals.max_devs;
151 for (i = 0; i < max_devs && count < nb_devices; i++) {
153 if (devs[i].attached == RTE_COMPRESSDEV_ATTACHED) {
156 cmp = strncmp(devs[i].device->driver->name,
158 strlen(driver_name));
161 devices[count++] = devs[i].data->dev_id;
169 rte_compressdev_socket_id(uint8_t dev_id)
171 struct rte_compressdev *dev;
173 if (!rte_compressdev_is_valid_dev(dev_id))
176 dev = rte_compressdev_get_dev(dev_id);
178 return dev->data->socket_id;
182 rte_compressdev_data_alloc(uint8_t dev_id, struct rte_compressdev_data **data,
185 char mz_name[RTE_COMPRESSDEV_NAME_MAX_LEN];
186 const struct rte_memzone *mz;
189 /* generate memzone name */
190 n = snprintf(mz_name, sizeof(mz_name),
191 "rte_compressdev_data_%u", dev_id);
192 if (n >= (int)sizeof(mz_name))
195 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
196 mz = rte_memzone_reserve(mz_name,
197 sizeof(struct rte_compressdev_data),
200 mz = rte_memzone_lookup(mz_name);
206 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
207 memset(*data, 0, sizeof(struct rte_compressdev_data));
213 rte_compressdev_find_free_device_index(void)
217 for (dev_id = 0; dev_id < RTE_COMPRESS_MAX_DEVS; dev_id++) {
218 if (rte_comp_devices[dev_id].attached ==
219 RTE_COMPRESSDEV_DETACHED)
222 return RTE_COMPRESS_MAX_DEVS;
225 struct rte_compressdev *
226 rte_compressdev_pmd_allocate(const char *name, int socket_id)
228 struct rte_compressdev *compressdev;
231 if (rte_compressdev_pmd_get_named_dev(name) != NULL) {
233 "comp device with name %s already allocated!", name);
237 dev_id = rte_compressdev_find_free_device_index();
238 if (dev_id == RTE_COMPRESS_MAX_DEVS) {
239 COMPRESSDEV_LOG(ERR, "Reached maximum number of comp devices");
242 compressdev = rte_compressdev_get_dev(dev_id);
244 if (compressdev->data == NULL) {
245 struct rte_compressdev_data *compressdev_data =
246 compressdev_globals.data[dev_id];
248 int retval = rte_compressdev_data_alloc(dev_id,
249 &compressdev_data, socket_id);
251 if (retval < 0 || compressdev_data == NULL)
254 compressdev->data = compressdev_data;
256 strlcpy(compressdev->data->name, name,
257 RTE_COMPRESSDEV_NAME_MAX_LEN);
259 compressdev->data->dev_id = dev_id;
260 compressdev->data->socket_id = socket_id;
261 compressdev->data->dev_started = 0;
263 compressdev->attached = RTE_COMPRESSDEV_ATTACHED;
265 compressdev_globals.nb_devs++;
272 rte_compressdev_pmd_release_device(struct rte_compressdev *compressdev)
276 if (compressdev == NULL)
279 /* Close device only if device operations have been set */
280 if (compressdev->dev_ops) {
281 ret = rte_compressdev_close(compressdev->data->dev_id);
286 compressdev->attached = RTE_COMPRESSDEV_DETACHED;
287 compressdev_globals.nb_devs--;
292 rte_compressdev_queue_pair_count(uint8_t dev_id)
294 struct rte_compressdev *dev;
296 dev = &rte_comp_devices[dev_id];
297 return dev->data->nb_queue_pairs;
301 rte_compressdev_queue_pairs_config(struct rte_compressdev *dev,
302 uint16_t nb_qpairs, int socket_id)
304 struct rte_compressdev_info dev_info;
308 if ((dev == NULL) || (nb_qpairs < 1)) {
309 COMPRESSDEV_LOG(ERR, "invalid param: dev %p, nb_queues %u",
314 COMPRESSDEV_LOG(DEBUG, "Setup %d queues pairs on device %u",
315 nb_qpairs, dev->data->dev_id);
317 memset(&dev_info, 0, sizeof(struct rte_compressdev_info));
319 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_infos_get, -ENOTSUP);
320 (*dev->dev_ops->dev_infos_get)(dev, &dev_info);
322 if ((dev_info.max_nb_queue_pairs != 0) &&
323 (nb_qpairs > dev_info.max_nb_queue_pairs)) {
324 COMPRESSDEV_LOG(ERR, "Invalid num queue_pairs (%u) for dev %u",
325 nb_qpairs, dev->data->dev_id);
329 if (dev->data->queue_pairs == NULL) { /* first time configuration */
330 dev->data->queue_pairs = rte_zmalloc_socket(
331 "compressdev->queue_pairs",
332 sizeof(dev->data->queue_pairs[0]) * nb_qpairs,
333 RTE_CACHE_LINE_SIZE, socket_id);
335 if (dev->data->queue_pairs == NULL) {
336 dev->data->nb_queue_pairs = 0;
338 "failed to get memory for qp meta data, nb_queues %u",
342 } else { /* re-configure */
344 uint16_t old_nb_queues = dev->data->nb_queue_pairs;
346 qp = dev->data->queue_pairs;
348 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_pair_release,
351 for (i = nb_qpairs; i < old_nb_queues; i++) {
352 ret = (*dev->dev_ops->queue_pair_release)(dev, i);
357 qp = rte_realloc(qp, sizeof(qp[0]) * nb_qpairs,
358 RTE_CACHE_LINE_SIZE);
361 "failed to realloc qp meta data, nb_queues %u",
366 if (nb_qpairs > old_nb_queues) {
367 uint16_t new_qs = nb_qpairs - old_nb_queues;
369 memset(qp + old_nb_queues, 0,
370 sizeof(qp[0]) * new_qs);
373 dev->data->queue_pairs = qp;
376 dev->data->nb_queue_pairs = nb_qpairs;
381 rte_compressdev_queue_pairs_release(struct rte_compressdev *dev)
387 COMPRESSDEV_LOG(ERR, "invalid param: dev %p", dev);
391 num_qps = dev->data->nb_queue_pairs;
396 COMPRESSDEV_LOG(DEBUG, "Free %d queues pairs on device %u",
397 dev->data->nb_queue_pairs, dev->data->dev_id);
399 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_pair_release,
402 for (i = 0; i < num_qps; i++) {
403 ret = (*dev->dev_ops->queue_pair_release)(dev, i);
408 if (dev->data->queue_pairs != NULL)
409 rte_free(dev->data->queue_pairs);
410 dev->data->queue_pairs = NULL;
411 dev->data->nb_queue_pairs = 0;
417 rte_compressdev_configure(uint8_t dev_id, struct rte_compressdev_config *config)
419 struct rte_compressdev *dev;
422 if (!rte_compressdev_is_valid_dev(dev_id)) {
423 COMPRESSDEV_LOG(ERR, "Invalid dev_id=%" PRIu8, dev_id);
427 dev = &rte_comp_devices[dev_id];
429 if (dev->data->dev_started) {
431 "device %d must be stopped to allow configuration", dev_id);
435 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_configure, -ENOTSUP);
437 /* Setup new number of queue pairs and reconfigure device. */
438 diag = rte_compressdev_queue_pairs_config(dev, config->nb_queue_pairs,
442 "dev%d rte_comp_dev_queue_pairs_config = %d",
447 return (*dev->dev_ops->dev_configure)(dev, config);
451 rte_compressdev_start(uint8_t dev_id)
453 struct rte_compressdev *dev;
456 COMPRESSDEV_LOG(DEBUG, "Start dev_id=%" PRIu8, dev_id);
458 if (!rte_compressdev_is_valid_dev(dev_id)) {
459 COMPRESSDEV_LOG(ERR, "Invalid dev_id=%" PRIu8, dev_id);
463 dev = &rte_comp_devices[dev_id];
465 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_start, -ENOTSUP);
467 if (dev->data->dev_started != 0) {
469 "Device with dev_id=%" PRIu8 " already started", dev_id);
473 diag = (*dev->dev_ops->dev_start)(dev);
475 dev->data->dev_started = 1;
483 rte_compressdev_stop(uint8_t dev_id)
485 struct rte_compressdev *dev;
487 if (!rte_compressdev_is_valid_dev(dev_id)) {
488 COMPRESSDEV_LOG(ERR, "Invalid dev_id=%" PRIu8, dev_id);
492 dev = &rte_comp_devices[dev_id];
494 RTE_FUNC_PTR_OR_RET(*dev->dev_ops->dev_stop);
496 if (dev->data->dev_started == 0) {
498 "Device with dev_id=%" PRIu8 " already stopped", dev_id);
502 (*dev->dev_ops->dev_stop)(dev);
503 dev->data->dev_started = 0;
507 rte_compressdev_close(uint8_t dev_id)
509 struct rte_compressdev *dev;
512 if (!rte_compressdev_is_valid_dev(dev_id)) {
513 COMPRESSDEV_LOG(ERR, "Invalid dev_id=%" PRIu8, dev_id);
517 dev = &rte_comp_devices[dev_id];
519 /* Device must be stopped before it can be closed */
520 if (dev->data->dev_started == 1) {
521 COMPRESSDEV_LOG(ERR, "Device %u must be stopped before closing",
526 /* Free queue pairs memory */
527 retval = rte_compressdev_queue_pairs_release(dev);
532 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_close, -ENOTSUP);
533 retval = (*dev->dev_ops->dev_close)(dev);
542 rte_compressdev_queue_pair_setup(uint8_t dev_id, uint16_t queue_pair_id,
543 uint32_t max_inflight_ops, int socket_id)
545 struct rte_compressdev *dev;
547 if (!rte_compressdev_is_valid_dev(dev_id)) {
548 COMPRESSDEV_LOG(ERR, "Invalid dev_id=%" PRIu8, dev_id);
552 dev = &rte_comp_devices[dev_id];
553 if (queue_pair_id >= dev->data->nb_queue_pairs) {
554 COMPRESSDEV_LOG(ERR, "Invalid queue_pair_id=%d", queue_pair_id);
558 if (dev->data->dev_started) {
560 "device %d must be stopped to allow configuration", dev_id);
564 if (max_inflight_ops == 0) {
566 "Invalid maximum number of inflight operations");
570 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_pair_setup, -ENOTSUP);
572 return (*dev->dev_ops->queue_pair_setup)(dev, queue_pair_id,
573 max_inflight_ops, socket_id);
577 rte_compressdev_dequeue_burst(uint8_t dev_id, uint16_t qp_id,
578 struct rte_comp_op **ops, uint16_t nb_ops)
580 struct rte_compressdev *dev = &rte_comp_devices[dev_id];
582 nb_ops = (*dev->dequeue_burst)
583 (dev->data->queue_pairs[qp_id], ops, nb_ops);
589 rte_compressdev_enqueue_burst(uint8_t dev_id, uint16_t qp_id,
590 struct rte_comp_op **ops, uint16_t nb_ops)
592 struct rte_compressdev *dev = &rte_comp_devices[dev_id];
594 return (*dev->enqueue_burst)(
595 dev->data->queue_pairs[qp_id], ops, nb_ops);
599 rte_compressdev_stats_get(uint8_t dev_id, struct rte_compressdev_stats *stats)
601 struct rte_compressdev *dev;
603 if (!rte_compressdev_is_valid_dev(dev_id)) {
604 COMPRESSDEV_LOG(ERR, "Invalid dev_id=%d", dev_id);
609 COMPRESSDEV_LOG(ERR, "Invalid stats ptr");
613 dev = &rte_comp_devices[dev_id];
614 memset(stats, 0, sizeof(*stats));
616 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->stats_get, -ENOTSUP);
617 (*dev->dev_ops->stats_get)(dev, stats);
622 rte_compressdev_stats_reset(uint8_t dev_id)
624 struct rte_compressdev *dev;
626 if (!rte_compressdev_is_valid_dev(dev_id)) {
627 COMPRESSDEV_LOG(ERR, "Invalid dev_id=%" PRIu8, dev_id);
631 dev = &rte_comp_devices[dev_id];
633 RTE_FUNC_PTR_OR_RET(*dev->dev_ops->stats_reset);
634 (*dev->dev_ops->stats_reset)(dev);
639 rte_compressdev_info_get(uint8_t dev_id, struct rte_compressdev_info *dev_info)
641 struct rte_compressdev *dev;
643 if (dev_id >= compressdev_globals.nb_devs) {
644 COMPRESSDEV_LOG(ERR, "Invalid dev_id=%d", dev_id);
648 dev = &rte_comp_devices[dev_id];
650 memset(dev_info, 0, sizeof(struct rte_compressdev_info));
652 RTE_FUNC_PTR_OR_RET(*dev->dev_ops->dev_infos_get);
653 (*dev->dev_ops->dev_infos_get)(dev, dev_info);
655 dev_info->driver_name = dev->device->driver->name;
659 rte_compressdev_private_xform_create(uint8_t dev_id,
660 const struct rte_comp_xform *xform,
663 struct rte_compressdev *dev;
666 dev = rte_compressdev_get_dev(dev_id);
668 if (xform == NULL || priv_xform == NULL || dev == NULL)
671 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->private_xform_create, -ENOTSUP);
672 ret = (*dev->dev_ops->private_xform_create)(dev, xform, priv_xform);
675 "dev_id %d failed to create private_xform: err=%d",
684 rte_compressdev_private_xform_free(uint8_t dev_id, void *priv_xform)
686 struct rte_compressdev *dev;
689 dev = rte_compressdev_get_dev(dev_id);
691 if (dev == NULL || priv_xform == NULL)
694 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->private_xform_free, -ENOTSUP);
695 ret = dev->dev_ops->private_xform_free(dev, priv_xform);
698 "dev_id %d failed to free private xform: err=%d",
707 rte_compressdev_stream_create(uint8_t dev_id,
708 const struct rte_comp_xform *xform,
711 struct rte_compressdev *dev;
714 dev = rte_compressdev_get_dev(dev_id);
716 if (xform == NULL || dev == NULL || stream == NULL)
719 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->stream_create, -ENOTSUP);
720 ret = (*dev->dev_ops->stream_create)(dev, xform, stream);
723 "dev_id %d failed to create stream: err=%d",
733 rte_compressdev_stream_free(uint8_t dev_id, void *stream)
735 struct rte_compressdev *dev;
738 dev = rte_compressdev_get_dev(dev_id);
740 if (dev == NULL || stream == NULL)
743 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->stream_free, -ENOTSUP);
744 ret = dev->dev_ops->stream_free(dev, stream);
747 "dev_id %d failed to free stream: err=%d",
756 rte_compressdev_name_get(uint8_t dev_id)
758 struct rte_compressdev *dev = rte_compressdev_get_dev(dev_id);
763 return dev->data->name;
766 RTE_LOG_REGISTER_DEFAULT(compressdev_logtype, NOTICE);