Skip to contentSkip to conversation

cxl_pci refactor for reusability

PATCH In review Ben Widawsky 8 participants 24 messages Sep 23, 2021 – Oct 1, 2021

Subsystems (9)
x86 PowerPC PCI perf events +5 more subsystems
lore
Share & tools
lore
#0 lore raw

v2 cover cxl_pci refactor for reusability

Changes since v1 [3]:
- get_max_afu_index() updated to new interface (Dan)
- siov_find_pci_dvsec updated to new interface (Dan)
- remove unnecessary pci request/release regions with refactor (Dan)
- move @base into cxl_register_map (Dan)
- Expanded the Cc list to include other users of PCIe DVSEC. (Dan)

Three spots are not converted to use the new PCI core DVSEC helper as the changes are non-trivial.
- find_dvsec_afu_ctrl (ocxl)
- pmt_pci_probe (David)
- intel_uncore_has_discovery_tables (Kan)

The interdiff is below. A range-diff can be generated against v1[3] if desired. Discussion occurred partially offlist between Dan and myself. To summarize, Dan contends that patch 4, "cxl/pci: Refactor cxl_pci_setup_regs" should either be rewrittn, or have a precursor patch that cxl_pci will still scan all register blocks, but only claim the specified types. While the current diff is somewhat complex, I contend that this is unneeded churn because we can easily test this change in our existing regression testing. It also ultimately results in a simpler function in the cxl_port patch series when cxl_pci stops trying to map the component register block (loop is removed entirely). A tiebreak here would be great :-)

---

Original commit message:

Provide the ability to obtain CXL register blocks as discrete functionality. This functionality will become useful for other CXL drivers that need access to CXL register blocks. It is also in line with other additions to core which moves register mapping functionality.

At the introduction of the CXL driver the only user of CXL MMIO was cxl_pci (then known as cxl_mem). As the driver has evolved it is clear that cxl_pci will not be the only entity that needs access to CXL MMIO. This series stops short of moving the generalized functionality into cxl_core for the sake of getting eyes on the important foundational bits sooner rather than later. The ultimate plan is to move much of the code into cxl_core.

Via review of two previous patches [1] & [2] it has been suggested that the bits which are being used for DVSEC enumeration move into PCI core. As CXL core is soon going to require these, let's try to get the ball rolling now on making that happen.

---

[1]: https://lore.kernel.org/linux-pci/[email protected]/
[2]: https://lore.kernel.org/linux-cxl/[email protected]/
[3]: https://lore.kernel.org/linux-cxl/[email protected]/

Ben Widawsky (9):
cxl: Convert "RBI" to enum
cxl/pci: Remove dev_dbg for unknown register blocks cxl/pci: Remove pci request/release regions cxl/pci: Refactor cxl_pci_setup_regs
cxl/pci: Make more use of cxl_register_map
PCI: Add pci_find_dvsec_capability to find designated VSEC cxl/pci: Use pci core's DVSEC functionality
ocxl: Use pci core's DVSEC functionality iommu/vt-d: Use pci core's DVSEC functionality

 arch/powerpc/platforms/powernv/ocxl.c |   3 +-
 drivers/cxl/cxl.h                     |   1 +
 drivers/cxl/pci.c                     | 176 ++++++++++++--------------
 drivers/cxl/pci.h                     |  14 +-
 drivers/iommu/intel/iommu.c           |  15 +--
 drivers/misc/ocxl/config.c            |  13 +-
 drivers/pci/pci.c                     |  32 +++++
 include/linux/pci.h                   |   1 +
 8 files changed, 127 insertions(+), 128 deletions(-)

Interdiff against v1:

arch/powerpc/platforms/powernv/ocxl.c +2 -1
index 9105efcf242a..28b009b46464 100644
@@ -107,7 +107,8 @@ static int get_max_afu_index(struct pci_dev *dev, int *afu_idx)
 	int pos;
 	u32 val;
 
-	pos = find_dvsec_from_pos(dev, OCXL_DVSEC_FUNC_ID, 0);
+	pos = pci_find_dvsec_capability(dev, PCI_VENDOR_ID_IBM,
+					OCXL_DVSEC_FUNC_ID);
 	if (!pos)
 		return -ESRCH;
 
drivers/cxl/cxl.h +1 -0
index 7d6b011dd963..3b128ce71564 100644
@@ -140,6 +140,7 @@ struct cxl_device_reg_map {
 };
 
 struct cxl_register_map {
+	void __iomem *base;
 	u64 block_offset;
 	u8 reg_type;
 	u8 barno;
drivers/cxl/pci.c +17 -29

123 lines not on this page · raw message

drivers/iommu/intel/iommu.c +1 -14
index d75f59ae28e6..30c97181f0ae 100644
@@ -5398,20 +5398,7 @@ static int intel_iommu_disable_sva(struct device *dev)
  */
 static int siov_find_pci_dvsec(struct pci_dev *pdev)
 {
-	int pos;
-	u16 vendor, id;
-
-	pos = pci_find_next_ext_capability(pdev, 0, 0x23);
-	while (pos) {
-		pci_read_config_word(pdev, pos + 4, &vendor);
-		pci_read_config_word(pdev, pos + 8, &id);
-		if (vendor == PCI_VENDOR_ID_INTEL && id == 5)
-			return pos;
-
-		pos = pci_find_next_ext_capability(pdev, pos, 0x23);
-	}
-
-	return 0;
+	return pci_find_dvsec_capability(pdev, PCI_VENDOR_ID_INTEL, 5);
 }
 
 static bool

base-commit: 18f2a85f5e7db7468530be5048fee5b9cc7a8013

Signature · 1 line
2.33.0
#1 lore raw

PATCH v2 1/9 cxl: Convert "RBI" to enum

In preparation for passing around the Register Block Indicator (RBI) as a parameter, it is desirable to convert the type to an enum so that the interface can use a well defined type checked parameter.

As a result of this change, should future versions of the spec add sparsely defined identifiers, it could become a problem if checking for invalid identifiers since the code currently checks for the max identifier. This is not an issue with current spec, and the algorithm to obtain the register blocks will change before those possible additions are made.

---
 drivers/cxl/pci.h | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
drivers/cxl/pci.h +8 -6
index 8c1a58813816..7d3e4bf06b45 100644
@@ -20,13 +20,15 @@
 #define CXL_REGLOC_BIR_MASK GENMASK(2, 0)
 
 /* Register Block Identifier (RBI) */
-#define CXL_REGLOC_RBI_MASK GENMASK(15, 8)
-#define CXL_REGLOC_RBI_EMPTY 0
-#define CXL_REGLOC_RBI_COMPONENT 1
-#define CXL_REGLOC_RBI_VIRT 2
-#define CXL_REGLOC_RBI_MEMDEV 3
-#define CXL_REGLOC_RBI_TYPES CXL_REGLOC_RBI_MEMDEV + 1
+enum cxl_regloc_type {
+	CXL_REGLOC_RBI_EMPTY = 0,
+	CXL_REGLOC_RBI_COMPONENT,
+	CXL_REGLOC_RBI_VIRT,
+	CXL_REGLOC_RBI_MEMDEV,
+	CXL_REGLOC_RBI_TYPES
+};
 
+#define CXL_REGLOC_RBI_MASK GENMASK(15, 8)
 #define CXL_REGLOC_ADDR_MASK GENMASK(31, 16)
 
 #endif /* __CXL_PCI_H__ */
Signature · 1 line
2.33.0
#3 lore raw

PATCH v2 2/9 cxl/pci: Remove dev_dbg for unknown register blocks

In reply to Ben Widawsky

While interesting to driver developers, the dev_dbg message doesn't do much except clutter up logs. This information should be attainable through sysfs, and someday lspci like utilities. This change additionally helps reduce the LOC in a subsequent patch to refactor some of cxl_pci register mapping.

---
 drivers/cxl/pci.c | 3 ---
 1 file changed, 3 deletions(-)
drivers/cxl/pci.c +0 -3
index 64180f46c895..ccc7c2573ddc 100644
@@ -475,9 +475,6 @@ static int cxl_pci_setup_regs(struct cxl_mem *cxlm)
 		cxl_decode_register_block(reg_lo, reg_hi, &bar, &offset,
 					  &reg_type);
 
-		dev_dbg(dev, "Found register block in bar %u @ 0x%llx of type %u\n",
-			bar, offset, reg_type);
-
 		/* Ignore unknown register block types */
 		if (reg_type > CXL_REGLOC_RBI_MEMDEV)
 			continue;
Signature · 1 line
2.33.0
#5 lore raw

PATCH v2 3/9 cxl/pci: Remove pci request/release regions

In reply to Ben Widawsky

Quoting Dan, "... the request + release regions should probably just be dropped. It's not like any of the register enumeration would collide with someone else who already has the registers mapped. The collision only comes when the registers are mapped for their final usage, and that will have more precision in the request."

---
 drivers/cxl/pci.c | 5 -----
 1 file changed, 5 deletions(-)
drivers/cxl/pci.c +0 -5
index ccc7c2573ddc..7256c236fdb3 100644
@@ -453,9 +453,6 @@ static int cxl_pci_setup_regs(struct cxl_mem *cxlm)
 		return -ENXIO;
 	}
 
