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