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  *
10  * All rights reserved.
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or (at
15  * your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful, but
18  * WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
20  * NON INFRINGEMENT.  See the GNU General Public License for more
21  * details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26  *
27  * Send feedback to <t-kochi@bq.jp.nec.com>
28  *
29  */
30 
31 /*
32  * Lifetime rules for pci_dev:
33  *  - The one in acpiphp_func has its refcount elevated by pci_get_slot()
34  *    when the driver is loaded or when an insertion event occurs.  It loses
35  *    a refcount when its ejected or the driver unloads.
36  *  - The one in acpiphp_bridge has its refcount elevated by pci_get_slot()
37  *    when the bridge is scanned and it loses a refcount when the bridge
38  *    is removed.
39  */
40 
41 #include <linux/init.h>
42 #include <linux/module.h>
43 
44 #include <linux/kernel.h>
45 #include <linux/pci.h>
46 #include <linux/smp_lock.h>
47 #include <asm/semaphore.h>
48 
49 #include "../pci.h"
50 #include "pci_hotplug.h"
51 #include "acpiphp.h"
52 
53 static LIST_HEAD(bridge_list);
54 
55 #define MY_NAME "acpiphp_glue"
56 
57 static void handle_hotplug_event_bridge (acpi_handle, u32, void *);
58 static void handle_hotplug_event_func (acpi_handle, u32, void *);
59 
60 /*
61  * initialization & terminatation routines
62  */
63 
64 /**
65  * is_ejectable - determine if a slot is ejectable
66  * @handle: handle to acpi namespace
67  *
68  * Ejectable slot should satisfy at least these conditions:
69  *
70  *  1. has _ADR method
71  *  2. has _EJ0 method
72  *
73  * optionally
74  *
75  *  1. has _STA method
76  *  2. has _PS0 method
77  *  3. has _PS3 method
78  *  4. ..
79  *
80  */
81 static int is_ejectable(acpi_handle handle)
82 {
83 	acpi_status status;
84 	acpi_handle tmp;
85 
86 	status = acpi_get_handle(handle, "_ADR", &tmp);
87 	if (ACPI_FAILURE(status)) {
88 		return 0;
89 	}
90 
91 	status = acpi_get_handle(handle, "_EJ0", &tmp);
92 	if (ACPI_FAILURE(status)) {
93 		return 0;
94 	}
95 
96 	return 1;
97 }
98 
99 
100 /* callback routine to check the existence of ejectable slots */
101 static acpi_status
102 is_ejectable_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
103 {
104 	int *count = (int *)context;
105 
106 	if (is_ejectable(handle)) {
107 		(*count)++;
108 		/* only one ejectable slot is enough */
109 		return AE_CTRL_TERMINATE;
110 	} else {
111 		return AE_OK;
112 	}
113 }
114 
115 
116 /* callback routine to register each ACPI PCI slot object */
117 static acpi_status
118 register_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
119 {
120 	struct acpiphp_bridge *bridge = (struct acpiphp_bridge *)context;
121 	struct acpiphp_slot *slot;
122 	struct acpiphp_func *newfunc;
123 	acpi_handle tmp;
124 	acpi_status status = AE_OK;
125 	unsigned long adr, sun;
126 	int device, function;
127 	static int num_slots = 0;	/* XXX if we support I/O node hotplug... */
128 
129 	status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
130 
131 	if (ACPI_FAILURE(status))
132 		return AE_OK;
133 
134 	status = acpi_get_handle(handle, "_EJ0", &tmp);
135 
136 	if (ACPI_FAILURE(status))
137 		return AE_OK;
138 
139 	device = (adr >> 16) & 0xffff;
140 	function = adr & 0xffff;
141 
142 	newfunc = kmalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
143 	if (!newfunc)
144 		return AE_NO_MEMORY;
145 	memset(newfunc, 0, sizeof(struct acpiphp_func));
146 
147 	INIT_LIST_HEAD(&newfunc->sibling);
148 	newfunc->handle = handle;
149 	newfunc->function = function;
150 	newfunc->flags = FUNC_HAS_EJ0;
151 
152 	if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp)))
153 		newfunc->flags |= FUNC_HAS_STA;
154 
155 	if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp)))
156 		newfunc->flags |= FUNC_HAS_PS0;
157 
158 	if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp)))
159 		newfunc->flags |= FUNC_HAS_PS3;
160 
161 	status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
162 	if (ACPI_FAILURE(status))
163 		sun = -1;
164 
165 	/* search for objects that share the same slot */
166 	for (slot = bridge->slots; slot; slot = slot->next)
167 		if (slot->device == device) {
168 			if (slot->sun != sun)
169 				warn("sibling found, but _SUN doesn't match!\n");
170 			break;
171 		}
172 
173 	if (!slot) {
174 		slot = kmalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
175 		if (!slot) {
176 			kfree(newfunc);
177 			return AE_NO_MEMORY;
178 		}
179 
180 		memset(slot, 0, sizeof(struct acpiphp_slot));
181 		slot->bridge = bridge;
182 		slot->id = num_slots++;
183 		slot->device = device;
184 		slot->sun = sun;
185 		INIT_LIST_HEAD(&slot->funcs);
186 		init_MUTEX(&slot->crit_sect);
187 
188 		slot->next = bridge->slots;
189 		bridge->slots = slot;
190 
191 		bridge->nr_slots++;
192 
193 		dbg("found ACPI PCI Hotplug slot %d at PCI %04x:%02x:%02x\n",
194 				slot->sun, pci_domain_nr(bridge->pci_bus),
195 				bridge->pci_bus->number, slot->device);
196 	}
197 
198 	newfunc->slot = slot;
199 	list_add_tail(&newfunc->sibling, &slot->funcs);
200 
201 	/* associate corresponding pci_dev */
202 	newfunc->pci_dev = pci_get_slot(bridge->pci_bus,
203 					 PCI_DEVFN(device, function));
204 	if (newfunc->pci_dev) {
205 		slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
206 	}
207 
208 	/* install notify handler */
209 	status = acpi_install_notify_handler(handle,
210 					     ACPI_SYSTEM_NOTIFY,
211 					     handle_hotplug_event_func,
212 					     newfunc);
213 
214 	if (ACPI_FAILURE(status)) {
215 		err("failed to register interrupt notify handler\n");
216 		return status;
217 	}
218 
219 	return AE_OK;
220 }
221 
222 
223 /* see if it's worth looking at this bridge */
224 static int detect_ejectable_slots(acpi_handle *bridge_handle)
225 {
226 	acpi_status status;
227 	int count;
228 
229 	count = 0;
230 
231 	/* only check slots defined directly below bridge object */
232 	status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge_handle, (u32)1,
233 				     is_ejectable_slot, (void *)&count, NULL);
234 
235 	return count;
236 }
237 
238 
239 /* decode ACPI 2.0 _HPP hot plug parameters */
240 static void decode_hpp(struct acpiphp_bridge *bridge)
241 {
242 	acpi_status status;
243 	struct acpi_buffer buffer = { .length = ACPI_ALLOCATE_BUFFER,
244 				      .pointer = NULL};
245 	union acpi_object *package;
246 	int i;
247 
248 	/* default numbers */
249 	bridge->hpp.cache_line_size = 0x10;
250 	bridge->hpp.latency_timer = 0x40;
251 	bridge->hpp.enable_SERR = 0;
252 	bridge->hpp.enable_PERR = 0;
253 
254 	status = acpi_evaluate_object(bridge->handle, "_HPP", NULL, &buffer);
255 
256 	if (ACPI_FAILURE(status)) {
257 		dbg("_HPP evaluation failed\n");
258 		return;
259 	}
260 
261 	package = (union acpi_object *) buffer.pointer;
262 
263 	if (!package || package->type != ACPI_TYPE_PACKAGE ||
264 	    package->package.count != 4 || !package->package.elements) {
265 		err("invalid _HPP object; ignoring\n");
266 		goto err_exit;
267 	}
268 
269 	for (i = 0; i < 4; i++) {
270 		if (package->package.elements[i].type != ACPI_TYPE_INTEGER) {
271 			err("invalid _HPP parameter type; ignoring\n");
272 			goto err_exit;
273 		}
274 	}
275 
276 	bridge->hpp.cache_line_size = package->package.elements[0].integer.value;
277 	bridge->hpp.latency_timer = package->package.elements[1].integer.value;
278 	bridge->hpp.enable_SERR = package->package.elements[2].integer.value;
279 	bridge->hpp.enable_PERR = package->package.elements[3].integer.value;
280 
281 	dbg("_HPP parameter = (%02x, %02x, %02x, %02x)\n",
282 		bridge->hpp.cache_line_size,
283 		bridge->hpp.latency_timer,
284 		bridge->hpp.enable_SERR,
285 		bridge->hpp.enable_PERR);
286 
287 	bridge->flags |= BRIDGE_HAS_HPP;
288 
289  err_exit:
290 	kfree(buffer.pointer);
291 }
292 
293 
294 /* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
295 static void init_bridge_misc(struct acpiphp_bridge *bridge)
296 {
297 	acpi_status status;
298 
299 	/* decode ACPI 2.0 _HPP (hot plug parameters) */
300 	decode_hpp(bridge);
301 
302 	/* register all slot objects under this bridge */
303 	status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
304 				     register_slot, bridge, NULL);
305 
306 	/* install notify handler */
307 	status = acpi_install_notify_handler(bridge->handle,
308 					     ACPI_SYSTEM_NOTIFY,
309 					     handle_hotplug_event_bridge,
310 					     bridge);
311 
312 	if (ACPI_FAILURE(status)) {
313 		err("failed to register interrupt notify handler\n");
314 	}
315 
316 	list_add(&bridge->list, &bridge_list);
317 }
318 
319 
320 /* allocate and initialize host bridge data structure */
321 static void add_host_bridge(acpi_handle *handle, struct pci_bus *pci_bus)
322 {
323 	struct acpiphp_bridge *bridge;
324 
325 	bridge = kmalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
326 	if (bridge == NULL)
327 		return;
328 
329 	memset(bridge, 0, sizeof(struct acpiphp_bridge));
330 
331 	bridge->type = BRIDGE_TYPE_HOST;
332 	bridge->handle = handle;
333 
334 	bridge->pci_bus = pci_bus;
335 
336 	spin_lock_init(&bridge->res_lock);
337 
338 	init_bridge_misc(bridge);
339 }
340 
341 
342 /* allocate and initialize PCI-to-PCI bridge data structure */
343 static void add_p2p_bridge(acpi_handle *handle, struct pci_dev *pci_dev)
344 {
345 	struct acpiphp_bridge *bridge;
346 
347 	bridge = kmalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
348 	if (bridge == NULL) {
349 		err("out of memory\n");
350 		return;
351 	}
352 
353 	memset(bridge, 0, sizeof(struct acpiphp_bridge));
354 
355 	bridge->type = BRIDGE_TYPE_P2P;
356 	bridge->handle = handle;
357 
358 	bridge->pci_dev = pci_dev_get(pci_dev);
359 	bridge->pci_bus = pci_dev->subordinate;
360 	if (!bridge->pci_bus) {
361 		err("This is not a PCI-to-PCI bridge!\n");
362 		goto err;
363 	}
364 
365 	spin_lock_init(&bridge->res_lock);
366 
367 	init_bridge_misc(bridge);
368 	return;
369  err:
370 	pci_dev_put(pci_dev);
371 	kfree(bridge);
372 	return;
373 }
374 
375 
376 /* callback routine to find P2P bridges */
377 static acpi_status
378 find_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
379 {
380 	acpi_status status;
381 	acpi_handle dummy_handle;
382 	unsigned long tmp;
383 	int device, function;
384 	struct pci_dev *dev;
385 	struct pci_bus *pci_bus = context;
386 
387 	status = acpi_get_handle(handle, "_ADR", &dummy_handle);
388 	if (ACPI_FAILURE(status))
389 		return AE_OK;		/* continue */
390 
391 	status = acpi_evaluate_integer(handle, "_ADR", NULL, &tmp);
392 	if (ACPI_FAILURE(status)) {
393 		dbg("%s: _ADR evaluation failure\n", __FUNCTION__);
394 		return AE_OK;
395 	}
396 
397 	device = (tmp >> 16) & 0xffff;
398 	function = tmp & 0xffff;
399 
400 	dev = pci_get_slot(pci_bus, PCI_DEVFN(device, function));
401 
402 	if (!dev || !dev->subordinate)
403 		goto out;
404 
405 	/* check if this bridge has ejectable slots */
406 	if (detect_ejectable_slots(handle) > 0) {
407 		dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev));
408 		add_p2p_bridge(handle, dev);
409 	}
410 
411  out:
412 	pci_dev_put(dev);
413 	return AE_OK;
414 }
415 
416 
417 /* find hot-pluggable slots, and then find P2P bridge */
418 static int add_bridge(acpi_handle handle)
419 {
420 	acpi_status status;
421 	unsigned long tmp;
422 	int seg, bus;
423 	acpi_handle dummy_handle;
424 	struct pci_bus *pci_bus;
425 
426 	/* if the bridge doesn't have _STA, we assume it is always there */
427 	status = acpi_get_handle(handle, "_STA", &dummy_handle);
428 	if (ACPI_SUCCESS(status)) {
429 		status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
430 		if (ACPI_FAILURE(status)) {
431 			dbg("%s: _STA evaluation failure\n", __FUNCTION__);
432 			return 0;
433 		}
434 		if ((tmp & ACPI_STA_FUNCTIONING) == 0)
435 			/* don't register this object */
436 			return 0;
437 	}
438 
439 	/* get PCI segment number */
440 	status = acpi_evaluate_integer(handle, "_SEG", NULL, &tmp);
441 
442 	seg = ACPI_SUCCESS(status) ? tmp : 0;
443 
444 	/* get PCI bus number */
445 	status = acpi_evaluate_integer(handle, "_BBN", NULL, &tmp);
446 
447 	if (ACPI_SUCCESS(status)) {
448 		bus = tmp;
449 	} else {
450 		warn("can't get bus number, assuming 0\n");
451 		bus = 0;
452 	}
453 
454 	pci_bus = pci_find_bus(seg, bus);
455 	if (!pci_bus) {
456 		err("Can't find bus %04x:%02x\n", seg, bus);
457 		return 0;
458 	}
459 
460 	/* check if this bridge has ejectable slots */
461 	if (detect_ejectable_slots(handle) > 0) {
462 		dbg("found PCI host-bus bridge with hot-pluggable slots\n");
463 		add_host_bridge(handle, pci_bus);
464 		return 0;
465 	}
466 
467 	/* search P2P bridges under this host bridge */
468 	status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
469 				     find_p2p_bridge, pci_bus, NULL);
470 
471 	if (ACPI_FAILURE(status))
472 		warn("find_p2p_bridge faied (error code = 0x%x)\n",status);
473 
474 	return 0;
475 }
476 
477 static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
478 {
479 	struct list_head *head;
480 	list_for_each(head, &bridge_list) {
481 		struct acpiphp_bridge *bridge = list_entry(head,
482 						struct acpiphp_bridge, list);
483 		if (bridge->handle == handle)
484 			return bridge;
485 	}
486 
487 	return NULL;
488 }
489 
490 static void remove_bridge(acpi_handle handle)
491 {
492 	struct list_head *list, *tmp;
493 	struct acpiphp_bridge *bridge;
494 	struct acpiphp_slot *slot;
495 	acpi_status status;
496 
497 	bridge = acpiphp_handle_to_bridge(handle);
498 	if (!bridge) {
499 		err("Could not find bridge for handle %p\n", handle);
500 		return;
501 	}
502 
503 	status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
504 					    handle_hotplug_event_bridge);
505 	if (ACPI_FAILURE(status))
506 		err("failed to remove notify handler\n");
507 
508 	slot = bridge->slots;
509 	while (slot) {
510 		struct acpiphp_slot *next = slot->next;
511 		list_for_each_safe (list, tmp, &slot->funcs) {
512 			struct acpiphp_func *func;
513 			func = list_entry(list, struct acpiphp_func, sibling);
514 			status = acpi_remove_notify_handler(func->handle,
515 						ACPI_SYSTEM_NOTIFY,
516 						handle_hotplug_event_func);
517 			if (ACPI_FAILURE(status))
518 				err("failed to remove notify handler\n");
519 			pci_dev_put(func->pci_dev);
520 			list_del(list);
521 			kfree(func);
522 		}
523 		kfree(slot);
524 		slot = next;
525 	}
526 
527 	pci_dev_put(bridge->pci_dev);
528 	list_del(&bridge->list);
529 	kfree(bridge);
530 }
531 
532 
533 static int power_on_slot(struct acpiphp_slot *slot)
534 {
535 	acpi_status status;
536 	struct acpiphp_func *func;
537 	struct list_head *l;
538 	int retval = 0;
539 
540 	/* if already enabled, just skip */
541 	if (slot->flags & SLOT_POWEREDON)
542 		goto err_exit;
543 
544 	list_for_each (l, &slot->funcs) {
545 		func = list_entry(l, struct acpiphp_func, sibling);
546 
547 		if (func->flags & FUNC_HAS_PS0) {
548 			dbg("%s: executing _PS0\n", __FUNCTION__);
549 			status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
550 			if (ACPI_FAILURE(status)) {
551 				warn("%s: _PS0 failed\n", __FUNCTION__);
552 				retval = -1;
553 				goto err_exit;
554 			} else
555 				break;
556 		}
557 	}
558 
559 	/* TBD: evaluate _STA to check if the slot is enabled */
560 
561 	slot->flags |= SLOT_POWEREDON;
562 
563  err_exit:
564 	return retval;
565 }
566 
567 
568 static int power_off_slot(struct acpiphp_slot *slot)
569 {
570 	acpi_status status;
571 	struct acpiphp_func *func;
572 	struct list_head *l;
573 	struct acpi_object_list arg_list;
574 	union acpi_object arg;
575 
576 	int retval = 0;
577 
578 	/* if already disabled, just skip */
579 	if ((slot->flags & SLOT_POWEREDON) == 0)
580 		goto err_exit;
581 
582 	list_for_each (l, &slot->funcs) {
583 		func = list_entry(l, struct acpiphp_func, sibling);
584 
585 		if (func->pci_dev && (func->flags & FUNC_HAS_PS3)) {
586 			status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
587 			if (ACPI_FAILURE(status)) {
588 				warn("%s: _PS3 failed\n", __FUNCTION__);
589 				retval = -1;
590 				goto err_exit;
591 			} else
592 				break;
593 		}
594 	}
595 
596 	list_for_each (l, &slot->funcs) {
597 		func = list_entry(l, struct acpiphp_func, sibling);
598 
599 		/* We don't want to call _EJ0 on non-existing functions. */
600 		if (func->pci_dev && (func->flags & FUNC_HAS_EJ0)) {
601 			/* _EJ0 method take one argument */
602 			arg_list.count = 1;
603 			arg_list.pointer = &arg;
604 			arg.type = ACPI_TYPE_INTEGER;
605 			arg.integer.value = 1;
606 
607 			status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
608 			if (ACPI_FAILURE(status)) {
609 				warn("%s: _EJ0 failed\n", __FUNCTION__);
610 				retval = -1;
611 				goto err_exit;
612 			} else
613 				break;
614 		}
615 	}
616 
617 	/* TBD: evaluate _STA to check if the slot is disabled */
618 
619 	slot->flags &= (~SLOT_POWEREDON);
620 
621  err_exit:
622 	return retval;
623 }
624 
625 
626 /**
627  * enable_device - enable, configure a slot
628  * @slot: slot to be enabled
629  *
630  * This function should be called per *physical slot*,
631  * not per each slot object in ACPI namespace.
632  *
633  */
634 static int enable_device(struct acpiphp_slot *slot)
635 {
636 	struct pci_dev *dev;
637 	struct pci_bus *bus = slot->bridge->pci_bus;
638 	struct list_head *l;
639 	struct acpiphp_func *func;
640 	int retval = 0;
641 	int num, max, pass;
642 
643 	if (slot->flags & SLOT_ENABLED)
644 		goto err_exit;
645 
646 	/* sanity check: dev should be NULL when hot-plugged in */
647 	dev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
648 	if (dev) {
649 		/* This case shouldn't happen */
650 		err("pci_dev structure already exists.\n");
651 		pci_dev_put(dev);
652 		retval = -1;
653 		goto err_exit;
654 	}
655 
656 	num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
657 	if (num == 0) {
658 		err("No new device found\n");
659 		retval = -1;
660 		goto err_exit;
661 	}
662 
663 	max = bus->secondary;
664 	for (pass = 0; pass < 2; pass++) {
665 		list_for_each_entry(dev, &bus->devices, bus_list) {
666 			if (PCI_SLOT(dev->devfn) != slot->device)
667 				continue;
668 			if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
669 			    dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
670 				max = pci_scan_bridge(bus, dev, max, pass);
671 		}
672 	}
673 
674 	pci_bus_assign_resources(bus);
675 	pci_bus_add_devices(bus);
676 
677 	/* associate pci_dev to our representation */
678 	list_for_each (l, &slot->funcs) {
679 		func = list_entry(l, struct acpiphp_func, sibling);
680 		func->pci_dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
681 							func->function));
682 	}
683 
684 	slot->flags |= SLOT_ENABLED;
685 
686  err_exit:
687 	return retval;
688 }
689 
690 
691 /**
692  * disable_device - disable a slot
693  */
694 static int disable_device(struct acpiphp_slot *slot)
695 {
696 	int retval = 0;
697 	struct acpiphp_func *func;
698 	struct list_head *l;
699 
700 	/* is this slot already disabled? */
701 	if (!(slot->flags & SLOT_ENABLED))
702 		goto err_exit;
703 
704 	list_for_each (l, &slot->funcs) {
705 		func = list_entry(l, struct acpiphp_func, sibling);
706 		if (!func->pci_dev)
707 			continue;
708 
709 		pci_remove_bus_device(func->pci_dev);
710 		pci_dev_put(func->pci_dev);
711 		func->pci_dev = NULL;
712 	}
713 
714 	slot->flags &= (~SLOT_ENABLED);
715 
716  err_exit:
717 	return retval;
718 }
719 
720 
721 /**
722  * get_slot_status - get ACPI slot status
723  *
724  * if a slot has _STA for each function and if any one of them
725  * returned non-zero status, return it
726  *
727  * if a slot doesn't have _STA and if any one of its functions'
728  * configuration space is configured, return 0x0f as a _STA
729  *
730  * otherwise return 0
731  */
732 static unsigned int get_slot_status(struct acpiphp_slot *slot)
733 {
734 	acpi_status status;
735 	unsigned long sta = 0;
736 	u32 dvid;
737 	struct list_head *l;
738 	struct acpiphp_func *func;
739 
740 	list_for_each (l, &slot->funcs) {
741 		func = list_entry(l, struct acpiphp_func, sibling);
742 
743 		if (func->flags & FUNC_HAS_STA) {
744 			status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
745 			if (ACPI_SUCCESS(status) && sta)
746 				break;
747 		} else {
748 			pci_bus_read_config_dword(slot->bridge->pci_bus,
749 						  PCI_DEVFN(slot->device,
750 							    func->function),
751 						  PCI_VENDOR_ID, &dvid);
752 			if (dvid != 0xffffffff) {
753 				sta = ACPI_STA_ALL;
754 				break;
755 			}
756 		}
757 	}
758 
759 	return (unsigned int)sta;
760 }
761 
762 /**
763  * acpiphp_check_bridge - re-enumerate devices
764  *
765  * Iterate over all slots under this bridge and make sure that if a
766  * card is present they are enabled, and if not they are disabled.
767  */
768 static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
769 {
770 	struct acpiphp_slot *slot;
771 	int retval = 0;
772 	int enabled, disabled;
773 
774 	enabled = disabled = 0;
775 
776 	for (slot = bridge->slots; slot; slot = slot->next) {
777 		unsigned int status = get_slot_status(slot);
778 		if (slot->flags & SLOT_ENABLED) {
779 			if (status == ACPI_STA_ALL)
780 				continue;
781 			retval = acpiphp_disable_slot(slot);
782 			if (retval) {
783 				err("Error occurred in disabling\n");
784 				goto err_exit;
785 			}
786 			disabled++;
787 		} else {
788 			if (status != ACPI_STA_ALL)
789 				continue;
790 			retval = acpiphp_enable_slot(slot);
791 			if (retval) {
792 				err("Error occurred in enabling\n");
793 				goto err_exit;
794 			}
795 			enabled++;
796 		}
797 	}
798 
799 	dbg("%s: %d enabled, %d disabled\n", __FUNCTION__, enabled, disabled);
800 
801  err_exit:
802 	return retval;
803 }
804 
805 /*
806  * ACPI event handlers
807  */
808 
809 /**
810  * handle_hotplug_event_bridge - handle ACPI event on bridges
811  *
812  * @handle: Notify()'ed acpi_handle
813  * @type: Notify code
814  * @context: pointer to acpiphp_bridge structure
815  *
816  * handles ACPI event notification on {host,p2p} bridges
817  *
818  */
819 static void handle_hotplug_event_bridge(acpi_handle handle, u32 type, void *context)
820 {
821 	struct acpiphp_bridge *bridge;
822 	char objname[64];
823 	struct acpi_buffer buffer = { .length = sizeof(objname),
824 				      .pointer = objname };
825 
826 	bridge = (struct acpiphp_bridge *)context;
827 
828 	acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
829 
830 	switch (type) {
831 	case ACPI_NOTIFY_BUS_CHECK:
832 		/* bus re-enumerate */
833 		dbg("%s: Bus check notify on %s\n", __FUNCTION__, objname);
834 		acpiphp_check_bridge(bridge);
835 		break;
836 
837 	case ACPI_NOTIFY_DEVICE_CHECK:
838 		/* device check */
839 		dbg("%s: Device check notify on %s\n", __FUNCTION__, objname);
840 		acpiphp_check_bridge(bridge);
841 		break;
842 
843 	case ACPI_NOTIFY_DEVICE_WAKE:
844 		/* wake event */
845 		dbg("%s: Device wake notify on %s\n", __FUNCTION__, objname);
846 		break;
847 
848 	case ACPI_NOTIFY_EJECT_REQUEST:
849 		/* request device eject */
850 		dbg("%s: Device eject notify on %s\n", __FUNCTION__, objname);
851 		break;
852 
853 	case ACPI_NOTIFY_FREQUENCY_MISMATCH:
854 		printk(KERN_ERR "Device %s cannot be configured due"
855 				" to a frequency mismatch\n", objname);
856 		break;
857 
858 	case ACPI_NOTIFY_BUS_MODE_MISMATCH:
859 		printk(KERN_ERR "Device %s cannot be configured due"
860 				" to a bus mode mismatch\n", objname);
861 		break;
862 
863 	case ACPI_NOTIFY_POWER_FAULT:
864 		printk(KERN_ERR "Device %s has suffered a power fault\n",
865 				objname);
866 		break;
867 
868 	default:
869 		warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
870 		break;
871 	}
872 }
873 
874 
875 /**
876  * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
877  *
878  * @handle: Notify()'ed acpi_handle
879  * @type: Notify code
880  * @context: pointer to acpiphp_func structure
881  *
882  * handles ACPI event notification on slots
883  *
884  */
885 static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context)
886 {
887 	struct acpiphp_func *func;
888 	char objname[64];
889 	struct acpi_buffer buffer = { .length = sizeof(objname),
890 				      .pointer = objname };
891 
892 	acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
893 
894 	func = (struct acpiphp_func *)context;
895 
896 	switch (type) {
897 	case ACPI_NOTIFY_BUS_CHECK:
898 		/* bus re-enumerate */
899 		dbg("%s: Bus check notify on %s\n", __FUNCTION__, objname);
900 		acpiphp_enable_slot(func->slot);
901 		break;
902 
903 	case ACPI_NOTIFY_DEVICE_CHECK:
904 		/* device check : re-enumerate from parent bus */
905 		dbg("%s: Device check notify on %s\n", __FUNCTION__, objname);
906 		acpiphp_check_bridge(func->slot->bridge);
907 		break;
908 
909 	case ACPI_NOTIFY_DEVICE_WAKE:
910 		/* wake event */
911 		dbg("%s: Device wake notify on %s\n", __FUNCTION__, objname);
912 		break;
913 
914 	case ACPI_NOTIFY_EJECT_REQUEST:
915 		/* request device eject */
916 		dbg("%s: Device eject notify on %s\n", __FUNCTION__, objname);
917 		acpiphp_disable_slot(func->slot);
918 		break;
919 
920 	default:
921 		warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
922 		break;
923 	}
924 }
925 
926 
927 static struct acpi_pci_driver acpi_pci_hp_driver = {
928 	.add =		add_bridge,
929 	.remove =	remove_bridge,
930 };
931 
932 /**
933  * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
934  *
935  */
936 int __init acpiphp_glue_init(void)
937 {
938 	int num;
939 
940 	if (list_empty(&pci_root_buses))
941 		return -1;
942 
943 	num = acpi_pci_register_driver(&acpi_pci_hp_driver);
944 
945 	if (num <= 0)
946 		return -1;
947 
948 	return 0;
949 }
950 
951 
952 /**
953  * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
954  *
955  * This function frees all data allocated in acpiphp_glue_init()
956  */
957 void __exit acpiphp_glue_exit(void)
958 {
959 	acpi_pci_unregister_driver(&acpi_pci_hp_driver);
960 }
961 
962 
963 /**
964  * acpiphp_get_num_slots - count number of slots in a system
965  */
966 int __init acpiphp_get_num_slots(void)
967 {
968 	struct list_head *node;
969 	struct acpiphp_bridge *bridge;
970 	int num_slots;
971 
972 	num_slots = 0;
973 
974 	list_for_each (node, &bridge_list) {
975 		bridge = (struct acpiphp_bridge *)node;
976 		dbg("Bus %04x:%02x has %d slot%s\n",
977 				pci_domain_nr(bridge->pci_bus),
978 				bridge->pci_bus->number, bridge->nr_slots,
979 				bridge->nr_slots == 1 ? "" : "s");
980 		num_slots += bridge->nr_slots;
981 	}
982 
983 	dbg("Total %d slots\n", num_slots);
984 	return num_slots;
985 }
986 
987 
988 #if 0
989 /**
990  * acpiphp_for_each_slot - call function for each slot
991  * @fn: callback function
992  * @data: context to be passed to callback function
993  *
994  */
995 static int acpiphp_for_each_slot(acpiphp_callback fn, void *data)
996 {
997 	struct list_head *node;
998 	struct acpiphp_bridge *bridge;
999 	struct acpiphp_slot *slot;
1000 	int retval = 0;
1001 
1002 	list_for_each (node, &bridge_list) {
1003 		bridge = (struct acpiphp_bridge *)node;
1004 		for (slot = bridge->slots; slot; slot = slot->next) {
1005 			retval = fn(slot, data);
1006 			if (!retval)
1007 				goto err_exit;
1008 		}
1009 	}
1010 
1011  err_exit:
1012 	return retval;
1013 }
1014 #endif
1015 
1016 /* search matching slot from id  */
1017 struct acpiphp_slot *get_slot_from_id(int id)
1018 {
1019 	struct list_head *node;
1020 	struct acpiphp_bridge *bridge;
1021 	struct acpiphp_slot *slot;
1022 
1023 	list_for_each (node, &bridge_list) {
1024 		bridge = (struct acpiphp_bridge *)node;
1025 		for (slot = bridge->slots; slot; slot = slot->next)
1026 			if (slot->id == id)
1027 				return slot;
1028 	}
1029 
1030 	/* should never happen! */
1031 	err("%s: no object for id %d\n", __FUNCTION__, id);
1032 	WARN_ON(1);
1033 	return NULL;
1034 }
1035 
1036 
1037 /**
1038  * acpiphp_enable_slot - power on slot
1039  */
1040 int acpiphp_enable_slot(struct acpiphp_slot *slot)
1041 {
1042 	int retval;
1043 
1044 	down(&slot->crit_sect);
1045 
1046 	/* wake up all functions */
1047 	retval = power_on_slot(slot);
1048 	if (retval)
1049 		goto err_exit;
1050 
1051 	if (get_slot_status(slot) == ACPI_STA_ALL)
1052 		/* configure all functions */
1053 		retval = enable_device(slot);
1054 
1055  err_exit:
1056 	up(&slot->crit_sect);
1057 	return retval;
1058 }
1059 
1060 
1061 /**
1062  * acpiphp_disable_slot - power off slot
1063  */
1064 int acpiphp_disable_slot(struct acpiphp_slot *slot)
1065 {
1066 	int retval = 0;
1067 
1068 	down(&slot->crit_sect);
1069 
1070 	/* unconfigure all functions */
1071 	retval = disable_device(slot);
1072 	if (retval)
1073 		goto err_exit;
1074 
1075 	/* power off all functions */
1076 	retval = power_off_slot(slot);
1077 	if (retval)
1078 		goto err_exit;
1079 
1080  err_exit:
1081 	up(&slot->crit_sect);
1082 	return retval;
1083 }
1084 
1085 
1086 /*
1087  * slot enabled:  1
1088  * slot disabled: 0
1089  */
1090 u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1091 {
1092 	unsigned int sta;
1093 
1094 	sta = get_slot_status(slot);
1095 
1096 	return (sta & ACPI_STA_ENABLED) ? 1 : 0;
1097 }
1098 
1099 
1100 /*
1101  * latch closed:  1
1102  * latch   open:  0
1103  */
1104 u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1105 {
1106 	unsigned int sta;
1107 
1108 	sta = get_slot_status(slot);
1109 
1110 	return (sta & ACPI_STA_SHOW_IN_UI) ? 1 : 0;
1111 }
1112 
1113 
1114 /*
1115  * adapter presence : 1
1116  *          absence : 0
1117  */
1118 u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1119 {
1120 	unsigned int sta;
1121 
1122 	sta = get_slot_status(slot);
1123 
1124 	return (sta == 0) ? 0 : 1;
1125 }
1126 
1127 
1128 /*
1129  * pci address (seg/bus/dev)
1130  */
1131 u32 acpiphp_get_address(struct acpiphp_slot *slot)
1132 {
1133 	u32 address;
1134 	struct pci_bus *pci_bus = slot->bridge->pci_bus;
1135 
1136 	address = (pci_domain_nr(pci_bus) << 16) |
1137 		  (pci_bus->number << 8) |
1138 		  slot->device;
1139 
1140 	return address;
1141 }
1142