Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

PCI/DOE: Expose DOE features via sysfs

PCIe r6.0 added support for Data Object Exchange (DOE). When DOE is
supported, the DOE Discovery Feature must be implemented per PCIe r6.1, sec
6.30.1.1. DOE allows a requester to obtain information about the other DOE
features supported by the device.

The kernel already queries the DOE features supported and caches the
values. Expose the values in sysfs to allow user space to determine which
DOE features are supported by the PCIe device.

By exposing the information to userspace, tools like lspci can relay the
information to users. By listing all of the supported features we can allow
userspace to parse the list, which might include vendor specific features
as well as yet to be supported features.

As the DOE Discovery feature must always be supported we treat it as a
special named attribute case. This allows the usual PCI attribute_group
handling to correctly create the doe_features directory when registering
pci_doe_sysfs_group (otherwise it doesn't and sysfs_add_file_to_group()
will seg fault).

After this patch is supported you can see something like this when
attaching a DOE device:

$ ls /sys/devices/pci0000:00/0000:00:02.0//doe*
0001:01 0001:02 doe_discovery

Link: https://lore.kernel.org/r/20250306075211.1855177-3-alistair@alistair23.me
Signed-off-by: Alistair Francis <alistair@alistair23.me>
[bhelgaas: drop pci_doe_sysfs_init() stub return, make
DEVICE_ATTR_RO(doe_discovery) static]
Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

authored by

Alistair Francis and committed by
Bjorn Helgaas
2311ab18 f810d177

