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