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