Skip to contentSkip to conversation

PCI/IOV: fix on offset/stride based on NumVFs change

PATCH In review Wei Yang 2 participants 5 messages Dec 22, 2014 – Jan 22, 2015 PCI

lore
Share & tools
lore

Wei Yang

author
#0 lore raw

cover PCI/IOV: fix on offset/stride based on NumVFs change

Per SRIOV SPEC sec 3.3.9, 3.3.10, offset/stride may change when NumVFs is written. While current kernel doesn't update offset/stride with the change of NumVFs.

This property will also affect the iov bus range in enumeration.

This patch set consists of two patches:
1. Update the offset/stride with NumVFs change
2. Calculate VF bus range on each possilbe NumVFs

Code based on 3.19-rc1 and tested on x86 and powernv platform. Bus range doesn't chage on current hardware.

Wei Yang (2):
PCI: Refresh offset/stride after NumVFs is written
PCI: Calculate the VF bus range on each possible NumVFs

 drivers/pci/iov.c |   54 +++++++++++++++++++++++++++++++++++++++++++++--------
 drivers/pci/pci.h |    1 +
 2 files changed, 47 insertions(+), 8 deletions(-)
Signature · 1 line
1.7.9.5
author
#1 lore raw

PATCH 1/2 PCI: Refresh offset/stride after NumVFs is written

According to SR-IOV SPEC sec 3.3.9, 3.3.10, the NumVFs setting change will affect the offset and stride. Current implementation doesn't refresh the offset/stride cached in pci_sriov structure.

This patch introduces a wrapper pci_iov_set_numvfs(), which refresh these two value after NumVFs is written.

---
 drivers/pci/iov.c |   23 +++++++++++++++++++----
 1 file changed, 19 insertions(+), 4 deletions(-)
drivers/pci/iov.c +19 -4
index 4b3a4ea..ef0ceaa 100644
@@ -31,6 +31,21 @@ static inline u8 virtfn_devfn(struct pci_dev *dev, int id)
 		dev->sriov->stride * id) & 0xff;
 }
 
+/**
+ * Per SRIOV SPEC section 3.3.10 and 3.3.11, VF Stride and VF offset may
+ * change when NumVFs changes.
+ *
+ * This function update the iov->offset and iov->stride when NumVFs is written.
+ */
+static inline void pci_iov_set_numvfs(struct pci_dev *dev, int nr_virtfn)
+{
+	struct pci_sriov *iov = dev->sriov;
+
+	pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, nr_virtfn);
+	pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_OFFSET, &iov->offset);
+	pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_STRIDE, &iov->stride);
+}
+
 static struct pci_bus *virtfn_add_bus(struct pci_bus *bus, int busnr)
 {
 	struct pci_bus *child;
@@ -243,7 +258,7 @@ static int sriov_enable(struct pci_dev *dev, int nr_virtfn)
 			return rc;
 	}
 
-	pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, nr_virtfn);
+	pci_iov_set_numvfs(dev, nr_virtfn);
 	iov->ctrl |= PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE;
 	pci_cfg_access_lock(dev);
 	pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
@@ -272,7 +287,7 @@ failed:
 	iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
 	pci_cfg_access_lock(dev);
 	pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
-	pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, 0);
+	pci_iov_set_numvfs(dev, 0);
 	ssleep(1);
 	pci_cfg_access_unlock(dev);
 
@@ -303,7 +318,7 @@ static void sriov_disable(struct pci_dev *dev)
 		sysfs_remove_link(&dev->dev.kobj, "dep_link");
 
 	iov->num_VFs = 0;
-	pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, 0);
+	pci_iov_set_numvfs(dev, 0);
 }
 
 static int sriov_init(struct pci_dev *dev, int pos)
@@ -439,7 +454,7 @@ static void sriov_restore_state(struct pci_dev *dev)
 		pci_update_resource(dev, i);
 
 	pci_write_config_dword(dev, iov->pos + PCI_SRIOV_SYS_PGSIZE, iov->pgsz);
