Skip to contentSkip to conversation

PCI: mvebu: Use for_each_of_range() iterator for parsing "ranges"

PATCH Applied Rob Herring (Arm) 3 participants 7 messages Nov 7, 2024 – Apr 19, 2025

Subsystems (3)
lore
Share & tools
lore

Rob Herring (Arm)

author reviewer
#0 lore raw

PATCH 1/1 PCI: mvebu: Use for_each_of_range() iterator for parsing "ranges"

The mvebu "ranges" is a bit unusual with its own encoding of addresses, but it's still just normal "ranges" as far as parsing is concerned. Convert mvebu_get_tgt_attr() to use the for_each_of_range() iterator instead of open coding the parsing.

---
Compile tested only.

---
 drivers/pci/controller/pci-mvebu.c | 26 +++++++++-----------------
 1 file changed, 9 insertions(+), 17 deletions(-)
drivers/pci/controller/pci-mvebu.c +9 -17
index 29fe09c99e7d..d4e3f1e76f84 100644
@@ -1179,37 +1179,29 @@ static int mvebu_get_tgt_attr(struct device_node *np, int devfn,
 			      unsigned int *tgt,
 			      unsigned int *attr)
 {
-	const int na = 3, ns = 2;
-	const __be32 *range;
-	int rlen, nranges, rangesz, pna, i;
+	struct of_range range;
+	struct of_range_parser parser;
 
 	*tgt = -1;
 	*attr = -1;
 
-	range = of_get_property(np, "ranges", &rlen);
-	if (!range)
+	if (of_pci_range_parser_init(&parser, np))
 		return -EINVAL;
 
-	pna = of_n_addr_cells(np);
-	rangesz = pna + na + ns;
-	nranges = rlen / sizeof(__be32) / rangesz;
-
-	for (i = 0; i < nranges; i++, range += rangesz) {
-		u32 flags = of_read_number(range, 1);
-		u32 slot = of_read_number(range + 1, 1);
-		u64 cpuaddr = of_read_number(range + na, pna);
+	for_each_of_range(&parser, &range) {
 		unsigned long rtype;
+		u32 slot = upper_32_bits(range.bus_addr);
 
-		if (DT_FLAGS_TO_TYPE(flags) == DT_TYPE_IO)
+		if (DT_FLAGS_TO_TYPE(range.flags) == DT_TYPE_IO)
 			rtype = IORESOURCE_IO;
-		else if (DT_FLAGS_TO_TYPE(flags) == DT_TYPE_MEM32)
+		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(cpuaddr);
-			*attr = DT_CPUADDR_TO_ATTR(cpuaddr);
+			*tgt = DT_CPUADDR_TO_TARGET(range.cpu_addr);
+			*attr = DT_CPUADDR_TO_ATTR(range.cpu_addr);
 			return 0;
 		}
 	}
Signature · 1 line
2.45.2
maintainer
#1 lore raw
Quoted 8 lines · On Thursday 07 November 2024 09:32:55 Rob Herring (Arm) wrote:

The mvebu "ranges" is a bit unusual with its own encoding of addresses, but it's still just normal "ranges" as far as parsing is concerned. Convert mvebu_get_tgt_attr() to use the for_each_of_range() iterator instead of open coding the parsing.

---
Compile tested only.

I see no reason for such change, which was even non tested at all and does not fix any issue. There are more important issues in the driver, it was decided that bug fixes are not going to be included (yet).

Quoted 58 lines
---
 drivers/pci/controller/pci-mvebu.c | 26 +++++++++-----------------
 1 file changed, 9 insertions(+), 17 deletions(-)
drivers/pci/controller/pci-mvebu.c +9 -17
index 29fe09c99e7d..d4e3f1e76f84 100644
@@ -1179,37 +1179,29 @@ static int mvebu_get_tgt_attr(struct device_node *np, int devfn,
 			      unsigned int *tgt,
 			      unsigned int *attr)
 {
-	const int na = 3, ns = 2;
-	const __be32 *range;
-	int rlen, nranges, rangesz, pna, i;
+	struct of_range range;
+	struct of_range_parser parser;
 
 	*tgt = -1;
 	*attr = -1;
 
-	range = of_get_property(np, "ranges", &rlen);
-	if (!range)
+	if (of_pci_range_parser_init(&parser, np))
 		return -EINVAL;
 
-	pna = of_n_addr_cells(np);
-	rangesz = pna + na + ns;
-	nranges = rlen / sizeof(__be32) / rangesz;
-
-	for (i = 0; i < nranges; i++, range += rangesz) {
-		u32 flags = of_read_number(range, 1);
-		u32 slot = of_read_number(range + 1, 1);
-		u64 cpuaddr = of_read_number(range + na, pna);
+	for_each_of_range(&parser, &range) {
 		unsigned long rtype;
+		u32 slot = upper_32_bits(range.bus_addr);
 
-		if (DT_FLAGS_TO_TYPE(flags) == DT_TYPE_IO)
+		if (DT_FLAGS_TO_TYPE(range.flags) == DT_TYPE_IO)
 			rtype = IORESOURCE_IO;
-		else if (DT_FLAGS_TO_TYPE(flags) == DT_TYPE_MEM32)
+		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(cpuaddr);
-			*attr = DT_CPUADDR_TO_ATTR(cpuaddr);
+			*tgt = DT_CPUADDR_TO_TARGET(range.cpu_addr);
+			*attr = DT_CPUADDR_TO_ATTR(range.cpu_addr);
 			return 0;
 		}
 	}
Signature · 1 line
2.45.2
author reviewer
#2 lore raw
Quoted 7 lines · Pali Rohár
Quoted 5 lines · On Thursday 07 November 2024 09:32:55 Rob Herring (Arm) wrote:

The mvebu "ranges" is a bit unusual with its own encoding of addresses, but it's still just normal "ranges" as far as parsing is concerned. Convert mvebu_get_tgt_attr() to use the for_each_of_range() iterator instead of open coding the parsing.

Quoted 6 lines
Quoted 3 lines

---
Compile tested only.

I see no reason for such change, which was even non tested at all and does not fix any issue.

Maintenance of the kernel is an issue. Maybe not for you, but for some of us.

Quoted 2 lines

There are more important issues in the driver,
it was decided that bug fixes are not going to be included (yet).

The larger reason is to get rid of custom parsing of address properties throughout the kernel. As well as remove and consolidate uses of_n_addr_cells() and remove its support of long since deprecated behavior. Also, I intend to make of_get_property/of_find_property() warn about leaking data since it just returns a pointer into the raw DT data and we have no control over the lifetime. Then we can't free those properties. Why do we care? Overlays and Rust.

Rob

Reviewed-by on v1 1/1
#3 lore raw
In reply to Rob Herring (Arm)
Quoted 6 lines · Rob Herring (Arm)

The mvebu "ranges" is a bit unusual with its own encoding of addresses, but it's still just normal "ranges" as far as parsing is concerned. Convert mvebu_get_tgt_attr() to use the for_each_of_range() iterator instead of open coding the parsing.

LGTM!

Could someone please verify it on mvebu machine?

- Mani

Quoted 60 lines

---
Compile tested only.

---
 drivers/pci/controller/pci-mvebu.c | 26 +++++++++-----------------
 1 file changed, 9 insertions(+), 17 deletions(-)
drivers/pci/controller/pci-mvebu.c +9 -17
index 29fe09c99e7d..d4e3f1e76f84 100644
@@ -1179,37 +1179,29 @@ static int mvebu_get_tgt_attr(struct device_node *np, int devfn,
 			      unsigned int *tgt,
 			      unsigned int *attr)
 {
-	const int na = 3, ns = 2;
-	const __be32 *range;
-	int rlen, nranges, rangesz, pna, i;
+	struct of_range range;
+	struct of_range_parser parser;
 
 	*tgt = -1;
 	*attr = -1;
 
-	range = of_get_property(np, "ranges", &rlen);
-	if (!range)
+	if (of_pci_range_parser_init(&parser, np))
 		return -EINVAL;
 
-	pna = of_n_addr_cells(np);
-	rangesz = pna + na + ns;
-	nranges = rlen / sizeof(__be32) / rangesz;
-
-	for (i = 0; i < nranges; i++, range += rangesz) {
-		u32 flags = of_read_number(range, 1);
-		u32 slot = of_read_number(range + 1, 1);
-		u64 cpuaddr = of_read_number(range + na, pna);
+	for_each_of_range(&parser, &range) {
 		unsigned long rtype;
+		u32 slot = upper_32_bits(range.bus_addr);
 
-		if (DT_FLAGS_TO_TYPE(flags) == DT_TYPE_IO)
+		if (DT_FLAGS_TO_TYPE(range.flags) == DT_TYPE_IO)
 			rtype = IORESOURCE_IO;
-		else if (DT_FLAGS_TO_TYPE(flags) == DT_TYPE_MEM32)
+		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(cpuaddr);
-			*attr = DT_CPUADDR_TO_ATTR(cpuaddr);
+			*tgt = DT_CPUADDR_TO_TARGET(range.cpu_addr);
+			*attr = DT_CPUADDR_TO_ATTR(range.cpu_addr);
 			return 0;
 		}
 	}
Signature · 1 line
2.45.2
Signature · 1 line
மணிவண்ணன் சதாசிவம்
maintainer
#4 lore raw
Quoted 8 lines · On Friday 15 November 2024 13:01:04 Manivannan Sadhasivam wrote:
Quoted 6 lines · Rob Herring (Arm)

The mvebu "ranges" is a bit unusual with its own encoding of addresses, but it's still just normal "ranges" as far as parsing is concerned. Convert mvebu_get_tgt_attr() to use the for_each_of_range() iterator instead of open coding the parsing.

Quoted 7 lines

LGTM!

Could someone please verify it on mvebu machine?

- Mani

That is mostly impossible as pci-mvebu is broken. Bjorn and Krzysztof in past already refused to take patches which would fix the driver or extend it for other platforms.

So I do not understand why you are rewriting something which worked, instead of fixing something which is broken. The only point can be to make driver even more broken...

Quoted 63 lines
Quoted 60 lines

---
Compile tested only.

---
 drivers/pci/controller/pci-mvebu.c | 26 +++++++++-----------------
 1 file changed, 9 insertions(+), 17 deletions(-)
drivers/pci/controller/pci-mvebu.c +9 -17
index 29fe09c99e7d..d4e3f1e76f84 100644
@@ -1179,37 +1179,29 @@ static int mvebu_get_tgt_attr(struct device_node *np, int devfn,
 			      unsigned int *tgt,
 			      unsigned int *attr)
 {
-	const int na = 3, ns = 2;
-	const __be32 *range;
-	int rlen, nranges, rangesz, pna, i;
+	struct of_range range;
+	struct of_range_parser parser;
 
 	*tgt = -1;
 	*attr = -1;
 
-	range = of_get_property(np, "ranges", &rlen);
-	if (!range)
+	if (of_pci_range_parser_init(&parser, np))
 		return -EINVAL;
 
-	pna = of_n_addr_cells(np);
-	rangesz = pna + na + ns;
-	nranges = rlen / sizeof(__be32) / rangesz;
-
-	for (i = 0; i < nranges; i++, range += rangesz) {
-		u32 flags = of_read_number(range, 1);
-		u32 slot = of_read_number(range + 1, 1);
-		u64 cpuaddr = of_read_number(range + na, pna);
+	for_each_of_range(&parser, &range) {
 		unsigned long rtype;
+		u32 slot = upper_32_bits(range.bus_addr);
 
-		if (DT_FLAGS_TO_TYPE(flags) == DT_TYPE_IO)
+		if (DT_FLAGS_TO_TYPE(range.flags) == DT_TYPE_IO)
 			rtype = IORESOURCE_IO;
-		else if (DT_FLAGS_TO_TYPE(flags) == DT_TYPE_MEM32)
+		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(cpuaddr);
-			*attr = DT_CPUADDR_TO_ATTR(cpuaddr);
+			*tgt = DT_CPUADDR_TO_TARGET(range.cpu_addr);
+			*attr = DT_CPUADDR_TO_ATTR(range.cpu_addr);
 			return 0;
 		}
 	}
Signature · 1 line
2.45.2
Signature · 1 line
மணிவண்ணன் சதாசிவம்
author reviewer
#5 lore raw
Quoted 14 lines · Pali Rohár
Quoted 12 lines · On Friday 15 November 2024 13:01:04 Manivannan Sadhasivam wrote:
Quoted 6 lines · Rob Herring (Arm)

The mvebu "ranges" is a bit unusual with its own encoding of addresses, but it's still just normal "ranges" as far as parsing is concerned. Convert mvebu_get_tgt_attr() to use the for_each_of_range() iterator instead of open coding the parsing.

LGTM!

Quoted 7 lines
Quoted 3 lines

Could someone please verify it on mvebu machine?

- Mani

That is mostly impossible as pci-mvebu is broken. Bjorn and Krzysztof in past already refused to take patches which would fix the driver or extend it for other platforms.

That makes no sense. The driver is broken and we won't take patches to fix it.

Quoted 3 lines

So I do not understand why you are rewriting something which worked, instead of fixing something which is broken. The only point can be to make driver even more broken...

We make coding improvements to the kernel code all the time. There is no way folks can test every platform. Either drivers need to get tested at some frequency or they should just be removed until someone cares enough to maintain them.

Rob

#6 lore raw
In reply to Rob Herring (Arm)
Quoted 6 lines · Rob Herring (Arm)

The mvebu "ranges" is a bit unusual with its own encoding of addresses, but it's still just normal "ranges" as far as parsing is concerned. Convert mvebu_get_tgt_attr() to use the for_each_of_range() iterator instead of open coding the parsing.

Applied to controller/mvebu, thanks!

[1/1] PCI: mvebu: Use for_each_of_range() iterator for parsing "ranges"

      commit: 506d34571e2f204c991aefe3f1300175907594e3

Best regards,

Signature · 1 line
Manivannan Sadhasivam <[email protected]>