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>
26 #include <linux/slab.h>
29 static int sysfs_initialized
; /* = 0 */
31 /* show configuration fields */
32 #define pci_config_attr(field, format_string) \
34 field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
36 struct pci_dev *pdev; \
38 pdev = to_pci_dev (dev); \
39 return sprintf (buf, format_string, pdev->field); \
42 pci_config_attr(vendor
, "0x%04x\n");
43 pci_config_attr(device
, "0x%04x\n");
44 pci_config_attr(subsystem_vendor
, "0x%04x\n");
45 pci_config_attr(subsystem_device
, "0x%04x\n");
46 pci_config_attr(class, "0x%06x\n");
47 pci_config_attr(irq
, "%u\n");
49 static ssize_t
broken_parity_status_show(struct device
*dev
,
50 struct device_attribute
*attr
,
53 struct pci_dev
*pdev
= to_pci_dev(dev
);
54 return sprintf (buf
, "%u\n", pdev
->broken_parity_status
);
57 static ssize_t
broken_parity_status_store(struct device
*dev
,
58 struct device_attribute
*attr
,
59 const char *buf
, size_t count
)
61 struct pci_dev
*pdev
= to_pci_dev(dev
);
64 if (strict_strtoul(buf
, 0, &val
) < 0)
67 pdev
->broken_parity_status
= !!val
;
72 static ssize_t
local_cpus_show(struct device
*dev
,
73 struct device_attribute
*attr
, char *buf
)
75 const struct cpumask
*mask
;
79 mask
= (dev_to_node(dev
) == -1) ? cpu_online_mask
:
80 cpumask_of_node(dev_to_node(dev
));
82 mask
= cpumask_of_pcibus(to_pci_dev(dev
)->bus
);
84 len
= cpumask_scnprintf(buf
, PAGE_SIZE
-2, mask
);
91 static ssize_t
local_cpulist_show(struct device
*dev
,
92 struct device_attribute
*attr
, char *buf
)
94 const struct cpumask
*mask
;
98 mask
= (dev_to_node(dev
) == -1) ? cpu_online_mask
:
99 cpumask_of_node(dev_to_node(dev
));
101 mask
= cpumask_of_pcibus(to_pci_dev(dev
)->bus
);
103 len
= cpulist_scnprintf(buf
, PAGE_SIZE
-2, mask
);
111 resource_show(struct device
* dev
, struct device_attribute
*attr
, char * buf
)
113 struct pci_dev
* pci_dev
= to_pci_dev(dev
);
117 resource_size_t start
, end
;
119 if (pci_dev
->subordinate
)
120 max
= DEVICE_COUNT_RESOURCE
;
122 max
= PCI_BRIDGE_RESOURCES
;
124 for (i
= 0; i
< max
; i
++) {
125 struct resource
*res
= &pci_dev
->resource
[i
];
126 pci_resource_to_user(pci_dev
, i
, res
, &start
, &end
);
127 str
+= sprintf(str
,"0x%016llx 0x%016llx 0x%016llx\n",
128 (unsigned long long)start
,
129 (unsigned long long)end
,
130 (unsigned long long)res
->flags
);
135 static ssize_t
modalias_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
137 struct pci_dev
*pci_dev
= to_pci_dev(dev
);
139 return sprintf(buf
, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02x\n",
140 pci_dev
->vendor
, pci_dev
->device
,
141 pci_dev
->subsystem_vendor
, pci_dev
->subsystem_device
,
142 (u8
)(pci_dev
->class >> 16), (u8
)(pci_dev
->class >> 8),
143 (u8
)(pci_dev
->class));
146 static ssize_t
is_enabled_store(struct device
*dev
,
147 struct device_attribute
*attr
, const char *buf
,
150 struct pci_dev
*pdev
= to_pci_dev(dev
);
152 ssize_t result
= strict_strtoul(buf
, 0, &val
);
157 /* this can crash the machine when done on the "wrong" device */
158 if (!capable(CAP_SYS_ADMIN
))
162 if (pci_is_enabled(pdev
))
163 pci_disable_device(pdev
);
167 result
= pci_enable_device(pdev
);
169 return result
< 0 ? result
: count
;
172 static ssize_t
is_enabled_show(struct device
*dev
,
173 struct device_attribute
*attr
, char *buf
)
175 struct pci_dev
*pdev
;
177 pdev
= to_pci_dev (dev
);
178 return sprintf (buf
, "%u\n", atomic_read(&pdev
->enable_cnt
));
183 numa_node_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
185 return sprintf (buf
, "%d\n", dev
->numa_node
);
190 dma_mask_bits_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
192 struct pci_dev
*pdev
= to_pci_dev(dev
);
194 return sprintf (buf
, "%d\n", fls64(pdev
->dma_mask
));
198 consistent_dma_mask_bits_show(struct device
*dev
, struct device_attribute
*attr
,
201 return sprintf (buf
, "%d\n", fls64(dev
->coherent_dma_mask
));
205 msi_bus_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
207 struct pci_dev
*pdev
= to_pci_dev(dev
);
209 if (!pdev
->subordinate
)
212 return sprintf (buf
, "%u\n",
213 !(pdev
->subordinate
->bus_flags
& PCI_BUS_FLAGS_NO_MSI
));
217 msi_bus_store(struct device
*dev
, struct device_attribute
*attr
,
218 const char *buf
, size_t count
)
220 struct pci_dev
*pdev
= to_pci_dev(dev
);
223 if (strict_strtoul(buf
, 0, &val
) < 0)
226 /* bad things may happen if the no_msi flag is changed
227 * while some drivers are loaded */
228 if (!capable(CAP_SYS_ADMIN
))
231 /* Maybe pci devices without subordinate busses shouldn't even have this
232 * attribute in the first place? */
233 if (!pdev
->subordinate
)
236 /* Is the flag going to change, or keep the value it already had? */
237 if (!(pdev
->subordinate
->bus_flags
& PCI_BUS_FLAGS_NO_MSI
) ^
239 pdev
->subordinate
->bus_flags
^= PCI_BUS_FLAGS_NO_MSI
;
241 dev_warn(&pdev
->dev
, "forced subordinate bus to%s support MSI,"
242 " bad things could happen\n", val
? "" : " not");
248 #ifdef CONFIG_HOTPLUG
249 static DEFINE_MUTEX(pci_remove_rescan_mutex
);
250 static ssize_t
bus_rescan_store(struct bus_type
*bus
, const char *buf
,
254 struct pci_bus
*b
= NULL
;
256 if (strict_strtoul(buf
, 0, &val
) < 0)
260 mutex_lock(&pci_remove_rescan_mutex
);
261 while ((b
= pci_find_next_bus(b
)) != NULL
)
263 mutex_unlock(&pci_remove_rescan_mutex
);
268 struct bus_attribute pci_bus_attrs
[] = {
269 __ATTR(rescan
, (S_IWUSR
|S_IWGRP
), NULL
, bus_rescan_store
),
274 dev_rescan_store(struct device
*dev
, struct device_attribute
*attr
,
275 const char *buf
, size_t count
)
278 struct pci_dev
*pdev
= to_pci_dev(dev
);
280 if (strict_strtoul(buf
, 0, &val
) < 0)
284 mutex_lock(&pci_remove_rescan_mutex
);
285 pci_rescan_bus(pdev
->bus
);
286 mutex_unlock(&pci_remove_rescan_mutex
);
291 static void remove_callback(struct device
*dev
)
293 struct pci_dev
*pdev
= to_pci_dev(dev
);
295 mutex_lock(&pci_remove_rescan_mutex
);
296 pci_remove_bus_device(pdev
);
297 mutex_unlock(&pci_remove_rescan_mutex
);
301 remove_store(struct device
*dev
, struct device_attribute
*dummy
,
302 const char *buf
, size_t count
)
307 if (strict_strtoul(buf
, 0, &val
) < 0)
310 /* An attribute cannot be unregistered by one of its own methods,
311 * so we have to use this roundabout approach.
314 ret
= device_schedule_callback(dev
, remove_callback
);
321 struct device_attribute pci_dev_attrs
[] = {
325 __ATTR_RO(subsystem_vendor
),
326 __ATTR_RO(subsystem_device
),
329 __ATTR_RO(local_cpus
),
330 __ATTR_RO(local_cpulist
),
333 __ATTR_RO(numa_node
),
335 __ATTR_RO(dma_mask_bits
),
336 __ATTR_RO(consistent_dma_mask_bits
),
337 __ATTR(enable
, 0600, is_enabled_show
, is_enabled_store
),
338 __ATTR(broken_parity_status
,(S_IRUGO
|S_IWUSR
),
339 broken_parity_status_show
,broken_parity_status_store
),
340 __ATTR(msi_bus
, 0644, msi_bus_show
, msi_bus_store
),
341 #ifdef CONFIG_HOTPLUG
342 __ATTR(remove
, (S_IWUSR
|S_IWGRP
), NULL
, remove_store
),
343 __ATTR(rescan
, (S_IWUSR
|S_IWGRP
), NULL
, dev_rescan_store
),
349 boot_vga_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
351 struct pci_dev
*pdev
= to_pci_dev(dev
);
353 return sprintf(buf
, "%u\n",
354 !!(pdev
->resource
[PCI_ROM_RESOURCE
].flags
&
355 IORESOURCE_ROM_SHADOW
));
357 struct device_attribute vga_attr
= __ATTR_RO(boot_vga
);
360 pci_read_config(struct kobject
*kobj
, struct bin_attribute
*bin_attr
,
361 char *buf
, loff_t off
, size_t count
)
363 struct pci_dev
*dev
= to_pci_dev(container_of(kobj
,struct device
,kobj
));
364 unsigned int size
= 64;
365 loff_t init_off
= off
;
366 u8
*data
= (u8
*) buf
;
368 /* Several chips lock up trying to read undefined config space */
369 if (capable(CAP_SYS_ADMIN
)) {
370 size
= dev
->cfg_size
;
371 } else if (dev
->hdr_type
== PCI_HEADER_TYPE_CARDBUS
) {
377 if (off
+ count
> size
) {
384 if ((off
& 1) && size
) {
386 pci_user_read_config_byte(dev
, off
, &val
);
387 data
[off
- init_off
] = val
;
392 if ((off
& 3) && size
> 2) {
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_dword(dev
, off
, &val
);
404 data
[off
- init_off
] = val
& 0xff;
405 data
[off
- init_off
+ 1] = (val
>> 8) & 0xff;
406 data
[off
- init_off
+ 2] = (val
>> 16) & 0xff;
407 data
[off
- init_off
+ 3] = (val
>> 24) & 0xff;
414 pci_user_read_config_word(dev
, off
, &val
);
415 data
[off
- init_off
] = val
& 0xff;
416 data
[off
- init_off
+ 1] = (val
>> 8) & 0xff;
423 pci_user_read_config_byte(dev
, off
, &val
);
424 data
[off
- init_off
] = val
;
433 pci_write_config(struct kobject
*kobj
, struct bin_attribute
*bin_attr
,
434 char *buf
, loff_t off
, size_t count
)
436 struct pci_dev
*dev
= to_pci_dev(container_of(kobj
,struct device
,kobj
));
437 unsigned int size
= count
;
438 loff_t init_off
= off
;
439 u8
*data
= (u8
*) buf
;
441 if (off
> dev
->cfg_size
)
443 if (off
+ count
> dev
->cfg_size
) {
444 size
= dev
->cfg_size
- off
;
448 if ((off
& 1) && size
) {
449 pci_user_write_config_byte(dev
, off
, data
[off
- init_off
]);
454 if ((off
& 3) && size
> 2) {
455 u16 val
= data
[off
- init_off
];
456 val
|= (u16
) data
[off
- init_off
+ 1] << 8;
457 pci_user_write_config_word(dev
, off
, val
);
463 u32 val
= data
[off
- init_off
];
464 val
|= (u32
) data
[off
- init_off
+ 1] << 8;
465 val
|= (u32
) data
[off
- init_off
+ 2] << 16;
466 val
|= (u32
) data
[off
- init_off
+ 3] << 24;
467 pci_user_write_config_dword(dev
, off
, val
);
473 u16 val
= data
[off
- init_off
];
474 val
|= (u16
) data
[off
- init_off
+ 1] << 8;
475 pci_user_write_config_word(dev
, off
, val
);
481 pci_user_write_config_byte(dev
, off
, data
[off
- init_off
]);
490 read_vpd_attr(struct kobject
*kobj
, struct bin_attribute
*bin_attr
,
491 char *buf
, loff_t off
, size_t count
)
493 struct pci_dev
*dev
=
494 to_pci_dev(container_of(kobj
, struct device
, kobj
));
496 if (off
> bin_attr
->size
)
498 else if (count
> bin_attr
->size
- off
)
499 count
= bin_attr
->size
- off
;
501 return pci_read_vpd(dev
, off
, count
, buf
);
505 write_vpd_attr(struct kobject
*kobj
, struct bin_attribute
*bin_attr
,
506 char *buf
, loff_t off
, size_t count
)
508 struct pci_dev
*dev
=
509 to_pci_dev(container_of(kobj
, struct device
, kobj
));
511 if (off
> bin_attr
->size
)
513 else if (count
> bin_attr
->size
- off
)
514 count
= bin_attr
->size
- off
;
516 return pci_write_vpd(dev
, off
, count
, buf
);
519 #ifdef HAVE_PCI_LEGACY
521 * pci_read_legacy_io - read byte(s) from legacy I/O port space
522 * @kobj: kobject corresponding to file to read from
523 * @bin_attr: struct bin_attribute for this file
524 * @buf: buffer to store results
525 * @off: offset into legacy I/O port space
526 * @count: number of bytes to read
528 * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
529 * callback routine (pci_legacy_read).
532 pci_read_legacy_io(struct kobject
*kobj
, struct bin_attribute
*bin_attr
,
533 char *buf
, loff_t off
, size_t count
)
535 struct pci_bus
*bus
= to_pci_bus(container_of(kobj
,
539 /* Only support 1, 2 or 4 byte accesses */
540 if (count
!= 1 && count
!= 2 && count
!= 4)
543 return pci_legacy_read(bus
, off
, (u32
*)buf
, count
);
547 * pci_write_legacy_io - write byte(s) to legacy I/O port space
548 * @kobj: kobject corresponding to file to read from
549 * @bin_attr: struct bin_attribute for this file
550 * @buf: buffer containing value to be written
551 * @off: offset into legacy I/O port space
552 * @count: number of bytes to write
554 * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
555 * callback routine (pci_legacy_write).
558 pci_write_legacy_io(struct kobject
*kobj
, struct bin_attribute
*bin_attr
,
559 char *buf
, loff_t off
, size_t count
)
561 struct pci_bus
*bus
= to_pci_bus(container_of(kobj
,
564 /* Only support 1, 2 or 4 byte accesses */
565 if (count
!= 1 && count
!= 2 && count
!= 4)
568 return pci_legacy_write(bus
, off
, *(u32
*)buf
, count
);
572 * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
573 * @kobj: kobject corresponding to device to be mapped
574 * @attr: struct bin_attribute for this file
575 * @vma: struct vm_area_struct passed to mmap
577 * Uses an arch specific callback, pci_mmap_legacy_mem_page_range, to mmap
578 * legacy memory space (first meg of bus space) into application virtual
582 pci_mmap_legacy_mem(struct kobject
*kobj
, struct bin_attribute
*attr
,
583 struct vm_area_struct
*vma
)
585 struct pci_bus
*bus
= to_pci_bus(container_of(kobj
,
589 return pci_mmap_legacy_page_range(bus
, vma
, pci_mmap_mem
);
593 * pci_mmap_legacy_io - map legacy PCI IO into user memory space
594 * @kobj: kobject corresponding to device to be mapped
595 * @attr: struct bin_attribute for this file
596 * @vma: struct vm_area_struct passed to mmap
598 * Uses an arch specific callback, pci_mmap_legacy_io_page_range, to mmap
599 * legacy IO space (first meg of bus space) into application virtual
600 * memory space. Returns -ENOSYS if the operation isn't supported
603 pci_mmap_legacy_io(struct kobject
*kobj
, struct bin_attribute
*attr
,
604 struct vm_area_struct
*vma
)
606 struct pci_bus
*bus
= to_pci_bus(container_of(kobj
,
610 return pci_mmap_legacy_page_range(bus
, vma
, pci_mmap_io
);
614 * pci_adjust_legacy_attr - adjustment of legacy file attributes
615 * @b: bus to create files under
616 * @mmap_type: I/O port or memory
618 * Stub implementation. Can be overridden by arch if necessary.
621 pci_adjust_legacy_attr(struct pci_bus
*b
, enum pci_mmap_state mmap_type
)
627 * pci_create_legacy_files - create legacy I/O port and memory files
628 * @b: bus to create files under
630 * Some platforms allow access to legacy I/O port and ISA memory space on
631 * a per-bus basis. This routine creates the files and ties them into
632 * their associated read, write and mmap files from pci-sysfs.c
634 * On error unwind, but don't propogate the error to the caller
635 * as it is ok to set up the PCI bus without these files.
637 void pci_create_legacy_files(struct pci_bus
*b
)
641 b
->legacy_io
= kzalloc(sizeof(struct bin_attribute
) * 2,
646 sysfs_bin_attr_init(b
->legacy_io
);
647 b
->legacy_io
->attr
.name
= "legacy_io";
648 b
->legacy_io
->size
= 0xffff;
649 b
->legacy_io
->attr
.mode
= S_IRUSR
| S_IWUSR
;
650 b
->legacy_io
->read
= pci_read_legacy_io
;
651 b
->legacy_io
->write
= pci_write_legacy_io
;
652 b
->legacy_io
->mmap
= pci_mmap_legacy_io
;
653 pci_adjust_legacy_attr(b
, pci_mmap_io
);
654 error
= device_create_bin_file(&b
->dev
, b
->legacy_io
);
658 /* Allocated above after the legacy_io struct */
659 b
->legacy_mem
= b
->legacy_io
+ 1;
660 sysfs_bin_attr_init(b
->legacy_mem
);
661 b
->legacy_mem
->attr
.name
= "legacy_mem";
662 b
->legacy_mem
->size
= 1024*1024;
663 b
->legacy_mem
->attr
.mode
= S_IRUSR
| S_IWUSR
;
664 b
->legacy_mem
->mmap
= pci_mmap_legacy_mem
;
665 pci_adjust_legacy_attr(b
, pci_mmap_mem
);
666 error
= device_create_bin_file(&b
->dev
, b
->legacy_mem
);
673 device_remove_bin_file(&b
->dev
, b
->legacy_io
);
678 printk(KERN_WARNING
"pci: warning: could not create legacy I/O port "
679 "and ISA memory resources to sysfs\n");
683 void pci_remove_legacy_files(struct pci_bus
*b
)
686 device_remove_bin_file(&b
->dev
, b
->legacy_io
);
687 device_remove_bin_file(&b
->dev
, b
->legacy_mem
);
688 kfree(b
->legacy_io
); /* both are allocated here */
691 #endif /* HAVE_PCI_LEGACY */
695 int pci_mmap_fits(struct pci_dev
*pdev
, int resno
, struct vm_area_struct
*vma
)
697 unsigned long nr
, start
, size
;
699 nr
= (vma
->vm_end
- vma
->vm_start
) >> PAGE_SHIFT
;
700 start
= vma
->vm_pgoff
;
701 size
= ((pci_resource_len(pdev
, resno
) - 1) >> PAGE_SHIFT
) + 1;
702 if (start
< size
&& size
- start
>= nr
)
704 WARN(1, "process \"%s\" tried to map 0x%08lx-0x%08lx on %s BAR %d (size 0x%08lx)\n",
705 current
->comm
, start
, start
+nr
, pci_name(pdev
), resno
, size
);
710 * pci_mmap_resource - map a PCI resource into user memory space
711 * @kobj: kobject for mapping
712 * @attr: struct bin_attribute for the file being mapped
713 * @vma: struct vm_area_struct passed into the mmap
714 * @write_combine: 1 for write_combine mapping
716 * Use the regular PCI mapping routines to map a PCI resource into userspace.
719 pci_mmap_resource(struct kobject
*kobj
, struct bin_attribute
*attr
,
720 struct vm_area_struct
*vma
, int write_combine
)
722 struct pci_dev
*pdev
= to_pci_dev(container_of(kobj
,
723 struct device
, kobj
));
724 struct resource
*res
= (struct resource
*)attr
->private;
725 enum pci_mmap_state mmap_type
;
726 resource_size_t start
, end
;
729 for (i
= 0; i
< PCI_ROM_RESOURCE
; i
++)
730 if (res
== &pdev
->resource
[i
])
732 if (i
>= PCI_ROM_RESOURCE
)
735 if (!pci_mmap_fits(pdev
, i
, vma
))
738 /* pci_mmap_page_range() expects the same kind of entry as coming
739 * from /proc/bus/pci/ which is a "user visible" value. If this is
740 * different from the resource itself, arch will do necessary fixup.
742 pci_resource_to_user(pdev
, i
, res
, &start
, &end
);
743 vma
->vm_pgoff
+= start
>> PAGE_SHIFT
;
744 mmap_type
= res
->flags
& IORESOURCE_MEM
? pci_mmap_mem
: pci_mmap_io
;
746 if (res
->flags
& IORESOURCE_MEM
&& iomem_is_exclusive(start
))
749 return pci_mmap_page_range(pdev
, vma
, mmap_type
, write_combine
);
753 pci_mmap_resource_uc(struct kobject
*kobj
, struct bin_attribute
*attr
,
754 struct vm_area_struct
*vma
)
756 return pci_mmap_resource(kobj
, attr
, vma
, 0);
760 pci_mmap_resource_wc(struct kobject
*kobj
, struct bin_attribute
*attr
,
761 struct vm_area_struct
*vma
)
763 return pci_mmap_resource(kobj
, attr
, vma
, 1);
767 * pci_remove_resource_files - cleanup resource files
768 * @pdev: dev to cleanup
770 * If we created resource files for @pdev, remove them from sysfs and
771 * free their resources.
774 pci_remove_resource_files(struct pci_dev
*pdev
)
778 for (i
= 0; i
< PCI_ROM_RESOURCE
; i
++) {
779 struct bin_attribute
*res_attr
;
781 res_attr
= pdev
->res_attr
[i
];
783 sysfs_remove_bin_file(&pdev
->dev
.kobj
, res_attr
);
787 res_attr
= pdev
->res_attr_wc
[i
];
789 sysfs_remove_bin_file(&pdev
->dev
.kobj
, res_attr
);
795 static int pci_create_attr(struct pci_dev
*pdev
, int num
, int write_combine
)
797 /* allocate attribute structure, piggyback attribute name */
798 int name_len
= write_combine
? 13 : 10;
799 struct bin_attribute
*res_attr
;
802 res_attr
= kzalloc(sizeof(*res_attr
) + name_len
, GFP_ATOMIC
);
804 char *res_attr_name
= (char *)(res_attr
+ 1);
806 sysfs_bin_attr_init(res_attr
);
808 pdev
->res_attr_wc
[num
] = res_attr
;
809 sprintf(res_attr_name
, "resource%d_wc", num
);
810 res_attr
->mmap
= pci_mmap_resource_wc
;
812 pdev
->res_attr
[num
] = res_attr
;
813 sprintf(res_attr_name
, "resource%d", num
);
814 res_attr
->mmap
= pci_mmap_resource_uc
;
816 res_attr
->attr
.name
= res_attr_name
;
817 res_attr
->attr
.mode
= S_IRUSR
| S_IWUSR
;
818 res_attr
->size
= pci_resource_len(pdev
, num
);
819 res_attr
->private = &pdev
->resource
[num
];
820 retval
= sysfs_create_bin_file(&pdev
->dev
.kobj
, res_attr
);
828 * pci_create_resource_files - create resource files in sysfs for @dev
829 * @pdev: dev in question
831 * Walk the resources in @pdev creating files for each resource available.
833 static int pci_create_resource_files(struct pci_dev
*pdev
)
838 /* Expose the PCI resources from this device as files */
839 for (i
= 0; i
< PCI_ROM_RESOURCE
; i
++) {
841 /* skip empty resources */
842 if (!pci_resource_len(pdev
, i
))
845 retval
= pci_create_attr(pdev
, i
, 0);
846 /* for prefetchable resources, create a WC mappable file */
847 if (!retval
&& pdev
->resource
[i
].flags
& IORESOURCE_PREFETCH
)
848 retval
= pci_create_attr(pdev
, i
, 1);
851 pci_remove_resource_files(pdev
);
857 #else /* !HAVE_PCI_MMAP */
858 int __weak
pci_create_resource_files(struct pci_dev
*dev
) { return 0; }
859 void __weak
pci_remove_resource_files(struct pci_dev
*dev
) { return; }
860 #endif /* HAVE_PCI_MMAP */
863 * pci_write_rom - used to enable access to the PCI ROM display
864 * @kobj: kernel object handle
865 * @bin_attr: struct bin_attribute for this file
868 * @count: number of byte in input
870 * writing anything except 0 enables it
873 pci_write_rom(struct kobject
*kobj
, struct bin_attribute
*bin_attr
,
874 char *buf
, loff_t off
, size_t count
)
876 struct pci_dev
*pdev
= to_pci_dev(container_of(kobj
, struct device
, kobj
));
878 if ((off
== 0) && (*buf
== '0') && (count
== 2))
879 pdev
->rom_attr_enabled
= 0;
881 pdev
->rom_attr_enabled
= 1;
887 * pci_read_rom - read a PCI ROM
888 * @kobj: kernel object handle
889 * @bin_attr: struct bin_attribute for this file
890 * @buf: where to put the data we read from the ROM
892 * @count: number of bytes to read
894 * Put @count bytes starting at @off into @buf from the ROM in the PCI
895 * device corresponding to @kobj.
898 pci_read_rom(struct kobject
*kobj
, struct bin_attribute
*bin_attr
,
899 char *buf
, loff_t off
, size_t count
)
901 struct pci_dev
*pdev
= to_pci_dev(container_of(kobj
, struct device
, kobj
));
905 if (!pdev
->rom_attr_enabled
)
908 rom
= pci_map_rom(pdev
, &size
); /* size starts out as PCI window size */
915 if (off
+ count
> size
)
918 memcpy_fromio(buf
, rom
+ off
, count
);
920 pci_unmap_rom(pdev
, rom
);
925 static struct bin_attribute pci_config_attr
= {
928 .mode
= S_IRUGO
| S_IWUSR
,
930 .size
= PCI_CFG_SPACE_SIZE
,
931 .read
= pci_read_config
,
932 .write
= pci_write_config
,
935 static struct bin_attribute pcie_config_attr
= {
938 .mode
= S_IRUGO
| S_IWUSR
,
940 .size
= PCI_CFG_SPACE_EXP_SIZE
,
941 .read
= pci_read_config
,
942 .write
= pci_write_config
,
945 int __attribute__ ((weak
)) pcibios_add_platform_entries(struct pci_dev
*dev
)
950 static ssize_t
reset_store(struct device
*dev
,
951 struct device_attribute
*attr
, const char *buf
,
954 struct pci_dev
*pdev
= to_pci_dev(dev
);
956 ssize_t result
= strict_strtoul(buf
, 0, &val
);
963 return pci_reset_function(pdev
);
966 static struct device_attribute reset_attr
= __ATTR(reset
, 0200, NULL
, reset_store
);
968 static int pci_create_capabilities_sysfs(struct pci_dev
*dev
)
971 struct bin_attribute
*attr
;
973 /* If the device has VPD, try to expose it in sysfs. */
975 attr
= kzalloc(sizeof(*attr
), GFP_ATOMIC
);
979 sysfs_bin_attr_init(attr
);
980 attr
->size
= dev
->vpd
->len
;
981 attr
->attr
.name
= "vpd";
982 attr
->attr
.mode
= S_IRUSR
| S_IWUSR
;
983 attr
->read
= read_vpd_attr
;
984 attr
->write
= write_vpd_attr
;
985 retval
= sysfs_create_bin_file(&dev
->dev
.kobj
, attr
);
987 kfree(dev
->vpd
->attr
);
990 dev
->vpd
->attr
= attr
;
993 /* Active State Power Management */
994 pcie_aspm_create_sysfs_dev_files(dev
);
996 if (!pci_probe_reset_function(dev
)) {
997 retval
= device_create_file(&dev
->dev
, &reset_attr
);
1005 pcie_aspm_remove_sysfs_dev_files(dev
);
1006 if (dev
->vpd
&& dev
->vpd
->attr
) {
1007 sysfs_remove_bin_file(&dev
->dev
.kobj
, dev
->vpd
->attr
);
1008 kfree(dev
->vpd
->attr
);
1014 int __must_check
pci_create_sysfs_dev_files (struct pci_dev
*pdev
)
1018 struct bin_attribute
*attr
;
1020 if (!sysfs_initialized
)
1023 if (pdev
->cfg_size
< PCI_CFG_SPACE_EXP_SIZE
)
1024 retval
= sysfs_create_bin_file(&pdev
->dev
.kobj
, &pci_config_attr
);
1026 retval
= sysfs_create_bin_file(&pdev
->dev
.kobj
, &pcie_config_attr
);
1030 retval
= pci_create_resource_files(pdev
);
1032 goto err_config_file
;
1034 if (pci_resource_len(pdev
, PCI_ROM_RESOURCE
))
1035 rom_size
= pci_resource_len(pdev
, PCI_ROM_RESOURCE
);
1036 else if (pdev
->resource
[PCI_ROM_RESOURCE
].flags
& IORESOURCE_ROM_SHADOW
)
1039 /* If the device has a ROM, try to expose it in sysfs. */
1041 attr
= kzalloc(sizeof(*attr
), GFP_ATOMIC
);
1044 goto err_resource_files
;
1046 sysfs_bin_attr_init(attr
);
1047 attr
->size
= rom_size
;
1048 attr
->attr
.name
= "rom";
1049 attr
->attr
.mode
= S_IRUSR
;
1050 attr
->read
= pci_read_rom
;
1051 attr
->write
= pci_write_rom
;
1052 retval
= sysfs_create_bin_file(&pdev
->dev
.kobj
, attr
);
1055 goto err_resource_files
;
1057 pdev
->rom_attr
= attr
;
1060 if ((pdev
->class >> 8) == PCI_CLASS_DISPLAY_VGA
) {
1061 retval
= device_create_file(&pdev
->dev
, &vga_attr
);
1066 /* add platform-specific attributes */
1067 retval
= pcibios_add_platform_entries(pdev
);
1071 /* add sysfs entries for various capabilities */
1072 retval
= pci_create_capabilities_sysfs(pdev
);
1079 if ((pdev
->class >> 8) == PCI_CLASS_DISPLAY_VGA
)
1080 device_remove_file(&pdev
->dev
, &vga_attr
);
1083 sysfs_remove_bin_file(&pdev
->dev
.kobj
, pdev
->rom_attr
);
1084 kfree(pdev
->rom_attr
);
1085 pdev
->rom_attr
= NULL
;
1088 pci_remove_resource_files(pdev
);
1090 if (pdev
->cfg_size
< PCI_CFG_SPACE_EXP_SIZE
)
1091 sysfs_remove_bin_file(&pdev
->dev
.kobj
, &pci_config_attr
);
1093 sysfs_remove_bin_file(&pdev
->dev
.kobj
, &pcie_config_attr
);
1098 static void pci_remove_capabilities_sysfs(struct pci_dev
*dev
)
1100 if (dev
->vpd
&& dev
->vpd
->attr
) {
1101 sysfs_remove_bin_file(&dev
->dev
.kobj
, dev
->vpd
->attr
);
1102 kfree(dev
->vpd
->attr
);
1105 pcie_aspm_remove_sysfs_dev_files(dev
);
1106 if (dev
->reset_fn
) {
1107 device_remove_file(&dev
->dev
, &reset_attr
);
1113 * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
1114 * @pdev: device whose entries we should free
1116 * Cleanup when @pdev is removed from sysfs.
1118 void pci_remove_sysfs_dev_files(struct pci_dev
*pdev
)
1122 if (!sysfs_initialized
)
1125 pci_remove_capabilities_sysfs(pdev
);
1127 if (pdev
->cfg_size
< PCI_CFG_SPACE_EXP_SIZE
)
1128 sysfs_remove_bin_file(&pdev
->dev
.kobj
, &pci_config_attr
);
1130 sysfs_remove_bin_file(&pdev
->dev
.kobj
, &pcie_config_attr
);
1132 pci_remove_resource_files(pdev
);
1134 if (pci_resource_len(pdev
, PCI_ROM_RESOURCE
))
1135 rom_size
= pci_resource_len(pdev
, PCI_ROM_RESOURCE
);
1136 else if (pdev
->resource
[PCI_ROM_RESOURCE
].flags
& IORESOURCE_ROM_SHADOW
)
1139 if (rom_size
&& pdev
->rom_attr
) {
1140 sysfs_remove_bin_file(&pdev
->dev
.kobj
, pdev
->rom_attr
);
1141 kfree(pdev
->rom_attr
);
1145 static int __init
pci_sysfs_init(void)
1147 struct pci_dev
*pdev
= NULL
;
1150 sysfs_initialized
= 1;
1151 for_each_pci_dev(pdev
) {
1152 retval
= pci_create_sysfs_dev_files(pdev
);
1162 late_initcall(pci_sysfs_init
);