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