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