Add functions of compressdev create and destroy.

Signed-off-by: Hanxiao Li <li.hanxiao@zte.com.cn>
---
 drivers/compress/zsda/zsda_comp_pmd.c | 119 ++++++++++++++++++++++++++
 drivers/compress/zsda/zsda_comp_pmd.h |   4 +
 2 files changed, 123 insertions(+)

diff --git a/drivers/compress/zsda/zsda_comp_pmd.c b/drivers/compress/zsda/zsda_comp_pmd.c
index d651116e3d..78c7ee01da 100644
--- a/drivers/compress/zsda/zsda_comp_pmd.c
+++ b/drivers/compress/zsda/zsda_comp_pmd.c
@@ -343,3 +343,122 @@ static struct rte_compressdev_ops compress_zsda_ops = {
     .private_xform_create = zsda_comp_private_xform_create,
     .private_xform_free = zsda_comp_private_xform_free
 };
+
+/* An rte_driver is needed in the registration of the device with compressdev.
+ * The actual zsda pci's rte_driver can't be used as its name represents
+ * the whole pci device with all services. Think of this as a holder for a name
+ * for the compression part of the pci device.
+ */
+static const char zsda_comp_drv_name[] = RTE_STR(COMPRESSDEV_NAME_ZSDA_PMD);
+static const struct rte_driver compdev_zsda_driver = {
+    .name = zsda_comp_drv_name, .alias = zsda_comp_drv_name};
+
+static uint16_t
+zsda_comp_pmd_enqueue_op_burst(void *qp, struct rte_comp_op **ops,
+                   uint16_t nb_ops)
+{
+    return zsda_enqueue_op_burst((struct zsda_qp *)qp, (void **)ops,
+                     nb_ops);
+}
+
+static uint16_t
+zsda_comp_pmd_dequeue_op_burst(void *qp, struct rte_comp_op **ops,
+                   uint16_t nb_ops)
+{
+    return zsda_dequeue_op_burst((struct zsda_qp *)qp, (void **)ops,
+                     nb_ops);
+}
+
+int
+zsda_comp_dev_create(struct zsda_pci_device *zsda_pci_dev)
+{
+    struct zsda_device_info *dev_info =
+        &zsda_devs[zsda_pci_dev->zsda_dev_id];
+
+    struct rte_compressdev_pmd_init_params init_params = {
+        .name = "",
+        .socket_id = (int)rte_socket_id(),
+    };
+
+    char name[RTE_COMPRESSDEV_NAME_MAX_LEN];
+    char capa_memz_name[RTE_COMPRESSDEV_NAME_MAX_LEN];
+    struct rte_compressdev *compressdev;
+    struct zsda_comp_dev_private *comp_dev;
+    const struct rte_compressdev_capabilities *capabilities;
+    uint16_t capa_size = sizeof(struct rte_compressdev_capabilities);
+
+    snprintf(name, RTE_COMPRESSDEV_NAME_MAX_LEN, "%s_%s",
+         zsda_pci_dev->name, "comp");
+
+    if (rte_eal_process_type() != RTE_PROC_PRIMARY)
+        return 0;
+
+    dev_info->comp_rte_dev.driver = &compdev_zsda_driver;
+    dev_info->comp_rte_dev.numa_node = dev_info->pci_dev->device.numa_node;
+    dev_info->comp_rte_dev.devargs = NULL;
+
+    compressdev = rte_compressdev_pmd_create(
+        name, &(dev_info->comp_rte_dev),
+        sizeof(struct zsda_comp_dev_private), &init_params);
+
+    if (compressdev == NULL)
+        return -ENODEV;
+
+    compressdev->dev_ops = &compress_zsda_ops;
+
+    compressdev->enqueue_burst = zsda_comp_pmd_enqueue_op_burst;
+    compressdev->dequeue_burst = zsda_comp_pmd_dequeue_op_burst;
+
+    compressdev->feature_flags = RTE_COMPDEV_FF_HW_ACCELERATED;
+
+    snprintf(capa_memz_name, RTE_COMPRESSDEV_NAME_MAX_LEN,
+         "ZSDA_COMP_CAPA");
+
+    comp_dev = compressdev->data->dev_private;
+    comp_dev->zsda_pci_dev = zsda_pci_dev;
+    comp_dev->compressdev = compressdev;
+    capabilities = zsda_comp_capabilities;
+
+    comp_dev->capa_mz = rte_memzone_lookup(capa_memz_name);
+    if (comp_dev->capa_mz == NULL) {
+        comp_dev->capa_mz = rte_memzone_reserve(
+            capa_memz_name, capa_size, rte_socket_id(), 0);
+    }
+    if (comp_dev->capa_mz == NULL) {
+        ZSDA_LOG(DEBUG, E_MALLOC);
+        memset(&dev_info->comp_rte_dev, 0,
+               sizeof(dev_info->comp_rte_dev));
+        rte_compressdev_pmd_destroy(compressdev);
+        return -EFAULT;
+    }
+
+    memcpy(comp_dev->capa_mz->addr, capabilities, capa_size);
+    comp_dev->zsda_dev_capabilities = comp_dev->capa_mz->addr;
+
+    zsda_pci_dev->comp_dev = comp_dev;
+
+    return ZSDA_SUCCESS;
+}
+
+int
+zsda_comp_dev_destroy(struct zsda_pci_device *zsda_pci_dev)
+{
+    struct zsda_comp_dev_private *comp_dev;
+
+    if (zsda_pci_dev == NULL)
+        return -ENODEV;
+
+    comp_dev = zsda_pci_dev->comp_dev;
+    if (comp_dev == NULL)
+        return ZSDA_SUCCESS;
+
+    if (rte_eal_process_type() == RTE_PROC_PRIMARY)
+        rte_memzone_free(zsda_pci_dev->comp_dev->capa_mz);
+
+    zsda_comp_dev_close(comp_dev->compressdev);
+
+    rte_compressdev_pmd_destroy(comp_dev->compressdev);
+    zsda_pci_dev->comp_dev = NULL;
+
+    return ZSDA_SUCCESS;
+}
diff --git a/drivers/compress/zsda/zsda_comp_pmd.h b/drivers/compress/zsda/zsda_comp_pmd.h
index f096878d09..da91cf06c3 100644
--- a/drivers/compress/zsda/zsda_comp_pmd.h
+++ b/drivers/compress/zsda/zsda_comp_pmd.h
@@ -27,4 +27,8 @@ struct zsda_comp_dev_private {
     /* Shared memzone for storing capabilities */
 };
 
+int zsda_comp_dev_create(struct zsda_pci_device *zsda_pci_dev);
+
+int zsda_comp_dev_destroy(struct zsda_pci_device *zsda_pci_dev);
+
 #endif /* _ZSDA_COMP_PMD_H_ */
-- 
2.27.0