-	pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, iov->num_VFs);
+	pci_iov_set_numvfs(dev, iov->num_VFs);
 	pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
 	if (iov->ctrl & PCI_SRIOV_CTRL_VFE)
 		msleep(100);
Signature · 1 line
1.7.9.5
author
#2 lore raw

PATCH 2/2 PCI: Calculate the VF bus range on each possible NumVFs

In reply to Wei Yang

Per SRIOV SPEC section 3.3.10 and 3.3.11, VF Stride and VF Offset may change when NumVFs changes. This will affect the bus range for VFs.

This patch iterates on each possible NumVFs and calculate the maximum bus range for VFs.

---
 drivers/pci/iov.c |   31 +++++++++++++++++++++++++++----
 drivers/pci/pci.h |    1 +
 2 files changed, 28 insertions(+), 4 deletions(-)
drivers/pci/iov.c +27 -4
index ef0ceaa..ea3a82c 100644
@@ -46,6 +46,30 @@ static inline void pci_iov_set_numvfs(struct pci_dev *dev, int nr_virtfn)
 	pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_STRIDE, &iov->stride);
 }
 
+/**
+ * Per SRIOV SPEC section 3.3.10 and 3.3.11, VF Stride and VF offset may
+ * change when NumVFs changes. Which will affect the bus range for VFs.
+ *
+ * This function iterate on all valide NumVFs and calculate the maximum bus
+ * range for VFs.
+ */
+static inline void pci_iov_max_bus_range(struct pci_dev *dev)
+{
+	struct pci_sriov *iov = dev->sriov;
+	int total = iov->total_VFs;
+	u8 max = 0;
+	u8 busnr;
+
+	for ( ; total >= 0; total--) {
+		pci_iov_set_numvfs(dev, total);
+		busnr = virtfn_bus(dev, iov->total_VFs - 1);
+		if (busnr > max)
+			max = busnr;
+	}
+
+	iov->max_bus_range = max;
+}
+
 static struct pci_bus *virtfn_add_bus(struct pci_bus *bus, int busnr)
 {
 	struct pci_bus *child;
@@ -415,6 +439,7 @@ found:
 
 	dev->sriov = iov;
 	dev->is_physfn = 1;
+	pci_iov_max_bus_range(dev);
 
 	return 0;
 
@@ -550,15 +575,13 @@ void pci_restore_iov_state(struct pci_dev *dev)
 int pci_iov_bus_range(struct pci_bus *bus)
 {
 	int max = 0;
-	u8 busnr;
 	struct pci_dev *dev;
 
 	list_for_each_entry(dev, &bus->devices, bus_list) {
 		if (!dev->is_physfn)
 			continue;
-		busnr = virtfn_bus(dev, dev->sriov->total_VFs - 1);
-		if (busnr > max)
-			max = busnr;
+		if (dev->sriov->max_bus_range > max)
+			max = dev->sriov->max_bus_range;
 	}
 
 	return max ? max - bus->number : 0;
drivers/pci/pci.h +1 -0
index 8aff29a..94faf97 100644
@@ -236,6 +236,7 @@ struct pci_sriov {
 	u16 stride;		/* following VF stride */
 	u32 pgsz;		/* page size for BAR alignment */
 	u8 link;		/* Function Dependency Link */
+	u8 max_bus_range;	/* Maximum bus range a VF could sit*/
 	u16 driver_max_VFs;	/* max num VFs driver supports */
 	struct pci_dev *dev;	/* lowest numbered PF */
 	struct pci_dev *self;	/* this PF */
Signature · 1 line
1.7.9.5
maintainer
#3 lore raw

Re: [PATCH 2/2] PCI: Calculate the VF bus range on each possible NumVFs

Quoted 27 lines · Wei Yang

Per SRIOV SPEC section 3.3.10 and 3.3.11, VF Stride and VF Offset may change when NumVFs changes. This will affect the bus range for VFs.

This patch iterates on each possible NumVFs and calculate the maximum bus range for VFs.

---
 drivers/pci/iov.c |   31 +++++++++++++++++++++++++++----
 drivers/pci/pci.h |    1 +
 2 files changed, 28 insertions(+), 4 deletions(-)
drivers/pci/iov.c +7 -0
index ef0ceaa..ea3a82c 100644
@@ -46,6 +46,30 @@ static inline void pci_iov_set_numvfs(struct pci_dev *dev, int nr_virtfn)
 	pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_STRIDE, &iov->stride);
 }
 
+/**
+ * Per SRIOV SPEC section 3.3.10 and 3.3.11, VF Stride and VF offset may
+ * change when NumVFs changes. Which will affect the bus range for VFs.
+ *
+ * This function iterate on all valide NumVFs and calculate the maximum bus
+ * range for VFs.
+ */

drivers/pci/iov.c static inline void pci_iov_set_numvfs(struct pci_dev *dev, int nr_virtfn) · line 65

Quoted 10 lines
drivers/pci/iov.c +10 -0
+static inline void pci_iov_max_bus_range(struct pci_dev *dev)
+{
+	struct pci_sriov *iov = dev->sriov;
+	int total = iov->total_VFs;
+	u8 max = 0;
+	u8 busnr;
+
+	for ( ; total >= 0; total--) {
+		pci_iov_set_numvfs(dev, total);
+		busnr = virtfn_bus(dev, iov->total_VFs - 1);

We don't need TotalVFs here, do we? I think NumVFs should be enough. Proposed updated patch attached below.

drivers/pci/iov.c static inline void pci_iov_set_numvfs(struct pci_dev *dev, int nr_virtfn) · line 71

Quoted 6 lines
drivers/pci/iov.c +6 -0
+		if (busnr > max)
+			max = busnr;
+	}
+
+	iov->max_bus_range = max;
+}

commit aecf80117ca5be299f85ead9125434f58f2ae3cb

Author: Wei Yang <[email protected]>
Date:   Mon Dec 22 13:48:46 2014 +0800

PCI: Calculate maximum number of buses required for VFs

An SR-IOV device can change its First VF OFfset and VF Stride based on the values of ARI Capable Hierarchy and NumVFs. The number of buses required for all VFs is determined by NumVFs, First VF Offset, and VF Stride (see SR-IOV spec r1.1, sec 2.1.2).

Previously pci_iov_bus_range() computed how many buses would be required by TotalVFs, but this was based on a single NumVFs value and may not have been the maximum for all NumVFs configurations.

Iterate over all valid NumVFs and calculate the maximum number of bus numbers that could ever be required for VFs of this device.

[bhelgaas: compute busnr of NumVFs, not TotalVFs, changelog]
drivers/pci/iov.c +27 -4
index dd4bc361f24a..62f9c8f6871f 100644
@@ -46,6 +46,30 @@ static inline void pci_iov_set_numvfs(struct pci_dev *dev, int nr_virtfn)
 	pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_STRIDE, &iov->stride);
 }
 
