ACPI: acpi_bus_{scan,bus,add}: return -ENODEV if no device was found
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / pci / hotplug / acpiphp_glue.c
blobcb2fd01eddaed6fa23b679ecadf86d15171e8518
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>
51 #include "../pci.h"
52 #include "acpiphp.h"
54 static LIST_HEAD(bridge_list);
56 #define MY_NAME "acpiphp_glue"
58 static void handle_hotplug_event_bridge (acpi_handle, u32, void *);
59 static void acpiphp_sanitize_bus(struct pci_bus *bus);
60 static void acpiphp_set_hpp_values(struct pci_bus *bus);
61 static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context);
63 /* callback routine to check for the existence of a pci dock device */
64 static acpi_status
65 is_pci_dock_device(acpi_handle handle, u32 lvl, void *context, void **rv)
67 int *count = (int *)context;
69 if (is_dock_device(handle)) {
70 (*count)++;
71 return AE_CTRL_TERMINATE;
72 } else {
73 return AE_OK;
78 * the _DCK method can do funny things... and sometimes not
79 * hah-hah funny.
81 * TBD - figure out a way to only call fixups for
82 * systems that require them.
84 static int post_dock_fixups(struct notifier_block *nb, unsigned long val,
85 void *v)
87 struct acpiphp_func *func = container_of(nb, struct acpiphp_func, nb);
88 struct pci_bus *bus = func->slot->bridge->pci_bus;
89 u32 buses;
91 if (!bus->self)
92 return NOTIFY_OK;
94 /* fixup bad _DCK function that rewrites
95 * secondary bridge on slot
97 pci_read_config_dword(bus->self,
98 PCI_PRIMARY_BUS,
99 &buses);
101 if (((buses >> 8) & 0xff) != bus->secondary) {
102 buses = (buses & 0xff000000)
103 | ((unsigned int)(bus->primary) << 0)
104 | ((unsigned int)(bus->secondary) << 8)
105 | ((unsigned int)(bus->subordinate) << 16);
106 pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses);
108 return NOTIFY_OK;
112 static struct acpi_dock_ops acpiphp_dock_ops = {
113 .handler = handle_hotplug_event_func,
116 /* callback routine to register each ACPI PCI slot object */
117 static acpi_status
118 register_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
120 struct acpiphp_bridge *bridge = (struct acpiphp_bridge *)context;
121 struct acpiphp_slot *slot;
122 struct acpiphp_func *newfunc;
123 acpi_handle tmp;
124 acpi_status status = AE_OK;
125 unsigned long long adr, sun;
126 int device, function, retval;
127 struct pci_bus *pbus = bridge->pci_bus;
128 struct pci_dev *pdev;
130 if (!acpi_pci_check_ejectable(pbus, handle) && !is_dock_device(handle))
131 return AE_OK;
133 acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
134 device = (adr >> 16) & 0xffff;
135 function = adr & 0xffff;
137 newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
138 if (!newfunc)
139 return AE_NO_MEMORY;
141 INIT_LIST_HEAD(&newfunc->sibling);
142 newfunc->handle = handle;
143 newfunc->function = function;
145 if (ACPI_SUCCESS(acpi_get_handle(handle, "_EJ0", &tmp)))
146 newfunc->flags = FUNC_HAS_EJ0;
148 if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp)))
149 newfunc->flags |= FUNC_HAS_STA;
151 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp)))
152 newfunc->flags |= FUNC_HAS_PS0;
154 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp)))
155 newfunc->flags |= FUNC_HAS_PS3;
157 if (ACPI_SUCCESS(acpi_get_handle(handle, "_DCK", &tmp)))
158 newfunc->flags |= FUNC_HAS_DCK;
160 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
161 if (ACPI_FAILURE(status)) {
163 * use the count of the number of slots we've found
164 * for the number of the slot
166 sun = bridge->nr_slots+1;
169 /* search for objects that share the same slot */
170 for (slot = bridge->slots; slot; slot = slot->next)
171 if (slot->device == device) {
172 if (slot->sun != sun)
173 warn("sibling found, but _SUN doesn't match!\n");
174 break;
177 if (!slot) {
178 slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
179 if (!slot) {
180 kfree(newfunc);
181 return AE_NO_MEMORY;
184 slot->bridge = bridge;
185 slot->device = device;
186 slot->sun = sun;
187 INIT_LIST_HEAD(&slot->funcs);
188 mutex_init(&slot->crit_sect);
190 slot->next = bridge->slots;
191 bridge->slots = slot;
193 bridge->nr_slots++;
195 dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
196 slot->sun, pci_domain_nr(pbus), pbus->number, device);
197 retval = acpiphp_register_hotplug_slot(slot);
198 if (retval) {
199 if (retval == -EBUSY)
200 warn("Slot %llu already registered by another "
201 "hotplug driver\n", slot->sun);
202 else
203 warn("acpiphp_register_hotplug_slot failed "
204 "(err code = 0x%x)\n", retval);
205 goto err_exit;
209 newfunc->slot = slot;
210 list_add_tail(&newfunc->sibling, &slot->funcs);
212 pdev = pci_get_slot(pbus, PCI_DEVFN(device, function));
213 if (pdev) {
214 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
215 pci_dev_put(pdev);
218 if (is_dock_device(handle)) {
219 /* we don't want to call this device's _EJ0
220 * because we want the dock notify handler
221 * to call it after it calls _DCK
223 newfunc->flags &= ~FUNC_HAS_EJ0;
224 if (register_hotplug_dock_device(handle,
225 &acpiphp_dock_ops, newfunc))
226 dbg("failed to register dock device\n");
228 /* we need to be notified when dock events happen
229 * outside of the hotplug operation, since we may
230 * need to do fixups before we can hotplug.
232 newfunc->nb.notifier_call = post_dock_fixups;
233 if (register_dock_notifier(&newfunc->nb))
234 dbg("failed to register a dock notifier");
237 /* install notify handler */
238 if (!(newfunc->flags & FUNC_HAS_DCK)) {
239 status = acpi_install_notify_handler(handle,
240 ACPI_SYSTEM_NOTIFY,
241 handle_hotplug_event_func,
242 newfunc);
244 if (ACPI_FAILURE(status))
245 err("failed to register interrupt notify handler\n");
246 } else
247 status = AE_OK;
249 return status;
251 err_exit:
252 bridge->nr_slots--;
253 bridge->slots = slot->next;
254 kfree(slot);
255 kfree(newfunc);
257 return AE_OK;
261 /* see if it's worth looking at this bridge */
262 static int detect_ejectable_slots(acpi_handle handle)
264 int found = acpi_pci_detect_ejectable(handle);
265 if (!found) {
266 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
267 is_pci_dock_device, NULL, (void *)&found, NULL);
269 return found;
272 /* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
273 static void init_bridge_misc(struct acpiphp_bridge *bridge)
275 acpi_status status;
277 /* must be added to the list prior to calling register_slot */
278 list_add(&bridge->list, &bridge_list);
280 /* register all slot objects under this bridge */
281 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
282 register_slot, NULL, bridge, NULL);
283 if (ACPI_FAILURE(status)) {
284 list_del(&bridge->list);
285 return;
288 /* install notify handler */
289 if (bridge->type != BRIDGE_TYPE_HOST) {
290 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
291 status = acpi_remove_notify_handler(bridge->func->handle,
292 ACPI_SYSTEM_NOTIFY,
293 handle_hotplug_event_func);
294 if (ACPI_FAILURE(status))
295 err("failed to remove notify handler\n");
297 status = acpi_install_notify_handler(bridge->handle,
298 ACPI_SYSTEM_NOTIFY,
299 handle_hotplug_event_bridge,
300 bridge);
302 if (ACPI_FAILURE(status)) {
303 err("failed to register interrupt notify handler\n");
309 /* find acpiphp_func from acpiphp_bridge */
310 static struct acpiphp_func *acpiphp_bridge_handle_to_function(acpi_handle handle)
312 struct acpiphp_bridge *bridge;
313 struct acpiphp_slot *slot;
314 struct acpiphp_func *func;
316 list_for_each_entry(bridge, &bridge_list, list) {
317 for (slot = bridge->slots; slot; slot = slot->next) {
318 list_for_each_entry(func, &slot->funcs, sibling) {
319 if (func->handle == handle)
320 return func;
325 return NULL;
329 static inline void config_p2p_bridge_flags(struct acpiphp_bridge *bridge)
331 acpi_handle dummy_handle;
333 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
334 "_STA", &dummy_handle)))
335 bridge->flags |= BRIDGE_HAS_STA;
337 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
338 "_EJ0", &dummy_handle)))
339 bridge->flags |= BRIDGE_HAS_EJ0;
341 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
342 "_PS0", &dummy_handle)))
343 bridge->flags |= BRIDGE_HAS_PS0;
345 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
346 "_PS3", &dummy_handle)))
347 bridge->flags |= BRIDGE_HAS_PS3;
349 /* is this ejectable p2p bridge? */
350 if (bridge->flags & BRIDGE_HAS_EJ0) {
351 struct acpiphp_func *func;
353 dbg("found ejectable p2p bridge\n");
355 /* make link between PCI bridge and PCI function */
356 func = acpiphp_bridge_handle_to_function(bridge->handle);
357 if (!func)
358 return;
359 bridge->func = func;
360 func->bridge = bridge;
365 /* allocate and initialize host bridge data structure */
366 static void add_host_bridge(acpi_handle *handle)
368 struct acpiphp_bridge *bridge;
369 struct acpi_pci_root *root = acpi_pci_find_root(handle);
371 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
372 if (bridge == NULL)
373 return;
375 bridge->type = BRIDGE_TYPE_HOST;
376 bridge->handle = handle;
378 bridge->pci_bus = root->bus;
380 spin_lock_init(&bridge->res_lock);
382 init_bridge_misc(bridge);
386 /* allocate and initialize PCI-to-PCI bridge data structure */
387 static void add_p2p_bridge(acpi_handle *handle)
389 struct acpiphp_bridge *bridge;
391 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
392 if (bridge == NULL) {
393 err("out of memory\n");
394 return;
397 bridge->type = BRIDGE_TYPE_P2P;
398 bridge->handle = handle;
399 config_p2p_bridge_flags(bridge);
401 bridge->pci_dev = acpi_get_pci_dev(handle);
402 bridge->pci_bus = bridge->pci_dev->subordinate;
403 if (!bridge->pci_bus) {
404 err("This is not a PCI-to-PCI bridge!\n");
405 goto err;
409 * Grab a ref to the subordinate PCI bus in case the bus is
410 * removed via PCI core logical hotplug. The ref pins the bus
411 * (which we access during module unload).
413 get_device(&bridge->pci_bus->dev);
414 spin_lock_init(&bridge->res_lock);
416 init_bridge_misc(bridge);
417 return;
418 err:
419 pci_dev_put(bridge->pci_dev);
420 kfree(bridge);
421 return;
425 /* callback routine to find P2P bridges */
426 static acpi_status
427 find_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
429 acpi_status status;
430 struct pci_dev *dev;
432 dev = acpi_get_pci_dev(handle);
433 if (!dev || !dev->subordinate)
434 goto out;
436 /* check if this bridge has ejectable slots */
437 if ((detect_ejectable_slots(handle) > 0)) {
438 dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev));
439 add_p2p_bridge(handle);
442 /* search P2P bridges under this p2p bridge */
443 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
444 find_p2p_bridge, NULL, NULL, NULL);
445 if (ACPI_FAILURE(status))
446 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
448 out:
449 pci_dev_put(dev);
450 return AE_OK;
454 /* find hot-pluggable slots, and then find P2P bridge */
455 static int add_bridge(acpi_handle handle)
457 acpi_status status;
458 unsigned long long tmp;
459 acpi_handle dummy_handle;
461 /* if the bridge doesn't have _STA, we assume it is always there */
462 status = acpi_get_handle(handle, "_STA", &dummy_handle);
463 if (ACPI_SUCCESS(status)) {
464 status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
465 if (ACPI_FAILURE(status)) {
466 dbg("%s: _STA evaluation failure\n", __func__);
467 return 0;
469 if ((tmp & ACPI_STA_FUNCTIONING) == 0)
470 /* don't register this object */
471 return 0;
474 /* check if this bridge has ejectable slots */
475 if (detect_ejectable_slots(handle) > 0) {
476 dbg("found PCI host-bus bridge with hot-pluggable slots\n");
477 add_host_bridge(handle);
480 /* search P2P bridges under this host bridge */
481 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
482 find_p2p_bridge, NULL, NULL, NULL);
484 if (ACPI_FAILURE(status))
485 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
487 return 0;
490 static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
492 struct acpiphp_bridge *bridge;
494 list_for_each_entry(bridge, &bridge_list, list)
495 if (bridge->handle == handle)
496 return bridge;
498 return NULL;
501 static void cleanup_bridge(struct acpiphp_bridge *bridge)
503 struct acpiphp_slot *slot, *next;
504 struct acpiphp_func *func, *tmp;
505 acpi_status status;
506 acpi_handle handle = bridge->handle;
508 status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
509 handle_hotplug_event_bridge);
510 if (ACPI_FAILURE(status))
511 err("failed to remove notify handler\n");
513 if ((bridge->type != BRIDGE_TYPE_HOST) &&
514 ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func)) {
515 status = acpi_install_notify_handler(bridge->func->handle,
516 ACPI_SYSTEM_NOTIFY,
517 handle_hotplug_event_func,
518 bridge->func);
519 if (ACPI_FAILURE(status))
520 err("failed to install interrupt notify handler\n");
523 slot = bridge->slots;
524 while (slot) {
525 next = slot->next;
526 list_for_each_entry_safe(func, tmp, &slot->funcs, sibling) {
527 if (is_dock_device(func->handle)) {
528 unregister_hotplug_dock_device(func->handle);
529 unregister_dock_notifier(&func->nb);
531 if (!(func->flags & FUNC_HAS_DCK)) {
532 status = acpi_remove_notify_handler(func->handle,
533 ACPI_SYSTEM_NOTIFY,
534 handle_hotplug_event_func);
535 if (ACPI_FAILURE(status))
536 err("failed to remove notify handler\n");
538 list_del(&func->sibling);
539 kfree(func);
541 acpiphp_unregister_hotplug_slot(slot);
542 list_del(&slot->funcs);
543 kfree(slot);
544 slot = next;
548 * Only P2P bridges have a pci_dev
550 if (bridge->pci_dev)
551 put_device(&bridge->pci_bus->dev);
553 pci_dev_put(bridge->pci_dev);
554 list_del(&bridge->list);
555 kfree(bridge);
558 static acpi_status
559 cleanup_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
561 struct acpiphp_bridge *bridge;
563 /* cleanup p2p bridges under this P2P bridge
564 in a depth-first manner */
565 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
566 cleanup_p2p_bridge, NULL, NULL, NULL);
568 bridge = acpiphp_handle_to_bridge(handle);
569 if (bridge)
570 cleanup_bridge(bridge);
572 return AE_OK;
575 static void remove_bridge(acpi_handle handle)
577 struct acpiphp_bridge *bridge;
579 /* cleanup p2p bridges under this host bridge
580 in a depth-first manner */
581 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
582 (u32)1, cleanup_p2p_bridge, NULL, NULL, NULL);
585 * On root bridges with hotplug slots directly underneath (ie,
586 * no p2p bridge inbetween), we call cleanup_bridge().
588 * The else clause cleans up root bridges that either had no
589 * hotplug slots at all, or had a p2p bridge underneath.
591 bridge = acpiphp_handle_to_bridge(handle);
592 if (bridge)
593 cleanup_bridge(bridge);
594 else
595 acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
596 handle_hotplug_event_bridge);
599 static int power_on_slot(struct acpiphp_slot *slot)
601 acpi_status status;
602 struct acpiphp_func *func;
603 int retval = 0;
605 /* if already enabled, just skip */
606 if (slot->flags & SLOT_POWEREDON)
607 goto err_exit;
609 list_for_each_entry(func, &slot->funcs, sibling) {
610 if (func->flags & FUNC_HAS_PS0) {
611 dbg("%s: executing _PS0\n", __func__);
612 status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
613 if (ACPI_FAILURE(status)) {
614 warn("%s: _PS0 failed\n", __func__);
615 retval = -1;
616 goto err_exit;
617 } else
618 break;
622 /* TBD: evaluate _STA to check if the slot is enabled */
624 slot->flags |= SLOT_POWEREDON;
626 err_exit:
627 return retval;
631 static int power_off_slot(struct acpiphp_slot *slot)
633 acpi_status status;
634 struct acpiphp_func *func;
636 int retval = 0;
638 /* if already disabled, just skip */
639 if ((slot->flags & SLOT_POWEREDON) == 0)
640 goto err_exit;
642 list_for_each_entry(func, &slot->funcs, sibling) {
643 if (func->flags & FUNC_HAS_PS3) {
644 status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
645 if (ACPI_FAILURE(status)) {
646 warn("%s: _PS3 failed\n", __func__);
647 retval = -1;
648 goto err_exit;
649 } else
650 break;
654 /* TBD: evaluate _STA to check if the slot is disabled */
656 slot->flags &= (~SLOT_POWEREDON);
658 err_exit:
659 return retval;
665 * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
666 * @bus: bus to start search with
668 static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
670 struct list_head *tmp;
671 unsigned char max, n;
674 * pci_bus_max_busnr will return the highest
675 * reserved busnr for all these children.
676 * that is equivalent to the bus->subordinate
677 * value. We don't want to use the parent's
678 * bus->subordinate value because it could have
679 * padding in it.
681 max = bus->secondary;
683 list_for_each(tmp, &bus->children) {
684 n = pci_bus_max_busnr(pci_bus_b(tmp));
685 if (n > max)
686 max = n;
688 return max;
693 * acpiphp_bus_add - add a new bus to acpi subsystem
694 * @func: acpiphp_func of the bridge
696 static int acpiphp_bus_add(struct acpiphp_func *func)
698 acpi_handle phandle;
699 struct acpi_device *device, *pdevice;
700 int ret_val;
702 acpi_get_parent(func->handle, &phandle);
703 if (acpi_bus_get_device(phandle, &pdevice)) {
704 dbg("no parent device, assuming NULL\n");
705 pdevice = NULL;
707 if (!acpi_bus_get_device(func->handle, &device)) {
708 dbg("bus exists... trim\n");
709 /* this shouldn't be in here, so remove
710 * the bus then re-add it...
712 ret_val = acpi_bus_trim(device, 1);
713 dbg("acpi_bus_trim return %x\n", ret_val);
716 ret_val = acpi_bus_add(&device, pdevice, func->handle,
717 ACPI_BUS_TYPE_DEVICE);
718 if (ret_val) {
719 dbg("error adding bus, %x\n",
720 -ret_val);
721 goto acpiphp_bus_add_out;
723 ret_val = acpi_bus_start(device);
725 acpiphp_bus_add_out:
726 return ret_val;
731 * acpiphp_bus_trim - trim a bus from acpi subsystem
732 * @handle: handle to acpi namespace
734 static int acpiphp_bus_trim(acpi_handle handle)
736 struct acpi_device *device;
737 int retval;
739 retval = acpi_bus_get_device(handle, &device);
740 if (retval) {
741 dbg("acpi_device not found\n");
742 return retval;
745 retval = acpi_bus_trim(device, 1);
746 if (retval)
747 err("cannot remove from acpi list\n");
749 return retval;
753 * enable_device - enable, configure a slot
754 * @slot: slot to be enabled
756 * This function should be called per *physical slot*,
757 * not per each slot object in ACPI namespace.
759 static int __ref enable_device(struct acpiphp_slot *slot)
761 struct pci_dev *dev;
762 struct pci_bus *bus = slot->bridge->pci_bus;
763 struct acpiphp_func *func;
764 int retval = 0;
765 int num, max, pass;
766 acpi_status status;
768 if (slot->flags & SLOT_ENABLED)
769 goto err_exit;
771 /* sanity check: dev should be NULL when hot-plugged in */
772 dev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
773 if (dev) {
774 /* This case shouldn't happen */
775 err("pci_dev structure already exists.\n");
776 pci_dev_put(dev);
777 retval = -1;
778 goto err_exit;
781 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
782 if (num == 0) {
783 err("No new device found\n");
784 retval = -1;
785 goto err_exit;
788 max = acpiphp_max_busnr(bus);
789 for (pass = 0; pass < 2; pass++) {
790 list_for_each_entry(dev, &bus->devices, bus_list) {
791 if (PCI_SLOT(dev->devfn) != slot->device)
792 continue;
793 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
794 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
795 max = pci_scan_bridge(bus, dev, max, pass);
796 if (pass && dev->subordinate)
797 pci_bus_size_bridges(dev->subordinate);
802 list_for_each_entry(func, &slot->funcs, sibling)
803 acpiphp_bus_add(func);
805 pci_bus_assign_resources(bus);
806 acpiphp_sanitize_bus(bus);
807 acpiphp_set_hpp_values(bus);
808 pci_enable_bridges(bus);
809 pci_bus_add_devices(bus);
811 list_for_each_entry(func, &slot->funcs, sibling) {
812 dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
813 func->function));
814 if (!dev)
815 continue;
817 if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE &&
818 dev->hdr_type != PCI_HEADER_TYPE_CARDBUS) {
819 pci_dev_put(dev);
820 continue;
823 status = find_p2p_bridge(func->handle, (u32)1, bus, NULL);
824 if (ACPI_FAILURE(status))
825 warn("find_p2p_bridge failed (error code = 0x%x)\n",
826 status);
827 pci_dev_put(dev);
830 slot->flags |= SLOT_ENABLED;
832 err_exit:
833 return retval;
836 static void disable_bridges(struct pci_bus *bus)
838 struct pci_dev *dev;
839 list_for_each_entry(dev, &bus->devices, bus_list) {
840 if (dev->subordinate) {
841 disable_bridges(dev->subordinate);
842 pci_disable_device(dev);
848 * disable_device - disable a slot
849 * @slot: ACPI PHP slot
851 static int disable_device(struct acpiphp_slot *slot)
853 struct acpiphp_func *func;
854 struct pci_dev *pdev;
856 /* is this slot already disabled? */
857 if (!(slot->flags & SLOT_ENABLED))
858 goto err_exit;
860 list_for_each_entry(func, &slot->funcs, sibling) {
861 if (func->bridge) {
862 /* cleanup p2p bridges under this P2P bridge */
863 cleanup_p2p_bridge(func->bridge->handle,
864 (u32)1, NULL, NULL);
865 func->bridge = NULL;
868 pdev = pci_get_slot(slot->bridge->pci_bus,
869 PCI_DEVFN(slot->device, func->function));
870 if (pdev) {
871 pci_stop_bus_device(pdev);
872 if (pdev->subordinate) {
873 disable_bridges(pdev->subordinate);
874 pci_disable_device(pdev);
876 pci_remove_bus_device(pdev);
877 pci_dev_put(pdev);
881 list_for_each_entry(func, &slot->funcs, sibling) {
882 acpiphp_bus_trim(func->handle);
885 slot->flags &= (~SLOT_ENABLED);
887 err_exit:
888 return 0;
893 * get_slot_status - get ACPI slot status
894 * @slot: ACPI PHP slot
896 * If a slot has _STA for each function and if any one of them
897 * returned non-zero status, return it.
899 * If a slot doesn't have _STA and if any one of its functions'
900 * configuration space is configured, return 0x0f as a _STA.
902 * Otherwise return 0.
904 static unsigned int get_slot_status(struct acpiphp_slot *slot)
906 acpi_status status;
907 unsigned long long sta = 0;
908 u32 dvid;
909 struct acpiphp_func *func;
911 list_for_each_entry(func, &slot->funcs, sibling) {
912 if (func->flags & FUNC_HAS_STA) {
913 status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
914 if (ACPI_SUCCESS(status) && sta)
915 break;
916 } else {
917 pci_bus_read_config_dword(slot->bridge->pci_bus,
918 PCI_DEVFN(slot->device,
919 func->function),
920 PCI_VENDOR_ID, &dvid);
921 if (dvid != 0xffffffff) {
922 sta = ACPI_STA_ALL;
923 break;
928 return (unsigned int)sta;
932 * acpiphp_eject_slot - physically eject the slot
933 * @slot: ACPI PHP slot
935 int acpiphp_eject_slot(struct acpiphp_slot *slot)
937 acpi_status status;
938 struct acpiphp_func *func;
939 struct acpi_object_list arg_list;
940 union acpi_object arg;
942 list_for_each_entry(func, &slot->funcs, sibling) {
943 /* We don't want to call _EJ0 on non-existing functions. */
944 if ((func->flags & FUNC_HAS_EJ0)) {
945 /* _EJ0 method take one argument */
946 arg_list.count = 1;
947 arg_list.pointer = &arg;
948 arg.type = ACPI_TYPE_INTEGER;
949 arg.integer.value = 1;
951 status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
952 if (ACPI_FAILURE(status)) {
953 warn("%s: _EJ0 failed\n", __func__);
954 return -1;
955 } else
956 break;
959 return 0;
963 * acpiphp_check_bridge - re-enumerate devices
964 * @bridge: where to begin re-enumeration
966 * Iterate over all slots under this bridge and make sure that if a
967 * card is present they are enabled, and if not they are disabled.
969 static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
971 struct acpiphp_slot *slot;
972 int retval = 0;
973 int enabled, disabled;
975 enabled = disabled = 0;
977 for (slot = bridge->slots; slot; slot = slot->next) {
978 unsigned int status = get_slot_status(slot);
979 if (slot->flags & SLOT_ENABLED) {
980 if (status == ACPI_STA_ALL)
981 continue;
982 retval = acpiphp_disable_slot(slot);
983 if (retval) {
984 err("Error occurred in disabling\n");
985 goto err_exit;
986 } else {
987 acpiphp_eject_slot(slot);
989 disabled++;
990 } else {
991 if (status != ACPI_STA_ALL)
992 continue;
993 retval = acpiphp_enable_slot(slot);
994 if (retval) {
995 err("Error occurred in enabling\n");
996 goto err_exit;
998 enabled++;
1002 dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
1004 err_exit:
1005 return retval;
1008 static void acpiphp_set_hpp_values(struct pci_bus *bus)
1010 struct pci_dev *dev;
1012 list_for_each_entry(dev, &bus->devices, bus_list)
1013 pci_configure_slot(dev);
1017 * Remove devices for which we could not assign resources, call
1018 * arch specific code to fix-up the bus
1020 static void acpiphp_sanitize_bus(struct pci_bus *bus)
1022 struct pci_dev *dev;
1023 int i;
1024 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
1026 list_for_each_entry(dev, &bus->devices, bus_list) {
1027 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
1028 struct resource *res = &dev->resource[i];
1029 if ((res->flags & type_mask) && !res->start &&
1030 res->end) {
1031 /* Could not assign a required resources
1032 * for this device, remove it */
1033 pci_remove_bus_device(dev);
1034 break;
1040 /* Program resources in newly inserted bridge */
1041 static int acpiphp_configure_bridge (acpi_handle handle)
1043 struct pci_bus *bus;
1045 if (acpi_is_root_bridge(handle)) {
1046 struct acpi_pci_root *root = acpi_pci_find_root(handle);
1047 bus = root->bus;
1048 } else {
1049 struct pci_dev *pdev = acpi_get_pci_dev(handle);
1050 bus = pdev->subordinate;
1051 pci_dev_put(pdev);
1054 pci_bus_size_bridges(bus);
1055 pci_bus_assign_resources(bus);
1056 acpiphp_sanitize_bus(bus);
1057 acpiphp_set_hpp_values(bus);
1058 pci_enable_bridges(bus);
1059 return 0;
1062 static void handle_bridge_insertion(acpi_handle handle, u32 type)
1064 struct acpi_device *device, *pdevice;
1065 acpi_handle phandle;
1067 if ((type != ACPI_NOTIFY_BUS_CHECK) &&
1068 (type != ACPI_NOTIFY_DEVICE_CHECK)) {
1069 err("unexpected notification type %d\n", type);
1070 return;
1073 acpi_get_parent(handle, &phandle);
1074 if (acpi_bus_get_device(phandle, &pdevice)) {
1075 dbg("no parent device, assuming NULL\n");
1076 pdevice = NULL;
1078 if (acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE)) {
1079 err("cannot add bridge to acpi list\n");
1080 return;
1082 if (!acpiphp_configure_bridge(handle) &&
1083 !acpi_bus_start(device))
1084 add_bridge(handle);
1085 else
1086 err("cannot configure and start bridge\n");
1091 * ACPI event handlers
1094 static acpi_status
1095 count_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1097 int *count = (int *)context;
1098 struct acpiphp_bridge *bridge;
1100 bridge = acpiphp_handle_to_bridge(handle);
1101 if (bridge)
1102 (*count)++;
1103 return AE_OK ;
1106 static acpi_status
1107 check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1109 struct acpiphp_bridge *bridge;
1110 char objname[64];
1111 struct acpi_buffer buffer = { .length = sizeof(objname),
1112 .pointer = objname };
1114 bridge = acpiphp_handle_to_bridge(handle);
1115 if (bridge) {
1116 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1117 dbg("%s: re-enumerating slots under %s\n",
1118 __func__, objname);
1119 acpiphp_check_bridge(bridge);
1121 return AE_OK ;
1125 * handle_hotplug_event_bridge - handle ACPI event on bridges
1126 * @handle: Notify()'ed acpi_handle
1127 * @type: Notify code
1128 * @context: pointer to acpiphp_bridge structure
1130 * Handles ACPI event notification on {host,p2p} bridges.
1132 static void handle_hotplug_event_bridge(acpi_handle handle, u32 type, void *context)
1134 struct acpiphp_bridge *bridge;
1135 char objname[64];
1136 struct acpi_buffer buffer = { .length = sizeof(objname),
1137 .pointer = objname };
1138 struct acpi_device *device;
1139 int num_sub_bridges = 0;
1141 if (acpi_bus_get_device(handle, &device)) {
1142 /* This bridge must have just been physically inserted */
1143 handle_bridge_insertion(handle, type);
1144 return;
1147 bridge = acpiphp_handle_to_bridge(handle);
1148 if (type == ACPI_NOTIFY_BUS_CHECK) {
1149 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, ACPI_UINT32_MAX,
1150 count_sub_bridges, NULL, &num_sub_bridges, NULL);
1153 if (!bridge && !num_sub_bridges) {
1154 err("cannot get bridge info\n");
1155 return;
1158 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1160 switch (type) {
1161 case ACPI_NOTIFY_BUS_CHECK:
1162 /* bus re-enumerate */
1163 dbg("%s: Bus check notify on %s\n", __func__, objname);
1164 if (bridge) {
1165 dbg("%s: re-enumerating slots under %s\n",
1166 __func__, objname);
1167 acpiphp_check_bridge(bridge);
1169 if (num_sub_bridges)
1170 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
1171 ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
1172 break;
1174 case ACPI_NOTIFY_DEVICE_CHECK:
1175 /* device check */
1176 dbg("%s: Device check notify on %s\n", __func__, objname);
1177 acpiphp_check_bridge(bridge);
1178 break;
1180 case ACPI_NOTIFY_DEVICE_WAKE:
1181 /* wake event */
1182 dbg("%s: Device wake notify on %s\n", __func__, objname);
1183 break;
1185 case ACPI_NOTIFY_EJECT_REQUEST:
1186 /* request device eject */
1187 dbg("%s: Device eject notify on %s\n", __func__, objname);
1188 if ((bridge->type != BRIDGE_TYPE_HOST) &&
1189 (bridge->flags & BRIDGE_HAS_EJ0)) {
1190 struct acpiphp_slot *slot;
1191 slot = bridge->func->slot;
1192 if (!acpiphp_disable_slot(slot))
1193 acpiphp_eject_slot(slot);
1195 break;
1197 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1198 printk(KERN_ERR "Device %s cannot be configured due"
1199 " to a frequency mismatch\n", objname);
1200 break;
1202 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1203 printk(KERN_ERR "Device %s cannot be configured due"
1204 " to a bus mode mismatch\n", objname);
1205 break;
1207 case ACPI_NOTIFY_POWER_FAULT:
1208 printk(KERN_ERR "Device %s has suffered a power fault\n",
1209 objname);
1210 break;
1212 default:
1213 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1214 break;
1219 * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
1220 * @handle: Notify()'ed acpi_handle
1221 * @type: Notify code
1222 * @context: pointer to acpiphp_func structure
1224 * Handles ACPI event notification on slots.
1226 static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context)
1228 struct acpiphp_func *func;
1229 char objname[64];
1230 struct acpi_buffer buffer = { .length = sizeof(objname),
1231 .pointer = objname };
1233 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1235 func = (struct acpiphp_func *)context;
1237 switch (type) {
1238 case ACPI_NOTIFY_BUS_CHECK:
1239 /* bus re-enumerate */
1240 dbg("%s: Bus check notify on %s\n", __func__, objname);
1241 acpiphp_enable_slot(func->slot);
1242 break;
1244 case ACPI_NOTIFY_DEVICE_CHECK:
1245 /* device check : re-enumerate from parent bus */
1246 dbg("%s: Device check notify on %s\n", __func__, objname);
1247 acpiphp_check_bridge(func->slot->bridge);
1248 break;
1250 case ACPI_NOTIFY_DEVICE_WAKE:
1251 /* wake event */
1252 dbg("%s: Device wake notify on %s\n", __func__, objname);
1253 break;
1255 case ACPI_NOTIFY_EJECT_REQUEST:
1256 /* request device eject */
1257 dbg("%s: Device eject notify on %s\n", __func__, objname);
1258 if (!(acpiphp_disable_slot(func->slot)))
1259 acpiphp_eject_slot(func->slot);
1260 break;
1262 default:
1263 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1264 break;
1269 static acpi_status
1270 find_root_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1272 int *count = (int *)context;
1274 if (acpi_is_root_bridge(handle)) {
1275 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1276 handle_hotplug_event_bridge, NULL);
1277 (*count)++;
1279 return AE_OK ;
1282 static struct acpi_pci_driver acpi_pci_hp_driver = {
1283 .add = add_bridge,
1284 .remove = remove_bridge,
1288 * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
1290 int __init acpiphp_glue_init(void)
1292 int num = 0;
1294 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
1295 ACPI_UINT32_MAX, find_root_bridges, NULL, &num, NULL);
1297 if (num <= 0)
1298 return -1;
1299 else
1300 acpi_pci_register_driver(&acpi_pci_hp_driver);
1302 return 0;
1307 * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
1309 * This function frees all data allocated in acpiphp_glue_init().
1311 void acpiphp_glue_exit(void)
1313 acpi_pci_unregister_driver(&acpi_pci_hp_driver);
1318 * acpiphp_get_num_slots - count number of slots in a system
1320 int __init acpiphp_get_num_slots(void)
1322 struct acpiphp_bridge *bridge;
1323 int num_slots = 0;
1325 list_for_each_entry(bridge, &bridge_list, list) {
1326 dbg("Bus %04x:%02x has %d slot%s\n",
1327 pci_domain_nr(bridge->pci_bus),
1328 bridge->pci_bus->number, bridge->nr_slots,
1329 bridge->nr_slots == 1 ? "" : "s");
1330 num_slots += bridge->nr_slots;
1333 dbg("Total %d slots\n", num_slots);
1334 return num_slots;
1338 #if 0
1340 * acpiphp_for_each_slot - call function for each slot
1341 * @fn: callback function
1342 * @data: context to be passed to callback function
1344 static int acpiphp_for_each_slot(acpiphp_callback fn, void *data)
1346 struct list_head *node;
1347 struct acpiphp_bridge *bridge;
1348 struct acpiphp_slot *slot;
1349 int retval = 0;
1351 list_for_each (node, &bridge_list) {
1352 bridge = (struct acpiphp_bridge *)node;
1353 for (slot = bridge->slots; slot; slot = slot->next) {
1354 retval = fn(slot, data);
1355 if (!retval)
1356 goto err_exit;
1360 err_exit:
1361 return retval;
1363 #endif
1367 * acpiphp_enable_slot - power on slot
1368 * @slot: ACPI PHP slot
1370 int acpiphp_enable_slot(struct acpiphp_slot *slot)
1372 int retval;
1374 mutex_lock(&slot->crit_sect);
1376 /* wake up all functions */
1377 retval = power_on_slot(slot);
1378 if (retval)
1379 goto err_exit;
1381 if (get_slot_status(slot) == ACPI_STA_ALL) {
1382 /* configure all functions */
1383 retval = enable_device(slot);
1384 if (retval)
1385 power_off_slot(slot);
1386 } else {
1387 dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
1388 power_off_slot(slot);
1391 err_exit:
1392 mutex_unlock(&slot->crit_sect);
1393 return retval;
1397 * acpiphp_disable_slot - power off slot
1398 * @slot: ACPI PHP slot
1400 int acpiphp_disable_slot(struct acpiphp_slot *slot)
1402 int retval = 0;
1404 mutex_lock(&slot->crit_sect);
1406 /* unconfigure all functions */
1407 retval = disable_device(slot);
1408 if (retval)
1409 goto err_exit;
1411 /* power off all functions */
1412 retval = power_off_slot(slot);
1413 if (retval)
1414 goto err_exit;
1416 err_exit:
1417 mutex_unlock(&slot->crit_sect);
1418 return retval;
1423 * slot enabled: 1
1424 * slot disabled: 0
1426 u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1428 return (slot->flags & SLOT_POWEREDON);
1433 * latch open: 1
1434 * latch closed: 0
1436 u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1438 unsigned int sta;
1440 sta = get_slot_status(slot);
1442 return (sta & ACPI_STA_SHOW_IN_UI) ? 0 : 1;
1447 * adapter presence : 1
1448 * absence : 0
1450 u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1452 unsigned int sta;
1454 sta = get_slot_status(slot);
1456 return (sta == 0) ? 0 : 1;