linux-pci.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Krzysztof Wilczyński" <kw@linux.com>
To: Bjorn Helgaas <bhelgaas@google.com>
Cc: "Pali Rohár" <pali@kernel.org>,
	"Oliver O'Halloran" <oohall@gmail.com>,
	"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	"Daniel Vetter" <daniel.vetter@ffwll.ch>,
	"Joe Perches" <joe@perches.com>,
	"Dan Williams" <dan.j.williams@intel.com>,
	"Mauro Carvalho Chehab" <mchehab+huawei@kernel.org>,
	"David Sterba" <dsterba@suse.com>,
	linux-pci@vger.kernel.org
Subject: [PATCH 01/20] PCI: Convert dynamic "config" sysfs object into static
Date: Fri, 16 Apr 2021 20:58:37 +0000	[thread overview]
Message-ID: <20210416205856.3234481-2-kw@linux.com> (raw)
In-Reply-To: <20210416205856.3234481-1-kw@linux.com>

The "config" sysfs object, which is a binary read-and-write attribute,
resides at the "/sys/bus/pci/devices/.../config" path and allows for
accessing either the legacy (PCI and PCI-X Mode 1) or the extended
(PCI-X Mode 2 and PCIe) device PCI configuration space.

At the moment, the static "config" sysfs object is dynamically created
when the pci_create_sysfs_dev_files() function executes that is part of
the PCI sysfs objects initialisation and when a device is added:

  late_initcall()
    pci_sysfs_init()
      pci_create_sysfs_dev_files()
        sysfs_create_bin_file()

  pci_bus_add_devices()
    pci_bus_add_device()
      pci_create_sysfs_dev_files()
        ...

And dynamically removed when the pci_remove_sysfs_dev_files() function
executes 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 dynamically and removed
dynamically, and thus there is no need to also manage its create and
remove life cycle manually as the PCI driver core can do it when the
device is either added or removed.

Convert the "config" sysfs object created dynamically to static and use
the .is_bin_visible callback to set the correct sysfs object size
(either 256 bytes of 4 KiB) at runtime.

Then, add a newly created attribute group to the existing group called
"pci_dev_groups" to ensure that the "config" sysfs object would be
automatically included when the PCI driver initializes.

Suggested-by: Oliver O'Halloran <oohall@gmail.com>
Suggested-by: Bjorn Helgaas <bhelgaas@google.com>
Signed-off-by: Krzysztof Wilczyński <kw@linux.com>
---
 drivers/pci/pci-sysfs.c | 64 ++++++++++++++++-------------------------
 1 file changed, 25 insertions(+), 39 deletions(-)

diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c
index f8afd54ca3e1..dc14daf404f5 100644
--- a/drivers/pci/pci-sysfs.c
+++ b/drivers/pci/pci-sysfs.c
@@ -808,6 +808,29 @@ static ssize_t pci_write_config(struct file *filp, struct kobject *kobj,
 
 	return count;
 }
+static BIN_ATTR(config, 0644, pci_read_config, pci_write_config, 0);
+
+static struct bin_attribute *pci_dev_config_attrs[] = {
+	&bin_attr_config,
+	NULL,
+};
+
+static umode_t pci_dev_config_attr_is_visible(struct kobject *kobj,
+					      struct bin_attribute *a, int n)
+{
+	struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
+
+	a->size = PCI_CFG_SPACE_SIZE;
+	if (pdev->cfg_size > PCI_CFG_SPACE_SIZE)
+		a->size = PCI_CFG_SPACE_EXP_SIZE;
+
+	return a->attr.mode;
+}
+
+static const struct attribute_group pci_dev_config_attr_group = {
+	.bin_attrs = pci_dev_config_attrs,
+	.is_bin_visible = pci_dev_config_attr_is_visible,
+};
 
 #ifdef HAVE_PCI_LEGACY
 /**
@@ -1284,26 +1307,6 @@ static ssize_t pci_read_rom(struct file *filp, struct kobject *kobj,
 	return count;
 }
 
-static const struct bin_attribute pci_config_attr = {
-	.attr =	{
-		.name = "config",
-		.mode = 0644,
-	},
-	.size = PCI_CFG_SPACE_SIZE,
-	.read = pci_read_config,
-	.write = pci_write_config,
-};
-
-static const struct bin_attribute pcie_config_attr = {
-	.attr =	{
-		.name = "config",
-		.mode = 0644,
-	},
-	.size = PCI_CFG_SPACE_EXP_SIZE,
-	.read = pci_read_config,
-	.write = pci_write_config,
-};
-
 static ssize_t reset_store(struct device *dev, struct device_attribute *attr,
 			   const char *buf, size_t count)
 {
@@ -1355,16 +1358,9 @@ int __must_check pci_create_sysfs_dev_files(struct pci_dev *pdev)
 	if (!sysfs_initialized)
 		return -EACCES;
 
-	if (pdev->cfg_size > PCI_CFG_SPACE_SIZE)
-		retval = sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr);
-	else
-		retval = sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr);
-	if (retval)
-		goto err;
-
 	retval = pci_create_resource_files(pdev);
 	if (retval)
-		goto err_config_file;
+		goto err;
 
 	/* If the device has a ROM, try to expose it in sysfs. */
 	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)
 	}
 err_resource_files:
 	pci_remove_resource_files(pdev);
