From: Manish Honap vfio_find_cap_start() walks through pci_config_map while the bytes hold same capability ID. When two DVSEC's are back-to-back, we cannot correctly tell what is the start of second capability. Due to this the offset into the second capability resolves to the start of first capability. For above case, walk the extended vconfig capability chain for determining the correct start of the abutting capabilities. Standard capabilities keep the walk back through the map. Signed-off-by: Manish Honap --- drivers/vfio/pci/vfio_pci_config.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/drivers/vfio/pci/vfio_pci_config.c b/drivers/vfio/pci/vfio_pci_config.c index 9914f3ac69ae..dd0b899fbdfd 100644 --- a/drivers/vfio/pci/vfio_pci_config.c +++ b/drivers/vfio/pci/vfio_pci_config.c @@ -1129,6 +1129,23 @@ int __init vfio_pci_init_perm_bits(void) return ret; } +static int vfio_find_ecap_start(struct vfio_pci_core_device *vdev, int pos) +{ + struct pci_dev *pdev = vdev->pdev; + int loops = (pdev->cfg_size - PCI_CFG_SPACE_SIZE) / PCI_CAP_SIZEOF; + int epos = PCI_CFG_SPACE_SIZE, start = PCI_CFG_SPACE_SIZE; + + while (loops-- && epos >= PCI_CFG_SPACE_SIZE) { + u32 header = le32_to_cpu(*(__le32 *)&vdev->vconfig[epos]); + + if (epos <= pos && epos > start) + start = epos; + epos = PCI_EXT_CAP_NEXT(header); + } + + return start; +} + static int vfio_find_cap_start(struct vfio_pci_core_device *vdev, int pos) { u8 cap; @@ -1139,6 +1156,10 @@ static int vfio_find_cap_start(struct vfio_pci_core_device *vdev, int pos) if (cap == PCI_CAP_ID_BASIC) return 0; + /* Abutting DVSECs share one map ID */ + if (pos >= PCI_CFG_SPACE_SIZE) + return vfio_find_ecap_start(vdev, pos); + /* XXX Can we have to abutting capabilities of the same type? */ while (pos - 1 >= base && vdev->pci_config_map[pos - 1] == cap) pos--; -- 2.53.0 From: Manish Honap Since commit 6467d0740a2b ("vfio/pci: Add DVSEC PCI Extended Config Capability to user visible list."), vfio-pci passes user writes to every DVSEC through to the device. Before that commit DVSECs were hidden. For a function that is CXL.cache and CXL.mem capable, the user can then: - clear Cache_Enable or Mem_Enable in DVSEC Control, - set Initiate CXL Reset or Initiate Cache Writeback and Invalidation in DVSEC Control2, which the host does not see or handle, - write the DVSEC Range registers, - set Config Lock, after which the host cannot change the locked registers. This change keeps the CXL Device DVSEC visible and makes it read-only: reads return the device values and writes are dropped. The capability is matched by its start, so a CXL DVSEC that directly follows another DVSEC relies on the earlier patch to correctly calculate start of abutting capabilities. Fixes: 6467d0740a2b ("vfio/pci: Add DVSEC PCI Extended Config Capability to user visible list.") Signed-off-by: Manish Honap --- drivers/vfio/pci/vfio_pci_config.c | 23 +++++++++++++++++++++++ include/linux/vfio_pci_core.h | 1 + 2 files changed, 24 insertions(+) diff --git a/drivers/vfio/pci/vfio_pci_config.c b/drivers/vfio/pci/vfio_pci_config.c index dd0b899fbdfd..2bc0c7cbc4b7 100644 --- a/drivers/vfio/pci/vfio_pci_config.c +++ b/drivers/vfio/pci/vfio_pci_config.c @@ -1746,6 +1746,23 @@ static int vfio_ecap_init(struct vfio_pci_core_device *vdev) return 0; } +static u16 vfio_pci_cxl_dvsec(struct pci_dev *pdev) +{ + u16 dvsec, cap; + + dvsec = pci_find_dvsec_capability(pdev, PCI_VENDOR_ID_CXL, + PCI_DVSEC_CXL_DEVICE); + if (!dvsec || + pci_read_config_word(pdev, dvsec + PCI_DVSEC_CXL_CAP, &cap)) + return 0; + + if (!(cap & PCI_DVSEC_CXL_CACHE_CAPABLE) || + !(cap & PCI_DVSEC_CXL_MEM_CAPABLE)) + return 0; + + return dvsec; +} + /* * Nag about hardware bugs, hopefully to have vendors fix them, but at least * to collect a list of dependencies for the VF INTx pin quirk below. @@ -1863,6 +1880,8 @@ int vfio_config_init(struct vfio_pci_core_device *vdev) if (ret) goto out; + vdev->cxl_dvsec = vfio_pci_cxl_dvsec(vdev->pdev); + return 0; out: @@ -1953,6 +1972,10 @@ ssize_t vfio_pci_config_rw_single(struct vfio_pci_core_device *vdev, perm = &ecap_perms[cap_id]; cap_start = vfio_find_cap_start(vdev, *ppos); + + if (cap_id == PCI_EXT_CAP_ID_DVSEC && + cap_start == vdev->cxl_dvsec) + perm = &direct_ro_perms; } else { WARN_ON(cap_id > PCI_CAP_ID_MAX); diff --git a/include/linux/vfio_pci_core.h b/include/linux/vfio_pci_core.h index 2f3fa2787737..dc25f737fe93 100644 --- a/include/linux/vfio_pci_core.h +++ b/include/linux/vfio_pci_core.h @@ -109,6 +109,7 @@ struct vfio_pci_core_device { u8 *pci_config_map; u8 *vconfig; struct perm_bits *msi_perm; + u16 cxl_dvsec; spinlock_t irqlock; struct mutex igate; struct xarray ctx; -- 2.53.0