+197
+29
Documentation/ABI/testing/sysfs-bus-pci
··· 583 583 enclosure-specific indications "specific0" to "specific7", 584 584 hence the corresponding led class devices are unavailable if 585 585 the DSM interface is used. 586 + 587 + What: /sys/bus/pci/devices/.../doe_features 588 + Date: March 2025 589 + Contact: Linux PCI developers <linux-pci@vger.kernel.org> 590 + Description: 591 + This directory contains a list of the supported Data Object 592 + Exchange (DOE) features. The features are the file name. 593 + The contents of each file is the raw Vendor ID and data 594 + object feature values. 595 + 596 + The value comes from the device and specifies the vendor and 597 + data object type supported. The lower (RHS of the colon) is 598 + the data object type in hex. The upper (LHS of the colon) 599 + is the vendor ID. 600 + 601 + As all DOE devices must support the DOE discovery feature, 602 + if DOE is supported you will at least see the doe_discovery 603 + file, with this contents: 604 + 605 + # cat doe_features/doe_discovery 606 + 0001:00 607 + 608 + If the device supports other features you will see other 609 + files as well. For example if CMA/SPDM and secure CMA/SPDM 610 + are supported the doe_features directory will look like 611 + this: 612 + 613 + # ls doe_features 614 + 0001:01 0001:02 doe_discovery
+153
drivers/pci/doe.c
··· 14 14 15 15 #include <linux/bitfield.h> 16 16 #include <linux/delay.h> 17 + #include <linux/device.h> 17 18 #include <linux/jiffies.h> 18 19 #include <linux/mutex.h> 19 20 #include <linux/pci.h> 20 21 #include <linux/pci-doe.h> 22 + #include <linux/sysfs.h> 21 23 #include <linux/workqueue.h> 22 24 23 25 #include "pci.h" ··· 49 47 * @wq: Wait queue for work item 50 48 * @work_queue: Queue of pci_doe_work items 51 49 * @flags: Bit array of PCI_DOE_FLAG_* flags 50 + * @sysfs_attrs: Array of sysfs device attributes 52 51 */ 53 52 struct pci_doe_mb { 54 53 struct pci_dev *pdev; ··· 59 56 wait_queue_head_t wq; 60 57 struct workqueue_struct *work_queue; 61 58 unsigned long flags; 59 + 60 + #ifdef CONFIG_SYSFS 61 + struct device_attribute *sysfs_attrs; 62 + #endif 62 63 }; 63 64 64 65 struct pci_doe_feature { ··· 98 91 struct work_struct work; 99 92 struct pci_doe_mb *doe_mb; 100 93 }; 94 + 95 + #ifdef CONFIG_SYSFS 96 + static ssize_t doe_discovery_show(struct device *dev, 97 + struct device_attribute *attr, 98 + char *buf) 99 + { 100 + return sysfs_emit(buf, "0001:00\n"); 101 + } 102 + static DEVICE_ATTR_RO(doe_discovery); 103 + 104 + static struct attribute *pci_doe_sysfs_feature_attrs[] = { 105 + &dev_attr_doe_discovery.attr, 106 + NULL 107 + }; 108 + 109 + static bool pci_doe_features_sysfs_group_visible(struct kobject *kobj) 110 + { 111 + struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); 112 + 113 + return !xa_empty(&pdev->doe_mbs); 114 + } 115 + DEFINE_SIMPLE_SYSFS_GROUP_VISIBLE(pci_doe_features_sysfs) 116 + 117 + const struct attribute_group pci_doe_sysfs_group = { 118 + .name = "doe_features", 119 + .attrs = pci_doe_sysfs_feature_attrs, 120 + .is_visible = SYSFS_GROUP_VISIBLE(pci_doe_features_sysfs), 121 + }; 122 + 123 + static ssize_t pci_doe_sysfs_feature_show(struct device *dev, 124 + struct device_attribute *attr, 125 + char *buf) 126 + { 127 + return sysfs_emit(buf, "%s\n", attr->attr.name); 128 + } 129 + 130 + static void pci_doe_sysfs_feature_remove(struct pci_dev *pdev, 131 + struct pci_doe_mb *doe_mb) 132 + { 133 + struct device_attribute *attrs = doe_mb->sysfs_attrs; 134 + struct device *dev = &pdev->dev; 135 + unsigned long i; 136 + void *entry; 137 + 138 + if (!attrs) 139 + return; 140 + 141 + doe_mb->sysfs_attrs = NULL; 142 + xa_for_each(&doe_mb->feats, i, entry) { 143 + if (attrs[i].show) 144 + sysfs_remove_file_from_group(&dev->kobj, &attrs[i].attr, 145 + pci_doe_sysfs_group.name); 146 + kfree(attrs[i].attr.name); 147 + } 148 + kfree(attrs); 149 + } 150 + 151 + static int pci_doe_sysfs_feature_populate(struct pci_dev *pdev, 152 + struct pci_doe_mb *doe_mb) 153 + { 154 + struct device *dev = &pdev->dev; 155 + struct device_attribute *attrs; 156 + unsigned long num_features = 0; 157 + unsigned long vid, type; 158 + unsigned long i; 159 + void *entry; 160 + int ret; 161 + 162 + xa_for_each(&doe_mb->feats, i, entry) 163 + num_features++; 164 + 165 + attrs = kcalloc(num_features, sizeof(*attrs), GFP_KERNEL); 166 + if (!attrs) { 167 + pci_warn(pdev, "Failed allocating the device_attribute array\n"); 168 + return -ENOMEM; 169 + } 170 + 171 + doe_mb->sysfs_attrs = attrs; 172 + xa_for_each(&doe_mb->feats, i, entry) { 173 + sysfs_attr_init(&attrs[i].attr); 174 + vid = xa_to_value(entry) >> 8; 175 + type = xa_to_value(entry) & 0xFF; 176 + 177 + if (vid == PCI_VENDOR_ID_PCI_SIG && 178 + type == PCI_DOE_FEATURE_DISCOVERY) { 179 + 180 + /* 181 + * DOE Discovery, manually displayed by 182 + * `dev_attr_doe_discovery` 183 + */ 184 + continue; 185 + } 186 + 187 + attrs[i].attr.name = kasprintf(GFP_KERNEL, 188 + "%04lx:%02lx", vid, type); 189 + if (!attrs[i].attr.name) { 190 + ret = -ENOMEM; 191 + pci_warn(pdev, "Failed allocating the attribute name\n"); 192 + goto fail; 193 + } 194 + 195 + attrs[i].attr.mode = 0444; 196 + attrs[i].show = pci_doe_sysfs_feature_show; 197 + 198 + ret = sysfs_add_file_to_group(&dev->kobj, &attrs[i].attr, 199 + pci_doe_sysfs_group.name); 200 + if (ret) { 201 + attrs[i].show = NULL; 202 + if (ret != -EEXIST) { 203 + pci_warn(pdev, "Failed adding %s to sysfs group\n", 204 + attrs[i].attr.name); 205 + goto fail; 206 + } else 207 + kfree(attrs[i].attr.name); 208 + } 209 + } 210 + 211 + return 0; 212 + 213 + fail: 214 + pci_doe_sysfs_feature_remove(pdev, doe_mb); 215 + return ret; 216 + } 217 + 218 + void pci_doe_sysfs_teardown(struct pci_dev *pdev) 219 + { 220 + struct pci_doe_mb *doe_mb; 221 + unsigned long index; 222 + 223 + xa_for_each(&pdev->doe_mbs, index, doe_mb) 224 + pci_doe_sysfs_feature_remove(pdev, doe_mb); 225 + } 226 + 227 + void pci_doe_sysfs_init(struct pci_dev *pdev) 228 + { 229 + struct pci_doe_mb *doe_mb; 230 + unsigned long index; 231 + int ret; 232 + 233 + xa_for_each(&pdev->doe_mbs, index, doe_mb) { 234 + ret = pci_doe_sysfs_feature_populate(pdev, doe_mb); 235 + if (ret) 236 + return; 237 + } 238 + } 239 + #endif 101 240 102 241 static int pci_doe_wait(struct pci_doe_mb *doe_mb, unsigned long timeout) 103 242 {
+3
drivers/pci/pci-sysfs.c
··· 1805 1805 #ifdef CONFIG_PCIEASPM 1806 1806 &aspm_ctrl_attr_group, 1807 1807 #endif 1808 + #ifdef CONFIG_PCI_DOE 1809 + &pci_doe_sysfs_group, 1810 + #endif 1808 1811 NULL, 1809 1812 };
+9
drivers/pci/pci.h
··· 253 253 extern const struct attribute_group *pci_dev_attr_groups[]; 254 254 extern const struct attribute_group *pcibus_groups[]; 255 255 extern const struct attribute_group *pci_bus_groups[]; 256 + extern const struct attribute_group pci_doe_sysfs_group; 256 257 #else 257 258 static inline int pci_create_sysfs_dev_files(struct pci_dev *pdev) { return 0; } 258 259 static inline void pci_remove_sysfs_dev_files(struct pci_dev *pdev) { } ··· 455 454 #else 456 455 static inline void pci_npem_create(struct pci_dev *dev) { } 457 456 static inline void pci_npem_remove(struct pci_dev *dev) { } 457 + #endif 458 + 459 + #if defined(CONFIG_PCI_DOE) && defined(CONFIG_SYSFS) 460 + void pci_doe_sysfs_init(struct pci_dev *pci_dev); 461 + void pci_doe_sysfs_teardown(struct pci_dev *pdev); 462 + #else 463 + static inline void pci_doe_sysfs_init(struct pci_dev *pdev) { } 464 + static inline void pci_doe_sysfs_teardown(struct pci_dev *pdev) { } 458 465 #endif 459 466 460 467 /**
+2
drivers/pci/probe.c
··· 2661 2661 WARN_ON(ret < 0); 2662 2662 2663 2663 pci_npem_create(dev); 2664 + 2665 + pci_doe_sysfs_init(dev); 2664 2666 } 2665 2667 2666 2668 struct pci_dev *pci_scan_single_device(struct pci_bus *bus, int devfn)
+1
drivers/pci/remove.c
··· 53 53 if (pci_dev_test_and_set_removed(dev)) 54 54 return; 55 55 56 + pci_doe_sysfs_teardown(dev); 56 57 pci_npem_remove(dev); 57 58 58 59 device_del(&dev->dev);