ACPI / hotplug / PCI: Consolidate acpiphp_enumerate_slots()
[linux-2.6.git] / drivers / pci / hotplug / acpiphp_glue.c
blob68c3809ed7ceaac4c8c3e070832f79653ba7039a
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);
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 */
69 static acpi_status
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)) {
75 (*count)++;
76 return AE_CTRL_TERMINATE;
77 } else {
78 return AE_OK;
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) {
102 kfree(func);
104 kfree(slot);
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);
112 kfree(bridge);
116 * the _DCK method can do funny things... and sometimes not
117 * hah-hah funny.
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;
126 u32 buses;
128 if (!bus->self)
129 return;
131 /* fixup bad _DCK function that rewrites
132 * secondary bridge on slot
134 pci_read_config_dword(bus->self,
135 PCI_PRIMARY_BUS,
136 &buses);
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)
156 u32 reg32;
157 acpi_handle tmp;
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, &reg32))
162 return false;
163 if (!(reg32 & PCI_EXP_SLTCAP_HPC))
164 return false;
167 * Check whether native PCIe hotplug has been enabled for
168 * this PCIe hierarchy.
170 tmp = acpi_find_root_bridge_handle(pdev);
171 if (!tmp)
172 return false;
173 root = acpi_pci_find_root(tmp);
174 if (!root)
175 return false;
176 if (!(root->osc_control_set & OSC_PCI_EXPRESS_NATIVE_HP_CONTROL))
177 return false;
179 return true;
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 */
197 static acpi_status
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;
208 u32 val;
210 if (!acpi_pci_check_ejectable(pbus, handle) && !is_dock_device(handle))
211 return AE_OK;
213 status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
214 if (ACPI_FAILURE(status)) {
215 warn("can't evaluate _ADR (%#x)\n", status);
216 return AE_OK;
219 device = (adr >> 16) & 0xffff;
220 function = adr & 0xffff;
222 pdev = bridge->pci_dev;
223 if (pdev && device_is_managed_by_native_pciehp(pdev))
224 return AE_OK;
226 newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
227 if (!newfunc)
228 return AE_NO_MEMORY;
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");
262 found = 1;
263 break;
266 if (!found) {
267 slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
268 if (!slot) {
269 kfree(newfunc);
270 return AE_NO_MEMORY;
273 slot->bridge = bridge;
274 slot->device = device;
275 slot->sun = sun;
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);
282 bridge->nr_slots++;
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);
287 if (retval) {
288 if (retval == -EBUSY)
289 warn("Slot %llu already registered by another "
290 "hotplug driver\n", slot->sun);
291 else
292 warn("acpiphp_register_hotplug_slot failed "
293 "(err code = 0x%x)\n", retval);
294 goto err_exit;
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),
304 &val, 60*1000))
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,
322 ACPI_SYSTEM_NOTIFY,
323 handle_hotplug_event_func,
324 newfunc);
326 if (ACPI_FAILURE(status))
327 err("failed to register interrupt notify handler\n");
328 } else
329 status = AE_OK;
331 return status;
333 err_exit:
334 bridge->nr_slots--;
335 mutex_lock(&bridge_mutex);
336 list_del(&slot->node);
337 mutex_unlock(&bridge_mutex);
338 kfree(slot);
339 kfree(newfunc);
341 return AE_OK;
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);
349 if (!found) {
350 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
351 is_pci_dock_device, NULL, (void *)&found, NULL);
353 return found;
357 /* find acpiphp_func from acpiphp_bridge */
358 static struct acpiphp_func *acpiphp_bridge_handle_to_function(acpi_handle handle)
360 struct acpiphp_bridge *bridge;
361 struct acpiphp_slot *slot;
362 struct acpiphp_func *func = NULL;
364 mutex_lock(&bridge_mutex);
365 list_for_each_entry(bridge, &bridge_list, list) {
366 list_for_each_entry(slot, &bridge->slots, node) {
367 list_for_each_entry(func, &slot->funcs, sibling) {
368 if (func->handle == handle) {
369 get_bridge(func->slot->bridge);
370 mutex_unlock(&bridge_mutex);
371 return func;
376 mutex_unlock(&bridge_mutex);
378 return NULL;
382 static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
384 struct acpiphp_bridge *bridge;
386 mutex_lock(&bridge_mutex);
387 list_for_each_entry(bridge, &bridge_list, list)
388 if (bridge->handle == handle) {
389 get_bridge(bridge);
390 mutex_unlock(&bridge_mutex);
391 return bridge;
393 mutex_unlock(&bridge_mutex);
395 return NULL;
398 static void cleanup_bridge(struct acpiphp_bridge *bridge)
400 struct acpiphp_slot *slot;
401 struct acpiphp_func *func;
402 acpi_status status;
403 acpi_handle handle = bridge->handle;
405 if (!pci_is_root_bus(bridge->pci_bus)) {
406 status = acpi_remove_notify_handler(handle,
407 ACPI_SYSTEM_NOTIFY,
408 handle_hotplug_event_bridge);
409 if (ACPI_FAILURE(status))
410 err("failed to remove notify handler\n");
413 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
414 status = acpi_install_notify_handler(bridge->func->handle,
415 ACPI_SYSTEM_NOTIFY,
416 handle_hotplug_event_func,
417 bridge->func);
418 if (ACPI_FAILURE(status))
419 err("failed to install interrupt notify handler\n");
422 list_for_each_entry(slot, &bridge->slots, node) {
423 list_for_each_entry(func, &slot->funcs, sibling) {
424 if (is_dock_device(func->handle)) {
425 unregister_hotplug_dock_device(func->handle);
427 if (!(func->flags & FUNC_HAS_DCK)) {
428 status = acpi_remove_notify_handler(func->handle,
429 ACPI_SYSTEM_NOTIFY,
430 handle_hotplug_event_func);
431 if (ACPI_FAILURE(status))
432 err("failed to remove notify handler\n");
435 acpiphp_unregister_hotplug_slot(slot);
438 mutex_lock(&bridge_mutex);
439 list_del(&bridge->list);
440 mutex_unlock(&bridge_mutex);
443 static int power_on_slot(struct acpiphp_slot *slot)
445 acpi_status status;
446 struct acpiphp_func *func;
447 int retval = 0;
449 /* if already enabled, just skip */
450 if (slot->flags & SLOT_POWEREDON)
451 goto err_exit;
453 list_for_each_entry(func, &slot->funcs, sibling) {
454 if (func->flags & FUNC_HAS_PS0) {
455 dbg("%s: executing _PS0\n", __func__);
456 status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
457 if (ACPI_FAILURE(status)) {
458 warn("%s: _PS0 failed\n", __func__);
459 retval = -1;
460 goto err_exit;
461 } else
462 break;
466 /* TBD: evaluate _STA to check if the slot is enabled */
468 slot->flags |= SLOT_POWEREDON;
470 err_exit:
471 return retval;
475 static int power_off_slot(struct acpiphp_slot *slot)
477 acpi_status status;
478 struct acpiphp_func *func;
480 int retval = 0;
482 /* if already disabled, just skip */
483 if ((slot->flags & SLOT_POWEREDON) == 0)
484 goto err_exit;
486 list_for_each_entry(func, &slot->funcs, sibling) {
487 if (func->flags & FUNC_HAS_PS3) {
488 status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
489 if (ACPI_FAILURE(status)) {
490 warn("%s: _PS3 failed\n", __func__);
491 retval = -1;
492 goto err_exit;
493 } else
494 break;
498 /* TBD: evaluate _STA to check if the slot is disabled */
500 slot->flags &= (~SLOT_POWEREDON);
502 err_exit:
503 return retval;
509 * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
510 * @bus: bus to start search with
512 static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
514 struct list_head *tmp;
515 unsigned char max, n;
518 * pci_bus_max_busnr will return the highest
519 * reserved busnr for all these children.
520 * that is equivalent to the bus->subordinate
521 * value. We don't want to use the parent's
522 * bus->subordinate value because it could have
523 * padding in it.
525 max = bus->busn_res.start;
527 list_for_each(tmp, &bus->children) {
528 n = pci_bus_max_busnr(pci_bus_b(tmp));
529 if (n > max)
530 max = n;
532 return max;
537 * acpiphp_bus_add - add a new bus to acpi subsystem
538 * @func: acpiphp_func of the bridge
540 static int acpiphp_bus_add(struct acpiphp_func *func)
542 struct acpi_device *device;
543 int ret_val;
545 if (!acpi_bus_get_device(func->handle, &device)) {
546 dbg("bus exists... trim\n");
547 /* this shouldn't be in here, so remove
548 * the bus then re-add it...
550 acpi_bus_trim(device);
553 ret_val = acpi_bus_scan(func->handle);
554 if (!ret_val)
555 ret_val = acpi_bus_get_device(func->handle, &device);
557 if (ret_val)
558 dbg("error adding bus, %x\n", -ret_val);
560 return ret_val;
565 * acpiphp_bus_trim - trim a bus from acpi subsystem
566 * @handle: handle to acpi namespace
568 static int acpiphp_bus_trim(acpi_handle handle)
570 struct acpi_device *device;
571 int retval;
573 retval = acpi_bus_get_device(handle, &device);
574 if (retval) {
575 dbg("acpi_device not found\n");
576 return retval;
579 acpi_bus_trim(device);
580 return 0;
583 static void acpiphp_set_acpi_region(struct acpiphp_slot *slot)
585 struct acpiphp_func *func;
586 union acpi_object params[2];
587 struct acpi_object_list arg_list;
589 list_for_each_entry(func, &slot->funcs, sibling) {
590 arg_list.count = 2;
591 arg_list.pointer = params;
592 params[0].type = ACPI_TYPE_INTEGER;
593 params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG;
594 params[1].type = ACPI_TYPE_INTEGER;
595 params[1].integer.value = 1;
596 /* _REG is optional, we don't care about if there is failure */
597 acpi_evaluate_object(func->handle, "_REG", &arg_list, NULL);
601 static void check_hotplug_bridge(struct acpiphp_slot *slot, struct pci_dev *dev)
603 struct acpiphp_func *func;
605 if (!dev->subordinate)
606 return;
608 /* quirk, or pcie could set it already */
609 if (dev->is_hotplug_bridge)
610 return;
612 if (PCI_SLOT(dev->devfn) != slot->device)
613 return;
615 list_for_each_entry(func, &slot->funcs, sibling) {
616 if (PCI_FUNC(dev->devfn) == func->function) {
617 /* check if this bridge has ejectable slots */
618 if ((detect_ejectable_slots(func->handle) > 0))
619 dev->is_hotplug_bridge = 1;
620 break;
626 * enable_device - enable, configure a slot
627 * @slot: slot to be enabled
629 * This function should be called per *physical slot*,
630 * not per each slot object in ACPI namespace.
632 static int __ref enable_device(struct acpiphp_slot *slot)
634 struct pci_dev *dev;
635 struct pci_bus *bus = slot->bridge->pci_bus;
636 struct acpiphp_func *func;
637 int num, max, pass;
638 LIST_HEAD(add_list);
640 if (slot->flags & SLOT_ENABLED)
641 goto err_exit;
643 list_for_each_entry(func, &slot->funcs, sibling)
644 acpiphp_bus_add(func);
646 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
647 if (num == 0) {
648 /* Maybe only part of funcs are added. */
649 dbg("No new device found\n");
650 goto err_exit;
653 max = acpiphp_max_busnr(bus);
654 for (pass = 0; pass < 2; pass++) {
655 list_for_each_entry(dev, &bus->devices, bus_list) {
656 if (PCI_SLOT(dev->devfn) != slot->device)
657 continue;
658 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
659 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
660 max = pci_scan_bridge(bus, dev, max, pass);
661 if (pass && dev->subordinate) {
662 check_hotplug_bridge(slot, dev);
663 pcibios_resource_survey_bus(dev->subordinate);
664 __pci_bus_size_bridges(dev->subordinate,
665 &add_list);
671 __pci_bus_assign_resources(bus, &add_list, NULL);
672 acpiphp_sanitize_bus(bus);
673 acpiphp_set_hpp_values(bus);
674 acpiphp_set_acpi_region(slot);
675 pci_enable_bridges(bus);
677 list_for_each_entry(dev, &bus->devices, bus_list) {
678 /* Assume that newly added devices are powered on already. */
679 if (!dev->is_added)
680 dev->current_state = PCI_D0;
683 pci_bus_add_devices(bus);
685 slot->flags |= SLOT_ENABLED;
686 list_for_each_entry(func, &slot->funcs, sibling) {
687 dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
688 func->function));
689 if (!dev) {
690 /* Do not set SLOT_ENABLED flag if some funcs
691 are not added. */
692 slot->flags &= (~SLOT_ENABLED);
693 continue;
698 err_exit:
699 return 0;
702 /* return first device in slot, acquiring a reference on it */
703 static struct pci_dev *dev_in_slot(struct acpiphp_slot *slot)
705 struct pci_bus *bus = slot->bridge->pci_bus;
706 struct pci_dev *dev;
707 struct pci_dev *ret = NULL;
709 down_read(&pci_bus_sem);
710 list_for_each_entry(dev, &bus->devices, bus_list)
711 if (PCI_SLOT(dev->devfn) == slot->device) {
712 ret = pci_dev_get(dev);
713 break;
715 up_read(&pci_bus_sem);
717 return ret;
721 * disable_device - disable a slot
722 * @slot: ACPI PHP slot
724 static int disable_device(struct acpiphp_slot *slot)
726 struct acpiphp_func *func;
727 struct pci_dev *pdev;
730 * enable_device() enumerates all functions in this device via
731 * pci_scan_slot(), whether they have associated ACPI hotplug
732 * methods (_EJ0, etc.) or not. Therefore, we remove all functions
733 * here.
735 while ((pdev = dev_in_slot(slot))) {
736 pci_stop_and_remove_bus_device(pdev);
737 pci_dev_put(pdev);
740 list_for_each_entry(func, &slot->funcs, sibling) {
741 acpiphp_bus_trim(func->handle);
744 slot->flags &= (~SLOT_ENABLED);
746 return 0;
751 * get_slot_status - get ACPI slot status
752 * @slot: ACPI PHP slot
754 * If a slot has _STA for each function and if any one of them
755 * returned non-zero status, return it.
757 * If a slot doesn't have _STA and if any one of its functions'
758 * configuration space is configured, return 0x0f as a _STA.
760 * Otherwise return 0.
762 static unsigned int get_slot_status(struct acpiphp_slot *slot)
764 acpi_status status;
765 unsigned long long sta = 0;
766 u32 dvid;
767 struct acpiphp_func *func;
769 list_for_each_entry(func, &slot->funcs, sibling) {
770 if (func->flags & FUNC_HAS_STA) {
771 status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
772 if (ACPI_SUCCESS(status) && sta)
773 break;
774 } else {
775 pci_bus_read_config_dword(slot->bridge->pci_bus,
776 PCI_DEVFN(slot->device,
777 func->function),
778 PCI_VENDOR_ID, &dvid);
779 if (dvid != 0xffffffff) {
780 sta = ACPI_STA_ALL;
781 break;
786 return (unsigned int)sta;
790 * acpiphp_eject_slot - physically eject the slot
791 * @slot: ACPI PHP slot
793 int acpiphp_eject_slot(struct acpiphp_slot *slot)
795 struct acpiphp_func *func;
797 list_for_each_entry(func, &slot->funcs, sibling) {
798 /* We don't want to call _EJ0 on non-existing functions. */
799 if ((func->flags & FUNC_HAS_EJ0)) {
800 if (ACPI_FAILURE(acpi_evaluate_ej0(func->handle)))
801 return -1;
802 else
803 break;
806 return 0;
810 * acpiphp_check_bridge - re-enumerate devices
811 * @bridge: where to begin re-enumeration
813 * Iterate over all slots under this bridge and make sure that if a
814 * card is present they are enabled, and if not they are disabled.
816 static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
818 struct acpiphp_slot *slot;
819 int retval = 0;
820 int enabled, disabled;
822 enabled = disabled = 0;
824 list_for_each_entry(slot, &bridge->slots, node) {
825 unsigned int status = get_slot_status(slot);
826 if (slot->flags & SLOT_ENABLED) {
827 if (status == ACPI_STA_ALL)
828 continue;
829 retval = acpiphp_disable_slot(slot);
830 if (retval) {
831 err("Error occurred in disabling\n");
832 goto err_exit;
833 } else {
834 acpiphp_eject_slot(slot);
836 disabled++;
837 } else {
838 if (status != ACPI_STA_ALL)
839 continue;
840 retval = acpiphp_enable_slot(slot);
841 if (retval) {
842 err("Error occurred in enabling\n");
843 goto err_exit;
845 enabled++;
849 dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
851 err_exit:
852 return retval;
855 static void acpiphp_set_hpp_values(struct pci_bus *bus)
857 struct pci_dev *dev;
859 list_for_each_entry(dev, &bus->devices, bus_list)
860 pci_configure_slot(dev);
864 * Remove devices for which we could not assign resources, call
865 * arch specific code to fix-up the bus
867 static void acpiphp_sanitize_bus(struct pci_bus *bus)
869 struct pci_dev *dev, *tmp;
870 int i;
871 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
873 list_for_each_entry_safe(dev, tmp, &bus->devices, bus_list) {
874 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
875 struct resource *res = &dev->resource[i];
876 if ((res->flags & type_mask) && !res->start &&
877 res->end) {
878 /* Could not assign a required resources
879 * for this device, remove it */
880 pci_stop_and_remove_bus_device(dev);
881 break;
888 * ACPI event handlers
891 static acpi_status
892 check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
894 struct acpiphp_bridge *bridge;
895 char objname[64];
896 struct acpi_buffer buffer = { .length = sizeof(objname),
897 .pointer = objname };
899 bridge = acpiphp_handle_to_bridge(handle);
900 if (bridge) {
901 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
902 dbg("%s: re-enumerating slots under %s\n",
903 __func__, objname);
904 acpiphp_check_bridge(bridge);
905 put_bridge(bridge);
907 return AE_OK ;
910 void acpiphp_check_host_bridge(acpi_handle handle)
912 struct acpiphp_bridge *bridge;
914 bridge = acpiphp_handle_to_bridge(handle);
915 if (bridge) {
916 acpiphp_check_bridge(bridge);
917 put_bridge(bridge);
920 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
921 ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
924 static void _handle_hotplug_event_bridge(struct work_struct *work)
926 struct acpiphp_bridge *bridge;
927 char objname[64];
928 struct acpi_buffer buffer = { .length = sizeof(objname),
929 .pointer = objname };
930 struct acpi_hp_work *hp_work;
931 acpi_handle handle;
932 u32 type;
934 hp_work = container_of(work, struct acpi_hp_work, work);
935 handle = hp_work->handle;
936 type = hp_work->type;
937 bridge = (struct acpiphp_bridge *)hp_work->context;
939 acpi_scan_lock_acquire();
941 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
943 switch (type) {
944 case ACPI_NOTIFY_BUS_CHECK:
945 /* bus re-enumerate */
946 dbg("%s: Bus check notify on %s\n", __func__, objname);
947 dbg("%s: re-enumerating slots under %s\n", __func__, objname);
948 acpiphp_check_bridge(bridge);
949 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
950 ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
951 break;
953 case ACPI_NOTIFY_DEVICE_CHECK:
954 /* device check */
955 dbg("%s: Device check notify on %s\n", __func__, objname);
956 acpiphp_check_bridge(bridge);
957 break;
959 case ACPI_NOTIFY_DEVICE_WAKE:
960 /* wake event */
961 dbg("%s: Device wake notify on %s\n", __func__, objname);
962 break;
964 case ACPI_NOTIFY_EJECT_REQUEST:
965 /* request device eject */
966 dbg("%s: Device eject notify on %s\n", __func__, objname);
967 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
968 struct acpiphp_slot *slot;
969 slot = bridge->func->slot;
970 if (!acpiphp_disable_slot(slot))
971 acpiphp_eject_slot(slot);
973 break;
975 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
976 printk(KERN_ERR "Device %s cannot be configured due"
977 " to a frequency mismatch\n", objname);
978 break;
980 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
981 printk(KERN_ERR "Device %s cannot be configured due"
982 " to a bus mode mismatch\n", objname);
983 break;
985 case ACPI_NOTIFY_POWER_FAULT:
986 printk(KERN_ERR "Device %s has suffered a power fault\n",
987 objname);
988 break;
990 default:
991 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
992 break;
995 acpi_scan_lock_release();
996 kfree(hp_work); /* allocated in handle_hotplug_event_bridge */
997 put_bridge(bridge);
1001 * handle_hotplug_event_bridge - handle ACPI event on bridges
1002 * @handle: Notify()'ed acpi_handle
1003 * @type: Notify code
1004 * @context: pointer to acpiphp_bridge structure
1006 * Handles ACPI event notification on {host,p2p} bridges.
1008 static void handle_hotplug_event_bridge(acpi_handle handle, u32 type,
1009 void *context)
1011 struct acpiphp_bridge *bridge = context;
1014 * Currently the code adds all hotplug events to the kacpid_wq
1015 * queue when it should add hotplug events to the kacpi_hotplug_wq.
1016 * The proper way to fix this is to reorganize the code so that
1017 * drivers (dock, etc.) do not call acpi_os_execute(), etc.
1018 * For now just re-add this work to the kacpi_hotplug_wq so we
1019 * don't deadlock on hotplug actions.
1021 get_bridge(bridge);
1022 alloc_acpi_hp_work(handle, type, context, _handle_hotplug_event_bridge);
1025 static void hotplug_event_func(acpi_handle handle, u32 type, void *context)
1027 struct acpiphp_func *func = context;
1028 char objname[64];
1029 struct acpi_buffer buffer = { .length = sizeof(objname),
1030 .pointer = objname };
1032 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1034 switch (type) {
1035 case ACPI_NOTIFY_BUS_CHECK:
1036 /* bus re-enumerate */
1037 dbg("%s: Bus check notify on %s\n", __func__, objname);
1038 acpiphp_enable_slot(func->slot);
1039 break;
1041 case ACPI_NOTIFY_DEVICE_CHECK:
1042 /* device check : re-enumerate from parent bus */
1043 dbg("%s: Device check notify on %s\n", __func__, objname);
1044 acpiphp_check_bridge(func->slot->bridge);
1045 break;
1047 case ACPI_NOTIFY_DEVICE_WAKE:
1048 /* wake event */
1049 dbg("%s: Device wake notify on %s\n", __func__, objname);
1050 break;
1052 case ACPI_NOTIFY_EJECT_REQUEST:
1053 /* request device eject */
1054 dbg("%s: Device eject notify on %s\n", __func__, objname);
1055 if (!(acpiphp_disable_slot(func->slot)))
1056 acpiphp_eject_slot(func->slot);
1057 break;
1059 default:
1060 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1061 break;
1065 static void _handle_hotplug_event_func(struct work_struct *work)
1067 struct acpi_hp_work *hp_work;
1068 struct acpiphp_func *func;
1070 hp_work = container_of(work, struct acpi_hp_work, work);
1071 func = hp_work->context;
1072 acpi_scan_lock_acquire();
1074 hotplug_event_func(hp_work->handle, hp_work->type, func);
1076 acpi_scan_lock_release();
1077 kfree(hp_work); /* allocated in handle_hotplug_event_func */
1078 put_bridge(func->slot->bridge);
1082 * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
1083 * @handle: Notify()'ed acpi_handle
1084 * @type: Notify code
1085 * @context: pointer to acpiphp_func structure
1087 * Handles ACPI event notification on slots.
1089 static void handle_hotplug_event_func(acpi_handle handle, u32 type,
1090 void *context)
1092 struct acpiphp_func *func = context;
1095 * Currently the code adds all hotplug events to the kacpid_wq
1096 * queue when it should add hotplug events to the kacpi_hotplug_wq.
1097 * The proper way to fix this is to reorganize the code so that
1098 * drivers (dock, etc.) do not call acpi_os_execute(), etc.
1099 * For now just re-add this work to the kacpi_hotplug_wq so we
1100 * don't deadlock on hotplug actions.
1102 get_bridge(func->slot->bridge);
1103 alloc_acpi_hp_work(handle, type, context, _handle_hotplug_event_func);
1107 * Create hotplug slots for the PCI bus.
1108 * It should always return 0 to avoid skipping following notifiers.
1110 void acpiphp_enumerate_slots(struct pci_bus *bus)
1112 struct acpiphp_bridge *bridge;
1113 acpi_handle handle;
1114 acpi_status status;
1116 if (acpiphp_disabled)
1117 return;
1119 handle = ACPI_HANDLE(bus->bridge);
1120 if (!handle || detect_ejectable_slots(handle) <= 0)
1121 return;
1123 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
1124 if (bridge == NULL) {
1125 err("out of memory\n");
1126 return;
1129 INIT_LIST_HEAD(&bridge->slots);
1130 kref_init(&bridge->ref);
1131 bridge->handle = handle;
1132 bridge->pci_dev = pci_dev_get(bus->self);
1133 bridge->pci_bus = bus;
1136 * Grab a ref to the subordinate PCI bus in case the bus is
1137 * removed via PCI core logical hotplug. The ref pins the bus
1138 * (which we access during module unload).
1140 get_device(&bus->dev);
1142 /* must be added to the list prior to calling register_slot */
1143 mutex_lock(&bridge_mutex);
1144 list_add(&bridge->list, &bridge_list);
1145 mutex_unlock(&bridge_mutex);
1147 /* register all slot objects under this bridge */
1148 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, 1,
1149 register_slot, NULL, bridge, NULL);
1150 if (ACPI_FAILURE(status)) {
1151 acpi_handle_err(bridge->handle, "failed to register slots\n");
1152 goto err;
1155 if (pci_is_root_bus(bridge->pci_bus))
1156 return;
1158 /* install notify handler for P2P bridges */
1159 status = acpi_install_notify_handler(bridge->handle, ACPI_SYSTEM_NOTIFY,
1160 handle_hotplug_event_bridge,
1161 bridge);
1162 if (ACPI_FAILURE(status)) {
1163 acpi_handle_err(bridge->handle,
1164 "failed to register notify handler\n");
1165 goto err;
1168 if (!acpi_has_method(bridge->handle, "_EJ0"))
1169 return;
1171 dbg("found ejectable p2p bridge\n");
1172 bridge->flags |= BRIDGE_HAS_EJ0;
1173 bridge->func = acpiphp_bridge_handle_to_function(bridge->handle);
1174 if (bridge->func) {
1175 status = acpi_remove_notify_handler(bridge->func->handle,
1176 ACPI_SYSTEM_NOTIFY,
1177 handle_hotplug_event_func);
1178 if (ACPI_FAILURE(status))
1179 acpi_handle_err(bridge->func->handle,
1180 "failed to remove notify handler\n");
1182 return;
1184 err:
1185 cleanup_bridge(bridge);
1186 put_bridge(bridge);
1189 /* Destroy hotplug slots associated with the PCI bus */
1190 void acpiphp_remove_slots(struct pci_bus *bus)
1192 struct acpiphp_bridge *bridge, *tmp;
1194 if (acpiphp_disabled)
1195 return;
1197 list_for_each_entry_safe(bridge, tmp, &bridge_list, list)
1198 if (bridge->pci_bus == bus) {
1199 cleanup_bridge(bridge);
1200 put_bridge(bridge);
1201 break;
1206 * acpiphp_enable_slot - power on slot
1207 * @slot: ACPI PHP slot
1209 int acpiphp_enable_slot(struct acpiphp_slot *slot)
1211 int retval;
1213 mutex_lock(&slot->crit_sect);
1215 /* wake up all functions */
1216 retval = power_on_slot(slot);
1217 if (retval)
1218 goto err_exit;
1220 if (get_slot_status(slot) == ACPI_STA_ALL) {
1221 /* configure all functions */
1222 retval = enable_device(slot);
1223 if (retval)
1224 power_off_slot(slot);
1225 } else {
1226 dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
1227 power_off_slot(slot);
1230 err_exit:
1231 mutex_unlock(&slot->crit_sect);
1232 return retval;
1236 * acpiphp_disable_slot - power off slot
1237 * @slot: ACPI PHP slot
1239 int acpiphp_disable_slot(struct acpiphp_slot *slot)
1241 int retval = 0;
1243 mutex_lock(&slot->crit_sect);
1245 /* unconfigure all functions */
1246 retval = disable_device(slot);
1247 if (retval)
1248 goto err_exit;
1250 /* power off all functions */
1251 retval = power_off_slot(slot);
1252 if (retval)
1253 goto err_exit;
1255 err_exit:
1256 mutex_unlock(&slot->crit_sect);
1257 return retval;
1262 * slot enabled: 1
1263 * slot disabled: 0
1265 u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1267 return (slot->flags & SLOT_POWEREDON);
1272 * latch open: 1
1273 * latch closed: 0
1275 u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1277 unsigned int sta;
1279 sta = get_slot_status(slot);
1281 return (sta & ACPI_STA_DEVICE_UI) ? 0 : 1;
1286 * adapter presence : 1
1287 * absence : 0
1289 u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1291 unsigned int sta;
1293 sta = get_slot_status(slot);
1295 return (sta == 0) ? 0 : 1;