Skip to contentSkip to conversation

PCI: mvebu: Fix use of for_each_of_range() iterator

PATCH Applied Klaus Kudielka 4 participants 5 messages Sep 7, 2025 – Sep 8, 2025

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

PATCH v2 1/1 PCI: mvebu: Fix use of for_each_of_range() iterator

The blamed commit simplifies code, by using the for_each_of_range() iterator. But it results in no pci devices being detected anymore on Turris Omnia (and probably other mvebu targets).

Issue #1:

To determine range.flags, of_pci_range_parser_one() uses bus->get_flags(), which resolves to of_bus_pci_get_flags(). That function already returns an IORESOURCE bit field, and NOT the original flags from the "ranges" resource.

Then mvebu_get_tgt_attr() attempts the very same conversion again. But this is a misinterpretation of range.flags.

Remove the misinterpretation of range.flags in mvebu_get_tgt_attr(), to restore the intended behavior.

Issue #2:

The driver needs target and attributes, which are encoded in the raw address values of the "/soc/pcie/ranges" resource. According to of_pci_range_parser_one(), the raw values are stored in range.bus_addr and range.parent_bus_addr, respectively. range.cpu_addr is a translated version of range.parent_bus_addr, and not relevant here.

Use the correct range structure member, to extract target and attributes. This restores the intended behavior.

---
v2: Fix issue #2, as well.

 drivers/pci/controller/pci-mvebu.c | 21 ++++-----------------
 1 file changed, 4 insertions(+), 17 deletions(-)
drivers/pci/controller/pci-mvebu.c +4 -17
index 755651f338..a72aa57591 100644
@@ -1168,12 +1168,6 @@ static void __iomem *mvebu_pcie_map_registers(struct platform_device *pdev,
 	return devm_ioremap_resource(&pdev->dev, &port->regs);
 }
 
