2 * drivers/pci/pci-sysfs.c
4 * (C) Copyright 2002-2004 Greg Kroah-Hartman <greg@kroah.com>
5 * (C) Copyright 2002-2004 IBM Corp.
6 * (C) Copyright 2003 Matthew Wilcox
7 * (C) Copyright 2003 Hewlett-Packard
8 * (C) Copyright 2004 Jon Smirl <jonsmirl@yahoo.com>
9 * (C) Copyright 2004 Silicon Graphics, Inc. Jesse Barnes <jbarnes@sgi.com>
11 * File attributes for PCI devices
13 * Modeled after usb's driverfs.c
18 #include <linux/kernel.h>
19 #include <linux/sched.h>
20 #include <linux/pci.h>
21 #include <linux/stat.h>
22 #include <linux/topology.h>
24 #include <linux/capability.h>
25 #include <linux/pci-aspm.h>
28 static int sysfs_initialized; /* = 0 */
30 /* show configuration fields */
31 #define pci_config_attr(field, format_string) \
33 field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
35 struct pci_dev *pdev; \
37 pdev = to_pci_dev (dev); \
38 return sprintf (buf, format_string, pdev->field); \
41 pci_config_attr(vendor, "0x%04x\n");
42 pci_config_attr(device, "0x%04x\n");
43 pci_config_attr(subsystem_vendor, "0x%04x\n");
44 pci_config_attr(subsystem_device, "0x%04x\n");
45 pci_config_attr(class, "0x%06x\n");
46 pci_config_attr(irq, "%u\n");
48 static ssize_t broken_parity_status_show(struct device *dev,
49 struct device_attribute *attr,
52 struct pci_dev *pdev = to_pci_dev(dev);
53 return sprintf (buf, "%u\n", pdev->broken_parity_status);
56 static ssize_t broken_parity_status_store(struct device *dev,
57 struct device_attribute *attr,
58 const char *buf, size_t count)
60 struct pci_dev *pdev = to_pci_dev(dev);
63 if (strict_strtoul(buf, 0, &val) < 0)
66 pdev->broken_parity_status = !!val;
71 static ssize_t local_cpus_show(struct device *dev,
72 struct device_attribute *attr, char *buf)
74 const struct cpumask *mask;
77 mask = cpumask_of_pcibus(to_pci_dev(dev)->bus);
78 len = cpumask_scnprintf(buf, PAGE_SIZE-2, mask);
85 static ssize_t local_cpulist_show(struct device *dev,
86 struct device_attribute *attr, char *buf)
88 const struct cpumask *mask;
91 mask = cpumask_of_pcibus(to_pci_dev(dev)->bus);
92 len = cpulist_scnprintf(buf, PAGE_SIZE-2, mask);
100 resource_show(struct device * dev, struct device_attribute *attr, char * buf)
102 struct pci_dev * pci_dev = to_pci_dev(dev);
106 resource_size_t start, end;
108 if (pci_dev->subordinate)
109 max = DEVICE_COUNT_RESOURCE;
111 max = PCI_BRIDGE_RESOURCES;
113 for (i = 0; i < max; i++) {
114 struct resource *res = &pci_dev->resource[i];
115 pci_resource_to_user(pci_dev, i, res, &start, &end);
116 str += sprintf(str,"0x%016llx 0x%016llx 0x%016llx\n",
117 (unsigned long long)start,
118 (unsigned long long)end,
119 (unsigned long long)res->flags);
124 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
126 struct pci_dev *pci_dev = to_pci_dev(dev);
128 return sprintf(buf, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02x\n",
129 pci_dev->vendor, pci_dev->device,
130 pci_dev->subsystem_vendor, pci_dev->subsystem_device,
131 (u8)(pci_dev->class >> 16), (u8)(pci_dev->class >> 8),
132 (u8)(pci_dev->class));
135 static ssize_t is_enabled_store(struct device *dev,
136 struct device_attribute *attr, const char *buf,
139 struct pci_dev *pdev = to_pci_dev(dev);
141 ssize_t result = strict_strtoul(buf, 0, &val);
146 /* this can crash the machine when done on the "wrong" device */
147 if (!capable(CAP_SYS_ADMIN))
151 if (pci_is_enabled(pdev))
152 pci_disable_device(pdev);
156 result = pci_enable_device(pdev);
158 return result < 0 ? result : count;
161 static ssize_t is_enabled_show(struct device *dev,
162 struct device_attribute *attr, char *buf)
164 struct pci_dev *pdev;
166 pdev = to_pci_dev (dev);
167 return sprintf (buf, "%u\n", atomic_read(&pdev->enable_cnt));
172 numa_node_show(struct device *dev, struct device_attribute *attr, char *buf)
174 return sprintf (buf, "%d\n", dev->numa_node);
179 msi_bus_show(struct device *dev, struct device_attribute *attr, char *buf)
181 struct pci_dev *pdev = to_pci_dev(dev);
183 if (!pdev->subordinate)
186 return sprintf (buf, "%u\n",
187 !(pdev->subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI));
191 msi_bus_store(struct device *dev, struct device_attribute *attr,
192 const char *buf, size_t count)
194 struct pci_dev *pdev = to_pci_dev(dev);
197 if (strict_strtoul(buf, 0, &val) < 0)
200 /* bad things may happen if the no_msi flag is changed
201 * while some drivers are loaded */
202 if (!capable(CAP_SYS_ADMIN))
205 /* Maybe pci devices without subordinate busses shouldn't even have this
206 * attribute in the first place? */
207 if (!pdev->subordinate)
210 /* Is the flag going to change, or keep the value it already had? */
211 if (!(pdev->subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI) ^
213 pdev->subordinate->bus_flags ^= PCI_BUS_FLAGS_NO_MSI;
215 dev_warn(&pdev->dev, "forced subordinate bus to%s support MSI,"
216 " bad things could happen\n", val ? "" : " not");
222 #ifdef CONFIG_HOTPLUG
223 static DEFINE_MUTEX(pci_remove_rescan_mutex);
224 static ssize_t bus_rescan_store(struct bus_type *bus, const char *buf,
228 struct pci_bus *b = NULL;
230 if (strict_strtoul(buf, 0, &val) < 0)
234 mutex_lock(&pci_remove_rescan_mutex);
235 while ((b = pci_find_next_bus(b)) != NULL)
237 mutex_unlock(&pci_remove_rescan_mutex);
242 struct bus_attribute pci_bus_attrs[] = {
243 __ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, bus_rescan_store),
248 dev_rescan_store(struct device *dev, struct device_attribute *attr,
249 const char *buf, size_t count)
252 struct pci_dev *pdev = to_pci_dev(dev);
254 if (strict_strtoul(buf, 0, &val) < 0)
258 mutex_lock(&pci_remove_rescan_mutex);
259 pci_rescan_bus(pdev->bus);
260 mutex_unlock(&pci_remove_rescan_mutex);
265 static void remove_callback(struct device *dev)
267 struct pci_dev *pdev = to_pci_dev(dev);
269 mutex_lock(&pci_remove_rescan_mutex);
270 pci_remove_bus_device(pdev);
271 mutex_unlock(&pci_remove_rescan_mutex);
275 remove_store(struct device *dev, struct device_attribute *dummy,
276 const char *buf, size_t count)
281 if (strict_strtoul(buf, 0, &val) < 0)
284 /* An attribute cannot be unregistered by one of its own methods,
285 * so we have to use this roundabout approach.
288 ret = device_schedule_callback(dev, remove_callback);
295 struct device_attribute pci_dev_attrs[] = {
299 __ATTR_RO(subsystem_vendor),
300 __ATTR_RO(subsystem_device),
303 __ATTR_RO(local_cpus),
304 __ATTR_RO(local_cpulist),
307 __ATTR_RO(numa_node),
309 __ATTR(enable, 0600, is_enabled_show, is_enabled_store),
310 __ATTR(broken_parity_status,(S_IRUGO|S_IWUSR),
311 broken_parity_status_show,broken_parity_status_store),
312 __ATTR(msi_bus, 0644, msi_bus_show, msi_bus_store),
313 #ifdef CONFIG_HOTPLUG
314 __ATTR(remove, (S_IWUSR|S_IWGRP), NULL, remove_store),
315 __ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, dev_rescan_store),
321 boot_vga_show(struct device *dev, struct device_attribute *attr, char *buf)
323 struct pci_dev *pdev = to_pci_dev(dev);
325 return sprintf(buf, "%u\n",
326 !!(pdev->resource[PCI_ROM_RESOURCE].flags &
327 IORESOURCE_ROM_SHADOW));
329 struct device_attribute vga_attr = __ATTR_RO(boot_vga);
332 pci_read_config(struct kobject *kobj, struct bin_attribute *bin_attr,
333 char *buf, loff_t off, size_t count)
335 struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
336 unsigned int size = 64;
337 loff_t init_off = off;
338 u8 *data = (u8*) buf;
340 /* Several chips lock up trying to read undefined config space */
341 if (capable(CAP_SYS_ADMIN)) {
342 size = dev->cfg_size;
343 } else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
349 if (off + count > size) {
356 if ((off & 1) && size) {
358 pci_user_read_config_byte(dev, off, &val);
359 data[off - init_off] = val;
364 if ((off & 3) && size > 2) {
366 pci_user_read_config_word(dev, off, &val);
367 data[off - init_off] = val & 0xff;
368 data[off - init_off + 1] = (val >> 8) & 0xff;
375 pci_user_read_config_dword(dev, off, &val);
376 data[off - init_off] = val & 0xff;
377 data[off - init_off + 1] = (val >> 8) & 0xff;
378 data[off - init_off + 2] = (val >> 16) & 0xff;
379 data[off - init_off + 3] = (val >> 24) & 0xff;
386 pci_user_read_config_word(dev, off, &val);
387 data[off - init_off] = val & 0xff;
388 data[off - init_off + 1] = (val >> 8) & 0xff;
395 pci_user_read_config_byte(dev, off, &val);
396 data[off - init_off] = val;
405 pci_write_config(struct kobject *kobj, struct bin_attribute *bin_attr,
406 char *buf, loff_t off, size_t count)
408 struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
409 unsigned int size = count;
410 loff_t init_off = off;
411 u8 *data = (u8*) buf;
413 if (off > dev->cfg_size)
415 if (off + count > dev->cfg_size) {
416 size = dev->cfg_size - off;
420 if ((off & 1) && size) {
421 pci_user_write_config_byte(dev, off, data[off - init_off]);
426 if ((off & 3) && size > 2) {
427 u16 val = data[off - init_off];
428 val |= (u16) data[off - init_off + 1] << 8;
429 pci_user_write_config_word(dev, off, val);
435 u32 val = data[off - init_off];
436 val |= (u32) data[off - init_off + 1] << 8;
437 val |= (u32) data[off - init_off + 2] << 16;
438 val |= (u32) data[off - init_off + 3] << 24;
439 pci_user_write_config_dword(dev, off, val);
445 u16 val = data[off - init_off];
446 val |= (u16) data[off - init_off + 1] << 8;
447 pci_user_write_config_word(dev, off, val);
453 pci_user_write_config_byte(dev, off, data[off - init_off]);
462 read_vpd_attr(struct kobject *kobj, struct bin_attribute *bin_attr,
463 char *buf, loff_t off, size_t count)
465 struct pci_dev *dev =
466 to_pci_dev(container_of(kobj, struct device, kobj));
468 if (off > bin_attr->size)
470 else if (count > bin_attr->size - off)
471 count = bin_attr->size - off;
473 return pci_read_vpd(dev, off, count, buf);
477 write_vpd_attr(struct kobject *kobj, struct bin_attribute *bin_attr,
478 char *buf, loff_t off, size_t count)
480 struct pci_dev *dev =
481 to_pci_dev(container_of(kobj, struct device, kobj));
483 if (off > bin_attr->size)
485 else if (count > bin_attr->size - off)
486 count = bin_attr->size - off;
488 return pci_write_vpd(dev, off, count, buf);
491 #ifdef HAVE_PCI_LEGACY
493 * pci_read_legacy_io - read byte(s) from legacy I/O port space
494 * @kobj: kobject corresponding to file to read from
495 * @bin_attr: struct bin_attribute for this file
496 * @buf: buffer to store results
497 * @off: offset into legacy I/O port space
498 * @count: number of bytes to read
500 * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
501 * callback routine (pci_legacy_read).
504 pci_read_legacy_io(struct kobject *kobj, struct bin_attribute *bin_attr,
505 char *buf, loff_t off, size_t count)
507 struct pci_bus *bus = to_pci_bus(container_of(kobj,
511 /* Only support 1, 2 or 4 byte accesses */
512 if (count != 1 && count != 2 && count != 4)
515 return pci_legacy_read(bus, off, (u32 *)buf, count);
519 * pci_write_legacy_io - write byte(s) to legacy I/O port space
520 * @kobj: kobject corresponding to file to read from
521 * @bin_attr: struct bin_attribute for this file
522 * @buf: buffer containing value to be written
523 * @off: offset into legacy I/O port space
524 * @count: number of bytes to write
526 * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
527 * callback routine (pci_legacy_write).
530 pci_write_legacy_io(struct kobject *kobj, struct bin_attribute *bin_attr,
531 char *buf, loff_t off, size_t count)
533 struct pci_bus *bus = to_pci_bus(container_of(kobj,
536 /* Only support 1, 2 or 4 byte accesses */
537 if (count != 1 && count != 2 && count != 4)
540 return pci_legacy_write(bus, off, *(u32 *)buf, count);
544 * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
545 * @kobj: kobject corresponding to device to be mapped
546 * @attr: struct bin_attribute for this file
547 * @vma: struct vm_area_struct passed to mmap
549 * Uses an arch specific callback, pci_mmap_legacy_mem_page_range, to mmap
550 * legacy memory space (first meg of bus space) into application virtual
554 pci_mmap_legacy_mem(struct kobject *kobj, struct bin_attribute *attr,
555 struct vm_area_struct *vma)
557 struct pci_bus *bus = to_pci_bus(container_of(kobj,
561 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_mem);
565 * pci_mmap_legacy_io - map legacy PCI IO into user memory space
566 * @kobj: kobject corresponding to device to be mapped
567 * @attr: struct bin_attribute for this file
568 * @vma: struct vm_area_struct passed to mmap
570 * Uses an arch specific callback, pci_mmap_legacy_io_page_range, to mmap
571 * legacy IO space (first meg of bus space) into application virtual
572 * memory space. Returns -ENOSYS if the operation isn't supported
575 pci_mmap_legacy_io(struct kobject *kobj, struct bin_attribute *attr,
576 struct vm_area_struct *vma)
578 struct pci_bus *bus = to_pci_bus(container_of(kobj,
582 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_io);
586 * pci_adjust_legacy_attr - adjustment of legacy file attributes
587 * @b: bus to create files under
588 * @mmap_type: I/O port or memory
590 * Stub implementation. Can be overridden by arch if necessary.
593 pci_adjust_legacy_attr(struct pci_bus *b, enum pci_mmap_state mmap_type)
599 * pci_create_legacy_files - create legacy I/O port and memory files
600 * @b: bus to create files under
602 * Some platforms allow access to legacy I/O port and ISA memory space on
603 * a per-bus basis. This routine creates the files and ties them into
604 * their associated read, write and mmap files from pci-sysfs.c
606 * On error unwind, but don't propogate the error to the caller
607 * as it is ok to set up the PCI bus without these files.
609 void pci_create_legacy_files(struct pci_bus *b)
613 b->legacy_io = kzalloc(sizeof(struct bin_attribute) * 2,
618 b->legacy_io->attr.name = "legacy_io";
619 b->legacy_io->size = 0xffff;
620 b->legacy_io->attr.mode = S_IRUSR | S_IWUSR;
621 b->legacy_io->read = pci_read_legacy_io;
622 b->legacy_io->write = pci_write_legacy_io;
623 b->legacy_io->mmap = pci_mmap_legacy_io;
624 pci_adjust_legacy_attr(b, pci_mmap_io);
625 error = device_create_bin_file(&b->dev, b->legacy_io);
629 /* Allocated above after the legacy_io struct */
630 b->legacy_mem = b->legacy_io + 1;
631 b->legacy_mem->attr.name = "legacy_mem";
632 b->legacy_mem->size = 1024*1024;
633 b->legacy_mem->attr.mode = S_IRUSR | S_IWUSR;
634 b->legacy_mem->mmap = pci_mmap_legacy_mem;
635 pci_adjust_legacy_attr(b, pci_mmap_mem);
636 error = device_create_bin_file(&b->dev, b->legacy_mem);
643 device_remove_bin_file(&b->dev, b->legacy_io);
648 printk(KERN_WARNING "pci: warning: could not create legacy I/O port "
649 "and ISA memory resources to sysfs\n");
653 void pci_remove_legacy_files(struct pci_bus *b)
656 device_remove_bin_file(&b->dev, b->legacy_io);
657 device_remove_bin_file(&b->dev, b->legacy_mem);
658 kfree(b->legacy_io); /* both are allocated here */
661 #endif /* HAVE_PCI_LEGACY */
665 int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vma,
666 enum pci_mmap_api mmap_api)
668 unsigned long nr, start, size, pci_start;
670 if (pci_resource_len(pdev, resno) == 0)
672 nr = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
673 start = vma->vm_pgoff;
674 size = ((pci_resource_len(pdev, resno) - 1) >> PAGE_SHIFT) + 1;
675 pci_start = (mmap_api == PCI_MMAP_PROCFS) ?
676 pci_resource_start(pdev, resno) >> PAGE_SHIFT : 0;
677 if (start >= pci_start && start < pci_start + size &&
678 start + nr <= pci_start + size)
684 * pci_mmap_resource - map a PCI resource into user memory space
685 * @kobj: kobject for mapping
686 * @attr: struct bin_attribute for the file being mapped
687 * @vma: struct vm_area_struct passed into the mmap
688 * @write_combine: 1 for write_combine mapping
690 * Use the regular PCI mapping routines to map a PCI resource into userspace.
693 pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr,
694 struct vm_area_struct *vma, int write_combine)
696 struct pci_dev *pdev = to_pci_dev(container_of(kobj,
697 struct device, kobj));
698 struct resource *res = (struct resource *)attr->private;
699 enum pci_mmap_state mmap_type;
700 resource_size_t start, end;
703 for (i = 0; i < PCI_ROM_RESOURCE; i++)
704 if (res == &pdev->resource[i])
706 if (i >= PCI_ROM_RESOURCE)
709 if (!pci_mmap_fits(pdev, i, vma, PCI_MMAP_SYSFS)) {
710 WARN(1, "process \"%s\" tried to map 0x%08lx bytes "
711 "at page 0x%08lx on %s BAR %d (start 0x%16Lx, size 0x%16Lx)\n",
712 current->comm, vma->vm_end-vma->vm_start, vma->vm_pgoff,
714 pci_resource_start(pdev, i), pci_resource_len(pdev, i));
718 /* pci_mmap_page_range() expects the same kind of entry as coming
719 * from /proc/bus/pci/ which is a "user visible" value. If this is
720 * different from the resource itself, arch will do necessary fixup.
722 pci_resource_to_user(pdev, i, res, &start, &end);
723 vma->vm_pgoff += start >> PAGE_SHIFT;
724 mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io;
726 if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(start))
729 return pci_mmap_page_range(pdev, vma, mmap_type, write_combine);
733 pci_mmap_resource_uc(struct kobject *kobj, struct bin_attribute *attr,
734 struct vm_area_struct *vma)
736 return pci_mmap_resource(kobj, attr, vma, 0);
740 pci_mmap_resource_wc(struct kobject *kobj, struct bin_attribute *attr,
741 struct vm_area_struct *vma)
743 return pci_mmap_resource(kobj, attr, vma, 1);
747 * pci_remove_resource_files - cleanup resource files
748 * @pdev: dev to cleanup
750 * If we created resource files for @pdev, remove them from sysfs and
751 * free their resources.
754 pci_remove_resource_files(struct pci_dev *pdev)
758 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
759 struct bin_attribute *res_attr;
761 res_attr = pdev->res_attr[i];
763 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
767 res_attr = pdev->res_attr_wc[i];
769 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
775 static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine)
777 /* allocate attribute structure, piggyback attribute name */
778 int name_len = write_combine ? 13 : 10;
779 struct bin_attribute *res_attr;
782 res_attr = kzalloc(sizeof(*res_attr) + name_len, GFP_ATOMIC);
784 char *res_attr_name = (char *)(res_attr + 1);
787 pdev->res_attr_wc[num] = res_attr;
788 sprintf(res_attr_name, "resource%d_wc", num);
789 res_attr->mmap = pci_mmap_resource_wc;
791 pdev->res_attr[num] = res_attr;
792 sprintf(res_attr_name, "resource%d", num);
793 res_attr->mmap = pci_mmap_resource_uc;
795 res_attr->attr.name = res_attr_name;
796 res_attr->attr.mode = S_IRUSR | S_IWUSR;
797 res_attr->size = pci_resource_len(pdev, num);
798 res_attr->private = &pdev->resource[num];
799 retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
807 * pci_create_resource_files - create resource files in sysfs for @dev
808 * @pdev: dev in question
810 * Walk the resources in @pdev creating files for each resource available.
812 static int pci_create_resource_files(struct pci_dev *pdev)
817 /* Expose the PCI resources from this device as files */
818 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
820 /* skip empty resources */
821 if (!pci_resource_len(pdev, i))
824 retval = pci_create_attr(pdev, i, 0);
825 /* for prefetchable resources, create a WC mappable file */
826 if (!retval && pdev->resource[i].flags & IORESOURCE_PREFETCH)
827 retval = pci_create_attr(pdev, i, 1);
830 pci_remove_resource_files(pdev);
836 #else /* !HAVE_PCI_MMAP */
837 int __weak pci_create_resource_files(struct pci_dev *dev) { return 0; }
838 void __weak pci_remove_resource_files(struct pci_dev *dev) { return; }
839 #endif /* HAVE_PCI_MMAP */
842 * pci_write_rom - used to enable access to the PCI ROM display
843 * @kobj: kernel object handle
844 * @bin_attr: struct bin_attribute for this file
847 * @count: number of byte in input
849 * writing anything except 0 enables it
852 pci_write_rom(struct kobject *kobj, struct bin_attribute *bin_attr,
853 char *buf, loff_t off, size_t count)
855 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
857 if ((off == 0) && (*buf == '0') && (count == 2))
858 pdev->rom_attr_enabled = 0;
860 pdev->rom_attr_enabled = 1;
866 * pci_read_rom - read a PCI ROM
867 * @kobj: kernel object handle
868 * @bin_attr: struct bin_attribute for this file
869 * @buf: where to put the data we read from the ROM
871 * @count: number of bytes to read
873 * Put @count bytes starting at @off into @buf from the ROM in the PCI
874 * device corresponding to @kobj.
877 pci_read_rom(struct kobject *kobj, struct bin_attribute *bin_attr,
878 char *buf, loff_t off, size_t count)
880 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
884 if (!pdev->rom_attr_enabled)
887 rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */
894 if (off + count > size)
897 memcpy_fromio(buf, rom + off, count);
899 pci_unmap_rom(pdev, rom);
904 static struct bin_attribute pci_config_attr = {
907 .mode = S_IRUGO | S_IWUSR,
909 .size = PCI_CFG_SPACE_SIZE,
910 .read = pci_read_config,
911 .write = pci_write_config,
914 static struct bin_attribute pcie_config_attr = {
917 .mode = S_IRUGO | S_IWUSR,
919 .size = PCI_CFG_SPACE_EXP_SIZE,
920 .read = pci_read_config,
921 .write = pci_write_config,
924 int __attribute__ ((weak)) pcibios_add_platform_entries(struct pci_dev *dev)
929 static ssize_t reset_store(struct device *dev,
930 struct device_attribute *attr, const char *buf,
933 struct pci_dev *pdev = to_pci_dev(dev);
935 ssize_t result = strict_strtoul(buf, 0, &val);
942 return pci_reset_function(pdev);
945 static struct device_attribute reset_attr = __ATTR(reset, 0200, NULL, reset_store);
947 static int pci_create_capabilities_sysfs(struct pci_dev *dev)
950 struct bin_attribute *attr;
952 /* If the device has VPD, try to expose it in sysfs. */
954 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
958 attr->size = dev->vpd->len;
959 attr->attr.name = "vpd";
960 attr->attr.mode = S_IRUSR | S_IWUSR;
961 attr->read = read_vpd_attr;
962 attr->write = write_vpd_attr;
963 retval = sysfs_create_bin_file(&dev->dev.kobj, attr);
965 kfree(dev->vpd->attr);
968 dev->vpd->attr = attr;
971 /* Active State Power Management */
972 pcie_aspm_create_sysfs_dev_files(dev);
974 if (!pci_probe_reset_function(dev)) {
975 retval = device_create_file(&dev->dev, &reset_attr);
983 pcie_aspm_remove_sysfs_dev_files(dev);
984 if (dev->vpd && dev->vpd->attr) {
985 sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
986 kfree(dev->vpd->attr);
992 int __must_check pci_create_sysfs_dev_files (struct pci_dev *pdev)
996 struct bin_attribute *attr;
998 if (!sysfs_initialized)
1001 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
1002 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr);
1004 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1008 retval = pci_create_resource_files(pdev);
1010 goto err_config_file;
1012 if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
1013 rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
1014 else if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
1017 /* If the device has a ROM, try to expose it in sysfs. */
1019 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
1022 goto err_resource_files;
1024 attr->size = rom_size;
1025 attr->attr.name = "rom";
1026 attr->attr.mode = S_IRUSR;
1027 attr->read = pci_read_rom;
1028 attr->write = pci_write_rom;
1029 retval = sysfs_create_bin_file(&pdev->dev.kobj, attr);
1032 goto err_resource_files;
1034 pdev->rom_attr = attr;
1037 if ((pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA) {
1038 retval = device_create_file(&pdev->dev, &vga_attr);
1043 /* add platform-specific attributes */
1044 retval = pcibios_add_platform_entries(pdev);
1048 /* add sysfs entries for various capabilities */
1049 retval = pci_create_capabilities_sysfs(pdev);
1056 if ((pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
1057 device_remove_file(&pdev->dev, &vga_attr);
1060 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
1061 kfree(pdev->rom_attr);
1062 pdev->rom_attr = NULL;
1065 pci_remove_resource_files(pdev);
1067 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
1068 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
1070 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1075 static void pci_remove_capabilities_sysfs(struct pci_dev *dev)
1077 if (dev->vpd && dev->vpd->attr) {
1078 sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
1079 kfree(dev->vpd->attr);
1082 pcie_aspm_remove_sysfs_dev_files(dev);
1083 if (dev->reset_fn) {
1084 device_remove_file(&dev->dev, &reset_attr);
1090 * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
1091 * @pdev: device whose entries we should free
1093 * Cleanup when @pdev is removed from sysfs.
1095 void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
1099 if (!sysfs_initialized)
1102 pci_remove_capabilities_sysfs(pdev);
1104 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
1105 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
1107 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1109 pci_remove_resource_files(pdev);
1111 if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
1112 rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
1113 else if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
1116 if (rom_size && pdev->rom_attr) {
1117 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
1118 kfree(pdev->rom_attr);
1122 static int __init pci_sysfs_init(void)
1124 struct pci_dev *pdev = NULL;
1127 sysfs_initialized = 1;
1128 for_each_pci_dev(pdev) {
1129 retval = pci_create_sysfs_dev_files(pdev);
1139 late_initcall(pci_sysfs_init);