Skip to contentSkip to conversation

fpga: dfl: pci: gracefully handle misconfigured port entries

PATCH In review [email protected] 4 participants 7 messages Apr 20, 2021 – Apr 26, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fpga: dfl: pci: gracefully handle misconfigured port entries

From: Matthew Gerlach <[email protected]>

Gracefully ignore misconfigured port entries encountered in incorrect FPGA images.

---
 drivers/fpga/dfl-pci.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
drivers/fpga/dfl-pci.c +15 -1
index b44523e..660d3b6 100644
@@ -212,6 +212,7 @@ static int find_dfls_by_default(struct pci_dev *pcidev,
 	int port_num, bar, i, ret = 0;
 	resource_size_t start, len;
 	void __iomem *base;
+	int bars = 0;
 	u32 offset;
 	u64 v;
 
@@ -228,6 +229,7 @@ static int find_dfls_by_default(struct pci_dev *pcidev,
 	if (dfl_feature_is_fme(base)) {
 		start = pci_resource_start(pcidev, 0);
 		len = pci_resource_len(pcidev, 0);
+		bars |= BIT(0);
 
 		dfl_fpga_enum_info_add_dfl(info, start, len);
 
@@ -253,9 +255,21 @@ static int find_dfls_by_default(struct pci_dev *pcidev,
 			 */
 			bar = FIELD_GET(FME_PORT_OFST_BAR_ID, v);
 			offset = FIELD_GET(FME_PORT_OFST_DFH_OFST, v);
+			if (bars & BIT(bar)) {
+				dev_warn(&pcidev->dev, "skipping bad port BAR %d\n", bar);
+				continue;
+			}
+
 			start = pci_resource_start(pcidev, bar) + offset;
-			len = pci_resource_len(pcidev, bar) - offset;
+			len = pci_resource_len(pcidev, bar);
+			if (offset >= len) {
+				dev_warn(&pcidev->dev, "bad port offset %u >= %pa\n",
+					 offset, &len);
+				continue;
+			}
 
+			len -= offset;
+			bars |= BIT(bar);
 			dfl_fpga_enum_info_add_dfl(info, start, len);
 		}
 	} else if (dfl_feature_is_port(base)) {
Signature · 1 line
1.8.3.1
reviewer
#1 lore raw

drivers/fpga/dfl-pci.c static int find_dfls_by_default(struct pci_dev *pcidev,

Quoted 15 lines · On 4/20/21 10:27 AM, [email protected] wrote:

From: Matthew Gerlach <[email protected]>

Gracefully ignore misconfigured port entries encountered in incorrect FPGA images.

---
  drivers/fpga/dfl-pci.c | 16 +++++++++++++++-
  1 file changed, 15 insertions(+), 1 deletion(-)
drivers/fpga/dfl-pci.c +0 -0
index b44523e..660d3b6 100644
@@ -212,6 +212,7 @@ static int find_dfls_by_default(struct pci_dev *pcidev,

Does something similar need to be added to find_dfls_by_vsec ?

Quoted 20 lines
drivers/fpga/dfl-pci.c +3 -0
  	int port_num, bar, i, ret = 0;
  	resource_size_t start, len;
  	void __iomem *base;
+	int bars = 0;
  	u32 offset;
  	u64 v;
 
@@ -228,6 +229,7 @@ static int find_dfls_by_default(struct pci_dev *pcidev,
  	if (dfl_feature_is_fme(base)) {
  		start = pci_resource_start(pcidev, 0);
  		len = pci_resource_len(pcidev, 0);
+		bars |= BIT(0);
 
  		dfl_fpga_enum_info_add_dfl(info, start, len);
 
@@ -253,9 +255,21 @@ static int find_dfls_by_default(struct pci_dev *pcidev,
  			 */
  			bar = FIELD_GET(FME_PORT_OFST_BAR_ID, v);
  			offset = FIELD_GET(FME_PORT_OFST_DFH_OFST, v);
+			if (bars & BIT(bar)) {

drivers/fpga/dfl-pci.c static int find_dfls_by_default(struct pci_dev *pcidev, · line 267

Quoted 10 lines
drivers/fpga/dfl-pci.c +8 -1
+				dev_warn(&pcidev->dev, "skipping bad port BAR %d\n", bar);
+				continue;
+			}
+
  			start = pci_resource_start(pcidev, bar) + offset;
-			len = pci_resource_len(pcidev, bar) - offset;
+			len = pci_resource_len(pcidev, bar);
+			if (offset >= len) {
+				dev_warn(&pcidev->dev, "bad port offset %u >= %pa\n",
+					 offset, &len);

why %pa,&len for instead of %u,len ?

Tom

Quoted 8 lines
drivers/fpga/dfl-pci.c +4 -0
+				continue;
+			}
 
+			len -= offset;
+			bars |= BIT(bar);
  			dfl_fpga_enum_info_add_dfl(info, start, len);
  		}
  	} else if (dfl_feature_is_port(base)) {
#2 lore raw
Quoted 52 lines · Tom Rix

drivers/fpga/dfl-pci.c static int find_dfls_by_default(struct pci_dev *pcidev,

Quoted 15 lines · On 4/20/21 10:27 AM, [email protected] wrote:

From: Matthew Gerlach <[email protected]>

Gracefully ignore misconfigured port entries encountered in incorrect FPGA images.

---
  drivers/fpga/dfl-pci.c | 16 +++++++++++++++-
  1 file changed, 15 insertions(+), 1 deletion(-)
drivers/fpga/dfl-pci.c +0 -0
index b44523e..660d3b6 100644
@@ -212,6 +212,7 @@ static int find_dfls_by_default(struct pci_dev *pcidev,

Does something similar need to be added to find_dfls_by_vsec ?

Quoted 30 lines
drivers/fpga/dfl-pci.c +1 -0
  	int port_num, bar, i, ret = 0;
  	resource_size_t start, len;
  	void __iomem *base;
+	int bars = 0;
  	u32 offset;
  	u64 v;
  @@ -228,6 +229,7 @@ static int find_dfls_by_default(struct pci_dev 

*pcidev,
if (dfl_feature_is_fme(base)) {
start = pci_resource_start(pcidev, 0); len = pci_resource_len(pcidev, 0);

drivers/fpga/dfl-pci.c +1 -0
+		bars |= BIT(0);
    		dfl_fpga_enum_info_add_dfl(info, start, len);
  @@ -253,9 +255,21 @@ static int find_dfls_by_default(struct pci_dev 

*pcidev,
*/
bar = FIELD_GET(FME_PORT_OFST_BAR_ID, v); offset = FIELD_GET(FME_PORT_OFST_DFH_OFST, v);

drivers/fpga/dfl-pci.c +2 -0
+			if (bars & BIT(bar)) {
+				dev_warn(&pcidev->dev, "skipping bad port BAR 

%d\n", bar);

drivers/fpga/dfl-pci.c +6 -1
+				continue;
+			}
+
  			start = pci_resource_start(pcidev, bar) + offset;
-			len = pci_resource_len(pcidev, bar) - offset;
+			len = pci_resource_len(pcidev, bar);
+			if (offset >= len) {
+				dev_warn(&pcidev->dev, "bad port offset %u >= 

%pa\n",

drivers/fpga/dfl-pci.c static int find_dfls_by_default(struct pci_dev *pcidev, · line 267

Quoted 1 line
drivers/fpga/dfl-pci.c +1 -0
+					 offset, &len);

why %pa,&len for instead of %u,len ?

Quoted 1 line

Tom

Hi Tom,

The variable len is of type resource_size_t, and I am following what it says to do in Documentation/core-api/printk-formats.rst:

Physical address types phys_addr_t
----------------------------------

::

        %pa[p]  0x01234567 or 0x0123456789abcdef

For printing a phys_addr_t type (and its derivatives, such as resource_size_t) which can vary based on build options, regardless of the width of the CPU data path.

Passed by reference.

Matthew

Quoted 10 lines
Quoted 8 lines
drivers/fpga/dfl-pci.c +3 -0
+				continue;
+			}
  +			len -= offset;
+			bars |= BIT(bar);
  			dfl_fpga_enum_info_add_dfl(info, start, len);
  		}
  	} else if (dfl_feature_is_port(base)) {
maintainer
#3 lore raw
Quoted 43 lines · On Tue, Apr 20, 2021 at 12:19:42PM -0700, [email protected] wrote:
Quoted 39 lines · Tom Rix

drivers/fpga/dfl-pci.c static int find_dfls_by_default(struct pci_dev *pcidev,

Quoted 15 lines · On 4/20/21 10:27 AM, [email protected] wrote:

From: Matthew Gerlach <[email protected]>

Gracefully ignore misconfigured port entries encountered in incorrect FPGA images.

---
  drivers/fpga/dfl-pci.c | 16 +++++++++++++++-
  1 file changed, 15 insertions(+), 1 deletion(-)
drivers/fpga/dfl-pci.c +0 -0
index b44523e..660d3b6 100644
@@ -212,6 +212,7 @@ static int find_dfls_by_default(struct pci_dev *pcidev,

Does something similar need to be added to find_dfls_by_vsec ?

Quoted 21 lines
drivers/fpga/dfl-pci.c +1 -0
  	int port_num, bar, i, ret = 0;
  	resource_size_t start, len;
  	void __iomem *base;
+	int bars = 0;
  	u32 offset;
  	u64 v;
  @@ -228,6 +229,7 @@ static int find_dfls_by_default(struct pci_dev

*pcidev,
if (dfl_feature_is_fme(base)) {
start = pci_resource_start(pcidev, 0); len = pci_resource_len(pcidev, 0);

drivers/fpga/dfl-pci.c +1 -0
+		bars |= BIT(0);
    		dfl_fpga_enum_info_add_dfl(info, start, len);
  @@ -253,9 +255,21 @@ static int find_dfls_by_default(struct

pci_dev *pcidev,
*/
bar = FIELD_GET(FME_PORT_OFST_BAR_ID, v); offset = FIELD_GET(FME_PORT_OFST_DFH_OFST, v);

drivers/fpga/dfl-pci.c +3 -0
+			if (bars & BIT(bar)) {
+				dev_warn(&pcidev->dev, "skipping bad port BAR %d\n", bar);
+				continue;
Quoted 8 lines
Quoted 8 lines

drivers/fpga/dfl-pci.c static int find_dfls_by_default(struct pci_dev *pcidev, · line 267

Quoted 8 lines
drivers/fpga/dfl-pci.c +6 -1
+			}
+
  			start = pci_resource_start(pcidev, bar) + offset;
-			len = pci_resource_len(pcidev, bar) - offset;
+			len = pci_resource_len(pcidev, bar);
+			if (offset >= len) {
+				dev_warn(&pcidev->dev, "bad port offset %u >= %pa\n",
+					 offset, &len);

Do you want pa or px here?

Quoted 17 lines
Quoted 4 lines

why %pa,&len for instead of %u,len ?

Tom

Hi Tom,

The variable len is of type resource_size_t, and I am following what it says to do in Documentation/core-api/printk-formats.rst:

Physical address types phys_addr_t
----------------------------------

::

        %pa[p]  0x01234567 or 0x0123456789abcdef
Quoted 17 lines

For printing a phys_addr_t type (and its derivatives, such as resource_size_t) which can vary based on build options, regardless of the width of the CPU data path.

Passed by reference.

Matthew

Quoted 10 lines
Quoted 8 lines
drivers/fpga/dfl-pci.c +3 -0
+				continue;
+			}
  +			len -= offset;
+			bars |= BIT(bar);
  			dfl_fpga_enum_info_add_dfl(info, start, len);
  		}
  	} else if (dfl_feature_is_port(base)) {

- Moritz

#4 lore raw
In reply to [email protected]
Quoted 33 lines

Subject: [PATCH] fpga: dfl: pci: gracefully handle misconfigured port entries

From: Matthew Gerlach <[email protected]>

Gracefully ignore misconfigured port entries encountered in incorrect FPGA images.

---
 drivers/fpga/dfl-pci.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
drivers/fpga/dfl-pci.c +2 -0
index b44523e..660d3b6 100644
@@ -212,6 +212,7 @@ static int find_dfls_by_default(struct pci_dev *pcidev,
 	int port_num, bar, i, ret = 0;
 	resource_size_t start, len;
 	void __iomem *base;
+	int bars = 0;
 	u32 offset;
 	u64 v;
 
@@ -228,6 +229,7 @@ static int find_dfls_by_default(struct pci_dev *pcidev,
 	if (dfl_feature_is_fme(base)) {
 		start = pci_resource_start(pcidev, 0);
 		len = pci_resource_len(pcidev, 0);
+		bars |= BIT(0);
 
 		dfl_fpga_enum_info_add_dfl(info, start, len);
 

drivers/fpga/dfl-pci.c static int find_dfls_by_default(struct pci_dev *pcidev, · line 262

Quoted 8 lines
drivers/fpga/dfl-pci.c +4 -0
@@ -253,9 +255,21 @@ static int find_dfls_by_default(struct pci_dev *pcidev,
 			 */
 			bar = FIELD_GET(FME_PORT_OFST_BAR_ID, v);
 			offset = FIELD_GET(FME_PORT_OFST_DFH_OFST, v);
+			if (bars & BIT(bar)) {
+				dev_warn(&pcidev->dev, "skipping bad port
 BAR %d\n", bar);
+				continue;
+			}

Will it be a real problem that multiple ports are inside one BAR but different offsets?

Hao

Quoted 18 lines
drivers/fpga/dfl-pci.c +9 -1
+
 			start = pci_resource_start(pcidev, bar) + offset;
-			len = pci_resource_len(pcidev, bar) - offset;
+			len = pci_resource_len(pcidev, bar);
+			if (offset >= len) {
+				dev_warn(&pcidev->dev, "bad port
 offset %u >= %pa\n",
+					 offset, &len);
+				continue;
+			}
 
+			len -= offset;
+			bars |= BIT(bar);
 			dfl_fpga_enum_info_add_dfl(info, start, len);
drivers/fpga/dfl-pci.c +0 -1
 		}
 	} else if (dfl_feature_is_port(base)) {
--

1.8.3.1

#5 lore raw
Quoted 44 lines · Hao
Quoted 33 lines

Subject: [PATCH] fpga: dfl: pci: gracefully handle misconfigured port entries

From: Matthew Gerlach <[email protected]>

Gracefully ignore misconfigured port entries encountered in incorrect FPGA images.

---
 drivers/fpga/dfl-pci.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
drivers/fpga/dfl-pci.c +2 -0
index b44523e..660d3b6 100644
@@ -212,6 +212,7 @@ static int find_dfls_by_default(struct pci_dev *pcidev,
 	int port_num, bar, i, ret = 0;
 	resource_size_t start, len;
 	void __iomem *base;
+	int bars = 0;
 	u32 offset;
 	u64 v;
 
@@ -228,6 +229,7 @@ static int find_dfls_by_default(struct pci_dev *pcidev,
 	if (dfl_feature_is_fme(base)) {
 		start = pci_resource_start(pcidev, 0);
 		len = pci_resource_len(pcidev, 0);
+		bars |= BIT(0);
 
 		dfl_fpga_enum_info_add_dfl(info, start, len);
 

drivers/fpga/dfl-pci.c static int find_dfls_by_default(struct pci_dev *pcidev, · line 262

Quoted 8 lines
drivers/fpga/dfl-pci.c +4 -0
@@ -253,9 +255,21 @@ static int find_dfls_by_default(struct pci_dev *pcidev,
 			 */
 			bar = FIELD_GET(FME_PORT_OFST_BAR_ID, v);
 			offset = FIELD_GET(FME_PORT_OFST_DFH_OFST, v);
+			if (bars & BIT(bar)) {
+				dev_warn(&pcidev->dev, "skipping bad port
 BAR %d\n", bar);
+				continue;
+			}

Will it be a real problem that multiple ports are inside one BAR but different offsets?

Quoted 1 line

Hao

I don't think multiple ports within a single BAR is something that has been supported in the past. The genesis for this patch was a misconfigured port entry pointing to BAR0. BAR0 had already been mapped for the FME and remapping BAR0 failed resulting in enumeration failure.

Matthew

Quoted 21 lines
Quoted 19 lines
drivers/fpga/dfl-pci.c +9 -1
+
 			start = pci_resource_start(pcidev, bar) + offset;
-			len = pci_resource_len(pcidev, bar) - offset;
+			len = pci_resource_len(pcidev, bar);
+			if (offset >= len) {
+				dev_warn(&pcidev->dev, "bad port
 offset %u >= %pa\n",
+					 offset, &len);
+				continue;
+			}
 
+			len -= offset;
+			bars |= BIT(bar);
 			dfl_fpga_enum_info_add_dfl(info, start, len);
drivers/fpga/dfl-pci.c +0 -1
 		}
 	} else if (dfl_feature_is_port(base)) {
--

1.8.3.1

#6 lore raw
Quoted 46 lines · Hao
Quoted 31 lines
Quoted 31 lines

Subject: [PATCH] fpga: dfl: pci: gracefully handle misconfigured port entries

From: Matthew Gerlach <[email protected]>

Gracefully ignore misconfigured port entries encountered in incorrect FPGA images.

---
 drivers/fpga/dfl-pci.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
drivers/fpga/dfl-pci.c +2 -0
index b44523e..660d3b6 100644
@@ -212,6 +212,7 @@ static int find_dfls_by_default(struct pci_dev *pcidev,
 	int port_num, bar, i, ret = 0;
 	resource_size_t start, len;
 	void __iomem *base;
+	int bars = 0;
 	u32 offset;
 	u64 v;
 
@@ -228,6 +229,7 @@ static int find_dfls_by_default(struct pci_dev *pcidev,
 	if (dfl_feature_is_fme(base)) {
 		start = pci_resource_start(pcidev, 0);
 		len = pci_resource_len(pcidev, 0);
+		bars |= BIT(0);
 
Quoted 3 lines

drivers/fpga/dfl-pci.c static int find_dfls_by_default(struct pci_dev

Quoted 3 lines
drivers/fpga/dfl-pci.c +0 -0
 		dfl_fpga_enum_info_add_dfl(info, start, len);
 
@@ -253,9 +255,21 @@ static int find_dfls_by_default(struct pci_dev

*pcidev,

Quoted 10 lines

drivers/fpga/dfl-pci.c static int find_dfls_by_default(struct pci_dev *pcidev, · line 261

Quoted 8 lines
drivers/fpga/dfl-pci.c +4 -0
 			 */
 			bar = FIELD_GET(FME_PORT_OFST_BAR_ID, v);
 			offset = FIELD_GET(FME_PORT_OFST_DFH_OFST, v);
+			if (bars & BIT(bar)) {
+				dev_warn(&pcidev->dev, "skipping bad port
 BAR %d\n", bar);
+				continue;
+			}

Will it be a real problem that multiple ports are inside one BAR but different

Quoted 8 lines

offsets?

Quoted 2 lines

Hao

I don't think multiple ports within a single BAR is something that has been supported in the past. The genesis for this patch was a misconfigured port entry pointing to BAR0. BAR0 had already been mapped for the FME and remapping BAR0 failed resulting in enumeration failure.

could some products put the port in BAR0? Or multiple ports in one BAR. As we consider this is a common driver can be reused and maintained for a long time, so I hope that we don't put limitation setup only for some specific products.

Hao

Quoted 24 lines

Matthew

Quoted 21 lines
Quoted 19 lines
drivers/fpga/dfl-pci.c +9 -1
+
 			start = pci_resource_start(pcidev, bar) + offset;
-			len = pci_resource_len(pcidev, bar) - offset;
+			len = pci_resource_len(pcidev, bar);
+			if (offset >= len) {
+				dev_warn(&pcidev->dev, "bad port
 offset %u >= %pa\n",
+					 offset, &len);
+				continue;
+			}
 
+			len -= offset;
+			bars |= BIT(bar);
 			dfl_fpga_enum_info_add_dfl(info, start, len);
drivers/fpga/dfl-pci.c +0 -1
 		}
 	} else if (dfl_feature_is_port(base)) {
--

1.8.3.1