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);
59 #define MY_NAME "acpiphp_glue"
61 static void handle_hotplug_event_bridge (acpi_handle, u32, void *);
62 static void acpiphp_sanitize_bus(struct pci_bus *bus);
63 static void acpiphp_set_hpp_values(struct pci_bus *bus);
64 static void hotplug_event_func(acpi_handle handle, u32 type, void *context);
65 static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context);
66 static void free_bridge(struct kref *kref);
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 inline void get_bridge(struct acpiphp_bridge *bridge)
84 kref_get(&bridge->ref);
87 static inline void put_bridge(struct acpiphp_bridge *bridge)
89 kref_put(&bridge->ref, free_bridge);
92 static void free_bridge(struct kref *kref)
94 struct acpiphp_bridge *bridge;
95 struct acpiphp_slot *slot, *next;
96 struct acpiphp_func *func, *tmp;
98 bridge = container_of(kref, struct acpiphp_bridge, ref);
100 list_for_each_entry_safe(slot, next, &bridge->slots, node) {
101 list_for_each_entry_safe(func, tmp, &slot->funcs, sibling) {
107 /* Release reference acquired by acpiphp_bridge_handle_to_function() */
108 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func)
109 put_bridge(bridge->func->slot->bridge);
110 put_device(&bridge->pci_bus->dev);
111 pci_dev_put(bridge->pci_dev);
116 * the _DCK method can do funny things... and sometimes not
119 * TBD - figure out a way to only call fixups for
120 * systems that require them.
122 static void post_dock_fixups(acpi_handle not_used, u32 event, void *data)
124 struct acpiphp_func *func = data;
125 struct pci_bus *bus = func->slot->bridge->pci_bus;
131 /* fixup bad _DCK function that rewrites
132 * secondary bridge on slot
134 pci_read_config_dword(bus->self,
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);
148 static const struct acpi_dock_ops acpiphp_dock_ops = {
149 .fixup = post_dock_fixups,
150 .handler = hotplug_event_func,
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)
158 struct acpi_pci_root *root;
160 /* Check whether the PCIe port supports native PCIe hotplug */
161 if (pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, ®32))
163 if (!(reg32 & PCI_EXP_SLTCAP_HPC))
167 * Check whether native PCIe hotplug has been enabled for
168 * this PCIe hierarchy.
170 tmp = acpi_find_root_bridge_handle(pdev);
173 root = acpi_pci_find_root(tmp);
176 if (!(root->osc_control_set & OSC_PCI_EXPRESS_NATIVE_HP_CONTROL))
182 static void acpiphp_dock_init(void *data)
184 struct acpiphp_func *func = data;
186 get_bridge(func->slot->bridge);
189 static void acpiphp_dock_release(void *data)
191 struct acpiphp_func *func = data;
193 put_bridge(func->slot->bridge);
196 /* callback routine to register each ACPI PCI slot object */
198 register_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
200 struct acpiphp_bridge *bridge = (struct acpiphp_bridge *)context;
201 struct acpiphp_slot *slot;
202 struct acpiphp_func *newfunc;
203 acpi_status status = AE_OK;
204 unsigned long long adr, sun;
205 int device, function, retval, found = 0;
206 struct pci_bus *pbus = bridge->pci_bus;
207 struct pci_dev *pdev;
210 if (!acpi_pci_check_ejectable(pbus, handle) && !is_dock_device(handle))
213 status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
214 if (ACPI_FAILURE(status)) {
215 warn("can't evaluate _ADR (%#x)\n", status);
219 device = (adr >> 16) & 0xffff;
220 function = adr & 0xffff;
222 pdev = bridge->pci_dev;
223 if (pdev && device_is_managed_by_native_pciehp(pdev))
226 newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
230 newfunc->handle = handle;
231 newfunc->function = function;
233 if (acpi_has_method(handle, "_EJ0"))
234 newfunc->flags = FUNC_HAS_EJ0;
236 if (acpi_has_method(handle, "_STA"))
237 newfunc->flags |= FUNC_HAS_STA;
239 if (acpi_has_method(handle, "_PS0"))
240 newfunc->flags |= FUNC_HAS_PS0;
242 if (acpi_has_method(handle, "_PS3"))
243 newfunc->flags |= FUNC_HAS_PS3;
245 if (acpi_has_method(handle, "_DCK"))
246 newfunc->flags |= FUNC_HAS_DCK;
248 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
249 if (ACPI_FAILURE(status)) {
251 * use the count of the number of slots we've found
252 * for the number of the slot
254 sun = bridge->nr_slots+1;
257 /* search for objects that share the same slot */
258 list_for_each_entry(slot, &bridge->slots, node)
259 if (slot->device == device) {
260 if (slot->sun != sun)
261 warn("sibling found, but _SUN doesn't match!\n");
267 slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
273 slot->bridge = bridge;
274 slot->device = device;
276 INIT_LIST_HEAD(&slot->funcs);
277 mutex_init(&slot->crit_sect);
279 mutex_lock(&bridge_mutex);
280 list_add_tail(&slot->node, &bridge->slots);
281 mutex_unlock(&bridge_mutex);
284 dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
285 slot->sun, pci_domain_nr(pbus), pbus->number, device);
286 retval = acpiphp_register_hotplug_slot(slot);
288 if (retval == -EBUSY)
289 warn("Slot %llu already registered by another "
290 "hotplug driver\n", slot->sun);
292 warn("acpiphp_register_hotplug_slot failed "
293 "(err code = 0x%x)\n", retval);
298 newfunc->slot = slot;
299 mutex_lock(&bridge_mutex);
300 list_add_tail(&newfunc->sibling, &slot->funcs);
301 mutex_unlock(&bridge_mutex);
303 if (pci_bus_read_dev_vendor_id(pbus, PCI_DEVFN(device, function),
305 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
307 if (is_dock_device(handle)) {
308 /* we don't want to call this device's _EJ0
309 * because we want the dock notify handler
310 * to call it after it calls _DCK
312 newfunc->flags &= ~FUNC_HAS_EJ0;
313 if (register_hotplug_dock_device(handle,
314 &acpiphp_dock_ops, newfunc,
315 acpiphp_dock_init, acpiphp_dock_release))
316 dbg("failed to register dock device\n");
319 /* install notify handler */
320 if (!(newfunc->flags & FUNC_HAS_DCK)) {
321 status = acpi_install_notify_handler(handle,
323 handle_hotplug_event_func,
326 if (ACPI_FAILURE(status))
327 err("failed to register interrupt notify handler\n");
335 mutex_lock(&bridge_mutex);
336 list_del(&slot->node);
337 mutex_unlock(&bridge_mutex);
345 /* see if it's worth looking at this bridge */
346 static int detect_ejectable_slots(acpi_handle handle)
348 int found = acpi_pci_detect_ejectable(handle);
350 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
351 is_pci_dock_device, NULL, (void *)&found, NULL);
356 /* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
357 static void init_bridge_misc(struct acpiphp_bridge *bridge)
361 /* must be added to the list prior to calling register_slot */
362 mutex_lock(&bridge_mutex);
363 list_add(&bridge->list, &bridge_list);
364 mutex_unlock(&bridge_mutex);
366 /* register all slot objects under this bridge */
367 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
368 register_slot, NULL, bridge, NULL);
369 if (ACPI_FAILURE(status)) {
370 mutex_lock(&bridge_mutex);
371 list_del(&bridge->list);
372 mutex_unlock(&bridge_mutex);
376 /* install notify handler for P2P bridges */
377 if (!pci_is_root_bus(bridge->pci_bus)) {
378 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
379 status = acpi_remove_notify_handler(bridge->func->handle,
381 handle_hotplug_event_func);
382 if (ACPI_FAILURE(status))
383 err("failed to remove notify handler\n");
385 status = acpi_install_notify_handler(bridge->handle,
387 handle_hotplug_event_bridge,
390 if (ACPI_FAILURE(status)) {
391 err("failed to register interrupt notify handler\n");
397 /* find acpiphp_func from acpiphp_bridge */
398 static struct acpiphp_func *acpiphp_bridge_handle_to_function(acpi_handle handle)
400 struct acpiphp_bridge *bridge;
401 struct acpiphp_slot *slot;
402 struct acpiphp_func *func = NULL;
404 mutex_lock(&bridge_mutex);
405 list_for_each_entry(bridge, &bridge_list, list) {
406 list_for_each_entry(slot, &bridge->slots, node) {
407 list_for_each_entry(func, &slot->funcs, sibling) {
408 if (func->handle == handle) {
409 get_bridge(func->slot->bridge);
410 mutex_unlock(&bridge_mutex);
416 mutex_unlock(&bridge_mutex);
422 static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
424 struct acpiphp_bridge *bridge;
426 mutex_lock(&bridge_mutex);
427 list_for_each_entry(bridge, &bridge_list, list)
428 if (bridge->handle == handle) {
430 mutex_unlock(&bridge_mutex);
433 mutex_unlock(&bridge_mutex);
438 static void cleanup_bridge(struct acpiphp_bridge *bridge)
440 struct acpiphp_slot *slot;
441 struct acpiphp_func *func;
443 acpi_handle handle = bridge->handle;
445 if (!pci_is_root_bus(bridge->pci_bus)) {
446 status = acpi_remove_notify_handler(handle,
448 handle_hotplug_event_bridge);
449 if (ACPI_FAILURE(status))
450 err("failed to remove notify handler\n");
453 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
454 status = acpi_install_notify_handler(bridge->func->handle,
456 handle_hotplug_event_func,
458 if (ACPI_FAILURE(status))
459 err("failed to install interrupt notify handler\n");
462 list_for_each_entry(slot, &bridge->slots, node) {
463 list_for_each_entry(func, &slot->funcs, sibling) {
464 if (is_dock_device(func->handle)) {
465 unregister_hotplug_dock_device(func->handle);
467 if (!(func->flags & FUNC_HAS_DCK)) {
468 status = acpi_remove_notify_handler(func->handle,
470 handle_hotplug_event_func);
471 if (ACPI_FAILURE(status))
472 err("failed to remove notify handler\n");
475 acpiphp_unregister_hotplug_slot(slot);
478 mutex_lock(&bridge_mutex);
479 list_del(&bridge->list);
480 mutex_unlock(&bridge_mutex);
483 static int power_on_slot(struct acpiphp_slot *slot)
486 struct acpiphp_func *func;
489 /* if already enabled, just skip */
490 if (slot->flags & SLOT_POWEREDON)
493 list_for_each_entry(func, &slot->funcs, sibling) {
494 if (func->flags & FUNC_HAS_PS0) {
495 dbg("%s: executing _PS0\n", __func__);
496 status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
497 if (ACPI_FAILURE(status)) {
498 warn("%s: _PS0 failed\n", __func__);
506 /* TBD: evaluate _STA to check if the slot is enabled */
508 slot->flags |= SLOT_POWEREDON;
515 static int power_off_slot(struct acpiphp_slot *slot)
518 struct acpiphp_func *func;
522 /* if already disabled, just skip */
523 if ((slot->flags & SLOT_POWEREDON) == 0)
526 list_for_each_entry(func, &slot->funcs, sibling) {
527 if (func->flags & FUNC_HAS_PS3) {
528 status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
529 if (ACPI_FAILURE(status)) {
530 warn("%s: _PS3 failed\n", __func__);
538 /* TBD: evaluate _STA to check if the slot is disabled */
540 slot->flags &= (~SLOT_POWEREDON);
549 * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
550 * @bus: bus to start search with
552 static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
554 struct list_head *tmp;
555 unsigned char max, n;
558 * pci_bus_max_busnr will return the highest
559 * reserved busnr for all these children.
560 * that is equivalent to the bus->subordinate
561 * value. We don't want to use the parent's
562 * bus->subordinate value because it could have
565 max = bus->busn_res.start;
567 list_for_each(tmp, &bus->children) {
568 n = pci_bus_max_busnr(pci_bus_b(tmp));
577 * acpiphp_bus_add - add a new bus to acpi subsystem
578 * @func: acpiphp_func of the bridge
580 static int acpiphp_bus_add(struct acpiphp_func *func)
582 struct acpi_device *device;
585 if (!acpi_bus_get_device(func->handle, &device)) {
586 dbg("bus exists... trim\n");
587 /* this shouldn't be in here, so remove
588 * the bus then re-add it...
590 acpi_bus_trim(device);
593 ret_val = acpi_bus_scan(func->handle);
595 ret_val = acpi_bus_get_device(func->handle, &device);
598 dbg("error adding bus, %x\n", -ret_val);
605 * acpiphp_bus_trim - trim a bus from acpi subsystem
606 * @handle: handle to acpi namespace
608 static int acpiphp_bus_trim(acpi_handle handle)
610 struct acpi_device *device;
613 retval = acpi_bus_get_device(handle, &device);
615 dbg("acpi_device not found\n");
619 acpi_bus_trim(device);
623 static void acpiphp_set_acpi_region(struct acpiphp_slot *slot)
625 struct acpiphp_func *func;
626 union acpi_object params[2];
627 struct acpi_object_list arg_list;
629 list_for_each_entry(func, &slot->funcs, sibling) {
631 arg_list.pointer = params;
632 params[0].type = ACPI_TYPE_INTEGER;
633 params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG;
634 params[1].type = ACPI_TYPE_INTEGER;
635 params[1].integer.value = 1;
636 /* _REG is optional, we don't care about if there is failure */
637 acpi_evaluate_object(func->handle, "_REG", &arg_list, NULL);
641 static void check_hotplug_bridge(struct acpiphp_slot *slot, struct pci_dev *dev)
643 struct acpiphp_func *func;
645 if (!dev->subordinate)
648 /* quirk, or pcie could set it already */
649 if (dev->is_hotplug_bridge)
652 if (PCI_SLOT(dev->devfn) != slot->device)
655 list_for_each_entry(func, &slot->funcs, sibling) {
656 if (PCI_FUNC(dev->devfn) == func->function) {
657 /* check if this bridge has ejectable slots */
658 if ((detect_ejectable_slots(func->handle) > 0))
659 dev->is_hotplug_bridge = 1;
666 * enable_device - enable, configure a slot
667 * @slot: slot to be enabled
669 * This function should be called per *physical slot*,
670 * not per each slot object in ACPI namespace.
672 static int __ref enable_device(struct acpiphp_slot *slot)
675 struct pci_bus *bus = slot->bridge->pci_bus;
676 struct acpiphp_func *func;
680 if (slot->flags & SLOT_ENABLED)
683 list_for_each_entry(func, &slot->funcs, sibling)
684 acpiphp_bus_add(func);
686 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
688 /* Maybe only part of funcs are added. */
689 dbg("No new device found\n");
693 max = acpiphp_max_busnr(bus);
694 for (pass = 0; pass < 2; pass++) {
695 list_for_each_entry(dev, &bus->devices, bus_list) {
696 if (PCI_SLOT(dev->devfn) != slot->device)
698 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
699 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
700 max = pci_scan_bridge(bus, dev, max, pass);
701 if (pass && dev->subordinate) {
702 check_hotplug_bridge(slot, dev);
703 pcibios_resource_survey_bus(dev->subordinate);
704 __pci_bus_size_bridges(dev->subordinate,
711 __pci_bus_assign_resources(bus, &add_list, NULL);
712 acpiphp_sanitize_bus(bus);
713 acpiphp_set_hpp_values(bus);
714 acpiphp_set_acpi_region(slot);
715 pci_enable_bridges(bus);
717 list_for_each_entry(dev, &bus->devices, bus_list) {
718 /* Assume that newly added devices are powered on already. */
720 dev->current_state = PCI_D0;
723 pci_bus_add_devices(bus);
725 slot->flags |= SLOT_ENABLED;
726 list_for_each_entry(func, &slot->funcs, sibling) {
727 dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
730 /* Do not set SLOT_ENABLED flag if some funcs
732 slot->flags &= (~SLOT_ENABLED);
742 /* return first device in slot, acquiring a reference on it */
743 static struct pci_dev *dev_in_slot(struct acpiphp_slot *slot)
745 struct pci_bus *bus = slot->bridge->pci_bus;
747 struct pci_dev *ret = NULL;
749 down_read(&pci_bus_sem);
750 list_for_each_entry(dev, &bus->devices, bus_list)
751 if (PCI_SLOT(dev->devfn) == slot->device) {
752 ret = pci_dev_get(dev);
755 up_read(&pci_bus_sem);
761 * disable_device - disable a slot
762 * @slot: ACPI PHP slot
764 static int disable_device(struct acpiphp_slot *slot)
766 struct acpiphp_func *func;
767 struct pci_dev *pdev;
770 * enable_device() enumerates all functions in this device via
771 * pci_scan_slot(), whether they have associated ACPI hotplug
772 * methods (_EJ0, etc.) or not. Therefore, we remove all functions
775 while ((pdev = dev_in_slot(slot))) {
776 pci_stop_and_remove_bus_device(pdev);
780 list_for_each_entry(func, &slot->funcs, sibling) {
781 acpiphp_bus_trim(func->handle);
784 slot->flags &= (~SLOT_ENABLED);
791 * get_slot_status - get ACPI slot status
792 * @slot: ACPI PHP slot
794 * If a slot has _STA for each function and if any one of them
795 * returned non-zero status, return it.
797 * If a slot doesn't have _STA and if any one of its functions'
798 * configuration space is configured, return 0x0f as a _STA.
800 * Otherwise return 0.
802 static unsigned int get_slot_status(struct acpiphp_slot *slot)
805 unsigned long long sta = 0;
807 struct acpiphp_func *func;
809 list_for_each_entry(func, &slot->funcs, sibling) {
810 if (func->flags & FUNC_HAS_STA) {
811 status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
812 if (ACPI_SUCCESS(status) && sta)
815 pci_bus_read_config_dword(slot->bridge->pci_bus,
816 PCI_DEVFN(slot->device,
818 PCI_VENDOR_ID, &dvid);
819 if (dvid != 0xffffffff) {
826 return (unsigned int)sta;
830 * acpiphp_eject_slot - physically eject the slot
831 * @slot: ACPI PHP slot
833 int acpiphp_eject_slot(struct acpiphp_slot *slot)
835 struct acpiphp_func *func;
837 list_for_each_entry(func, &slot->funcs, sibling) {
838 /* We don't want to call _EJ0 on non-existing functions. */
839 if ((func->flags & FUNC_HAS_EJ0)) {
840 if (ACPI_FAILURE(acpi_evaluate_ej0(func->handle)))
850 * acpiphp_check_bridge - re-enumerate devices
851 * @bridge: where to begin re-enumeration
853 * Iterate over all slots under this bridge and make sure that if a
854 * card is present they are enabled, and if not they are disabled.
856 static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
858 struct acpiphp_slot *slot;
860 int enabled, disabled;
862 enabled = disabled = 0;
864 list_for_each_entry(slot, &bridge->slots, node) {
865 unsigned int status = get_slot_status(slot);
866 if (slot->flags & SLOT_ENABLED) {
867 if (status == ACPI_STA_ALL)
869 retval = acpiphp_disable_slot(slot);
871 err("Error occurred in disabling\n");
874 acpiphp_eject_slot(slot);
878 if (status != ACPI_STA_ALL)
880 retval = acpiphp_enable_slot(slot);
882 err("Error occurred in enabling\n");
889 dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
895 static void acpiphp_set_hpp_values(struct pci_bus *bus)
899 list_for_each_entry(dev, &bus->devices, bus_list)
900 pci_configure_slot(dev);
904 * Remove devices for which we could not assign resources, call
905 * arch specific code to fix-up the bus
907 static void acpiphp_sanitize_bus(struct pci_bus *bus)
909 struct pci_dev *dev, *tmp;
911 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
913 list_for_each_entry_safe(dev, tmp, &bus->devices, bus_list) {
914 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
915 struct resource *res = &dev->resource[i];
916 if ((res->flags & type_mask) && !res->start &&
918 /* Could not assign a required resources
919 * for this device, remove it */
920 pci_stop_and_remove_bus_device(dev);
928 * ACPI event handlers
932 check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
934 struct acpiphp_bridge *bridge;
936 struct acpi_buffer buffer = { .length = sizeof(objname),
937 .pointer = objname };
939 bridge = acpiphp_handle_to_bridge(handle);
941 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
942 dbg("%s: re-enumerating slots under %s\n",
944 acpiphp_check_bridge(bridge);
950 void acpiphp_check_host_bridge(acpi_handle handle)
952 struct acpiphp_bridge *bridge;
954 bridge = acpiphp_handle_to_bridge(handle);
956 acpiphp_check_bridge(bridge);
960 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
961 ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
964 static void _handle_hotplug_event_bridge(struct work_struct *work)
966 struct acpiphp_bridge *bridge;
968 struct acpi_buffer buffer = { .length = sizeof(objname),
969 .pointer = objname };
970 struct acpi_hp_work *hp_work;
974 hp_work = container_of(work, struct acpi_hp_work, work);
975 handle = hp_work->handle;
976 type = hp_work->type;
977 bridge = (struct acpiphp_bridge *)hp_work->context;
979 acpi_scan_lock_acquire();
981 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
984 case ACPI_NOTIFY_BUS_CHECK:
985 /* bus re-enumerate */
986 dbg("%s: Bus check notify on %s\n", __func__, objname);
987 dbg("%s: re-enumerating slots under %s\n", __func__, objname);
988 acpiphp_check_bridge(bridge);
989 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
990 ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
993 case ACPI_NOTIFY_DEVICE_CHECK:
995 dbg("%s: Device check notify on %s\n", __func__, objname);
996 acpiphp_check_bridge(bridge);
999 case ACPI_NOTIFY_DEVICE_WAKE:
1001 dbg("%s: Device wake notify on %s\n", __func__, objname);
1004 case ACPI_NOTIFY_EJECT_REQUEST:
1005 /* request device eject */
1006 dbg("%s: Device eject notify on %s\n", __func__, objname);
1007 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
1008 struct acpiphp_slot *slot;
1009 slot = bridge->func->slot;
1010 if (!acpiphp_disable_slot(slot))
1011 acpiphp_eject_slot(slot);
1015 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1016 printk(KERN_ERR "Device %s cannot be configured due"
1017 " to a frequency mismatch\n", objname);
1020 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1021 printk(KERN_ERR "Device %s cannot be configured due"
1022 " to a bus mode mismatch\n", objname);
1025 case ACPI_NOTIFY_POWER_FAULT:
1026 printk(KERN_ERR "Device %s has suffered a power fault\n",
1031 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1035 acpi_scan_lock_release();
1036 kfree(hp_work); /* allocated in handle_hotplug_event_bridge */
1041 * handle_hotplug_event_bridge - handle ACPI event on bridges
1042 * @handle: Notify()'ed acpi_handle
1043 * @type: Notify code
1044 * @context: pointer to acpiphp_bridge structure
1046 * Handles ACPI event notification on {host,p2p} bridges.
1048 static void handle_hotplug_event_bridge(acpi_handle handle, u32 type,
1051 struct acpiphp_bridge *bridge = context;
1054 * Currently the code adds all hotplug events to the kacpid_wq
1055 * queue when it should add hotplug events to the kacpi_hotplug_wq.
1056 * The proper way to fix this is to reorganize the code so that
1057 * drivers (dock, etc.) do not call acpi_os_execute(), etc.
1058 * For now just re-add this work to the kacpi_hotplug_wq so we
1059 * don't deadlock on hotplug actions.
1062 alloc_acpi_hp_work(handle, type, context, _handle_hotplug_event_bridge);
1065 static void hotplug_event_func(acpi_handle handle, u32 type, void *context)
1067 struct acpiphp_func *func = context;
1069 struct acpi_buffer buffer = { .length = sizeof(objname),
1070 .pointer = objname };
1072 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1075 case ACPI_NOTIFY_BUS_CHECK:
1076 /* bus re-enumerate */
1077 dbg("%s: Bus check notify on %s\n", __func__, objname);
1078 acpiphp_enable_slot(func->slot);
1081 case ACPI_NOTIFY_DEVICE_CHECK:
1082 /* device check : re-enumerate from parent bus */
1083 dbg("%s: Device check notify on %s\n", __func__, objname);
1084 acpiphp_check_bridge(func->slot->bridge);
1087 case ACPI_NOTIFY_DEVICE_WAKE:
1089 dbg("%s: Device wake notify on %s\n", __func__, objname);
1092 case ACPI_NOTIFY_EJECT_REQUEST:
1093 /* request device eject */
1094 dbg("%s: Device eject notify on %s\n", __func__, objname);
1095 if (!(acpiphp_disable_slot(func->slot)))
1096 acpiphp_eject_slot(func->slot);
1100 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1105 static void _handle_hotplug_event_func(struct work_struct *work)
1107 struct acpi_hp_work *hp_work;
1108 struct acpiphp_func *func;
1110 hp_work = container_of(work, struct acpi_hp_work, work);
1111 func = hp_work->context;
1112 acpi_scan_lock_acquire();
1114 hotplug_event_func(hp_work->handle, hp_work->type, func);
1116 acpi_scan_lock_release();
1117 kfree(hp_work); /* allocated in handle_hotplug_event_func */
1118 put_bridge(func->slot->bridge);
1122 * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
1123 * @handle: Notify()'ed acpi_handle
1124 * @type: Notify code
1125 * @context: pointer to acpiphp_func structure
1127 * Handles ACPI event notification on slots.
1129 static void handle_hotplug_event_func(acpi_handle handle, u32 type,
1132 struct acpiphp_func *func = context;
1135 * Currently the code adds all hotplug events to the kacpid_wq
1136 * queue when it should add hotplug events to the kacpi_hotplug_wq.
1137 * The proper way to fix this is to reorganize the code so that
1138 * drivers (dock, etc.) do not call acpi_os_execute(), etc.
1139 * For now just re-add this work to the kacpi_hotplug_wq so we
1140 * don't deadlock on hotplug actions.
1142 get_bridge(func->slot->bridge);
1143 alloc_acpi_hp_work(handle, type, context, _handle_hotplug_event_func);
1147 * Create hotplug slots for the PCI bus.
1148 * It should always return 0 to avoid skipping following notifiers.
1150 void acpiphp_enumerate_slots(struct pci_bus *bus)
1153 struct acpiphp_bridge *bridge;
1155 if (acpiphp_disabled)
1158 handle = ACPI_HANDLE(bus->bridge);
1159 if (!handle || detect_ejectable_slots(handle) <= 0)
1162 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
1163 if (bridge == NULL) {
1164 err("out of memory\n");
1168 INIT_LIST_HEAD(&bridge->slots);
1169 kref_init(&bridge->ref);
1170 bridge->handle = handle;
1171 bridge->pci_dev = pci_dev_get(bus->self);
1172 bridge->pci_bus = bus;
1175 * Grab a ref to the subordinate PCI bus in case the bus is
1176 * removed via PCI core logical hotplug. The ref pins the bus
1177 * (which we access during module unload).
1179 get_device(&bus->dev);
1181 if (!pci_is_root_bus(bridge->pci_bus) &&
1182 acpi_has_method(bridge->handle, "_EJ0")) {
1183 dbg("found ejectable p2p bridge\n");
1184 bridge->flags |= BRIDGE_HAS_EJ0;
1185 bridge->func = acpiphp_bridge_handle_to_function(handle);
1188 init_bridge_misc(bridge);
1191 /* Destroy hotplug slots associated with the PCI bus */
1192 void acpiphp_remove_slots(struct pci_bus *bus)
1194 struct acpiphp_bridge *bridge, *tmp;
1196 if (acpiphp_disabled)
1199 list_for_each_entry_safe(bridge, tmp, &bridge_list, list)
1200 if (bridge->pci_bus == bus) {
1201 cleanup_bridge(bridge);
1208 * acpiphp_enable_slot - power on slot
1209 * @slot: ACPI PHP slot
1211 int acpiphp_enable_slot(struct acpiphp_slot *slot)
1215 mutex_lock(&slot->crit_sect);
1217 /* wake up all functions */
1218 retval = power_on_slot(slot);
1222 if (get_slot_status(slot) == ACPI_STA_ALL) {
1223 /* configure all functions */
1224 retval = enable_device(slot);
1226 power_off_slot(slot);
1228 dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
1229 power_off_slot(slot);
1233 mutex_unlock(&slot->crit_sect);
1238 * acpiphp_disable_slot - power off slot
1239 * @slot: ACPI PHP slot
1241 int acpiphp_disable_slot(struct acpiphp_slot *slot)
1245 mutex_lock(&slot->crit_sect);
1247 /* unconfigure all functions */
1248 retval = disable_device(slot);
1252 /* power off all functions */
1253 retval = power_off_slot(slot);
1258 mutex_unlock(&slot->crit_sect);
1267 u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1269 return (slot->flags & SLOT_POWEREDON);
1277 u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1281 sta = get_slot_status(slot);
1283 return (sta & ACPI_STA_DEVICE_UI) ? 0 : 1;
1288 * adapter presence : 1
1291 u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1295 sta = get_slot_status(slot);
1297 return (sta == 0) ? 0 : 1;