1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * (C) Copyright 2002-2004 Greg Kroah-Hartman <greg@kroah.com> 4 * (C) Copyright 2002-2004 IBM Corp. 5 * (C) Copyright 2003 Matthew Wilcox 6 * (C) Copyright 2003 Hewlett-Packard 7 * (C) Copyright 2004 Jon Smirl <jonsmirl@yahoo.com> 8 * (C) Copyright 2004 Silicon Graphics, Inc. Jesse Barnes <jbarnes@sgi.com> 9 * 10 * File attributes for PCI devices 11 * 12 * Modeled after usb's driverfs.c 13 */ 14 15 #include <linux/bitfield.h> 16 #include <linux/cleanup.h> 17 #include <linux/kernel.h> 18 #include <linux/sched.h> 19 #include <linux/pci.h> 20 #include <linux/stat.h> 21 #include <linux/export.h> 22 #include <linux/topology.h> 23 #include <linux/mm.h> 24 #include <linux/fs.h> 25 #include <linux/capability.h> 26 #include <linux/security.h> 27 #include <linux/slab.h> 28 #include <linux/vgaarb.h> 29 #include <linux/pm_runtime.h> 30 #include <linux/msi.h> 31 #include <linux/of.h> 32 #include <linux/aperture.h> 33 #include <linux/unaligned.h> 34 #include "pci.h" 35 36 #ifndef ARCH_PCI_DEV_GROUPS 37 #define ARCH_PCI_DEV_GROUPS 38 #endif 39 40 /* show configuration fields */ 41 #define pci_config_attr(field, format_string) \ 42 static ssize_t \ 43 field##_show(struct device *dev, struct device_attribute *attr, char *buf) \ 44 { \ 45 struct pci_dev *pdev; \ 46 \ 47 pdev = to_pci_dev(dev); \ 48 return sysfs_emit(buf, format_string, pdev->field); \ 49 } \ 50 static DEVICE_ATTR_RO(field) 51 52 pci_config_attr(vendor, "0x%04x\n"); 53 pci_config_attr(device, "0x%04x\n"); 54 pci_config_attr(subsystem_vendor, "0x%04x\n"); 55 pci_config_attr(subsystem_device, "0x%04x\n"); 56 pci_config_attr(revision, "0x%02x\n"); 57 pci_config_attr(class, "0x%06x\n"); 58 59 static ssize_t irq_show(struct device *dev, 60 struct device_attribute *attr, 61 char *buf) 62 { 63 struct pci_dev *pdev = to_pci_dev(dev); 64 65 #ifdef CONFIG_PCI_MSI 66 /* 67 * For MSI, show the first MSI IRQ; for all other cases including 68 * MSI-X, show the legacy INTx IRQ. 69 */ 70 if (pdev->msi_enabled) 71 return sysfs_emit(buf, "%u\n", pci_irq_vector(pdev, 0)); 72 #endif 73 74 return sysfs_emit(buf, "%u\n", pdev->irq); 75 } 76 static DEVICE_ATTR_RO(irq); 77 78 static ssize_t broken_parity_status_show(struct device *dev, 79 struct device_attribute *attr, 80 char *buf) 81 { 82 struct pci_dev *pdev = to_pci_dev(dev); 83 return sysfs_emit(buf, "%u\n", pdev->broken_parity_status); 84 } 85 86 static ssize_t broken_parity_status_store(struct device *dev, 87 struct device_attribute *attr, 88 const char *buf, size_t count) 89 { 90 struct pci_dev *pdev = to_pci_dev(dev); 91 unsigned long val; 92 93 if (kstrtoul(buf, 0, &val) < 0) 94 return -EINVAL; 95 96 pdev->broken_parity_status = !!val; 97 98 return count; 99 } 100 static DEVICE_ATTR_RW(broken_parity_status); 101 102 static ssize_t pci_dev_show_local_cpu(struct device *dev, bool list, 103 struct device_attribute *attr, char *buf) 104 { 105 const struct cpumask *mask; 106 107 #ifdef CONFIG_NUMA 108 if (dev_to_node(dev) == NUMA_NO_NODE) 109 mask = cpu_online_mask; 110 else 111 mask = cpumask_of_node(dev_to_node(dev)); 112 #else 113 mask = cpumask_of_pcibus(to_pci_dev(dev)->bus); 114 #endif 115 return cpumap_print_to_pagebuf(list, buf, mask); 116 } 117 118 static ssize_t local_cpus_show(struct device *dev, 119 struct device_attribute *attr, char *buf) 120 { 121 return pci_dev_show_local_cpu(dev, false, attr, buf); 122 } 123 static DEVICE_ATTR_RO(local_cpus); 124 125 static ssize_t local_cpulist_show(struct device *dev, 126 struct device_attribute *attr, char *buf) 127 { 128 return pci_dev_show_local_cpu(dev, true, attr, buf); 129 } 130 static DEVICE_ATTR_RO(local_cpulist); 131 132 /* 133 * PCI Bus Class Devices 134 */ 135 static ssize_t cpuaffinity_show(struct device *dev, 136 struct device_attribute *attr, char *buf) 137 { 138 const struct cpumask *cpumask = cpumask_of_pcibus(to_pci_bus(dev)); 139 140 return cpumap_print_to_pagebuf(false, buf, cpumask); 141 } 142 static DEVICE_ATTR_RO(cpuaffinity); 143 144 static ssize_t cpulistaffinity_show(struct device *dev, 145 struct device_attribute *attr, char *buf) 146 { 147 const struct cpumask *cpumask = cpumask_of_pcibus(to_pci_bus(dev)); 148 149 return cpumap_print_to_pagebuf(true, buf, cpumask); 150 } 151 static DEVICE_ATTR_RO(cpulistaffinity); 152 153 static ssize_t power_state_show(struct device *dev, 154 struct device_attribute *attr, char *buf) 155 { 156 struct pci_dev *pdev = to_pci_dev(dev); 157 158 return sysfs_emit(buf, "%s\n", pci_power_name(pdev->current_state)); 159 } 160 static DEVICE_ATTR_RO(power_state); 161 162 /* show resources */ 163 static ssize_t resource_show(struct device *dev, struct device_attribute *attr, 164 char *buf) 165 { 166 struct pci_dev *pci_dev = to_pci_dev(dev); 167 int i; 168 int max; 169 resource_size_t start, end; 170 size_t len = 0; 171 172 if (pci_dev->subordinate) 173 max = DEVICE_COUNT_RESOURCE; 174 else 175 max = PCI_BRIDGE_RESOURCES; 176 177 for (i = 0; i < max; i++) { 178 struct resource *res = pci_resource_n(pci_dev, i); 179 struct resource zerores = {}; 180 181 /* For backwards compatibility */ 182 if (pci_resource_is_bridge_win(i) && 183 res->flags & (IORESOURCE_UNSET | IORESOURCE_DISABLED)) 184 res = &zerores; 185 186 pci_resource_to_user(pci_dev, i, res, &start, &end); 187 len += sysfs_emit_at(buf, len, "0x%016llx 0x%016llx 0x%016llx\n", 188 (unsigned long long)start, 189 (unsigned long long)end, 190 (unsigned long long)res->flags); 191 } 192 return len; 193 } 194 static DEVICE_ATTR_RO(resource); 195 196 static ssize_t max_link_speed_show(struct device *dev, 197 struct device_attribute *attr, char *buf) 198 { 199 struct pci_dev *pdev = to_pci_dev(dev); 200 201 return sysfs_emit(buf, "%s\n", 202 pci_speed_string(pcie_get_speed_cap(pdev))); 203 } 204 static DEVICE_ATTR_RO(max_link_speed); 205 206 static ssize_t max_link_width_show(struct device *dev, 207 struct device_attribute *attr, char *buf) 208 { 209 struct pci_dev *pdev = to_pci_dev(dev); 210 ssize_t ret; 211 212 /* We read PCI_EXP_LNKCAP, so we need the device to be accessible. */ 213 pci_config_pm_runtime_get(pdev); 214 ret = sysfs_emit(buf, "%u\n", pcie_get_width_cap(pdev)); 215 pci_config_pm_runtime_put(pdev); 216 217 return ret; 218 } 219 static DEVICE_ATTR_RO(max_link_width); 220 221 static ssize_t current_link_speed_show(struct device *dev, 222 struct device_attribute *attr, char *buf) 223 { 224 struct pci_dev *pci_dev = to_pci_dev(dev); 225 u16 linkstat; 226 int err; 227 enum pci_bus_speed speed; 228 229 pci_config_pm_runtime_get(pci_dev); 230 err = pcie_capability_read_word(pci_dev, PCI_EXP_LNKSTA, &linkstat); 231 pci_config_pm_runtime_put(pci_dev); 232 233 if (err) 234 return -EINVAL; 235 236 speed = pcie_link_speed[linkstat & PCI_EXP_LNKSTA_CLS]; 237 238 return sysfs_emit(buf, "%s\n", pci_speed_string(speed)); 239 } 240 static DEVICE_ATTR_RO(current_link_speed); 241 242 static ssize_t current_link_width_show(struct device *dev, 243 struct device_attribute *attr, char *buf) 244 { 245 struct pci_dev *pci_dev = to_pci_dev(dev); 246 u16 linkstat; 247 int err; 248 249 pci_config_pm_runtime_get(pci_dev); 250 err = pcie_capability_read_word(pci_dev, PCI_EXP_LNKSTA, &linkstat); 251 pci_config_pm_runtime_put(pci_dev); 252 253 if (err) 254 return -EINVAL; 255 256 return sysfs_emit(buf, "%u\n", FIELD_GET(PCI_EXP_LNKSTA_NLW, linkstat)); 257 } 258 static DEVICE_ATTR_RO(current_link_width); 259 260 static ssize_t secondary_bus_number_show(struct device *dev, 261 struct device_attribute *attr, 262 char *buf) 263 { 264 struct pci_dev *pci_dev = to_pci_dev(dev); 265 u8 sec_bus; 266 int err; 267 268 pci_config_pm_runtime_get(pci_dev); 269 err = pci_read_config_byte(pci_dev, PCI_SECONDARY_BUS, &sec_bus); 270 pci_config_pm_runtime_put(pci_dev); 271 272 if (err) 273 return -EINVAL; 274 275 return sysfs_emit(buf, "%u\n", sec_bus); 276 } 277 static DEVICE_ATTR_RO(secondary_bus_number); 278 279 static ssize_t subordinate_bus_number_show(struct device *dev, 280 struct device_attribute *attr, 281 char *buf) 282 { 283 struct pci_dev *pci_dev = to_pci_dev(dev); 284 u8 sub_bus; 285 int err; 286 287 pci_config_pm_runtime_get(pci_dev); 288 err = pci_read_config_byte(pci_dev, PCI_SUBORDINATE_BUS, &sub_bus); 289 pci_config_pm_runtime_put(pci_dev); 290 291 if (err) 292 return -EINVAL; 293 294 return sysfs_emit(buf, "%u\n", sub_bus); 295 } 296 static DEVICE_ATTR_RO(subordinate_bus_number); 297 298 static ssize_t ari_enabled_show(struct device *dev, 299 struct device_attribute *attr, 300 char *buf) 301 { 302 struct pci_dev *pci_dev = to_pci_dev(dev); 303 304 return sysfs_emit(buf, "%u\n", pci_ari_enabled(pci_dev->bus)); 305 } 306 static DEVICE_ATTR_RO(ari_enabled); 307 308 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, 309 char *buf) 310 { 311 struct pci_dev *pci_dev = to_pci_dev(dev); 312 313 return sysfs_emit(buf, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02X\n", 314 pci_dev->vendor, pci_dev->device, 315 pci_dev->subsystem_vendor, pci_dev->subsystem_device, 316 (u8)(pci_dev->class >> 16), (u8)(pci_dev->class >> 8), 317 (u8)(pci_dev->class)); 318 } 319 static DEVICE_ATTR_RO(modalias); 320 321 static ssize_t enable_store(struct device *dev, struct device_attribute *attr, 322 const char *buf, size_t count) 323 { 324 struct pci_dev *pdev = to_pci_dev(dev); 325 unsigned long val; 326 ssize_t result = 0; 327 328 /* this can crash the machine when done on the "wrong" device */ 329 if (!capable(CAP_SYS_ADMIN)) 330 return -EPERM; 331 332 if (kstrtoul(buf, 0, &val) < 0) 333 return -EINVAL; 334 335 device_lock(dev); 336 if (dev->driver) 337 result = -EBUSY; 338 else if (val) 339 result = pci_enable_device(pdev); 340 else if (pci_is_enabled(pdev)) 341 pci_disable_device(pdev); 342 else 343 result = -EIO; 344 device_unlock(dev); 345 346 return result < 0 ? result : count; 347 } 348 349 static ssize_t enable_show(struct device *dev, struct device_attribute *attr, 350 char *buf) 351 { 352 struct pci_dev *pdev; 353 354 pdev = to_pci_dev(dev); 355 return sysfs_emit(buf, "%u\n", atomic_read(&pdev->enable_cnt)); 356 } 357 static DEVICE_ATTR_RW(enable); 358 359 #ifdef CONFIG_NUMA 360 static ssize_t numa_node_store(struct device *dev, 361 struct device_attribute *attr, const char *buf, 362 size_t count) 363 { 364 struct pci_dev *pdev = to_pci_dev(dev); 365 int node; 366 367 if (!capable(CAP_SYS_ADMIN)) 368 return -EPERM; 369 370 if (kstrtoint(buf, 0, &node) < 0) 371 return -EINVAL; 372 373 if ((node < 0 && node != NUMA_NO_NODE) || node >= MAX_NUMNODES) 374 return -EINVAL; 375 376 if (node != NUMA_NO_NODE && !node_online(node)) 377 return -EINVAL; 378 379 if (node == dev->numa_node) 380 return count; 381 382 add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_STILL_OK); 383 pci_alert(pdev, FW_BUG "Overriding NUMA node to %d. Contact your vendor for updates.", 384 node); 385 386 dev->numa_node = node; 387 return count; 388 } 389 390 static ssize_t numa_node_show(struct device *dev, struct device_attribute *attr, 391 char *buf) 392 { 393 return sysfs_emit(buf, "%d\n", dev->numa_node); 394 } 395 static DEVICE_ATTR_RW(numa_node); 396 #endif 397 398 static ssize_t dma_mask_bits_show(struct device *dev, 399 struct device_attribute *attr, char *buf) 400 { 401 struct pci_dev *pdev = to_pci_dev(dev); 402 403 return sysfs_emit(buf, "%d\n", fls64(pdev->dma_mask)); 404 } 405 static DEVICE_ATTR_RO(dma_mask_bits); 406 407 static ssize_t consistent_dma_mask_bits_show(struct device *dev, 408 struct device_attribute *attr, 409 char *buf) 410 { 411 return sysfs_emit(buf, "%d\n", fls64(dev->coherent_dma_mask)); 412 } 413 static DEVICE_ATTR_RO(consistent_dma_mask_bits); 414 415 static ssize_t msi_bus_show(struct device *dev, struct device_attribute *attr, 416 char *buf) 417 { 418 struct pci_dev *pdev = to_pci_dev(dev); 419 struct pci_bus *subordinate = pdev->subordinate; 420 421 return sysfs_emit(buf, "%u\n", subordinate ? 422 !(subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI) 423 : !pdev->no_msi); 424 } 425 426 static ssize_t msi_bus_store(struct device *dev, struct device_attribute *attr, 427 const char *buf, size_t count) 428 { 429 struct pci_dev *pdev = to_pci_dev(dev); 430 struct pci_bus *subordinate = pdev->subordinate; 431 unsigned long val; 432 433 if (!capable(CAP_SYS_ADMIN)) 434 return -EPERM; 435 436 if (kstrtoul(buf, 0, &val) < 0) 437 return -EINVAL; 438 439 /* 440 * "no_msi" and "bus_flags" only affect what happens when a driver 441 * requests MSI or MSI-X. They don't affect any drivers that have 442 * already requested MSI or MSI-X. 443 */ 444 if (!subordinate) { 445 pdev->no_msi = !val; 446 pci_info(pdev, "MSI/MSI-X %s for future drivers\n", 447 val ? "allowed" : "disallowed"); 448 return count; 449 } 450 451 if (val) 452 subordinate->bus_flags &= ~PCI_BUS_FLAGS_NO_MSI; 453 else 454 subordinate->bus_flags |= PCI_BUS_FLAGS_NO_MSI; 455 456 dev_info(&subordinate->dev, "MSI/MSI-X %s for future drivers of devices on this bus\n", 457 val ? "allowed" : "disallowed"); 458 return count; 459 } 460 static DEVICE_ATTR_RW(msi_bus); 461 462 static ssize_t rescan_store(const struct bus_type *bus, const char *buf, size_t count) 463 { 464 unsigned long val; 465 struct pci_bus *b = NULL; 466 467 if (kstrtoul(buf, 0, &val) < 0) 468 return -EINVAL; 469 470 if (val) { 471 pci_lock_rescan_remove(); 472 while ((b = pci_find_next_bus(b)) != NULL) 473 pci_rescan_bus(b); 474 pci_unlock_rescan_remove(); 475 } 476 return count; 477 } 478 static BUS_ATTR_WO(rescan); 479 480 static struct attribute *pci_bus_attrs[] = { 481 &bus_attr_rescan.attr, 482 NULL, 483 }; 484 485 static const struct attribute_group pci_bus_group = { 486 .attrs = pci_bus_attrs, 487 }; 488 489 const struct attribute_group *pci_bus_groups[] = { 490 &pci_bus_group, 491 NULL, 492 }; 493 494 static ssize_t dev_rescan_store(struct device *dev, 495 struct device_attribute *attr, const char *buf, 496 size_t count) 497 { 498 unsigned long val; 499 struct pci_dev *pdev = to_pci_dev(dev); 500 501 if (kstrtoul(buf, 0, &val) < 0) 502 return -EINVAL; 503 504 if (val) { 505 pci_lock_rescan_remove(); 506 pci_rescan_bus(pdev->bus); 507 pci_unlock_rescan_remove(); 508 } 509 return count; 510 } 511 static struct device_attribute dev_attr_dev_rescan = __ATTR(rescan, 0200, NULL, 512 dev_rescan_store); 513 514 static ssize_t remove_store(struct device *dev, struct device_attribute *attr, 515 const char *buf, size_t count) 516 { 517 unsigned long val; 518 519 if (kstrtoul(buf, 0, &val) < 0) 520 return -EINVAL; 521 522 if (val && device_remove_file_self(dev, attr)) 523 pci_stop_and_remove_bus_device_locked(to_pci_dev(dev)); 524 return count; 525 } 526 static DEVICE_ATTR_IGNORE_LOCKDEP(remove, 0220, NULL, 527 remove_store); 528 529 static ssize_t bus_rescan_store(struct device *dev, 530 struct device_attribute *attr, 531 const char *buf, size_t count) 532 { 533 unsigned long val; 534 struct pci_bus *bus = to_pci_bus(dev); 535 536 if (kstrtoul(buf, 0, &val) < 0) 537 return -EINVAL; 538 539 if (val) { 540 pci_lock_rescan_remove(); 541 if (!pci_is_root_bus(bus) && list_empty(&bus->devices)) 542 pci_rescan_bus_bridge_resize(bus->self); 543 else 544 pci_rescan_bus(bus); 545 pci_unlock_rescan_remove(); 546 } 547 return count; 548 } 549 static struct device_attribute dev_attr_bus_rescan = __ATTR(rescan, 0200, NULL, 550 bus_rescan_store); 551 552 static ssize_t reset_subordinate_store(struct device *dev, 553 struct device_attribute *attr, 554 const char *buf, size_t count) 555 { 556 struct pci_dev *pdev = to_pci_dev(dev); 557 unsigned long val; 558 559 if (!capable(CAP_SYS_ADMIN)) 560 return -EPERM; 561 562 if (kstrtoul(buf, 0, &val) < 0) 563 return -EINVAL; 564 565 if (val) { 566 int ret = pci_try_reset_bridge(pdev); 567 568 if (ret) 569 return ret; 570 } 571 572 return count; 573 } 574 static DEVICE_ATTR_WO(reset_subordinate); 575 576 #if defined(CONFIG_PM) && defined(CONFIG_ACPI) 577 static ssize_t d3cold_allowed_store(struct device *dev, 578 struct device_attribute *attr, 579 const char *buf, size_t count) 580 { 581 struct pci_dev *pdev = to_pci_dev(dev); 582 unsigned long val; 583 584 if (kstrtoul(buf, 0, &val) < 0) 585 return -EINVAL; 586 587 pdev->d3cold_allowed = !!val; 588 pci_bridge_d3_update(pdev); 589 590 pm_runtime_resume(dev); 591 592 return count; 593 } 594 595 static ssize_t d3cold_allowed_show(struct device *dev, 596 struct device_attribute *attr, char *buf) 597 { 598 struct pci_dev *pdev = to_pci_dev(dev); 599 return sysfs_emit(buf, "%u\n", pdev->d3cold_allowed); 600 } 601 static DEVICE_ATTR_RW(d3cold_allowed); 602 #endif 603 604 #ifdef CONFIG_OF 605 static ssize_t devspec_show(struct device *dev, 606 struct device_attribute *attr, char *buf) 607 { 608 struct pci_dev *pdev = to_pci_dev(dev); 609 struct device_node *np = pci_device_to_OF_node(pdev); 610 611 if (np == NULL) 612 return 0; 613 return sysfs_emit(buf, "%pOF\n", np); 614 } 615 static DEVICE_ATTR_RO(devspec); 616 #endif 617 618 static struct attribute *pci_dev_attrs[] = { 619 &dev_attr_power_state.attr, 620 &dev_attr_resource.attr, 621 &dev_attr_vendor.attr, 622 &dev_attr_device.attr, 623 &dev_attr_subsystem_vendor.attr, 624 &dev_attr_subsystem_device.attr, 625 &dev_attr_revision.attr, 626 &dev_attr_class.attr, 627 &dev_attr_irq.attr, 628 &dev_attr_local_cpus.attr, 629 &dev_attr_local_cpulist.attr, 630 &dev_attr_modalias.attr, 631 #ifdef CONFIG_NUMA 632 &dev_attr_numa_node.attr, 633 #endif 634 &dev_attr_dma_mask_bits.attr, 635 &dev_attr_consistent_dma_mask_bits.attr, 636 &dev_attr_enable.attr, 637 &dev_attr_broken_parity_status.attr, 638 &dev_attr_msi_bus.attr, 639 #if defined(CONFIG_PM) && defined(CONFIG_ACPI) 640 &dev_attr_d3cold_allowed.attr, 641 #endif 642 #ifdef CONFIG_OF 643 &dev_attr_devspec.attr, 644 #endif 645 &dev_attr_ari_enabled.attr, 646 NULL, 647 }; 648 649 static struct attribute *pci_bridge_attrs[] = { 650 &dev_attr_subordinate_bus_number.attr, 651 &dev_attr_secondary_bus_number.attr, 652 &dev_attr_reset_subordinate.attr, 653 NULL, 654 }; 655 656 static struct attribute *pcie_dev_attrs[] = { 657 &dev_attr_current_link_speed.attr, 658 &dev_attr_current_link_width.attr, 659 &dev_attr_max_link_width.attr, 660 &dev_attr_max_link_speed.attr, 661 NULL, 662 }; 663 664 static struct attribute *pcibus_attrs[] = { 665 &dev_attr_bus_rescan.attr, 666 &dev_attr_cpuaffinity.attr, 667 &dev_attr_cpulistaffinity.attr, 668 NULL, 669 }; 670 671 static const struct attribute_group pcibus_group = { 672 .attrs = pcibus_attrs, 673 }; 674 675 static ssize_t boot_vga_show(struct device *dev, struct device_attribute *attr, 676 char *buf) 677 { 678 struct pci_dev *pdev = to_pci_dev(dev); 679 struct pci_dev *vga_dev = vga_default_device(); 680 681 if (vga_dev) 682 return sysfs_emit(buf, "%u\n", (pdev == vga_dev)); 683 684 return sysfs_emit(buf, "%u\n", 685 !!(pci_resource_flags(pdev, PCI_ROM_RESOURCE) & 686 IORESOURCE_ROM_SHADOW)); 687 } 688 static DEVICE_ATTR_RO(boot_vga); 689 690 static ssize_t serial_number_show(struct device *dev, 691 struct device_attribute *attr, char *buf) 692 { 693 struct pci_dev *pci_dev = to_pci_dev(dev); 694 u64 dsn; 695 u8 bytes[8]; 696 697 dsn = pci_get_dsn(pci_dev); 698 if (!dsn) 699 return -EIO; 700 701 put_unaligned_be64(dsn, bytes); 702 return sysfs_emit(buf, "%8phD\n", bytes); 703 } 704 static DEVICE_ATTR_ADMIN_RO(serial_number); 705 706 static ssize_t pci_read_config(struct file *filp, struct kobject *kobj, 707 const struct bin_attribute *bin_attr, char *buf, 708 loff_t off, size_t count) 709 { 710 struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj)); 711 unsigned int size = 64; 712 loff_t init_off = off; 713 u8 *data = (u8 *) buf; 714 715 /* Several chips lock up trying to read undefined config space */ 716 if (file_ns_capable(filp, &init_user_ns, CAP_SYS_ADMIN)) 717 size = dev->cfg_size; 718 else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) 719 size = 128; 720 721 if (off > size) 722 return 0; 723 if (off + count > size) { 724 size -= off; 725 count = size; 726 } else { 727 size = count; 728 } 729 730 pci_config_pm_runtime_get(dev); 731 732 if ((off & 1) && size) { 733 u8 val; 734 pci_user_read_config_byte(dev, off, &val); 735 data[off - init_off] = val; 736 off++; 737 size--; 738 } 739 740 if ((off & 3) && size > 2) { 741 u16 val; 742 pci_user_read_config_word(dev, off, &val); 743 data[off - init_off] = val & 0xff; 744 data[off - init_off + 1] = (val >> 8) & 0xff; 745 off += 2; 746 size -= 2; 747 } 748 749 while (size > 3) { 750 u32 val; 751 pci_user_read_config_dword(dev, off, &val); 752 data[off - init_off] = val & 0xff; 753 data[off - init_off + 1] = (val >> 8) & 0xff; 754 data[off - init_off + 2] = (val >> 16) & 0xff; 755 data[off - init_off + 3] = (val >> 24) & 0xff; 756 off += 4; 757 size -= 4; 758 cond_resched(); 759 } 760 761 if (size >= 2) { 762 u16 val; 763 pci_user_read_config_word(dev, off, &val); 764 data[off - init_off] = val & 0xff; 765 data[off - init_off + 1] = (val >> 8) & 0xff; 766 off += 2; 767 size -= 2; 768 } 769 770 if (size > 0) { 771 u8 val; 772 pci_user_read_config_byte(dev, off, &val); 773 data[off - init_off] = val; 774 } 775 776 pci_config_pm_runtime_put(dev); 777 778 return count; 779 } 780 781 static ssize_t pci_write_config(struct file *filp, struct kobject *kobj, 782 const struct bin_attribute *bin_attr, char *buf, 783 loff_t off, size_t count) 784 { 785 struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj)); 786 unsigned int size = count; 787 loff_t init_off = off; 788 u8 *data = (u8 *) buf; 789 int ret; 790 791 ret = security_locked_down(LOCKDOWN_PCI_ACCESS); 792 if (ret) 793 return ret; 794 795 if (resource_is_exclusive(&dev->driver_exclusive_resource, off, 796 count)) { 797 pci_warn_once(dev, "%s: Unexpected write to kernel-exclusive config offset %llx", 798 current->comm, off); 799 add_taint(TAINT_USER, LOCKDEP_STILL_OK); 800 } 801 802 if (off > dev->cfg_size) 803 return 0; 804 if (off + count > dev->cfg_size) { 805 size = dev->cfg_size - off; 806 count = size; 807 } 808 809 pci_config_pm_runtime_get(dev); 810 811 if ((off & 1) && size) { 812 pci_user_write_config_byte(dev, off, data[off - init_off]); 813 off++; 814 size--; 815 } 816 817 if ((off & 3) && size > 2) { 818 u16 val = data[off - init_off]; 819 val |= (u16) data[off - init_off + 1] << 8; 820 pci_user_write_config_word(dev, off, val); 821 off += 2; 822 size -= 2; 823 } 824 825 while (size > 3) { 826 u32 val = data[off - init_off]; 827 val |= (u32) data[off - init_off + 1] << 8; 828 val |= (u32) data[off - init_off + 2] << 16; 829 val |= (u32) data[off - init_off + 3] << 24; 830 pci_user_write_config_dword(dev, off, val); 831 off += 4; 832 size -= 4; 833 } 834 835 if (size >= 2) { 836 u16 val = data[off - init_off]; 837 val |= (u16) data[off - init_off + 1] << 8; 838 pci_user_write_config_word(dev, off, val); 839 off += 2; 840 size -= 2; 841 } 842 843 if (size) 844 pci_user_write_config_byte(dev, off, data[off - init_off]); 845 846 pci_config_pm_runtime_put(dev); 847 848 return count; 849 } 850 static const BIN_ATTR(config, 0644, pci_read_config, pci_write_config, 0); 851 852 static const struct bin_attribute *const pci_dev_config_attrs[] = { 853 &bin_attr_config, 854 NULL, 855 }; 856 857 static size_t pci_dev_config_attr_bin_size(struct kobject *kobj, 858 const struct bin_attribute *a, 859 int n) 860 { 861 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); 862 863 if (pdev->cfg_size > PCI_CFG_SPACE_SIZE) 864 return PCI_CFG_SPACE_EXP_SIZE; 865 return PCI_CFG_SPACE_SIZE; 866 } 867 868 static const struct attribute_group pci_dev_config_attr_group = { 869 .bin_attrs = pci_dev_config_attrs, 870 .bin_size = pci_dev_config_attr_bin_size, 871 }; 872 873 #ifdef HAVE_PCI_LEGACY 874 /** 875 * pci_read_legacy_io - read byte(s) from legacy I/O port space 876 * @filp: open sysfs file 877 * @kobj: kobject corresponding to file to read from 878 * @bin_attr: struct bin_attribute for this file 879 * @buf: buffer to store results 880 * @off: offset into legacy I/O port space 881 * @count: number of bytes to read 882 * 883 * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific 884 * callback routine (pci_legacy_read). 885 */ 886 static ssize_t pci_read_legacy_io(struct file *filp, struct kobject *kobj, 887 const struct bin_attribute *bin_attr, 888 char *buf, loff_t off, size_t count) 889 { 890 struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj)); 891 892 /* Only support 1, 2 or 4 byte accesses */ 893 if (count != 1 && count != 2 && count != 4) 894 return -EINVAL; 895 896 return pci_legacy_read(bus, off, (u32 *)buf, count); 897 } 898 899 /** 900 * pci_write_legacy_io - write byte(s) to legacy I/O port space 901 * @filp: open sysfs file 902 * @kobj: kobject corresponding to file to read from 903 * @bin_attr: struct bin_attribute for this file 904 * @buf: buffer containing value to be written 905 * @off: offset into legacy I/O port space 906 * @count: number of bytes to write 907 * 908 * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific 909 * callback routine (pci_legacy_write). 910 */ 911 static ssize_t pci_write_legacy_io(struct file *filp, struct kobject *kobj, 912 const struct bin_attribute *bin_attr, 913 char *buf, loff_t off, size_t count) 914 { 915 struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj)); 916 917 /* Only support 1, 2 or 4 byte accesses */ 918 if (count != 1 && count != 2 && count != 4) 919 return -EINVAL; 920 921 return pci_legacy_write(bus, off, *(u32 *)buf, count); 922 } 923 924 /** 925 * pci_mmap_legacy_mem - map legacy PCI memory into user memory space 926 * @filp: open sysfs file 927 * @kobj: kobject corresponding to device to be mapped 928 * @attr: struct bin_attribute for this file 929 * @vma: struct vm_area_struct passed to mmap 930 * 931 * Uses an arch specific callback, pci_mmap_legacy_mem_page_range, to mmap 932 * legacy memory space (first meg of bus space) into application virtual 933 * memory space. 934 */ 935 static int pci_mmap_legacy_mem(struct file *filp, struct kobject *kobj, 936 const struct bin_attribute *attr, 937 struct vm_area_struct *vma) 938 { 939 struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj)); 940 941 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_mem); 942 } 943 944 /** 945 * pci_mmap_legacy_io - map legacy PCI IO into user memory space 946 * @filp: open sysfs file 947 * @kobj: kobject corresponding to device to be mapped 948 * @attr: struct bin_attribute for this file 949 * @vma: struct vm_area_struct passed to mmap 950 * 951 * Uses an arch specific callback, pci_mmap_legacy_io_page_range, to mmap 952 * legacy IO space (first meg of bus space) into application virtual 953 * memory space. Returns -ENOSYS if the operation isn't supported 954 */ 955 static int pci_mmap_legacy_io(struct file *filp, struct kobject *kobj, 956 const struct bin_attribute *attr, 957 struct vm_area_struct *vma) 958 { 959 struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj)); 960 961 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_io); 962 } 963 964 bool __weak pci_legacy_has_sparse(struct pci_bus *bus, 965 enum pci_mmap_state type) 966 { 967 return false; 968 } 969 970 static inline umode_t __pci_legacy_is_visible(struct kobject *kobj, 971 const struct bin_attribute *a, 972 enum pci_mmap_state type, 973 bool sparse) 974 { 975 struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj)); 976 977 if (pci_legacy_has_sparse(bus, type) != sparse) 978 return 0; 979 980 return a->attr.mode; 981 } 982 983 static umode_t pci_legacy_io_is_visible(struct kobject *kobj, 984 const struct bin_attribute *a, int n) 985 { 986 return __pci_legacy_is_visible(kobj, a, pci_mmap_io, false); 987 } 988 989 static umode_t pci_legacy_io_sparse_is_visible(struct kobject *kobj, 990 const struct bin_attribute *a, 991 int n) 992 { 993 return __pci_legacy_is_visible(kobj, a, pci_mmap_io, true); 994 } 995 996 static umode_t pci_legacy_mem_is_visible(struct kobject *kobj, 997 const struct bin_attribute *a, int n) 998 { 999 return __pci_legacy_is_visible(kobj, a, pci_mmap_mem, false); 1000 } 1001 1002 static umode_t pci_legacy_mem_sparse_is_visible(struct kobject *kobj, 1003 const struct bin_attribute *a, 1004 int n) 1005 { 1006 return __pci_legacy_is_visible(kobj, a, pci_mmap_mem, true); 1007 } 1008 1009 static loff_t pci_llseek_resource_legacy(struct file *filep, 1010 struct kobject *kobj __always_unused, 1011 const struct bin_attribute *attr, 1012 loff_t offset, int whence) 1013 { 1014 return fixed_size_llseek(filep, offset, whence, attr->size); 1015 } 1016 1017 static const struct bin_attribute pci_legacy_io_attr = { 1018 .attr = { .name = "legacy_io", .mode = 0600 }, 1019 .size = PCI_LEGACY_IO_SIZE, 1020 .read = pci_read_legacy_io, 1021 .write = pci_write_legacy_io, 1022 .mmap = pci_mmap_legacy_io, 1023 .llseek = pci_llseek_resource_legacy, 1024 .f_mapping = iomem_get_mapping, 1025 }; 1026 1027 static const struct bin_attribute pci_legacy_io_sparse_attr = { 1028 .attr = { .name = "legacy_io_sparse", .mode = 0600 }, 1029 .size = PCI_LEGACY_IO_SIZE << 5, 1030 .read = pci_read_legacy_io, 1031 .write = pci_write_legacy_io, 1032 .mmap = pci_mmap_legacy_io, 1033 .llseek = pci_llseek_resource_legacy, 1034 .f_mapping = iomem_get_mapping, 1035 }; 1036 1037 static const struct bin_attribute pci_legacy_mem_attr = { 1038 .attr = { .name = "legacy_mem", .mode = 0600 }, 1039 .size = PCI_LEGACY_MEM_SIZE, 1040 .mmap = pci_mmap_legacy_mem, 1041 .llseek = pci_llseek_resource_legacy, 1042 .f_mapping = iomem_get_mapping, 1043 }; 1044 1045 static const struct bin_attribute pci_legacy_mem_sparse_attr = { 1046 .attr = { .name = "legacy_mem_sparse", .mode = 0600 }, 1047 .size = PCI_LEGACY_MEM_SIZE << 5, 1048 .mmap = pci_mmap_legacy_mem, 1049 .llseek = pci_llseek_resource_legacy, 1050 .f_mapping = iomem_get_mapping, 1051 }; 1052 1053 static const struct bin_attribute *const pci_legacy_io_attrs[] = { 1054 &pci_legacy_io_attr, 1055 NULL, 1056 }; 1057 1058 static const struct bin_attribute *const pci_legacy_io_sparse_attrs[] = { 1059 &pci_legacy_io_sparse_attr, 1060 NULL, 1061 }; 1062 1063 static const struct bin_attribute *const pci_legacy_mem_attrs[] = { 1064 &pci_legacy_mem_attr, 1065 NULL, 1066 }; 1067 1068 static const struct bin_attribute *const pci_legacy_mem_sparse_attrs[] = { 1069 &pci_legacy_mem_sparse_attr, 1070 NULL, 1071 }; 1072 1073 static const struct attribute_group pci_legacy_io_group = { 1074 .bin_attrs = pci_legacy_io_attrs, 1075 .is_bin_visible = pci_legacy_io_is_visible, 1076 }; 1077 1078 static const struct attribute_group pci_legacy_io_sparse_group = { 1079 .bin_attrs = pci_legacy_io_sparse_attrs, 1080 .is_bin_visible = pci_legacy_io_sparse_is_visible, 1081 }; 1082 1083 static const struct attribute_group pci_legacy_mem_group = { 1084 .bin_attrs = pci_legacy_mem_attrs, 1085 .is_bin_visible = pci_legacy_mem_is_visible, 1086 }; 1087 1088 static const struct attribute_group pci_legacy_mem_sparse_group = { 1089 .bin_attrs = pci_legacy_mem_sparse_attrs, 1090 .is_bin_visible = pci_legacy_mem_sparse_is_visible, 1091 }; 1092 1093 #endif /* HAVE_PCI_LEGACY */ 1094 1095 const struct attribute_group *pcibus_groups[] = { 1096 &pcibus_group, 1097 #ifdef HAVE_PCI_LEGACY 1098 &pci_legacy_io_group, 1099 &pci_legacy_io_sparse_group, 1100 &pci_legacy_mem_group, 1101 &pci_legacy_mem_sparse_group, 1102 #endif 1103 NULL, 1104 }; 1105 1106 #if defined(HAVE_PCI_MMAP) || defined(ARCH_GENERIC_PCI_MMAP_RESOURCE) 1107 /** 1108 * pci_mmap_resource - map a PCI resource into user memory space 1109 * @kobj: kobject for mapping 1110 * @attr: struct bin_attribute for the file being mapped 1111 * @vma: struct vm_area_struct passed into the mmap 1112 * @write_combine: 1 for write_combine mapping 1113 * 1114 * Use the regular PCI mapping routines to map a PCI resource into userspace. 1115 */ 1116 static int pci_mmap_resource(struct kobject *kobj, const struct bin_attribute *attr, 1117 struct vm_area_struct *vma, int write_combine) 1118 { 1119 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); 1120 int bar = (unsigned long)attr->private; 1121 enum pci_mmap_state mmap_type; 1122 int ret; 1123 1124 ret = security_locked_down(LOCKDOWN_PCI_ACCESS); 1125 if (ret) 1126 return ret; 1127 1128 if (!pci_resource_is_mem(pdev, bar) && 1129 !(pci_resource_is_io(pdev, bar) && arch_can_pci_mmap_io())) 1130 return -EIO; 1131 1132 if (pci_resource_is_mem(pdev, bar) && 1133 iomem_is_exclusive(pci_resource_start(pdev, bar))) 1134 return -EINVAL; 1135 1136 if (!pci_mmap_fits(pdev, bar, vma, PCI_MMAP_SYSFS)) 1137 return -EINVAL; 1138 1139 mmap_type = pci_resource_is_mem(pdev, bar) ? pci_mmap_mem : pci_mmap_io; 1140 1141 return pci_mmap_resource_range(pdev, bar, vma, mmap_type, write_combine); 1142 } 1143 1144 static int pci_mmap_resource_uc(struct file *filp, struct kobject *kobj, 1145 const struct bin_attribute *attr, 1146 struct vm_area_struct *vma) 1147 { 1148 return pci_mmap_resource(kobj, attr, vma, 0); 1149 } 1150 1151 static int pci_mmap_resource_wc(struct file *filp, struct kobject *kobj, 1152 const struct bin_attribute *attr, 1153 struct vm_area_struct *vma) 1154 { 1155 return pci_mmap_resource(kobj, attr, vma, 1); 1156 } 1157 1158 static ssize_t pci_resource_io(struct file *filp, struct kobject *kobj, 1159 const struct bin_attribute *attr, char *buf, 1160 loff_t off, size_t count, bool write) 1161 { 1162 #ifdef CONFIG_HAS_IOPORT 1163 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); 1164 int bar = (unsigned long)attr->private; 1165 unsigned long port = off; 1166 1167 if (!pci_resource_is_io(pdev, bar)) 1168 return -EIO; 1169 1170 port += pci_resource_start(pdev, bar); 1171 1172 if (port > pci_resource_end(pdev, bar)) 1173 return 0; 1174 1175 if (port + count - 1 > pci_resource_end(pdev, bar)) 1176 return -EINVAL; 1177 1178 switch (count) { 1179 case 1: 1180 if (write) 1181 outb(*(u8 *)buf, port); 1182 else 1183 *(u8 *)buf = inb(port); 1184 return 1; 1185 case 2: 1186 if (write) 1187 outw(*(u16 *)buf, port); 1188 else 1189 *(u16 *)buf = inw(port); 1190 return 2; 1191 case 4: 1192 if (write) 1193 outl(*(u32 *)buf, port); 1194 else 1195 *(u32 *)buf = inl(port); 1196 return 4; 1197 } 1198 return -EINVAL; 1199 #else 1200 return -ENXIO; 1201 #endif 1202 } 1203 1204 static ssize_t pci_read_resource(struct file *filp, struct kobject *kobj, 1205 const struct bin_attribute *attr, char *buf, 1206 loff_t off, size_t count) 1207 { 1208 return pci_resource_io(filp, kobj, attr, buf, off, count, false); 1209 } 1210 1211 static ssize_t pci_write_resource(struct file *filp, struct kobject *kobj, 1212 const struct bin_attribute *attr, char *buf, 1213 loff_t off, size_t count) 1214 { 1215 int ret; 1216 1217 ret = security_locked_down(LOCKDOWN_PCI_ACCESS); 1218 if (ret) 1219 return ret; 1220 1221 return pci_resource_io(filp, kobj, attr, buf, off, count, true); 1222 } 1223 1224 static loff_t pci_llseek_resource(struct file *filep, 1225 struct kobject *kobj, 1226 const struct bin_attribute *attr, 1227 loff_t offset, int whence) 1228 { 1229 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); 1230 int bar = (unsigned long)attr->private; 1231 1232 return fixed_size_llseek(filep, offset, whence, 1233 pci_resource_len(pdev, bar)); 1234 } 1235 1236 /* 1237 * generic_file_llseek() consults f_mapping->host to determine 1238 * the file size. As iomem_inode knows nothing about the 1239 * attribute, it's not going to work, so override it as well. 1240 */ 1241 #if arch_can_pci_mmap_io() 1242 # define __PCI_RESOURCE_IO_MMAP_ATTRS \ 1243 .f_mapping = iomem_get_mapping, \ 1244 .llseek = pci_llseek_resource, \ 1245 .mmap = pci_mmap_resource_uc, 1246 #else 1247 # define __PCI_RESOURCE_IO_MMAP_ATTRS 1248 #endif 1249 1250 #define pci_dev_resource_io_attr(_bar) \ 1251 static const struct bin_attribute dev_resource##_bar##_io_attr = { \ 1252 .attr = { .name = "resource" __stringify(_bar), .mode = 0600 }, \ 1253 .private = (void *)(unsigned long)(_bar), \ 1254 .read = pci_read_resource, \ 1255 .write = pci_write_resource, \ 1256 __PCI_RESOURCE_IO_MMAP_ATTRS \ 1257 } 1258 1259 #define pci_dev_resource_uc_attr(_bar) \ 1260 static const struct bin_attribute dev_resource##_bar##_uc_attr = { \ 1261 .attr = { .name = "resource" __stringify(_bar), .mode = 0600 }, \ 1262 .private = (void *)(unsigned long)(_bar), \ 1263 .f_mapping = iomem_get_mapping, \ 1264 .llseek = pci_llseek_resource, \ 1265 .mmap = pci_mmap_resource_uc, \ 1266 } 1267 1268 #define pci_dev_resource_wc_attr(_bar) \ 1269 static const struct bin_attribute dev_resource##_bar##_wc_attr = { \ 1270 .attr = { .name = "resource" __stringify(_bar) "_wc", .mode = 0600 }, \ 1271 .private = (void *)(unsigned long)(_bar), \ 1272 .f_mapping = iomem_get_mapping, \ 1273 .llseek = pci_llseek_resource, \ 1274 .mmap = pci_mmap_resource_wc, \ 1275 } 1276 1277 static inline umode_t 1278 __pci_resource_attr_is_visible(struct kobject *kobj, 1279 const struct bin_attribute *a, 1280 int bar, bool write_combine, 1281 unsigned long flags) 1282 { 1283 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); 1284 1285 if (pdev->non_mappable_bars) 1286 return 0; 1287 1288 if (!pci_resource_len(pdev, bar)) 1289 return 0; 1290 1291 if ((pci_resource_flags(pdev, bar) & flags) != flags) 1292 return 0; 1293 1294 if (write_combine && !arch_can_pci_mmap_wc()) 1295 return 0; 1296 1297 return a->attr.mode; 1298 } 1299 1300 static umode_t pci_dev_resource_io_is_visible(struct kobject *kobj, 1301 const struct bin_attribute *a, 1302 int n) 1303 { 1304 return __pci_resource_attr_is_visible(kobj, a, n, false, 1305 IORESOURCE_IO); 1306 } 1307 1308 static umode_t pci_dev_resource_uc_is_visible(struct kobject *kobj, 1309 const struct bin_attribute *a, 1310 int n) 1311 { 1312 return __pci_resource_attr_is_visible(kobj, a, n, false, 1313 IORESOURCE_MEM); 1314 } 1315 1316 static umode_t pci_dev_resource_wc_is_visible(struct kobject *kobj, 1317 const struct bin_attribute *a, 1318 int n) 1319 { 1320 return __pci_resource_attr_is_visible(kobj, a, n, true, 1321 IORESOURCE_MEM | IORESOURCE_PREFETCH); 1322 } 1323 1324 static size_t pci_dev_resource_bin_size(struct kobject *kobj, 1325 const struct bin_attribute *a, 1326 int n) 1327 { 1328 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); 1329 1330 return pci_resource_len(pdev, n); 1331 } 1332 1333 pci_dev_resource_io_attr(0); 1334 pci_dev_resource_io_attr(1); 1335 pci_dev_resource_io_attr(2); 1336 pci_dev_resource_io_attr(3); 1337 pci_dev_resource_io_attr(4); 1338 pci_dev_resource_io_attr(5); 1339 1340 pci_dev_resource_uc_attr(0); 1341 pci_dev_resource_uc_attr(1); 1342 pci_dev_resource_uc_attr(2); 1343 pci_dev_resource_uc_attr(3); 1344 pci_dev_resource_uc_attr(4); 1345 pci_dev_resource_uc_attr(5); 1346 1347 pci_dev_resource_wc_attr(0); 1348 pci_dev_resource_wc_attr(1); 1349 pci_dev_resource_wc_attr(2); 1350 pci_dev_resource_wc_attr(3); 1351 pci_dev_resource_wc_attr(4); 1352 pci_dev_resource_wc_attr(5); 1353 1354 static const struct bin_attribute *const pci_dev_resource_io_attrs[] = { 1355 &dev_resource0_io_attr, 1356 &dev_resource1_io_attr, 1357 &dev_resource2_io_attr, 1358 &dev_resource3_io_attr, 1359 &dev_resource4_io_attr, 1360 &dev_resource5_io_attr, 1361 NULL, 1362 }; 1363 1364 static const struct bin_attribute *const pci_dev_resource_uc_attrs[] = { 1365 &dev_resource0_uc_attr, 1366 &dev_resource1_uc_attr, 1367 &dev_resource2_uc_attr, 1368 &dev_resource3_uc_attr, 1369 &dev_resource4_uc_attr, 1370 &dev_resource5_uc_attr, 1371 NULL, 1372 }; 1373 1374 static const struct bin_attribute *const pci_dev_resource_wc_attrs[] = { 1375 &dev_resource0_wc_attr, 1376 &dev_resource1_wc_attr, 1377 &dev_resource2_wc_attr, 1378 &dev_resource3_wc_attr, 1379 &dev_resource4_wc_attr, 1380 &dev_resource5_wc_attr, 1381 NULL, 1382 }; 1383 1384 static const struct attribute_group pci_dev_resource_io_attr_group = { 1385 .bin_attrs = pci_dev_resource_io_attrs, 1386 .is_bin_visible = pci_dev_resource_io_is_visible, 1387 .bin_size = pci_dev_resource_bin_size, 1388 }; 1389 1390 static const struct attribute_group pci_dev_resource_uc_attr_group = { 1391 .bin_attrs = pci_dev_resource_uc_attrs, 1392 .is_bin_visible = pci_dev_resource_uc_is_visible, 1393 .bin_size = pci_dev_resource_bin_size, 1394 }; 1395 1396 static const struct attribute_group pci_dev_resource_wc_attr_group = { 1397 .bin_attrs = pci_dev_resource_wc_attrs, 1398 .is_bin_visible = pci_dev_resource_wc_is_visible, 1399 .bin_size = pci_dev_resource_bin_size, 1400 }; 1401 1402 static const struct attribute_group *pci_dev_resource_attr_groups[] = { 1403 &pci_dev_resource_io_attr_group, 1404 &pci_dev_resource_uc_attr_group, 1405 &pci_dev_resource_wc_attr_group, 1406 NULL, 1407 }; 1408 #else 1409 #define pci_dev_resource_attr_groups NULL 1410 #endif 1411 1412 /** 1413 * pci_write_rom - used to enable access to the PCI ROM display 1414 * @filp: sysfs file 1415 * @kobj: kernel object handle 1416 * @bin_attr: struct bin_attribute for this file 1417 * @buf: user input 1418 * @off: file offset 1419 * @count: number of byte in input 1420 * 1421 * Writing a boolean value enables or disables the ROM display. 1422 */ 1423 static ssize_t pci_write_rom(struct file *filp, struct kobject *kobj, 1424 const struct bin_attribute *bin_attr, char *buf, 1425 loff_t off, size_t count) 1426 { 1427 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); 1428 bool enable; 1429 1430 if (kstrtobool(buf, &enable)) 1431 return -EINVAL; 1432 1433 pdev->rom_attr_enabled = enable; 1434 1435 return count; 1436 } 1437 1438 /** 1439 * pci_read_rom - read a PCI ROM 1440 * @filp: sysfs file 1441 * @kobj: kernel object handle 1442 * @bin_attr: struct bin_attribute for this file 1443 * @buf: where to put the data we read from the ROM 1444 * @off: file offset 1445 * @count: number of bytes to read 1446 * 1447 * Put @count bytes starting at @off into @buf from the ROM in the PCI 1448 * device corresponding to @kobj. 1449 */ 1450 static ssize_t pci_read_rom(struct file *filp, struct kobject *kobj, 1451 const struct bin_attribute *bin_attr, char *buf, 1452 loff_t off, size_t count) 1453 { 1454 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); 1455 void __iomem *rom; 1456 size_t size; 1457 1458 if (!pdev->rom_attr_enabled) 1459 return -EINVAL; 1460 1461 rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */ 1462 if (!rom || !size) 1463 return -EIO; 1464 1465 if (off >= size) 1466 count = 0; 1467 else { 1468 if (off + count > size) 1469 count = size - off; 1470 1471 memcpy_fromio(buf, rom + off, count); 1472 } 1473 pci_unmap_rom(pdev, rom); 1474 1475 return count; 1476 } 1477 static const BIN_ATTR(rom, 0600, pci_read_rom, pci_write_rom, 0); 1478 1479 static const struct bin_attribute *const pci_dev_rom_attrs[] = { 1480 &bin_attr_rom, 1481 NULL, 1482 }; 1483 1484 static umode_t pci_dev_rom_attr_is_visible(struct kobject *kobj, 1485 const struct bin_attribute *a, int n) 1486 { 1487 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); 1488 1489 /* If the device has a ROM, try to expose it in sysfs. */ 1490 if (!pci_resource_end(pdev, PCI_ROM_RESOURCE)) 1491 return 0; 1492 1493 return a->attr.mode; 1494 } 1495 1496 static size_t pci_dev_rom_attr_bin_size(struct kobject *kobj, 1497 const struct bin_attribute *a, int n) 1498 { 1499 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); 1500 1501 return pci_resource_len(pdev, PCI_ROM_RESOURCE); 1502 } 1503 1504 static const struct attribute_group pci_dev_rom_attr_group = { 1505 .bin_attrs = pci_dev_rom_attrs, 1506 .is_bin_visible = pci_dev_rom_attr_is_visible, 1507 .bin_size = pci_dev_rom_attr_bin_size, 1508 }; 1509 1510 static ssize_t reset_store(struct device *dev, struct device_attribute *attr, 1511 const char *buf, size_t count) 1512 { 1513 struct pci_dev *pdev = to_pci_dev(dev); 1514 unsigned long val; 1515 ssize_t result; 1516 1517 if (kstrtoul(buf, 0, &val) < 0) 1518 return -EINVAL; 1519 1520 if (val != 1) 1521 return -EINVAL; 1522 1523 pm_runtime_get_sync(dev); 1524 result = pci_reset_function(pdev); 1525 pm_runtime_put(dev); 1526 if (result < 0) 1527 return result; 1528 1529 return count; 1530 } 1531 static DEVICE_ATTR_WO(reset); 1532 1533 static struct attribute *pci_dev_reset_attrs[] = { 1534 &dev_attr_reset.attr, 1535 NULL, 1536 }; 1537 1538 static umode_t pci_dev_reset_attr_is_visible(struct kobject *kobj, 1539 struct attribute *a, int n) 1540 { 1541 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); 1542 1543 if (!pci_reset_supported(pdev)) 1544 return 0; 1545 1546 return a->mode; 1547 } 1548 1549 static const struct attribute_group pci_dev_reset_attr_group = { 1550 .attrs = pci_dev_reset_attrs, 1551 .is_visible = pci_dev_reset_attr_is_visible, 1552 }; 1553 1554 static ssize_t reset_method_show(struct device *dev, 1555 struct device_attribute *attr, char *buf) 1556 { 1557 struct pci_dev *pdev = to_pci_dev(dev); 1558 ssize_t len = 0; 1559 int i, m; 1560 1561 for (i = 0; i < PCI_NUM_RESET_METHODS; i++) { 1562 m = pdev->reset_methods[i]; 1563 if (!m) 1564 break; 1565 1566 len += sysfs_emit_at(buf, len, "%s%s", len ? " " : "", 1567 pci_reset_fn_methods[m].name); 1568 } 1569 1570 if (len) 1571 len += sysfs_emit_at(buf, len, "\n"); 1572 1573 return len; 1574 } 1575 1576 static int reset_method_lookup(const char *name) 1577 { 1578 int m; 1579 1580 for (m = 1; m < PCI_NUM_RESET_METHODS; m++) { 1581 if (sysfs_streq(name, pci_reset_fn_methods[m].name)) 1582 return m; 1583 } 1584 1585 return 0; /* not found */ 1586 } 1587 1588 static ssize_t reset_method_store(struct device *dev, 1589 struct device_attribute *attr, 1590 const char *buf, size_t count) 1591 { 1592 struct pci_dev *pdev = to_pci_dev(dev); 1593 char *tmp_options, *name; 1594 int m, n; 1595 u8 reset_methods[PCI_NUM_RESET_METHODS] = {}; 1596 1597 if (sysfs_streq(buf, "")) { 1598 pdev->reset_methods[0] = 0; 1599 pci_warn(pdev, "All device reset methods disabled by user"); 1600 return count; 1601 } 1602 1603 PM_RUNTIME_ACQUIRE(dev, pm); 1604 if (PM_RUNTIME_ACQUIRE_ERR(&pm)) 1605 return -ENXIO; 1606 1607 if (sysfs_streq(buf, "default")) { 1608 pci_init_reset_methods(pdev); 1609 return count; 1610 } 1611 1612 char *options __free(kfree) = kstrndup(buf, count, GFP_KERNEL); 1613 if (!options) 1614 return -ENOMEM; 1615 1616 n = 0; 1617 tmp_options = options; 1618 while ((name = strsep(&tmp_options, " ")) != NULL) { 1619 if (sysfs_streq(name, "")) 1620 continue; 1621 1622 name = strim(name); 1623 1624 /* Leave previous methods unchanged if input is invalid */ 1625 m = reset_method_lookup(name); 1626 if (!m) { 1627 pci_err(pdev, "Invalid reset method '%s'", name); 1628 return -EINVAL; 1629 } 1630 1631 if (pci_reset_fn_methods[m].reset_fn(pdev, PCI_RESET_PROBE)) { 1632 pci_err(pdev, "Unsupported reset method '%s'", name); 1633 return -EINVAL; 1634 } 1635 1636 if (n == PCI_NUM_RESET_METHODS - 1) { 1637 pci_err(pdev, "Too many reset methods\n"); 1638 return -EINVAL; 1639 } 1640 1641 reset_methods[n++] = m; 1642 } 1643 1644 reset_methods[n] = 0; 1645 1646 /* Warn if dev-specific supported but not highest priority */ 1647 if (pci_reset_fn_methods[1].reset_fn(pdev, PCI_RESET_PROBE) == 0 && 1648 reset_methods[0] != 1) 1649 pci_warn(pdev, "Device-specific reset disabled/de-prioritized by user"); 1650 memcpy(pdev->reset_methods, reset_methods, sizeof(pdev->reset_methods)); 1651 return count; 1652 } 1653 static DEVICE_ATTR_RW(reset_method); 1654 1655 static struct attribute *pci_dev_reset_method_attrs[] = { 1656 &dev_attr_reset_method.attr, 1657 NULL, 1658 }; 1659 1660 static const struct attribute_group pci_dev_reset_method_attr_group = { 1661 .attrs = pci_dev_reset_method_attrs, 1662 .is_visible = pci_dev_reset_attr_is_visible, 1663 }; 1664 1665 #if defined(HAVE_PCI_MMAP) || defined(ARCH_GENERIC_PCI_MMAP_RESOURCE) 1666 static ssize_t __resource_resize_show(struct device *dev, int n, char *buf) 1667 { 1668 struct pci_dev *pdev = to_pci_dev(dev); 1669 ssize_t ret; 1670 1671 pci_config_pm_runtime_get(pdev); 1672 1673 ret = sysfs_emit(buf, "%016llx\n", 1674 pci_rebar_get_possible_sizes(pdev, n)); 1675 1676 pci_config_pm_runtime_put(pdev); 1677 1678 return ret; 1679 } 1680 1681 static ssize_t __resource_resize_store(struct device *dev, int n, 1682 const char *buf, size_t count) 1683 { 1684 struct pci_dev *pdev = to_pci_dev(dev); 1685 struct pci_bus *bus = pdev->bus; 1686 unsigned long size; 1687 int ret; 1688 u16 cmd; 1689 1690 if (!capable(CAP_SYS_ADMIN)) 1691 return -EPERM; 1692 1693 if (kstrtoul(buf, 0, &size) < 0) 1694 return -EINVAL; 1695 1696 device_lock(dev); 1697 if (dev->driver || pci_num_vf(pdev)) { 1698 ret = -EBUSY; 1699 goto unlock; 1700 } 1701 1702 pci_config_pm_runtime_get(pdev); 1703 1704 if ((pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA) { 1705 ret = aperture_remove_conflicting_pci_devices(pdev, 1706 "resourceN_resize"); 1707 if (ret) 1708 goto pm_put; 1709 } 1710 1711 pci_read_config_word(pdev, PCI_COMMAND, &cmd); 1712 pci_write_config_word(pdev, PCI_COMMAND, 1713 cmd & ~PCI_COMMAND_MEMORY); 1714 1715 sysfs_remove_groups(&pdev->dev.kobj, pci_dev_resource_attr_groups); 1716 1717 ret = pci_resize_resource(pdev, n, size, 0); 1718 if (ret) 1719 pci_warn(pdev, "Failed to resize BAR %d: %pe\n", 1720 n, ERR_PTR(ret)); 1721 1722 pci_assign_unassigned_bus_resources(bus); 1723 1724 if (sysfs_create_groups(&pdev->dev.kobj, pci_dev_resource_attr_groups)) 1725 pci_warn(pdev, "Failed to recreate resource groups after BAR resizing\n"); 1726 1727 pci_write_config_word(pdev, PCI_COMMAND, cmd); 1728 pm_put: 1729 pci_config_pm_runtime_put(pdev); 1730 unlock: 1731 device_unlock(dev); 1732 1733 return ret ? ret : count; 1734 } 1735 1736 #define pci_dev_resource_resize_attr(n) \ 1737 static ssize_t resource##n##_resize_show(struct device *dev, \ 1738 struct device_attribute *attr, \ 1739 char *buf) \ 1740 { \ 1741 return __resource_resize_show(dev, n, buf); \ 1742 } \ 1743 static ssize_t resource##n##_resize_store(struct device *dev, \ 1744 struct device_attribute *attr,\ 1745 const char *buf, size_t count)\ 1746 { \ 1747 return __resource_resize_store(dev, n, buf, count); \ 1748 } \ 1749 static DEVICE_ATTR_RW(resource##n##_resize) 1750 1751 pci_dev_resource_resize_attr(0); 1752 pci_dev_resource_resize_attr(1); 1753 pci_dev_resource_resize_attr(2); 1754 pci_dev_resource_resize_attr(3); 1755 pci_dev_resource_resize_attr(4); 1756 pci_dev_resource_resize_attr(5); 1757 1758 static struct attribute *resource_resize_attrs[] = { 1759 &dev_attr_resource0_resize.attr, 1760 &dev_attr_resource1_resize.attr, 1761 &dev_attr_resource2_resize.attr, 1762 &dev_attr_resource3_resize.attr, 1763 &dev_attr_resource4_resize.attr, 1764 &dev_attr_resource5_resize.attr, 1765 NULL, 1766 }; 1767 1768 static umode_t resource_resize_attr_is_visible(struct kobject *kobj, 1769 struct attribute *a, int n) 1770 { 1771 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); 1772 1773 return pci_rebar_get_current_size(pdev, n) < 0 ? 0 : a->mode; 1774 } 1775 1776 static const struct attribute_group pci_dev_resource_resize_attr_group = { 1777 .attrs = resource_resize_attrs, 1778 .is_visible = resource_resize_attr_is_visible, 1779 }; 1780 #endif 1781 1782 static struct attribute *pci_dev_dev_attrs[] = { 1783 &dev_attr_boot_vga.attr, 1784 &dev_attr_serial_number.attr, 1785 NULL, 1786 }; 1787 1788 static umode_t pci_dev_attrs_are_visible(struct kobject *kobj, 1789 struct attribute *a, int n) 1790 { 1791 struct device *dev = kobj_to_dev(kobj); 1792 struct pci_dev *pdev = to_pci_dev(dev); 1793 1794 if (a == &dev_attr_boot_vga.attr && pci_is_vga(pdev)) 1795 return a->mode; 1796 1797 if (a == &dev_attr_serial_number.attr && pci_get_dsn(pdev)) 1798 return a->mode; 1799 1800 return 0; 1801 } 1802 1803 static struct attribute *pci_dev_hp_attrs[] = { 1804 &dev_attr_remove.attr, 1805 &dev_attr_dev_rescan.attr, 1806 NULL, 1807 }; 1808 1809 static umode_t pci_dev_hp_attrs_are_visible(struct kobject *kobj, 1810 struct attribute *a, int n) 1811 { 1812 struct device *dev = kobj_to_dev(kobj); 1813 struct pci_dev *pdev = to_pci_dev(dev); 1814 1815 if (pdev->is_virtfn) 1816 return 0; 1817 1818 return a->mode; 1819 } 1820 1821 static umode_t pci_bridge_attrs_are_visible(struct kobject *kobj, 1822 struct attribute *a, int n) 1823 { 1824 struct device *dev = kobj_to_dev(kobj); 1825 struct pci_dev *pdev = to_pci_dev(dev); 1826 1827 if (pci_is_bridge(pdev)) 1828 return a->mode; 1829 1830 return 0; 1831 } 1832 1833 static umode_t pcie_dev_attrs_are_visible(struct kobject *kobj, 1834 struct attribute *a, int n) 1835 { 1836 struct device *dev = kobj_to_dev(kobj); 1837 struct pci_dev *pdev = to_pci_dev(dev); 1838 1839 if (pci_is_pcie(pdev)) 1840 return a->mode; 1841 1842 return 0; 1843 } 1844 1845 static const struct attribute_group pci_dev_group = { 1846 .attrs = pci_dev_attrs, 1847 }; 1848 1849 const struct attribute_group *pci_dev_groups[] = { 1850 &pci_dev_group, 1851 #if defined(HAVE_PCI_MMAP) || defined(ARCH_GENERIC_PCI_MMAP_RESOURCE) 1852 &pci_dev_resource_io_attr_group, 1853 &pci_dev_resource_uc_attr_group, 1854 &pci_dev_resource_wc_attr_group, 1855 &pci_dev_resource_resize_attr_group, 1856 #endif 1857 &pci_dev_config_attr_group, 1858 &pci_dev_rom_attr_group, 1859 &pci_dev_reset_attr_group, 1860 &pci_dev_reset_method_attr_group, 1861 &pci_dev_vpd_attr_group, 1862 #ifdef CONFIG_DMI 1863 &pci_dev_smbios_attr_group, 1864 #endif 1865 #ifdef CONFIG_ACPI 1866 &pci_dev_acpi_attr_group, 1867 #endif 1868 ARCH_PCI_DEV_GROUPS 1869 NULL, 1870 }; 1871 1872 static const struct attribute_group pci_dev_hp_attr_group = { 1873 .attrs = pci_dev_hp_attrs, 1874 .is_visible = pci_dev_hp_attrs_are_visible, 1875 }; 1876 1877 static const struct attribute_group pci_dev_attr_group = { 1878 .attrs = pci_dev_dev_attrs, 1879 .is_visible = pci_dev_attrs_are_visible, 1880 }; 1881 1882 static const struct attribute_group pci_bridge_attr_group = { 1883 .attrs = pci_bridge_attrs, 1884 .is_visible = pci_bridge_attrs_are_visible, 1885 }; 1886 1887 static const struct attribute_group pcie_dev_attr_group = { 1888 .attrs = pcie_dev_attrs, 1889 .is_visible = pcie_dev_attrs_are_visible, 1890 }; 1891 1892 const struct attribute_group *pci_dev_attr_groups[] = { 1893 &pci_dev_attr_group, 1894 &pci_dev_hp_attr_group, 1895 #ifdef CONFIG_PCI_IOV 1896 &sriov_pf_dev_attr_group, 1897 &sriov_vf_dev_attr_group, 1898 #endif 1899 &pci_bridge_attr_group, 1900 &pcie_dev_attr_group, 1901 #ifdef CONFIG_PCIEAER 1902 &aer_stats_attr_group, 1903 &aer_attr_group, 1904 #endif 1905 #ifdef CONFIG_PCIEASPM 1906 &aspm_ctrl_attr_group, 1907 #endif 1908 #ifdef CONFIG_PCI_DOE 1909 &pci_doe_sysfs_group, 1910 #endif 1911 #ifdef CONFIG_PCI_TSM 1912 &pci_tsm_auth_attr_group, 1913 &pci_tsm_attr_group, 1914 #endif 1915 NULL, 1916 }; 1917