1 /* 2 * ACPI PCI HotPlug glue functions to ACPI CA subsystem 3 * 4 * Copyright (C) 2002,2003 Takayoshi Kochi (t-kochi@bq.jp.nec.com) 5 * Copyright (C) 2002 Hiroshi Aono (h-aono@ap.jp.nec.com) 6 * Copyright (C) 2002,2003 NEC Corporation 7 * Copyright (C) 2003-2005 Matthew Wilcox (matthew.wilcox@hp.com) 8 * Copyright (C) 2003-2005 Hewlett Packard 9 * Copyright (C) 2005 Rajesh Shah (rajesh.shah@intel.com) 10 * Copyright (C) 2005 Intel Corporation 11 * 12 * All rights reserved. 13 * 14 * This program is free software; you can redistribute it and/or modify 15 * it under the terms of the GNU General Public License as published by 16 * the Free Software Foundation; either version 2 of the License, or (at 17 * your option) any later version. 18 * 19 * This program is distributed in the hope that it will be useful, but 20 * WITHOUT ANY WARRANTY; without even the implied warranty of 21 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or 22 * NON INFRINGEMENT. See the GNU General Public License for more 23 * details. 24 * 25 * You should have received a copy of the GNU General Public License 26 * along with this program; if not, write to the Free Software 27 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 28 * 29 * Send feedback to <kristen.c.accardi@intel.com> 30 * 31 */ 32 33 /* 34 * Lifetime rules for pci_dev: 35 * - The one in acpiphp_bridge has its refcount elevated by pci_get_slot() 36 * when the bridge is scanned and it loses a refcount when the bridge 37 * is removed. 38 * - When a P2P bridge is present, we elevate the refcount on the subordinate 39 * bus. It loses the refcount when the the driver unloads. 40 */ 41 42 #include <linux/init.h> 43 #include <linux/module.h> 44 45 #include <linux/kernel.h> 46 #include <linux/pci.h> 47 #include <linux/pci_hotplug.h> 48 #include <linux/pci-acpi.h> 49 #include <linux/mutex.h> 50 #include <linux/slab.h> 51 #include <linux/acpi.h> 52 53 #include "../pci.h" 54 #include "acpiphp.h" 55 56 static LIST_HEAD(bridge_list); 57 static DEFINE_MUTEX(bridge_mutex); 58 static DEFINE_MUTEX(acpiphp_context_lock); 59 60 #define MY_NAME "acpiphp_glue" 61 62 static void handle_hotplug_event(acpi_handle handle, u32 type, void *data); 63 static void acpiphp_sanitize_bus(struct pci_bus *bus); 64 static void acpiphp_set_hpp_values(struct pci_bus *bus); 65 static void hotplug_event_func(acpi_handle handle, u32 type, void *context); 66 static void free_bridge(struct kref *kref); 67 68 /* callback routine to check for the existence of a pci dock device */ 69 static acpi_status 70 is_pci_dock_device(acpi_handle handle, u32 lvl, void *context, void **rv) 71 { 72 int *count = (int *)context; 73 74 if (is_dock_device(handle)) { 75 (*count)++; 76 return AE_CTRL_TERMINATE; 77 } else { 78 return AE_OK; 79 } 80 } 81 82 static void acpiphp_context_handler(acpi_handle handle, void *context) 83 { 84 /* Intentionally empty. */ 85 } 86 87 /** 88 * acpiphp_init_context - Create hotplug context and grab a reference to it. 89 * @handle: ACPI object handle to create the context for. 90 * 91 * Call under acpiphp_context_lock. 92 */ 93 static struct acpiphp_context *acpiphp_init_context(acpi_handle handle) 94 { 95 struct acpiphp_context *context; 96 acpi_status status; 97 98 context = kzalloc(sizeof(*context), GFP_KERNEL); 99 if (!context) 100 return NULL; 101 102 context->handle = handle; 103 context->refcount = 1; 104 status = acpi_attach_data(handle, acpiphp_context_handler, context); 105 if (ACPI_FAILURE(status)) { 106 kfree(context); 107 return NULL; 108 } 109 return context; 110 } 111 112 /** 113 * acpiphp_get_context - Get hotplug context and grab a reference to it. 114 * @handle: ACPI object handle to get the context for. 115 * 116 * Call under acpiphp_context_lock. 117 */ 118 static struct acpiphp_context *acpiphp_get_context(acpi_handle handle) 119 { 120 struct acpiphp_context *context = NULL; 121 acpi_status status; 122 void *data; 123 124 status = acpi_get_data(handle, acpiphp_context_handler, &data); 125 if (ACPI_SUCCESS(status)) { 126 context = data; 127 context->refcount++; 128 } 129 return context; 130 } 131 132 /** 133 * acpiphp_put_context - Drop a reference to ACPI hotplug context. 134 * @handle: ACPI object handle to put the context for. 135 * 136 * The context object is removed if there are no more references to it. 137 * 138 * Call under acpiphp_context_lock. 139 */ 140 static void acpiphp_put_context(struct acpiphp_context *context) 141 { 142 if (--context->refcount) 143 return; 144 145 WARN_ON(context->func || context->bridge); 146 acpi_detach_data(context->handle, acpiphp_context_handler); 147 kfree(context); 148 } 149 150 static inline void get_bridge(struct acpiphp_bridge *bridge) 151 { 152 kref_get(&bridge->ref); 153 } 154 155 static inline void put_bridge(struct acpiphp_bridge *bridge) 156 { 157 kref_put(&bridge->ref, free_bridge); 158 } 159 160 static void free_bridge(struct kref *kref) 161 { 162 struct acpiphp_context *context; 163 struct acpiphp_bridge *bridge; 164 struct acpiphp_slot *slot, *next; 165 struct acpiphp_func *func, *tmp; 166 167 mutex_lock(&acpiphp_context_lock); 168 169 bridge = container_of(kref, struct acpiphp_bridge, ref); 170 171 list_for_each_entry_safe(slot, next, &bridge->slots, node) { 172 list_for_each_entry_safe(func, tmp, &slot->funcs, sibling) { 173 context = func->context; 174 context->func = NULL; 175 acpiphp_put_context(context); 176 kfree(func); 177 } 178 kfree(slot); 179 } 180 181 context = bridge->context; 182 /* Release the reference acquired by acpiphp_enumerate_slots(). */ 183 if (context->handler_for_func) 184 put_bridge(bridge->func->slot->bridge); 185 186 put_device(&bridge->pci_bus->dev); 187 pci_dev_put(bridge->pci_dev); 188 context->bridge = NULL; 189 acpiphp_put_context(context); 190 kfree(bridge); 191 192 mutex_unlock(&acpiphp_context_lock); 193 } 194 195 /* 196 * the _DCK method can do funny things... and sometimes not 197 * hah-hah funny. 198 * 199 * TBD - figure out a way to only call fixups for 200 * systems that require them. 201 */ 202 static void post_dock_fixups(acpi_handle not_used, u32 event, void *data) 203 { 204 struct acpiphp_func *func = data; 205 struct pci_bus *bus = func->slot->bridge->pci_bus; 206 u32 buses; 207 208 if (!bus->self) 209 return; 210 211 /* fixup bad _DCK function that rewrites 212 * secondary bridge on slot 213 */ 214 pci_read_config_dword(bus->self, 215 PCI_PRIMARY_BUS, 216 &buses); 217 218 if (((buses >> 8) & 0xff) != bus->busn_res.start) { 219 buses = (buses & 0xff000000) 220 | ((unsigned int)(bus->primary) << 0) 221 | ((unsigned int)(bus->busn_res.start) << 8) 222 | ((unsigned int)(bus->busn_res.end) << 16); 223 pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses); 224 } 225 } 226 227 228 static const struct acpi_dock_ops acpiphp_dock_ops = { 229 .fixup = post_dock_fixups, 230 .handler = hotplug_event_func, 231 }; 232 233 /* Check whether the PCI device is managed by native PCIe hotplug driver */ 234 static bool device_is_managed_by_native_pciehp(struct pci_dev *pdev) 235 { 236 u32 reg32; 237 acpi_handle tmp; 238 struct acpi_pci_root *root; 239 240 /* Check whether the PCIe port supports native PCIe hotplug */ 241 if (pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, ®32)) 242 return false; 243 if (!(reg32 & PCI_EXP_SLTCAP_HPC)) 244 return false; 245 246 /* 247 * Check whether native PCIe hotplug has been enabled for 248 * this PCIe hierarchy. 249 */ 250 tmp = acpi_find_root_bridge_handle(pdev); 251 if (!tmp) 252 return false; 253 root = acpi_pci_find_root(tmp); 254 if (!root) 255 return false; 256 if (!(root->osc_control_set & OSC_PCI_EXPRESS_NATIVE_HP_CONTROL)) 257 return false; 258 259 return true; 260 } 261 262 static void acpiphp_dock_init(void *data) 263 { 264 struct acpiphp_func *func = data; 265 266 get_bridge(func->slot->bridge); 267 } 268 269 static void acpiphp_dock_release(void *data) 270 { 271 struct acpiphp_func *func = data; 272 273 put_bridge(func->slot->bridge); 274 } 275 276 /* callback routine to register each ACPI PCI slot object */ 277 static acpi_status register_slot(acpi_handle handle, u32 lvl, void *data, 278 void **rv) 279 { 280 struct acpiphp_bridge *bridge = data; 281 struct acpiphp_context *context; 282 struct acpiphp_slot *slot; 283 struct acpiphp_func *newfunc; 284 acpi_status status = AE_OK; 285 unsigned long long adr, sun; 286 int device, function, retval, found = 0; 287 struct pci_bus *pbus = bridge->pci_bus; 288 struct pci_dev *pdev; 289 u32 val; 290 291 if (!acpi_pci_check_ejectable(pbus, handle) && !is_dock_device(handle)) 292 return AE_OK; 293 294 status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr); 295 if (ACPI_FAILURE(status)) { 296 warn("can't evaluate _ADR (%#x)\n", status); 297 return AE_OK; 298 } 299 300 device = (adr >> 16) & 0xffff; 301 function = adr & 0xffff; 302 303 pdev = bridge->pci_dev; 304 if (pdev && device_is_managed_by_native_pciehp(pdev)) 305 return AE_OK; 306 307 newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL); 308 if (!newfunc) 309 return AE_NO_MEMORY; 310 311 newfunc->handle = handle; 312 newfunc->function = function; 313 314 mutex_lock(&acpiphp_context_lock); 315 context = acpiphp_init_context(handle); 316 if (!context) { 317 mutex_unlock(&acpiphp_context_lock); 318 acpi_handle_err(handle, "No hotplug context\n"); 319 kfree(newfunc); 320 return AE_NOT_EXIST; 321 } 322 newfunc->context = context; 323 context->func = newfunc; 324 mutex_unlock(&acpiphp_context_lock); 325 326 if (acpi_has_method(handle, "_EJ0")) 327 newfunc->flags = FUNC_HAS_EJ0; 328 329 if (acpi_has_method(handle, "_STA")) 330 newfunc->flags |= FUNC_HAS_STA; 331 332 if (acpi_has_method(handle, "_PS0")) 333 newfunc->flags |= FUNC_HAS_PS0; 334 335 if (acpi_has_method(handle, "_PS3")) 336 newfunc->flags |= FUNC_HAS_PS3; 337 338 if (acpi_has_method(handle, "_DCK")) 339 newfunc->flags |= FUNC_HAS_DCK; 340 341 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun); 342 if (ACPI_FAILURE(status)) { 343 /* 344 * use the count of the number of slots we've found 345 * for the number of the slot 346 */ 347 sun = bridge->nr_slots+1; 348 } 349 350 /* search for objects that share the same slot */ 351 list_for_each_entry(slot, &bridge->slots, node) 352 if (slot->device == device) { 353 if (slot->sun != sun) 354 warn("sibling found, but _SUN doesn't match!\n"); 355 found = 1; 356 break; 357 } 358 359 if (!found) { 360 slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL); 361 if (!slot) { 362 status = AE_NO_MEMORY; 363 goto err_out; 364 } 365 366 slot->bridge = bridge; 367 slot->device = device; 368 slot->sun = sun; 369 INIT_LIST_HEAD(&slot->funcs); 370 mutex_init(&slot->crit_sect); 371 372 mutex_lock(&bridge_mutex); 373 list_add_tail(&slot->node, &bridge->slots); 374 mutex_unlock(&bridge_mutex); 375 bridge->nr_slots++; 376 377 dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n", 378 slot->sun, pci_domain_nr(pbus), pbus->number, device); 379 retval = acpiphp_register_hotplug_slot(slot); 380 if (retval) { 381 if (retval == -EBUSY) 382 warn("Slot %llu already registered by another " 383 "hotplug driver\n", slot->sun); 384 else 385 warn("acpiphp_register_hotplug_slot failed " 386 "(err code = 0x%x)\n", retval); 387 388 status = AE_OK; 389 goto err; 390 } 391 } 392 393 newfunc->slot = slot; 394 mutex_lock(&bridge_mutex); 395 list_add_tail(&newfunc->sibling, &slot->funcs); 396 mutex_unlock(&bridge_mutex); 397 398 if (pci_bus_read_dev_vendor_id(pbus, PCI_DEVFN(device, function), 399 &val, 60*1000)) 400 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON); 401 402 if (is_dock_device(handle)) { 403 /* we don't want to call this device's _EJ0 404 * because we want the dock notify handler 405 * to call it after it calls _DCK 406 */ 407 newfunc->flags &= ~FUNC_HAS_EJ0; 408 if (register_hotplug_dock_device(handle, 409 &acpiphp_dock_ops, newfunc, 410 acpiphp_dock_init, acpiphp_dock_release)) 411 dbg("failed to register dock device\n"); 412 } 413 414 /* install notify handler */ 415 if (!(newfunc->flags & FUNC_HAS_DCK)) { 416 status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY, 417 handle_hotplug_event, 418 context); 419 if (ACPI_SUCCESS(status)) 420 context->handler_for_func = true; 421 else 422 err("failed to register interrupt notify handler\n"); 423 } 424 425 return AE_OK; 426 427 err: 428 bridge->nr_slots--; 429 mutex_lock(&bridge_mutex); 430 list_del(&slot->node); 431 mutex_unlock(&bridge_mutex); 432 kfree(slot); 433 434 err_out: 435 mutex_lock(&acpiphp_context_lock); 436 context->func = NULL; 437 acpiphp_put_context(context); 438 mutex_unlock(&acpiphp_context_lock); 439 kfree(newfunc); 440 return status; 441 } 442 443 444 /* see if it's worth looking at this bridge */ 445 static int detect_ejectable_slots(acpi_handle handle) 446 { 447 int found = acpi_pci_detect_ejectable(handle); 448 if (!found) { 449 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1, 450 is_pci_dock_device, NULL, (void *)&found, NULL); 451 } 452 return found; 453 } 454 455 static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle) 456 { 457 struct acpiphp_context *context; 458 struct acpiphp_bridge *bridge = NULL; 459 460 mutex_lock(&acpiphp_context_lock); 461 context = acpiphp_get_context(handle); 462 if (context) { 463 bridge = context->bridge; 464 if (bridge) 465 get_bridge(bridge); 466 467 acpiphp_put_context(context); 468 } 469 mutex_unlock(&acpiphp_context_lock); 470 return bridge; 471 } 472 473 static void cleanup_bridge(struct acpiphp_bridge *bridge) 474 { 475 struct acpiphp_slot *slot; 476 struct acpiphp_func *func; 477 acpi_status status; 478 acpi_handle handle = bridge->handle; 479 480 if (!bridge->context->handler_for_func) { 481 status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY, 482 handle_hotplug_event); 483 if (ACPI_FAILURE(status)) 484 err("failed to remove notify handler\n"); 485 } 486 487 list_for_each_entry(slot, &bridge->slots, node) { 488 list_for_each_entry(func, &slot->funcs, sibling) { 489 if (is_dock_device(func->handle)) { 490 unregister_hotplug_dock_device(func->handle); 491 } 492 if (!(func->flags & FUNC_HAS_DCK)) { 493 func->context->handler_for_func = false; 494 status = acpi_remove_notify_handler(func->handle, 495 ACPI_SYSTEM_NOTIFY, 496 handle_hotplug_event); 497 if (ACPI_FAILURE(status)) 498 err("failed to remove notify handler\n"); 499 } 500 } 501 acpiphp_unregister_hotplug_slot(slot); 502 } 503 504 mutex_lock(&bridge_mutex); 505 list_del(&bridge->list); 506 mutex_unlock(&bridge_mutex); 507 } 508 509 static int power_on_slot(struct acpiphp_slot *slot) 510 { 511 acpi_status status; 512 struct acpiphp_func *func; 513 int retval = 0; 514 515 /* if already enabled, just skip */ 516 if (slot->flags & SLOT_POWEREDON) 517 goto err_exit; 518 519 list_for_each_entry(func, &slot->funcs, sibling) { 520 if (func->flags & FUNC_HAS_PS0) { 521 dbg("%s: executing _PS0\n", __func__); 522 status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL); 523 if (ACPI_FAILURE(status)) { 524 warn("%s: _PS0 failed\n", __func__); 525 retval = -1; 526 goto err_exit; 527 } else 528 break; 529 } 530 } 531 532 /* TBD: evaluate _STA to check if the slot is enabled */ 533 534 slot->flags |= SLOT_POWEREDON; 535 536 err_exit: 537 return retval; 538 } 539 540 541 static int power_off_slot(struct acpiphp_slot *slot) 542 { 543 acpi_status status; 544 struct acpiphp_func *func; 545 546 int retval = 0; 547 548 /* if already disabled, just skip */ 549 if ((slot->flags & SLOT_POWEREDON) == 0) 550 goto err_exit; 551 552 list_for_each_entry(func, &slot->funcs, sibling) { 553 if (func->flags & FUNC_HAS_PS3) { 554 status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL); 555 if (ACPI_FAILURE(status)) { 556 warn("%s: _PS3 failed\n", __func__); 557 retval = -1; 558 goto err_exit; 559 } else 560 break; 561 } 562 } 563 564 /* TBD: evaluate _STA to check if the slot is disabled */ 565 566 slot->flags &= (~SLOT_POWEREDON); 567 568 err_exit: 569 return retval; 570 } 571 572 573 574 /** 575 * acpiphp_max_busnr - return the highest reserved bus number under the given bus. 576 * @bus: bus to start search with 577 */ 578 static unsigned char acpiphp_max_busnr(struct pci_bus *bus) 579 { 580 struct list_head *tmp; 581 unsigned char max, n; 582 583 /* 584 * pci_bus_max_busnr will return the highest 585 * reserved busnr for all these children. 586 * that is equivalent to the bus->subordinate 587 * value. We don't want to use the parent's 588 * bus->subordinate value because it could have 589 * padding in it. 590 */ 591 max = bus->busn_res.start; 592 593 list_for_each(tmp, &bus->children) { 594 n = pci_bus_max_busnr(pci_bus_b(tmp)); 595 if (n > max) 596 max = n; 597 } 598 return max; 599 } 600 601 602 /** 603 * acpiphp_bus_add - add a new bus to acpi subsystem 604 * @func: acpiphp_func of the bridge 605 */ 606 static int acpiphp_bus_add(struct acpiphp_func *func) 607 { 608 struct acpi_device *device; 609 int ret_val; 610 611 if (!acpi_bus_get_device(func->handle, &device)) { 612 dbg("bus exists... trim\n"); 613 /* this shouldn't be in here, so remove 614 * the bus then re-add it... 615 */ 616 acpi_bus_trim(device); 617 } 618 619 ret_val = acpi_bus_scan(func->handle); 620 if (!ret_val) 621 ret_val = acpi_bus_get_device(func->handle, &device); 622 623 if (ret_val) 624 dbg("error adding bus, %x\n", -ret_val); 625 626 return ret_val; 627 } 628 629 630 /** 631 * acpiphp_bus_trim - trim a bus from acpi subsystem 632 * @handle: handle to acpi namespace 633 */ 634 static int acpiphp_bus_trim(acpi_handle handle) 635 { 636 struct acpi_device *device; 637 int retval; 638 639 retval = acpi_bus_get_device(handle, &device); 640 if (retval) { 641 dbg("acpi_device not found\n"); 642 return retval; 643 } 644 645 acpi_bus_trim(device); 646 return 0; 647 } 648 649 static void acpiphp_set_acpi_region(struct acpiphp_slot *slot) 650 { 651 struct acpiphp_func *func; 652 union acpi_object params[2]; 653 struct acpi_object_list arg_list; 654 655 list_for_each_entry(func, &slot->funcs, sibling) { 656 arg_list.count = 2; 657 arg_list.pointer = params; 658 params[0].type = ACPI_TYPE_INTEGER; 659 params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG; 660 params[1].type = ACPI_TYPE_INTEGER; 661 params[1].integer.value = 1; 662 /* _REG is optional, we don't care about if there is failure */ 663 acpi_evaluate_object(func->handle, "_REG", &arg_list, NULL); 664 } 665 } 666 667 static void check_hotplug_bridge(struct acpiphp_slot *slot, struct pci_dev *dev) 668 { 669 struct acpiphp_func *func; 670 671 if (!dev->subordinate) 672 return; 673 674 /* quirk, or pcie could set it already */ 675 if (dev->is_hotplug_bridge) 676 return; 677 678 if (PCI_SLOT(dev->devfn) != slot->device) 679 return; 680 681 list_for_each_entry(func, &slot->funcs, sibling) { 682 if (PCI_FUNC(dev->devfn) == func->function) { 683 /* check if this bridge has ejectable slots */ 684 if ((detect_ejectable_slots(func->handle) > 0)) 685 dev->is_hotplug_bridge = 1; 686 break; 687 } 688 } 689 } 690 691 /** 692 * enable_device - enable, configure a slot 693 * @slot: slot to be enabled 694 * 695 * This function should be called per *physical slot*, 696 * not per each slot object in ACPI namespace. 697 */ 698 static int __ref enable_device(struct acpiphp_slot *slot) 699 { 700 struct pci_dev *dev; 701 struct pci_bus *bus = slot->bridge->pci_bus; 702 struct acpiphp_func *func; 703 int num, max, pass; 704 LIST_HEAD(add_list); 705 706 if (slot->flags & SLOT_ENABLED) 707 goto err_exit; 708 709 list_for_each_entry(func, &slot->funcs, sibling) 710 acpiphp_bus_add(func); 711 712 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0)); 713 if (num == 0) { 714 /* Maybe only part of funcs are added. */ 715 dbg("No new device found\n"); 716 goto err_exit; 717 } 718 719 max = acpiphp_max_busnr(bus); 720 for (pass = 0; pass < 2; pass++) { 721 list_for_each_entry(dev, &bus->devices, bus_list) { 722 if (PCI_SLOT(dev->devfn) != slot->device) 723 continue; 724 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE || 725 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) { 726 max = pci_scan_bridge(bus, dev, max, pass); 727 if (pass && dev->subordinate) { 728 check_hotplug_bridge(slot, dev); 729 pcibios_resource_survey_bus(dev->subordinate); 730 __pci_bus_size_bridges(dev->subordinate, 731 &add_list); 732 } 733 } 734 } 735 } 736 737 __pci_bus_assign_resources(bus, &add_list, NULL); 738 acpiphp_sanitize_bus(bus); 739 acpiphp_set_hpp_values(bus); 740 acpiphp_set_acpi_region(slot); 741 pci_enable_bridges(bus); 742 743 list_for_each_entry(dev, &bus->devices, bus_list) { 744 /* Assume that newly added devices are powered on already. */ 745 if (!dev->is_added) 746 dev->current_state = PCI_D0; 747 } 748 749 pci_bus_add_devices(bus); 750 751 slot->flags |= SLOT_ENABLED; 752 list_for_each_entry(func, &slot->funcs, sibling) { 753 dev = pci_get_slot(bus, PCI_DEVFN(slot->device, 754 func->function)); 755 if (!dev) { 756 /* Do not set SLOT_ENABLED flag if some funcs 757 are not added. */ 758 slot->flags &= (~SLOT_ENABLED); 759 continue; 760 } 761 } 762 763 764 err_exit: 765 return 0; 766 } 767 768 /* return first device in slot, acquiring a reference on it */ 769 static struct pci_dev *dev_in_slot(struct acpiphp_slot *slot) 770 { 771 struct pci_bus *bus = slot->bridge->pci_bus; 772 struct pci_dev *dev; 773 struct pci_dev *ret = NULL; 774 775 down_read(&pci_bus_sem); 776 list_for_each_entry(dev, &bus->devices, bus_list) 777 if (PCI_SLOT(dev->devfn) == slot->device) { 778 ret = pci_dev_get(dev); 779 break; 780 } 781 up_read(&pci_bus_sem); 782 783 return ret; 784 } 785 786 /** 787 * disable_device - disable a slot 788 * @slot: ACPI PHP slot 789 */ 790 static int disable_device(struct acpiphp_slot *slot) 791 { 792 struct acpiphp_func *func; 793 struct pci_dev *pdev; 794 795 /* 796 * enable_device() enumerates all functions in this device via 797 * pci_scan_slot(), whether they have associated ACPI hotplug 798 * methods (_EJ0, etc.) or not. Therefore, we remove all functions 799 * here. 800 */ 801 while ((pdev = dev_in_slot(slot))) { 802 pci_stop_and_remove_bus_device(pdev); 803 pci_dev_put(pdev); 804 } 805 806 list_for_each_entry(func, &slot->funcs, sibling) { 807 acpiphp_bus_trim(func->handle); 808 } 809 810 slot->flags &= (~SLOT_ENABLED); 811 812 return 0; 813 } 814 815 816 /** 817 * get_slot_status - get ACPI slot status 818 * @slot: ACPI PHP slot 819 * 820 * If a slot has _STA for each function and if any one of them 821 * returned non-zero status, return it. 822 * 823 * If a slot doesn't have _STA and if any one of its functions' 824 * configuration space is configured, return 0x0f as a _STA. 825 * 826 * Otherwise return 0. 827 */ 828 static unsigned int get_slot_status(struct acpiphp_slot *slot) 829 { 830 acpi_status status; 831 unsigned long long sta = 0; 832 u32 dvid; 833 struct acpiphp_func *func; 834 835 list_for_each_entry(func, &slot->funcs, sibling) { 836 if (func->flags & FUNC_HAS_STA) { 837 status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta); 838 if (ACPI_SUCCESS(status) && sta) 839 break; 840 } else { 841 pci_bus_read_config_dword(slot->bridge->pci_bus, 842 PCI_DEVFN(slot->device, 843 func->function), 844 PCI_VENDOR_ID, &dvid); 845 if (dvid != 0xffffffff) { 846 sta = ACPI_STA_ALL; 847 break; 848 } 849 } 850 } 851 852 return (unsigned int)sta; 853 } 854 855 /** 856 * acpiphp_eject_slot - physically eject the slot 857 * @slot: ACPI PHP slot 858 */ 859 int acpiphp_eject_slot(struct acpiphp_slot *slot) 860 { 861 struct acpiphp_func *func; 862 863 list_for_each_entry(func, &slot->funcs, sibling) { 864 /* We don't want to call _EJ0 on non-existing functions. */ 865 if ((func->flags & FUNC_HAS_EJ0)) { 866 if (ACPI_FAILURE(acpi_evaluate_ej0(func->handle))) 867 return -1; 868 else 869 break; 870 } 871 } 872 return 0; 873 } 874 875 /** 876 * acpiphp_check_bridge - re-enumerate devices 877 * @bridge: where to begin re-enumeration 878 * 879 * Iterate over all slots under this bridge and make sure that if a 880 * card is present they are enabled, and if not they are disabled. 881 */ 882 static int acpiphp_check_bridge(struct acpiphp_bridge *bridge) 883 { 884 struct acpiphp_slot *slot; 885 int retval = 0; 886 int enabled, disabled; 887 888 enabled = disabled = 0; 889 890 list_for_each_entry(slot, &bridge->slots, node) { 891 unsigned int status = get_slot_status(slot); 892 if (slot->flags & SLOT_ENABLED) { 893 if (status == ACPI_STA_ALL) 894 continue; 895 retval = acpiphp_disable_slot(slot); 896 if (retval) { 897 err("Error occurred in disabling\n"); 898 goto err_exit; 899 } else { 900 acpiphp_eject_slot(slot); 901 } 902 disabled++; 903 } else { 904 if (status != ACPI_STA_ALL) 905 continue; 906 retval = acpiphp_enable_slot(slot); 907 if (retval) { 908 err("Error occurred in enabling\n"); 909 goto err_exit; 910 } 911 enabled++; 912 } 913 } 914 915 dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled); 916 917 err_exit: 918 return retval; 919 } 920 921 static void acpiphp_set_hpp_values(struct pci_bus *bus) 922 { 923 struct pci_dev *dev; 924 925 list_for_each_entry(dev, &bus->devices, bus_list) 926 pci_configure_slot(dev); 927 } 928 929 /* 930 * Remove devices for which we could not assign resources, call 931 * arch specific code to fix-up the bus 932 */ 933 static void acpiphp_sanitize_bus(struct pci_bus *bus) 934 { 935 struct pci_dev *dev, *tmp; 936 int i; 937 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM; 938 939 list_for_each_entry_safe(dev, tmp, &bus->devices, bus_list) { 940 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) { 941 struct resource *res = &dev->resource[i]; 942 if ((res->flags & type_mask) && !res->start && 943 res->end) { 944 /* Could not assign a required resources 945 * for this device, remove it */ 946 pci_stop_and_remove_bus_device(dev); 947 break; 948 } 949 } 950 } 951 } 952 953 /* 954 * ACPI event handlers 955 */ 956 957 static acpi_status 958 check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv) 959 { 960 struct acpiphp_bridge *bridge; 961 char objname[64]; 962 struct acpi_buffer buffer = { .length = sizeof(objname), 963 .pointer = objname }; 964 965 bridge = acpiphp_handle_to_bridge(handle); 966 if (bridge) { 967 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer); 968 dbg("%s: re-enumerating slots under %s\n", 969 __func__, objname); 970 acpiphp_check_bridge(bridge); 971 put_bridge(bridge); 972 } 973 return AE_OK ; 974 } 975 976 void acpiphp_check_host_bridge(acpi_handle handle) 977 { 978 struct acpiphp_bridge *bridge; 979 980 bridge = acpiphp_handle_to_bridge(handle); 981 if (bridge) { 982 acpiphp_check_bridge(bridge); 983 put_bridge(bridge); 984 } 985 986 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 987 ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL); 988 } 989 990 static void _handle_hotplug_event_bridge(struct work_struct *work) 991 { 992 struct acpiphp_bridge *bridge; 993 char objname[64]; 994 struct acpi_buffer buffer = { .length = sizeof(objname), 995 .pointer = objname }; 996 struct acpi_hp_work *hp_work; 997 acpi_handle handle; 998 u32 type; 999 1000 hp_work = container_of(work, struct acpi_hp_work, work); 1001 handle = hp_work->handle; 1002 type = hp_work->type; 1003 bridge = (struct acpiphp_bridge *)hp_work->context; 1004 1005 acpi_scan_lock_acquire(); 1006 1007 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer); 1008 1009 switch (type) { 1010 case ACPI_NOTIFY_BUS_CHECK: 1011 /* bus re-enumerate */ 1012 dbg("%s: Bus check notify on %s\n", __func__, objname); 1013 dbg("%s: re-enumerating slots under %s\n", __func__, objname); 1014 acpiphp_check_bridge(bridge); 1015 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1016 ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL); 1017 break; 1018 1019 case ACPI_NOTIFY_DEVICE_CHECK: 1020 /* device check */ 1021 dbg("%s: Device check notify on %s\n", __func__, objname); 1022 acpiphp_check_bridge(bridge); 1023 break; 1024 1025 case ACPI_NOTIFY_DEVICE_WAKE: 1026 /* wake event */ 1027 dbg("%s: Device wake notify on %s\n", __func__, objname); 1028 break; 1029 1030 case ACPI_NOTIFY_EJECT_REQUEST: 1031 /* request device eject */ 1032 dbg("%s: Device eject notify on %s\n", __func__, objname); 1033 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) { 1034 struct acpiphp_slot *slot; 1035 slot = bridge->func->slot; 1036 if (!acpiphp_disable_slot(slot)) 1037 acpiphp_eject_slot(slot); 1038 } 1039 break; 1040 1041 case ACPI_NOTIFY_FREQUENCY_MISMATCH: 1042 printk(KERN_ERR "Device %s cannot be configured due" 1043 " to a frequency mismatch\n", objname); 1044 break; 1045 1046 case ACPI_NOTIFY_BUS_MODE_MISMATCH: 1047 printk(KERN_ERR "Device %s cannot be configured due" 1048 " to a bus mode mismatch\n", objname); 1049 break; 1050 1051 case ACPI_NOTIFY_POWER_FAULT: 1052 printk(KERN_ERR "Device %s has suffered a power fault\n", 1053 objname); 1054 break; 1055 1056 default: 1057 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname); 1058 break; 1059 } 1060 1061 acpi_scan_lock_release(); 1062 kfree(hp_work); /* allocated in handle_hotplug_event_bridge */ 1063 put_bridge(bridge); 1064 } 1065 1066 static void hotplug_event_func(acpi_handle handle, u32 type, void *context) 1067 { 1068 struct acpiphp_func *func = context; 1069 char objname[64]; 1070 struct acpi_buffer buffer = { .length = sizeof(objname), 1071 .pointer = objname }; 1072 1073 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer); 1074 1075 switch (type) { 1076 case ACPI_NOTIFY_BUS_CHECK: 1077 /* bus re-enumerate */ 1078 dbg("%s: Bus check notify on %s\n", __func__, objname); 1079 acpiphp_enable_slot(func->slot); 1080 break; 1081 1082 case ACPI_NOTIFY_DEVICE_CHECK: 1083 /* device check : re-enumerate from parent bus */ 1084 dbg("%s: Device check notify on %s\n", __func__, objname); 1085 acpiphp_check_bridge(func->slot->bridge); 1086 break; 1087 1088 case ACPI_NOTIFY_DEVICE_WAKE: 1089 /* wake event */ 1090 dbg("%s: Device wake notify on %s\n", __func__, objname); 1091 break; 1092 1093 case ACPI_NOTIFY_EJECT_REQUEST: 1094 /* request device eject */ 1095 dbg("%s: Device eject notify on %s\n", __func__, objname); 1096 if (!(acpiphp_disable_slot(func->slot))) 1097 acpiphp_eject_slot(func->slot); 1098 break; 1099 1100 default: 1101 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname); 1102 break; 1103 } 1104 } 1105 1106 static void _handle_hotplug_event_func(struct work_struct *work) 1107 { 1108 struct acpi_hp_work *hp_work; 1109 struct acpiphp_func *func; 1110 1111 hp_work = container_of(work, struct acpi_hp_work, work); 1112 func = hp_work->context; 1113 acpi_scan_lock_acquire(); 1114 1115 hotplug_event_func(hp_work->handle, hp_work->type, func); 1116 1117 acpi_scan_lock_release(); 1118 kfree(hp_work); /* allocated in handle_hotplug_event_func */ 1119 put_bridge(func->slot->bridge); 1120 } 1121 1122 /** 1123 * handle_hotplug_event - handle ACPI hotplug event 1124 * @handle: Notify()'ed acpi_handle 1125 * @type: Notify code 1126 * @data: pointer to acpiphp_context structure 1127 * 1128 * Handles ACPI event notification on slots. 1129 */ 1130 static void handle_hotplug_event(acpi_handle handle, u32 type, void *data) 1131 { 1132 struct acpiphp_context *context; 1133 void (*work_func)(struct work_struct *work) = NULL; 1134 1135 mutex_lock(&acpiphp_context_lock); 1136 context = acpiphp_get_context(handle); 1137 if (context) { 1138 if (context->bridge) { 1139 get_bridge(context->bridge); 1140 data = context->bridge; 1141 work_func = _handle_hotplug_event_bridge; 1142 } else if (context->func) { 1143 get_bridge(context->func->slot->bridge); 1144 data = context->func; 1145 work_func = _handle_hotplug_event_func; 1146 } 1147 acpiphp_put_context(context); 1148 } 1149 mutex_unlock(&acpiphp_context_lock); 1150 1151 /* 1152 * Currently the code adds all hotplug events to the kacpid_wq 1153 * queue when it should add hotplug events to the kacpi_hotplug_wq. 1154 * The proper way to fix this is to reorganize the code so that 1155 * drivers (dock, etc.) do not call acpi_os_execute(), etc. 1156 * For now just re-add this work to the kacpi_hotplug_wq so we 1157 * don't deadlock on hotplug actions. 1158 */ 1159 if (work_func) 1160 alloc_acpi_hp_work(handle, type, data, work_func); 1161 } 1162 1163 /* 1164 * Create hotplug slots for the PCI bus. 1165 * It should always return 0 to avoid skipping following notifiers. 1166 */ 1167 void acpiphp_enumerate_slots(struct pci_bus *bus) 1168 { 1169 struct acpiphp_context *context; 1170 struct acpiphp_bridge *bridge; 1171 acpi_handle handle; 1172 acpi_status status; 1173 1174 if (acpiphp_disabled) 1175 return; 1176 1177 handle = ACPI_HANDLE(bus->bridge); 1178 if (!handle || detect_ejectable_slots(handle) <= 0) 1179 return; 1180 1181 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL); 1182 if (!bridge) { 1183 acpi_handle_err(handle, "No memory for bridge object\n"); 1184 return; 1185 } 1186 1187 INIT_LIST_HEAD(&bridge->slots); 1188 kref_init(&bridge->ref); 1189 bridge->handle = handle; 1190 bridge->pci_dev = pci_dev_get(bus->self); 1191 bridge->pci_bus = bus; 1192 1193 mutex_lock(&acpiphp_context_lock); 1194 context = acpiphp_get_context(handle); 1195 if (!context) { 1196 context = acpiphp_init_context(handle); 1197 if (!context) { 1198 mutex_unlock(&acpiphp_context_lock); 1199 acpi_handle_err(handle, "No hotplug context\n"); 1200 kfree(bridge); 1201 return; 1202 } 1203 } 1204 bridge->context = context; 1205 context->bridge = bridge; 1206 mutex_unlock(&acpiphp_context_lock); 1207 1208 /* 1209 * Grab a ref to the subordinate PCI bus in case the bus is 1210 * removed via PCI core logical hotplug. The ref pins the bus 1211 * (which we access during module unload). 1212 */ 1213 get_device(&bus->dev); 1214 1215 /* must be added to the list prior to calling register_slot */ 1216 mutex_lock(&bridge_mutex); 1217 list_add(&bridge->list, &bridge_list); 1218 mutex_unlock(&bridge_mutex); 1219 1220 /* register all slot objects under this bridge */ 1221 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, 1, 1222 register_slot, NULL, bridge, NULL); 1223 if (ACPI_FAILURE(status)) { 1224 acpi_handle_err(bridge->handle, "failed to register slots\n"); 1225 goto err; 1226 } 1227 1228 if (pci_is_root_bus(bridge->pci_bus)) 1229 return; 1230 1231 if (acpi_has_method(bridge->handle, "_EJ0")) { 1232 dbg("found ejectable p2p bridge\n"); 1233 bridge->flags |= BRIDGE_HAS_EJ0; 1234 } 1235 if (context->handler_for_func) { 1236 /* Notify handler already installed. */ 1237 bridge->func = context->func; 1238 get_bridge(context->func->slot->bridge); 1239 return; 1240 } 1241 1242 /* install notify handler for P2P bridges */ 1243 status = acpi_install_notify_handler(bridge->handle, ACPI_SYSTEM_NOTIFY, 1244 handle_hotplug_event, NULL); 1245 if (ACPI_SUCCESS(status)) 1246 return; 1247 1248 acpi_handle_err(bridge->handle, "failed to register notify handler\n"); 1249 1250 err: 1251 cleanup_bridge(bridge); 1252 put_bridge(bridge); 1253 } 1254 1255 /* Destroy hotplug slots associated with the PCI bus */ 1256 void acpiphp_remove_slots(struct pci_bus *bus) 1257 { 1258 struct acpiphp_bridge *bridge, *tmp; 1259 1260 if (acpiphp_disabled) 1261 return; 1262 1263 list_for_each_entry_safe(bridge, tmp, &bridge_list, list) 1264 if (bridge->pci_bus == bus) { 1265 cleanup_bridge(bridge); 1266 put_bridge(bridge); 1267 break; 1268 } 1269 } 1270 1271 /** 1272 * acpiphp_enable_slot - power on slot 1273 * @slot: ACPI PHP slot 1274 */ 1275 int acpiphp_enable_slot(struct acpiphp_slot *slot) 1276 { 1277 int retval; 1278 1279 mutex_lock(&slot->crit_sect); 1280 1281 /* wake up all functions */ 1282 retval = power_on_slot(slot); 1283 if (retval) 1284 goto err_exit; 1285 1286 if (get_slot_status(slot) == ACPI_STA_ALL) { 1287 /* configure all functions */ 1288 retval = enable_device(slot); 1289 if (retval) 1290 power_off_slot(slot); 1291 } else { 1292 dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__); 1293 power_off_slot(slot); 1294 } 1295 1296 err_exit: 1297 mutex_unlock(&slot->crit_sect); 1298 return retval; 1299 } 1300 1301 /** 1302 * acpiphp_disable_slot - power off slot 1303 * @slot: ACPI PHP slot 1304 */ 1305 int acpiphp_disable_slot(struct acpiphp_slot *slot) 1306 { 1307 int retval = 0; 1308 1309 mutex_lock(&slot->crit_sect); 1310 1311 /* unconfigure all functions */ 1312 retval = disable_device(slot); 1313 if (retval) 1314 goto err_exit; 1315 1316 /* power off all functions */ 1317 retval = power_off_slot(slot); 1318 if (retval) 1319 goto err_exit; 1320 1321 err_exit: 1322 mutex_unlock(&slot->crit_sect); 1323 return retval; 1324 } 1325 1326 1327 /* 1328 * slot enabled: 1 1329 * slot disabled: 0 1330 */ 1331 u8 acpiphp_get_power_status(struct acpiphp_slot *slot) 1332 { 1333 return (slot->flags & SLOT_POWEREDON); 1334 } 1335 1336 1337 /* 1338 * latch open: 1 1339 * latch closed: 0 1340 */ 1341 u8 acpiphp_get_latch_status(struct acpiphp_slot *slot) 1342 { 1343 unsigned int sta; 1344 1345 sta = get_slot_status(slot); 1346 1347 return (sta & ACPI_STA_DEVICE_UI) ? 0 : 1; 1348 } 1349 1350 1351 /* 1352 * adapter presence : 1 1353 * absence : 0 1354 */ 1355 u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot) 1356 { 1357 unsigned int sta; 1358 1359 sta = get_slot_status(slot); 1360 1361 return (sta == 0) ? 0 : 1; 1362 } 1363