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