[PATCH v18 02/19] cxl: Share CXL port upstream PCI device lookup

From: Srirangan Madhavan

Date: Fri Dec 04 2026 - 23:15:20 EST


read_cdat_data() resolves a CXL port's upstream device to its backing PCI
device. HDM cache updates need the same mapping.

Factor the lookup into cxl_port_get_uport_pci_dev() and return a referenced
PCI device to make caller ownership explicit.

Use automatic reference cleanup in read_cdat_data(), as suggested by
Li Ming, so early returns release the PCI device reference.

Signed-off-by: Srirangan Madhavan <smadhavan@xxxxxxxxxx>
Reviewed-by: Jonathan Cameron <jonathan.cameron@xxxxxxxxxxxxxxxx>
Reviewed-by: Dave Jiang <dave.jiang@xxxxxxxxx>
Reviewed-by: Richard Cheng <icheng@xxxxxxxxxx>
Reviewed-by: Li Ming <ming.li@xxxxxxxxxxxx>

---
Changes in v18:
- Restructure read_cdat_data() around a cxl_cdat_read_validate() helper
and __free(pci_dev_put), removing the goto/out layout (Dave Jiang)
---
drivers/cxl/core/core.h | 1 +
drivers/cxl/core/pci.c | 67 ++++++++++++++++++-----------------------
drivers/cxl/core/port.c | 26 ++++++++++++++++
3 files changed, 57 insertions(+), 37 deletions(-)

diff --git a/drivers/cxl/core/core.h b/drivers/cxl/core/core.h
index 79918fde1575..ee639ac2c670 100644
--- a/drivers/cxl/core/core.h
+++ b/drivers/cxl/core/core.h
@@ -157,6 +157,7 @@ long cxl_pci_get_latency(struct pci_dev *pdev);
int cxl_pci_get_bandwidth(struct pci_dev *pdev, struct access_coordinate *c);
int cxl_port_get_switch_dport_bandwidth(struct cxl_port *port,
struct access_coordinate *c);
+struct pci_dev *cxl_port_get_uport_pci_dev(struct cxl_port *port);

static inline struct device *port_to_host(struct cxl_port *port)
{
diff --git a/drivers/cxl/core/pci.c b/drivers/cxl/core/pci.c
index 125abb36df7f..1dd16d0f17f6 100644
--- a/drivers/cxl/core/pci.c
+++ b/drivers/cxl/core/pci.c
@@ -556,6 +556,27 @@ static unsigned char cdat_checksum(void *buf, size_t size)
return sum;
}

+static int cxl_cdat_read_validate(struct device *dev,
+ struct pci_doe_mb *doe_mb,
+ struct cdat_doe_rsp *buf, size_t *length)
+{
+ size_t table_length = *length;
+ int rc;
+
+ rc = cxl_cdat_read_table(dev, doe_mb, buf, length);
+ if (rc)
+ return rc;
+
+ if (table_length != *length)
+ dev_warn(dev, "Malformed CDAT table length (%zu:%zu), discarding trailing data\n",
+ table_length, *length);
+
+ if (cdat_checksum(buf->data, *length))
+ return -EIO;
+
+ return 0;
+}
+
/**
* read_cdat_data - Read the CDAT data on this port
* @port: Port to read data from
@@ -564,26 +585,13 @@ static unsigned char cdat_checksum(void *buf, size_t size)
*/
void read_cdat_data(struct cxl_port *port)
{
- struct device *uport = port->uport_dev;
struct device *dev = &port->dev;
struct pci_doe_mb *doe_mb;
- struct pci_dev *pdev = NULL;
- struct cxl_memdev *cxlmd;
struct cdat_doe_rsp *buf;
- size_t table_length, length;
- int rc;
-
- if (is_cxl_memdev(uport)) {
- struct device *host;
-
- cxlmd = to_cxl_memdev(uport);
- host = cxlmd->dev.parent;
- if (dev_is_pci(host))
- pdev = to_pci_dev(host);
- } else if (dev_is_pci(uport)) {
- pdev = to_pci_dev(uport);
- }
+ size_t length;

+ struct pci_dev *pdev __free(pci_dev_put) =
+ cxl_port_get_uport_pci_dev(port);
if (!pdev)
return;

@@ -606,30 +614,15 @@ void read_cdat_data(struct cxl_port *port)
* bytes for the DOE header. Table data starts afterwards.
*/
buf = devm_kzalloc(dev, sizeof(*buf) + length, GFP_KERNEL);
- if (!buf)
- goto err;
-
- table_length = length;
-
- rc = cxl_cdat_read_table(dev, doe_mb, buf, &length);
- if (rc)
- goto err;
-
- if (table_length != length)
- dev_warn(dev, "Malformed CDAT table length (%zu:%zu), discarding trailing data\n",
- table_length, length);
-
- if (cdat_checksum(buf->data, length))
- goto err;
+ if (!buf || cxl_cdat_read_validate(dev, doe_mb, buf, &length)) {
+ /* Don't leave table data allocated on error */
+ devm_kfree(dev, buf);
+ dev_err(dev, "Failed to read/validate CDAT.\n");
+ return;
+ }

port->cdat.table = buf->data;
port->cdat.length = length;
-
- return;
-err:
- /* Don't leave table data allocated on error */
- devm_kfree(dev, buf);
- dev_err(dev, "Failed to read/validate CDAT.\n");
}
EXPORT_SYMBOL_NS_GPL(read_cdat_data, "CXL");

diff --git a/drivers/cxl/core/port.c b/drivers/cxl/core/port.c
index f3705ea44db7..e4d593a36514 100644
--- a/drivers/cxl/core/port.c
+++ b/drivers/cxl/core/port.c
@@ -34,6 +34,32 @@
static DEFINE_IDA(cxl_port_ida);
static DEFINE_XARRAY(cxl_root_buses);

+/**
+ * cxl_port_get_uport_pci_dev - get the PCI device for a port's upstream device
+ * @port: CXL port to map to a PCI device
+ *
+ * Return: A referenced PCI device, or NULL if the upstream device is not PCI.
+ * The caller must release the returned reference with pci_dev_put().
+ */
+struct pci_dev *cxl_port_get_uport_pci_dev(struct cxl_port *port)
+{
+ struct device *uport = port->uport_dev;
+ struct device *host;
+
+ if (is_cxl_memdev(uport)) {
+ struct cxl_memdev *cxlmd = to_cxl_memdev(uport);
+
+ host = cxlmd->dev.parent;
+ } else {
+ host = uport;
+ }
+
+ if (!host || !dev_is_pci(host))
+ return NULL;
+
+ return pci_dev_get(to_pci_dev(host));
+}
+
/*
* The terminal device in PCI is NULL and @platform_bus
* for platform devices (for cxl_test)
--
2.43.0