ACPI / hotplug / PCI: Do not queue up event handling work items in vain
[linux-2.6.git] / drivers / pci / hotplug / acpiphp_glue.c
blob44191db1f050582117c7e6bd7dda6cfc472d27a4
1 /*
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
23 * details.
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
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.
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>
53 #include "../pci.h"
54 #include "acpiphp.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. */
73 /**
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;
82 acpi_status status;
84 context = kzalloc(sizeof(*context), GFP_KERNEL);
85 if (!context)
86 return NULL;
88 context->handle = handle;
89 context->refcount = 1;
90 status = acpi_attach_data(handle, acpiphp_context_handler, context);
91 if (ACPI_FAILURE(status)) {
92 kfree(context);
93 return NULL;
95 return context;
98 /**
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;
107 acpi_status status;
108 void *data;
110 status = acpi_get_data(handle, acpiphp_context_handler, &data);
111 if (ACPI_SUCCESS(status)) {
112 context = data;
113 context->refcount++;
115 return context;
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)
129 return;
131 WARN_ON(context->bridge);
132 acpi_detach_data(context->handle, acpiphp_context_handler);
133 kfree(context);
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));
161 kfree(slot);
164 context = bridge->context;
165 /* Root bridges will not have hotplug context. */
166 if (context) {
167 /* Release the reference taken by acpiphp_enumerate_slots(). */
168 put_bridge(context->func.parent);
169 context->bridge = NULL;
170 acpiphp_put_context(context);
173 put_device(&bridge->pci_bus->dev);
174 pci_dev_put(bridge->pci_dev);
175 kfree(bridge);
177 mutex_unlock(&acpiphp_context_lock);
181 * the _DCK method can do funny things... and sometimes not
182 * hah-hah funny.
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->bus;
191 u32 buses;
193 if (!bus->self)
194 return;
196 /* fixup bad _DCK function that rewrites
197 * secondary bridge on slot
199 pci_read_config_dword(bus->self,
200 PCI_PRIMARY_BUS,
201 &buses);
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)
221 u32 reg32;
222 acpi_handle tmp;
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, &reg32))
227 return false;
228 if (!(reg32 & PCI_EXP_SLTCAP_HPC))
229 return false;
232 * Check whether native PCIe hotplug has been enabled for
233 * this PCIe hierarchy.
235 tmp = acpi_find_root_bridge_handle(pdev);
236 if (!tmp)
237 return false;
238 root = acpi_pci_find_root(tmp);
239 if (!root)
240 return false;
241 if (!(root->osc_control_set & OSC_PCI_EXPRESS_NATIVE_HP_CONTROL))
242 return false;
244 return true;
247 static void acpiphp_dock_init(void *data)
249 struct acpiphp_context *context = data;
251 get_bridge(context->func.parent);
254 static void acpiphp_dock_release(void *data)
256 struct acpiphp_context *context = data;
258 put_bridge(context->func.parent);
261 /* callback routine to register each ACPI PCI slot object */
262 static acpi_status register_slot(acpi_handle handle, u32 lvl, void *data,
263 void **rv)
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;
274 u32 val;
276 if (pdev && device_is_managed_by_native_pciehp(pdev))
277 return AE_OK;
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);
282 return AE_OK;
285 device = (adr >> 16) & 0xffff;
286 function = adr & 0xffff;
288 mutex_lock(&acpiphp_context_lock);
289 context = acpiphp_init_context(handle);
290 if (!context) {
291 mutex_unlock(&acpiphp_context_lock);
292 acpi_handle_err(handle, "No hotplug context\n");
293 return AE_NOT_EXIST;
295 newfunc = &context->func;
296 newfunc->function = function;
297 newfunc->parent = bridge;
298 mutex_unlock(&acpiphp_context_lock);
300 if (acpi_has_method(handle, "_EJ0"))
301 newfunc->flags = FUNC_HAS_EJ0;
303 if (acpi_has_method(handle, "_STA"))
304 newfunc->flags |= FUNC_HAS_STA;
306 if (acpi_has_method(handle, "_PS0"))
307 newfunc->flags |= FUNC_HAS_PS0;
309 if (acpi_has_method(handle, "_PS3"))
310 newfunc->flags |= FUNC_HAS_PS3;
312 if (acpi_has_method(handle, "_DCK"))
313 newfunc->flags |= FUNC_HAS_DCK;
315 /* search for objects that share the same slot */
316 list_for_each_entry(slot, &bridge->slots, node)
317 if (slot->device == device)
318 goto slot_found;
320 slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
321 if (!slot) {
322 status = AE_NO_MEMORY;
323 goto err;
326 slot->bus = bridge->pci_bus;
327 slot->device = device;
328 INIT_LIST_HEAD(&slot->funcs);
329 mutex_init(&slot->crit_sect);
331 mutex_lock(&bridge_mutex);
332 list_add_tail(&slot->node, &bridge->slots);
333 mutex_unlock(&bridge_mutex);
335 /* Register slots for ejectable funtions only. */
336 if (acpi_pci_check_ejectable(pbus, handle) || is_dock_device(handle)) {
337 unsigned long long sun;
338 int retval;
340 bridge->nr_slots++;
341 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
342 if (ACPI_FAILURE(status))
343 sun = bridge->nr_slots;
345 dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
346 sun, pci_domain_nr(pbus), pbus->number, device);
348 retval = acpiphp_register_hotplug_slot(slot, sun);
349 if (retval) {
350 bridge->nr_slots--;
351 if (retval == -EBUSY)
352 warn("Slot %llu already registered by another "
353 "hotplug driver\n", sun);
354 else
355 warn("acpiphp_register_hotplug_slot failed "
356 "(err code = 0x%x)\n", retval);
358 /* Even if the slot registration fails, we can still use it. */
361 slot_found:
362 newfunc->slot = slot;
363 mutex_lock(&bridge_mutex);
364 list_add_tail(&newfunc->sibling, &slot->funcs);
365 mutex_unlock(&bridge_mutex);
367 if (pci_bus_read_dev_vendor_id(pbus, PCI_DEVFN(device, function),
368 &val, 60*1000))
369 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
371 if (is_dock_device(handle)) {
372 /* we don't want to call this device's _EJ0
373 * because we want the dock notify handler
374 * to call it after it calls _DCK
376 newfunc->flags &= ~FUNC_HAS_EJ0;
377 if (register_hotplug_dock_device(handle,
378 &acpiphp_dock_ops, context,
379 acpiphp_dock_init, acpiphp_dock_release))
380 dbg("failed to register dock device\n");
383 /* install notify handler */
384 if (!(newfunc->flags & FUNC_HAS_DCK)) {
385 status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
386 handle_hotplug_event,
387 context);
388 if (ACPI_FAILURE(status))
389 acpi_handle_err(handle,
390 "failed to install notify handler\n");
393 return AE_OK;
395 err:
396 mutex_lock(&acpiphp_context_lock);
397 acpiphp_put_context(context);
398 mutex_unlock(&acpiphp_context_lock);
399 return status;
402 static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
404 struct acpiphp_context *context;
405 struct acpiphp_bridge *bridge = NULL;
407 mutex_lock(&acpiphp_context_lock);
408 context = acpiphp_get_context(handle);
409 if (context) {
410 bridge = context->bridge;
411 if (bridge)
412 get_bridge(bridge);
414 acpiphp_put_context(context);
416 mutex_unlock(&acpiphp_context_lock);
417 return bridge;
420 static void cleanup_bridge(struct acpiphp_bridge *bridge)
422 struct acpiphp_slot *slot;
423 struct acpiphp_func *func;
424 acpi_status status;
426 list_for_each_entry(slot, &bridge->slots, node) {
427 list_for_each_entry(func, &slot->funcs, sibling) {
428 acpi_handle handle = func_to_handle(func);
430 if (is_dock_device(handle))
431 unregister_hotplug_dock_device(handle);
433 if (!(func->flags & FUNC_HAS_DCK)) {
434 status = acpi_remove_notify_handler(handle,
435 ACPI_SYSTEM_NOTIFY,
436 handle_hotplug_event);
437 if (ACPI_FAILURE(status))
438 err("failed to remove notify handler\n");
441 acpiphp_unregister_hotplug_slot(slot);
444 mutex_lock(&bridge_mutex);
445 list_del(&bridge->list);
446 mutex_unlock(&bridge_mutex);
449 static int power_on_slot(struct acpiphp_slot *slot)
451 acpi_status status;
452 struct acpiphp_func *func;
453 int retval = 0;
455 /* if already enabled, just skip */
456 if (slot->flags & SLOT_POWEREDON)
457 goto err_exit;
459 list_for_each_entry(func, &slot->funcs, sibling) {
460 if (func->flags & FUNC_HAS_PS0) {
461 dbg("%s: executing _PS0\n", __func__);
462 status = acpi_evaluate_object(func_to_handle(func),
463 "_PS0", NULL, NULL);
464 if (ACPI_FAILURE(status)) {
465 warn("%s: _PS0 failed\n", __func__);
466 retval = -1;
467 goto err_exit;
468 } else
469 break;
473 /* TBD: evaluate _STA to check if the slot is enabled */
475 slot->flags |= SLOT_POWEREDON;
477 err_exit:
478 return retval;
482 static int power_off_slot(struct acpiphp_slot *slot)
484 acpi_status status;
485 struct acpiphp_func *func;
487 int retval = 0;
489 /* if already disabled, just skip */
490 if ((slot->flags & SLOT_POWEREDON) == 0)
491 goto err_exit;
493 list_for_each_entry(func, &slot->funcs, sibling) {
494 if (func->flags & FUNC_HAS_PS3) {
495 status = acpi_evaluate_object(func_to_handle(func),
496 "_PS3", NULL, NULL);
497 if (ACPI_FAILURE(status)) {
498 warn("%s: _PS3 failed\n", __func__);
499 retval = -1;
500 goto err_exit;
501 } else
502 break;
506 /* TBD: evaluate _STA to check if the slot is disabled */
508 slot->flags &= (~SLOT_POWEREDON);
510 err_exit:
511 return retval;
517 * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
518 * @bus: bus to start search with
520 static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
522 struct list_head *tmp;
523 unsigned char max, n;
526 * pci_bus_max_busnr will return the highest
527 * reserved busnr for all these children.
528 * that is equivalent to the bus->subordinate
529 * value. We don't want to use the parent's
530 * bus->subordinate value because it could have
531 * padding in it.
533 max = bus->busn_res.start;
535 list_for_each(tmp, &bus->children) {
536 n = pci_bus_max_busnr(pci_bus_b(tmp));
537 if (n > max)
538 max = n;
540 return max;
544 * acpiphp_bus_trim - Trim device objects in an ACPI namespace subtree.
545 * @handle: ACPI device object handle to start from.
547 static void acpiphp_bus_trim(acpi_handle handle)
549 struct acpi_device *adev = NULL;
551 acpi_bus_get_device(handle, &adev);
552 if (adev)
553 acpi_bus_trim(adev);
557 * acpiphp_bus_add - Scan ACPI namespace subtree.
558 * @handle: ACPI object handle to start the scan from.
560 static void acpiphp_bus_add(acpi_handle handle)
562 acpiphp_bus_trim(handle);
563 acpi_bus_scan(handle);
566 static void acpiphp_set_acpi_region(struct acpiphp_slot *slot)
568 struct acpiphp_func *func;
569 union acpi_object params[2];
570 struct acpi_object_list arg_list;
572 list_for_each_entry(func, &slot->funcs, sibling) {
573 arg_list.count = 2;
574 arg_list.pointer = params;
575 params[0].type = ACPI_TYPE_INTEGER;
576 params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG;
577 params[1].type = ACPI_TYPE_INTEGER;
578 params[1].integer.value = 1;
579 /* _REG is optional, we don't care about if there is failure */
580 acpi_evaluate_object(func_to_handle(func), "_REG", &arg_list,
581 NULL);
585 static void check_hotplug_bridge(struct acpiphp_slot *slot, struct pci_dev *dev)
587 struct acpiphp_func *func;
589 /* quirk, or pcie could set it already */
590 if (dev->is_hotplug_bridge)
591 return;
593 list_for_each_entry(func, &slot->funcs, sibling) {
594 if (PCI_FUNC(dev->devfn) == func->function) {
595 dev->is_hotplug_bridge = 1;
596 break;
602 * enable_device - enable, configure a slot
603 * @slot: slot to be enabled
605 * This function should be called per *physical slot*,
606 * not per each slot object in ACPI namespace.
608 static int __ref enable_device(struct acpiphp_slot *slot)
610 struct pci_dev *dev;
611 struct pci_bus *bus = slot->bus;
612 struct acpiphp_func *func;
613 int num, max, pass;
614 LIST_HEAD(add_list);
616 if (slot->flags & SLOT_ENABLED)
617 goto err_exit;
619 list_for_each_entry(func, &slot->funcs, sibling)
620 acpiphp_bus_add(func_to_handle(func));
622 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
623 if (num == 0) {
624 /* Maybe only part of funcs are added. */
625 dbg("No new device found\n");
626 goto err_exit;
629 max = acpiphp_max_busnr(bus);
630 for (pass = 0; pass < 2; pass++) {
631 list_for_each_entry(dev, &bus->devices, bus_list) {
632 if (PCI_SLOT(dev->devfn) != slot->device)
633 continue;
634 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
635 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
636 max = pci_scan_bridge(bus, dev, max, pass);
637 if (pass && dev->subordinate) {
638 check_hotplug_bridge(slot, dev);
639 pcibios_resource_survey_bus(dev->subordinate);
640 __pci_bus_size_bridges(dev->subordinate,
641 &add_list);
647 __pci_bus_assign_resources(bus, &add_list, NULL);
648 acpiphp_sanitize_bus(bus);
649 acpiphp_set_hpp_values(bus);
650 acpiphp_set_acpi_region(slot);
651 pci_enable_bridges(bus);
653 list_for_each_entry(dev, &bus->devices, bus_list) {
654 /* Assume that newly added devices are powered on already. */
655 if (!dev->is_added)
656 dev->current_state = PCI_D0;
659 pci_bus_add_devices(bus);
661 slot->flags |= SLOT_ENABLED;
662 list_for_each_entry(func, &slot->funcs, sibling) {
663 dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
664 func->function));
665 if (!dev) {
666 /* Do not set SLOT_ENABLED flag if some funcs
667 are not added. */
668 slot->flags &= (~SLOT_ENABLED);
669 continue;
674 err_exit:
675 return 0;
678 /* return first device in slot, acquiring a reference on it */
679 static struct pci_dev *dev_in_slot(struct acpiphp_slot *slot)
681 struct pci_bus *bus = slot->bus;
682 struct pci_dev *dev;
683 struct pci_dev *ret = NULL;
685 down_read(&pci_bus_sem);
686 list_for_each_entry(dev, &bus->devices, bus_list)
687 if (PCI_SLOT(dev->devfn) == slot->device) {
688 ret = pci_dev_get(dev);
689 break;
691 up_read(&pci_bus_sem);
693 return ret;
697 * disable_device - disable a slot
698 * @slot: ACPI PHP slot
700 static int disable_device(struct acpiphp_slot *slot)
702 struct acpiphp_func *func;
703 struct pci_dev *pdev;
706 * enable_device() enumerates all functions in this device via
707 * pci_scan_slot(), whether they have associated ACPI hotplug
708 * methods (_EJ0, etc.) or not. Therefore, we remove all functions
709 * here.
711 while ((pdev = dev_in_slot(slot))) {
712 pci_stop_and_remove_bus_device(pdev);
713 pci_dev_put(pdev);
716 list_for_each_entry(func, &slot->funcs, sibling)
717 acpiphp_bus_trim(func_to_handle(func));
719 slot->flags &= (~SLOT_ENABLED);
721 return 0;
726 * get_slot_status - get ACPI slot status
727 * @slot: ACPI PHP slot
729 * If a slot has _STA for each function and if any one of them
730 * returned non-zero status, return it.
732 * If a slot doesn't have _STA and if any one of its functions'
733 * configuration space is configured, return 0x0f as a _STA.
735 * Otherwise return 0.
737 static unsigned int get_slot_status(struct acpiphp_slot *slot)
739 unsigned long long sta = 0;
740 struct acpiphp_func *func;
742 list_for_each_entry(func, &slot->funcs, sibling) {
743 if (func->flags & FUNC_HAS_STA) {
744 acpi_status status;
746 status = acpi_evaluate_integer(func_to_handle(func),
747 "_STA", NULL, &sta);
748 if (ACPI_SUCCESS(status) && sta)
749 break;
750 } else {
751 u32 dvid;
753 pci_bus_read_config_dword(slot->bus,
754 PCI_DEVFN(slot->device,
755 func->function),
756 PCI_VENDOR_ID, &dvid);
757 if (dvid != 0xffffffff) {
758 sta = ACPI_STA_ALL;
759 break;
764 return (unsigned int)sta;
768 * acpiphp_check_bridge - re-enumerate devices
769 * @bridge: where to begin re-enumeration
771 * Iterate over all slots under this bridge and make sure that if a
772 * card is present they are enabled, and if not they are disabled.
774 static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
776 struct acpiphp_slot *slot;
777 int retval = 0;
778 int enabled, disabled;
780 enabled = disabled = 0;
782 list_for_each_entry(slot, &bridge->slots, node) {
783 unsigned int status = get_slot_status(slot);
784 if (slot->flags & SLOT_ENABLED) {
785 if (status == ACPI_STA_ALL)
786 continue;
788 retval = acpiphp_disable_and_eject_slot(slot);
789 if (retval)
790 goto err_exit;
792 disabled++;
793 } else {
794 if (status != ACPI_STA_ALL)
795 continue;
796 retval = acpiphp_enable_slot(slot);
797 if (retval) {
798 err("Error occurred in enabling\n");
799 goto err_exit;
801 enabled++;
805 dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
807 err_exit:
808 return retval;
811 static void acpiphp_set_hpp_values(struct pci_bus *bus)
813 struct pci_dev *dev;
815 list_for_each_entry(dev, &bus->devices, bus_list)
816 pci_configure_slot(dev);
820 * Remove devices for which we could not assign resources, call
821 * arch specific code to fix-up the bus
823 static void acpiphp_sanitize_bus(struct pci_bus *bus)
825 struct pci_dev *dev, *tmp;
826 int i;
827 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
829 list_for_each_entry_safe(dev, tmp, &bus->devices, bus_list) {
830 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
831 struct resource *res = &dev->resource[i];
832 if ((res->flags & type_mask) && !res->start &&
833 res->end) {
834 /* Could not assign a required resources
835 * for this device, remove it */
836 pci_stop_and_remove_bus_device(dev);
837 break;
844 * ACPI event handlers
847 static acpi_status
848 check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
850 struct acpiphp_bridge *bridge;
851 char objname[64];
852 struct acpi_buffer buffer = { .length = sizeof(objname),
853 .pointer = objname };
855 bridge = acpiphp_handle_to_bridge(handle);
856 if (bridge) {
857 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
858 dbg("%s: re-enumerating slots under %s\n",
859 __func__, objname);
860 acpiphp_check_bridge(bridge);
861 put_bridge(bridge);
863 return AE_OK ;
866 void acpiphp_check_host_bridge(acpi_handle handle)
868 struct acpiphp_bridge *bridge;
870 bridge = acpiphp_handle_to_bridge(handle);
871 if (bridge) {
872 acpiphp_check_bridge(bridge);
873 put_bridge(bridge);
876 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
877 ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
880 static void hotplug_event(acpi_handle handle, u32 type, void *data)
882 struct acpiphp_context *context = data;
883 struct acpiphp_func *func = &context->func;
884 struct acpiphp_bridge *bridge;
885 char objname[64];
886 struct acpi_buffer buffer = { .length = sizeof(objname),
887 .pointer = objname };
889 mutex_lock(&acpiphp_context_lock);
890 bridge = context->bridge;
891 if (bridge)
892 get_bridge(bridge);
894 mutex_unlock(&acpiphp_context_lock);
896 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
898 switch (type) {
899 case ACPI_NOTIFY_BUS_CHECK:
900 /* bus re-enumerate */
901 dbg("%s: Bus check notify on %s\n", __func__, objname);
902 dbg("%s: re-enumerating slots under %s\n", __func__, objname);
903 if (bridge) {
904 acpiphp_check_bridge(bridge);
905 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
906 ACPI_UINT32_MAX, check_sub_bridges,
907 NULL, NULL, NULL);
908 } else {
909 acpiphp_enable_slot(func->slot);
911 break;
913 case ACPI_NOTIFY_DEVICE_CHECK:
914 /* device check */
915 dbg("%s: Device check notify on %s\n", __func__, objname);
916 if (bridge)
917 acpiphp_check_bridge(bridge);
918 else
919 acpiphp_check_bridge(func->parent);
921 break;
923 case ACPI_NOTIFY_EJECT_REQUEST:
924 /* request device eject */
925 dbg("%s: Device eject notify on %s\n", __func__, objname);
926 acpiphp_disable_and_eject_slot(func->slot);
927 break;
930 if (bridge)
931 put_bridge(bridge);
934 static void hotplug_event_work(struct work_struct *work)
936 struct acpiphp_context *context;
937 struct acpi_hp_work *hp_work;
939 hp_work = container_of(work, struct acpi_hp_work, work);
940 context = hp_work->context;
941 acpi_scan_lock_acquire();
943 hotplug_event(hp_work->handle, hp_work->type, context);
945 acpi_scan_lock_release();
946 kfree(hp_work); /* allocated in handle_hotplug_event() */
947 put_bridge(context->func.parent);
951 * handle_hotplug_event - handle ACPI hotplug event
952 * @handle: Notify()'ed acpi_handle
953 * @type: Notify code
954 * @data: pointer to acpiphp_context structure
956 * Handles ACPI event notification on slots.
958 static void handle_hotplug_event(acpi_handle handle, u32 type, void *data)
960 struct acpiphp_context *context;
962 switch (type) {
963 case ACPI_NOTIFY_BUS_CHECK:
964 case ACPI_NOTIFY_DEVICE_CHECK:
965 case ACPI_NOTIFY_EJECT_REQUEST:
966 break;
968 case ACPI_NOTIFY_DEVICE_WAKE:
969 return;
971 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
972 acpi_handle_err(handle, "Device cannot be configured due "
973 "to a frequency mismatch\n");
974 return;
976 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
977 acpi_handle_err(handle, "Device cannot be configured due "
978 "to a bus mode mismatch\n");
979 return;
981 case ACPI_NOTIFY_POWER_FAULT:
982 acpi_handle_err(handle, "Device has suffered a power fault\n");
983 return;
985 default:
986 acpi_handle_warn(handle, "Unsupported event type 0x%x\n", type);
987 return;
990 mutex_lock(&acpiphp_context_lock);
991 context = acpiphp_get_context(handle);
992 if (context) {
993 get_bridge(context->func.parent);
994 acpiphp_put_context(context);
995 alloc_acpi_hp_work(handle, type, context, hotplug_event_work);
997 mutex_unlock(&acpiphp_context_lock);
1001 * Create hotplug slots for the PCI bus.
1002 * It should always return 0 to avoid skipping following notifiers.
1004 void acpiphp_enumerate_slots(struct pci_bus *bus)
1006 struct acpiphp_bridge *bridge;
1007 acpi_handle handle;
1008 acpi_status status;
1010 if (acpiphp_disabled)
1011 return;
1013 handle = ACPI_HANDLE(bus->bridge);
1014 if (!handle)
1015 return;
1017 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
1018 if (!bridge) {
1019 acpi_handle_err(handle, "No memory for bridge object\n");
1020 return;
1023 INIT_LIST_HEAD(&bridge->slots);
1024 kref_init(&bridge->ref);
1025 bridge->pci_dev = pci_dev_get(bus->self);
1026 bridge->pci_bus = bus;
1029 * Grab a ref to the subordinate PCI bus in case the bus is
1030 * removed via PCI core logical hotplug. The ref pins the bus
1031 * (which we access during module unload).
1033 get_device(&bus->dev);
1035 if (!pci_is_root_bus(bridge->pci_bus)) {
1036 struct acpiphp_context *context;
1039 * This bridge should have been registered as a hotplug function
1040 * under its parent, so the context has to be there. If not, we
1041 * are in deep goo.
1043 mutex_lock(&acpiphp_context_lock);
1044 context = acpiphp_get_context(handle);
1045 if (WARN_ON(!context)) {
1046 mutex_unlock(&acpiphp_context_lock);
1047 put_device(&bus->dev);
1048 kfree(bridge);
1049 return;
1051 bridge->context = context;
1052 context->bridge = bridge;
1053 /* Get a reference to the parent bridge. */
1054 get_bridge(context->func.parent);
1055 mutex_unlock(&acpiphp_context_lock);
1058 /* must be added to the list prior to calling register_slot */
1059 mutex_lock(&bridge_mutex);
1060 list_add(&bridge->list, &bridge_list);
1061 mutex_unlock(&bridge_mutex);
1063 /* register all slot objects under this bridge */
1064 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
1065 register_slot, NULL, bridge, NULL);
1066 if (ACPI_FAILURE(status)) {
1067 acpi_handle_err(handle, "failed to register slots\n");
1068 cleanup_bridge(bridge);
1069 put_bridge(bridge);
1073 /* Destroy hotplug slots associated with the PCI bus */
1074 void acpiphp_remove_slots(struct pci_bus *bus)
1076 struct acpiphp_bridge *bridge, *tmp;
1078 if (acpiphp_disabled)
1079 return;
1081 list_for_each_entry_safe(bridge, tmp, &bridge_list, list)
1082 if (bridge->pci_bus == bus) {
1083 cleanup_bridge(bridge);
1084 put_bridge(bridge);
1085 break;
1090 * acpiphp_enable_slot - power on slot
1091 * @slot: ACPI PHP slot
1093 int acpiphp_enable_slot(struct acpiphp_slot *slot)
1095 int retval;
1097 mutex_lock(&slot->crit_sect);
1099 /* wake up all functions */
1100 retval = power_on_slot(slot);
1101 if (retval)
1102 goto err_exit;
1104 if (get_slot_status(slot) == ACPI_STA_ALL) {
1105 /* configure all functions */
1106 retval = enable_device(slot);
1107 if (retval)
1108 power_off_slot(slot);
1109 } else {
1110 dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
1111 power_off_slot(slot);
1114 err_exit:
1115 mutex_unlock(&slot->crit_sect);
1116 return retval;
1120 * acpiphp_disable_and_eject_slot - power off and eject slot
1121 * @slot: ACPI PHP slot
1123 int acpiphp_disable_and_eject_slot(struct acpiphp_slot *slot)
1125 struct acpiphp_func *func;
1126 int retval = 0;
1128 mutex_lock(&slot->crit_sect);
1130 /* unconfigure all functions */
1131 retval = disable_device(slot);
1132 if (retval)
1133 goto err_exit;
1135 /* power off all functions */
1136 retval = power_off_slot(slot);
1137 if (retval)
1138 goto err_exit;
1140 list_for_each_entry(func, &slot->funcs, sibling)
1141 if (func->flags & FUNC_HAS_EJ0) {
1142 acpi_handle handle = func_to_handle(func);
1144 if (ACPI_FAILURE(acpi_evaluate_ej0(handle)))
1145 acpi_handle_err(handle, "_EJ0 failed\n");
1147 break;
1150 err_exit:
1151 mutex_unlock(&slot->crit_sect);
1152 return retval;
1157 * slot enabled: 1
1158 * slot disabled: 0
1160 u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1162 return (slot->flags & SLOT_POWEREDON);
1167 * latch open: 1
1168 * latch closed: 0
1170 u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1172 unsigned int sta;
1174 sta = get_slot_status(slot);
1176 return (sta & ACPI_STA_DEVICE_UI) ? 0 : 1;
1181 * adapter presence : 1
1182 * absence : 0
1184 u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1186 unsigned int sta;
1188 sta = get_slot_status(slot);
1190 return (sta == 0) ? 0 : 1;