+/**
+ * The PF consumes one bus number.  NumVFs, First VF Offset, and VF Stride
+ * determine how many additional bus numbers will be consumed by VFs.
+ *
+ * Iterate over all valid NumVFs and calculate the maximum number of bus
+ * numbers that could ever be required.
+ */
+static inline u8 virtfn_max_buses(struct pci_dev *dev)
+{
+	struct pci_sriov *iov = dev->sriov;
+	int nr_virtfn;
+	u8 max = 0;
+	u8 busnr;
+
+	for (nr_virtfn = 1; nr_virtfn <= iov->total_VFs; nr_virtfn++) {
+		pci_iov_set_numvfs(dev, nr_virtfn);
+		busnr = virtfn_bus(dev, nr_virtfn - 1);
+		if (busnr > max)
+			max = busnr;
+	}
+
+	return max;
+}
+
 static struct pci_bus *virtfn_add_bus(struct pci_bus *bus, int busnr)
 {
 	struct pci_bus *child;
@@ -413,6 +437,7 @@ found:
 
 	mutex_init(&iov->lock);
 
+	iov->max_VF_buses = virtfn_max_buses(dev);
 	dev->sriov = iov;
 	dev->is_physfn = 1;
 
@@ -550,15 +575,13 @@ void pci_restore_iov_state(struct pci_dev *dev)
 int pci_iov_bus_range(struct pci_bus *bus)
 {
 	int max = 0;
-	u8 busnr;
 	struct pci_dev *dev;
 
 	list_for_each_entry(dev, &bus->devices, bus_list) {
 		if (!dev->is_physfn)
 			continue;
-		busnr = virtfn_bus(dev, dev->sriov->total_VFs - 1);
-		if (busnr > max)
-			max = busnr;
+		if (dev->sriov->max_VF_buses > max)
+			max = dev->sriov->max_VF_buses;
 	}
 
 	return max ? max - bus->number : 0;
drivers/pci/pci.h +1 -0
index 8aff29a804ff..f0a19dc7acdd 100644
@@ -236,6 +236,7 @@ struct pci_sriov {
 	u16 stride;		/* following VF stride */
 	u32 pgsz;		/* page size for BAR alignment */
 	u8 link;		/* Function Dependency Link */
+	u8 max_VF_buses;	/* max buses consumed by VFs */
 	u16 driver_max_VFs;	/* max num VFs driver supports */
 	struct pci_dev *dev;	/* lowest numbered PF */
 	struct pci_dev *self;	/* this PF */
author
#4 lore raw

Re: [PATCH 2/2] PCI: Calculate the VF bus range on each possible NumVFs

Quoted 33 lines · Bjorn Helgaas
Quoted 32 lines · Wei Yang

Per SRIOV SPEC section 3.3.10 and 3.3.11, VF Stride and VF Offset may change when NumVFs changes. This will affect the bus range for VFs.

This patch iterates on each possible NumVFs and calculate the maximum bus range for VFs.

---
 drivers/pci/iov.c |   31 +++++++++++++++++++++++++++----
 drivers/pci/pci.h |    1 +
 2 files changed, 28 insertions(+), 4 deletions(-)
drivers/pci/iov.c +12 -0
index ef0ceaa..ea3a82c 100644
@@ -46,6 +46,30 @@ static inline void pci_iov_set_numvfs(struct pci_dev *dev, int nr_virtfn)
 	pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_STRIDE, &iov->stride);
 }
 
+/**
+ * Per SRIOV SPEC section 3.3.10 and 3.3.11, VF Stride and VF offset may
+ * change when NumVFs changes. Which will affect the bus range for VFs.
+ *
+ * This function iterate on all valide NumVFs and calculate the maximum bus
+ * range for VFs.
+ */
+static inline void pci_iov_max_bus_range(struct pci_dev *dev)
+{
+	struct pci_sriov *iov = dev->sriov;
+	int total = iov->total_VFs;
+	u8 max = 0;
Quoted 9 lines

drivers/pci/iov.c static inline void pci_iov_set_numvfs(struct pci_dev *dev, int nr_virtfn) · line 65

Quoted 5 lines
drivers/pci/iov.c +5 -0
+	u8 busnr;
+
+	for ( ; total >= 0; total--) {
+		pci_iov_set_numvfs(dev, total);
+		busnr = virtfn_bus(dev, iov->total_VFs - 1);

We don't need TotalVFs here, do we? I think NumVFs should be enough. Proposed updated patch attached below.

Agree use NumVFs is reasonable. One small comment in below patch.

Quoted 66 lines

drivers/pci/iov.c static inline void pci_iov_set_numvfs(struct pci_dev *dev, int nr_virtfn) · line 71

Quoted 6 lines
drivers/pci/iov.c +6 -0
+		if (busnr > max)
+			max = busnr;
+	}
+
+	iov->max_bus_range = max;
+}

commit aecf80117ca5be299f85ead9125434f58f2ae3cb

Author: Wei Yang <[email protected]>
Date:   Mon Dec 22 13:48:46 2014 +0800

PCI: Calculate maximum number of buses required for VFs

An SR-IOV device can change its First VF OFfset and VF Stride based on the values of ARI Capable Hierarchy and NumVFs. The number of buses required for all VFs is determined by NumVFs, First VF Offset, and VF Stride (see SR-IOV spec r1.1, sec 2.1.2).

Previously pci_iov_bus_range() computed how many buses would be required by TotalVFs, but this was based on a single NumVFs value and may not have been the maximum for all NumVFs configurations.

Iterate over all valid NumVFs and calculate the maximum number of bus numbers that could ever be required for VFs of this device.

[bhelgaas: compute busnr of NumVFs, not TotalVFs, changelog]
drivers/pci/iov.c +24 -0
index dd4bc361f24a..62f9c8f6871f 100644
@@ -46,6 +46,30 @@ static inline void pci_iov_set_numvfs(struct pci_dev *dev, int nr_virtfn)
 	pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_STRIDE, &iov->stride);
 }
 
