pciexp_device: Join pciexp_find_(next_)extended_cap() APIs

Move the `offset` parameter into pciexp_find_extended_cap(). If it's
called with `0`, we start a new search. If it's an existing offset,
we continue the search.

This makes it easier to search for multiple occurences of a capa-
bility in a single loop.

Change-Id: I80115372a82523b90460d97f0fd0fa565c3f56cb
Signed-off-by: Nico Huber <nico.h@gmx.de>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/66453
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Tim Wawrzynczak <twawrzynczak@chromium.org>
diff --git a/src/device/pciexp_device.c b/src/device/pciexp_device.c
index 694f1dd..ea0ec1a 100644
--- a/src/device/pciexp_device.c
+++ b/src/device/pciexp_device.c
@@ -32,17 +32,29 @@
 	return 0;
 }
 
-unsigned int pciexp_find_next_extended_cap(const struct device *dev, unsigned int cap,
-					   unsigned int pos)
+/*
+ * Search for an extended capability with the ID `cap`.
+ *
+ * Returns the offset of the first matching extended
+ * capability if found, or 0 otherwise.
+ *
+ * A new search is started with `offset == 0`.
+ * To continue a search, the prior return value
+ * should be passed as `offset`.
+ */
+unsigned int pciexp_find_extended_cap(const struct device *dev, unsigned int cap,
+				      unsigned int offset)
 {
-	const unsigned int next_cap_offset = pci_read_config32(dev, pos) >> 20;
+	unsigned int next_cap_offset;
+
+	if (offset)
+		next_cap_offset = pci_read_config32(dev, offset) >> 20;
+	else
+		next_cap_offset = PCIE_EXT_CAP_OFFSET;
+
 	return pciexp_get_ext_cap_offset(dev, cap, next_cap_offset);
 }
 
-unsigned int pciexp_find_extended_cap(const struct device *dev, unsigned int cap)
-{
-	return pciexp_get_ext_cap_offset(dev, cap, PCIE_EXT_CAP_OFFSET);
-}
 
 /*
  * Re-train a PCIe link
@@ -215,7 +227,7 @@
 	if (!_pciexp_enable_ltr(parent, parent_cap, dev, cap))
 		return;
 
-	const unsigned int ltr_cap = pciexp_find_extended_cap(dev, PCIE_EXT_CAP_LTR_ID);
+	const unsigned int ltr_cap = pciexp_find_extended_cap(dev, PCIE_EXT_CAP_LTR_ID, 0);
 	if (!ltr_cap)
 		return;
 
@@ -340,13 +352,13 @@
 	if (dev->path.pci.devfn & 0x7)
 		return;
 
-	root_cap = pciexp_find_extended_cap(root, PCIE_EXT_CAP_L1SS_ID);
+	root_cap = pciexp_find_extended_cap(root, PCIE_EXT_CAP_L1SS_ID, 0);
 	if (!root_cap)
 		return;
 
-	end_cap = pciexp_find_extended_cap(dev, PCIE_EXT_CAP_L1SS_ID);
+	end_cap = pciexp_find_extended_cap(dev, PCIE_EXT_CAP_L1SS_ID, 0);
 	if (!end_cap) {
-		end_cap = pciexp_find_extended_cap(dev, 0xcafe);
+		end_cap = pciexp_find_extended_cap(dev, 0xcafe, 0);
 		if (!end_cap)
 			return;
 	}