-err_config_file:
-	if (pdev->cfg_size > PCI_CFG_SPACE_SIZE)
-		sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
-	else
-		sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
 err:
 	return retval;
 }
@@ -1435,12 +1426,6 @@ void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
 		return;
 
 	pci_remove_capabilities_sysfs(pdev);
-
-	if (pdev->cfg_size > PCI_CFG_SPACE_SIZE)
-		sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
-	else
-		sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
-
 	pci_remove_resource_files(pdev);
 
 	if (pdev->rom_attr) {
@@ -1540,6 +1525,7 @@ static const struct attribute_group pci_dev_group = {
 
 const struct attribute_group *pci_dev_groups[] = {
 	&pci_dev_group,
+	&pci_dev_config_attr_group,
 	NULL,
 };
 
-- 
2.31.0


  reply	other threads:[~2021-04-16 20:59 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-16 20:58 [PATCH 00/20] PCI: Convert dynamic sysfs objects into static Krzysztof Wilczyński
2021-04-16 20:58 ` Krzysztof Wilczyński [this message]
2021-04-16 20:58 ` [PATCH 02/20] PCI: Convert dynamic "rom" sysfs object " Krzysztof Wilczyński
2021-04-16 20:58 ` [PATCH 03/20] PCI: Convert dynamic "reset" " Krzysztof Wilczyński
2021-04-16 20:58 ` [PATCH 04/20] PCI/VPD: Convert dynamic "vpd" " Krzysztof Wilczyński
2021-04-16 20:58 ` [PATCH 05/20] PCI: Convert dynamic "index" and "label" sysfs objects " Krzysztof Wilczyński
2021-04-16 20:58 ` [PATCH 06/20] sysfs: Introduce BIN_ATTR_ADMIN_RO and BIN_ATTR_ADMIN_RW Krzysztof Wilczyński
2021-04-16 20:58 ` [PATCH 07/20] PCI: Convert PCI sysfs objects to use BIN_ATTR_ADMIN_RW macro Krzysztof Wilczyński
2021-04-16 20:58 ` [PATCH 08/20] PCI: Move to kstrtobool() to handle user input Krzysztof Wilczyński
2021-04-16 20:58 ` [PATCH 09/20] PCI: Use sysfs_emit() and sysfs_emit_at() in "show" functions Krzysztof Wilczyński
2021-04-16 20:58 ` [PATCH 10/20] PCI: Update style to be more consistent Krzysztof Wilczyński
2021-04-16 20:58 ` [PATCH 11/20] PCI: Rearrange attributes from the pci_dev_group Krzysztof Wilczyński
2021-04-16 20:58 ` [PATCH 12/20] PCI: Rearrange attributes from the pci_dev_config_attr_group Krzysztof Wilczyński
2021-04-16 20:58 ` [PATCH 13/20] PCI: Rearrange attributes from the pci_dev_rom_attr_group Krzysztof Wilczyński
2021-04-16 20:58 ` [PATCH 14/20] PCI: Rearrange attributes from the pci_dev_reset_attr_group Krzysztof Wilczyński
2021-04-16 20:58 ` [PATCH 15/20] PCI: Rearrange attributes from the pci_dev_attr_group Krzysztof Wilczyński
2021-04-16 20:58 ` [PATCH 16/20] PCI: Rearrange attributes from the pci_dev_hp_attr_group Krzysztof Wilczyński
2021-04-16 20:58 ` [PATCH 17/20] PCI: Rearrange attributes from the pci_bridge_attr_group Krzysztof Wilczyński
2021-04-16 20:58 ` [PATCH 18/20] PCI: Rearrange attributes from the pcie_dev_attr_group Krzysztof Wilczyński
2021-04-16 20:58 ` [PATCH 19/20] PCI: Rearrange attributes from the pci_bus_group Krzysztof Wilczyński
2021-04-16 20:58 ` [PATCH 20/20] PCI: Rearrange attributes from the pcibus_group Krzysztof Wilczyński

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210416205856.3234481-2-kw@linux.com \
    --to=kw@linux.com \
    --cc=bhelgaas@google.com \
    --cc=dan.j.williams@intel.com \
    --cc=daniel.vetter@ffwll.ch \
    --cc=dsterba@suse.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=joe@perches.com \
    --cc=linux-pci@vger.kernel.org \
    --cc=mchehab+huawei@kernel.org \
    --cc=oohall@gmail.com \
    --cc=pali@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).