Add three macros for declaring static binary attributes for PCI
resource files:

  - pci_dev_resource_io_attr(), for I/O BAR resources (read/write)
  - pci_dev_resource_uc_attr(), for memory BAR resources (mmap uncached)
  - pci_dev_resource_wc_attr(), for write-combine resources (mmap WC)

Each macro only sets the callbacks its resource type needs.  The I/O
macro conditionally includes mmap support via __PCI_RESOURCE_IO_MMAP_ATTRS
on architectures where arch_can_pci_mmap_io() is true at compile time
(such as PowerPC, SPARC, and Xtensa).

Tested-by: Shivaprasad G Bhat <[email protected]>
Signed-off-by: Krzysztof Wilczyński <[email protected]>
---
 drivers/pci/pci-sysfs.c | 41 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)

diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c
index dac780597727..d50b8fe1498c 100644
--- a/drivers/pci/pci-sysfs.c
+++ b/drivers/pci/pci-sysfs.c
@@ -1197,6 +1197,47 @@ static ssize_t pci_write_resource_io(struct file *filp, 
struct kobject *kobj,
        return pci_resource_io(filp, kobj, attr, buf, off, count, true);
 }
 
+/*
+ * generic_file_llseek() consults f_mapping->host to determine
+ * the file size. As iomem_inode knows nothing about the
+ * attribute, it's not going to work, so override it as well.
+ */
+#if arch_can_pci_mmap_io()
+# define __PCI_RESOURCE_IO_MMAP_ATTRS          \
+       .f_mapping = iomem_get_mapping,         \
+       .llseek = pci_llseek_resource,          \
+       .mmap = pci_mmap_resource_uc,
+#else
+# define __PCI_RESOURCE_IO_MMAP_ATTRS
+#endif
+
+#define pci_dev_resource_io_attr(_bar)                                 \
+static const struct bin_attribute dev_resource##_bar##_io_attr = {     \
+       .attr = { .name = "resource" __stringify(_bar), .mode = 0600 }, \
+       .private = (void *)(unsigned long)(_bar),                       \
+       .read = pci_read_resource_io,                                   \
+       .write = pci_write_resource_io,                                 \
+       __PCI_RESOURCE_IO_MMAP_ATTRS                                    \
+}
+
+#define pci_dev_resource_uc_attr(_bar)                                 \
+static const struct bin_attribute dev_resource##_bar##_uc_attr = {     \
+       .attr = { .name = "resource" __stringify(_bar), .mode = 0600 }, \
+       .private = (void *)(unsigned long)(_bar),                       \
+       .f_mapping = iomem_get_mapping,                                 \
+       .llseek = pci_llseek_resource,                                  \
+       .mmap = pci_mmap_resource_uc,                                   \
+}
+
+#define pci_dev_resource_wc_attr(_bar)                                         
\
+static const struct bin_attribute dev_resource##_bar##_wc_attr = {             
\
+       .attr = { .name = "resource" __stringify(_bar) "_wc", .mode = 0600 },   
\
+       .private = (void *)(unsigned long)(_bar),                               
\
+       .f_mapping = iomem_get_mapping,                                         
\
+       .llseek = pci_llseek_resource,                                          
\
+       .mmap = pci_mmap_resource_wc,                                           
\
+}
+
 /**
  * pci_remove_resource_files - cleanup resource files
  * @pdev: dev to cleanup
-- 
2.54.0


Reply via email to