From: Jie Liu <[email protected]>

Implement DMA mapping and unmapping functionality using ioctl
calls. This allows the driver to configure the hardware's IOMMU/DMA
tables, ensuring the device can safely access memory buffers
allocated by the userspace.

The mapping is established during device initialization or queue
setup and is revoked during device closure to prevent memory
leaks and ensure hardware security.

Signed-off-by: Jie Liu <[email protected]>
---
 drivers/common/sxe2/sxe2_common.c          |  50 +++++++++-
 drivers/common/sxe2/sxe2_ioctl_chnl.c      | 108 ++++++++++++++++++++-
 drivers/common/sxe2/sxe2_ioctl_chnl_func.h |   9 ++
 3 files changed, 164 insertions(+), 3 deletions(-)

diff --git a/drivers/common/sxe2/sxe2_common.c 
b/drivers/common/sxe2/sxe2_common.c
index 7d4001343a..e04982e92f 100644
--- a/drivers/common/sxe2/sxe2_common.c
+++ b/drivers/common/sxe2/sxe2_common.c
@@ -443,7 +443,7 @@ static s32 sxe2_common_pci_remove(struct rte_pci_device 
*pci_dev)
        cdev = sxe2_rtedev_to_cdev(&pci_dev->device);
        if (cdev == NULL) {
                ret = -ENODEV;
-               PMD_LOG_ERR(COM, "Fail to get remove device.");
+               PMD_LOG_ERR(COM, "Fail to get device when remove.");
                goto l_end;
        }
 
@@ -467,12 +467,60 @@ static s32 sxe2_common_pci_remove(struct rte_pci_device 
*pci_dev)
        return ret;
 }
 
+static s32 sxe2_common_pci_dma_map(struct rte_pci_device *pci_dev,
+               void *addr,     u64 iova, size_t len)
+{
+       struct sxe2_common_device *cdev;
+       s32 ret = SXE2_ERROR;
+
+       cdev = sxe2_rtedev_to_cdev(&pci_dev->device);
+       if (cdev == NULL) {
+               ret = -ENODEV;
+               PMD_LOG_ERR(COM, "Fail to get device when dma map.");
+               goto l_end;
+       }
+
+       ret = sxe2_drv_dev_dma_map(cdev, (u64)(uintptr_t)addr, iova, len);
+       if (ret) {
+               PMD_LOG_ERR(COM, "Fail to map dma map, ret=%d", ret);
+               goto l_end;
+       }
+
+l_end:
+       return ret;
+}
+
+static s32 sxe2_common_pci_dma_unmap(struct rte_pci_device *pci_dev,
+               void *addr __rte_unused, u64 iova, size_t len __rte_unused)
+{
+       struct sxe2_common_device *cdev;
+       s32 ret = SXE2_ERROR;
+
+       cdev = sxe2_rtedev_to_cdev(&pci_dev->device);
+       if (cdev == NULL) {
+               ret = -ENODEV;
+               PMD_LOG_ERR(COM, "Fail to get device when dma unmap.");
+               goto l_end;
+       }
+
+       ret = sxe2_drv_dev_dma_unmap(cdev, iova);
+       if (ret) {
+               PMD_LOG_ERR(COM, "Fail to unmap dma map, ret=%d", ret);
+               goto l_end;
+       }
+
+l_end:
+       return ret;
+}
+
 static struct rte_pci_driver sxe2_common_pci_driver = {
        .driver = {
                   .name = SXE2_COMMON_PCI_DRIVER_NAME,
        },
        .probe = sxe2_common_pci_probe,
        .remove = sxe2_common_pci_remove,
+       .dma_map = sxe2_common_pci_dma_map,
+       .dma_unmap = sxe2_common_pci_dma_unmap,
 };
 
 static u32 sxe2_common_pci_id_table_size_get(const struct rte_pci_id *id_table)
diff --git a/drivers/common/sxe2/sxe2_ioctl_chnl.c 
b/drivers/common/sxe2/sxe2_ioctl_chnl.c
index 8c55f5098f..4dfc4fd0fa 100644
--- a/drivers/common/sxe2/sxe2_ioctl_chnl.c
+++ b/drivers/common/sxe2/sxe2_ioctl_chnl.c
@@ -168,7 +168,7 @@ void
        void *virt = NULL;
 
        if (cdev->config.kernel_reset) {
-               PMD_LOG_WARN(COM, "kernel reseted, need restart app.");
+               PMD_LOG_WARN(COM, "kernel reset, need restart app.");
                goto l_err;
        }
 
@@ -202,7 +202,7 @@ sxe2_drv_dev_munmap(struct sxe2_common_device *cdev, void 
*virt, u64 len)
 
        if (cdev->config.kernel_reset) {
                ret = -EPERM;
-               PMD_LOG_WARN(COM, "kernel reseted, need restart app.");
+               PMD_LOG_WARN(COM, "kernel reset, need restart app.");
                goto l_end;
        }
 
@@ -220,3 +220,107 @@ sxe2_drv_dev_munmap(struct sxe2_common_device *cdev, void 
*virt, u64 len)
 l_end:
        return ret;
 }
