Add configuration of compressdev_ops and related functions.

Signed-off-by: Hanxiao Li <li.hanxiao@zte.com.cn>
---
 drivers/common/zsda/meson.build       |   2 +-
 drivers/compress/zsda/zsda_comp_pmd.c | 345 ++++++++++++++++++++++++++
 drivers/compress/zsda/zsda_comp_pmd.h |  30 +++
 3 files changed, 376 insertions(+), 1 deletion(-)
 create mode 100644 drivers/compress/zsda/zsda_comp_pmd.c
 create mode 100644 drivers/compress/zsda/zsda_comp_pmd.h

diff --git a/drivers/common/zsda/meson.build b/drivers/common/zsda/meson.build
index 3eca11b19b..318adb1fd7 100644
--- a/drivers/common/zsda/meson.build
+++ b/drivers/common/zsda/meson.build
@@ -20,7 +20,7 @@ zsda_compress_path = 'compress/zsda'
 zsda_compress_relpath = '../../' + zsda_compress_path
 includes += include_directories(zsda_compress_relpath)
 if zsda_compress
-    foreach f: ['zsda_comp.c']
+    foreach f: ['zsda_comp.c', 'zsda_comp_pmd.c']
         sources += files(join_paths(zsda_compress_relpath, f))
     endforeach
 endif
