Use rte_pci_device to map device resources and delete
useless device search processes.
Signed-off-by: Yong Zhang <zhang.yong25@zte.com.cn>
---
drivers/raw/zxdh/meson.build | 2 +-
drivers/raw/zxdh/zxdh_pci.c | 562 ---------------------------------
drivers/raw/zxdh/zxdh_pci.h | 41 ---
drivers/raw/zxdh/zxdh_rawdev.c | 93 +++++-
4 files changed, 78 insertions(+), 620 deletions(-)
delete mode 100644 drivers/raw/zxdh/zxdh_pci.c
delete mode 100644 drivers/raw/zxdh/zxdh_pci.h
diff --git a/drivers/raw/zxdh/meson.build b/drivers/raw/zxdh/meson.build
index c9e5c83f1b..266d3db6d8 100644
--- a/drivers/raw/zxdh/meson.build
+++ b/drivers/raw/zxdh/meson.build
@@ -2,4 +2,4 @@
#Copyright 2024 ZTE Corporation
deps += ['rawdev', 'kvargs', 'mbuf', 'bus_pci']
-sources = files('zxdh_rawdev.c', 'zxdh_pci.c')
+sources = files('zxdh_rawdev.c')
diff --git a/drivers/raw/zxdh/zxdh_pci.c b/drivers/raw/zxdh/zxdh_pci.c
deleted file mode 100644
index 884e39109a..0000000000
--- a/drivers/raw/zxdh/zxdh_pci.c
+++ /dev/null
@@ -1,562 +0,0 @@
-/* SPDX-License-Identifier: BSD-3-Clause
- * Copyright 2024 ZTE Corporation
- */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <getopt.h>
-#include <sys/mman.h>
-#include <fcntl.h>
-#include <errno.h>
-#include <dirent.h>
-#include <unistd.h>
-
-#include "zxdh_rawdev.h"
-#include "zxdh_pci.h"
-
-#define PCI_DEVICES_DIR "/sys/bus/pci/devices"
-
-#define BAR0_IDX (0)
-#define BAR2_IDX (2)
-
-#define IORESOURCE_MEM (0x00000200)
-#define FILE_FMT_NVAL (2)
-
-#define STR_BUFF_LEN (128)
-
-#define BYTES_NO_SWAP (0)
-#define BYTES_SWAP (1)
-
-#define PCI_CMD_OFFSET (0x04)
-#define PCI_CMD_BYTES (2)
-#define PCI_CMD_MSE_BIT (1)
-#define FPGA_VER_OFFSET (0x420)
-#define FPGA_VER_BYTES (4)
-#define BOM_ID_OFFSET (0x424)
-#define BOM_ID_BYTES (1)
-#define FPGA_PR_FLAG_OFFSET (0x425)
-#define FPGA_PR_FLAG_BYTES (1)
-#define BOARD_ID_OFFSET (0x426)
-#define BOARD_ID_BYTES (2)
-#define FPGA_MAKE_TIME_OFFSET (0x428)
-#define FPGA_MAKE_TIME_BYTES (4)
-
-#define PARA_PR_FLAG (0)
-#define PARA_FPGA_VER (1)
-#define PARA_FPGA_MAKE_TIME (2)
-#define PARA_BOARD_ID (3)
-#define PARA_BOM_ID (4)
-#define PARA_PCI_CMD (5)
-
-#define PCI_READ (0)
-#define PCI_WRITE (1)
-
-struct zxdh_pci_dev gdev;
-
-static int
-zxdh_gdma_rw_pci_config(struct zxdh_pci_dev *dev, uint8_t rw, uint offset, uint count, uint8_t *buf)
-{
- int fd = -1;
- uint res = 0;
- int ret = -1;
- char filename[FILE_PATH_LEN] = {0};
-
- snprintf(filename, sizeof(filename), "/proc/bus/pci/%02x/%02x.%d",
- dev->bus, dev->devid, dev->function);
- fd = open(filename, O_RDWR);
- if (fd < 0) {
- snprintf(filename, sizeof(filename), "/proc/bus/pci/%04x:%02x/%02x.%d",
- dev->domain, dev->bus, dev->devid, dev->function);
- fd = open(filename, O_RDWR);
- if (fd < 0) {
- ZXDH_PMD_LOG(ERR, "Failed to open file:%s, fd:%d!", filename, fd);
- return -1;
- }
- }
-
- res = lseek(fd, offset, SEEK_SET);
- if (res != offset) {
- close(fd);
- ZXDH_PMD_LOG(ERR, "Failed to lseek pci, res:%d!", res);
- return -1;
- }
-
- if (rw == PCI_READ)
- ret = read(fd, buf, count);
- else
- ret = write(fd, buf, count);
-
- if (ret < 0) {
- close(fd);
- ZXDH_PMD_LOG(ERR, "Failed to rw pci:%d, ret:%d!", rw, ret);
- return -1;
- }
-
- close(fd);
- return 0;
-}
-
-static int
-zxdh_gdma_cfg_space_read(struct zxdh_pci_dev *dev, uint8_t ParaType, uint *pParaVer)
-{
- int ret = 0;
- uint8_t aRegVal[sizeof(uint)] = {0};
- uint8_t ucLoop = 0;
- uint8_t ucSwap = BYTES_NO_SWAP;
- uint dwRegOffset = 0;
- uint dwRegLen = 0;
-
- if ((dev == NULL) || (pParaVer == NULL)) {
- ZXDH_PMD_LOG(ERR, "Param is invalid!");
- return -EINVAL;
- }
-
- switch (ParaType) {
- case PARA_PR_FLAG:
- dwRegOffset = FPGA_PR_FLAG_OFFSET;
- dwRegLen = FPGA_PR_FLAG_BYTES;
- ucSwap = BYTES_NO_SWAP;
- break;
- case PARA_FPGA_VER:
- dwRegOffset = FPGA_VER_OFFSET;
- dwRegLen = FPGA_VER_BYTES;
- ucSwap = BYTES_NO_SWAP;
- break;
- case PARA_FPGA_MAKE_TIME:
- dwRegOffset = FPGA_MAKE_TIME_OFFSET;
- dwRegLen = FPGA_MAKE_TIME_BYTES;
- ucSwap = BYTES_NO_SWAP;
- break;
- case PARA_BOARD_ID:
- dwRegOffset = BOARD_ID_OFFSET;
- dwRegLen = BOARD_ID_BYTES;
- ucSwap = BYTES_NO_SWAP;
- break;
- case PARA_BOM_ID:
- dwRegOffset = BOM_ID_OFFSET;
- dwRegLen = BOM_ID_BYTES;
- ucSwap = BYTES_NO_SWAP;
- break;
- case PARA_PCI_CMD:
- dwRegOffset = PCI_CMD_OFFSET;
- dwRegLen = PCI_CMD_BYTES;
- ucSwap = BYTES_SWAP;
- break;
- default:
- ZXDH_PMD_LOG(ERR, "ParaType %u not support!", ParaType);
- return -EINVAL;
- }
-
- if (dwRegLen > sizeof(uint)) {
- ZXDH_PMD_LOG(ERR, "dwRegLen %u is invalid", dwRegLen);
- return -1;
- }
-
- *pParaVer = 0;
- ret = zxdh_gdma_rw_pci_config(dev, PCI_READ, dwRegOffset, dwRegLen, aRegVal);
- if (ret != 0) {
- ZXDH_PMD_LOG(ERR, "ParaType %u, zxdh_gdma_rw_pci_config failed!", ParaType);
- return ret;
- }
-
- if (ucSwap == BYTES_SWAP) {
- for (ucLoop = 0; ucLoop < dwRegLen; ucLoop++)
- *pParaVer = (*pParaVer << 8) | aRegVal[dwRegLen-1-ucLoop];
- } else {
- for (ucLoop = 0; ucLoop < dwRegLen; ucLoop++)
- *pParaVer = (*pParaVer << 8) | aRegVal[ucLoop];
- }
-
- return ret;
-}
-
-static int
-zxdh_gdma_cfg_space_write(struct zxdh_pci_dev *dev, uint8_t ParaType, uint *pParaVer)
-{
- int ret = 0;
- uint8_t aRegVal[sizeof(uint)] = {0};
- uint8_t ucLoop = 0;
- uint8_t ucSwap = BYTES_NO_SWAP;
- uint dwRegOffset = 0;
- uint dwRegLen = 0;
-
- if ((dev == NULL) || (pParaVer == NULL)) {
- ZXDH_PMD_LOG(ERR, "Param is invalid");
- return -EINVAL;
- }
-
- if (ParaType != PARA_PCI_CMD) {
- ZXDH_PMD_LOG(ERR, "ParaType %u not support!", ParaType);
- return -EINVAL;
- }
-
- dwRegOffset = PCI_CMD_OFFSET;
- dwRegLen = PCI_CMD_BYTES;
- ucSwap = BYTES_SWAP;
-
- if (dwRegLen > sizeof(uint)) {
- ZXDH_PMD_LOG(ERR, "dwRegLen %u is invalid", dwRegLen);
- return -1;
- }
-
- if (ucSwap == BYTES_SWAP) {
- for (ucLoop = 0; ucLoop < dwRegLen; ucLoop++)
- aRegVal[ucLoop] = (*pParaVer >> 8*ucLoop) & 0xff;
- } else {
- for (ucLoop = 0; ucLoop < dwRegLen; ucLoop++)
- aRegVal[ucLoop] = (*pParaVer >> 8*(dwRegLen-1-ucLoop)) & 0xff;
- }
-
- ret = zxdh_gdma_rw_pci_config(dev, PCI_WRITE, dwRegOffset, dwRegLen, aRegVal);
- if (ret != 0) {
- ZXDH_PMD_LOG(ERR, "ParaType %u, zxdh_gdma_rw_pci_config failed!", ParaType);
- return ret;
- }
-
- return ret;
-}
-
-static int
-zxdh_gdma_str_split(char *string, int stringlen, char **tokens, int maxtokens, char delim)
-{
- int loop = 0;
- int tok = 0;
- int tokstart = 1; /* first token is right at start of string */
-
- if (string == NULL || tokens == NULL) {
- ZXDH_PMD_LOG(ERR, "Param is invalid!");
- return -1;
- }
-
- for (loop = 0; loop < stringlen; loop++) {
- if (string[loop] == '\0' || tok >= maxtokens)
- break;
-
- if (tokstart) {
- tokstart = 0;
- tokens[tok++] = &string[loop];
- }
-
- if (string[loop] == delim) {
- string[loop] = '\0';
- tokstart = 1;
- }
- }
-
- return tok;
-}
-
-static int
-zxdh_gdma_devfs_parse(const char *filename, unsigned long *val)
-{
- FILE *f = NULL;
- char *end = NULL;
- char buf[STR_BUFF_LEN] = {0};
-
- f = fopen(filename, "r");
- if (f == NULL) {
- ZXDH_PMD_LOG(ERR, "Cannot open sysfs %s", filename);
- return -1;
- }
-
- if (fgets(buf, sizeof(buf), f) == NULL) {
- ZXDH_PMD_LOG(ERR, "Cannot read sysfs value %s", filename);
- fclose(f);
- return -1;
- }
-
- *val = strtoul(buf, &end, 0);
- if ((buf[0] == '\0') || (end == NULL) || (*end != '\n')) {
- ZXDH_PMD_LOG(ERR, "Cannot parse sysfs value %s", filename);
- fclose(f);
- return -1;
- }
-
- fclose(f);
- return 0;
-}
-
-static int
-zxdh_gdma_resfs_parse(const char *filename, struct zxdh_pci_dev *dev)
-{
- FILE *fp = NULL;
- char buf[STR_BUFF_LEN] = {0};
- uint8_t loop = 0;
- uint64_t phys_addr = 0;
- uint64_t end_addr = 0;
- uint64_t flags = 0;
- int ret = 0;
- union pci_resource_info {
- struct {
- char *phys_addr;
- char *end_addr;
- char *flags;
- };
- char *ptrs[PCI_RESOURCE_FMT_NVAL];
- } res_info;
-
- fp = fopen(filename, "r");
- if (fp == NULL) {
- ZXDH_PMD_LOG(ERR, "Failed to open file %s", filename);
- return -1;
- }
-
- for (loop = 0; loop < PCI_MAX_RESOURCE; loop++) {
- if (fgets(buf, sizeof(buf), fp) == NULL) {
- ZXDH_PMD_LOG(ERR, "Failed to gets file %s", filename);
- goto err_exit;
- }
-
- ret = zxdh_gdma_str_split(buf, sizeof(buf), res_info.ptrs,
- PCI_RESOURCE_FMT_NVAL, ' ');
- if (ret != PCI_RESOURCE_FMT_NVAL) {
- ZXDH_PMD_LOG(ERR, "file %s:zxdh_gdma_str_split failed!", filename);
- goto err_exit;
- }
- errno = 0;
- phys_addr = strtoull(res_info.phys_addr, NULL, 16);
- end_addr = strtoull(res_info.end_addr, NULL, 16);
- flags = strtoull(res_info.flags, NULL, 16);
-
- if (errno != 0) {
- ZXDH_PMD_LOG(ERR, "file %s:bad resource format!", filename);
- goto err_exit;
- }
-
- if (flags & IORESOURCE_MEM) {
- if (loop == BAR0_IDX) {
- dev->bar_pa[BAR0_IDX] = phys_addr;
- dev->bar_len[BAR0_IDX] = end_addr - phys_addr + 1;
- }
-
- if (loop == BAR2_IDX) {
- dev->bar_pa[BAR2_IDX] = phys_addr;
- dev->bar_len[BAR2_IDX] = end_addr - phys_addr + 1;
- fclose(fp);
- return 0;
- }
- }
- }
-
- ZXDH_PMD_LOG(ERR, "file %s: Not found IO resource memory!", filename);
-
-err_exit:
- fclose(fp);
- return -1;
-}
-
-static int
-zxdh_gdma_pci_addr_parse(const char *buf, int buf_size, struct zxdh_pci_dev *dev)
-{
- char *buf_copy = NULL;
- int ret = 0;
- union splitaddr {
- struct {
- char *domain;
- char *bus;
- char *devid;
- char *function;
- };
- char *str[PCI_FMT_NVAL];
- } splitaddr;
-
- buf_copy = strndup(buf, buf_size);
- if (buf_copy == NULL) {
- ZXDH_PMD_LOG(ERR, "buf %s: strndup failed!", buf);
- return -1;
- }
-
- /* first split on ':' */
- ret = zxdh_gdma_str_split(buf_copy, buf_size, splitaddr.str, PCI_FMT_NVAL, ':');
- if (ret != (PCI_FMT_NVAL - 1)) {
- ZXDH_PMD_LOG(ERR, "buf %s: zxdh_gdma_str_split failed!", buf);
- goto err_exit;
- }
-
- /* final split is on '.' between devid and function */
- splitaddr.function = strchr(splitaddr.devid, '.');
- if (splitaddr.function == NULL) {
- ZXDH_PMD_LOG(ERR, "buf %s: strchr failed!", buf);
- goto err_exit;
- }
- *splitaddr.function++ = '\0';
-
- /* now convert to int values */
- errno = 0;
- dev->domain = (uint16_t)strtoul(splitaddr.domain, NULL, 16);
- dev->bus = (uint8_t)strtoul(splitaddr.bus, NULL, 16);
- dev->devid = (uint8_t)strtoul(splitaddr.devid, NULL, 16);
- dev->function = (uint8_t)strtoul(splitaddr.function, NULL, 10);
- if (errno != 0) {
- ZXDH_PMD_LOG(ERR, "buf %s: bad format!", buf);
- goto err_exit;
- }
- free(buf_copy);
- return 0;
-
-err_exit:
- free(buf_copy);
- return -1;
-}
-
-static int
-zxdh_gdma_pci_dev_mmap(const char *filename, struct zxdh_pci_dev *dev, uint8_t bar_idx)
-{
- int fd = -1;
-
- if (dev->bar_va[bar_idx] == NULL) {
- fd = open(filename, O_RDWR);
- if (fd < 0) {
- ZXDH_PMD_LOG(ERR, "Failed to open file %s", filename);
- return -1;
- }
-
- dev->bar_va[bar_idx] = mmap((void *)dev->bar_pa[bar_idx],
- dev->bar_len[bar_idx],
- PROT_READ | PROT_WRITE,
- MAP_SHARED, fd, 0);
-
- if (dev->bar_va[bar_idx] == MAP_FAILED) {
- ZXDH_PMD_LOG(ERR, "Failed to mmap file %s!", filename);
- goto err_exit;
- }
- close(fd);
- } else
- ZXDH_PMD_LOG(ERR, "BarVirtAddr is not NULL!");
-
- return 0;
-
-err_exit:
- close(fd);
- return -1;
-}
-
-void
-zxdh_gdma_pci_dev_munmap(void)
-{
- if (gdev.bar_va[BAR0_IDX] != NULL) {
- munmap(gdev.bar_va[BAR0_IDX], gdev.bar_len[BAR0_IDX]);
- gdev.bar_va[BAR0_IDX] = NULL;
- }
-
- if (gdev.bar_va[BAR2_IDX] != NULL) {
- munmap(gdev.bar_va[BAR2_IDX], gdev.bar_len[BAR2_IDX]);
- gdev.bar_va[BAR2_IDX] = NULL;
- }
-}
-
-static int
-zxdh_gdma_pci_mse_en(struct zxdh_pci_dev *dev)
-{
- int ret = 0;
- uint RegVal = 0;
-
- ret = zxdh_gdma_cfg_space_read(dev, PARA_PCI_CMD, &RegVal);
- if (ret != 0) {
- ZXDH_PMD_LOG(ERR, "Failed to read %04x:%02x:%02x.%01x pci config space!",
- dev->domain, dev->bus, dev->devid, dev->function);
- return ret;
- }
-
- if ((RegVal & (1 << PCI_CMD_MSE_BIT)) == 0) {
- RegVal = RegVal | (1 << PCI_CMD_MSE_BIT);
-
- ret = zxdh_gdma_cfg_space_write(dev, PARA_PCI_CMD, &RegVal);
- if (ret != 0) {
- ZXDH_PMD_LOG(ERR, "Failed to write %04x:%02x:%02x.%01x pci config space!",
- dev->domain, dev->bus,
- dev->devid, dev->function);
- return ret;
- }
- }
-
- return ret;
-}
-
-int
-zxdh_gdma_pci_scan(void)
-{
- struct dirent *e = NULL;
- DIR *dir = NULL;
- char dirname[FILE_PATH_LEN] = {0};
- char filename[FILE_PATH_LEN] = {0};
- uint16_t vendor_id = 0;
- uint16_t device_id = 0;
- unsigned long tmp = 0;
- bool found = false;
- int ret = 0;
-
- dir = opendir(PCI_DEVICES_DIR);
- if (dir == NULL) {
- ZXDH_PMD_LOG(ERR, "Failed to opendir %s", PCI_DEVICES_DIR);
- return -1;
- }
-
- while ((e = readdir(dir)) != NULL) {
- if (e->d_name[0] == '.')
- continue;
-
- memset(dirname, 0, FILE_PATH_LEN);
- snprintf(dirname, FILE_PATH_LEN, "%s/%s", PCI_DEVICES_DIR, e->d_name);
-
- snprintf(filename, sizeof(filename), "%s/vendor", dirname);
- ret = zxdh_gdma_devfs_parse(filename, &tmp);
- if (ret != 0)
- goto out;
-
- vendor_id = (uint16_t)tmp;
-
- snprintf(filename, sizeof(filename), "%s/device", dirname);
- ret = zxdh_gdma_devfs_parse(filename, &tmp);
- if (ret != 0)
- goto out;
-
- device_id = (uint16_t)tmp;
-
- if ((vendor_id == ZXDH_GDMA_VENDORID) && (device_id == ZXDH_GDMA_DEVICEID)) {
- found = true;
- break;
- }
- }
-
- if (found != true) {
- ZXDH_PMD_LOG(ERR, "Failed to found gdma pci dev");
- ret = -1;
- goto out;
- }
-
- gdev.vendor_id = vendor_id;
- gdev.device_id = device_id;
- memcpy(gdev.d_name, e->d_name, PCI_BUFF_LEN);
- memcpy(gdev.dirname, dirname, FILE_PATH_LEN);
- ZXDH_PMD_LOG(INFO, "Found gdma pci dev %s", e->d_name);
-
- /* Parse pci addr */
- ret = zxdh_gdma_pci_addr_parse(e->d_name, sizeof(e->d_name), &gdev);
- if (ret != 0)
- goto out;
-
- /* Enable MSE */
- ret = zxdh_gdma_pci_mse_en(&gdev);
- if (ret != 0)
- goto out;
-
- /* Get bar0 phyaddr and len */
- snprintf(filename, sizeof(filename), "%s/resource", dirname);
- ret = zxdh_gdma_resfs_parse(filename, &gdev);
- if (ret != 0)
- goto out;
-
- /* Mmap bar0 virtaddr */
- snprintf(filename, sizeof(filename), "%s/resource0", dirname);
- ret = zxdh_gdma_pci_dev_mmap(filename, &gdev, BAR0_IDX);
- if (ret != 0)
- goto out;
-
- ZXDH_PMD_LOG(INFO, "Found pci_scan success");
-
-out:
- closedir(dir);
- return ret;
-}
diff --git a/drivers/raw/zxdh/zxdh_pci.h b/drivers/raw/zxdh/zxdh_pci.h
deleted file mode 100644
index 6ecaaeaa2f..0000000000
--- a/drivers/raw/zxdh/zxdh_pci.h
+++ /dev/null
@@ -1,41 +0,0 @@
-/* SPDX-License-Identifier: BSD-3-Clause
- * Copyright 2024 ZTE Corporation
- */
-
-#ifndef __ZXDH_PCI_H__
-#define __ZXDH_PCI_H__
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-#include <rte_pci.h>
-
-#define FILE_PATH_LEN (100)
-#define PCI_BUFF_LEN (16)
-
-struct zxdh_pci_dev {
- uint16_t vendor_id;
- uint16_t device_id;
- uint16_t domain;
- uint8_t bus;
- uint8_t devid;
- uint8_t function;
- char dirname[FILE_PATH_LEN];
- char d_name[PCI_BUFF_LEN];
- void *bar_va[PCI_MAX_RESOURCE];
- uint64_t bar_pa[PCI_MAX_RESOURCE];
- uint64_t bar_len[PCI_MAX_RESOURCE];
-};
-
-extern struct zxdh_pci_dev gdev;
-
-void zxdh_gdma_pci_dev_munmap(void);
-int zxdh_gdma_pci_scan(void);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* __ZXDH_PCI_H__ */
-
diff --git a/drivers/raw/zxdh/zxdh_rawdev.c b/drivers/raw/zxdh/zxdh_rawdev.c
index 07d2784468..f6f54a74c0 100644
--- a/drivers/raw/zxdh/zxdh_rawdev.c
+++ b/drivers/raw/zxdh/zxdh_rawdev.c
@@ -9,8 +9,9 @@
#include <stdint.h>
#include <inttypes.h>
#include <string.h>
-#include <time.h>
#include <sys/types.h>
+#include <unistd.h>
+#include <fcntl.h>
#include <rte_byteorder.h>
#include <rte_errno.h>
@@ -31,9 +32,9 @@
#include <rte_rawdev_pmd.h>
#include <rte_pci.h>
#include <bus_pci_driver.h>
+#include <rte_eal_paging.h>
#include "zxdh_rawdev.h"
-#include "zxdh_pci.h"
/*
* User define:
@@ -824,6 +825,58 @@ zxdh_gdma_queue_free(struct rte_rawdev *dev, uint16_t queue_id)
return 0;
}
+static int
+zxdh_gdma_map_resource(struct rte_pci_device *dev)
+{
+ int fd = -1;
+ char devname[PATH_MAX];
+ void *mapaddr = NULL;
+ struct rte_pci_addr *loc;
+
+ loc = &dev->addr;
+ snprintf(devname, sizeof(devname), "%s/" PCI_PRI_FMT "/resource0",
+ rte_pci_get_sysfs_path(),
+ loc->domain, loc->bus, loc->devid,
+ loc->function);
+
+ fd = open(devname, O_RDWR);
+ if (fd < 0) {
+ ZXDH_PMD_LOG(ERR, "Cannot open %s: %s", devname, strerror(errno));
+ return -1;
+ }
+
+ /* Map the PCI memory resource of device */
+ mapaddr = rte_mem_map(NULL, (size_t)dev->mem_resource[0].len,
+ RTE_PROT_READ | RTE_PROT_WRITE,
+ RTE_MAP_SHARED, fd, 0);
+ if (mapaddr == NULL) {
+ ZXDH_PMD_LOG(ERR, "cannot map resource(%d, 0x%zx): %s (%p)",
+ fd, (size_t)dev->mem_resource[0].len,
+ rte_strerror(rte_errno), mapaddr);
+ close(fd);
+ return -1;
+ }
+
+ close(fd);
+ dev->mem_resource[0].addr = mapaddr;
+
+ return 0;
+}
+
+static void
+zxdh_gdma_unmap_resource(void *requested_addr, size_t size)
+{
+ if (requested_addr == NULL)
+ return;
+
+ /* Unmap the PCI memory resource of device */
+ if (rte_mem_unmap(requested_addr, size))
+ ZXDH_PMD_LOG(ERR, "cannot mem unmap(%p, %#zx): %s",
+ requested_addr, size, rte_strerror(rte_errno));
+ else
+ ZXDH_PMD_LOG(DEBUG, "PCI memory unmapped at %p", requested_addr);
+}
+
static int
zxdh_gdma_rawdev_probe(struct rte_pci_driver *pci_drv __rte_unused,
struct rte_pci_device *pci_dev)
@@ -832,27 +885,26 @@ zxdh_gdma_rawdev_probe(struct rte_pci_driver *pci_drv __rte_unused,
struct zxdh_gdma_rawdev *gdmadev = NULL;
struct zxdh_gdma_queue *queue = NULL;
uint8_t i = 0;
+ int ret;
- if (zxdh_gdma_pci_scan() != 0) {
- ZXDH_PMD_LOG(ERR, "Failed to scan gdma pci device!");
+ if (pci_dev->mem_resource[0].phys_addr == 0) {
+ ZXDH_PMD_LOG(ERR, "PCI bar0 resource is invalid");
return -1;
}
- if ((gdev.bar_pa[0]) == 0) {
- ZXDH_PMD_LOG(ERR, "Empty bars 0x%"PRIx64,
- (uint64_t)gdev.bar_pa[0]);
- zxdh_gdma_pci_dev_munmap();
- return -ENODEV;
+ ret = zxdh_gdma_map_resource(pci_dev);
+ if (ret != 0) {
+ ZXDH_PMD_LOG(ERR, "Failed to mmap pci device(%s)", pci_dev->name);
+ return -1;
}
- ZXDH_PMD_LOG(INFO, "%04x:%02x:%02x.%01x Bar0 PhyAddr: 0x%"PRIx64,
- gdev.domain, gdev.bus, gdev.devid, gdev.function,
- (uint64_t)gdev.bar_pa[0]);
+ ZXDH_PMD_LOG(INFO, "%s bar0 0x%"PRIx64" mapped at %p",
+ pci_dev->name, pci_dev->mem_resource[0].phys_addr,
+ pci_dev->mem_resource[0].addr);
dev = rte_rawdev_pmd_allocate(dev_name, sizeof(struct zxdh_gdma_rawdev), rte_socket_id());
if (dev == NULL) {
ZXDH_PMD_LOG(ERR, "Unable to allocate gdma rawdev");
- zxdh_gdma_pci_dev_munmap();
- return -1;
+ goto err_out;
}
ZXDH_PMD_LOG(INFO, "Init %s on NUMA node %d, dev_id is %d",
dev_name, rte_socket_id(), dev->dev_id);
@@ -865,7 +917,7 @@ zxdh_gdma_rawdev_probe(struct rte_pci_driver *pci_drv __rte_unused,
gdmadev->rawdev = dev;
gdmadev->queue_num = ZXDH_GDMA_TOTAL_CHAN_NUM;
gdmadev->used_num = 0;
- gdmadev->base_addr = (uintptr_t)gdev.bar_va[0] + ZXDH_GDMA_BASE_OFFSET;
+ gdmadev->base_addr = (uintptr_t)pci_dev->mem_resource[0].addr + ZXDH_GDMA_BASE_OFFSET;
for (i = 0; i < ZXDH_GDMA_TOTAL_CHAN_NUM; i++) {
queue = &(gdmadev->vqs[i]);
@@ -875,10 +927,15 @@ zxdh_gdma_rawdev_probe(struct rte_pci_driver *pci_drv __rte_unused,
}
return 0;
+
+err_out:
+ zxdh_gdma_unmap_resource(pci_dev->mem_resource[0].addr,
+ (size_t)pci_dev->mem_resource[0].len);
+ return -1;
}
static int
-zxdh_gdma_rawdev_remove(__rte_unused struct rte_pci_device *pci_dev)
+zxdh_gdma_rawdev_remove(struct rte_pci_device *pci_dev)
{
struct rte_rawdev *dev = NULL;
int ret = 0;
@@ -893,6 +950,10 @@ zxdh_gdma_rawdev_remove(__rte_unused struct rte_pci_device *pci_dev)
ZXDH_PMD_LOG(ERR, "Device cleanup failed");
return -1;
}
+
+ zxdh_gdma_unmap_resource(pci_dev->mem_resource[0].addr,
+ (size_t)pci_dev->mem_resource[0].len);
+
ZXDH_PMD_LOG(DEBUG, "rawdev %s remove done!", dev_name);
return ret;
--
2.43.0