1 // SPDX-License-Identifier: GPL-2.0
3 * drivers/pci/pci-sysfs.c
5 * (C) Copyright 2002-2004 Greg Kroah-Hartman <greg@kroah.com>
6 * (C) Copyright 2002-2004 IBM Corp.
7 * (C) Copyright 2003 Matthew Wilcox
8 * (C) Copyright 2003 Hewlett-Packard
9 * (C) Copyright 2004 Jon Smirl <jonsmirl@yahoo.com>
10 * (C) Copyright 2004 Silicon Graphics, Inc. Jesse Barnes <jbarnes@sgi.com>
12 * File attributes for PCI devices
14 * Modeled after usb's driverfs.c
19 #include <linux/kernel.h>
20 #include <linux/sched.h>
21 #include <linux/pci.h>
22 #include <linux/stat.h>
23 #include <linux/export.h>
24 #include <linux/topology.h>
27 #include <linux/capability.h>
28 #include <linux/security.h>
29 #include <linux/pci-aspm.h>
30 #include <linux/slab.h>
31 #include <linux/vgaarb.h>
32 #include <linux/pm_runtime.h>
36 static int sysfs_initialized
; /* = 0 */
38 /* show configuration fields */
39 #define pci_config_attr(field, format_string) \
41 field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
43 struct pci_dev *pdev; \
45 pdev = to_pci_dev(dev); \
46 return sprintf(buf, format_string, pdev->field); \
48 static DEVICE_ATTR_RO(field)
50 pci_config_attr(vendor
, "0x%04x\n");
51 pci_config_attr(device
, "0x%04x\n");
52 pci_config_attr(subsystem_vendor
, "0x%04x\n");
53 pci_config_attr(subsystem_device
, "0x%04x\n");
54 pci_config_attr(revision
, "0x%02x\n");
55 pci_config_attr(class, "0x%06x\n");
56 pci_config_attr(irq
, "%u\n");
58 static ssize_t
broken_parity_status_show(struct device
*dev
,
59 struct device_attribute
*attr
,
62 struct pci_dev
*pdev
= to_pci_dev(dev
);
63 return sprintf(buf
, "%u\n", pdev
->broken_parity_status
);
66 static ssize_t
broken_parity_status_store(struct device
*dev
,
67 struct device_attribute
*attr
,
68 const char *buf
, size_t count
)
70 struct pci_dev
*pdev
= to_pci_dev(dev
);
73 if (kstrtoul(buf
, 0, &val
) < 0)
76 pdev
->broken_parity_status
= !!val
;
80 static DEVICE_ATTR_RW(broken_parity_status
);
82 static ssize_t
pci_dev_show_local_cpu(struct device
*dev
, bool list
,
83 struct device_attribute
*attr
, char *buf
)
85 const struct cpumask
*mask
;
88 mask
= (dev_to_node(dev
) == -1) ? cpu_online_mask
:
89 cpumask_of_node(dev_to_node(dev
));
91 mask
= cpumask_of_pcibus(to_pci_dev(dev
)->bus
);
93 return cpumap_print_to_pagebuf(list
, buf
, mask
);
96 static ssize_t
local_cpus_show(struct device
*dev
,
97 struct device_attribute
*attr
, char *buf
)
99 return pci_dev_show_local_cpu(dev
, false, attr
, buf
);
101 static DEVICE_ATTR_RO(local_cpus
);
103 static ssize_t
local_cpulist_show(struct device
*dev
,
104 struct device_attribute
*attr
, char *buf
)
106 return pci_dev_show_local_cpu(dev
, true, attr
, buf
);
108 static DEVICE_ATTR_RO(local_cpulist
);
111 * PCI Bus Class Devices
113 static ssize_t
cpuaffinity_show(struct device
*dev
,
114 struct device_attribute
*attr
, char *buf
)
116 const struct cpumask
*cpumask
= cpumask_of_pcibus(to_pci_bus(dev
));
118 return cpumap_print_to_pagebuf(false, buf
, cpumask
);
120 static DEVICE_ATTR_RO(cpuaffinity
);
122 static ssize_t
cpulistaffinity_show(struct device
*dev
,
123 struct device_attribute
*attr
, char *buf
)
125 const struct cpumask
*cpumask
= cpumask_of_pcibus(to_pci_bus(dev
));
127 return cpumap_print_to_pagebuf(true, buf
, cpumask
);
129 static DEVICE_ATTR_RO(cpulistaffinity
);
132 static ssize_t
resource_show(struct device
*dev
, struct device_attribute
*attr
,
135 struct pci_dev
*pci_dev
= to_pci_dev(dev
);
139 resource_size_t start
, end
;
141 if (pci_dev
->subordinate
)
142 max
= DEVICE_COUNT_RESOURCE
;
144 max
= PCI_BRIDGE_RESOURCES
;
146 for (i
= 0; i
< max
; i
++) {
147 struct resource
*res
= &pci_dev
->resource
[i
];
148 pci_resource_to_user(pci_dev
, i
, res
, &start
, &end
);
149 str
+= sprintf(str
, "0x%016llx 0x%016llx 0x%016llx\n",
150 (unsigned long long)start
,
151 (unsigned long long)end
,
152 (unsigned long long)res
->flags
);
156 static DEVICE_ATTR_RO(resource
);
158 static ssize_t
max_link_speed_show(struct device
*dev
,
159 struct device_attribute
*attr
, char *buf
)
161 struct pci_dev
*pci_dev
= to_pci_dev(dev
);
166 err
= pcie_capability_read_dword(pci_dev
, PCI_EXP_LNKCAP
, &linkcap
);
170 switch (linkcap
& PCI_EXP_LNKCAP_SLS
) {
171 case PCI_EXP_LNKCAP_SLS_16_0GB
:
174 case PCI_EXP_LNKCAP_SLS_8_0GB
:
177 case PCI_EXP_LNKCAP_SLS_5_0GB
:
180 case PCI_EXP_LNKCAP_SLS_2_5GB
:
184 speed
= "Unknown speed";
187 return sprintf(buf
, "%s\n", speed
);
189 static DEVICE_ATTR_RO(max_link_speed
);
191 static ssize_t
max_link_width_show(struct device
*dev
,
192 struct device_attribute
*attr
, char *buf
)
194 struct pci_dev
*pci_dev
= to_pci_dev(dev
);
198 err
= pcie_capability_read_dword(pci_dev
, PCI_EXP_LNKCAP
, &linkcap
);
202 return sprintf(buf
, "%u\n", (linkcap
& PCI_EXP_LNKCAP_MLW
) >> 4);
204 static DEVICE_ATTR_RO(max_link_width
);
206 static ssize_t
current_link_speed_show(struct device
*dev
,
207 struct device_attribute
*attr
, char *buf
)
209 struct pci_dev
*pci_dev
= to_pci_dev(dev
);
214 err
= pcie_capability_read_word(pci_dev
, PCI_EXP_LNKSTA
, &linkstat
);
218 switch (linkstat
& PCI_EXP_LNKSTA_CLS
) {
219 case PCI_EXP_LNKSTA_CLS_16_0GB
:
222 case PCI_EXP_LNKSTA_CLS_8_0GB
:
225 case PCI_EXP_LNKSTA_CLS_5_0GB
:
228 case PCI_EXP_LNKSTA_CLS_2_5GB
:
232 speed
= "Unknown speed";
235 return sprintf(buf
, "%s\n", speed
);
237 static DEVICE_ATTR_RO(current_link_speed
);
239 static ssize_t
current_link_width_show(struct device
*dev
,
240 struct device_attribute
*attr
, char *buf
)
242 struct pci_dev
*pci_dev
= to_pci_dev(dev
);
246 err
= pcie_capability_read_word(pci_dev
, PCI_EXP_LNKSTA
, &linkstat
);
250 return sprintf(buf
, "%u\n",
251 (linkstat
& PCI_EXP_LNKSTA_NLW
) >> PCI_EXP_LNKSTA_NLW_SHIFT
);
253 static DEVICE_ATTR_RO(current_link_width
);
255 static ssize_t
secondary_bus_number_show(struct device
*dev
,
256 struct device_attribute
*attr
,
259 struct pci_dev
*pci_dev
= to_pci_dev(dev
);
263 err
= pci_read_config_byte(pci_dev
, PCI_SECONDARY_BUS
, &sec_bus
);
267 return sprintf(buf
, "%u\n", sec_bus
);
269 static DEVICE_ATTR_RO(secondary_bus_number
);
271 static ssize_t
subordinate_bus_number_show(struct device
*dev
,
272 struct device_attribute
*attr
,
275 struct pci_dev
*pci_dev
= to_pci_dev(dev
);
279 err
= pci_read_config_byte(pci_dev
, PCI_SUBORDINATE_BUS
, &sub_bus
);
283 return sprintf(buf
, "%u\n", sub_bus
);
285 static DEVICE_ATTR_RO(subordinate_bus_number
);
287 static ssize_t
modalias_show(struct device
*dev
, struct device_attribute
*attr
,
290 struct pci_dev
*pci_dev
= to_pci_dev(dev
);
292 return sprintf(buf
, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02X\n",
293 pci_dev
->vendor
, pci_dev
->device
,
294 pci_dev
->subsystem_vendor
, pci_dev
->subsystem_device
,
295 (u8
)(pci_dev
->class >> 16), (u8
)(pci_dev
->class >> 8),
296 (u8
)(pci_dev
->class));
298 static DEVICE_ATTR_RO(modalias
);
300 static ssize_t
enable_store(struct device
*dev
, struct device_attribute
*attr
,
301 const char *buf
, size_t count
)
303 struct pci_dev
*pdev
= to_pci_dev(dev
);
305 ssize_t result
= kstrtoul(buf
, 0, &val
);
310 /* this can crash the machine when done on the "wrong" device */
311 if (!capable(CAP_SYS_ADMIN
))
315 if (pci_is_enabled(pdev
))
316 pci_disable_device(pdev
);
320 result
= pci_enable_device(pdev
);
322 return result
< 0 ? result
: count
;
325 static ssize_t
enable_show(struct device
*dev
, struct device_attribute
*attr
,
328 struct pci_dev
*pdev
;
330 pdev
= to_pci_dev(dev
);
331 return sprintf(buf
, "%u\n", atomic_read(&pdev
->enable_cnt
));
333 static DEVICE_ATTR_RW(enable
);
336 static ssize_t
numa_node_store(struct device
*dev
,
337 struct device_attribute
*attr
, const char *buf
,
340 struct pci_dev
*pdev
= to_pci_dev(dev
);
343 if (!capable(CAP_SYS_ADMIN
))
346 ret
= kstrtoint(buf
, 0, &node
);
350 if ((node
< 0 && node
!= NUMA_NO_NODE
) || node
>= MAX_NUMNODES
)
353 if (node
!= NUMA_NO_NODE
&& !node_online(node
))
356 add_taint(TAINT_FIRMWARE_WORKAROUND
, LOCKDEP_STILL_OK
);
357 dev_alert(&pdev
->dev
, FW_BUG
"Overriding NUMA node to %d. Contact your vendor for updates.",
360 dev
->numa_node
= node
;
364 static ssize_t
numa_node_show(struct device
*dev
, struct device_attribute
*attr
,
367 return sprintf(buf
, "%d\n", dev
->numa_node
);
369 static DEVICE_ATTR_RW(numa_node
);
372 static ssize_t
dma_mask_bits_show(struct device
*dev
,
373 struct device_attribute
*attr
, char *buf
)
375 struct pci_dev
*pdev
= to_pci_dev(dev
);
377 return sprintf(buf
, "%d\n", fls64(pdev
->dma_mask
));
379 static DEVICE_ATTR_RO(dma_mask_bits
);
381 static ssize_t
consistent_dma_mask_bits_show(struct device
*dev
,
382 struct device_attribute
*attr
,
385 return sprintf(buf
, "%d\n", fls64(dev
->coherent_dma_mask
));
387 static DEVICE_ATTR_RO(consistent_dma_mask_bits
);
389 static ssize_t
msi_bus_show(struct device
*dev
, struct device_attribute
*attr
,
392 struct pci_dev
*pdev
= to_pci_dev(dev
);
393 struct pci_bus
*subordinate
= pdev
->subordinate
;
395 return sprintf(buf
, "%u\n", subordinate
?
396 !(subordinate
->bus_flags
& PCI_BUS_FLAGS_NO_MSI
)
400 static ssize_t
msi_bus_store(struct device
*dev
, struct device_attribute
*attr
,
401 const char *buf
, size_t count
)
403 struct pci_dev
*pdev
= to_pci_dev(dev
);
404 struct pci_bus
*subordinate
= pdev
->subordinate
;
407 if (kstrtoul(buf
, 0, &val
) < 0)
410 if (!capable(CAP_SYS_ADMIN
))
414 * "no_msi" and "bus_flags" only affect what happens when a driver
415 * requests MSI or MSI-X. They don't affect any drivers that have
416 * already requested MSI or MSI-X.
420 dev_info(&pdev
->dev
, "MSI/MSI-X %s for future drivers\n",
421 val
? "allowed" : "disallowed");
426 subordinate
->bus_flags
&= ~PCI_BUS_FLAGS_NO_MSI
;
428 subordinate
->bus_flags
|= PCI_BUS_FLAGS_NO_MSI
;
430 dev_info(&subordinate
->dev
, "MSI/MSI-X %s for future drivers of devices on this bus\n",
431 val
? "allowed" : "disallowed");
434 static DEVICE_ATTR_RW(msi_bus
);
436 static ssize_t
bus_rescan_store(struct bus_type
*bus
, const char *buf
,
440 struct pci_bus
*b
= NULL
;
442 if (kstrtoul(buf
, 0, &val
) < 0)
446 pci_lock_rescan_remove();
447 while ((b
= pci_find_next_bus(b
)) != NULL
)
449 pci_unlock_rescan_remove();
453 static BUS_ATTR(rescan
, (S_IWUSR
|S_IWGRP
), NULL
, bus_rescan_store
);
455 static struct attribute
*pci_bus_attrs
[] = {
456 &bus_attr_rescan
.attr
,
460 static const struct attribute_group pci_bus_group
= {
461 .attrs
= pci_bus_attrs
,
464 const struct attribute_group
*pci_bus_groups
[] = {
469 static ssize_t
dev_rescan_store(struct device
*dev
,
470 struct device_attribute
*attr
, const char *buf
,
474 struct pci_dev
*pdev
= to_pci_dev(dev
);
476 if (kstrtoul(buf
, 0, &val
) < 0)
480 pci_lock_rescan_remove();
481 pci_rescan_bus(pdev
->bus
);
482 pci_unlock_rescan_remove();
486 static struct device_attribute dev_rescan_attr
= __ATTR(rescan
,
488 NULL
, dev_rescan_store
);
490 static ssize_t
remove_store(struct device
*dev
, struct device_attribute
*attr
,
491 const char *buf
, size_t count
)
495 if (kstrtoul(buf
, 0, &val
) < 0)
498 if (val
&& device_remove_file_self(dev
, attr
))
499 pci_stop_and_remove_bus_device_locked(to_pci_dev(dev
));
502 static struct device_attribute dev_remove_attr
= __ATTR(remove
,
506 static ssize_t
dev_bus_rescan_store(struct device
*dev
,
507 struct device_attribute
*attr
,
508 const char *buf
, size_t count
)
511 struct pci_bus
*bus
= to_pci_bus(dev
);
513 if (kstrtoul(buf
, 0, &val
) < 0)
517 pci_lock_rescan_remove();
518 if (!pci_is_root_bus(bus
) && list_empty(&bus
->devices
))
519 pci_rescan_bus_bridge_resize(bus
->self
);
522 pci_unlock_rescan_remove();
526 static DEVICE_ATTR(rescan
, (S_IWUSR
|S_IWGRP
), NULL
, dev_bus_rescan_store
);
528 #if defined(CONFIG_PM) && defined(CONFIG_ACPI)
529 static ssize_t
d3cold_allowed_store(struct device
*dev
,
530 struct device_attribute
*attr
,
531 const char *buf
, size_t count
)
533 struct pci_dev
*pdev
= to_pci_dev(dev
);
536 if (kstrtoul(buf
, 0, &val
) < 0)
539 pdev
->d3cold_allowed
= !!val
;
540 if (pdev
->d3cold_allowed
)
541 pci_d3cold_enable(pdev
);
543 pci_d3cold_disable(pdev
);
545 pm_runtime_resume(dev
);
550 static ssize_t
d3cold_allowed_show(struct device
*dev
,
551 struct device_attribute
*attr
, char *buf
)
553 struct pci_dev
*pdev
= to_pci_dev(dev
);
554 return sprintf(buf
, "%u\n", pdev
->d3cold_allowed
);
556 static DEVICE_ATTR_RW(d3cold_allowed
);
560 static ssize_t
devspec_show(struct device
*dev
,
561 struct device_attribute
*attr
, char *buf
)
563 struct pci_dev
*pdev
= to_pci_dev(dev
);
564 struct device_node
*np
= pci_device_to_OF_node(pdev
);
568 return sprintf(buf
, "%pOF", np
);
570 static DEVICE_ATTR_RO(devspec
);
573 #ifdef CONFIG_PCI_IOV
574 static ssize_t
sriov_totalvfs_show(struct device
*dev
,
575 struct device_attribute
*attr
,
578 struct pci_dev
*pdev
= to_pci_dev(dev
);
580 return sprintf(buf
, "%u\n", pci_sriov_get_totalvfs(pdev
));
584 static ssize_t
sriov_numvfs_show(struct device
*dev
,
585 struct device_attribute
*attr
,
588 struct pci_dev
*pdev
= to_pci_dev(dev
);
590 return sprintf(buf
, "%u\n", pdev
->sriov
->num_VFs
);
594 * num_vfs > 0; number of VFs to enable
595 * num_vfs = 0; disable all VFs
597 * Note: SRIOV spec doesn't allow partial VF
598 * disable, so it's all or none.
600 static ssize_t
sriov_numvfs_store(struct device
*dev
,
601 struct device_attribute
*attr
,
602 const char *buf
, size_t count
)
604 struct pci_dev
*pdev
= to_pci_dev(dev
);
608 ret
= kstrtou16(buf
, 0, &num_vfs
);
612 if (num_vfs
> pci_sriov_get_totalvfs(pdev
))
615 device_lock(&pdev
->dev
);
617 if (num_vfs
== pdev
->sriov
->num_VFs
)
620 /* is PF driver loaded w/callback */
621 if (!pdev
->driver
|| !pdev
->driver
->sriov_configure
) {
622 dev_info(&pdev
->dev
, "Driver doesn't support SRIOV configuration via sysfs\n");
629 ret
= pdev
->driver
->sriov_configure(pdev
, 0);
634 if (pdev
->sriov
->num_VFs
) {
635 dev_warn(&pdev
->dev
, "%d VFs already enabled. Disable before enabling %d VFs\n",
636 pdev
->sriov
->num_VFs
, num_vfs
);
641 ret
= pdev
->driver
->sriov_configure(pdev
, num_vfs
);
646 dev_warn(&pdev
->dev
, "%d VFs requested; only %d enabled\n",
650 device_unlock(&pdev
->dev
);
658 static ssize_t
sriov_offset_show(struct device
*dev
,
659 struct device_attribute
*attr
,
662 struct pci_dev
*pdev
= to_pci_dev(dev
);
664 return sprintf(buf
, "%u\n", pdev
->sriov
->offset
);
667 static ssize_t
sriov_stride_show(struct device
*dev
,
668 struct device_attribute
*attr
,
671 struct pci_dev
*pdev
= to_pci_dev(dev
);
673 return sprintf(buf
, "%u\n", pdev
->sriov
->stride
);
676 static ssize_t
sriov_vf_device_show(struct device
*dev
,
677 struct device_attribute
*attr
,
680 struct pci_dev
*pdev
= to_pci_dev(dev
);
682 return sprintf(buf
, "%x\n", pdev
->sriov
->vf_device
);
685 static ssize_t
sriov_drivers_autoprobe_show(struct device
*dev
,
686 struct device_attribute
*attr
,
689 struct pci_dev
*pdev
= to_pci_dev(dev
);
691 return sprintf(buf
, "%u\n", pdev
->sriov
->drivers_autoprobe
);
694 static ssize_t
sriov_drivers_autoprobe_store(struct device
*dev
,
695 struct device_attribute
*attr
,
696 const char *buf
, size_t count
)
698 struct pci_dev
*pdev
= to_pci_dev(dev
);
699 bool drivers_autoprobe
;
701 if (kstrtobool(buf
, &drivers_autoprobe
) < 0)
704 pdev
->sriov
->drivers_autoprobe
= drivers_autoprobe
;
709 static struct device_attribute sriov_totalvfs_attr
= __ATTR_RO(sriov_totalvfs
);
710 static struct device_attribute sriov_numvfs_attr
=
711 __ATTR(sriov_numvfs
, (S_IRUGO
|S_IWUSR
|S_IWGRP
),
712 sriov_numvfs_show
, sriov_numvfs_store
);
713 static struct device_attribute sriov_offset_attr
= __ATTR_RO(sriov_offset
);
714 static struct device_attribute sriov_stride_attr
= __ATTR_RO(sriov_stride
);
715 static struct device_attribute sriov_vf_device_attr
= __ATTR_RO(sriov_vf_device
);
716 static struct device_attribute sriov_drivers_autoprobe_attr
=
717 __ATTR(sriov_drivers_autoprobe
, (S_IRUGO
|S_IWUSR
|S_IWGRP
),
718 sriov_drivers_autoprobe_show
, sriov_drivers_autoprobe_store
);
719 #endif /* CONFIG_PCI_IOV */
721 static ssize_t
driver_override_store(struct device
*dev
,
722 struct device_attribute
*attr
,
723 const char *buf
, size_t count
)
725 struct pci_dev
*pdev
= to_pci_dev(dev
);
726 char *driver_override
, *old
, *cp
;
728 /* We need to keep extra room for a newline */
729 if (count
>= (PAGE_SIZE
- 1))
732 driver_override
= kstrndup(buf
, count
, GFP_KERNEL
);
733 if (!driver_override
)
736 cp
= strchr(driver_override
, '\n');
741 old
= pdev
->driver_override
;
742 if (strlen(driver_override
)) {
743 pdev
->driver_override
= driver_override
;
745 kfree(driver_override
);
746 pdev
->driver_override
= NULL
;
755 static ssize_t
driver_override_show(struct device
*dev
,
756 struct device_attribute
*attr
, char *buf
)
758 struct pci_dev
*pdev
= to_pci_dev(dev
);
762 len
= snprintf(buf
, PAGE_SIZE
, "%s\n", pdev
->driver_override
);
766 static DEVICE_ATTR_RW(driver_override
);
768 static struct attribute
*pci_dev_attrs
[] = {
769 &dev_attr_resource
.attr
,
770 &dev_attr_vendor
.attr
,
771 &dev_attr_device
.attr
,
772 &dev_attr_subsystem_vendor
.attr
,
773 &dev_attr_subsystem_device
.attr
,
774 &dev_attr_revision
.attr
,
775 &dev_attr_class
.attr
,
777 &dev_attr_local_cpus
.attr
,
778 &dev_attr_local_cpulist
.attr
,
779 &dev_attr_modalias
.attr
,
781 &dev_attr_numa_node
.attr
,
783 &dev_attr_dma_mask_bits
.attr
,
784 &dev_attr_consistent_dma_mask_bits
.attr
,
785 &dev_attr_enable
.attr
,
786 &dev_attr_broken_parity_status
.attr
,
787 &dev_attr_msi_bus
.attr
,
788 #if defined(CONFIG_PM) && defined(CONFIG_ACPI)
789 &dev_attr_d3cold_allowed
.attr
,
792 &dev_attr_devspec
.attr
,
794 &dev_attr_driver_override
.attr
,
798 static struct attribute
*pci_bridge_attrs
[] = {
799 &dev_attr_subordinate_bus_number
.attr
,
800 &dev_attr_secondary_bus_number
.attr
,
804 static struct attribute
*pcie_dev_attrs
[] = {
805 &dev_attr_current_link_speed
.attr
,
806 &dev_attr_current_link_width
.attr
,
807 &dev_attr_max_link_width
.attr
,
808 &dev_attr_max_link_speed
.attr
,
812 static struct attribute
*pcibus_attrs
[] = {
813 &dev_attr_rescan
.attr
,
814 &dev_attr_cpuaffinity
.attr
,
815 &dev_attr_cpulistaffinity
.attr
,
819 static const struct attribute_group pcibus_group
= {
820 .attrs
= pcibus_attrs
,
823 const struct attribute_group
*pcibus_groups
[] = {
828 static ssize_t
boot_vga_show(struct device
*dev
, struct device_attribute
*attr
,
831 struct pci_dev
*pdev
= to_pci_dev(dev
);
832 struct pci_dev
*vga_dev
= vga_default_device();
835 return sprintf(buf
, "%u\n", (pdev
== vga_dev
));
837 return sprintf(buf
, "%u\n",
838 !!(pdev
->resource
[PCI_ROM_RESOURCE
].flags
&
839 IORESOURCE_ROM_SHADOW
));
841 static struct device_attribute vga_attr
= __ATTR_RO(boot_vga
);
843 static ssize_t
pci_read_config(struct file
*filp
, struct kobject
*kobj
,
844 struct bin_attribute
*bin_attr
, char *buf
,
845 loff_t off
, size_t count
)
847 struct pci_dev
*dev
= to_pci_dev(kobj_to_dev(kobj
));
848 unsigned int size
= 64;
849 loff_t init_off
= off
;
850 u8
*data
= (u8
*) buf
;
852 /* Several chips lock up trying to read undefined config space */
853 if (file_ns_capable(filp
, &init_user_ns
, CAP_SYS_ADMIN
))
854 size
= dev
->cfg_size
;
855 else if (dev
->hdr_type
== PCI_HEADER_TYPE_CARDBUS
)
860 if (off
+ count
> size
) {
867 pci_config_pm_runtime_get(dev
);
869 if ((off
& 1) && size
) {
871 pci_user_read_config_byte(dev
, off
, &val
);
872 data
[off
- init_off
] = val
;
877 if ((off
& 3) && size
> 2) {
879 pci_user_read_config_word(dev
, off
, &val
);
880 data
[off
- init_off
] = val
& 0xff;
881 data
[off
- init_off
+ 1] = (val
>> 8) & 0xff;
888 pci_user_read_config_dword(dev
, off
, &val
);
889 data
[off
- init_off
] = val
& 0xff;
890 data
[off
- init_off
+ 1] = (val
>> 8) & 0xff;
891 data
[off
- init_off
+ 2] = (val
>> 16) & 0xff;
892 data
[off
- init_off
+ 3] = (val
>> 24) & 0xff;
899 pci_user_read_config_word(dev
, off
, &val
);
900 data
[off
- init_off
] = val
& 0xff;
901 data
[off
- init_off
+ 1] = (val
>> 8) & 0xff;
908 pci_user_read_config_byte(dev
, off
, &val
);
909 data
[off
- init_off
] = val
;
914 pci_config_pm_runtime_put(dev
);
919 static ssize_t
pci_write_config(struct file
*filp
, struct kobject
*kobj
,
920 struct bin_attribute
*bin_attr
, char *buf
,
921 loff_t off
, size_t count
)
923 struct pci_dev
*dev
= to_pci_dev(kobj_to_dev(kobj
));
924 unsigned int size
= count
;
925 loff_t init_off
= off
;
926 u8
*data
= (u8
*) buf
;
928 if (kernel_is_locked_down("Direct PCI access"))
931 if (off
> dev
->cfg_size
)
933 if (off
+ count
> dev
->cfg_size
) {
934 size
= dev
->cfg_size
- off
;
938 pci_config_pm_runtime_get(dev
);
940 if ((off
& 1) && size
) {
941 pci_user_write_config_byte(dev
, off
, data
[off
- init_off
]);
946 if ((off
& 3) && size
> 2) {
947 u16 val
= data
[off
- init_off
];
948 val
|= (u16
) data
[off
- init_off
+ 1] << 8;
949 pci_user_write_config_word(dev
, off
, val
);
955 u32 val
= data
[off
- init_off
];
956 val
|= (u32
) data
[off
- init_off
+ 1] << 8;
957 val
|= (u32
) data
[off
- init_off
+ 2] << 16;
958 val
|= (u32
) data
[off
- init_off
+ 3] << 24;
959 pci_user_write_config_dword(dev
, off
, val
);
965 u16 val
= data
[off
- init_off
];
966 val
|= (u16
) data
[off
- init_off
+ 1] << 8;
967 pci_user_write_config_word(dev
, off
, val
);
973 pci_user_write_config_byte(dev
, off
, data
[off
- init_off
]);
978 pci_config_pm_runtime_put(dev
);
983 static ssize_t
read_vpd_attr(struct file
*filp
, struct kobject
*kobj
,
984 struct bin_attribute
*bin_attr
, char *buf
,
985 loff_t off
, size_t count
)
987 struct pci_dev
*dev
= to_pci_dev(kobj_to_dev(kobj
));
989 if (bin_attr
->size
> 0) {
990 if (off
> bin_attr
->size
)
992 else if (count
> bin_attr
->size
- off
)
993 count
= bin_attr
->size
- off
;
996 return pci_read_vpd(dev
, off
, count
, buf
);
999 static ssize_t
write_vpd_attr(struct file
*filp
, struct kobject
*kobj
,
1000 struct bin_attribute
*bin_attr
, char *buf
,
1001 loff_t off
, size_t count
)
1003 struct pci_dev
*dev
= to_pci_dev(kobj_to_dev(kobj
));
1005 if (bin_attr
->size
> 0) {
1006 if (off
> bin_attr
->size
)
1008 else if (count
> bin_attr
->size
- off
)
1009 count
= bin_attr
->size
- off
;
1012 return pci_write_vpd(dev
, off
, count
, buf
);
1015 #ifdef HAVE_PCI_LEGACY
1017 * pci_read_legacy_io - read byte(s) from legacy I/O port space
1018 * @filp: open sysfs file
1019 * @kobj: kobject corresponding to file to read from
1020 * @bin_attr: struct bin_attribute for this file
1021 * @buf: buffer to store results
1022 * @off: offset into legacy I/O port space
1023 * @count: number of bytes to read
1025 * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
1026 * callback routine (pci_legacy_read).
1028 static ssize_t
pci_read_legacy_io(struct file
*filp
, struct kobject
*kobj
,
1029 struct bin_attribute
*bin_attr
, char *buf
,
1030 loff_t off
, size_t count
)
1032 struct pci_bus
*bus
= to_pci_bus(kobj_to_dev(kobj
));
1034 /* Only support 1, 2 or 4 byte accesses */
1035 if (count
!= 1 && count
!= 2 && count
!= 4)
1038 return pci_legacy_read(bus
, off
, (u32
*)buf
, count
);
1042 * pci_write_legacy_io - write byte(s) to legacy I/O port space
1043 * @filp: open sysfs file
1044 * @kobj: kobject corresponding to file to read from
1045 * @bin_attr: struct bin_attribute for this file
1046 * @buf: buffer containing value to be written
1047 * @off: offset into legacy I/O port space
1048 * @count: number of bytes to write
1050 * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
1051 * callback routine (pci_legacy_write).
1053 static ssize_t
pci_write_legacy_io(struct file
*filp
, struct kobject
*kobj
,
1054 struct bin_attribute
*bin_attr
, char *buf
,
1055 loff_t off
, size_t count
)
1057 struct pci_bus
*bus
= to_pci_bus(kobj_to_dev(kobj
));
1059 /* Only support 1, 2 or 4 byte accesses */
1060 if (count
!= 1 && count
!= 2 && count
!= 4)
1063 return pci_legacy_write(bus
, off
, *(u32
*)buf
, count
);
1067 * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
1068 * @filp: open sysfs file
1069 * @kobj: kobject corresponding to device to be mapped
1070 * @attr: struct bin_attribute for this file
1071 * @vma: struct vm_area_struct passed to mmap
1073 * Uses an arch specific callback, pci_mmap_legacy_mem_page_range, to mmap
1074 * legacy memory space (first meg of bus space) into application virtual
1077 static int pci_mmap_legacy_mem(struct file
*filp
, struct kobject
*kobj
,
1078 struct bin_attribute
*attr
,
1079 struct vm_area_struct
*vma
)
1081 struct pci_bus
*bus
= to_pci_bus(kobj_to_dev(kobj
));
1083 return pci_mmap_legacy_page_range(bus
, vma
, pci_mmap_mem
);
1087 * pci_mmap_legacy_io - map legacy PCI IO into user memory space
1088 * @filp: open sysfs file
1089 * @kobj: kobject corresponding to device to be mapped
1090 * @attr: struct bin_attribute for this file
1091 * @vma: struct vm_area_struct passed to mmap
1093 * Uses an arch specific callback, pci_mmap_legacy_io_page_range, to mmap
1094 * legacy IO space (first meg of bus space) into application virtual
1095 * memory space. Returns -ENOSYS if the operation isn't supported
1097 static int pci_mmap_legacy_io(struct file
*filp
, struct kobject
*kobj
,
1098 struct bin_attribute
*attr
,
1099 struct vm_area_struct
*vma
)
1101 struct pci_bus
*bus
= to_pci_bus(kobj_to_dev(kobj
));
1103 return pci_mmap_legacy_page_range(bus
, vma
, pci_mmap_io
);
1107 * pci_adjust_legacy_attr - adjustment of legacy file attributes
1108 * @b: bus to create files under
1109 * @mmap_type: I/O port or memory
1111 * Stub implementation. Can be overridden by arch if necessary.
1113 void __weak
pci_adjust_legacy_attr(struct pci_bus
*b
,
1114 enum pci_mmap_state mmap_type
)
1119 * pci_create_legacy_files - create legacy I/O port and memory files
1120 * @b: bus to create files under
1122 * Some platforms allow access to legacy I/O port and ISA memory space on
1123 * a per-bus basis. This routine creates the files and ties them into
1124 * their associated read, write and mmap files from pci-sysfs.c
1126 * On error unwind, but don't propagate the error to the caller
1127 * as it is ok to set up the PCI bus without these files.
1129 void pci_create_legacy_files(struct pci_bus
*b
)
1133 b
->legacy_io
= kzalloc(sizeof(struct bin_attribute
) * 2,
1138 sysfs_bin_attr_init(b
->legacy_io
);
1139 b
->legacy_io
->attr
.name
= "legacy_io";
1140 b
->legacy_io
->size
= 0xffff;
1141 b
->legacy_io
->attr
.mode
= S_IRUSR
| S_IWUSR
;
1142 b
->legacy_io
->read
= pci_read_legacy_io
;
1143 b
->legacy_io
->write
= pci_write_legacy_io
;
1144 b
->legacy_io
->mmap
= pci_mmap_legacy_io
;
1145 pci_adjust_legacy_attr(b
, pci_mmap_io
);
1146 error
= device_create_bin_file(&b
->dev
, b
->legacy_io
);
1150 /* Allocated above after the legacy_io struct */
1151 b
->legacy_mem
= b
->legacy_io
+ 1;
1152 sysfs_bin_attr_init(b
->legacy_mem
);
1153 b
->legacy_mem
->attr
.name
= "legacy_mem";
1154 b
->legacy_mem
->size
= 1024*1024;
1155 b
->legacy_mem
->attr
.mode
= S_IRUSR
| S_IWUSR
;
1156 b
->legacy_mem
->mmap
= pci_mmap_legacy_mem
;
1157 pci_adjust_legacy_attr(b
, pci_mmap_mem
);
1158 error
= device_create_bin_file(&b
->dev
, b
->legacy_mem
);
1160 goto legacy_mem_err
;
1165 device_remove_bin_file(&b
->dev
, b
->legacy_io
);
1167 kfree(b
->legacy_io
);
1168 b
->legacy_io
= NULL
;
1170 printk(KERN_WARNING
"pci: warning: could not create legacy I/O port and ISA memory resources to sysfs\n");
1174 void pci_remove_legacy_files(struct pci_bus
*b
)
1177 device_remove_bin_file(&b
->dev
, b
->legacy_io
);
1178 device_remove_bin_file(&b
->dev
, b
->legacy_mem
);
1179 kfree(b
->legacy_io
); /* both are allocated here */
1182 #endif /* HAVE_PCI_LEGACY */
1184 #if defined(HAVE_PCI_MMAP) || defined(ARCH_GENERIC_PCI_MMAP_RESOURCE)
1186 int pci_mmap_fits(struct pci_dev
*pdev
, int resno
, struct vm_area_struct
*vma
,
1187 enum pci_mmap_api mmap_api
)
1189 unsigned long nr
, start
, size
;
1190 resource_size_t pci_start
= 0, pci_end
;
1192 if (pci_resource_len(pdev
, resno
) == 0)
1194 nr
= vma_pages(vma
);
1195 start
= vma
->vm_pgoff
;
1196 size
= ((pci_resource_len(pdev
, resno
) - 1) >> PAGE_SHIFT
) + 1;
1197 if (mmap_api
== PCI_MMAP_PROCFS
) {
1198 pci_resource_to_user(pdev
, resno
, &pdev
->resource
[resno
],
1199 &pci_start
, &pci_end
);
1200 pci_start
>>= PAGE_SHIFT
;
1202 if (start
>= pci_start
&& start
< pci_start
+ size
&&
1203 start
+ nr
<= pci_start
+ size
)
1209 * pci_mmap_resource - map a PCI resource into user memory space
1210 * @kobj: kobject for mapping
1211 * @attr: struct bin_attribute for the file being mapped
1212 * @vma: struct vm_area_struct passed into the mmap
1213 * @write_combine: 1 for write_combine mapping
1215 * Use the regular PCI mapping routines to map a PCI resource into userspace.
1217 static int pci_mmap_resource(struct kobject
*kobj
, struct bin_attribute
*attr
,
1218 struct vm_area_struct
*vma
, int write_combine
)
1220 struct pci_dev
*pdev
= to_pci_dev(kobj_to_dev(kobj
));
1221 int bar
= (unsigned long)attr
->private;
1222 enum pci_mmap_state mmap_type
;
1223 struct resource
*res
= &pdev
->resource
[bar
];
1225 if (kernel_is_locked_down("Direct PCI access"))
1228 if (res
->flags
& IORESOURCE_MEM
&& iomem_is_exclusive(res
->start
))
1231 if (!pci_mmap_fits(pdev
, bar
, vma
, PCI_MMAP_SYSFS
)) {
1232 WARN(1, "process \"%s\" tried to map 0x%08lx bytes at page 0x%08lx on %s BAR %d (start 0x%16Lx, size 0x%16Lx)\n",
1233 current
->comm
, vma
->vm_end
-vma
->vm_start
, vma
->vm_pgoff
,
1234 pci_name(pdev
), bar
,
1235 (u64
)pci_resource_start(pdev
, bar
),
1236 (u64
)pci_resource_len(pdev
, bar
));
1239 mmap_type
= res
->flags
& IORESOURCE_MEM
? pci_mmap_mem
: pci_mmap_io
;
1241 return pci_mmap_resource_range(pdev
, bar
, vma
, mmap_type
, write_combine
);
1244 static int pci_mmap_resource_uc(struct file
*filp
, struct kobject
*kobj
,
1245 struct bin_attribute
*attr
,
1246 struct vm_area_struct
*vma
)
1248 return pci_mmap_resource(kobj
, attr
, vma
, 0);
1251 static int pci_mmap_resource_wc(struct file
*filp
, struct kobject
*kobj
,
1252 struct bin_attribute
*attr
,
1253 struct vm_area_struct
*vma
)
1255 return pci_mmap_resource(kobj
, attr
, vma
, 1);
1258 static ssize_t
pci_resource_io(struct file
*filp
, struct kobject
*kobj
,
1259 struct bin_attribute
*attr
, char *buf
,
1260 loff_t off
, size_t count
, bool write
)
1262 struct pci_dev
*pdev
= to_pci_dev(kobj_to_dev(kobj
));
1263 int bar
= (unsigned long)attr
->private;
1264 unsigned long port
= off
;
1266 port
+= pci_resource_start(pdev
, bar
);
1268 if (port
> pci_resource_end(pdev
, bar
))
1271 if (port
+ count
- 1 > pci_resource_end(pdev
, bar
))
1277 outb(*(u8
*)buf
, port
);
1279 *(u8
*)buf
= inb(port
);
1283 outw(*(u16
*)buf
, port
);
1285 *(u16
*)buf
= inw(port
);
1289 outl(*(u32
*)buf
, port
);
1291 *(u32
*)buf
= inl(port
);
1297 static ssize_t
pci_read_resource_io(struct file
*filp
, struct kobject
*kobj
,
1298 struct bin_attribute
*attr
, char *buf
,
1299 loff_t off
, size_t count
)
1301 return pci_resource_io(filp
, kobj
, attr
, buf
, off
, count
, false);
1304 static ssize_t
pci_write_resource_io(struct file
*filp
, struct kobject
*kobj
,
1305 struct bin_attribute
*attr
, char *buf
,
1306 loff_t off
, size_t count
)
1308 if (kernel_is_locked_down("Direct PCI access"))
1311 return pci_resource_io(filp
, kobj
, attr
, buf
, off
, count
, true);
1315 * pci_remove_resource_files - cleanup resource files
1316 * @pdev: dev to cleanup
1318 * If we created resource files for @pdev, remove them from sysfs and
1319 * free their resources.
1321 static void pci_remove_resource_files(struct pci_dev
*pdev
)
1325 for (i
= 0; i
< PCI_ROM_RESOURCE
; i
++) {
1326 struct bin_attribute
*res_attr
;
1328 res_attr
= pdev
->res_attr
[i
];
1330 sysfs_remove_bin_file(&pdev
->dev
.kobj
, res_attr
);
1334 res_attr
= pdev
->res_attr_wc
[i
];
1336 sysfs_remove_bin_file(&pdev
->dev
.kobj
, res_attr
);
1342 static int pci_create_attr(struct pci_dev
*pdev
, int num
, int write_combine
)
1344 /* allocate attribute structure, piggyback attribute name */
1345 int name_len
= write_combine
? 13 : 10;
1346 struct bin_attribute
*res_attr
;
1347 char *res_attr_name
;
1350 res_attr
= kzalloc(sizeof(*res_attr
) + name_len
, GFP_ATOMIC
);
1354 res_attr_name
= (char *)(res_attr
+ 1);
1356 sysfs_bin_attr_init(res_attr
);
1357 if (write_combine
) {
1358 pdev
->res_attr_wc
[num
] = res_attr
;
1359 sprintf(res_attr_name
, "resource%d_wc", num
);
1360 res_attr
->mmap
= pci_mmap_resource_wc
;
1362 pdev
->res_attr
[num
] = res_attr
;
1363 sprintf(res_attr_name
, "resource%d", num
);
1364 if (pci_resource_flags(pdev
, num
) & IORESOURCE_IO
) {
1365 res_attr
->read
= pci_read_resource_io
;
1366 res_attr
->write
= pci_write_resource_io
;
1367 if (arch_can_pci_mmap_io())
1368 res_attr
->mmap
= pci_mmap_resource_uc
;
1370 res_attr
->mmap
= pci_mmap_resource_uc
;
1373 res_attr
->attr
.name
= res_attr_name
;
1374 res_attr
->attr
.mode
= S_IRUSR
| S_IWUSR
;
1375 res_attr
->size
= pci_resource_len(pdev
, num
);
1376 res_attr
->private = (void *)(unsigned long)num
;
1377 retval
= sysfs_create_bin_file(&pdev
->dev
.kobj
, res_attr
);
1385 * pci_create_resource_files - create resource files in sysfs for @dev
1386 * @pdev: dev in question
1388 * Walk the resources in @pdev creating files for each resource available.
1390 static int pci_create_resource_files(struct pci_dev
*pdev
)
1395 /* Expose the PCI resources from this device as files */
1396 for (i
= 0; i
< PCI_ROM_RESOURCE
; i
++) {
1398 /* skip empty resources */
1399 if (!pci_resource_len(pdev
, i
))
1402 retval
= pci_create_attr(pdev
, i
, 0);
1403 /* for prefetchable resources, create a WC mappable file */
1404 if (!retval
&& arch_can_pci_mmap_wc() &&
1405 pdev
->resource
[i
].flags
& IORESOURCE_PREFETCH
)
1406 retval
= pci_create_attr(pdev
, i
, 1);
1408 pci_remove_resource_files(pdev
);
1414 #else /* !HAVE_PCI_MMAP */
1415 int __weak
pci_create_resource_files(struct pci_dev
*dev
) { return 0; }
1416 void __weak
pci_remove_resource_files(struct pci_dev
*dev
) { return; }
1417 #endif /* HAVE_PCI_MMAP */
1420 * pci_write_rom - used to enable access to the PCI ROM display
1422 * @kobj: kernel object handle
1423 * @bin_attr: struct bin_attribute for this file
1426 * @count: number of byte in input
1428 * writing anything except 0 enables it
1430 static ssize_t
pci_write_rom(struct file
*filp
, struct kobject
*kobj
,
1431 struct bin_attribute
*bin_attr
, char *buf
,
1432 loff_t off
, size_t count
)
1434 struct pci_dev
*pdev
= to_pci_dev(kobj_to_dev(kobj
));
1436 if ((off
== 0) && (*buf
== '0') && (count
== 2))
1437 pdev
->rom_attr_enabled
= 0;
1439 pdev
->rom_attr_enabled
= 1;
1445 * pci_read_rom - read a PCI ROM
1447 * @kobj: kernel object handle
1448 * @bin_attr: struct bin_attribute for this file
1449 * @buf: where to put the data we read from the ROM
1451 * @count: number of bytes to read
1453 * Put @count bytes starting at @off into @buf from the ROM in the PCI
1454 * device corresponding to @kobj.
1456 static ssize_t
pci_read_rom(struct file
*filp
, struct kobject
*kobj
,
1457 struct bin_attribute
*bin_attr
, char *buf
,
1458 loff_t off
, size_t count
)
1460 struct pci_dev
*pdev
= to_pci_dev(kobj_to_dev(kobj
));
1464 if (!pdev
->rom_attr_enabled
)
1467 rom
= pci_map_rom(pdev
, &size
); /* size starts out as PCI window size */
1474 if (off
+ count
> size
)
1477 memcpy_fromio(buf
, rom
+ off
, count
);
1479 pci_unmap_rom(pdev
, rom
);
1484 static const struct bin_attribute pci_config_attr
= {
1487 .mode
= S_IRUGO
| S_IWUSR
,
1489 .size
= PCI_CFG_SPACE_SIZE
,
1490 .read
= pci_read_config
,
1491 .write
= pci_write_config
,
1494 static const struct bin_attribute pcie_config_attr
= {
1497 .mode
= S_IRUGO
| S_IWUSR
,
1499 .size
= PCI_CFG_SPACE_EXP_SIZE
,
1500 .read
= pci_read_config
,
1501 .write
= pci_write_config
,
1504 static ssize_t
reset_store(struct device
*dev
, struct device_attribute
*attr
,
1505 const char *buf
, size_t count
)
1507 struct pci_dev
*pdev
= to_pci_dev(dev
);
1509 ssize_t result
= kstrtoul(buf
, 0, &val
);
1517 result
= pci_reset_function(pdev
);
1524 static struct device_attribute reset_attr
= __ATTR(reset
, 0200, NULL
, reset_store
);
1526 static int pci_create_capabilities_sysfs(struct pci_dev
*dev
)
1529 struct bin_attribute
*attr
;
1531 /* If the device has VPD, try to expose it in sysfs. */
1533 attr
= kzalloc(sizeof(*attr
), GFP_ATOMIC
);
1537 sysfs_bin_attr_init(attr
);
1539 attr
->attr
.name
= "vpd";
1540 attr
->attr
.mode
= S_IRUSR
| S_IWUSR
;
1541 attr
->read
= read_vpd_attr
;
1542 attr
->write
= write_vpd_attr
;
1543 retval
= sysfs_create_bin_file(&dev
->dev
.kobj
, attr
);
1548 dev
->vpd
->attr
= attr
;
1551 /* Active State Power Management */
1552 pcie_aspm_create_sysfs_dev_files(dev
);
1554 if (!pci_probe_reset_function(dev
)) {
1555 retval
= device_create_file(&dev
->dev
, &reset_attr
);
1563 pcie_aspm_remove_sysfs_dev_files(dev
);
1564 if (dev
->vpd
&& dev
->vpd
->attr
) {
1565 sysfs_remove_bin_file(&dev
->dev
.kobj
, dev
->vpd
->attr
);
1566 kfree(dev
->vpd
->attr
);
1572 int __must_check
pci_create_sysfs_dev_files(struct pci_dev
*pdev
)
1576 struct bin_attribute
*attr
;
1578 if (!sysfs_initialized
)
1581 if (pdev
->cfg_size
> PCI_CFG_SPACE_SIZE
)
1582 retval
= sysfs_create_bin_file(&pdev
->dev
.kobj
, &pcie_config_attr
);
1584 retval
= sysfs_create_bin_file(&pdev
->dev
.kobj
, &pci_config_attr
);
1588 retval
= pci_create_resource_files(pdev
);
1590 goto err_config_file
;
1592 /* If the device has a ROM, try to expose it in sysfs. */
1593 rom_size
= pci_resource_len(pdev
, PCI_ROM_RESOURCE
);
1595 attr
= kzalloc(sizeof(*attr
), GFP_ATOMIC
);
1598 goto err_resource_files
;
1600 sysfs_bin_attr_init(attr
);
1601 attr
->size
= rom_size
;
1602 attr
->attr
.name
= "rom";
1603 attr
->attr
.mode
= S_IRUSR
| S_IWUSR
;
1604 attr
->read
= pci_read_rom
;
1605 attr
->write
= pci_write_rom
;
1606 retval
= sysfs_create_bin_file(&pdev
->dev
.kobj
, attr
);
1609 goto err_resource_files
;
1611 pdev
->rom_attr
= attr
;
1614 /* add sysfs entries for various capabilities */
1615 retval
= pci_create_capabilities_sysfs(pdev
);
1619 pci_create_firmware_label_files(pdev
);
1624 if (pdev
->rom_attr
) {
1625 sysfs_remove_bin_file(&pdev
->dev
.kobj
, pdev
->rom_attr
);
1626 kfree(pdev
->rom_attr
);
1627 pdev
->rom_attr
= NULL
;
1630 pci_remove_resource_files(pdev
);
1632 if (pdev
->cfg_size
> PCI_CFG_SPACE_SIZE
)
1633 sysfs_remove_bin_file(&pdev
->dev
.kobj
, &pcie_config_attr
);
1635 sysfs_remove_bin_file(&pdev
->dev
.kobj
, &pci_config_attr
);
1640 static void pci_remove_capabilities_sysfs(struct pci_dev
*dev
)
1642 if (dev
->vpd
&& dev
->vpd
->attr
) {
1643 sysfs_remove_bin_file(&dev
->dev
.kobj
, dev
->vpd
->attr
);
1644 kfree(dev
->vpd
->attr
);
1647 pcie_aspm_remove_sysfs_dev_files(dev
);
1648 if (dev
->reset_fn
) {
1649 device_remove_file(&dev
->dev
, &reset_attr
);
1655 * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
1656 * @pdev: device whose entries we should free
1658 * Cleanup when @pdev is removed from sysfs.
1660 void pci_remove_sysfs_dev_files(struct pci_dev
*pdev
)
1662 if (!sysfs_initialized
)
1665 pci_remove_capabilities_sysfs(pdev
);
1667 if (pdev
->cfg_size
> PCI_CFG_SPACE_SIZE
)
1668 sysfs_remove_bin_file(&pdev
->dev
.kobj
, &pcie_config_attr
);
1670 sysfs_remove_bin_file(&pdev
->dev
.kobj
, &pci_config_attr
);
1672 pci_remove_resource_files(pdev
);
1674 if (pdev
->rom_attr
) {
1675 sysfs_remove_bin_file(&pdev
->dev
.kobj
, pdev
->rom_attr
);
1676 kfree(pdev
->rom_attr
);
1677 pdev
->rom_attr
= NULL
;
1680 pci_remove_firmware_label_files(pdev
);
1683 static int __init
pci_sysfs_init(void)
1685 struct pci_dev
*pdev
= NULL
;
1688 sysfs_initialized
= 1;
1689 for_each_pci_dev(pdev
) {
1690 retval
= pci_create_sysfs_dev_files(pdev
);
1699 late_initcall(pci_sysfs_init
);
1701 static struct attribute
*pci_dev_dev_attrs
[] = {
1706 static umode_t
pci_dev_attrs_are_visible(struct kobject
*kobj
,
1707 struct attribute
*a
, int n
)
1709 struct device
*dev
= kobj_to_dev(kobj
);
1710 struct pci_dev
*pdev
= to_pci_dev(dev
);
1712 if (a
== &vga_attr
.attr
)
1713 if ((pdev
->class >> 8) != PCI_CLASS_DISPLAY_VGA
)
1719 static struct attribute
*pci_dev_hp_attrs
[] = {
1720 &dev_remove_attr
.attr
,
1721 &dev_rescan_attr
.attr
,
1725 static umode_t
pci_dev_hp_attrs_are_visible(struct kobject
*kobj
,
1726 struct attribute
*a
, int n
)
1728 struct device
*dev
= kobj_to_dev(kobj
);
1729 struct pci_dev
*pdev
= to_pci_dev(dev
);
1731 if (pdev
->is_virtfn
)
1737 static umode_t
pci_bridge_attrs_are_visible(struct kobject
*kobj
,
1738 struct attribute
*a
, int n
)
1740 struct device
*dev
= kobj_to_dev(kobj
);
1741 struct pci_dev
*pdev
= to_pci_dev(dev
);
1743 if (pci_is_bridge(pdev
))
1749 static umode_t
pcie_dev_attrs_are_visible(struct kobject
*kobj
,
1750 struct attribute
*a
, int n
)
1752 struct device
*dev
= kobj_to_dev(kobj
);
1753 struct pci_dev
*pdev
= to_pci_dev(dev
);
1755 if (pci_is_pcie(pdev
))
1761 static const struct attribute_group pci_dev_group
= {
1762 .attrs
= pci_dev_attrs
,
1765 const struct attribute_group
*pci_dev_groups
[] = {
1770 static const struct attribute_group pci_bridge_group
= {
1771 .attrs
= pci_bridge_attrs
,
1774 const struct attribute_group
*pci_bridge_groups
[] = {
1779 static const struct attribute_group pcie_dev_group
= {
1780 .attrs
= pcie_dev_attrs
,
1783 const struct attribute_group
*pcie_dev_groups
[] = {
1788 static const struct attribute_group pci_dev_hp_attr_group
= {
1789 .attrs
= pci_dev_hp_attrs
,
1790 .is_visible
= pci_dev_hp_attrs_are_visible
,
1793 #ifdef CONFIG_PCI_IOV
1794 static struct attribute
*sriov_dev_attrs
[] = {
1795 &sriov_totalvfs_attr
.attr
,
1796 &sriov_numvfs_attr
.attr
,
1797 &sriov_offset_attr
.attr
,
1798 &sriov_stride_attr
.attr
,
1799 &sriov_vf_device_attr
.attr
,
1800 &sriov_drivers_autoprobe_attr
.attr
,
1804 static umode_t
sriov_attrs_are_visible(struct kobject
*kobj
,
1805 struct attribute
*a
, int n
)
1807 struct device
*dev
= kobj_to_dev(kobj
);
1809 if (!dev_is_pf(dev
))
1815 static const struct attribute_group sriov_dev_attr_group
= {
1816 .attrs
= sriov_dev_attrs
,
1817 .is_visible
= sriov_attrs_are_visible
,
1819 #endif /* CONFIG_PCI_IOV */
1821 static const struct attribute_group pci_dev_attr_group
= {
1822 .attrs
= pci_dev_dev_attrs
,
1823 .is_visible
= pci_dev_attrs_are_visible
,
1826 static const struct attribute_group pci_bridge_attr_group
= {
1827 .attrs
= pci_bridge_attrs
,
1828 .is_visible
= pci_bridge_attrs_are_visible
,
1831 static const struct attribute_group pcie_dev_attr_group
= {
1832 .attrs
= pcie_dev_attrs
,
1833 .is_visible
= pcie_dev_attrs_are_visible
,
1836 static const struct attribute_group
*pci_dev_attr_groups
[] = {
1837 &pci_dev_attr_group
,
1838 &pci_dev_hp_attr_group
,
1839 #ifdef CONFIG_PCI_IOV
1840 &sriov_dev_attr_group
,
1842 &pci_bridge_attr_group
,
1843 &pcie_dev_attr_group
,
1847 const struct device_type pci_dev_type
= {
1848 .groups
= pci_dev_attr_groups
,