+/**
+ * The PF consumes one bus number.  NumVFs, First VF Offset, and VF Stride
+ * determine how many additional bus numbers will be consumed by VFs.
+ *
+ * Iterate over all valid NumVFs and calculate the maximum number of bus
+ * numbers that could ever be required.
+ */
+static inline u8 virtfn_max_buses(struct pci_dev *dev)
+{
+	struct pci_sriov *iov = dev->sriov;
+	int nr_virtfn;
+	u8 max = 0;
+	u8 busnr;
+
+	for (nr_virtfn = 1; nr_virtfn <= iov->total_VFs; nr_virtfn++) {
+		pci_iov_set_numvfs(dev, nr_virtfn);
+		busnr = virtfn_bus(dev, nr_virtfn - 1);
+		if (busnr > max)
+			max = busnr;
+	}
+
+	return max;
+}
+
 static struct pci_bus *virtfn_add_bus(struct pci_bus *bus, int busnr)
 {
 	struct pci_bus *child;

drivers/pci/iov.c found: · line 442

Quoted 6 lines
drivers/pci/iov.c +1 -0
@@ -413,6 +437,7 @@ found:
 
 	mutex_init(&iov->lock);
 
+	iov->max_VF_buses = virtfn_max_buses(dev);
 	dev->sriov = iov;
 	dev->is_physfn = 1;

We need to move the function after dev->sriov is set, otherwise in virtfn_max_buses() the dev->sriov is not valid.

Quoted 31 lines
drivers/pci/iov.c +2 -4
@@ -550,15 +575,13 @@ void pci_restore_iov_state(struct pci_dev *dev)
 int pci_iov_bus_range(struct pci_bus *bus)
 {
 	int max = 0;
-	u8 busnr;
 	struct pci_dev *dev;
 
 	list_for_each_entry(dev, &bus->devices, bus_list) {
 		if (!dev->is_physfn)
 			continue;
-		busnr = virtfn_bus(dev, dev->sriov->total_VFs - 1);
-		if (busnr > max)
-			max = busnr;
+		if (dev->sriov->max_VF_buses > max)
+			max = dev->sriov->max_VF_buses;
 	}
 
 	return max ? max - bus->number : 0;
drivers/pci/pci.h +1 -0
index 8aff29a804ff..f0a19dc7acdd 100644
@@ -236,6 +236,7 @@ struct pci_sriov {
 	u16 stride;		/* following VF stride */
 	u32 pgsz;		/* page size for BAR alignment */
 	u8 link;		/* Function Dependency Link */
+	u8 max_VF_buses;	/* max buses consumed by VFs */
 	u16 driver_max_VFs;	/* max num VFs driver supports */
 	struct pci_dev *dev;	/* lowest numbered PF */
 	struct pci_dev *self;	/* this PF */
Signature · 2 lines
Richard Yang
Help you, Help me