2021-12-27 16:45:13

by Niklas Schnelle

[permalink] [raw]
Subject: [RFC 27/32] PCI/sysfs: make I/O resource depend on HAS_IOPORT

Exporting I/O resources only makes sense if legacy I/O spaces are
supported so conditionally add them only if HAS_IOPORT is set.

Co-developed-by: Arnd Bergmann <[email protected]>
Signed-off-by: Arnd Bergmann <[email protected]>
Signed-off-by: Niklas Schnelle <[email protected]>
---
drivers/pci/pci-sysfs.c | 16 ++++++++++++++++
1 file changed, 16 insertions(+)

diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c
index cfe2f85af09e..a59a85593972 100644
--- a/drivers/pci/pci-sysfs.c
+++ b/drivers/pci/pci-sysfs.c
@@ -1099,6 +1099,7 @@ static int pci_mmap_resource_wc(struct file *filp, struct kobject *kobj,
return pci_mmap_resource(kobj, attr, vma, 1);
}

+#ifdef CONFIG_HAS_IOPORT
static ssize_t pci_resource_io(struct file *filp, struct kobject *kobj,
struct bin_attribute *attr, char *buf,
loff_t off, size_t count, bool write)
@@ -1157,6 +1158,21 @@ static ssize_t pci_write_resource_io(struct file *filp, struct kobject *kobj,

return pci_resource_io(filp, kobj, attr, buf, off, count, true);
}
+#else
+static ssize_t pci_read_resource_io(struct file *filp, struct kobject *kobj,
+ struct bin_attribute *attr, char *buf,
+ loff_t off, size_t count)
+{
+ return -ENXIO;
+}
+
+static ssize_t pci_write_resource_io(struct file *filp, struct kobject *kobj,
+ struct bin_attribute *attr, char *buf,
+ loff_t off, size_t count)
+{
+ return -ENXIO;
+}
+#endif

/**
* pci_remove_resource_files - cleanup resource files
--
2.32.0



2021-12-27 22:04:49

by Bjorn Helgaas

[permalink] [raw]
Subject: Re: [RFC 27/32] PCI/sysfs: make I/O resource depend on HAS_IOPORT

Make the subject match historical convention (capitalize "Make").

On Mon, Dec 27, 2021 at 05:43:12PM +0100, Niklas Schnelle wrote:
> Exporting I/O resources only makes sense if legacy I/O spaces are
> supported so conditionally add them only if HAS_IOPORT is set.

IIUC, the effect of this is that the "resource%d" file for an I/O BAR
still appears in /sys, but reads or writes will fail with ENXIO.
Worth mentioning that in the commit log, since one could interpret the
above as meaning that the "resource%d" file exists only if HAS_IOPORT
is set. I think I will *exist* but not be very useful.

I also wonder what this looks like in the sysfs "resource" file and
via lspci. I suppose it's useful if lspci shows the fact that the BAR
exists and is an I/O BAR, even if the arch doesn't set HAS_IOPORT.

> Co-developed-by: Arnd Bergmann <[email protected]>
> Signed-off-by: Arnd Bergmann <[email protected]>
> Signed-off-by: Niklas Schnelle <[email protected]>
> ---
> drivers/pci/pci-sysfs.c | 16 ++++++++++++++++
> 1 file changed, 16 insertions(+)
>
> diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c
> index cfe2f85af09e..a59a85593972 100644
> --- a/drivers/pci/pci-sysfs.c
> +++ b/drivers/pci/pci-sysfs.c
> @@ -1099,6 +1099,7 @@ static int pci_mmap_resource_wc(struct file *filp, struct kobject *kobj,
> return pci_mmap_resource(kobj, attr, vma, 1);
> }
>
> +#ifdef CONFIG_HAS_IOPORT
> static ssize_t pci_resource_io(struct file *filp, struct kobject *kobj,
> struct bin_attribute *attr, char *buf,
> loff_t off, size_t count, bool write)
> @@ -1157,6 +1158,21 @@ static ssize_t pci_write_resource_io(struct file *filp, struct kobject *kobj,
>
> return pci_resource_io(filp, kobj, attr, buf, off, count, true);
> }
> +#else
> +static ssize_t pci_read_resource_io(struct file *filp, struct kobject *kobj,
> + struct bin_attribute *attr, char *buf,
> + loff_t off, size_t count)
> +{
> + return -ENXIO;
> +}

I assume the sysfs infrastructure prevents or fails reads/write if
res_attr->read and res_attr->write are not set at all, so maybe we
wouldn't need the stubs if we did something like this?

if (pci_resource_flags(pdev, num) & IORESOURCE_IO) {
#ifdef CONFIG_HAS_IOPORT
res_attr->read = pci_read_resource_io;
res_attr->write = pci_write_resource_io;
...
#endif
} else {

> +static ssize_t pci_write_resource_io(struct file *filp, struct kobject *kobj,
> + struct bin_attribute *attr, char *buf,
> + loff_t off, size_t count)
> +{
> + return -ENXIO;
> +}
> +#endif
>
> /**
> * pci_remove_resource_files - cleanup resource files
> --
> 2.32.0
>