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 switch (count) { 1216 case 1: 1217 if (write) 1218 outb(*(u8 *)buf, port); 1219 else 1220 *(u8 *)buf = inb(port); 1221 return 1; 1222 case 2: 1223 if (write) 1224 outw(*(u16 *)buf, port); 1225 else 1226 *(u16 *)buf = inw(port); 1227 return 2; 1228 case 4: 1229 if (write) 1230 outl(*(u32 *)buf, port); 1231 else 1232 *(u32 *)buf = inl(port); 1233 return 4; 1234 } 1235 return -EINVAL; 1236 #else 1237 return -ENXIO; 1238 #endif 1239 } 1240 1241 static ssize_t pci_read_resource(struct file *filp, struct kobject *kobj, 1242 const struct bin_attribute *attr, char *buf, 1243 loff_t off, size_t count) 1244 { 1245 return pci_resource_io(filp, kobj, attr, buf, off, count, false); 1246 } 1247 1248 static ssize_t pci_write_resource(struct file *filp, struct kobject *kobj, 1249 const struct bin_attribute *attr, char *buf, 1250 loff_t off, size_t count) 1251 { 1252 int ret; 1253 1254 ret = security_locked_down(LOCKDOWN_PCI_ACCESS); 1255 if (ret) 1256 return ret; 1257 1258 return pci_resource_io(filp, kobj, attr, buf, off, count, true); 1259 } 1260 1261 static loff_t pci_llseek_resource(struct file *filep, 1262 struct kobject *kobj, 1263 const struct bin_attribute *attr, 1264 loff_t offset, int whence) 1265 { 1266 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); 1267 int bar = (unsigned long)attr->private; 1268 1269 return fixed_size_llseek(filep, offset, whence, 1270 pci_resource_len(pdev, bar)); 1271 } 1272 1273 /* 1274 * generic_file_llseek() consults f_mapping->host to determine 1275 * the file size. As iomem_inode knows nothing about the 1276 * attribute, it's not going to work, so override it as well. 1277 */ 1278 #if arch_can_pci_mmap_io() 1279 # define __PCI_RESOURCE_IO_MMAP_ATTRS \ 1280 .f_mapping = iomem_get_mapping, \ 1281 .llseek = pci_llseek_resource, \ 1282 .mmap = pci_mmap_resource_uc, 1283 #else 1284 static int pci_mmap_resource_io_unsupported(struct file *filp, 1285 struct kobject *kobj, 1286 const struct bin_attribute *attr, 1287 struct vm_area_struct *vma) 1288 { 1289 return -EINVAL; 1290 } 1291 1292 # define __PCI_RESOURCE_IO_MMAP_ATTRS \ 1293 .mmap = pci_mmap_resource_io_unsupported, 1294 #endif 1295 1296 #define pci_dev_resource_io_attr(_bar) \ 1297 static const struct bin_attribute pci_dev_resource##_bar##_io_attr = { \ 1298 .attr = { .name = "resource" __stringify(_bar), .mode = 0600 }, \ 1299 .private = (void *)(unsigned long)(_bar), \ 1300 .read = pci_read_resource, \ 1301 .write = pci_write_resource, \ 1302 __PCI_RESOURCE_IO_MMAP_ATTRS \ 1303 } 1304 1305 #define pci_dev_resource_uc_attr(_bar) \ 1306 static const struct bin_attribute pci_dev_resource##_bar##_uc_attr = { \ 1307 .attr = { .name = "resource" __stringify(_bar), .mode = 0600 }, \ 1308 .private = (void *)(unsigned long)(_bar), \ 1309 .f_mapping = iomem_get_mapping, \ 1310 .llseek = pci_llseek_resource, \ 1311 .mmap = pci_mmap_resource_uc, \ 1312 } 1313 1314 #define pci_dev_resource_wc_attr(_bar) \ 1315 static const struct bin_attribute pci_dev_resource##_bar##_wc_attr = { \ 1316 .attr = { .name = "resource" __stringify(_bar) "_wc", .mode = 0600 }, \ 1317 .private = (void *)(unsigned long)(_bar), \ 1318 .f_mapping = iomem_get_mapping, \ 1319 .llseek = pci_llseek_resource, \ 1320 .mmap = pci_mmap_resource_wc, \ 1321 } 1322 1323 static inline umode_t 1324 __pci_resource_attr_is_visible(struct kobject *kobj, 1325 const struct bin_attribute *a, 1326 int bar, bool write_combine, 1327 unsigned long flags) 1328 { 1329 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); 1330 1331 if (pdev->non_mappable_bars) 1332 return 0; 1333 1334 if (!pci_resource_len(pdev, bar)) 1335 return 0; 1336 1337 if ((pci_resource_flags(pdev, bar) & flags) != flags) 1338 return 0; 1339 1340 if (write_combine && !arch_can_pci_mmap_wc()) 1341 return 0; 1342 1343 return a->attr.mode; 1344 } 1345 1346 static umode_t pci_dev_resource_io_is_visible(struct kobject *kobj, 1347 const struct bin_attribute *a, 1348 int n) 1349 { 1350 return __pci_resource_attr_is_visible(kobj, a, n, false, 1351 IORESOURCE_IO); 1352 } 1353 1354 static umode_t pci_dev_resource_uc_is_visible(struct kobject *kobj, 1355 const struct bin_attribute *a, 1356 int n) 1357 { 1358 return __pci_resource_attr_is_visible(kobj, a, n, false, 1359 IORESOURCE_MEM); 1360 } 1361 1362 static umode_t pci_dev_resource_wc_is_visible(struct kobject *kobj, 1363 const struct bin_attribute *a, 1364 int n) 1365 { 1366 return __pci_resource_attr_is_visible(kobj, a, n, true, 1367 IORESOURCE_MEM | IORESOURCE_PREFETCH); 1368 } 1369 1370 static size_t pci_dev_resource_bin_size(struct kobject *kobj, 1371 const struct bin_attribute *a, 1372 int n) 1373 { 1374 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); 1375 1376 return pci_resource_len(pdev, n); 1377 } 1378 1379 pci_dev_resource_io_attr(0); 1380 pci_dev_resource_io_attr(1); 1381 pci_dev_resource_io_attr(2); 1382 pci_dev_resource_io_attr(3); 1383 pci_dev_resource_io_attr(4); 1384 pci_dev_resource_io_attr(5); 1385 1386 pci_dev_resource_uc_attr(0); 1387 pci_dev_resource_uc_attr(1); 1388 pci_dev_resource_uc_attr(2); 1389 pci_dev_resource_uc_attr(3); 1390 pci_dev_resource_uc_attr(4); 1391 pci_dev_resource_uc_attr(5); 1392 1393 pci_dev_resource_wc_attr(0); 1394 pci_dev_resource_wc_attr(1); 1395 pci_dev_resource_wc_attr(2); 1396 pci_dev_resource_wc_attr(3); 1397 pci_dev_resource_wc_attr(4); 1398 pci_dev_resource_wc_attr(5); 1399 1400 static const struct bin_attribute *const pci_dev_resource_io_attrs[] = { 1401 &pci_dev_resource0_io_attr, 1402 &pci_dev_resource1_io_attr, 1403 &pci_dev_resource2_io_attr, 1404 &pci_dev_resource3_io_attr, 1405 &pci_dev_resource4_io_attr, 1406 &pci_dev_resource5_io_attr, 1407 NULL, 1408 }; 1409 1410 static const struct bin_attribute *const pci_dev_resource_uc_attrs[] = { 1411 &pci_dev_resource0_uc_attr, 1412 &pci_dev_resource1_uc_attr, 1413 &pci_dev_resource2_uc_attr, 1414 &pci_dev_resource3_uc_attr, 1415 &pci_dev_resource4_uc_attr, 1416 &pci_dev_resource5_uc_attr, 1417 NULL, 1418 }; 1419 1420 static const struct bin_attribute *const pci_dev_resource_wc_attrs[] = { 1421 &pci_dev_resource0_wc_attr, 1422 &pci_dev_resource1_wc_attr, 1423 &pci_dev_resource2_wc_attr, 1424 &pci_dev_resource3_wc_attr, 1425 &pci_dev_resource4_wc_attr, 1426 &pci_dev_resource5_wc_attr, 1427 NULL, 1428 }; 1429 1430 static const struct attribute_group pci_dev_resource_io_attr_group = { 1431 .bin_attrs = pci_dev_resource_io_attrs, 1432 .is_bin_visible = pci_dev_resource_io_is_visible, 1433 .bin_size = pci_dev_resource_bin_size, 1434 }; 1435 1436 static const struct attribute_group pci_dev_resource_uc_attr_group = { 1437 .bin_attrs = pci_dev_resource_uc_attrs, 1438 .is_bin_visible = pci_dev_resource_uc_is_visible, 1439 .bin_size = pci_dev_resource_bin_size, 1440 }; 1441 1442 static const struct attribute_group pci_dev_resource_wc_attr_group = { 1443 .bin_attrs = pci_dev_resource_wc_attrs, 1444 .is_bin_visible = pci_dev_resource_wc_is_visible, 1445 .bin_size = pci_dev_resource_bin_size, 1446 }; 1447 1448 static const struct attribute_group *pci_dev_resource_attr_groups[] = { 1449 &pci_dev_resource_io_attr_group, 1450 &pci_dev_resource_uc_attr_group, 1451 &pci_dev_resource_wc_attr_group, 1452 NULL, 1453 }; 1454 #else 1455 #define pci_dev_resource_attr_groups NULL 1456 #endif 1457 1458 /** 1459 * pci_write_rom - used to enable access to the PCI ROM display 1460 * @filp: sysfs file 1461 * @kobj: kernel object handle 1462 * @bin_attr: struct bin_attribute for this file 1463 * @buf: user input 1464 * @off: file offset 1465 * @count: number of byte in input 1466 * 1467 * Writing a boolean value enables or disables the ROM display. 1468 */ 1469 static ssize_t pci_write_rom(struct file *filp, struct kobject *kobj, 1470 const struct bin_attribute *bin_attr, char *buf, 1471 loff_t off, size_t count) 1472 { 1473 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); 1474 bool enable; 1475 1476 if (kstrtobool(buf, &enable)) 1477 return -EINVAL; 1478 1479 pdev->rom_attr_enabled = enable; 1480 1481 return count; 1482 } 1483 1484 /** 1485 * pci_read_rom - read a PCI ROM 1486 * @filp: sysfs file 1487 * @kobj: kernel object handle 1488 * @bin_attr: struct bin_attribute for this file 1489 * @buf: where to put the data we read from the ROM 1490 * @off: file offset 1491 * @count: number of bytes to read 1492 * 1493 * Put @count bytes starting at @off into @buf from the ROM in the PCI 1494 * device corresponding to @kobj. 1495 */ 1496 static ssize_t pci_read_rom(struct file *filp, struct kobject *kobj, 1497 const struct bin_attribute *bin_attr, char *buf, 1498 loff_t off, size_t count) 1499 { 1500 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); 1501 void __iomem *rom; 1502 size_t size; 1503 1504 if (!pdev->rom_attr_enabled) 1505 return -EINVAL; 1506 1507 rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */ 1508 if (!rom || !size) 1509 return -EIO; 1510 1511 if (off >= size) 1512 count = 0; 1513 else { 1514 if (off + count > size) 1515 count = size - off; 1516 1517 memcpy_fromio(buf, rom + off, count); 1518 } 1519 pci_unmap_rom(pdev, rom); 1520 1521 return count; 1522 } 1523 static const BIN_ATTR(rom, 0600, pci_read_rom, pci_write_rom, 0); 1524 1525 static const struct bin_attribute *const pci_dev_rom_attrs[] = { 1526 &bin_attr_rom, 1527 NULL, 1528 }; 1529 1530 static umode_t pci_dev_rom_attr_is_visible(struct kobject *kobj, 1531 const struct bin_attribute *a, int n) 1532 { 1533 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); 1534 1535 /* If the device has a ROM, try to expose it in sysfs. */ 1536 if (!pci_resource_end(pdev, PCI_ROM_RESOURCE)) 1537 return 0; 1538 1539 return a->attr.mode; 1540 } 1541 1542 static size_t pci_dev_rom_attr_bin_size(struct kobject *kobj, 1543 const struct bin_attribute *a, int n) 1544 { 1545 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); 1546 1547 return pci_resource_len(pdev, PCI_ROM_RESOURCE); 1548 } 1549 1550 static const struct attribute_group pci_dev_rom_attr_group = { 1551 .bin_attrs = pci_dev_rom_attrs, 1552 .is_bin_visible = pci_dev_rom_attr_is_visible, 1553 .bin_size = pci_dev_rom_attr_bin_size, 1554 }; 1555 1556 static ssize_t reset_store(struct device *dev, struct device_attribute *attr, 1557 const char *buf, size_t count) 1558 { 1559 struct pci_dev *pdev = to_pci_dev(dev); 1560 unsigned long val; 1561 ssize_t result; 1562 1563 if (kstrtoul(buf, 0, &val) < 0) 1564 return -EINVAL; 1565 1566 if (val != 1) 1567 return -EINVAL; 1568 1569 pm_runtime_get_sync(dev); 1570 result = pci_reset_function(pdev); 1571 pm_runtime_put(dev); 1572 if (result < 0) 1573 return result; 1574 1575 return count; 1576 } 1577 static DEVICE_ATTR_WO(reset); 1578 1579 static struct attribute *pci_dev_reset_attrs[] = { 1580 &dev_attr_reset.attr, 1581 NULL, 1582 }; 1583 1584 static umode_t pci_dev_reset_attr_is_visible(struct kobject *kobj, 1585 struct attribute *a, int n) 1586 { 1587 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); 1588 1589 if (!pci_reset_supported(pdev)) 1590 return 0; 1591 1592 return a->mode; 1593 } 1594 1595 static const struct attribute_group pci_dev_reset_attr_group = { 1596 .attrs = pci_dev_reset_attrs, 1597 .is_visible = pci_dev_reset_attr_is_visible, 1598 }; 1599 1600 static ssize_t reset_method_show(struct device *dev, 1601 struct device_attribute *attr, char *buf) 1602 { 1603 struct pci_dev *pdev = to_pci_dev(dev); 1604 ssize_t len = 0; 1605 int i, m; 1606 1607 for (i = 0; i < PCI_NUM_RESET_METHODS; i++) { 1608 m = pdev->reset_methods[i]; 1609 if (!m) 1610 break; 1611 1612 len += sysfs_emit_at(buf, len, "%s%s", len ? " " : "", 1613 pci_reset_fn_methods[m].name); 1614 } 1615 1616 if (len) 1617 len += sysfs_emit_at(buf, len, "\n"); 1618 1619 return len; 1620 } 1621 1622 static int reset_method_lookup(const char *name) 1623 { 1624 int m; 1625 1626 for (m = 1; m < PCI_NUM_RESET_METHODS; m++) { 1627 if (sysfs_streq(name, pci_reset_fn_methods[m].name)) 1628 return m; 1629 } 1630 1631 return 0; /* not found */ 1632 } 1633 1634 static ssize_t reset_method_store(struct device *dev, 1635 struct device_attribute *attr, 1636 const char *buf, size_t count) 1637 { 1638 struct pci_dev *pdev = to_pci_dev(dev); 1639 char *tmp_options, *name; 1640 int m, n; 1641 u8 reset_methods[PCI_NUM_RESET_METHODS] = {}; 1642 1643 if (sysfs_streq(buf, "")) { 1644 pdev->reset_methods[0] = 0; 1645 pci_warn(pdev, "All device reset methods disabled by user"); 1646 return count; 1647 } 1648 1649 PM_RUNTIME_ACQUIRE(dev, pm); 1650 if (PM_RUNTIME_ACQUIRE_ERR(&pm)) 1651 return -ENXIO; 1652 1653 if (sysfs_streq(buf, "default")) { 1654 pci_init_reset_methods(pdev); 1655 return count; 1656 } 1657 1658 char *options __free(kfree) = kstrndup(buf, count, GFP_KERNEL); 1659 if (!options) 1660 return -ENOMEM; 1661 1662 n = 0; 1663 tmp_options = options; 1664 while ((name = strsep(&tmp_options, " ")) != NULL) { 1665 if (sysfs_streq(name, "")) 1666 continue; 1667 1668 name = strim(name); 1669 1670 /* Leave previous methods unchanged if input is invalid */ 1671 m = reset_method_lookup(name); 1672 if (!m) { 1673 pci_err(pdev, "Invalid reset method '%s'", name); 1674 return -EINVAL; 1675 } 1676 1677 if (pci_reset_fn_methods[m].reset_fn(pdev, PCI_RESET_PROBE)) { 1678 pci_err(pdev, "Unsupported reset method '%s'", name); 1679 return -EINVAL; 1680 } 1681 1682 if (n == PCI_NUM_RESET_METHODS - 1) { 1683 pci_err(pdev, "Too many reset methods\n"); 1684 return -EINVAL; 1685 } 1686 1687 reset_methods[n++] = m; 1688 } 1689 1690 reset_methods[n] = 0; 1691 1692 /* Warn if dev-specific supported but not highest priority */ 1693 if (pci_reset_fn_methods[1].reset_fn(pdev, PCI_RESET_PROBE) == 0 && 1694 reset_methods[0] != 1) 1695 pci_warn(pdev, "Device-specific reset disabled/de-prioritized by user"); 1696 memcpy(pdev->reset_methods, reset_methods, sizeof(pdev->reset_methods)); 1697 return count; 1698 } 1699 static DEVICE_ATTR_RW(reset_method); 1700 1701 static struct attribute *pci_dev_reset_method_attrs[] = { 1702 &dev_attr_reset_method.attr, 1703 NULL, 1704 }; 1705 1706 static const struct attribute_group pci_dev_reset_method_attr_group = { 1707 .attrs = pci_dev_reset_method_attrs, 1708 .is_visible = pci_dev_reset_attr_is_visible, 1709 }; 1710 1711 #if defined(HAVE_PCI_MMAP) || defined(ARCH_GENERIC_PCI_MMAP_RESOURCE) 1712 static ssize_t __resource_resize_show(struct device *dev, int n, char *buf) 1713 { 1714 struct pci_dev *pdev = to_pci_dev(dev); 1715 ssize_t ret; 1716 1717 pci_config_pm_runtime_get(pdev); 1718 1719 ret = sysfs_emit(buf, "%016llx\n", 1720 pci_rebar_get_possible_sizes(pdev, n)); 1721 1722 pci_config_pm_runtime_put(pdev); 1723 1724 return ret; 1725 } 1726 1727 static ssize_t __resource_resize_store(struct device *dev, int n, 1728 const char *buf, size_t count) 1729 { 1730 struct pci_dev *pdev = to_pci_dev(dev); 1731 struct pci_bus *bus = pdev->bus; 1732 unsigned long size; 1733 int ret; 1734 u16 cmd; 1735 1736 if (!capable(CAP_SYS_ADMIN)) 1737 return -EPERM; 1738 1739 if (kstrtoul(buf, 0, &size) < 0) 1740 return -EINVAL; 1741 1742 device_lock(dev); 1743 if (dev->driver || pci_num_vf(pdev)) { 1744 ret = -EBUSY; 1745 goto unlock; 1746 } 1747 1748 pci_config_pm_runtime_get(pdev); 1749 1750 if ((pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA) { 1751 ret = aperture_remove_conflicting_pci_devices(pdev, 1752 "resourceN_resize"); 1753 if (ret) 1754 goto pm_put; 1755 } 1756 1757 pci_read_config_word(pdev, PCI_COMMAND, &cmd); 1758 pci_write_config_word(pdev, PCI_COMMAND, 1759 cmd & ~PCI_COMMAND_MEMORY); 1760 1761 sysfs_remove_groups(&pdev->dev.kobj, pci_dev_resource_attr_groups); 1762 1763 ret = pci_resize_resource(pdev, n, size, 0); 1764 if (ret) 1765 pci_warn(pdev, "Failed to resize BAR %d: %pe\n", 1766 n, ERR_PTR(ret)); 1767 1768 pci_assign_unassigned_bus_resources(bus); 1769 1770 if (sysfs_create_groups(&pdev->dev.kobj, pci_dev_resource_attr_groups)) 1771 pci_warn(pdev, "Failed to recreate resource groups after BAR resizing\n"); 1772 1773 pci_write_config_word(pdev, PCI_COMMAND, cmd); 1774 pm_put: 1775 pci_config_pm_runtime_put(pdev); 1776 unlock: 1777 device_unlock(dev); 1778 1779 return ret ? ret : count; 1780 } 1781 1782 #define pci_dev_resource_resize_attr(n) \ 1783 static ssize_t resource##n##_resize_show(struct device *dev, \ 1784 struct device_attribute *attr, \ 1785 char *buf) \ 1786 { \ 1787 return __resource_resize_show(dev, n, buf); \ 1788 } \ 1789 static ssize_t resource##n##_resize_store(struct device *dev, \ 1790 struct device_attribute *attr,\ 1791 const char *buf, size_t count)\ 1792 { \ 1793 return __resource_resize_store(dev, n, buf, count); \ 1794 } \ 1795 static DEVICE_ATTR_RW(resource##n##_resize) 1796 1797 pci_dev_resource_resize_attr(0); 1798 pci_dev_resource_resize_attr(1); 1799 pci_dev_resource_resize_attr(2); 1800 pci_dev_resource_resize_attr(3); 1801 pci_dev_resource_resize_attr(4); 1802 pci_dev_resource_resize_attr(5); 1803 1804 static struct attribute *resource_resize_attrs[] = { 1805 &dev_attr_resource0_resize.attr, 1806 &dev_attr_resource1_resize.attr, 1807 &dev_attr_resource2_resize.attr, 1808 &dev_attr_resource3_resize.attr, 1809 &dev_attr_resource4_resize.attr, 1810 &dev_attr_resource5_resize.attr, 1811 NULL, 1812 }; 1813 1814 static umode_t resource_resize_attr_is_visible(struct kobject *kobj, 1815 struct attribute *a, int n) 1816 { 1817 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); 1818 1819 return pci_rebar_get_current_size(pdev, n) < 0 ? 0 : a->mode; 1820 } 1821 1822 static const struct attribute_group pci_dev_resource_resize_attr_group = { 1823 .attrs = resource_resize_attrs, 1824 .is_visible = resource_resize_attr_is_visible, 1825 }; 1826 #endif 1827 1828 static struct attribute *pci_dev_dev_attrs[] = { 1829 &dev_attr_boot_vga.attr, 1830 &dev_attr_serial_number.attr, 1831 NULL, 1832 }; 1833 1834 static umode_t pci_dev_attrs_are_visible(struct kobject *kobj, 1835 struct attribute *a, int n) 1836 { 1837 struct device *dev = kobj_to_dev(kobj); 1838 struct pci_dev *pdev = to_pci_dev(dev); 1839 1840 if (a == &dev_attr_boot_vga.attr && pci_is_vga(pdev)) 1841 return a->mode; 1842 1843 if (a == &dev_attr_serial_number.attr && pci_get_dsn(pdev)) 1844 return a->mode; 1845 1846 return 0; 1847 } 1848 1849 static struct attribute *pci_dev_hp_attrs[] = { 1850 &dev_attr_remove.attr, 1851 &dev_attr_dev_rescan.attr, 1852 NULL, 1853 }; 1854 1855 static umode_t pci_dev_hp_attrs_are_visible(struct kobject *kobj, 1856 struct attribute *a, int n) 1857 { 1858 struct device *dev = kobj_to_dev(kobj); 1859 struct pci_dev *pdev = to_pci_dev(dev); 1860 1861 if (pdev->is_virtfn) 1862 return 0; 1863 1864 return a->mode; 1865 } 1866 1867 static umode_t pci_bridge_attrs_are_visible(struct kobject *kobj, 1868 struct attribute *a, int n) 1869 { 1870 struct device *dev = kobj_to_dev(kobj); 1871 struct pci_dev *pdev = to_pci_dev(dev); 1872 1873 if (pci_is_bridge(pdev)) 1874 return a->mode; 1875 1876 return 0; 1877 } 1878 1879 static umode_t pcie_dev_attrs_are_visible(struct kobject *kobj, 1880 struct attribute *a, int n) 1881 { 1882 struct device *dev = kobj_to_dev(kobj); 1883 struct pci_dev *pdev = to_pci_dev(dev); 1884 1885 if (pci_is_pcie(pdev)) 1886 return a->mode; 1887 1888 return 0; 1889 } 1890 1891 static const struct attribute_group pci_dev_group = { 1892 .attrs = pci_dev_attrs, 1893 }; 1894 1895 const struct attribute_group *pci_dev_groups[] = { 1896 &pci_dev_group, 1897 #if defined(HAVE_PCI_MMAP) || defined(ARCH_GENERIC_PCI_MMAP_RESOURCE) 1898 &pci_dev_resource_io_attr_group, 1899 &pci_dev_resource_uc_attr_group, 1900 &pci_dev_resource_wc_attr_group, 1901 &pci_dev_resource_resize_attr_group, 1902 #endif 1903 &pci_dev_config_attr_group, 1904 &pci_dev_rom_attr_group, 1905 &pci_dev_reset_attr_group, 1906 &pci_dev_reset_method_attr_group, 1907 &pci_dev_vpd_attr_group, 1908 #ifdef CONFIG_DMI 1909 &pci_dev_smbios_attr_group, 1910 #endif 1911 #ifdef CONFIG_ACPI 1912 &pci_dev_acpi_attr_group, 1913 #endif 1914 ARCH_PCI_DEV_GROUPS 1915 NULL, 1916 }; 1917 1918 static const struct attribute_group pci_dev_hp_attr_group = { 1919 .attrs = pci_dev_hp_attrs, 1920 .is_visible = pci_dev_hp_attrs_are_visible, 1921 }; 1922 1923 static const struct attribute_group pci_dev_attr_group = { 1924 .attrs = pci_dev_dev_attrs, 1925 .is_visible = pci_dev_attrs_are_visible, 1926 }; 1927 1928 static const struct attribute_group pci_bridge_attr_group = { 1929 .attrs = pci_bridge_attrs, 1930 .is_visible = pci_bridge_attrs_are_visible, 1931 }; 1932 1933 static const struct attribute_group pcie_dev_attr_group = { 1934 .attrs = pcie_dev_attrs, 1935 .is_visible = pcie_dev_attrs_are_visible, 1936 }; 1937 1938 const struct attribute_group *pci_dev_attr_groups[] = { 1939 &pci_dev_attr_group, 1940 &pci_dev_hp_attr_group, 1941 #ifdef CONFIG_PCI_IOV 1942 &sriov_pf_dev_attr_group, 1943 &sriov_vf_dev_attr_group, 1944 #endif 1945 &pci_bridge_attr_group, 1946 &pcie_dev_attr_group, 1947 #ifdef CONFIG_PCIEAER 1948 &aer_stats_attr_group, 1949 &aer_attr_group, 1950 #endif 1951 #ifdef CONFIG_PCIEASPM 1952 &aspm_ctrl_attr_group, 1953 #endif 1954 #ifdef CONFIG_PCI_DOE 1955 &pci_doe_sysfs_group, 1956 #endif 1957 #ifdef CONFIG_PCI_TSM 1958 &pci_tsm_auth_attr_group, 1959 &pci_tsm_attr_group, 1960 #endif 1961 NULL, 1962 }; 1963