1 /*
2  * ACPI PCI HotPlug glue functions to ACPI CA subsystem
3  *
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
11  *
12  * All rights reserved.
13  *
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.
18  *
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.
24  *
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.
28  *
29  * Send feedback to <kristen.c.accardi@intel.com>
30  *
31  */
32 
33 /*
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.
40  */
41 
42 #include <linux/init.h>
43 #include <linux/module.h>
44 
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>
52 
53 #include "../pci.h"
54 #include "acpiphp.h"
55 
56 static LIST_HEAD(bridge_list);
57 
58 #define MY_NAME "acpiphp_glue"
59 
60 static void handle_hotplug_event_bridge (acpi_handle, u32, void *);
61 static void acpiphp_sanitize_bus(struct pci_bus *bus);
62 static void acpiphp_set_hpp_values(struct pci_bus *bus);
63 static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context);
64 
65 /* callback routine to check for the existence of a pci dock device */
66 static acpi_status
67 is_pci_dock_device(acpi_handle handle, u32 lvl, void *context, void **rv)
68 {
69 	int *count = (int *)context;
70 
71 	if (is_dock_device(handle)) {
72 		(*count)++;
73 		return AE_CTRL_TERMINATE;
74 	} else {
75 		return AE_OK;
76 	}
77 }
78 
79 /*
80  * the _DCK method can do funny things... and sometimes not
81  * hah-hah funny.
82  *
83  * TBD - figure out a way to only call fixups for
84  * systems that require them.
85  */
86 static int post_dock_fixups(struct notifier_block *nb, unsigned long val,
87 	void *v)
88 {
89 	struct acpiphp_func *func = container_of(nb, struct acpiphp_func, nb);
90 	struct pci_bus *bus = func->slot->bridge->pci_bus;
91 	u32 buses;
92 
93 	if (!bus->self)
94 		return  NOTIFY_OK;
95 
96 	/* fixup bad _DCK function that rewrites
97 	 * secondary bridge on slot
98 	 */
99 	pci_read_config_dword(bus->self,
100 			PCI_PRIMARY_BUS,
101 			&buses);
102 
103 	if (((buses >> 8) & 0xff) != bus->busn_res.start) {
104 		buses = (buses & 0xff000000)
105 			| ((unsigned int)(bus->primary)     <<  0)
106 			| ((unsigned int)(bus->busn_res.start)   <<  8)
107 			| ((unsigned int)(bus->busn_res.end) << 16);
108 		pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses);
109 	}
110 	return NOTIFY_OK;
111 }
112 
113 
114 static const struct acpi_dock_ops acpiphp_dock_ops = {
115 	.handler = handle_hotplug_event_func,
116 };
117 
118 /* Check whether the PCI device is managed by native PCIe hotplug driver */
119 static bool device_is_managed_by_native_pciehp(struct pci_dev *pdev)
120 {
121 	u32 reg32;
122 	acpi_handle tmp;
123 	struct acpi_pci_root *root;
124 
125 	/* Check whether the PCIe port supports native PCIe hotplug */
126 	if (pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, &reg32))
127 		return false;
128 	if (!(reg32 & PCI_EXP_SLTCAP_HPC))
129 		return false;
130 
131 	/*
132 	 * Check whether native PCIe hotplug has been enabled for
133 	 * this PCIe hierarchy.
134 	 */
135 	tmp = acpi_find_root_bridge_handle(pdev);
136 	if (!tmp)
137 		return false;
138 	root = acpi_pci_find_root(tmp);
139 	if (!root)
140 		return false;
141 	if (!(root->osc_control_set & OSC_PCI_EXPRESS_NATIVE_HP_CONTROL))
142 		return false;
143 
144 	return true;
145 }
146 
147 /* callback routine to register each ACPI PCI slot object */
148 static acpi_status
149 register_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
150 {
151 	struct acpiphp_bridge *bridge = (struct acpiphp_bridge *)context;
152 	struct acpiphp_slot *slot;
153 	struct acpiphp_func *newfunc;
154 	acpi_handle tmp;
155 	acpi_status status = AE_OK;
156 	unsigned long long adr, sun;
157 	int device, function, retval;
158 	struct pci_bus *pbus = bridge->pci_bus;
159 	struct pci_dev *pdev;
160 
161 	if (!acpi_pci_check_ejectable(pbus, handle) && !is_dock_device(handle))
162 		return AE_OK;
163 
164 	status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
165 	if (ACPI_FAILURE(status)) {
166 		warn("can't evaluate _ADR (%#x)\n", status);
167 		return AE_OK;
168 	}
169 
170 	device = (adr >> 16) & 0xffff;
171 	function = adr & 0xffff;
172 
173 	pdev = pbus->self;
174 	if (pdev && device_is_managed_by_native_pciehp(pdev))
175 		return AE_OK;
176 
177 	newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
178 	if (!newfunc)
179 		return AE_NO_MEMORY;
180 
181 	INIT_LIST_HEAD(&newfunc->sibling);
182 	newfunc->handle = handle;
183 	newfunc->function = function;
184 
185 	if (ACPI_SUCCESS(acpi_get_handle(handle, "_EJ0", &tmp)))
186 		newfunc->flags = FUNC_HAS_EJ0;
187 
188 	if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp)))
189 		newfunc->flags |= FUNC_HAS_STA;
190 
191 	if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp)))
192 		newfunc->flags |= FUNC_HAS_PS0;
193 
194 	if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp)))
195 		newfunc->flags |= FUNC_HAS_PS3;
196 
197 	if (ACPI_SUCCESS(acpi_get_handle(handle, "_DCK", &tmp)))
198 		newfunc->flags |= FUNC_HAS_DCK;
199 
200 	status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
201 	if (ACPI_FAILURE(status)) {
202 		/*
203 		 * use the count of the number of slots we've found
204 		 * for the number of the slot
205 		 */
206 		sun = bridge->nr_slots+1;
207 	}
208 
209 	/* search for objects that share the same slot */
210 	for (slot = bridge->slots; slot; slot = slot->next)
211 		if (slot->device == device) {
212 			if (slot->sun != sun)
213 				warn("sibling found, but _SUN doesn't match!\n");
214 			break;
215 		}
216 
217 	if (!slot) {
218 		slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
219 		if (!slot) {
220 			kfree(newfunc);
221 			return AE_NO_MEMORY;
222 		}
223 
224 		slot->bridge = bridge;
225 		slot->device = device;
226 		slot->sun = sun;
227 		INIT_LIST_HEAD(&slot->funcs);
228 		mutex_init(&slot->crit_sect);
229 
230 		slot->next = bridge->slots;
231 		bridge->slots = slot;
232 
233 		bridge->nr_slots++;
234 
235 		dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
236 		    slot->sun, pci_domain_nr(pbus), pbus->number, device);
237 		retval = acpiphp_register_hotplug_slot(slot);
238 		if (retval) {
239 			if (retval == -EBUSY)
240 				warn("Slot %llu already registered by another "
241 					"hotplug driver\n", slot->sun);
242 			else
243 				warn("acpiphp_register_hotplug_slot failed "
244 					"(err code = 0x%x)\n", retval);
245 			goto err_exit;
246 		}
247 	}
248 
249 	newfunc->slot = slot;
250 	list_add_tail(&newfunc->sibling, &slot->funcs);
251 
252 	pdev = pci_get_slot(pbus, PCI_DEVFN(device, function));
253 	if (pdev) {
254 		slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
255 		pci_dev_put(pdev);
256 	}
257 
258 	if (is_dock_device(handle)) {
259 		/* we don't want to call this device's _EJ0
260 		 * because we want the dock notify handler
261 		 * to call it after it calls _DCK
262 		 */
263 		newfunc->flags &= ~FUNC_HAS_EJ0;
264 		if (register_hotplug_dock_device(handle,
265 			&acpiphp_dock_ops, newfunc))
266 			dbg("failed to register dock device\n");
267 
268 		/* we need to be notified when dock events happen
269 		 * outside of the hotplug operation, since we may
270 		 * need to do fixups before we can hotplug.
271 		 */
272 		newfunc->nb.notifier_call = post_dock_fixups;
273 		if (register_dock_notifier(&newfunc->nb))
274 			dbg("failed to register a dock notifier");
275 	}
276 
277 	/* install notify handler */
278 	if (!(newfunc->flags & FUNC_HAS_DCK)) {
279 		status = acpi_install_notify_handler(handle,
280 					     ACPI_SYSTEM_NOTIFY,
281 					     handle_hotplug_event_func,
282 					     newfunc);
283 
284 		if (ACPI_FAILURE(status))
285 			err("failed to register interrupt notify handler\n");
286 	} else
287 		status = AE_OK;
288 
289 	return status;
290 
291  err_exit:
292 	bridge->nr_slots--;
293 	bridge->slots = slot->next;
294 	kfree(slot);
295 	kfree(newfunc);
296 
297 	return AE_OK;
298 }
299 
300 
301 /* see if it's worth looking at this bridge */
302 static int detect_ejectable_slots(acpi_handle handle)
303 {
304 	int found = acpi_pci_detect_ejectable(handle);
305 	if (!found) {
306 		acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
307 				    is_pci_dock_device, NULL, (void *)&found, NULL);
308 	}
309 	return found;
310 }
311 
312 /* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
313 static void init_bridge_misc(struct acpiphp_bridge *bridge)
314 {
315 	acpi_status status;
316 
317 	/* must be added to the list prior to calling register_slot */
318 	list_add(&bridge->list, &bridge_list);
319 
320 	/* register all slot objects under this bridge */
321 	status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
322 				     register_slot, NULL, bridge, NULL);
323 	if (ACPI_FAILURE(status)) {
324 		list_del(&bridge->list);
325 		return;
326 	}
327 
328 	/* install notify handler */
329 	if (bridge->type != BRIDGE_TYPE_HOST) {
330 		if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
331 			status = acpi_remove_notify_handler(bridge->func->handle,
332 						ACPI_SYSTEM_NOTIFY,
333 						handle_hotplug_event_func);
334 			if (ACPI_FAILURE(status))
335 				err("failed to remove notify handler\n");
336 		}
337 		status = acpi_install_notify_handler(bridge->handle,
338 					     ACPI_SYSTEM_NOTIFY,
339 					     handle_hotplug_event_bridge,
340 					     bridge);
341 
342 		if (ACPI_FAILURE(status)) {
343 			err("failed to register interrupt notify handler\n");
344 		}
345 	}
346 }
347 
348 
349 /* find acpiphp_func from acpiphp_bridge */
350 static struct acpiphp_func *acpiphp_bridge_handle_to_function(acpi_handle handle)
351 {
352 	struct acpiphp_bridge *bridge;
353 	struct acpiphp_slot *slot;
354 	struct acpiphp_func *func;
355 
356 	list_for_each_entry(bridge, &bridge_list, list) {
357 		for (slot = bridge->slots; slot; slot = slot->next) {
358 			list_for_each_entry(func, &slot->funcs, sibling) {
359 				if (func->handle == handle)
360 					return func;
361 			}
362 		}
363 	}
364 
365 	return NULL;
366 }
367 
368 
369 static inline void config_p2p_bridge_flags(struct acpiphp_bridge *bridge)
370 {
371 	acpi_handle dummy_handle;
372 
373 	if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
374 					"_STA", &dummy_handle)))
375 		bridge->flags |= BRIDGE_HAS_STA;
376 
377 	if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
378 					"_EJ0", &dummy_handle)))
379 		bridge->flags |= BRIDGE_HAS_EJ0;
380 
381 	if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
382 					"_PS0", &dummy_handle)))
383 		bridge->flags |= BRIDGE_HAS_PS0;
384 
385 	if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
386 					"_PS3", &dummy_handle)))
387 		bridge->flags |= BRIDGE_HAS_PS3;
388 
389 	/* is this ejectable p2p bridge? */
390 	if (bridge->flags & BRIDGE_HAS_EJ0) {
391 		struct acpiphp_func *func;
392 
393 		dbg("found ejectable p2p bridge\n");
394 
395 		/* make link between PCI bridge and PCI function */
396 		func = acpiphp_bridge_handle_to_function(bridge->handle);
397 		if (!func)
398 			return;
399 		bridge->func = func;
400 		func->bridge = bridge;
401 	}
402 }
403 
404 
405 /* allocate and initialize host bridge data structure */
406 static void add_host_bridge(struct acpi_pci_root *root)
407 {
408 	struct acpiphp_bridge *bridge;
409 	acpi_handle handle = root->device->handle;
410 
411 	bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
412 	if (bridge == NULL)
413 		return;
414 
415 	bridge->type = BRIDGE_TYPE_HOST;
416 	bridge->handle = handle;
417 
418 	bridge->pci_bus = root->bus;
419 
420 	init_bridge_misc(bridge);
421 }
422 
423 
424 /* allocate and initialize PCI-to-PCI bridge data structure */
425 static void add_p2p_bridge(acpi_handle *handle)
426 {
427 	struct acpiphp_bridge *bridge;
428 
429 	bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
430 	if (bridge == NULL) {
431 		err("out of memory\n");
432 		return;
433 	}
434 
435 	bridge->type = BRIDGE_TYPE_P2P;
436 	bridge->handle = handle;
437 	config_p2p_bridge_flags(bridge);
438 
439 	bridge->pci_dev = acpi_get_pci_dev(handle);
440 	bridge->pci_bus = bridge->pci_dev->subordinate;
441 	if (!bridge->pci_bus) {
442 		err("This is not a PCI-to-PCI bridge!\n");
443 		goto err;
444 	}
445 
446 	/*
447 	 * Grab a ref to the subordinate PCI bus in case the bus is
448 	 * removed via PCI core logical hotplug. The ref pins the bus
449 	 * (which we access during module unload).
450 	 */
451 	get_device(&bridge->pci_bus->dev);
452 
453 	init_bridge_misc(bridge);
454 	return;
455  err:
456 	pci_dev_put(bridge->pci_dev);
457 	kfree(bridge);
458 	return;
459 }
460 
461 
462 /* callback routine to find P2P bridges */
463 static acpi_status
464 find_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
465 {
466 	acpi_status status;
467 	struct pci_dev *dev;
468 
469 	dev = acpi_get_pci_dev(handle);
470 	if (!dev || !dev->subordinate)
471 		goto out;
472 
473 	/* check if this bridge has ejectable slots */
474 	if ((detect_ejectable_slots(handle) > 0)) {
475 		dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev));
476 		add_p2p_bridge(handle);
477 	}
478 
479 	/* search P2P bridges under this p2p bridge */
480 	status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
481 				     find_p2p_bridge, NULL, NULL, NULL);
482 	if (ACPI_FAILURE(status))
483 		warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
484 
485  out:
486 	pci_dev_put(dev);
487 	return AE_OK;
488 }
489 
490 
491 /* find hot-pluggable slots, and then find P2P bridge */
492 static int add_bridge(struct acpi_pci_root *root)
493 {
494 	acpi_status status;
495 	unsigned long long tmp;
496 	acpi_handle dummy_handle;
497 	acpi_handle handle = root->device->handle;
498 
499 	/* if the bridge doesn't have _STA, we assume it is always there */
500 	status = acpi_get_handle(handle, "_STA", &dummy_handle);
501 	if (ACPI_SUCCESS(status)) {
502 		status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
503 		if (ACPI_FAILURE(status)) {
504 			dbg("%s: _STA evaluation failure\n", __func__);
505 			return 0;
506 		}
507 		if ((tmp & ACPI_STA_FUNCTIONING) == 0)
508 			/* don't register this object */
509 			return 0;
510 	}
511 
512 	/* check if this bridge has ejectable slots */
513 	if (detect_ejectable_slots(handle) > 0) {
514 		dbg("found PCI host-bus bridge with hot-pluggable slots\n");
515 		add_host_bridge(root);
516 	}
517 
518 	/* search P2P bridges under this host bridge */
519 	status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
520 				     find_p2p_bridge, NULL, NULL, NULL);
521 
522 	if (ACPI_FAILURE(status))
523 		warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
524 
525 	return 0;
526 }
527 
528 static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
529 {
530 	struct acpiphp_bridge *bridge;
531 
532 	list_for_each_entry(bridge, &bridge_list, list)
533 		if (bridge->handle == handle)
534 			return bridge;
535 
536 	return NULL;
537 }
538 
539 static void cleanup_bridge(struct acpiphp_bridge *bridge)
540 {
541 	struct acpiphp_slot *slot, *next;
542 	struct acpiphp_func *func, *tmp;
543 	acpi_status status;
544 	acpi_handle handle = bridge->handle;
545 
546 	status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
547 					    handle_hotplug_event_bridge);
548 	if (ACPI_FAILURE(status))
549 		err("failed to remove notify handler\n");
550 
551 	if ((bridge->type != BRIDGE_TYPE_HOST) &&
552 	    ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func)) {
553 		status = acpi_install_notify_handler(bridge->func->handle,
554 						ACPI_SYSTEM_NOTIFY,
555 						handle_hotplug_event_func,
556 						bridge->func);
557 		if (ACPI_FAILURE(status))
558 			err("failed to install interrupt notify handler\n");
559 	}
560 
561 	slot = bridge->slots;
562 	while (slot) {
563 		next = slot->next;
564 		list_for_each_entry_safe(func, tmp, &slot->funcs, sibling) {
565 			if (is_dock_device(func->handle)) {
566 				unregister_hotplug_dock_device(func->handle);
567 				unregister_dock_notifier(&func->nb);
568 			}
569 			if (!(func->flags & FUNC_HAS_DCK)) {
570 				status = acpi_remove_notify_handler(func->handle,
571 						ACPI_SYSTEM_NOTIFY,
572 						handle_hotplug_event_func);
573 				if (ACPI_FAILURE(status))
574 					err("failed to remove notify handler\n");
575 			}
576 			list_del(&func->sibling);
577 			kfree(func);
578 		}
579 		acpiphp_unregister_hotplug_slot(slot);
580 		list_del(&slot->funcs);
581 		kfree(slot);
582 		slot = next;
583 	}
584 
585 	/*
586 	 * Only P2P bridges have a pci_dev
587 	 */
588 	if (bridge->pci_dev)
589 		put_device(&bridge->pci_bus->dev);
590 
591 	pci_dev_put(bridge->pci_dev);
592 	list_del(&bridge->list);
593 	kfree(bridge);
594 }
595 
596 static acpi_status
597 cleanup_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
598 {
599 	struct acpiphp_bridge *bridge;
600 
601 	/* cleanup p2p bridges under this P2P bridge
602 	   in a depth-first manner */
603 	acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
604 				cleanup_p2p_bridge, NULL, NULL, NULL);
605 
606 	bridge = acpiphp_handle_to_bridge(handle);
607 	if (bridge)
608 		cleanup_bridge(bridge);
609 
610 	return AE_OK;
611 }
612 
613 static void remove_bridge(struct acpi_pci_root *root)
614 {
615 	struct acpiphp_bridge *bridge;
616 	acpi_handle handle = root->device->handle;
617 
618 	/* cleanup p2p bridges under this host bridge
619 	   in a depth-first manner */
620 	acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
621 				(u32)1, cleanup_p2p_bridge, NULL, NULL, NULL);
622 
623 	/*
624 	 * On root bridges with hotplug slots directly underneath (ie,
625 	 * no p2p bridge between), we call cleanup_bridge().
626 	 *
627 	 * The else clause cleans up root bridges that either had no
628 	 * hotplug slots at all, or had a p2p bridge underneath.
629 	 */
630 	bridge = acpiphp_handle_to_bridge(handle);
631 	if (bridge)
632 		cleanup_bridge(bridge);
633 	else
634 		acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
635 					   handle_hotplug_event_bridge);
636 }
637 
638 static int power_on_slot(struct acpiphp_slot *slot)
639 {
640 	acpi_status status;
641 	struct acpiphp_func *func;
642 	int retval = 0;
643 
644 	/* if already enabled, just skip */
645 	if (slot->flags & SLOT_POWEREDON)
646 		goto err_exit;
647 
648 	list_for_each_entry(func, &slot->funcs, sibling) {
649 		if (func->flags & FUNC_HAS_PS0) {
650 			dbg("%s: executing _PS0\n", __func__);
651 			status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
652 			if (ACPI_FAILURE(status)) {
653 				warn("%s: _PS0 failed\n", __func__);
654 				retval = -1;
655 				goto err_exit;
656 			} else
657 				break;
658 		}
659 	}
660 
661 	/* TBD: evaluate _STA to check if the slot is enabled */
662 
663 	slot->flags |= SLOT_POWEREDON;
664 
665  err_exit:
666 	return retval;
667 }
668 
669 
670 static int power_off_slot(struct acpiphp_slot *slot)
671 {
672 	acpi_status status;
673 	struct acpiphp_func *func;
674 
675 	int retval = 0;
676 
677 	/* if already disabled, just skip */
678 	if ((slot->flags & SLOT_POWEREDON) == 0)
679 		goto err_exit;
680 
681 	list_for_each_entry(func, &slot->funcs, sibling) {
682 		if (func->flags & FUNC_HAS_PS3) {
683 			status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
684 			if (ACPI_FAILURE(status)) {
685 				warn("%s: _PS3 failed\n", __func__);
686 				retval = -1;
687 				goto err_exit;
688 			} else
689 				break;
690 		}
691 	}
692 
693 	/* TBD: evaluate _STA to check if the slot is disabled */
694 
695 	slot->flags &= (~SLOT_POWEREDON);
696 
697  err_exit:
698 	return retval;
699 }
700 
701 
702 
703 /**
704  * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
705  * @bus: bus to start search with
706  */
707 static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
708 {
709 	struct list_head *tmp;
710 	unsigned char max, n;
711 
712 	/*
713 	 * pci_bus_max_busnr will return the highest
714 	 * reserved busnr for all these children.
715 	 * that is equivalent to the bus->subordinate
716 	 * value.  We don't want to use the parent's
717 	 * bus->subordinate value because it could have
718 	 * padding in it.
719 	 */
720 	max = bus->busn_res.start;
721 
722 	list_for_each(tmp, &bus->children) {
723 		n = pci_bus_max_busnr(pci_bus_b(tmp));
724 		if (n > max)
725 			max = n;
726 	}
727 	return max;
728 }
729 
730 
731 /**
732  * acpiphp_bus_add - add a new bus to acpi subsystem
733  * @func: acpiphp_func of the bridge
734  */
735 static int acpiphp_bus_add(struct acpiphp_func *func)
736 {
737 	struct acpi_device *device;
738 	int ret_val;
739 
740 	if (!acpi_bus_get_device(func->handle, &device)) {
741 		dbg("bus exists... trim\n");
742 		/* this shouldn't be in here, so remove
743 		 * the bus then re-add it...
744 		 */
745 		ret_val = acpi_bus_trim(device, 1);
746 		dbg("acpi_bus_trim return %x\n", ret_val);
747 	}
748 
749 	ret_val = acpi_bus_add(func->handle, &device);
750 	if (ret_val) {
751 		dbg("error adding bus, %x\n",
752 			-ret_val);
753 		goto acpiphp_bus_add_out;
754 	}
755 
756 acpiphp_bus_add_out:
757 	return ret_val;
758 }
759 
760 
761 /**
762  * acpiphp_bus_trim - trim a bus from acpi subsystem
763  * @handle: handle to acpi namespace
764  */
765 static int acpiphp_bus_trim(acpi_handle handle)
766 {
767 	struct acpi_device *device;
768 	int retval;
769 
770 	retval = acpi_bus_get_device(handle, &device);
771 	if (retval) {
772 		dbg("acpi_device not found\n");
773 		return retval;
774 	}
775 
776 	retval = acpi_bus_trim(device, 1);
777 	if (retval)
778 		err("cannot remove from acpi list\n");
779 
780 	return retval;
781 }
782 
783 static void acpiphp_set_acpi_region(struct acpiphp_slot *slot)
784 {
785 	struct acpiphp_func *func;
786 	union acpi_object params[2];
787 	struct acpi_object_list arg_list;
788 
789 	list_for_each_entry(func, &slot->funcs, sibling) {
790 		arg_list.count = 2;
791 		arg_list.pointer = params;
792 		params[0].type = ACPI_TYPE_INTEGER;
793 		params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG;
794 		params[1].type = ACPI_TYPE_INTEGER;
795 		params[1].integer.value = 1;
796 		/* _REG is optional, we don't care about if there is failure */
797 		acpi_evaluate_object(func->handle, "_REG", &arg_list, NULL);
798 	}
799 }
800 
801 /**
802  * enable_device - enable, configure a slot
803  * @slot: slot to be enabled
804  *
805  * This function should be called per *physical slot*,
806  * not per each slot object in ACPI namespace.
807  */
808 static int __ref enable_device(struct acpiphp_slot *slot)
809 {
810 	struct pci_dev *dev;
811 	struct pci_bus *bus = slot->bridge->pci_bus;
812 	struct acpiphp_func *func;
813 	int retval = 0;
814 	int num, max, pass;
815 	acpi_status status;
816 
817 	if (slot->flags & SLOT_ENABLED)
818 		goto err_exit;
819 
820 	num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
821 	if (num == 0) {
822 		/* Maybe only part of funcs are added. */
823 		dbg("No new device found\n");
824 		goto err_exit;
825 	}
826 
827 	max = acpiphp_max_busnr(bus);
828 	for (pass = 0; pass < 2; pass++) {
829 		list_for_each_entry(dev, &bus->devices, bus_list) {
830 			if (PCI_SLOT(dev->devfn) != slot->device)
831 				continue;
832 			if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
833 			    dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
834 				max = pci_scan_bridge(bus, dev, max, pass);
835 				if (pass && dev->subordinate)
836 					pci_bus_size_bridges(dev->subordinate);
837 			}
838 		}
839 	}
840 
841 	list_for_each_entry(func, &slot->funcs, sibling)
842 		acpiphp_bus_add(func);
843 
844 	pci_bus_assign_resources(bus);
845 	acpiphp_sanitize_bus(bus);
846 	acpiphp_set_hpp_values(bus);
847 	acpiphp_set_acpi_region(slot);
848 	pci_enable_bridges(bus);
849 
850 	list_for_each_entry(dev, &bus->devices, bus_list) {
851 		/* Assume that newly added devices are powered on already. */
852 		if (!dev->is_added)
853 			dev->current_state = PCI_D0;
854 	}
855 
856 	pci_bus_add_devices(bus);
857 
858 	slot->flags |= SLOT_ENABLED;
859 	list_for_each_entry(func, &slot->funcs, sibling) {
860 		dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
861 						  func->function));
862 		if (!dev) {
863 			/* Do not set SLOT_ENABLED flag if some funcs
864 			   are not added. */
865 			slot->flags &= (~SLOT_ENABLED);
866 			continue;
867 		}
868 
869 		if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE &&
870 		    dev->hdr_type != PCI_HEADER_TYPE_CARDBUS) {
871 			pci_dev_put(dev);
872 			continue;
873 		}
874 
875 		status = find_p2p_bridge(func->handle, (u32)1, bus, NULL);
876 		if (ACPI_FAILURE(status))
877 			warn("find_p2p_bridge failed (error code = 0x%x)\n",
878 				status);
879 		pci_dev_put(dev);
880 	}
881 
882 
883  err_exit:
884 	return retval;
885 }
886 
887 /* return first device in slot, acquiring a reference on it */
888 static struct pci_dev *dev_in_slot(struct acpiphp_slot *slot)
889 {
890 	struct pci_bus *bus = slot->bridge->pci_bus;
891 	struct pci_dev *dev;
892 	struct pci_dev *ret = NULL;
893 
894 	down_read(&pci_bus_sem);
895 	list_for_each_entry(dev, &bus->devices, bus_list)
896 		if (PCI_SLOT(dev->devfn) == slot->device) {
897 			ret = pci_dev_get(dev);
898 			break;
899 		}
900 	up_read(&pci_bus_sem);
901 
902 	return ret;
903 }
904 
905 /**
906  * disable_device - disable a slot
907  * @slot: ACPI PHP slot
908  */
909 static int disable_device(struct acpiphp_slot *slot)
910 {
911 	struct acpiphp_func *func;
912 	struct pci_dev *pdev;
913 	struct pci_bus *bus = slot->bridge->pci_bus;
914 
915 	/* The slot will be enabled when func 0 is added, so check
916 	   func 0 before disable the slot. */
917 	pdev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
918 	if (!pdev)
919 		goto err_exit;
920 	pci_dev_put(pdev);
921 
922 	list_for_each_entry(func, &slot->funcs, sibling) {
923 		if (func->bridge) {
924 			/* cleanup p2p bridges under this P2P bridge */
925 			cleanup_p2p_bridge(func->bridge->handle,
926 						(u32)1, NULL, NULL);
927 			func->bridge = NULL;
928 		}
929 	}
930 
931 	/*
932 	 * enable_device() enumerates all functions in this device via
933 	 * pci_scan_slot(), whether they have associated ACPI hotplug
934 	 * methods (_EJ0, etc.) or not.  Therefore, we remove all functions
935 	 * here.
936 	 */
937 	while ((pdev = dev_in_slot(slot))) {
938 		pci_stop_and_remove_bus_device(pdev);
939 		pci_dev_put(pdev);
940 	}
941 
942 	list_for_each_entry(func, &slot->funcs, sibling) {
943 		acpiphp_bus_trim(func->handle);
944 	}
945 
946 	slot->flags &= (~SLOT_ENABLED);
947 
948 err_exit:
949 	return 0;
950 }
951 
952 
953 /**
954  * get_slot_status - get ACPI slot status
955  * @slot: ACPI PHP slot
956  *
957  * If a slot has _STA for each function and if any one of them
958  * returned non-zero status, return it.
959  *
960  * If a slot doesn't have _STA and if any one of its functions'
961  * configuration space is configured, return 0x0f as a _STA.
962  *
963  * Otherwise return 0.
964  */
965 static unsigned int get_slot_status(struct acpiphp_slot *slot)
966 {
967 	acpi_status status;
968 	unsigned long long sta = 0;
969 	u32 dvid;
970 	struct acpiphp_func *func;
971 
972 	list_for_each_entry(func, &slot->funcs, sibling) {
973 		if (func->flags & FUNC_HAS_STA) {
974 			status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
975 			if (ACPI_SUCCESS(status) && sta)
976 				break;
977 		} else {
978 			pci_bus_read_config_dword(slot->bridge->pci_bus,
979 						  PCI_DEVFN(slot->device,
980 							    func->function),
981 						  PCI_VENDOR_ID, &dvid);
982 			if (dvid != 0xffffffff) {
983 				sta = ACPI_STA_ALL;
984 				break;
985 			}
986 		}
987 	}
988 
989 	return (unsigned int)sta;
990 }
991 
992 /**
993  * acpiphp_eject_slot - physically eject the slot
994  * @slot: ACPI PHP slot
995  */
996 int acpiphp_eject_slot(struct acpiphp_slot *slot)
997 {
998 	acpi_status status;
999 	struct acpiphp_func *func;
1000 	struct acpi_object_list arg_list;
1001 	union acpi_object arg;
1002 
1003 	list_for_each_entry(func, &slot->funcs, sibling) {
1004 		/* We don't want to call _EJ0 on non-existing functions. */
1005 		if ((func->flags & FUNC_HAS_EJ0)) {
1006 			/* _EJ0 method take one argument */
1007 			arg_list.count = 1;
1008 			arg_list.pointer = &arg;
1009 			arg.type = ACPI_TYPE_INTEGER;
1010 			arg.integer.value = 1;
1011 
1012 			status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
1013 			if (ACPI_FAILURE(status)) {
1014 				warn("%s: _EJ0 failed\n", __func__);
1015 				return -1;
1016 			} else
1017 				break;
1018 		}
1019 	}
1020 	return 0;
1021 }
1022 
1023 /**
1024  * acpiphp_check_bridge - re-enumerate devices
1025  * @bridge: where to begin re-enumeration
1026  *
1027  * Iterate over all slots under this bridge and make sure that if a
1028  * card is present they are enabled, and if not they are disabled.
1029  */
1030 static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
1031 {
1032 	struct acpiphp_slot *slot;
1033 	int retval = 0;
1034 	int enabled, disabled;
1035 
1036 	enabled = disabled = 0;
1037 
1038 	for (slot = bridge->slots; slot; slot = slot->next) {
1039 		unsigned int status = get_slot_status(slot);
1040 		if (slot->flags & SLOT_ENABLED) {
1041 			if (status == ACPI_STA_ALL)
1042 				continue;
1043 			retval = acpiphp_disable_slot(slot);
1044 			if (retval) {
1045 				err("Error occurred in disabling\n");
1046 				goto err_exit;
1047 			} else {
1048 				acpiphp_eject_slot(slot);
1049 			}
1050 			disabled++;
1051 		} else {
1052 			if (status != ACPI_STA_ALL)
1053 				continue;
1054 			retval = acpiphp_enable_slot(slot);
1055 			if (retval) {
1056 				err("Error occurred in enabling\n");
1057 				goto err_exit;
1058 			}
1059 			enabled++;
1060 		}
1061 	}
1062 
1063 	dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
1064 
1065  err_exit:
1066 	return retval;
1067 }
1068 
1069 static void acpiphp_set_hpp_values(struct pci_bus *bus)
1070 {
1071 	struct pci_dev *dev;
1072 
1073 	list_for_each_entry(dev, &bus->devices, bus_list)
1074 		pci_configure_slot(dev);
1075 }
1076 
1077 /*
1078  * Remove devices for which we could not assign resources, call
1079  * arch specific code to fix-up the bus
1080  */
1081 static void acpiphp_sanitize_bus(struct pci_bus *bus)
1082 {
1083 	struct pci_dev *dev;
1084 	int i;
1085 	unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
1086 
1087 	list_for_each_entry(dev, &bus->devices, bus_list) {
1088 		for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
1089 			struct resource *res = &dev->resource[i];
1090 			if ((res->flags & type_mask) && !res->start &&
1091 					res->end) {
1092 				/* Could not assign a required resources
1093 				 * for this device, remove it */
1094 				pci_stop_and_remove_bus_device(dev);
1095 				break;
1096 			}
1097 		}
1098 	}
1099 }
1100 
1101 /* Program resources in newly inserted bridge */
1102 static int acpiphp_configure_bridge (acpi_handle handle)
1103 {
1104 	struct pci_bus *bus;
1105 
1106 	if (acpi_is_root_bridge(handle)) {
1107 		struct acpi_pci_root *root = acpi_pci_find_root(handle);
1108 		bus = root->bus;
1109 	} else {
1110 		struct pci_dev *pdev = acpi_get_pci_dev(handle);
1111 		bus = pdev->subordinate;
1112 		pci_dev_put(pdev);
1113 	}
1114 
1115 	pci_bus_size_bridges(bus);
1116 	pci_bus_assign_resources(bus);
1117 	acpiphp_sanitize_bus(bus);
1118 	acpiphp_set_hpp_values(bus);
1119 	pci_enable_bridges(bus);
1120 	return 0;
1121 }
1122 
1123 static void handle_bridge_insertion(acpi_handle handle, u32 type)
1124 {
1125 	struct acpi_device *device;
1126 
1127 	if ((type != ACPI_NOTIFY_BUS_CHECK) &&
1128 			(type != ACPI_NOTIFY_DEVICE_CHECK)) {
1129 		err("unexpected notification type %d\n", type);
1130 		return;
1131 	}
1132 
1133 	if (acpi_bus_add(handle, &device)) {
1134 		err("cannot add bridge to acpi list\n");
1135 		return;
1136 	}
1137 	if (!acpiphp_configure_bridge(handle))
1138 		add_bridge(handle);
1139 	else
1140 		err("cannot configure and start bridge\n");
1141 
1142 }
1143 
1144 /*
1145  * ACPI event handlers
1146  */
1147 
1148 static acpi_status
1149 count_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1150 {
1151 	int *count = (int *)context;
1152 	struct acpiphp_bridge *bridge;
1153 
1154 	bridge = acpiphp_handle_to_bridge(handle);
1155 	if (bridge)
1156 		(*count)++;
1157 	return AE_OK ;
1158 }
1159 
1160 static acpi_status
1161 check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1162 {
1163 	struct acpiphp_bridge *bridge;
1164 	char objname[64];
1165 	struct acpi_buffer buffer = { .length = sizeof(objname),
1166 				      .pointer = objname };
1167 
1168 	bridge = acpiphp_handle_to_bridge(handle);
1169 	if (bridge) {
1170 		acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1171 		dbg("%s: re-enumerating slots under %s\n",
1172 			__func__, objname);
1173 		acpiphp_check_bridge(bridge);
1174 	}
1175 	return AE_OK ;
1176 }
1177 
1178 struct acpiphp_hp_work {
1179 	struct work_struct work;
1180 	acpi_handle handle;
1181 	u32 type;
1182 	void *context;
1183 };
1184 
1185 static void alloc_acpiphp_hp_work(acpi_handle handle, u32 type,
1186 				  void *context,
1187 				  void (*func)(struct work_struct *work))
1188 {
1189 	struct acpiphp_hp_work *hp_work;
1190 	int ret;
1191 
1192 	hp_work = kmalloc(sizeof(*hp_work), GFP_KERNEL);
1193 	if (!hp_work)
1194 		return;
1195 
1196 	hp_work->handle = handle;
1197 	hp_work->type = type;
1198 	hp_work->context = context;
1199 
1200 	INIT_WORK(&hp_work->work, func);
1201 	ret = queue_work(kacpi_hotplug_wq, &hp_work->work);
1202 	if (!ret)
1203 		kfree(hp_work);
1204 }
1205 
1206 static void _handle_hotplug_event_bridge(struct work_struct *work)
1207 {
1208 	struct acpiphp_bridge *bridge;
1209 	char objname[64];
1210 	struct acpi_buffer buffer = { .length = sizeof(objname),
1211 				      .pointer = objname };
1212 	struct acpi_device *device;
1213 	int num_sub_bridges = 0;
1214 	struct acpiphp_hp_work *hp_work;
1215 	acpi_handle handle;
1216 	u32 type;
1217 
1218 	hp_work = container_of(work, struct acpiphp_hp_work, work);
1219 	handle = hp_work->handle;
1220 	type = hp_work->type;
1221 
1222 	if (acpi_bus_get_device(handle, &device)) {
1223 		/* This bridge must have just been physically inserted */
1224 		handle_bridge_insertion(handle, type);
1225 		goto out;
1226 	}
1227 
1228 	bridge = acpiphp_handle_to_bridge(handle);
1229 	if (type == ACPI_NOTIFY_BUS_CHECK) {
1230 		acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, ACPI_UINT32_MAX,
1231 			count_sub_bridges, NULL, &num_sub_bridges, NULL);
1232 	}
1233 
1234 	if (!bridge && !num_sub_bridges) {
1235 		err("cannot get bridge info\n");
1236 		goto out;
1237 	}
1238 
1239 	acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1240 
1241 	switch (type) {
1242 	case ACPI_NOTIFY_BUS_CHECK:
1243 		/* bus re-enumerate */
1244 		dbg("%s: Bus check notify on %s\n", __func__, objname);
1245 		if (bridge) {
1246 			dbg("%s: re-enumerating slots under %s\n",
1247 				__func__, objname);
1248 			acpiphp_check_bridge(bridge);
1249 		}
1250 		if (num_sub_bridges)
1251 			acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
1252 				ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
1253 		break;
1254 
1255 	case ACPI_NOTIFY_DEVICE_CHECK:
1256 		/* device check */
1257 		dbg("%s: Device check notify on %s\n", __func__, objname);
1258 		acpiphp_check_bridge(bridge);
1259 		break;
1260 
1261 	case ACPI_NOTIFY_DEVICE_WAKE:
1262 		/* wake event */
1263 		dbg("%s: Device wake notify on %s\n", __func__, objname);
1264 		break;
1265 
1266 	case ACPI_NOTIFY_EJECT_REQUEST:
1267 		/* request device eject */
1268 		dbg("%s: Device eject notify on %s\n", __func__, objname);
1269 		if ((bridge->type != BRIDGE_TYPE_HOST) &&
1270 		    (bridge->flags & BRIDGE_HAS_EJ0)) {
1271 			struct acpiphp_slot *slot;
1272 			slot = bridge->func->slot;
1273 			if (!acpiphp_disable_slot(slot))
1274 				acpiphp_eject_slot(slot);
1275 		}
1276 		break;
1277 
1278 	case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1279 		printk(KERN_ERR "Device %s cannot be configured due"
1280 				" to a frequency mismatch\n", objname);
1281 		break;
1282 
1283 	case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1284 		printk(KERN_ERR "Device %s cannot be configured due"
1285 				" to a bus mode mismatch\n", objname);
1286 		break;
1287 
1288 	case ACPI_NOTIFY_POWER_FAULT:
1289 		printk(KERN_ERR "Device %s has suffered a power fault\n",
1290 				objname);
1291 		break;
1292 
1293 	default:
1294 		warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1295 		break;
1296 	}
1297 
1298 out:
1299 	kfree(hp_work); /* allocated in handle_hotplug_event_bridge */
1300 }
1301 
1302 /**
1303  * handle_hotplug_event_bridge - handle ACPI event on bridges
1304  * @handle: Notify()'ed acpi_handle
1305  * @type: Notify code
1306  * @context: pointer to acpiphp_bridge structure
1307  *
1308  * Handles ACPI event notification on {host,p2p} bridges.
1309  */
1310 static void handle_hotplug_event_bridge(acpi_handle handle, u32 type,
1311 					void *context)
1312 {
1313 	/*
1314 	 * Currently the code adds all hotplug events to the kacpid_wq
1315 	 * queue when it should add hotplug events to the kacpi_hotplug_wq.
1316 	 * The proper way to fix this is to reorganize the code so that
1317 	 * drivers (dock, etc.) do not call acpi_os_execute(), etc.
1318 	 * For now just re-add this work to the kacpi_hotplug_wq so we
1319 	 * don't deadlock on hotplug actions.
1320 	 */
1321 	alloc_acpiphp_hp_work(handle, type, context,
1322 			      _handle_hotplug_event_bridge);
1323 }
1324 
1325 static void _handle_hotplug_event_func(struct work_struct *work)
1326 {
1327 	struct acpiphp_func *func;
1328 	char objname[64];
1329 	struct acpi_buffer buffer = { .length = sizeof(objname),
1330 				      .pointer = objname };
1331 	struct acpiphp_hp_work *hp_work;
1332 	acpi_handle handle;
1333 	u32 type;
1334 	void *context;
1335 
1336 	hp_work = container_of(work, struct acpiphp_hp_work, work);
1337 	handle = hp_work->handle;
1338 	type = hp_work->type;
1339 	context = hp_work->context;
1340 
1341 	acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1342 
1343 	func = (struct acpiphp_func *)context;
1344 
1345 	switch (type) {
1346 	case ACPI_NOTIFY_BUS_CHECK:
1347 		/* bus re-enumerate */
1348 		dbg("%s: Bus check notify on %s\n", __func__, objname);
1349 		acpiphp_enable_slot(func->slot);
1350 		break;
1351 
1352 	case ACPI_NOTIFY_DEVICE_CHECK:
1353 		/* device check : re-enumerate from parent bus */
1354 		dbg("%s: Device check notify on %s\n", __func__, objname);
1355 		acpiphp_check_bridge(func->slot->bridge);
1356 		break;
1357 
1358 	case ACPI_NOTIFY_DEVICE_WAKE:
1359 		/* wake event */
1360 		dbg("%s: Device wake notify on %s\n", __func__, objname);
1361 		break;
1362 
1363 	case ACPI_NOTIFY_EJECT_REQUEST:
1364 		/* request device eject */
1365 		dbg("%s: Device eject notify on %s\n", __func__, objname);
1366 		if (!(acpiphp_disable_slot(func->slot)))
1367 			acpiphp_eject_slot(func->slot);
1368 		break;
1369 
1370 	default:
1371 		warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1372 		break;
1373 	}
1374 
1375 	kfree(hp_work); /* allocated in handle_hotplug_event_func */
1376 }
1377 
1378 /**
1379  * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
1380  * @handle: Notify()'ed acpi_handle
1381  * @type: Notify code
1382  * @context: pointer to acpiphp_func structure
1383  *
1384  * Handles ACPI event notification on slots.
1385  */
1386 static void handle_hotplug_event_func(acpi_handle handle, u32 type,
1387 				      void *context)
1388 {
1389 	/*
1390 	 * Currently the code adds all hotplug events to the kacpid_wq
1391 	 * queue when it should add hotplug events to the kacpi_hotplug_wq.
1392 	 * The proper way to fix this is to reorganize the code so that
1393 	 * drivers (dock, etc.) do not call acpi_os_execute(), etc.
1394 	 * For now just re-add this work to the kacpi_hotplug_wq so we
1395 	 * don't deadlock on hotplug actions.
1396 	 */
1397 	alloc_acpiphp_hp_work(handle, type, context,
1398 			      _handle_hotplug_event_func);
1399 }
1400 
1401 static acpi_status
1402 find_root_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1403 {
1404 	int *count = (int *)context;
1405 
1406 	if (!acpi_is_root_bridge(handle))
1407 		return AE_OK;
1408 
1409 	(*count)++;
1410 	acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1411 				    handle_hotplug_event_bridge, NULL);
1412 
1413 	return AE_OK ;
1414 }
1415 
1416 static struct acpi_pci_driver acpi_pci_hp_driver = {
1417 	.add =		add_bridge,
1418 	.remove =	remove_bridge,
1419 };
1420 
1421 /**
1422  * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
1423  */
1424 int __init acpiphp_glue_init(void)
1425 {
1426 	int num = 0;
1427 
1428 	acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
1429 			ACPI_UINT32_MAX, find_root_bridges, NULL, &num, NULL);
1430 
1431 	if (num <= 0)
1432 		return -1;
1433 	else
1434 		acpi_pci_register_driver(&acpi_pci_hp_driver);
1435 
1436 	return 0;
1437 }
1438 
1439 
1440 /**
1441  * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
1442  *
1443  * This function frees all data allocated in acpiphp_glue_init().
1444  */
1445 void  acpiphp_glue_exit(void)
1446 {
1447 	acpi_pci_unregister_driver(&acpi_pci_hp_driver);
1448 }
1449 
1450 
1451 /**
1452  * acpiphp_get_num_slots - count number of slots in a system
1453  */
1454 int __init acpiphp_get_num_slots(void)
1455 {
1456 	struct acpiphp_bridge *bridge;
1457 	int num_slots = 0;
1458 
1459 	list_for_each_entry(bridge, &bridge_list, list) {
1460 		dbg("Bus %04x:%02x has %d slot%s\n",
1461 				pci_domain_nr(bridge->pci_bus),
1462 				bridge->pci_bus->number, bridge->nr_slots,
1463 				bridge->nr_slots == 1 ? "" : "s");
1464 		num_slots += bridge->nr_slots;
1465 	}
1466 
1467 	dbg("Total %d slots\n", num_slots);
1468 	return num_slots;
1469 }
1470 
1471 
1472 /**
1473  * acpiphp_enable_slot - power on slot
1474  * @slot: ACPI PHP slot
1475  */
1476 int acpiphp_enable_slot(struct acpiphp_slot *slot)
1477 {
1478 	int retval;
1479 
1480 	mutex_lock(&slot->crit_sect);
1481 
1482 	/* wake up all functions */
1483 	retval = power_on_slot(slot);
1484 	if (retval)
1485 		goto err_exit;
1486 
1487 	if (get_slot_status(slot) == ACPI_STA_ALL) {
1488 		/* configure all functions */
1489 		retval = enable_device(slot);
1490 		if (retval)
1491 			power_off_slot(slot);
1492 	} else {
1493 		dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
1494 		power_off_slot(slot);
1495 	}
1496 
1497  err_exit:
1498 	mutex_unlock(&slot->crit_sect);
1499 	return retval;
1500 }
1501 
1502 /**
1503  * acpiphp_disable_slot - power off slot
1504  * @slot: ACPI PHP slot
1505  */
1506 int acpiphp_disable_slot(struct acpiphp_slot *slot)
1507 {
1508 	int retval = 0;
1509 
1510 	mutex_lock(&slot->crit_sect);
1511 
1512 	/* unconfigure all functions */
1513 	retval = disable_device(slot);
1514 	if (retval)
1515 		goto err_exit;
1516 
1517 	/* power off all functions */
1518 	retval = power_off_slot(slot);
1519 	if (retval)
1520 		goto err_exit;
1521 
1522  err_exit:
1523 	mutex_unlock(&slot->crit_sect);
1524 	return retval;
1525 }
1526 
1527 
1528 /*
1529  * slot enabled:  1
1530  * slot disabled: 0
1531  */
1532 u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1533 {
1534 	return (slot->flags & SLOT_POWEREDON);
1535 }
1536 
1537 
1538 /*
1539  * latch   open:  1
1540  * latch closed:  0
1541  */
1542 u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1543 {
1544 	unsigned int sta;
1545 
1546 	sta = get_slot_status(slot);
1547 
1548 	return (sta & ACPI_STA_SHOW_IN_UI) ? 0 : 1;
1549 }
1550 
1551 
1552 /*
1553  * adapter presence : 1
1554  *          absence : 0
1555  */
1556 u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1557 {
1558 	unsigned int sta;
1559 
1560 	sta = get_slot_status(slot);
1561 
1562 	return (sta == 0) ? 0 : 1;
1563 }
1564