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>
25 #include <linux/capability.h>
26 #include <linux/security.h>
27 #include <linux/pci-aspm.h>
28 #include <linux/slab.h>
31 static int sysfs_initialized; /* = 0 */
33 /* show configuration fields */
34 #define pci_config_attr(field, format_string) \
36 field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
38 struct pci_dev *pdev; \
40 pdev = to_pci_dev (dev); \
41 return sprintf (buf, format_string, pdev->field); \
44 pci_config_attr(vendor, "0x%04x\n");
45 pci_config_attr(device, "0x%04x\n");
46 pci_config_attr(subsystem_vendor, "0x%04x\n");
47 pci_config_attr(subsystem_device, "0x%04x\n");
48 pci_config_attr(class, "0x%06x\n");
49 pci_config_attr(irq, "%u\n");
51 static ssize_t broken_parity_status_show(struct device *dev,
52 struct device_attribute *attr,
55 struct pci_dev *pdev = to_pci_dev(dev);
56 return sprintf (buf, "%u\n", pdev->broken_parity_status);
59 static ssize_t broken_parity_status_store(struct device *dev,
60 struct device_attribute *attr,
61 const char *buf, size_t count)
63 struct pci_dev *pdev = to_pci_dev(dev);
66 if (strict_strtoul(buf, 0, &val) < 0)
69 pdev->broken_parity_status = !!val;
74 static ssize_t local_cpus_show(struct device *dev,
75 struct device_attribute *attr, char *buf)
77 const struct cpumask *mask;
81 mask = (dev_to_node(dev) == -1) ? cpu_online_mask :
82 cpumask_of_node(dev_to_node(dev));
84 mask = cpumask_of_pcibus(to_pci_dev(dev)->bus);
86 len = cpumask_scnprintf(buf, PAGE_SIZE-2, mask);
93 static ssize_t local_cpulist_show(struct device *dev,
94 struct device_attribute *attr, char *buf)
96 const struct cpumask *mask;
100 mask = (dev_to_node(dev) == -1) ? cpu_online_mask :
101 cpumask_of_node(dev_to_node(dev));
103 mask = cpumask_of_pcibus(to_pci_dev(dev)->bus);
105 len = cpulist_scnprintf(buf, PAGE_SIZE-2, mask);
113 resource_show(struct device * dev, struct device_attribute *attr, char * buf)
115 struct pci_dev * pci_dev = to_pci_dev(dev);
119 resource_size_t start, end;
121 if (pci_dev->subordinate)
122 max = DEVICE_COUNT_RESOURCE;
124 max = PCI_BRIDGE_RESOURCES;
126 for (i = 0; i < max; i++) {
127 struct resource *res = &pci_dev->resource[i];
128 pci_resource_to_user(pci_dev, i, res, &start, &end);
129 str += sprintf(str,"0x%016llx 0x%016llx 0x%016llx\n",
130 (unsigned long long)start,
131 (unsigned long long)end,
132 (unsigned long long)res->flags);
137 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
139 struct pci_dev *pci_dev = to_pci_dev(dev);
141 return sprintf(buf, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02x\n",
142 pci_dev->vendor, pci_dev->device,
143 pci_dev->subsystem_vendor, pci_dev->subsystem_device,
144 (u8)(pci_dev->class >> 16), (u8)(pci_dev->class >> 8),
145 (u8)(pci_dev->class));
148 static ssize_t is_enabled_store(struct device *dev,
149 struct device_attribute *attr, const char *buf,
152 struct pci_dev *pdev = to_pci_dev(dev);
154 ssize_t result = strict_strtoul(buf, 0, &val);
159 /* this can crash the machine when done on the "wrong" device */
160 if (!capable(CAP_SYS_ADMIN))
164 if (pci_is_enabled(pdev))
165 pci_disable_device(pdev);
169 result = pci_enable_device(pdev);
171 return result < 0 ? result : count;
174 static ssize_t is_enabled_show(struct device *dev,
175 struct device_attribute *attr, char *buf)
177 struct pci_dev *pdev;
179 pdev = to_pci_dev (dev);
180 return sprintf (buf, "%u\n", atomic_read(&pdev->enable_cnt));
185 numa_node_show(struct device *dev, struct device_attribute *attr, char *buf)
187 return sprintf (buf, "%d\n", dev->numa_node);
192 dma_mask_bits_show(struct device *dev, struct device_attribute *attr, char *buf)
194 struct pci_dev *pdev = to_pci_dev(dev);
196 return sprintf (buf, "%d\n", fls64(pdev->dma_mask));
200 consistent_dma_mask_bits_show(struct device *dev, struct device_attribute *attr,
203 return sprintf (buf, "%d\n", fls64(dev->coherent_dma_mask));
207 msi_bus_show(struct device *dev, struct device_attribute *attr, char *buf)
209 struct pci_dev *pdev = to_pci_dev(dev);
211 if (!pdev->subordinate)
214 return sprintf (buf, "%u\n",
215 !(pdev->subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI));
219 msi_bus_store(struct device *dev, struct device_attribute *attr,
220 const char *buf, size_t count)
222 struct pci_dev *pdev = to_pci_dev(dev);
225 if (strict_strtoul(buf, 0, &val) < 0)
228 /* bad things may happen if the no_msi flag is changed
229 * while some drivers are loaded */
230 if (!capable(CAP_SYS_ADMIN))
233 /* Maybe pci devices without subordinate busses shouldn't even have this
234 * attribute in the first place? */
235 if (!pdev->subordinate)
238 /* Is the flag going to change, or keep the value it already had? */
239 if (!(pdev->subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI) ^
241 pdev->subordinate->bus_flags ^= PCI_BUS_FLAGS_NO_MSI;
243 dev_warn(&pdev->dev, "forced subordinate bus to%s support MSI,"
244 " bad things could happen\n", val ? "" : " not");
250 #ifdef CONFIG_HOTPLUG
251 static DEFINE_MUTEX(pci_remove_rescan_mutex);
252 static ssize_t bus_rescan_store(struct bus_type *bus, const char *buf,
256 struct pci_bus *b = NULL;
258 if (strict_strtoul(buf, 0, &val) < 0)
262 mutex_lock(&pci_remove_rescan_mutex);
263 while ((b = pci_find_next_bus(b)) != NULL)
265 mutex_unlock(&pci_remove_rescan_mutex);
270 struct bus_attribute pci_bus_attrs[] = {
271 __ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, bus_rescan_store),
276 dev_rescan_store(struct device *dev, struct device_attribute *attr,
277 const char *buf, size_t count)
280 struct pci_dev *pdev = to_pci_dev(dev);
282 if (strict_strtoul(buf, 0, &val) < 0)
286 mutex_lock(&pci_remove_rescan_mutex);
287 pci_rescan_bus(pdev->bus);
288 mutex_unlock(&pci_remove_rescan_mutex);
293 static void remove_callback(struct device *dev)
295 struct pci_dev *pdev = to_pci_dev(dev);
297 mutex_lock(&pci_remove_rescan_mutex);
298 pci_remove_bus_device(pdev);
299 mutex_unlock(&pci_remove_rescan_mutex);
303 remove_store(struct device *dev, struct device_attribute *dummy,
304 const char *buf, size_t count)
309 if (strict_strtoul(buf, 0, &val) < 0)
312 /* An attribute cannot be unregistered by one of its own methods,
313 * so we have to use this roundabout approach.
316 ret = device_schedule_callback(dev, remove_callback);
323 dev_bus_rescan_store(struct device *dev, struct device_attribute *attr,
324 const char *buf, size_t count)
327 struct pci_bus *bus = to_pci_bus(dev);
329 if (strict_strtoul(buf, 0, &val) < 0)
333 mutex_lock(&pci_remove_rescan_mutex);
335 mutex_unlock(&pci_remove_rescan_mutex);
342 struct device_attribute pci_dev_attrs[] = {
346 __ATTR_RO(subsystem_vendor),
347 __ATTR_RO(subsystem_device),
350 __ATTR_RO(local_cpus),
351 __ATTR_RO(local_cpulist),
354 __ATTR_RO(numa_node),
356 __ATTR_RO(dma_mask_bits),
357 __ATTR_RO(consistent_dma_mask_bits),
358 __ATTR(enable, 0600, is_enabled_show, is_enabled_store),
359 __ATTR(broken_parity_status,(S_IRUGO|S_IWUSR),
360 broken_parity_status_show,broken_parity_status_store),
361 __ATTR(msi_bus, 0644, msi_bus_show, msi_bus_store),
362 #ifdef CONFIG_HOTPLUG
363 __ATTR(remove, (S_IWUSR|S_IWGRP), NULL, remove_store),
364 __ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, dev_rescan_store),
369 struct device_attribute pcibus_dev_attrs[] = {
370 #ifdef CONFIG_HOTPLUG
371 __ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, dev_bus_rescan_store),
377 boot_vga_show(struct device *dev, struct device_attribute *attr, char *buf)
379 struct pci_dev *pdev = to_pci_dev(dev);
381 return sprintf(buf, "%u\n",
382 !!(pdev->resource[PCI_ROM_RESOURCE].flags &
383 IORESOURCE_ROM_SHADOW));
385 struct device_attribute vga_attr = __ATTR_RO(boot_vga);
388 pci_read_config(struct file *filp, struct kobject *kobj,
389 struct bin_attribute *bin_attr,
390 char *buf, loff_t off, size_t count)
392 struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
393 unsigned int size = 64;
394 loff_t init_off = off;
395 u8 *data = (u8*) buf;
397 /* Several chips lock up trying to read undefined config space */
398 if (security_capable(&init_user_ns, filp->f_cred, CAP_SYS_ADMIN) == 0) {
399 size = dev->cfg_size;
400 } else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
406 if (off + count > size) {
413 if ((off & 1) && size) {
415 pci_user_read_config_byte(dev, off, &val);
416 data[off - init_off] = val;
421 if ((off & 3) && size > 2) {
423 pci_user_read_config_word(dev, off, &val);
424 data[off - init_off] = val & 0xff;
425 data[off - init_off + 1] = (val >> 8) & 0xff;
432 pci_user_read_config_dword(dev, off, &val);
433 data[off - init_off] = val & 0xff;
434 data[off - init_off + 1] = (val >> 8) & 0xff;
435 data[off - init_off + 2] = (val >> 16) & 0xff;
436 data[off - init_off + 3] = (val >> 24) & 0xff;
443 pci_user_read_config_word(dev, off, &val);
444 data[off - init_off] = val & 0xff;
445 data[off - init_off + 1] = (val >> 8) & 0xff;
452 pci_user_read_config_byte(dev, off, &val);
453 data[off - init_off] = val;
462 pci_write_config(struct file* filp, struct kobject *kobj,
463 struct bin_attribute *bin_attr,
464 char *buf, loff_t off, size_t count)
466 struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
467 unsigned int size = count;
468 loff_t init_off = off;
469 u8 *data = (u8*) buf;
471 if (off > dev->cfg_size)
473 if (off + count > dev->cfg_size) {
474 size = dev->cfg_size - off;
478 if ((off & 1) && size) {
479 pci_user_write_config_byte(dev, off, data[off - init_off]);
484 if ((off & 3) && size > 2) {
485 u16 val = data[off - init_off];
486 val |= (u16) data[off - init_off + 1] << 8;
487 pci_user_write_config_word(dev, off, val);
493 u32 val = data[off - init_off];
494 val |= (u32) data[off - init_off + 1] << 8;
495 val |= (u32) data[off - init_off + 2] << 16;
496 val |= (u32) data[off - init_off + 3] << 24;
497 pci_user_write_config_dword(dev, off, val);
503 u16 val = data[off - init_off];
504 val |= (u16) data[off - init_off + 1] << 8;
505 pci_user_write_config_word(dev, off, val);
511 pci_user_write_config_byte(dev, off, data[off - init_off]);
520 read_vpd_attr(struct file *filp, struct kobject *kobj,
521 struct bin_attribute *bin_attr,
522 char *buf, loff_t off, size_t count)
524 struct pci_dev *dev =
525 to_pci_dev(container_of(kobj, struct device, kobj));
527 if (off > bin_attr->size)
529 else if (count > bin_attr->size - off)
530 count = bin_attr->size - off;
532 return pci_read_vpd(dev, off, count, buf);
536 write_vpd_attr(struct file *filp, struct kobject *kobj,
537 struct bin_attribute *bin_attr,
538 char *buf, loff_t off, size_t count)
540 struct pci_dev *dev =
541 to_pci_dev(container_of(kobj, struct device, kobj));
543 if (off > bin_attr->size)
545 else if (count > bin_attr->size - off)
546 count = bin_attr->size - off;
548 return pci_write_vpd(dev, off, count, buf);
551 #ifdef HAVE_PCI_LEGACY
553 * pci_read_legacy_io - read byte(s) from legacy I/O port space
554 * @filp: open sysfs file
555 * @kobj: kobject corresponding to file to read from
556 * @bin_attr: struct bin_attribute for this file
557 * @buf: buffer to store results
558 * @off: offset into legacy I/O port space
559 * @count: number of bytes to read
561 * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
562 * callback routine (pci_legacy_read).
565 pci_read_legacy_io(struct file *filp, struct kobject *kobj,
566 struct bin_attribute *bin_attr,
567 char *buf, loff_t off, size_t count)
569 struct pci_bus *bus = to_pci_bus(container_of(kobj,
573 /* Only support 1, 2 or 4 byte accesses */
574 if (count != 1 && count != 2 && count != 4)
577 return pci_legacy_read(bus, off, (u32 *)buf, count);
581 * pci_write_legacy_io - write byte(s) to legacy I/O port space
582 * @filp: open sysfs file
583 * @kobj: kobject corresponding to file to read from
584 * @bin_attr: struct bin_attribute for this file
585 * @buf: buffer containing value to be written
586 * @off: offset into legacy I/O port space
587 * @count: number of bytes to write
589 * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
590 * callback routine (pci_legacy_write).
593 pci_write_legacy_io(struct file *filp, struct kobject *kobj,
594 struct bin_attribute *bin_attr,
595 char *buf, loff_t off, size_t count)
597 struct pci_bus *bus = to_pci_bus(container_of(kobj,
600 /* Only support 1, 2 or 4 byte accesses */
601 if (count != 1 && count != 2 && count != 4)
604 return pci_legacy_write(bus, off, *(u32 *)buf, count);
608 * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
609 * @filp: open sysfs file
610 * @kobj: kobject corresponding to device to be mapped
611 * @attr: struct bin_attribute for this file
612 * @vma: struct vm_area_struct passed to mmap
614 * Uses an arch specific callback, pci_mmap_legacy_mem_page_range, to mmap
615 * legacy memory space (first meg of bus space) into application virtual
619 pci_mmap_legacy_mem(struct file *filp, struct kobject *kobj,
620 struct bin_attribute *attr,
621 struct vm_area_struct *vma)
623 struct pci_bus *bus = to_pci_bus(container_of(kobj,
627 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_mem);
631 * pci_mmap_legacy_io - map legacy PCI IO into user memory space
632 * @filp: open sysfs file
633 * @kobj: kobject corresponding to device to be mapped
634 * @attr: struct bin_attribute for this file
635 * @vma: struct vm_area_struct passed to mmap
637 * Uses an arch specific callback, pci_mmap_legacy_io_page_range, to mmap
638 * legacy IO space (first meg of bus space) into application virtual
639 * memory space. Returns -ENOSYS if the operation isn't supported
642 pci_mmap_legacy_io(struct file *filp, struct kobject *kobj,
643 struct bin_attribute *attr,
644 struct vm_area_struct *vma)
646 struct pci_bus *bus = to_pci_bus(container_of(kobj,
650 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_io);
654 * pci_adjust_legacy_attr - adjustment of legacy file attributes
655 * @b: bus to create files under
656 * @mmap_type: I/O port or memory
658 * Stub implementation. Can be overridden by arch if necessary.
661 pci_adjust_legacy_attr(struct pci_bus *b, enum pci_mmap_state mmap_type)
667 * pci_create_legacy_files - create legacy I/O port and memory files
668 * @b: bus to create files under
670 * Some platforms allow access to legacy I/O port and ISA memory space on
671 * a per-bus basis. This routine creates the files and ties them into
672 * their associated read, write and mmap files from pci-sysfs.c
674 * On error unwind, but don't propagate the error to the caller
675 * as it is ok to set up the PCI bus without these files.
677 void pci_create_legacy_files(struct pci_bus *b)
681 b->legacy_io = kzalloc(sizeof(struct bin_attribute) * 2,
686 sysfs_bin_attr_init(b->legacy_io);
687 b->legacy_io->attr.name = "legacy_io";
688 b->legacy_io->size = 0xffff;
689 b->legacy_io->attr.mode = S_IRUSR | S_IWUSR;
690 b->legacy_io->read = pci_read_legacy_io;
691 b->legacy_io->write = pci_write_legacy_io;
692 b->legacy_io->mmap = pci_mmap_legacy_io;
693 pci_adjust_legacy_attr(b, pci_mmap_io);
694 error = device_create_bin_file(&b->dev, b->legacy_io);
698 /* Allocated above after the legacy_io struct */
699 b->legacy_mem = b->legacy_io + 1;
700 sysfs_bin_attr_init(b->legacy_mem);
701 b->legacy_mem->attr.name = "legacy_mem";
702 b->legacy_mem->size = 1024*1024;
703 b->legacy_mem->attr.mode = S_IRUSR | S_IWUSR;
704 b->legacy_mem->mmap = pci_mmap_legacy_mem;
705 pci_adjust_legacy_attr(b, pci_mmap_mem);
706 error = device_create_bin_file(&b->dev, b->legacy_mem);
713 device_remove_bin_file(&b->dev, b->legacy_io);
718 printk(KERN_WARNING "pci: warning: could not create legacy I/O port "
719 "and ISA memory resources to sysfs\n");
723 void pci_remove_legacy_files(struct pci_bus *b)
726 device_remove_bin_file(&b->dev, b->legacy_io);
727 device_remove_bin_file(&b->dev, b->legacy_mem);
728 kfree(b->legacy_io); /* both are allocated here */
731 #endif /* HAVE_PCI_LEGACY */
735 int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vma,
736 enum pci_mmap_api mmap_api)
738 unsigned long nr, start, size, pci_start;
740 if (pci_resource_len(pdev, resno) == 0)
742 nr = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
743 start = vma->vm_pgoff;
744 size = ((pci_resource_len(pdev, resno) - 1) >> PAGE_SHIFT) + 1;
745 pci_start = (mmap_api == PCI_MMAP_PROCFS) ?
746 pci_resource_start(pdev, resno) >> PAGE_SHIFT : 0;
747 if (start >= pci_start && start < pci_start + size &&
748 start + nr <= pci_start + size)
754 * pci_mmap_resource - map a PCI resource into user memory space
755 * @kobj: kobject for mapping
756 * @attr: struct bin_attribute for the file being mapped
757 * @vma: struct vm_area_struct passed into the mmap
758 * @write_combine: 1 for write_combine mapping
760 * Use the regular PCI mapping routines to map a PCI resource into userspace.
763 pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr,
764 struct vm_area_struct *vma, int write_combine)
766 struct pci_dev *pdev = to_pci_dev(container_of(kobj,
767 struct device, kobj));
768 struct resource *res = attr->private;
769 enum pci_mmap_state mmap_type;
770 resource_size_t start, end;
773 for (i = 0; i < PCI_ROM_RESOURCE; i++)
774 if (res == &pdev->resource[i])
776 if (i >= PCI_ROM_RESOURCE)
779 if (!pci_mmap_fits(pdev, i, vma, PCI_MMAP_SYSFS)) {
780 WARN(1, "process \"%s\" tried to map 0x%08lx bytes "
781 "at page 0x%08lx on %s BAR %d (start 0x%16Lx, size 0x%16Lx)\n",
782 current->comm, vma->vm_end-vma->vm_start, vma->vm_pgoff,
784 (u64)pci_resource_start(pdev, i),
785 (u64)pci_resource_len(pdev, i));
789 /* pci_mmap_page_range() expects the same kind of entry as coming
790 * from /proc/bus/pci/ which is a "user visible" value. If this is
791 * different from the resource itself, arch will do necessary fixup.
793 pci_resource_to_user(pdev, i, res, &start, &end);
794 vma->vm_pgoff += start >> PAGE_SHIFT;
795 mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io;
797 if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(start))
800 return pci_mmap_page_range(pdev, vma, mmap_type, write_combine);
804 pci_mmap_resource_uc(struct file *filp, struct kobject *kobj,
805 struct bin_attribute *attr,
806 struct vm_area_struct *vma)
808 return pci_mmap_resource(kobj, attr, vma, 0);
812 pci_mmap_resource_wc(struct file *filp, struct kobject *kobj,
813 struct bin_attribute *attr,
814 struct vm_area_struct *vma)
816 return pci_mmap_resource(kobj, attr, vma, 1);
820 pci_resource_io(struct file *filp, struct kobject *kobj,
821 struct bin_attribute *attr, char *buf,
822 loff_t off, size_t count, bool write)
824 struct pci_dev *pdev = to_pci_dev(container_of(kobj,
825 struct device, kobj));
826 struct resource *res = attr->private;
827 unsigned long port = off;
830 for (i = 0; i < PCI_ROM_RESOURCE; i++)
831 if (res == &pdev->resource[i])
833 if (i >= PCI_ROM_RESOURCE)
836 port += pci_resource_start(pdev, i);
838 if (port > pci_resource_end(pdev, i))
841 if (port + count - 1 > pci_resource_end(pdev, i))
847 outb(*(u8 *)buf, port);
849 *(u8 *)buf = inb(port);
853 outw(*(u16 *)buf, port);
855 *(u16 *)buf = inw(port);
859 outl(*(u32 *)buf, port);
861 *(u32 *)buf = inl(port);
868 pci_read_resource_io(struct file *filp, struct kobject *kobj,
869 struct bin_attribute *attr, char *buf,
870 loff_t off, size_t count)
872 return pci_resource_io(filp, kobj, attr, buf, off, count, false);
876 pci_write_resource_io(struct file *filp, struct kobject *kobj,
877 struct bin_attribute *attr, char *buf,
878 loff_t off, size_t count)
880 return pci_resource_io(filp, kobj, attr, buf, off, count, true);
884 * pci_remove_resource_files - cleanup resource files
885 * @pdev: dev to cleanup
887 * If we created resource files for @pdev, remove them from sysfs and
888 * free their resources.
891 pci_remove_resource_files(struct pci_dev *pdev)
895 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
896 struct bin_attribute *res_attr;
898 res_attr = pdev->res_attr[i];
900 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
904 res_attr = pdev->res_attr_wc[i];
906 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
912 static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine)
914 /* allocate attribute structure, piggyback attribute name */
915 int name_len = write_combine ? 13 : 10;
916 struct bin_attribute *res_attr;
919 res_attr = kzalloc(sizeof(*res_attr) + name_len, GFP_ATOMIC);
921 char *res_attr_name = (char *)(res_attr + 1);
923 sysfs_bin_attr_init(res_attr);
925 pdev->res_attr_wc[num] = res_attr;
926 sprintf(res_attr_name, "resource%d_wc", num);
927 res_attr->mmap = pci_mmap_resource_wc;
929 pdev->res_attr[num] = res_attr;
930 sprintf(res_attr_name, "resource%d", num);
931 res_attr->mmap = pci_mmap_resource_uc;
933 if (pci_resource_flags(pdev, num) & IORESOURCE_IO) {
934 res_attr->read = pci_read_resource_io;
935 res_attr->write = pci_write_resource_io;
937 res_attr->attr.name = res_attr_name;
938 res_attr->attr.mode = S_IRUSR | S_IWUSR;
939 res_attr->size = pci_resource_len(pdev, num);
940 res_attr->private = &pdev->resource[num];
941 retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
949 * pci_create_resource_files - create resource files in sysfs for @dev
950 * @pdev: dev in question
952 * Walk the resources in @pdev creating files for each resource available.
954 static int pci_create_resource_files(struct pci_dev *pdev)
959 /* Expose the PCI resources from this device as files */
960 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
962 /* skip empty resources */
963 if (!pci_resource_len(pdev, i))
966 retval = pci_create_attr(pdev, i, 0);
967 /* for prefetchable resources, create a WC mappable file */
968 if (!retval && pdev->resource[i].flags & IORESOURCE_PREFETCH)
969 retval = pci_create_attr(pdev, i, 1);
972 pci_remove_resource_files(pdev);
978 #else /* !HAVE_PCI_MMAP */
979 int __weak pci_create_resource_files(struct pci_dev *dev) { return 0; }
980 void __weak pci_remove_resource_files(struct pci_dev *dev) { return; }
981 #endif /* HAVE_PCI_MMAP */
984 * pci_write_rom - used to enable access to the PCI ROM display
986 * @kobj: kernel object handle
987 * @bin_attr: struct bin_attribute for this file
990 * @count: number of byte in input
992 * writing anything except 0 enables it
995 pci_write_rom(struct file *filp, struct kobject *kobj,
996 struct bin_attribute *bin_attr,
997 char *buf, loff_t off, size_t count)
999 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
1001 if ((off == 0) && (*buf == '0') && (count == 2))
1002 pdev->rom_attr_enabled = 0;
1004 pdev->rom_attr_enabled = 1;
1010 * pci_read_rom - read a PCI ROM
1012 * @kobj: kernel object handle
1013 * @bin_attr: struct bin_attribute for this file
1014 * @buf: where to put the data we read from the ROM
1016 * @count: number of bytes to read
1018 * Put @count bytes starting at @off into @buf from the ROM in the PCI
1019 * device corresponding to @kobj.
1022 pci_read_rom(struct file *filp, struct kobject *kobj,
1023 struct bin_attribute *bin_attr,
1024 char *buf, loff_t off, size_t count)
1026 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
1030 if (!pdev->rom_attr_enabled)
1033 rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */
1040 if (off + count > size)
1043 memcpy_fromio(buf, rom + off, count);
1045 pci_unmap_rom(pdev, rom);
1050 static struct bin_attribute pci_config_attr = {
1053 .mode = S_IRUGO | S_IWUSR,
1055 .size = PCI_CFG_SPACE_SIZE,
1056 .read = pci_read_config,
1057 .write = pci_write_config,
1060 static struct bin_attribute pcie_config_attr = {
1063 .mode = S_IRUGO | S_IWUSR,
1065 .size = PCI_CFG_SPACE_EXP_SIZE,
1066 .read = pci_read_config,
1067 .write = pci_write_config,
1070 int __attribute__ ((weak)) pcibios_add_platform_entries(struct pci_dev *dev)
1075 static ssize_t reset_store(struct device *dev,
1076 struct device_attribute *attr, const char *buf,
1079 struct pci_dev *pdev = to_pci_dev(dev);
1081 ssize_t result = strict_strtoul(buf, 0, &val);
1089 result = pci_reset_function(pdev);
1096 static struct device_attribute reset_attr = __ATTR(reset, 0200, NULL, reset_store);
1098 static int pci_create_capabilities_sysfs(struct pci_dev *dev)
1101 struct bin_attribute *attr;
1103 /* If the device has VPD, try to expose it in sysfs. */
1105 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
1109 sysfs_bin_attr_init(attr);
1110 attr->size = dev->vpd->len;
1111 attr->attr.name = "vpd";
1112 attr->attr.mode = S_IRUSR | S_IWUSR;
1113 attr->read = read_vpd_attr;
1114 attr->write = write_vpd_attr;
1115 retval = sysfs_create_bin_file(&dev->dev.kobj, attr);
1120 dev->vpd->attr = attr;
1123 /* Active State Power Management */
1124 pcie_aspm_create_sysfs_dev_files(dev);
1126 if (!pci_probe_reset_function(dev)) {
1127 retval = device_create_file(&dev->dev, &reset_attr);
1135 pcie_aspm_remove_sysfs_dev_files(dev);
1136 if (dev->vpd && dev->vpd->attr) {
1137 sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
1138 kfree(dev->vpd->attr);
1144 int __must_check pci_create_sysfs_dev_files (struct pci_dev *pdev)
1148 struct bin_attribute *attr;
1150 if (!sysfs_initialized)
1153 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
1154 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr);
1156 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1160 retval = pci_create_resource_files(pdev);
1162 goto err_config_file;
1164 if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
1165 rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
1166 else if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
1169 /* If the device has a ROM, try to expose it in sysfs. */
1171 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
1174 goto err_resource_files;
1176 sysfs_bin_attr_init(attr);
1177 attr->size = rom_size;
1178 attr->attr.name = "rom";
1179 attr->attr.mode = S_IRUSR | S_IWUSR;
1180 attr->read = pci_read_rom;
1181 attr->write = pci_write_rom;
1182 retval = sysfs_create_bin_file(&pdev->dev.kobj, attr);
1185 goto err_resource_files;
1187 pdev->rom_attr = attr;
1190 if ((pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA) {
1191 retval = device_create_file(&pdev->dev, &vga_attr);
1196 /* add platform-specific attributes */
1197 retval = pcibios_add_platform_entries(pdev);
1201 /* add sysfs entries for various capabilities */
1202 retval = pci_create_capabilities_sysfs(pdev);
1206 pci_create_firmware_label_files(pdev);
1211 if ((pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
1212 device_remove_file(&pdev->dev, &vga_attr);
1215 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
1216 kfree(pdev->rom_attr);
1217 pdev->rom_attr = NULL;
1220 pci_remove_resource_files(pdev);
1222 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
1223 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
1225 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1230 static void pci_remove_capabilities_sysfs(struct pci_dev *dev)
1232 if (dev->vpd && dev->vpd->attr) {
1233 sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
1234 kfree(dev->vpd->attr);
1237 pcie_aspm_remove_sysfs_dev_files(dev);
1238 if (dev->reset_fn) {
1239 device_remove_file(&dev->dev, &reset_attr);
1245 * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
1246 * @pdev: device whose entries we should free
1248 * Cleanup when @pdev is removed from sysfs.
1250 void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
1254 if (!sysfs_initialized)
1257 pci_remove_capabilities_sysfs(pdev);
1259 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
1260 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
1262 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1264 pci_remove_resource_files(pdev);
1266 if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
1267 rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
1268 else if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
1271 if (rom_size && pdev->rom_attr) {
1272 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
1273 kfree(pdev->rom_attr);
1276 pci_remove_firmware_label_files(pdev);
1280 static int __init pci_sysfs_init(void)
1282 struct pci_dev *pdev = NULL;
1285 sysfs_initialized = 1;
1286 for_each_pci_dev(pdev) {
1287 retval = pci_create_sysfs_dev_files(pdev);
1297 late_initcall(pci_sysfs_init);