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