1 /*
2  * ACPI PCI HotPlug glue functions to ACPI CA subsystem
3  *
4  * Copyright (C) 2002,2003 Takayoshi Kochi (t-kochi@bq.jp.nec.com)
5  * Copyright (C) 2002 Hiroshi Aono (h-aono@ap.jp.nec.com)
6  * Copyright (C) 2002,2003 NEC Corporation
7  * Copyright (C) 2003-2005 Matthew Wilcox (matthew.wilcox@hp.com)
8  * Copyright (C) 2003-2005 Hewlett Packard
9  * Copyright (C) 2005 Rajesh Shah (rajesh.shah@intel.com)
10  * Copyright (C) 2005 Intel Corporation
11  *
12  * All rights reserved.
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or (at
17  * your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful, but
20  * WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
22  * NON INFRINGEMENT.  See the GNU General Public License for more
23  * details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28  *
29  * Send feedback to <kristen.c.accardi@intel.com>
30  *
31  */
32 
33 /*
34  * Lifetime rules for pci_dev:
35  *  - The one in acpiphp_bridge has its refcount elevated by pci_get_slot()
36  *    when the bridge is scanned and it loses a refcount when the bridge
37  *    is removed.
38  *  - When a P2P bridge is present, we elevate the refcount on the subordinate
39  *    bus. It loses the refcount when the the driver unloads.
40  */
41 
42 #include <linux/init.h>
43 #include <linux/module.h>
44 
45 #include <linux/kernel.h>
46 #include <linux/pci.h>
47 #include <linux/pci_hotplug.h>
48 #include <linux/pci-acpi.h>
49 #include <linux/mutex.h>
50 #include <linux/slab.h>
51 #include <linux/acpi.h>
52 
53 #include "../pci.h"
54 #include "acpiphp.h"
55 
56 static LIST_HEAD(bridge_list);
57 static DEFINE_MUTEX(bridge_mutex);
58 
59 #define MY_NAME "acpiphp_glue"
60 
61 static void handle_hotplug_event_bridge (acpi_handle, u32, void *);
62 static void acpiphp_sanitize_bus(struct pci_bus *bus);
63 static void acpiphp_set_hpp_values(struct pci_bus *bus);
64 static void hotplug_event_func(acpi_handle handle, u32 type, void *context);
65 static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context);
66 static void free_bridge(struct kref *kref);
67 
68 /* callback routine to check for the existence of a pci dock device */
69 static acpi_status
70 is_pci_dock_device(acpi_handle handle, u32 lvl, void *context, void **rv)
71 {
72 	int *count = (int *)context;
73 
74 	if (is_dock_device(handle)) {
75 		(*count)++;
76 		return AE_CTRL_TERMINATE;
77 	} else {
78 		return AE_OK;
79 	}
80 }
81 
82 static inline void get_bridge(struct acpiphp_bridge *bridge)
83 {
84 	kref_get(&bridge->ref);
85 }
86 
87 static inline void put_bridge(struct acpiphp_bridge *bridge)
88 {
89 	kref_put(&bridge->ref, free_bridge);
90 }
91 
92 static void free_bridge(struct kref *kref)
93 {
94 	struct acpiphp_bridge *bridge;
95 	struct acpiphp_slot *slot, *next;
96 	struct acpiphp_func *func, *tmp;
97 
98 	bridge = container_of(kref, struct acpiphp_bridge, ref);
99 
100 	list_for_each_entry_safe(slot, next, &bridge->slots, node) {
101 		list_for_each_entry_safe(func, tmp, &slot->funcs, sibling) {
102 			kfree(func);
103 		}
104 		kfree(slot);
105 	}
106 
107 	/* Release reference acquired by acpiphp_bridge_handle_to_function() */
108 	if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func)
109 		put_bridge(bridge->func->slot->bridge);
110 	put_device(&bridge->pci_bus->dev);
111 	pci_dev_put(bridge->pci_dev);
112 	kfree(bridge);
113 }
114 
115 /*
116  * the _DCK method can do funny things... and sometimes not
117  * hah-hah funny.
118  *
119  * TBD - figure out a way to only call fixups for
120  * systems that require them.
121  */
122 static void post_dock_fixups(acpi_handle not_used, u32 event, void *data)
123 {
124 	struct acpiphp_func *func = data;
125 	struct pci_bus *bus = func->slot->bridge->pci_bus;
126 	u32 buses;
127 
128 	if (!bus->self)
129 		return;
130 
131 	/* fixup bad _DCK function that rewrites
132 	 * secondary bridge on slot
133 	 */
134 	pci_read_config_dword(bus->self,
135 			PCI_PRIMARY_BUS,
136 			&buses);
137 
138 	if (((buses >> 8) & 0xff) != bus->busn_res.start) {
139 		buses = (buses & 0xff000000)
140 			| ((unsigned int)(bus->primary)     <<  0)
141 			| ((unsigned int)(bus->busn_res.start)   <<  8)
142 			| ((unsigned int)(bus->busn_res.end) << 16);
143 		pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses);
144 	}
145 }
146 
147 
148 static const struct acpi_dock_ops acpiphp_dock_ops = {
149 	.fixup = post_dock_fixups,
150 	.handler = hotplug_event_func,
151 };
152 
153 /* Check whether the PCI device is managed by native PCIe hotplug driver */
154 static bool device_is_managed_by_native_pciehp(struct pci_dev *pdev)
155 {
156 	u32 reg32;
157 	acpi_handle tmp;
158 	struct acpi_pci_root *root;
159 
160 	/* Check whether the PCIe port supports native PCIe hotplug */
161 	if (pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, &reg32))
162 		return false;
163 	if (!(reg32 & PCI_EXP_SLTCAP_HPC))
164 		return false;
165 
166 	/*
167 	 * Check whether native PCIe hotplug has been enabled for
168 	 * this PCIe hierarchy.
169 	 */
170 	tmp = acpi_find_root_bridge_handle(pdev);
171 	if (!tmp)
172 		return false;
173 	root = acpi_pci_find_root(tmp);
174 	if (!root)
175 		return false;
176 	if (!(root->osc_control_set & OSC_PCI_EXPRESS_NATIVE_HP_CONTROL))
177 		return false;
178 
179 	return true;
180 }
181 
182 static void acpiphp_dock_init(void *data)
183 {
184 	struct acpiphp_func *func = data;
185 
186 	get_bridge(func->slot->bridge);
187 }
188 
189 static void acpiphp_dock_release(void *data)
190 {
191 	struct acpiphp_func *func = data;
192 
193 	put_bridge(func->slot->bridge);
194 }
195 
196 /* callback routine to register each ACPI PCI slot object */
197 static acpi_status
198 register_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
199 {
200 	struct acpiphp_bridge *bridge = (struct acpiphp_bridge *)context;
201 	struct acpiphp_slot *slot;
202 	struct acpiphp_func *newfunc;
203 	acpi_status status = AE_OK;
204 	unsigned long long adr, sun;
205 	int device, function, retval, found = 0;
206 	struct pci_bus *pbus = bridge->pci_bus;
207 	struct pci_dev *pdev;
208 	u32 val;
209 
210 	if (!acpi_pci_check_ejectable(pbus, handle) && !is_dock_device(handle))
211 		return AE_OK;
212 
213 	status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
214 	if (ACPI_FAILURE(status)) {
215 		warn("can't evaluate _ADR (%#x)\n", status);
216 		return AE_OK;
217 	}
218 
219 	device = (adr >> 16) & 0xffff;
220 	function = adr & 0xffff;
221 
222 	pdev = bridge->pci_dev;
223 	if (pdev && device_is_managed_by_native_pciehp(pdev))
224 		return AE_OK;
225 
226 	newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
227 	if (!newfunc)
228 		return AE_NO_MEMORY;
229 
230 	newfunc->handle = handle;
231 	newfunc->function = function;
232 
233 	if (acpi_has_method(handle, "_EJ0"))
234 		newfunc->flags = FUNC_HAS_EJ0;
235 
236 	if (acpi_has_method(handle, "_STA"))
237 		newfunc->flags |= FUNC_HAS_STA;
238 
239 	if (acpi_has_method(handle, "_PS0"))
240 		newfunc->flags |= FUNC_HAS_PS0;
241 
242 	if (acpi_has_method(handle, "_PS3"))
243 		newfunc->flags |= FUNC_HAS_PS3;
244 
245 	if (acpi_has_method(handle, "_DCK"))
246 		newfunc->flags |= FUNC_HAS_DCK;
247 
248 	status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
249 	if (ACPI_FAILURE(status)) {
250 		/*
251 		 * use the count of the number of slots we've found
252 		 * for the number of the slot
253 		 */
254 		sun = bridge->nr_slots+1;
255 	}
256 
257 	/* search for objects that share the same slot */
258 	list_for_each_entry(slot, &bridge->slots, node)
259 		if (slot->device == device) {
260 			if (slot->sun != sun)
261 				warn("sibling found, but _SUN doesn't match!\n");
262 			found = 1;
263 			break;
264 		}
265 
266 	if (!found) {
267 		slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
268 		if (!slot) {
269 			kfree(newfunc);
270 			return AE_NO_MEMORY;
271 		}
272 
273 		slot->bridge = bridge;
274 		slot->device = device;
275 		slot->sun = sun;
276 		INIT_LIST_HEAD(&slot->funcs);
277 		mutex_init(&slot->crit_sect);
278 
279 		mutex_lock(&bridge_mutex);
280 		list_add_tail(&slot->node, &bridge->slots);
281 		mutex_unlock(&bridge_mutex);
282 		bridge->nr_slots++;
283 
284 		dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
285 		    slot->sun, pci_domain_nr(pbus), pbus->number, device);
286 		retval = acpiphp_register_hotplug_slot(slot);
287 		if (retval) {
288 			if (retval == -EBUSY)
289 				warn("Slot %llu already registered by another "
290 					"hotplug driver\n", slot->sun);
291 			else
292 				warn("acpiphp_register_hotplug_slot failed "
293 					"(err code = 0x%x)\n", retval);
294 			goto err_exit;
295 		}
296 	}
297 
298 	newfunc->slot = slot;
299 	mutex_lock(&bridge_mutex);
300 	list_add_tail(&newfunc->sibling, &slot->funcs);
301 	mutex_unlock(&bridge_mutex);
302 
303 	if (pci_bus_read_dev_vendor_id(pbus, PCI_DEVFN(device, function),
304 				       &val, 60*1000))
305 		slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
306 
307 	if (is_dock_device(handle)) {
308 		/* we don't want to call this device's _EJ0
309 		 * because we want the dock notify handler
310 		 * to call it after it calls _DCK
311 		 */
312 		newfunc->flags &= ~FUNC_HAS_EJ0;
313 		if (register_hotplug_dock_device(handle,
314 			&acpiphp_dock_ops, newfunc,
315 			acpiphp_dock_init, acpiphp_dock_release))
316 			dbg("failed to register dock device\n");
317 	}
318 
319 	/* install notify handler */
320 	if (!(newfunc->flags & FUNC_HAS_DCK)) {
321 		status = acpi_install_notify_handler(handle,
322 					     ACPI_SYSTEM_NOTIFY,
323 					     handle_hotplug_event_func,
324 					     newfunc);
325 
326 		if (ACPI_FAILURE(status))
327 			err("failed to register interrupt notify handler\n");
328 	} else
329 		status = AE_OK;
330 
331 	return status;
332 
333  err_exit:
334 	bridge->nr_slots--;
335 	mutex_lock(&bridge_mutex);
336 	list_del(&slot->node);
337 	mutex_unlock(&bridge_mutex);
338 	kfree(slot);
339 	kfree(newfunc);
340 
341 	return AE_OK;
342 }
343 
344 
345 /* see if it's worth looking at this bridge */
346 static int detect_ejectable_slots(acpi_handle handle)
347 {
348 	int found = acpi_pci_detect_ejectable(handle);
349 	if (!found) {
350 		acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
351 				    is_pci_dock_device, NULL, (void *)&found, NULL);
352 	}
353 	return found;
354 }
355 
356 /* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
357 static void init_bridge_misc(struct acpiphp_bridge *bridge)
358 {
359 	acpi_status status;
360 
361 	/* must be added to the list prior to calling register_slot */
362 	mutex_lock(&bridge_mutex);
363 	list_add(&bridge->list, &bridge_list);
364 	mutex_unlock(&bridge_mutex);
365 
366 	/* register all slot objects under this bridge */
367 	status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
368 				     register_slot, NULL, bridge, NULL);
369 	if (ACPI_FAILURE(status)) {
370 		mutex_lock(&bridge_mutex);
371 		list_del(&bridge->list);
372 		mutex_unlock(&bridge_mutex);
373 		return;
374 	}
375 
376 	/* install notify handler for P2P bridges */
377 	if (!pci_is_root_bus(bridge->pci_bus)) {
378 		if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
379 			status = acpi_remove_notify_handler(bridge->func->handle,
380 						ACPI_SYSTEM_NOTIFY,
381 						handle_hotplug_event_func);
382 			if (ACPI_FAILURE(status))
383 				err("failed to remove notify handler\n");
384 		}
385 		status = acpi_install_notify_handler(bridge->handle,
386 					     ACPI_SYSTEM_NOTIFY,
387 					     handle_hotplug_event_bridge,
388 					     bridge);
389 
390 		if (ACPI_FAILURE(status)) {
391 			err("failed to register interrupt notify handler\n");
392 		}
393 	}
394 }
395 
396 
397 /* find acpiphp_func from acpiphp_bridge */
398 static struct acpiphp_func *acpiphp_bridge_handle_to_function(acpi_handle handle)
399 {
400 	struct acpiphp_bridge *bridge;
401 	struct acpiphp_slot *slot;
402 	struct acpiphp_func *func = NULL;
403 
404 	mutex_lock(&bridge_mutex);
405 	list_for_each_entry(bridge, &bridge_list, list) {
406 		list_for_each_entry(slot, &bridge->slots, node) {
407 			list_for_each_entry(func, &slot->funcs, sibling) {
408 				if (func->handle == handle) {
409 					get_bridge(func->slot->bridge);
410 					mutex_unlock(&bridge_mutex);
411 					return func;
412 				}
413 			}
414 		}
415 	}
416 	mutex_unlock(&bridge_mutex);
417 
418 	return NULL;
419 }
420 
421 
422 static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
423 {
424 	struct acpiphp_bridge *bridge;
425 
426 	mutex_lock(&bridge_mutex);
427 	list_for_each_entry(bridge, &bridge_list, list)
428 		if (bridge->handle == handle) {
429 			get_bridge(bridge);
430 			mutex_unlock(&bridge_mutex);
431 			return bridge;
432 		}
433 	mutex_unlock(&bridge_mutex);
434 
435 	return NULL;
436 }
437 
438 static void cleanup_bridge(struct acpiphp_bridge *bridge)
439 {
440 	struct acpiphp_slot *slot;
441 	struct acpiphp_func *func;
442 	acpi_status status;
443 	acpi_handle handle = bridge->handle;
444 
445 	if (!pci_is_root_bus(bridge->pci_bus)) {
446 		status = acpi_remove_notify_handler(handle,
447 					    ACPI_SYSTEM_NOTIFY,
448 					    handle_hotplug_event_bridge);
449 		if (ACPI_FAILURE(status))
450 			err("failed to remove notify handler\n");
451 	}
452 
453 	if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
454 		status = acpi_install_notify_handler(bridge->func->handle,
455 						ACPI_SYSTEM_NOTIFY,
456 						handle_hotplug_event_func,
457 						bridge->func);
458 		if (ACPI_FAILURE(status))
459 			err("failed to install interrupt notify handler\n");
460 	}
461 
462 	list_for_each_entry(slot, &bridge->slots, node) {
463 		list_for_each_entry(func, &slot->funcs, sibling) {
464 			if (is_dock_device(func->handle)) {
465 				unregister_hotplug_dock_device(func->handle);
466 			}
467 			if (!(func->flags & FUNC_HAS_DCK)) {
468 				status = acpi_remove_notify_handler(func->handle,
469 						ACPI_SYSTEM_NOTIFY,
470 						handle_hotplug_event_func);
471 				if (ACPI_FAILURE(status))
472 					err("failed to remove notify handler\n");
473 			}
474 		}
475 		acpiphp_unregister_hotplug_slot(slot);
476 	}
477 
478 	mutex_lock(&bridge_mutex);
479 	list_del(&bridge->list);
480 	mutex_unlock(&bridge_mutex);
481 }
482 
483 static int power_on_slot(struct acpiphp_slot *slot)
484 {
485 	acpi_status status;
486 	struct acpiphp_func *func;
487 	int retval = 0;
488 
489 	/* if already enabled, just skip */
490 	if (slot->flags & SLOT_POWEREDON)
491 		goto err_exit;
492 
493 	list_for_each_entry(func, &slot->funcs, sibling) {
494 		if (func->flags & FUNC_HAS_PS0) {
495 			dbg("%s: executing _PS0\n", __func__);
496 			status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
497 			if (ACPI_FAILURE(status)) {
498 				warn("%s: _PS0 failed\n", __func__);
499 				retval = -1;
500 				goto err_exit;
501 			} else
502 				break;
503 		}
504 	}
505 
506 	/* TBD: evaluate _STA to check if the slot is enabled */
507 
508 	slot->flags |= SLOT_POWEREDON;
509 
510  err_exit:
511 	return retval;
512 }
513 
514 
515 static int power_off_slot(struct acpiphp_slot *slot)
516 {
517 	acpi_status status;
518 	struct acpiphp_func *func;
519 
520 	int retval = 0;
521 
522 	/* if already disabled, just skip */
523 	if ((slot->flags & SLOT_POWEREDON) == 0)
524 		goto err_exit;
525 
526 	list_for_each_entry(func, &slot->funcs, sibling) {
527 		if (func->flags & FUNC_HAS_PS3) {
528 			status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
529 			if (ACPI_FAILURE(status)) {
530 				warn("%s: _PS3 failed\n", __func__);
531 				retval = -1;
532 				goto err_exit;
533 			} else
534 				break;
535 		}
536 	}
537 
538 	/* TBD: evaluate _STA to check if the slot is disabled */
539 
540 	slot->flags &= (~SLOT_POWEREDON);
541 
542  err_exit:
543 	return retval;
544 }
545 
546 
547 
548 /**
549  * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
550  * @bus: bus to start search with
551  */
552 static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
553 {
554 	struct list_head *tmp;
555 	unsigned char max, n;
556 
557 	/*
558 	 * pci_bus_max_busnr will return the highest
559 	 * reserved busnr for all these children.
560 	 * that is equivalent to the bus->subordinate
561 	 * value.  We don't want to use the parent's
562 	 * bus->subordinate value because it could have
563 	 * padding in it.
564 	 */
565 	max = bus->busn_res.start;
566 
567 	list_for_each(tmp, &bus->children) {
568 		n = pci_bus_max_busnr(pci_bus_b(tmp));
569 		if (n > max)
570 			max = n;
571 	}
572 	return max;
573 }
574 
575 
576 /**
577  * acpiphp_bus_add - add a new bus to acpi subsystem
578  * @func: acpiphp_func of the bridge
579  */
580 static int acpiphp_bus_add(struct acpiphp_func *func)
581 {
582 	struct acpi_device *device;
583 	int ret_val;
584 
585 	if (!acpi_bus_get_device(func->handle, &device)) {
586 		dbg("bus exists... trim\n");
587 		/* this shouldn't be in here, so remove
588 		 * the bus then re-add it...
589 		 */
590 		acpi_bus_trim(device);
591 	}
592 
593 	ret_val = acpi_bus_scan(func->handle);
594 	if (!ret_val)
595 		ret_val = acpi_bus_get_device(func->handle, &device);
596 
597 	if (ret_val)
598 		dbg("error adding bus, %x\n", -ret_val);
599 
600 	return ret_val;
601 }
602 
603 
604 /**
605  * acpiphp_bus_trim - trim a bus from acpi subsystem
606  * @handle: handle to acpi namespace
607  */
608 static int acpiphp_bus_trim(acpi_handle handle)
609 {
610 	struct acpi_device *device;
611 	int retval;
612 
613 	retval = acpi_bus_get_device(handle, &device);
614 	if (retval) {
615 		dbg("acpi_device not found\n");
616 		return retval;
617 	}
618 
619 	acpi_bus_trim(device);
620 	return 0;
621 }
622 
623 static void acpiphp_set_acpi_region(struct acpiphp_slot *slot)
624 {
625 	struct acpiphp_func *func;
626 	union acpi_object params[2];
627 	struct acpi_object_list arg_list;
628 
629 	list_for_each_entry(func, &slot->funcs, sibling) {
630 		arg_list.count = 2;
631 		arg_list.pointer = params;
632 		params[0].type = ACPI_TYPE_INTEGER;
633 		params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG;
634 		params[1].type = ACPI_TYPE_INTEGER;
635 		params[1].integer.value = 1;
636 		/* _REG is optional, we don't care about if there is failure */
637 		acpi_evaluate_object(func->handle, "_REG", &arg_list, NULL);
638 	}
639 }
640 
641 static void check_hotplug_bridge(struct acpiphp_slot *slot, struct pci_dev *dev)
642 {
643 	struct acpiphp_func *func;
644 
645 	if (!dev->subordinate)
646 		return;
647 
648 	/* quirk, or pcie could set it already */
649 	if (dev->is_hotplug_bridge)
650 		return;
651 
652 	if (PCI_SLOT(dev->devfn) != slot->device)
653 		return;
654 
655 	list_for_each_entry(func, &slot->funcs, sibling) {
656 		if (PCI_FUNC(dev->devfn) == func->function) {
657 			/* check if this bridge has ejectable slots */
658 			if ((detect_ejectable_slots(func->handle) > 0))
659 				dev->is_hotplug_bridge = 1;
660 			break;
661 		}
662 	}
663 }
664 
665 /**
666  * enable_device - enable, configure a slot
667  * @slot: slot to be enabled
668  *
669  * This function should be called per *physical slot*,
670  * not per each slot object in ACPI namespace.
671  */
672 static int __ref enable_device(struct acpiphp_slot *slot)
673 {
674 	struct pci_dev *dev;
675 	struct pci_bus *bus = slot->bridge->pci_bus;
676 	struct acpiphp_func *func;
677 	int num, max, pass;
678 	LIST_HEAD(add_list);
679 
680 	if (slot->flags & SLOT_ENABLED)
681 		goto err_exit;
682 
683 	list_for_each_entry(func, &slot->funcs, sibling)
684 		acpiphp_bus_add(func);
685 
686 	num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
687 	if (num == 0) {
688 		/* Maybe only part of funcs are added. */
689 		dbg("No new device found\n");
690 		goto err_exit;
691 	}
692 
693 	max = acpiphp_max_busnr(bus);
694 	for (pass = 0; pass < 2; pass++) {
695 		list_for_each_entry(dev, &bus->devices, bus_list) {
696 			if (PCI_SLOT(dev->devfn) != slot->device)
697 				continue;
698 			if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
699 			    dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
700 				max = pci_scan_bridge(bus, dev, max, pass);
701 				if (pass && dev->subordinate) {
702 					check_hotplug_bridge(slot, dev);
703 					pcibios_resource_survey_bus(dev->subordinate);
704 					__pci_bus_size_bridges(dev->subordinate,
705 							       &add_list);
706 				}
707 			}
708 		}
709 	}
710 
711 	__pci_bus_assign_resources(bus, &add_list, NULL);
712 	acpiphp_sanitize_bus(bus);
713 	acpiphp_set_hpp_values(bus);
714 	acpiphp_set_acpi_region(slot);
715 	pci_enable_bridges(bus);
716 
717 	list_for_each_entry(dev, &bus->devices, bus_list) {
718 		/* Assume that newly added devices are powered on already. */
719 		if (!dev->is_added)
720 			dev->current_state = PCI_D0;
721 	}
722 
723 	pci_bus_add_devices(bus);
724 
725 	slot->flags |= SLOT_ENABLED;
726 	list_for_each_entry(func, &slot->funcs, sibling) {
727 		dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
728 						  func->function));
729 		if (!dev) {
730 			/* Do not set SLOT_ENABLED flag if some funcs
731 			   are not added. */
732 			slot->flags &= (~SLOT_ENABLED);
733 			continue;
734 		}
735 	}
736 
737 
738  err_exit:
739 	return 0;
740 }
741 
742 /* return first device in slot, acquiring a reference on it */
743 static struct pci_dev *dev_in_slot(struct acpiphp_slot *slot)
744 {
745 	struct pci_bus *bus = slot->bridge->pci_bus;
746 	struct pci_dev *dev;
747 	struct pci_dev *ret = NULL;
748 
749 	down_read(&pci_bus_sem);
750 	list_for_each_entry(dev, &bus->devices, bus_list)
751 		if (PCI_SLOT(dev->devfn) == slot->device) {
752 			ret = pci_dev_get(dev);
753 			break;
754 		}
755 	up_read(&pci_bus_sem);
756 
757 	return ret;
758 }
759 
760 /**
761  * disable_device - disable a slot
762  * @slot: ACPI PHP slot
763  */
764 static int disable_device(struct acpiphp_slot *slot)
765 {
766 	struct acpiphp_func *func;
767 	struct pci_dev *pdev;
768 
769 	/*
770 	 * enable_device() enumerates all functions in this device via
771 	 * pci_scan_slot(), whether they have associated ACPI hotplug
772 	 * methods (_EJ0, etc.) or not.  Therefore, we remove all functions
773 	 * here.
774 	 */
775 	while ((pdev = dev_in_slot(slot))) {
776 		pci_stop_and_remove_bus_device(pdev);
777 		pci_dev_put(pdev);
778 	}
779 
780 	list_for_each_entry(func, &slot->funcs, sibling) {
781 		acpiphp_bus_trim(func->handle);
782 	}
783 
784 	slot->flags &= (~SLOT_ENABLED);
785 
786 	return 0;
787 }
788 
789 
790 /**
791  * get_slot_status - get ACPI slot status
792  * @slot: ACPI PHP slot
793  *
794  * If a slot has _STA for each function and if any one of them
795  * returned non-zero status, return it.
796  *
797  * If a slot doesn't have _STA and if any one of its functions'
798  * configuration space is configured, return 0x0f as a _STA.
799  *
800  * Otherwise return 0.
801  */
802 static unsigned int get_slot_status(struct acpiphp_slot *slot)
803 {
804 	acpi_status status;
805 	unsigned long long sta = 0;
806 	u32 dvid;
807 	struct acpiphp_func *func;
808 
809 	list_for_each_entry(func, &slot->funcs, sibling) {
810 		if (func->flags & FUNC_HAS_STA) {
811 			status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
812 			if (ACPI_SUCCESS(status) && sta)
813 				break;
814 		} else {
815 			pci_bus_read_config_dword(slot->bridge->pci_bus,
816 						  PCI_DEVFN(slot->device,
817 							    func->function),
818 						  PCI_VENDOR_ID, &dvid);
819 			if (dvid != 0xffffffff) {
820 				sta = ACPI_STA_ALL;
821 				break;
822 			}
823 		}
824 	}
825 
826 	return (unsigned int)sta;
827 }
828 
829 /**
830  * acpiphp_eject_slot - physically eject the slot
831  * @slot: ACPI PHP slot
832  */
833 int acpiphp_eject_slot(struct acpiphp_slot *slot)
834 {
835 	struct acpiphp_func *func;
836 
837 	list_for_each_entry(func, &slot->funcs, sibling) {
838 		/* We don't want to call _EJ0 on non-existing functions. */
839 		if ((func->flags & FUNC_HAS_EJ0)) {
840 			if (ACPI_FAILURE(acpi_evaluate_ej0(func->handle)))
841 				return -1;
842 			else
843 				break;
844 		}
845 	}
846 	return 0;
847 }
848 
849 /**
850  * acpiphp_check_bridge - re-enumerate devices
851  * @bridge: where to begin re-enumeration
852  *
853  * Iterate over all slots under this bridge and make sure that if a
854  * card is present they are enabled, and if not they are disabled.
855  */
856 static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
857 {
858 	struct acpiphp_slot *slot;
859 	int retval = 0;
860 	int enabled, disabled;
861 
862 	enabled = disabled = 0;
863 
864 	list_for_each_entry(slot, &bridge->slots, node) {
865 		unsigned int status = get_slot_status(slot);
866 		if (slot->flags & SLOT_ENABLED) {
867 			if (status == ACPI_STA_ALL)
868 				continue;
869 			retval = acpiphp_disable_slot(slot);
870 			if (retval) {
871 				err("Error occurred in disabling\n");
872 				goto err_exit;
873 			} else {
874 				acpiphp_eject_slot(slot);
875 			}
876 			disabled++;
877 		} else {
878 			if (status != ACPI_STA_ALL)
879 				continue;
880 			retval = acpiphp_enable_slot(slot);
881 			if (retval) {
882 				err("Error occurred in enabling\n");
883 				goto err_exit;
884 			}
885 			enabled++;
886 		}
887 	}
888 
889 	dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
890 
891  err_exit:
892 	return retval;
893 }
894 
895 static void acpiphp_set_hpp_values(struct pci_bus *bus)
896 {
897 	struct pci_dev *dev;
898 
899 	list_for_each_entry(dev, &bus->devices, bus_list)
900 		pci_configure_slot(dev);
901 }
902 
903 /*
904  * Remove devices for which we could not assign resources, call
905  * arch specific code to fix-up the bus
906  */
907 static void acpiphp_sanitize_bus(struct pci_bus *bus)
908 {
909 	struct pci_dev *dev, *tmp;
910 	int i;
911 	unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
912 
913 	list_for_each_entry_safe(dev, tmp, &bus->devices, bus_list) {
914 		for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
915 			struct resource *res = &dev->resource[i];
916 			if ((res->flags & type_mask) && !res->start &&
917 					res->end) {
918 				/* Could not assign a required resources
919 				 * for this device, remove it */
920 				pci_stop_and_remove_bus_device(dev);
921 				break;
922 			}
923 		}
924 	}
925 }
926 
927 /*
928  * ACPI event handlers
929  */
930 
931 static acpi_status
932 check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
933 {
934 	struct acpiphp_bridge *bridge;
935 	char objname[64];
936 	struct acpi_buffer buffer = { .length = sizeof(objname),
937 				      .pointer = objname };
938 
939 	bridge = acpiphp_handle_to_bridge(handle);
940 	if (bridge) {
941 		acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
942 		dbg("%s: re-enumerating slots under %s\n",
943 			__func__, objname);
944 		acpiphp_check_bridge(bridge);
945 		put_bridge(bridge);
946 	}
947 	return AE_OK ;
948 }
949 
950 void acpiphp_check_host_bridge(acpi_handle handle)
951 {
952 	struct acpiphp_bridge *bridge;
953 
954 	bridge = acpiphp_handle_to_bridge(handle);
955 	if (bridge) {
956 		acpiphp_check_bridge(bridge);
957 		put_bridge(bridge);
958 	}
959 
960 	acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
961 		ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
962 }
963 
964 static void _handle_hotplug_event_bridge(struct work_struct *work)
965 {
966 	struct acpiphp_bridge *bridge;
967 	char objname[64];
968 	struct acpi_buffer buffer = { .length = sizeof(objname),
969 				      .pointer = objname };
970 	struct acpi_hp_work *hp_work;
971 	acpi_handle handle;
972 	u32 type;
973 
974 	hp_work = container_of(work, struct acpi_hp_work, work);
975 	handle = hp_work->handle;
976 	type = hp_work->type;
977 	bridge = (struct acpiphp_bridge *)hp_work->context;
978 
979 	acpi_scan_lock_acquire();
980 
981 	acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
982 
983 	switch (type) {
984 	case ACPI_NOTIFY_BUS_CHECK:
985 		/* bus re-enumerate */
986 		dbg("%s: Bus check notify on %s\n", __func__, objname);
987 		dbg("%s: re-enumerating slots under %s\n", __func__, objname);
988 		acpiphp_check_bridge(bridge);
989 		acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
990 			ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
991 		break;
992 
993 	case ACPI_NOTIFY_DEVICE_CHECK:
994 		/* device check */
995 		dbg("%s: Device check notify on %s\n", __func__, objname);
996 		acpiphp_check_bridge(bridge);
997 		break;
998 
999 	case ACPI_NOTIFY_DEVICE_WAKE:
1000 		/* wake event */
1001 		dbg("%s: Device wake notify on %s\n", __func__, objname);
1002 		break;
1003 
1004 	case ACPI_NOTIFY_EJECT_REQUEST:
1005 		/* request device eject */
1006 		dbg("%s: Device eject notify on %s\n", __func__, objname);
1007 		if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
1008 			struct acpiphp_slot *slot;
1009 			slot = bridge->func->slot;
1010 			if (!acpiphp_disable_slot(slot))
1011 				acpiphp_eject_slot(slot);
1012 		}
1013 		break;
1014 
1015 	case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1016 		printk(KERN_ERR "Device %s cannot be configured due"
1017 				" to a frequency mismatch\n", objname);
1018 		break;
1019 
1020 	case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1021 		printk(KERN_ERR "Device %s cannot be configured due"
1022 				" to a bus mode mismatch\n", objname);
1023 		break;
1024 
1025 	case ACPI_NOTIFY_POWER_FAULT:
1026 		printk(KERN_ERR "Device %s has suffered a power fault\n",
1027 				objname);
1028 		break;
1029 
1030 	default:
1031 		warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1032 		break;
1033 	}
1034 
1035 	acpi_scan_lock_release();
1036 	kfree(hp_work); /* allocated in handle_hotplug_event_bridge */
1037 	put_bridge(bridge);
1038 }
1039 
1040 /**
1041  * handle_hotplug_event_bridge - handle ACPI event on bridges
1042  * @handle: Notify()'ed acpi_handle
1043  * @type: Notify code
1044  * @context: pointer to acpiphp_bridge structure
1045  *
1046  * Handles ACPI event notification on {host,p2p} bridges.
1047  */
1048 static void handle_hotplug_event_bridge(acpi_handle handle, u32 type,
1049 					void *context)
1050 {
1051 	struct acpiphp_bridge *bridge = context;
1052 
1053 	/*
1054 	 * Currently the code adds all hotplug events to the kacpid_wq
1055 	 * queue when it should add hotplug events to the kacpi_hotplug_wq.
1056 	 * The proper way to fix this is to reorganize the code so that
1057 	 * drivers (dock, etc.) do not call acpi_os_execute(), etc.
1058 	 * For now just re-add this work to the kacpi_hotplug_wq so we
1059 	 * don't deadlock on hotplug actions.
1060 	 */
1061 	get_bridge(bridge);
1062 	alloc_acpi_hp_work(handle, type, context, _handle_hotplug_event_bridge);
1063 }
1064 
1065 static void hotplug_event_func(acpi_handle handle, u32 type, void *context)
1066 {
1067 	struct acpiphp_func *func = context;
1068 	char objname[64];
1069 	struct acpi_buffer buffer = { .length = sizeof(objname),
1070 				      .pointer = objname };
1071 
1072 	acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1073 
1074 	switch (type) {
1075 	case ACPI_NOTIFY_BUS_CHECK:
1076 		/* bus re-enumerate */
1077 		dbg("%s: Bus check notify on %s\n", __func__, objname);
1078 		acpiphp_enable_slot(func->slot);
1079 		break;
1080 
1081 	case ACPI_NOTIFY_DEVICE_CHECK:
1082 		/* device check : re-enumerate from parent bus */
1083 		dbg("%s: Device check notify on %s\n", __func__, objname);
1084 		acpiphp_check_bridge(func->slot->bridge);
1085 		break;
1086 
1087 	case ACPI_NOTIFY_DEVICE_WAKE:
1088 		/* wake event */
1089 		dbg("%s: Device wake notify on %s\n", __func__, objname);
1090 		break;
1091 
1092 	case ACPI_NOTIFY_EJECT_REQUEST:
1093 		/* request device eject */
1094 		dbg("%s: Device eject notify on %s\n", __func__, objname);
1095 		if (!(acpiphp_disable_slot(func->slot)))
1096 			acpiphp_eject_slot(func->slot);
1097 		break;
1098 
1099 	default:
1100 		warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1101 		break;
1102 	}
1103 }
1104 
1105 static void _handle_hotplug_event_func(struct work_struct *work)
1106 {
1107 	struct acpi_hp_work *hp_work;
1108 	struct acpiphp_func *func;
1109 
1110 	hp_work = container_of(work, struct acpi_hp_work, work);
1111 	func = hp_work->context;
1112 	acpi_scan_lock_acquire();
1113 
1114 	hotplug_event_func(hp_work->handle, hp_work->type, func);
1115 
1116 	acpi_scan_lock_release();
1117 	kfree(hp_work); /* allocated in handle_hotplug_event_func */
1118 	put_bridge(func->slot->bridge);
1119 }
1120 
1121 /**
1122  * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
1123  * @handle: Notify()'ed acpi_handle
1124  * @type: Notify code
1125  * @context: pointer to acpiphp_func structure
1126  *
1127  * Handles ACPI event notification on slots.
1128  */
1129 static void handle_hotplug_event_func(acpi_handle handle, u32 type,
1130 				      void *context)
1131 {
1132 	struct acpiphp_func *func = context;
1133 
1134 	/*
1135 	 * Currently the code adds all hotplug events to the kacpid_wq
1136 	 * queue when it should add hotplug events to the kacpi_hotplug_wq.
1137 	 * The proper way to fix this is to reorganize the code so that
1138 	 * drivers (dock, etc.) do not call acpi_os_execute(), etc.
1139 	 * For now just re-add this work to the kacpi_hotplug_wq so we
1140 	 * don't deadlock on hotplug actions.
1141 	 */
1142 	get_bridge(func->slot->bridge);
1143 	alloc_acpi_hp_work(handle, type, context, _handle_hotplug_event_func);
1144 }
1145 
1146 /*
1147  * Create hotplug slots for the PCI bus.
1148  * It should always return 0 to avoid skipping following notifiers.
1149  */
1150 void acpiphp_enumerate_slots(struct pci_bus *bus, acpi_handle handle)
1151 {
1152 	struct acpiphp_bridge *bridge;
1153 
1154 	if (acpiphp_disabled)
1155 		return;
1156 
1157 	if (detect_ejectable_slots(handle) <= 0)
1158 		return;
1159 
1160 	bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
1161 	if (bridge == NULL) {
1162 		err("out of memory\n");
1163 		return;
1164 	}
1165 
1166 	INIT_LIST_HEAD(&bridge->slots);
1167 	kref_init(&bridge->ref);
1168 	bridge->handle = handle;
1169 	bridge->pci_dev = pci_dev_get(bus->self);
1170 	bridge->pci_bus = bus;
1171 
1172 	/*
1173 	 * Grab a ref to the subordinate PCI bus in case the bus is
1174 	 * removed via PCI core logical hotplug. The ref pins the bus
1175 	 * (which we access during module unload).
1176 	 */
1177 	get_device(&bus->dev);
1178 
1179 	if (!pci_is_root_bus(bridge->pci_bus) &&
1180 	    acpi_has_method(bridge->handle, "_EJ0")) {
1181 		dbg("found ejectable p2p bridge\n");
1182 		bridge->flags |= BRIDGE_HAS_EJ0;
1183 		bridge->func = acpiphp_bridge_handle_to_function(handle);
1184 	}
1185 
1186 	init_bridge_misc(bridge);
1187 }
1188 
1189 /* Destroy hotplug slots associated with the PCI bus */
1190 void acpiphp_remove_slots(struct pci_bus *bus)
1191 {
1192 	struct acpiphp_bridge *bridge, *tmp;
1193 
1194 	if (acpiphp_disabled)
1195 		return;
1196 
1197 	list_for_each_entry_safe(bridge, tmp, &bridge_list, list)
1198 		if (bridge->pci_bus == bus) {
1199 			cleanup_bridge(bridge);
1200 			put_bridge(bridge);
1201 			break;
1202 		}
1203 }
1204 
1205 /**
1206  * acpiphp_enable_slot - power on slot
1207  * @slot: ACPI PHP slot
1208  */
1209 int acpiphp_enable_slot(struct acpiphp_slot *slot)
1210 {
1211 	int retval;
1212 
1213 	mutex_lock(&slot->crit_sect);
1214 
1215 	/* wake up all functions */
1216 	retval = power_on_slot(slot);
1217 	if (retval)
1218 		goto err_exit;
1219 
1220 	if (get_slot_status(slot) == ACPI_STA_ALL) {
1221 		/* configure all functions */
1222 		retval = enable_device(slot);
1223 		if (retval)
1224 			power_off_slot(slot);
1225 	} else {
1226 		dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
1227 		power_off_slot(slot);
1228 	}
1229 
1230  err_exit:
1231 	mutex_unlock(&slot->crit_sect);
1232 	return retval;
1233 }
1234 
1235 /**
1236  * acpiphp_disable_slot - power off slot
1237  * @slot: ACPI PHP slot
1238  */
1239 int acpiphp_disable_slot(struct acpiphp_slot *slot)
1240 {
1241 	int retval = 0;
1242 
1243 	mutex_lock(&slot->crit_sect);
1244 
1245 	/* unconfigure all functions */
1246 	retval = disable_device(slot);
1247 	if (retval)
1248 		goto err_exit;
1249 
1250 	/* power off all functions */
1251 	retval = power_off_slot(slot);
1252 	if (retval)
1253 		goto err_exit;
1254 
1255  err_exit:
1256 	mutex_unlock(&slot->crit_sect);
1257 	return retval;
1258 }
1259 
1260 
1261 /*
1262  * slot enabled:  1
1263  * slot disabled: 0
1264  */
1265 u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1266 {
1267 	return (slot->flags & SLOT_POWEREDON);
1268 }
1269 
1270 
1271 /*
1272  * latch   open:  1
1273  * latch closed:  0
1274  */
1275 u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1276 {
1277 	unsigned int sta;
1278 
1279 	sta = get_slot_status(slot);
1280 
1281 	return (sta & ACPI_STA_DEVICE_UI) ? 0 : 1;
1282 }
1283 
1284 
1285 /*
1286  * adapter presence : 1
1287  *          absence : 0
1288  */
1289 u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1290 {
1291 	unsigned int sta;
1292 
1293 	sta = get_slot_status(slot);
1294 
1295 	return (sta == 0) ? 0 : 1;
1296 }
1297