Skip to contentSkip to conversation

bus/pci: fix legacy device IO port map in secondary process

PATCH Superseded Wenwu Ma 8 participants 29 messages Aug 7, 2023 – Mar 6, 2024

lore
Share & tools
lore

Wenwu Ma

author
#0 lore raw

PATCH 1/1 bus/pci: fix legacy device IO port map in secondary process

When doing IO port mapping for legacy device
in secondary process, the region information
is missing, so, we need to refill it.

Fixes 4b741542ecde ("bus/pci: avoid depending on private kernel value")
---
 drivers/bus/pci/linux/pci_vfio.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
drivers/bus/pci/linux/pci_vfio.c +21 -0
index e634de8322..eea1c9851e 100644
@@ -1306,6 +1306,11 @@ int
 pci_vfio_ioport_map(struct rte_pci_device *dev, int bar,
 		    struct rte_pci_ioport *p)
 {
+	struct vfio_device_info device_info = { .argsz = sizeof(device_info) };
+	char pci_addr[PATH_MAX] = {0};
+	int vfio_dev_fd;
+	struct rte_pci_addr *loc = &dev->addr;
+	int ret;
 	uint64_t size, offset;
 
 	if (bar < VFIO_PCI_BAR0_REGION_INDEX ||
@@ -1314,6 +1319,22 @@ pci_vfio_ioport_map(struct rte_pci_device *dev, int bar,
 		return -1;
 	}
 
+	if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
+		/* store PCI address string */
+		snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
+				loc->domain, loc->bus, loc->devid, loc->function);
+
+		ret = rte_vfio_setup_device(rte_pci_get_sysfs_path(), pci_addr,
+						&vfio_dev_fd, &device_info);
+		if (ret)
+			return -1;
+
+		ret = pci_vfio_fill_regions(dev, vfio_dev_fd, &device_info);
+		if (ret)
+			return -1;
+
+	}
+
 	if (pci_vfio_get_region(dev, bar, &size, &offset) != 0) {
 		RTE_LOG(ERR, EAL, "Cannot get offset of region %d.\n", bar);
 		return -1;
Signature · 1 line
2.25.1
#1 lore raw

Hi Wenwu,

Quoted 29 lines · Wenwu Ma

When doing IO port mapping for legacy device
in secondary process, the region information
is missing, so, we need to refill it.

Fixes 4b741542ecde ("bus/pci: avoid depending on private kernel value")
---
  drivers/bus/pci/linux/pci_vfio.c | 21 +++++++++++++++++++++
  1 file changed, 21 insertions(+)
drivers/bus/pci/linux/pci_vfio.c +5 -0
index e634de8322..eea1c9851e 100644
@@ -1306,6 +1306,11 @@ int
  pci_vfio_ioport_map(struct rte_pci_device *dev, int bar,
  		    struct rte_pci_ioport *p)
  {
+	struct vfio_device_info device_info = { .argsz = sizeof(device_info) };
+	char pci_addr[PATH_MAX] = {0};
+	int vfio_dev_fd;
+	struct rte_pci_addr *loc = &dev->addr;
+	int ret;
  	uint64_t size, offset;
 
  	if (bar < VFIO_PCI_BAR0_REGION_INDEX ||

drivers/bus/pci/linux/pci_vfio.c pci_vfio_ioport_map(struct rte_pci_device *dev, int bar, · line 1328

Quoted 10 lines
drivers/bus/pci/linux/pci_vfio.c +7 -0
@@ -1314,6 +1319,22 @@ pci_vfio_ioport_map(struct rte_pci_device *dev, int bar,
  		return -1;
  	}
 
+	if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
+		/* store PCI address string */
+		snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
+				loc->domain, loc->bus, loc->devid, loc->function);
+
+		ret = rte_vfio_setup_device(rte_pci_get_sysfs_path(), pci_addr,
+						&vfio_dev_fd, &device_info);

This looks better than fixing in "virtio_remap_pci".

Ideally, these API should be called irrespective of PRIMARY or SECONDARY process here. Miao Li mentioned earlier that "rte_pci_map_device" is called from primary process, is it via "virtio_read_caps" API? Isn't there any other way to detect if it is a virtio legacy device without calling "rte_pci_map_device"?

drivers/bus/pci/linux/pci_vfio.c pci_vfio_ioport_map(struct rte_pci_device *dev, int bar, · line 1334

Quoted 6 lines
drivers/bus/pci/linux/pci_vfio.c +6 -0
+		if (ret)
+			return -1;
+
+		ret = pci_vfio_fill_regions(dev, vfio_dev_fd, &device_info);
+		if (ret)
+			return -1;

Corresponding cleanup required in "pci_vfio_ioport_unmap"?

Thanks,
Nipun

Quoted 6 lines
drivers/bus/pci/linux/pci_vfio.c +3 -0
+
+	}
+
  	if (pci_vfio_get_region(dev, bar, &size, &offset) != 0) {
  		RTE_LOG(ERR, EAL, "Cannot get offset of region %d.\n", bar);
  		return -1;
author
#2 lore raw

Hi,

Quoted 59 lines

-----Original Message-----
From: Gupta, Nipun <[email protected]>
Sent: 2023年8月13日 14:15
To: Ma, WenwuX <[email protected]>; [email protected]
Cc: [email protected]; [email protected]; Xia, Chenbo <[email protected]>; Li, Miao <[email protected]>; Ling, WeiX <[email protected]>; [email protected]
Subject: Re: [PATCH] bus/pci: fix legacy device IO port map in secondary process

Hi Wenwu,

Quoted 19 lines · Wenwu Ma

When doing IO port mapping for legacy device in secondary process, the region information is missing, so, we need to refill it.

Fixes 4b741542ecde ("bus/pci: avoid depending on private kernel

value")
Cc: [email protected]

---
  drivers/bus/pci/linux/pci_vfio.c | 21 +++++++++++++++++++++
  1 file changed, 21 insertions(+)
drivers/bus/pci/linux/pci_vfio.c +0 -0
index e634de8322..eea1c9851e 100644
@@ -1306,6 +1306,11 @@ int
  pci_vfio_ioport_map(struct rte_pci_device *dev, int bar,

drivers/bus/pci/linux/pci_vfio.c int · line 1316

Quoted 10 lines
drivers/bus/pci/linux/pci_vfio.c +5 -0
  		    struct rte_pci_ioport *p)
  {
+	struct vfio_device_info device_info = { .argsz = sizeof(device_info) };
+	char pci_addr[PATH_MAX] = {0};
+	int vfio_dev_fd;
+	struct rte_pci_addr *loc = &dev->addr;
+	int ret;
  	uint64_t size, offset;
 
  	if (bar < VFIO_PCI_BAR0_REGION_INDEX || @@ -1314,6 +1319,22

@@

drivers/bus/pci/linux/pci_vfio.c

Quoted 11 lines

pci_vfio_ioport_map(struct rte_pci_device *dev, int bar,
return -1;
}

drivers/bus/pci/linux/pci_vfio.c +4 -0
+	if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
+		/* store PCI address string */
+		snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
+				loc->domain, loc->bus, loc->devid, loc-

function);

drivers/bus/pci/linux/pci_vfio.c +2 -0
+
+		ret = rte_vfio_setup_device(rte_pci_get_sysfs_path(),

pci_addr,

drivers/bus/pci/linux/pci_vfio.c pci_vfio_ioport_map(struct rte_pci_device *dev, int bar, · line 1328

Quoted 1 line
drivers/bus/pci/linux/pci_vfio.c +1 -0
+						&vfio_dev_fd, &device_info);

This looks better than fixing in "virtio_remap_pci".

Quoted 6 lines

Ideally, these API should be called irrespective of PRIMARY or SECONDARY process here. Miao Li mentioned earlier that "rte_pci_map_device" is called from primary process, is it via "virtio_read_caps" API? Isn't there any other way to detect if it is a virtio legacy device without calling "rte_pci_map_device"?

1. yes, it is via "virtio_read_caps".

2. if (pdev->region[bar].size == 0 && pdev->region[bar].offset == 0) Do you think it's better this way?

Quoted 10 lines

drivers/bus/pci/linux/pci_vfio.c pci_vfio_ioport_map(struct rte_pci_device *dev, int bar, · line 1334

Quoted 7 lines
drivers/bus/pci/linux/pci_vfio.c +6 -0
+		if (ret)
+			return -1;
+
+		ret = pci_vfio_fill_regions(dev, vfio_dev_fd, &device_info);
+		if (ret)
+			return -1;

Corresponding cleanup required in "pci_vfio_ioport_unmap"?

Yes, we need release group fd, I will add it.

Thanks.

Quoted 9 lines

Thanks,
Nipun

Quoted 6 lines
drivers/bus/pci/linux/pci_vfio.c +3 -0
+
+	}
+
  	if (pci_vfio_get_region(dev, bar, &size, &offset) != 0) {
  		RTE_LOG(ERR, EAL, "Cannot get offset of region %d.\n", bar);
  		return -1;
author
#3 lore raw

PATCH v2 1/1 bus/pci: fix legacy device IO port map in secondary process

In reply to Wenwu Ma

When doing IO port mapping for legacy device
in secondary process, the region information
is missing, so, we need to refill it.

Fixes 4b741542ecde ("bus/pci: avoid depending on private kernel value")

---
v2: add release of device in pci_vfio_ioport_unmap

---
 drivers/bus/pci/linux/pci_vfio.c | 43 ++++++++++++++++++++++++++++++--
 1 file changed, 41 insertions(+), 2 deletions(-)
drivers/bus/pci/linux/pci_vfio.c +41 -2
index e634de8322..f2cc55c431 100644
@@ -1306,6 +1306,11 @@ int
 pci_vfio_ioport_map(struct rte_pci_device *dev, int bar,
 		    struct rte_pci_ioport *p)
 {
+	struct vfio_device_info device_info = { .argsz = sizeof(device_info) };
+	char pci_addr[PATH_MAX] = {0};
+	int vfio_dev_fd;
+	struct rte_pci_addr *loc = &dev->addr;
+	int ret;
 	uint64_t size, offset;
 
 	if (bar < VFIO_PCI_BAR0_REGION_INDEX ||
@@ -1314,6 +1319,22 @@ pci_vfio_ioport_map(struct rte_pci_device *dev, int bar,
 		return -1;
 	}
 
+	if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
+		/* store PCI address string */
+		snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
+				loc->domain, loc->bus, loc->devid, loc->function);
+
+		ret = rte_vfio_setup_device(rte_pci_get_sysfs_path(), pci_addr,
+						&vfio_dev_fd, &device_info);
+		if (ret)
+			return -1;
+
+		ret = pci_vfio_fill_regions(dev, vfio_dev_fd, &device_info);
+		if (ret)
+			return -1;
+
+	}
+
 	if (pci_vfio_get_region(dev, bar, &size, &offset) != 0) {
 		RTE_LOG(ERR, EAL, "Cannot get offset of region %d.\n", bar);
 		return -1;
@@ -1361,8 +1382,26 @@ pci_vfio_ioport_write(struct rte_pci_ioport *p,
 int
 pci_vfio_ioport_unmap(struct rte_pci_ioport *p)
 {
-	RTE_SET_USED(p);
-	return -1;
+	char pci_addr[PATH_MAX] = {0};
+	struct rte_pci_addr *loc = &p->dev->addr;
+	int ret, vfio_dev_fd;
+
+	/* store PCI address string */
+	snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
+			loc->domain, loc->bus, loc->devid, loc->function);
+
+	vfio_dev_fd = rte_intr_dev_fd_get(p->dev->intr_handle);
+	if (vfio_dev_fd < 0)
+		return -1;
+
+	ret = rte_vfio_release_device(rte_pci_get_sysfs_path(), pci_addr,
+				      vfio_dev_fd);
+	if (ret < 0) {
+		RTE_LOG(ERR, EAL, "Cannot release VFIO device\n");
+		return ret;
+	}
+
+	return 0;
 }
 
 int
Signature · 1 line
2.25.1
#4 lore raw

Review

Quoted 2 lines · On Mon, 21 Aug 2023 09:27:07 +0800 Wenwu Ma <[email protected]> wrote:
(quoted diff) +2 -0
+	struct vfio_device_info device_info = { .argsz = sizeof(device_info) };
+	char pci_addr[PATH_MAX] = {0}

Not sure if some tools will complain about initializing chars as zero. Anyway, why bother since you are using it with snprintf.

Also, the new variables that are only used in the secondary case should be declared in that if() not for whole function.

author
#5 lore raw

Hi,

Quoted 14 lines

-----Original Message-----
From: Stephen Hemminger <[email protected]>
Sent: 2023年8月21日 10:53
To: Ma, WenwuX <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; Xia, Chenbo <[email protected]>; Li, Miao <[email protected]>; Ling, WeiX <[email protected]>; [email protected]
Subject: Re: [PATCH v2] bus/pci: fix legacy device IO port map in secondary process

On Mon, 21 Aug 2023 09:27:07 +0800
Wenwu Ma <[email protected]> wrote:

Quoted 10 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+	struct vfio_device_info device_info = { .argsz = sizeof(device_info) };
+	char pci_addr[PATH_MAX] = {0}

Not sure if some tools will complain about initializing chars as zero. Anyway, why bother since you are using it with snprintf.

Also, the new variables that are only used in the secondary case should be declared in that if() not for whole function.

Ok, thanks

author
#6 lore raw

PATCH v3 1/1 bus/pci: fix legacy device IO port map in secondary process

In reply to Wenwu Ma

When doing IO port mapping for legacy device
in secondary process, the region information
is missing, so, we need to refill it.

Fixes 4b741542ecde ("bus/pci: avoid depending on private kernel value")

---
v3:
- adjusting variable settings
v2:
- add release of device in pci_vfio_ioport_unmap

---
 drivers/bus/pci/linux/pci_vfio.c | 43 ++++++++++++++++++++++++++++++--
 1 file changed, 41 insertions(+), 2 deletions(-)
drivers/bus/pci/linux/pci_vfio.c +41 -2
index e634de8322..5ef26c98d1 100644
@@ -1314,6 +1314,27 @@ pci_vfio_ioport_map(struct rte_pci_device *dev, int bar,
 		return -1;
 	}
 
+	if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
+		struct vfio_device_info device_info = { .argsz = sizeof(device_info) };
+		char pci_addr[PATH_MAX];
+		int vfio_dev_fd;
+		struct rte_pci_addr *loc = &dev->addr;
+		int ret;
+		/* store PCI address string */
+		snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
+				loc->domain, loc->bus, loc->devid, loc->function);
+
+		ret = rte_vfio_setup_device(rte_pci_get_sysfs_path(), pci_addr,
+						&vfio_dev_fd, &device_info);
+		if (ret)
+			return -1;
+
+		ret = pci_vfio_fill_regions(dev, vfio_dev_fd, &device_info);
+		if (ret)
+			return -1;
+
+	}
+
 	if (pci_vfio_get_region(dev, bar, &size, &offset) != 0) {
 		RTE_LOG(ERR, EAL, "Cannot get offset of region %d.\n", bar);
 		return -1;
@@ -1361,8 +1382,26 @@ pci_vfio_ioport_write(struct rte_pci_ioport *p,
 int
 pci_vfio_ioport_unmap(struct rte_pci_ioport *p)
 {
-	RTE_SET_USED(p);
-	return -1;
+	char pci_addr[PATH_MAX] = {0};
+	struct rte_pci_addr *loc = &p->dev->addr;
+	int ret, vfio_dev_fd;
+
+	/* store PCI address string */
+	snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
+			loc->domain, loc->bus, loc->devid, loc->function);
+
+	vfio_dev_fd = rte_intr_dev_fd_get(p->dev->intr_handle);
+	if (vfio_dev_fd < 0)
+		return -1;
+
+	ret = rte_vfio_release_device(rte_pci_get_sysfs_path(), pci_addr,
+				      vfio_dev_fd);
+	if (ret < 0) {
+		RTE_LOG(ERR, EAL, "Cannot release VFIO device\n");
+		return ret;
+	}
+
+	return 0;
 }
 
 int
Signature · 1 line
2.25.1
#7 lore raw

Hi Wenwu

Quoted 3 lines · Wenwu Ma

When doing IO port mapping for legacy device
in secondary process, the region information
is missing, so, we need to refill it.

Please use 72 columns in the commit log

Quoted 20 lines
Fixes 4b741542ecde ("bus/pci: avoid depending on private kernel value")

---
v3:
- adjusting variable settings
v2:
- add release of device in pci_vfio_ioport_unmap

---
  drivers/bus/pci/linux/pci_vfio.c | 43 ++++++++++++++++++++++++++++++--
  1 file changed, 41 insertions(+), 2 deletions(-)
drivers/bus/pci/linux/pci_vfio.c +0 -0
index e634de8322..5ef26c98d1 100644

drivers/bus/pci/linux/pci_vfio.c pci_vfio_ioport_map(struct rte_pci_device *dev, int bar, · line 1317

Quoted 4 lines
drivers/bus/pci/linux/pci_vfio.c +1 -0
@@ -1314,6 +1314,27 @@ pci_vfio_ioport_map(struct rte_pci_device *dev, int bar,
  		return -1;
  	}
 
+	if (rte_eal_process_type() == RTE_PROC_SECONDARY) {

One thing I am not fully convinced is that why VFIO setup is not required for primary process here?

Thanks,
Nipun

author
#8 lore raw
Quoted 12 lines

-----Original Message-----
From: Gupta, Nipun <[email protected]>
Sent: 2023年8月28日 14:07
To: Ma, WenwuX <[email protected]>; [email protected]
Cc: [email protected]; [email protected]; Xia, Chenbo <[email protected]>; Li, Miao <[email protected]>; Ling, WeiX <[email protected]>; [email protected]
Subject: Re: [PATCH v3] bus/pci: fix legacy device IO port map in secondary process

Quoted 8 lines

Hi Wenwu

Quoted 2 lines · Wenwu Ma

When doing IO port mapping for legacy device in secondary process, the region information is missing, so, we need to refill it.

Please use 72 columns in the commit log

Ok

Quoted 24 lines
Quoted 6 lines
Fixes 4b741542ecde ("bus/pci: avoid depending on private kernel

value")
Cc: [email protected]

Quoted 8 lines

---
v3:
- adjusting variable settings
v2:
- add release of device in pci_vfio_ioport_unmap

---
  drivers/bus/pci/linux/pci_vfio.c | 43

++++++++++++++++++++++++++++++--

drivers/bus/pci/linux/pci_vfio.c pci_vfio_ioport_map(struct rte_pci_device *dev,

Quoted 8 lines
  1 file changed, 41 insertions(+), 2 deletions(-)
drivers/bus/pci/linux/pci_vfio.c +0 -0
index e634de8322..5ef26c98d1 100644
@@ -1314,6 +1314,27 @@ pci_vfio_ioport_map(struct rte_pci_device *dev,

int bar,

Quoted 8 lines

drivers/bus/pci/linux/pci_vfio.c pci_vfio_ioport_map(struct rte_pci_device *dev, int bar, · line 1317

Quoted 4 lines
drivers/bus/pci/linux/pci_vfio.c +1 -0
  		return -1;
  	}
 
+	if (rte_eal_process_type() == RTE_PROC_SECONDARY) {

One thing I am not fully convinced is that why VFIO setup is not required for primary process here?

In primary process, if virtio is not modern mode, it will call rte_pci_map_device and pci_vfio_ioport_map, VFIO setup will be called in rte_pci_map_device, so it cannot be called in pci_vfio_ioport_map again.

pci_vfio_ioport_map will not be called if the virtio is modern mode.

Quoted 2 lines

Thanks,
Nipun

author
#9 lore raw

PATCH v4 1/1 bus/pci: fix legacy device IO port map in secondary process

In reply to Wenwu Ma

When doing IO port mapping for legacy device in secondary process, the region information is missing, so, we need to refill it.

Fixes 4b741542ecde ("bus/pci: avoid depending on private kernel value")

---
v4:
- adjusting commit log
v3:
- adjusting variable settings
v2:
- add release of device in pci_vfio_ioport_unmap

---
 drivers/bus/pci/linux/pci_vfio.c | 43 ++++++++++++++++++++++++++++++--
 1 file changed, 41 insertions(+), 2 deletions(-)
drivers/bus/pci/linux/pci_vfio.c +41 -2
index e634de8322..5ef26c98d1 100644
@@ -1314,6 +1314,27 @@ pci_vfio_ioport_map(struct rte_pci_device *dev, int bar,
 		return -1;
 	}
 
+	if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
+		struct vfio_device_info device_info = { .argsz = sizeof(device_info) };
+		char pci_addr[PATH_MAX];
+		int vfio_dev_fd;
+		struct rte_pci_addr *loc = &dev->addr;
+		int ret;
+		/* store PCI address string */
+		snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
+				loc->domain, loc->bus, loc->devid, loc->function);
+
+		ret = rte_vfio_setup_device(rte_pci_get_sysfs_path(), pci_addr,
+						&vfio_dev_fd, &device_info);
+		if (ret)
+			return -1;
+
+		ret = pci_vfio_fill_regions(dev, vfio_dev_fd, &device_info);
+		if (ret)
+			return -1;
+
+	}
+
 	if (pci_vfio_get_region(dev, bar, &size, &offset) != 0) {
 		RTE_LOG(ERR, EAL, "Cannot get offset of region %d.\n", bar);
 		return -1;
@@ -1361,8 +1382,26 @@ pci_vfio_ioport_write(struct rte_pci_ioport *p,
 int
 pci_vfio_ioport_unmap(struct rte_pci_ioport *p)
 {
-	RTE_SET_USED(p);
-	return -1;
+	char pci_addr[PATH_MAX] = {0};
+	struct rte_pci_addr *loc = &p->dev->addr;
+	int ret, vfio_dev_fd;
+
+	/* store PCI address string */
+	snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
+			loc->domain, loc->bus, loc->devid, loc->function);
+
+	vfio_dev_fd = rte_intr_dev_fd_get(p->dev->intr_handle);
+	if (vfio_dev_fd < 0)
+		return -1;
+
+	ret = rte_vfio_release_device(rte_pci_get_sysfs_path(), pci_addr,
+				      vfio_dev_fd);
+	if (ret < 0) {
+		RTE_LOG(ERR, EAL, "Cannot release VFIO device\n");
+		return ret;
+	}
+
+	return 0;
 }
 
 int
Signature · 1 line
2.34.1
Tested-by on v4 1/1
#11 lore raw
In reply to Wenwu Ma
Quoted 11 lines

-----Original Message-----
From: Ma, WenwuX <[email protected]>
Sent: Wednesday, August 30, 2023 1:07 PM
To: [email protected]; [email protected]
Cc: [email protected]; [email protected]; Xia, Chenbo <[email protected]>; Li, Miao <[email protected]>; Ling, WeiX <[email protected]>; Ma, WenwuX <[email protected]>; [email protected]
Subject: [PATCH v4] bus/pci: fix legacy device IO port map in secondary process

Quoted 7 lines

When doing IO port mapping for legacy device in secondary process, the region information is missing, so, we need to refill it.

Fixes 4b741542ecde ("bus/pci: avoid depending on private kernel value")
#12 lore raw
In reply to Wenwu Ma
Quoted 23 lines · Wenwu Ma

When doing IO port mapping for legacy device in secondary process, the region information is missing, so, we need to refill it.

Fixes 4b741542ecde ("bus/pci: avoid depending on private kernel value")
---
 drivers/bus/pci/linux/pci_vfio.c | 43 ++++++++++++++++++++++++++++++--
 1 file changed, 41 insertions(+), 2 deletions(-)
drivers/bus/pci/linux/pci_vfio.c +2 -0
index e634de8322..5ef26c98d1 100644
@@ -1314,6 +1314,27 @@ pci_vfio_ioport_map(struct rte_pci_device *dev, int bar,
                return -1;
        }
 
+       if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
+               struct vfio_device_info device_info = { .argsz = sizeof(device_info) };

drivers/bus/pci/linux/pci_vfio.c pci_vfio_ioport_map(struct rte_pci_device *dev, int bar, · line 1328

Quoted 10 lines
drivers/bus/pci/linux/pci_vfio.c +10 -0
+               char pci_addr[PATH_MAX];
+               int vfio_dev_fd;
+               struct rte_pci_addr *loc = &dev->addr;
+               int ret;
+               /* store PCI address string */
+               snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
+                               loc->domain, loc->bus, loc->devid, loc->function);
+
+               ret = rte_vfio_setup_device(rte_pci_get_sysfs_path(), pci_addr,
+                                               &vfio_dev_fd, &device_info);

From a pci bus API pov, nothing prevents a driver from mixing memory mapped with vfio and ioport resources (iow, calls to rte_pci_map_resource() and rte_pci_ioport_map()).
IOW, it may not be the case with the net/virtio driver but, in theory, rte_pci_ioport_map()/pci_vfio_ioport_map() may be called after a rte_pci_map_resource() call.

In a similar manner, from the API pov, rte_pci_ioport_map()/pci_vfio_ioport_map() may be called for multiple bars.

In summary, nothing in this patch checks that vfio has been configured already and I think we need a refcount to handle those situations.

Nipun, Chenbo, WDYT?

drivers/bus/pci/linux/pci_vfio.c pci_vfio_ioport_map(struct rte_pci_device *dev, int bar, · line 1330

Quoted 2 lines
drivers/bus/pci/linux/pci_vfio.c +2 -0
+               if (ret)
+                       return -1;

ret value is not used, so there is no need for this variable.

if (rte_vfio_setup_device(rte_pci_get_sysfs_path(), pci_addr,

        &vfio_dev_fd, &device_info) != 0)
    return -1;

drivers/bus/pci/linux/pci_vfio.c pci_vfio_ioport_map(struct rte_pci_device *dev, int bar, · line 1334

Quoted 4 lines
drivers/bus/pci/linux/pci_vfio.c +4 -0
+
+               ret = pci_vfio_fill_regions(dev, vfio_dev_fd, &device_info);
+               if (ret)
+                       return -1;

Same here, ret is not needed.

Quoted 13 lines
drivers/bus/pci/linux/pci_vfio.c +4 -2
+
+       }
+
        if (pci_vfio_get_region(dev, bar, &size, &offset) != 0) {
                RTE_LOG(ERR, EAL, "Cannot get offset of region %d.\n", bar);
                return -1;
@@ -1361,8 +1382,26 @@ pci_vfio_ioport_write(struct rte_pci_ioport *p,
 int
 pci_vfio_ioport_unmap(struct rte_pci_ioport *p)
 {
-       RTE_SET_USED(p);
-       return -1;
+       char pci_addr[PATH_MAX] = {0};

drivers/bus/pci/linux/pci_vfio.c pci_vfio_ioport_write(struct rte_pci_ioport *p, · line 1395

Quoted 10 lines
drivers/bus/pci/linux/pci_vfio.c +10 -0
+       struct rte_pci_addr *loc = &p->dev->addr;
+       int ret, vfio_dev_fd;
+
+       /* store PCI address string */
+       snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
+                       loc->domain, loc->bus, loc->devid, loc->function);
+
+       vfio_dev_fd = rte_intr_dev_fd_get(p->dev->intr_handle);
+       if (vfio_dev_fd < 0)
+               return -1;

This check is odd and does not seem related.

Quoted 7 lines
drivers/bus/pci/linux/pci_vfio.c +7 -0
+
+       ret = rte_vfio_release_device(rte_pci_get_sysfs_path(), pci_addr,
+                                     vfio_dev_fd);
+       if (ret < 0) {
+               RTE_LOG(ERR, EAL, "Cannot release VFIO device\n");
+               return ret;
+       }
Signature · 1 line
David Marchand
author
#13 lore raw

Hi,

Quoted 59 lines

-----Original Message-----
From: David Marchand <[email protected]>
Sent: 2023年10月3日 18:21
To: Ma, WenwuX <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; [email protected]; Li, Miao <[email protected]>; Ling, WeiX <[email protected]>; [email protected]
Subject: Re: [PATCH v4] bus/pci: fix legacy device IO port map in secondary process

drivers/bus/pci/linux/pci_vfio.c pci_vfio_ioport_map(struct rte_pci_device

Quoted 21 lines · Wenwu Ma

When doing IO port mapping for legacy device in secondary process, the region information is missing, so, we need to refill it.

Fixes 4b741542ecde ("bus/pci: avoid depending on private kernel

value")
Cc: [email protected]

---
 drivers/bus/pci/linux/pci_vfio.c | 43
++++++++++++++++++++++++++++++--
 1 file changed, 41 insertions(+), 2 deletions(-)
drivers/bus/pci/linux/pci_vfio.c +0 -0
index e634de8322..5ef26c98d1 100644
@@ -1314,6 +1314,27 @@ pci_vfio_ioport_map(struct rte_pci_device

*dev, int bar,

Quoted 7 lines
drivers/bus/pci/linux/pci_vfio.c +4 -0
                return -1;
        }
 
+       if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
+               struct vfio_device_info device_info = { .argsz = sizeof(device_info) };
+               char pci_addr[PATH_MAX];
+               int vfio_dev_fd;

drivers/bus/pci/linux/pci_vfio.c pci_vfio_ioport_map(struct rte_pci_device *dev, int bar,

Quoted 10 lines
drivers/bus/pci/linux/pci_vfio.c +10 -0
+               struct rte_pci_addr *loc = &dev->addr;
+               int ret;
+               /* store PCI address string */
+               snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
+                               loc->domain, loc->bus, loc->devid,
+ loc->function);
+
+               ret = rte_vfio_setup_device(rte_pci_get_sysfs_path(), pci_addr,
+                                               &vfio_dev_fd,
+ &device_info);

From a pci bus API pov, nothing prevents a driver from mixing memory mapped with vfio and ioport resources (iow, calls to rte_pci_map_resource() and rte_pci_ioport_map()).
IOW, it may not be the case with the net/virtio driver but, in theory, rte_pci_ioport_map()/pci_vfio_ioport_map() may be called after a rte_pci_map_resource() call.

Quoted 6 lines

In a similar manner, from the API pov, rte_pci_ioport_map()/pci_vfio_ioport_map() may be called for multiple bars.

In summary, nothing in this patch checks that vfio has been configured already and I think we need a refcount to handle those situations.

We call rte_vfio_setup_device just to get device info, we can call rte_vfio_release_device as soon as pci_vfio_fill_regions is done. This avoids reference counting operations, do you think it works?

Quoted 57 lines

Nipun, Chenbo, WDYT?

drivers/bus/pci/linux/pci_vfio.c pci_vfio_ioport_map(struct rte_pci_device · line 1332

Quoted 2 lines
drivers/bus/pci/linux/pci_vfio.c +2 -0
+               if (ret)
+                       return -1;

ret value is not used, so there is no need for this variable.

if (rte_vfio_setup_device(rte_pci_get_sysfs_path(), pci_addr,

        &vfio_dev_fd, &device_info) != 0)
    return -1;

drivers/bus/pci/linux/pci_vfio.c pci_vfio_ioport_map(struct rte_pci_device *dev, int bar, · line 1334

Quoted 4 lines
drivers/bus/pci/linux/pci_vfio.c +4 -0
+
+               ret = pci_vfio_fill_regions(dev, vfio_dev_fd, &device_info);
+               if (ret)
+                       return -1;

Same here, ret is not needed.

Quoted 12 lines
drivers/bus/pci/linux/pci_vfio.c +5 -2
+
+       }
+
        if (pci_vfio_get_region(dev, bar, &size, &offset) != 0) {
                RTE_LOG(ERR, EAL, "Cannot get offset of region %d.\n", bar);
                return -1;
@@ -1361,8 +1382,26 @@ pci_vfio_ioport_write(struct rte_pci_ioport *p,
 int  pci_vfio_ioport_unmap(struct rte_pci_ioport *p)  {
-       RTE_SET_USED(p);
-       return -1;
+       char pci_addr[PATH_MAX] = {0};
+       struct rte_pci_addr *loc = &p->dev->addr;

drivers/bus/pci/linux/pci_vfio.c pci_vfio_ioport_write(struct rte_pci_ioport *p, · line 1394

Quoted 10 lines
drivers/bus/pci/linux/pci_vfio.c +10 -0
+       int ret, vfio_dev_fd;
+
+       /* store PCI address string */
+       snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
+                       loc->domain, loc->bus, loc->devid,
+ loc->function);
+
+       vfio_dev_fd = rte_intr_dev_fd_get(p->dev->intr_handle);
+       if (vfio_dev_fd < 0)
+               return -1;

This check is odd and does not seem related.

drivers/bus/pci/linux/pci_vfio.c pci_vfio_ioport_write(struct rte_pci_ioport *p, · line 1402

Quoted 7 lines
drivers/bus/pci/linux/pci_vfio.c +7 -0
+
+       ret = rte_vfio_release_device(rte_pci_get_sysfs_path(), pci_addr,
+                                     vfio_dev_fd);
+       if (ret < 0) {
+               RTE_LOG(ERR, EAL, "Cannot release VFIO device\n");
+               return ret;
+       }

--
David Marchand

#14 lore raw
Quoted 7 lines · WenwuX
Quoted 7 lines

From a pci bus API pov, nothing prevents a driver from mixing memory mapped with vfio and ioport resources (iow, calls to rte_pci_map_resource() and rte_pci_ioport_map()).
IOW, it may not be the case with the net/virtio driver but, in theory, rte_pci_ioport_map()/pci_vfio_ioport_map() may be called after a rte_pci_map_resource() call.

Quoted 8 lines
Quoted 6 lines

In a similar manner, from the API pov, rte_pci_ioport_map()/pci_vfio_ioport_map() may be called for multiple bars.

In summary, nothing in this patch checks that vfio has been configured already and I think we need a refcount to handle those situations.

We call rte_vfio_setup_device just to get device info, we can call rte_vfio_release_device as soon as pci_vfio_fill_regions is done. This avoids reference counting operations, do you think it works?

Afaics, rte_vfio_setup_device should not be called if a call to rte_pci_map_device for this device was successful (rte_pci_map_device itself calls rte_vfio_setup_device).
And as a consequence, calling rte_vfio_release_device cannot be done unconditionnally neither.

Signature · 1 line
David Marchand
#15 lore raw
Quoted 14 lines · David Marchand
Quoted 13 lines · WenwuX
Quoted 13 lines

From a pci bus API pov, nothing prevents a driver from mixing memory
mapped with vfio and ioport resources (iow, calls to rte_pci_map_resource() and rte_pci_ioport_map()).
IOW, it may not be the case with the net/virtio driver but, in theory, rte_pci_ioport_map()/pci_vfio_ioport_map() may be called after a rte_pci_map_resource() call.

In a similar manner, from the API pov, rte_pci_ioport_map()/pci_vfio_ioport_map() may be called for multiple bars.

In summary, nothing in this patch checks that vfio has been configured already and I think we need a refcount to handle those situations.

Quoted 8 lines
Quoted 2 lines

We call rte_vfio_setup_device just to get device info, we can call rte_vfio_release_device as soon as pci_vfio_fill_regions is done. This avoids reference counting operations, do you think it works?

Afaics, rte_vfio_setup_device should not be called if a call to rte_pci_map_device for this device was successful (rte_pci_map_device itself calls rte_vfio_setup_device).
And as a consequence, calling rte_vfio_release_device cannot be done unconditionnally neither.

Hi David,

AFAIU, rte_vfio_setup_device() is written as re-entrant and does not create the DMA mapping again if it is already done for the iommu group.

When this API is called again either for a device within the same group or from the device for which it is already called, it mainly only does the work for device info get. Though not the best thing to use like this, but if this is called multiple times it should not have any negative impact.

As Wenmu mention that they need only device info from VFIO, a separate API to get device info can be added in eal_vfio.c/h. The device info portion of rte_vfio_setup_device() can be moved out to a new API, and rte_vfio_setup_device() can call this new API too?

Thanks,
Nipun

#16 lore raw
Quoted 26 lines · Nipun
Quoted 14 lines · David Marchand
Quoted 13 lines · WenwuX
Quoted 13 lines

From a pci bus API pov, nothing prevents a driver from mixing memory
mapped with vfio and ioport resources (iow, calls to rte_pci_map_resource() and rte_pci_ioport_map()).
IOW, it may not be the case with the net/virtio driver but, in theory, rte_pci_ioport_map()/pci_vfio_ioport_map() may be called after a rte_pci_map_resource() call.

In a similar manner, from the API pov, rte_pci_ioport_map()/pci_vfio_ioport_map() may be called for multiple bars.

In summary, nothing in this patch checks that vfio has been configured already and I think we need a refcount to handle those situations.

Quoted 8 lines
Quoted 2 lines

We call rte_vfio_setup_device just to get device info, we can call rte_vfio_release_device as soon as pci_vfio_fill_regions is done. This avoids reference counting operations, do you think it works?

Afaics, rte_vfio_setup_device should not be called if a call to rte_pci_map_device for this device was successful (rte_pci_map_device itself calls rte_vfio_setup_device).
And as a consequence, calling rte_vfio_release_device cannot be done unconditionnally neither.

Hi David,

Quoted 8 lines

AFAIU, c() is written as re-entrant and does not
create the DMA mapping again if it is already done for the iommu group.

When this API is called again either for a device within the same group or from the device for which it is already called, it mainly only does the work for device info get. Though not the best thing to use like this, but if this is called multiple times it should not have any negative impact.

Even if rte_vfio_setup_device() is reentrant, there is still the question when to call rte_vfio_release_device().

Quoted 5 lines

As Wenmu mention that they need only device info from VFIO, a separate API to get device info can be added in eal_vfio.c/h. The device info portion of rte_vfio_setup_device() can be moved out to a new API, and rte_vfio_setup_device() can call this new API too?

Ok, I think I understand your suggestion.

Do we have a reference to the vfio device fd stored somewhere in the pci device object?
I don't think it is the case, but if the pci layer keeps a reference to it (it would be populated/reset during rte_pci_map_device/rte_pci_unmap_device), then the ioport code can call the VFIO_DEVICE_GET_INFO ioctl() similarly to what is done for irq msi info, and there is no need for a new EAL api.

For the case when this device fd is not available (no previous call to rte_pci_map_device()), then the ioport code can call rte_vfio_setup_device() / rte_vfio_release_device().

Is this what you have in mind?

Signature · 1 line
David Marchand
author
#17 lore raw

PATCH v5 1/1 bus/pci: fix legacy device IO port map in secondary process

In reply to Wenwu Ma

When doing IO port mapping for legacy device in secondary process, the region information is missing, so, we need to refill it.

Fixes 4b741542ecde ("bus/pci: avoid depending on private kernel value")

---
v5:
- adding checks to vfio setup
v4:
- adjusting commit log
v3:
- adjusting variable settings
v2:
- add release of device in pci_vfio_ioport_unmap

---
 drivers/bus/pci/linux/pci_vfio.c | 52 ++++++++++++++++++++++++++++++--
 1 file changed, 50 insertions(+), 2 deletions(-)
drivers/bus/pci/linux/pci_vfio.c +50 -2
index 3f3201daf2..a1f7cc2421 100644
@@ -1230,6 +1230,36 @@ pci_vfio_ioport_map(struct rte_pci_device *dev, int bar,
 		return -1;
 	}
 
+	if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
+		struct vfio_device_info device_info = { .argsz = sizeof(device_info) };
+		char pci_addr[PATH_MAX];
+		int vfio_dev_fd;
+		struct rte_pci_addr *loc = &dev->addr;
+		/* store PCI address string */
+		snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
+				loc->domain, loc->bus, loc->devid, loc->function);
+
+		vfio_dev_fd = rte_intr_dev_fd_get(dev->intr_handle);
+		if (vfio_dev_fd < 0)
+			return -1;
+
+		if (vfio_dev_fd == 0) {
+			if (rte_vfio_setup_device(rte_pci_get_sysfs_path(), pci_addr,
+					&vfio_dev_fd, &device_info))
+				return -1;
+
+			/* we need save vfio_dev_fd, so it can be used during release */
+			if (rte_intr_dev_fd_set(dev->intr_handle, vfio_dev_fd))
+				return -1;
+		} else {
+			if (ioctl(vfio_dev_fd, VFIO_DEVICE_GET_INFO, &device_info))
+				return -1;
+		}
+
+		if (pci_vfio_fill_regions(dev, vfio_dev_fd, &device_info))
+			return -1;
+	}
+
 	if (pci_vfio_get_region(dev, bar, &size, &offset) != 0) {
 		RTE_LOG(ERR, EAL, "Cannot get offset of region %d.\n", bar);
 		return -1;
@@ -1277,8 +1307,26 @@ pci_vfio_ioport_write(struct rte_pci_ioport *p,
 int
 pci_vfio_ioport_unmap(struct rte_pci_ioport *p)
 {
-	RTE_SET_USED(p);
-	return -1;
+	char pci_addr[PATH_MAX] = {0};
+	struct rte_pci_addr *loc = &p->dev->addr;
+	int ret, vfio_dev_fd;
+
+	/* store PCI address string */
+	snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
+			loc->domain, loc->bus, loc->devid, loc->function);
+
+	vfio_dev_fd = rte_intr_dev_fd_get(p->dev->intr_handle);
+	if (vfio_dev_fd < 0)
+		return -1;
+
+	ret = rte_vfio_release_device(rte_pci_get_sysfs_path(), pci_addr,
+				      vfio_dev_fd);
+	if (ret < 0) {
+		RTE_LOG(ERR, EAL, "Cannot release VFIO device\n");
+		return ret;
+	}
+
+	return 0;
 }
 
 int
Signature · 1 line
2.34.1
#18 lore raw

PATCH v6 1/1 bus/pci: fix legacy device IO port map

When doing IO port mapping for legacy device in secondary process, the region information is missing, so, we need to refill it.

Fixes 4b741542ecde ("bus/pci: avoid depending on private kernel value")

---
v6:
- split patch
v5:
- adding checks to vfio setup
v4:
- adjusting commit log
v3:
- adjusting variable settings
v2:
- add release of device in pci_vfio_ioport_unmap

---
 drivers/bus/pci/linux/pci_vfio.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
drivers/bus/pci/linux/pci_vfio.c +26 -0
index 3f3201daf2..a18161c27b 100644
@@ -1230,6 +1230,32 @@ pci_vfio_ioport_map(struct rte_pci_device *dev, int bar,
 		return -1;
 	}
 
+	if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
+		struct vfio_device_info device_info = { .argsz = sizeof(device_info) };
+		char pci_addr[PATH_MAX];
+		int vfio_dev_fd;
+		struct rte_pci_addr *loc = &dev->addr;
+
+		/* store PCI address string */
+		snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
+				loc->domain, loc->bus, loc->devid, loc->function);
+
+		vfio_dev_fd = rte_intr_dev_fd_get(dev->intr_handle);
+		if (vfio_dev_fd < 0) {
+			return -1;
+		} else if (vfio_dev_fd == 0) {
+			if (rte_vfio_get_device_info(rte_pci_get_sysfs_path(), pci_addr,
+				&vfio_dev_fd, &device_info) != 0)
+				return -1;
+			/* we need save vfio_dev_fd, so it can be used during release */
+			if (rte_intr_dev_fd_set(dev->intr_handle, vfio_dev_fd))
+				return -1;
+
+			if (pci_vfio_fill_regions(dev, vfio_dev_fd, &device_info) != 0)
+				return -1;
+		}
+	}
+
 	if (pci_vfio_get_region(dev, bar, &size, &offset) != 0) {
 		RTE_LOG(ERR, EAL, "Cannot get offset of region %d.\n", bar);
 		return -1;
Signature · 1 line
2.25.1
Acked-by on v6 1/1
#19 lore raw

Re: [PATCH v6] bus/pci: fix legacy device IO port map

Quoted 27 lines · Mingjin Ye

When doing IO port mapping for legacy device in secondary process, the region information is missing, so, we need to refill it.

Fixes 4b741542ecde ("bus/pci: avoid depending on private kernel value")

---
v6:
- split patch
v5:
- adding checks to vfio setup
v4:
- adjusting commit log
v3:
- adjusting variable settings
v2:
- add release of device in pci_vfio_ioport_unmap

---
  drivers/bus/pci/linux/pci_vfio.c | 26 ++++++++++++++++++++++++++
  1 file changed, 26 insertions(+)
drivers/bus/pci/linux/pci_vfio.c +0 -0
index 3f3201daf2..a18161c27b 100644

drivers/bus/pci/linux/pci_vfio.c pci_vfio_ioport_map(struct rte_pci_device *dev, int bar, · line 1233

Quoted 4 lines
drivers/bus/pci/linux/pci_vfio.c +1 -0
@@ -1230,6 +1230,32 @@ pci_vfio_ioport_map(struct rte_pci_device *dev, int bar,
  		return -1;
  	}
 
+	if (rte_eal_process_type() == RTE_PROC_SECONDARY) {

Please add a comment why we are doing this in secondary only. With this change/comment incorporated

#20 lore raw

v7 cover fix legacy device missing region info

In reply to Mingjin Ye

Fill in missing region information for legacy devices in secondary processes. And added rte_vfio_get_device_info API for EAL, which makes it easier and more generic to get device information through vfio driver.

Mingjin Ye (2):
vfio: add get device info API
bus/pci: fix legacy device missing region info

 drivers/bus/pci/linux/pci_vfio.c | 26 ++++++++++++++++++++++++++
 lib/eal/include/rte_vfio.h       | 29 +++++++++++++++++++++++++++++
 lib/eal/linux/eal_vfio.c         | 27 +++++++++++++++++++++++++++
 lib/eal/version.map              |  3 +++
 4 files changed, 85 insertions(+)
Signature · 1 line
2.25.1
#21 lore raw

PATCH v7 1/2 vfio: add get device info API

This patch adds an API to support getting device information.

The driver can use the "rte_vfio_get_device_info" helper to get device information from EAL.

---
 lib/eal/include/rte_vfio.h | 29 +++++++++++++++++++++++++++++
 lib/eal/linux/eal_vfio.c   | 27 +++++++++++++++++++++++++++
 lib/eal/version.map        |  3 +++
 3 files changed, 59 insertions(+)
lib/eal/include/rte_vfio.h +29 -0
index 22832afd0f..50c4f5b617 100644
@@ -17,6 +17,8 @@ extern "C" {
 #include <stdbool.h>
 #include <stdint.h>
 
+#include <rte_compat.h>
+
 /*
  * determine if VFIO is present on the system
  */
@@ -239,6 +241,33 @@ int
 rte_vfio_get_group_num(const char *sysfs_base,
 		      const char *dev_addr, int *iommu_group_num);
 
+/**
+ * Get device information
+ *
+ * This function is only relevant to linux and will return
+ * an error on BSD.
+ *
+ * @param sysfs_base
+ *   sysfs path prefix.
+ *
+ * @param dev_addr
+ *   device location.
+ *
+ * @param vfio_dev_fd
+ *   VFIO fd.
+ *
+ * @param device_info
+ *   Device information.
+ *
+ * @return
+ *   0 on success.
+ *   <0 on failure.
+ */
+__rte_experimental
+int
+rte_vfio_get_device_info(const char *sysfs_base, const char *dev_addr,
+			int *vfio_dev_fd, struct vfio_device_info *device_info);
+
 /**
  * Open a new VFIO container fd
  *
lib/eal/linux/eal_vfio.c +27 -0
index ad3c1654b2..05a0091006 100644
@@ -1222,6 +1222,33 @@ vfio_set_iommu_type(int vfio_container_fd)
 	return NULL;
 }
 
+int
+rte_vfio_get_device_info(const char *sysfs_base, const char *dev_addr,
+			int *vfio_dev_fd, struct vfio_device_info *device_info)
+{
+	int ret;
+
+	if (!device_info || *vfio_dev_fd < 0)
+		return -1;
+
+	if (*vfio_dev_fd == 0) {
+		ret = rte_vfio_setup_device(sysfs_base, dev_addr,
+				vfio_dev_fd, device_info);
+		if (ret)
+			return -1;
+	} else {
+		ret = ioctl(*vfio_dev_fd, VFIO_DEVICE_GET_INFO, device_info);
+		if (ret) {
+			RTE_LOG(ERR, EAL, "%s cannot get device info, "
+					"error %i (%s)\n", dev_addr, errno,
+					strerror(errno));
+			return -1;
+		}
+	}
+
+	return 0;
+}
+
 int
 vfio_has_supported_extensions(int vfio_container_fd)
 {
lib/eal/version.map +3 -0
index 5e0cd47c82..c06ceaad50 100644
@@ -393,6 +393,9 @@ EXPERIMENTAL {
 	# added in 23.07
 	rte_memzone_max_get;
 	rte_memzone_max_set;
+
+	# added in 23.11
+	rte_vfio_get_device_info; # WINDOWS_NO_EXPORT
 };
 
 INTERNAL {
Signature · 1 line
2.25.1
Acked-by on v7 1/2
#22 lore raw

RE: [PATCH v7 1/2] vfio: add get device info API

[AMD Official Use Only - General]

Quoted 8 lines

-----Original Message-----
From: Mingjin Ye <[email protected]>
Sent: Wednesday, November 22, 2023 3:53 PM
To: [email protected]
Cc: [email protected]; Mingjin Ye <[email protected]>; Anatoly Burakov <[email protected]>
Subject: [PATCH v7 1/2] vfio: add get device info API

Quoted 6 lines

This patch adds an API to support getting device information.

The driver can use the "rte_vfio_get_device_info" helper to get device information from EAL.

#23 lore raw

Re: [PATCH v7 1/2] vfio: add get device info API

In reply to Mingjin Ye
Quoted 9 lines · Mingjin Ye

External email: Use caution opening links or attachments

This patch adds an API to support getting device information.

The driver can use the "rte_vfio_get_device_info" helper to get device information from EAL.

Quoted 7 lines

---

lib/eal/include/rte_vfio.h | 29 +++++++++++++++++++++++++++++
lib/eal/linux/eal_vfio.c   | 27 +++++++++++++++++++++++++++
lib/eal/version.map        |  3 +++
3 files changed, 59 insertions(+)

Comments not addressed:

https://mails.dpdk.org/archives/dev/2023-November/282200.html

/Chenbo

#24 lore raw

RE: [PATCH v7 1/2] vfio: add get device info API

Quoted 28 lines

-----Original Message-----
From: Chenbo Xia <[email protected]>
Sent: Wednesday, November 29, 2023 9:48 AM
To: Ye, MingjinX <[email protected]>
Cc: [email protected]; Yang, Qiming <[email protected]>; Burakov, Anatoly <[email protected]>
Subject: Re: [PATCH v7 1/2] vfio: add get device info API

Quoted 9 lines · Mingjin Ye

External email: Use caution opening links or attachments

This patch adds an API to support getting device information.

The driver can use the "rte_vfio_get_device_info" helper to get device information from EAL.

Quoted 7 lines

---

lib/eal/include/rte_vfio.h | 29 +++++++++++++++++++++++++++++
lib/eal/linux/eal_vfio.c   | 27 +++++++++++++++++++++++++++
lib/eal/version.map        |  3 +++
3 files changed, 59 insertions(+)
Quoted 3 lines

Comments not addressed:

https://mails.dpdk.org/archives/dev/2023-November/282200.html

Sorry for not responding in time.

Experimental API, no CC stable.

This API is used essentially the same as the rte_vfio_setup_device API.
Reference: https://patchwork.dpdk.org/project/dpdk/patch/[email protected]/

/Mingjin

#25 lore raw

PATCH v7 2/2 bus/pci: fix legacy device missing region info

In reply to Mingjin Ye

If the legacy device in the primary process does not support mapping the io port bar, the secondary process needs to proactively get the device information and fil the region information.

Fixes 4b741542ecde ("bus/pci: avoid depending on private kernel value")

---
v7: Modify commit log
---
v6:
- split patch
v5:
- adding checks to vfio setup
v4:
- adjusting commit log
v3:
- adjusting variable settings
v2:
- add release of device in pci_vfio_ioport_unmap

---
 drivers/bus/pci/linux/pci_vfio.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
drivers/bus/pci/linux/pci_vfio.c +26 -0
index 3f3201daf2..ee4cfb2598 100644
@@ -1230,6 +1230,32 @@ pci_vfio_ioport_map(struct rte_pci_device *dev, int bar,
 		return -1;
 	}
 
+	if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
+		struct vfio_device_info device_info = { .argsz = sizeof(device_info) };
+		char pci_addr[PATH_MAX];
+		int vfio_dev_fd;
+		struct rte_pci_addr *loc = &dev->addr;
+
+		/* store PCI address string */
+		snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
+				loc->domain, loc->bus, loc->devid, loc->function);
+
+		vfio_dev_fd = rte_intr_dev_fd_get(dev->intr_handle);
+		if (vfio_dev_fd < 0) {
+			return -1;
+		} else if (vfio_dev_fd == 0) {
+			if (rte_vfio_get_device_info(rte_pci_get_sysfs_path(), pci_addr,
+				&vfio_dev_fd, &device_info) != 0)
+				return -1;
+			/* we need save vfio_dev_fd, so it can be used during release */
+			if (rte_intr_dev_fd_set(dev->intr_handle, vfio_dev_fd) != 0)
+				return -1;
+
+			if (pci_vfio_fill_regions(dev, vfio_dev_fd, &device_info) != 0)
+				return -1;
+		}
+	}
+
 	if (pci_vfio_get_region(dev, bar, &size, &offset) != 0) {
 		RTE_LOG(ERR, EAL, "Cannot get offset of region %d.\n", bar);
 		return -1;
Signature · 1 line
2.25.1
Acked-by on v7 2/2
#26 lore raw

Re: [PATCH v7 2/2] bus/pci: fix legacy device missing region info

Quoted 7 lines · Mingjin Ye

If the legacy device in the primary process does not support mapping the io port bar, the secondary process needs to proactively get the device information and fil the region information.

Fixes 4b741542ecde ("bus/pci: avoid depending on private kernel value")

I have already sent my Acked-by on v6. Once you receive an Ack, you need to add it in the subsequent versions.

For this one I am again doing for the committer.

#27 lore raw

Re: [PATCH v7 2/2] bus/pci: fix legacy device missing region info

24/11/2023 11:47, Gupta, Nipun:

Quoted 8 lines · Mingjin Ye
Quoted 6 lines

If the legacy device in the primary process does not support mapping the io port bar, the secondary process needs to proactively get the device information and fil the region information.

Fixes 4b741542ecde ("bus/pci: avoid depending on private kernel value")
Quoted 8 lines
Quoted 1 line

I have already sent my Acked-by on v6. Once you receive an Ack, you need to add it in the subsequent versions.

For this one I am again doing for the committer.

Series applied with a bit of rebase and styling fixes, thanks.

#28 lore raw

RE: [PATCH v7 0/2] fix legacy device missing region info

In reply to Mingjin Ye

Hi,

can you please take a look at this patch.

Thanks,
Mingjin

Quoted 24 lines

-----Original Message-----
From: Ye, MingjinX <[email protected]>
Sent: Wednesday, November 22, 2023 6:23 PM
To: [email protected]
Cc: Yang, Qiming <[email protected]>; Ye, MingjinX <[email protected]>
Subject: [PATCH v7 0/2] fix legacy device missing region info

Fill in missing region information for legacy devices in secondary processes. And added rte_vfio_get_device_info API for EAL, which makes it easier and more generic to get device information through vfio driver.

Mingjin Ye (2):
vfio: add get device info API
bus/pci: fix legacy device missing region info

 drivers/bus/pci/linux/pci_vfio.c | 26 ++++++++++++++++++++++++++
 lib/eal/include/rte_vfio.h       | 29 +++++++++++++++++++++++++++++
 lib/eal/linux/eal_vfio.c         | 27 +++++++++++++++++++++++++++
 lib/eal/version.map              |  3 +++
 4 files changed, 85 insertions(+)

--
2.25.1