Skip to content

Commit e1d3f32

Browse files
kwilczynskibjorn-helgaas
authored andcommitted
PCI/sysfs: Convert "config" to static attribute
The "config" sysfs attribute allows access to either the legacy (PCI and PCI-X Mode 1) or the extended (PCI-X Mode 2 and PCIe) device configuration space. Previously it was dynamically created either when a device was added (for hot-added devices) or via a late_initcall (for devices present at boot): pci_bus_add_devices pci_bus_add_device pci_create_sysfs_dev_files if (!sysfs_initialized) return sysfs_create_bin_file # for hot-added devices pci_sysfs_init # late_initcall sysfs_initialized = 1 for_each_pci_dev(pdev) pci_create_sysfs_dev_files(pdev) # for devices present at boot And dynamically removed when the PCI device is stopped and removed: pci_stop_bus_device pci_stop_dev pci_remove_sysfs_dev_files sysfs_remove_bin_file This attribute does not need to be created or removed dynamically, so we can use a static attribute so the device model takes care of addition and removal automatically. Convert "config" to a static attribute and use the .is_bin_visible() callback to set the correct object size (either 256 bytes or 4 KiB) at runtime. The pci_sysfs_init() scheme was added in the pre-git era by https://git.kernel.org/pub/scm/linux/kernel/git/tglx/history.git/commit/drivers/pci/pci-sysfs.c?id=f6d553444da2 [bhelgaas: commit log] Suggested-by: Oliver O'Halloran <[email protected]> Link: https://lore.kernel.org/r/CAOSf1CHss03DBSDO4PmTtMp0tCEu5kScn704ZEwLKGXQzBfqaA@mail.gmail.com Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Krzysztof Wilczyński <[email protected]> Signed-off-by: Bjorn Helgaas <[email protected]>
1 parent a38fd87 commit e1d3f32

File tree

1 file changed

+25
-39
lines changed

1 file changed

+25
-39
lines changed

drivers/pci/pci-sysfs.c

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,29 @@ static ssize_t pci_write_config(struct file *filp, struct kobject *kobj,
808808

809809
return count;
810810
}
811+
static BIN_ATTR(config, 0644, pci_read_config, pci_write_config, 0);
812+
813+
static struct bin_attribute *pci_dev_config_attrs[] = {
814+
&bin_attr_config,
815+
NULL,
816+
};
817+
818+
static umode_t pci_dev_config_attr_is_visible(struct kobject *kobj,
819+
struct bin_attribute *a, int n)
820+
{
821+
struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
822+
823+
a->size = PCI_CFG_SPACE_SIZE;
824+
if (pdev->cfg_size > PCI_CFG_SPACE_SIZE)
825+
a->size = PCI_CFG_SPACE_EXP_SIZE;
826+
827+
return a->attr.mode;
828+
}
829+
830+
static const struct attribute_group pci_dev_config_attr_group = {
831+
.bin_attrs = pci_dev_config_attrs,
832+
.is_bin_visible = pci_dev_config_attr_is_visible,
833+
};
811834

812835
#ifdef HAVE_PCI_LEGACY
813836
/**
@@ -1284,26 +1307,6 @@ static ssize_t pci_read_rom(struct file *filp, struct kobject *kobj,
12841307
return count;
12851308
}
12861309

1287-
static const struct bin_attribute pci_config_attr = {
1288-
.attr = {
1289-
.name = "config",
1290-
.mode = 0644,
1291-
},
1292-
.size = PCI_CFG_SPACE_SIZE,
1293-
.read = pci_read_config,
1294-
.write = pci_write_config,
1295-
};
1296-
1297-
static const struct bin_attribute pcie_config_attr = {
1298-
.attr = {
1299-
.name = "config",
1300-
.mode = 0644,
1301-
},
1302-
.size = PCI_CFG_SPACE_EXP_SIZE,
1303-
.read = pci_read_config,
1304-
.write = pci_write_config,
1305-
};
1306-
13071310
static ssize_t reset_store(struct device *dev, struct device_attribute *attr,
13081311
const char *buf, size_t count)
13091312
{
@@ -1355,16 +1358,9 @@ int __must_check pci_create_sysfs_dev_files(struct pci_dev *pdev)
13551358
if (!sysfs_initialized)
13561359
return -EACCES;
13571360

1358-
if (pdev->cfg_size > PCI_CFG_SPACE_SIZE)
1359-
retval = sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1360-
else
1361-
retval = sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr);
1362-
if (retval)
1363-
goto err;
1364-
13651361
retval = pci_create_resource_files(pdev);
13661362
if (retval)
1367-
goto err_config_file;
1363+
goto err;
13681364

13691365
/* If the device has a ROM, try to expose it in sysfs. */
13701366
rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
@@ -1405,11 +1401,6 @@ int __must_check pci_create_sysfs_dev_files(struct pci_dev *pdev)
14051401
}
14061402
err_resource_files:
14071403
pci_remove_resource_files(pdev);
1408-
err_config_file:
1409-
if (pdev->cfg_size > PCI_CFG_SPACE_SIZE)
1410-
sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1411-
else
1412-
sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
14131404
err:
14141405
return retval;
14151406
}
@@ -1435,12 +1426,6 @@ void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
14351426
return;
14361427

14371428
pci_remove_capabilities_sysfs(pdev);
1438-
1439-
if (pdev->cfg_size > PCI_CFG_SPACE_SIZE)
1440-
sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1441-
else
1442-
sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
1443-
14441429
pci_remove_resource_files(pdev);
14451430

14461431
if (pdev->rom_attr) {
@@ -1540,6 +1525,7 @@ static const struct attribute_group pci_dev_group = {
15401525

15411526
const struct attribute_group *pci_dev_groups[] = {
15421527
&pci_dev_group,
1528+
&pci_dev_config_attr_group,
15431529
NULL,
15441530
};
15451531

0 commit comments

Comments
 (0)