rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * drivers/pci/pci-sysfs.c |
| 4 | * |
| 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> |
| 11 | * |
| 12 | * File attributes for PCI devices |
| 13 | * |
| 14 | * Modeled after usb's driverfs.c |
| 15 | * |
| 16 | */ |
| 17 | |
| 18 | |
| 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> |
| 25 | #include <linux/mm.h> |
| 26 | #include <linux/fs.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> |
| 33 | #include <linux/of.h> |
| 34 | #include "pci.h" |
| 35 | |
| 36 | static int sysfs_initialized; /* = 0 */ |
| 37 | |
| 38 | /* show configuration fields */ |
| 39 | #define pci_config_attr(field, format_string) \ |
| 40 | static ssize_t \ |
| 41 | field##_show(struct device *dev, struct device_attribute *attr, char *buf) \ |
| 42 | { \ |
| 43 | struct pci_dev *pdev; \ |
| 44 | \ |
| 45 | pdev = to_pci_dev(dev); \ |
| 46 | return sprintf(buf, format_string, pdev->field); \ |
| 47 | } \ |
| 48 | static DEVICE_ATTR_RO(field) |
| 49 | |
| 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"); |
| 57 | |
| 58 | static ssize_t broken_parity_status_show(struct device *dev, |
| 59 | struct device_attribute *attr, |
| 60 | char *buf) |
| 61 | { |
| 62 | struct pci_dev *pdev = to_pci_dev(dev); |
| 63 | return sprintf(buf, "%u\n", pdev->broken_parity_status); |
| 64 | } |
| 65 | |
| 66 | static ssize_t broken_parity_status_store(struct device *dev, |
| 67 | struct device_attribute *attr, |
| 68 | const char *buf, size_t count) |
| 69 | { |
| 70 | struct pci_dev *pdev = to_pci_dev(dev); |
| 71 | unsigned long val; |
| 72 | |
| 73 | if (kstrtoul(buf, 0, &val) < 0) |
| 74 | return -EINVAL; |
| 75 | |
| 76 | pdev->broken_parity_status = !!val; |
| 77 | |
| 78 | return count; |
| 79 | } |
| 80 | static DEVICE_ATTR_RW(broken_parity_status); |
| 81 | |
| 82 | static ssize_t pci_dev_show_local_cpu(struct device *dev, bool list, |
| 83 | struct device_attribute *attr, char *buf) |
| 84 | { |
| 85 | const struct cpumask *mask; |
| 86 | |
| 87 | #ifdef CONFIG_NUMA |
| 88 | mask = (dev_to_node(dev) == -1) ? cpu_online_mask : |
| 89 | cpumask_of_node(dev_to_node(dev)); |
| 90 | #else |
| 91 | mask = cpumask_of_pcibus(to_pci_dev(dev)->bus); |
| 92 | #endif |
| 93 | return cpumap_print_to_pagebuf(list, buf, mask); |
| 94 | } |
| 95 | |
| 96 | static ssize_t local_cpus_show(struct device *dev, |
| 97 | struct device_attribute *attr, char *buf) |
| 98 | { |
| 99 | return pci_dev_show_local_cpu(dev, false, attr, buf); |
| 100 | } |
| 101 | static DEVICE_ATTR_RO(local_cpus); |
| 102 | |
| 103 | static ssize_t local_cpulist_show(struct device *dev, |
| 104 | struct device_attribute *attr, char *buf) |
| 105 | { |
| 106 | return pci_dev_show_local_cpu(dev, true, attr, buf); |
| 107 | } |
| 108 | static DEVICE_ATTR_RO(local_cpulist); |
| 109 | |
| 110 | /* |
| 111 | * PCI Bus Class Devices |
| 112 | */ |
| 113 | static ssize_t cpuaffinity_show(struct device *dev, |
| 114 | struct device_attribute *attr, char *buf) |
| 115 | { |
| 116 | const struct cpumask *cpumask = cpumask_of_pcibus(to_pci_bus(dev)); |
| 117 | |
| 118 | return cpumap_print_to_pagebuf(false, buf, cpumask); |
| 119 | } |
| 120 | static DEVICE_ATTR_RO(cpuaffinity); |
| 121 | |
| 122 | static ssize_t cpulistaffinity_show(struct device *dev, |
| 123 | struct device_attribute *attr, char *buf) |
| 124 | { |
| 125 | const struct cpumask *cpumask = cpumask_of_pcibus(to_pci_bus(dev)); |
| 126 | |
| 127 | return cpumap_print_to_pagebuf(true, buf, cpumask); |
| 128 | } |
| 129 | static DEVICE_ATTR_RO(cpulistaffinity); |
| 130 | |
| 131 | /* show resources */ |
| 132 | static ssize_t resource_show(struct device *dev, struct device_attribute *attr, |
| 133 | char *buf) |
| 134 | { |
| 135 | struct pci_dev *pci_dev = to_pci_dev(dev); |
| 136 | char *str = buf; |
| 137 | int i; |
| 138 | int max; |
| 139 | resource_size_t start, end; |
| 140 | |
| 141 | if (pci_dev->subordinate) |
| 142 | max = DEVICE_COUNT_RESOURCE; |
| 143 | else |
| 144 | max = PCI_BRIDGE_RESOURCES; |
| 145 | |
| 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); |
| 153 | } |
| 154 | return (str - buf); |
| 155 | } |
| 156 | static DEVICE_ATTR_RO(resource); |
| 157 | |
| 158 | static ssize_t max_link_speed_show(struct device *dev, |
| 159 | struct device_attribute *attr, char *buf) |
| 160 | { |
| 161 | struct pci_dev *pci_dev = to_pci_dev(dev); |
| 162 | u32 linkcap; |
| 163 | int err; |
| 164 | const char *speed; |
| 165 | |
| 166 | err = pcie_capability_read_dword(pci_dev, PCI_EXP_LNKCAP, &linkcap); |
| 167 | if (err) |
| 168 | return -EINVAL; |
| 169 | |
| 170 | switch (linkcap & PCI_EXP_LNKCAP_SLS) { |
| 171 | case PCI_EXP_LNKCAP_SLS_8_0GB: |
| 172 | speed = "8 GT/s"; |
| 173 | break; |
| 174 | case PCI_EXP_LNKCAP_SLS_5_0GB: |
| 175 | speed = "5 GT/s"; |
| 176 | break; |
| 177 | case PCI_EXP_LNKCAP_SLS_2_5GB: |
| 178 | speed = "2.5 GT/s"; |
| 179 | break; |
| 180 | default: |
| 181 | speed = "Unknown speed"; |
| 182 | } |
| 183 | |
| 184 | return sprintf(buf, "%s\n", speed); |
| 185 | } |
| 186 | static DEVICE_ATTR_RO(max_link_speed); |
| 187 | |
| 188 | static ssize_t max_link_width_show(struct device *dev, |
| 189 | struct device_attribute *attr, char *buf) |
| 190 | { |
| 191 | struct pci_dev *pci_dev = to_pci_dev(dev); |
| 192 | u32 linkcap; |
| 193 | int err; |
| 194 | |
| 195 | err = pcie_capability_read_dword(pci_dev, PCI_EXP_LNKCAP, &linkcap); |
| 196 | if (err) |
| 197 | return -EINVAL; |
| 198 | |
| 199 | return sprintf(buf, "%u\n", (linkcap & PCI_EXP_LNKCAP_MLW) >> 4); |
| 200 | } |
| 201 | static DEVICE_ATTR_RO(max_link_width); |
| 202 | |
| 203 | static ssize_t current_link_speed_show(struct device *dev, |
| 204 | struct device_attribute *attr, char *buf) |
| 205 | { |
| 206 | struct pci_dev *pci_dev = to_pci_dev(dev); |
| 207 | u16 linkstat; |
| 208 | int err; |
| 209 | const char *speed; |
| 210 | |
| 211 | err = pcie_capability_read_word(pci_dev, PCI_EXP_LNKSTA, &linkstat); |
| 212 | if (err) |
| 213 | return -EINVAL; |
| 214 | |
| 215 | switch (linkstat & PCI_EXP_LNKSTA_CLS) { |
| 216 | case PCI_EXP_LNKSTA_CLS_8_0GB: |
| 217 | speed = "8 GT/s"; |
| 218 | break; |
| 219 | case PCI_EXP_LNKSTA_CLS_5_0GB: |
| 220 | speed = "5 GT/s"; |
| 221 | break; |
| 222 | case PCI_EXP_LNKSTA_CLS_2_5GB: |
| 223 | speed = "2.5 GT/s"; |
| 224 | break; |
| 225 | default: |
| 226 | speed = "Unknown speed"; |
| 227 | } |
| 228 | |
| 229 | return sprintf(buf, "%s\n", speed); |
| 230 | } |
| 231 | static DEVICE_ATTR_RO(current_link_speed); |
| 232 | |
| 233 | static ssize_t current_link_width_show(struct device *dev, |
| 234 | struct device_attribute *attr, char *buf) |
| 235 | { |
| 236 | struct pci_dev *pci_dev = to_pci_dev(dev); |
| 237 | u16 linkstat; |
| 238 | int err; |
| 239 | |
| 240 | err = pcie_capability_read_word(pci_dev, PCI_EXP_LNKSTA, &linkstat); |
| 241 | if (err) |
| 242 | return -EINVAL; |
| 243 | |
| 244 | return sprintf(buf, "%u\n", |
| 245 | (linkstat & PCI_EXP_LNKSTA_NLW) >> PCI_EXP_LNKSTA_NLW_SHIFT); |
| 246 | } |
| 247 | static DEVICE_ATTR_RO(current_link_width); |
| 248 | |
| 249 | static ssize_t secondary_bus_number_show(struct device *dev, |
| 250 | struct device_attribute *attr, |
| 251 | char *buf) |
| 252 | { |
| 253 | struct pci_dev *pci_dev = to_pci_dev(dev); |
| 254 | u8 sec_bus; |
| 255 | int err; |
| 256 | |
| 257 | err = pci_read_config_byte(pci_dev, PCI_SECONDARY_BUS, &sec_bus); |
| 258 | if (err) |
| 259 | return -EINVAL; |
| 260 | |
| 261 | return sprintf(buf, "%u\n", sec_bus); |
| 262 | } |
| 263 | static DEVICE_ATTR_RO(secondary_bus_number); |
| 264 | |
| 265 | static ssize_t subordinate_bus_number_show(struct device *dev, |
| 266 | struct device_attribute *attr, |
| 267 | char *buf) |
| 268 | { |
| 269 | struct pci_dev *pci_dev = to_pci_dev(dev); |
| 270 | u8 sub_bus; |
| 271 | int err; |
| 272 | |
| 273 | err = pci_read_config_byte(pci_dev, PCI_SUBORDINATE_BUS, &sub_bus); |
| 274 | if (err) |
| 275 | return -EINVAL; |
| 276 | |
| 277 | return sprintf(buf, "%u\n", sub_bus); |
| 278 | } |
| 279 | static DEVICE_ATTR_RO(subordinate_bus_number); |
| 280 | |
| 281 | static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, |
| 282 | char *buf) |
| 283 | { |
| 284 | struct pci_dev *pci_dev = to_pci_dev(dev); |
| 285 | |
| 286 | return sprintf(buf, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02X\n", |
| 287 | pci_dev->vendor, pci_dev->device, |
| 288 | pci_dev->subsystem_vendor, pci_dev->subsystem_device, |
| 289 | (u8)(pci_dev->class >> 16), (u8)(pci_dev->class >> 8), |
| 290 | (u8)(pci_dev->class)); |
| 291 | } |
| 292 | static DEVICE_ATTR_RO(modalias); |
| 293 | |
| 294 | static ssize_t enable_store(struct device *dev, struct device_attribute *attr, |
| 295 | const char *buf, size_t count) |
| 296 | { |
| 297 | struct pci_dev *pdev = to_pci_dev(dev); |
| 298 | unsigned long val; |
| 299 | ssize_t result = kstrtoul(buf, 0, &val); |
| 300 | |
| 301 | if (result < 0) |
| 302 | return result; |
| 303 | |
| 304 | /* this can crash the machine when done on the "wrong" device */ |
| 305 | if (!capable(CAP_SYS_ADMIN)) |
| 306 | return -EPERM; |
| 307 | |
| 308 | device_lock(dev); |
| 309 | if (dev->driver) |
| 310 | result = -EBUSY; |
| 311 | else if (val) |
| 312 | result = pci_enable_device(pdev); |
| 313 | else if (pci_is_enabled(pdev)) |
| 314 | pci_disable_device(pdev); |
| 315 | else |
| 316 | result = -EIO; |
| 317 | device_unlock(dev); |
| 318 | |
| 319 | return result < 0 ? result : count; |
| 320 | } |
| 321 | |
| 322 | static ssize_t enable_show(struct device *dev, struct device_attribute *attr, |
| 323 | char *buf) |
| 324 | { |
| 325 | struct pci_dev *pdev; |
| 326 | |
| 327 | pdev = to_pci_dev(dev); |
| 328 | return sprintf(buf, "%u\n", atomic_read(&pdev->enable_cnt)); |
| 329 | } |
| 330 | static DEVICE_ATTR_RW(enable); |
| 331 | |
| 332 | #ifdef CONFIG_NUMA |
| 333 | static ssize_t numa_node_store(struct device *dev, |
| 334 | struct device_attribute *attr, const char *buf, |
| 335 | size_t count) |
| 336 | { |
| 337 | struct pci_dev *pdev = to_pci_dev(dev); |
| 338 | int node, ret; |
| 339 | |
| 340 | if (!capable(CAP_SYS_ADMIN)) |
| 341 | return -EPERM; |
| 342 | |
| 343 | ret = kstrtoint(buf, 0, &node); |
| 344 | if (ret) |
| 345 | return ret; |
| 346 | |
| 347 | if ((node < 0 && node != NUMA_NO_NODE) || node >= MAX_NUMNODES) |
| 348 | return -EINVAL; |
| 349 | |
| 350 | if (node != NUMA_NO_NODE && !node_online(node)) |
| 351 | return -EINVAL; |
| 352 | |
| 353 | add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_STILL_OK); |
| 354 | dev_alert(&pdev->dev, FW_BUG "Overriding NUMA node to %d. Contact your vendor for updates.", |
| 355 | node); |
| 356 | |
| 357 | dev->numa_node = node; |
| 358 | return count; |
| 359 | } |
| 360 | |
| 361 | static ssize_t numa_node_show(struct device *dev, struct device_attribute *attr, |
| 362 | char *buf) |
| 363 | { |
| 364 | return sprintf(buf, "%d\n", dev->numa_node); |
| 365 | } |
| 366 | static DEVICE_ATTR_RW(numa_node); |
| 367 | #endif |
| 368 | |
| 369 | static ssize_t dma_mask_bits_show(struct device *dev, |
| 370 | struct device_attribute *attr, char *buf) |
| 371 | { |
| 372 | struct pci_dev *pdev = to_pci_dev(dev); |
| 373 | |
| 374 | return sprintf(buf, "%d\n", fls64(pdev->dma_mask)); |
| 375 | } |
| 376 | static DEVICE_ATTR_RO(dma_mask_bits); |
| 377 | |
| 378 | static ssize_t consistent_dma_mask_bits_show(struct device *dev, |
| 379 | struct device_attribute *attr, |
| 380 | char *buf) |
| 381 | { |
| 382 | return sprintf(buf, "%d\n", fls64(dev->coherent_dma_mask)); |
| 383 | } |
| 384 | static DEVICE_ATTR_RO(consistent_dma_mask_bits); |
| 385 | |
| 386 | static ssize_t msi_bus_show(struct device *dev, struct device_attribute *attr, |
| 387 | char *buf) |
| 388 | { |
| 389 | struct pci_dev *pdev = to_pci_dev(dev); |
| 390 | struct pci_bus *subordinate = pdev->subordinate; |
| 391 | |
| 392 | return sprintf(buf, "%u\n", subordinate ? |
| 393 | !(subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI) |
| 394 | : !pdev->no_msi); |
| 395 | } |
| 396 | |
| 397 | static ssize_t msi_bus_store(struct device *dev, struct device_attribute *attr, |
| 398 | const char *buf, size_t count) |
| 399 | { |
| 400 | struct pci_dev *pdev = to_pci_dev(dev); |
| 401 | struct pci_bus *subordinate = pdev->subordinate; |
| 402 | unsigned long val; |
| 403 | |
| 404 | if (kstrtoul(buf, 0, &val) < 0) |
| 405 | return -EINVAL; |
| 406 | |
| 407 | if (!capable(CAP_SYS_ADMIN)) |
| 408 | return -EPERM; |
| 409 | |
| 410 | /* |
| 411 | * "no_msi" and "bus_flags" only affect what happens when a driver |
| 412 | * requests MSI or MSI-X. They don't affect any drivers that have |
| 413 | * already requested MSI or MSI-X. |
| 414 | */ |
| 415 | if (!subordinate) { |
| 416 | pdev->no_msi = !val; |
| 417 | dev_info(&pdev->dev, "MSI/MSI-X %s for future drivers\n", |
| 418 | val ? "allowed" : "disallowed"); |
| 419 | return count; |
| 420 | } |
| 421 | |
| 422 | if (val) |
| 423 | subordinate->bus_flags &= ~PCI_BUS_FLAGS_NO_MSI; |
| 424 | else |
| 425 | subordinate->bus_flags |= PCI_BUS_FLAGS_NO_MSI; |
| 426 | |
| 427 | dev_info(&subordinate->dev, "MSI/MSI-X %s for future drivers of devices on this bus\n", |
| 428 | val ? "allowed" : "disallowed"); |
| 429 | return count; |
| 430 | } |
| 431 | static DEVICE_ATTR_RW(msi_bus); |
| 432 | |
| 433 | static ssize_t bus_rescan_store(struct bus_type *bus, const char *buf, |
| 434 | size_t count) |
| 435 | { |
| 436 | unsigned long val; |
| 437 | struct pci_bus *b = NULL; |
| 438 | |
| 439 | if (kstrtoul(buf, 0, &val) < 0) |
| 440 | return -EINVAL; |
| 441 | |
| 442 | if (val) { |
| 443 | pci_lock_rescan_remove(); |
| 444 | while ((b = pci_find_next_bus(b)) != NULL) |
| 445 | pci_rescan_bus(b); |
| 446 | pci_unlock_rescan_remove(); |
| 447 | } |
| 448 | return count; |
| 449 | } |
| 450 | static BUS_ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, bus_rescan_store); |
| 451 | |
| 452 | static struct attribute *pci_bus_attrs[] = { |
| 453 | &bus_attr_rescan.attr, |
| 454 | NULL, |
| 455 | }; |
| 456 | |
| 457 | static const struct attribute_group pci_bus_group = { |
| 458 | .attrs = pci_bus_attrs, |
| 459 | }; |
| 460 | |
| 461 | const struct attribute_group *pci_bus_groups[] = { |
| 462 | &pci_bus_group, |
| 463 | NULL, |
| 464 | }; |
| 465 | |
| 466 | static ssize_t dev_rescan_store(struct device *dev, |
| 467 | struct device_attribute *attr, const char *buf, |
| 468 | size_t count) |
| 469 | { |
| 470 | unsigned long val; |
| 471 | struct pci_dev *pdev = to_pci_dev(dev); |
| 472 | |
| 473 | if (kstrtoul(buf, 0, &val) < 0) |
| 474 | return -EINVAL; |
| 475 | |
| 476 | if (val) { |
| 477 | pci_lock_rescan_remove(); |
| 478 | pci_rescan_bus(pdev->bus); |
| 479 | pci_unlock_rescan_remove(); |
| 480 | } |
| 481 | return count; |
| 482 | } |
| 483 | static struct device_attribute dev_rescan_attr = __ATTR(rescan, |
| 484 | (S_IWUSR|S_IWGRP), |
| 485 | NULL, dev_rescan_store); |
| 486 | |
| 487 | static ssize_t remove_store(struct device *dev, struct device_attribute *attr, |
| 488 | const char *buf, size_t count) |
| 489 | { |
| 490 | unsigned long val; |
| 491 | |
| 492 | if (kstrtoul(buf, 0, &val) < 0) |
| 493 | return -EINVAL; |
| 494 | |
| 495 | if (val && device_remove_file_self(dev, attr)) |
| 496 | pci_stop_and_remove_bus_device_locked(to_pci_dev(dev)); |
| 497 | return count; |
| 498 | } |
| 499 | static struct device_attribute dev_remove_attr = __ATTR_IGNORE_LOCKDEP(remove, |
| 500 | (S_IWUSR|S_IWGRP), |
| 501 | NULL, remove_store); |
| 502 | |
| 503 | static ssize_t dev_bus_rescan_store(struct device *dev, |
| 504 | struct device_attribute *attr, |
| 505 | const char *buf, size_t count) |
| 506 | { |
| 507 | unsigned long val; |
| 508 | struct pci_bus *bus = to_pci_bus(dev); |
| 509 | |
| 510 | if (kstrtoul(buf, 0, &val) < 0) |
| 511 | return -EINVAL; |
| 512 | |
| 513 | if (val) { |
| 514 | pci_lock_rescan_remove(); |
| 515 | if (!pci_is_root_bus(bus) && list_empty(&bus->devices)) |
| 516 | pci_rescan_bus_bridge_resize(bus->self); |
| 517 | else |
| 518 | pci_rescan_bus(bus); |
| 519 | pci_unlock_rescan_remove(); |
| 520 | } |
| 521 | return count; |
| 522 | } |
| 523 | static DEVICE_ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, dev_bus_rescan_store); |
| 524 | |
| 525 | #if defined(CONFIG_PM) && defined(CONFIG_ACPI) |
| 526 | static ssize_t d3cold_allowed_store(struct device *dev, |
| 527 | struct device_attribute *attr, |
| 528 | const char *buf, size_t count) |
| 529 | { |
| 530 | struct pci_dev *pdev = to_pci_dev(dev); |
| 531 | unsigned long val; |
| 532 | |
| 533 | if (kstrtoul(buf, 0, &val) < 0) |
| 534 | return -EINVAL; |
| 535 | |
| 536 | pdev->d3cold_allowed = !!val; |
| 537 | if (pdev->d3cold_allowed) |
| 538 | pci_d3cold_enable(pdev); |
| 539 | else |
| 540 | pci_d3cold_disable(pdev); |
| 541 | |
| 542 | pm_runtime_resume(dev); |
| 543 | |
| 544 | return count; |
| 545 | } |
| 546 | |
| 547 | static ssize_t d3cold_allowed_show(struct device *dev, |
| 548 | struct device_attribute *attr, char *buf) |
| 549 | { |
| 550 | struct pci_dev *pdev = to_pci_dev(dev); |
| 551 | return sprintf(buf, "%u\n", pdev->d3cold_allowed); |
| 552 | } |
| 553 | static DEVICE_ATTR_RW(d3cold_allowed); |
| 554 | #endif |
| 555 | |
| 556 | #ifdef CONFIG_OF |
| 557 | static ssize_t devspec_show(struct device *dev, |
| 558 | struct device_attribute *attr, char *buf) |
| 559 | { |
| 560 | struct pci_dev *pdev = to_pci_dev(dev); |
| 561 | struct device_node *np = pci_device_to_OF_node(pdev); |
| 562 | |
| 563 | if (np == NULL) |
| 564 | return 0; |
| 565 | return sprintf(buf, "%pOF", np); |
| 566 | } |
| 567 | static DEVICE_ATTR_RO(devspec); |
| 568 | #endif |
| 569 | |
| 570 | #ifdef CONFIG_PCI_IOV |
| 571 | static ssize_t sriov_totalvfs_show(struct device *dev, |
| 572 | struct device_attribute *attr, |
| 573 | char *buf) |
| 574 | { |
| 575 | struct pci_dev *pdev = to_pci_dev(dev); |
| 576 | |
| 577 | return sprintf(buf, "%u\n", pci_sriov_get_totalvfs(pdev)); |
| 578 | } |
| 579 | |
| 580 | |
| 581 | static ssize_t sriov_numvfs_show(struct device *dev, |
| 582 | struct device_attribute *attr, |
| 583 | char *buf) |
| 584 | { |
| 585 | struct pci_dev *pdev = to_pci_dev(dev); |
| 586 | |
| 587 | return sprintf(buf, "%u\n", pdev->sriov->num_VFs); |
| 588 | } |
| 589 | |
| 590 | /* |
| 591 | * num_vfs > 0; number of VFs to enable |
| 592 | * num_vfs = 0; disable all VFs |
| 593 | * |
| 594 | * Note: SRIOV spec doesn't allow partial VF |
| 595 | * disable, so it's all or none. |
| 596 | */ |
| 597 | static ssize_t sriov_numvfs_store(struct device *dev, |
| 598 | struct device_attribute *attr, |
| 599 | const char *buf, size_t count) |
| 600 | { |
| 601 | struct pci_dev *pdev = to_pci_dev(dev); |
| 602 | int ret; |
| 603 | u16 num_vfs; |
| 604 | |
| 605 | ret = kstrtou16(buf, 0, &num_vfs); |
| 606 | if (ret < 0) |
| 607 | return ret; |
| 608 | |
| 609 | if (num_vfs > pci_sriov_get_totalvfs(pdev)) |
| 610 | return -ERANGE; |
| 611 | |
| 612 | device_lock(&pdev->dev); |
| 613 | |
| 614 | if (num_vfs == pdev->sriov->num_VFs) |
| 615 | goto exit; |
| 616 | |
| 617 | /* is PF driver loaded w/callback */ |
| 618 | if (!pdev->driver || !pdev->driver->sriov_configure) { |
| 619 | dev_info(&pdev->dev, "Driver doesn't support SRIOV configuration via sysfs\n"); |
| 620 | ret = -ENOENT; |
| 621 | goto exit; |
| 622 | } |
| 623 | |
| 624 | if (num_vfs == 0) { |
| 625 | /* disable VFs */ |
| 626 | ret = pdev->driver->sriov_configure(pdev, 0); |
| 627 | goto exit; |
| 628 | } |
| 629 | |
| 630 | /* enable VFs */ |
| 631 | if (pdev->sriov->num_VFs) { |
| 632 | dev_warn(&pdev->dev, "%d VFs already enabled. Disable before enabling %d VFs\n", |
| 633 | pdev->sriov->num_VFs, num_vfs); |
| 634 | ret = -EBUSY; |
| 635 | goto exit; |
| 636 | } |
| 637 | |
| 638 | ret = pdev->driver->sriov_configure(pdev, num_vfs); |
| 639 | if (ret < 0) |
| 640 | goto exit; |
| 641 | |
| 642 | if (ret != num_vfs) |
| 643 | dev_warn(&pdev->dev, "%d VFs requested; only %d enabled\n", |
| 644 | num_vfs, ret); |
| 645 | |
| 646 | exit: |
| 647 | device_unlock(&pdev->dev); |
| 648 | |
| 649 | if (ret < 0) |
| 650 | return ret; |
| 651 | |
| 652 | return count; |
| 653 | } |
| 654 | |
| 655 | static ssize_t sriov_drivers_autoprobe_show(struct device *dev, |
| 656 | struct device_attribute *attr, |
| 657 | char *buf) |
| 658 | { |
| 659 | struct pci_dev *pdev = to_pci_dev(dev); |
| 660 | |
| 661 | return sprintf(buf, "%u\n", pdev->sriov->drivers_autoprobe); |
| 662 | } |
| 663 | |
| 664 | static ssize_t sriov_drivers_autoprobe_store(struct device *dev, |
| 665 | struct device_attribute *attr, |
| 666 | const char *buf, size_t count) |
| 667 | { |
| 668 | struct pci_dev *pdev = to_pci_dev(dev); |
| 669 | bool drivers_autoprobe; |
| 670 | |
| 671 | if (kstrtobool(buf, &drivers_autoprobe) < 0) |
| 672 | return -EINVAL; |
| 673 | |
| 674 | pdev->sriov->drivers_autoprobe = drivers_autoprobe; |
| 675 | |
| 676 | return count; |
| 677 | } |
| 678 | |
| 679 | static struct device_attribute sriov_totalvfs_attr = __ATTR_RO(sriov_totalvfs); |
| 680 | static struct device_attribute sriov_numvfs_attr = |
| 681 | __ATTR(sriov_numvfs, (S_IRUGO|S_IWUSR|S_IWGRP), |
| 682 | sriov_numvfs_show, sriov_numvfs_store); |
| 683 | static struct device_attribute sriov_drivers_autoprobe_attr = |
| 684 | __ATTR(sriov_drivers_autoprobe, (S_IRUGO|S_IWUSR|S_IWGRP), |
| 685 | sriov_drivers_autoprobe_show, sriov_drivers_autoprobe_store); |
| 686 | #endif /* CONFIG_PCI_IOV */ |
| 687 | |
| 688 | static ssize_t driver_override_store(struct device *dev, |
| 689 | struct device_attribute *attr, |
| 690 | const char *buf, size_t count) |
| 691 | { |
| 692 | struct pci_dev *pdev = to_pci_dev(dev); |
| 693 | char *driver_override, *old, *cp; |
| 694 | |
| 695 | /* We need to keep extra room for a newline */ |
| 696 | if (count >= (PAGE_SIZE - 1)) |
| 697 | return -EINVAL; |
| 698 | |
| 699 | driver_override = kstrndup(buf, count, GFP_KERNEL); |
| 700 | if (!driver_override) |
| 701 | return -ENOMEM; |
| 702 | |
| 703 | cp = strchr(driver_override, '\n'); |
| 704 | if (cp) |
| 705 | *cp = '\0'; |
| 706 | |
| 707 | device_lock(dev); |
| 708 | old = pdev->driver_override; |
| 709 | if (strlen(driver_override)) { |
| 710 | pdev->driver_override = driver_override; |
| 711 | } else { |
| 712 | kfree(driver_override); |
| 713 | pdev->driver_override = NULL; |
| 714 | } |
| 715 | device_unlock(dev); |
| 716 | |
| 717 | kfree(old); |
| 718 | |
| 719 | return count; |
| 720 | } |
| 721 | |
| 722 | static ssize_t driver_override_show(struct device *dev, |
| 723 | struct device_attribute *attr, char *buf) |
| 724 | { |
| 725 | struct pci_dev *pdev = to_pci_dev(dev); |
| 726 | ssize_t len; |
| 727 | |
| 728 | device_lock(dev); |
| 729 | len = snprintf(buf, PAGE_SIZE, "%s\n", pdev->driver_override); |
| 730 | device_unlock(dev); |
| 731 | return len; |
| 732 | } |
| 733 | static DEVICE_ATTR_RW(driver_override); |
| 734 | |
| 735 | static struct attribute *pci_dev_attrs[] = { |
| 736 | &dev_attr_resource.attr, |
| 737 | &dev_attr_vendor.attr, |
| 738 | &dev_attr_device.attr, |
| 739 | &dev_attr_subsystem_vendor.attr, |
| 740 | &dev_attr_subsystem_device.attr, |
| 741 | &dev_attr_revision.attr, |
| 742 | &dev_attr_class.attr, |
| 743 | &dev_attr_irq.attr, |
| 744 | &dev_attr_local_cpus.attr, |
| 745 | &dev_attr_local_cpulist.attr, |
| 746 | &dev_attr_modalias.attr, |
| 747 | #ifdef CONFIG_NUMA |
| 748 | &dev_attr_numa_node.attr, |
| 749 | #endif |
| 750 | &dev_attr_dma_mask_bits.attr, |
| 751 | &dev_attr_consistent_dma_mask_bits.attr, |
| 752 | &dev_attr_enable.attr, |
| 753 | &dev_attr_broken_parity_status.attr, |
| 754 | &dev_attr_msi_bus.attr, |
| 755 | #if defined(CONFIG_PM) && defined(CONFIG_ACPI) |
| 756 | &dev_attr_d3cold_allowed.attr, |
| 757 | #endif |
| 758 | #ifdef CONFIG_OF |
| 759 | &dev_attr_devspec.attr, |
| 760 | #endif |
| 761 | &dev_attr_driver_override.attr, |
| 762 | NULL, |
| 763 | }; |
| 764 | |
| 765 | static struct attribute *pci_bridge_attrs[] = { |
| 766 | &dev_attr_subordinate_bus_number.attr, |
| 767 | &dev_attr_secondary_bus_number.attr, |
| 768 | NULL, |
| 769 | }; |
| 770 | |
| 771 | static struct attribute *pcie_dev_attrs[] = { |
| 772 | &dev_attr_current_link_speed.attr, |
| 773 | &dev_attr_current_link_width.attr, |
| 774 | &dev_attr_max_link_width.attr, |
| 775 | &dev_attr_max_link_speed.attr, |
| 776 | NULL, |
| 777 | }; |
| 778 | |
| 779 | static struct attribute *pcibus_attrs[] = { |
| 780 | &dev_attr_rescan.attr, |
| 781 | &dev_attr_cpuaffinity.attr, |
| 782 | &dev_attr_cpulistaffinity.attr, |
| 783 | NULL, |
| 784 | }; |
| 785 | |
| 786 | static const struct attribute_group pcibus_group = { |
| 787 | .attrs = pcibus_attrs, |
| 788 | }; |
| 789 | |
| 790 | const struct attribute_group *pcibus_groups[] = { |
| 791 | &pcibus_group, |
| 792 | NULL, |
| 793 | }; |
| 794 | |
| 795 | static ssize_t boot_vga_show(struct device *dev, struct device_attribute *attr, |
| 796 | char *buf) |
| 797 | { |
| 798 | struct pci_dev *pdev = to_pci_dev(dev); |
| 799 | struct pci_dev *vga_dev = vga_default_device(); |
| 800 | |
| 801 | if (vga_dev) |
| 802 | return sprintf(buf, "%u\n", (pdev == vga_dev)); |
| 803 | |
| 804 | return sprintf(buf, "%u\n", |
| 805 | !!(pdev->resource[PCI_ROM_RESOURCE].flags & |
| 806 | IORESOURCE_ROM_SHADOW)); |
| 807 | } |
| 808 | static struct device_attribute vga_attr = __ATTR_RO(boot_vga); |
| 809 | |
| 810 | static ssize_t pci_read_config(struct file *filp, struct kobject *kobj, |
| 811 | struct bin_attribute *bin_attr, char *buf, |
| 812 | loff_t off, size_t count) |
| 813 | { |
| 814 | struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj)); |
| 815 | unsigned int size = 64; |
| 816 | loff_t init_off = off; |
| 817 | u8 *data = (u8 *) buf; |
| 818 | |
| 819 | /* Several chips lock up trying to read undefined config space */ |
| 820 | if (file_ns_capable(filp, &init_user_ns, CAP_SYS_ADMIN)) |
| 821 | size = dev->cfg_size; |
| 822 | else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) |
| 823 | size = 128; |
| 824 | |
| 825 | if (off > size) |
| 826 | return 0; |
| 827 | if (off + count > size) { |
| 828 | size -= off; |
| 829 | count = size; |
| 830 | } else { |
| 831 | size = count; |
| 832 | } |
| 833 | |
| 834 | pci_config_pm_runtime_get(dev); |
| 835 | |
| 836 | if ((off & 1) && size) { |
| 837 | u8 val; |
| 838 | pci_user_read_config_byte(dev, off, &val); |
| 839 | data[off - init_off] = val; |
| 840 | off++; |
| 841 | size--; |
| 842 | } |
| 843 | |
| 844 | if ((off & 3) && size > 2) { |
| 845 | u16 val; |
| 846 | pci_user_read_config_word(dev, off, &val); |
| 847 | data[off - init_off] = val & 0xff; |
| 848 | data[off - init_off + 1] = (val >> 8) & 0xff; |
| 849 | off += 2; |
| 850 | size -= 2; |
| 851 | } |
| 852 | |
| 853 | while (size > 3) { |
| 854 | u32 val; |
| 855 | pci_user_read_config_dword(dev, off, &val); |
| 856 | data[off - init_off] = val & 0xff; |
| 857 | data[off - init_off + 1] = (val >> 8) & 0xff; |
| 858 | data[off - init_off + 2] = (val >> 16) & 0xff; |
| 859 | data[off - init_off + 3] = (val >> 24) & 0xff; |
| 860 | off += 4; |
| 861 | size -= 4; |
| 862 | } |
| 863 | |
| 864 | if (size >= 2) { |
| 865 | u16 val; |
| 866 | pci_user_read_config_word(dev, off, &val); |
| 867 | data[off - init_off] = val & 0xff; |
| 868 | data[off - init_off + 1] = (val >> 8) & 0xff; |
| 869 | off += 2; |
| 870 | size -= 2; |
| 871 | } |
| 872 | |
| 873 | if (size > 0) { |
| 874 | u8 val; |
| 875 | pci_user_read_config_byte(dev, off, &val); |
| 876 | data[off - init_off] = val; |
| 877 | off++; |
| 878 | --size; |
| 879 | } |
| 880 | |
| 881 | pci_config_pm_runtime_put(dev); |
| 882 | |
| 883 | return count; |
| 884 | } |
| 885 | |
| 886 | static ssize_t pci_write_config(struct file *filp, struct kobject *kobj, |
| 887 | struct bin_attribute *bin_attr, char *buf, |
| 888 | loff_t off, size_t count) |
| 889 | { |
| 890 | struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj)); |
| 891 | unsigned int size = count; |
| 892 | loff_t init_off = off; |
| 893 | u8 *data = (u8 *) buf; |
| 894 | |
| 895 | if (off > dev->cfg_size) |
| 896 | return 0; |
| 897 | if (off + count > dev->cfg_size) { |
| 898 | size = dev->cfg_size - off; |
| 899 | count = size; |
| 900 | } |
| 901 | |
| 902 | pci_config_pm_runtime_get(dev); |
| 903 | |
| 904 | if ((off & 1) && size) { |
| 905 | pci_user_write_config_byte(dev, off, data[off - init_off]); |
| 906 | off++; |
| 907 | size--; |
| 908 | } |
| 909 | |
| 910 | if ((off & 3) && size > 2) { |
| 911 | u16 val = data[off - init_off]; |
| 912 | val |= (u16) data[off - init_off + 1] << 8; |
| 913 | pci_user_write_config_word(dev, off, val); |
| 914 | off += 2; |
| 915 | size -= 2; |
| 916 | } |
| 917 | |
| 918 | while (size > 3) { |
| 919 | u32 val = data[off - init_off]; |
| 920 | val |= (u32) data[off - init_off + 1] << 8; |
| 921 | val |= (u32) data[off - init_off + 2] << 16; |
| 922 | val |= (u32) data[off - init_off + 3] << 24; |
| 923 | pci_user_write_config_dword(dev, off, val); |
| 924 | off += 4; |
| 925 | size -= 4; |
| 926 | } |
| 927 | |
| 928 | if (size >= 2) { |
| 929 | u16 val = data[off - init_off]; |
| 930 | val |= (u16) data[off - init_off + 1] << 8; |
| 931 | pci_user_write_config_word(dev, off, val); |
| 932 | off += 2; |
| 933 | size -= 2; |
| 934 | } |
| 935 | |
| 936 | if (size) { |
| 937 | pci_user_write_config_byte(dev, off, data[off - init_off]); |
| 938 | off++; |
| 939 | --size; |
| 940 | } |
| 941 | |
| 942 | pci_config_pm_runtime_put(dev); |
| 943 | |
| 944 | return count; |
| 945 | } |
| 946 | |
| 947 | static ssize_t read_vpd_attr(struct file *filp, struct kobject *kobj, |
| 948 | struct bin_attribute *bin_attr, char *buf, |
| 949 | loff_t off, size_t count) |
| 950 | { |
| 951 | struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj)); |
| 952 | |
| 953 | if (bin_attr->size > 0) { |
| 954 | if (off > bin_attr->size) |
| 955 | count = 0; |
| 956 | else if (count > bin_attr->size - off) |
| 957 | count = bin_attr->size - off; |
| 958 | } |
| 959 | |
| 960 | return pci_read_vpd(dev, off, count, buf); |
| 961 | } |
| 962 | |
| 963 | static ssize_t write_vpd_attr(struct file *filp, struct kobject *kobj, |
| 964 | struct bin_attribute *bin_attr, char *buf, |
| 965 | loff_t off, size_t count) |
| 966 | { |
| 967 | struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj)); |
| 968 | |
| 969 | if (bin_attr->size > 0) { |
| 970 | if (off > bin_attr->size) |
| 971 | count = 0; |
| 972 | else if (count > bin_attr->size - off) |
| 973 | count = bin_attr->size - off; |
| 974 | } |
| 975 | |
| 976 | return pci_write_vpd(dev, off, count, buf); |
| 977 | } |
| 978 | |
| 979 | #ifdef HAVE_PCI_LEGACY |
| 980 | /** |
| 981 | * pci_read_legacy_io - read byte(s) from legacy I/O port space |
| 982 | * @filp: open sysfs file |
| 983 | * @kobj: kobject corresponding to file to read from |
| 984 | * @bin_attr: struct bin_attribute for this file |
| 985 | * @buf: buffer to store results |
| 986 | * @off: offset into legacy I/O port space |
| 987 | * @count: number of bytes to read |
| 988 | * |
| 989 | * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific |
| 990 | * callback routine (pci_legacy_read). |
| 991 | */ |
| 992 | static ssize_t pci_read_legacy_io(struct file *filp, struct kobject *kobj, |
| 993 | struct bin_attribute *bin_attr, char *buf, |
| 994 | loff_t off, size_t count) |
| 995 | { |
| 996 | struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj)); |
| 997 | |
| 998 | /* Only support 1, 2 or 4 byte accesses */ |
| 999 | if (count != 1 && count != 2 && count != 4) |
| 1000 | return -EINVAL; |
| 1001 | |
| 1002 | return pci_legacy_read(bus, off, (u32 *)buf, count); |
| 1003 | } |
| 1004 | |
| 1005 | /** |
| 1006 | * pci_write_legacy_io - write byte(s) to legacy I/O port space |
| 1007 | * @filp: open sysfs file |
| 1008 | * @kobj: kobject corresponding to file to read from |
| 1009 | * @bin_attr: struct bin_attribute for this file |
| 1010 | * @buf: buffer containing value to be written |
| 1011 | * @off: offset into legacy I/O port space |
| 1012 | * @count: number of bytes to write |
| 1013 | * |
| 1014 | * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific |
| 1015 | * callback routine (pci_legacy_write). |
| 1016 | */ |
| 1017 | static ssize_t pci_write_legacy_io(struct file *filp, struct kobject *kobj, |
| 1018 | struct bin_attribute *bin_attr, char *buf, |
| 1019 | loff_t off, size_t count) |
| 1020 | { |
| 1021 | struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj)); |
| 1022 | |
| 1023 | /* Only support 1, 2 or 4 byte accesses */ |
| 1024 | if (count != 1 && count != 2 && count != 4) |
| 1025 | return -EINVAL; |
| 1026 | |
| 1027 | return pci_legacy_write(bus, off, *(u32 *)buf, count); |
| 1028 | } |
| 1029 | |
| 1030 | /** |
| 1031 | * pci_mmap_legacy_mem - map legacy PCI memory into user memory space |
| 1032 | * @filp: open sysfs file |
| 1033 | * @kobj: kobject corresponding to device to be mapped |
| 1034 | * @attr: struct bin_attribute for this file |
| 1035 | * @vma: struct vm_area_struct passed to mmap |
| 1036 | * |
| 1037 | * Uses an arch specific callback, pci_mmap_legacy_mem_page_range, to mmap |
| 1038 | * legacy memory space (first meg of bus space) into application virtual |
| 1039 | * memory space. |
| 1040 | */ |
| 1041 | static int pci_mmap_legacy_mem(struct file *filp, struct kobject *kobj, |
| 1042 | struct bin_attribute *attr, |
| 1043 | struct vm_area_struct *vma) |
| 1044 | { |
| 1045 | struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj)); |
| 1046 | |
| 1047 | return pci_mmap_legacy_page_range(bus, vma, pci_mmap_mem); |
| 1048 | } |
| 1049 | |
| 1050 | /** |
| 1051 | * pci_mmap_legacy_io - map legacy PCI IO into user memory space |
| 1052 | * @filp: open sysfs file |
| 1053 | * @kobj: kobject corresponding to device to be mapped |
| 1054 | * @attr: struct bin_attribute for this file |
| 1055 | * @vma: struct vm_area_struct passed to mmap |
| 1056 | * |
| 1057 | * Uses an arch specific callback, pci_mmap_legacy_io_page_range, to mmap |
| 1058 | * legacy IO space (first meg of bus space) into application virtual |
| 1059 | * memory space. Returns -ENOSYS if the operation isn't supported |
| 1060 | */ |
| 1061 | static int pci_mmap_legacy_io(struct file *filp, struct kobject *kobj, |
| 1062 | struct bin_attribute *attr, |
| 1063 | struct vm_area_struct *vma) |
| 1064 | { |
| 1065 | struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj)); |
| 1066 | |
| 1067 | return pci_mmap_legacy_page_range(bus, vma, pci_mmap_io); |
| 1068 | } |
| 1069 | |
| 1070 | /** |
| 1071 | * pci_adjust_legacy_attr - adjustment of legacy file attributes |
| 1072 | * @b: bus to create files under |
| 1073 | * @mmap_type: I/O port or memory |
| 1074 | * |
| 1075 | * Stub implementation. Can be overridden by arch if necessary. |
| 1076 | */ |
| 1077 | void __weak pci_adjust_legacy_attr(struct pci_bus *b, |
| 1078 | enum pci_mmap_state mmap_type) |
| 1079 | { |
| 1080 | } |
| 1081 | |
| 1082 | /** |
| 1083 | * pci_create_legacy_files - create legacy I/O port and memory files |
| 1084 | * @b: bus to create files under |
| 1085 | * |
| 1086 | * Some platforms allow access to legacy I/O port and ISA memory space on |
| 1087 | * a per-bus basis. This routine creates the files and ties them into |
| 1088 | * their associated read, write and mmap files from pci-sysfs.c |
| 1089 | * |
| 1090 | * On error unwind, but don't propagate the error to the caller |
| 1091 | * as it is ok to set up the PCI bus without these files. |
| 1092 | */ |
| 1093 | void pci_create_legacy_files(struct pci_bus *b) |
| 1094 | { |
| 1095 | int error; |
| 1096 | |
| 1097 | b->legacy_io = kzalloc(sizeof(struct bin_attribute) * 2, |
| 1098 | GFP_ATOMIC); |
| 1099 | if (!b->legacy_io) |
| 1100 | goto kzalloc_err; |
| 1101 | |
| 1102 | sysfs_bin_attr_init(b->legacy_io); |
| 1103 | b->legacy_io->attr.name = "legacy_io"; |
| 1104 | b->legacy_io->size = 0xffff; |
| 1105 | b->legacy_io->attr.mode = S_IRUSR | S_IWUSR; |
| 1106 | b->legacy_io->read = pci_read_legacy_io; |
| 1107 | b->legacy_io->write = pci_write_legacy_io; |
| 1108 | b->legacy_io->mmap = pci_mmap_legacy_io; |
| 1109 | pci_adjust_legacy_attr(b, pci_mmap_io); |
| 1110 | error = device_create_bin_file(&b->dev, b->legacy_io); |
| 1111 | if (error) |
| 1112 | goto legacy_io_err; |
| 1113 | |
| 1114 | /* Allocated above after the legacy_io struct */ |
| 1115 | b->legacy_mem = b->legacy_io + 1; |
| 1116 | sysfs_bin_attr_init(b->legacy_mem); |
| 1117 | b->legacy_mem->attr.name = "legacy_mem"; |
| 1118 | b->legacy_mem->size = 1024*1024; |
| 1119 | b->legacy_mem->attr.mode = S_IRUSR | S_IWUSR; |
| 1120 | b->legacy_mem->mmap = pci_mmap_legacy_mem; |
| 1121 | pci_adjust_legacy_attr(b, pci_mmap_mem); |
| 1122 | error = device_create_bin_file(&b->dev, b->legacy_mem); |
| 1123 | if (error) |
| 1124 | goto legacy_mem_err; |
| 1125 | |
| 1126 | return; |
| 1127 | |
| 1128 | legacy_mem_err: |
| 1129 | device_remove_bin_file(&b->dev, b->legacy_io); |
| 1130 | legacy_io_err: |
| 1131 | kfree(b->legacy_io); |
| 1132 | b->legacy_io = NULL; |
| 1133 | kzalloc_err: |
| 1134 | printk(KERN_WARNING "pci: warning: could not create legacy I/O port and ISA memory resources to sysfs\n"); |
| 1135 | return; |
| 1136 | } |
| 1137 | |
| 1138 | void pci_remove_legacy_files(struct pci_bus *b) |
| 1139 | { |
| 1140 | if (b->legacy_io) { |
| 1141 | device_remove_bin_file(&b->dev, b->legacy_io); |
| 1142 | device_remove_bin_file(&b->dev, b->legacy_mem); |
| 1143 | kfree(b->legacy_io); /* both are allocated here */ |
| 1144 | } |
| 1145 | } |
| 1146 | #endif /* HAVE_PCI_LEGACY */ |
| 1147 | |
| 1148 | #if defined(HAVE_PCI_MMAP) || defined(ARCH_GENERIC_PCI_MMAP_RESOURCE) |
| 1149 | |
| 1150 | int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vma, |
| 1151 | enum pci_mmap_api mmap_api) |
| 1152 | { |
| 1153 | unsigned long nr, start, size; |
| 1154 | resource_size_t pci_start = 0, pci_end; |
| 1155 | |
| 1156 | if (pci_resource_len(pdev, resno) == 0) |
| 1157 | return 0; |
| 1158 | nr = vma_pages(vma); |
| 1159 | start = vma->vm_pgoff; |
| 1160 | size = ((pci_resource_len(pdev, resno) - 1) >> PAGE_SHIFT) + 1; |
| 1161 | if (mmap_api == PCI_MMAP_PROCFS) { |
| 1162 | pci_resource_to_user(pdev, resno, &pdev->resource[resno], |
| 1163 | &pci_start, &pci_end); |
| 1164 | pci_start >>= PAGE_SHIFT; |
| 1165 | } |
| 1166 | if (start >= pci_start && start < pci_start + size && |
| 1167 | start + nr <= pci_start + size) |
| 1168 | return 1; |
| 1169 | return 0; |
| 1170 | } |
| 1171 | |
| 1172 | /** |
| 1173 | * pci_mmap_resource - map a PCI resource into user memory space |
| 1174 | * @kobj: kobject for mapping |
| 1175 | * @attr: struct bin_attribute for the file being mapped |
| 1176 | * @vma: struct vm_area_struct passed into the mmap |
| 1177 | * @write_combine: 1 for write_combine mapping |
| 1178 | * |
| 1179 | * Use the regular PCI mapping routines to map a PCI resource into userspace. |
| 1180 | */ |
| 1181 | static int pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr, |
| 1182 | struct vm_area_struct *vma, int write_combine) |
| 1183 | { |
| 1184 | struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); |
| 1185 | int bar = (unsigned long)attr->private; |
| 1186 | enum pci_mmap_state mmap_type; |
| 1187 | struct resource *res = &pdev->resource[bar]; |
| 1188 | |
| 1189 | if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(res->start)) |
| 1190 | return -EINVAL; |
| 1191 | |
| 1192 | if (!pci_mmap_fits(pdev, bar, vma, PCI_MMAP_SYSFS)) { |
| 1193 | 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", |
| 1194 | current->comm, vma->vm_end-vma->vm_start, vma->vm_pgoff, |
| 1195 | pci_name(pdev), bar, |
| 1196 | (u64)pci_resource_start(pdev, bar), |
| 1197 | (u64)pci_resource_len(pdev, bar)); |
| 1198 | return -EINVAL; |
| 1199 | } |
| 1200 | mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io; |
| 1201 | |
| 1202 | return pci_mmap_resource_range(pdev, bar, vma, mmap_type, write_combine); |
| 1203 | } |
| 1204 | |
| 1205 | static int pci_mmap_resource_uc(struct file *filp, struct kobject *kobj, |
| 1206 | struct bin_attribute *attr, |
| 1207 | struct vm_area_struct *vma) |
| 1208 | { |
| 1209 | return pci_mmap_resource(kobj, attr, vma, 0); |
| 1210 | } |
| 1211 | |
| 1212 | static int pci_mmap_resource_wc(struct file *filp, struct kobject *kobj, |
| 1213 | struct bin_attribute *attr, |
| 1214 | struct vm_area_struct *vma) |
| 1215 | { |
| 1216 | return pci_mmap_resource(kobj, attr, vma, 1); |
| 1217 | } |
| 1218 | |
| 1219 | static ssize_t pci_resource_io(struct file *filp, struct kobject *kobj, |
| 1220 | struct bin_attribute *attr, char *buf, |
| 1221 | loff_t off, size_t count, bool write) |
| 1222 | { |
| 1223 | struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); |
| 1224 | int bar = (unsigned long)attr->private; |
| 1225 | unsigned long port = off; |
| 1226 | |
| 1227 | port += pci_resource_start(pdev, bar); |
| 1228 | |
| 1229 | if (port > pci_resource_end(pdev, bar)) |
| 1230 | return 0; |
| 1231 | |
| 1232 | if (port + count - 1 > pci_resource_end(pdev, bar)) |
| 1233 | return -EINVAL; |
| 1234 | |
| 1235 | switch (count) { |
| 1236 | case 1: |
| 1237 | if (write) |
| 1238 | outb(*(u8 *)buf, port); |
| 1239 | else |
| 1240 | *(u8 *)buf = inb(port); |
| 1241 | return 1; |
| 1242 | case 2: |
| 1243 | if (write) |
| 1244 | outw(*(u16 *)buf, port); |
| 1245 | else |
| 1246 | *(u16 *)buf = inw(port); |
| 1247 | return 2; |
| 1248 | case 4: |
| 1249 | if (write) |
| 1250 | outl(*(u32 *)buf, port); |
| 1251 | else |
| 1252 | *(u32 *)buf = inl(port); |
| 1253 | return 4; |
| 1254 | } |
| 1255 | return -EINVAL; |
| 1256 | } |
| 1257 | |
| 1258 | static ssize_t pci_read_resource_io(struct file *filp, struct kobject *kobj, |
| 1259 | struct bin_attribute *attr, char *buf, |
| 1260 | loff_t off, size_t count) |
| 1261 | { |
| 1262 | return pci_resource_io(filp, kobj, attr, buf, off, count, false); |
| 1263 | } |
| 1264 | |
| 1265 | static ssize_t pci_write_resource_io(struct file *filp, struct kobject *kobj, |
| 1266 | struct bin_attribute *attr, char *buf, |
| 1267 | loff_t off, size_t count) |
| 1268 | { |
| 1269 | return pci_resource_io(filp, kobj, attr, buf, off, count, true); |
| 1270 | } |
| 1271 | |
| 1272 | /** |
| 1273 | * pci_remove_resource_files - cleanup resource files |
| 1274 | * @pdev: dev to cleanup |
| 1275 | * |
| 1276 | * If we created resource files for @pdev, remove them from sysfs and |
| 1277 | * free their resources. |
| 1278 | */ |
| 1279 | static void pci_remove_resource_files(struct pci_dev *pdev) |
| 1280 | { |
| 1281 | int i; |
| 1282 | |
| 1283 | for (i = 0; i < PCI_ROM_RESOURCE; i++) { |
| 1284 | struct bin_attribute *res_attr; |
| 1285 | |
| 1286 | res_attr = pdev->res_attr[i]; |
| 1287 | if (res_attr) { |
| 1288 | sysfs_remove_bin_file(&pdev->dev.kobj, res_attr); |
| 1289 | kfree(res_attr); |
| 1290 | } |
| 1291 | |
| 1292 | res_attr = pdev->res_attr_wc[i]; |
| 1293 | if (res_attr) { |
| 1294 | sysfs_remove_bin_file(&pdev->dev.kobj, res_attr); |
| 1295 | kfree(res_attr); |
| 1296 | } |
| 1297 | } |
| 1298 | } |
| 1299 | |
| 1300 | static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine) |
| 1301 | { |
| 1302 | /* allocate attribute structure, piggyback attribute name */ |
| 1303 | int name_len = write_combine ? 13 : 10; |
| 1304 | struct bin_attribute *res_attr; |
| 1305 | char *res_attr_name; |
| 1306 | int retval; |
| 1307 | |
| 1308 | res_attr = kzalloc(sizeof(*res_attr) + name_len, GFP_ATOMIC); |
| 1309 | if (!res_attr) |
| 1310 | return -ENOMEM; |
| 1311 | |
| 1312 | res_attr_name = (char *)(res_attr + 1); |
| 1313 | |
| 1314 | sysfs_bin_attr_init(res_attr); |
| 1315 | if (write_combine) { |
| 1316 | pdev->res_attr_wc[num] = res_attr; |
| 1317 | sprintf(res_attr_name, "resource%d_wc", num); |
| 1318 | res_attr->mmap = pci_mmap_resource_wc; |
| 1319 | } else { |
| 1320 | pdev->res_attr[num] = res_attr; |
| 1321 | sprintf(res_attr_name, "resource%d", num); |
| 1322 | if (pci_resource_flags(pdev, num) & IORESOURCE_IO) { |
| 1323 | res_attr->read = pci_read_resource_io; |
| 1324 | res_attr->write = pci_write_resource_io; |
| 1325 | if (arch_can_pci_mmap_io()) |
| 1326 | res_attr->mmap = pci_mmap_resource_uc; |
| 1327 | } else { |
| 1328 | res_attr->mmap = pci_mmap_resource_uc; |
| 1329 | } |
| 1330 | } |
| 1331 | res_attr->attr.name = res_attr_name; |
| 1332 | res_attr->attr.mode = S_IRUSR | S_IWUSR; |
| 1333 | res_attr->size = pci_resource_len(pdev, num); |
| 1334 | res_attr->private = (void *)(unsigned long)num; |
| 1335 | retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr); |
| 1336 | if (retval) |
| 1337 | kfree(res_attr); |
| 1338 | |
| 1339 | return retval; |
| 1340 | } |
| 1341 | |
| 1342 | /** |
| 1343 | * pci_create_resource_files - create resource files in sysfs for @dev |
| 1344 | * @pdev: dev in question |
| 1345 | * |
| 1346 | * Walk the resources in @pdev creating files for each resource available. |
| 1347 | */ |
| 1348 | static int pci_create_resource_files(struct pci_dev *pdev) |
| 1349 | { |
| 1350 | int i; |
| 1351 | int retval; |
| 1352 | |
| 1353 | /* Expose the PCI resources from this device as files */ |
| 1354 | for (i = 0; i < PCI_ROM_RESOURCE; i++) { |
| 1355 | |
| 1356 | /* skip empty resources */ |
| 1357 | if (!pci_resource_len(pdev, i)) |
| 1358 | continue; |
| 1359 | |
| 1360 | retval = pci_create_attr(pdev, i, 0); |
| 1361 | /* for prefetchable resources, create a WC mappable file */ |
| 1362 | if (!retval && arch_can_pci_mmap_wc() && |
| 1363 | pdev->resource[i].flags & IORESOURCE_PREFETCH) |
| 1364 | retval = pci_create_attr(pdev, i, 1); |
| 1365 | if (retval) { |
| 1366 | pci_remove_resource_files(pdev); |
| 1367 | return retval; |
| 1368 | } |
| 1369 | } |
| 1370 | return 0; |
| 1371 | } |
| 1372 | #else /* !HAVE_PCI_MMAP */ |
| 1373 | int __weak pci_create_resource_files(struct pci_dev *dev) { return 0; } |
| 1374 | void __weak pci_remove_resource_files(struct pci_dev *dev) { return; } |
| 1375 | #endif /* HAVE_PCI_MMAP */ |
| 1376 | |
| 1377 | /** |
| 1378 | * pci_write_rom - used to enable access to the PCI ROM display |
| 1379 | * @filp: sysfs file |
| 1380 | * @kobj: kernel object handle |
| 1381 | * @bin_attr: struct bin_attribute for this file |
| 1382 | * @buf: user input |
| 1383 | * @off: file offset |
| 1384 | * @count: number of byte in input |
| 1385 | * |
| 1386 | * writing anything except 0 enables it |
| 1387 | */ |
| 1388 | static ssize_t pci_write_rom(struct file *filp, struct kobject *kobj, |
| 1389 | struct bin_attribute *bin_attr, char *buf, |
| 1390 | loff_t off, size_t count) |
| 1391 | { |
| 1392 | struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); |
| 1393 | |
| 1394 | if ((off == 0) && (*buf == '0') && (count == 2)) |
| 1395 | pdev->rom_attr_enabled = 0; |
| 1396 | else |
| 1397 | pdev->rom_attr_enabled = 1; |
| 1398 | |
| 1399 | return count; |
| 1400 | } |
| 1401 | |
| 1402 | /** |
| 1403 | * pci_read_rom - read a PCI ROM |
| 1404 | * @filp: sysfs file |
| 1405 | * @kobj: kernel object handle |
| 1406 | * @bin_attr: struct bin_attribute for this file |
| 1407 | * @buf: where to put the data we read from the ROM |
| 1408 | * @off: file offset |
| 1409 | * @count: number of bytes to read |
| 1410 | * |
| 1411 | * Put @count bytes starting at @off into @buf from the ROM in the PCI |
| 1412 | * device corresponding to @kobj. |
| 1413 | */ |
| 1414 | static ssize_t pci_read_rom(struct file *filp, struct kobject *kobj, |
| 1415 | struct bin_attribute *bin_attr, char *buf, |
| 1416 | loff_t off, size_t count) |
| 1417 | { |
| 1418 | struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); |
| 1419 | void __iomem *rom; |
| 1420 | size_t size; |
| 1421 | |
| 1422 | if (!pdev->rom_attr_enabled) |
| 1423 | return -EINVAL; |
| 1424 | |
| 1425 | rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */ |
| 1426 | if (!rom || !size) |
| 1427 | return -EIO; |
| 1428 | |
| 1429 | if (off >= size) |
| 1430 | count = 0; |
| 1431 | else { |
| 1432 | if (off + count > size) |
| 1433 | count = size - off; |
| 1434 | |
| 1435 | memcpy_fromio(buf, rom + off, count); |
| 1436 | } |
| 1437 | pci_unmap_rom(pdev, rom); |
| 1438 | |
| 1439 | return count; |
| 1440 | } |
| 1441 | |
| 1442 | static const struct bin_attribute pci_config_attr = { |
| 1443 | .attr = { |
| 1444 | .name = "config", |
| 1445 | .mode = S_IRUGO | S_IWUSR, |
| 1446 | }, |
| 1447 | .size = PCI_CFG_SPACE_SIZE, |
| 1448 | .read = pci_read_config, |
| 1449 | .write = pci_write_config, |
| 1450 | }; |
| 1451 | |
| 1452 | static const struct bin_attribute pcie_config_attr = { |
| 1453 | .attr = { |
| 1454 | .name = "config", |
| 1455 | .mode = S_IRUGO | S_IWUSR, |
| 1456 | }, |
| 1457 | .size = PCI_CFG_SPACE_EXP_SIZE, |
| 1458 | .read = pci_read_config, |
| 1459 | .write = pci_write_config, |
| 1460 | }; |
| 1461 | |
| 1462 | static ssize_t reset_store(struct device *dev, struct device_attribute *attr, |
| 1463 | const char *buf, size_t count) |
| 1464 | { |
| 1465 | struct pci_dev *pdev = to_pci_dev(dev); |
| 1466 | unsigned long val; |
| 1467 | ssize_t result = kstrtoul(buf, 0, &val); |
| 1468 | |
| 1469 | if (result < 0) |
| 1470 | return result; |
| 1471 | |
| 1472 | if (val != 1) |
| 1473 | return -EINVAL; |
| 1474 | |
| 1475 | result = pci_reset_function(pdev); |
| 1476 | if (result < 0) |
| 1477 | return result; |
| 1478 | |
| 1479 | return count; |
| 1480 | } |
| 1481 | |
| 1482 | static struct device_attribute reset_attr = __ATTR(reset, 0200, NULL, reset_store); |
| 1483 | |
| 1484 | static int pci_create_capabilities_sysfs(struct pci_dev *dev) |
| 1485 | { |
| 1486 | int retval; |
| 1487 | struct bin_attribute *attr; |
| 1488 | |
| 1489 | /* If the device has VPD, try to expose it in sysfs. */ |
| 1490 | if (dev->vpd) { |
| 1491 | attr = kzalloc(sizeof(*attr), GFP_ATOMIC); |
| 1492 | if (!attr) |
| 1493 | return -ENOMEM; |
| 1494 | |
| 1495 | sysfs_bin_attr_init(attr); |
| 1496 | attr->size = 0; |
| 1497 | attr->attr.name = "vpd"; |
| 1498 | attr->attr.mode = S_IRUSR | S_IWUSR; |
| 1499 | attr->read = read_vpd_attr; |
| 1500 | attr->write = write_vpd_attr; |
| 1501 | retval = sysfs_create_bin_file(&dev->dev.kobj, attr); |
| 1502 | if (retval) { |
| 1503 | kfree(attr); |
| 1504 | return retval; |
| 1505 | } |
| 1506 | dev->vpd->attr = attr; |
| 1507 | } |
| 1508 | |
| 1509 | /* Active State Power Management */ |
| 1510 | pcie_aspm_create_sysfs_dev_files(dev); |
| 1511 | |
| 1512 | if (!pci_probe_reset_function(dev)) { |
| 1513 | retval = device_create_file(&dev->dev, &reset_attr); |
| 1514 | if (retval) |
| 1515 | goto error; |
| 1516 | dev->reset_fn = 1; |
| 1517 | } |
| 1518 | return 0; |
| 1519 | |
| 1520 | error: |
| 1521 | pcie_aspm_remove_sysfs_dev_files(dev); |
| 1522 | if (dev->vpd && dev->vpd->attr) { |
| 1523 | sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr); |
| 1524 | kfree(dev->vpd->attr); |
| 1525 | } |
| 1526 | |
| 1527 | return retval; |
| 1528 | } |
| 1529 | |
| 1530 | int __must_check pci_create_sysfs_dev_files(struct pci_dev *pdev) |
| 1531 | { |
| 1532 | int retval; |
| 1533 | int rom_size; |
| 1534 | struct bin_attribute *attr; |
| 1535 | |
| 1536 | if (!sysfs_initialized) |
| 1537 | return -EACCES; |
| 1538 | |
| 1539 | if (pdev->cfg_size > PCI_CFG_SPACE_SIZE) |
| 1540 | retval = sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr); |
| 1541 | else |
| 1542 | retval = sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr); |
| 1543 | if (retval) |
| 1544 | goto err; |
| 1545 | |
| 1546 | retval = pci_create_resource_files(pdev); |
| 1547 | if (retval) |
| 1548 | goto err_config_file; |
| 1549 | |
| 1550 | /* If the device has a ROM, try to expose it in sysfs. */ |
| 1551 | rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE); |
| 1552 | if (rom_size) { |
| 1553 | attr = kzalloc(sizeof(*attr), GFP_ATOMIC); |
| 1554 | if (!attr) { |
| 1555 | retval = -ENOMEM; |
| 1556 | goto err_resource_files; |
| 1557 | } |
| 1558 | sysfs_bin_attr_init(attr); |
| 1559 | attr->size = rom_size; |
| 1560 | attr->attr.name = "rom"; |
| 1561 | attr->attr.mode = S_IRUSR | S_IWUSR; |
| 1562 | attr->read = pci_read_rom; |
| 1563 | attr->write = pci_write_rom; |
| 1564 | retval = sysfs_create_bin_file(&pdev->dev.kobj, attr); |
| 1565 | if (retval) { |
| 1566 | kfree(attr); |
| 1567 | goto err_resource_files; |
| 1568 | } |
| 1569 | pdev->rom_attr = attr; |
| 1570 | } |
| 1571 | |
| 1572 | /* add sysfs entries for various capabilities */ |
| 1573 | retval = pci_create_capabilities_sysfs(pdev); |
| 1574 | if (retval) |
| 1575 | goto err_rom_file; |
| 1576 | |
| 1577 | pci_create_firmware_label_files(pdev); |
| 1578 | |
| 1579 | return 0; |
| 1580 | |
| 1581 | err_rom_file: |
| 1582 | if (pdev->rom_attr) { |
| 1583 | sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr); |
| 1584 | kfree(pdev->rom_attr); |
| 1585 | pdev->rom_attr = NULL; |
| 1586 | } |
| 1587 | err_resource_files: |
| 1588 | pci_remove_resource_files(pdev); |
| 1589 | err_config_file: |
| 1590 | if (pdev->cfg_size > PCI_CFG_SPACE_SIZE) |
| 1591 | sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr); |
| 1592 | else |
| 1593 | sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr); |
| 1594 | err: |
| 1595 | return retval; |
| 1596 | } |
| 1597 | |
| 1598 | static void pci_remove_capabilities_sysfs(struct pci_dev *dev) |
| 1599 | { |
| 1600 | if (dev->vpd && dev->vpd->attr) { |
| 1601 | sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr); |
| 1602 | kfree(dev->vpd->attr); |
| 1603 | } |
| 1604 | |
| 1605 | pcie_aspm_remove_sysfs_dev_files(dev); |
| 1606 | if (dev->reset_fn) { |
| 1607 | device_remove_file(&dev->dev, &reset_attr); |
| 1608 | dev->reset_fn = 0; |
| 1609 | } |
| 1610 | } |
| 1611 | |
| 1612 | /** |
| 1613 | * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files |
| 1614 | * @pdev: device whose entries we should free |
| 1615 | * |
| 1616 | * Cleanup when @pdev is removed from sysfs. |
| 1617 | */ |
| 1618 | void pci_remove_sysfs_dev_files(struct pci_dev *pdev) |
| 1619 | { |
| 1620 | if (!sysfs_initialized) |
| 1621 | return; |
| 1622 | |
| 1623 | pci_remove_capabilities_sysfs(pdev); |
| 1624 | |
| 1625 | if (pdev->cfg_size > PCI_CFG_SPACE_SIZE) |
| 1626 | sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr); |
| 1627 | else |
| 1628 | sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr); |
| 1629 | |
| 1630 | pci_remove_resource_files(pdev); |
| 1631 | |
| 1632 | if (pdev->rom_attr) { |
| 1633 | sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr); |
| 1634 | kfree(pdev->rom_attr); |
| 1635 | pdev->rom_attr = NULL; |
| 1636 | } |
| 1637 | |
| 1638 | pci_remove_firmware_label_files(pdev); |
| 1639 | } |
| 1640 | |
| 1641 | static int __init pci_sysfs_init(void) |
| 1642 | { |
| 1643 | struct pci_dev *pdev = NULL; |
| 1644 | int retval; |
| 1645 | |
| 1646 | sysfs_initialized = 1; |
| 1647 | for_each_pci_dev(pdev) { |
| 1648 | retval = pci_create_sysfs_dev_files(pdev); |
| 1649 | if (retval) { |
| 1650 | pci_dev_put(pdev); |
| 1651 | return retval; |
| 1652 | } |
| 1653 | } |
| 1654 | |
| 1655 | return 0; |
| 1656 | } |
| 1657 | late_initcall(pci_sysfs_init); |
| 1658 | |
| 1659 | static struct attribute *pci_dev_dev_attrs[] = { |
| 1660 | &vga_attr.attr, |
| 1661 | NULL, |
| 1662 | }; |
| 1663 | |
| 1664 | static umode_t pci_dev_attrs_are_visible(struct kobject *kobj, |
| 1665 | struct attribute *a, int n) |
| 1666 | { |
| 1667 | struct device *dev = kobj_to_dev(kobj); |
| 1668 | struct pci_dev *pdev = to_pci_dev(dev); |
| 1669 | |
| 1670 | if (a == &vga_attr.attr) |
| 1671 | if ((pdev->class >> 8) != PCI_CLASS_DISPLAY_VGA) |
| 1672 | return 0; |
| 1673 | |
| 1674 | return a->mode; |
| 1675 | } |
| 1676 | |
| 1677 | static struct attribute *pci_dev_hp_attrs[] = { |
| 1678 | &dev_remove_attr.attr, |
| 1679 | &dev_rescan_attr.attr, |
| 1680 | NULL, |
| 1681 | }; |
| 1682 | |
| 1683 | static umode_t pci_dev_hp_attrs_are_visible(struct kobject *kobj, |
| 1684 | struct attribute *a, int n) |
| 1685 | { |
| 1686 | struct device *dev = kobj_to_dev(kobj); |
| 1687 | struct pci_dev *pdev = to_pci_dev(dev); |
| 1688 | |
| 1689 | if (pdev->is_virtfn) |
| 1690 | return 0; |
| 1691 | |
| 1692 | return a->mode; |
| 1693 | } |
| 1694 | |
| 1695 | static umode_t pci_bridge_attrs_are_visible(struct kobject *kobj, |
| 1696 | struct attribute *a, int n) |
| 1697 | { |
| 1698 | struct device *dev = kobj_to_dev(kobj); |
| 1699 | struct pci_dev *pdev = to_pci_dev(dev); |
| 1700 | |
| 1701 | if (pci_is_bridge(pdev)) |
| 1702 | return a->mode; |
| 1703 | |
| 1704 | return 0; |
| 1705 | } |
| 1706 | |
| 1707 | static umode_t pcie_dev_attrs_are_visible(struct kobject *kobj, |
| 1708 | struct attribute *a, int n) |
| 1709 | { |
| 1710 | struct device *dev = kobj_to_dev(kobj); |
| 1711 | struct pci_dev *pdev = to_pci_dev(dev); |
| 1712 | |
| 1713 | if (pci_is_pcie(pdev)) |
| 1714 | return a->mode; |
| 1715 | |
| 1716 | return 0; |
| 1717 | } |
| 1718 | |
| 1719 | static const struct attribute_group pci_dev_group = { |
| 1720 | .attrs = pci_dev_attrs, |
| 1721 | }; |
| 1722 | |
| 1723 | const struct attribute_group *pci_dev_groups[] = { |
| 1724 | &pci_dev_group, |
| 1725 | NULL, |
| 1726 | }; |
| 1727 | |
| 1728 | static const struct attribute_group pci_bridge_group = { |
| 1729 | .attrs = pci_bridge_attrs, |
| 1730 | }; |
| 1731 | |
| 1732 | const struct attribute_group *pci_bridge_groups[] = { |
| 1733 | &pci_bridge_group, |
| 1734 | NULL, |
| 1735 | }; |
| 1736 | |
| 1737 | static const struct attribute_group pcie_dev_group = { |
| 1738 | .attrs = pcie_dev_attrs, |
| 1739 | }; |
| 1740 | |
| 1741 | const struct attribute_group *pcie_dev_groups[] = { |
| 1742 | &pcie_dev_group, |
| 1743 | NULL, |
| 1744 | }; |
| 1745 | |
| 1746 | static const struct attribute_group pci_dev_hp_attr_group = { |
| 1747 | .attrs = pci_dev_hp_attrs, |
| 1748 | .is_visible = pci_dev_hp_attrs_are_visible, |
| 1749 | }; |
| 1750 | |
| 1751 | #ifdef CONFIG_PCI_IOV |
| 1752 | static struct attribute *sriov_dev_attrs[] = { |
| 1753 | &sriov_totalvfs_attr.attr, |
| 1754 | &sriov_numvfs_attr.attr, |
| 1755 | &sriov_drivers_autoprobe_attr.attr, |
| 1756 | NULL, |
| 1757 | }; |
| 1758 | |
| 1759 | static umode_t sriov_attrs_are_visible(struct kobject *kobj, |
| 1760 | struct attribute *a, int n) |
| 1761 | { |
| 1762 | struct device *dev = kobj_to_dev(kobj); |
| 1763 | |
| 1764 | if (!dev_is_pf(dev)) |
| 1765 | return 0; |
| 1766 | |
| 1767 | return a->mode; |
| 1768 | } |
| 1769 | |
| 1770 | static const struct attribute_group sriov_dev_attr_group = { |
| 1771 | .attrs = sriov_dev_attrs, |
| 1772 | .is_visible = sriov_attrs_are_visible, |
| 1773 | }; |
| 1774 | #endif /* CONFIG_PCI_IOV */ |
| 1775 | |
| 1776 | static const struct attribute_group pci_dev_attr_group = { |
| 1777 | .attrs = pci_dev_dev_attrs, |
| 1778 | .is_visible = pci_dev_attrs_are_visible, |
| 1779 | }; |
| 1780 | |
| 1781 | static const struct attribute_group pci_bridge_attr_group = { |
| 1782 | .attrs = pci_bridge_attrs, |
| 1783 | .is_visible = pci_bridge_attrs_are_visible, |
| 1784 | }; |
| 1785 | |
| 1786 | static const struct attribute_group pcie_dev_attr_group = { |
| 1787 | .attrs = pcie_dev_attrs, |
| 1788 | .is_visible = pcie_dev_attrs_are_visible, |
| 1789 | }; |
| 1790 | |
| 1791 | static const struct attribute_group *pci_dev_attr_groups[] = { |
| 1792 | &pci_dev_attr_group, |
| 1793 | &pci_dev_hp_attr_group, |
| 1794 | #ifdef CONFIG_PCI_IOV |
| 1795 | &sriov_dev_attr_group, |
| 1796 | #endif |
| 1797 | &pci_bridge_attr_group, |
| 1798 | &pcie_dev_attr_group, |
| 1799 | NULL, |
| 1800 | }; |
| 1801 | |
| 1802 | struct device_type pci_dev_type = { |
| 1803 | .groups = pci_dev_attr_groups, |
| 1804 | }; |