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 dma_mask_bits_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
189 struct pci_dev
*pdev
= to_pci_dev(dev
);
191 return sprintf (buf
, "%d\n", fls64(pdev
->dma_mask
));
195 consistent_dma_mask_bits_show(struct device
*dev
, struct device_attribute
*attr
,
198 return sprintf (buf
, "%d\n", fls64(dev
->coherent_dma_mask
));
202 msi_bus_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
204 struct pci_dev
*pdev
= to_pci_dev(dev
);
206 if (!pdev
->subordinate
)
209 return sprintf (buf
, "%u\n",
210 !(pdev
->subordinate
->bus_flags
& PCI_BUS_FLAGS_NO_MSI
));
214 msi_bus_store(struct device
*dev
, struct device_attribute
*attr
,
215 const char *buf
, size_t count
)
217 struct pci_dev
*pdev
= to_pci_dev(dev
);
220 if (strict_strtoul(buf
, 0, &val
) < 0)
223 /* bad things may happen if the no_msi flag is changed
224 * while some drivers are loaded */
225 if (!capable(CAP_SYS_ADMIN
))
228 /* Maybe pci devices without subordinate busses shouldn't even have this
229 * attribute in the first place? */
230 if (!pdev
->subordinate
)
233 /* Is the flag going to change, or keep the value it already had? */
234 if (!(pdev
->subordinate
->bus_flags
& PCI_BUS_FLAGS_NO_MSI
) ^
236 pdev
->subordinate
->bus_flags
^= PCI_BUS_FLAGS_NO_MSI
;
238 dev_warn(&pdev
->dev
, "forced subordinate bus to%s support MSI,"
239 " bad things could happen\n", val
? "" : " not");
245 #ifdef CONFIG_HOTPLUG
246 static DEFINE_MUTEX(pci_remove_rescan_mutex
);
247 static ssize_t
bus_rescan_store(struct bus_type
*bus
, const char *buf
,
251 struct pci_bus
*b
= NULL
;
253 if (strict_strtoul(buf
, 0, &val
) < 0)
257 mutex_lock(&pci_remove_rescan_mutex
);
258 while ((b
= pci_find_next_bus(b
)) != NULL
)
260 mutex_unlock(&pci_remove_rescan_mutex
);
265 struct bus_attribute pci_bus_attrs
[] = {
266 __ATTR(rescan
, (S_IWUSR
|S_IWGRP
), NULL
, bus_rescan_store
),
271 dev_rescan_store(struct device
*dev
, struct device_attribute
*attr
,
272 const char *buf
, size_t count
)
275 struct pci_dev
*pdev
= to_pci_dev(dev
);
277 if (strict_strtoul(buf
, 0, &val
) < 0)
281 mutex_lock(&pci_remove_rescan_mutex
);
282 pci_rescan_bus(pdev
->bus
);
283 mutex_unlock(&pci_remove_rescan_mutex
);
288 static void remove_callback(struct device
*dev
)
290 struct pci_dev
*pdev
= to_pci_dev(dev
);
292 mutex_lock(&pci_remove_rescan_mutex
);
293 pci_remove_bus_device(pdev
);
294 mutex_unlock(&pci_remove_rescan_mutex
);
298 remove_store(struct device
*dev
, struct device_attribute
*dummy
,
299 const char *buf
, size_t count
)
304 if (strict_strtoul(buf
, 0, &val
) < 0)
307 /* An attribute cannot be unregistered by one of its own methods,
308 * so we have to use this roundabout approach.
311 ret
= device_schedule_callback(dev
, remove_callback
);
318 struct device_attribute pci_dev_attrs
[] = {
322 __ATTR_RO(subsystem_vendor
),
323 __ATTR_RO(subsystem_device
),
326 __ATTR_RO(local_cpus
),
327 __ATTR_RO(local_cpulist
),
330 __ATTR_RO(numa_node
),
332 __ATTR_RO(dma_mask_bits
),
333 __ATTR_RO(consistent_dma_mask_bits
),
334 __ATTR(enable
, 0600, is_enabled_show
, is_enabled_store
),
335 __ATTR(broken_parity_status
,(S_IRUGO
|S_IWUSR
),
336 broken_parity_status_show
,broken_parity_status_store
),
337 __ATTR(msi_bus
, 0644, msi_bus_show
, msi_bus_store
),
338 #ifdef CONFIG_HOTPLUG
339 __ATTR(remove
, (S_IWUSR
|S_IWGRP
), NULL
, remove_store
),
340 __ATTR(rescan
, (S_IWUSR
|S_IWGRP
), NULL
, dev_rescan_store
),
346 boot_vga_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
348 struct pci_dev
*pdev
= to_pci_dev(dev
);
350 return sprintf(buf
, "%u\n",
351 !!(pdev
->resource
[PCI_ROM_RESOURCE
].flags
&
352 IORESOURCE_ROM_SHADOW
));
354 struct device_attribute vga_attr
= __ATTR_RO(boot_vga
);
357 pci_read_config(struct kobject
*kobj
, struct bin_attribute
*bin_attr
,
358 char *buf
, loff_t off
, size_t count
)
360 struct pci_dev
*dev
= to_pci_dev(container_of(kobj
,struct device
,kobj
));
361 unsigned int size
= 64;
362 loff_t init_off
= off
;
363 u8
*data
= (u8
*) buf
;
365 /* Several chips lock up trying to read undefined config space */
366 if (capable(CAP_SYS_ADMIN
)) {
367 size
= dev
->cfg_size
;
368 } else if (dev
->hdr_type
== PCI_HEADER_TYPE_CARDBUS
) {
374 if (off
+ count
> size
) {
381 if ((off
& 1) && size
) {
383 pci_user_read_config_byte(dev
, off
, &val
);
384 data
[off
- init_off
] = val
;
389 if ((off
& 3) && size
> 2) {
391 pci_user_read_config_word(dev
, off
, &val
);
392 data
[off
- init_off
] = val
& 0xff;
393 data
[off
- init_off
+ 1] = (val
>> 8) & 0xff;
400 pci_user_read_config_dword(dev
, off
, &val
);
401 data
[off
- init_off
] = val
& 0xff;
402 data
[off
- init_off
+ 1] = (val
>> 8) & 0xff;
403 data
[off
- init_off
+ 2] = (val
>> 16) & 0xff;
404 data
[off
- init_off
+ 3] = (val
>> 24) & 0xff;
411 pci_user_read_config_word(dev
, off
, &val
);
412 data
[off
- init_off
] = val
& 0xff;
413 data
[off
- init_off
+ 1] = (val
>> 8) & 0xff;
420 pci_user_read_config_byte(dev
, off
, &val
);
421 data
[off
- init_off
] = val
;
430 pci_write_config(struct kobject
*kobj
, struct bin_attribute
*bin_attr
,
431 char *buf
, loff_t off
, size_t count
)
433 struct pci_dev
*dev
= to_pci_dev(container_of(kobj
,struct device
,kobj
));
434 unsigned int size
= count
;
435 loff_t init_off
= off
;
436 u8
*data
= (u8
*) buf
;
438 if (off
> dev
->cfg_size
)
440 if (off
+ count
> dev
->cfg_size
) {
441 size
= dev
->cfg_size
- off
;
445 if ((off
& 1) && size
) {
446 pci_user_write_config_byte(dev
, off
, data
[off
- init_off
]);
451 if ((off
& 3) && size
> 2) {
452 u16 val
= data
[off
- init_off
];
453 val
|= (u16
) data
[off
- init_off
+ 1] << 8;
454 pci_user_write_config_word(dev
, off
, val
);
460 u32 val
= data
[off
- init_off
];
461 val
|= (u32
) data
[off
- init_off
+ 1] << 8;
462 val
|= (u32
) data
[off
- init_off
+ 2] << 16;
463 val
|= (u32
) data
[off
- init_off
+ 3] << 24;
464 pci_user_write_config_dword(dev
, off
, val
);
470 u16 val
= data
[off
- init_off
];
471 val
|= (u16
) data
[off
- init_off
+ 1] << 8;
472 pci_user_write_config_word(dev
, off
, val
);
478 pci_user_write_config_byte(dev
, off
, data
[off
- init_off
]);
487 read_vpd_attr(struct kobject
*kobj
, struct bin_attribute
*bin_attr
,
488 char *buf
, loff_t off
, size_t count
)
490 struct pci_dev
*dev
=
491 to_pci_dev(container_of(kobj
, struct device
, kobj
));
493 if (off
> bin_attr
->size
)
495 else if (count
> bin_attr
->size
- off
)
496 count
= bin_attr
->size
- off
;
498 return pci_read_vpd(dev
, off
, count
, buf
);
502 write_vpd_attr(struct kobject
*kobj
, struct bin_attribute
*bin_attr
,
503 char *buf
, loff_t off
, size_t count
)
505 struct pci_dev
*dev
=
506 to_pci_dev(container_of(kobj
, struct device
, kobj
));
508 if (off
> bin_attr
->size
)
510 else if (count
> bin_attr
->size
- off
)
511 count
= bin_attr
->size
- off
;
513 return pci_write_vpd(dev
, off
, count
, buf
);
516 #ifdef HAVE_PCI_LEGACY
518 * pci_read_legacy_io - read byte(s) from legacy I/O port space
519 * @kobj: kobject corresponding to file to read from
520 * @bin_attr: struct bin_attribute for this file
521 * @buf: buffer to store results
522 * @off: offset into legacy I/O port space
523 * @count: number of bytes to read
525 * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
526 * callback routine (pci_legacy_read).
529 pci_read_legacy_io(struct kobject
*kobj
, struct bin_attribute
*bin_attr
,
530 char *buf
, loff_t off
, size_t count
)
532 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_read(bus
, off
, (u32
*)buf
, count
);
544 * pci_write_legacy_io - write byte(s) to legacy I/O port space
545 * @kobj: kobject corresponding to file to read from
546 * @bin_attr: struct bin_attribute for this file
547 * @buf: buffer containing value to be written
548 * @off: offset into legacy I/O port space
549 * @count: number of bytes to write
551 * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
552 * callback routine (pci_legacy_write).
555 pci_write_legacy_io(struct kobject
*kobj
, struct bin_attribute
*bin_attr
,
556 char *buf
, loff_t off
, size_t count
)
558 struct pci_bus
*bus
= to_pci_bus(container_of(kobj
,
561 /* Only support 1, 2 or 4 byte accesses */
562 if (count
!= 1 && count
!= 2 && count
!= 4)
565 return pci_legacy_write(bus
, off
, *(u32
*)buf
, count
);
569 * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
570 * @kobj: kobject corresponding to device to be mapped
571 * @attr: struct bin_attribute for this file
572 * @vma: struct vm_area_struct passed to mmap
574 * Uses an arch specific callback, pci_mmap_legacy_mem_page_range, to mmap
575 * legacy memory space (first meg of bus space) into application virtual
579 pci_mmap_legacy_mem(struct kobject
*kobj
, struct bin_attribute
*attr
,
580 struct vm_area_struct
*vma
)
582 struct pci_bus
*bus
= to_pci_bus(container_of(kobj
,
586 return pci_mmap_legacy_page_range(bus
, vma
, pci_mmap_mem
);
590 * pci_mmap_legacy_io - map legacy PCI IO into user memory space
591 * @kobj: kobject corresponding to device to be mapped
592 * @attr: struct bin_attribute for this file
593 * @vma: struct vm_area_struct passed to mmap
595 * Uses an arch specific callback, pci_mmap_legacy_io_page_range, to mmap
596 * legacy IO space (first meg of bus space) into application virtual
597 * memory space. Returns -ENOSYS if the operation isn't supported
600 pci_mmap_legacy_io(struct kobject
*kobj
, struct bin_attribute
*attr
,
601 struct vm_area_struct
*vma
)
603 struct pci_bus
*bus
= to_pci_bus(container_of(kobj
,
607 return pci_mmap_legacy_page_range(bus
, vma
, pci_mmap_io
);
611 * pci_adjust_legacy_attr - adjustment of legacy file attributes
612 * @b: bus to create files under
613 * @mmap_type: I/O port or memory
615 * Stub implementation. Can be overridden by arch if necessary.
618 pci_adjust_legacy_attr(struct pci_bus
*b
, enum pci_mmap_state mmap_type
)
624 * pci_create_legacy_files - create legacy I/O port and memory files
625 * @b: bus to create files under
627 * Some platforms allow access to legacy I/O port and ISA memory space on
628 * a per-bus basis. This routine creates the files and ties them into
629 * their associated read, write and mmap files from pci-sysfs.c
631 * On error unwind, but don't propogate the error to the caller
632 * as it is ok to set up the PCI bus without these files.
634 void pci_create_legacy_files(struct pci_bus
*b
)
638 b
->legacy_io
= kzalloc(sizeof(struct bin_attribute
) * 2,
643 b
->legacy_io
->attr
.name
= "legacy_io";
644 b
->legacy_io
->size
= 0xffff;
645 b
->legacy_io
->attr
.mode
= S_IRUSR
| S_IWUSR
;
646 b
->legacy_io
->read
= pci_read_legacy_io
;
647 b
->legacy_io
->write
= pci_write_legacy_io
;
648 b
->legacy_io
->mmap
= pci_mmap_legacy_io
;
649 pci_adjust_legacy_attr(b
, pci_mmap_io
);
650 error
= device_create_bin_file(&b
->dev
, b
->legacy_io
);
654 /* Allocated above after the legacy_io struct */
655 b
->legacy_mem
= b
->legacy_io
+ 1;
656 b
->legacy_mem
->attr
.name
= "legacy_mem";
657 b
->legacy_mem
->size
= 1024*1024;
658 b
->legacy_mem
->attr
.mode
= S_IRUSR
| S_IWUSR
;
659 b
->legacy_mem
->mmap
= pci_mmap_legacy_mem
;
660 pci_adjust_legacy_attr(b
, pci_mmap_mem
);
661 error
= device_create_bin_file(&b
->dev
, b
->legacy_mem
);
668 device_remove_bin_file(&b
->dev
, b
->legacy_io
);
673 printk(KERN_WARNING
"pci: warning: could not create legacy I/O port "
674 "and ISA memory resources to sysfs\n");
678 void pci_remove_legacy_files(struct pci_bus
*b
)
681 device_remove_bin_file(&b
->dev
, b
->legacy_io
);
682 device_remove_bin_file(&b
->dev
, b
->legacy_mem
);
683 kfree(b
->legacy_io
); /* both are allocated here */
686 #endif /* HAVE_PCI_LEGACY */
690 int pci_mmap_fits(struct pci_dev
*pdev
, int resno
, struct vm_area_struct
*vma
)
692 unsigned long nr
, start
, size
;
694 nr
= (vma
->vm_end
- vma
->vm_start
) >> PAGE_SHIFT
;
695 start
= vma
->vm_pgoff
;
696 size
= ((pci_resource_len(pdev
, resno
) - 1) >> PAGE_SHIFT
) + 1;
697 if (start
< size
&& size
- start
>= nr
)
699 WARN(1, "process \"%s\" tried to map 0x%08lx-0x%08lx on %s BAR %d (size 0x%08lx)\n",
700 current
->comm
, start
, start
+nr
, pci_name(pdev
), resno
, size
);
705 * pci_mmap_resource - map a PCI resource into user memory space
706 * @kobj: kobject for mapping
707 * @attr: struct bin_attribute for the file being mapped
708 * @vma: struct vm_area_struct passed into the mmap
709 * @write_combine: 1 for write_combine mapping
711 * Use the regular PCI mapping routines to map a PCI resource into userspace.
714 pci_mmap_resource(struct kobject
*kobj
, struct bin_attribute
*attr
,
715 struct vm_area_struct
*vma
, int write_combine
)
717 struct pci_dev
*pdev
= to_pci_dev(container_of(kobj
,
718 struct device
, kobj
));
719 struct resource
*res
= (struct resource
*)attr
->private;
720 enum pci_mmap_state mmap_type
;
721 resource_size_t start
, end
;
724 for (i
= 0; i
< PCI_ROM_RESOURCE
; i
++)
725 if (res
== &pdev
->resource
[i
])
727 if (i
>= PCI_ROM_RESOURCE
)
730 if (!pci_mmap_fits(pdev
, i
, vma
))
733 /* pci_mmap_page_range() expects the same kind of entry as coming
734 * from /proc/bus/pci/ which is a "user visible" value. If this is
735 * different from the resource itself, arch will do necessary fixup.
737 pci_resource_to_user(pdev
, i
, res
, &start
, &end
);
738 vma
->vm_pgoff
+= start
>> PAGE_SHIFT
;
739 mmap_type
= res
->flags
& IORESOURCE_MEM
? pci_mmap_mem
: pci_mmap_io
;
741 if (res
->flags
& IORESOURCE_MEM
&& iomem_is_exclusive(start
))
744 return pci_mmap_page_range(pdev
, vma
, mmap_type
, write_combine
);
748 pci_mmap_resource_uc(struct kobject
*kobj
, struct bin_attribute
*attr
,
749 struct vm_area_struct
*vma
)
751 return pci_mmap_resource(kobj
, attr
, vma
, 0);
755 pci_mmap_resource_wc(struct kobject
*kobj
, struct bin_attribute
*attr
,
756 struct vm_area_struct
*vma
)
758 return pci_mmap_resource(kobj
, attr
, vma
, 1);
762 * pci_remove_resource_files - cleanup resource files
763 * @pdev: dev to cleanup
765 * If we created resource files for @pdev, remove them from sysfs and
766 * free their resources.
769 pci_remove_resource_files(struct pci_dev
*pdev
)
773 for (i
= 0; i
< PCI_ROM_RESOURCE
; i
++) {
774 struct bin_attribute
*res_attr
;
776 res_attr
= pdev
->res_attr
[i
];
778 sysfs_remove_bin_file(&pdev
->dev
.kobj
, res_attr
);
782 res_attr
= pdev
->res_attr_wc
[i
];
784 sysfs_remove_bin_file(&pdev
->dev
.kobj
, res_attr
);
790 static int pci_create_attr(struct pci_dev
*pdev
, int num
, int write_combine
)
792 /* allocate attribute structure, piggyback attribute name */
793 int name_len
= write_combine
? 13 : 10;
794 struct bin_attribute
*res_attr
;
797 res_attr
= kzalloc(sizeof(*res_attr
) + name_len
, GFP_ATOMIC
);
799 char *res_attr_name
= (char *)(res_attr
+ 1);
802 pdev
->res_attr_wc
[num
] = res_attr
;
803 sprintf(res_attr_name
, "resource%d_wc", num
);
804 res_attr
->mmap
= pci_mmap_resource_wc
;
806 pdev
->res_attr
[num
] = res_attr
;
807 sprintf(res_attr_name
, "resource%d", num
);
808 res_attr
->mmap
= pci_mmap_resource_uc
;
810 res_attr
->attr
.name
= res_attr_name
;
811 res_attr
->attr
.mode
= S_IRUSR
| S_IWUSR
;
812 res_attr
->size
= pci_resource_len(pdev
, num
);
813 res_attr
->private = &pdev
->resource
[num
];
814 retval
= sysfs_create_bin_file(&pdev
->dev
.kobj
, res_attr
);
822 * pci_create_resource_files - create resource files in sysfs for @dev
823 * @pdev: dev in question
825 * Walk the resources in @pdev creating files for each resource available.
827 static int pci_create_resource_files(struct pci_dev
*pdev
)
832 /* Expose the PCI resources from this device as files */
833 for (i
= 0; i
< PCI_ROM_RESOURCE
; i
++) {
835 /* skip empty resources */
836 if (!pci_resource_len(pdev
, i
))
839 retval
= pci_create_attr(pdev
, i
, 0);
840 /* for prefetchable resources, create a WC mappable file */
841 if (!retval
&& pdev
->resource
[i
].flags
& IORESOURCE_PREFETCH
)
842 retval
= pci_create_attr(pdev
, i
, 1);
845 pci_remove_resource_files(pdev
);
851 #else /* !HAVE_PCI_MMAP */
852 int __weak
pci_create_resource_files(struct pci_dev
*dev
) { return 0; }
853 void __weak
pci_remove_resource_files(struct pci_dev
*dev
) { return; }
854 #endif /* HAVE_PCI_MMAP */
857 * pci_write_rom - used to enable access to the PCI ROM display
858 * @kobj: kernel object handle
859 * @bin_attr: struct bin_attribute for this file
862 * @count: number of byte in input
864 * writing anything except 0 enables it
867 pci_write_rom(struct kobject
*kobj
, struct bin_attribute
*bin_attr
,
868 char *buf
, loff_t off
, size_t count
)
870 struct pci_dev
*pdev
= to_pci_dev(container_of(kobj
, struct device
, kobj
));
872 if ((off
== 0) && (*buf
== '0') && (count
== 2))
873 pdev
->rom_attr_enabled
= 0;
875 pdev
->rom_attr_enabled
= 1;
881 * pci_read_rom - read a PCI ROM
882 * @kobj: kernel object handle
883 * @bin_attr: struct bin_attribute for this file
884 * @buf: where to put the data we read from the ROM
886 * @count: number of bytes to read
888 * Put @count bytes starting at @off into @buf from the ROM in the PCI
889 * device corresponding to @kobj.
892 pci_read_rom(struct kobject
*kobj
, struct bin_attribute
*bin_attr
,
893 char *buf
, loff_t off
, size_t count
)
895 struct pci_dev
*pdev
= to_pci_dev(container_of(kobj
, struct device
, kobj
));
899 if (!pdev
->rom_attr_enabled
)
902 rom
= pci_map_rom(pdev
, &size
); /* size starts out as PCI window size */
909 if (off
+ count
> size
)
912 memcpy_fromio(buf
, rom
+ off
, count
);
914 pci_unmap_rom(pdev
, rom
);
919 static struct bin_attribute pci_config_attr
= {
922 .mode
= S_IRUGO
| S_IWUSR
,
924 .size
= PCI_CFG_SPACE_SIZE
,
925 .read
= pci_read_config
,
926 .write
= pci_write_config
,
929 static struct bin_attribute pcie_config_attr
= {
932 .mode
= S_IRUGO
| S_IWUSR
,
934 .size
= PCI_CFG_SPACE_EXP_SIZE
,
935 .read
= pci_read_config
,
936 .write
= pci_write_config
,
939 int __attribute__ ((weak
)) pcibios_add_platform_entries(struct pci_dev
*dev
)
944 static ssize_t
reset_store(struct device
*dev
,
945 struct device_attribute
*attr
, const char *buf
,
948 struct pci_dev
*pdev
= to_pci_dev(dev
);
950 ssize_t result
= strict_strtoul(buf
, 0, &val
);
957 return pci_reset_function(pdev
);
960 static struct device_attribute reset_attr
= __ATTR(reset
, 0200, NULL
, reset_store
);
962 static int pci_create_capabilities_sysfs(struct pci_dev
*dev
)
965 struct bin_attribute
*attr
;
967 /* If the device has VPD, try to expose it in sysfs. */
969 attr
= kzalloc(sizeof(*attr
), GFP_ATOMIC
);
973 attr
->size
= dev
->vpd
->len
;
974 attr
->attr
.name
= "vpd";
975 attr
->attr
.mode
= S_IRUSR
| S_IWUSR
;
976 attr
->read
= read_vpd_attr
;
977 attr
->write
= write_vpd_attr
;
978 retval
= sysfs_create_bin_file(&dev
->dev
.kobj
, attr
);
980 kfree(dev
->vpd
->attr
);
983 dev
->vpd
->attr
= attr
;
986 /* Active State Power Management */
987 pcie_aspm_create_sysfs_dev_files(dev
);
989 if (!pci_probe_reset_function(dev
)) {
990 retval
= device_create_file(&dev
->dev
, &reset_attr
);
998 pcie_aspm_remove_sysfs_dev_files(dev
);
999 if (dev
->vpd
&& dev
->vpd
->attr
) {
1000 sysfs_remove_bin_file(&dev
->dev
.kobj
, dev
->vpd
->attr
);
1001 kfree(dev
->vpd
->attr
);
1007 int __must_check
pci_create_sysfs_dev_files (struct pci_dev
*pdev
)
1011 struct bin_attribute
*attr
;
1013 if (!sysfs_initialized
)
1016 if (pdev
->cfg_size
< PCI_CFG_SPACE_EXP_SIZE
)
1017 retval
= sysfs_create_bin_file(&pdev
->dev
.kobj
, &pci_config_attr
);
1019 retval
= sysfs_create_bin_file(&pdev
->dev
.kobj
, &pcie_config_attr
);
1023 retval
= pci_create_resource_files(pdev
);
1025 goto err_config_file
;
1027 if (pci_resource_len(pdev
, PCI_ROM_RESOURCE
))
1028 rom_size
= pci_resource_len(pdev
, PCI_ROM_RESOURCE
);
1029 else if (pdev
->resource
[PCI_ROM_RESOURCE
].flags
& IORESOURCE_ROM_SHADOW
)
1032 /* If the device has a ROM, try to expose it in sysfs. */
1034 attr
= kzalloc(sizeof(*attr
), GFP_ATOMIC
);
1037 goto err_resource_files
;
1039 attr
->size
= rom_size
;
1040 attr
->attr
.name
= "rom";
1041 attr
->attr
.mode
= S_IRUSR
;
1042 attr
->read
= pci_read_rom
;
1043 attr
->write
= pci_write_rom
;
1044 retval
= sysfs_create_bin_file(&pdev
->dev
.kobj
, attr
);
1047 goto err_resource_files
;
1049 pdev
->rom_attr
= attr
;
1052 if ((pdev
->class >> 8) == PCI_CLASS_DISPLAY_VGA
) {
1053 retval
= device_create_file(&pdev
->dev
, &vga_attr
);
1058 /* add platform-specific attributes */
1059 retval
= pcibios_add_platform_entries(pdev
);
1063 /* add sysfs entries for various capabilities */
1064 retval
= pci_create_capabilities_sysfs(pdev
);
1071 if ((pdev
->class >> 8) == PCI_CLASS_DISPLAY_VGA
)
1072 device_remove_file(&pdev
->dev
, &vga_attr
);
1075 sysfs_remove_bin_file(&pdev
->dev
.kobj
, pdev
->rom_attr
);
1076 kfree(pdev
->rom_attr
);
1077 pdev
->rom_attr
= NULL
;
1080 pci_remove_resource_files(pdev
);
1082 if (pdev
->cfg_size
< PCI_CFG_SPACE_EXP_SIZE
)
1083 sysfs_remove_bin_file(&pdev
->dev
.kobj
, &pci_config_attr
);
1085 sysfs_remove_bin_file(&pdev
->dev
.kobj
, &pcie_config_attr
);
1090 static void pci_remove_capabilities_sysfs(struct pci_dev
*dev
)
1092 if (dev
->vpd
&& dev
->vpd
->attr
) {
1093 sysfs_remove_bin_file(&dev
->dev
.kobj
, dev
->vpd
->attr
);
1094 kfree(dev
->vpd
->attr
);
1097 pcie_aspm_remove_sysfs_dev_files(dev
);
1098 if (dev
->reset_fn
) {
1099 device_remove_file(&dev
->dev
, &reset_attr
);
1105 * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
1106 * @pdev: device whose entries we should free
1108 * Cleanup when @pdev is removed from sysfs.
1110 void pci_remove_sysfs_dev_files(struct pci_dev
*pdev
)
1114 if (!sysfs_initialized
)
1117 pci_remove_capabilities_sysfs(pdev
);
1119 if (pdev
->cfg_size
< PCI_CFG_SPACE_EXP_SIZE
)
1120 sysfs_remove_bin_file(&pdev
->dev
.kobj
, &pci_config_attr
);
1122 sysfs_remove_bin_file(&pdev
->dev
.kobj
, &pcie_config_attr
);
1124 pci_remove_resource_files(pdev
);
1126 if (pci_resource_len(pdev
, PCI_ROM_RESOURCE
))
1127 rom_size
= pci_resource_len(pdev
, PCI_ROM_RESOURCE
);
1128 else if (pdev
->resource
[PCI_ROM_RESOURCE
].flags
& IORESOURCE_ROM_SHADOW
)
1131 if (rom_size
&& pdev
->rom_attr
) {
1132 sysfs_remove_bin_file(&pdev
->dev
.kobj
, pdev
->rom_attr
);
1133 kfree(pdev
->rom_attr
);
1137 static int __init
pci_sysfs_init(void)
1139 struct pci_dev
*pdev
= NULL
;
1142 sysfs_initialized
= 1;
1143 for_each_pci_dev(pdev
) {
1144 retval
= pci_create_sysfs_dev_files(pdev
);
1154 late_initcall(pci_sysfs_init
);