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 #define pr_fmt(fmt) "acpiphp_glue: " fmt 43 44 #include <linux/init.h> 45 #include <linux/module.h> 46 47 #include <linux/kernel.h> 48 #include <linux/pci.h> 49 #include <linux/pci_hotplug.h> 50 #include <linux/pci-acpi.h> 51 #include <linux/pm_runtime.h> 52 #include <linux/mutex.h> 53 #include <linux/slab.h> 54 #include <linux/acpi.h> 55 56 #include "../pci.h" 57 #include "acpiphp.h" 58 59 static LIST_HEAD(bridge_list); 60 static DEFINE_MUTEX(bridge_mutex); 61 62 static int acpiphp_hotplug_event(struct acpi_device *adev, u32 type); 63 static void acpiphp_sanitize_bus(struct pci_bus *bus); 64 static void acpiphp_set_hpp_values(struct pci_bus *bus); 65 static void hotplug_event(u32 type, struct acpiphp_context *context); 66 static void free_bridge(struct kref *kref); 67 68 /** 69 * acpiphp_init_context - Create hotplug context and grab a reference to it. 70 * @adev: ACPI device object to create the context for. 71 * 72 * Call under acpi_hp_context_lock. 73 */ 74 static struct acpiphp_context *acpiphp_init_context(struct acpi_device *adev) 75 { 76 struct acpiphp_context *context; 77 78 context = kzalloc(sizeof(*context), GFP_KERNEL); 79 if (!context) 80 return NULL; 81 82 context->refcount = 1; 83 acpi_set_hp_context(adev, &context->hp, acpiphp_hotplug_event); 84 return context; 85 } 86 87 /** 88 * acpiphp_get_context - Get hotplug context and grab a reference to it. 89 * @adev: ACPI device object to get the context for. 90 * 91 * Call under acpi_hp_context_lock. 92 */ 93 static struct acpiphp_context *acpiphp_get_context(struct acpi_device *adev) 94 { 95 struct acpiphp_context *context; 96 97 if (!adev->hp) 98 return NULL; 99 100 context = to_acpiphp_context(adev->hp); 101 context->refcount++; 102 return context; 103 } 104 105 /** 106 * acpiphp_put_context - Drop a reference to ACPI hotplug context. 107 * @context: ACPI hotplug context to drop a reference to. 108 * 109 * The context object is removed if there are no more references to it. 110 * 111 * Call under acpi_hp_context_lock. 112 */ 113 static void acpiphp_put_context(struct acpiphp_context *context) 114 { 115 if (--context->refcount) 116 return; 117 118 WARN_ON(context->bridge); 119 context->hp.self->hp = NULL; 120 kfree(context); 121 } 122 123 static inline void get_bridge(struct acpiphp_bridge *bridge) 124 { 125 kref_get(&bridge->ref); 126 } 127 128 static inline void put_bridge(struct acpiphp_bridge *bridge) 129 { 130 kref_put(&bridge->ref, free_bridge); 131 } 132 133 static void free_bridge(struct kref *kref) 134 { 135 struct acpiphp_context *context; 136 struct acpiphp_bridge *bridge; 137 struct acpiphp_slot *slot, *next; 138 struct acpiphp_func *func, *tmp; 139 140 acpi_lock_hp_context(); 141 142 bridge = container_of(kref, struct acpiphp_bridge, ref); 143 144 list_for_each_entry_safe(slot, next, &bridge->slots, node) { 145 list_for_each_entry_safe(func, tmp, &slot->funcs, sibling) 146 acpiphp_put_context(func_to_context(func)); 147 148 kfree(slot); 149 } 150 151 context = bridge->context; 152 /* Root bridges will not have hotplug context. */ 153 if (context) { 154 /* Release the reference taken by acpiphp_enumerate_slots(). */ 155 put_bridge(context->func.parent); 156 context->bridge = NULL; 157 acpiphp_put_context(context); 158 } 159 160 put_device(&bridge->pci_bus->dev); 161 pci_dev_put(bridge->pci_dev); 162 kfree(bridge); 163 164 acpi_unlock_hp_context(); 165 } 166 167 /* 168 * the _DCK method can do funny things... and sometimes not 169 * hah-hah funny. 170 * 171 * TBD - figure out a way to only call fixups for 172 * systems that require them. 173 */ 174 static void post_dock_fixups(acpi_handle not_used, u32 event, void *data) 175 { 176 struct acpiphp_context *context = data; 177 struct pci_bus *bus = context->func.slot->bus; 178 u32 buses; 179 180 if (!bus->self) 181 return; 182 183 /* fixup bad _DCK function that rewrites 184 * secondary bridge on slot 185 */ 186 pci_read_config_dword(bus->self, 187 PCI_PRIMARY_BUS, 188 &buses); 189 190 if (((buses >> 8) & 0xff) != bus->busn_res.start) { 191 buses = (buses & 0xff000000) 192 | ((unsigned int)(bus->primary) << 0) 193 | ((unsigned int)(bus->busn_res.start) << 8) 194 | ((unsigned int)(bus->busn_res.end) << 16); 195 pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses); 196 } 197 } 198 199 static void dock_event(acpi_handle handle, u32 type, void *data) 200 { 201 struct acpi_device *adev; 202 203 adev = acpi_bus_get_acpi_device(handle); 204 if (adev) { 205 acpiphp_hotplug_event(adev, type); 206 acpi_bus_put_acpi_device(adev); 207 } 208 } 209 210 static const struct acpi_dock_ops acpiphp_dock_ops = { 211 .fixup = post_dock_fixups, 212 .handler = dock_event, 213 }; 214 215 /* Check whether the PCI device is managed by native PCIe hotplug driver */ 216 static bool device_is_managed_by_native_pciehp(struct pci_dev *pdev) 217 { 218 u32 reg32; 219 acpi_handle tmp; 220 struct acpi_pci_root *root; 221 222 /* Check whether the PCIe port supports native PCIe hotplug */ 223 if (pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, ®32)) 224 return false; 225 if (!(reg32 & PCI_EXP_SLTCAP_HPC)) 226 return false; 227 228 /* 229 * Check whether native PCIe hotplug has been enabled for 230 * this PCIe hierarchy. 231 */ 232 tmp = acpi_find_root_bridge_handle(pdev); 233 if (!tmp) 234 return false; 235 root = acpi_pci_find_root(tmp); 236 if (!root) 237 return false; 238 if (!(root->osc_control_set & OSC_PCI_EXPRESS_NATIVE_HP_CONTROL)) 239 return false; 240 241 return true; 242 } 243 244 static void acpiphp_dock_init(void *data) 245 { 246 struct acpiphp_context *context = data; 247 248 get_bridge(context->func.parent); 249 } 250 251 static void acpiphp_dock_release(void *data) 252 { 253 struct acpiphp_context *context = data; 254 255 put_bridge(context->func.parent); 256 } 257 258 /** 259 * acpiphp_add_context - Add ACPIPHP context to an ACPI device object. 260 * @handle: ACPI handle of the object to add a context to. 261 * @lvl: Not used. 262 * @data: The object's parent ACPIPHP bridge. 263 * @rv: Not used. 264 */ 265 static acpi_status acpiphp_add_context(acpi_handle handle, u32 lvl, void *data, 266 void **rv) 267 { 268 struct acpiphp_bridge *bridge = data; 269 struct acpiphp_context *context; 270 struct acpi_device *adev; 271 struct acpiphp_slot *slot; 272 struct acpiphp_func *newfunc; 273 acpi_status status = AE_OK; 274 unsigned long long adr; 275 int device, function; 276 struct pci_bus *pbus = bridge->pci_bus; 277 struct pci_dev *pdev = bridge->pci_dev; 278 u32 val; 279 280 status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr); 281 if (ACPI_FAILURE(status)) { 282 if (status != AE_NOT_FOUND) 283 acpi_handle_warn(handle, 284 "can't evaluate _ADR (%#x)\n", status); 285 return AE_OK; 286 } 287 if (acpi_bus_get_device(handle, &adev)) 288 return AE_OK; 289 290 device = (adr >> 16) & 0xffff; 291 function = adr & 0xffff; 292 293 acpi_lock_hp_context(); 294 context = acpiphp_init_context(adev); 295 if (!context) { 296 acpi_unlock_hp_context(); 297 acpi_handle_err(handle, "No hotplug context\n"); 298 return AE_NOT_EXIST; 299 } 300 newfunc = &context->func; 301 newfunc->function = function; 302 newfunc->parent = bridge; 303 304 if (acpi_has_method(handle, "_EJ0")) 305 newfunc->flags = FUNC_HAS_EJ0; 306 307 if (acpi_has_method(handle, "_STA")) 308 newfunc->flags |= FUNC_HAS_STA; 309 310 /* 311 * Dock stations' notify handler should be used for dock devices instead 312 * of the common one, so clear hp.event in their contexts. 313 */ 314 if (acpi_has_method(handle, "_DCK")) 315 context->hp.event = NULL; 316 317 acpi_unlock_hp_context(); 318 319 /* search for objects that share the same slot */ 320 list_for_each_entry(slot, &bridge->slots, node) 321 if (slot->device == device) 322 goto slot_found; 323 324 slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL); 325 if (!slot) { 326 acpi_lock_hp_context(); 327 acpiphp_put_context(context); 328 acpi_unlock_hp_context(); 329 return AE_NO_MEMORY; 330 } 331 332 slot->bus = bridge->pci_bus; 333 slot->device = device; 334 INIT_LIST_HEAD(&slot->funcs); 335 336 list_add_tail(&slot->node, &bridge->slots); 337 338 /* 339 * Expose slots to user space for functions that have _EJ0 or _RMV or 340 * are located in dock stations. Do not expose them for devices handled 341 * by the native PCIe hotplug (PCIeHP), becuase that code is supposed to 342 * expose slots to user space in those cases. 343 */ 344 if ((acpi_pci_check_ejectable(pbus, handle) || is_dock_device(handle)) 345 && !(pdev && device_is_managed_by_native_pciehp(pdev))) { 346 unsigned long long sun; 347 int retval; 348 349 bridge->nr_slots++; 350 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun); 351 if (ACPI_FAILURE(status)) 352 sun = bridge->nr_slots; 353 354 pr_debug("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n", 355 sun, pci_domain_nr(pbus), pbus->number, device); 356 357 retval = acpiphp_register_hotplug_slot(slot, sun); 358 if (retval) { 359 slot->slot = NULL; 360 bridge->nr_slots--; 361 if (retval == -EBUSY) 362 pr_warn("Slot %llu already registered by another " 363 "hotplug driver\n", sun); 364 else 365 pr_warn("acpiphp_register_hotplug_slot failed " 366 "(err code = 0x%x)\n", retval); 367 } 368 /* Even if the slot registration fails, we can still use it. */ 369 } 370 371 slot_found: 372 newfunc->slot = slot; 373 list_add_tail(&newfunc->sibling, &slot->funcs); 374 375 if (pci_bus_read_dev_vendor_id(pbus, PCI_DEVFN(device, function), 376 &val, 60*1000)) 377 slot->flags |= SLOT_ENABLED; 378 379 if (is_dock_device(handle)) { 380 /* we don't want to call this device's _EJ0 381 * because we want the dock notify handler 382 * to call it after it calls _DCK 383 */ 384 newfunc->flags &= ~FUNC_HAS_EJ0; 385 if (register_hotplug_dock_device(handle, 386 &acpiphp_dock_ops, context, 387 acpiphp_dock_init, acpiphp_dock_release)) 388 pr_debug("failed to register dock device\n"); 389 } 390 391 return AE_OK; 392 } 393 394 static struct acpiphp_bridge *acpiphp_dev_to_bridge(struct acpi_device *adev) 395 { 396 struct acpiphp_context *context; 397 struct acpiphp_bridge *bridge = NULL; 398 399 acpi_lock_hp_context(); 400 context = acpiphp_get_context(adev); 401 if (context) { 402 bridge = context->bridge; 403 if (bridge) 404 get_bridge(bridge); 405 406 acpiphp_put_context(context); 407 } 408 acpi_unlock_hp_context(); 409 return bridge; 410 } 411 412 static void cleanup_bridge(struct acpiphp_bridge *bridge) 413 { 414 struct acpiphp_slot *slot; 415 struct acpiphp_func *func; 416 417 list_for_each_entry(slot, &bridge->slots, node) { 418 list_for_each_entry(func, &slot->funcs, sibling) { 419 struct acpi_device *adev = func_to_acpi_device(func); 420 421 if (is_dock_device(adev->handle)) 422 unregister_hotplug_dock_device(adev->handle); 423 424 acpi_lock_hp_context(); 425 adev->hp->event = NULL; 426 acpi_unlock_hp_context(); 427 } 428 slot->flags |= SLOT_IS_GOING_AWAY; 429 if (slot->slot) 430 acpiphp_unregister_hotplug_slot(slot); 431 } 432 433 mutex_lock(&bridge_mutex); 434 list_del(&bridge->list); 435 mutex_unlock(&bridge_mutex); 436 437 acpi_lock_hp_context(); 438 bridge->is_going_away = true; 439 acpi_unlock_hp_context(); 440 } 441 442 /** 443 * acpiphp_max_busnr - return the highest reserved bus number under the given bus. 444 * @bus: bus to start search with 445 */ 446 static unsigned char acpiphp_max_busnr(struct pci_bus *bus) 447 { 448 struct list_head *tmp; 449 unsigned char max, n; 450 451 /* 452 * pci_bus_max_busnr will return the highest 453 * reserved busnr for all these children. 454 * that is equivalent to the bus->subordinate 455 * value. We don't want to use the parent's 456 * bus->subordinate value because it could have 457 * padding in it. 458 */ 459 max = bus->busn_res.start; 460 461 list_for_each(tmp, &bus->children) { 462 n = pci_bus_max_busnr(pci_bus_b(tmp)); 463 if (n > max) 464 max = n; 465 } 466 return max; 467 } 468 469 static void acpiphp_set_acpi_region(struct acpiphp_slot *slot) 470 { 471 struct acpiphp_func *func; 472 union acpi_object params[2]; 473 struct acpi_object_list arg_list; 474 475 list_for_each_entry(func, &slot->funcs, sibling) { 476 arg_list.count = 2; 477 arg_list.pointer = params; 478 params[0].type = ACPI_TYPE_INTEGER; 479 params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG; 480 params[1].type = ACPI_TYPE_INTEGER; 481 params[1].integer.value = 1; 482 /* _REG is optional, we don't care about if there is failure */ 483 acpi_evaluate_object(func_to_handle(func), "_REG", &arg_list, 484 NULL); 485 } 486 } 487 488 static void check_hotplug_bridge(struct acpiphp_slot *slot, struct pci_dev *dev) 489 { 490 struct acpiphp_func *func; 491 492 /* quirk, or pcie could set it already */ 493 if (dev->is_hotplug_bridge) 494 return; 495 496 list_for_each_entry(func, &slot->funcs, sibling) { 497 if (PCI_FUNC(dev->devfn) == func->function) { 498 dev->is_hotplug_bridge = 1; 499 break; 500 } 501 } 502 } 503 504 static int acpiphp_rescan_slot(struct acpiphp_slot *slot) 505 { 506 struct acpiphp_func *func; 507 508 list_for_each_entry(func, &slot->funcs, sibling) { 509 struct acpi_device *adev = func_to_acpi_device(func); 510 511 acpi_bus_scan(adev->handle); 512 if (acpi_device_enumerated(adev)) 513 acpi_device_set_power(adev, ACPI_STATE_D0); 514 } 515 return pci_scan_slot(slot->bus, PCI_DEVFN(slot->device, 0)); 516 } 517 518 /** 519 * enable_slot - enable, configure a slot 520 * @slot: slot to be enabled 521 * 522 * This function should be called per *physical slot*, 523 * not per each slot object in ACPI namespace. 524 */ 525 static void __ref enable_slot(struct acpiphp_slot *slot) 526 { 527 struct pci_dev *dev; 528 struct pci_bus *bus = slot->bus; 529 struct acpiphp_func *func; 530 int max, pass; 531 LIST_HEAD(add_list); 532 533 acpiphp_rescan_slot(slot); 534 max = acpiphp_max_busnr(bus); 535 for (pass = 0; pass < 2; pass++) { 536 list_for_each_entry(dev, &bus->devices, bus_list) { 537 if (PCI_SLOT(dev->devfn) != slot->device) 538 continue; 539 540 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE || 541 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) { 542 max = pci_scan_bridge(bus, dev, max, pass); 543 if (pass && dev->subordinate) { 544 check_hotplug_bridge(slot, dev); 545 pcibios_resource_survey_bus(dev->subordinate); 546 __pci_bus_size_bridges(dev->subordinate, 547 &add_list); 548 } 549 } 550 } 551 } 552 __pci_bus_assign_resources(bus, &add_list, NULL); 553 554 acpiphp_sanitize_bus(bus); 555 acpiphp_set_hpp_values(bus); 556 acpiphp_set_acpi_region(slot); 557 558 list_for_each_entry(dev, &bus->devices, bus_list) { 559 /* Assume that newly added devices are powered on already. */ 560 if (!dev->is_added) 561 dev->current_state = PCI_D0; 562 } 563 564 pci_bus_add_devices(bus); 565 566 slot->flags |= SLOT_ENABLED; 567 list_for_each_entry(func, &slot->funcs, sibling) { 568 dev = pci_get_slot(bus, PCI_DEVFN(slot->device, 569 func->function)); 570 if (!dev) { 571 /* Do not set SLOT_ENABLED flag if some funcs 572 are not added. */ 573 slot->flags &= (~SLOT_ENABLED); 574 continue; 575 } 576 } 577 } 578 579 /** 580 * disable_slot - disable a slot 581 * @slot: ACPI PHP slot 582 */ 583 static void disable_slot(struct acpiphp_slot *slot) 584 { 585 struct pci_bus *bus = slot->bus; 586 struct pci_dev *dev, *prev; 587 struct acpiphp_func *func; 588 589 /* 590 * enable_slot() enumerates all functions in this device via 591 * pci_scan_slot(), whether they have associated ACPI hotplug 592 * methods (_EJ0, etc.) or not. Therefore, we remove all functions 593 * here. 594 */ 595 list_for_each_entry_safe_reverse(dev, prev, &bus->devices, bus_list) 596 if (PCI_SLOT(dev->devfn) == slot->device) 597 pci_stop_and_remove_bus_device(dev); 598 599 list_for_each_entry(func, &slot->funcs, sibling) 600 acpi_bus_trim(func_to_acpi_device(func)); 601 602 slot->flags &= (~SLOT_ENABLED); 603 } 604 605 static bool acpiphp_no_hotplug(struct acpi_device *adev) 606 { 607 return adev && adev->flags.no_hotplug; 608 } 609 610 static bool slot_no_hotplug(struct acpiphp_slot *slot) 611 { 612 struct acpiphp_func *func; 613 614 list_for_each_entry(func, &slot->funcs, sibling) 615 if (acpiphp_no_hotplug(func_to_acpi_device(func))) 616 return true; 617 618 return false; 619 } 620 621 /** 622 * get_slot_status - get ACPI slot status 623 * @slot: ACPI PHP slot 624 * 625 * If a slot has _STA for each function and if any one of them 626 * returned non-zero status, return it. 627 * 628 * If a slot doesn't have _STA and if any one of its functions' 629 * configuration space is configured, return 0x0f as a _STA. 630 * 631 * Otherwise return 0. 632 */ 633 static unsigned int get_slot_status(struct acpiphp_slot *slot) 634 { 635 unsigned long long sta = 0; 636 struct acpiphp_func *func; 637 638 list_for_each_entry(func, &slot->funcs, sibling) { 639 if (func->flags & FUNC_HAS_STA) { 640 acpi_status status; 641 642 status = acpi_evaluate_integer(func_to_handle(func), 643 "_STA", NULL, &sta); 644 if (ACPI_SUCCESS(status) && sta) 645 break; 646 } else { 647 u32 dvid; 648 649 pci_bus_read_config_dword(slot->bus, 650 PCI_DEVFN(slot->device, 651 func->function), 652 PCI_VENDOR_ID, &dvid); 653 if (dvid != 0xffffffff) { 654 sta = ACPI_STA_ALL; 655 break; 656 } 657 } 658 } 659 660 return (unsigned int)sta; 661 } 662 663 static inline bool device_status_valid(unsigned int sta) 664 { 665 /* 666 * ACPI spec says that _STA may return bit 0 clear with bit 3 set 667 * if the device is valid but does not require a device driver to be 668 * loaded (Section 6.3.7 of ACPI 5.0A). 669 */ 670 unsigned int mask = ACPI_STA_DEVICE_ENABLED | ACPI_STA_DEVICE_FUNCTIONING; 671 return (sta & mask) == mask; 672 } 673 674 /** 675 * trim_stale_devices - remove PCI devices that are not responding. 676 * @dev: PCI device to start walking the hierarchy from. 677 */ 678 static void trim_stale_devices(struct pci_dev *dev) 679 { 680 struct acpi_device *adev = ACPI_COMPANION(&dev->dev); 681 struct pci_bus *bus = dev->subordinate; 682 bool alive = false; 683 684 if (adev) { 685 acpi_status status; 686 unsigned long long sta; 687 688 status = acpi_evaluate_integer(adev->handle, "_STA", NULL, &sta); 689 alive = (ACPI_SUCCESS(status) && device_status_valid(sta)) 690 || acpiphp_no_hotplug(adev); 691 } 692 if (!alive) 693 alive = pci_device_is_present(dev); 694 695 if (!alive) { 696 pci_stop_and_remove_bus_device(dev); 697 if (adev) 698 acpi_bus_trim(adev); 699 } else if (bus) { 700 struct pci_dev *child, *tmp; 701 702 /* The device is a bridge. so check the bus below it. */ 703 pm_runtime_get_sync(&dev->dev); 704 list_for_each_entry_safe_reverse(child, tmp, &bus->devices, bus_list) 705 trim_stale_devices(child); 706 707 pm_runtime_put(&dev->dev); 708 } 709 } 710 711 /** 712 * acpiphp_check_bridge - re-enumerate devices 713 * @bridge: where to begin re-enumeration 714 * 715 * Iterate over all slots under this bridge and make sure that if a 716 * card is present they are enabled, and if not they are disabled. 717 */ 718 static void acpiphp_check_bridge(struct acpiphp_bridge *bridge) 719 { 720 struct acpiphp_slot *slot; 721 722 /* Bail out if the bridge is going away. */ 723 if (bridge->is_going_away) 724 return; 725 726 list_for_each_entry(slot, &bridge->slots, node) { 727 struct pci_bus *bus = slot->bus; 728 struct pci_dev *dev, *tmp; 729 730 if (slot_no_hotplug(slot)) { 731 ; /* do nothing */ 732 } else if (device_status_valid(get_slot_status(slot))) { 733 /* remove stale devices if any */ 734 list_for_each_entry_safe_reverse(dev, tmp, 735 &bus->devices, bus_list) 736 if (PCI_SLOT(dev->devfn) == slot->device) 737 trim_stale_devices(dev); 738 739 /* configure all functions */ 740 enable_slot(slot); 741 } else { 742 disable_slot(slot); 743 } 744 } 745 } 746 747 static void acpiphp_set_hpp_values(struct pci_bus *bus) 748 { 749 struct pci_dev *dev; 750 751 list_for_each_entry(dev, &bus->devices, bus_list) 752 pci_configure_slot(dev); 753 } 754 755 /* 756 * Remove devices for which we could not assign resources, call 757 * arch specific code to fix-up the bus 758 */ 759 static void acpiphp_sanitize_bus(struct pci_bus *bus) 760 { 761 struct pci_dev *dev, *tmp; 762 int i; 763 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM; 764 765 list_for_each_entry_safe_reverse(dev, tmp, &bus->devices, bus_list) { 766 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) { 767 struct resource *res = &dev->resource[i]; 768 if ((res->flags & type_mask) && !res->start && 769 res->end) { 770 /* Could not assign a required resources 771 * for this device, remove it */ 772 pci_stop_and_remove_bus_device(dev); 773 break; 774 } 775 } 776 } 777 } 778 779 /* 780 * ACPI event handlers 781 */ 782 783 void acpiphp_check_host_bridge(struct acpi_device *adev) 784 { 785 struct acpiphp_bridge *bridge; 786 787 bridge = acpiphp_dev_to_bridge(adev); 788 if (bridge) { 789 pci_lock_rescan_remove(); 790 791 acpiphp_check_bridge(bridge); 792 793 pci_unlock_rescan_remove(); 794 put_bridge(bridge); 795 } 796 } 797 798 static int acpiphp_disable_and_eject_slot(struct acpiphp_slot *slot); 799 800 static void hotplug_event(u32 type, struct acpiphp_context *context) 801 { 802 acpi_handle handle = context->hp.self->handle; 803 struct acpiphp_func *func = &context->func; 804 struct acpiphp_slot *slot = func->slot; 805 struct acpiphp_bridge *bridge; 806 807 acpi_lock_hp_context(); 808 bridge = context->bridge; 809 if (bridge) 810 get_bridge(bridge); 811 812 acpi_unlock_hp_context(); 813 814 pci_lock_rescan_remove(); 815 816 switch (type) { 817 case ACPI_NOTIFY_BUS_CHECK: 818 /* bus re-enumerate */ 819 acpi_handle_debug(handle, "Bus check in %s()\n", __func__); 820 if (bridge) 821 acpiphp_check_bridge(bridge); 822 else if (!(slot->flags & SLOT_IS_GOING_AWAY)) 823 enable_slot(slot); 824 825 break; 826 827 case ACPI_NOTIFY_DEVICE_CHECK: 828 /* device check */ 829 acpi_handle_debug(handle, "Device check in %s()\n", __func__); 830 if (bridge) { 831 acpiphp_check_bridge(bridge); 832 } else if (!(slot->flags & SLOT_IS_GOING_AWAY)) { 833 /* 834 * Check if anything has changed in the slot and rescan 835 * from the parent if that's the case. 836 */ 837 if (acpiphp_rescan_slot(slot)) 838 acpiphp_check_bridge(func->parent); 839 } 840 break; 841 842 case ACPI_NOTIFY_EJECT_REQUEST: 843 /* request device eject */ 844 acpi_handle_debug(handle, "Eject request in %s()\n", __func__); 845 acpiphp_disable_and_eject_slot(slot); 846 break; 847 } 848 849 pci_unlock_rescan_remove(); 850 if (bridge) 851 put_bridge(bridge); 852 } 853 854 static int acpiphp_hotplug_event(struct acpi_device *adev, u32 type) 855 { 856 struct acpiphp_context *context; 857 858 acpi_lock_hp_context(); 859 context = acpiphp_get_context(adev); 860 if (!context || context->func.parent->is_going_away) { 861 acpi_unlock_hp_context(); 862 return -ENODATA; 863 } 864 get_bridge(context->func.parent); 865 acpiphp_put_context(context); 866 acpi_unlock_hp_context(); 867 868 hotplug_event(type, context); 869 870 put_bridge(context->func.parent); 871 return 0; 872 } 873 874 /** 875 * acpiphp_enumerate_slots - Enumerate PCI slots for a given bus. 876 * @bus: PCI bus to enumerate the slots for. 877 * 878 * A "slot" is an object associated with a PCI device number. All functions 879 * (PCI devices) with the same bus and device number belong to the same slot. 880 */ 881 void acpiphp_enumerate_slots(struct pci_bus *bus) 882 { 883 struct acpiphp_bridge *bridge; 884 struct acpi_device *adev; 885 acpi_handle handle; 886 acpi_status status; 887 888 if (acpiphp_disabled) 889 return; 890 891 adev = ACPI_COMPANION(bus->bridge); 892 if (!adev) 893 return; 894 895 handle = adev->handle; 896 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL); 897 if (!bridge) { 898 acpi_handle_err(handle, "No memory for bridge object\n"); 899 return; 900 } 901 902 INIT_LIST_HEAD(&bridge->slots); 903 kref_init(&bridge->ref); 904 bridge->pci_dev = pci_dev_get(bus->self); 905 bridge->pci_bus = bus; 906 907 /* 908 * Grab a ref to the subordinate PCI bus in case the bus is 909 * removed via PCI core logical hotplug. The ref pins the bus 910 * (which we access during module unload). 911 */ 912 get_device(&bus->dev); 913 914 if (!pci_is_root_bus(bridge->pci_bus)) { 915 struct acpiphp_context *context; 916 917 /* 918 * This bridge should have been registered as a hotplug function 919 * under its parent, so the context should be there, unless the 920 * parent is going to be handled by pciehp, in which case this 921 * bridge is not interesting to us either. 922 */ 923 acpi_lock_hp_context(); 924 context = acpiphp_get_context(adev); 925 if (!context) { 926 acpi_unlock_hp_context(); 927 put_device(&bus->dev); 928 pci_dev_put(bridge->pci_dev); 929 kfree(bridge); 930 return; 931 } 932 bridge->context = context; 933 context->bridge = bridge; 934 /* Get a reference to the parent bridge. */ 935 get_bridge(context->func.parent); 936 acpi_unlock_hp_context(); 937 } 938 939 /* Must be added to the list prior to calling acpiphp_add_context(). */ 940 mutex_lock(&bridge_mutex); 941 list_add(&bridge->list, &bridge_list); 942 mutex_unlock(&bridge_mutex); 943 944 /* register all slot objects under this bridge */ 945 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1, 946 acpiphp_add_context, NULL, bridge, NULL); 947 if (ACPI_FAILURE(status)) { 948 acpi_handle_err(handle, "failed to register slots\n"); 949 cleanup_bridge(bridge); 950 put_bridge(bridge); 951 } 952 } 953 954 /** 955 * acpiphp_remove_slots - Remove slot objects associated with a given bus. 956 * @bus: PCI bus to remove the slot objects for. 957 */ 958 void acpiphp_remove_slots(struct pci_bus *bus) 959 { 960 struct acpiphp_bridge *bridge; 961 962 if (acpiphp_disabled) 963 return; 964 965 mutex_lock(&bridge_mutex); 966 list_for_each_entry(bridge, &bridge_list, list) 967 if (bridge->pci_bus == bus) { 968 mutex_unlock(&bridge_mutex); 969 cleanup_bridge(bridge); 970 put_bridge(bridge); 971 return; 972 } 973 974 mutex_unlock(&bridge_mutex); 975 } 976 977 /** 978 * acpiphp_enable_slot - power on slot 979 * @slot: ACPI PHP slot 980 */ 981 int acpiphp_enable_slot(struct acpiphp_slot *slot) 982 { 983 pci_lock_rescan_remove(); 984 985 if (slot->flags & SLOT_IS_GOING_AWAY) 986 return -ENODEV; 987 988 /* configure all functions */ 989 if (!(slot->flags & SLOT_ENABLED)) 990 enable_slot(slot); 991 992 pci_unlock_rescan_remove(); 993 return 0; 994 } 995 996 /** 997 * acpiphp_disable_and_eject_slot - power off and eject slot 998 * @slot: ACPI PHP slot 999 */ 1000 static int acpiphp_disable_and_eject_slot(struct acpiphp_slot *slot) 1001 { 1002 struct acpiphp_func *func; 1003 1004 if (slot->flags & SLOT_IS_GOING_AWAY) 1005 return -ENODEV; 1006 1007 /* unconfigure all functions */ 1008 disable_slot(slot); 1009 1010 list_for_each_entry(func, &slot->funcs, sibling) 1011 if (func->flags & FUNC_HAS_EJ0) { 1012 acpi_handle handle = func_to_handle(func); 1013 1014 if (ACPI_FAILURE(acpi_evaluate_ej0(handle))) 1015 acpi_handle_err(handle, "_EJ0 failed\n"); 1016 1017 break; 1018 } 1019 1020 return 0; 1021 } 1022 1023 int acpiphp_disable_slot(struct acpiphp_slot *slot) 1024 { 1025 int ret; 1026 1027 /* 1028 * Acquire acpi_scan_lock to ensure that the execution of _EJ0 in 1029 * acpiphp_disable_and_eject_slot() will be synchronized properly. 1030 */ 1031 acpi_scan_lock_acquire(); 1032 pci_lock_rescan_remove(); 1033 ret = acpiphp_disable_and_eject_slot(slot); 1034 pci_unlock_rescan_remove(); 1035 acpi_scan_lock_release(); 1036 return ret; 1037 } 1038 1039 /* 1040 * slot enabled: 1 1041 * slot disabled: 0 1042 */ 1043 u8 acpiphp_get_power_status(struct acpiphp_slot *slot) 1044 { 1045 return (slot->flags & SLOT_ENABLED); 1046 } 1047 1048 /* 1049 * latch open: 1 1050 * latch closed: 0 1051 */ 1052 u8 acpiphp_get_latch_status(struct acpiphp_slot *slot) 1053 { 1054 return !(get_slot_status(slot) & ACPI_STA_DEVICE_UI); 1055 } 1056 1057 /* 1058 * adapter presence : 1 1059 * absence : 0 1060 */ 1061 u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot) 1062 { 1063 return !!get_slot_status(slot); 1064 } 1065