-	if (pci_request_mem_regions(pdev, pci_name(pdev)))
-		return -ENODEV;
-
 	/* Get the size of the Register Locator DVSEC */
 	pci_read_config_dword(pdev, regloc + PCI_DVSEC_HEADER1, &regloc_size);
 	regloc_size = FIELD_GET(PCI_DVSEC_HEADER1_LENGTH_MASK, regloc_size);
@@ -499,8 +496,6 @@ static int cxl_pci_setup_regs(struct cxl_mem *cxlm)
 		n_maps++;
 	}
 
-	pci_release_mem_regions(pdev);
-
 	for (i = 0; i < n_maps; i++) {
 		ret = cxl_map_regs(cxlm, &maps[i]);
 		if (ret)
Signature · 1 line
2.33.0
#7 lore raw

PATCH v2 4/9 cxl/pci: Refactor cxl_pci_setup_regs

In reply to Ben Widawsky

In preparation for moving parts of register mapping to cxl_core, the cxl_pci driver is refactored to utilize a new helper to find register blocks by type.

cxl_pci scanned through all register blocks and mapping the ones that the driver will use. This logic is inverted so that the driver specifically requests the register blocks from a new helper. Under the hood, the same implementation of scanning through all register locator DVSEC entries exists.

There are 2 behavioral changes (#2 is arguable):
1. A dev_err is introduced if cxl_map_regs fails.
2. The previous logic would try to map component registers and device registers multiple times if there were present and keep the mapping of the last one found (furthest offset in the register locator). While this is disallowed in the spec, CXL 2.0 8.1.9: "Each register block identifier shall only occur once in the Register Locator DVSEC structure" it was how the driver would respond to the spec violation. The new logic will take the first found register block by type and move on.

---
Changes since v1:

---
 drivers/cxl/pci.c | 126 +++++++++++++++++++++++++---------------------
 1 file changed, 70 insertions(+), 56 deletions(-)
drivers/cxl/pci.c +70 -56
index 7256c236fdb3..bbbacbc94fbf 100644
@@ -428,6 +428,45 @@ static void cxl_decode_register_block(u32 reg_lo, u32 reg_hi,
 	*reg_type = FIELD_GET(CXL_REGLOC_RBI_MASK, reg_lo);
 }
 
+static int find_register_block(struct pci_dev *pdev, enum cxl_regloc_type type,
+			       struct cxl_register_map *map)
+{
+	int regloc, i, rc = -ENODEV;
+	u32 regloc_size, regblocks;
+
+	regloc = cxl_pci_dvsec(pdev, PCI_DVSEC_ID_CXL_REGLOC_DVSEC_ID);
+	if (!regloc)
+		return -ENXIO;
+
+	pci_read_config_dword(pdev, regloc + PCI_DVSEC_HEADER1, &regloc_size);
+	regloc_size = FIELD_GET(PCI_DVSEC_HEADER1_LENGTH_MASK, regloc_size);
+
+	regloc += PCI_DVSEC_ID_CXL_REGLOC_BLOCK1_OFFSET;
+	regblocks = (regloc_size - PCI_DVSEC_ID_CXL_REGLOC_BLOCK1_OFFSET) / 8;
+
+	for (i = 0; i < regblocks; i++, regloc += 8) {
+		u32 reg_lo, reg_hi;
+		u8 reg_type, bar;
+		u64 offset;
+
+		pci_read_config_dword(pdev, regloc, &reg_lo);
+		pci_read_config_dword(pdev, regloc + 4, &reg_hi);
+
+		cxl_decode_register_block(reg_lo, reg_hi, &bar, &offset,
+					  &reg_type);
+
+		if (reg_type == type) {
+			map->barno = bar;
+			map->block_offset = offset;
+			map->reg_type = reg_type;
+			rc = 0;
+			break;
+		}
+	}
+
+	return rc;
+}
+
 /**
  * cxl_pci_setup_regs() - Setup necessary MMIO.
  * @cxlm: The CXL memory device to communicate with.
@@ -440,69 +479,44 @@ static void cxl_decode_register_block(u32 reg_lo, u32 reg_hi,
  */
 static int cxl_pci_setup_regs(struct cxl_mem *cxlm)
 {
-	void __iomem *base;
-	u32 regloc_size, regblocks;
-	int regloc, i, n_maps, ret = 0;
+	int rc, i;
 	struct device *dev = cxlm->dev;
 	struct pci_dev *pdev = to_pci_dev(dev);
-	struct cxl_register_map *map, maps[CXL_REGLOC_RBI_TYPES];
+	const enum cxl_regloc_type types[] = { CXL_REGLOC_RBI_MEMDEV,
+					       CXL_REGLOC_RBI_COMPONENT };
 
-	regloc = cxl_pci_dvsec(pdev, PCI_DVSEC_ID_CXL_REGLOC_DVSEC_ID);
-	if (!regloc) {
-		dev_err(dev, "register location dvsec not found\n");
-		return -ENXIO;
-	}
+	for (i = 0; i < ARRAY_SIZE(types); i++) {
+		struct cxl_register_map map;
+		void __iomem *base;
 
-	/* Get the size of the Register Locator DVSEC */
-	pci_read_config_dword(pdev, regloc + PCI_DVSEC_HEADER1, &regloc_size);
-	regloc_size = FIELD_GET(PCI_DVSEC_HEADER1_LENGTH_MASK, regloc_size);
-
-	regloc += PCI_DVSEC_ID_CXL_REGLOC_BLOCK1_OFFSET;
-	regblocks = (regloc_size - PCI_DVSEC_ID_CXL_REGLOC_BLOCK1_OFFSET) / 8;
-
-	for (i = 0, n_maps = 0; i < regblocks; i++, regloc += 8) {
-		u32 reg_lo, reg_hi;
-		u8 reg_type;
-		u64 offset;
-		u8 bar;
-
-		pci_read_config_dword(pdev, regloc, &reg_lo);
-		pci_read_config_dword(pdev, regloc + 4, &reg_hi);
-
-		cxl_decode_register_block(reg_lo, reg_hi, &bar, &offset,
-					  &reg_type);
-
-		/* Ignore unknown register block types */
-		if (reg_type > CXL_REGLOC_RBI_MEMDEV)
-			continue;
-
-		base = cxl_pci_map_regblock(cxlm, bar, offset);
-		if (!base)
-			return -ENOMEM;
-
-		map = &maps[n_maps];
-		map->barno = bar;
-		map->block_offset = offset;
-		map->reg_type = reg_type;
-
-		ret = cxl_probe_regs(cxlm, base + offset, map);
-
-		/* Always unmap the regblock regardless of probe success */
-		cxl_pci_unmap_regblock(cxlm, base);
-
-		if (ret)
-			return ret;
-
-		n_maps++;
-	}
-
-	for (i = 0; i < n_maps; i++) {
-		ret = cxl_map_regs(cxlm, &maps[i]);
-		if (ret)
+		rc = find_register_block(pdev, types[i], &map);
+		if (rc) {
+			dev_err(dev, "Couldn't find %s register block\n",
+				types[i] == CXL_REGLOC_RBI_MEMDEV ?
+					      "device" :
+					      "component");
 			break;
+		}
+
+		base = cxl_pci_map_regblock(cxlm, map.barno, map.block_offset);
+		if (!base) {
+			rc = -ENOMEM;
+			break;
+		}
+
+		rc = cxl_probe_regs(cxlm, base + map.block_offset, &map);
+		cxl_pci_unmap_regblock(cxlm, base);
+		if (rc)
+			break;
+
+		rc = cxl_map_regs(cxlm, &map);
+		if (rc) {
+			dev_err(dev, "Failed to map CXL registers\n");
+			break;
+		}
 	}
 
-	return ret;
+	return rc;
 }
 
 static int cxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Signature · 1 line
2.33.0
#9 lore raw

PATCH v2 5/9 cxl/pci: Make more use of cxl_register_map

In reply to Ben Widawsky

The structure exists to pass around information about register mapping. Using it more extensively cleans up many existing functions.

---
 drivers/cxl/cxl.h |  1 +
 drivers/cxl/pci.c | 36 +++++++++++++++++-------------------
 2 files changed, 18 insertions(+), 19 deletions(-)
drivers/cxl/cxl.h +1 -0
index 7d6b011dd963..3b128ce71564 100644
@@ -140,6 +140,7 @@ struct cxl_device_reg_map {
 };
 
 struct cxl_register_map {
+	void __iomem *base;
 	u64 block_offset;
 	u8 reg_type;
 	u8 barno;
drivers/cxl/pci.c +17 -19
index bbbacbc94fbf..5eaf2736f779 100644
@@ -306,35 +306,36 @@ static int cxl_pci_setup_mailbox(struct cxl_mem *cxlm)
 	return 0;
 }
 
-static void __iomem *cxl_pci_map_regblock(struct cxl_mem *cxlm,
-					  u8 bar, u64 offset)
+static int cxl_pci_map_regblock(struct cxl_mem *cxlm, struct cxl_register_map *map)
 {
-	void __iomem *addr;
+	int bar = map->barno;
 	struct device *dev = cxlm->dev;
 	struct pci_dev *pdev = to_pci_dev(dev);
+	resource_size_t offset = map->block_offset;
 
 	/* Basic sanity check that BAR is big enough */
 	if (pci_resource_len(pdev, bar) < offset) {
 		dev_err(dev, "BAR%d: %pr: too small (offset: %#llx)\n", bar,
 			&pdev->resource[bar], (unsigned long long)offset);
-		return IOMEM_ERR_PTR(-ENXIO);
+		return -ENXIO;
 	}
 
-	addr = pci_iomap(pdev, bar, 0);
-	if (!addr) {
+	map->base = pci_iomap(pdev, bar, 0);
+	if (!map->base) {
 		dev_err(dev, "failed to map registers\n");
-		return addr;
+		return PTR_ERR(map->base);
 	}
 
 	dev_dbg(dev, "Mapped CXL Memory Device resource bar %u @ %#llx\n",
 		bar, offset);
 
-	return addr;
+	return 0;
 }
 
-static void cxl_pci_unmap_regblock(struct cxl_mem *cxlm, void __iomem *base)
+static void cxl_pci_unmap_regblock(struct cxl_mem *cxlm, struct cxl_register_map *map)
 {
-	pci_iounmap(to_pci_dev(cxlm->dev), base);
+	pci_iounmap(to_pci_dev(cxlm->dev), map->base);
+	map->base = 0;
 }
 
 static int cxl_pci_dvsec(struct pci_dev *pdev, int dvsec)
@@ -360,9 +361,9 @@ static int cxl_pci_dvsec(struct pci_dev *pdev, int dvsec)
 	return 0;
 }
 
-static int cxl_probe_regs(struct cxl_mem *cxlm, void __iomem *base,
-			  struct cxl_register_map *map)
+static int cxl_probe_regs(struct cxl_mem *cxlm, struct cxl_register_map *map)
 {
+	void __iomem *base = map->base + map->block_offset;
 	struct cxl_component_reg_map *comp_map;
 	struct cxl_device_reg_map *dev_map;
 	struct device *dev = cxlm->dev;
@@ -487,7 +488,6 @@ static int cxl_pci_setup_regs(struct cxl_mem *cxlm)
 
 	for (i = 0; i < ARRAY_SIZE(types); i++) {
 		struct cxl_register_map map;
-		void __iomem *base;
 
 		rc = find_register_block(pdev, types[i], &map);
 		if (rc) {
@@ -498,14 +498,12 @@ static int cxl_pci_setup_regs(struct cxl_mem *cxlm)
 			break;
 		}
 
-		base = cxl_pci_map_regblock(cxlm, map.barno, map.block_offset);
-		if (!base) {
-			rc = -ENOMEM;
+		rc = cxl_pci_map_regblock(cxlm, &map);
+		if (rc)
 			break;
-		}
 
-		rc = cxl_probe_regs(cxlm, base + map.block_offset, &map);
-		cxl_pci_unmap_regblock(cxlm, base);
+		rc = cxl_probe_regs(cxlm, &map);
+		cxl_pci_unmap_regblock(cxlm, &map);
 		if (rc)
 			break;
 
Signature · 1 line
2.33.0
#11 lore raw

PATCH v2 6/9 PCI: Add pci_find_dvsec_capability to find designated VSEC

In reply to Ben Widawsky

Add pci_find_dvsec_capability to locate a Designated Vendor-Specific Extended Capability with the specified DVSEC ID.

The Designated Vendor-Specific Extended Capability (DVSEC) allows one or more vendor specific capabilities that aren't tied to the vendor ID of the PCI component.

DVSEC is critical for both the Compute Express Link (CXL) driver as well as the driver for OpenCAPI coherent accelerator (OCXL).

Cc David E. Box <[email protected]>
Cc Jonathan Cameron <[email protected]>
Cc Bjorn Helgaas <[email protected]>
Cc Dan Williams <[email protected]>
Cc Andrew Donnellan <[email protected]>
Cc Lu Baolu <[email protected]>
---
 drivers/pci/pci.c   | 32 ++++++++++++++++++++++++++++++++
 include/linux/pci.h |  1 +
 2 files changed, 33 insertions(+)
drivers/pci/pci.c +32 -0
index ce2ab62b64cf..94ac86ff28b0 100644
@@ -732,6 +732,38 @@ u16 pci_find_vsec_capability(struct pci_dev *dev, u16 vendor, int cap)
 }
 EXPORT_SYMBOL_GPL(pci_find_vsec_capability);
 
+/**
+ * pci_find_dvsec_capability - Find DVSEC for vendor
+ * @dev: PCI device to query
+ * @vendor: Vendor ID to match for the DVSEC
+ * @dvsec: Designated Vendor-specific capability ID
+ *
+ * If DVSEC has Vendor ID @vendor and DVSEC ID @dvsec return the capability
+ * offset in config space; otherwise return 0.
+ */
+u16 pci_find_dvsec_capability(struct pci_dev *dev, u16 vendor, u16 dvsec)
+{
+	int pos;
+
+	pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_DVSEC);
+	if (!pos)
+		return 0;
+
+	while (pos) {
+		u16 v, id;
+
+		pci_read_config_word(dev, pos + PCI_DVSEC_HEADER1, &v);
+		pci_read_config_word(dev, pos + PCI_DVSEC_HEADER2, &id);
+		if (vendor == v && dvsec == id)
+			return pos;
+
+		pos = pci_find_next_ext_capability(dev, pos, PCI_EXT_CAP_ID_DVSEC);
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(pci_find_dvsec_capability);
+
 /**
  * pci_find_parent_resource - return resource region of parent bus of given
  *			      region
include/linux/pci.h +1 -0
index cd8aa6fce204..c93ccfa4571b 100644
@@ -1130,6 +1130,7 @@ u16 pci_find_ext_capability(struct pci_dev *dev, int cap);
 u16 pci_find_next_ext_capability(struct pci_dev *dev, u16 pos, int cap);
 struct pci_bus *pci_find_next_bus(const struct pci_bus *from);
 u16 pci_find_vsec_capability(struct pci_dev *dev, u16 vendor, int cap);
+u16 pci_find_dvsec_capability(struct pci_dev *dev, u16 vendor, u16 dvsec);
 
 u64 pci_get_dsn(struct pci_dev *dev);
 
Signature · 1 line
2.33.0
#16 lore raw

PATCH v2 7/9 cxl/pci: Use pci core's DVSEC functionality

In reply to Ben Widawsky

Reduce maintenance burden of DVSEC query implementation by using the centralized PCI core implementation.

---
 drivers/cxl/pci.c | 20 +-------------------
 1 file changed, 1 insertion(+), 19 deletions(-)
drivers/cxl/pci.c +1 -19
index 5eaf2736f779..79d4d9b16d83 100644
@@ -340,25 +340,7 @@ static void cxl_pci_unmap_regblock(struct cxl_mem *cxlm, struct cxl_register_map
 
 static int cxl_pci_dvsec(struct pci_dev *pdev, int dvsec)
 {
-	int pos;
-
-	pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_DVSEC);
-	if (!pos)
-		return 0;
-
-	while (pos) {
-		u16 vendor, id;
-
-		pci_read_config_word(pdev, pos + PCI_DVSEC_HEADER1, &vendor);
-		pci_read_config_word(pdev, pos + PCI_DVSEC_HEADER2, &id);
-		if (vendor == PCI_DVSEC_VENDOR_ID_CXL && dvsec == id)
-			return pos;
-
-		pos = pci_find_next_ext_capability(pdev, pos,
-						   PCI_EXT_CAP_ID_DVSEC);
-	}
-
-	return 0;
+	return pci_find_dvsec_capability(pdev, PCI_DVSEC_VENDOR_ID_CXL, dvsec);
 }
 
 static int cxl_probe_regs(struct cxl_mem *cxlm, struct cxl_register_map *map)
Signature · 1 line
2.33.0
#18 lore raw

PATCH v2 8/9 ocxl: Use pci core's DVSEC functionality

In reply to Ben Widawsky

Reduce maintenance burden of DVSEC query implementation by using the centralized PCI core implementation.

There are two obvious places to simply drop in the new core implementation. There remains find_dvsec_from_pos() which would benefit from using a core implementation. As that change is less trivial it is reserved for later.

---
 arch/powerpc/platforms/powernv/ocxl.c |  3 ++-
 drivers/misc/ocxl/config.c            | 13 +------------
 2 files changed, 3 insertions(+), 13 deletions(-)
arch/powerpc/platforms/powernv/ocxl.c +2 -1
index 9105efcf242a..28b009b46464 100644
@@ -107,7 +107,8 @@ static int get_max_afu_index(struct pci_dev *dev, int *afu_idx)
 	int pos;
 	u32 val;
 
-	pos = find_dvsec_from_pos(dev, OCXL_DVSEC_FUNC_ID, 0);
+	pos = pci_find_dvsec_capability(dev, PCI_VENDOR_ID_IBM,
+					OCXL_DVSEC_FUNC_ID);
 	if (!pos)
 		return -ESRCH;
 
drivers/misc/ocxl/config.c +1 -12
index a68738f38252..e401a51596b9 100644
@@ -33,18 +33,7 @@
 
 static int find_dvsec(struct pci_dev *dev, int dvsec_id)
 {
-	int vsec = 0;
-	u16 vendor, id;
-
-	while ((vsec = pci_find_next_ext_capability(dev, vsec,
-						    OCXL_EXT_CAP_ID_DVSEC))) {
-		pci_read_config_word(dev, vsec + OCXL_DVSEC_VENDOR_OFFSET,
-				&vendor);
-		pci_read_config_word(dev, vsec + OCXL_DVSEC_ID_OFFSET, &id);
-		if (vendor == PCI_VENDOR_ID_IBM && id == dvsec_id)
-			return vsec;
-	}
-	return 0;
+	return pci_find_dvsec_capability(dev, PCI_VENDOR_ID_IBM, dvsec_id);
 }
 
 static int find_dvsec_afu_ctrl(struct pci_dev *dev, u8 afu_idx)
Signature · 1 line
2.33.0
#20 lore raw

PATCH v2 9/9 iommu/vt-d: Use pci core's DVSEC functionality

In reply to Ben Widawsky

Reduce maintenance burden of DVSEC query implementation by using the centralized PCI core implementation.

---
 drivers/iommu/intel/iommu.c | 15 +--------------
 1 file changed, 1 insertion(+), 14 deletions(-)
drivers/iommu/intel/iommu.c +1 -14
index d75f59ae28e6..30c97181f0ae 100644
@@ -5398,20 +5398,7 @@ static int intel_iommu_disable_sva(struct device *dev)
  */
 static int siov_find_pci_dvsec(struct pci_dev *pdev)
 {
-	int pos;
-	u16 vendor, id;
-
-	pos = pci_find_next_ext_capability(pdev, 0, 0x23);
-	while (pos) {
-		pci_read_config_word(pdev, pos + 4, &vendor);
-		pci_read_config_word(pdev, pos + 8, &id);
-		if (vendor == PCI_VENDOR_ID_INTEL && id == 5)
-			return pos;
-
-		pos = pci_find_next_ext_capability(pdev, pos, 0x23);
-	}
-
-	return 0;
+	return pci_find_dvsec_capability(pdev, PCI_VENDOR_ID_INTEL, 5);
 }
 
 static bool
Signature · 1 line
2.33.0
Tested-by on v2 6/9
#12 lore raw

Re: [PATCH v2 6/9] PCI: Add pci_find_dvsec_capability to find designated VSEC

In reply to Ben Widawsky
Quoted 10 lines · Ben Widawsky

Add pci_find_dvsec_capability to locate a Designated Vendor-Specific Extended Capability with the specified DVSEC ID.

The Designated Vendor-Specific Extended Capability (DVSEC) allows one or more vendor specific capabilities that aren't tied to the vendor ID of the PCI component.

DVSEC is critical for both the Compute Express Link (CXL) driver as well as the driver for OpenCAPI coherent accelerator (OCXL).

Quoted 10 lines
Cc David E. Box <[email protected]>
Cc Jonathan Cameron <[email protected]>
Cc Bjorn Helgaas <[email protected]>
Cc Dan Williams <[email protected]>
Cc Andrew Donnellan <[email protected]>
Cc Lu Baolu <[email protected]>

Applied the interface for the perf uncore driver as below. The interface works properly.

From ebb69ba386dca91fb372522b13af9feb84adcbc0 Mon Sep 17 00:00:00 2001
From: Kan Liang <[email protected]>
Date: Thu, 23 Sep 2021 13:59:24 -0700
Subject: [PATCH] perf/x86/intel/uncore: Use pci core's DVSEC functionality

Apply standard interface pci_find_dvsec_capability for perf uncore driver and remove unused macros.

Reduce maintenance burden of DVSEC query implementation.

---
 arch/x86/events/intel/uncore_discovery.c | 41 
+++++++++++++++-----------------
 arch/x86/events/intel/uncore_discovery.h |  6 -----
 2 files changed, 19 insertions(+), 28 deletions(-)
arch/x86/events/intel/uncore_discovery.c +2 -2
index 3049c64..f8ea092 100644
@@ -21,7 +21,7 @@ static bool has_generic_discovery_table(void)
 		return false;
 
 	/* A discovery table device has the unique capability ID. */
-	dvsec = pci_find_next_ext_capability(dev, 0, UNCORE_EXT_CAP_ID_DISCOVERY);
+	dvsec = pci_find_next_ext_capability(dev, 0, PCI_EXT_CAP_ID_DVSEC);
 	pci_dev_put(dev);
 	if (dvsec)
 		return true;
@@ -260,7 +260,7 @@ static int parse_discovery_table(struct pci_dev *dev, int die,
 
 bool intel_uncore_has_discovery_tables(void)
 {
-	u32 device, val, entry_id, bar_offset;
+	u32 device, val, bar_offset;
 	int die, dvsec = 0, ret = true;
 	struct pci_dev *dev = NULL;
 	bool parsed = false;
@@ -275,27 +275,24 @@ bool intel_uncore_has_discovery_tables(void)
 	 * the discovery table devices.
 	 */
 	while ((dev = pci_get_device(PCI_VENDOR_ID_INTEL, device, dev)) != 

NULL) {
- while ((dvsec = pci_find_next_ext_capability(dev, dvsec, UNCORE_EXT_CAP_ID_DISCOVERY))) {
- pci_read_config_dword(dev, dvsec + UNCORE_DISCOVERY_DVSEC_OFFSET, &val);
- entry_id = val & UNCORE_DISCOVERY_DVSEC_ID_MASK;
- if (entry_id != UNCORE_DISCOVERY_DVSEC_ID_PMON)
- continue;
-
- pci_read_config_dword(dev, dvsec + UNCORE_DISCOVERY_DVSEC2_OFFSET, &val);
-
- if (val & ~UNCORE_DISCOVERY_DVSEC2_BIR_MASK) {
- ret = false;
- goto err;
- }
- bar_offset = UNCORE_DISCOVERY_BIR_BASE +
- (val & UNCORE_DISCOVERY_DVSEC2_BIR_MASK) * UNCORE_DISCOVERY_BIR_STEP;
-
- die = get_device_die_id(dev);
- if (die < 0)
- continue;
-
- parse_discovery_table(dev, die, bar_offset, &parsed);
+ dvsec = pci_find_dvsec_capability(dev, PCI_VENDOR_ID_INTEL, UNCORE_DISCOVERY_DVSEC_ID_PMON);
+ if (!dvsec)
+ continue;
+
+ pci_read_config_dword(dev, dvsec + UNCORE_DISCOVERY_DVSEC2_OFFSET, &val);
+
+ if (val & ~UNCORE_DISCOVERY_DVSEC2_BIR_MASK) {
+ ret = false;
+ goto err;
}
+ bar_offset = UNCORE_DISCOVERY_BIR_BASE +
+ (val & UNCORE_DISCOVERY_DVSEC2_BIR_MASK) * UNCORE_DISCOVERY_BIR_STEP;
+
+ die = get_device_die_id(dev);
+ if (die < 0)
+ continue;
+
+ parse_discovery_table(dev, die, bar_offset, &parsed);
}

/* None of the discovery tables are available */

arch/x86/events/intel/uncore_discovery.h +0 -6
index 6d735611..84d56e5 100644
@@ -2,12 +2,6 @@
 
 /* Generic device ID of a discovery table device */
 #define UNCORE_DISCOVERY_TABLE_DEVICE		0x09a7
-/* Capability ID for a discovery table device */
-#define UNCORE_EXT_CAP_ID_DISCOVERY		0x23
-/* First DVSEC offset */
-#define UNCORE_DISCOVERY_DVSEC_OFFSET		0x8
-/* Mask of the supported discovery entry type */
-#define UNCORE_DISCOVERY_DVSEC_ID_MASK		0xffff
 /* PMON discovery entry type ID */
 #define UNCORE_DISCOVERY_DVSEC_ID_PMON		0x1
 /* Second DVSEC offset */
Signature · 4 lines
2.7.4

Thanks,
Kan
Quoted 61 lines
---
  drivers/pci/pci.c   | 32 ++++++++++++++++++++++++++++++++
  include/linux/pci.h |  1 +
  2 files changed, 33 insertions(+)
drivers/pci/pci.c +32 -0
index ce2ab62b64cf..94ac86ff28b0 100644
@@ -732,6 +732,38 @@ u16 pci_find_vsec_capability(struct pci_dev *dev, u16 vendor, int cap)
  }
  EXPORT_SYMBOL_GPL(pci_find_vsec_capability);
 
+/**
+ * pci_find_dvsec_capability - Find DVSEC for vendor
+ * @dev: PCI device to query
+ * @vendor: Vendor ID to match for the DVSEC
+ * @dvsec: Designated Vendor-specific capability ID
+ *
+ * If DVSEC has Vendor ID @vendor and DVSEC ID @dvsec return the capability
+ * offset in config space; otherwise return 0.
+ */
+u16 pci_find_dvsec_capability(struct pci_dev *dev, u16 vendor, u16 dvsec)
+{
+	int pos;
+
+	pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_DVSEC);
+	if (!pos)
+		return 0;
+
+	while (pos) {
+		u16 v, id;
+
+		pci_read_config_word(dev, pos + PCI_DVSEC_HEADER1, &v);
+		pci_read_config_word(dev, pos + PCI_DVSEC_HEADER2, &id);
+		if (vendor == v && dvsec == id)
+			return pos;
+
+		pos = pci_find_next_ext_capability(dev, pos, PCI_EXT_CAP_ID_DVSEC);
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(pci_find_dvsec_capability);
+
  /**
   * pci_find_parent_resource - return resource region of parent bus of given
   *			      region
include/linux/pci.h +1 -0
index cd8aa6fce204..c93ccfa4571b 100644
@@ -1130,6 +1130,7 @@ u16 pci_find_ext_capability(struct pci_dev *dev, int cap);
  u16 pci_find_next_ext_capability(struct pci_dev *dev, u16 pos, int cap);
  struct pci_bus *pci_find_next_bus(const struct pci_bus *from);
  u16 pci_find_vsec_capability(struct pci_dev *dev, u16 vendor, int cap);
+u16 pci_find_dvsec_capability(struct pci_dev *dev, u16 vendor, u16 dvsec);
 
  u64 pci_get_dsn(struct pci_dev *dev);
 
#21 lore raw

Re: [PATCH v2 9/9] iommu/vt-d: Use pci core's DVSEC functionality

In reply to Ben Widawsky

drivers/iommu/intel/iommu.c static int intel_iommu_disable_sva(struct device *dev) · line 5402

Quoted 5 lines · Ben Widawsky

*/
static int siov_find_pci_dvsec(struct pci_dev *pdev) {

drivers/iommu/intel/iommu.c +1 -0
+	return pci_find_dvsec_capability(pdev, PCI_VENDOR_ID_INTEL, 5);
 }

I hink the siov_find_pci_dvsec helper is pretty pointless now and can be folded into its only caller. And independent of that: this capability really needs a symbolic name. Especially for a vendor like Intel that might have a few there should be a list of them somewhere.

Acked-by on v2 6/9
#13 lore raw

Re: [PATCH v2 6/9] PCI: Add pci_find_dvsec_capability to find designated VSEC

In reply to Ben Widawsky

s/pci_find_dvsec_capability/pci_find_dvsec_capability()/ in subject and commit log.

Quoted 2 lines · Ben Widawsky

Add pci_find_dvsec_capability to locate a Designated Vendor-Specific Extended Capability with the specified DVSEC ID.

"specified Vendor ID and Capability ID".

Quoted 6 lines

The Designated Vendor-Specific Extended Capability (DVSEC) allows one or more vendor specific capabilities that aren't tied to the vendor ID of the PCI component.

DVSEC is critical for both the Compute Express Link (CXL) driver as well as the driver for OpenCAPI coherent accelerator (OCXL).

Strictly speaking, not really relevant for the commit log.

Quoted 10 lines
Cc David E. Box <[email protected]>
Cc Jonathan Cameron <[email protected]>
Cc Bjorn Helgaas <[email protected]>
Cc Dan Williams <[email protected]>
Cc Andrew Donnellan <[email protected]>
Cc Lu Baolu <[email protected]>

If you want to merge this with the series,

Or if you want me to merge this on a branch, let me know.

Quoted 63 lines
---
 drivers/pci/pci.c   | 32 ++++++++++++++++++++++++++++++++
 include/linux/pci.h |  1 +
 2 files changed, 33 insertions(+)
drivers/pci/pci.c +32 -0
index ce2ab62b64cf..94ac86ff28b0 100644
@@ -732,6 +732,38 @@ u16 pci_find_vsec_capability(struct pci_dev *dev, u16 vendor, int cap)
 }
 EXPORT_SYMBOL_GPL(pci_find_vsec_capability);
 
+/**
+ * pci_find_dvsec_capability - Find DVSEC for vendor
+ * @dev: PCI device to query
+ * @vendor: Vendor ID to match for the DVSEC
+ * @dvsec: Designated Vendor-specific capability ID
+ *
+ * If DVSEC has Vendor ID @vendor and DVSEC ID @dvsec return the capability
+ * offset in config space; otherwise return 0.
+ */
+u16 pci_find_dvsec_capability(struct pci_dev *dev, u16 vendor, u16 dvsec)
+{
+	int pos;
+
+	pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_DVSEC);
+	if (!pos)
+		return 0;
+
+	while (pos) {
+		u16 v, id;
+
+		pci_read_config_word(dev, pos + PCI_DVSEC_HEADER1, &v);
+		pci_read_config_word(dev, pos + PCI_DVSEC_HEADER2, &id);
+		if (vendor == v && dvsec == id)
+			return pos;
+
+		pos = pci_find_next_ext_capability(dev, pos, PCI_EXT_CAP_ID_DVSEC);
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(pci_find_dvsec_capability);
+
 /**
  * pci_find_parent_resource - return resource region of parent bus of given
  *			      region
include/linux/pci.h +1 -0
index cd8aa6fce204..c93ccfa4571b 100644
@@ -1130,6 +1130,7 @@ u16 pci_find_ext_capability(struct pci_dev *dev, int cap);
 u16 pci_find_next_ext_capability(struct pci_dev *dev, u16 pos, int cap);
 struct pci_bus *pci_find_next_bus(const struct pci_bus *from);
 u16 pci_find_vsec_capability(struct pci_dev *dev, u16 vendor, int cap);
+u16 pci_find_dvsec_capability(struct pci_dev *dev, u16 vendor, u16 dvsec);
 
 u64 pci_get_dsn(struct pci_dev *dev);
 
Signature · 1 line
2.33.0

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#2 lore raw

Re: [PATCH v2 1/9] cxl: Convert "RBI" to enum

In reply to Ben Widawsky

Please spell out "register block indicator" in the subject so that the shortlog remains somewhat readable.

Quoted 4 lines · Ben Widawsky

In preparation for passing around the Register Block Indicator (RBI) as a parameter, it is desirable to convert the type to an enum so that the interface can use a well defined type checked parameter.

C wouldn't type check this unless it failed an integer conversion, right? It would need to be a struct to get useful type checking.

I don't mind this for the self documenting properties it has for the functions that will take this as a parameter, but maybe clarify what you mean by type checked parameter?

Quoted 7 lines

As a result of this change, should future versions of the spec add sparsely defined identifiers, it could become a problem if checking for invalid identifiers since the code currently checks for the max identifier. This is not an issue with current spec, and the algorithm to obtain the register blocks will change before those possible additions are made.

In general let's not spend changelog space trying to guess what future specs may or may not do. I.e. I think this text can be dropped, especially because enums can support sparse number spaces.

Reviewed-by on v2 6/9
#14 lore raw

Re: [PATCH v2 6/9] PCI: Add pci_find_dvsec_capability to find designated VSEC

In reply to Ben Widawsky
Quoted 10 lines · Ben Widawsky

Add pci_find_dvsec_capability to locate a Designated Vendor-Specific Extended Capability with the specified DVSEC ID.

The Designated Vendor-Specific Extended Capability (DVSEC) allows one or more vendor specific capabilities that aren't tied to the vendor ID of the PCI component.

DVSEC is critical for both the Compute Express Link (CXL) driver as well as the driver for OpenCAPI coherent accelerator (OCXL).

Quoted 10 lines
Cc David E. Box <[email protected]>
Cc Jonathan Cameron <[email protected]>
Cc Bjorn Helgaas <[email protected]>
Cc Dan Williams <[email protected]>
Cc Andrew Donnellan <[email protected]>
Cc Lu Baolu <[email protected]>

Looks good to me, it's essentially identical to the existing implementation in ocxl.

Signature · 2 lines
Andrew Donnellan              OzLabs, ADL Canberra
[email protected]             IBM Australia Limited
Reviewed-by on v2 8/9
#19 lore raw

Re: [PATCH v2 8/9] ocxl: Use pci core's DVSEC functionality

In reply to Ben Widawsky
Quoted 8 lines · Ben Widawsky

Reduce maintenance burden of DVSEC query implementation by using the centralized PCI core implementation.

There are two obvious places to simply drop in the new core implementation. There remains find_dvsec_from_pos() which would benefit from using a core implementation. As that change is less trivial it is reserved for later.

Quoted 4 lines

Looks fine, but we should clean up find_dvsec_from_pos() afterwards.

Signature · 2 lines
Andrew Donnellan              OzLabs, ADL Canberra
[email protected]             IBM Australia Limited
Reviewed-by on v2 2/9
#4 lore raw

Re: [PATCH v2 2/9] cxl/pci: Remove dev_dbg for unknown register blocks

In reply to Ben Widawsky
Quoted 6 lines · Ben Widawsky

While interesting to driver developers, the dev_dbg message doesn't do much except clutter up logs. This information should be attainable through sysfs, and someday lspci like utilities. This change additionally helps reduce the LOC in a subsequent patch to refactor some of cxl_pci register mapping.

Looks good to me:

Reviewed-by on v2 3/9
#6 lore raw

Re: [PATCH v2 3/9] cxl/pci: Remove pci request/release regions

In reply to Ben Widawsky
Quoted 6 lines · Ben Widawsky

Quoting Dan, "... the request + release regions should probably just be dropped. It's not like any of the register enumeration would collide with someone else who already has the registers mapped. The collision only comes when the registers are mapped for their final usage, and that will have more precision in the request."

Looks good to me:

Quoted 2 lines

This isn't one of the canonical tags:
Documentation/process/submitting-patches.rst

I'll change this to Suggested-by:

Quoted 31 lines
---
 drivers/cxl/pci.c | 5 -----
 1 file changed, 5 deletions(-)
drivers/cxl/pci.c +0 -6
index ccc7c2573ddc..7256c236fdb3 100644
@@ -453,9 +453,6 @@ static int cxl_pci_setup_regs(struct cxl_mem *cxlm)
                return -ENXIO;
        }
 
-       if (pci_request_mem_regions(pdev, pci_name(pdev)))
-               return -ENODEV;
-
        /* Get the size of the Register Locator DVSEC */
        pci_read_config_dword(pdev, regloc + PCI_DVSEC_HEADER1, &regloc_size);
        regloc_size = FIELD_GET(PCI_DVSEC_HEADER1_LENGTH_MASK, regloc_size);
@@ -499,8 +496,6 @@ static int cxl_pci_setup_regs(struct cxl_mem *cxlm)
                n_maps++;
        }
 
-       pci_release_mem_regions(pdev);
-
        for (i = 0; i < n_maps; i++) {
                ret = cxl_map_regs(cxlm, &maps[i]);
                if (ret)
--

2.33.0

#8 lore raw

Re: [PATCH v2 4/9] cxl/pci: Refactor cxl_pci_setup_regs

In reply to Ben Widawsky
Quoted 22 lines · Ben Widawsky

In preparation for moving parts of register mapping to cxl_core, the cxl_pci driver is refactored to utilize a new helper to find register blocks by type.

cxl_pci scanned through all register blocks and mapping the ones that the driver will use. This logic is inverted so that the driver specifically requests the register blocks from a new helper. Under the hood, the same implementation of scanning through all register locator DVSEC entries exists.

There are 2 behavioral changes (#2 is arguable):
1. A dev_err is introduced if cxl_map_regs fails.
2. The previous logic would try to map component registers and device registers multiple times if there were present and keep the mapping of the last one found (furthest offset in the register locator). While this is disallowed in the spec, CXL 2.0 8.1.9: "Each register block identifier shall only occur once in the Register Locator DVSEC structure" it was how the driver would respond to the spec violation. The new logic will take the first found register block by type and move on.

Quoted 4 lines

---
Changes since v1:

No changes? Luckily git am strips this section...

Overall I think this refactor can be broken down further for readability and cleanup the long standing problem that the driver maps component registers for no reason. The main contributing factor to readability is that cxl_setup_pci_regs() still exists after the refactor, which also contributes to the component register problem. If the register mapping is up leveled to the caller of cxl_setup_pci_regs() (and drops mapping component registers) then a follow-on patch to rename cxl_setup_pci_regs to find_register_block becomes easier to read. Moving the cxl_register_map array out of cxl_setup_pci_regs() also makes a later patch to pass component register enumeration details to the endpoint-port that much cleaner.

#10 lore raw

Re: [PATCH v2 5/9] cxl/pci: Make more use of cxl_register_map

In reply to Ben Widawsky
Quoted 3 lines · Ben Widawsky

The structure exists to pass around information about register mapping. Using it more extensively cleans up many existing functions.

I would have liked to have seen "add @base to cxl_register_map" and "use @map for @bar and @offset arguments" somewhere in this changelog to set expectations for what changes are included. That would have also highlighted that adding a @base to cxl_register_map deserves its own patch vs the conversion of @bar and @offset to instead use @map->bar and @map->offset. Can you resend with that split and those mentions?

#17 lore raw

Re: [PATCH v2 7/9] cxl/pci: Use pci core's DVSEC functionality

In reply to Ben Widawsky
Quoted 14 lines · Ben Widawsky

Reduce maintenance burden of DVSEC query implementation by using the centralized PCI core implementation.

---
 drivers/cxl/pci.c | 20 +-------------------
 1 file changed, 1 insertion(+), 19 deletions(-)
drivers/cxl/pci.c +0 -0
index 5eaf2736f779..79d4d9b16d83 100644

drivers/cxl/pci.c static void cxl_pci_unmap_regblock(struct cxl_mem *cxlm, struct cxl_register_map · line 341

Quoted 2 lines
drivers/cxl/pci.c +0 -0
@@ -340,25 +340,7 @@ static void cxl_pci_unmap_regblock(struct cxl_mem *cxlm, struct cxl_register_map
 
 static int cxl_pci_dvsec(struct pci_dev *pdev, int dvsec)

cxl_pci_dvsec() has no reason to exist anymore. Let's just have the caller use pci_find_dvsec_capability() directly.
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#22 lore raw

Re: [PATCH v2 9/9] iommu/vt-d: Use pci core's DVSEC functionality

In reply to Ben Widawsky
Quoted 26 lines · Ben Widawsky

Reduce maintenance burden of DVSEC query implementation by using the centralized PCI core implementation.

---
 drivers/iommu/intel/iommu.c | 15 +--------------
 1 file changed, 1 insertion(+), 14 deletions(-)
drivers/iommu/intel/iommu.c +0 -6
index d75f59ae28e6..30c97181f0ae 100644
@@ -5398,20 +5398,7 @@ static int intel_iommu_disable_sva(struct device *dev)
  */
 static int siov_find_pci_dvsec(struct pci_dev *pdev)
 {
-       int pos;
-       u16 vendor, id;
-
-       pos = pci_find_next_ext_capability(pdev, 0, 0x23);
-       while (pos) {
-               pci_read_config_word(pdev, pos + 4, &vendor);

drivers/iommu/intel/iommu.c static int intel_iommu_disable_sva(struct device *dev) · line 5402

Quoted 10 lines
drivers/iommu/intel/iommu.c +1 -8
-               pci_read_config_word(pdev, pos + 8, &id);
-               if (vendor == PCI_VENDOR_ID_INTEL && id == 5)
-                       return pos;
-
-               pos = pci_find_next_ext_capability(pdev, pos, 0x23);
-       }
-
-       return 0;
+       return pci_find_dvsec_capability(pdev, PCI_VENDOR_ID_INTEL, 5);
 }

Same comments as the CXL patch, siov_find_pci_dvsec() doesn't seem to have a reason to exist anymore. What is 5?

maintainer
#23 lore raw

Re: [PATCH v2 9/9] iommu/vt-d: Use pci core's DVSEC functionality

Hi Dan,

Quoted 32 lines · Dan Williams
Quoted 31 lines · Ben Widawsky

Reduce maintenance burden of DVSEC query implementation by using the centralized PCI core implementation.

---
  drivers/iommu/intel/iommu.c | 15 +--------------
  1 file changed, 1 insertion(+), 14 deletions(-)
drivers/iommu/intel/iommu.c +0 -11
index d75f59ae28e6..30c97181f0ae 100644
@@ -5398,20 +5398,7 @@ static int intel_iommu_disable_sva(struct device *dev)
   */
  static int siov_find_pci_dvsec(struct pci_dev *pdev)
  {
-       int pos;
-       u16 vendor, id;
-
-       pos = pci_find_next_ext_capability(pdev, 0, 0x23);
-       while (pos) {
-               pci_read_config_word(pdev, pos + 4, &vendor);
-               pci_read_config_word(pdev, pos + 8, &id);
-               if (vendor == PCI_VENDOR_ID_INTEL && id == 5)
-                       return pos;
-
-               pos = pci_find_next_ext_capability(pdev, pos, 0x23);
Quoted 8 lines

drivers/iommu/intel/iommu.c static int intel_iommu_disable_sva(struct device *dev) · line 5402

Quoted 5 lines
drivers/iommu/intel/iommu.c +1 -3
-       }
-
-       return 0;
+       return pci_find_dvsec_capability(pdev, PCI_VENDOR_ID_INTEL, 5);
  }

Same comments as the CXL patch, siov_find_pci_dvsec() doesn't seem to have a reason to exist anymore. What is 5?

"5" is DVSEC ID for Scalable IOV.

Anyway, the siov_find_pci_dvsec() has been dead code since commit 262948f8ba57 ("iommu: Delete iommu_dev_has_feature()"). I have a patch to clean it up. No need to care about it in this series.

Best regards,
baolu

Reviewed-by on v2 6/9
#15 lore raw

Re: [PATCH v2 6/9] PCI: Add pci_find_dvsec_capability to find designated VSEC

In reply to Ben Widawsky
Quoted 10 lines · On Thu, 23 Sep 2021 10:26:44 -0700 Ben Widawsky <[email protected]> wrote:

Add pci_find_dvsec_capability to locate a Designated Vendor-Specific Extended Capability with the specified DVSEC ID.

The Designated Vendor-Specific Extended Capability (DVSEC) allows one or more vendor specific capabilities that aren't tied to the vendor ID of the PCI component.

DVSEC is critical for both the Compute Express Link (CXL) driver as well as the driver for OpenCAPI coherent accelerator (OCXL).

Quoted 10 lines
Cc David E. Box <[email protected]>
Cc Jonathan Cameron <[email protected]>
Cc Bjorn Helgaas <[email protected]>
Cc Dan Williams <[email protected]>
Cc Andrew Donnellan <[email protected]>
Cc Lu Baolu <[email protected]>

Great to see this cleaned up.

Quoted 60 lines
---
 drivers/pci/pci.c   | 32 ++++++++++++++++++++++++++++++++
 include/linux/pci.h |  1 +
 2 files changed, 33 insertions(+)
drivers/pci/pci.c +32 -0
index ce2ab62b64cf..94ac86ff28b0 100644
@@ -732,6 +732,38 @@ u16 pci_find_vsec_capability(struct pci_dev *dev, u16 vendor, int cap)
 }
 EXPORT_SYMBOL_GPL(pci_find_vsec_capability);
 
+/**
+ * pci_find_dvsec_capability - Find DVSEC for vendor
+ * @dev: PCI device to query
+ * @vendor: Vendor ID to match for the DVSEC
+ * @dvsec: Designated Vendor-specific capability ID
+ *
+ * If DVSEC has Vendor ID @vendor and DVSEC ID @dvsec return the capability
+ * offset in config space; otherwise return 0.
+ */
+u16 pci_find_dvsec_capability(struct pci_dev *dev, u16 vendor, u16 dvsec)
+{
+	int pos;
+
+	pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_DVSEC);
+	if (!pos)
+		return 0;
+
+	while (pos) {
+		u16 v, id;
+
+		pci_read_config_word(dev, pos + PCI_DVSEC_HEADER1, &v);
+		pci_read_config_word(dev, pos + PCI_DVSEC_HEADER2, &id);
+		if (vendor == v && dvsec == id)
+			return pos;
+
+		pos = pci_find_next_ext_capability(dev, pos, PCI_EXT_CAP_ID_DVSEC);
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(pci_find_dvsec_capability);
+
 /**
  * pci_find_parent_resource - return resource region of parent bus of given
  *			      region
include/linux/pci.h +1 -0
index cd8aa6fce204..c93ccfa4571b 100644
@@ -1130,6 +1130,7 @@ u16 pci_find_ext_capability(struct pci_dev *dev, int cap);
 u16 pci_find_next_ext_capability(struct pci_dev *dev, u16 pos, int cap);
 struct pci_bus *pci_find_next_bus(const struct pci_bus *from);
 u16 pci_find_vsec_capability(struct pci_dev *dev, u16 vendor, int cap);
+u16 pci_find_dvsec_capability(struct pci_dev *dev, u16 vendor, u16 dvsec);
 
 u64 pci_get_dsn(struct pci_dev *dev);