diff --git a/drivers/compress/zsda/zsda_comp_pmd.c b/drivers/compress/zsda/zsda_comp_pmd.c
new file mode 100644
index 0000000000..d651116e3d
--- /dev/null
+++ b/drivers/compress/zsda/zsda_comp_pmd.c
@@ -0,0 +1,345 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2024 ZTE Corporation
+ */
+
+#include <rte_malloc.h>
+
+#include "zsda_comp.h"
+#include "zsda_comp_pmd.h"
+
+static const struct rte_compressdev_capabilities zsda_comp_capabilities[] = {
+    {
+        .algo = RTE_COMP_ALGO_DEFLATE,
+        .comp_feature_flags = RTE_COMP_FF_HUFFMAN_DYNAMIC |
+                            RTE_COMP_FF_OOP_SGL_IN_SGL_OUT |
+                            RTE_COMP_FF_OOP_SGL_IN_LB_OUT |
+                            RTE_COMP_FF_OOP_LB_IN_SGL_OUT |
+                            RTE_COMP_FF_CRC32_CHECKSUM |
+                            RTE_COMP_FF_ADLER32_CHECKSUM,
+        .window_size = {.min = 15, .max = 15, .increment = 0},
+    },
+};
+
+static void
+zsda_comp_stats_get(struct rte_compressdev *dev,
+            struct rte_compressdev_stats *stats)
+{
+    struct zsda_common_stat comm = {0};
+
+    zsda_stats_get(dev->data->queue_pairs, dev->data->nb_queue_pairs,
+               &comm);
+    stats->enqueued_count = comm.enqueued_count;
+    stats->dequeued_count = comm.dequeued_count;
+    stats->enqueue_err_count = comm.enqueue_err_count;
+    stats->dequeue_err_count = comm.dequeue_err_count;
+}
+
+static void
+zsda_comp_stats_reset(struct rte_compressdev *dev)
+{
+    zsda_stats_reset(dev->data->queue_pairs, dev->data->nb_queue_pairs);
+}
+
+static int
+zsda_comp_qp_release(struct rte_compressdev *dev, uint16_t queue_pair_id)
+{
+    return zsda_queue_pair_release(
+        (struct zsda_qp **)&(dev->data->queue_pairs[queue_pair_id]));
+}
+
+
+static int
+zsda_setup_comp_queue(struct zsda_pci_device *zsda_pci_dev, const uint16_t qp_id,
+         struct zsda_qp *qp, uint16_t nb_des, int socket_id)
+{
+    enum zsda_service_type type = ZSDA_SERVICE_COMPRESSION;
+    struct zsda_qp_config conf;
+    int ret;
+    struct zsda_qp_hw *qp_hw;
+
+    qp_hw = zsda_qps_hw_per_service(zsda_pci_dev, type);
+    conf.hw = qp_hw->data + qp_id;
+    conf.service_type = type;
+    conf.cookie_size = sizeof(struct zsda_op_cookie);
+    conf.nb_descriptors = nb_des;
+    conf.socket_id = socket_id;
+    conf.service_str = "comp";
+
+    ret = zsda_common_setup_qp(zsda_pci_dev->zsda_dev_id, &qp, qp_id, &conf);
+    qp->srv[type].rx_cb = zsda_comp_callback;
+    qp->srv[type].tx_cb = zsda_build_comp_request;
+    qp->srv[type].match = zsda_comp_match;
+
+    return ret;
+}
+
+static int
+zsda_setup_decomp_queue(struct zsda_pci_device *zsda_pci_dev, const uint16_t qp_id,
+           struct zsda_qp *qp, uint16_t nb_des, int socket_id)
+{
+    enum zsda_service_type type = ZSDA_SERVICE_DECOMPRESSION;
+    struct zsda_qp_config conf;
+    int ret;
+    struct zsda_qp_hw *qp_hw;
+
+    qp_hw = zsda_qps_hw_per_service(zsda_pci_dev, type);
+    conf.hw = qp_hw->data + qp_id;
+    conf.service_type = type;
+    conf.cookie_size = sizeof(struct zsda_op_cookie);
+    conf.nb_descriptors = nb_des;
+    conf.socket_id = socket_id;
+    conf.service_str = "decomp";
+
+    ret = zsda_common_setup_qp(zsda_pci_dev->zsda_dev_id, &qp, qp_id, &conf);
+    qp->srv[type].rx_cb = zsda_comp_callback;
+    qp->srv[type].tx_cb = zsda_build_decomp_request;
+    qp->srv[type].match = zsda_decomp_match;
+
+    return ret;
+}
+
+static int
+zsda_comp_qp_setup(struct rte_compressdev *dev, uint16_t qp_id,
+           uint32_t max_inflight_ops, int socket_id)
+{
+    int ret = ZSDA_SUCCESS;
+    struct zsda_qp *qp_new;
+
+    struct zsda_qp **qp_addr =
+        (struct zsda_qp **)&(dev->data->queue_pairs[qp_id]);
+    struct zsda_comp_dev_private *comp_priv = dev->data->dev_private;
+    struct zsda_pci_device *zsda_pci_dev = comp_priv->zsda_pci_dev;
+    uint16_t num_qps_comp = zsda_nb_qps.encomp;
+    uint16_t num_qps_decomp = zsda_nb_qps.decomp;
+    uint16_t nb_des = max_inflight_ops & 0xffff;
+
+    nb_des = (nb_des == NB_DES) ? nb_des : NB_DES;
+
+    if (*qp_addr != NULL) {
+        ret = zsda_comp_qp_release(dev, qp_id);
+        if (ret)
+            return ret;
+    }
+
+    qp_new = rte_zmalloc_socket("zsda PMD qp metadata", sizeof(*qp_new),
+                    RTE_CACHE_LINE_SIZE, socket_id);
+    if (qp_new == NULL) {
+        ZSDA_LOG(ERR, E_MALLOC);
+        return -ENOMEM;
+    }
+
+    if (num_qps_comp == MAX_QPS_ON_FUNCTION)
+        ret = zsda_setup_comp_queue(zsda_pci_dev, qp_id, qp_new, nb_des,
+                    socket_id);
+    else if (num_qps_decomp == MAX_QPS_ON_FUNCTION)
+        ret = zsda_setup_decomp_queue(zsda_pci_dev, qp_id, qp_new, nb_des,
+                      socket_id);
+    else {
+        ret = zsda_setup_comp_queue(zsda_pci_dev, qp_id, qp_new, nb_des,
+                    socket_id);
+        ret |= zsda_setup_decomp_queue(zsda_pci_dev, qp_id, qp_new, nb_des,
+                      socket_id);
+    }
+
+    if (ret) {
+        rte_free(qp_new);
+        return ret;
+    }
+
+    *qp_addr = qp_new;
+
+    return ret;
+}
+
+static int
+zsda_comp_xform_size(void)
+{
+    return RTE_ALIGN_CEIL(sizeof(struct zsda_comp_xform), 8);
+}
+
+static struct rte_mempool *
+zsda_comp_create_xform_pool(struct zsda_comp_dev_private *comp_dev,
+                struct rte_compressdev_config *config,
+                uint32_t num_elements)
+{
+    char xform_pool_name[RTE_MEMPOOL_NAMESIZE];
+    struct rte_mempool *mp;
+
+    snprintf(xform_pool_name, RTE_MEMPOOL_NAMESIZE, "%s_xforms",
+         comp_dev->zsda_pci_dev->name);
+
+    ZSDA_LOG(DEBUG, "xformpool: %s", xform_pool_name);
+    mp = rte_mempool_lookup(xform_pool_name);
+
+    if (mp != NULL) {
+        ZSDA_LOG(DEBUG, "xformpool already created");
+        if (mp->size != num_elements) {
+            ZSDA_LOG(DEBUG, "xformpool wrong size - delete it");
+            rte_mempool_free(mp);
+            mp = NULL;
+            comp_dev->xformpool = NULL;
+        }
+    } else {
+        mp = rte_mempool_create(xform_pool_name, num_elements,
+                    zsda_comp_xform_size(), 0, 0, NULL,
+                    NULL, NULL, NULL, config->socket_id, 0);
+        if (mp == NULL) {
+            ZSDA_LOG(ERR, E_CREATE);
+            return NULL;
+        }
+    }
+
+    return mp;
+}
+
+static int
+zsda_comp_private_xform_create(struct rte_compressdev *dev,
+                   const struct rte_comp_xform *xform,
+                   void **private_xform)
+{
+    struct zsda_comp_dev_private *zsda = dev->data->dev_private;
+
+    if (unlikely(private_xform == NULL)) {
+        ZSDA_LOG(ERR, E_NULL);
+        return -EINVAL;
+    }
+    if (unlikely(zsda->xformpool == NULL)) {
+        ZSDA_LOG(ERR, E_NULL);
+        return -ENOMEM;
+    }
+    if (rte_mempool_get(zsda->xformpool, private_xform)) {
+        ZSDA_LOG(ERR, E_NULL);
+        return -ENOMEM;
+    }
+
+    struct zsda_comp_xform *zsda_xform = *private_xform;
+    zsda_xform->type = xform->type;
+
+    if (zsda_xform->type == RTE_COMP_COMPRESS)
+        zsda_xform->checksum_type = xform->compress.chksum;
+    else
+        zsda_xform->checksum_type = xform->decompress.chksum;
+
+    if (zsda_xform->checksum_type == RTE_COMP_CHECKSUM_CRC32_ADLER32)
+        return -EINVAL;
+
+    return ZSDA_SUCCESS;
+}
+
+static int
+zsda_comp_private_xform_free(struct rte_compressdev *dev __rte_unused,
+                 void *private_xform)
+{
+    struct zsda_comp_xform *zsda_xform = private_xform;
+
+    if (zsda_xform) {
+        memset(zsda_xform, 0, zsda_comp_xform_size());
+        struct rte_mempool *mp = rte_mempool_from_obj(zsda_xform);
+
+        rte_mempool_put(mp, zsda_xform);
+        return ZSDA_SUCCESS;
+    }
+    return -EINVAL;
+}
+
+static int
+zsda_comp_dev_close(struct rte_compressdev *dev)
+{
+    uint16_t i;
+    struct zsda_comp_dev_private *comp_dev = dev->data->dev_private;
+
+    for (i = 0; i < dev->data->nb_queue_pairs; i++)
+        zsda_comp_qp_release(dev, i);
+
+    rte_mempool_free(comp_dev->xformpool);
+    comp_dev->xformpool = NULL;
+
+    return ZSDA_SUCCESS;
+}
+
+static int
+zsda_comp_dev_config(struct rte_compressdev *dev,
+             struct rte_compressdev_config *config)
+{
+    struct zsda_comp_dev_private *comp_dev = dev->data->dev_private;
+
+    if (config->max_nb_priv_xforms) {
+        comp_dev->xformpool = zsda_comp_create_xform_pool(
+            comp_dev, config, config->max_nb_priv_xforms);
+        if (comp_dev->xformpool == NULL)
+            return -ENOMEM;
+    } else
+        comp_dev->xformpool = NULL;
+
+    return ZSDA_SUCCESS;
+}
+
+static int
+zsda_comp_dev_start(struct rte_compressdev *dev)
+{
+    struct zsda_comp_dev_private *comp_dev = dev->data->dev_private;
+    int ret;
+
+    ret = zsda_queue_start(comp_dev->zsda_pci_dev->pci_dev);
+
+    if (ret)
+        ZSDA_LOG(ERR, E_START_Q);
+
+    return ret;
+}
+
+static void
+zsda_comp_dev_stop(struct rte_compressdev *dev)
+{
+    struct zsda_comp_dev_private *comp_dev = dev->data->dev_private;
+
+    zsda_queue_stop(comp_dev->zsda_pci_dev->pci_dev);
+}
+
+static uint16_t
+zsda_comp_max_nb_qps(void)
+{
+    uint16_t comp = zsda_nb_qps.encomp;
+    uint16_t decomp = zsda_nb_qps.decomp;
+    uint16_t min = 0;
+
+    if ((comp == MAX_QPS_ON_FUNCTION) ||
+        (decomp == MAX_QPS_ON_FUNCTION))
+        min = MAX_QPS_ON_FUNCTION;
+    else
+        min = (comp < decomp) ? comp : decomp;
+    if (min == 0)
+        return MAX_QPS_ON_FUNCTION;
+    return min;
+}
+
+
+static void
+zsda_comp_dev_info_get(struct rte_compressdev *dev,
+               struct rte_compressdev_info *info)
+{
+    struct zsda_comp_dev_private *comp_dev = dev->data->dev_private;
+
+    if (info != NULL) {
+        info->max_nb_queue_pairs = zsda_comp_max_nb_qps();
+        info->feature_flags = dev->feature_flags;
+        info->capabilities = comp_dev->zsda_dev_capabilities;
+    }
+}
+
+static struct rte_compressdev_ops compress_zsda_ops = {
+
+    .dev_configure = zsda_comp_dev_config,
+    .dev_start = zsda_comp_dev_start,
+    .dev_stop = zsda_comp_dev_stop,
+    .dev_close = zsda_comp_dev_close,
+    .dev_infos_get = zsda_comp_dev_info_get,
+
+    .stats_get = zsda_comp_stats_get,
+    .stats_reset = zsda_comp_stats_reset,
+    .queue_pair_setup = zsda_comp_qp_setup,
+    .queue_pair_release = zsda_comp_qp_release,
+
+    .private_xform_create = zsda_comp_private_xform_create,
+    .private_xform_free = zsda_comp_private_xform_free
+};
diff --git a/drivers/compress/zsda/zsda_comp_pmd.h b/drivers/compress/zsda/zsda_comp_pmd.h
new file mode 100644
index 0000000000..f096878d09
--- /dev/null
+++ b/drivers/compress/zsda/zsda_comp_pmd.h
@@ -0,0 +1,30 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2024 ZTE Corporation
+ */
+
+#ifndef _ZSDA_COMP_PMD_H_
+#define _ZSDA_COMP_PMD_H_
+
+#include <rte_compressdev_pmd.h>
+
+/**< ZSDA Compression PMD driver name */
+#define COMPRESSDEV_NAME_ZSDA_PMD compress_zsda
+
+/** private data structure for a ZSDA compression device.
+ * This ZSDA device is a device offering only a compression service,
+ * there can be one of these on each zsda_pci_device (VF).
+ */
+struct zsda_comp_dev_private {
+    struct zsda_pci_device *zsda_pci_dev;
+    /**< The zsda pci device hosting the service */
+    struct rte_compressdev *compressdev;
+    /**< The pointer to this compression device structure */
+    const struct rte_compressdev_capabilities *zsda_dev_capabilities;
+    /* ZSDA device compression capabilities */
+    struct rte_mempool *xformpool;
+    /**< The device's pool for zsda_comp_xforms */
+    const struct rte_memzone *capa_mz;
+    /* Shared memzone for storing capabilities */
+};
+
+#endif /* _ZSDA_COMP_PMD_H_ */
-- 
2.27.0