2 * ACPI PCI HotPlug glue functions to ACPI CA subsystem
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
12 * All rights reserved.
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.
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
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.
29 * Send feedback to <kristen.c.accardi@intel.com>
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
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.
42 #include <linux/init.h>
43 #include <linux/module.h>
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>
56 static LIST_HEAD(bridge_list);
57 static DEFINE_MUTEX(bridge_mutex);
58 static DEFINE_MUTEX(acpiphp_context_lock);
60 #define MY_NAME "acpiphp_glue"
62 static void handle_hotplug_event(acpi_handle handle, u32 type, void *data);
63 static void acpiphp_sanitize_bus(struct pci_bus *bus);
64 static void acpiphp_set_hpp_values(struct pci_bus *bus);
65 static void hotplug_event(acpi_handle handle, u32 type, void *data);
66 static void free_bridge(struct kref *kref);
68 /* callback routine to check for the existence of a pci dock device */
70 is_pci_dock_device(acpi_handle handle, u32 lvl, void *context, void **rv)
72 int *count = (int *)context;
74 if (is_dock_device(handle)) {
76 return AE_CTRL_TERMINATE;
82 static void acpiphp_context_handler(acpi_handle handle, void *context)
84 /* Intentionally empty. */
88 * acpiphp_init_context - Create hotplug context and grab a reference to it.
89 * @handle: ACPI object handle to create the context for.
91 * Call under acpiphp_context_lock.
93 static struct acpiphp_context *acpiphp_init_context(acpi_handle handle)
95 struct acpiphp_context *context;
98 context = kzalloc(sizeof(*context), GFP_KERNEL);
102 context->handle = handle;
103 context->refcount = 1;
104 status = acpi_attach_data(handle, acpiphp_context_handler, context);
105 if (ACPI_FAILURE(status)) {
113 * acpiphp_get_context - Get hotplug context and grab a reference to it.
114 * @handle: ACPI object handle to get the context for.
116 * Call under acpiphp_context_lock.
118 static struct acpiphp_context *acpiphp_get_context(acpi_handle handle)
120 struct acpiphp_context *context = NULL;
124 status = acpi_get_data(handle, acpiphp_context_handler, &data);
125 if (ACPI_SUCCESS(status)) {
133 * acpiphp_put_context - Drop a reference to ACPI hotplug context.
134 * @handle: ACPI object handle to put the context for.
136 * The context object is removed if there are no more references to it.
138 * Call under acpiphp_context_lock.
140 static void acpiphp_put_context(struct acpiphp_context *context)
142 if (--context->refcount)
145 WARN_ON(context->func || context->bridge);
146 acpi_detach_data(context->handle, acpiphp_context_handler);
150 static inline void get_bridge(struct acpiphp_bridge *bridge)
152 kref_get(&bridge->ref);
155 static inline void put_bridge(struct acpiphp_bridge *bridge)
157 kref_put(&bridge->ref, free_bridge);
160 static void free_bridge(struct kref *kref)
162 struct acpiphp_context *context;
163 struct acpiphp_bridge *bridge;
164 struct acpiphp_slot *slot, *next;
165 struct acpiphp_func *func, *tmp;
167 mutex_lock(&acpiphp_context_lock);
169 bridge = container_of(kref, struct acpiphp_bridge, ref);
171 list_for_each_entry_safe(slot, next, &bridge->slots, node) {
172 list_for_each_entry_safe(func, tmp, &slot->funcs, sibling) {
173 context = func->context;
174 context->func = NULL;
175 acpiphp_put_context(context);
181 context = bridge->context;
182 /* Release the reference acquired by acpiphp_enumerate_slots(). */
183 if (context->handler_for_func)
184 put_bridge(context->func->slot->bridge);
186 put_device(&bridge->pci_bus->dev);
187 pci_dev_put(bridge->pci_dev);
188 context->bridge = NULL;
189 acpiphp_put_context(context);
192 mutex_unlock(&acpiphp_context_lock);
196 * the _DCK method can do funny things... and sometimes not
199 * TBD - figure out a way to only call fixups for
200 * systems that require them.
202 static void post_dock_fixups(acpi_handle not_used, u32 event, void *data)
204 struct acpiphp_context *context = data;
205 struct pci_bus *bus = context->func->slot->bridge->pci_bus;
211 /* fixup bad _DCK function that rewrites
212 * secondary bridge on slot
214 pci_read_config_dword(bus->self,
218 if (((buses >> 8) & 0xff) != bus->busn_res.start) {
219 buses = (buses & 0xff000000)
220 | ((unsigned int)(bus->primary) << 0)
221 | ((unsigned int)(bus->busn_res.start) << 8)
222 | ((unsigned int)(bus->busn_res.end) << 16);
223 pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses);
228 static const struct acpi_dock_ops acpiphp_dock_ops = {
229 .fixup = post_dock_fixups,
230 .handler = hotplug_event,
233 /* Check whether the PCI device is managed by native PCIe hotplug driver */
234 static bool device_is_managed_by_native_pciehp(struct pci_dev *pdev)
238 struct acpi_pci_root *root;
240 /* Check whether the PCIe port supports native PCIe hotplug */
241 if (pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, ®32))
243 if (!(reg32 & PCI_EXP_SLTCAP_HPC))
247 * Check whether native PCIe hotplug has been enabled for
248 * this PCIe hierarchy.
250 tmp = acpi_find_root_bridge_handle(pdev);
253 root = acpi_pci_find_root(tmp);
256 if (!(root->osc_control_set & OSC_PCI_EXPRESS_NATIVE_HP_CONTROL))
262 static void acpiphp_dock_init(void *data)
264 struct acpiphp_context *context = data;
266 get_bridge(context->func->slot->bridge);
269 static void acpiphp_dock_release(void *data)
271 struct acpiphp_context *context = data;
273 put_bridge(context->func->slot->bridge);
276 /* callback routine to register each ACPI PCI slot object */
277 static acpi_status register_slot(acpi_handle handle, u32 lvl, void *data,
280 struct acpiphp_bridge *bridge = data;
281 struct acpiphp_context *context;
282 struct acpiphp_slot *slot;
283 struct acpiphp_func *newfunc;
284 acpi_status status = AE_OK;
285 unsigned long long adr, sun;
286 int device, function, retval, found = 0;
287 struct pci_bus *pbus = bridge->pci_bus;
288 struct pci_dev *pdev;
291 if (!acpi_pci_check_ejectable(pbus, handle) && !is_dock_device(handle))
294 status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
295 if (ACPI_FAILURE(status)) {
296 warn("can't evaluate _ADR (%#x)\n", status);
300 device = (adr >> 16) & 0xffff;
301 function = adr & 0xffff;
303 pdev = bridge->pci_dev;
304 if (pdev && device_is_managed_by_native_pciehp(pdev))
307 newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
311 newfunc->handle = handle;
312 newfunc->function = function;
314 mutex_lock(&acpiphp_context_lock);
315 context = acpiphp_init_context(handle);
317 mutex_unlock(&acpiphp_context_lock);
318 acpi_handle_err(handle, "No hotplug context\n");
322 newfunc->context = context;
323 context->func = newfunc;
324 mutex_unlock(&acpiphp_context_lock);
326 if (acpi_has_method(handle, "_EJ0"))
327 newfunc->flags = FUNC_HAS_EJ0;
329 if (acpi_has_method(handle, "_STA"))
330 newfunc->flags |= FUNC_HAS_STA;
332 if (acpi_has_method(handle, "_PS0"))
333 newfunc->flags |= FUNC_HAS_PS0;
335 if (acpi_has_method(handle, "_PS3"))
336 newfunc->flags |= FUNC_HAS_PS3;
338 if (acpi_has_method(handle, "_DCK"))
339 newfunc->flags |= FUNC_HAS_DCK;
341 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
342 if (ACPI_FAILURE(status)) {
344 * use the count of the number of slots we've found
345 * for the number of the slot
347 sun = bridge->nr_slots+1;
350 /* search for objects that share the same slot */
351 list_for_each_entry(slot, &bridge->slots, node)
352 if (slot->device == device) {
353 if (slot->sun != sun)
354 warn("sibling found, but _SUN doesn't match!\n");
360 slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
362 status = AE_NO_MEMORY;
366 slot->bridge = bridge;
367 slot->device = device;
369 INIT_LIST_HEAD(&slot->funcs);
370 mutex_init(&slot->crit_sect);
372 mutex_lock(&bridge_mutex);
373 list_add_tail(&slot->node, &bridge->slots);
374 mutex_unlock(&bridge_mutex);
377 dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
378 slot->sun, pci_domain_nr(pbus), pbus->number, device);
379 retval = acpiphp_register_hotplug_slot(slot);
381 if (retval == -EBUSY)
382 warn("Slot %llu already registered by another "
383 "hotplug driver\n", slot->sun);
385 warn("acpiphp_register_hotplug_slot failed "
386 "(err code = 0x%x)\n", retval);
393 newfunc->slot = slot;
394 mutex_lock(&bridge_mutex);
395 list_add_tail(&newfunc->sibling, &slot->funcs);
396 mutex_unlock(&bridge_mutex);
398 if (pci_bus_read_dev_vendor_id(pbus, PCI_DEVFN(device, function),
400 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
402 if (is_dock_device(handle)) {
403 /* we don't want to call this device's _EJ0
404 * because we want the dock notify handler
405 * to call it after it calls _DCK
407 newfunc->flags &= ~FUNC_HAS_EJ0;
408 if (register_hotplug_dock_device(handle,
409 &acpiphp_dock_ops, context,
410 acpiphp_dock_init, acpiphp_dock_release))
411 dbg("failed to register dock device\n");
414 /* install notify handler */
415 if (!(newfunc->flags & FUNC_HAS_DCK)) {
416 status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
417 handle_hotplug_event,
419 if (ACPI_SUCCESS(status))
420 context->handler_for_func = true;
422 err("failed to register interrupt notify handler\n");
429 mutex_lock(&bridge_mutex);
430 list_del(&slot->node);
431 mutex_unlock(&bridge_mutex);
435 mutex_lock(&acpiphp_context_lock);
436 context->func = NULL;
437 acpiphp_put_context(context);
438 mutex_unlock(&acpiphp_context_lock);
444 /* see if it's worth looking at this bridge */
445 static int detect_ejectable_slots(acpi_handle handle)
447 int found = acpi_pci_detect_ejectable(handle);
449 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
450 is_pci_dock_device, NULL, (void *)&found, NULL);
455 static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
457 struct acpiphp_context *context;
458 struct acpiphp_bridge *bridge = NULL;
460 mutex_lock(&acpiphp_context_lock);
461 context = acpiphp_get_context(handle);
463 bridge = context->bridge;
467 acpiphp_put_context(context);
469 mutex_unlock(&acpiphp_context_lock);
473 static void cleanup_bridge(struct acpiphp_bridge *bridge)
475 struct acpiphp_slot *slot;
476 struct acpiphp_func *func;
478 acpi_handle handle = bridge->handle;
480 if (!bridge->context->handler_for_func) {
481 status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
482 handle_hotplug_event);
483 if (ACPI_FAILURE(status))
484 err("failed to remove notify handler\n");
487 list_for_each_entry(slot, &bridge->slots, node) {
488 list_for_each_entry(func, &slot->funcs, sibling) {
489 if (is_dock_device(func->handle)) {
490 unregister_hotplug_dock_device(func->handle);
492 if (!(func->flags & FUNC_HAS_DCK)) {
493 func->context->handler_for_func = false;
494 status = acpi_remove_notify_handler(func->handle,
496 handle_hotplug_event);
497 if (ACPI_FAILURE(status))
498 err("failed to remove notify handler\n");
501 acpiphp_unregister_hotplug_slot(slot);
504 mutex_lock(&bridge_mutex);
505 list_del(&bridge->list);
506 mutex_unlock(&bridge_mutex);
509 static int power_on_slot(struct acpiphp_slot *slot)
512 struct acpiphp_func *func;
515 /* if already enabled, just skip */
516 if (slot->flags & SLOT_POWEREDON)
519 list_for_each_entry(func, &slot->funcs, sibling) {
520 if (func->flags & FUNC_HAS_PS0) {
521 dbg("%s: executing _PS0\n", __func__);
522 status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
523 if (ACPI_FAILURE(status)) {
524 warn("%s: _PS0 failed\n", __func__);
532 /* TBD: evaluate _STA to check if the slot is enabled */
534 slot->flags |= SLOT_POWEREDON;
541 static int power_off_slot(struct acpiphp_slot *slot)
544 struct acpiphp_func *func;
548 /* if already disabled, just skip */
549 if ((slot->flags & SLOT_POWEREDON) == 0)
552 list_for_each_entry(func, &slot->funcs, sibling) {
553 if (func->flags & FUNC_HAS_PS3) {
554 status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
555 if (ACPI_FAILURE(status)) {
556 warn("%s: _PS3 failed\n", __func__);
564 /* TBD: evaluate _STA to check if the slot is disabled */
566 slot->flags &= (~SLOT_POWEREDON);
575 * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
576 * @bus: bus to start search with
578 static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
580 struct list_head *tmp;
581 unsigned char max, n;
584 * pci_bus_max_busnr will return the highest
585 * reserved busnr for all these children.
586 * that is equivalent to the bus->subordinate
587 * value. We don't want to use the parent's
588 * bus->subordinate value because it could have
591 max = bus->busn_res.start;
593 list_for_each(tmp, &bus->children) {
594 n = pci_bus_max_busnr(pci_bus_b(tmp));
603 * acpiphp_bus_add - add a new bus to acpi subsystem
604 * @func: acpiphp_func of the bridge
606 static int acpiphp_bus_add(struct acpiphp_func *func)
608 struct acpi_device *device;
611 if (!acpi_bus_get_device(func->handle, &device)) {
612 dbg("bus exists... trim\n");
613 /* this shouldn't be in here, so remove
614 * the bus then re-add it...
616 acpi_bus_trim(device);
619 ret_val = acpi_bus_scan(func->handle);
621 ret_val = acpi_bus_get_device(func->handle, &device);
624 dbg("error adding bus, %x\n", -ret_val);
631 * acpiphp_bus_trim - trim a bus from acpi subsystem
632 * @handle: handle to acpi namespace
634 static int acpiphp_bus_trim(acpi_handle handle)
636 struct acpi_device *device;
639 retval = acpi_bus_get_device(handle, &device);
641 dbg("acpi_device not found\n");
645 acpi_bus_trim(device);
649 static void acpiphp_set_acpi_region(struct acpiphp_slot *slot)
651 struct acpiphp_func *func;
652 union acpi_object params[2];
653 struct acpi_object_list arg_list;
655 list_for_each_entry(func, &slot->funcs, sibling) {
657 arg_list.pointer = params;
658 params[0].type = ACPI_TYPE_INTEGER;
659 params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG;
660 params[1].type = ACPI_TYPE_INTEGER;
661 params[1].integer.value = 1;
662 /* _REG is optional, we don't care about if there is failure */
663 acpi_evaluate_object(func->handle, "_REG", &arg_list, NULL);
667 static void check_hotplug_bridge(struct acpiphp_slot *slot, struct pci_dev *dev)
669 struct acpiphp_func *func;
671 if (!dev->subordinate)
674 /* quirk, or pcie could set it already */
675 if (dev->is_hotplug_bridge)
678 if (PCI_SLOT(dev->devfn) != slot->device)
681 list_for_each_entry(func, &slot->funcs, sibling) {
682 if (PCI_FUNC(dev->devfn) == func->function) {
683 /* check if this bridge has ejectable slots */
684 if ((detect_ejectable_slots(func->handle) > 0))
685 dev->is_hotplug_bridge = 1;
692 * enable_device - enable, configure a slot
693 * @slot: slot to be enabled
695 * This function should be called per *physical slot*,
696 * not per each slot object in ACPI namespace.
698 static int __ref enable_device(struct acpiphp_slot *slot)
701 struct pci_bus *bus = slot->bridge->pci_bus;
702 struct acpiphp_func *func;
706 if (slot->flags & SLOT_ENABLED)
709 list_for_each_entry(func, &slot->funcs, sibling)
710 acpiphp_bus_add(func);
712 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
714 /* Maybe only part of funcs are added. */
715 dbg("No new device found\n");
719 max = acpiphp_max_busnr(bus);
720 for (pass = 0; pass < 2; pass++) {
721 list_for_each_entry(dev, &bus->devices, bus_list) {
722 if (PCI_SLOT(dev->devfn) != slot->device)
724 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
725 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
726 max = pci_scan_bridge(bus, dev, max, pass);
727 if (pass && dev->subordinate) {
728 check_hotplug_bridge(slot, dev);
729 pcibios_resource_survey_bus(dev->subordinate);
730 __pci_bus_size_bridges(dev->subordinate,
737 __pci_bus_assign_resources(bus, &add_list, NULL);
738 acpiphp_sanitize_bus(bus);
739 acpiphp_set_hpp_values(bus);
740 acpiphp_set_acpi_region(slot);
741 pci_enable_bridges(bus);
743 list_for_each_entry(dev, &bus->devices, bus_list) {
744 /* Assume that newly added devices are powered on already. */
746 dev->current_state = PCI_D0;
749 pci_bus_add_devices(bus);
751 slot->flags |= SLOT_ENABLED;
752 list_for_each_entry(func, &slot->funcs, sibling) {
753 dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
756 /* Do not set SLOT_ENABLED flag if some funcs
758 slot->flags &= (~SLOT_ENABLED);
768 /* return first device in slot, acquiring a reference on it */
769 static struct pci_dev *dev_in_slot(struct acpiphp_slot *slot)
771 struct pci_bus *bus = slot->bridge->pci_bus;
773 struct pci_dev *ret = NULL;
775 down_read(&pci_bus_sem);
776 list_for_each_entry(dev, &bus->devices, bus_list)
777 if (PCI_SLOT(dev->devfn) == slot->device) {
778 ret = pci_dev_get(dev);
781 up_read(&pci_bus_sem);
787 * disable_device - disable a slot
788 * @slot: ACPI PHP slot
790 static int disable_device(struct acpiphp_slot *slot)
792 struct acpiphp_func *func;
793 struct pci_dev *pdev;
796 * enable_device() enumerates all functions in this device via
797 * pci_scan_slot(), whether they have associated ACPI hotplug
798 * methods (_EJ0, etc.) or not. Therefore, we remove all functions
801 while ((pdev = dev_in_slot(slot))) {
802 pci_stop_and_remove_bus_device(pdev);
806 list_for_each_entry(func, &slot->funcs, sibling) {
807 acpiphp_bus_trim(func->handle);
810 slot->flags &= (~SLOT_ENABLED);
817 * get_slot_status - get ACPI slot status
818 * @slot: ACPI PHP slot
820 * If a slot has _STA for each function and if any one of them
821 * returned non-zero status, return it.
823 * If a slot doesn't have _STA and if any one of its functions'
824 * configuration space is configured, return 0x0f as a _STA.
826 * Otherwise return 0.
828 static unsigned int get_slot_status(struct acpiphp_slot *slot)
831 unsigned long long sta = 0;
833 struct acpiphp_func *func;
835 list_for_each_entry(func, &slot->funcs, sibling) {
836 if (func->flags & FUNC_HAS_STA) {
837 status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
838 if (ACPI_SUCCESS(status) && sta)
841 pci_bus_read_config_dword(slot->bridge->pci_bus,
842 PCI_DEVFN(slot->device,
844 PCI_VENDOR_ID, &dvid);
845 if (dvid != 0xffffffff) {
852 return (unsigned int)sta;
856 * acpiphp_eject_slot - physically eject the slot
857 * @slot: ACPI PHP slot
859 int acpiphp_eject_slot(struct acpiphp_slot *slot)
861 struct acpiphp_func *func;
863 list_for_each_entry(func, &slot->funcs, sibling) {
864 /* We don't want to call _EJ0 on non-existing functions. */
865 if ((func->flags & FUNC_HAS_EJ0)) {
866 if (ACPI_FAILURE(acpi_evaluate_ej0(func->handle)))
876 * acpiphp_check_bridge - re-enumerate devices
877 * @bridge: where to begin re-enumeration
879 * Iterate over all slots under this bridge and make sure that if a
880 * card is present they are enabled, and if not they are disabled.
882 static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
884 struct acpiphp_slot *slot;
886 int enabled, disabled;
888 enabled = disabled = 0;
890 list_for_each_entry(slot, &bridge->slots, node) {
891 unsigned int status = get_slot_status(slot);
892 if (slot->flags & SLOT_ENABLED) {
893 if (status == ACPI_STA_ALL)
895 retval = acpiphp_disable_slot(slot);
897 err("Error occurred in disabling\n");
900 acpiphp_eject_slot(slot);
904 if (status != ACPI_STA_ALL)
906 retval = acpiphp_enable_slot(slot);
908 err("Error occurred in enabling\n");
915 dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
921 static void acpiphp_set_hpp_values(struct pci_bus *bus)
925 list_for_each_entry(dev, &bus->devices, bus_list)
926 pci_configure_slot(dev);
930 * Remove devices for which we could not assign resources, call
931 * arch specific code to fix-up the bus
933 static void acpiphp_sanitize_bus(struct pci_bus *bus)
935 struct pci_dev *dev, *tmp;
937 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
939 list_for_each_entry_safe(dev, tmp, &bus->devices, bus_list) {
940 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
941 struct resource *res = &dev->resource[i];
942 if ((res->flags & type_mask) && !res->start &&
944 /* Could not assign a required resources
945 * for this device, remove it */
946 pci_stop_and_remove_bus_device(dev);
954 * ACPI event handlers
958 check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
960 struct acpiphp_bridge *bridge;
962 struct acpi_buffer buffer = { .length = sizeof(objname),
963 .pointer = objname };
965 bridge = acpiphp_handle_to_bridge(handle);
967 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
968 dbg("%s: re-enumerating slots under %s\n",
970 acpiphp_check_bridge(bridge);
976 void acpiphp_check_host_bridge(acpi_handle handle)
978 struct acpiphp_bridge *bridge;
980 bridge = acpiphp_handle_to_bridge(handle);
982 acpiphp_check_bridge(bridge);
986 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
987 ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
990 static void hotplug_event(acpi_handle handle, u32 type, void *data)
992 struct acpiphp_context *context = data;
993 struct acpiphp_bridge *bridge;
994 struct acpiphp_func *func;
996 struct acpi_buffer buffer = { .length = sizeof(objname),
997 .pointer = objname };
999 mutex_lock(&acpiphp_context_lock);
1000 bridge = context->bridge;
1005 * If context->func is not NULL, we are holding a reference to its
1006 * parent bridge, so it won't go away until we drop that reference.
1008 func = context->func;
1009 mutex_unlock(&acpiphp_context_lock);
1011 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1014 case ACPI_NOTIFY_BUS_CHECK:
1015 /* bus re-enumerate */
1016 dbg("%s: Bus check notify on %s\n", __func__, objname);
1017 dbg("%s: re-enumerating slots under %s\n", __func__, objname);
1019 acpiphp_check_bridge(bridge);
1020 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
1021 ACPI_UINT32_MAX, check_sub_bridges,
1024 acpiphp_enable_slot(func->slot);
1028 case ACPI_NOTIFY_DEVICE_CHECK:
1030 dbg("%s: Device check notify on %s\n", __func__, objname);
1032 acpiphp_check_bridge(bridge);
1034 acpiphp_check_bridge(func->slot->bridge);
1038 case ACPI_NOTIFY_DEVICE_WAKE:
1040 dbg("%s: Device wake notify on %s\n", __func__, objname);
1043 case ACPI_NOTIFY_EJECT_REQUEST:
1044 /* request device eject */
1045 dbg("%s: Device eject notify on %s\n", __func__, objname);
1049 if (bridge && !(bridge->flags & BRIDGE_HAS_EJ0))
1052 if (!(acpiphp_disable_slot(func->slot)))
1053 acpiphp_eject_slot(func->slot);
1057 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1058 printk(KERN_ERR "Device %s cannot be configured due"
1059 " to a frequency mismatch\n", objname);
1062 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1063 printk(KERN_ERR "Device %s cannot be configured due"
1064 " to a bus mode mismatch\n", objname);
1067 case ACPI_NOTIFY_POWER_FAULT:
1068 printk(KERN_ERR "Device %s has suffered a power fault\n",
1073 warn("notify_handler: unknown event type 0x%x for %s\n", type,
1082 static void hotplug_event_work(struct work_struct *work)
1084 struct acpiphp_context *context;
1085 struct acpi_hp_work *hp_work;
1087 hp_work = container_of(work, struct acpi_hp_work, work);
1088 context = hp_work->context;
1089 acpi_scan_lock_acquire();
1091 hotplug_event(hp_work->handle, hp_work->type, context);
1093 acpi_scan_lock_release();
1094 kfree(hp_work); /* allocated in handle_hotplug_event() */
1096 mutex_lock(&acpiphp_context_lock);
1098 put_bridge(context->func->slot->bridge);
1100 acpiphp_put_context(context);
1102 mutex_unlock(&acpiphp_context_lock);
1106 * handle_hotplug_event - handle ACPI hotplug event
1107 * @handle: Notify()'ed acpi_handle
1108 * @type: Notify code
1109 * @data: pointer to acpiphp_context structure
1111 * Handles ACPI event notification on slots.
1113 static void handle_hotplug_event(acpi_handle handle, u32 type, void *data)
1115 struct acpiphp_context *context;
1117 mutex_lock(&acpiphp_context_lock);
1118 context = acpiphp_get_context(handle);
1120 if (context->func) {
1121 get_bridge(context->func->slot->bridge);
1122 acpiphp_put_context(context);
1123 } else if (!context->bridge) {
1124 acpiphp_put_context(context);
1128 mutex_unlock(&acpiphp_context_lock);
1130 * Currently the code adds all hotplug events to the kacpid_wq
1131 * queue when it should add hotplug events to the kacpi_hotplug_wq.
1132 * The proper way to fix this is to reorganize the code so that
1133 * drivers (dock, etc.) do not call acpi_os_execute(), etc.
1134 * For now just re-add this work to the kacpi_hotplug_wq so we
1135 * don't deadlock on hotplug actions.
1138 alloc_acpi_hp_work(handle, type, context, hotplug_event_work);
1142 * Create hotplug slots for the PCI bus.
1143 * It should always return 0 to avoid skipping following notifiers.
1145 void acpiphp_enumerate_slots(struct pci_bus *bus)
1147 struct acpiphp_context *context;
1148 struct acpiphp_bridge *bridge;
1152 if (acpiphp_disabled)
1155 handle = ACPI_HANDLE(bus->bridge);
1156 if (!handle || detect_ejectable_slots(handle) <= 0)
1159 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
1161 acpi_handle_err(handle, "No memory for bridge object\n");
1165 INIT_LIST_HEAD(&bridge->slots);
1166 kref_init(&bridge->ref);
1167 bridge->handle = handle;
1168 bridge->pci_dev = pci_dev_get(bus->self);
1169 bridge->pci_bus = bus;
1171 mutex_lock(&acpiphp_context_lock);
1172 context = acpiphp_get_context(handle);
1174 context = acpiphp_init_context(handle);
1176 mutex_unlock(&acpiphp_context_lock);
1177 acpi_handle_err(handle, "No hotplug context\n");
1182 bridge->context = context;
1183 context->bridge = bridge;
1184 mutex_unlock(&acpiphp_context_lock);
1187 * Grab a ref to the subordinate PCI bus in case the bus is
1188 * removed via PCI core logical hotplug. The ref pins the bus
1189 * (which we access during module unload).
1191 get_device(&bus->dev);
1193 /* must be added to the list prior to calling register_slot */
1194 mutex_lock(&bridge_mutex);
1195 list_add(&bridge->list, &bridge_list);
1196 mutex_unlock(&bridge_mutex);
1198 /* register all slot objects under this bridge */
1199 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, 1,
1200 register_slot, NULL, bridge, NULL);
1201 if (ACPI_FAILURE(status)) {
1202 acpi_handle_err(bridge->handle, "failed to register slots\n");
1206 if (pci_is_root_bus(bridge->pci_bus))
1209 if (acpi_has_method(bridge->handle, "_EJ0")) {
1210 dbg("found ejectable p2p bridge\n");
1211 bridge->flags |= BRIDGE_HAS_EJ0;
1213 if (context->handler_for_func) {
1214 /* Notify handler already installed. */
1215 get_bridge(context->func->slot->bridge);
1219 /* install notify handler for P2P bridges */
1220 status = acpi_install_notify_handler(bridge->handle, ACPI_SYSTEM_NOTIFY,
1221 handle_hotplug_event, NULL);
1222 if (ACPI_SUCCESS(status))
1225 acpi_handle_err(bridge->handle, "failed to register notify handler\n");
1228 cleanup_bridge(bridge);
1232 /* Destroy hotplug slots associated with the PCI bus */
1233 void acpiphp_remove_slots(struct pci_bus *bus)
1235 struct acpiphp_bridge *bridge, *tmp;
1237 if (acpiphp_disabled)
1240 list_for_each_entry_safe(bridge, tmp, &bridge_list, list)
1241 if (bridge->pci_bus == bus) {
1242 cleanup_bridge(bridge);
1249 * acpiphp_enable_slot - power on slot
1250 * @slot: ACPI PHP slot
1252 int acpiphp_enable_slot(struct acpiphp_slot *slot)
1256 mutex_lock(&slot->crit_sect);
1258 /* wake up all functions */
1259 retval = power_on_slot(slot);
1263 if (get_slot_status(slot) == ACPI_STA_ALL) {
1264 /* configure all functions */
1265 retval = enable_device(slot);
1267 power_off_slot(slot);
1269 dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
1270 power_off_slot(slot);
1274 mutex_unlock(&slot->crit_sect);
1279 * acpiphp_disable_slot - power off slot
1280 * @slot: ACPI PHP slot
1282 int acpiphp_disable_slot(struct acpiphp_slot *slot)
1286 mutex_lock(&slot->crit_sect);
1288 /* unconfigure all functions */
1289 retval = disable_device(slot);
1293 /* power off all functions */
1294 retval = power_off_slot(slot);
1299 mutex_unlock(&slot->crit_sect);
1308 u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1310 return (slot->flags & SLOT_POWEREDON);
1318 u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1322 sta = get_slot_status(slot);
1324 return (sta & ACPI_STA_DEVICE_UI) ? 0 : 1;
1329 * adapter presence : 1
1332 u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1336 sta = get_slot_status(slot);
1338 return (sta == 0) ? 0 : 1;