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;
78 mask = cpumask_of_node(dev_to_node(dev));
80 mask = cpumask_of_pcibus(to_pci_dev(dev)->bus);
82 len = cpumask_scnprintf(buf, PAGE_SIZE-2, mask);
89 static ssize_t local_cpulist_show(struct device *dev,
90 struct device_attribute *attr, char *buf)
92 const struct cpumask *mask;
96 mask = cpumask_of_node(dev_to_node(dev));
98 mask = cpumask_of_pcibus(to_pci_dev(dev)->bus);
100 len = cpulist_scnprintf(buf, PAGE_SIZE-2, mask);
108 resource_show(struct device * dev, struct device_attribute *attr, char * buf)
110 struct pci_dev * pci_dev = to_pci_dev(dev);
114 resource_size_t start, end;
116 if (pci_dev->subordinate)
117 max = DEVICE_COUNT_RESOURCE;
119 max = PCI_BRIDGE_RESOURCES;
121 for (i = 0; i < max; i++) {
122 struct resource *res = &pci_dev->resource[i];
123 pci_resource_to_user(pci_dev, i, res, &start, &end);
124 str += sprintf(str,"0x%016llx 0x%016llx 0x%016llx\n",
125 (unsigned long long)start,
126 (unsigned long long)end,
127 (unsigned long long)res->flags);
132 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
134 struct pci_dev *pci_dev = to_pci_dev(dev);
136 return sprintf(buf, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02x\n",
137 pci_dev->vendor, pci_dev->device,
138 pci_dev->subsystem_vendor, pci_dev->subsystem_device,
139 (u8)(pci_dev->class >> 16), (u8)(pci_dev->class >> 8),
140 (u8)(pci_dev->class));
143 static ssize_t is_enabled_store(struct device *dev,
144 struct device_attribute *attr, const char *buf,
147 struct pci_dev *pdev = to_pci_dev(dev);
149 ssize_t result = strict_strtoul(buf, 0, &val);
154 /* this can crash the machine when done on the "wrong" device */
155 if (!capable(CAP_SYS_ADMIN))
159 if (pci_is_enabled(pdev))
160 pci_disable_device(pdev);
164 result = pci_enable_device(pdev);
166 return result < 0 ? result : count;
169 static ssize_t is_enabled_show(struct device *dev,
170 struct device_attribute *attr, char *buf)
172 struct pci_dev *pdev;
174 pdev = to_pci_dev (dev);
175 return sprintf (buf, "%u\n", atomic_read(&pdev->enable_cnt));
180 numa_node_show(struct device *dev, struct device_attribute *attr, char *buf)
182 return sprintf (buf, "%d\n", dev->numa_node);
187 msi_bus_show(struct device *dev, struct device_attribute *attr, char *buf)
189 struct pci_dev *pdev = to_pci_dev(dev);
191 if (!pdev->subordinate)
194 return sprintf (buf, "%u\n",
195 !(pdev->subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI));
199 msi_bus_store(struct device *dev, struct device_attribute *attr,
200 const char *buf, size_t count)
202 struct pci_dev *pdev = to_pci_dev(dev);
205 if (strict_strtoul(buf, 0, &val) < 0)
208 /* bad things may happen if the no_msi flag is changed
209 * while some drivers are loaded */
210 if (!capable(CAP_SYS_ADMIN))
213 /* Maybe pci devices without subordinate busses shouldn't even have this
214 * attribute in the first place? */
215 if (!pdev->subordinate)
218 /* Is the flag going to change, or keep the value it already had? */
219 if (!(pdev->subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI) ^
221 pdev->subordinate->bus_flags ^= PCI_BUS_FLAGS_NO_MSI;
223 dev_warn(&pdev->dev, "forced subordinate bus to%s support MSI,"
224 " bad things could happen\n", val ? "" : " not");
230 #ifdef CONFIG_HOTPLUG
231 static DEFINE_MUTEX(pci_remove_rescan_mutex);
232 static ssize_t bus_rescan_store(struct bus_type *bus, const char *buf,
236 struct pci_bus *b = NULL;
238 if (strict_strtoul(buf, 0, &val) < 0)
242 mutex_lock(&pci_remove_rescan_mutex);
243 while ((b = pci_find_next_bus(b)) != NULL)
245 mutex_unlock(&pci_remove_rescan_mutex);
250 struct bus_attribute pci_bus_attrs[] = {
251 __ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, bus_rescan_store),
256 dev_rescan_store(struct device *dev, struct device_attribute *attr,
257 const char *buf, size_t count)
260 struct pci_dev *pdev = to_pci_dev(dev);
262 if (strict_strtoul(buf, 0, &val) < 0)
266 mutex_lock(&pci_remove_rescan_mutex);
267 pci_rescan_bus(pdev->bus);
268 mutex_unlock(&pci_remove_rescan_mutex);
273 static void remove_callback(struct device *dev)
275 struct pci_dev *pdev = to_pci_dev(dev);
277 mutex_lock(&pci_remove_rescan_mutex);
278 pci_remove_bus_device(pdev);
279 mutex_unlock(&pci_remove_rescan_mutex);
283 remove_store(struct device *dev, struct device_attribute *dummy,
284 const char *buf, size_t count)
289 if (strict_strtoul(buf, 0, &val) < 0)
292 /* An attribute cannot be unregistered by one of its own methods,
293 * so we have to use this roundabout approach.
296 ret = device_schedule_callback(dev, remove_callback);
303 struct device_attribute pci_dev_attrs[] = {
307 __ATTR_RO(subsystem_vendor),
308 __ATTR_RO(subsystem_device),
311 __ATTR_RO(local_cpus),
312 __ATTR_RO(local_cpulist),
315 __ATTR_RO(numa_node),
317 __ATTR(enable, 0600, is_enabled_show, is_enabled_store),
318 __ATTR(broken_parity_status,(S_IRUGO|S_IWUSR),
319 broken_parity_status_show,broken_parity_status_store),
320 __ATTR(msi_bus, 0644, msi_bus_show, msi_bus_store),
321 #ifdef CONFIG_HOTPLUG
322 __ATTR(remove, (S_IWUSR|S_IWGRP), NULL, remove_store),
323 __ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, dev_rescan_store),
329 boot_vga_show(struct device *dev, struct device_attribute *attr, char *buf)
331 struct pci_dev *pdev = to_pci_dev(dev);
333 return sprintf(buf, "%u\n",
334 !!(pdev->resource[PCI_ROM_RESOURCE].flags &
335 IORESOURCE_ROM_SHADOW));
337 struct device_attribute vga_attr = __ATTR_RO(boot_vga);
340 pci_read_config(struct kobject *kobj, struct bin_attribute *bin_attr,
341 char *buf, loff_t off, size_t count)
343 struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
344 unsigned int size = 64;
345 loff_t init_off = off;
346 u8 *data = (u8*) buf;
348 /* Several chips lock up trying to read undefined config space */
349 if (capable(CAP_SYS_ADMIN)) {
350 size = dev->cfg_size;
351 } else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
357 if (off + count > size) {
364 if ((off & 1) && size) {
366 pci_user_read_config_byte(dev, off, &val);
367 data[off - init_off] = val;
372 if ((off & 3) && size > 2) {
374 pci_user_read_config_word(dev, off, &val);
375 data[off - init_off] = val & 0xff;
376 data[off - init_off + 1] = (val >> 8) & 0xff;
383 pci_user_read_config_dword(dev, off, &val);
384 data[off - init_off] = val & 0xff;
385 data[off - init_off + 1] = (val >> 8) & 0xff;
386 data[off - init_off + 2] = (val >> 16) & 0xff;
387 data[off - init_off + 3] = (val >> 24) & 0xff;
394 pci_user_read_config_word(dev, off, &val);
395 data[off - init_off] = val & 0xff;
396 data[off - init_off + 1] = (val >> 8) & 0xff;
403 pci_user_read_config_byte(dev, off, &val);
404 data[off - init_off] = val;
413 pci_write_config(struct kobject *kobj, struct bin_attribute *bin_attr,
414 char *buf, loff_t off, size_t count)
416 struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
417 unsigned int size = count;
418 loff_t init_off = off;
419 u8 *data = (u8*) buf;
421 if (off > dev->cfg_size)
423 if (off + count > dev->cfg_size) {
424 size = dev->cfg_size - off;
428 if ((off & 1) && size) {
429 pci_user_write_config_byte(dev, off, data[off - init_off]);
434 if ((off & 3) && size > 2) {
435 u16 val = data[off - init_off];
436 val |= (u16) data[off - init_off + 1] << 8;
437 pci_user_write_config_word(dev, off, val);
443 u32 val = data[off - init_off];
444 val |= (u32) data[off - init_off + 1] << 8;
445 val |= (u32) data[off - init_off + 2] << 16;
446 val |= (u32) data[off - init_off + 3] << 24;
447 pci_user_write_config_dword(dev, off, val);
453 u16 val = data[off - init_off];
454 val |= (u16) data[off - init_off + 1] << 8;
455 pci_user_write_config_word(dev, off, val);
461 pci_user_write_config_byte(dev, off, data[off - init_off]);
470 read_vpd_attr(struct kobject *kobj, struct bin_attribute *bin_attr,
471 char *buf, loff_t off, size_t count)
473 struct pci_dev *dev =
474 to_pci_dev(container_of(kobj, struct device, kobj));
476 if (off > bin_attr->size)
478 else if (count > bin_attr->size - off)
479 count = bin_attr->size - off;
481 return pci_read_vpd(dev, off, count, buf);
485 write_vpd_attr(struct kobject *kobj, struct bin_attribute *bin_attr,
486 char *buf, loff_t off, size_t count)
488 struct pci_dev *dev =
489 to_pci_dev(container_of(kobj, struct device, kobj));
491 if (off > bin_attr->size)
493 else if (count > bin_attr->size - off)
494 count = bin_attr->size - off;
496 return pci_write_vpd(dev, off, count, buf);
499 #ifdef HAVE_PCI_LEGACY
501 * pci_read_legacy_io - read byte(s) from legacy I/O port space
502 * @kobj: kobject corresponding to file to read from
503 * @bin_attr: struct bin_attribute for this file
504 * @buf: buffer to store results
505 * @off: offset into legacy I/O port space
506 * @count: number of bytes to read
508 * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
509 * callback routine (pci_legacy_read).
512 pci_read_legacy_io(struct kobject *kobj, struct bin_attribute *bin_attr,
513 char *buf, loff_t off, size_t count)
515 struct pci_bus *bus = to_pci_bus(container_of(kobj,
519 /* Only support 1, 2 or 4 byte accesses */
520 if (count != 1 && count != 2 && count != 4)
523 return pci_legacy_read(bus, off, (u32 *)buf, count);
527 * pci_write_legacy_io - write byte(s) to legacy I/O port space
528 * @kobj: kobject corresponding to file to read from
529 * @bin_attr: struct bin_attribute for this file
530 * @buf: buffer containing value to be written
531 * @off: offset into legacy I/O port space
532 * @count: number of bytes to write
534 * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
535 * callback routine (pci_legacy_write).
538 pci_write_legacy_io(struct kobject *kobj, struct bin_attribute *bin_attr,
539 char *buf, loff_t off, size_t count)
541 struct pci_bus *bus = to_pci_bus(container_of(kobj,
544 /* Only support 1, 2 or 4 byte accesses */
545 if (count != 1 && count != 2 && count != 4)
548 return pci_legacy_write(bus, off, *(u32 *)buf, count);
552 * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
553 * @kobj: kobject corresponding to device to be mapped
554 * @attr: struct bin_attribute for this file
555 * @vma: struct vm_area_struct passed to mmap
557 * Uses an arch specific callback, pci_mmap_legacy_mem_page_range, to mmap
558 * legacy memory space (first meg of bus space) into application virtual
562 pci_mmap_legacy_mem(struct kobject *kobj, struct bin_attribute *attr,
563 struct vm_area_struct *vma)
565 struct pci_bus *bus = to_pci_bus(container_of(kobj,
569 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_mem);
573 * pci_mmap_legacy_io - map legacy PCI IO into user memory space
574 * @kobj: kobject corresponding to device to be mapped
575 * @attr: struct bin_attribute for this file
576 * @vma: struct vm_area_struct passed to mmap
578 * Uses an arch specific callback, pci_mmap_legacy_io_page_range, to mmap
579 * legacy IO space (first meg of bus space) into application virtual
580 * memory space. Returns -ENOSYS if the operation isn't supported
583 pci_mmap_legacy_io(struct kobject *kobj, struct bin_attribute *attr,
584 struct vm_area_struct *vma)
586 struct pci_bus *bus = to_pci_bus(container_of(kobj,
590 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_io);
594 * pci_adjust_legacy_attr - adjustment of legacy file attributes
595 * @b: bus to create files under
596 * @mmap_type: I/O port or memory
598 * Stub implementation. Can be overridden by arch if necessary.
601 pci_adjust_legacy_attr(struct pci_bus *b, enum pci_mmap_state mmap_type)
607 * pci_create_legacy_files - create legacy I/O port and memory files
608 * @b: bus to create files under
610 * Some platforms allow access to legacy I/O port and ISA memory space on
611 * a per-bus basis. This routine creates the files and ties them into
612 * their associated read, write and mmap files from pci-sysfs.c
614 * On error unwind, but don't propogate the error to the caller
615 * as it is ok to set up the PCI bus without these files.
617 void pci_create_legacy_files(struct pci_bus *b)
621 b->legacy_io = kzalloc(sizeof(struct bin_attribute) * 2,
626 b->legacy_io->attr.name = "legacy_io";
627 b->legacy_io->size = 0xffff;
628 b->legacy_io->attr.mode = S_IRUSR | S_IWUSR;
629 b->legacy_io->read = pci_read_legacy_io;
630 b->legacy_io->write = pci_write_legacy_io;
631 b->legacy_io->mmap = pci_mmap_legacy_io;
632 pci_adjust_legacy_attr(b, pci_mmap_io);
633 error = device_create_bin_file(&b->dev, b->legacy_io);
637 /* Allocated above after the legacy_io struct */
638 b->legacy_mem = b->legacy_io + 1;
639 b->legacy_mem->attr.name = "legacy_mem";
640 b->legacy_mem->size = 1024*1024;
641 b->legacy_mem->attr.mode = S_IRUSR | S_IWUSR;
642 b->legacy_mem->mmap = pci_mmap_legacy_mem;
643 pci_adjust_legacy_attr(b, pci_mmap_mem);
644 error = device_create_bin_file(&b->dev, b->legacy_mem);
651 device_remove_bin_file(&b->dev, b->legacy_io);
656 printk(KERN_WARNING "pci: warning: could not create legacy I/O port "
657 "and ISA memory resources to sysfs\n");
661 void pci_remove_legacy_files(struct pci_bus *b)
664 device_remove_bin_file(&b->dev, b->legacy_io);
665 device_remove_bin_file(&b->dev, b->legacy_mem);
666 kfree(b->legacy_io); /* both are allocated here */
669 #endif /* HAVE_PCI_LEGACY */
673 int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vma)
675 unsigned long nr, start, size;
677 nr = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
678 start = vma->vm_pgoff;
679 size = ((pci_resource_len(pdev, resno) - 1) >> PAGE_SHIFT) + 1;
680 if (start < size && size - start >= nr)
682 WARN(1, "process \"%s\" tried to map 0x%08lx-0x%08lx on %s BAR %d (size 0x%08lx)\n",
683 current->comm, start, start+nr, pci_name(pdev), resno, size);
688 * pci_mmap_resource - map a PCI resource into user memory space
689 * @kobj: kobject for mapping
690 * @attr: struct bin_attribute for the file being mapped
691 * @vma: struct vm_area_struct passed into the mmap
692 * @write_combine: 1 for write_combine mapping
694 * Use the regular PCI mapping routines to map a PCI resource into userspace.
697 pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr,
698 struct vm_area_struct *vma, int write_combine)
700 struct pci_dev *pdev = to_pci_dev(container_of(kobj,
701 struct device, kobj));
702 struct resource *res = (struct resource *)attr->private;
703 enum pci_mmap_state mmap_type;
704 resource_size_t start, end;
707 for (i = 0; i < PCI_ROM_RESOURCE; i++)
708 if (res == &pdev->resource[i])
710 if (i >= PCI_ROM_RESOURCE)
713 if (!pci_mmap_fits(pdev, i, vma))
716 /* pci_mmap_page_range() expects the same kind of entry as coming
717 * from /proc/bus/pci/ which is a "user visible" value. If this is
718 * different from the resource itself, arch will do necessary fixup.
720 pci_resource_to_user(pdev, i, res, &start, &end);
721 vma->vm_pgoff += start >> PAGE_SHIFT;
722 mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io;
724 if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(start))
727 return pci_mmap_page_range(pdev, vma, mmap_type, write_combine);
731 pci_mmap_resource_uc(struct kobject *kobj, struct bin_attribute *attr,
732 struct vm_area_struct *vma)
734 return pci_mmap_resource(kobj, attr, vma, 0);
738 pci_mmap_resource_wc(struct kobject *kobj, struct bin_attribute *attr,
739 struct vm_area_struct *vma)
741 return pci_mmap_resource(kobj, attr, vma, 1);
745 * pci_remove_resource_files - cleanup resource files
746 * @pdev: dev to cleanup
748 * If we created resource files for @pdev, remove them from sysfs and
749 * free their resources.
752 pci_remove_resource_files(struct pci_dev *pdev)
756 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
757 struct bin_attribute *res_attr;
759 res_attr = pdev->res_attr[i];
761 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
765 res_attr = pdev->res_attr_wc[i];
767 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
773 static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine)
775 /* allocate attribute structure, piggyback attribute name */
776 int name_len = write_combine ? 13 : 10;
777 struct bin_attribute *res_attr;
780 res_attr = kzalloc(sizeof(*res_attr) + name_len, GFP_ATOMIC);
782 char *res_attr_name = (char *)(res_attr + 1);
785 pdev->res_attr_wc[num] = res_attr;
786 sprintf(res_attr_name, "resource%d_wc", num);
787 res_attr->mmap = pci_mmap_resource_wc;
789 pdev->res_attr[num] = res_attr;
790 sprintf(res_attr_name, "resource%d", num);
791 res_attr->mmap = pci_mmap_resource_uc;
793 res_attr->attr.name = res_attr_name;
794 res_attr->attr.mode = S_IRUSR | S_IWUSR;
795 res_attr->size = pci_resource_len(pdev, num);
796 res_attr->private = &pdev->resource[num];
797 retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
805 * pci_create_resource_files - create resource files in sysfs for @dev
806 * @pdev: dev in question
808 * Walk the resources in @pdev creating files for each resource available.
810 static int pci_create_resource_files(struct pci_dev *pdev)
815 /* Expose the PCI resources from this device as files */
816 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
818 /* skip empty resources */
819 if (!pci_resource_len(pdev, i))
822 retval = pci_create_attr(pdev, i, 0);
823 /* for prefetchable resources, create a WC mappable file */
824 if (!retval && pdev->resource[i].flags & IORESOURCE_PREFETCH)
825 retval = pci_create_attr(pdev, i, 1);
828 pci_remove_resource_files(pdev);
834 #else /* !HAVE_PCI_MMAP */
835 int __weak pci_create_resource_files(struct pci_dev *dev) { return 0; }
836 void __weak pci_remove_resource_files(struct pci_dev *dev) { return; }
837 #endif /* HAVE_PCI_MMAP */
840 * pci_write_rom - used to enable access to the PCI ROM display
841 * @kobj: kernel object handle
842 * @bin_attr: struct bin_attribute for this file
845 * @count: number of byte in input
847 * writing anything except 0 enables it
850 pci_write_rom(struct kobject *kobj, struct bin_attribute *bin_attr,
851 char *buf, loff_t off, size_t count)
853 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
855 if ((off == 0) && (*buf == '0') && (count == 2))
856 pdev->rom_attr_enabled = 0;
858 pdev->rom_attr_enabled = 1;
864 * pci_read_rom - read a PCI ROM
865 * @kobj: kernel object handle
866 * @bin_attr: struct bin_attribute for this file
867 * @buf: where to put the data we read from the ROM
869 * @count: number of bytes to read
871 * Put @count bytes starting at @off into @buf from the ROM in the PCI
872 * device corresponding to @kobj.
875 pci_read_rom(struct kobject *kobj, struct bin_attribute *bin_attr,
876 char *buf, loff_t off, size_t count)
878 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
882 if (!pdev->rom_attr_enabled)
885 rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */
892 if (off + count > size)
895 memcpy_fromio(buf, rom + off, count);
897 pci_unmap_rom(pdev, rom);
902 static struct bin_attribute pci_config_attr = {
905 .mode = S_IRUGO | S_IWUSR,
907 .size = PCI_CFG_SPACE_SIZE,
908 .read = pci_read_config,
909 .write = pci_write_config,
912 static struct bin_attribute pcie_config_attr = {
915 .mode = S_IRUGO | S_IWUSR,
917 .size = PCI_CFG_SPACE_EXP_SIZE,
918 .read = pci_read_config,
919 .write = pci_write_config,
922 int __attribute__ ((weak)) pcibios_add_platform_entries(struct pci_dev *dev)
927 static ssize_t reset_store(struct device *dev,
928 struct device_attribute *attr, const char *buf,
931 struct pci_dev *pdev = to_pci_dev(dev);
933 ssize_t result = strict_strtoul(buf, 0, &val);
940 return pci_reset_function(pdev);
943 static struct device_attribute reset_attr = __ATTR(reset, 0200, NULL, reset_store);
945 static int pci_create_capabilities_sysfs(struct pci_dev *dev)
948 struct bin_attribute *attr;
950 /* If the device has VPD, try to expose it in sysfs. */
952 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
956 attr->size = dev->vpd->len;
957 attr->attr.name = "vpd";
958 attr->attr.mode = S_IRUSR | S_IWUSR;
959 attr->read = read_vpd_attr;
960 attr->write = write_vpd_attr;
961 retval = sysfs_create_bin_file(&dev->dev.kobj, attr);
963 kfree(dev->vpd->attr);
966 dev->vpd->attr = attr;
969 /* Active State Power Management */
970 pcie_aspm_create_sysfs_dev_files(dev);
972 if (!pci_probe_reset_function(dev)) {
973 retval = device_create_file(&dev->dev, &reset_attr);
981 pcie_aspm_remove_sysfs_dev_files(dev);
982 if (dev->vpd && dev->vpd->attr) {
983 sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
984 kfree(dev->vpd->attr);
990 int __must_check pci_create_sysfs_dev_files (struct pci_dev *pdev)
994 struct bin_attribute *attr;
996 if (!sysfs_initialized)
999 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
1000 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr);
1002 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1006 retval = pci_create_resource_files(pdev);
1008 goto err_config_file;
1010 if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
1011 rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
1012 else if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
1015 /* If the device has a ROM, try to expose it in sysfs. */
1017 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
1020 goto err_resource_files;
1022 attr->size = rom_size;
1023 attr->attr.name = "rom";
1024 attr->attr.mode = S_IRUSR;
1025 attr->read = pci_read_rom;
1026 attr->write = pci_write_rom;
1027 retval = sysfs_create_bin_file(&pdev->dev.kobj, attr);
1030 goto err_resource_files;
1032 pdev->rom_attr = attr;
1035 if ((pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA) {
1036 retval = device_create_file(&pdev->dev, &vga_attr);
1041 /* add platform-specific attributes */
1042 retval = pcibios_add_platform_entries(pdev);
1046 /* add sysfs entries for various capabilities */
1047 retval = pci_create_capabilities_sysfs(pdev);
1054 if ((pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
1055 device_remove_file(&pdev->dev, &vga_attr);
1058 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
1059 kfree(pdev->rom_attr);
1060 pdev->rom_attr = NULL;
1063 pci_remove_resource_files(pdev);
1065 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
1066 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
1068 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1073 static void pci_remove_capabilities_sysfs(struct pci_dev *dev)
1075 if (dev->vpd && dev->vpd->attr) {
1076 sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
1077 kfree(dev->vpd->attr);
1080 pcie_aspm_remove_sysfs_dev_files(dev);
1081 if (dev->reset_fn) {
1082 device_remove_file(&dev->dev, &reset_attr);
1088 * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
1089 * @pdev: device whose entries we should free
1091 * Cleanup when @pdev is removed from sysfs.
1093 void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
1097 if (!sysfs_initialized)
1100 pci_remove_capabilities_sysfs(pdev);
1102 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
1103 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
1105 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1107 pci_remove_resource_files(pdev);
1109 if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
1110 rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
1111 else if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
1114 if (rom_size && pdev->rom_attr) {
1115 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
1116 kfree(pdev->rom_attr);
1120 static int __init pci_sysfs_init(void)
1122 struct pci_dev *pdev = NULL;
1125 sysfs_initialized = 1;
1126 for_each_pci_dev(pdev) {
1127 retval = pci_create_sysfs_dev_files(pdev);
1137 late_initcall(pci_sysfs_init);