Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * (C) Copyright 2002-2004 Greg Kroah-Hartman <greg@kroah.com>
0004  * (C) Copyright 2002-2004 IBM Corp.
0005  * (C) Copyright 2003 Matthew Wilcox
0006  * (C) Copyright 2003 Hewlett-Packard
0007  * (C) Copyright 2004 Jon Smirl <jonsmirl@yahoo.com>
0008  * (C) Copyright 2004 Silicon Graphics, Inc. Jesse Barnes <jbarnes@sgi.com>
0009  *
0010  * File attributes for PCI devices
0011  *
0012  * Modeled after usb's driverfs.c
0013  */
0014 
0015 
0016 #include <linux/kernel.h>
0017 #include <linux/sched.h>
0018 #include <linux/pci.h>
0019 #include <linux/stat.h>
0020 #include <linux/export.h>
0021 #include <linux/topology.h>
0022 #include <linux/mm.h>
0023 #include <linux/fs.h>
0024 #include <linux/capability.h>
0025 #include <linux/security.h>
0026 #include <linux/slab.h>
0027 #include <linux/vgaarb.h>
0028 #include <linux/pm_runtime.h>
0029 #include <linux/msi.h>
0030 #include <linux/of.h>
0031 #include "pci.h"
0032 
0033 static int sysfs_initialized;   /* = 0 */
0034 
0035 /* show configuration fields */
0036 #define pci_config_attr(field, format_string)               \
0037 static ssize_t                              \
0038 field##_show(struct device *dev, struct device_attribute *attr, char *buf)              \
0039 {                                   \
0040     struct pci_dev *pdev;                       \
0041                                     \
0042     pdev = to_pci_dev(dev);                     \
0043     return sysfs_emit(buf, format_string, pdev->field);     \
0044 }                                   \
0045 static DEVICE_ATTR_RO(field)
0046 
0047 pci_config_attr(vendor, "0x%04x\n");
0048 pci_config_attr(device, "0x%04x\n");
0049 pci_config_attr(subsystem_vendor, "0x%04x\n");
0050 pci_config_attr(subsystem_device, "0x%04x\n");
0051 pci_config_attr(revision, "0x%02x\n");
0052 pci_config_attr(class, "0x%06x\n");
0053 
0054 static ssize_t irq_show(struct device *dev,
0055             struct device_attribute *attr,
0056             char *buf)
0057 {
0058     struct pci_dev *pdev = to_pci_dev(dev);
0059 
0060 #ifdef CONFIG_PCI_MSI
0061     /*
0062      * For MSI, show the first MSI IRQ; for all other cases including
0063      * MSI-X, show the legacy INTx IRQ.
0064      */
0065     if (pdev->msi_enabled)
0066         return sysfs_emit(buf, "%u\n", pci_irq_vector(pdev, 0));
0067 #endif
0068 
0069     return sysfs_emit(buf, "%u\n", pdev->irq);
0070 }
0071 static DEVICE_ATTR_RO(irq);
0072 
0073 static ssize_t broken_parity_status_show(struct device *dev,
0074                      struct device_attribute *attr,
0075                      char *buf)
0076 {
0077     struct pci_dev *pdev = to_pci_dev(dev);
0078     return sysfs_emit(buf, "%u\n", pdev->broken_parity_status);
0079 }
0080 
0081 static ssize_t broken_parity_status_store(struct device *dev,
0082                       struct device_attribute *attr,
0083                       const char *buf, size_t count)
0084 {
0085     struct pci_dev *pdev = to_pci_dev(dev);
0086     unsigned long val;
0087 
0088     if (kstrtoul(buf, 0, &val) < 0)
0089         return -EINVAL;
0090 
0091     pdev->broken_parity_status = !!val;
0092 
0093     return count;
0094 }
0095 static DEVICE_ATTR_RW(broken_parity_status);
0096 
0097 static ssize_t pci_dev_show_local_cpu(struct device *dev, bool list,
0098                       struct device_attribute *attr, char *buf)
0099 {
0100     const struct cpumask *mask;
0101 
0102 #ifdef CONFIG_NUMA
0103     if (dev_to_node(dev) == NUMA_NO_NODE)
0104         mask = cpu_online_mask;
0105     else
0106         mask = cpumask_of_node(dev_to_node(dev));
0107 #else
0108     mask = cpumask_of_pcibus(to_pci_dev(dev)->bus);
0109 #endif
0110     return cpumap_print_to_pagebuf(list, buf, mask);
0111 }
0112 
0113 static ssize_t local_cpus_show(struct device *dev,
0114                    struct device_attribute *attr, char *buf)
0115 {
0116     return pci_dev_show_local_cpu(dev, false, attr, buf);
0117 }
0118 static DEVICE_ATTR_RO(local_cpus);
0119 
0120 static ssize_t local_cpulist_show(struct device *dev,
0121                   struct device_attribute *attr, char *buf)
0122 {
0123     return pci_dev_show_local_cpu(dev, true, attr, buf);
0124 }
0125 static DEVICE_ATTR_RO(local_cpulist);
0126 
0127 /*
0128  * PCI Bus Class Devices
0129  */
0130 static ssize_t cpuaffinity_show(struct device *dev,
0131                 struct device_attribute *attr, char *buf)
0132 {
0133     const struct cpumask *cpumask = cpumask_of_pcibus(to_pci_bus(dev));
0134 
0135     return cpumap_print_to_pagebuf(false, buf, cpumask);
0136 }
0137 static DEVICE_ATTR_RO(cpuaffinity);
0138 
0139 static ssize_t cpulistaffinity_show(struct device *dev,
0140                     struct device_attribute *attr, char *buf)
0141 {
0142     const struct cpumask *cpumask = cpumask_of_pcibus(to_pci_bus(dev));
0143 
0144     return cpumap_print_to_pagebuf(true, buf, cpumask);
0145 }
0146 static DEVICE_ATTR_RO(cpulistaffinity);
0147 
0148 static ssize_t power_state_show(struct device *dev,
0149                 struct device_attribute *attr, char *buf)
0150 {
0151     struct pci_dev *pdev = to_pci_dev(dev);
0152 
0153     return sysfs_emit(buf, "%s\n", pci_power_name(pdev->current_state));
0154 }
0155 static DEVICE_ATTR_RO(power_state);
0156 
0157 /* show resources */
0158 static ssize_t resource_show(struct device *dev, struct device_attribute *attr,
0159                  char *buf)
0160 {
0161     struct pci_dev *pci_dev = to_pci_dev(dev);
0162     int i;
0163     int max;
0164     resource_size_t start, end;
0165     size_t len = 0;
0166 
0167     if (pci_dev->subordinate)
0168         max = DEVICE_COUNT_RESOURCE;
0169     else
0170         max = PCI_BRIDGE_RESOURCES;
0171 
0172     for (i = 0; i < max; i++) {
0173         struct resource *res =  &pci_dev->resource[i];
0174         pci_resource_to_user(pci_dev, i, res, &start, &end);
0175         len += sysfs_emit_at(buf, len, "0x%016llx 0x%016llx 0x%016llx\n",
0176                      (unsigned long long)start,
0177                      (unsigned long long)end,
0178                      (unsigned long long)res->flags);
0179     }
0180     return len;
0181 }
0182 static DEVICE_ATTR_RO(resource);
0183 
0184 static ssize_t max_link_speed_show(struct device *dev,
0185                    struct device_attribute *attr, char *buf)
0186 {
0187     struct pci_dev *pdev = to_pci_dev(dev);
0188 
0189     return sysfs_emit(buf, "%s\n",
0190               pci_speed_string(pcie_get_speed_cap(pdev)));
0191 }
0192 static DEVICE_ATTR_RO(max_link_speed);
0193 
0194 static ssize_t max_link_width_show(struct device *dev,
0195                    struct device_attribute *attr, char *buf)
0196 {
0197     struct pci_dev *pdev = to_pci_dev(dev);
0198 
0199     return sysfs_emit(buf, "%u\n", pcie_get_width_cap(pdev));
0200 }
0201 static DEVICE_ATTR_RO(max_link_width);
0202 
0203 static ssize_t current_link_speed_show(struct device *dev,
0204                        struct device_attribute *attr, char *buf)
0205 {
0206     struct pci_dev *pci_dev = to_pci_dev(dev);
0207     u16 linkstat;
0208     int err;
0209     enum pci_bus_speed speed;
0210 
0211     err = pcie_capability_read_word(pci_dev, PCI_EXP_LNKSTA, &linkstat);
0212     if (err)
0213         return -EINVAL;
0214 
0215     speed = pcie_link_speed[linkstat & PCI_EXP_LNKSTA_CLS];
0216 
0217     return sysfs_emit(buf, "%s\n", pci_speed_string(speed));
0218 }
0219 static DEVICE_ATTR_RO(current_link_speed);
0220 
0221 static ssize_t current_link_width_show(struct device *dev,
0222                        struct device_attribute *attr, char *buf)
0223 {
0224     struct pci_dev *pci_dev = to_pci_dev(dev);
0225     u16 linkstat;
0226     int err;
0227 
0228     err = pcie_capability_read_word(pci_dev, PCI_EXP_LNKSTA, &linkstat);
0229     if (err)
0230         return -EINVAL;
0231 
0232     return sysfs_emit(buf, "%u\n",
0233         (linkstat & PCI_EXP_LNKSTA_NLW) >> PCI_EXP_LNKSTA_NLW_SHIFT);
0234 }
0235 static DEVICE_ATTR_RO(current_link_width);
0236 
0237 static ssize_t secondary_bus_number_show(struct device *dev,
0238                      struct device_attribute *attr,
0239                      char *buf)
0240 {
0241     struct pci_dev *pci_dev = to_pci_dev(dev);
0242     u8 sec_bus;
0243     int err;
0244 
0245     err = pci_read_config_byte(pci_dev, PCI_SECONDARY_BUS, &sec_bus);
0246     if (err)
0247         return -EINVAL;
0248 
0249     return sysfs_emit(buf, "%u\n", sec_bus);
0250 }
0251 static DEVICE_ATTR_RO(secondary_bus_number);
0252 
0253 static ssize_t subordinate_bus_number_show(struct device *dev,
0254                        struct device_attribute *attr,
0255                        char *buf)
0256 {
0257     struct pci_dev *pci_dev = to_pci_dev(dev);
0258     u8 sub_bus;
0259     int err;
0260 
0261     err = pci_read_config_byte(pci_dev, PCI_SUBORDINATE_BUS, &sub_bus);
0262     if (err)
0263         return -EINVAL;
0264 
0265     return sysfs_emit(buf, "%u\n", sub_bus);
0266 }
0267 static DEVICE_ATTR_RO(subordinate_bus_number);
0268 
0269 static ssize_t ari_enabled_show(struct device *dev,
0270                 struct device_attribute *attr,
0271                 char *buf)
0272 {
0273     struct pci_dev *pci_dev = to_pci_dev(dev);
0274 
0275     return sysfs_emit(buf, "%u\n", pci_ari_enabled(pci_dev->bus));
0276 }
0277 static DEVICE_ATTR_RO(ari_enabled);
0278 
0279 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
0280                  char *buf)
0281 {
0282     struct pci_dev *pci_dev = to_pci_dev(dev);
0283 
0284     return sysfs_emit(buf, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02X\n",
0285               pci_dev->vendor, pci_dev->device,
0286               pci_dev->subsystem_vendor, pci_dev->subsystem_device,
0287               (u8)(pci_dev->class >> 16), (u8)(pci_dev->class >> 8),
0288               (u8)(pci_dev->class));
0289 }
0290 static DEVICE_ATTR_RO(modalias);
0291 
0292 static ssize_t enable_store(struct device *dev, struct device_attribute *attr,
0293                  const char *buf, size_t count)
0294 {
0295     struct pci_dev *pdev = to_pci_dev(dev);
0296     unsigned long val;
0297     ssize_t result = 0;
0298 
0299     /* this can crash the machine when done on the "wrong" device */
0300     if (!capable(CAP_SYS_ADMIN))
0301         return -EPERM;
0302 
0303     if (kstrtoul(buf, 0, &val) < 0)
0304         return -EINVAL;
0305 
0306     device_lock(dev);
0307     if (dev->driver)
0308         result = -EBUSY;
0309     else if (val)
0310         result = pci_enable_device(pdev);
0311     else if (pci_is_enabled(pdev))
0312         pci_disable_device(pdev);
0313     else
0314         result = -EIO;
0315     device_unlock(dev);
0316 
0317     return result < 0 ? result : count;
0318 }
0319 
0320 static ssize_t enable_show(struct device *dev, struct device_attribute *attr,
0321                 char *buf)
0322 {
0323     struct pci_dev *pdev;
0324 
0325     pdev = to_pci_dev(dev);
0326     return sysfs_emit(buf, "%u\n", atomic_read(&pdev->enable_cnt));
0327 }
0328 static DEVICE_ATTR_RW(enable);
0329 
0330 #ifdef CONFIG_NUMA
0331 static ssize_t numa_node_store(struct device *dev,
0332                    struct device_attribute *attr, const char *buf,
0333                    size_t count)
0334 {
0335     struct pci_dev *pdev = to_pci_dev(dev);
0336     int node;
0337 
0338     if (!capable(CAP_SYS_ADMIN))
0339         return -EPERM;
0340 
0341     if (kstrtoint(buf, 0, &node) < 0)
0342         return -EINVAL;
0343 
0344     if ((node < 0 && node != NUMA_NO_NODE) || node >= MAX_NUMNODES)
0345         return -EINVAL;
0346 
0347     if (node != NUMA_NO_NODE && !node_online(node))
0348         return -EINVAL;
0349 
0350     add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_STILL_OK);
0351     pci_alert(pdev, FW_BUG "Overriding NUMA node to %d.  Contact your vendor for updates.",
0352           node);
0353 
0354     dev->numa_node = node;
0355     return count;
0356 }
0357 
0358 static ssize_t numa_node_show(struct device *dev, struct device_attribute *attr,
0359                   char *buf)
0360 {
0361     return sysfs_emit(buf, "%d\n", dev->numa_node);
0362 }
0363 static DEVICE_ATTR_RW(numa_node);
0364 #endif
0365 
0366 static ssize_t dma_mask_bits_show(struct device *dev,
0367                   struct device_attribute *attr, char *buf)
0368 {
0369     struct pci_dev *pdev = to_pci_dev(dev);
0370 
0371     return sysfs_emit(buf, "%d\n", fls64(pdev->dma_mask));
0372 }
0373 static DEVICE_ATTR_RO(dma_mask_bits);
0374 
0375 static ssize_t consistent_dma_mask_bits_show(struct device *dev,
0376                          struct device_attribute *attr,
0377                          char *buf)
0378 {
0379     return sysfs_emit(buf, "%d\n", fls64(dev->coherent_dma_mask));
0380 }
0381 static DEVICE_ATTR_RO(consistent_dma_mask_bits);
0382 
0383 static ssize_t msi_bus_show(struct device *dev, struct device_attribute *attr,
0384                 char *buf)
0385 {
0386     struct pci_dev *pdev = to_pci_dev(dev);
0387     struct pci_bus *subordinate = pdev->subordinate;
0388 
0389     return sysfs_emit(buf, "%u\n", subordinate ?
0390               !(subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI)
0391                 : !pdev->no_msi);
0392 }
0393 
0394 static ssize_t msi_bus_store(struct device *dev, struct device_attribute *attr,
0395                  const char *buf, size_t count)
0396 {
0397     struct pci_dev *pdev = to_pci_dev(dev);
0398     struct pci_bus *subordinate = pdev->subordinate;
0399     unsigned long val;
0400 
0401     if (!capable(CAP_SYS_ADMIN))
0402         return -EPERM;
0403 
0404     if (kstrtoul(buf, 0, &val) < 0)
0405         return -EINVAL;
0406 
0407     /*
0408      * "no_msi" and "bus_flags" only affect what happens when a driver
0409      * requests MSI or MSI-X.  They don't affect any drivers that have
0410      * already requested MSI or MSI-X.
0411      */
0412     if (!subordinate) {
0413         pdev->no_msi = !val;
0414         pci_info(pdev, "MSI/MSI-X %s for future drivers\n",
0415              val ? "allowed" : "disallowed");
0416         return count;
0417     }
0418 
0419     if (val)
0420         subordinate->bus_flags &= ~PCI_BUS_FLAGS_NO_MSI;
0421     else
0422         subordinate->bus_flags |= PCI_BUS_FLAGS_NO_MSI;
0423 
0424     dev_info(&subordinate->dev, "MSI/MSI-X %s for future drivers of devices on this bus\n",
0425          val ? "allowed" : "disallowed");
0426     return count;
0427 }
0428 static DEVICE_ATTR_RW(msi_bus);
0429 
0430 static ssize_t rescan_store(struct bus_type *bus, const char *buf, size_t count)
0431 {
0432     unsigned long val;
0433     struct pci_bus *b = NULL;
0434 
0435     if (kstrtoul(buf, 0, &val) < 0)
0436         return -EINVAL;
0437 
0438     if (val) {
0439         pci_lock_rescan_remove();
0440         while ((b = pci_find_next_bus(b)) != NULL)
0441             pci_rescan_bus(b);
0442         pci_unlock_rescan_remove();
0443     }
0444     return count;
0445 }
0446 static BUS_ATTR_WO(rescan);
0447 
0448 static struct attribute *pci_bus_attrs[] = {
0449     &bus_attr_rescan.attr,
0450     NULL,
0451 };
0452 
0453 static const struct attribute_group pci_bus_group = {
0454     .attrs = pci_bus_attrs,
0455 };
0456 
0457 const struct attribute_group *pci_bus_groups[] = {
0458     &pci_bus_group,
0459     NULL,
0460 };
0461 
0462 static ssize_t dev_rescan_store(struct device *dev,
0463                 struct device_attribute *attr, const char *buf,
0464                 size_t count)
0465 {
0466     unsigned long val;
0467     struct pci_dev *pdev = to_pci_dev(dev);
0468 
0469     if (kstrtoul(buf, 0, &val) < 0)
0470         return -EINVAL;
0471 
0472     if (val) {
0473         pci_lock_rescan_remove();
0474         pci_rescan_bus(pdev->bus);
0475         pci_unlock_rescan_remove();
0476     }
0477     return count;
0478 }
0479 static struct device_attribute dev_attr_dev_rescan = __ATTR(rescan, 0200, NULL,
0480                                 dev_rescan_store);
0481 
0482 static ssize_t remove_store(struct device *dev, struct device_attribute *attr,
0483                 const char *buf, size_t count)
0484 {
0485     unsigned long val;
0486 
0487     if (kstrtoul(buf, 0, &val) < 0)
0488         return -EINVAL;
0489 
0490     if (val && device_remove_file_self(dev, attr))
0491         pci_stop_and_remove_bus_device_locked(to_pci_dev(dev));
0492     return count;
0493 }
0494 static DEVICE_ATTR_IGNORE_LOCKDEP(remove, 0220, NULL,
0495                   remove_store);
0496 
0497 static ssize_t bus_rescan_store(struct device *dev,
0498                 struct device_attribute *attr,
0499                 const char *buf, size_t count)
0500 {
0501     unsigned long val;
0502     struct pci_bus *bus = to_pci_bus(dev);
0503 
0504     if (kstrtoul(buf, 0, &val) < 0)
0505         return -EINVAL;
0506 
0507     if (val) {
0508         pci_lock_rescan_remove();
0509         if (!pci_is_root_bus(bus) && list_empty(&bus->devices))
0510             pci_rescan_bus_bridge_resize(bus->self);
0511         else
0512             pci_rescan_bus(bus);
0513         pci_unlock_rescan_remove();
0514     }
0515     return count;
0516 }
0517 static struct device_attribute dev_attr_bus_rescan = __ATTR(rescan, 0200, NULL,
0518                                 bus_rescan_store);
0519 
0520 #if defined(CONFIG_PM) && defined(CONFIG_ACPI)
0521 static ssize_t d3cold_allowed_store(struct device *dev,
0522                     struct device_attribute *attr,
0523                     const char *buf, size_t count)
0524 {
0525     struct pci_dev *pdev = to_pci_dev(dev);
0526     unsigned long val;
0527 
0528     if (kstrtoul(buf, 0, &val) < 0)
0529         return -EINVAL;
0530 
0531     pdev->d3cold_allowed = !!val;
0532     if (pdev->d3cold_allowed)
0533         pci_d3cold_enable(pdev);
0534     else
0535         pci_d3cold_disable(pdev);
0536 
0537     pm_runtime_resume(dev);
0538 
0539     return count;
0540 }
0541 
0542 static ssize_t d3cold_allowed_show(struct device *dev,
0543                    struct device_attribute *attr, char *buf)
0544 {
0545     struct pci_dev *pdev = to_pci_dev(dev);
0546     return sysfs_emit(buf, "%u\n", pdev->d3cold_allowed);
0547 }
0548 static DEVICE_ATTR_RW(d3cold_allowed);
0549 #endif
0550 
0551 #ifdef CONFIG_OF
0552 static ssize_t devspec_show(struct device *dev,
0553                 struct device_attribute *attr, char *buf)
0554 {
0555     struct pci_dev *pdev = to_pci_dev(dev);
0556     struct device_node *np = pci_device_to_OF_node(pdev);
0557 
0558     if (np == NULL)
0559         return 0;
0560     return sysfs_emit(buf, "%pOF\n", np);
0561 }
0562 static DEVICE_ATTR_RO(devspec);
0563 #endif
0564 
0565 static ssize_t driver_override_store(struct device *dev,
0566                      struct device_attribute *attr,
0567                      const char *buf, size_t count)
0568 {
0569     struct pci_dev *pdev = to_pci_dev(dev);
0570     int ret;
0571 
0572     ret = driver_set_override(dev, &pdev->driver_override, buf, count);
0573     if (ret)
0574         return ret;
0575 
0576     return count;
0577 }
0578 
0579 static ssize_t driver_override_show(struct device *dev,
0580                     struct device_attribute *attr, char *buf)
0581 {
0582     struct pci_dev *pdev = to_pci_dev(dev);
0583     ssize_t len;
0584 
0585     device_lock(dev);
0586     len = sysfs_emit(buf, "%s\n", pdev->driver_override);
0587     device_unlock(dev);
0588     return len;
0589 }
0590 static DEVICE_ATTR_RW(driver_override);
0591 
0592 static struct attribute *pci_dev_attrs[] = {
0593     &dev_attr_power_state.attr,
0594     &dev_attr_resource.attr,
0595     &dev_attr_vendor.attr,
0596     &dev_attr_device.attr,
0597     &dev_attr_subsystem_vendor.attr,
0598     &dev_attr_subsystem_device.attr,
0599     &dev_attr_revision.attr,
0600     &dev_attr_class.attr,
0601     &dev_attr_irq.attr,
0602     &dev_attr_local_cpus.attr,
0603     &dev_attr_local_cpulist.attr,
0604     &dev_attr_modalias.attr,
0605 #ifdef CONFIG_NUMA
0606     &dev_attr_numa_node.attr,
0607 #endif
0608     &dev_attr_dma_mask_bits.attr,
0609     &dev_attr_consistent_dma_mask_bits.attr,
0610     &dev_attr_enable.attr,
0611     &dev_attr_broken_parity_status.attr,
0612     &dev_attr_msi_bus.attr,
0613 #if defined(CONFIG_PM) && defined(CONFIG_ACPI)
0614     &dev_attr_d3cold_allowed.attr,
0615 #endif
0616 #ifdef CONFIG_OF
0617     &dev_attr_devspec.attr,
0618 #endif
0619     &dev_attr_driver_override.attr,
0620     &dev_attr_ari_enabled.attr,
0621     NULL,
0622 };
0623 
0624 static struct attribute *pci_bridge_attrs[] = {
0625     &dev_attr_subordinate_bus_number.attr,
0626     &dev_attr_secondary_bus_number.attr,
0627     NULL,
0628 };
0629 
0630 static struct attribute *pcie_dev_attrs[] = {
0631     &dev_attr_current_link_speed.attr,
0632     &dev_attr_current_link_width.attr,
0633     &dev_attr_max_link_width.attr,
0634     &dev_attr_max_link_speed.attr,
0635     NULL,
0636 };
0637 
0638 static struct attribute *pcibus_attrs[] = {
0639     &dev_attr_bus_rescan.attr,
0640     &dev_attr_cpuaffinity.attr,
0641     &dev_attr_cpulistaffinity.attr,
0642     NULL,
0643 };
0644 
0645 static const struct attribute_group pcibus_group = {
0646     .attrs = pcibus_attrs,
0647 };
0648 
0649 const struct attribute_group *pcibus_groups[] = {
0650     &pcibus_group,
0651     NULL,
0652 };
0653 
0654 static ssize_t boot_vga_show(struct device *dev, struct device_attribute *attr,
0655                  char *buf)
0656 {
0657     struct pci_dev *pdev = to_pci_dev(dev);
0658     struct pci_dev *vga_dev = vga_default_device();
0659 
0660     if (vga_dev)
0661         return sysfs_emit(buf, "%u\n", (pdev == vga_dev));
0662 
0663     return sysfs_emit(buf, "%u\n",
0664               !!(pdev->resource[PCI_ROM_RESOURCE].flags &
0665                  IORESOURCE_ROM_SHADOW));
0666 }
0667 static DEVICE_ATTR_RO(boot_vga);
0668 
0669 static ssize_t pci_read_config(struct file *filp, struct kobject *kobj,
0670                    struct bin_attribute *bin_attr, char *buf,
0671                    loff_t off, size_t count)
0672 {
0673     struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj));
0674     unsigned int size = 64;
0675     loff_t init_off = off;
0676     u8 *data = (u8 *) buf;
0677 
0678     /* Several chips lock up trying to read undefined config space */
0679     if (file_ns_capable(filp, &init_user_ns, CAP_SYS_ADMIN))
0680         size = dev->cfg_size;
0681     else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
0682         size = 128;
0683 
0684     if (off > size)
0685         return 0;
0686     if (off + count > size) {
0687         size -= off;
0688         count = size;
0689     } else {
0690         size = count;
0691     }
0692 
0693     pci_config_pm_runtime_get(dev);
0694 
0695     if ((off & 1) && size) {
0696         u8 val;
0697         pci_user_read_config_byte(dev, off, &val);
0698         data[off - init_off] = val;
0699         off++;
0700         size--;
0701     }
0702 
0703     if ((off & 3) && size > 2) {
0704         u16 val;
0705         pci_user_read_config_word(dev, off, &val);
0706         data[off - init_off] = val & 0xff;
0707         data[off - init_off + 1] = (val >> 8) & 0xff;
0708         off += 2;
0709         size -= 2;
0710     }
0711 
0712     while (size > 3) {
0713         u32 val;
0714         pci_user_read_config_dword(dev, off, &val);
0715         data[off - init_off] = val & 0xff;
0716         data[off - init_off + 1] = (val >> 8) & 0xff;
0717         data[off - init_off + 2] = (val >> 16) & 0xff;
0718         data[off - init_off + 3] = (val >> 24) & 0xff;
0719         off += 4;
0720         size -= 4;
0721         cond_resched();
0722     }
0723 
0724     if (size >= 2) {
0725         u16 val;
0726         pci_user_read_config_word(dev, off, &val);
0727         data[off - init_off] = val & 0xff;
0728         data[off - init_off + 1] = (val >> 8) & 0xff;
0729         off += 2;
0730         size -= 2;
0731     }
0732 
0733     if (size > 0) {
0734         u8 val;
0735         pci_user_read_config_byte(dev, off, &val);
0736         data[off - init_off] = val;
0737     }
0738 
0739     pci_config_pm_runtime_put(dev);
0740 
0741     return count;
0742 }
0743 
0744 static ssize_t pci_write_config(struct file *filp, struct kobject *kobj,
0745                 struct bin_attribute *bin_attr, char *buf,
0746                 loff_t off, size_t count)
0747 {
0748     struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj));
0749     unsigned int size = count;
0750     loff_t init_off = off;
0751     u8 *data = (u8 *) buf;
0752     int ret;
0753 
0754     ret = security_locked_down(LOCKDOWN_PCI_ACCESS);
0755     if (ret)
0756         return ret;
0757 
0758     if (off > dev->cfg_size)
0759         return 0;
0760     if (off + count > dev->cfg_size) {
0761         size = dev->cfg_size - off;
0762         count = size;
0763     }
0764 
0765     pci_config_pm_runtime_get(dev);
0766 
0767     if ((off & 1) && size) {
0768         pci_user_write_config_byte(dev, off, data[off - init_off]);
0769         off++;
0770         size--;
0771     }
0772 
0773     if ((off & 3) && size > 2) {
0774         u16 val = data[off - init_off];
0775         val |= (u16) data[off - init_off + 1] << 8;
0776         pci_user_write_config_word(dev, off, val);
0777         off += 2;
0778         size -= 2;
0779     }
0780 
0781     while (size > 3) {
0782         u32 val = data[off - init_off];
0783         val |= (u32) data[off - init_off + 1] << 8;
0784         val |= (u32) data[off - init_off + 2] << 16;
0785         val |= (u32) data[off - init_off + 3] << 24;
0786         pci_user_write_config_dword(dev, off, val);
0787         off += 4;
0788         size -= 4;
0789     }
0790 
0791     if (size >= 2) {
0792         u16 val = data[off - init_off];
0793         val |= (u16) data[off - init_off + 1] << 8;
0794         pci_user_write_config_word(dev, off, val);
0795         off += 2;
0796         size -= 2;
0797     }
0798 
0799     if (size)
0800         pci_user_write_config_byte(dev, off, data[off - init_off]);
0801 
0802     pci_config_pm_runtime_put(dev);
0803 
0804     return count;
0805 }
0806 static BIN_ATTR(config, 0644, pci_read_config, pci_write_config, 0);
0807 
0808 static struct bin_attribute *pci_dev_config_attrs[] = {
0809     &bin_attr_config,
0810     NULL,
0811 };
0812 
0813 static umode_t pci_dev_config_attr_is_visible(struct kobject *kobj,
0814                           struct bin_attribute *a, int n)
0815 {
0816     struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
0817 
0818     a->size = PCI_CFG_SPACE_SIZE;
0819     if (pdev->cfg_size > PCI_CFG_SPACE_SIZE)
0820         a->size = PCI_CFG_SPACE_EXP_SIZE;
0821 
0822     return a->attr.mode;
0823 }
0824 
0825 static const struct attribute_group pci_dev_config_attr_group = {
0826     .bin_attrs = pci_dev_config_attrs,
0827     .is_bin_visible = pci_dev_config_attr_is_visible,
0828 };
0829 
0830 #ifdef HAVE_PCI_LEGACY
0831 /**
0832  * pci_read_legacy_io - read byte(s) from legacy I/O port space
0833  * @filp: open sysfs file
0834  * @kobj: kobject corresponding to file to read from
0835  * @bin_attr: struct bin_attribute for this file
0836  * @buf: buffer to store results
0837  * @off: offset into legacy I/O port space
0838  * @count: number of bytes to read
0839  *
0840  * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
0841  * callback routine (pci_legacy_read).
0842  */
0843 static ssize_t pci_read_legacy_io(struct file *filp, struct kobject *kobj,
0844                   struct bin_attribute *bin_attr, char *buf,
0845                   loff_t off, size_t count)
0846 {
0847     struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
0848 
0849     /* Only support 1, 2 or 4 byte accesses */
0850     if (count != 1 && count != 2 && count != 4)
0851         return -EINVAL;
0852 
0853     return pci_legacy_read(bus, off, (u32 *)buf, count);
0854 }
0855 
0856 /**
0857  * pci_write_legacy_io - write byte(s) to legacy I/O port space
0858  * @filp: open sysfs file
0859  * @kobj: kobject corresponding to file to read from
0860  * @bin_attr: struct bin_attribute for this file
0861  * @buf: buffer containing value to be written
0862  * @off: offset into legacy I/O port space
0863  * @count: number of bytes to write
0864  *
0865  * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
0866  * callback routine (pci_legacy_write).
0867  */
0868 static ssize_t pci_write_legacy_io(struct file *filp, struct kobject *kobj,
0869                    struct bin_attribute *bin_attr, char *buf,
0870                    loff_t off, size_t count)
0871 {
0872     struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
0873 
0874     /* Only support 1, 2 or 4 byte accesses */
0875     if (count != 1 && count != 2 && count != 4)
0876         return -EINVAL;
0877 
0878     return pci_legacy_write(bus, off, *(u32 *)buf, count);
0879 }
0880 
0881 /**
0882  * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
0883  * @filp: open sysfs file
0884  * @kobj: kobject corresponding to device to be mapped
0885  * @attr: struct bin_attribute for this file
0886  * @vma: struct vm_area_struct passed to mmap
0887  *
0888  * Uses an arch specific callback, pci_mmap_legacy_mem_page_range, to mmap
0889  * legacy memory space (first meg of bus space) into application virtual
0890  * memory space.
0891  */
0892 static int pci_mmap_legacy_mem(struct file *filp, struct kobject *kobj,
0893                    struct bin_attribute *attr,
0894                    struct vm_area_struct *vma)
0895 {
0896     struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
0897 
0898     return pci_mmap_legacy_page_range(bus, vma, pci_mmap_mem);
0899 }
0900 
0901 /**
0902  * pci_mmap_legacy_io - map legacy PCI IO into user memory space
0903  * @filp: open sysfs file
0904  * @kobj: kobject corresponding to device to be mapped
0905  * @attr: struct bin_attribute for this file
0906  * @vma: struct vm_area_struct passed to mmap
0907  *
0908  * Uses an arch specific callback, pci_mmap_legacy_io_page_range, to mmap
0909  * legacy IO space (first meg of bus space) into application virtual
0910  * memory space. Returns -ENOSYS if the operation isn't supported
0911  */
0912 static int pci_mmap_legacy_io(struct file *filp, struct kobject *kobj,
0913                   struct bin_attribute *attr,
0914                   struct vm_area_struct *vma)
0915 {
0916     struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
0917 
0918     return pci_mmap_legacy_page_range(bus, vma, pci_mmap_io);
0919 }
0920 
0921 /**
0922  * pci_adjust_legacy_attr - adjustment of legacy file attributes
0923  * @b: bus to create files under
0924  * @mmap_type: I/O port or memory
0925  *
0926  * Stub implementation. Can be overridden by arch if necessary.
0927  */
0928 void __weak pci_adjust_legacy_attr(struct pci_bus *b,
0929                    enum pci_mmap_state mmap_type)
0930 {
0931 }
0932 
0933 /**
0934  * pci_create_legacy_files - create legacy I/O port and memory files
0935  * @b: bus to create files under
0936  *
0937  * Some platforms allow access to legacy I/O port and ISA memory space on
0938  * a per-bus basis.  This routine creates the files and ties them into
0939  * their associated read, write and mmap files from pci-sysfs.c
0940  *
0941  * On error unwind, but don't propagate the error to the caller
0942  * as it is ok to set up the PCI bus without these files.
0943  */
0944 void pci_create_legacy_files(struct pci_bus *b)
0945 {
0946     int error;
0947 
0948     if (!sysfs_initialized)
0949         return;
0950 
0951     b->legacy_io = kcalloc(2, sizeof(struct bin_attribute),
0952                    GFP_ATOMIC);
0953     if (!b->legacy_io)
0954         goto kzalloc_err;
0955 
0956     sysfs_bin_attr_init(b->legacy_io);
0957     b->legacy_io->attr.name = "legacy_io";
0958     b->legacy_io->size = 0xffff;
0959     b->legacy_io->attr.mode = 0600;
0960     b->legacy_io->read = pci_read_legacy_io;
0961     b->legacy_io->write = pci_write_legacy_io;
0962     b->legacy_io->mmap = pci_mmap_legacy_io;
0963     b->legacy_io->f_mapping = iomem_get_mapping;
0964     pci_adjust_legacy_attr(b, pci_mmap_io);
0965     error = device_create_bin_file(&b->dev, b->legacy_io);
0966     if (error)
0967         goto legacy_io_err;
0968 
0969     /* Allocated above after the legacy_io struct */
0970     b->legacy_mem = b->legacy_io + 1;
0971     sysfs_bin_attr_init(b->legacy_mem);
0972     b->legacy_mem->attr.name = "legacy_mem";
0973     b->legacy_mem->size = 1024*1024;
0974     b->legacy_mem->attr.mode = 0600;
0975     b->legacy_mem->mmap = pci_mmap_legacy_mem;
0976     b->legacy_mem->f_mapping = iomem_get_mapping;
0977     pci_adjust_legacy_attr(b, pci_mmap_mem);
0978     error = device_create_bin_file(&b->dev, b->legacy_mem);
0979     if (error)
0980         goto legacy_mem_err;
0981 
0982     return;
0983 
0984 legacy_mem_err:
0985     device_remove_bin_file(&b->dev, b->legacy_io);
0986 legacy_io_err:
0987     kfree(b->legacy_io);
0988     b->legacy_io = NULL;
0989 kzalloc_err:
0990     dev_warn(&b->dev, "could not create legacy I/O port and ISA memory resources in sysfs\n");
0991 }
0992 
0993 void pci_remove_legacy_files(struct pci_bus *b)
0994 {
0995     if (b->legacy_io) {
0996         device_remove_bin_file(&b->dev, b->legacy_io);
0997         device_remove_bin_file(&b->dev, b->legacy_mem);
0998         kfree(b->legacy_io); /* both are allocated here */
0999     }
1000 }
1001 #endif /* HAVE_PCI_LEGACY */
1002 
1003 #if defined(HAVE_PCI_MMAP) || defined(ARCH_GENERIC_PCI_MMAP_RESOURCE)
1004 
1005 int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vma,
1006           enum pci_mmap_api mmap_api)
1007 {
1008     unsigned long nr, start, size;
1009     resource_size_t pci_start = 0, pci_end;
1010 
1011     if (pci_resource_len(pdev, resno) == 0)
1012         return 0;
1013     nr = vma_pages(vma);
1014     start = vma->vm_pgoff;
1015     size = ((pci_resource_len(pdev, resno) - 1) >> PAGE_SHIFT) + 1;
1016     if (mmap_api == PCI_MMAP_PROCFS) {
1017         pci_resource_to_user(pdev, resno, &pdev->resource[resno],
1018                      &pci_start, &pci_end);
1019         pci_start >>= PAGE_SHIFT;
1020     }
1021     if (start >= pci_start && start < pci_start + size &&
1022             start + nr <= pci_start + size)
1023         return 1;
1024     return 0;
1025 }
1026 
1027 /**
1028  * pci_mmap_resource - map a PCI resource into user memory space
1029  * @kobj: kobject for mapping
1030  * @attr: struct bin_attribute for the file being mapped
1031  * @vma: struct vm_area_struct passed into the mmap
1032  * @write_combine: 1 for write_combine mapping
1033  *
1034  * Use the regular PCI mapping routines to map a PCI resource into userspace.
1035  */
1036 static int pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr,
1037                  struct vm_area_struct *vma, int write_combine)
1038 {
1039     struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
1040     int bar = (unsigned long)attr->private;
1041     enum pci_mmap_state mmap_type;
1042     struct resource *res = &pdev->resource[bar];
1043     int ret;
1044 
1045     ret = security_locked_down(LOCKDOWN_PCI_ACCESS);
1046     if (ret)
1047         return ret;
1048 
1049     if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(res->start))
1050         return -EINVAL;
1051 
1052     if (!pci_mmap_fits(pdev, bar, vma, PCI_MMAP_SYSFS))
1053         return -EINVAL;
1054 
1055     mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io;
1056 
1057     return pci_mmap_resource_range(pdev, bar, vma, mmap_type, write_combine);
1058 }
1059 
1060 static int pci_mmap_resource_uc(struct file *filp, struct kobject *kobj,
1061                 struct bin_attribute *attr,
1062                 struct vm_area_struct *vma)
1063 {
1064     return pci_mmap_resource(kobj, attr, vma, 0);
1065 }
1066 
1067 static int pci_mmap_resource_wc(struct file *filp, struct kobject *kobj,
1068                 struct bin_attribute *attr,
1069                 struct vm_area_struct *vma)
1070 {
1071     return pci_mmap_resource(kobj, attr, vma, 1);
1072 }
1073 
1074 static ssize_t pci_resource_io(struct file *filp, struct kobject *kobj,
1075                    struct bin_attribute *attr, char *buf,
1076                    loff_t off, size_t count, bool write)
1077 {
1078     struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
1079     int bar = (unsigned long)attr->private;
1080     unsigned long port = off;
1081 
1082     port += pci_resource_start(pdev, bar);
1083 
1084     if (port > pci_resource_end(pdev, bar))
1085         return 0;
1086 
1087     if (port + count - 1 > pci_resource_end(pdev, bar))
1088         return -EINVAL;
1089 
1090     switch (count) {
1091     case 1:
1092         if (write)
1093             outb(*(u8 *)buf, port);
1094         else
1095             *(u8 *)buf = inb(port);
1096         return 1;
1097     case 2:
1098         if (write)
1099             outw(*(u16 *)buf, port);
1100         else
1101             *(u16 *)buf = inw(port);
1102         return 2;
1103     case 4:
1104         if (write)
1105             outl(*(u32 *)buf, port);
1106         else
1107             *(u32 *)buf = inl(port);
1108         return 4;
1109     }
1110     return -EINVAL;
1111 }
1112 
1113 static ssize_t pci_read_resource_io(struct file *filp, struct kobject *kobj,
1114                     struct bin_attribute *attr, char *buf,
1115                     loff_t off, size_t count)
1116 {
1117     return pci_resource_io(filp, kobj, attr, buf, off, count, false);
1118 }
1119 
1120 static ssize_t pci_write_resource_io(struct file *filp, struct kobject *kobj,
1121                      struct bin_attribute *attr, char *buf,
1122                      loff_t off, size_t count)
1123 {
1124     int ret;
1125 
1126     ret = security_locked_down(LOCKDOWN_PCI_ACCESS);
1127     if (ret)
1128         return ret;
1129 
1130     return pci_resource_io(filp, kobj, attr, buf, off, count, true);
1131 }
1132 
1133 /**
1134  * pci_remove_resource_files - cleanup resource files
1135  * @pdev: dev to cleanup
1136  *
1137  * If we created resource files for @pdev, remove them from sysfs and
1138  * free their resources.
1139  */
1140 static void pci_remove_resource_files(struct pci_dev *pdev)
1141 {
1142     int i;
1143 
1144     for (i = 0; i < PCI_STD_NUM_BARS; i++) {
1145         struct bin_attribute *res_attr;
1146 
1147         res_attr = pdev->res_attr[i];
1148         if (res_attr) {
1149             sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
1150             kfree(res_attr);
1151         }
1152 
1153         res_attr = pdev->res_attr_wc[i];
1154         if (res_attr) {
1155             sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
1156             kfree(res_attr);
1157         }
1158     }
1159 }
1160 
1161 static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine)
1162 {
1163     /* allocate attribute structure, piggyback attribute name */
1164     int name_len = write_combine ? 13 : 10;
1165     struct bin_attribute *res_attr;
1166     char *res_attr_name;
1167     int retval;
1168 
1169     res_attr = kzalloc(sizeof(*res_attr) + name_len, GFP_ATOMIC);
1170     if (!res_attr)
1171         return -ENOMEM;
1172 
1173     res_attr_name = (char *)(res_attr + 1);
1174 
1175     sysfs_bin_attr_init(res_attr);
1176     if (write_combine) {
1177         pdev->res_attr_wc[num] = res_attr;
1178         sprintf(res_attr_name, "resource%d_wc", num);
1179         res_attr->mmap = pci_mmap_resource_wc;
1180     } else {
1181         pdev->res_attr[num] = res_attr;
1182         sprintf(res_attr_name, "resource%d", num);
1183         if (pci_resource_flags(pdev, num) & IORESOURCE_IO) {
1184             res_attr->read = pci_read_resource_io;
1185             res_attr->write = pci_write_resource_io;
1186             if (arch_can_pci_mmap_io())
1187                 res_attr->mmap = pci_mmap_resource_uc;
1188         } else {
1189             res_attr->mmap = pci_mmap_resource_uc;
1190         }
1191     }
1192     if (res_attr->mmap)
1193         res_attr->f_mapping = iomem_get_mapping;
1194     res_attr->attr.name = res_attr_name;
1195     res_attr->attr.mode = 0600;
1196     res_attr->size = pci_resource_len(pdev, num);
1197     res_attr->private = (void *)(unsigned long)num;
1198     retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
1199     if (retval)
1200         kfree(res_attr);
1201 
1202     return retval;
1203 }
1204 
1205 /**
1206  * pci_create_resource_files - create resource files in sysfs for @dev
1207  * @pdev: dev in question
1208  *
1209  * Walk the resources in @pdev creating files for each resource available.
1210  */
1211 static int pci_create_resource_files(struct pci_dev *pdev)
1212 {
1213     int i;
1214     int retval;
1215 
1216     /* Expose the PCI resources from this device as files */
1217     for (i = 0; i < PCI_STD_NUM_BARS; i++) {
1218 
1219         /* skip empty resources */
1220         if (!pci_resource_len(pdev, i))
1221             continue;
1222 
1223         retval = pci_create_attr(pdev, i, 0);
1224         /* for prefetchable resources, create a WC mappable file */
1225         if (!retval && arch_can_pci_mmap_wc() &&
1226             pdev->resource[i].flags & IORESOURCE_PREFETCH)
1227             retval = pci_create_attr(pdev, i, 1);
1228         if (retval) {
1229             pci_remove_resource_files(pdev);
1230             return retval;
1231         }
1232     }
1233     return 0;
1234 }
1235 #else /* !(defined(HAVE_PCI_MMAP) || defined(ARCH_GENERIC_PCI_MMAP_RESOURCE)) */
1236 int __weak pci_create_resource_files(struct pci_dev *dev) { return 0; }
1237 void __weak pci_remove_resource_files(struct pci_dev *dev) { return; }
1238 #endif
1239 
1240 /**
1241  * pci_write_rom - used to enable access to the PCI ROM display
1242  * @filp: sysfs file
1243  * @kobj: kernel object handle
1244  * @bin_attr: struct bin_attribute for this file
1245  * @buf: user input
1246  * @off: file offset
1247  * @count: number of byte in input
1248  *
1249  * writing anything except 0 enables it
1250  */
1251 static ssize_t pci_write_rom(struct file *filp, struct kobject *kobj,
1252                  struct bin_attribute *bin_attr, char *buf,
1253                  loff_t off, size_t count)
1254 {
1255     struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
1256 
1257     if ((off ==  0) && (*buf == '0') && (count == 2))
1258         pdev->rom_attr_enabled = 0;
1259     else
1260         pdev->rom_attr_enabled = 1;
1261 
1262     return count;
1263 }
1264 
1265 /**
1266  * pci_read_rom - read a PCI ROM
1267  * @filp: sysfs file
1268  * @kobj: kernel object handle
1269  * @bin_attr: struct bin_attribute for this file
1270  * @buf: where to put the data we read from the ROM
1271  * @off: file offset
1272  * @count: number of bytes to read
1273  *
1274  * Put @count bytes starting at @off into @buf from the ROM in the PCI
1275  * device corresponding to @kobj.
1276  */
1277 static ssize_t pci_read_rom(struct file *filp, struct kobject *kobj,
1278                 struct bin_attribute *bin_attr, char *buf,
1279                 loff_t off, size_t count)
1280 {
1281     struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
1282     void __iomem *rom;
1283     size_t size;
1284 
1285     if (!pdev->rom_attr_enabled)
1286         return -EINVAL;
1287 
1288     rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */
1289     if (!rom || !size)
1290         return -EIO;
1291 
1292     if (off >= size)
1293         count = 0;
1294     else {
1295         if (off + count > size)
1296             count = size - off;
1297 
1298         memcpy_fromio(buf, rom + off, count);
1299     }
1300     pci_unmap_rom(pdev, rom);
1301 
1302     return count;
1303 }
1304 static BIN_ATTR(rom, 0600, pci_read_rom, pci_write_rom, 0);
1305 
1306 static struct bin_attribute *pci_dev_rom_attrs[] = {
1307     &bin_attr_rom,
1308     NULL,
1309 };
1310 
1311 static umode_t pci_dev_rom_attr_is_visible(struct kobject *kobj,
1312                        struct bin_attribute *a, int n)
1313 {
1314     struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
1315     size_t rom_size;
1316 
1317     /* If the device has a ROM, try to expose it in sysfs. */
1318     rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
1319     if (!rom_size)
1320         return 0;
1321 
1322     a->size = rom_size;
1323 
1324     return a->attr.mode;
1325 }
1326 
1327 static const struct attribute_group pci_dev_rom_attr_group = {
1328     .bin_attrs = pci_dev_rom_attrs,
1329     .is_bin_visible = pci_dev_rom_attr_is_visible,
1330 };
1331 
1332 static ssize_t reset_store(struct device *dev, struct device_attribute *attr,
1333                const char *buf, size_t count)
1334 {
1335     struct pci_dev *pdev = to_pci_dev(dev);
1336     unsigned long val;
1337     ssize_t result;
1338 
1339     if (kstrtoul(buf, 0, &val) < 0)
1340         return -EINVAL;
1341 
1342     if (val != 1)
1343         return -EINVAL;
1344 
1345     pm_runtime_get_sync(dev);
1346     result = pci_reset_function(pdev);
1347     pm_runtime_put(dev);
1348     if (result < 0)
1349         return result;
1350 
1351     return count;
1352 }
1353 static DEVICE_ATTR_WO(reset);
1354 
1355 static struct attribute *pci_dev_reset_attrs[] = {
1356     &dev_attr_reset.attr,
1357     NULL,
1358 };
1359 
1360 static umode_t pci_dev_reset_attr_is_visible(struct kobject *kobj,
1361                          struct attribute *a, int n)
1362 {
1363     struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
1364 
1365     if (!pci_reset_supported(pdev))
1366         return 0;
1367 
1368     return a->mode;
1369 }
1370 
1371 static const struct attribute_group pci_dev_reset_attr_group = {
1372     .attrs = pci_dev_reset_attrs,
1373     .is_visible = pci_dev_reset_attr_is_visible,
1374 };
1375 
1376 int __must_check pci_create_sysfs_dev_files(struct pci_dev *pdev)
1377 {
1378     if (!sysfs_initialized)
1379         return -EACCES;
1380 
1381     return pci_create_resource_files(pdev);
1382 }
1383 
1384 /**
1385  * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
1386  * @pdev: device whose entries we should free
1387  *
1388  * Cleanup when @pdev is removed from sysfs.
1389  */
1390 void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
1391 {
1392     if (!sysfs_initialized)
1393         return;
1394 
1395     pci_remove_resource_files(pdev);
1396 }
1397 
1398 static int __init pci_sysfs_init(void)
1399 {
1400     struct pci_dev *pdev = NULL;
1401     struct pci_bus *pbus = NULL;
1402     int retval;
1403 
1404     sysfs_initialized = 1;
1405     for_each_pci_dev(pdev) {
1406         retval = pci_create_sysfs_dev_files(pdev);
1407         if (retval) {
1408             pci_dev_put(pdev);
1409             return retval;
1410         }
1411     }
1412 
1413     while ((pbus = pci_find_next_bus(pbus)))
1414         pci_create_legacy_files(pbus);
1415 
1416     return 0;
1417 }
1418 late_initcall(pci_sysfs_init);
1419 
1420 static struct attribute *pci_dev_dev_attrs[] = {
1421     &dev_attr_boot_vga.attr,
1422     NULL,
1423 };
1424 
1425 static umode_t pci_dev_attrs_are_visible(struct kobject *kobj,
1426                      struct attribute *a, int n)
1427 {
1428     struct device *dev = kobj_to_dev(kobj);
1429     struct pci_dev *pdev = to_pci_dev(dev);
1430 
1431     if (a == &dev_attr_boot_vga.attr)
1432         if ((pdev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
1433             return 0;
1434 
1435     return a->mode;
1436 }
1437 
1438 static struct attribute *pci_dev_hp_attrs[] = {
1439     &dev_attr_remove.attr,
1440     &dev_attr_dev_rescan.attr,
1441     NULL,
1442 };
1443 
1444 static umode_t pci_dev_hp_attrs_are_visible(struct kobject *kobj,
1445                         struct attribute *a, int n)
1446 {
1447     struct device *dev = kobj_to_dev(kobj);
1448     struct pci_dev *pdev = to_pci_dev(dev);
1449 
1450     if (pdev->is_virtfn)
1451         return 0;
1452 
1453     return a->mode;
1454 }
1455 
1456 static umode_t pci_bridge_attrs_are_visible(struct kobject *kobj,
1457                         struct attribute *a, int n)
1458 {
1459     struct device *dev = kobj_to_dev(kobj);
1460     struct pci_dev *pdev = to_pci_dev(dev);
1461 
1462     if (pci_is_bridge(pdev))
1463         return a->mode;
1464 
1465     return 0;
1466 }
1467 
1468 static umode_t pcie_dev_attrs_are_visible(struct kobject *kobj,
1469                       struct attribute *a, int n)
1470 {
1471     struct device *dev = kobj_to_dev(kobj);
1472     struct pci_dev *pdev = to_pci_dev(dev);
1473 
1474     if (pci_is_pcie(pdev))
1475         return a->mode;
1476 
1477     return 0;
1478 }
1479 
1480 static const struct attribute_group pci_dev_group = {
1481     .attrs = pci_dev_attrs,
1482 };
1483 
1484 const struct attribute_group *pci_dev_groups[] = {
1485     &pci_dev_group,
1486     &pci_dev_config_attr_group,
1487     &pci_dev_rom_attr_group,
1488     &pci_dev_reset_attr_group,
1489     &pci_dev_reset_method_attr_group,
1490     &pci_dev_vpd_attr_group,
1491 #ifdef CONFIG_DMI
1492     &pci_dev_smbios_attr_group,
1493 #endif
1494 #ifdef CONFIG_ACPI
1495     &pci_dev_acpi_attr_group,
1496 #endif
1497     NULL,
1498 };
1499 
1500 static const struct attribute_group pci_dev_hp_attr_group = {
1501     .attrs = pci_dev_hp_attrs,
1502     .is_visible = pci_dev_hp_attrs_are_visible,
1503 };
1504 
1505 static const struct attribute_group pci_dev_attr_group = {
1506     .attrs = pci_dev_dev_attrs,
1507     .is_visible = pci_dev_attrs_are_visible,
1508 };
1509 
1510 static const struct attribute_group pci_bridge_attr_group = {
1511     .attrs = pci_bridge_attrs,
1512     .is_visible = pci_bridge_attrs_are_visible,
1513 };
1514 
1515 static const struct attribute_group pcie_dev_attr_group = {
1516     .attrs = pcie_dev_attrs,
1517     .is_visible = pcie_dev_attrs_are_visible,
1518 };
1519 
1520 static const struct attribute_group *pci_dev_attr_groups[] = {
1521     &pci_dev_attr_group,
1522     &pci_dev_hp_attr_group,
1523 #ifdef CONFIG_PCI_IOV
1524     &sriov_pf_dev_attr_group,
1525     &sriov_vf_dev_attr_group,
1526 #endif
1527     &pci_bridge_attr_group,
1528     &pcie_dev_attr_group,
1529 #ifdef CONFIG_PCIEAER
1530     &aer_stats_attr_group,
1531 #endif
1532 #ifdef CONFIG_PCIEASPM
1533     &aspm_ctrl_attr_group,
1534 #endif
1535     NULL,
1536 };
1537 
1538 const struct device_type pci_dev_type = {
1539     .groups = pci_dev_attr_groups,
1540 };