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 static void acpiphp_context_handler(acpi_handle handle, void *context)
70 /* Intentionally empty. */
74 * acpiphp_init_context - Create hotplug context and grab a reference to it.
75 * @handle: ACPI object handle to create the context for.
77 * Call under acpiphp_context_lock.
79 static struct acpiphp_context *acpiphp_init_context(acpi_handle handle)
81 struct acpiphp_context *context;
84 context = kzalloc(sizeof(*context), GFP_KERNEL);
88 context->handle = handle;
89 context->refcount = 1;
90 status = acpi_attach_data(handle, acpiphp_context_handler, context);
91 if (ACPI_FAILURE(status)) {
99 * acpiphp_get_context - Get hotplug context and grab a reference to it.
100 * @handle: ACPI object handle to get the context for.
102 * Call under acpiphp_context_lock.
104 static struct acpiphp_context *acpiphp_get_context(acpi_handle handle)
106 struct acpiphp_context *context = NULL;
110 status = acpi_get_data(handle, acpiphp_context_handler, &data);
111 if (ACPI_SUCCESS(status)) {
119 * acpiphp_put_context - Drop a reference to ACPI hotplug context.
120 * @handle: ACPI object handle to put the context for.
122 * The context object is removed if there are no more references to it.
124 * Call under acpiphp_context_lock.
126 static void acpiphp_put_context(struct acpiphp_context *context)
128 if (--context->refcount)
131 WARN_ON(context->bridge);
132 acpi_detach_data(context->handle, acpiphp_context_handler);
136 static inline void get_bridge(struct acpiphp_bridge *bridge)
138 kref_get(&bridge->ref);
141 static inline void put_bridge(struct acpiphp_bridge *bridge)
143 kref_put(&bridge->ref, free_bridge);
146 static void free_bridge(struct kref *kref)
148 struct acpiphp_context *context;
149 struct acpiphp_bridge *bridge;
150 struct acpiphp_slot *slot, *next;
151 struct acpiphp_func *func, *tmp;
153 mutex_lock(&acpiphp_context_lock);
155 bridge = container_of(kref, struct acpiphp_bridge, ref);
157 list_for_each_entry_safe(slot, next, &bridge->slots, node) {
158 list_for_each_entry_safe(func, tmp, &slot->funcs, sibling)
159 acpiphp_put_context(func_to_context(func));
164 context = bridge->context;
165 /* Root bridges will not have hotplug context. */
167 /* Release the reference taken by acpiphp_enumerate_slots(). */
168 put_bridge(context->func.slot->bridge);
169 context->bridge = NULL;
170 acpiphp_put_context(context);
173 put_device(&bridge->pci_bus->dev);
174 pci_dev_put(bridge->pci_dev);
177 mutex_unlock(&acpiphp_context_lock);
181 * the _DCK method can do funny things... and sometimes not
184 * TBD - figure out a way to only call fixups for
185 * systems that require them.
187 static void post_dock_fixups(acpi_handle not_used, u32 event, void *data)
189 struct acpiphp_context *context = data;
190 struct pci_bus *bus = context->func.slot->bridge->pci_bus;
196 /* fixup bad _DCK function that rewrites
197 * secondary bridge on slot
199 pci_read_config_dword(bus->self,
203 if (((buses >> 8) & 0xff) != bus->busn_res.start) {
204 buses = (buses & 0xff000000)
205 | ((unsigned int)(bus->primary) << 0)
206 | ((unsigned int)(bus->busn_res.start) << 8)
207 | ((unsigned int)(bus->busn_res.end) << 16);
208 pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses);
213 static const struct acpi_dock_ops acpiphp_dock_ops = {
214 .fixup = post_dock_fixups,
215 .handler = hotplug_event,
218 /* Check whether the PCI device is managed by native PCIe hotplug driver */
219 static bool device_is_managed_by_native_pciehp(struct pci_dev *pdev)
223 struct acpi_pci_root *root;
225 /* Check whether the PCIe port supports native PCIe hotplug */
226 if (pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, ®32))
228 if (!(reg32 & PCI_EXP_SLTCAP_HPC))
232 * Check whether native PCIe hotplug has been enabled for
233 * this PCIe hierarchy.
235 tmp = acpi_find_root_bridge_handle(pdev);
238 root = acpi_pci_find_root(tmp);
241 if (!(root->osc_control_set & OSC_PCI_EXPRESS_NATIVE_HP_CONTROL))
247 static void acpiphp_dock_init(void *data)
249 struct acpiphp_context *context = data;
251 get_bridge(context->func.slot->bridge);
254 static void acpiphp_dock_release(void *data)
256 struct acpiphp_context *context = data;
258 put_bridge(context->func.slot->bridge);
261 /* callback routine to register each ACPI PCI slot object */
262 static acpi_status register_slot(acpi_handle handle, u32 lvl, void *data,
265 struct acpiphp_bridge *bridge = data;
266 struct acpiphp_context *context;
267 struct acpiphp_slot *slot;
268 struct acpiphp_func *newfunc;
269 acpi_status status = AE_OK;
270 unsigned long long adr;
271 int device, function;
272 struct pci_bus *pbus = bridge->pci_bus;
273 struct pci_dev *pdev = bridge->pci_dev;
276 if (pdev && device_is_managed_by_native_pciehp(pdev))
279 status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
280 if (ACPI_FAILURE(status)) {
281 acpi_handle_warn(handle, "can't evaluate _ADR (%#x)\n", status);
285 device = (adr >> 16) & 0xffff;
286 function = adr & 0xffff;
288 mutex_lock(&acpiphp_context_lock);
289 context = acpiphp_init_context(handle);
291 mutex_unlock(&acpiphp_context_lock);
292 acpi_handle_err(handle, "No hotplug context\n");
295 newfunc = &context->func;
296 newfunc->function = function;
297 mutex_unlock(&acpiphp_context_lock);
299 if (acpi_has_method(handle, "_EJ0"))
300 newfunc->flags = FUNC_HAS_EJ0;
302 if (acpi_has_method(handle, "_STA"))
303 newfunc->flags |= FUNC_HAS_STA;
305 if (acpi_has_method(handle, "_PS0"))
306 newfunc->flags |= FUNC_HAS_PS0;
308 if (acpi_has_method(handle, "_PS3"))
309 newfunc->flags |= FUNC_HAS_PS3;
311 if (acpi_has_method(handle, "_DCK"))
312 newfunc->flags |= FUNC_HAS_DCK;
314 /* search for objects that share the same slot */
315 list_for_each_entry(slot, &bridge->slots, node)
316 if (slot->device == device)
319 slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
321 status = AE_NO_MEMORY;
325 slot->bridge = bridge;
326 slot->device = device;
327 INIT_LIST_HEAD(&slot->funcs);
328 mutex_init(&slot->crit_sect);
330 mutex_lock(&bridge_mutex);
331 list_add_tail(&slot->node, &bridge->slots);
332 mutex_unlock(&bridge_mutex);
334 /* Register slots for ejectable funtions only. */
335 if (acpi_pci_check_ejectable(pbus, handle) || is_dock_device(handle)) {
336 unsigned long long sun;
340 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
341 if (ACPI_FAILURE(status))
342 sun = bridge->nr_slots;
344 dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
345 sun, pci_domain_nr(pbus), pbus->number, device);
347 retval = acpiphp_register_hotplug_slot(slot, sun);
350 if (retval == -EBUSY)
351 warn("Slot %llu already registered by another "
352 "hotplug driver\n", sun);
354 warn("acpiphp_register_hotplug_slot failed "
355 "(err code = 0x%x)\n", retval);
357 /* Even if the slot registration fails, we can still use it. */
361 newfunc->slot = slot;
362 mutex_lock(&bridge_mutex);
363 list_add_tail(&newfunc->sibling, &slot->funcs);
364 mutex_unlock(&bridge_mutex);
366 if (pci_bus_read_dev_vendor_id(pbus, PCI_DEVFN(device, function),
368 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
370 if (is_dock_device(handle)) {
371 /* we don't want to call this device's _EJ0
372 * because we want the dock notify handler
373 * to call it after it calls _DCK
375 newfunc->flags &= ~FUNC_HAS_EJ0;
376 if (register_hotplug_dock_device(handle,
377 &acpiphp_dock_ops, context,
378 acpiphp_dock_init, acpiphp_dock_release))
379 dbg("failed to register dock device\n");
382 /* install notify handler */
383 if (!(newfunc->flags & FUNC_HAS_DCK)) {
384 status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
385 handle_hotplug_event,
387 if (ACPI_FAILURE(status))
388 acpi_handle_err(handle,
389 "failed to install notify handler\n");
395 mutex_lock(&acpiphp_context_lock);
396 acpiphp_put_context(context);
397 mutex_unlock(&acpiphp_context_lock);
401 static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
403 struct acpiphp_context *context;
404 struct acpiphp_bridge *bridge = NULL;
406 mutex_lock(&acpiphp_context_lock);
407 context = acpiphp_get_context(handle);
409 bridge = context->bridge;
413 acpiphp_put_context(context);
415 mutex_unlock(&acpiphp_context_lock);
419 static void cleanup_bridge(struct acpiphp_bridge *bridge)
421 struct acpiphp_slot *slot;
422 struct acpiphp_func *func;
425 list_for_each_entry(slot, &bridge->slots, node) {
426 list_for_each_entry(func, &slot->funcs, sibling) {
427 acpi_handle handle = func_to_handle(func);
429 if (is_dock_device(handle))
430 unregister_hotplug_dock_device(handle);
432 if (!(func->flags & FUNC_HAS_DCK)) {
433 status = acpi_remove_notify_handler(handle,
435 handle_hotplug_event);
436 if (ACPI_FAILURE(status))
437 err("failed to remove notify handler\n");
440 acpiphp_unregister_hotplug_slot(slot);
443 mutex_lock(&bridge_mutex);
444 list_del(&bridge->list);
445 mutex_unlock(&bridge_mutex);
448 static int power_on_slot(struct acpiphp_slot *slot)
451 struct acpiphp_func *func;
454 /* if already enabled, just skip */
455 if (slot->flags & SLOT_POWEREDON)
458 list_for_each_entry(func, &slot->funcs, sibling) {
459 if (func->flags & FUNC_HAS_PS0) {
460 dbg("%s: executing _PS0\n", __func__);
461 status = acpi_evaluate_object(func_to_handle(func),
463 if (ACPI_FAILURE(status)) {
464 warn("%s: _PS0 failed\n", __func__);
472 /* TBD: evaluate _STA to check if the slot is enabled */
474 slot->flags |= SLOT_POWEREDON;
481 static int power_off_slot(struct acpiphp_slot *slot)
484 struct acpiphp_func *func;
488 /* if already disabled, just skip */
489 if ((slot->flags & SLOT_POWEREDON) == 0)
492 list_for_each_entry(func, &slot->funcs, sibling) {
493 if (func->flags & FUNC_HAS_PS3) {
494 status = acpi_evaluate_object(func_to_handle(func),
496 if (ACPI_FAILURE(status)) {
497 warn("%s: _PS3 failed\n", __func__);
505 /* TBD: evaluate _STA to check if the slot is disabled */
507 slot->flags &= (~SLOT_POWEREDON);
516 * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
517 * @bus: bus to start search with
519 static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
521 struct list_head *tmp;
522 unsigned char max, n;
525 * pci_bus_max_busnr will return the highest
526 * reserved busnr for all these children.
527 * that is equivalent to the bus->subordinate
528 * value. We don't want to use the parent's
529 * bus->subordinate value because it could have
532 max = bus->busn_res.start;
534 list_for_each(tmp, &bus->children) {
535 n = pci_bus_max_busnr(pci_bus_b(tmp));
544 * acpiphp_bus_add - add a new bus to acpi subsystem
545 * @func: acpiphp_func of the bridge
547 static int acpiphp_bus_add(struct acpiphp_func *func)
549 acpi_handle handle = func_to_handle(func);
550 struct acpi_device *device;
553 if (!acpi_bus_get_device(handle, &device)) {
554 dbg("bus exists... trim\n");
555 /* this shouldn't be in here, so remove
556 * the bus then re-add it...
558 acpi_bus_trim(device);
561 ret_val = acpi_bus_scan(handle);
563 ret_val = acpi_bus_get_device(handle, &device);
566 dbg("error adding bus, %x\n", -ret_val);
573 * acpiphp_bus_trim - trim a bus from acpi subsystem
574 * @handle: handle to acpi namespace
576 static int acpiphp_bus_trim(acpi_handle handle)
578 struct acpi_device *device;
581 retval = acpi_bus_get_device(handle, &device);
583 dbg("acpi_device not found\n");
587 acpi_bus_trim(device);
591 static void acpiphp_set_acpi_region(struct acpiphp_slot *slot)
593 struct acpiphp_func *func;
594 union acpi_object params[2];
595 struct acpi_object_list arg_list;
597 list_for_each_entry(func, &slot->funcs, sibling) {
599 arg_list.pointer = params;
600 params[0].type = ACPI_TYPE_INTEGER;
601 params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG;
602 params[1].type = ACPI_TYPE_INTEGER;
603 params[1].integer.value = 1;
604 /* _REG is optional, we don't care about if there is failure */
605 acpi_evaluate_object(func_to_handle(func), "_REG", &arg_list,
610 static void check_hotplug_bridge(struct acpiphp_slot *slot, struct pci_dev *dev)
612 struct acpiphp_func *func;
614 if (!dev->subordinate)
617 /* quirk, or pcie could set it already */
618 if (dev->is_hotplug_bridge)
621 if (PCI_SLOT(dev->devfn) != slot->device)
624 list_for_each_entry(func, &slot->funcs, sibling) {
625 if (PCI_FUNC(dev->devfn) == func->function) {
626 dev->is_hotplug_bridge = 1;
633 * enable_device - enable, configure a slot
634 * @slot: slot to be enabled
636 * This function should be called per *physical slot*,
637 * not per each slot object in ACPI namespace.
639 static int __ref enable_device(struct acpiphp_slot *slot)
642 struct pci_bus *bus = slot->bridge->pci_bus;
643 struct acpiphp_func *func;
647 if (slot->flags & SLOT_ENABLED)
650 list_for_each_entry(func, &slot->funcs, sibling)
651 acpiphp_bus_add(func);
653 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
655 /* Maybe only part of funcs are added. */
656 dbg("No new device found\n");
660 max = acpiphp_max_busnr(bus);
661 for (pass = 0; pass < 2; pass++) {
662 list_for_each_entry(dev, &bus->devices, bus_list) {
663 if (PCI_SLOT(dev->devfn) != slot->device)
665 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
666 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
667 max = pci_scan_bridge(bus, dev, max, pass);
668 if (pass && dev->subordinate) {
669 check_hotplug_bridge(slot, dev);
670 pcibios_resource_survey_bus(dev->subordinate);
671 __pci_bus_size_bridges(dev->subordinate,
678 __pci_bus_assign_resources(bus, &add_list, NULL);
679 acpiphp_sanitize_bus(bus);
680 acpiphp_set_hpp_values(bus);
681 acpiphp_set_acpi_region(slot);
682 pci_enable_bridges(bus);
684 list_for_each_entry(dev, &bus->devices, bus_list) {
685 /* Assume that newly added devices are powered on already. */
687 dev->current_state = PCI_D0;
690 pci_bus_add_devices(bus);
692 slot->flags |= SLOT_ENABLED;
693 list_for_each_entry(func, &slot->funcs, sibling) {
694 dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
697 /* Do not set SLOT_ENABLED flag if some funcs
699 slot->flags &= (~SLOT_ENABLED);
709 /* return first device in slot, acquiring a reference on it */
710 static struct pci_dev *dev_in_slot(struct acpiphp_slot *slot)
712 struct pci_bus *bus = slot->bridge->pci_bus;
714 struct pci_dev *ret = NULL;
716 down_read(&pci_bus_sem);
717 list_for_each_entry(dev, &bus->devices, bus_list)
718 if (PCI_SLOT(dev->devfn) == slot->device) {
719 ret = pci_dev_get(dev);
722 up_read(&pci_bus_sem);
728 * disable_device - disable a slot
729 * @slot: ACPI PHP slot
731 static int disable_device(struct acpiphp_slot *slot)
733 struct acpiphp_func *func;
734 struct pci_dev *pdev;
737 * enable_device() enumerates all functions in this device via
738 * pci_scan_slot(), whether they have associated ACPI hotplug
739 * methods (_EJ0, etc.) or not. Therefore, we remove all functions
742 while ((pdev = dev_in_slot(slot))) {
743 pci_stop_and_remove_bus_device(pdev);
747 list_for_each_entry(func, &slot->funcs, sibling)
748 acpiphp_bus_trim(func_to_handle(func));
750 slot->flags &= (~SLOT_ENABLED);
757 * get_slot_status - get ACPI slot status
758 * @slot: ACPI PHP slot
760 * If a slot has _STA for each function and if any one of them
761 * returned non-zero status, return it.
763 * If a slot doesn't have _STA and if any one of its functions'
764 * configuration space is configured, return 0x0f as a _STA.
766 * Otherwise return 0.
768 static unsigned int get_slot_status(struct acpiphp_slot *slot)
770 unsigned long long sta = 0;
771 struct acpiphp_func *func;
773 list_for_each_entry(func, &slot->funcs, sibling) {
774 if (func->flags & FUNC_HAS_STA) {
777 status = acpi_evaluate_integer(func_to_handle(func),
779 if (ACPI_SUCCESS(status) && sta)
784 pci_bus_read_config_dword(slot->bridge->pci_bus,
785 PCI_DEVFN(slot->device,
787 PCI_VENDOR_ID, &dvid);
788 if (dvid != 0xffffffff) {
795 return (unsigned int)sta;
799 * acpiphp_eject_slot - physically eject the slot
800 * @slot: ACPI PHP slot
802 int acpiphp_eject_slot(struct acpiphp_slot *slot)
804 struct acpiphp_func *func;
806 list_for_each_entry(func, &slot->funcs, sibling) {
807 /* We don't want to call _EJ0 on non-existing functions. */
808 if (!(func->flags & FUNC_HAS_EJ0))
811 if (ACPI_FAILURE(acpi_evaluate_ej0(func_to_handle(func))))
820 * acpiphp_check_bridge - re-enumerate devices
821 * @bridge: where to begin re-enumeration
823 * Iterate over all slots under this bridge and make sure that if a
824 * card is present they are enabled, and if not they are disabled.
826 static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
828 struct acpiphp_slot *slot;
830 int enabled, disabled;
832 enabled = disabled = 0;
834 list_for_each_entry(slot, &bridge->slots, node) {
835 unsigned int status = get_slot_status(slot);
836 if (slot->flags & SLOT_ENABLED) {
837 if (status == ACPI_STA_ALL)
839 retval = acpiphp_disable_slot(slot);
841 err("Error occurred in disabling\n");
844 acpiphp_eject_slot(slot);
848 if (status != ACPI_STA_ALL)
850 retval = acpiphp_enable_slot(slot);
852 err("Error occurred in enabling\n");
859 dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
865 static void acpiphp_set_hpp_values(struct pci_bus *bus)
869 list_for_each_entry(dev, &bus->devices, bus_list)
870 pci_configure_slot(dev);
874 * Remove devices for which we could not assign resources, call
875 * arch specific code to fix-up the bus
877 static void acpiphp_sanitize_bus(struct pci_bus *bus)
879 struct pci_dev *dev, *tmp;
881 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
883 list_for_each_entry_safe(dev, tmp, &bus->devices, bus_list) {
884 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
885 struct resource *res = &dev->resource[i];
886 if ((res->flags & type_mask) && !res->start &&
888 /* Could not assign a required resources
889 * for this device, remove it */
890 pci_stop_and_remove_bus_device(dev);
898 * ACPI event handlers
902 check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
904 struct acpiphp_bridge *bridge;
906 struct acpi_buffer buffer = { .length = sizeof(objname),
907 .pointer = objname };
909 bridge = acpiphp_handle_to_bridge(handle);
911 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
912 dbg("%s: re-enumerating slots under %s\n",
914 acpiphp_check_bridge(bridge);
920 void acpiphp_check_host_bridge(acpi_handle handle)
922 struct acpiphp_bridge *bridge;
924 bridge = acpiphp_handle_to_bridge(handle);
926 acpiphp_check_bridge(bridge);
930 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
931 ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
934 static void hotplug_event(acpi_handle handle, u32 type, void *data)
936 struct acpiphp_context *context = data;
937 struct acpiphp_func *func = &context->func;
938 struct acpiphp_bridge *bridge;
940 struct acpi_buffer buffer = { .length = sizeof(objname),
941 .pointer = objname };
943 mutex_lock(&acpiphp_context_lock);
944 bridge = context->bridge;
948 mutex_unlock(&acpiphp_context_lock);
950 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
953 case ACPI_NOTIFY_BUS_CHECK:
954 /* bus re-enumerate */
955 dbg("%s: Bus check notify on %s\n", __func__, objname);
956 dbg("%s: re-enumerating slots under %s\n", __func__, objname);
958 acpiphp_check_bridge(bridge);
959 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
960 ACPI_UINT32_MAX, check_sub_bridges,
963 acpiphp_enable_slot(func->slot);
967 case ACPI_NOTIFY_DEVICE_CHECK:
969 dbg("%s: Device check notify on %s\n", __func__, objname);
971 acpiphp_check_bridge(bridge);
973 acpiphp_check_bridge(func->slot->bridge);
977 case ACPI_NOTIFY_DEVICE_WAKE:
979 dbg("%s: Device wake notify on %s\n", __func__, objname);
982 case ACPI_NOTIFY_EJECT_REQUEST:
983 /* request device eject */
984 dbg("%s: Device eject notify on %s\n", __func__, objname);
985 if (!(acpiphp_disable_slot(func->slot)))
986 acpiphp_eject_slot(func->slot);
990 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
991 printk(KERN_ERR "Device %s cannot be configured due"
992 " to a frequency mismatch\n", objname);
995 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
996 printk(KERN_ERR "Device %s cannot be configured due"
997 " to a bus mode mismatch\n", objname);
1000 case ACPI_NOTIFY_POWER_FAULT:
1001 printk(KERN_ERR "Device %s has suffered a power fault\n",
1006 warn("notify_handler: unknown event type 0x%x for %s\n", type,
1015 static void hotplug_event_work(struct work_struct *work)
1017 struct acpiphp_context *context;
1018 struct acpi_hp_work *hp_work;
1020 hp_work = container_of(work, struct acpi_hp_work, work);
1021 context = hp_work->context;
1022 acpi_scan_lock_acquire();
1024 hotplug_event(hp_work->handle, hp_work->type, context);
1026 acpi_scan_lock_release();
1027 kfree(hp_work); /* allocated in handle_hotplug_event() */
1028 put_bridge(context->func.slot->bridge);
1032 * handle_hotplug_event - handle ACPI hotplug event
1033 * @handle: Notify()'ed acpi_handle
1034 * @type: Notify code
1035 * @data: pointer to acpiphp_context structure
1037 * Handles ACPI event notification on slots.
1039 static void handle_hotplug_event(acpi_handle handle, u32 type, void *data)
1041 struct acpiphp_context *context;
1043 mutex_lock(&acpiphp_context_lock);
1044 context = acpiphp_get_context(handle);
1046 get_bridge(context->func.slot->bridge);
1047 acpiphp_put_context(context);
1049 mutex_unlock(&acpiphp_context_lock);
1051 * Currently the code adds all hotplug events to the kacpid_wq
1052 * queue when it should add hotplug events to the kacpi_hotplug_wq.
1053 * The proper way to fix this is to reorganize the code so that
1054 * drivers (dock, etc.) do not call acpi_os_execute(), etc.
1055 * For now just re-add this work to the kacpi_hotplug_wq so we
1056 * don't deadlock on hotplug actions.
1059 alloc_acpi_hp_work(handle, type, context, hotplug_event_work);
1063 * Create hotplug slots for the PCI bus.
1064 * It should always return 0 to avoid skipping following notifiers.
1066 void acpiphp_enumerate_slots(struct pci_bus *bus)
1068 struct acpiphp_bridge *bridge;
1072 if (acpiphp_disabled)
1075 handle = ACPI_HANDLE(bus->bridge);
1079 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
1081 acpi_handle_err(handle, "No memory for bridge object\n");
1085 INIT_LIST_HEAD(&bridge->slots);
1086 kref_init(&bridge->ref);
1087 bridge->pci_dev = pci_dev_get(bus->self);
1088 bridge->pci_bus = bus;
1091 * Grab a ref to the subordinate PCI bus in case the bus is
1092 * removed via PCI core logical hotplug. The ref pins the bus
1093 * (which we access during module unload).
1095 get_device(&bus->dev);
1097 if (!pci_is_root_bus(bridge->pci_bus)) {
1098 struct acpiphp_context *context;
1101 * This bridge should have been registered as a hotplug function
1102 * under its parent, so the context has to be there. If not, we
1105 mutex_lock(&acpiphp_context_lock);
1106 context = acpiphp_get_context(handle);
1107 if (WARN_ON(!context)) {
1108 mutex_unlock(&acpiphp_context_lock);
1109 put_device(&bus->dev);
1113 bridge->context = context;
1114 context->bridge = bridge;
1115 /* Get a reference to the parent bridge. */
1116 get_bridge(context->func.slot->bridge);
1117 mutex_unlock(&acpiphp_context_lock);
1120 /* must be added to the list prior to calling register_slot */
1121 mutex_lock(&bridge_mutex);
1122 list_add(&bridge->list, &bridge_list);
1123 mutex_unlock(&bridge_mutex);
1125 /* register all slot objects under this bridge */
1126 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
1127 register_slot, NULL, bridge, NULL);
1128 if (ACPI_FAILURE(status)) {
1129 acpi_handle_err(handle, "failed to register slots\n");
1130 cleanup_bridge(bridge);
1135 /* Destroy hotplug slots associated with the PCI bus */
1136 void acpiphp_remove_slots(struct pci_bus *bus)
1138 struct acpiphp_bridge *bridge, *tmp;
1140 if (acpiphp_disabled)
1143 list_for_each_entry_safe(bridge, tmp, &bridge_list, list)
1144 if (bridge->pci_bus == bus) {
1145 cleanup_bridge(bridge);
1152 * acpiphp_enable_slot - power on slot
1153 * @slot: ACPI PHP slot
1155 int acpiphp_enable_slot(struct acpiphp_slot *slot)
1159 mutex_lock(&slot->crit_sect);
1161 /* wake up all functions */
1162 retval = power_on_slot(slot);
1166 if (get_slot_status(slot) == ACPI_STA_ALL) {
1167 /* configure all functions */
1168 retval = enable_device(slot);
1170 power_off_slot(slot);
1172 dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
1173 power_off_slot(slot);
1177 mutex_unlock(&slot->crit_sect);
1182 * acpiphp_disable_slot - power off slot
1183 * @slot: ACPI PHP slot
1185 int acpiphp_disable_slot(struct acpiphp_slot *slot)
1189 mutex_lock(&slot->crit_sect);
1191 /* unconfigure all functions */
1192 retval = disable_device(slot);
1196 /* power off all functions */
1197 retval = power_off_slot(slot);
1202 mutex_unlock(&slot->crit_sect);
1211 u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1213 return (slot->flags & SLOT_POWEREDON);
1221 u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1225 sta = get_slot_status(slot);
1227 return (sta & ACPI_STA_DEVICE_UI) ? 0 : 1;
1232 * adapter presence : 1
1235 u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1239 sta = get_slot_status(slot);
1241 return (sta == 0) ? 0 : 1;