-#define DT_FLAGS_TO_TYPE(flags)       (((flags) >> 24) & 0x03)
-#define    DT_TYPE_IO                 0x1
-#define    DT_TYPE_MEM32              0x2
-#define DT_CPUADDR_TO_TARGET(cpuaddr) (((cpuaddr) >> 56) & 0xFF)
-#define DT_CPUADDR_TO_ATTR(cpuaddr)   (((cpuaddr) >> 48) & 0xFF)
-
 static int mvebu_get_tgt_attr(struct device_node *np, int devfn,
 			      unsigned long type,
 			      unsigned int *tgt,
@@ -1189,19 +1183,12 @@ static int mvebu_get_tgt_attr(struct device_node *np, int devfn,
 		return -EINVAL;
 
 	for_each_of_range(&parser, &range) {
-		unsigned long rtype;
 		u32 slot = upper_32_bits(range.bus_addr);
 
-		if (DT_FLAGS_TO_TYPE(range.flags) == DT_TYPE_IO)
-			rtype = IORESOURCE_IO;
-		else if (DT_FLAGS_TO_TYPE(range.flags) == DT_TYPE_MEM32)
-			rtype = IORESOURCE_MEM;
-		else
-			continue;
-
-		if (slot == PCI_SLOT(devfn) && type == rtype) {
-			*tgt = DT_CPUADDR_TO_TARGET(range.cpu_addr);
-			*attr = DT_CPUADDR_TO_ATTR(range.cpu_addr);
+		if (slot == PCI_SLOT(devfn) &&
+		    type == (range.flags & IORESOURCE_TYPE_BITS)) {
+			*tgt = (range.parent_bus_addr >> 56) & 0xFF;
+			*attr = (range.parent_bus_addr >> 48) & 0xFF;
 			return 0;
 		}
 	}
Signature · 1 line
2.50.1
Tested-by on v2 1/1
#1 lore raw
Quoted 83 lines · Klaus Kudielka

The blamed commit simplifies code, by using the for_each_of_range() iterator. But it results in no pci devices being detected anymore on Turris Omnia (and probably other mvebu targets).

Issue #1:

To determine range.flags, of_pci_range_parser_one() uses bus->get_flags(), which resolves to of_bus_pci_get_flags(). That function already returns an IORESOURCE bit field, and NOT the original flags from the "ranges" resource.

Then mvebu_get_tgt_attr() attempts the very same conversion again. But this is a misinterpretation of range.flags.

Remove the misinterpretation of range.flags in mvebu_get_tgt_attr(), to restore the intended behavior.

Issue #2:

The driver needs target and attributes, which are encoded in the raw address values of the "/soc/pcie/ranges" resource. According to of_pci_range_parser_one(), the raw values are stored in range.bus_addr and range.parent_bus_addr, respectively. range.cpu_addr is a translated version of range.parent_bus_addr, and not relevant here.

Use the correct range structure member, to extract target and attributes. This restores the intended behavior.

---
v2: Fix issue #2, as well.

 drivers/pci/controller/pci-mvebu.c | 21 ++++-----------------
 1 file changed, 4 insertions(+), 17 deletions(-)
drivers/pci/controller/pci-mvebu.c +4 -18
index 755651f338..a72aa57591 100644
@@ -1168,12 +1168,6 @@ static void __iomem *mvebu_pcie_map_registers(struct platform_device *pdev,
        return devm_ioremap_resource(&pdev->dev, &port->regs);
 }
 
-#define DT_FLAGS_TO_TYPE(flags)       (((flags) >> 24) & 0x03)
-#define    DT_TYPE_IO                 0x1
-#define    DT_TYPE_MEM32              0x2
-#define DT_CPUADDR_TO_TARGET(cpuaddr) (((cpuaddr) >> 56) & 0xFF)
-#define DT_CPUADDR_TO_ATTR(cpuaddr)   (((cpuaddr) >> 48) & 0xFF)
-
 static int mvebu_get_tgt_attr(struct device_node *np, int devfn,
                              unsigned long type,
                              unsigned int *tgt,
@@ -1189,19 +1183,12 @@ static int mvebu_get_tgt_attr(struct device_node *np, int devfn,
                return -EINVAL;
 
        for_each_of_range(&parser, &range) {
-               unsigned long rtype;
                u32 slot = upper_32_bits(range.bus_addr);
 
-               if (DT_FLAGS_TO_TYPE(range.flags) == DT_TYPE_IO)
-                       rtype = IORESOURCE_IO;
-               else if (DT_FLAGS_TO_TYPE(range.flags) == DT_TYPE_MEM32)
-                       rtype = IORESOURCE_MEM;
-               else
-                       continue;
-
-               if (slot == PCI_SLOT(devfn) && type == rtype) {
-                       *tgt = DT_CPUADDR_TO_TARGET(range.cpu_addr);
-                       *attr = DT_CPUADDR_TO_ATTR(range.cpu_addr);
+               if (slot == PCI_SLOT(devfn) &&
+                   type == (range.flags & IORESOURCE_TYPE_BITS)) {
+                       *tgt = (range.parent_bus_addr >> 56) & 0xFF;
+                       *attr = (range.parent_bus_addr >> 48) & 0xFF;
                        return 0;
                }
        }
--
Quoted 3 lines

2.50.1

Tested on these Marvell Kirkwood SoC boards: Cloudengine Pogoplug Series 4 (88F6192), ZyXEL NAS325 (88F6282), and Synology DS112 (88F6282).

All the best,
Tony

Tested-by on v2 1/1
#2 lore raw
In reply to Klaus Kudielka
Quoted 34 lines · Klaus Kudielka

The blamed commit simplifies code, by using the for_each_of_range() iterator. But it results in no pci devices being detected anymore on Turris Omnia (and probably other mvebu targets).

Issue #1:

To determine range.flags, of_pci_range_parser_one() uses bus->get_flags(), which resolves to of_bus_pci_get_flags(). That function already returns an IORESOURCE bit field, and NOT the original flags from the "ranges" resource.

Then mvebu_get_tgt_attr() attempts the very same conversion again. But this is a misinterpretation of range.flags.

Remove the misinterpretation of range.flags in mvebu_get_tgt_attr(), to restore the intended behavior.

Issue #2:

The driver needs target and attributes, which are encoded in the raw address values of the "/soc/pcie/ranges" resource. According to of_pci_range_parser_one(), the raw values are stored in range.bus_addr and range.parent_bus_addr, respectively. range.cpu_addr is a translated version of range.parent_bus_addr, and not relevant here.

Use the correct range structure member, to extract target and attributes. This restores the intended behavior.

Quoted 5 lines

---
v2: Fix issue #2, as well.

 drivers/pci/controller/pci-mvebu.c | 21 ++++-----------------
 1 file changed, 4 insertions(+), 17 deletions(-)

Confirmed the patch applied on top of 6.16.5 fixes all the issues introduced with 5da3d94a23c6.

#3 lore raw
Quoted 39 lines · Jan Palus
Quoted 37 lines · Klaus Kudielka

The blamed commit simplifies code, by using the for_each_of_range() iterator. But it results in no pci devices being detected anymore on Turris Omnia (and probably other mvebu targets).

Issue #1:

To determine range.flags, of_pci_range_parser_one() uses bus->get_flags(), which resolves to of_bus_pci_get_flags(). That function already returns an IORESOURCE bit field, and NOT the original flags from the "ranges" resource.

Then mvebu_get_tgt_attr() attempts the very same conversion again. But this is a misinterpretation of range.flags.

Remove the misinterpretation of range.flags in mvebu_get_tgt_attr(), to restore the intended behavior.

Issue #2:

The driver needs target and attributes, which are encoded in the raw address values of the "/soc/pcie/ranges" resource. According to of_pci_range_parser_one(), the raw values are stored in range.bus_addr and range.parent_bus_addr, respectively. range.cpu_addr is a translated version of range.parent_bus_addr, and not relevant here.

Use the correct range structure member, to extract target and attributes. This restores the intended behavior.

---
v2: Fix issue #2, as well.

Quoted 8 lines
Quoted 2 lines
 drivers/pci/controller/pci-mvebu.c | 21 ++++-----------------
 1 file changed, 4 insertions(+), 17 deletions(-)

Confirmed the patch applied on top of 6.16.5 fixes all the issues introduced with 5da3d94a23c6.

Sorry, should have said:

Tested on these Marvell Kirkwood SoC boards: Cloudengine Pogoplug Series 4 (88F6192), ZyXEL NAS325 (88F6282), and Synology DS112 (88F6282) running kernel 6.16.5.

All the best,
Tony

#4 lore raw
In reply to Klaus Kudielka
Quoted 28 lines · Klaus Kudielka

The blamed commit simplifies code, by using the for_each_of_range() iterator. But it results in no pci devices being detected anymore on Turris Omnia (and probably other mvebu targets).

Issue #1:

To determine range.flags, of_pci_range_parser_one() uses bus->get_flags(), which resolves to of_bus_pci_get_flags(). That function already returns an IORESOURCE bit field, and NOT the original flags from the "ranges" resource.

Then mvebu_get_tgt_attr() attempts the very same conversion again. But this is a misinterpretation of range.flags.

Remove the misinterpretation of range.flags in mvebu_get_tgt_attr(), to restore the intended behavior.

Issue #2:

The driver needs target and attributes, which are encoded in the raw address values of the "/soc/pcie/ranges" resource. According to of_pci_range_parser_one(), the raw values are stored in range.bus_addr and range.parent_bus_addr, respectively. range.cpu_addr is a translated version of range.parent_bus_addr, and not relevant here.

Use the correct range structure member, to extract target and attributes. This restores the intended behavior.

Quoted 6 lines

Applied to for-linus for v6.17, thanks!

Quoted 50 lines

---
v2: Fix issue #2, as well.

 drivers/pci/controller/pci-mvebu.c | 21 ++++-----------------
 1 file changed, 4 insertions(+), 17 deletions(-)
drivers/pci/controller/pci-mvebu.c +4 -17
index 755651f338..a72aa57591 100644
@@ -1168,12 +1168,6 @@ static void __iomem *mvebu_pcie_map_registers(struct platform_device *pdev,
 	return devm_ioremap_resource(&pdev->dev, &port->regs);
 }
 
-#define DT_FLAGS_TO_TYPE(flags)       (((flags) >> 24) & 0x03)
-#define    DT_TYPE_IO                 0x1
-#define    DT_TYPE_MEM32              0x2
-#define DT_CPUADDR_TO_TARGET(cpuaddr) (((cpuaddr) >> 56) & 0xFF)
-#define DT_CPUADDR_TO_ATTR(cpuaddr)   (((cpuaddr) >> 48) & 0xFF)
-
 static int mvebu_get_tgt_attr(struct device_node *np, int devfn,
 			      unsigned long type,
 			      unsigned int *tgt,
@@ -1189,19 +1183,12 @@ static int mvebu_get_tgt_attr(struct device_node *np, int devfn,
 		return -EINVAL;
 
 	for_each_of_range(&parser, &range) {
-		unsigned long rtype;
 		u32 slot = upper_32_bits(range.bus_addr);
 
-		if (DT_FLAGS_TO_TYPE(range.flags) == DT_TYPE_IO)
-			rtype = IORESOURCE_IO;
-		else if (DT_FLAGS_TO_TYPE(range.flags) == DT_TYPE_MEM32)
-			rtype = IORESOURCE_MEM;
-		else
-			continue;
-
-		if (slot == PCI_SLOT(devfn) && type == rtype) {
-			*tgt = DT_CPUADDR_TO_TARGET(range.cpu_addr);
-			*attr = DT_CPUADDR_TO_ATTR(range.cpu_addr);
+		if (slot == PCI_SLOT(devfn) &&
+		    type == (range.flags & IORESOURCE_TYPE_BITS)) {
+			*tgt = (range.parent_bus_addr >> 56) & 0xFF;
+			*attr = (range.parent_bus_addr >> 48) & 0xFF;
 			return 0;
 		}
 	}
Signature · 1 line
2.50.1