+
+RTE_EXPORT_INTERNAL_SYMBOL(sxe2_drv_dev_dma_map)
+s32
+sxe2_drv_dev_dma_map(struct sxe2_common_device *cdev, u64 vaddr,
+                       u64 iova, u64 size)
+{
+       struct sxe2_ioctl_iommu_dma_map cmd_params;
+       enum rte_iova_mode iova_mode;
+       s32 ret = SXE2_SUCCESS;
+       s32 cmd_fd = 0;
+
+       if (cdev->config.kernel_reset) {
+               ret = -EPERM;
+               PMD_LOG_WARN(COM, "kernel reset, need restart app.");
+               goto l_end;
+       }
+
+       iova_mode = rte_eal_iova_mode();
+       if (iova_mode == RTE_IOVA_PA) {
+               if (cdev->config.support_iommu) {
+                       PMD_LOG_ERR(COM, "iommu not support pa mode");
+                       ret = -EIO;
+               }
+               goto l_end;
+       } else if (iova_mode == RTE_IOVA_VA) {
+               if (!cdev->config.support_iommu) {
+                       PMD_LOG_ERR(COM, "no iommu not support va mode, please 
use pa mode.");
+                       ret = -EIO;
+                       goto l_end;
+               }
+       }
+
+       cmd_fd = SXE2_CDEV_TO_CMD_FD(cdev);
+       if (cmd_fd < 0) {
+               ret = -EBADF;
+               PMD_LOG_ERR(COM, "Failed to exec cmd, fd=%d", cmd_fd);
+               goto l_end;
+       }
+
+       memset(&cmd_params, 0, sizeof(struct sxe2_ioctl_iommu_dma_map));
+       cmd_params.vaddr = vaddr;
+       cmd_params.iova = iova;
+       cmd_params.size = size;
+
+       rte_ticketlock_lock(&cdev->config.lock);
+       ret = ioctl(cmd_fd, SXE2_COM_CMD_DMA_MAP, &cmd_params);
+       if (ret < 0) {
+               PMD_LOG_ERR(COM, "Failed to dma map, fd=%d, ret=%d, err:%s",
+                               cmd_fd, ret, strerror(errno));
+               ret = -EIO;
+               rte_ticketlock_unlock(&cdev->config.lock);
+               goto l_end;
+       }
+       rte_ticketlock_unlock(&cdev->config.lock);
+
+l_end:
+       return ret;
+}
+
+RTE_EXPORT_INTERNAL_SYMBOL(sxe2_drv_dev_dma_unmap)
+s32
+sxe2_drv_dev_dma_unmap(struct sxe2_common_device *cdev, u64 iova)
+{
+       s32 ret = SXE2_SUCCESS;
+       s32 cmd_fd = 0;
+       struct sxe2_ioctl_iommu_dma_unmap cmd_params;
+
+       if (cdev->config.kernel_reset) {
+               ret = -EPERM;
+               PMD_LOG_WARN(COM, "kernel reset, need restart app.");
+               goto l_end;
+       }
+
+       if (!cdev->config.support_iommu)
+               goto l_end;
+
+       cmd_fd = SXE2_CDEV_TO_CMD_FD(cdev);
+       if (cmd_fd < 0) {
+               ret = -EBADF;
+               PMD_LOG_ERR(COM, "Failed to exec cmd, fd=%d", cmd_fd);
+               goto l_end;
+       }
+
+       PMD_LOG_DEBUG(COM, "fd %d dma unmap iova=0x%"PRIX64"",
+               cmd_fd, iova);
+
+       memset(&cmd_params, 0, sizeof(struct sxe2_ioctl_iommu_dma_unmap));
+       cmd_params.iova = iova;
+
+       rte_ticketlock_lock(&cdev->config.lock);
+       ret = ioctl(cmd_fd, SXE2_COM_CMD_DMA_UNMAP, &cmd_params);
+       if (ret < 0) {
+               PMD_LOG_INFO(COM, "Failed to dma unmap, fd=%d, ret=%d, err:%s",
+                               cmd_fd, ret, strerror(errno));
+               ret = -EIO;
+               rte_ticketlock_unlock(&cdev->config.lock);
+               goto l_end;
+       }
+       rte_ticketlock_unlock(&cdev->config.lock);
+
+l_end:
+       return ret;
+}
+
diff --git a/drivers/common/sxe2/sxe2_ioctl_chnl_func.h 
b/drivers/common/sxe2/sxe2_ioctl_chnl_func.h
index 376c5e3ac7..e8f983e40e 100644
--- a/drivers/common/sxe2/sxe2_ioctl_chnl_func.h
+++ b/drivers/common/sxe2/sxe2_ioctl_chnl_func.h
@@ -47,6 +47,15 @@ __rte_internal
 s32
 sxe2_drv_dev_munmap(struct sxe2_common_device *cdev, void *virt, u64 len);
 
+__rte_internal
+s32
+sxe2_drv_dev_dma_map(struct sxe2_common_device *cdev, u64 vaddr,
+               u64 iova, u64 size);
+
+__rte_internal
+s32
+sxe2_drv_dev_dma_unmap(struct sxe2_common_device *cdev, u64 iova);
+
 #ifdef __cplusplus
 }
 #endif
-- 
2.47.3

Reply via email to