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